pub const BATCH_METADATA_STRUCT: &str = r#"
struct BatchMeta {
input_offset: u32,
input_len: u32,
output_offset: u32,
output_len: u32,
};
"#;
pub const COMMON_COMPUTE_UTILS: &str = r#"
fn ceil_div_u32(value: u32, divisor: u32) -> u32 {
return (value + divisor - 1u) / divisor;
}
fn in_bounds(index: u32, len: u32) -> bool {
return index < len;
}
"#;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ShaderSource {
pub label: String,
pub source: String,
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ShaderSourceBuilder {
label: String,
chunks: Vec<String>,
}
impl ShaderSourceBuilder {
pub fn new(label: impl Into<String>) -> Self {
Self {
label: label.into(),
chunks: Vec::new(),
}
}
pub fn with_batch_metadata(mut self) -> Self {
self.chunks.push(BATCH_METADATA_STRUCT.trim().to_owned());
self
}
pub fn with_common_compute_utils(mut self) -> Self {
self.chunks.push(COMMON_COMPUTE_UTILS.trim().to_owned());
self
}
pub fn with_source(mut self, source: impl Into<String>) -> Self {
self.chunks.push(source.into());
self
}
pub fn finish(self) -> ShaderSource {
ShaderSource {
label: self.label,
source: self.chunks.join("\n\n"),
}
}
}