From 9e0debd91ef009de485220b3f0f2a70fa732833b Mon Sep 17 00:00:00 2001 From: James Price Date: Tue, 13 Apr 2021 14:52:44 +0000 Subject: [PATCH] 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 Auto-Submit: James Price Reviewed-by: Ben Clayton --- examples/Animometer.cpp | 29 ++++++++++++------------ examples/CHelloTriangle.cpp | 17 ++++++-------- examples/ComputeBoids.cpp | 29 +++++++++++------------- examples/CppHelloTriangle.cpp | 16 +++++--------- examples/CubeReflection.cpp | 38 ++++++++++++++------------------ examples/ManualSwapChainTest.cpp | 16 +++++--------- 6 files changed, 64 insertions(+), 81 deletions(-) diff --git a/examples/Animometer.cpp b/examples/Animometer.cpp index 80cc96ca2a..51a4924f73 100644 --- a/examples/Animometer.cpp +++ b/examples/Animometer.cpp @@ -66,11 +66,12 @@ void init() { }; [[set(0), binding(0)]] var c : Constants; - [[location(0)]] var v_color : vec4; - [[builtin(vertex_idx)]] var VertexIndex : u32; - [[builtin(position)]] var Position : vec4; + struct VertexOut { + [[location(0)]] v_color : vec4; + [[builtin(position)]] Position : vec4; + }; - [[stage(vertex)]] fn main() { + [[stage(vertex)]] fn main([[builtin(vertex_idx)]] VertexIndex : u32) -> VertexOut { var positions : array, 3> = array, 3>( vec4( 0.0, 0.1, 0.0, 1.0), vec4(-0.1, -0.1, 0.0, 1.0), @@ -97,22 +98,22 @@ void init() { var xpos : f32 = position.x * c.scale; var ypos : f32 = position.y * c.scale; - const angle : f32 = 3.14159 * 2.0 * fade; - const xrot : f32 = xpos * cos(angle) - ypos * sin(angle); - const yrot : f32 = xpos * sin(angle) + ypos * cos(angle); + let angle : f32 = 3.14159 * 2.0 * fade; + let xrot : f32 = xpos * cos(angle) - ypos * sin(angle); + let yrot : f32 = xpos * sin(angle) + ypos * cos(angle); xpos = xrot + c.offsetX; ypos = yrot + c.offsetY; - v_color = vec4(fade, 1.0 - fade, 0.0, 1.0) + color; - Position = vec4(xpos, ypos, 0.0, 1.0); + var output : VertexOut; + output.v_color = vec4(fade, 1.0 - fade, 0.0, 1.0) + color; + output.Position = vec4(xpos, ypos, 0.0, 1.0); + return output; })"); wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( - [[location(0)]] var FragColor : vec4; - [[location(0)]] var v_color : vec4; - - [[stage(fragment)]] fn main() { - FragColor = v_color; + [[stage(fragment)]] fn main([[location(0)]] v_color : vec4) + -> [[location(0)]] vec4 { + return v_color; })"); wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( diff --git a/examples/CHelloTriangle.cpp b/examples/CHelloTriangle.cpp index a184fb3656..737da41082 100644 --- a/examples/CHelloTriangle.cpp +++ b/examples/CHelloTriangle.cpp @@ -37,24 +37,21 @@ void init() { wgpuSwapChainConfigure(swapchain, swapChainFormat, WGPUTextureUsage_RenderAttachment, 640, 480); const char* vs = - "[[builtin(vertex_index)]] var VertexIndex : u32;\n" - "[[builtin(position)]] var Position : vec4;\n" - "const pos : array, 3> = array, 3>(\n" + "let pos : array, 3> = array, 3>(\n" " vec2( 0.0, 0.5),\n" " vec2(-0.5, -0.5),\n" " vec2( 0.5, -0.5)\n" ");\n" - "[[stage(vertex)]] fn main() {\n" - " Position = vec4(pos[VertexIndex], 0.0, 1.0);\n" - " return;\n" + "[[stage(vertex)]] fn main(\n" + " [[builtin(vertex_index)]] VertexIndex : u32\n" + ") -> [[builtin(position)]] vec4 {\n" + " return vec4(pos[VertexIndex], 0.0, 1.0);\n" "}\n"; WGPUShaderModule vsModule = utils::CreateShaderModule(device, vs).Release(); const char* fs = - "[[location(0)]] var fragColor : vec4;\n" - "[[stage(fragment)]] fn main() {\n" - " fragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" - " return;\n" + "[[stage(fragment)]] fn main() -> [[location(0)]] vec4 {\n" + " return vec4(1.0, 0.0, 0.0, 1.0);\n" "}\n"; WGPUShaderModule fsModule = utils::CreateShaderModule(device, fs).Release(); diff --git a/examples/ComputeBoids.cpp b/examples/ComputeBoids.cpp index 35314f2071..29bd9c5e95 100644 --- a/examples/ComputeBoids.cpp +++ b/examples/ComputeBoids.cpp @@ -96,28 +96,26 @@ void initBuffers() { void initRender() { wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"( - [[location(0)]] var a_particlePos : vec2; - [[location(1)]] var a_particleVel : vec2; - [[location(2)]] var a_pos : vec2; - [[builtin(position)]] var Position : vec4; + struct VertexIn { + [[location(0)]] a_particlePos : vec2; + [[location(1)]] a_particleVel : vec2; + [[location(2)]] a_pos : vec2; + }; [[stage(vertex)]] - fn main() { - var angle : f32 = -atan2(a_particleVel.x, a_particleVel.y); + fn main(input : VertexIn) -> [[builtin(position)]] vec4 { + var angle : f32 = -atan2(input.a_particleVel.x, input.a_particleVel.y); var pos : vec2 = vec2( - (a_pos.x * cos(angle)) - (a_pos.y * sin(angle)), - (a_pos.x * sin(angle)) + (a_pos.y * cos(angle))); - Position = vec4(pos + a_particlePos, 0.0, 1.0); - return; + (input.a_pos.x * cos(angle)) - (input.a_pos.y * sin(angle)), + (input.a_pos.x * sin(angle)) + (input.a_pos.y * cos(angle))); + return vec4(pos + input.a_particlePos, 0.0, 1.0); } )"); wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( - [[location(0)]] var FragColor : vec4; [[stage(fragment)]] - fn main() { - FragColor = vec4(1.0, 1.0, 1.0, 1.0); - return; + fn main() -> [[location(0)]] vec4 { + return vec4(1.0, 1.0, 1.0, 1.0); } )"); @@ -170,11 +168,10 @@ void initSim() { [[binding(0), group(0)]] var params : SimParams; [[binding(1), group(0)]] var particlesA : [[access(read)]] Particles; [[binding(2), group(0)]] var particlesB : [[access(read_write)]] Particles; - [[builtin(global_invocation_id)]] var GlobalInvocationID : vec3; // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp [[stage(compute)]] - fn main() { + fn main([[builtin(global_invocation_id)]] GlobalInvocationID : vec3) { var index : u32 = GlobalInvocationID.x; if (index >= params.particleCount) { return; diff --git a/examples/CppHelloTriangle.cpp b/examples/CppHelloTriangle.cpp index a0c2cbef50..ad57cfe0c1 100644 --- a/examples/CppHelloTriangle.cpp +++ b/examples/CppHelloTriangle.cpp @@ -96,22 +96,18 @@ void init() { initTextures(); wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"( - [[builtin(position)]] var Position : vec4; - [[location(0)]] var pos : vec4; - [[stage(vertex)]] fn main() { - Position = pos; - return; + [[stage(vertex)]] fn main([[location(0)]] pos : vec4) + -> [[builtin(position)]] vec4 { + return pos; })"); wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( - [[builtin(frag_coord)]] var FragCoord : vec4; [[group(0), binding(0)]] var mySampler: sampler; [[group(0), binding(1)]] var myTexture : texture_2d; - [[location(0)]] var FragColor : vec4; - [[stage(fragment)]] fn main() { - FragColor = textureSample(myTexture, mySampler, FragCoord.xy / vec2(640.0, 480.0)); - return; + [[stage(fragment)]] fn main([[builtin(frag_coord)]] FragCoord : vec4) + -> [[location(0)]] vec4 { + return textureSample(myTexture, mySampler, FragCoord.xy / vec2(640.0, 480.0)); })"); auto bgl = utils::MakeBindGroupLayout( diff --git a/examples/CubeReflection.cpp b/examples/CubeReflection.cpp index f8d43aaf79..7358a3d8dc 100644 --- a/examples/CubeReflection.cpp +++ b/examples/CubeReflection.cpp @@ -113,34 +113,30 @@ void init() { }; [[group(0), binding(1)]] var model : Model; - [[location(0)]] var pos : vec3; - [[location(1)]] var col : vec3; + struct VertexOut { + [[location(2)]] f_col : vec3; + [[builtin(position)]] Position : vec4; + }; - [[location(2)]] var f_col : vec3; - [[builtin(position)]] var Position : vec4; - - [[stage(vertex)]] fn main() { - f_col = col; - Position = camera.proj * camera.view * model.matrix * vec4(pos, 1.0); - return; + [[stage(vertex)]] fn main( + [[location(0)]] pos : vec3, + [[location(1)]] col : vec3) -> VertexOut { + var output : VertexOut; + output.f_col = col; + output.Position = camera.proj * camera.view * model.matrix * vec4(pos, 1.0); + return output; })"); wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"( - [[location(0)]] var FragColor : vec4; - [[location(2)]] var f_col : vec3; - - [[stage(fragment)]] fn main() { - FragColor = vec4(f_col, 1.0); - return; + [[stage(fragment)]] fn main( + [[location(2)]] f_col : vec3) -> [[location(0)]] vec4 { + return vec4(f_col, 1.0); })"); wgpu::ShaderModule fsReflectionModule = utils::CreateShaderModule(device, R"( - [[location(0)]] var FragColor : vec4; - [[location(2)]] var f_col : vec3; - - [[stage(fragment)]] fn main() { - FragColor = vec4(mix(f_col, vec3(0.5, 0.5, 0.5), vec3(0.5, 0.5, 0.5)), 1.0); - return; + [[stage(fragment)]] fn main( + [[location(2)]] f_col : vec3) -> [[location(0)]] vec4 { + return vec4(mix(f_col, vec3(0.5, 0.5, 0.5), vec3(0.5, 0.5, 0.5)), 1.0); })"); wgpu::VertexAttribute attributes[2]; diff --git a/examples/ManualSwapChainTest.cpp b/examples/ManualSwapChainTest.cpp index 674d18502b..065b19bf2c 100644 --- a/examples/ManualSwapChainTest.cpp +++ b/examples/ManualSwapChainTest.cpp @@ -313,22 +313,18 @@ int main(int argc, const char* argv[]) { // The hacky pipeline to render a triangle. utils::ComboRenderPipelineDescriptor2 pipelineDesc; pipelineDesc.vertex.module = utils::CreateShaderModule(device, R"( - [[builtin(vertex_index)]] var VertexIndex : u32; - [[builtin(position)]] var Position : vec4; - const pos : array, 3> = array, 3>( + let pos : array, 3> = array, 3>( vec2( 0.0, 0.5), vec2(-0.5, -0.5), vec2( 0.5, -0.5) ); - [[stage(vertex)]] fn main() { - Position = vec4(pos[VertexIndex], 0.0, 1.0); - return; + [[stage(vertex)]] fn main([[builtin(vertex_index)]] VertexIndex : u32) + -> [[builtin(position)]] vec4 { + return vec4(pos[VertexIndex], 0.0, 1.0); })"); pipelineDesc.cFragment.module = utils::CreateShaderModule(device, R"( - [[location(0)]] var fragColor : vec4; - [[stage(fragment)]] fn main() { - fragColor = vec4(1.0, 0.0, 0.0, 1.0); - return; + [[stage(fragment)]] fn main() -> [[location(0)]] vec4 { + return vec4(1.0, 0.0, 0.0, 1.0); })"); // BGRA shouldn't be hardcoded. Consider having a map[format -> pipeline]. pipelineDesc.cTargets[0].format = wgpu::TextureFormat::BGRA8Unorm;