[naga] Let constant evaluation of As preserve Splat expressions.

When asked to evaluate an `Expression::As` cast applied to a `Splat`
expression, change `ConstantEvaluator::cast` to preserve the `Splat`,
rather than expanding it out to a `Compose` expression.
This commit is contained in:
Jim Blandy
2023-11-17 14:23:20 -08:00
parent 666f681dae
commit fd53ea90e6
2 changed files with 26 additions and 2 deletions

View File

@@ -863,6 +863,22 @@ impl<'a> ConstantEvaluator<'a> {
}
}
/// Lower [`ZeroValue`] expressions to [`Literal`] and [`Compose`] expressions.
///
/// [`ZeroValue`]: Expression::ZeroValue
/// [`Literal`]: Expression::Literal
/// [`Compose`]: Expression::Compose
fn eval_zero_value(
&mut self,
expr: Handle<Expression>,
span: Span,
) -> Result<Handle<Expression>, ConstantEvaluatorError> {
match self.expressions[expr] {
Expression::ZeroValue(ty) => self.eval_zero_value_impl(ty, span),
_ => Ok(expr),
}
}
/// Lower [`ZeroValue`] expressions to [`Literal`] and [`Compose`] expressions.
///
/// [`ZeroValue`]: Expression::ZeroValue
@@ -953,7 +969,7 @@ impl<'a> ConstantEvaluator<'a> {
) -> Result<Handle<Expression>, ConstantEvaluatorError> {
use crate::Scalar as Sc;
let expr = self.eval_zero_value_and_splat(expr, span)?;
let expr = self.eval_zero_value(expr, span)?;
let expr = match self.expressions[expr] {
Expression::Literal(literal) => {
@@ -1022,6 +1038,14 @@ impl<'a> ConstantEvaluator<'a> {
Expression::Compose { ty, components }
}
Expression::Splat { size, value } => {
let value_span = self.expressions.get_span(value);
let cast_value = self.cast(value, target, value_span)?;
Expression::Splat {
size,
value: cast_value,
}
}
_ => return Err(ConstantEvaluatorError::InvalidCastArg),
};

View File

@@ -57,7 +57,7 @@ fn implicit_dims_3(v_6: vec4<f32>) {
fn main_1() {
exact_1(1);
implicit(1.0);
implicit_dims_2(vec3<f32>(1.0, 1.0, 1.0));
implicit_dims_2(vec3(1.0));
return;
}