Skip to content

Commit

Permalink
[naga] Let constant evaluation of As preserve Splat expressions.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimblandy committed Nov 18, 2023
1 parent 666f681 commit fd53ea9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
26 changes: 25 additions & 1 deletion naga/src/proc/constant_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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),
};

Expand Down
2 changes: 1 addition & 1 deletion naga/tests/out/wgsl/900-implicit-conversions.frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit fd53ea9

Please sign in to comment.