1mod effects;
8mod transitions;
9
10pub use effects::*;
11pub use transitions::*;
12
13macro_rules! built_module {
15 ($entry:literal) => {
16 include_str!(concat!(env!("OUT_DIR"), "/", $entry, ".wgsl"))
17 };
18}
19
20pub const CANVAS_PRESENT_WGSL: &str = built_module!("canvas-present");
22pub const FULLSCREEN_TEXTURE_QUAD_WGSL: &str = built_module!("fullscreen-quad");
24pub const COMPOSITE_WGSL: &str = built_module!("composite");
26pub const READBACK_PACK_WGSL: &str = built_module!("readback-pack");
28pub const RGBA_PRESENT_WGSL: &str = built_module!("rgba-present");
30pub const RGB_TO_YUV420_WGSL: &str = built_module!("rgb-to-yuv420");
32pub const YUV_REPACK_WGSL: &str = built_module!("yuv-repack");
34pub const YUV_TO_LINEAR_WGSL: &str = built_module!("yuv-to-linear");
36pub const YUV16_TO_LINEAR_WGSL: &str = built_module!("yuv16-to-linear");
38
39#[cfg(test)]
40mod tests {
41 use std::path::{Path, PathBuf};
42
43 fn shader_files(directory: &Path, files: &mut Vec<PathBuf>) {
44 for entry in std::fs::read_dir(directory).expect("shader directory") {
45 let path = entry.expect("shader entry").path();
46 if path.is_dir() {
47 shader_files(&path, files);
48 } else if path
49 .extension()
50 .is_some_and(|extension| extension == "wgsl" || extension == "wesl")
51 {
52 files.push(path);
53 }
54 }
55 }
56
57 fn code(text: &str) -> String {
59 text.lines()
60 .map(|line| line.split("//").next().unwrap_or_default())
61 .flat_map(str::chars)
62 .filter(|character| !character.is_whitespace())
63 .collect()
64 }
65
66 #[test]
67 fn no_built_in_shader_hashes_through_sin() {
68 let mut files = Vec::new();
71 shader_files(
72 &Path::new(env!("CARGO_MANIFEST_DIR")).join("shaders"),
73 &mut files,
74 );
75 assert!(files.len() > 10, "found only {files:?}");
76 for file in files {
77 let text = std::fs::read_to_string(&file).expect("shader text");
78 assert!(!text.contains("/*"), "{}: block comment", file.display());
79 let code = code(&text);
80 assert!(
81 !code.contains("fract(sin("),
82 "{} hashes through fract(sin(...)); use an integer hash (PCG) instead",
83 file.display()
84 );
85 }
86 }
87
88 fn smoothstep_edges(text: &str) -> Vec<(String, String)> {
91 let text = text
92 .lines()
93 .map(|line| line.split("//").next().unwrap_or_default())
94 .collect::<Vec<_>>()
95 .join("\n");
96 let mut found = Vec::new();
97 let mut search = 0;
98 while let Some(offset) = text[search..].find("smoothstep") {
99 let start = search + offset;
100 search = start + "smoothstep".len();
101 let before = text[..start].chars().next_back();
102 let rest = text[search..].trim_start();
103 if before.is_some_and(|character| character.is_ascii_alphanumeric() || character == '_')
104 || !rest.starts_with('(')
105 {
106 continue;
107 }
108 let (mut depth, mut arguments, mut argument) = (0_usize, Vec::new(), String::new());
109 for character in rest[1..].chars() {
110 match character {
111 '(' | '[' => depth += 1,
112 ')' | ']' if depth == 0 => break,
113 ')' | ']' => depth -= 1,
114 ',' if depth == 0 => {
115 arguments.push(std::mem::take(&mut argument));
116 continue;
117 }
118 _ if character.is_whitespace() => continue,
119 _ => {}
120 }
121 argument.push(character);
122 }
123 if !argument.is_empty() {
125 arguments.push(argument);
126 }
127 let [edge0, edge1, _] = <[String; 3]>::try_from(arguments).expect("three arguments");
128 found.push((edge0, edge1));
129 }
130 found
131 }
132
133 #[test]
134 fn built_in_shaders_call_smoothstep_only_with_ascending_edges() {
135 let ascending = [
139 ("cross-warp.wgsl", "0.0", "1.0", "constants"),
140 ("ripple.wgsl", "0.2", "1.0", "constants"),
141 ("window-blinds.wgsl", "0.8", "1.0", "constants"),
142 (
143 "simple-zoom.wgsl",
144 "0.0",
145 "quickness",
146 "quickness is clamped to 0.2..1",
147 ),
148 (
149 "simple-zoom.wgsl",
150 "quickness-0.2",
151 "1.0",
152 "quickness is clamped to 0.2..1",
153 ),
154 (
155 "directional-warp.wgsl",
156 "-params.smoothness",
157 "0.0",
158 "smoothness >= 0.0001",
159 ),
160 (
161 "crop.wgsl",
162 "line-feather",
163 "line+feather",
164 "feather >= 0.0001, line in 0..1",
165 ),
166 (
167 "vignette.wgsl",
168 "params.radius-params.softness",
169 "params.radius+params.softness",
170 "softness >= 0.01, radius <= 1.5",
171 ),
172 (
175 "chroma-key.wgsl",
176 "params.threshold",
177 "params.threshold+params.softness",
178 "softness >= f32::EPSILON, threshold <= sqrt(3)",
179 ),
180 ];
181 let mut files = Vec::new();
182 shader_files(
183 &Path::new(env!("CARGO_MANIFEST_DIR")).join("shaders"),
184 &mut files,
185 );
186 let mut used = vec![false; ascending.len()];
187 for file in files {
188 let name = file
189 .file_name()
190 .and_then(|name| name.to_str())
191 .expect("name");
192 let text = std::fs::read_to_string(&file).expect("shader text");
193 for (edge0, edge1) in smoothstep_edges(&text) {
194 let listed = ascending.iter().position(|&(listed, low, high, _)| {
195 (listed, low, high) == (name, edge0.as_str(), edge1.as_str())
196 });
197 let Some(index) = listed else {
198 panic!(
199 "{}: smoothstep({edge0}, {edge1}, ...) is not known to be ascending; \
200 call builtin_smoothstep instead",
201 file.display()
202 );
203 };
204 used[index] = true;
205 }
206 }
207 for (index, entry) in ascending.iter().enumerate() {
208 assert!(used[index], "no longer called: {entry:?}");
209 }
210 }
211}