Skip to content

Commit 22e87f1

Browse files
committed
Update README.md
1 parent 76eeb11 commit 22e87f1

File tree

1 file changed

+81
-11
lines changed

1 file changed

+81
-11
lines changed

README.md

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,101 @@ As such, its use implies all the caveats of the above, plus the following:
2222
* [Using SPIR-V in a bevy `Material` requires a custom `bevy` fork](https://github.com/Bevy-Rust-GPU/bevy-rust-gpu/issues/12)
2323
* [Storage buffers are currently unsupported](https://github.com/Bevy-Rust-GPU/bevy-rust-gpu/issues/13)
2424

25-
Beyond that, `bevy-rust-gpu` is also in active development, but has a low user-facing API footprint.
25+
Beyond that, `bevy-rust-gpu` is also in active development, but has a relatively small user-facing API footprint.
2626
Major changes will be driven by development in the upstream `bevy` and `rust-gpu` crates.
2727

28+
In practical terms, it's able to support the hot-rebuild workflow depicted above,
29+
and allows for relatively complex shader implementations (storage buffer issues notwithstanding), such as [a Rust reimplementation of `bevy_pbr`](https://github.com/Bevy-Rust-GPU/bevy-pbr-rust).
30+
2831
Currently, none of the [Bevy Rust-GPU](https://github.com/Bevy-Rust-GPU) crates are published on crates.io;
29-
this may change as and when the major caveats are solved, but in the meantime will be hosted on github and versioned via tag.
32+
this may change as and when the major caveats are solved, but in the meantime will be hosted on github and versioned by tag.
3033

3134
## Usage
3235

33-
First, implement a `Material` type to describe your material's bind group layout and pipeline specialization.
34-
The vertex and fragment shaders specified here will be used as the fallback if their `rust-gpu` equivalents are not available,
35-
so can be left to the default `Material` implementation if desired.
36+
First, add `bevy-rust-gpu` to your `Cargo.toml`:
37+
38+
```toml
39+
[dependencies]
40+
bevy-rust-gpu = { git = "https://github.com/Bevy-Rust-GPU/bevy-rust-gpu", tag = "v0.3.0" }
41+
```
42+
43+
Next, implement a `Material` type to describe your material's bind group layout and pipeline specialization:
44+
45+
```rust
46+
struct MyRustGpuMaterial {
47+
#[uniform(0)]
48+
color: Vec4,
49+
}
50+
51+
// The vertex and fragment shaders specified here can be used as a fallback
52+
// when entrypoints are unavailable (see the documentation of bevy_rust_gpu::prelude::RustGpuSettings),
53+
// but are otherwise deferred to ShaderRef::Default, so can be left unimplemented.
54+
impl Material for MyRustGpuMaterial {}
55+
```
3656

3757
Then, implement `RustGpuMaterial` for your `Material` type.
38-
This will require creating marker structs to represent its vertex and fragment shaders,
39-
and implementing the `EntryPoint` trait on them to describe entry point names and compile-time parameters.
58+
59+
```rust
60+
// First, implement some marker structs to represent our shader entry points
61+
62+
pub enum MyVertex {}
63+
64+
impl EntryPoint for MyVertex {
65+
const NAME: EntryPointName = "vertex";
66+
const PARAMETERS: EntryPointParameters = &[];
67+
}
68+
69+
pub enum MyFragment {}
70+
71+
impl EntryPoint for MyFragment {
72+
const NAME: EntryPointName = "fragment";
73+
const PARAMETERS: EntryPointParameters = &[];
74+
}
75+
76+
// Then, impl RustGpuMaterial for our material to tie them together
77+
78+
impl RustGpuMaterial for MyRustGpuMaterial {
79+
type Vertex = MyVertex;
80+
type Fragment = MyFragment;
81+
}
82+
```
83+
4084
(See [`bevy_pbr_rust.rs`](https://github.com/Bevy-Rust-GPU/bevy-rust-gpu/blob/master/src/bevy_pbr_rust.rs) for the [`bevy-pbr-rust`](https://github.com/Bevy-Rust-GPU/bevy-pbr-rust)-backed `StandardMaterial` reference implementation.)
4185

4286
Next, add `RustGpuPlugin` to your bevy app to configure the backend.
43-
Currently this must occur before `RenderPlugin` is added (most often via `DefaultPlugins`), as it requires early access to `WgpuSettings` to disable storage buffer support.
87+
88+
```rust
89+
let mut app = App::default();
90+
app.add_plugin(RustGpuPlugin); // Must be before RenderPlugin, i.e. before DefaultPlugins
91+
app.add_plugins(DefaultPlugins);
92+
```
4493

4594
For each `RustGpuMaterial` implementor, add a `RustGpuMaterialPlugin::<M>` to your app to setup rendering machinery and hot-reload / hot-rebuild support if the respective features are enabled (see below.)
4695

47-
When instantiating `RustGpu` materials, `RustGpuShader` handles will be required.
48-
These are equivalent to `Handle<Shader>` with some extra hot-reloading machinery,
49-
and can be acquired via the `AssetServer::load_rust_gpu_shader` extension method provided by the `LoadRustGpuShader` trait.
96+
```rust
97+
app.add_plugin(RustGpuMaterialPlugin::<MyRustGpuMaterial>::default());
98+
99+
```
100+
101+
Finally, load your shader, and add it to a material:
102+
103+
```rust
104+
fn setup(materials: ResMut<Assets<RustGpu<MyRustGpuMaterial>>>) {
105+
// Extension method provided by the LoadRustGpuShader trait
106+
// Returns a RustGpuShader, which is akin to Handle<Shader> with some extra hot-reloading machinery.
107+
let shader = asset_server.load_rust_gpu_shader(SHADER_PATH);
108+
109+
// Add it to a RustGpu material, which can be used with bevy's MaterialMeshBundle
110+
let material = materials.add(RustGpu {
111+
vertex_shader = Some(shader),
112+
fragment_shader = Some(shader),
113+
..default()
114+
});
115+
116+
// Use material as per any other
117+
...
118+
}
119+
```
50120

51121
## Feature Flags
52122

0 commit comments

Comments
 (0)