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

Adds ShaderStorageBuffer asset #14663

Merged
merged 25 commits into from
Sep 2, 2024

Conversation

tychedelia
Copy link
Contributor

@tychedelia tychedelia commented Aug 8, 2024

Adds a new Handle<Storage> asset type that can be used as a render asset, particularly for use with AsBindGroup.

Closes: #13658

Objective

Allow users to create storage buffers in the main world without having to access the RenderDevice. While this resource is technically available, it's bad form to use in the main world and requires mixing rendering details with main world code. Additionally, this makes storage buffers easier to use with AsBindGroup, particularly in the following scenarios:

  • Sharing the same buffers between a compute stage and material shader. We already have examples of this for storage textures (see game of life example) and these changes allow a similar pattern to be used with storage buffers.
  • Preventing repeated gpu upload (see the previous easier to use Vec AsBindGroup option).
  • Allow initializing custom materials using Default. Previously, the lack of a Default implement for the raw wgpu::Buffer type made implementing a AsBindGroup + Default bound difficult in the presence of buffers.

Solution

Adds a new Handle<Storage> asset type that is prepared into a GpuStorageBuffer render asset. This asset can either be initialized with a Vec<u8> of properly aligned data or with a size hint. Users can modify the underlying wgpu::BufferDescriptor to provide additional usage flags.

Migration Guide

The AsBindGroup storage attribute has been modified to reference the new Handle<Storage> asset instead. Usages of Vec` should be converted into assets instead.

@tychedelia tychedelia added A-Rendering Drawing game state to the screen S-Needs-Review Needs reviewer attention (from anyone!) to move forward C-Enhancement A new feature labels Aug 8, 2024
@JMS55
Copy link
Contributor

JMS55 commented Aug 16, 2024

See related old PR: #6669

@tychedelia tychedelia requested a review from JMS55 August 21, 2024 23:45
@IceSentry IceSentry self-requested a review August 24, 2024 19:43
@IceSentry
Copy link
Contributor

Like mentioned on discord. I'd prefer if this kept the ability to use a raw Buffer with AsBindGroup.

@tychedelia
Copy link
Contributor Author

@IceSentry The refactor we did in #14909 helped clean this up a lot. Changes since you last reviewed:

  • Restored buffer argument that allows setting a raw buffer.
  • Changed the name to ShaderStorageBuffer and GpuShaderStorageBuffer per our bikeshed on discord.

@@ -0,0 +1,113 @@
//! This example demonstrates how to use a storage buffer with `AsBindGroup` in a custom material.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the example be renamed to match the new type? I don't really have an opinion either way honestly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thought was just that shader/shader_storage_buffer was a bit redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But happy to change :)

Copy link
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

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

I didn't test the example, but the implementation LGTM

@IceSentry
Copy link
Contributor

IceSentry commented Aug 29, 2024

Also, could you update the PR description with the new name?

@tychedelia tychedelia changed the title Adds storage buffer asset Adds ShaderStorageBuffer asset Aug 29, 2024
Cargo.toml Outdated Show resolved Hide resolved
tychedelia and others added 3 commits August 29, 2024 09:52
Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
Copy link
Contributor

You added a new example but didn't add metadata for it. Please update the root Cargo.toml file.

Copy link
Contributor

@ChristopherBiscardi ChristopherBiscardi left a comment

Choose a reason for hiding this comment

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

ran the example successfully and the new usage makes sense to me

@IceSentry IceSentry added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 31, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 2, 2024
Merged via the queue into bevyengine:main with commit a464004 Sep 2, 2024
31 checks passed
github-merge-queue bot pushed a commit that referenced this pull request Sep 9, 2024
…ata (#15044)

Adds some methods to assist in building `ShaderStorageBuffer` without
using `bytemuck`. We keep the `&[u8]` constructors since this is still
modeled as a thin wrapper around the buffer descriptor, but should make
it easier to interact with at the cost of an extra allocation in the
`ShaderType` path for the buffer writer.

Follow up from #14663
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 C-Enhancement A new feature S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Handle<Storage> abstraction for material shaders
5 participants