From 0cccf9ccee515ec9300d8c5077903cc08da2fe88 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 13 Mar 2021 23:25:25 -0500 Subject: [PATCH] Update to naga-14 --- Cargo.toml | 10 ++--- examples/boids/compute.wgsl | 6 +-- examples/boids/draw.wgsl | 30 +++++--------- examples/cube/shader.wgsl | 39 ++++++++---------- examples/hello-compute/shader.wgsl | 5 +-- examples/hello-triangle/shader.wgsl | 16 ++------ examples/hello-windows/main.rs | 4 +- examples/mipmap/blit.wgsl | 36 ++++++++--------- examples/mipmap/draw.wgsl | 31 ++++++-------- examples/msaa-line/shader.wgsl | 32 +++++++-------- examples/shadow/shader.wgsl | 53 ++++++++++-------------- examples/skybox/shader.wgsl | 63 ++++++++++++++++------------- src/backend/direct.rs | 16 +++----- 13 files changed, 147 insertions(+), 194 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 753764e43..3de4f3289 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,20 +26,20 @@ webgl = ["wgc"] [target.'cfg(not(target_arch = "wasm32"))'.dependencies.wgc] package = "wgpu-core" git = "https://github.com/gfx-rs/wgpu" -rev = "0fa52d3826ec8fe8f6177a558922085fe7d0e0cc" +rev = "5e15980bb53d2a5c24546dab640dcc72d211bd36" features = ["raw-window-handle", "cross"] [target.'cfg(target_arch = "wasm32")'.dependencies.wgc] package = "wgpu-core" git = "https://github.com/gfx-rs/wgpu" -rev = "0fa52d3826ec8fe8f6177a558922085fe7d0e0cc" +rev = "5e15980bb53d2a5c24546dab640dcc72d211bd36" features = ["raw-window-handle", "cross"] optional = true [dependencies.wgt] package = "wgpu-types" git = "https://github.com/gfx-rs/wgpu" -rev = "0fa52d3826ec8fe8f6177a558922085fe7d0e0cc" +rev = "5e15980bb53d2a5c24546dab640dcc72d211bd36" [dependencies] arrayvec = "0.5" @@ -68,13 +68,13 @@ wgpu-subscriber = "0.1" # used to test all the example shaders [dev-dependencies.naga] git = "https://github.com/gfx-rs/naga" -tag = "gfx-13" +tag = "gfx-14" features = ["wgsl-in"] # used to generate SPIR-V for the Web target [target.'cfg(target_arch = "wasm32")'.dependencies.naga] git = "https://github.com/gfx-rs/naga" -tag = "gfx-13" +tag = "gfx-14" features = ["wgsl-in", "spv-out"] [[example]] diff --git a/examples/boids/compute.wgsl b/examples/boids/compute.wgsl index 8c1c505a2..b141c023a 100644 --- a/examples/boids/compute.wgsl +++ b/examples/boids/compute.wgsl @@ -27,12 +27,10 @@ struct Particles { [[group(0), binding(1)]] var particlesSrc : [[access(read)]] Particles; [[group(0), binding(2)]] var particlesDst : [[access(read_write)]] Particles; -[[builtin(global_invocation_id)]] var gl_GlobalInvocationID : vec3; - // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp [[stage(compute), workgroup_size(64)]] -fn main() { - const index : u32 = gl_GlobalInvocationID.x; +fn main([[builtin(global_invocation_id)]] global_invocation_id: vec3) { + const index : u32 = global_invocation_id.x; if (index >= NUM_PARTICLES) { return; } diff --git a/examples/boids/draw.wgsl b/examples/boids/draw.wgsl index cab88bdb3..48e2386ea 100644 --- a/examples/boids/draw.wgsl +++ b/examples/boids/draw.wgsl @@ -1,26 +1,18 @@ -[[location(0)]] -var in_particle_pos: vec2; -[[location(1)]] -var in_particle_vel: vec2; -[[location(2)]] -var in_position: vec2; -[[builtin(position)]] -var out_position: vec4; - [[stage(vertex)]] -fn main() { - const angle: f32 = -atan2(in_particle_vel.x, in_particle_vel.y); +fn main( + [[location(0)]] particle_pos: vec2, + [[location(1)]] particle_vel: vec2, + [[location(2)]] position: vec2, +) -> [[builtin(position)]] vec4 { + const angle: f32 = -atan2(particle_vel.x, particle_vel.y); const pos: vec2 = vec2( - in_position.x * cos(angle) - in_position.y * sin(angle), - in_position.x * sin(angle) + in_position.y * cos(angle) + position.x * cos(angle) - position.y * sin(angle), + position.x * sin(angle) + position.y * cos(angle) ); - out_position = vec4(pos + in_particle_pos, 0.0, 1.0); + return vec4(pos + particle_pos, 0.0, 1.0); } -[[location(0)]] -var out_color: vec4; - [[stage(fragment)]] -fn main() { - out_color = vec4(1.0, 1.0, 1.0, 1.0); +fn main() -> [[location(0)]] vec4 { + return vec4(1.0, 1.0, 1.0, 1.0); } diff --git a/examples/cube/shader.wgsl b/examples/cube/shader.wgsl index cc9404240..059d3f88f 100644 --- a/examples/cube/shader.wgsl +++ b/examples/cube/shader.wgsl @@ -1,11 +1,7 @@ -[[location(0)]] -var in_position: vec4; -[[location(1)]] -var in_tex_coord_vs: vec2; -[[location(0)]] -var out_tex_coord: vec2; -[[builtin(position)]] -var out_position: vec4; +struct VertexOutput { + [[location(0)]] tex_coord: vec2; + [[builtin(position)]] position: vec4; +}; [[block]] struct Locals { @@ -15,30 +11,27 @@ struct Locals { var r_locals: Locals; [[stage(vertex)]] -fn vs_main() { - out_tex_coord = in_tex_coord_vs; - out_position = r_locals.transform * in_position; +fn vs_main( + [[location(0)]] position: vec4, + [[location(1)]] tex_coord: vec2, +) -> VertexOutput { + var out: VertexOutput; + out.tex_coord = tex_coord; + out.position = r_locals.transform * position; + return out; } -[[location(0)]] -var in_tex_coord_fs: vec2; -[[location(0)]] -var out_color: vec4; [[group(0), binding(1)]] var r_color: texture_2d; [[group(0), binding(2)]] var r_sampler: sampler; [[stage(fragment)]] -fn fs_main() { - var tex: vec4 = textureSample(r_color, r_sampler, in_tex_coord_fs); - out_color = tex; - //TODO: support `length` and `mix` functions - //var mag: f32 = length(in_tex_coord_fs-vec2(0.5, 0.5)); - //out_color = vec4(mix(tex.xyz, vec3(0.0, 0.0, 0.0), mag*mag), 1.0); +fn fs_main(in: VertexOutput) -> [[location(0)]] vec4 { + return textureSample(r_color, r_sampler, in.tex_coord); } [[stage(fragment)]] -fn fs_wire() { - out_color = vec4(0.0, 0.5, 0.0, 0.5); +fn fs_wire() -> [[location(0)]] vec4 { + return vec4(0.0, 0.5, 0.0, 0.5); } diff --git a/examples/hello-compute/shader.wgsl b/examples/hello-compute/shader.wgsl index 2433f3243..c6ddb7203 100644 --- a/examples/hello-compute/shader.wgsl +++ b/examples/hello-compute/shader.wgsl @@ -1,6 +1,3 @@ -[[builtin(global_invocation_id)]] -var global_id: vec3; - [[block]] struct PrimeIndices { data: [[stride(4)]] array; @@ -34,6 +31,6 @@ fn collatz_iterations(n_base: u32) -> u32{ } [[stage(compute), workgroup_size(1)]] -fn main() { +fn main([[builtin(global_invocation_id)]] global_id: vec3) { v_indices.data[global_id.x] = collatz_iterations(v_indices.data[global_id.x]); } diff --git a/examples/hello-triangle/shader.wgsl b/examples/hello-triangle/shader.wgsl index 71934415b..9594a07c7 100644 --- a/examples/hello-triangle/shader.wgsl +++ b/examples/hello-triangle/shader.wgsl @@ -1,19 +1,11 @@ -[[builtin(vertex_index)]] -var in_vertex_index: u32; -[[builtin(position)]] -var out_pos: vec4; - [[stage(vertex)]] -fn vs_main() { +fn vs_main([[builtin(vertex_index)]] in_vertex_index: u32) -> [[builtin(position)]] vec4 { var x: f32 = f32(i32(in_vertex_index) - 1); var y: f32 = f32(i32(in_vertex_index & 1) * 2 - 1); - out_pos = vec4(x, y, 0.0, 1.0); + return vec4(x, y, 0.0, 1.0); } -[[location(0)]] -var out_color: vec4; - [[stage(fragment)]] -fn fs_main() { - out_color = vec4(1.0, 0.0, 0.0, 1.0); +fn fs_main() -> [[location(0)]] vec4 { + return vec4(1.0, 0.0, 0.0, 1.0); } diff --git a/examples/hello-windows/main.rs b/examples/hello-windows/main.rs index 299846640..d1ac37323 100644 --- a/examples/hello-windows/main.rs +++ b/examples/hello-windows/main.rs @@ -32,7 +32,9 @@ impl ViewportDesc { let sc_desc = wgpu::SwapChainDescriptor { usage: wgpu::TextureUsage::RENDER_ATTACHMENT, - format: adapter.get_swap_chain_preferred_format(&self.surface).unwrap(), + format: adapter + .get_swap_chain_preferred_format(&self.surface) + .unwrap(), width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, diff --git a/examples/mipmap/blit.wgsl b/examples/mipmap/blit.wgsl index e5cb571f5..61bb6caa5 100644 --- a/examples/mipmap/blit.wgsl +++ b/examples/mipmap/blit.wgsl @@ -1,36 +1,32 @@ -[[builtin(vertex_index)]] -var in_vertex_index: u32; -[[builtin(position)]] -var out_position: vec4; -[[location(0)]] -var out_tex_coords_vs: vec2; +struct VertexOutput { + [[builtin(position)]] position: vec4; + [[location(0)]] tex_coords: vec2; +}; [[stage(vertex)]] -fn vs_main() { - var x: i32 = i32(in_vertex_index) / 2; - var y: i32 = i32(in_vertex_index) & 1; - out_tex_coords_vs = vec2( +fn vs_main([[builtin(vertex_index)]] vertex_index: u32) -> VertexOutput { + var out: VertexOutput; + var x: i32 = i32(vertex_index) / 2; + var y: i32 = i32(vertex_index) & 1; + const tc: vec2 = vec2( f32(x) * 2.0, f32(y) * 2.0 ); - out_position = vec4( - out_tex_coords_vs.x * 2.0 - 1.0, - 1.0 - out_tex_coords_vs.y * 2.0, + out.position = vec4( + tc.x * 2.0 - 1.0, + 1.0 - tc.y * 2.0, 0.0, 1.0 ); + out.tex_coords = tc; + return out; } -[[location(0)]] -var in_tex_coord_fs: vec2; -[[location(0)]] -var out_color_fs: vec4; - [[group(0), binding(0)]] var r_color: texture_2d; [[group(0), binding(1)]] var r_sampler: sampler; [[stage(fragment)]] -fn fs_main() { - out_color_fs = textureSample(r_color, r_sampler, in_tex_coord_fs); +fn fs_main(in: VertexOutput) -> [[location(0)]] vec4 { + return textureSample(r_color, r_sampler, in.tex_coords); } diff --git a/examples/mipmap/draw.wgsl b/examples/mipmap/draw.wgsl index 56afdd028..0b9cec5bd 100644 --- a/examples/mipmap/draw.wgsl +++ b/examples/mipmap/draw.wgsl @@ -1,9 +1,7 @@ -[[builtin(vertex_index)]] -var in_vertex_index: u32; -[[builtin(position)]] -var out_position: vec4; -[[location(0)]] -var out_tex_coords_vs: vec2; +struct VertexOutput { + [[builtin(position)]] position: vec4; + [[location(0)]] tex_coords: vec2; +}; [[block]] struct Locals { @@ -13,26 +11,23 @@ struct Locals { var r_data: Locals; [[stage(vertex)]] -fn vs_main() { +fn vs_main([[builtin(vertex_index)]] vertex_index: u32) -> VertexOutput { var pos: vec2 = vec2( - 100.0 * (1.0 - f32(in_vertex_index & 2u)), - 1000.0 * f32(in_vertex_index & 1u) + 100.0 * (1.0 - f32(vertex_index & 2u)), + 1000.0 * f32(vertex_index & 1u) ); - out_tex_coords_vs = 0.05 * pos + vec2(0.5, 0.5); - out_position = r_data.transform * vec4(pos, 0.0, 1.0); + var out: VertexOutput; + out.tex_coords = 0.05 * pos + vec2(0.5, 0.5); + out.position = r_data.transform * vec4(pos, 0.0, 1.0); + return out; } -[[location(0)]] -var in_tex_coord_fs: vec2; -[[location(0)]] -var out_color_fs: vec4; - [[group(0), binding(1)]] var r_color: texture_2d; [[group(0), binding(2)]] var r_sampler: sampler; [[stage(fragment)]] -fn fs_main() { - out_color_fs = textureSample(r_color, r_sampler, in_tex_coord_fs); +fn fs_main(in: VertexOutput) -> [[location(0)]] vec4 { + return textureSample(r_color, r_sampler, in.tex_coords); } diff --git a/examples/msaa-line/shader.wgsl b/examples/msaa-line/shader.wgsl index 606eab5a1..8dbce73b3 100644 --- a/examples/msaa-line/shader.wgsl +++ b/examples/msaa-line/shader.wgsl @@ -1,24 +1,20 @@ -[[location(0)]] -var in_position: vec2; -[[location(1)]] -var in_color_vs: vec4; -[[location(0)]] -var out_color_vs: vec4; -[[builtin(position)]] -var out_position: vec4; +struct VertexOutput { + [[location(0)]] color: vec4; + [[builtin(position)]] position: vec4; +}; [[stage(vertex)]] -fn vs_main() { - out_position = vec4(in_position, 0.0, 1.0); - out_color_vs = in_color_vs; +fn vs_main( + [[location(0)]] position: vec2, + [[location(1)]] color: vec4, +) -> VertexOutput { + var out: VertexOutput; + out.position = vec4(position, 0.0, 1.0); + out.color = color; + return out; } -[[location(0)]] -var in_color_fs: vec4; -[[location(0)]] -var out_color_fs: vec4; - [[stage(fragment)]] -fn fs_main() { - out_color_fs = in_color_fs; +fn fs_main(in: VertexOutput) -> [[location(0)]] vec4 { + return in.color; } diff --git a/examples/shadow/shader.wgsl b/examples/shadow/shader.wgsl index 6f6106b0f..2cdfadcef 100644 --- a/examples/shadow/shader.wgsl +++ b/examples/shadow/shader.wgsl @@ -1,8 +1,3 @@ -[[location(0)]] -var in_position: vec4; -[[builtin(position)]] -var out_position: vec4; - [[block]] struct Globals { view_proj: mat4x4; @@ -22,25 +17,28 @@ struct Entity { var u_entity: Entity; [[stage(vertex)]] -fn vs_bake() { - out_position = u_globals.view_proj * u_entity.world * vec4(in_position); +fn vs_bake([[location(0)]] position: vec4) -> [[builtin(position)]] vec4 { + return u_globals.view_proj * u_entity.world * vec4(position); } - -[[location(1)]] -var in_normal: vec4; - -[[location(0)]] -var out_normal_vs: vec3; -[[location(1)]] -var out_position_vs: vec4; +struct VertexOutput { + [[builtin(position)]] proj_position: vec4; + [[location(0)]] world_normal: vec3; + [[location(1)]] world_position: vec4; +}; [[stage(vertex)]] -fn vs_main() { +fn vs_main( + [[location(0)]] position: vec4, + [[location(1)]] normal: vec4, +) -> VertexOutput { const w: mat4x4 = u_entity.world; - out_normal_vs = mat3x3(w.x.xyz, w.y.xyz, w.z.xyz) * vec3(in_normal.xyz); - out_position_vs = u_entity.world * vec4(in_position); - out_position = u_globals.view_proj * out_position_vs; + const world_pos: vec4 = u_entity.world * vec4(position); + var out: VertexOutput; + out.world_normal = mat3x3(w.x.xyz, w.y.xyz, w.z.xyz) * vec3(normal.xyz); + out.world_position = world_pos; + out.proj_position = u_globals.view_proj * world_pos; + return out; } // fragment shader @@ -77,19 +75,12 @@ fn fetch_shadow(light_id: u32, homogeneous_coords: vec4) -> f32 { return textureSampleCompare(t_shadow, sampler_shadow, light_local, i32(light_id), homogeneous_coords.z * proj_correction); } -[[location(0)]] -var in_normal_fs: vec3; -[[location(1)]] -var in_position_fs: vec4; -[[location(0)]] -var out_color_fs: vec4; - const c_ambient: vec3 = vec3(0.05, 0.05, 0.05); const c_max_lights: u32 = 10u; [[stage(fragment)]] -fn fs_main() { - const normal: vec3 = normalize(in_normal_fs); +fn fs_main(in: VertexOutput) -> [[location(0)]] vec4 { + const normal: vec3 = normalize(in.world_normal); // accumulate color var color: vec3 = c_ambient; var i: u32 = 0u; @@ -99,9 +90,9 @@ fn fs_main() { } const light: Light = s_lights.data[i]; // project into the light space - const shadow: f32 = fetch_shadow(i, light.proj * in_position_fs); + const shadow: f32 = fetch_shadow(i, light.proj * in.world_position); // compute Lambertian diffuse term - const light_dir: vec3 = normalize(light.pos.xyz - in_position_fs.xyz); + const light_dir: vec3 = normalize(light.pos.xyz - in.world_position.xyz); const diffuse: f32 = max(0.0, dot(normal, light_dir)); // add light contribution color = color + shadow * diffuse * light.color.xyz; @@ -110,5 +101,5 @@ fn fs_main() { } } // multiply the light by material color - out_color_fs = vec4(color, 1.0) * u_entity.color; + return vec4(color, 1.0) * u_entity.color; } diff --git a/examples/skybox/shader.wgsl b/examples/skybox/shader.wgsl index 283e1a8c8..fe4af3aed 100644 --- a/examples/skybox/shader.wgsl +++ b/examples/skybox/shader.wgsl @@ -1,7 +1,7 @@ -[[builtin(position)]] -var out_position: vec4; -[[location(0)]] var out_uv: vec3; -[[builtin(vertex_index)]] var in_vertex_index: u32; +struct SkyOutput { + [[builtin(position)]] position: vec4; + [[location(0)]] uv: vec3; +}; [[block]] struct Data { @@ -18,10 +18,10 @@ struct Data { var r_data: Data; [[stage(vertex)]] -fn vs_sky() { +fn vs_sky([[builtin(vertex_index)]] vertex_index: u32) -> SkyOutput { // hacky way to draw a large triangle - var tmp1: i32 = i32(in_vertex_index) / 2; - var tmp2: i32 = i32(in_vertex_index) & 1; + var tmp1: i32 = i32(vertex_index) / 2; + var tmp2: i32 = i32(vertex_index) & 1; const pos: vec4 = vec4( f32(tmp1) * 4.0 - 1.0, f32(tmp2) * 4.0 - 1.0, @@ -31,21 +31,30 @@ fn vs_sky() { // transposition = inversion for this orthonormal matrix const inv_model_view: mat3x3 = transpose(mat3x3(r_data.view.x.xyz, r_data.view.y.xyz, r_data.view.z.xyz)); - var unprojected: vec4 = r_data.proj_inv * pos; //TODO: const - out_uv = inv_model_view * unprojected.xyz; - out_position = pos; + const unprojected: vec4 = r_data.proj_inv * pos; + + var out: SkyOutput; + out.uv = inv_model_view * unprojected.xyz; + out.position = pos; + return out; } -[[location(0)]] var in_pos: vec3; -[[location(1)]] var in_normal: vec3; -[[location(1)]] var out_normal: vec3; -[[location(3)]] var out_view: vec3; +struct EntityOutput { + [[builtin(position)]] position: vec4; + [[location(1)]] normal: vec3; + [[location(3)]] view: vec3; +}; [[stage(vertex)]] -fn vs_entity() { - out_normal = in_normal; - out_view = in_pos - r_data.cam_pos.xyz; - out_position = r_data.proj * r_data.view * vec4(in_pos, 1.0); +fn vs_entity( + [[location(0)]] pos: vec3, + [[location(1)]] normal: vec3, +) -> EntityOutput { + var out: EntityOutput; + out.normal = normal; + out.view = pos - r_data.cam_pos.xyz; + out.position = r_data.proj * r_data.view * vec4(pos, 1.0); + return out; } [[group(0), binding(1)]] @@ -53,21 +62,17 @@ var r_texture: texture_cube; [[group(0), binding(2)]] var r_sampler: sampler; -[[location(0)]] var in_uv: vec3; -[[location(0)]] var out_color: vec4; -[[location(3)]] var in_view: vec3; - [[stage(fragment)]] -fn fs_sky() { - out_color = textureSample(r_texture, r_sampler, in_uv); +fn fs_sky(in: SkyOutput) -> [[location(0)]] vec4 { + return textureSample(r_texture, r_sampler, in.uv); } [[stage(fragment)]] -fn fs_entity() { - const incident: vec3 = normalize(in_view); - const normal: vec3 = normalize(in_normal); +fn fs_entity(in: EntityOutput) -> [[location(0)]] vec4 { + const incident: vec3 = normalize(in.view); + const normal: vec3 = normalize(in.normal); const reflected: vec3 = incident - 2.0 * dot(normal, incident) * normal; - var reflected_color: vec4 = textureSample(r_texture, r_sampler, reflected); - out_color = vec4(0.1, 0.1, 0.1, 0.1) + 0.5 * reflected_color; + const reflected_color: vec4 = textureSample(r_texture, r_sampler, reflected); + return vec4(0.1, 0.1, 0.1, 0.1) + 0.5 * reflected_color; } diff --git a/src/backend/direct.rs b/src/backend/direct.rs index 59b37e2fb..4fa5563d6 100644 --- a/src/backend/direct.rs +++ b/src/backend/direct.rs @@ -171,7 +171,7 @@ mod pass_impl { } fn write_timestamp(&mut self, query_set: &wgc::id::QuerySetId, query_index: u32) { - unsafe { wgpu_compute_pass_write_timestamp(self, *query_set, query_index) } + wgpu_compute_pass_write_timestamp(self, *query_set, query_index) } fn begin_pipeline_statistics_query( @@ -179,13 +179,11 @@ mod pass_impl { query_set: &wgc::id::QuerySetId, query_index: u32, ) { - unsafe { - wgpu_compute_pass_begin_pipeline_statistics_query(self, *query_set, query_index) - } + wgpu_compute_pass_begin_pipeline_statistics_query(self, *query_set, query_index) } fn end_pipeline_statistics_query(&mut self) { - unsafe { wgpu_compute_pass_end_pipeline_statistics_query(self) } + wgpu_compute_pass_end_pipeline_statistics_query(self) } fn dispatch(&mut self, x: u32, y: u32, z: u32) { @@ -380,7 +378,7 @@ mod pass_impl { } fn write_timestamp(&mut self, query_set: &wgc::id::QuerySetId, query_index: u32) { - unsafe { wgpu_render_pass_write_timestamp(self, *query_set, query_index) } + wgpu_render_pass_write_timestamp(self, *query_set, query_index) } fn begin_pipeline_statistics_query( @@ -388,13 +386,11 @@ mod pass_impl { query_set: &wgc::id::QuerySetId, query_index: u32, ) { - unsafe { - wgpu_render_pass_begin_pipeline_statistics_query(self, *query_set, query_index) - } + wgpu_render_pass_begin_pipeline_statistics_query(self, *query_set, query_index) } fn end_pipeline_statistics_query(&mut self) { - unsafe { wgpu_render_pass_end_pipeline_statistics_query(self) } + wgpu_render_pass_end_pipeline_statistics_query(self) } fn execute_bundles<'a, I: Iterator>(