From e5c1605f48fda574c844fed4d1e6a6b299dea2d4 Mon Sep 17 00:00:00 2001 From: Raph Levien Date: Wed, 30 Oct 2024 21:38:57 -0700 Subject: [PATCH] Fix modern WGSL syntax (#84) The webgpu examples have ancient WGSL annotation syntax. This patch cleans that up, and also renames the fragment shader so the name doesn't collide. Note that it's presenting a black screen, I didn't dig into why we're not seeing correct rendering. But the motivation for this is to capture performance traces in Xcode, so that's good enough. --- webgpu-examples/boids/boids.ts | 2 +- webgpu-examples/boids/compute.wgsl | 32 ++++++++++++++---------------- webgpu-examples/boids/shader.wgsl | 14 ++++++------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/webgpu-examples/boids/boids.ts b/webgpu-examples/boids/boids.ts index eda07bf..4f90888 100644 --- a/webgpu-examples/boids/boids.ts +++ b/webgpu-examples/boids/boids.ts @@ -164,7 +164,7 @@ class Boids { }, fragment: { module: drawShader, - entryPoint: "main", + entryPoint: "frag_main", targets: [ { format: "rgba8unorm-srgb", diff --git a/webgpu-examples/boids/compute.wgsl b/webgpu-examples/boids/compute.wgsl index a3a86a6..5cefedf 100644 --- a/webgpu-examples/boids/compute.wgsl +++ b/webgpu-examples/boids/compute.wgsl @@ -1,31 +1,29 @@ struct Particle { - pos : vec2; - vel : vec2; + pos : vec2, + vel : vec2, }; -[[block]] struct SimParams { - deltaT : f32; - rule1Distance : f32; - rule2Distance : f32; - rule3Distance : f32; - rule1Scale : f32; - rule2Scale : f32; - rule3Scale : f32; + deltaT : f32, + rule1Distance : f32, + rule2Distance : f32, + rule3Distance : f32, + rule1Scale : f32, + rule2Scale : f32, + rule3Scale : f32, }; -[[block]] struct Particles { - particles : [[stride(16)]] array; + particles : array, }; -[[group(0), binding(0)]] var params : SimParams; -[[group(0), binding(1)]] var particlesSrc : Particles; -[[group(0), binding(2)]] var particlesDst : Particles; +@group(0) @binding(0) var params : SimParams; +@group(0) @binding(1) var particlesSrc : Particles; +@group(0) @binding(2) var particlesDst : Particles; // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp -[[stage(compute), workgroup_size(64)]] -fn main([[builtin(global_invocation_id)]] global_invocation_id: vec3) { +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) global_invocation_id: vec3) { let total = arrayLength(&particlesSrc.particles); let index = global_invocation_id.x; if (index >= total) { diff --git a/webgpu-examples/boids/shader.wgsl b/webgpu-examples/boids/shader.wgsl index e2bfe4a..6afc516 100644 --- a/webgpu-examples/boids/shader.wgsl +++ b/webgpu-examples/boids/shader.wgsl @@ -1,9 +1,9 @@ -[[stage(vertex)]] +@vertex fn main( - [[location(0)]] particle_pos: vec2, - [[location(1)]] particle_vel: vec2, - [[location(2)]] position: vec2, -) -> [[builtin(position)]] vec4 { + @location(0) particle_pos: vec2, + @location(1) particle_vel: vec2, + @location(2) position: vec2, +) -> @builtin(position) vec4 { let angle = -atan2(particle_vel.x, particle_vel.y); let pos = vec2( position.x * cos(angle) - position.y * sin(angle), @@ -12,7 +12,7 @@ fn main( return vec4(pos + particle_pos, 0.0, 1.0); } -[[stage(fragment)]] -fn main() -> [[location(0)]] vec4 { +@fragment +fn frag_main() -> @location(0) vec4 { return vec4(1.0, 1.0, 1.0, 1.0); } \ No newline at end of file