pub const COMMON_WGSL: &str = include_str!("gpu/common.wgsl");
#[must_use]
pub fn wgsl(body: &str) -> String {
let mut s = String::with_capacity(COMMON_WGSL.len() + body.len() + 1);
s.push_str(COMMON_WGSL);
s.push('\n');
s.push_str(body);
s
}
pub const CULL_WGSL: &str = include_str!("gpu/cull.wgsl");
pub const DRAW_WGSL: &str = include_str!("gpu/draw.wgsl");
pub const SDF_WGSL: &str = include_str!("gpu/sdf.wgsl");
pub const LINE_WGSL: &str = include_str!("gpu/line.wgsl");
pub const OIT_WGSL: &str = include_str!("gpu/oit.wgsl");
pub const PICK_WGSL: &str = include_str!("gpu/pick.wgsl");
pub const BLUR_WGSL: &str = include_str!("gpu/blur.wgsl");
pub const COLORGRADE_WGSL: &str = include_str!("gpu/colorgrade.wgsl");
pub const MSDF_WGSL: &str = include_str!("gpu/msdf.wgsl");
pub const TAA_WGSL: &str = include_str!("gpu/taa.wgsl");
pub const PARTICLES_WGSL: &str = include_str!("gpu/particles.wgsl");
pub const LABEL_COLLIDE_WGSL: &str = include_str!("gpu/label_collide.wgsl");
pub const LABEL_DRAW_WGSL: &str = include_str!("gpu/label_draw.wgsl");
pub const GRAPHCLOUD_WGSL: &str = include_str!("gpu/graphcloud.wgsl");
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cull_shader_keeps_its_per_way_contract() {
assert!(CULL_WGSL.contains("override VPV: u32 = 6u;"), "VPV is overridable and still defaults to 6");
assert!(!CULL_WGSL.contains("const VPV"), "VPV must not go back to a baked const — that forces a twin shader");
assert!(CULL_WGSL.contains("@workgroup_size(64)"));
for entry in ["fn count_pass(", "fn scan_pass(", "fn main("] {
assert!(CULL_WGSL.contains(entry), "the ordered compaction's `{entry}` entry point is gone");
}
let code: String =
CULL_WGSL.lines().filter(|l| !l.trim_start().starts_with("//")).collect::<Vec<_>>().join("\n");
assert!(
!code.contains("atomicAdd"),
"the cull must not claim its destination with `atomicAdd` — that is arrival order, \
not paint order, and it is not even stable between two runs of the same dispatch"
);
assert!(code.contains("atomicStore"), "the scan pass must still WRITE the count it computed");
assert!(!CULL_WGSL.contains("binding(5)"), "binding 5 would take the cull over the 4-storage-buffer floor");
}
#[test]
fn every_shared_shader_takes_its_maths_from_the_one_prelude() {
for (name, body) in
[("sdf", SDF_WGSL), ("line", LINE_WGSL), ("draw", DRAW_WGSL), ("oit", OIT_WGSL)]
{
assert!(
!body.contains("fn px_to_ndc"),
"{name}.wgsl re-declares px_to_ndc instead of using the prelude"
);
assert!(
!body.contains("fn coverage_from_sd"),
"{name}.wgsl re-declares coverage_from_sd instead of using the prelude"
);
assert!(body.contains("px_to_ndc("), "{name}.wgsl calls the shared px_to_ndc");
let composed = wgsl(body);
assert!(composed.starts_with(COMMON_WGSL), "{name}.wgsl is composed prelude-first");
assert!(composed.contains("fn px_to_ndc"), "the composed {name} module defines px_to_ndc once");
assert_eq!(
composed.matches("fn px_to_ndc").count(),
1,
"exactly ONE definition of px_to_ndc reaches the {name} shader module"
);
}
assert!(COMMON_WGSL.contains("fn resolve_line_width_px"));
assert!(COMMON_WGSL.contains("fn coverage_inside"));
}
#[test]
fn every_wgsl_file_is_included_here_and_nowhere_else() {
let src = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
let me = src.join("render/wgsl.rs");
let my_text = std::fs::read_to_string(&me).expect("this file is readable");
let shaders: Vec<String> = std::fs::read_dir(src.join("render/gpu"))
.expect("render/gpu exists")
.filter_map(|e| {
let n = e.ok()?.file_name().to_string_lossy().into_owned();
n.ends_with(".wgsl").then_some(n)
})
.collect();
assert!(shaders.len() >= 14, "the shader set shrank unexpectedly: {}", shaders.len());
for s in &shaders {
let want = format!("include_str!(\"gpu/{s}\")");
assert!(
my_text.contains(&want),
"{s} is not included by render/wgsl.rs — no shader-text guard can reach it \
(expected the literal `{want}`)"
);
}
let mut stack = vec![src.clone()];
let mut strays: Vec<String> = Vec::new();
while let Some(dir) = stack.pop() {
for e in std::fs::read_dir(&dir).expect("readable dir").flatten() {
let p = e.path();
if p.is_dir() {
stack.push(p);
} else if p.extension().is_some_and(|x| x == "rs") && p != me {
let t = std::fs::read_to_string(&p).unwrap_or_default();
for line in t.lines() {
let l = line.trim_start();
if l.starts_with("//") {
continue;
}
if l.contains("include_str!(") && l.contains(".wgsl") {
strays.push(format!(
"{}: {}",
p.strip_prefix(&src).unwrap_or(&p).display(),
l.trim()
));
}
}
}
}
}
assert!(
strays.is_empty(),
"a second include_str of shader text — the twin render::wgsl exists to prevent:\n {}",
strays.join("\n ")
);
}
}