diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index 9e534df539..cba3b3ca0a 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -1526,20 +1526,30 @@ impl<'a, W: Write> Writer<'a, W> { arg: Handle, arg1: Handle, size: usize, + ctx: &back::FunctionCtx<'_>, ) -> BackendResult { + // Write parantheses around the dot product expression to prevent operators + // with different precedences from applying earlier. write!(self.out, "(")?; - let arg0_name = &self.named_expressions[&arg]; - let arg1_name = &self.named_expressions[&arg1]; - - // This will print an extra '+' at the beginning but that is fine in glsl + // Cycle trough all the components of the vector for index in 0..size { let component = back::COMPONENTS[index]; - write!( - self.out, - " + {}.{} * {}.{}", - arg0_name, component, arg1_name, component - )?; + // Write the addition to the previous product + // This will print an extra '+' at the beginning but that is fine in glsl + write!(self.out, " + ")?; + // Write the first vector expression, this expression is marked to be + // cached so unless it can't be cached (for example, it's a Constant) + // it shouldn't produce large expressions. + self.write_expr(arg, ctx)?; + // Access the current component on the first vector + write!(self.out, ".{} * ", component)?; + // Write the second vector expression, this expression is marked to be + // cached so unless it can't be cached (for example, it's a Constant) + // it shouldn't produce large expressions. + self.write_expr(arg1, ctx)?; + // Access the current component on the second vector + write!(self.out, ".{}", component)?; } write!(self.out, ")")?; @@ -2726,7 +2736,7 @@ impl<'a, W: Write> Writer<'a, W> { .. } => "dot", crate::TypeInner::Vector { size, .. } => { - return self.write_dot_product(arg, arg1.unwrap(), size as usize) + return self.write_dot_product(arg, arg1.unwrap(), size as usize, ctx) } _ => unreachable!( "Correct TypeInner for dot product should be already validated"