我正在使用 wgpu-rs 并在 WGSL 中编写着色器。为了测试示例着色器,我从这里复制了一些示例代码:https ://www.w3.org/TR/WGSL/#var-and-let 。
这是我的简单着色器:
// 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);
}
但是,当我尝试编译它时,出现以下错误:
[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
该错误是由 line 引起的i = i + step;
,但是如前所述,这段代码是从 W3 文档中复制的,为什么它不能编译?