1//! GPU unit tests for all 9 Wave 36 tools (`VectorDenoise`, `Mandel`,
2//! `UnsharpMask`, `Letterbox`, `Fuse.Wipe`, `Fuse.Null`, `Fuse.SetMetaData`,
3//! `Fuse.Linger`, `Fuse.OCLRays`).
4
5use super::*;
6
7fn comp(tools: &str) -> Comp {
8 let src = format!(
9 "Composition {{ CurrentTime = 0, RenderRange = {{ 0, 10 }}, GlobalRange = {{ 0, 10 }}, Tools = ordered() {{ {tools} }} }}"
10 );
11 Comp::from_fusion_str(&src).expect("parse comp")
12}
13
14fn render(c: &Comp, name: &str, time: f64) -> Option<(CachedTexture, Vec<u8>)> {
15 let mut eng = Engine::new_headless().ok()?;
16 let id = c.find_node(name).expect("find node");
17 let (tex, stats) = eng.evaluate(c, id, time);
18 assert!(stats.unsupported_nodes.is_empty(), "unexpected unsupported: {:?}", stats.unsupported_nodes);
19 let px = eng.readback_rgba(&tex).unwrap();
20 Some((tex, px))
21}
22
23fn at(px: &[u8], w: u32, x: u32, y: u32) -> [u8; 4] {
24 let i = ((y * w + x) * 4) as usize;
25 [px[i], px[i + 1], px[i + 2], px[i + 3]]
26}
27
28#[test]
29fn vector_denoise_reduces_temporal_flicker_and_preserves_edges() {
30 let c = comp(r#"
31 BG = Background { Inputs = {
32 Width = Input { Value = 32, }, Height = Input { Value = 32, },
33 TopLeftRed = Input { SourceOp = "FlickerR", Source = "Value", },
34 TopLeftGreen = Input { Value = 0.5, }, TopLeftBlue = Input { Value = 0.5, },
35 }, },
36 FlickerR = BezierSpline { KeyFrames = {
37 [0] = { 0.45, Flags = { Linear = true } },
38 [1] = { 0.55, Flags = { Linear = true } },
39 [2] = { 0.45, Flags = { Linear = true } },
40 } },
41 VD = VectorDenoise { Inputs = {
42 Input = Input { SourceOp = "BG", Source = "Output", },
43 TemporalRadius = Input { Value = 1, },
44 Sigma = Input { Value = 0.25, },
45 }, },
46 OcclBG = Background { Inputs = {
47 Width = Input { Value = 32, }, Height = Input { Value = 32, },
48 TopLeftRed = Input { SourceOp = "JumpR", Source = "Value", },
49 }, },
50 JumpR = BezierSpline { KeyFrames = {
51 [0] = { 0.0, Flags = { Linear = true } },
52 [1] = { 1.0, Flags = { Linear = true } },
53 [2] = { 0.0, Flags = { Linear = true } },
54 } },
55 VDOccl = VectorDenoise { Inputs = {
56 Input = Input { SourceOp = "OcclBG", Source = "Output", },
57 Average = Input { Value = 1, },
58 Sigma = Input { Value = 0.05, },
59 }, },
60 "#);
61 let Some((_, px_smooth)) = render(&c, "VD", 1.0) else { return };
62 let r_smooth = at(&px_smooth, 32, 16, 16)[0];
63 assert!(r_smooth >= 118 && r_smooth <= 134, "expected temporal smoothing near ~124, got {r_smooth}");
64
65 let (_, px_occl) = render(&c, "VDOccl", 1.0).unwrap();
66 let r_occl = at(&px_occl, 32, 16, 16)[0];
67 assert!(r_occl >= 250, "expected occlusion preservation near 255, got {r_occl}");
68}
69
70#[test]
71fn mandel_renders_mandelbrot_julia_and_gradient_lut() {
72 let c = comp(r#"
73 M = Mandel { Inputs = {
74 Width = Input { Value = 64, }, Height = Input { Value = 64, },
75 XPosition = Input { Value = -0.5, }, Zoom = Input { Value = 1.0, },
76 Iterations = Input { Value = 80, }, EscapeLimit = Input { Value = 4, },
77 GradientCurve = Input { Value = 0.45, },
78 RedRepetitions = Input { Value = 1.8, }, GreenRepetitions = Input { Value = 2.0, },
79 BluePhase = Input { Value = 0.25, },
80 }, },
81 J = Mandel { Inputs = {
82 Width = Input { Value = 64, }, Height = Input { Value = 64, },
83 Mode = Input { Value = FuID { "Julia" }, },
84 JuliaSeed = Input { Value = { -0.7, 0.27015 }, },
85 Iterations = Input { Value = 64, },
86 }, },
87 G = Mandel { Inputs = {
88 Width = Input { Value = 64, }, Height = Input { Value = 64, },
89 Gradient = Input { Value = Gradient { Colors = {
90 [0] = { 0, 0, 1, 1 },
91 [1] = { 1, 1, 0, 1 },
92 } }, },
93 }, },
94 "#);
95 let Some((tex_m, px_m)) = render(&c, "M", 0.0) else { return };
96 assert_eq!(tex_m.size, [64, 64]);
97 assert_eq!(at(&px_m, 64, 32, 32), [0, 0, 0, 255], "cardioid center should be inside set");
98 let corner = at(&px_m, 64, 2, 2);
99 assert!(corner[0] as u32 + corner[1] as u32 + corner[2] as u32 > 20, "exterior should be colored: {corner:?}");
100
101 let (_, px_j) = render(&c, "J", 0.0).unwrap();
102 assert_ne!(px_m, px_j, "Julia mode should differ from Mandelbrot");
103
104 let (_, px_g) = render(&c, "G", 0.0).unwrap();
105 let g_corner = at(&px_g, 64, 2, 2);
106 assert!(g_corner[2] > 100, "Gradient LUT exterior near t=0 should be blue: {g_corner:?}");
107}
108
109#[test]
110fn unsharp_mask_boosts_edges_and_respects_threshold_and_channels() {
111 let c = comp(r#"
112 Dark = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 0.3, }, TopLeftGreen = Input { Value = 0.3, }, TopLeftBlue = Input { Value = 0.3, }, }, },
113 Bright = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 0.7, }, TopLeftGreen = Input { Value = 0.7, }, TopLeftBlue = Input { Value = 0.7, }, }, },
114 Rect = RectangleMask { Inputs = { Center = Input { Value = { 0.75, 0.5 }, }, Width = Input { Value = 0.5, }, Height = Input { Value = 1.0, }, }, },
115 Step = Merge { Inputs = { Background = Input { SourceOp = "Dark", Source = "Output", }, Foreground = Input { SourceOp = "Bright", Source = "Output", }, EffectMask = Input { SourceOp = "Rect", Source = "Mask", }, }, },
116 USM = UnsharpMask { Inputs = {
117 Input = Input { SourceOp = "Step", Source = "Output", },
118 Size = Input { Value = 24.0, }, Gain = Input { Value = 2.0, }, Threshold = Input { Value = 0.0, },
119 }, },
120 USMHighT = UnsharpMask { Inputs = {
121 Input = Input { SourceOp = "Step", Source = "Output", },
122 Size = Input { Value = 24.0, }, Gain = Input { Value = 2.0, }, Threshold = Input { Value = 0.9, },
123 }, },
124 USMNoRed = UnsharpMask { Inputs = {
125 Input = Input { SourceOp = "Step", Source = "Output", },
126 Size = Input { Value = 24.0, }, Gain = Input { Value = 2.0, }, ProcessRed = Input { Value = 0, },
127 }, },
128 "#);
129 let Some((_, px_step)) = render(&c, "Step", 0.0) else { return };
130 let (_, px_usm) = render(&c, "USM", 0.0).unwrap();
131 let (_, px_hi) = render(&c, "USMHighT", 0.0).unwrap();
132 let (_, px_nr) = render(&c, "USMNoRed", 0.0).unwrap();
133
134 // Right at x=31 (dark side) and x=32 (bright side) of the step edge:
135 assert!(at(&px_usm, 64, 31, 32)[0] + 25 < at(&px_step, 64, 31, 32)[0], "expected dark undershoot at x=31");
136 assert!(at(&px_usm, 64, 32, 32)[0] > at(&px_step, 64, 32, 32)[0] + 25, "expected bright overshoot at x=32");
137 assert_eq!(at(&px_hi, 64, 32, 32)[0], at(&px_step, 64, 32, 32)[0], "high threshold must suppress boost");
138 assert_eq!(at(&px_nr, 64, 32, 32)[0], at(&px_step, 64, 32, 32)[0], "ProcessRed=0 keeps red unchanged");
139 assert!(at(&px_nr, 64, 32, 32)[1] > at(&px_step, 64, 32, 32)[1] + 25, "ProcessRed=0 still boosts green");
140}
141
142#[test]
143fn letterbox_frames_black_bars_position_and_panscan_crops() {
144 let c = comp(r#"
145 Wide = Background { Inputs = {
146 Width = Input { Value = 64, }, Height = Input { Value = 32, },
147 TopLeftRed = Input { Value = 0.9, }, TopLeftGreen = Input { Value = 0.2, }, TopLeftBlue = Input { Value = 0.1, },
148 }, },
149 LB = Letterbox { Inputs = {
150 Input = Input { SourceOp = "Wide", Source = "Output", },
151 Width = Input { Value = 64, }, Height = Input { Value = 64, },
152 Mode = Input { Value = 0, },
153 }, },
154 LBTop = Letterbox { Inputs = {
155 Input = Input { SourceOp = "Wide", Source = "Output", },
156 Width = Input { Value = 64, }, Height = Input { Value = 64, },
157 Mode = Input { Value = 0, }, Position = Input { Value = 1.0, },
158 FilterMethod = Input { Value = 0, },
159 }, },
160 PS = Letterbox { Inputs = {
161 Input = Input { SourceOp = "Wide", Source = "Output", },
162 Width = Input { Value = 64, }, Height = Input { Value = 64, },
163 Mode = Input { Value = 1, },
164 }, },
165 "#);
166 let Some((tex_lb, px_lb)) = render(&c, "LB", 0.0) else { return };
167 assert_eq!(tex_lb.size, [64, 64]);
168 assert_eq!(at(&px_lb, 64, 32, 4), [0, 0, 0, 0]);
169 assert_eq!(at(&px_lb, 64, 32, 60), [0, 0, 0, 0]);
170 assert!(at(&px_lb, 64, 32, 32)[0] > 200);
171
172 // Position = +1 pushes image to top half (y=0..32) and leaves bottom half black.
173 let (_, px_top) = render(&c, "LBTop", 0.0).unwrap();
174 assert!(at(&px_top, 64, 32, 8)[0] > 200);
175 assert_eq!(at(&px_top, 64, 32, 56), [0, 0, 0, 0]);
176
177 let (_, px_ps) = render(&c, "PS", 0.0).unwrap();
178 assert!(at(&px_ps, 64, 32, 4)[0] > 200);
179 assert!(at(&px_ps, 64, 32, 60)[0] > 200);
180}
181
182#[test]
183fn fuse_wipe_linear_invert_softness_radial_and_iris() {
184 let c = comp(r#"
185 Red = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 1, }, }, },
186 Blue = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftBlue = Input { Value = 1, }, }, },
187 Lin = Fuse.Wipe { Inputs = {
188 Input1 = Input { SourceOp = "Red", Source = "Output", },
189 Input2 = Input { SourceOp = "Blue", Source = "Output", },
190 Amount = Input { Value = 0.5, }, Angle = Input { Value = 0, },
191 }, },
192 Inv = Fuse.Wipe { Inputs = {
193 Input1 = Input { SourceOp = "Red", Source = "Output", },
194 Input2 = Input { SourceOp = "Blue", Source = "Output", },
195 Amount = Input { Value = 0.5, }, Invert = Input { Value = 1, },
196 }, },
197 Soft = Fuse.Wipe { Inputs = {
198 Input1 = Input { SourceOp = "Red", Source = "Output", },
199 Input2 = Input { SourceOp = "Blue", Source = "Output", },
200 Amount = Input { Value = 0.5, }, Softness = Input { Value = 0.4, },
201 }, },
202 Iris = Fuse.Wipe { Inputs = {
203 Input1 = Input { SourceOp = "Red", Source = "Output", },
204 Input2 = Input { SourceOp = "Blue", Source = "Output", },
205 Amount = Input { Value = 0.5, }, WipeType = Input { Value = FuID { "Iris" }, },
206 }, },
207 "#);
208 let Some((_, px_lin)) = render(&c, "Lin", 0.0) else { return };
209 assert_eq!(at(&px_lin, 64, 8, 32), [0, 0, 255, 255]);
210 assert_eq!(at(&px_lin, 64, 56, 32), [255, 0, 0, 255]);
211
212 let (_, px_inv) = render(&c, "Inv", 0.0).unwrap();
213 assert_eq!(at(&px_inv, 64, 8, 32), [255, 0, 0, 255]);
214 assert_eq!(at(&px_inv, 64, 56, 32), [0, 0, 255, 255]);
215
216 let (_, px_soft) = render(&c, "Soft", 0.0).unwrap();
217 let mid = at(&px_soft, 64, 32, 32);
218 assert!(mid[0] > 80 && mid[2] > 80, "soft wipe center should blend both inputs: {mid:?}");
219
220 let (_, px_iris) = render(&c, "Iris", 0.0).unwrap();
221 assert_eq!(at(&px_iris, 64, 32, 32), [0, 0, 255, 255]);
222 assert_eq!(at(&px_iris, 64, 2, 2), [255, 0, 0, 255]);
223}
224
225#[test]
226fn fuse_null_and_set_metadata_preserve_pixels_dod_and_aux() {
227 let c = comp(r#"
228 BG = Background { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, TopLeftGreen = Input { Value = 0.8, }, }, },
229 Aux = Dimension.CopyAux { Inputs = {
230 Input = Input { SourceOp = "BG", Source = "Output", },
231 Channel = Input { Value = FuID { "Z" }, },
232 Mode = Input { Value = 1, },
233 }, },
234 NullPass = Fuse.Null { Inputs = { Input = Input { SourceOp = "Aux", Source = "Output", }, }, },
235 MetaPass = Fuse.SetMetaData { Inputs = {
236 Input = Input { SourceOp = "NullPass", Source = "Output", },
237 FieldName = Input { Value = "TEST", }, FieldValue = Input { Value = "ok", },
238 }, },
239 NullEmpty = Fuse.Null { },
240 "#);
241 let Some((tex_aux, px_aux)) = render(&c, "Aux", 0.0) else { return };
242 let (tex_meta, px_meta) = render(&c, "MetaPass", 0.0).unwrap();
243 assert_eq!(px_aux, px_meta);
244 assert_eq!(tex_aux.origin, tex_meta.origin);
245 assert_eq!(tex_aux.canvas_size, tex_meta.canvas_size);
246 assert!(tex_meta.aux.as_ref().and_then(|a| a.z.as_ref()).is_some(), "aux.z must be preserved");
247
248 let (tex_empty, _) = render(&c, "NullEmpty", 0.0).unwrap();
249 assert_eq!(tex_empty.size, [1, 1]);
250}
251
252#[test]
253fn fuse_linger_accumulates_decaying_past_frames_and_preserves_static() {
254 let c = comp(r#"
255 Pulse = Background { Inputs = {
256 Width = Input { Value = 16, }, Height = Input { Value = 16, },
257 TopLeftRed = Input { SourceOp = "PulseR", Source = "Value", },
258 }, },
259 PulseR = BezierSpline { KeyFrames = {
260 [0] = { 1.0, Flags = { StepIn = true } },
261 [1] = { 0.0, Flags = { StepIn = true } },
262 [2] = { 0.0, Flags = { StepIn = true } },
263 } },
264 L = Fuse.Linger { Inputs = {
265 Background = Input { SourceOp = "Pulse", Source = "Output", },
266 Frames = Input { Value = 4, }, Linger = Input { Value = 0.7, },
267 }, },
268 Static = Background { Inputs = { Width = Input { Value = 16, }, Height = Input { Value = 16, }, TopLeftGreen = Input { Value = 0.6, }, }, },
269 LStatic = Fuse.Linger { Inputs = {
270 Background = Input { SourceOp = "Static", Source = "Output", },
271 Frames = Input { Value = 4, }, Linger = Input { Value = 0.8, },
272 }, },
273 "#);
274 let Some((_, px_t1)) = render(&c, "L", 1.0) else { return };
275 let (_, px_t2) = render(&c, "L", 2.0).unwrap();
276 let r1 = at(&px_t1, 16, 8, 8)[0];
277 let r2 = at(&px_t2, 16, 8, 8)[0];
278 assert!(r1 > 40, "expected lingering persistence at frame 1, got {r1}");
279 assert!(r2 > 10 && r2 < r1, "expected monotonic decay at frame 2 (r1={r1}, r2={r2})");
280
281 let (_, px_s) = render(&c, "Static", 3.0).unwrap();
282 let (_, px_ls) = render(&c, "LStatic", 3.0).unwrap();
283 assert_eq!(px_s, px_ls, "static clip must be exact identity under Linger");
284}
285
286#[test]
287fn fuse_ocl_rays_casts_radial_light_shafts_with_threshold_and_tint() {
288 let c = comp(r#"
289 Black = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftAlpha = Input { Value = 1, }, }, },
290 White = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 1, }, TopLeftGreen = Input { Value = 1, }, TopLeftBlue = Input { Value = 1, }, }, },
291 Dot = EllipseMask { Inputs = { Center = Input { Value = { 0.5, 0.5 }, }, Width = Input { Value = 0.12, }, Height = Input { Value = 0.12, }, }, },
292 Src = Merge { Inputs = { Background = Input { SourceOp = "Black", Source = "Output", }, Foreground = Input { SourceOp = "White", Source = "Output", }, EffectMask = Input { SourceOp = "Dot", Source = "Mask", }, }, },
293 Rays = Fuse.OCLRays { Inputs = {
294 Image = Input { SourceOp = "Src", Source = "Output", },
295 Center = Input { Value = { 0.5, 0.5 }, },
296 Threshold = Input { Value = 0.2, }, Decay = Input { Value = 0.02, },
297 Weight = Input { Value = 3.5, }, Exposure = Input { Value = 1.0, },
298 Samples = Input { Value = 48, },
299 ColorRed = Input { Value = 1.0, }, ColorGreen = Input { Value = 0.5, }, ColorBlue = Input { Value = 0.0, },
300 }, },
301 "#);
302 let Some((_, px_src)) = render(&c, "Src", 0.0) else { return };
303 let (_, px_rays) = render(&c, "Rays", 0.0).unwrap();
304 assert_eq!(at(&px_src, 64, 48, 32)[0], 0);
305 let shaft = at(&px_rays, 64, 48, 32);
306 assert!(shaft[0] > 15, "expected outward radial light shaft at (48, 32), got {shaft:?}");
307 assert!(shaft[0] > shaft[1] && shaft[2] == 0, "expected warm orange tint: {shaft:?}");
308 assert!(at(&px_rays, 64, 40, 32)[0] > at(&px_rays, 64, 58, 32)[0], "closer to source should be brighter");
309}
310
311#[test]
312fn fx_fuses_uniform_ring_grows_resets_and_trims() {
313 let Ok(eng) = Engine::new_headless() else { return };
314 let mut pipes = FxFusesPipelines::new(&eng.device, &eng.queue);
315 let params = UniformParams::default();
316 for _ in 0..(FX_UNIFORM_SLOTS_PER_CHUNK * 2 + 5) {
317 pipes.alloc_uniform_slot(&eng.device, &eng.queue, &params);
318 }
319 assert_eq!(pipes.uniform_chunks.len(), 3);
320 pipes.reset_uniform_cursor();
321 assert_eq!(pipes.uniform_cursor, 0);
322 pipes.trim_uniform_chunks(2);
323 assert_eq!(pipes.uniform_chunks.len(), 2);
324}
325
326#[test]
327fn cyclic_temporal_graphs_terminate_immediately_without_hanging() {
328 let c = comp(r#"
329 VDLoop = VectorDenoise { Inputs = {
330 Input = Input { SourceOp = "VDLoop", Source = "Output", },
331 TemporalRadius = Input { Value = 3, },
332 }, },
333 TrLoop = Trails { Inputs = {
334 Input = Input { SourceOp = "TrMid", Source = "Output", },
335 GainRed = Input { Value = 0.9, },
336 }, },
337 TrMid = BrightnessContrast { Inputs = {
338 Input = Input { SourceOp = "TrLoop", Source = "Output", },
339 }, },
340 LingerLoop = Fuse.Linger { Inputs = {
341 Background = Input { SourceOp = "LingerLoop", Source = "Output", },
342 Frames = Input { Value = 16, },
343 Linger = Input { Value = 0.9, },
344 }, },
345 "#);
346 let Ok(mut eng) = Engine::new_headless() else { return };
347 for name in ["VDLoop", "TrLoop", "LingerLoop"] {
348 let id = c.find_node(name).expect("find cyclic node");
349 let (tex, _) = eng.evaluate(&c, id, 5.0);
350 assert!(tex.size[0] >= 1 && tex.size[1] >= 1);
351 }
352}