1

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?

can
  • 45
  • 6
  • Hmm, `step()` is a [built-in function](https://www.w3.org/TR/WGSL/#float-builtin-functions), what happens if you name it something different? – kmdreko Aug 18 '21 at 16:00
  • Not saying the code is wrong. The docs say that identifier shadowing is allowed as long as they're different scopes ([link](https://www.w3.org/TR/WGSL/#declaration-and-scope)). It could be a problem with wgpu-rs. – kmdreko Aug 18 '21 at 16:11
  • @kmdreko I changed the name of the variable and it works now. Thanks a lot! (I if you post your comment as an answer I will gladly accept it). – can Aug 18 '21 at 17:34

1 Answers1

0

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.

kmdreko
  • 42,554
  • 6
  • 57
  • 106