use super::identity::ShaderId;
pub const SHADER_PARAMS_MAX_BYTES: u32 = 4096;
pub const SHADER_LOOP_BUDGET: u64 = 256;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShaderSource {
pub wgsl: String,
pub label: String,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MaterialShader {
pub shader: ShaderId,
pub displacement_bound: f32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShaderParamsLayout {
pub size: u32,
pub fields: Vec<ShaderParamField>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShaderParamField {
pub name: String,
pub offset: u32,
pub size: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ForbiddenItem {
Variable,
Override,
Binding,
EntryPoint,
Directive,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ShaderError {
Parse {
message: String,
line: u32,
column: u32,
},
Validate { message: String },
MissingFunction { name: &'static str },
Signature {
name: &'static str,
expected: &'static str,
},
Forbidden { item: ForbiddenItem, name: String },
Discard { function: String },
UnboundedLoop { function: String },
LoopBudget { function: String, iterations: u64 },
SceneDepthInVertex { function: String },
NonUniformDerivative { function: String },
NameTaken { name: String },
ParamsTooLarge { size: u32, max: u32 },
}
impl std::fmt::Display for ShaderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Parse {
message,
line,
column,
} => write!(f, "{line}:{column}: {message}"),
Self::Validate { message } => f.write_str(message),
Self::MissingFunction { name } => write!(f, "the shader declares no {name}"),
Self::Signature { name, expected } => {
write!(f, "{name} must be declared as {expected}")
}
Self::Forbidden { item, name } => {
write!(f, "a shader may not declare {item:?} {name}")
}
Self::Discard { function } => write!(f, "{function} discards"),
Self::UnboundedLoop { function } => {
write!(f, "{function} has a loop that is not a counted loop")
}
Self::LoopBudget {
function,
iterations,
} => write!(
f,
"{function} makes {iterations} loop iterations, more than {SHADER_LOOP_BUDGET}"
),
Self::SceneDepthInVertex { function } => write!(
f,
"{function} reads the scene depth from material_vertex, which only material_surface may"
),
Self::NonUniformDerivative { function } => write!(
f,
"{function} takes a derivative within control flow, which WGSL allows only in uniform control flow"
),
Self::NameTaken { name } => write!(f, "SGL3D already declares {name}"),
Self::ParamsTooLarge { size, max } => {
write!(f, "ShaderParams takes {size} bytes, more than {max}")
}
}
}
}
impl std::error::Error for ShaderError {}