diff --git a/src/front/glsl/ast.rs b/src/front/glsl/ast.rs index d1ba273d3..43d62f901 100644 --- a/src/front/glsl/ast.rs +++ b/src/front/glsl/ast.rs @@ -883,6 +883,25 @@ impl<'function> Context<'function> { Ok(()) } + + pub fn implicit_splat( + &mut self, + program: &mut Program, + expr: &mut Handle, + meta: SourceMetadata, + vector_size: Option, + ) -> Result<(), ErrorKind> { + let expr_type = program.resolve_type(self, *expr, meta)?; + + if let (&TypeInner::Scalar { .. }, Some(size)) = (expr_type, vector_size) { + *expr = self.expressions.append(Expression::Splat { + size, + value: *expr, + }) + } + + Ok(()) + } } pub fn type_power(kind: ScalarKind) -> Option { diff --git a/src/front/glsl/functions.rs b/src/front/glsl/functions.rs index f8dfe6f42..509e96452 100644 --- a/src/front/glsl/functions.rs +++ b/src/front/glsl/functions.rs @@ -444,14 +444,40 @@ impl Program<'_> { }, )) } - "clamp" | "faceforward" | "refract" | "fma" | "smoothstep" => { + "clamp" => { + if args.len() != 3 { + return Err(ErrorKind::wrong_function_args(name, 3, args.len(), meta)); + } + + let (arg0, arg0_meta) = args[0]; + let vector_size = match *(self.resolve_type(ctx, arg0, arg0_meta)?) { + TypeInner::Vector{ size, .. } => Some(size), + _ => None, + }; + + let (mut arg1, arg1_meta) = args[1]; + ctx.implicit_splat(self, &mut arg1, arg1_meta, vector_size)?; + + let (mut arg2, arg2_meta) = args[2]; + ctx.implicit_splat(self, &mut arg2, arg2_meta, vector_size)?; + + Ok(Some(ctx.add_expression( + Expression::Math { + fun: MathFunction::Clamp, + arg: arg0, + arg1: Some(arg1), + arg2: Some(arg2), + }, + body, + ))) + } + "faceforward" | "refract" | "fma" | "smoothstep" => { if args.len() != 3 { return Err(ErrorKind::wrong_function_args(name, 3, args.len(), meta)); } Ok(Some(ctx.add_expression( Expression::Math { fun: match name.as_str() { - "clamp" => MathFunction::Clamp, "faceforward" => MathFunction::FaceForward, "refract" => MathFunction::Refract, "fma" => MathFunction::Fma, diff --git a/tests/in/glsl/clamp-splat.vert b/tests/in/glsl/clamp-splat.vert new file mode 100644 index 000000000..70ade6098 --- /dev/null +++ b/tests/in/glsl/clamp-splat.vert @@ -0,0 +1,6 @@ +#version 450 +layout(location = 0) in vec2 a_pos; + +void main() { + gl_Position = vec4(clamp(a_pos, 0.0, 1.0), 0.0, 1.0); +} diff --git a/tests/out/wgsl/clamp-splat-vert.wgsl b/tests/out/wgsl/clamp-splat-vert.wgsl new file mode 100644 index 000000000..08f00587b --- /dev/null +++ b/tests/out/wgsl/clamp-splat-vert.wgsl @@ -0,0 +1,20 @@ +struct VertexOutput { + [[builtin(position)]] member: vec4; +}; + +var a_pos1: vec2; +var gl_Position: vec4; + +fn main1() { + let _e2: vec2 = a_pos1; + gl_Position = vec4(clamp(_e2, vec2(0.0), vec2(1.0)), 0.0, 1.0); + return; +} + +[[stage(vertex)]] +fn main([[location(0), interpolate(perspective)]] a_pos: vec2) -> VertexOutput { + a_pos1 = a_pos; + main1(); + let _e3: vec4 = gl_Position; + return VertexOutput(_e3); +}