Skip to content

Commit 8eedc8f

Browse files
committed
ShaderDefVal: add an UInt option (#6881)
# Objective - Fixes #6841 - In some case, the number of maximum storage buffers is `u32::MAX` which doesn't fit in a `i32` ## Solution - Add an option to have a `u32` in a `ShaderDefVal`
1 parent bac0d89 commit 8eedc8f

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

crates/bevy_pbr/src/render/light.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,9 @@ impl SpecializedMeshPipeline for ShadowPipeline {
321321

322322
let mut bind_group_layout = vec![self.view_layout.clone()];
323323
let mut shader_defs = Vec::new();
324-
shader_defs.push(ShaderDefVal::Int(
324+
shader_defs.push(ShaderDefVal::UInt(
325325
"MAX_DIRECTIONAL_LIGHTS".to_string(),
326-
MAX_DIRECTIONAL_LIGHTS as i32,
326+
MAX_DIRECTIONAL_LIGHTS as u32,
327327
));
328328

329329
if layout.contains(Mesh::ATTRIBUTE_JOINT_INDEX)

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,9 @@ impl SpecializedMeshPipeline for MeshPipeline {
590590
vertex_attributes.push(Mesh::ATTRIBUTE_NORMAL.at_shader_location(1));
591591
}
592592

593-
shader_defs.push(ShaderDefVal::Int(
593+
shader_defs.push(ShaderDefVal::UInt(
594594
"MAX_DIRECTIONAL_LIGHTS".to_string(),
595-
MAX_DIRECTIONAL_LIGHTS as i32,
595+
MAX_DIRECTIONAL_LIGHTS as u32,
596596
));
597597

598598
if layout.contains(Mesh::ATTRIBUTE_UV_0) {

crates/bevy_render/src/render_resource/pipeline_cache.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ struct ShaderCache {
123123
pub enum ShaderDefVal {
124124
Bool(String, bool),
125125
Int(String, i32),
126+
UInt(String, u32),
126127
}
127128

128129
impl From<&str> for ShaderDefVal {
@@ -137,6 +138,16 @@ impl From<String> for ShaderDefVal {
137138
}
138139
}
139140

141+
impl ShaderDefVal {
142+
pub fn value_as_string(&self) -> String {
143+
match self {
144+
ShaderDefVal::Bool(_, def) => def.to_string(),
145+
ShaderDefVal::Int(_, def) => def.to_string(),
146+
ShaderDefVal::UInt(_, def) => def.to_string(),
147+
}
148+
}
149+
}
150+
140151
impl ShaderCache {
141152
fn get(
142153
&mut self,
@@ -176,9 +187,9 @@ impl ShaderCache {
176187
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
177188
}
178189

179-
shader_defs.push(ShaderDefVal::Int(
190+
shader_defs.push(ShaderDefVal::UInt(
180191
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
181-
render_device.limits().max_storage_buffers_per_shader_stage as i32,
192+
render_device.limits().max_storage_buffers_per_shader_stage,
182193
));
183194

184195
debug!(

crates/bevy_render/src/render_resource/shader.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ impl ShaderProcessor {
425425

426426
let shader_defs_unique =
427427
HashMap::<String, ShaderDefVal>::from_iter(shader_defs.iter().map(|v| match v {
428-
ShaderDefVal::Bool(k, _) | ShaderDefVal::Int(k, _) => (k.clone(), v.clone()),
428+
ShaderDefVal::Bool(k, _) | ShaderDefVal::Int(k, _) | ShaderDefVal::UInt(k, _) => {
429+
(k.clone(), v.clone())
430+
}
429431
}));
430432
let mut scopes = vec![true];
431433
let mut final_string = String::new();
@@ -484,6 +486,16 @@ impl ShaderProcessor {
484486
})?;
485487
act_on(*def, val, op.as_str())?
486488
}
489+
ShaderDefVal::UInt(name, def) => {
490+
let val = val.as_str().parse().map_err(|_| {
491+
ProcessShaderError::InvalidShaderDefComparisonValue {
492+
shader_def_name: name.clone(),
493+
value: val.as_str().to_string(),
494+
expected: "uint".to_string(),
495+
}
496+
})?;
497+
act_on(*def, val, op.as_str())?
498+
}
487499
};
488500
scopes.push(*scopes.last().unwrap() && new_scope);
489501
} else if self.else_regex.is_match(line) {
@@ -536,24 +548,18 @@ impl ShaderProcessor {
536548
for capture in self.def_regex.captures_iter(line) {
537549
let def = capture.get(1).unwrap();
538550
if let Some(def) = shader_defs_unique.get(def.as_str()) {
539-
let def = match def {
540-
ShaderDefVal::Bool(_, def) => def.to_string(),
541-
ShaderDefVal::Int(_, def) => def.to_string(),
542-
};
543-
line_with_defs =
544-
self.def_regex.replace(&line_with_defs, def).to_string();
551+
line_with_defs = self
552+
.def_regex
553+
.replace(&line_with_defs, def.value_as_string())
554+
.to_string();
545555
}
546556
}
547557
for capture in self.def_regex_delimited.captures_iter(line) {
548558
let def = capture.get(1).unwrap();
549559
if let Some(def) = shader_defs_unique.get(def.as_str()) {
550-
let def = match def {
551-
ShaderDefVal::Bool(_, def) => def.to_string(),
552-
ShaderDefVal::Int(_, def) => def.to_string(),
553-
};
554560
line_with_defs = self
555561
.def_regex_delimited
556-
.replace(&line_with_defs, def)
562+
.replace(&line_with_defs, def.value_as_string())
557563
.to_string();
558564
}
559565
}

0 commit comments

Comments
 (0)