Update examples for recent WGSL changes

* Use new entry point IO syntax
* Use `let` instead of `const`
* Remove `-> void` from function headers

Bug: dawn:755
Change-Id: I39b5687a342ea2298c3d2e85517c9b8d9017727e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/47500
Commit-Queue: James Price <jrprice@google.com>
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
James Price 2021-04-13 14:52:44 +00:00 committed by Commit Bot service account
parent f3fe648675
commit 9e0debd91e
6 changed files with 64 additions and 81 deletions

View File

@ -66,11 +66,12 @@ void init() {
}; };
[[set(0), binding(0)]] var<uniform> c : Constants; [[set(0), binding(0)]] var<uniform> c : Constants;
[[location(0)]] var<out> v_color : vec4<f32>; struct VertexOut {
[[builtin(vertex_idx)]] var<in> VertexIndex : u32; [[location(0)]] v_color : vec4<f32>;
[[builtin(position)]] var<out> Position : vec4<f32>; [[builtin(position)]] Position : vec4<f32>;
};
[[stage(vertex)]] fn main() { [[stage(vertex)]] fn main([[builtin(vertex_idx)]] VertexIndex : u32) -> VertexOut {
var positions : array<vec4<f32>, 3> = array<vec4<f32>, 3>( var positions : array<vec4<f32>, 3> = array<vec4<f32>, 3>(
vec4<f32>( 0.0, 0.1, 0.0, 1.0), vec4<f32>( 0.0, 0.1, 0.0, 1.0),
vec4<f32>(-0.1, -0.1, 0.0, 1.0), vec4<f32>(-0.1, -0.1, 0.0, 1.0),
@ -97,22 +98,22 @@ void init() {
var xpos : f32 = position.x * c.scale; var xpos : f32 = position.x * c.scale;
var ypos : f32 = position.y * c.scale; var ypos : f32 = position.y * c.scale;
const angle : f32 = 3.14159 * 2.0 * fade; let angle : f32 = 3.14159 * 2.0 * fade;
const xrot : f32 = xpos * cos(angle) - ypos * sin(angle); let xrot : f32 = xpos * cos(angle) - ypos * sin(angle);
const yrot : f32 = xpos * sin(angle) + ypos * cos(angle); let yrot : f32 = xpos * sin(angle) + ypos * cos(angle);
xpos = xrot + c.offsetX; xpos = xrot + c.offsetX;
ypos = yrot + c.offsetY; ypos = yrot + c.offsetY;
v_color = vec4<f32>(fade, 1.0 - fade, 0.0, 1.0) + color; var output : VertexOut;
Position = vec4<f32>(xpos, ypos, 0.0, 1.0); output.v_color = vec4<f32>(fade, 1.0 - fade, 0.0, 1.0) + color;
output.Position = vec4<f32>(xpos, ypos, 0.0, 1.0);
return output;
})"); })");
wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
[[location(0)]] var<out> FragColor : vec4<f32>; [[stage(fragment)]] fn main([[location(0)]] v_color : vec4<f32>)
[[location(0)]] var<in> v_color : vec4<f32>; -> [[location(0)]] vec4<f32> {
return v_color;
[[stage(fragment)]] fn main() {
FragColor = v_color;
})"); })");
wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(

View File

@ -37,24 +37,21 @@ void init() {
wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_RenderAttachment, 640, 480); wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_RenderAttachment, 640, 480);
const char* vs = const char* vs =
"[[builtin(vertex_index)]] var<in> VertexIndex : u32;\n" "let pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(\n"
"[[builtin(position)]] var<out> Position : vec4<f32>;\n"
"const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(\n"
" vec2<f32>( 0.0, 0.5),\n" " vec2<f32>( 0.0, 0.5),\n"
" vec2<f32>(-0.5, -0.5),\n" " vec2<f32>(-0.5, -0.5),\n"
" vec2<f32>( 0.5, -0.5)\n" " vec2<f32>( 0.5, -0.5)\n"
");\n" ");\n"
"[[stage(vertex)]] fn main() {\n" "[[stage(vertex)]] fn main(\n"
" Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);\n" " [[builtin(vertex_index)]] VertexIndex : u32\n"
" return;\n" ") -> [[builtin(position)]] vec4<f32> {\n"
" return vec4<f32>(pos[VertexIndex], 0.0, 1.0);\n"
"}\n"; "}\n";
WGPUShaderModule vsModule = utils::CreateShaderModule(device, vs).Release(); WGPUShaderModule vsModule = utils::CreateShaderModule(device, vs).Release();
const char* fs = const char* fs =
"[[location(0)]] var<out> fragColor : vec4<f32>;\n" "[[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> {\n"
"[[stage(fragment)]] fn main() {\n" " return vec4<f32>(1.0, 0.0, 0.0, 1.0);\n"
" fragColor = vec4<f32>(1.0, 0.0, 0.0, 1.0);\n"
" return;\n"
"}\n"; "}\n";
WGPUShaderModule fsModule = utils::CreateShaderModule(device, fs).Release(); WGPUShaderModule fsModule = utils::CreateShaderModule(device, fs).Release();

View File

@ -96,28 +96,26 @@ void initBuffers() {
void initRender() { void initRender() {
wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"( wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
[[location(0)]] var<in> a_particlePos : vec2<f32>; struct VertexIn {
[[location(1)]] var<in> a_particleVel : vec2<f32>; [[location(0)]] a_particlePos : vec2<f32>;
[[location(2)]] var<in> a_pos : vec2<f32>; [[location(1)]] a_particleVel : vec2<f32>;
[[builtin(position)]] var<out> Position : vec4<f32>; [[location(2)]] a_pos : vec2<f32>;
};
[[stage(vertex)]] [[stage(vertex)]]
fn main() { fn main(input : VertexIn) -> [[builtin(position)]] vec4<f32> {
var angle : f32 = -atan2(a_particleVel.x, a_particleVel.y); var angle : f32 = -atan2(input.a_particleVel.x, input.a_particleVel.y);
var pos : vec2<f32> = vec2<f32>( var pos : vec2<f32> = vec2<f32>(
(a_pos.x * cos(angle)) - (a_pos.y * sin(angle)), (input.a_pos.x * cos(angle)) - (input.a_pos.y * sin(angle)),
(a_pos.x * sin(angle)) + (a_pos.y * cos(angle))); (input.a_pos.x * sin(angle)) + (input.a_pos.y * cos(angle)));
Position = vec4<f32>(pos + a_particlePos, 0.0, 1.0); return vec4<f32>(pos + input.a_particlePos, 0.0, 1.0);
return;
} }
)"); )");
wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
[[location(0)]] var<out> FragColor : vec4<f32>;
[[stage(fragment)]] [[stage(fragment)]]
fn main() { fn main() -> [[location(0)]] vec4<f32> {
FragColor = vec4<f32>(1.0, 1.0, 1.0, 1.0); return vec4<f32>(1.0, 1.0, 1.0, 1.0);
return;
} }
)"); )");
@ -170,11 +168,10 @@ void initSim() {
[[binding(0), group(0)]] var<uniform> params : SimParams; [[binding(0), group(0)]] var<uniform> params : SimParams;
[[binding(1), group(0)]] var<storage> particlesA : [[access(read)]] Particles; [[binding(1), group(0)]] var<storage> particlesA : [[access(read)]] Particles;
[[binding(2), group(0)]] var<storage> particlesB : [[access(read_write)]] Particles; [[binding(2), group(0)]] var<storage> particlesB : [[access(read_write)]] Particles;
[[builtin(global_invocation_id)]] var<in> GlobalInvocationID : vec3<u32>;
// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
[[stage(compute)]] [[stage(compute)]]
fn main() { fn main([[builtin(global_invocation_id)]] GlobalInvocationID : vec3<u32>) {
var index : u32 = GlobalInvocationID.x; var index : u32 = GlobalInvocationID.x;
if (index >= params.particleCount) { if (index >= params.particleCount) {
return; return;

View File

@ -96,22 +96,18 @@ void init() {
initTextures(); initTextures();
wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"( wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
[[builtin(position)]] var<out> Position : vec4<f32>; [[stage(vertex)]] fn main([[location(0)]] pos : vec4<f32>)
[[location(0)]] var<in> pos : vec4<f32>; -> [[builtin(position)]] vec4<f32> {
[[stage(vertex)]] fn main() { return pos;
Position = pos;
return;
})"); })");
wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
[[builtin(frag_coord)]] var<in> FragCoord : vec4<f32>;
[[group(0), binding(0)]] var mySampler: sampler; [[group(0), binding(0)]] var mySampler: sampler;
[[group(0), binding(1)]] var myTexture : texture_2d<f32>; [[group(0), binding(1)]] var myTexture : texture_2d<f32>;
[[location(0)]] var<out> FragColor : vec4<f32>; [[stage(fragment)]] fn main([[builtin(frag_coord)]] FragCoord : vec4<f32>)
[[stage(fragment)]] fn main() { -> [[location(0)]] vec4<f32> {
FragColor = textureSample(myTexture, mySampler, FragCoord.xy / vec2<f32>(640.0, 480.0)); return textureSample(myTexture, mySampler, FragCoord.xy / vec2<f32>(640.0, 480.0));
return;
})"); })");
auto bgl = utils::MakeBindGroupLayout( auto bgl = utils::MakeBindGroupLayout(

View File

@ -113,34 +113,30 @@ void init() {
}; };
[[group(0), binding(1)]] var<uniform> model : Model; [[group(0), binding(1)]] var<uniform> model : Model;
[[location(0)]] var<in> pos : vec3<f32>; struct VertexOut {
[[location(1)]] var<in> col : vec3<f32>; [[location(2)]] f_col : vec3<f32>;
[[builtin(position)]] Position : vec4<f32>;
};
[[location(2)]] var<out> f_col : vec3<f32>; [[stage(vertex)]] fn main(
[[builtin(position)]] var<out> Position : vec4<f32>; [[location(0)]] pos : vec3<f32>,
[[location(1)]] col : vec3<f32>) -> VertexOut {
[[stage(vertex)]] fn main() { var output : VertexOut;
f_col = col; output.f_col = col;
Position = camera.proj * camera.view * model.matrix * vec4<f32>(pos, 1.0); output.Position = camera.proj * camera.view * model.matrix * vec4<f32>(pos, 1.0);
return; return output;
})"); })");
wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
[[location(0)]] var<out> FragColor : vec4<f32>; [[stage(fragment)]] fn main(
[[location(2)]] var<in> f_col : vec3<f32>; [[location(2)]] f_col : vec3<f32>) -> [[location(0)]] vec4<f32> {
return vec4<f32>(f_col, 1.0);
[[stage(fragment)]] fn main() {
FragColor = vec4<f32>(f_col, 1.0);
return;
})"); })");
wgpu::ShaderModule fsReflectionModule = utils::CreateShaderModule(device, R"( wgpu::ShaderModule fsReflectionModule = utils::CreateShaderModule(device, R"(
[[location(0)]] var<out> FragColor : vec4<f32>; [[stage(fragment)]] fn main(
[[location(2)]] var<in> f_col : vec3<f32>; [[location(2)]] f_col : vec3<f32>) -> [[location(0)]] vec4<f32> {
return vec4<f32>(mix(f_col, vec3<f32>(0.5, 0.5, 0.5), vec3<f32>(0.5, 0.5, 0.5)), 1.0);
[[stage(fragment)]] fn main() {
FragColor = vec4<f32>(mix(f_col, vec3<f32>(0.5, 0.5, 0.5), vec3<f32>(0.5, 0.5, 0.5)), 1.0);
return;
})"); })");
wgpu::VertexAttribute attributes[2]; wgpu::VertexAttribute attributes[2];

View File

@ -313,22 +313,18 @@ int main(int argc, const char* argv[]) {
// The hacky pipeline to render a triangle. // The hacky pipeline to render a triangle.
utils::ComboRenderPipelineDescriptor2 pipelineDesc; utils::ComboRenderPipelineDescriptor2 pipelineDesc;
pipelineDesc.vertex.module = utils::CreateShaderModule(device, R"( pipelineDesc.vertex.module = utils::CreateShaderModule(device, R"(
[[builtin(vertex_index)]] var<in> VertexIndex : u32; let pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
[[builtin(position)]] var<out> Position : vec4<f32>;
const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
vec2<f32>( 0.0, 0.5), vec2<f32>( 0.0, 0.5),
vec2<f32>(-0.5, -0.5), vec2<f32>(-0.5, -0.5),
vec2<f32>( 0.5, -0.5) vec2<f32>( 0.5, -0.5)
); );
[[stage(vertex)]] fn main() { [[stage(vertex)]] fn main([[builtin(vertex_index)]] VertexIndex : u32)
Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0); -> [[builtin(position)]] vec4<f32> {
return; return vec4<f32>(pos[VertexIndex], 0.0, 1.0);
})"); })");
pipelineDesc.cFragment.module = utils::CreateShaderModule(device, R"( pipelineDesc.cFragment.module = utils::CreateShaderModule(device, R"(
[[location(0)]] var<out> fragColor : vec4<f32>; [[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> {
[[stage(fragment)]] fn main() { return vec4<f32>(1.0, 0.0, 0.0, 1.0);
fragColor = vec4<f32>(1.0, 0.0, 0.0, 1.0);
return;
})"); })");
// BGRA shouldn't be hardcoded. Consider having a map[format -> pipeline]. // BGRA shouldn't be hardcoded. Consider having a map[format -> pipeline].
pipelineDesc.cTargets[0].format = wgpu::TextureFormat::BGRA8Unorm; pipelineDesc.cTargets[0].format = wgpu::TextureFormat::BGRA8Unorm;