diff --git a/src/front/glsl/context.rs b/src/front/glsl/context.rs index fa29b5ceb9..eefef012b7 100644 --- a/src/front/glsl/context.rs +++ b/src/front/glsl/context.rs @@ -881,7 +881,7 @@ impl Context { pub fn expr_scalar_components( &mut self, - parser: &mut Parser, + parser: &Parser, expr: Handle, meta: Span, ) -> Result> { @@ -891,7 +891,7 @@ impl Context { pub fn expr_power( &mut self, - parser: &mut Parser, + parser: &Parser, expr: Handle, meta: Span, ) -> Result> { @@ -921,7 +921,7 @@ impl Context { pub fn implicit_conversion( &mut self, - parser: &mut Parser, + parser: &Parser, expr: &mut Handle, meta: Span, kind: ScalarKind, @@ -941,7 +941,7 @@ impl Context { pub fn binary_implicit_conversion( &mut self, - parser: &mut Parser, + parser: &Parser, left: &mut Handle, left_meta: Span, right: &mut Handle, @@ -974,7 +974,7 @@ impl Context { pub fn implicit_splat( &mut self, - parser: &mut Parser, + parser: &Parser, expr: &mut Handle, meta: Span, vector_size: Option, diff --git a/src/front/glsl/functions.rs b/src/front/glsl/functions.rs index fc9a0677ee..14f30bdfb7 100644 --- a/src/front/glsl/functions.rs +++ b/src/front/glsl/functions.rs @@ -195,14 +195,39 @@ impl Parser { (value, expr_meta), meta, )?, - TypeInner::Struct { .. } | TypeInner::Array { .. } => ctx.add_expression( - Expression::Compose { - ty, - components: vec![value], - }, - meta, - body, - ), + TypeInner::Struct { ref members, .. } => { + let scalar_components = members + .get(0) + .and_then(|member| scalar_components(&self.module.types[member.ty].inner)); + if let Some((kind, width)) = scalar_components { + ctx.implicit_conversion(self, &mut value, expr_meta, kind, width)?; + } + + ctx.add_expression( + Expression::Compose { + ty, + components: vec![value], + }, + meta, + body, + ) + } + + TypeInner::Array { base, .. } => { + let scalar_components = scalar_components(&self.module.types[base].inner); + if let Some((kind, width)) = scalar_components { + ctx.implicit_conversion(self, &mut value, expr_meta, kind, width)?; + } + + ctx.add_expression( + Expression::Compose { + ty, + components: vec![value], + }, + meta, + body, + ) + } _ => { self.errors.push(Error { kind: ErrorKind::SemanticError("Bad type constructor".into()), @@ -535,6 +560,26 @@ impl Parser { TypeInner::Vector { size, kind, width } => { return self.vector_constructor(ctx, body, ty, size, kind, width, &args, meta) } + TypeInner::Array { base, .. } => { + for (mut arg, meta) in args.iter().copied() { + let scalar_components = scalar_components(&self.module.types[base].inner); + if let Some((kind, width)) = scalar_components { + ctx.implicit_conversion(self, &mut arg, meta, kind, width)?; + } + + components.push(arg) + } + } + TypeInner::Struct { ref members, .. } => { + for ((mut arg, meta), member) in args.iter().copied().zip(members.iter()) { + let scalar_components = scalar_components(&self.module.types[member.ty].inner); + if let Some((kind, width)) = scalar_components { + ctx.implicit_conversion(self, &mut arg, meta, kind, width)?; + } + + components.push(arg) + } + } _ => { return Err(Error { kind: ErrorKind::SemanticError("Constructor: Too many arguments".into()), diff --git a/tests/in/glsl/declarations.vert b/tests/in/glsl/declarations.vert index e0c2ec6e8d..c8dab77954 100644 --- a/tests/in/glsl/declarations.vert +++ b/tests/in/glsl/declarations.vert @@ -10,5 +10,16 @@ layout(location = 0) out FragmentData { vec2 a; } frag; +struct TestStruct { + float a; + float b; +}; + + void main() { + const vec3 positions[2] = vec3[2]( + vec3(-1.0, 1.0, 0.0), + vec3(-1.0, -1.0, 0.0) + ); + const TestStruct strct = TestStruct( 1, 2 ); } diff --git a/tests/out/wgsl/declarations-vert.wgsl b/tests/out/wgsl/declarations-vert.wgsl index 952ca9dca8..654a1cc2ce 100644 --- a/tests/out/wgsl/declarations-vert.wgsl +++ b/tests/out/wgsl/declarations-vert.wgsl @@ -8,6 +8,11 @@ struct FragmentData { a: vec2; }; +struct TestStruct { + a: f32; + b: f32; +}; + struct VertexOutput { [[location(0)]] position: vec2; [[location(1)]] a: vec2; @@ -17,6 +22,9 @@ var vert: VertexData; var frag: FragmentData; 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); + } [[stage(vertex)]]