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_matches_fusion_coloring() {
72 let c = comp(r#"
73 M = Mandel { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, }, },
74 P = Mandel { Inputs = {
75 Width = Input { Value = 64, }, Height = Input { Value = 64, },
76 RedPhase = Input { Value = 0.5, },
77 }, },
78 "#);
79 let Some((tex_m, px_m)) = render(&c, "M", 0.0) else { return };
80 assert_eq!(tex_m.size, [64, 64]);
81 assert_eq!(at(&px_m, 64, 40, 31), [0, 0, 0, 0], "cardioid interior stays transparent black");
82 let corner = at(&px_m, 64, 0, 63);
83 assert!(corner[3] <= 1, "the frame's largest potential maps to t = 0: {corner:?}");
84 let near = at(&px_m, 64, 20, 28);
85 assert!(near[0] == near[3] && near[3] > 30, "exterior is an unpremultiplied grey ramp: {near:?}");
86
87 let (_, px_p) = render(&c, "P", 0.0).unwrap();
88 assert_eq!(at(&px_p, 64, 40, 31), [0, 0, 0, 0], "RedPhase leaves the interior untouched");
89 let shifted = at(&px_p, 64, 0, 63)[0];
90 assert!((126..=129).contains(&shifted), "RedPhase 0.5 lifts t = 0 to mid-grey: {shifted}");
91}
92
93#[test]
94fn unsharp_mask_boosts_edges_and_respects_threshold_and_channels() {
95 let c = comp(r#"
96 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, }, }, },
97 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, }, }, },
98 Rect = RectangleMask { Inputs = { Center = Input { Value = { 0.75, 0.5 }, }, Width = Input { Value = 0.5, }, Height = Input { Value = 1.0, }, }, },
99 Step = Merge { Inputs = { Background = Input { SourceOp = "Dark", Source = "Output", }, Foreground = Input { SourceOp = "Bright", Source = "Output", }, EffectMask = Input { SourceOp = "Rect", Source = "Mask", }, }, },
100 USM = UnsharpMask { Inputs = {
101 Input = Input { SourceOp = "Step", Source = "Output", },
102 Size = Input { Value = 24.0, }, Gain = Input { Value = 2.0, }, Threshold = Input { Value = 0.0, },
103 }, },
104 USMHighT = UnsharpMask { Inputs = {
105 Input = Input { SourceOp = "Step", Source = "Output", },
106 Size = Input { Value = 24.0, }, Gain = Input { Value = 2.0, }, Threshold = Input { Value = 0.9, },
107 }, },
108 USMNoRed = UnsharpMask { Inputs = {
109 Input = Input { SourceOp = "Step", Source = "Output", },
110 Size = Input { Value = 24.0, }, Gain = Input { Value = 2.0, }, ProcessRed = Input { Value = 0, },
111 }, },
112 "#);
113 let Some((_, px_step)) = render(&c, "Step", 0.0) else { return };
114 let (_, px_usm) = render(&c, "USM", 0.0).unwrap();
115 let (_, px_hi) = render(&c, "USMHighT", 0.0).unwrap();
116 let (_, px_nr) = render(&c, "USMNoRed", 0.0).unwrap();
117
118 // Right at x=31 (dark side) and x=32 (bright side) of the step edge:
119 assert!(at(&px_usm, 64, 31, 32)[0] + 25 < at(&px_step, 64, 31, 32)[0], "expected dark undershoot at x=31");
120 assert!(at(&px_usm, 64, 32, 32)[0] > at(&px_step, 64, 32, 32)[0] + 25, "expected bright overshoot at x=32");
121 assert_eq!(at(&px_hi, 64, 32, 32)[0], at(&px_step, 64, 32, 32)[0], "high threshold must suppress boost");
122 assert_eq!(at(&px_nr, 64, 32, 32)[0], at(&px_step, 64, 32, 32)[0], "ProcessRed=0 keeps red unchanged");
123 assert!(at(&px_nr, 64, 32, 32)[1] > at(&px_step, 64, 32, 32)[1] + 25, "ProcessRed=0 still boosts green");
124}
125
126#[test]
127fn letterbox_frames_black_bars_position_and_panscan_crops() {
128 let c = comp(r#"
129 Wide = Background { Inputs = {
130 Width = Input { Value = 64, }, Height = Input { Value = 32, },
131 TopLeftRed = Input { Value = 0.9, }, TopLeftGreen = Input { Value = 0.2, }, TopLeftBlue = Input { Value = 0.1, },
132 }, },
133 LB = Letterbox { Inputs = {
134 Input = Input { SourceOp = "Wide", Source = "Output", },
135 Width = Input { Value = 64, }, Height = Input { Value = 64, },
136 Mode = Input { Value = 0, },
137 }, },
138 LBTop = Letterbox { Inputs = {
139 Input = Input { SourceOp = "Wide", Source = "Output", },
140 Width = Input { Value = 64, }, Height = Input { Value = 64, },
141 Mode = Input { Value = 0, }, Position = Input { Value = 1.0, },
142 FilterMethod = Input { Value = 0, },
143 }, },
144 PS = Letterbox { Inputs = {
145 Input = Input { SourceOp = "Wide", Source = "Output", },
146 Width = Input { Value = 64, }, Height = Input { Value = 64, },
147 Mode = Input { Value = 1, },
148 }, },
149 "#);
150 let Some((tex_lb, px_lb)) = render(&c, "LB", 0.0) else { return };
151 assert_eq!(tex_lb.size, [64, 64]);
152 assert_eq!(at(&px_lb, 64, 32, 4), [0, 0, 0, 0]);
153 assert_eq!(at(&px_lb, 64, 32, 60), [0, 0, 0, 0]);
154 assert!(at(&px_lb, 64, 32, 32)[0] > 200);
155
156 // Position = +1 pushes image to top half (y=0..32) and leaves bottom half black.
157 let (_, px_top) = render(&c, "LBTop", 0.0).unwrap();
158 assert!(at(&px_top, 64, 32, 8)[0] > 200);
159 assert_eq!(at(&px_top, 64, 32, 56), [0, 0, 0, 0]);
160
161 let (_, px_ps) = render(&c, "PS", 0.0).unwrap();
162 assert!(at(&px_ps, 64, 32, 4)[0] > 200);
163 assert!(at(&px_ps, 64, 32, 60)[0] > 200);
164}
165
166#[test]
167fn fuse_wipe_linear_invert_softness_radial_and_iris() {
168 let c = comp(r#"
169 Red = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 1, }, }, },
170 Blue = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftBlue = Input { Value = 1, }, }, },
171 Lin = Fuse.Wipe { Inputs = {
172 Input1 = Input { SourceOp = "Red", Source = "Output", },
173 Input2 = Input { SourceOp = "Blue", Source = "Output", },
174 Amount = Input { Value = 0.5, }, Angle = Input { Value = 0, },
175 }, },
176 Inv = Fuse.Wipe { Inputs = {
177 Input1 = Input { SourceOp = "Red", Source = "Output", },
178 Input2 = Input { SourceOp = "Blue", Source = "Output", },
179 Amount = Input { Value = 0.5, }, Invert = Input { Value = 1, },
180 }, },
181 Soft = Fuse.Wipe { Inputs = {
182 Input1 = Input { SourceOp = "Red", Source = "Output", },
183 Input2 = Input { SourceOp = "Blue", Source = "Output", },
184 Amount = Input { Value = 0.5, }, Softness = Input { Value = 0.4, },
185 }, },
186 Iris = Fuse.Wipe { Inputs = {
187 Input1 = Input { SourceOp = "Red", Source = "Output", },
188 Input2 = Input { SourceOp = "Blue", Source = "Output", },
189 Amount = Input { Value = 0.5, }, WipeType = Input { Value = FuID { "Iris" }, },
190 }, },
191 "#);
192 let Some((_, px_lin)) = render(&c, "Lin", 0.0) else { return };
193 assert_eq!(at(&px_lin, 64, 8, 32), [0, 0, 255, 255]);
194 assert_eq!(at(&px_lin, 64, 56, 32), [255, 0, 0, 255]);
195
196 let (_, px_inv) = render(&c, "Inv", 0.0).unwrap();
197 assert_eq!(at(&px_inv, 64, 8, 32), [255, 0, 0, 255]);
198 assert_eq!(at(&px_inv, 64, 56, 32), [0, 0, 255, 255]);
199
200 let (_, px_soft) = render(&c, "Soft", 0.0).unwrap();
201 let mid = at(&px_soft, 64, 32, 32);
202 assert!(mid[0] > 80 && mid[2] > 80, "soft wipe center should blend both inputs: {mid:?}");
203
204 let (_, px_iris) = render(&c, "Iris", 0.0).unwrap();
205 assert_eq!(at(&px_iris, 64, 32, 32), [0, 0, 255, 255]);
206 assert_eq!(at(&px_iris, 64, 2, 2), [255, 0, 0, 255]);
207}
208
209#[test]
210fn fuse_null_and_set_metadata_preserve_pixels_dod_and_aux() {
211 let c = comp(r#"
212 BG = Background { Inputs = { Width = Input { Value = 32, }, Height = Input { Value = 32, }, TopLeftGreen = Input { Value = 0.8, }, }, },
213 Aux = Dimension.CopyAux { Inputs = {
214 Input = Input { SourceOp = "BG", Source = "Output", },
215 Channel = Input { Value = FuID { "Z" }, },
216 Mode = Input { Value = 1, },
217 }, },
218 NullPass = Fuse.Null { Inputs = { Input = Input { SourceOp = "Aux", Source = "Output", }, }, },
219 MetaPass = Fuse.SetMetaData { Inputs = {
220 Input = Input { SourceOp = "NullPass", Source = "Output", },
221 FieldName = Input { Value = "TEST", }, FieldValue = Input { Value = "ok", },
222 }, },
223 NullEmpty = Fuse.Null { },
224 "#);
225 let Some((tex_aux, px_aux)) = render(&c, "Aux", 0.0) else { return };
226 let (tex_meta, px_meta) = render(&c, "MetaPass", 0.0).unwrap();
227 assert_eq!(px_aux, px_meta);
228 assert_eq!(tex_aux.origin, tex_meta.origin);
229 assert_eq!(tex_aux.canvas_size, tex_meta.canvas_size);
230 assert!(tex_meta.aux.as_ref().and_then(|a| a.z.as_ref()).is_some(), "aux.z must be preserved");
231
232 let (tex_empty, _) = render(&c, "NullEmpty", 0.0).unwrap();
233 assert_eq!(tex_empty.size, [1, 1]);
234}
235
236#[test]
237fn fuse_linger_accumulates_decaying_past_frames_and_preserves_static() {
238 let c = comp(r#"
239 Pulse = Background { Inputs = {
240 Width = Input { Value = 16, }, Height = Input { Value = 16, },
241 TopLeftRed = Input { SourceOp = "PulseR", Source = "Value", },
242 }, },
243 PulseR = BezierSpline { KeyFrames = {
244 [0] = { 1.0, Flags = { StepIn = true } },
245 [1] = { 0.0, Flags = { StepIn = true } },
246 [2] = { 0.0, Flags = { StepIn = true } },
247 } },
248 L = Fuse.Linger { Inputs = {
249 Background = Input { SourceOp = "Pulse", Source = "Output", },
250 Frames = Input { Value = 4, }, Linger = Input { Value = 0.7, },
251 }, },
252 Static = Background { Inputs = { Width = Input { Value = 16, }, Height = Input { Value = 16, }, TopLeftGreen = Input { Value = 0.6, }, }, },
253 LStatic = Fuse.Linger { Inputs = {
254 Background = Input { SourceOp = "Static", Source = "Output", },
255 Frames = Input { Value = 4, }, Linger = Input { Value = 0.8, },
256 }, },
257 "#);
258 let Some((_, px_t1)) = render(&c, "L", 1.0) else { return };
259 let (_, px_t2) = render(&c, "L", 2.0).unwrap();
260 let r1 = at(&px_t1, 16, 8, 8)[0];
261 let r2 = at(&px_t2, 16, 8, 8)[0];
262 assert!(r1 > 40, "expected lingering persistence at frame 1, got {r1}");
263 assert!(r2 > 10 && r2 < r1, "expected monotonic decay at frame 2 (r1={r1}, r2={r2})");
264
265 let (_, px_s) = render(&c, "Static", 3.0).unwrap();
266 let (_, px_ls) = render(&c, "LStatic", 3.0).unwrap();
267 assert_eq!(px_s, px_ls, "static clip must be exact identity under Linger");
268}
269
270#[test]
271fn fuse_ocl_rays_casts_radial_light_shafts_with_threshold_and_tint() {
272 let c = comp(r#"
273 Black = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftAlpha = Input { Value = 1, }, }, },
274 White = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftRed = Input { Value = 1, }, TopLeftGreen = Input { Value = 1, }, TopLeftBlue = Input { Value = 1, }, }, },
275 Dot = EllipseMask { Inputs = { Center = Input { Value = { 0.5, 0.5 }, }, Width = Input { Value = 0.12, }, Height = Input { Value = 0.12, }, }, },
276 Src = Merge { Inputs = { Background = Input { SourceOp = "Black", Source = "Output", }, Foreground = Input { SourceOp = "White", Source = "Output", }, EffectMask = Input { SourceOp = "Dot", Source = "Mask", }, }, },
277 Rays = Fuse.OCLRays { Inputs = {
278 Image = Input { SourceOp = "Src", Source = "Output", },
279 Center = Input { Value = { 0.5, 0.5 }, },
280 Threshold = Input { Value = 0.2, }, Decay = Input { Value = 0.02, },
281 Weight = Input { Value = 3.5, }, Exposure = Input { Value = 1.0, },
282 Samples = Input { Value = 48, },
283 ColorRed = Input { Value = 1.0, }, ColorGreen = Input { Value = 0.5, }, ColorBlue = Input { Value = 0.0, },
284 }, },
285 "#);
286 let Some((_, px_src)) = render(&c, "Src", 0.0) else { return };
287 let (_, px_rays) = render(&c, "Rays", 0.0).unwrap();
288 assert_eq!(at(&px_src, 64, 48, 32)[0], 0);
289 let shaft = at(&px_rays, 64, 48, 32);
290 assert!(shaft[0] > 15, "expected outward radial light shaft at (48, 32), got {shaft:?}");
291 assert!(shaft[0] > shaft[1] && shaft[2] == 0, "expected warm orange tint: {shaft:?}");
292 assert!(at(&px_rays, 64, 40, 32)[0] > at(&px_rays, 64, 58, 32)[0], "closer to source should be brighter");
293}
294
295#[test]
296fn fx_fuses_uniform_ring_grows_resets_and_trims() {
297 let Ok(eng) = Engine::new_headless() else { return };
298 let mut pipes = FxFusesPipelines::new(&eng.device, &eng.queue);
299 let params = UniformParams::default();
300 for _ in 0..(FX_UNIFORM_SLOTS_PER_CHUNK * 2 + 5) {
301 pipes.alloc_uniform_slot(&eng.device, &eng.queue, &params);
302 }
303 assert_eq!(pipes.uniform_chunks.len(), 3);
304 pipes.reset_uniform_cursor();
305 assert_eq!(pipes.uniform_cursor, 0);
306 pipes.trim_uniform_chunks(2);
307 assert_eq!(pipes.uniform_chunks.len(), 2);
308}
309
310#[test]
311fn cyclic_temporal_graphs_terminate_immediately_without_hanging() {
312 let c = comp(r#"
313 VDLoop = VectorDenoise { Inputs = {
314 Input = Input { SourceOp = "VDLoop", Source = "Output", },
315 TemporalRadius = Input { Value = 3, },
316 }, },
317 TrLoop = Trails { Inputs = {
318 Input = Input { SourceOp = "TrMid", Source = "Output", },
319 GainRed = Input { Value = 0.9, },
320 }, },
321 TrMid = BrightnessContrast { Inputs = {
322 Input = Input { SourceOp = "TrLoop", Source = "Output", },
323 }, },
324 LingerLoop = Fuse.Linger { Inputs = {
325 Background = Input { SourceOp = "LingerLoop", Source = "Output", },
326 Frames = Input { Value = 16, },
327 Linger = Input { Value = 0.9, },
328 }, },
329 "#);
330 let Ok(mut eng) = Engine::new_headless() else { return };
331 for name in ["VDLoop", "TrLoop", "LingerLoop"] {
332 let id = c.find_node(name).expect("find cyclic node");
333 let (tex, _) = eng.evaluate(&c, id, 5.0);
334 assert!(tex.size[0] >= 1 && tex.size[1] >= 1);
335 }
336}