Skip to content

Commit

Permalink
ShaderDefVal: add an UInt option (bevyengine#6881)
Browse files Browse the repository at this point in the history
# Objective

- Fixes bevyengine#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`
  • Loading branch information
mockersf authored and alradish committed Jan 22, 2023
1 parent 2f09508 commit 87b2b12
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ impl SpecializedMeshPipeline for ShadowPipeline {

let mut bind_group_layout = vec![self.view_layout.clone()];
let mut shader_defs = Vec::new();
shader_defs.push(ShaderDefVal::Int(
shader_defs.push(ShaderDefVal::UInt(
"MAX_DIRECTIONAL_LIGHTS".to_string(),
MAX_DIRECTIONAL_LIGHTS as i32,
MAX_DIRECTIONAL_LIGHTS as u32,
));

if layout.contains(Mesh::ATTRIBUTE_JOINT_INDEX)
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,9 @@ impl SpecializedMeshPipeline for MeshPipeline {
vertex_attributes.push(Mesh::ATTRIBUTE_NORMAL.at_shader_location(1));
}

shader_defs.push(ShaderDefVal::Int(
shader_defs.push(ShaderDefVal::UInt(
"MAX_DIRECTIONAL_LIGHTS".to_string(),
MAX_DIRECTIONAL_LIGHTS as i32,
MAX_DIRECTIONAL_LIGHTS as u32,
));

if layout.contains(Mesh::ATTRIBUTE_UV_0) {
Expand Down
15 changes: 13 additions & 2 deletions crates/bevy_render/src/render_resource/pipeline_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ struct ShaderCache {
pub enum ShaderDefVal {
Bool(String, bool),
Int(String, i32),
UInt(String, u32),
}

impl From<&str> for ShaderDefVal {
Expand All @@ -137,6 +138,16 @@ impl From<String> for ShaderDefVal {
}
}

impl ShaderDefVal {
pub fn value_as_string(&self) -> String {
match self {
ShaderDefVal::Bool(_, def) => def.to_string(),
ShaderDefVal::Int(_, def) => def.to_string(),
ShaderDefVal::UInt(_, def) => def.to_string(),
}
}
}

impl ShaderCache {
fn get(
&mut self,
Expand Down Expand Up @@ -176,9 +187,9 @@ impl ShaderCache {
shader_defs.push("SIXTEEN_BYTE_ALIGNMENT".into());
}

shader_defs.push(ShaderDefVal::Int(
shader_defs.push(ShaderDefVal::UInt(
String::from("AVAILABLE_STORAGE_BUFFER_BINDINGS"),
render_device.limits().max_storage_buffers_per_shader_stage as i32,
render_device.limits().max_storage_buffers_per_shader_stage,
));

debug!(
Expand Down
30 changes: 18 additions & 12 deletions crates/bevy_render/src/render_resource/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ impl ShaderProcessor {

let shader_defs_unique =
HashMap::<String, ShaderDefVal>::from_iter(shader_defs.iter().map(|v| match v {
ShaderDefVal::Bool(k, _) | ShaderDefVal::Int(k, _) => (k.clone(), v.clone()),
ShaderDefVal::Bool(k, _) | ShaderDefVal::Int(k, _) | ShaderDefVal::UInt(k, _) => {
(k.clone(), v.clone())
}
}));
let mut scopes = vec![true];
let mut final_string = String::new();
Expand Down Expand Up @@ -484,6 +486,16 @@ impl ShaderProcessor {
})?;
act_on(*def, val, op.as_str())?
}
ShaderDefVal::UInt(name, def) => {
let val = val.as_str().parse().map_err(|_| {
ProcessShaderError::InvalidShaderDefComparisonValue {
shader_def_name: name.clone(),
value: val.as_str().to_string(),
expected: "uint".to_string(),
}
})?;
act_on(*def, val, op.as_str())?
}
};
scopes.push(*scopes.last().unwrap() && new_scope);
} else if self.else_regex.is_match(line) {
Expand Down Expand Up @@ -536,24 +548,18 @@ impl ShaderProcessor {
for capture in self.def_regex.captures_iter(line) {
let def = capture.get(1).unwrap();
if let Some(def) = shader_defs_unique.get(def.as_str()) {
let def = match def {
ShaderDefVal::Bool(_, def) => def.to_string(),
ShaderDefVal::Int(_, def) => def.to_string(),
};
line_with_defs =
self.def_regex.replace(&line_with_defs, def).to_string();
line_with_defs = self
.def_regex
.replace(&line_with_defs, def.value_as_string())
.to_string();
}
}
for capture in self.def_regex_delimited.captures_iter(line) {
let def = capture.get(1).unwrap();
if let Some(def) = shader_defs_unique.get(def.as_str()) {
let def = match def {
ShaderDefVal::Bool(_, def) => def.to_string(),
ShaderDefVal::Int(_, def) => def.to_string(),
};
line_with_defs = self
.def_regex_delimited
.replace(&line_with_defs, def)
.replace(&line_with_defs, def.value_as_string())
.to_string();
}
}
Expand Down

0 comments on commit 87b2b12

Please sign in to comment.