Wgpu: correctly written WGSL shader parsing error

1.4k Views Asked by At

I am using wgpu-rs and writing shaders in WGSL. To test an example shader, I copied some sample code from here: https://www.w3.org/TR/WGSL/#var-and-let.

Here is my simple shader:

// Vertex shader

struct VertexInput {
    [[location(0)]] pos: vec3<f32>;
};

struct VertexOutput {
    [[builtin(position)]] pos: vec4<f32>;
};

[[stage(vertex)]]
fn main(vertex_input: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.pos = vec4<f32>(vertex_input.pos, 1.0);

    var a: i32 = 2;
    var i: i32 = 0;
    loop {
        if (i >= 4) { break; }
    
        let step: i32 = 1;
    
        i = i + step;
        if (i % 2 == 0) { continue; }
    
        a = a * 2;
    }

    return out;
}

// Fragment shader

[[stage(fragment)]]
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
    return vec4<f32>(1.0, 1.0, 1.0, 1.0);
}

However, when I try to compile it, I get the following error:

[2021-08-18T16:13:33Z ERROR wgpu_core::device] Failed to parse WGSL code for Some("Shader: simple shader"): expected '(', found ';'
[2021-08-18T16:13:33Z ERROR wgpu::backend::direct] wgpu error: Validation Error

    Caused by:
        In Device::create_shader_module
          note: label = `Shader: simple shader`
        Failed to parse WGSL

The error is caused by the line i = i + step;, however as mentioned before this snippet of code was copied from the W3 documentation so why doesn't it compile?

1

There are 1 best solutions below

0
On BEST ANSWER

It appears the wgpu-rs shader validation is favoring the built-in step() function over a variable declared with the same name. Going by the W3 WGSL docs, this should resolve to the variable since it is in a closer scope.

Renaming the step variable to something else should fix the immediate problem.

There is already an issue created to track this, but I've added this as another example.