[naga wgsl-in] Ensure constant evaluation correctly handles Composes of vector ZeroValues #7138
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Connections
Fixes #7137
Description
Constant evaluation often relies on the expressions being evaluated being either a Literal, or a vector Compose for which it can iterate over each component.
Currently this is achieved by calling two functions:
eval_zero_value_and_splat() which transforms a scalar ZeroValue into a Literal, or a Splat or vector ZeroValue into a Compose of Literals.
proc::flatten_compose() takes potentially nested nested Compose and Splat expressions and produces an flat iterator that yields each component. eg
vec3(vec2(0), 0)
would yieldLiteral(0), Literal(0), Literal(0)
.For component-wise vector operations, we can then iterate through each component of the flattened compose and apply the operation. When there are multiple operands it is crucial they have both been flattened correctly so that we use the corresponding component from each operand together.
Where this falls short is if a vector ZeroValue is nested within a Compose. eg
vec3(vec2(), 0)
. flatten_compose() is unable to flatten this, and the resulting iterator will yieldZeroValue, Literal(0)
This causes various issues. Take binary_op(), for example. If we attempt to add
vec3(1, 2, 3)
to our unflattenablevec3(vec2(), 0)
this should be evaluated component-wise as 0 + 1, 0 + 2, and 0 + 3. As this has not been correctly flattened, however, we will evaluate vec2() + 1, and 0 + 2, which is simply incorrect.To solve this, we make eval_zero_value_and_splat() recursively call itself for each component if the expression is a Compose. This ensures no ZeroValues will be present during flatten_compose(), meaning it will successfully fully flatten the expression.
Testing
Added snapshot tests
Checklist
cargo fmt
.cargo clippy
. If applicable, add:cargo xtask test
to run tests.CHANGELOG.md
. See simple instructions inside file.