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`
This commit is contained in:
François 2022-12-07 23:10:27 +00:00
parent bac0d89059
commit 8eedc8f69d
4 changed files with 35 additions and 18 deletions

View file

@ -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)

View file

@ -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) {

View file

@ -123,6 +123,7 @@ struct ShaderCache {
pub enum ShaderDefVal {
Bool(String, bool),
Int(String, i32),
UInt(String, u32),
}
impl From<&str> for ShaderDefVal {
@ -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,
@ -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!(

View file

@ -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();
@ -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) {
@ -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();
}
}