bevy icon indicating copy to clipboard operation
bevy copied to clipboard

An example of a shader returning values back to the CPU via a buffer

Open dcz-self opened this issue 3 years ago • 4 comments
trafficstars

Objective

To have an example showing how to get data back from the GPU after processing in a compute shader. Currently, bevy doesn't have any calls to map_buffer in the codebase at all. The API changed in 0.8, so examples easily found online are only misleading.

With an example, bevy will be forced to maintain a current description of how to do it as well, across API changes :)

Solution

This is implemented as a tumor on top of game of life, and doesn't calculate anything useful either. I'm not really sure how it can be demonstrated tht the buffer is on the CPU without making the example carry too much context. Currently it's printing numbers on the terminal.

So my question is: what should this example look like to look approachable but also stay focused on the buffer problem?

dcz-self avatar Nov 11 '22 17:11 dcz-self

A simple example could be a color picker. Draw a gradient to the screen, return that buffer to the CPU. Look up the mouse's current position on that buffer to get the color at that point. We can know what the quad should look like at any point ahead of time, which means we could easily write test for this.

aevyrie avatar Nov 11 '22 21:11 aevyrie

I turned this into a color picker. I had trouble finding a way to pass "intract" a resource from the render world back to the normal world, so I cheesed it using an Arc<Mutex>.

This example looks like it's a superset of the animate example, maybe with the one exception that the animation uses a render shader rather than compute.

dcz-self avatar Nov 12 '22 11:11 dcz-self

Bumped to main branch and removed "RFC".

dcz-self avatar Nov 12 '22 15:11 dcz-self

I don't know what that deps failure is about. I swear I hadn't fiddled with that.

dcz-self avatar Nov 12 '22 16:11 dcz-self

is there any chance this pull request could be re-looked into? I'm not sure about the global importance but I think that for those few people who need this, like me, this simple example it's kind of a "deal-breaker" because any other online resource I could find regarding the topic either didn't talk about reading back values from gpu memory (thus giving the impression that it wasn't actually possible / supported) or required a good understanding of bevy's codebase / render system

btw: there's still a mention to "cow's game of life" at the beginning of compute_shader_buffer.rs, is it a left-over?

dennisorlando avatar Aug 13 '23 17:08 dennisorlando

Regarding the original problem: I think that that the classic "double an array" might be more intuitive (at least for me); If I get compute shaders to work (spoilers: this PR is outdated thus it doesn't work anymore) I'll try to make a PR and convince bevy to merge it

dennisorlando avatar Sep 01 '23 11:09 dennisorlando

done in #12877

mockersf avatar Apr 08 '24 18:04 mockersf