1mod transitions;
15
16pub use transitions::{
17 ANGULAR, CIRCLE, CIRCLE_CROP, CIRCLE_OPEN, COLOR_PHASE, COLOUR_DISTANCE, CROSS_WARP,
18 DIRECTIONAL, DIRECTIONAL_WARP, DIRECTIONAL_WIPE, DREAMY, FADE, FADE_COLOR, FADE_GRAYSCALE,
19 HEART, MORPH, PINWHEEL, PIXELIZE, POLKA_DOTS_CURTAIN, RADIAL, RIPPLE, SIMPLE_ZOOM, SQUEEZE,
20 TRANSITION_HELPERS, WIND, WINDOW_BLINDS, WINDOW_SLICE, WIPE_DOWN, WIPE_LEFT, WIPE_RIGHT,
21 WIPE_UP,
22};
23
24macro_rules! built_module {
26 ($entry:literal) => {
27 include_str!(concat!(env!("OUT_DIR"), "/", $entry, ".wgsl"))
28 };
29}
30
31pub const CANVAS_PRESENT_WGSL: &str = built_module!("canvas-present");
33pub const FULLSCREEN_TEXTURE_QUAD_WGSL: &str = built_module!("fullscreen-quad");
35pub const COMPOSITE_WGSL: &str = built_module!("composite");
37pub const READBACK_PACK_WGSL: &str = built_module!("readback-pack");
39pub const RGBA_PRESENT_WGSL: &str = built_module!("rgba-present");
41pub const RGB_TO_YUV420_WGSL: &str = built_module!("rgb-to-yuv420");
43pub const YUV_REPACK_WGSL: &str = built_module!("yuv-repack");
45pub const YUV_TO_LINEAR_WGSL: &str = built_module!("yuv-to-linear");
47pub const YUV16_TO_LINEAR_WGSL: &str = built_module!("yuv16-to-linear");
50
51pub const CHROMA_KEY_BODY: &str = include_str!("../shaders/runtime/effects/chroma-key.wgsl");
53pub const GAUSSIAN_BLUR: &str = include_str!("../shaders/runtime/effects/gaussian-blur.wgsl");
55pub const LUT_3D_BODY: &str = include_str!("../shaders/runtime/effects/lut-3d.wgsl");
57pub const EXPANDED_EFFECT: &str = include_str!("../shaders/runtime/effects/expanded.wgsl");
59pub const COLOR_CONTROLS: &str = include_str!("../shaders/runtime/effects/color-controls.wgsl");
61pub const VIGNETTE: &str = include_str!("../shaders/runtime/effects/vignette.wgsl");
63pub const SHARPEN: &str = include_str!("../shaders/runtime/effects/sharpen.wgsl");
65pub const PIXELATE_EFFECT: &str = include_str!("../shaders/runtime/effects/pixelate.wgsl");
67pub const GLOW_THRESHOLD: &str = include_str!("../shaders/runtime/effects/glow-threshold.wgsl");
69pub const GLOW_COMBINE: &str = include_str!("../shaders/runtime/effects/glow-combine.wgsl");
71pub const FILM_GRAIN: &str = include_str!("../shaders/runtime/effects/film-grain.wgsl");
73pub const CROP: &str = include_str!("../shaders/runtime/effects/crop.wgsl");
75pub const MIRROR: &str = include_str!("../shaders/runtime/effects/mirror.wgsl");
77
78#[cfg(test)]
79mod tests {
80 use std::path::{Path, PathBuf};
81
82 fn shader_files(directory: &Path, files: &mut Vec<PathBuf>) {
83 for entry in std::fs::read_dir(directory).expect("shader directory") {
84 let path = entry.expect("shader entry").path();
85 if path.is_dir() {
86 shader_files(&path, files);
87 } else if path
88 .extension()
89 .is_some_and(|extension| extension == "wgsl" || extension == "wesl")
90 {
91 files.push(path);
92 }
93 }
94 }
95
96 fn code(text: &str) -> String {
99 text.lines()
100 .map(|line| line.split("//").next().unwrap_or_default())
101 .flat_map(str::chars)
102 .filter(|character| !character.is_whitespace())
103 .collect()
104 }
105
106 #[test]
107 fn no_built_in_shader_hashes_through_sin() {
108 let mut files = Vec::new();
111 shader_files(
112 &Path::new(env!("CARGO_MANIFEST_DIR")).join("shaders"),
113 &mut files,
114 );
115 assert!(files.len() > 10, "found only {files:?}");
116 for file in files {
117 let text = std::fs::read_to_string(&file).expect("shader text");
118 assert!(!text.contains("/*"), "{}: block comment", file.display());
119 let code = code(&text);
120 assert!(
121 !code.contains("fract(sin("),
122 "{} hashes through fract(sin(...)); use an integer hash (PCG) instead",
123 file.display()
124 );
125 }
126 }
127
128 fn smoothstep_edges(text: &str) -> Vec<(String, String)> {
132 let text = text
133 .lines()
134 .map(|line| line.split("//").next().unwrap_or_default())
135 .collect::<Vec<_>>()
136 .join("\n");
137 let mut found = Vec::new();
138 let mut search = 0;
139 while let Some(offset) = text[search..].find("smoothstep") {
140 let start = search + offset;
141 search = start + "smoothstep".len();
142 let before = text[..start].chars().next_back();
143 let rest = text[search..].trim_start();
144 if before.is_some_and(|character| character.is_ascii_alphanumeric() || character == '_')
145 || !rest.starts_with('(')
146 {
147 continue;
148 }
149 let (mut depth, mut arguments, mut argument) = (0_usize, Vec::new(), String::new());
150 for character in rest[1..].chars() {
151 match character {
152 '(' | '[' => depth += 1,
153 ')' | ']' if depth == 0 => break,
154 ')' | ']' => depth -= 1,
155 ',' if depth == 0 => {
156 arguments.push(std::mem::take(&mut argument));
157 continue;
158 }
159 _ if character.is_whitespace() => continue,
160 _ => {}
161 }
162 argument.push(character);
163 }
164 if !argument.is_empty() {
166 arguments.push(argument);
167 }
168 let [edge0, edge1, _] = <[String; 3]>::try_from(arguments).expect("three arguments");
169 found.push((edge0, edge1));
170 }
171 found
172 }
173
174 #[test]
175 fn built_in_shaders_call_smoothstep_only_with_ascending_edges() {
176 let ascending = [
180 ("cross-warp.wgsl", "0.0", "1.0", "constants"),
181 ("ripple.wgsl", "0.2", "1.0", "constants"),
182 ("window-blinds.wgsl", "0.8", "1.0", "constants"),
183 (
184 "simple-zoom.wgsl",
185 "0.0",
186 "quickness",
187 "quickness is clamped to 0.2..1",
188 ),
189 (
190 "simple-zoom.wgsl",
191 "quickness-0.2",
192 "1.0",
193 "quickness is clamped to 0.2..1",
194 ),
195 (
196 "directional-warp.wgsl",
197 "-params.smoothness",
198 "0.0",
199 "smoothness >= 0.0001",
200 ),
201 (
202 "crop.wgsl",
203 "line-feather",
204 "line+feather",
205 "feather >= 0.0001, line in 0..1",
206 ),
207 (
208 "vignette.wgsl",
209 "params.radius-params.softness",
210 "params.radius+params.softness",
211 "softness >= 0.01, radius <= 1.5",
212 ),
213 (
216 "chroma-key.wgsl",
217 "params.threshold",
218 "params.threshold+params.softness",
219 "softness >= f32::EPSILON, threshold <= sqrt(3)",
220 ),
221 ];
222 let mut files = Vec::new();
223 shader_files(
224 &Path::new(env!("CARGO_MANIFEST_DIR")).join("shaders"),
225 &mut files,
226 );
227 let mut used = vec![false; ascending.len()];
228 for file in files {
229 let name = file
230 .file_name()
231 .and_then(|name| name.to_str())
232 .expect("name");
233 let text = std::fs::read_to_string(&file).expect("shader text");
234 for (edge0, edge1) in smoothstep_edges(&text) {
235 let listed = ascending.iter().position(|&(listed, low, high, _)| {
236 (listed, low, high) == (name, edge0.as_str(), edge1.as_str())
237 });
238 let Some(index) = listed else {
239 panic!(
240 "{}: smoothstep({edge0}, {edge1}, ...) is not known to be ascending; \
241 call builtin_smoothstep instead",
242 file.display()
243 );
244 };
245 used[index] = true;
246 }
247 }
248 for (index, entry) in ascending.iter().enumerate() {
249 assert!(used[index], "no longer called: {entry:?}");
250 }
251 }
252}