javascripttextureswebgpu

Why reading from a storage texture 2d array always returns the same value in webGPU?


What i'm trying to do is write to a storage texture in one render pass, and then read from this texture in a second pass, but value i get in the second pass is always the same, regardless of what i write in the first pass.

This is what setup code looks like:

const storageTexture = this._device.createTexture({
    dimension: '2d',
    size: [512, 512, 1],
    format: 'r32float',
    usage: GPUTextureUsage.STORAGE_BINDING
})

const textureView = storageTexture.createView({
    dimension: '2d-array',
    format: 'r32float',
})

const bindGroup = this.device.createBindGroup({
      layout: this.lightsBindGroupLayout,
      entries: [
                .....irrelevant stuff
                {
                    binding: 3,
                    resource: textureView,
                },
            ],
        })

const firstPass = commandEncoder.beginRenderPass(this._renderPassDescriptor)
......
firstPass.setBindGroup(2, bindGroup)
......
firstPass.end()

const secondPass = commandEncoder.beginRenderPass(this._renderPassDescriptor)
.....
secondPass.setBindGroup(3, bindGroup)
.....
secondPass.end()

Fragment shader code for first pass:

@fragment
fn fragment_main(in: VertexOutput) -> @location(0) float4 {

    textureStore(texture, vec2u(0, 0), 0, vec4f(1, 1, 1, 1));

    return in.position;
} 

Fragment shader code for second pass:

@fragment
fn fragment_main(in: VertexOutput) -> @location(0) float4 {

    let a = textureLoad(texture, vec2u(0, 0), 0);

    return vec4(a.r, 0, 0, 0);
} 

What i expected to get in the second pass is a.r == 1, therefore all fragments of the resulting image should be red, but what i actually get is a.r == 0, and all fragments are black.

I read about default value for r32float that is returned in case there is a reading error, and it's also mentioned that if there's a writing error, the textureStore function is not called. Right now, i can`t even understand which of this cases i have, but i can't see how could there be read/write error with a predefined location.

Any help in diagnosing what is wrong would be much appreciated.


Solution

  • The issue in my case was that i returned vertex positions as vec4(position, 0.0). I should have returned vec4(position, 1.0) instead. As per specs, a dynamic error occurs if w is 0. This error could not be captured though, and causes everything to fail silently.