From 142038c85bdcb8acd391e8cb2bf9f0dca5679631 Mon Sep 17 00:00:00 2001 From: Mehmet Oguz Derin Date: Sun, 11 Apr 2021 07:35:03 +0300 Subject: [PATCH] WGSL: `const` to `let` --- src/front/wgsl/mod.rs | 10 +++++----- src/front/wgsl/tests.rs | 40 ++++++++++++++++++++-------------------- tests/errors.rs | 16 ++++++++-------- tests/in/boids.wgsl | 4 ++-- tests/in/image-copy.wgsl | 6 +++--- tests/in/quad.wgsl | 6 +++--- tests/in/shadow.wgsl | 20 ++++++++++---------- tests/in/skybox.wgsl | 6 +++--- 8 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/front/wgsl/mod.rs b/src/front/wgsl/mod.rs index e618d68124..5f791a37aa 100644 --- a/src/front/wgsl/mod.rs +++ b/src/front/wgsl/mod.rs @@ -123,7 +123,7 @@ pub enum Error<'a> { #[error("builtin {0:?} is not implemented")] UnimplementedBuiltin(crate::BuiltIn), #[error("expression {0} doesn't match its given type {1:?}")] - ConstTypeMismatch(&'a str, Handle), + LetTypeMismatch(&'a str, Handle), #[error("other error")] Other, } @@ -1999,7 +1999,7 @@ impl Parser { self.scopes.push(Scope::Statement); let mut emitter = super::Emitter::default(); match word { - "const" => { + "let" => { emitter.start(context.expressions); let name = lexer.next_ident()?; let given_ty = if lexer.skip(Token::Separator(':')) { @@ -2026,7 +2026,7 @@ impl Parser { given_inner, expr_inner ); - return Err(Error::ConstTypeMismatch(name, ty)); + return Err(Error::LetTypeMismatch(name, ty)); } } block.extend(emitter.finish(context.expressions)); @@ -2584,7 +2584,7 @@ impl Parser { self.lookup_type.insert(name.to_owned(), ty); lexer.expect(Token::Separator(';'))?; } - (Token::Word("const"), _) => { + (Token::Word("let"), _) => { let (name, explicit_ty, _access) = self.parse_variable_ident_decl( lexer, &mut module.types, @@ -2611,7 +2611,7 @@ impl Parser { crate::ConstantInner::Composite { ty, components: _ } => ty == explicit_ty, }; if !type_match { - return Err(Error::ConstTypeMismatch(name, explicit_ty)); + return Err(Error::LetTypeMismatch(name, explicit_ty)); } //TODO: check `ty` against `const_handle`. lexer.expect(Token::Separator(';'))?; diff --git a/src/front/wgsl/tests.rs b/src/front/wgsl/tests.rs index 64da1fbd46..96b96a7187 100644 --- a/src/front/wgsl/tests.rs +++ b/src/front/wgsl/tests.rs @@ -16,8 +16,8 @@ fn parse_comment() { #[test] fn parse_types() { - parse_str("const a : i32 = 2;").unwrap(); - assert!(parse_str("const a : x32 = 2;").is_err()); + parse_str("let a : i32 = 2;").unwrap(); + assert!(parse_str("let a : x32 = 2;").is_err()); parse_str("var t: texture_2d;").unwrap(); parse_str("var t: texture_cube_array;").unwrap(); parse_str("var t: texture_multisampled_2d;").unwrap(); @@ -30,14 +30,14 @@ fn parse_type_inference() { parse_str( " fn foo() { - const a = 2u; - const b: u32 = a; + let a = 2u; + let b: u32 = a; }", ) .unwrap(); assert!(parse_str( " - fn foo() { const c : i32 = 2.0; }", + fn foo() { let c : i32 = 2.0; }", ) .is_err()); } @@ -46,7 +46,7 @@ fn parse_type_inference() { fn parse_type_cast() { parse_str( " - const a : i32 = 2; + let a : i32 = 2; fn main() { var x: f32 = f32(a); x = f32(i32(a + 1) / 2); @@ -57,8 +57,8 @@ fn parse_type_cast() { parse_str( " fn main() { - const x: vec2 = vec2(1.0, 2.0); - const y: vec2 = vec2(x); + let x: vec2 = vec2(1.0, 2.0); + let y: vec2 = vec2(x); } ", ) @@ -201,7 +201,7 @@ fn parse_texture_load() { " var t: texture_3d; fn foo() { - const r: vec4 = textureLoad(t, vec3(0.0, 1.0, 2.0), 1); + let r: vec4 = textureLoad(t, vec3(0.0, 1.0, 2.0), 1); } ", ) @@ -210,7 +210,7 @@ fn parse_texture_load() { " var t: texture_multisampled_2d_array; fn foo() { - const r: vec4 = textureLoad(t, vec2(10, 20), 2, 3); + let r: vec4 = textureLoad(t, vec2(10, 20), 2, 3); } ", ) @@ -219,7 +219,7 @@ fn parse_texture_load() { " var t: [[access(read)]] texture_storage_1d_array; fn foo() { - const r: vec4 = textureLoad(t, 10, 2); + let r: vec4 = textureLoad(t, 10, 2); } ", ) @@ -247,8 +247,8 @@ fn parse_texture_query() { fn foo() { var dim: vec2 = textureDimensions(t); dim = textureDimensions(t, 0); - const layers: i32 = textureNumLayers(t); - const samples: i32 = textureNumSamples(t); + let layers: i32 = textureNumLayers(t); + let samples: i32 = textureNumSamples(t); } ", ) @@ -259,8 +259,8 @@ fn parse_texture_query() { fn parse_postfix() { parse_str( "fn foo() { - const x: f32 = vec4(1.0, 2.0, 3.0, 4.0).xyz.rgbr.aaaa.wz.g; - const y: f32 = fract(vec2(0.5, x)).x; + let x: f32 = vec4(1.0, 2.0, 3.0, 4.0).xyz.rgbr.aaaa.wz.g; + let y: f32 = fract(vec2(0.5, x)).x; }", ) .unwrap(); @@ -269,9 +269,9 @@ fn parse_postfix() { #[test] fn parse_expressions() { parse_str("fn foo() { - const x: f32 = select(0.0, 1.0, true); - const y: vec2 = select(vec2(1.0, 1.0), vec2(x, x), vec2(x < 0.5, x > 0.5)); - const z: bool = !(0.0 == 1.0); + let x: f32 = select(0.0, 1.0, true); + let y: vec2 = select(vec2(1.0, 1.0), vec2(x, x), vec2(x < 0.5, x > 0.5)); + let z: bool = !(0.0 == 1.0); }").unwrap(); } @@ -280,8 +280,8 @@ fn parse_pointers() { parse_str( "fn foo() { var x: f32 = 1.0; - const px = &x; - const py = frexp(0.5, px); + let px = &x; + let py = frexp(0.5, px); }", ) .unwrap(); diff --git a/tests/errors.rs b/tests/errors.rs index 2f159c7442..437288ab58 100644 --- a/tests/errors.rs +++ b/tests/errors.rs @@ -46,13 +46,13 @@ fn invalid_integer() { #[test] fn invalid_float() { err!( - "const scale: f32 = 1.1.;", + "let scale: f32 = 1.1.;", @r###" error: expected floating-point literal, found `1.1.` - ┌─ wgsl:1:20 + ┌─ wgsl:1:18 │ - 1 │ const scale: f32 = 1.1.; - │ ^^^^ expected floating-point literal + 1 │ let scale: f32 = 1.1.; + │ ^^^^ expected floating-point literal "### ); @@ -62,13 +62,13 @@ fn invalid_float() { #[test] fn invalid_scalar_width() { err!( - "const scale: f32 = 1.1f1000;", + "let scale: f32 = 1.1f1000;", @r###" error: invalid width of `1000` for literal - ┌─ wgsl:1:20 + ┌─ wgsl:1:18 │ - 1 │ const scale: f32 = 1.1f1000; - │ ^^^^^^^^ invalid width + 1 │ let scale: f32 = 1.1f1000; + │ ^^^^^^^^ invalid width │ = note: valid width is 32 diff --git a/tests/in/boids.wgsl b/tests/in/boids.wgsl index 935d8ee08c..3db45e1cb8 100644 --- a/tests/in/boids.wgsl +++ b/tests/in/boids.wgsl @@ -1,4 +1,4 @@ -const NUM_PARTICLES: u32 = 1500u; +let NUM_PARTICLES: u32 = 1500u; struct Particle { pos : vec2; @@ -28,7 +28,7 @@ struct 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) { - const index : u32 = global_invocation_id.x; + let index : u32 = global_invocation_id.x; if (index >= NUM_PARTICLES) { return; } diff --git a/tests/in/image-copy.wgsl b/tests/in/image-copy.wgsl index cb09083b9a..157d081461 100644 --- a/tests/in/image-copy.wgsl +++ b/tests/in/image-copy.wgsl @@ -9,8 +9,8 @@ fn main( //TODO: https://github.com/gpuweb/gpuweb/issues/1590 //[[builtin(workgroup_size)]] wg_size: vec3 ) { - const dim = textureDimensions(image_src); - const itc = dim * vec2(local_id.xy) % vec2(10, 20); - const value = textureLoad(image_src, itc); + let dim = textureDimensions(image_src); + let itc = dim * vec2(local_id.xy) % vec2(10, 20); + let value = textureLoad(image_src, itc); textureStore(image_dst, itc.x, value); } diff --git a/tests/in/quad.wgsl b/tests/in/quad.wgsl index 6283687ef7..fdaa9600c0 100644 --- a/tests/in/quad.wgsl +++ b/tests/in/quad.wgsl @@ -1,5 +1,5 @@ // vertex -const c_scale: f32 = 1.2; +let c_scale: f32 = 1.2; struct VertexOutput { [[location(0)]] uv : vec2; @@ -20,12 +20,12 @@ fn main([[location(0)]] pos : vec2, [[location(1)]] uv : vec2) -> Vert [[stage(fragment)]] fn main([[location(0)]] uv : vec2) -> [[location(0)]] vec4 { - const color = textureSample(u_texture, u_sampler, uv); + let color = textureSample(u_texture, u_sampler, uv); if (color.a == 0.0) { discard; } // forcing the expression here to be emitted in order to check the // uniformity of the control flow a bit more strongly. - const premultiplied = color.a * color; + let premultiplied = color.a * color; return premultiplied; } diff --git a/tests/in/shadow.wgsl b/tests/in/shadow.wgsl index f40802cb43..16d2e79d03 100644 --- a/tests/in/shadow.wgsl +++ b/tests/in/shadow.wgsl @@ -28,21 +28,21 @@ fn fetch_shadow(light_id: u32, homogeneous_coords: vec4) -> f32 { if (homogeneous_coords.w <= 0.0) { return 1.0; } - const flip_correction = vec2(0.5, -0.5); - const proj_correction = 1.0 / homogeneous_coords.w; - const light_local = homogeneous_coords.xy * flip_correction * proj_correction + vec2(0.5, 0.5); + let flip_correction = vec2(0.5, -0.5); + let proj_correction = 1.0 / homogeneous_coords.w; + let light_local = homogeneous_coords.xy * flip_correction * proj_correction + vec2(0.5, 0.5); return textureSampleCompare(t_shadow, sampler_shadow, light_local, i32(light_id), homogeneous_coords.z * proj_correction); } -const c_ambient: vec3 = vec3(0.05, 0.05, 0.05); -const c_max_lights: u32 = 10u; +let c_ambient: vec3 = vec3(0.05, 0.05, 0.05); +let c_max_lights: u32 = 10u; [[stage(fragment)]] fn fs_main( [[location(0)]] raw_normal: vec3, [[location(1)]] position: vec4 ) -> [[location(0)]] vec4 { - const normal: vec3 = normalize(raw_normal); + let normal: vec3 = normalize(raw_normal); // accumulate color var color: vec3 = c_ambient; var i: u32 = 0u; @@ -50,10 +50,10 @@ fn fs_main( if (i >= min(u_globals.num_lights.x, c_max_lights)) { break; } - const light = s_lights.data[i]; - const shadow = fetch_shadow(i, light.proj * position); - const light_dir = normalize(light.pos.xyz - position.xyz); - const diffuse = max(0.0, dot(normal, light_dir)); + let light = s_lights.data[i]; + let shadow = fetch_shadow(i, light.proj * position); + let light_dir = normalize(light.pos.xyz - position.xyz); + let diffuse = max(0.0, dot(normal, light_dir)); color = color + shadow * diffuse * light.color.xyz; continuing { i = i + 1u; diff --git a/tests/in/skybox.wgsl b/tests/in/skybox.wgsl index 32ad80ca11..8123737928 100644 --- a/tests/in/skybox.wgsl +++ b/tests/in/skybox.wgsl @@ -16,15 +16,15 @@ fn vs_main([[builtin(vertex_index)]] vertex_index: u32) -> VertexOutput { // hacky way to draw a large triangle var tmp1: i32 = i32(vertex_index) / 2; var tmp2: i32 = i32(vertex_index) & 1; - const pos = vec4( + let pos = vec4( f32(tmp1) * 4.0 - 1.0, f32(tmp2) * 4.0 - 1.0, 0.0, 1.0 ); - const inv_model_view = transpose(mat3x3(r_data.view.x.xyz, r_data.view.y.xyz, r_data.view.z.xyz)); - const unprojected = r_data.proj_inv * pos; + let inv_model_view = transpose(mat3x3(r_data.view.x.xyz, r_data.view.y.xyz, r_data.view.z.xyz)); + let unprojected = r_data.proj_inv * pos; var out: VertexOutput; out.uv = inv_model_view * unprojected.xyz; out.position = pos;