From c9bca441ba634012208e36c352349651f558ca92 Mon Sep 17 00:00:00 2001 From: Adeline Thompson Date: Mon, 15 Aug 2022 21:07:40 -0400 Subject: [PATCH 1/2] glsl-in: Fix missing stores for local declarations Previously, if a local variable was declared with a constant value, we would elide the store and instead give the variable an initial value (as if it was a global variable). This caused variables to not be re-initialized each time through a loop. --- src/front/glsl/parser/declarations.rs | 22 ++++------- src/front/glsl/variables.rs | 2 +- tests/out/wgsl/246-collatz-comp.wgsl | 4 +- tests/out/wgsl/277-casting-vert.wgsl | 5 ++- tests/out/wgsl/280-matrix-cast-vert.wgsl | 5 ++- tests/out/wgsl/901-lhs-field-select-vert.wgsl | 4 +- tests/out/wgsl/932-for-loop-if-vert.wgsl | 3 +- tests/out/wgsl/bevy-pbr-frag.wgsl | 21 +++++----- tests/out/wgsl/bits_glsl-frag.wgsl | 39 ++++++++++--------- tests/out/wgsl/constant-array-size-vert.wgsl | 7 ++-- tests/out/wgsl/declarations-vert.wgsl | 8 ++-- tests/out/wgsl/expressions-frag.wgsl | 24 +++++++----- tests/out/wgsl/long-form-matrix-vert.wgsl | 33 ++++++++-------- tests/out/wgsl/math-functions-vert.wgsl | 11 +++--- tests/out/wgsl/prepostfix-frag.wgsl | 11 +++--- tests/out/wgsl/sampler-functions-frag.wgsl | 3 +- tests/out/wgsl/swizzle_write-frag.wgsl | 4 +- 17 files changed, 107 insertions(+), 99 deletions(-) diff --git a/src/front/glsl/parser/declarations.rs b/src/front/glsl/parser/declarations.rs index a43f2fe772..2878b2f61b 100644 --- a/src/front/glsl/parser/declarations.rs +++ b/src/front/glsl/parser/declarations.rs @@ -190,22 +190,14 @@ impl<'source> ParsingContext<'source> { }) .transpose()?; - // If the declaration has an initializer try to make a constant out of it, - // this is only strictly needed for global constant declarations (and if the - // initializer can't be made a constant it should throw an error) but we also - // try to do it for all other types of declarations. - let maybe_constant = if let Some((root, meta)) = init { - let is_const = ctx.qualifiers.storage.0 == StorageQualifier::Const; - - match parser.solve_constant(ctx.ctx, root, meta) { - Ok(res) => Some(res), - // If the declaration is external (global scope) and is constant qualified - // then the initializer must be a constant expression - Err(err) if ctx.external && is_const => return Err(err), - _ => None, + // If the variable is global and const qualified, solve the initializer for a constant + // and use that as the variable's initial value. + let is_const = ctx.qualifiers.storage.0 == StorageQualifier::Const; + let maybe_constant = match (init, ctx.external, is_const) { + (Some((root, meta)), true, true) => { + Some(parser.solve_constant(ctx.ctx, root, meta)?) } - } else { - None + _ => None, }; let pointer = ctx.add_var(parser, ty, name, maybe_constant, meta)?; diff --git a/src/front/glsl/variables.rs b/src/front/glsl/variables.rs index 8375e1674f..6aa20ce289 100644 --- a/src/front/glsl/variables.rs +++ b/src/front/glsl/variables.rs @@ -643,7 +643,7 @@ impl Parser { LocalVariable { name: decl.name.clone(), ty: decl.ty, - init: decl.init, + init: None, }, decl.meta, ); diff --git a/tests/out/wgsl/246-collatz-comp.wgsl b/tests/out/wgsl/246-collatz-comp.wgsl index 65f47ceb39..ffd05885ec 100644 --- a/tests/out/wgsl/246-collatz-comp.wgsl +++ b/tests/out/wgsl/246-collatz-comp.wgsl @@ -8,11 +8,11 @@ var gl_GlobalInvocationID: vec3; fn collatz_iterations(n: u32) -> u32 { var n_1: u32; - var i: u32 = 0u; + var i: u32; _ = (&global.indices); n_1 = n; - _ = u32(0); + i = u32(0); loop { let _e7 = n_1; if !((_e7 != u32(1))) { diff --git a/tests/out/wgsl/277-casting-vert.wgsl b/tests/out/wgsl/277-casting-vert.wgsl index 13c86353bd..045edd83f3 100644 --- a/tests/out/wgsl/277-casting-vert.wgsl +++ b/tests/out/wgsl/277-casting-vert.wgsl @@ -1,7 +1,8 @@ fn main_1() { - var a: f32 = 1.0; + var a: f32; - _ = f32(1); + a = f32(1); + return; } @vertex diff --git a/tests/out/wgsl/280-matrix-cast-vert.wgsl b/tests/out/wgsl/280-matrix-cast-vert.wgsl index 7c34f07563..86df4628e3 100644 --- a/tests/out/wgsl/280-matrix-cast-vert.wgsl +++ b/tests/out/wgsl/280-matrix-cast-vert.wgsl @@ -1,8 +1,9 @@ fn main_1() { - var a: mat4x4 = mat4x4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0)); + var a: mat4x4; let _e1 = f32(1); - _ = mat4x4(vec4(_e1, 0.0, 0.0, 0.0), vec4(0.0, _e1, 0.0, 0.0), vec4(0.0, 0.0, _e1, 0.0), vec4(0.0, 0.0, 0.0, _e1)); + a = mat4x4(vec4(_e1, 0.0, 0.0, 0.0), vec4(0.0, _e1, 0.0, 0.0), vec4(0.0, 0.0, _e1, 0.0), vec4(0.0, 0.0, 0.0, _e1)); + return; } @vertex diff --git a/tests/out/wgsl/901-lhs-field-select-vert.wgsl b/tests/out/wgsl/901-lhs-field-select-vert.wgsl index 8f30bc8aae..1b41758097 100644 --- a/tests/out/wgsl/901-lhs-field-select-vert.wgsl +++ b/tests/out/wgsl/901-lhs-field-select-vert.wgsl @@ -1,7 +1,7 @@ fn main_1() { - var a: vec4 = vec4(1.0, 1.0, 1.0, 1.0); + var a: vec4; - _ = vec4(1.0); + a = vec4(1.0); a.x = 2.0; return; } diff --git a/tests/out/wgsl/932-for-loop-if-vert.wgsl b/tests/out/wgsl/932-for-loop-if-vert.wgsl index 2d599cca85..7fdd3c6135 100644 --- a/tests/out/wgsl/932-for-loop-if-vert.wgsl +++ b/tests/out/wgsl/932-for-loop-if-vert.wgsl @@ -1,6 +1,7 @@ fn main_1() { - var i: i32 = 0; + var i: i32; + i = 0; loop { let _e2 = i; if !((_e2 < 1)) { diff --git a/tests/out/wgsl/bevy-pbr-frag.wgsl b/tests/out/wgsl/bevy-pbr-frag.wgsl index 339012853b..8c48bd9ae5 100644 --- a/tests/out/wgsl/bevy-pbr-frag.wgsl +++ b/tests/out/wgsl/bevy-pbr-frag.wgsl @@ -448,8 +448,8 @@ fn EnvBRDFApprox(f0_7: vec3, perceptual_roughness: f32, NoV_6: f32) -> vec3 var f0_8: vec3; var perceptual_roughness_1: f32; var NoV_7: f32; - var c0_: vec4 = vec4(-1.0, -0.027499999850988388, -0.5720000267028809, 0.02199999988079071); - var c1_: vec4 = vec4(1.0, 0.042500000447034836, 1.0399999618530273, -0.03999999910593033); + var c0_: vec4; + var c1_: vec4; var r: vec4; var a004_: f32; var AB: vec2; @@ -468,8 +468,8 @@ fn EnvBRDFApprox(f0_7: vec3, perceptual_roughness: f32, NoV_6: f32) -> vec3 f0_8 = f0_7; perceptual_roughness_1 = perceptual_roughness; NoV_7 = NoV_6; - _ = vec4(-(1.0), -(0.027499999850988388), -(0.5720000267028809), 0.02199999988079071); - _ = vec4(1.0, 0.042500000447034836, 1.0399999618530273, -(0.03999999910593033)); + c0_ = vec4(-(1.0), -(0.027499999850988388), -(0.5720000267028809), 0.02199999988079071); + c1_ = vec4(1.0, 0.042500000447034836, 1.0399999618530273, -(0.03999999910593033)); let _e62 = perceptual_roughness_1; let _e64 = c0_; let _e66 = c1_; @@ -939,7 +939,7 @@ fn dir_light(light_2: DirectionalLight, roughness_10: f32, NdotV_2: f32, normal: var NoH_5: f32; var LoH_7: f32; var diffuse_1: vec3; - var specularIntensity_3: f32 = 1.0; + var specularIntensity_3: f32; var specular_2: vec3; _ = (&global.ViewProj); @@ -1010,6 +1010,7 @@ fn dir_light(light_2: DirectionalLight, roughness_10: f32, NdotV_2: f32, normal: let _e124 = LoH_7; let _e125 = Fd_Burley(_e121, _e122, _e123, _e124); diffuse_1 = (_e116 * _e125); + specularIntensity_3 = 1.0; _ = F0_3; _ = roughness_11; _ = half_vector; @@ -1055,9 +1056,9 @@ fn main_1() { var F0_4: vec3; var diffuseColor_4: vec3; var R_4: vec3; - var light_accum: vec3 = vec3(0.0, 0.0, 0.0); - var i: i32 = 0; - var i_1: i32 = 0; + var light_accum: vec3; + var i: i32; + var i_1: i32; var diffuse_ambient: vec3; var specular_ambient: vec3; @@ -1186,7 +1187,8 @@ fn main_1() { let _e217 = V_3; let _e219 = N_2; R_4 = reflect(-(_e217), _e219); - _ = vec3(0.0); + light_accum = vec3(0.0); + i = 0; loop { let _e227 = i; let _e228 = global_2.NumLights; @@ -1222,6 +1224,7 @@ fn main_1() { i = (_e236 + 1); } } + i_1 = 0; loop { let _e264 = i_1; let _e265 = global_2.NumLights; diff --git a/tests/out/wgsl/bits_glsl-frag.wgsl b/tests/out/wgsl/bits_glsl-frag.wgsl index 8e1fb15f4f..b68253a277 100644 --- a/tests/out/wgsl/bits_glsl-frag.wgsl +++ b/tests/out/wgsl/bits_glsl-frag.wgsl @@ -1,24 +1,25 @@ fn main_1() { - var i: i32 = 0; - var i2_: vec2 = vec2(0, 0); - var i3_: vec3 = vec3(0, 0, 0); - var i4_: vec4 = vec4(0, 0, 0, 0); - var u: u32 = 0u; - var u2_: vec2 = vec2(0u, 0u); - var u3_: vec3 = vec3(0u, 0u, 0u); - var u4_: vec4 = vec4(0u, 0u, 0u, 0u); - var f2_: vec2 = vec2(0.0, 0.0); - var f4_: vec4 = vec4(0.0, 0.0, 0.0, 0.0); + var i: i32; + var i2_: vec2; + var i3_: vec3; + var i4_: vec4; + var u: u32; + var u2_: vec2; + var u3_: vec3; + var u4_: vec4; + var f2_: vec2; + var f4_: vec4; - _ = vec2(0); - _ = vec3(0); - _ = vec4(0); - _ = u32(0); - _ = vec2(u32(0)); - _ = vec3(u32(0)); - _ = vec4(u32(0)); - _ = vec2(0.0); - _ = vec4(0.0); + i = 0; + i2_ = vec2(0); + i3_ = vec3(0); + i4_ = vec4(0); + u = u32(0); + u2_ = vec2(u32(0)); + u3_ = vec3(u32(0)); + u4_ = vec4(u32(0)); + f2_ = vec2(0.0); + f4_ = vec4(0.0); _ = f4_; let _e33 = f4_; u = pack4x8snorm(_e33); diff --git a/tests/out/wgsl/constant-array-size-vert.wgsl b/tests/out/wgsl/constant-array-size-vert.wgsl index ba193b7c56..f2f7f9d24d 100644 --- a/tests/out/wgsl/constant-array-size-vert.wgsl +++ b/tests/out/wgsl/constant-array-size-vert.wgsl @@ -6,10 +6,11 @@ struct Data { var global: Data; fn function_() -> vec4 { - var sum: vec4 = vec4(0.0, 0.0, 0.0, 0.0); - var i: i32 = 0; + var sum: vec4; + var i: i32; - _ = vec4(f32(0)); + sum = vec4(f32(0)); + i = 0; loop { let _e9 = i; if !((_e9 < 42)) { diff --git a/tests/out/wgsl/declarations-vert.wgsl b/tests/out/wgsl/declarations-vert.wgsl index f0ab4c0fa2..4b61704b96 100644 --- a/tests/out/wgsl/declarations-vert.wgsl +++ b/tests/out/wgsl/declarations-vert.wgsl @@ -28,8 +28,8 @@ var array_2d: array,2u>; var array_toomanyd: array,2u>,2u>,2u>,2u>,2u>,2u>; fn main_1() { - var positions: array,2u> = array,2u>(vec3(-1.0, 1.0, 0.0), vec3(-1.0, -1.0, 0.0)); - var strct: TestStruct = TestStruct(1.0, 2.0); + var positions: array,2u>; + var strct: TestStruct; var from_input_array: vec4; var a_1: f32; var b: f32; @@ -38,8 +38,8 @@ fn main_1() { _ = (&vert.a); _ = (&frag.position); _ = (&frag.a); - _ = array,2u>(vec3(-(1.0), 1.0, 0.0), vec3(-(1.0), -(1.0), 0.0)); - _ = TestStruct(f32(1), f32(2)); + positions = array,2u>(vec3(-(1.0), 1.0, 0.0), vec3(-(1.0), -(1.0), 0.0)); + strct = TestStruct(f32(1), f32(2)); let _e35 = in_array_2[1]; from_input_array = _e35; let _e41 = array_2d[0][0]; diff --git a/tests/out/wgsl/expressions-frag.wgsl b/tests/out/wgsl/expressions-frag.wgsl index 5c8a3de5fd..f92bb53a58 100644 --- a/tests/out/wgsl/expressions-frag.wgsl +++ b/tests/out/wgsl/expressions-frag.wgsl @@ -274,21 +274,24 @@ fn testUnaryOpMat(a_16: mat3x3) { } fn testStructConstructor() { - var tree: BST = BST(1); + var tree: BST; - _ = BST(1); + tree = BST(1); + return; } fn testNonScalarToScalarConstructor() { - var f: f32 = 1.0; + var f: f32; - _ = f32(mat2x2(vec2(1.0, 0.0), vec2(0.0, 1.0))[0].x); + f = f32(mat2x2(vec2(1.0, 0.0), vec2(0.0, 1.0))[0].x); + return; } fn testArrayConstructor() { - var tree_1: array = array(0.0); + var tree_1: array; - _ = array(0.0); + tree_1 = array(0.0); + return; } fn testFreestandingConstructor() { @@ -296,10 +299,10 @@ fn testFreestandingConstructor() { } fn testNonImplicitCastVectorCast() { - var a_18: u32 = 1u; + var a_18: u32; var b_16: vec4; - _ = u32(1); + a_18 = u32(1); let _e3 = a_18; b_16 = vec4(i32(_e3)); return; @@ -384,12 +387,13 @@ fn testLength() { fn testConstantLength(a_24: array) { var a_25: array; - var len_1: i32 = 4; + var len_1: i32; _ = (&global_1.a); a_25 = a_24; _ = a_25; - _ = i32(4u); + len_1 = i32(4u); + return; } fn main_1() { diff --git a/tests/out/wgsl/long-form-matrix-vert.wgsl b/tests/out/wgsl/long-form-matrix-vert.wgsl index 8ce67c709b..5048a419a1 100644 --- a/tests/out/wgsl/long-form-matrix-vert.wgsl +++ b/tests/out/wgsl/long-form-matrix-vert.wgsl @@ -1,36 +1,37 @@ fn main_1() { - var splat: mat2x2 = mat2x2(vec2(1.0, 0.0), vec2(0.0, 1.0)); - var normal: mat2x2 = mat2x2(vec2(1.0, 1.0), vec2(2.0, 2.0)); - var from_matrix: mat2x4 = mat2x4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0)); - var a: mat2x2 = mat2x2(vec2(1.0, 2.0), vec2(3.0, 4.0)); - var b: mat2x2 = mat2x2(vec2(1.0, 2.0), vec2(3.0, 4.0)); - var c: mat3x3 = mat3x3(vec3(1.0, 2.0, 3.0), vec3(1.0, 1.0, 1.0), vec3(1.0, 1.0, 1.0)); - var d: mat3x3 = mat3x3(vec3(2.0, 2.0, 1.0), vec3(1.0, 1.0, 1.0), vec3(1.0, 1.0, 1.0)); - var e: mat4x4 = mat4x4(vec4(2.0, 2.0, 1.0, 1.0), vec4(1.0, 1.0, 2.0, 2.0), vec4(1.0, 1.0, 1.0, 1.0), vec4(1.0, 1.0, 1.0, 1.0)); + var splat: mat2x2; + var normal: mat2x2; + var from_matrix: mat2x4; + var a: mat2x2; + var b: mat2x2; + var c: mat3x3; + var d: mat3x3; + var e: mat4x4; let _e1 = f32(1); - _ = mat2x2(vec2(_e1, 0.0), vec2(0.0, _e1)); + splat = mat2x2(vec2(_e1, 0.0), vec2(0.0, _e1)); let _e9 = vec2(f32(1)); let _e12 = vec2(f32(2)); - _ = mat2x2(vec2(_e9.x, _e9.y), vec2(_e12.x, _e12.y)); + normal = mat2x2(vec2(_e9.x, _e9.y), vec2(_e12.x, _e12.y)); let _e26 = mat3x3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0)); - _ = mat2x4(vec4(_e26[0].x, _e26[0].y, _e26[0].z, 0.0), vec4(_e26[1].x, _e26[1].y, _e26[1].z, 0.0)); - _ = mat2x2(vec2(f32(1), f32(2)), vec2(f32(3), f32(4))); + from_matrix = mat2x4(vec4(_e26[0].x, _e26[0].y, _e26[0].z, 0.0), vec4(_e26[1].x, _e26[1].y, _e26[1].z, 0.0)); + a = mat2x2(vec2(f32(1), f32(2)), vec2(f32(3), f32(4))); let _e58 = vec2(f32(2), f32(3)); - _ = mat2x2(vec2(f32(1), _e58.x), vec2(_e58.y, f32(4))); + b = mat2x2(vec2(f32(1), _e58.x), vec2(_e58.y, f32(4))); let _e73 = vec3(f32(1)); let _e76 = vec3(f32(1)); - _ = mat3x3(vec3(f32(1), f32(2), f32(3)), vec3(_e73.x, _e73.y, _e73.z), vec3(_e76.x, _e76.y, _e76.z)); + c = mat3x3(vec3(f32(1), f32(2), f32(3)), vec3(_e73.x, _e73.y, _e73.z), vec3(_e76.x, _e76.y, _e76.z)); let _e93 = vec2(f32(2)); let _e97 = vec3(f32(1)); let _e100 = vec3(f32(1)); - _ = mat3x3(vec3(_e93.x, _e93.y, f32(1)), vec3(_e97.x, _e97.y, _e97.z), vec3(_e100.x, _e100.y, _e100.z)); + d = mat3x3(vec3(_e93.x, _e93.y, f32(1)), vec3(_e97.x, _e97.y, _e97.z), vec3(_e100.x, _e100.y, _e100.z)); let _e117 = vec2(f32(2)); let _e120 = vec4(f32(1)); let _e123 = vec2(f32(2)); let _e126 = vec4(f32(1)); let _e129 = vec4(f32(1)); - _ = mat4x4(vec4(_e117.x, _e117.y, _e120.x, _e120.y), vec4(_e120.z, _e120.w, _e123.x, _e123.y), vec4(_e126.x, _e126.y, _e126.z, _e126.w), vec4(_e129.x, _e129.y, _e129.z, _e129.w)); + e = mat4x4(vec4(_e117.x, _e117.y, _e120.x, _e120.y), vec4(_e120.z, _e120.w, _e123.x, _e123.y), vec4(_e126.x, _e126.y, _e126.z, _e126.w), vec4(_e129.x, _e129.y, _e129.z, _e129.w)); + return; } @vertex diff --git a/tests/out/wgsl/math-functions-vert.wgsl b/tests/out/wgsl/math-functions-vert.wgsl index 58e95f6737..0d499fc58a 100644 --- a/tests/out/wgsl/math-functions-vert.wgsl +++ b/tests/out/wgsl/math-functions-vert.wgsl @@ -1,8 +1,8 @@ fn main_1() { - var a: vec4 = vec4(1.0, 1.0, 1.0, 1.0); - var b: vec4 = vec4(2.0, 2.0, 2.0, 2.0); + var a: vec4; + var b: vec4; var m: mat4x4; - var i: i32 = 5; + var i: i32; var ceilOut: vec4; var roundOut: vec4; var floorOut: vec4; @@ -48,13 +48,14 @@ fn main_1() { var smoothStepVector: vec4; var smoothStepMixed: vec4; - _ = vec4(1.0); - _ = vec4(2.0); + a = vec4(1.0); + b = vec4(2.0); let _e6 = a; let _e7 = b; let _e8 = a; let _e9 = b; m = mat4x4(vec4(_e6.x, _e6.y, _e6.z, _e6.w), vec4(_e7.x, _e7.y, _e7.z, _e7.w), vec4(_e8.x, _e8.y, _e8.z, _e8.w), vec4(_e9.x, _e9.y, _e9.z, _e9.w)); + i = 5; _ = a; let _e35 = a; ceilOut = ceil(_e35); diff --git a/tests/out/wgsl/prepostfix-frag.wgsl b/tests/out/wgsl/prepostfix-frag.wgsl index f020792af9..72347bfd64 100644 --- a/tests/out/wgsl/prepostfix-frag.wgsl +++ b/tests/out/wgsl/prepostfix-frag.wgsl @@ -1,11 +1,12 @@ fn main_1() { var scalar_target: i32; - var scalar: i32 = 1; + var scalar: i32; var vec_target: vec2; - var vec_: vec2 = vec2(1u, 1u); + var vec_: vec2; var mat_target: mat4x3; - var mat_: mat4x3 = mat4x3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, 0.0)); + var mat_: mat4x3; + scalar = 1; let _e3 = scalar; scalar = (_e3 + 1); scalar_target = _e3; @@ -13,7 +14,7 @@ fn main_1() { let _e8 = (_e6 - 1); scalar = _e8; scalar_target = _e8; - _ = vec2(u32(1)); + vec_ = vec2(u32(1)); let _e14 = vec_; vec_ = (_e14 - vec2(1u)); vec_target = _e14; @@ -22,7 +23,7 @@ fn main_1() { vec_ = _e21; vec_target = _e21; let _e24 = f32(1); - _ = mat4x3(vec3(_e24, 0.0, 0.0), vec3(0.0, _e24, 0.0), vec3(0.0, 0.0, _e24), vec3(0.0, 0.0, 0.0)); + mat_ = mat4x3(vec3(_e24, 0.0, 0.0), vec3(0.0, _e24, 0.0), vec3(0.0, 0.0, _e24), vec3(0.0, 0.0, 0.0)); let _e32 = mat_; let _e34 = vec3(1.0); mat_ = (_e32 + mat4x3(_e34, _e34, _e34, _e34)); diff --git a/tests/out/wgsl/sampler-functions-frag.wgsl b/tests/out/wgsl/sampler-functions-frag.wgsl index e9e4830eea..805dc8f7f1 100644 --- a/tests/out/wgsl/sampler-functions-frag.wgsl +++ b/tests/out/wgsl/sampler-functions-frag.wgsl @@ -1,8 +1,9 @@ fn CalcShadowPCF1_(T_P_t_TextureDepth: texture_depth_2d, S_P_t_TextureDepth: sampler_comparison, t_ProjCoord: vec3) -> f32 { var t_ProjCoord_1: vec3; - var t_Res: f32 = 0.0; + var t_Res: f32; t_ProjCoord_1 = t_ProjCoord; + t_Res = 0.0; let _e6 = t_Res; let _e7 = t_ProjCoord_1; _ = _e7.xyz; diff --git a/tests/out/wgsl/swizzle_write-frag.wgsl b/tests/out/wgsl/swizzle_write-frag.wgsl index a6d7da9e93..e6b29a58ec 100644 --- a/tests/out/wgsl/swizzle_write-frag.wgsl +++ b/tests/out/wgsl/swizzle_write-frag.wgsl @@ -3,10 +3,10 @@ fn foo(p: ptr>) { } fn main_1() { - var x: vec3 = vec3(2.0, 2.0, 2.0); + var x: vec3; var local: vec2; - _ = vec3(2.0); + x = vec3(2.0); let _e3 = x; _ = _e3.zxy; _ = _e3.zx; From 473198b9fbc0652e290c5893ce7ae0032b5d9c5c Mon Sep 17 00:00:00 2001 From: Adeline Thompson Date: Sun, 21 Aug 2022 18:11:22 -0400 Subject: [PATCH 2/2] Solve constants for non-const globals globals --- src/front/glsl/parser/declarations.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/front/glsl/parser/declarations.rs b/src/front/glsl/parser/declarations.rs index 2878b2f61b..346d20b19e 100644 --- a/src/front/glsl/parser/declarations.rs +++ b/src/front/glsl/parser/declarations.rs @@ -190,14 +190,21 @@ impl<'source> ParsingContext<'source> { }) .transpose()?; - // If the variable is global and const qualified, solve the initializer for a constant - // and use that as the variable's initial value. let is_const = ctx.qualifiers.storage.0 == StorageQualifier::Const; - let maybe_constant = match (init, ctx.external, is_const) { - (Some((root, meta)), true, true) => { - Some(parser.solve_constant(ctx.ctx, root, meta)?) + let maybe_constant = if ctx.external { + if let Some((root, meta)) = init { + match parser.solve_constant(ctx.ctx, root, meta) { + Ok(res) => Some(res), + // If the declaration is external (global scope) and is constant qualified + // then the initializer must be a constant expression + Err(err) if is_const => return Err(err), + _ => None, + } + } else { + None } - _ => None, + } else { + None }; let pointer = ctx.add_var(parser, ty, name, maybe_constant, meta)?;