Skip to content

Commit 00fb737

Browse files
committed
Implement RustGpuShader to ensure shader meta load
1 parent ef35ddd commit 00fb737

File tree

5 files changed

+25
-33
lines changed

5 files changed

+25
-33
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Currently this must occur before `RenderPlugin` is added (most often via `Defaul
2525

2626
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.)
2727

28-
When instantiating `RustGpu` materials, handles to the `Shader` representing your `rust-gpu` SPIR-V will be required.
29-
These handles should be acquired via the `AssetServer::load_rust_gpu_shader` extension method, which is provided by the `LoadRustGpuShader` trait.
30-
This will allow the plugin to automatically load the `.spv.json` metadata asset required for hot-reloading support.
28+
When instantiating `RustGpu` materials, `RustGpuShader` handles will be required.
29+
These are equivalent to `Handle<Shader>` with some extra hot-reloading machinery,
30+
and can be acquired via the `AssetServer::load_rust_gpu_shader` extension method provided by the `LoadRustGpuShader` trait.
3131

3232
## Feature Flags
3333

src/load_rust_gpu_shader.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
use std::path::PathBuf;
22

3-
use bevy::prelude::{Shader, Handle, AssetServer};
3+
use bevy::prelude::{AssetServer, Handle, Shader};
44

55
use crate::prelude::SHADER_META_MAP;
66

7+
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8+
pub struct RustGpuShader(pub(crate) Handle<Shader>);
9+
710
/// Loads a `Shader` asset,
811
/// and optionally its metadata if the corresponding feature flag is enabled.
912
pub trait LoadRustGpuShader {
10-
fn load_rust_gpu_shader<'a, P: Into<PathBuf>>(&self, path: P) -> Handle<Shader>;
13+
fn load_rust_gpu_shader<'a, P: Into<PathBuf>>(&self, path: P) -> RustGpuShader;
1114
}
1215

1316
impl LoadRustGpuShader for AssetServer {
14-
fn load_rust_gpu_shader<'a, P: Into<PathBuf>>(&self, path: P) -> Handle<Shader> {
17+
fn load_rust_gpu_shader<'a, P: Into<PathBuf>>(&self, path: P) -> RustGpuShader {
1518
let path = path.into();
1619

1720
let mut meta_path: PathBuf;
@@ -23,7 +26,6 @@ impl LoadRustGpuShader for AssetServer {
2326
meta_path.push(last + ".json");
2427
}
2528

26-
#[allow(unused_variables)]
2729
let shader = self.load(path);
2830

2931
#[cfg(feature = "hot-reload")]
@@ -35,7 +37,6 @@ impl LoadRustGpuShader for AssetServer {
3537
);
3638
}
3739

38-
shader
40+
RustGpuShader(shader)
3941
}
4042
}
41-

src/rust_gpu.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::{any::TypeId, marker::PhantomData, sync::RwLock};
55
use bevy::{
66
pbr::MaterialPipelineKey,
77
prelude::{
8-
default, info, warn, CoreStage, Handle, Image, IntoSystemDescriptor, Material,
9-
MaterialPlugin, Plugin, Shader,
8+
default, info, warn, CoreStage, Image, IntoSystemDescriptor, Material, MaterialPlugin,
9+
Plugin,
1010
},
1111
reflect::TypeUuid,
1212
render::render_resource::{AsBindGroup, PreparedBindGroup, ShaderRef},
@@ -15,6 +15,7 @@ use bevy::{
1515
use once_cell::sync::Lazy;
1616

1717
use crate::{
18+
load_rust_gpu_shader::RustGpuShader,
1819
prelude::{EntryPoint, Export, ExportHandle, RustGpuMaterial, SHADER_META},
1920
systems::{reload_materials, shader_events},
2021
};
@@ -82,8 +83,8 @@ where
8283
M: AsBindGroup,
8384
{
8485
pub base: M::Data,
85-
pub vertex_shader: Option<Handle<Shader>>,
86-
pub fragment_shader: Option<Handle<Shader>>,
86+
pub vertex_shader: Option<RustGpuShader>,
87+
pub fragment_shader: Option<RustGpuShader>,
8788
pub iteration: usize,
8889
#[cfg(feature = "hot-rebuild")]
8990
pub export_handle: Option<ExportHandle>,
@@ -146,10 +147,10 @@ pub struct RustGpu<M> {
146147
pub base: M,
147148

148149
/// If `Some`, overrides [`Material::vertex_shader`] during specialization.
149-
pub vertex_shader: Option<Handle<Shader>>,
150+
pub vertex_shader: Option<RustGpuShader>,
150151

151152
/// If `Some`, overrides [`Material::fragment_shader`] during specialization.
152-
pub fragment_shader: Option<Handle<Shader>>,
153+
pub fragment_shader: Option<RustGpuShader>,
153154

154155
/// Current reload iteration, used to drive hot-reloading.
155156
pub iteration: usize,
@@ -329,7 +330,7 @@ where
329330
#[cfg(feature = "hot-reload")]
330331
{
331332
let metas = SHADER_META.read().unwrap();
332-
if let Some(vertex_meta) = metas.get(&vertex_shader) {
333+
if let Some(vertex_meta) = metas.get(&vertex_shader.0) {
333334
info!("Vertex meta is valid");
334335
info!("Checking entry point {entry_point:}");
335336
if !vertex_meta.entry_points.contains(&entry_point) {
@@ -354,7 +355,7 @@ where
354355

355356
if apply {
356357
info!("Applying vertex shader and entry point");
357-
descriptor.vertex.shader = vertex_shader;
358+
descriptor.vertex.shader = vertex_shader.0;
358359
descriptor.vertex.entry_point = entry_point.into();
359360
} else {
360361
warn!("Falling back to default vertex shader.");
@@ -382,7 +383,7 @@ where
382383
{
383384
info!("Fragment meta is present");
384385
let metas = SHADER_META.read().unwrap();
385-
if let Some(fragment_meta) = metas.get(&fragment_shader) {
386+
if let Some(fragment_meta) = metas.get(&fragment_shader.0) {
386387
info!("Fragment meta is valid");
387388
info!("Checking entry point {entry_point:}");
388389
if !fragment_meta.entry_points.contains(&entry_point) {
@@ -406,7 +407,7 @@ where
406407

407408
if apply {
408409
info!("Applying fragment shader and entry point");
409-
fragment_descriptor.shader = fragment_shader;
410+
fragment_descriptor.shader = fragment_shader.0;
410411
fragment_descriptor.entry_point = entry_point.into();
411412
} else {
412413
warn!("Falling back to default fragment shader.");

src/rust_gpu_material.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
//! Trait
22
3-
use std::marker::PhantomData;
3+
use bevy::prelude::Material;
44

5-
use bevy::{
6-
prelude::{
7-
default, CoreStage, Handle, IntoSystemDescriptor, Material, MaterialPlugin, Plugin, Shader,
8-
},
9-
render::render_resource::AsBindGroup,
10-
};
11-
12-
use crate::prelude::{reload_materials, shader_events, EntryPoint, RustGpu};
13-
14-
#[cfg(feature = "hot-rebuild")]
15-
use crate::prelude::ExportHandle;
5+
use crate::prelude::EntryPoint;
166

177
/// A [`Material`] type with statically-known `rust-gpu` vertex and fragment entry points.
188
pub trait RustGpuMaterial: Material {

src/systems.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ pub fn reload_materials<M>(
4848
let mut reload = false;
4949

5050
if let Some(vertex_shader) = &material.vertex_shader {
51-
if changed_shaders.contains(vertex_shader) {
51+
if changed_shaders.contains(&vertex_shader.0) {
5252
reload = true;
5353
}
5454
}
5555

5656
if let Some(fragment_shader) = &material.fragment_shader {
57-
if changed_shaders.contains(fragment_shader) {
57+
if changed_shaders.contains(&fragment_shader.0) {
5858
reload = true;
5959
}
6060
}

0 commit comments

Comments
 (0)