Skip to content

Commit b0f014b

Browse files
committed
Update to match bevy-pbr-rust 0.2
1 parent c5e887a commit b0f014b

File tree

5 files changed

+121
-10
lines changed

5 files changed

+121
-10
lines changed

src/bevy_pbr_rust.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
33
use bevy::{prelude::StandardMaterial, render::render_resource::ShaderDefVal};
44

5-
use crate::prelude::{EntryPoint, EntryPointName, EntryPointParameters, RustGpuMaterial};
5+
use crate::{
6+
prelude::{EntryPoint, EntryPointName, EntryPointParameters, RustGpuMaterial},
7+
EntryPointConstants,
8+
};
69

710
/// `bevy_rust_gpu::mesh::entry_points::vertex`
811
pub enum MeshVertex {}
@@ -14,6 +17,7 @@ impl EntryPoint for MeshVertex {
1417
(&[("VERTEX_COLORS", "some")], "none"),
1518
(&[("SKINNED", "some")], "none"),
1619
];
20+
const CONSTANTS: EntryPointConstants = &[];
1721
}
1822

1923
/// `bevy_rust_gpu::mesh::entry_points::fragment`
@@ -22,6 +26,7 @@ pub enum MeshFragment {}
2226
impl EntryPoint for MeshFragment {
2327
const NAME: EntryPointName = "mesh::entry_points::fragment";
2428
const PARAMETERS: EntryPointParameters = &[];
29+
const CONSTANTS: EntryPointConstants = &[];
2530
}
2631

2732
/// `bevy_rust_gpu::pbr::entry_points::fragment`
@@ -37,8 +42,16 @@ impl EntryPoint for PbrFragment {
3742
(&[("STANDARDMATERIAL_NORMAL_MAP", "some")], "none"),
3843
(&[("SKINNED", "some")], "none"),
3944
(&[("TONEMAP_IN_SHADER", "some")], "none"),
40-
(&[("PREMULTIPLY_ALPHA", "some")], "none"),
4145
(&[("DEBAND_DITHER", "some")], "none"),
46+
(
47+
&[
48+
("BLEND_MULTIPLY", "multiply"),
49+
("BLEND_PREMULTIPLIED_ALPHA", "blend_premultiplied_alpha"),
50+
],
51+
"none",
52+
),
53+
(&[("ENVIRONMENT_MAP", "some")], "none"),
54+
(&[("PREMULTIPLY_ALPHA", "some")], "none"),
4255
(
4356
&[
4457
("CLUSTERED_FORWARD_DEBUG_Z_SLICES", "debug_z_slices"),
@@ -53,7 +66,12 @@ impl EntryPoint for PbrFragment {
5366
],
5467
"none",
5568
),
69+
(
70+
&[("DIRECTIONAL_LIGHT_SHADOW_MAP_DEBUG_CASCADES", "some")],
71+
"none",
72+
),
5673
];
74+
const CONSTANTS: EntryPointConstants = &["MAX_DIRECTIONAL_LIGHTS", "MAX_CASCADES_PER_LIGHT"];
5775

5876
fn permutation(shader_defs: &Vec<ShaderDefVal>) -> Vec<String> {
5977
let mut permutation = vec![];

src/entry_point.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
//! Trait representation of a `rust-gpu` entry point.
22
3+
use std::collections::BTreeMap;
4+
35
use bevy::render::render_resource::ShaderDefVal;
46

57
/// An entry point name for use with the [`EntryPoint`] trait.
68
pub type EntryPointName = &'static str;
79

8-
/// A set of entry compile parameters for use with the [`EntryPoint`] trait.
10+
/// A set of entry point compile parameters for use with the [`EntryPoint`] trait.
911
pub type EntryPointParameters =
1012
&'static [(&'static [(&'static str, &'static str)], &'static str)];
1113

14+
/// A set of entry point constants for use with the [`EntryPoint`] trait.
15+
pub type EntryPointConstants = &'static [&'static str];
16+
1217
/// A `rust-gpu` entry point for use with [`RustGpuMaterial`](crate::rust_gpu_material::RustGpuMaterial).
1318
pub trait EntryPoint: 'static + Send + Sync {
1419
/// The entry point's base function name, including module path
@@ -31,6 +36,9 @@ pub trait EntryPoint: 'static + Send + Sync {
3136
/// ```
3237
const PARAMETERS: EntryPointParameters;
3338

39+
/// Mapping from bevy shader def values to `permutate-macro` constants
40+
const CONSTANTS: EntryPointConstants;
41+
3442
/// Constructs a permutation set from the provided shader defs
3543
fn permutation(shader_defs: &Vec<ShaderDefVal>) -> Vec<String> {
3644
let mut permutation = vec![];
@@ -52,19 +60,56 @@ pub trait EntryPoint: 'static + Send + Sync {
5260
permutation
5361
}
5462

63+
fn constants(shader_defs: &Vec<ShaderDefVal>) -> Vec<ShaderDefVal> {
64+
shader_defs
65+
.iter()
66+
.filter(|def| match def {
67+
ShaderDefVal::Bool(key, _)
68+
| ShaderDefVal::Int(key, _)
69+
| ShaderDefVal::UInt(key, _) => Self::CONSTANTS.contains(&key.as_str()),
70+
})
71+
.cloned()
72+
.collect()
73+
}
74+
5575
/// Build an entry point name from the provided shader defs
5676
fn build(shader_defs: &Vec<ShaderDefVal>) -> String {
77+
let constants = Self::constants(shader_defs)
78+
.into_iter()
79+
.map(|def| {
80+
(
81+
match &def {
82+
ShaderDefVal::Bool(key, _)
83+
| ShaderDefVal::Int(key, _)
84+
| ShaderDefVal::UInt(key, _) => key.clone(),
85+
},
86+
match &def {
87+
ShaderDefVal::Bool(value, _) => value.to_string(),
88+
ShaderDefVal::Int(_, value) => value.to_string(),
89+
ShaderDefVal::UInt(_, value) => value.to_string(),
90+
},
91+
)
92+
})
93+
.collect::<BTreeMap<_, _>>();
94+
5795
std::iter::once(Self::NAME.to_string())
5896
.chain(
5997
Self::permutation(shader_defs)
6098
.into_iter()
6199
.map(|variant| "__".to_string() + &variant),
62100
)
101+
.chain(
102+
constants
103+
.into_iter()
104+
.map(|(key, value)| key + "_" + &value)
105+
.map(|variant| "__".to_string() + &variant),
106+
)
63107
.collect::<String>()
64108
}
65109
}
66110

67111
impl EntryPoint for () {
68112
const NAME: &'static str = "";
69113
const PARAMETERS: EntryPointParameters = &[];
114+
const CONSTANTS: EntryPointConstants = &[];
70115
}

src/entry_point_export.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010

1111
use bevy::{
1212
prelude::{default, info, CoreSet, Deref, DerefMut, IntoSystemConfig, NonSendMut, Plugin},
13+
render::render_resource::ShaderDefVal,
1314
tasks::IoTaskPool,
1415
utils::HashMap,
1516
};
@@ -53,13 +54,49 @@ type EntryPointReceiver = Receiver<Export>;
5354
pub struct Export {
5455
pub shader: &'static str,
5556
pub permutation: Vec<String>,
57+
pub constants: Vec<ShaderDefVal>,
58+
}
59+
60+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
61+
#[serde(untagged)]
62+
enum PermutationConstant {
63+
Bool(bool),
64+
Uint(u32),
65+
Int(i32),
66+
}
67+
68+
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize, Deref, DerefMut)]
69+
struct PermutationConstants {
70+
#[serde(flatten)]
71+
constants: HashMap<String, PermutationConstant>,
72+
}
73+
74+
impl From<Vec<ShaderDefVal>> for PermutationConstants {
75+
fn from(value: Vec<ShaderDefVal>) -> Self {
76+
PermutationConstants {
77+
constants: value
78+
.into_iter()
79+
.map(|def| match def {
80+
ShaderDefVal::Bool(key, value) => (key, PermutationConstant::Bool(value)),
81+
ShaderDefVal::Int(key, value) => (key, PermutationConstant::Int(value)),
82+
ShaderDefVal::UInt(key, value) => (key, PermutationConstant::Uint(value)),
83+
})
84+
.collect(),
85+
}
86+
}
87+
}
88+
89+
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
90+
struct Permutation {
91+
parameters: Vec<String>,
92+
constants: PermutationConstants,
5693
}
5794

5895
/// Serializable container for a single entry point
5996
#[derive(Debug, Default, Clone, Deref, DerefMut, Serialize, Deserialize)]
6097
struct EntryPoints {
6198
#[serde(flatten)]
62-
entry_points: HashMap<String, Vec<Vec<String>>>,
99+
entry_points: HashMap<String, Vec<Permutation>>,
63100
}
64101

65102
/// Container for a set of entry points, with MPSC handles and change tracking
@@ -110,13 +147,18 @@ impl EntryPointExport {
110147
}
111148

112149
let entry = &export.entry_points[entry_point.shader];
113-
if !entry.contains(&entry_point.permutation) {
114-
info!("New permutation: {:?}", entry_point.permutation);
150+
let permutation = Permutation {
151+
parameters: entry_point.permutation,
152+
constants: entry_point.constants.into(),
153+
};
154+
155+
if !entry.contains(&permutation) {
156+
info!("New permutation: {:?}", permutation);
115157
export
116158
.entry_points
117159
.get_mut(entry_point.shader)
118160
.unwrap()
119-
.push(entry_point.permutation);
161+
.push(permutation);
120162
export.changed = true;
121163
}
122164
}

src/rust_gpu.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ where
4747
M::Data: Clone + Eq + std::hash::Hash,
4848
{
4949
fn build(&self, app: &mut bevy::prelude::App) {
50-
app.add_system(reload_materials::<M>.in_base_set(CoreSet::Last));
51-
app.add_system(shader_events::<M>.before(reload_materials::<M>));
50+
app.add_system(reload_materials::<M>.in_base_set(CoreSet::PreUpdate));
51+
app.add_system(
52+
shader_events::<M>
53+
.in_base_set(CoreSet::PreUpdate)
54+
.before(reload_materials::<M>),
55+
);
5256

5357
app.add_plugin(MaterialPlugin::<RustGpu<M>>::default());
5458

@@ -335,6 +339,7 @@ where
335339
.send(crate::prelude::Export {
336340
shader: M::Vertex::NAME,
337341
permutation: M::Vertex::permutation(&descriptor.vertex.shader_defs),
342+
constants: M::Vertex::constants(&descriptor.vertex.shader_defs),
338343
})
339344
.unwrap();
340345
};
@@ -394,6 +399,7 @@ where
394399
.send(crate::prelude::Export {
395400
shader: M::Fragment::NAME,
396401
permutation: M::Fragment::permutation(&fragment_descriptor.shader_defs),
402+
constants: M::Fragment::constants(&fragment_descriptor.shader_defs),
397403
})
398404
.unwrap();
399405
};

src/shader_meta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ where
5050

5151
app.add_system(
5252
module_meta_events::<M>
53-
.in_base_set(CoreSet::Last)
53+
.in_base_set(CoreSet::PreUpdate)
5454
.after(shader_events::<M>)
5555
.before(reload_materials::<M>),
5656
);

0 commit comments

Comments
 (0)