From 24eaad10fc481e4bb0b4438941d40cabffb8c145 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Thu, 22 Apr 2021 11:17:44 -0400 Subject: [PATCH] Follow up on the swizzle PR review notes (#750) --- src/back/glsl/mod.rs | 2 +- src/front/glsl/constants.rs | 38 ++++++++++++++++++------------------- src/lib.rs | 9 +++++---- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index 002777f5e7..a26865c2c2 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -1522,7 +1522,7 @@ impl<'a, W: Write> Writer<'a, W> { self.write_expr(vector, ctx)?; write!(self.out, ".")?; for &sc in pattern[..size as usize].iter() { - write!(self.out, "{}", COMPONENTS[sc as usize])?; + self.out.write_char(COMPONENTS[sc as usize])?; } } // `Compose` is pretty simple we just write `type(components)` where `components` is a diff --git a/src/front/glsl/constants.rs b/src/front/glsl/constants.rs index 82221f527c..848fa40c1c 100644 --- a/src/front/glsl/constants.rs +++ b/src/front/glsl/constants.rs @@ -70,8 +70,8 @@ impl<'a> ConstantSolver<'a> { size, value: splat_value, } => { - let tgt = self.solve(splat_value)?; - let ty = match self.constants[tgt].inner { + let value_constant = self.solve(splat_value)?; + let ty = match self.constants[value_constant].inner { ConstantInner::Scalar { ref value, width } => { let kind = value.scalar_kind(); self.types @@ -79,13 +79,15 @@ impl<'a> ConstantSolver<'a> { } ConstantInner::Composite { .. } => None, }; + + //TODO: register the new type if needed + let ty = ty.ok_or(ConstantSolvingError::DestinationTypeNotFound)?; Ok(self.constants.fetch_or_append(Constant { name: None, specialization: None, inner: ConstantInner::Composite { - //TODO: register the new type if needed - ty: ty.ok_or(ConstantSolvingError::DestinationTypeNotFound)?, - components: vec![tgt; size as usize], + ty, + components: vec![value_constant; size as usize], }, })) } @@ -94,8 +96,8 @@ impl<'a> ConstantSolver<'a> { vector: src_vector, pattern, } => { - let tgt = self.solve(src_vector)?; - let (ty, src_components) = match self.constants[tgt].inner { + let src_constant = self.solve(src_vector)?; + let (ty, src_components) = match self.constants[src_constant].inner { ConstantInner::Scalar { .. } => (None, &[][..]), ConstantInner::Composite { ty, @@ -114,6 +116,8 @@ impl<'a> ConstantSolver<'a> { _ => (None, &[][..]), }, }; + //TODO: register the new type if needed + let ty = ty.ok_or(ConstantSolvingError::DestinationTypeNotFound)?; let components = pattern .iter() .map(|&sc| src_components[sc as usize]) @@ -122,11 +126,7 @@ impl<'a> ConstantSolver<'a> { Ok(self.constants.fetch_or_append(Constant { name: None, specialization: None, - inner: ConstantInner::Composite { - //TODO: register the new type if needed - ty: ty.ok_or(ConstantSolvingError::DestinationTypeNotFound)?, - components, - }, + inner: ConstantInner::Composite { ty, components }, })) } Expression::Compose { ty, ref components } => { @@ -142,15 +142,15 @@ impl<'a> ConstantSolver<'a> { })) } Expression::Unary { expr, op } => { - let tgt = self.solve(expr)?; + let expr_constant = self.solve(expr)?; - self.unary_op(op, tgt) + self.unary_op(op, expr_constant) } Expression::Binary { left, right, op } => { - let left = self.solve(left)?; - let right = self.solve(right)?; + let left_constant = self.solve(left)?; + let right_constant = self.solve(right)?; - self.binary_op(op, left, right) + self.binary_op(op, left_constant, right_constant) } Expression::Math { .. } => todo!(), Expression::As { @@ -158,10 +158,10 @@ impl<'a> ConstantSolver<'a> { expr, kind, } => { - let tgt = self.solve(expr)?; + let expr_constant = self.solve(expr)?; if convert { - self.cast(tgt, kind) + self.cast(expr_constant, kind) } else { Err(ConstantSolvingError::Bitcast) } diff --git a/src/lib.rs b/src/lib.rs index f6a4b59f0c..ce0897ae37 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -680,18 +680,19 @@ pub enum ImageQuery { } /// Component selection for a vector swizzle. +#[repr(u8)] #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "serialize", derive(Serialize))] #[cfg_attr(feature = "deserialize", derive(Deserialize))] pub enum SwizzleComponent { /// - X, + X = 0, /// - Y, + Y = 1, /// - Z, + Z = 2, /// - W, + W = 3, } /// An expression that can be evaluated to obtain a value.