Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PersistentGpuBufferable a safe trait #12744

Merged

Conversation

james7132
Copy link
Member

@james7132 james7132 commented Mar 27, 2024

Objective

Fixes #12727. All parts that PersistentGpuBuffer interact with should be 100% safe both on the CPU and the GPU: Queue::write_buffer_with zeroes out the slice being written to and when uploading to the GPU, and all slice writes are bounds checked on the CPU side.

Solution

Make PersistentGpuBufferable a safe trait. Enforce it's correct implementation via assertions. Re-enable forbid(unsafe_code) on bevy_pbr.

@james7132 james7132 added A-Rendering Drawing game state to the screen P-Unsound A bug that results in undefined compiler behavior labels Mar 27, 2024
@james7132 james7132 requested a review from JMS55 March 27, 2024 06:35
@james7132
Copy link
Member Author

james7132 commented Mar 27, 2024

Note: the current implementations for Arc<[Meshlet]> and Arc<[MeshletBoundingSphere]> do not work correctly as described in the trait documentation on big endian systems. We may need to use encase here to ensure correctness on those platforms (as rare as they are).

Copy link
Contributor

@JMS55 JMS55 left a comment

Choose a reason for hiding this comment

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

Encase can be slow, and I highly doubt anyone is using this feature on big-endian systems. Unless there's a way to do it without overhead, or we put it behind a conditional config, I'd rather just do an assertion that the system is little-endian or something.

crates/bevy_pbr/src/meshlet/persistent_buffer.rs Outdated Show resolved Hide resolved
fn size_in_bytes(&self) -> usize;

/// Convert `self` + `metadata` into bytes (little-endian), and write to the provided buffer slice.
/// Any bytes not written to in the slice will be zeroed out when uploaded to the GPU.
Copy link
Contributor

Choose a reason for hiding this comment

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

Theoretically, it would be nice for wgpu to provide a way to skip the zeroing, so this may change in the future if they add that. We can leave it for now though :)

Copy link
Member Author

@james7132 james7132 Mar 27, 2024

Choose a reason for hiding this comment

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

I asked about this in the wgpu matrix chat and it seems feasible, though it may need to be exposed via &mut [MaybeUninit<u8>], which would only require unsafe inside the implementation instead of as a part of the trait itself.

@JMS55 JMS55 self-requested a review March 27, 2024 19:37
@JMS55
Copy link
Contributor

JMS55 commented Mar 27, 2024

(Didn't mean to approve, meant to request changes)

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Mar 29, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Mar 29, 2024
Merged via the queue into bevyengine:main with commit e62a01f Mar 29, 2024
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen P-Unsound A bug that results in undefined compiler behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PersistentGpuBufferable probably shouldn't be an unsafe trait
3 participants