From b5a046982f97e27a93885dfcfa624c29fbcaf3dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Capucho?= Date: Tue, 7 Jun 2022 15:58:37 +0100 Subject: [PATCH] glsl-out: don't rely on cached expressions Expressions marked for caching might not be cached, this can happen for example when these expressions are pre emitted (for example Contants). --- src/back/glsl/mod.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) 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"