Bevy Simple Compute
Dispatch and run compute shaders on Bevy from App World.
Credits
Many thanks to Kjolnyr for developing the original bevy_app_compute
crate. This crate is a fork updated to support newer versions of the Bevy Engine.
Getting Started
Add the following line to your Cargo.toml
[]
= "0.15.3"
Usage
Setup
Declare your shaders in structs implementing ComputeShader
. The shader()
fn should point to your shader source code.
You need to derive TypePath
as well and assign a unique Uuid:
;
Next, declare a struct implementing ComputeWorker
to declare the bindings and the logic of your worker:
;
Don't forget to add a shader file to your assets/
folder:
@group @binding
uni: f32;
@group @binding
my_storage: ;
@compute @workgroup_size
Add the AppComputePlugin
plugin to your app, as well as one AppComputeWorkerPlugin
per struct implementing ComputeWorker
:
// ... other plugins ...
.add_plugins;
new
Your compute worker will now run every frame, during the PostUpdate
stage. To read/write from it, use the AppComputeWorker<T>
resource!
(see simple.rs)
Multiple passes
You can have multiple passes without having to copy data back to the CPU in between:
let worker = new
.add_uniform
.add_storage
.add_staging
// add each item + `value` from `input` to `output`
.
// multiply each element of `output` by itself
.
.
.build;
// the `output` buffer will contain [16.0, 25.0, 36.0, 49.0]
(see multi_pass.rs)
One shot computes
You can configure your worker to execute only when requested:
let worker = new
.add_uniform
.add_staging
.
// This `one_shot()` function will configure your worker accordingly
.one_shot
.build;
Then, you can call execute()
on your worker when you are ready to execute it:
// Execute it only when the left mouse button is pressed.
}
It will run at the end of the current frame, and you'll be able to read the data in the next frame.
(see one_shot.rs)
Examples
See examples
Features being worked upon
- Ability to read/write between compute passes.
- Add more options to the API, like deciding
BufferUsages
or size of buffers. - Optimization.
- Tests.
Any contributions are welcome.
Bevy version mapping
Newer versions of the crate have the following versioning format: first two numbers show supported major Bevy version (e.g. bevy_simple_compute of version range 0.15.* implies support of all version range of Bevy 0.15) and the third number is a crate's version for this version of Bevy.
Bevy | bevy_simple_compute |
---|---|
main | main |
0.16 | 0.16.0 |
0.15 | 0.15.1 |
0.15 | 0.15.2 |
0.15 | 0.15.3 |
0.10 | 0.10.3 |
0.12 | 0.10.5 |