0

I have a storage texture which I (try to) write to in a compute shader. Then in the fragment shader I am trying to read from that storage texture using textureLoad, but I am getting this error and I have no clue what I'm doing wrong:

Caused by:
    In Device::create_shader_module
      note: label = `shader.wgsl`
    
Shader 'shader.wgsl' parsing error: wrong number of arguments: expected 3, found 3
   ┌─ wgsl:25:17
   │
25 │     let color = textureLoad(input, coords.xy, 1u);
   │                 ^^^^^^^^^^^ wrong number of arguments


    wrong number of arguments: expected 3, found 3

This is the compute shader:

@group(0) @binding(0)
var input: texture_storage_2d<rgba8unorm, read_write>;

struct VertexInput {
    @location(0) position: vec3<f32>,
    @location(1) color: vec3<f32>,
};

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) color: vec3<f32>,
}

@vertex
fn vs_main(model: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.color = model.color;
    out.clip_position = vec4<f32>(model.position, 1.0);
    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let coordinates = vec2<u32>(in.clip_position.xy);
    let color = textureLoad(input, coordinates, 0u);
    return color;
}

This is the vertex/fragment shader code:

@group(0) @binding(0)
var input: texture_storage_2d<rgba8unorm, read_write>;

struct VertexInput {
    @location(0) position: vec3<f32>,
    @location(1) color: vec3<f32>,
};

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) color: vec3<f32>,
}

@vertex
fn vs_main(model: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.color = model.color;
    out.clip_position = vec4<f32>(model.position, 1.0);
    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let coordinates = vec2<u32>(in.clip_position.xy);
    let color = textureLoad(input, coordinates, 0u);
    return color;
}

I tried reading through the documentation and it doesn't seem like I'm doing something wrong which breaks the shader like this.

I basically created a texture and passed the texture view into both the bind group for the compute and render pipeline. Is it even possible to use the same texture for this or do I need to copy it to another texture after the compute shader?

LordRobin
  • 76
  • 5

1 Answers1

1

There is no overload of textureLoad accepting an argument of type texture_storage_2d (as first parameter) in the official WGSL Spec.

Thats because baseline wgpu does not support read and write storage textures. However as stated here it is possible to

read from a texture by binding it as a normal texture (not a storage texture) and then using textureLoad on it

There has also been this similar question before.

frankenapps
  • 5,800
  • 6
  • 28
  • 69
  • Yeah, I figured that could be the problem. So I create a normal texture_2d, add it as a bindgroup to both the compute and render pipeline and should then be able to write to it in the compute shader and read from in the fragment shader. Did I understand you correctly? And what are the use cases of storage textures then? – LordRobin Jul 06 '23 at 17:54
  • So I just tried it out and got it to actually run. I declared the usage of the texture in the texturedescriptor as `STORAGA_TEXTURE | TEXTURE_BINDING` and then defined it as a storage texture in the compute bindgroup layout and as a texture in the render bindgroup layout. Does that make sense? Because it's just giving me a black window right now. – LordRobin Jul 06 '23 at 18:43
  • 1
    I think you did not fully understand the answer correctly. Your second comment sounds to me like you did try to write to a normal texture in the compute shader. However you can only write to `Storage Texture` and `Storaga Buffer` in compute stages. See here for more information https://stackoverflow.com/questions/71599251/wgpu-compute-write-direct-to-surface-texture-view – frankenapps Jul 07 '23 at 04:36
  • As I said I tried to create the texture as storage texture **and** texture binding and specified it as storage texture or normal texture in the comptue / render bind group layout respectively. I created a [gist](https://gist.github.com/LordRobin1/c9c46e9cadb8354b6c029be17aecbbc9) so you can take a look at what I am doing if you have the time to do so. – LordRobin Jul 07 '23 at 08:30
  • 1
    A [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) would help much more to find out what the issue is now, instead of a gist missing code required to actually run it. – frankenapps Jul 07 '23 at 15:16
  • I updated the gist to contain everything that is needed to demonstrate the problem. I left it as a gist since it is still a lot of code. – LordRobin Jul 08 '23 at 09:25
  • 1
    Looks much better now, I'll see when I can take a look, since I'm curious what the problem is now... – frankenapps Jul 08 '23 at 09:46