mod effects;
mod transitions;
pub use effects::*;
pub use transitions::*;
macro_rules! built_module {
($entry:literal) => {
include_str!(concat!(env!("OUT_DIR"), "/", $entry, ".wgsl"))
};
}
pub const CANVAS_PRESENT_WGSL: &str = built_module!("canvas-present");
pub const FULLSCREEN_TEXTURE_QUAD_WGSL: &str = built_module!("fullscreen-quad");
pub const COMPOSITE_WGSL: &str = built_module!("composite");
pub const READBACK_PACK_WGSL: &str = built_module!("readback-pack");
pub const RGBA_PRESENT_WGSL: &str = built_module!("rgba-present");
pub const RGB_TO_YUV420_WGSL: &str = built_module!("rgb-to-yuv420");
pub const YUV_REPACK_WGSL: &str = built_module!("yuv-repack");
pub const YUV_TO_LINEAR_WGSL: &str = built_module!("yuv-to-linear");
pub const YUV16_TO_LINEAR_WGSL: &str = built_module!("yuv16-to-linear");
#[cfg(test)]
mod tests {
use std::path::{Path, PathBuf};
fn shader_files(directory: &Path, files: &mut Vec<PathBuf>) {
for entry in std::fs::read_dir(directory).expect("shader directory") {
let path = entry.expect("shader entry").path();
if path.is_dir() {
shader_files(&path, files);
} else if path
.extension()
.is_some_and(|extension| extension == "wgsl" || extension == "wesl")
{
files.push(path);
}
}
}
fn code(text: &str) -> String {
text.lines()
.map(|line| line.split("//").next().unwrap_or_default())
.flat_map(str::chars)
.filter(|character| !character.is_whitespace())
.collect()
}
#[test]
fn no_built_in_shader_hashes_through_sin() {
let mut files = Vec::new();
shader_files(
&Path::new(env!("CARGO_MANIFEST_DIR")).join("shaders"),
&mut files,
);
assert!(files.len() > 10, "found only {files:?}");
for file in files {
let text = std::fs::read_to_string(&file).expect("shader text");
assert!(!text.contains("/*"), "{}: block comment", file.display());
let code = code(&text);
assert!(
!code.contains("fract(sin("),
"{} hashes through fract(sin(...)); use an integer hash (PCG) instead",
file.display()
);
}
}
fn smoothstep_edges(text: &str) -> Vec<(String, String)> {
let text = text
.lines()
.map(|line| line.split("//").next().unwrap_or_default())
.collect::<Vec<_>>()
.join("\n");
let mut found = Vec::new();
let mut search = 0;
while let Some(offset) = text[search..].find("smoothstep") {
let start = search + offset;
search = start + "smoothstep".len();
let before = text[..start].chars().next_back();
let rest = text[search..].trim_start();
if before.is_some_and(|character| character.is_ascii_alphanumeric() || character == '_')
|| !rest.starts_with('(')
{
continue;
}
let (mut depth, mut arguments, mut argument) = (0_usize, Vec::new(), String::new());
for character in rest[1..].chars() {
match character {
'(' | '[' => depth += 1,
')' | ']' if depth == 0 => break,
')' | ']' => depth -= 1,
',' if depth == 0 => {
arguments.push(std::mem::take(&mut argument));
continue;
}
_ if character.is_whitespace() => continue,
_ => {}
}
argument.push(character);
}
if !argument.is_empty() {
arguments.push(argument);
}
let [edge0, edge1, _] = <[String; 3]>::try_from(arguments).expect("three arguments");
found.push((edge0, edge1));
}
found
}
#[test]
fn built_in_shaders_call_smoothstep_only_with_ascending_edges() {
let ascending = [
("cross-warp.wgsl", "0.0", "1.0", "constants"),
("ripple.wgsl", "0.2", "1.0", "constants"),
("window-blinds.wgsl", "0.8", "1.0", "constants"),
(
"simple-zoom.wgsl",
"0.0",
"quickness",
"quickness is clamped to 0.2..1",
),
(
"simple-zoom.wgsl",
"quickness-0.2",
"1.0",
"quickness is clamped to 0.2..1",
),
(
"directional-warp.wgsl",
"-params.smoothness",
"0.0",
"smoothness >= 0.0001",
),
(
"crop.wgsl",
"line-feather",
"line+feather",
"feather >= 0.0001, line in 0..1",
),
(
"vignette.wgsl",
"params.radius-params.softness",
"params.radius+params.softness",
"softness >= 0.01, radius <= 1.5",
),
(
"chroma-key.wgsl",
"params.threshold",
"params.threshold+params.softness",
"softness >= f32::EPSILON, threshold <= sqrt(3)",
),
];
let mut files = Vec::new();
shader_files(
&Path::new(env!("CARGO_MANIFEST_DIR")).join("shaders"),
&mut files,
);
let mut used = vec![false; ascending.len()];
for file in files {
let name = file
.file_name()
.and_then(|name| name.to_str())
.expect("name");
let text = std::fs::read_to_string(&file).expect("shader text");
for (edge0, edge1) in smoothstep_edges(&text) {
let listed = ascending.iter().position(|&(listed, low, high, _)| {
(listed, low, high) == (name, edge0.as_str(), edge1.as_str())
});
let Some(index) = listed else {
panic!(
"{}: smoothstep({edge0}, {edge1}, ...) is not known to be ascending; \
call builtin_smoothstep instead",
file.display()
);
};
used[index] = true;
}
}
for (index, entry) in ascending.iter().enumerate() {
assert!(used[index], "no longer called: {entry:?}");
}
}
}