Skip to content

fix(examples): use pod_collect_to_vec for mapped buffer reads - #9378

Merged
cwfitzgerald merged 1 commit into
gfx-rs:trunkfrom
countgitmick:fix/examples-bytemuck-alignment
Apr 8, 2026
Merged

fix(examples): use pod_collect_to_vec for mapped buffer reads#9378
cwfitzgerald merged 1 commit into
gfx-rs:trunkfrom
countgitmick:fix/examples-bytemuck-alignment

Conversation

@countgitmick

Copy link
Copy Markdown
Contributor

Summary

  • Replace bytemuck::cast_slice with bytemuck::allocation::pod_collect_to_vec when reading from mapped buffer ranges
  • Add extern_crate_alloc feature to workspace bytemuck dependency

cast_slice requires the source slice to be properly aligned for the target type, but BufferView from get_mapped_range() is not guaranteed to meet alignment requirements. pod_collect_to_vec copies bytes into a new properly-aligned Vec, avoiding potential undefined behavior from misaligned reads.

As suggested by @cwfitzgerald in #6191.

Files changed

  • examples/src/hello_workgroups/mod.rs
  • examples/src/hello_synchronization/mod.rs
  • examples/src/hello_compute/mod.rs
  • examples/src/repeated_compute/mod.rs
  • examples/src/timestamp_queries/mod.rs
  • Cargo.toml (add extern_crate_alloc feature to bytemuck)

Test plan

  • cargo check -p wgpu-examples
  • cargo fmt --check -p wgpu-examples

Closes #6191

Replace bytemuck::cast_slice with bytemuck::allocation::pod_collect_to_vec
when reading from mapped buffer ranges. cast_slice requires the source
slice to be properly aligned for the target type, but BufferView from
get_mapped_range() is not guaranteed to meet alignment requirements.
pod_collect_to_vec copies bytes into a new properly-aligned Vec,
avoiding potential undefined behavior from misaligned reads.

Closes gfx-rs#6191
@countgitmick
countgitmick force-pushed the fix/examples-bytemuck-alignment branch from b349e56 to 2f5cbe4 Compare April 6, 2026 05:54
@countgitmick

Copy link
Copy Markdown
Contributor Author

Rebased on trunk to resolve merge conflicts from the examples restructure. Also caught a missed call site in big_compute_buffers/mod.rs and fixed cooperative_matrix/mod.rs which has the same pattern.

Updated file list (8 files):

  • examples/features/src/hello_workgroups/mod.rs
  • examples/features/src/hello_synchronization/mod.rs
  • examples/features/src/repeated_compute/mod.rs
  • examples/features/src/timestamp_queries/mod.rs
  • examples/features/src/big_compute_buffers/mod.rs (newly caught)
  • examples/features/src/cooperative_matrix/mod.rs (newly caught)
  • examples/standalone/01_hello_compute/src/main.rs
  • examples/standalone/01_hello_compute/Cargo.toml

@ErichDonGubler ErichDonGubler self-assigned this Apr 8, 2026
@cwfitzgerald
cwfitzgerald merged commit 5d03e09 into gfx-rs:trunk Apr 8, 2026
58 checks passed
@ErichDonGubler

Copy link
Copy Markdown
Member

question: Is there an interesting CHANGELOG entry we might produce for this? If this is obscure enough, I don't feel like we need to chase that down.

@cwfitzgerald

Copy link
Copy Markdown
Member

It's example only.

slyedoc pushed a commit to slyedoc/wgpu that referenced this pull request May 28, 2026
…#9378)

Co-authored-by: countgitmick <countgitmick@users.noreply.github.com>
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.

Examples shouldn't use bytemuck to cast out of a get_mapped_range() BufferView

3 participants