Skip to content

Commit ef35ddd

Browse files
committed
Fallback on no metadata if hot-reload is enabled, implement MaterialSettings
1 parent 364af20 commit ef35ddd

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/rust_gpu.rs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Wrapper for extending a `Material` with `rust-gpu` shader functionality.
22
3-
use std::marker::PhantomData;
3+
use std::{any::TypeId, marker::PhantomData, sync::RwLock};
44

55
use bevy::{
66
pbr::MaterialPipelineKey,
@@ -9,8 +9,10 @@ use bevy::{
99
MaterialPlugin, Plugin, Shader,
1010
},
1111
reflect::TypeUuid,
12-
render::render_resource::{AsBindGroup, PreparedBindGroup},
12+
render::render_resource::{AsBindGroup, PreparedBindGroup, ShaderRef},
13+
utils::HashMap,
1314
};
15+
use once_cell::sync::Lazy;
1416

1517
use crate::{
1618
prelude::{EntryPoint, Export, ExportHandle, RustGpuMaterial, SHADER_META},
@@ -25,6 +27,8 @@ const SHADER_DEFS: &[&'static str] = &[
2527
"SIXTEEN_BYTE_ALIGNMENT",
2628
];
2729

30+
static MATERIAL_SETTINGS: Lazy<RwLock<HashMap<TypeId, RustGpuSettings>>> = Lazy::new(default);
31+
2832
/// Configures backend support for [`RustGpu<M>`].
2933
pub struct RustGpuMaterialPlugin<M>
3034
where
@@ -63,6 +67,15 @@ where
6367
}
6468
}
6569

70+
/// Type-level RustGpu material settings
71+
#[derive(Debug, Default, Copy, Clone)]
72+
pub struct RustGpuSettings {
73+
/// If true, use M::vertex as a fallback instead of ShaderRef::default
74+
pub fallback_base_vertex: bool,
75+
/// If true, use M::fragment as a fallback instead of ShaderRef::default
76+
pub fallback_base_fragment: bool,
77+
}
78+
6679
/// [`RustGpu`] pipeline key.
6780
pub struct RustGpuKey<M>
6881
where
@@ -241,11 +254,29 @@ where
241254
M: RustGpuMaterial,
242255
{
243256
fn vertex_shader() -> bevy::render::render_resource::ShaderRef {
244-
M::vertex_shader()
257+
if let Some(true) = MATERIAL_SETTINGS
258+
.read()
259+
.unwrap()
260+
.get(&TypeId::of::<Self>())
261+
.map(|settings| settings.fallback_base_vertex)
262+
{
263+
M::vertex_shader()
264+
} else {
265+
ShaderRef::Default
266+
}
245267
}
246268

247269
fn fragment_shader() -> bevy::render::render_resource::ShaderRef {
248-
M::fragment_shader()
270+
if let Some(true) = MATERIAL_SETTINGS
271+
.read()
272+
.unwrap()
273+
.get(&TypeId::of::<Self>())
274+
.map(|settings| settings.fallback_base_vertex)
275+
{
276+
M::fragment_shader()
277+
} else {
278+
ShaderRef::Default
279+
}
249280
}
250281

251282
fn alpha_mode(&self) -> bevy::prelude::AlphaMode {
@@ -300,10 +331,13 @@ where
300331
let metas = SHADER_META.read().unwrap();
301332
if let Some(vertex_meta) = metas.get(&vertex_shader) {
302333
info!("Vertex meta is valid");
334+
info!("Checking entry point {entry_point:}");
303335
if !vertex_meta.entry_points.contains(&entry_point) {
304336
warn!("Missing entry point {entry_point:}");
305337
apply = false;
306338
}
339+
} else {
340+
apply = false;
307341
}
308342
}
309343

@@ -350,10 +384,13 @@ where
350384
let metas = SHADER_META.read().unwrap();
351385
if let Some(fragment_meta) = metas.get(&fragment_shader) {
352386
info!("Fragment meta is valid");
387+
info!("Checking entry point {entry_point:}");
353388
if !fragment_meta.entry_points.contains(&entry_point) {
354389
apply = false;
355390
warn!("Missing entry point {entry_point:}, falling back to default fragment shader.");
356391
}
392+
} else {
393+
apply = false;
357394
}
358395
}
359396

@@ -384,3 +421,13 @@ where
384421
Ok(())
385422
}
386423
}
424+
425+
impl<M> RustGpu<M>
426+
where
427+
M: 'static,
428+
{
429+
pub fn map_settings<F: FnOnce(&mut RustGpuSettings)>(f: F) {
430+
let mut settings = MATERIAL_SETTINGS.write().unwrap();
431+
f(&mut settings.entry(TypeId::of::<Self>()).or_default());
432+
}
433+
}

src/shader_meta.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ use bevy_common_assets::json::JsonAssetPlugin;
1616

1717
use serde::{Deserialize, Serialize};
1818

19-
use crate::prelude::{reload_materials, RustGpuMaterial};
19+
use crate::{
20+
prelude::{reload_materials, RustGpuMaterial},
21+
systems::shader_events,
22+
};
2023

2124
pub(crate) static SHADER_META: Lazy<RwLock<ShaderMeta>> = Lazy::new(Default::default);
2225
pub(crate) static SHADER_META_MAP: Lazy<RwLock<ShaderMetaMap>> = Lazy::new(Default::default);
@@ -46,7 +49,9 @@ where
4649

4750
app.add_system_to_stage(
4851
bevy::prelude::CoreStage::Last,
49-
module_meta_events::<M>.before(reload_materials::<M>),
52+
module_meta_events::<M>
53+
.after(shader_events::<M>)
54+
.before(reload_materials::<M>),
5055
);
5156
}
5257
}

0 commit comments

Comments
 (0)