From 1d1aea243fb2352af0d5f33d33df25ca1cfc1ca6 Mon Sep 17 00:00:00 2001 From: pyrotechnick <13998+pyrotechnick@users.noreply.github.com> Date: Wed, 14 Jul 2021 11:57:52 +1000 Subject: [PATCH] Improve WGSL interpolation attributes (#1083) * [wgsl-in] Map interpolate(..., center) to Sampling::Center * [wgsl-out] Improve interpolation attributes * [wgsl] Fix tests/in/interpolate.wgsl typo/format * [wgsl] Snapshot WGSL artefacts * [wgsl-out] Stack-only writing of interpolation --- src/back/wgsl/writer.rs | 34 +++++++++---------- src/front/wgsl/conv.rs | 1 + tests/in/interpolate.wgsl | 10 +++--- tests/out/wgsl/210-bevy-2d-shader-frag.wgsl | 2 +- tests/out/wgsl/210-bevy-2d-shader-vert.wgsl | 4 +-- tests/out/wgsl/210-bevy-shader-vert.wgsl | 8 ++--- .../wgsl/800-out-of-bounds-panic-vert.wgsl | 4 +-- tests/out/wgsl/bevy-pbr-frag.wgsl | 4 +-- tests/out/wgsl/bevy-pbr-vert.wgsl | 10 +++--- tests/out/wgsl/bool-select-frag.wgsl | 2 +- tests/out/wgsl/clamp-splat-vert.wgsl | 2 +- tests/out/wgsl/extra.wgsl | 2 +- tests/out/wgsl/interface.wgsl | 2 +- tests/out/wgsl/interpolate.wgsl | 10 +++--- .../out/wgsl/multiple_entry_points-glsl.wgsl | 2 +- tests/out/wgsl/quad-vert.wgsl | 2 +- tests/out/wgsl/quad.wgsl | 4 +-- tests/out/wgsl/quad_glsl-frag.wgsl | 2 +- tests/out/wgsl/quad_glsl-vert.wgsl | 4 +-- tests/out/wgsl/shadow.wgsl | 2 +- tests/out/wgsl/skybox.wgsl | 2 +- 21 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/back/wgsl/writer.rs b/src/back/wgsl/writer.rs index 354acd1915..53b70df668 100644 --- a/src/back/wgsl/writer.rs +++ b/src/back/wgsl/writer.rs @@ -324,23 +324,23 @@ impl Writer { Attribute::Binding(id) => format!("binding({})", id), Attribute::Group(id) => format!("group({})", id), Attribute::Interpolate(interpolation, sampling) => { - if interpolation.is_some() || sampling.is_some() { - let interpolation_str = if let Some(interpolation) = interpolation { - interpolation_str(interpolation) - } else { - "" - }; - let sampling_str = if let Some(sampling) = sampling { - // Center sampling is the default - if sampling == crate::Sampling::Center { - String::from("") - } else { - format!(",{}", sampling_str(sampling)) - } - } else { - String::from("") - }; - format!("interpolate({}{})", interpolation_str, sampling_str) + if sampling.is_some() && sampling != Some(crate::Sampling::Center) { + format!( + "interpolate({}, {})", + interpolation_str( + interpolation.unwrap_or(crate::Interpolation::Perspective) + ), + sampling_str(sampling.unwrap_or(crate::Sampling::Center)) + ) + } else if interpolation.is_some() + && interpolation != Some(crate::Interpolation::Perspective) + { + format!( + "interpolate({})", + interpolation_str( + interpolation.unwrap_or(crate::Interpolation::Perspective) + ) + ) } else { String::from("") } diff --git a/src/front/wgsl/conv.rs b/src/front/wgsl/conv.rs index deb9643df9..edd4e30195 100644 --- a/src/front/wgsl/conv.rs +++ b/src/front/wgsl/conv.rs @@ -52,6 +52,7 @@ pub fn map_interpolation(word: &str, span: Span) -> Result Result> { match word { + "center" => Ok(crate::Sampling::Center), "centroid" => Ok(crate::Sampling::Centroid), "sample" => Ok(crate::Sampling::Sample), _ => Err(Error::UnknownAttribute(span)), diff --git a/tests/in/interpolate.wgsl b/tests/in/interpolate.wgsl index dcb8a66c5a..f95c0b6ca9 100644 --- a/tests/in/interpolate.wgsl +++ b/tests/in/interpolate.wgsl @@ -1,14 +1,14 @@ -//TODO: mere with "interface"? +//TODO: merge with "interface"? struct FragmentInput { [[builtin(position)]] position: vec4; [[location(0), interpolate(flat)]] flat : u32; [[location(1), interpolate(linear)]] linear : f32; - [[location(2), interpolate(linear,centroid)]] linear_centroid : vec2; - [[location(3), interpolate(linear,sample)]] linear_sample : vec3; + [[location(2), interpolate(linear, centroid)]] linear_centroid : vec2; + [[location(3), interpolate(linear, sample)]] linear_sample : vec3; [[location(4), interpolate(perspective)]] perspective : vec4; - [[location(5), interpolate(perspective,centroid)]] perspective_centroid : f32; - [[location(6), interpolate(perspective,sample)]] perspective_sample : f32; + [[location(5), interpolate(perspective, centroid)]] perspective_centroid : f32; + [[location(6), interpolate(perspective, sample)]] perspective_sample : f32; }; [[stage(vertex)]] diff --git a/tests/out/wgsl/210-bevy-2d-shader-frag.wgsl b/tests/out/wgsl/210-bevy-2d-shader-frag.wgsl index 59986e9861..f7eff0a34e 100644 --- a/tests/out/wgsl/210-bevy-2d-shader-frag.wgsl +++ b/tests/out/wgsl/210-bevy-2d-shader-frag.wgsl @@ -4,7 +4,7 @@ struct ColorMaterial_color { }; struct FragmentOutput { - [[location(0), interpolate(perspective)]] o_Target: vec4; + [[location(0)]] o_Target: vec4; }; var v_Uv: vec2; diff --git a/tests/out/wgsl/210-bevy-2d-shader-vert.wgsl b/tests/out/wgsl/210-bevy-2d-shader-vert.wgsl index 45b32f3510..4bdf88b0e7 100644 --- a/tests/out/wgsl/210-bevy-2d-shader-vert.wgsl +++ b/tests/out/wgsl/210-bevy-2d-shader-vert.wgsl @@ -14,7 +14,7 @@ struct Sprite_size { }; struct VertexOutput { - [[location(0), interpolate(perspective)]] v_Uv: vec2; + [[location(0)]] v_Uv: vec2; [[builtin(position)]] member: vec4; }; @@ -46,7 +46,7 @@ fn main1() { } [[stage(vertex)]] -fn main([[location(0), interpolate(perspective)]] Vertex_Position: vec3, [[location(2), interpolate(perspective)]] Vertex_Uv: vec2) -> VertexOutput { +fn main([[location(0)]] Vertex_Position: vec3, [[location(2)]] Vertex_Uv: vec2) -> VertexOutput { Vertex_Position1 = Vertex_Position; Vertex_Uv1 = Vertex_Uv; main1(); diff --git a/tests/out/wgsl/210-bevy-shader-vert.wgsl b/tests/out/wgsl/210-bevy-shader-vert.wgsl index 2cdca5706e..50319b857a 100644 --- a/tests/out/wgsl/210-bevy-shader-vert.wgsl +++ b/tests/out/wgsl/210-bevy-shader-vert.wgsl @@ -9,9 +9,9 @@ struct Transform { }; struct VertexOutput { - [[location(0), interpolate(perspective)]] v_Position: vec3; - [[location(1), interpolate(perspective)]] v_Normal: vec3; - [[location(2), interpolate(perspective)]] v_Uv: vec2; + [[location(0)]] v_Position: vec3; + [[location(1)]] v_Normal: vec3; + [[location(2)]] v_Uv: vec2; [[builtin(position)]] member: vec4; }; @@ -46,7 +46,7 @@ fn main1() { } [[stage(vertex)]] -fn main([[location(0), interpolate(perspective)]] Vertex_Position: vec3, [[location(1), interpolate(perspective)]] Vertex_Normal: vec3, [[location(2), interpolate(perspective)]] Vertex_Uv: vec2) -> VertexOutput { +fn main([[location(0)]] Vertex_Position: vec3, [[location(1)]] Vertex_Normal: vec3, [[location(2)]] Vertex_Uv: vec2) -> VertexOutput { Vertex_Position1 = Vertex_Position; Vertex_Normal1 = Vertex_Normal; Vertex_Uv1 = Vertex_Uv; diff --git a/tests/out/wgsl/800-out-of-bounds-panic-vert.wgsl b/tests/out/wgsl/800-out-of-bounds-panic-vert.wgsl index 80198bf1ae..66ba8cc4bf 100644 --- a/tests/out/wgsl/800-out-of-bounds-panic-vert.wgsl +++ b/tests/out/wgsl/800-out-of-bounds-panic-vert.wgsl @@ -9,7 +9,7 @@ struct VertexPushConstants { }; struct VertexOutput { - [[location(0), interpolate(perspective)]] frag_color: vec4; + [[location(0)]] frag_color: vec4; [[builtin(position)]] member: vec4; }; @@ -32,7 +32,7 @@ fn main1() { } [[stage(vertex)]] -fn main([[location(0), interpolate(perspective)]] position: vec2, [[location(1), interpolate(perspective)]] color: vec4) -> VertexOutput { +fn main([[location(0)]] position: vec2, [[location(1)]] color: vec4) -> VertexOutput { position1 = position; color1 = color; main1(); diff --git a/tests/out/wgsl/bevy-pbr-frag.wgsl b/tests/out/wgsl/bevy-pbr-frag.wgsl index 948b2cfb9c..dbd65184f9 100644 --- a/tests/out/wgsl/bevy-pbr-frag.wgsl +++ b/tests/out/wgsl/bevy-pbr-frag.wgsl @@ -53,7 +53,7 @@ struct StandardMaterial_emissive { }; struct FragmentOutput { - [[location(0), interpolate(perspective)]] o_Target: vec4; + [[location(0)]] o_Target: vec4; }; var v_WorldPosition1: vec3; @@ -842,7 +842,7 @@ fn main1() { } [[stage(fragment)]] -fn main([[location(0), interpolate(perspective)]] v_WorldPosition: vec3, [[location(1), interpolate(perspective)]] v_WorldNormal: vec3, [[location(2), interpolate(perspective)]] v_Uv: vec2, [[location(3), interpolate(perspective)]] v_WorldTangent: vec4, [[builtin(front_facing)]] param: bool) -> FragmentOutput { +fn main([[location(0)]] v_WorldPosition: vec3, [[location(1)]] v_WorldNormal: vec3, [[location(2)]] v_Uv: vec2, [[location(3)]] v_WorldTangent: vec4, [[builtin(front_facing)]] param: bool) -> FragmentOutput { v_WorldPosition1 = v_WorldPosition; v_WorldNormal1 = v_WorldNormal; v_Uv1 = v_Uv; diff --git a/tests/out/wgsl/bevy-pbr-vert.wgsl b/tests/out/wgsl/bevy-pbr-vert.wgsl index 63001d8086..3acb744206 100644 --- a/tests/out/wgsl/bevy-pbr-vert.wgsl +++ b/tests/out/wgsl/bevy-pbr-vert.wgsl @@ -9,10 +9,10 @@ struct Transform { }; struct VertexOutput { - [[location(0), interpolate(perspective)]] v_WorldPosition: vec3; - [[location(1), interpolate(perspective)]] v_WorldNormal: vec3; - [[location(2), interpolate(perspective)]] v_Uv: vec2; - [[location(3), interpolate(perspective)]] v_WorldTangent: vec4; + [[location(0)]] v_WorldPosition: vec3; + [[location(1)]] v_WorldNormal: vec3; + [[location(2)]] v_Uv: vec2; + [[location(3)]] v_WorldTangent: vec4; [[builtin(position)]] member: vec4; }; @@ -54,7 +54,7 @@ fn main1() { } [[stage(vertex)]] -fn main([[location(0), interpolate(perspective)]] Vertex_Position: vec3, [[location(1), interpolate(perspective)]] Vertex_Normal: vec3, [[location(2), interpolate(perspective)]] Vertex_Uv: vec2, [[location(3), interpolate(perspective)]] Vertex_Tangent: vec4) -> VertexOutput { +fn main([[location(0)]] Vertex_Position: vec3, [[location(1)]] Vertex_Normal: vec3, [[location(2)]] Vertex_Uv: vec2, [[location(3)]] Vertex_Tangent: vec4) -> VertexOutput { Vertex_Position1 = Vertex_Position; Vertex_Normal1 = Vertex_Normal; Vertex_Uv1 = Vertex_Uv; diff --git a/tests/out/wgsl/bool-select-frag.wgsl b/tests/out/wgsl/bool-select-frag.wgsl index 51524143bc..3702c01c56 100644 --- a/tests/out/wgsl/bool-select-frag.wgsl +++ b/tests/out/wgsl/bool-select-frag.wgsl @@ -1,5 +1,5 @@ struct FragmentOutput { - [[location(0), interpolate(perspective)]] o_color: vec4; + [[location(0)]] o_color: vec4; }; var o_color: vec4; diff --git a/tests/out/wgsl/clamp-splat-vert.wgsl b/tests/out/wgsl/clamp-splat-vert.wgsl index 08f00587b6..f4afbc77c4 100644 --- a/tests/out/wgsl/clamp-splat-vert.wgsl +++ b/tests/out/wgsl/clamp-splat-vert.wgsl @@ -12,7 +12,7 @@ fn main1() { } [[stage(vertex)]] -fn main([[location(0), interpolate(perspective)]] a_pos: vec2) -> VertexOutput { +fn main([[location(0)]] a_pos: vec2) -> VertexOutput { a_pos1 = a_pos; main1(); let _e3: vec4 = gl_Position; diff --git a/tests/out/wgsl/extra.wgsl b/tests/out/wgsl/extra.wgsl index 8db56b157e..b68b85568f 100644 --- a/tests/out/wgsl/extra.wgsl +++ b/tests/out/wgsl/extra.wgsl @@ -7,6 +7,6 @@ struct PushConstants { var pc: PushConstants; [[stage(fragment)]] -fn main([[location(0), interpolate(perspective)]] color: vec4) -> [[location(0)]] vec4 { +fn main([[location(0)]] color: vec4) -> [[location(0)]] vec4 { return color; } diff --git a/tests/out/wgsl/interface.wgsl b/tests/out/wgsl/interface.wgsl index e574da5c9e..30875ee8e6 100644 --- a/tests/out/wgsl/interface.wgsl +++ b/tests/out/wgsl/interface.wgsl @@ -1,6 +1,6 @@ struct VertexOutput { [[builtin(position)]] position: vec4; - [[location(1), interpolate(perspective)]] varying: f32; + [[location(1)]] varying: f32; }; struct FragmentOutput { diff --git a/tests/out/wgsl/interpolate.wgsl b/tests/out/wgsl/interpolate.wgsl index 45a7d0a4bf..b0b2a9eb1b 100644 --- a/tests/out/wgsl/interpolate.wgsl +++ b/tests/out/wgsl/interpolate.wgsl @@ -2,11 +2,11 @@ struct FragmentInput { [[builtin(position)]] position: vec4; [[location(0), interpolate(flat)]] flat: u32; [[location(1), interpolate(linear)]] linear: f32; - [[location(2), interpolate(linear,centroid)]] linear_centroid: vec2; - [[location(3), interpolate(linear,sample)]] linear_sample: vec3; - [[location(4), interpolate(perspective)]] perspective: vec4; - [[location(5), interpolate(perspective,centroid)]] perspective_centroid: f32; - [[location(6), interpolate(perspective,sample)]] perspective_sample: f32; + [[location(2), interpolate(linear, centroid)]] linear_centroid: vec2; + [[location(3), interpolate(linear, sample)]] linear_sample: vec3; + [[location(4)]] perspective: vec4; + [[location(5), interpolate(perspective, centroid)]] perspective_centroid: f32; + [[location(6), interpolate(perspective, sample)]] perspective_sample: f32; }; [[stage(vertex)]] diff --git a/tests/out/wgsl/multiple_entry_points-glsl.wgsl b/tests/out/wgsl/multiple_entry_points-glsl.wgsl index 2b8844526f..ee890b8be9 100644 --- a/tests/out/wgsl/multiple_entry_points-glsl.wgsl +++ b/tests/out/wgsl/multiple_entry_points-glsl.wgsl @@ -3,7 +3,7 @@ struct VertexOutput { }; struct FragmentOutput { - [[location(0), interpolate(perspective)]] o_color: vec4; + [[location(0)]] o_color: vec4; }; var gl_Position: vec4; diff --git a/tests/out/wgsl/quad-vert.wgsl b/tests/out/wgsl/quad-vert.wgsl index 6830078153..bb6a86a19c 100644 --- a/tests/out/wgsl/quad-vert.wgsl +++ b/tests/out/wgsl/quad-vert.wgsl @@ -4,7 +4,7 @@ struct gl_PerVertex { }; struct VertexOutput { - [[location(0), interpolate(perspective)]] member: vec2; + [[location(0)]] member: vec2; [[builtin(position)]] gl_Position: vec4; }; diff --git a/tests/out/wgsl/quad.wgsl b/tests/out/wgsl/quad.wgsl index 5c14f1ee46..0b2a0c1d89 100644 --- a/tests/out/wgsl/quad.wgsl +++ b/tests/out/wgsl/quad.wgsl @@ -1,5 +1,5 @@ struct VertexOutput { - [[location(0), interpolate(perspective)]] uv: vec2; + [[location(0)]] uv: vec2; [[builtin(position)]] position: vec4; }; @@ -16,7 +16,7 @@ fn main([[location(0)]] pos: vec2, [[location(1)]] uv: vec2) -> Vertex } [[stage(fragment)]] -fn main1([[location(0), interpolate(perspective)]] uv1: vec2) -> [[location(0)]] vec4 { +fn main1([[location(0)]] uv1: vec2) -> [[location(0)]] vec4 { let color: vec4 = textureSample(u_texture, u_sampler, uv1); if ((color.w == 0.0)) { discard; diff --git a/tests/out/wgsl/quad_glsl-frag.wgsl b/tests/out/wgsl/quad_glsl-frag.wgsl index ea2f651abf..9f8f2eaf25 100644 --- a/tests/out/wgsl/quad_glsl-frag.wgsl +++ b/tests/out/wgsl/quad_glsl-frag.wgsl @@ -1,5 +1,5 @@ struct FragmentOutput { - [[location(0), interpolate(perspective)]] o_color: vec4; + [[location(0)]] o_color: vec4; }; var v_uv: vec2; diff --git a/tests/out/wgsl/quad_glsl-vert.wgsl b/tests/out/wgsl/quad_glsl-vert.wgsl index fe5aa367a3..9e659a3b35 100644 --- a/tests/out/wgsl/quad_glsl-vert.wgsl +++ b/tests/out/wgsl/quad_glsl-vert.wgsl @@ -1,5 +1,5 @@ struct VertexOutput { - [[location(0), interpolate(perspective)]] v_uv: vec2; + [[location(0)]] v_uv: vec2; [[builtin(position)]] member: vec4; }; @@ -17,7 +17,7 @@ fn main1() { } [[stage(vertex)]] -fn main([[location(0), interpolate(perspective)]] a_pos: vec2, [[location(1), interpolate(perspective)]] a_uv: vec2) -> VertexOutput { +fn main([[location(0)]] a_pos: vec2, [[location(1)]] a_uv: vec2) -> VertexOutput { a_pos1 = a_pos; a_uv1 = a_uv; main1(); diff --git a/tests/out/wgsl/shadow.wgsl b/tests/out/wgsl/shadow.wgsl index 26993e5e2e..f1291003d9 100644 --- a/tests/out/wgsl/shadow.wgsl +++ b/tests/out/wgsl/shadow.wgsl @@ -37,7 +37,7 @@ fn fetch_shadow(light_id: u32, homogeneous_coords: vec4) -> f32 { } [[stage(fragment)]] -fn fs_main([[location(0), interpolate(perspective)]] raw_normal: vec3, [[location(1), interpolate(perspective)]] position: vec4) -> [[location(0)]] vec4 { +fn fs_main([[location(0)]] raw_normal: vec3, [[location(1)]] position: vec4) -> [[location(0)]] vec4 { var color: vec3 = vec3(0.05, 0.05, 0.05); var i: u32 = 0u; diff --git a/tests/out/wgsl/skybox.wgsl b/tests/out/wgsl/skybox.wgsl index 83f170f8a5..75460c49b8 100644 --- a/tests/out/wgsl/skybox.wgsl +++ b/tests/out/wgsl/skybox.wgsl @@ -1,6 +1,6 @@ struct VertexOutput { [[builtin(position)]] position: vec4; - [[location(0), interpolate(perspective)]] uv: vec3; + [[location(0)]] uv: vec3; }; [[block]]