Search code examples
wgpu-rswgsl

WGSL throws parsing error on seemingly correct code


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?


Solution

  • 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.