bevyengine / bevyengine/bevy

Handle explicit alignment in `BufferVec`

Open
#4,642 3 comments 0 reactions 0 assignees View on GitHub
A-Rendering C-Performance C-Usability
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

`BufferVec` does not currently handle explicit alignment, and instead tightly packs data inside the buffer. This is fine for _e.g._ vertex buffers where there's no alignment requirement, but for other kinds of buffers this breaks the required hardware alignment and prevents the use of the buffer (see djeedai/bevy_hanabi#26 for an example of crash).

See _e.g._ #4319 for some example of alignments per hardware for uniform buffers. Storage buffers have similar requirements, highlighted by the [`WgpuLimits::min_storage_buffer_offset_alignment`](https://docs.rs/bevy/latest/bevy/render/render_resource/struct.WgpuLimits.html#structfield.min_storage_buffer_offset_alignment) which can be up to 256 bytes on macOS for example.

The proposal is to add an explicit `item_align` argument to the `new()` method, that the caller can fill with the expected alignment based on the buffer usage. Internally, there are multiple options for handling this:

1. Continue tightly packing the items into the internal `Vec`, and expand those into a properly-aligned CPU buffer at the point where the data is about to be copied to the GPU buffer. This has the advantage of lower CPU-side memory consumption, at the price of an extra memory copy.

2. Properly align the items with the `item_align` parameter when they're gathered in the CPU-side buffer (the `Vec`). This wastes the alignment padding, but avoids an extra allocation and memory copy when the GPU buffer is initialized.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.