Skip to content

Conversation

@andyleiserson
Copy link
Contributor

@andyleiserson andyleiserson commented Apr 1, 2025

The vertex pulling transform transforms vertex shaders:

to receive the vertex buffers, lengths, and vertex id as args, and bounds-check the vertex id and use the index into the vertex buffers to access attributes, rather than using Metal's [[stage-in]] assembled attribute data

It is possible for the vertices to require truncation upon loading. For example, the vertex buffer could define the texture coordinate as a vec3, but the vertex shader might not care about the z value and load the texture coordinate as a vec2. Before this change, the generated code would do this by casting, which seems to be allowed from float4 to float3, but not from float3 to float2. This PR changes the truncation to use a swizzle selector to select the appropriate number of components.

Fixes #7410

Testing
Adds more snapshot tests for the vertex pulling transform. I also tested that this fix does resolve the issue seen on the website linked from the original bugzilla issue.

Review note: there are four new snapshot tests, with suffixes -x1 through -x4, for loading vertices into a scalar, vec2, vec3, or vec4. The TOML files for all four are identical since they are describing the source vertex formats. The WGSL files are the same except for the type of variable that the vertex is loaded to.

Squash or Rebase? Squash

Checklist

  • Run cargo fmt.
  • Run taplo format.
  • Run cargo clippy --tests. If applicable, add:
    • --target wasm32-unknown-unknown
  • Run cargo xtask test to run tests.
  • If this contains user-facing changes, add a CHANGELOG.md entry.

if needs_padding_or_truncation != Ordering::Equal {
writeln!(
self.out,
"{}// {attribute_ty_name} <- {:?}",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the swizzle selector tends to be buried at the end of a very long argument list, this comment helps to call out what's going on, but if it's preferred not to add anything extraneous to the generated shaders, I'd be fine removing it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sometimes add these sort of comments to help debugging - it should be fine.


// Check dimensionality of the attribute compared to the unpacking
// function. If attribute dimension is < unpack dimension, then
// we need to explicitly cast down the result. Otherwise, if attribute
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc is now out of date.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated.

Copy link
Member

@teoxoy teoxoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@andyleiserson andyleiserson merged commit 110d737 into gfx-rs:trunk Apr 3, 2025
37 checks passed
@andyleiserson andyleiserson deleted the vertex-pulling branch April 3, 2025 17:27
Vecvec pushed a commit to Vecvec/wgpu that referenced this pull request Apr 11, 2025
jimblandy pushed a commit to jimblandy/wgpu that referenced this pull request Apr 18, 2025
sharmajai pushed a commit to sharmajai/wgpu that referenced this pull request Oct 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vertex-pulling transform generates invalid metal code in certain cases

2 participants