我正在使用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这个错误是由行i = i + step;引起的,但是正如前面提到的,这段代码是从W3文档复制的,为什么它不能编译呢?
发布于 2021-08-19 04:05:08
与使用相同名称声明的变量相比,wgpu-rs着色器验证似乎更倾向于built-in step() function。根据W3 WGSL文档,这个should resolve to the variable是在一个更接近的范围内。
将step变量重命名为其他名称应该可以解决直接的问题。
已经创建了an issue来跟踪这一点,但我将其添加为另一个示例。
https://stackoverflow.com/questions/68833291
复制相似问题