Skip to content

Gpu readback#15419

Merged
alice-i-cecile merged 26 commits intobevyengine:mainfrom
tychedelia:gpu-readback
Sep 30, 2024
Merged

Gpu readback#15419
alice-i-cecile merged 26 commits intobevyengine:mainfrom
tychedelia:gpu-readback

Conversation

@tychedelia
Copy link
Copy Markdown
Member

@tychedelia tychedelia commented Sep 25, 2024

Objective

Adds a new Readback component to request for readback of a Handle<Image> or Handle<ShaderStorageBuffer> to the CPU in a future frame.

Solution

We track the Readback component and allocate a target buffer to write the gpu resource into and map it back asynchronously, which then fires a trigger on the entity in the main world. This proccess is asynchronous, and generally takes a few frames.

Showcase

let mut buffer = ShaderStorageBuffer::from(vec![0u32; 16]);
buffer.buffer_description.usage |= BufferUsages::COPY_SRC;
let buffer = buffers.add(buffer);

commands
    .spawn(Readback::buffer(buffer.clone()))
    .observe(|trigger: Trigger<ReadbackComplete>| {
        info!("Buffer data from previous frame {:?}", trigger.event());
    });

@tychedelia tychedelia added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 25, 2024
@IceSentry IceSentry self-requested a review September 25, 2024 16:39
Copy link
Copy Markdown
Contributor

@kristoff3r kristoff3r left a comment

Choose a reason for hiding this comment

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

Code looks good to me, none of my comments are blockers so I'm just going to approve already. I tested the example on Linux and it works.

Co-authored-by: Kristoffer Søholm <k.soeholm@gmail.com>
@tychedelia tychedelia removed the S-Needs-Review Needs reviewer attention (from anyone!) to move forward label Sep 26, 2024
@tychedelia tychedelia 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 Sep 26, 2024
Copy link
Copy Markdown
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.

It would be nice if it was able to automatically convert it back to the right type. At least in the case of buffervec but not sure if that's even possible without user input.

I'd like a bit more docs in a few places especially the example but things are already pretty good.

Overall, the implementation is really nice, LGTM

tychedelia and others added 6 commits September 26, 2024 13:33
Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
@tychedelia tychedelia added this to the 0.15 milestone Sep 30, 2024
@alice-i-cecile alice-i-cecile added the M-Release-Note Work that should be called out in the blog due to impact label Sep 30, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 30, 2024
Merged via the queue into bevyengine:main with commit 40c26f8 Sep 30, 2024
robtfm pushed a commit to robtfm/bevy that referenced this pull request Oct 4, 2024
# Objective

Adds a new `Readback` component to request for readback of a
`Handle<Image>` or `Handle<ShaderStorageBuffer>` to the CPU in a future
frame.

## Solution

We track the `Readback` component and allocate a target buffer to write
the gpu resource into and map it back asynchronously, which then fires a
trigger on the entity in the main world. This proccess is asynchronous,
and generally takes a few frames.

## Showcase

```rust
let mut buffer = ShaderStorageBuffer::from(vec![0u32; 16]);
buffer.buffer_description.usage |= BufferUsages::COPY_SRC;
let buffer = buffers.add(buffer);

commands
    .spawn(Readback::buffer(buffer.clone()))
    .observe(|trigger: Trigger<ReadbackComplete>| {
        info!("Buffer data from previous frame {:?}", trigger.event());
    });
```

---------

Co-authored-by: Kristoffer Søholm <k.soeholm@gmail.com>
Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
@alice-i-cecile
Copy link
Copy Markdown
Member

Thank you to everyone involved with the authoring or reviewing of this PR! This work is relatively important and needs release notes! Head over to bevyengine/bevy-website#1704 if you'd like to help out.

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-Feature A new feature, making something new possible M-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants