From c084e2cfa80cefdf672dc138113d346f6999004b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Capucho?= Date: Sun, 1 Aug 2021 18:35:46 +0100 Subject: [PATCH] [glsl-in] Handle Pow in constant evaluation --- src/front/glsl/constants.rs | 46 +++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/front/glsl/constants.rs b/src/front/glsl/constants.rs index 9f5036aa80..a0811d0c3f 100644 --- a/src/front/glsl/constants.rs +++ b/src/front/glsl/constants.rs @@ -49,6 +49,8 @@ pub enum ConstantSolvingError { InvalidUnaryOpArg, #[error("Cannot apply the binary op to the arguments")] InvalidBinaryOpArgs, + #[error("Cannot apply math function to type")] + InvalidMathArg, #[error("Splat/swizzle type is not registered")] DestinationTypeNotFound, #[error("Not implemented: {0}")] @@ -154,8 +156,48 @@ impl<'a> ConstantSolver<'a> { self.binary_op(op, left_constant, right_constant) } - Expression::Math { fun, .. } => { - Err(ConstantSolvingError::NotImplemented(format!("{:?}", fun))) + Expression::Math { fun, arg, arg1, .. } => { + let arg = self.solve(arg)?; + let arg1 = arg1.map(|arg| self.solve(arg)).transpose()?; + + let const0 = &self.constants[arg].inner; + let const1 = arg1.map(|arg| &self.constants[arg].inner); + + match fun { + crate::MathFunction::Pow => { + let (value, width) = match (const0, const1.unwrap()) { + ( + &ConstantInner::Scalar { + width, + value: value0, + }, + &ConstantInner::Scalar { value: value1, .. }, + ) => ( + match (value0, value1) { + (ScalarValue::Sint(a), ScalarValue::Sint(b)) => { + ScalarValue::Sint(a.pow(b as u32)) + } + (ScalarValue::Uint(a), ScalarValue::Uint(b)) => { + ScalarValue::Uint(a.pow(b as u32)) + } + (ScalarValue::Float(a), ScalarValue::Float(b)) => { + ScalarValue::Float(a.powf(b)) + } + _ => return Err(ConstantSolvingError::InvalidMathArg), + }, + width, + ), + _ => return Err(ConstantSolvingError::InvalidMathArg), + }; + + Ok(self.constants.fetch_or_append(Constant { + name: None, + specialization: None, + inner: ConstantInner::Scalar { width, value }, + })) + } + _ => Err(ConstantSolvingError::NotImplemented(format!("{:?}", fun))), + } } Expression::As { convert,