ff-render 0.18.0

GPU compositing pipeline for real-time preview (wgpu-based)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//! GPU-path integration tests for the `ff-render` nodes.
//!
//! Unlike `render_graph_tests.rs` (which covers only the CPU fallback
//! `process_cpu`), these exercise the real wgpu `process()` path end to end:
//! upload the input frame to a GPU texture, run the node on a real device via
//! [`RenderGraph::process_gpu`], read the output back, and assert the expected
//! pixel semantics within a tolerance that absorbs GPU/driver variance.
//!
//! Each assertion mirrors the expectation the corresponding CPU test encodes;
//! they are independent expectations, not a GPU-vs-CPU parity check. The tests
//! skip gracefully (no failure, no panic) when no GPU adapter is available.
//!
//! Requires the `wgpu` feature (see `[[test]] required-features` in Cargo.toml).

use std::sync::Arc;

use ff_render::{
    AlphaMatteNode, BlendMode, BlendModeNode, ChromaKeyNode, ColorGradeNode, CompositeOp,
    CrossfadeNode, DissolveTransitionNode, FadeTransitionNode, LumaMaskNode, OverlayNode,
    RenderContext, RenderGraph, RenderNode, RenderNodeCpu, ScaleAlgorithm, ScaleNode,
    ShapeMaskNode, TransformNode, YuvFormat, YuvUploadNode,
};

// Helpers

/// A `w × h` buffer filled with a single RGBA colour.
fn solid_rgba(r: u8, g: u8, b: u8, a: u8, w: u32, h: u32) -> Vec<u8> {
    let mut v = Vec::with_capacity((w * h * 4) as usize);
    for _ in 0..(w * h) {
        v.extend_from_slice(&[r, g, b, a]);
    }
    v
}

/// A shared GPU context, or `None` when no adapter is available (CI without a
/// GPU). Every test starts with `let Some(ctx) = gpu_ctx() else { return; };`.
fn gpu_ctx() -> Option<Arc<RenderContext>> {
    match futures::executor::block_on(RenderContext::init()) {
        Ok(ctx) => Some(Arc::new(ctx)),
        Err(e) => {
            println!("skipping GPU test: no adapter ({e})");
            None
        }
    }
}

/// Upload `rgba`, run `node` on the GPU, and read the result back as RGBA.
fn run_gpu(
    ctx: &Arc<RenderContext>,
    node: impl RenderNode + 'static,
    rgba: &[u8],
    w: u32,
    h: u32,
) -> Vec<u8> {
    RenderGraph::new(Arc::clone(ctx))
        .push(node)
        .process_gpu(rgba, w, h)
        .expect("process_gpu must succeed on a graph with a GPU context")
}

/// `|a - b| <= tol` for two `u8` channel values.
fn close(a: u8, b: u8, tol: i32) -> bool {
    (i32::from(a) - i32::from(b)).abs() <= tol
}

// ColorGradeNode

#[test]
fn color_grade_gpu_brightness_boost_should_increase_rgb_channels() {
    let Some(ctx) = gpu_ctx() else { return };
    let rgba = solid_rgba(100, 100, 100, 255, 4, 4);
    let out = run_gpu(
        &ctx,
        ColorGradeNode::new(0.3, 1.0, 1.0, 0.0, 0.0),
        &rgba,
        4,
        4,
    );
    assert!(out[0] > 100, "brightness +0.3 must raise R; got {}", out[0]);
    assert!(out[1] > 100, "brightness +0.3 must raise G; got {}", out[1]);
    assert!(out[2] > 100, "brightness +0.3 must raise B; got {}", out[2]);
    assert_eq!(out[3], 255, "alpha must be unchanged");
}

#[test]
fn color_grade_gpu_saturation_zero_should_produce_equal_rgb_channels() {
    let Some(ctx) = gpu_ctx() else { return };
    // saturation is the 3rd arg: new(brightness, contrast, saturation, temp, tint).
    let rgba = solid_rgba(200, 100, 50, 255, 4, 4);
    let out = run_gpu(
        &ctx,
        ColorGradeNode::new(0.0, 1.0, 0.0, 0.0, 0.0),
        &rgba,
        4,
        4,
    );
    assert!(
        close(out[0], out[1], 3) && close(out[1], out[2], 3),
        "saturation=0 must greyscale R=G=B; got {} {} {}",
        out[0],
        out[1],
        out[2]
    );
}

// ScaleNode
//
// `run_gpu` allocates the output texture at the input dimensions, so this
// checks colour preservation through the scale sampler, not a size change.

#[test]
fn scale_gpu_solid_input_should_preserve_color() {
    let Some(ctx) = gpu_ctx() else { return };
    let rgba = solid_rgba(128, 64, 32, 255, 4, 4);
    let out = run_gpu(
        &ctx,
        ScaleNode::new(2, 2, ScaleAlgorithm::Bilinear),
        &rgba,
        4,
        4,
    );
    assert!(
        close(out[0], 128, 4) && close(out[1], 64, 4) && close(out[2], 32, 4),
        "scaling a solid colour must preserve it; got {} {} {}",
        out[0],
        out[1],
        out[2]
    );
}

// TransformNode

#[test]
fn transform_gpu_identity_should_return_input() {
    let Some(ctx) = gpu_ctx() else { return };
    let rgba = solid_rgba(77, 88, 99, 255, 4, 4);
    let out = run_gpu(
        &ctx,
        TransformNode::new([0.0, 0.0], 0.0, [1.0, 1.0]),
        &rgba,
        4,
        4,
    );
    assert!(
        close(out[0], 77, 3) && close(out[1], 88, 3) && close(out[2], 99, 3),
        "identity transform must reproduce the input; got {} {} {}",
        out[0],
        out[1],
        out[2]
    );
}

// OverlayNode

#[test]
fn overlay_gpu_opaque_overlay_should_replace_base_color() {
    let Some(ctx) = gpu_ctx() else { return };
    let base = solid_rgba(0, 0, 0, 255, 4, 4);
    let overlay = solid_rgba(200, 100, 50, 255, 4, 4);
    let out = run_gpu(&ctx, OverlayNode::new(overlay, 4, 4), &base, 4, 4);
    assert!(
        out[0] >= 190,
        "opaque overlay must dominate base R; got {}",
        out[0]
    );
}

// CrossfadeNode

#[test]
fn crossfade_gpu_half_factor_should_average_inputs() {
    let Some(ctx) = gpu_ctx() else { return };
    let from = solid_rgba(0, 0, 0, 255, 2, 2);
    let to = solid_rgba(200, 200, 200, 255, 2, 2);
    let out = run_gpu(&ctx, CrossfadeNode::new(0.5, to, 2, 2), &from, 2, 2);
    assert!(
        close(out[0], 100, 8),
        "factor=0.5 must blend R to ~100; got {}",
        out[0]
    );
}

// FadeTransitionNode

/// Clip A and clip B for the transition tests. Every channel differs between the two and
/// none repeats within a frame, so a swapped pair, a dropped channel or a transposed one
/// all show up. Mirrors the constants the CPU tests in `nodes/transition.rs` use.
const FADE_A: [u8; 4] = [10, 200, 30, 255];
const FADE_B: [u8; 4] = [210, 40, 130, 55];

/// A 2x2 frame filled with one tagged colour.
fn tagged(px: [u8; 4]) -> Vec<u8> {
    solid_rgba(px[0], px[1], px[2], px[3], 2, 2)
}

#[test]
fn fade_transition_gpu_half_progress_should_average_inputs() {
    let Some(ctx) = gpu_ctx() else { return };
    let a = tagged(FADE_A);
    let b = tagged(FADE_B);
    let out = run_gpu(&ctx, FadeTransitionNode::new(0.5, b, 2, 2), &a, 2, 2);
    for c in 0..4 {
        let want = u8::midpoint(FADE_A[c], FADE_B[c]);
        assert!(
            close(out[c], want, 8),
            "channel {c}: progress=0.5 must blend to ~{want}; got {}",
            out[c]
        );
    }
}

#[test]
fn fade_transition_gpu_progress_endpoints_should_be_each_clip() {
    // The 0.5 blend above is symmetric, so it cannot tell clip A from clip B. These
    // endpoints are what pin the direction on the GPU path.
    let Some(ctx) = gpu_ctx() else { return };
    let a = tagged(FADE_A);
    let b = tagged(FADE_B);
    let at_zero = run_gpu(
        &ctx,
        FadeTransitionNode::new(0.0, b.clone(), 2, 2),
        &a,
        2,
        2,
    );
    let at_one = run_gpu(&ctx, FadeTransitionNode::new(1.0, b, 2, 2), &a, 2, 2);
    for c in 0..4 {
        assert!(
            close(at_zero[c], FADE_A[c], 2),
            "channel {c}: progress=0 must be clip A; got {}",
            at_zero[c]
        );
        assert!(
            close(at_one[c], FADE_B[c], 2),
            "channel {c}: progress=1 must be clip B; got {}",
            at_one[c]
        );
    }
}

// DissolveTransitionNode

/// A mask revealing every `nth` pixel, as an irregular stand-in for a real selection.
///
/// The node takes its selection as an input rather than deriving one, so these tests
/// supply their own; `avio`'s `xfade_reference_parity` is what pins the real mask
/// against `FFmpeg`.
fn nth_mask(w: u32, h: u32, nth: usize) -> Vec<u8> {
    let n = (w * h) as usize;
    let mut mask = vec![0u8; n * 4];
    for i in 0..n {
        if i % nth == 0 {
            mask[i * 4..i * 4 + 4].fill(255);
        }
    }
    mask
}

#[test]
fn dissolve_gpu_should_threshold_pixels_not_blend_them() {
    // The property that separates a dissolve from a fade, asserted on the GPU path in
    // its own right (this file states independent expectations, not GPU-vs-CPU parity).
    // Black into white makes a mixed value unmistakable.
    let Some(ctx) = gpu_ctx() else { return };
    let (w, h) = (32u32, 32u32);
    let a = solid_rgba(0, 0, 0, 255, w, h);
    let b = solid_rgba(255, 255, 255, 255, w, h);
    let out = run_gpu(
        &ctx,
        DissolveTransitionNode::new(nth_mask(w, h, 2), b, w, h),
        &a,
        w,
        h,
    );

    let reds: Vec<u8> = out.chunks_exact(4).map(|px| px[0]).collect();
    let mixed = reds.iter().filter(|v| (8..=247).contains(*v)).count();
    let revealed = reds.iter().filter(|v| **v > 247).count();
    println!(
        "dissolve GPU @50%: revealed={revealed}/{} mixed={mixed}",
        reds.len()
    );
    assert_eq!(
        mixed, 0,
        "a dissolve must never produce a mixed value; {mixed} pixels were between"
    );
    let ratio = revealed as f64 / reds.len() as f64;
    assert!(
        (0.35..=0.65).contains(&ratio),
        "an every-other-pixel mask should reveal about half of clip B, got {ratio:.3}"
    );
}

#[test]
fn dissolve_gpu_mask_endpoints_should_be_each_clip() {
    // The half case above says nothing about which way the mask reads; these pin it.
    let Some(ctx) = gpu_ctx() else { return };
    let (w, h) = (16u32, 16u32);
    let n = (w * h) as usize;
    let a = solid_rgba(0, 0, 0, 255, w, h);
    let b = solid_rgba(255, 255, 255, 255, w, h);
    let at_zero = run_gpu(
        &ctx,
        DissolveTransitionNode::new(vec![0u8; n * 4], b.clone(), w, h),
        &a,
        w,
        h,
    );
    let at_one = run_gpu(
        &ctx,
        DissolveTransitionNode::new(vec![255u8; n * 4], b, w, h),
        &a,
        w,
        h,
    );
    assert!(
        at_zero.chunks_exact(4).all(|px| px[0] < 8),
        "an unset mask must be entirely clip A"
    );
    assert!(
        at_one.chunks_exact(4).all(|px| px[0] > 247),
        "a set mask must be entirely clip B"
    );
}

// BlendModeNode

#[test]
fn blend_gpu_multiply_should_darken_base() {
    let Some(ctx) = gpu_ctx() else { return };
    let base = solid_rgba(128, 128, 128, 255, 2, 2);
    let overlay = solid_rgba(128, 128, 128, 255, 2, 2);
    let out = run_gpu(
        &ctx,
        BlendModeNode::new(BlendMode::Multiply, 1.0, overlay, 2, 2),
        &base,
        2,
        2,
    );
    assert!(out[0] < 128, "Multiply must darken base R; got {}", out[0]);
}

#[test]
fn blend_gpu_screen_should_lighten_base() {
    let Some(ctx) = gpu_ctx() else { return };
    let base = solid_rgba(100, 100, 100, 255, 2, 2);
    let overlay = solid_rgba(100, 100, 100, 255, 2, 2);
    let out = run_gpu(
        &ctx,
        BlendModeNode::new(BlendMode::Screen, 1.0, overlay, 2, 2),
        &base,
        2,
        2,
    );
    assert!(out[0] > 100, "Screen must lighten base R; got {}", out[0]);
}

#[test]
fn blend_gpu_normal_at_zero_opacity_should_leave_base_unchanged() {
    let Some(ctx) = gpu_ctx() else { return };
    let base = solid_rgba(200, 100, 50, 255, 2, 2);
    let overlay = solid_rgba(0, 0, 0, 255, 2, 2);
    let out = run_gpu(
        &ctx,
        BlendModeNode::new(BlendMode::Normal, 0.0, overlay, 2, 2),
        &base,
        2,
        2,
    );
    assert!(
        close(out[0], 200, 3) && close(out[1], 100, 3) && close(out[2], 50, 3),
        "opacity=0 must leave the base unchanged; got {} {} {}",
        out[0],
        out[1],
        out[2]
    );
}

/// Every blend mode, shader against the Rust implementation.
///
/// `blend_math.rs`'s table pins the Rust to `FFmpeg`'s formulas; this pins the
/// shader to the Rust, so the two together tie the GPU output to `FFmpeg` (#1669).
///
/// The colour pairs are the same ones that table uses. They are **coloured, not
/// grey** (RK-022), and every channel sits at least 16 LSB away from 0, 128 and
/// 255: `HardMix`, `PinLight`, `VividLight`, `HardOverlay` and `SoftDifference`
/// are discontinuous at those points, and a one-LSB sampling difference there
/// would flip the branch and produce a large, flaky delta. Across the nine
/// (base, overlay) channel combinations both sides of every `< 0.5` branch are
/// exercised, so no mode's case is vacuous (RK-015).
///
/// A 3x1 texture puts every fragment centre exactly on a texel centre, so the
/// linear sampler returns exact texels and no interpolation error enters the
/// comparison.
#[test]
fn blend_gpu_should_match_the_cpu_path_for_every_mode() {
    let Some(ctx) = gpu_ctx() else { return };

    let base: Vec<u8> = [
        [32u8, 96, 200, 255],
        [208, 24, 144, 255],
        [144, 176, 64, 255],
    ]
    .concat();
    let overlay: Vec<u8> = [
        [176u8, 64, 24, 255],
        [48, 232, 112, 255],
        [96, 32, 208, 255],
    ]
    .concat();

    for mode in ALL_BLEND_MODES {
        let node = BlendModeNode::new(mode, 1.0, overlay.clone(), 3, 1);
        let mut cpu = base.clone();
        node.process_cpu(&mut cpu, 3, 1);
        let gpu = run_gpu(&ctx, node, &base, 3, 1);

        for px in 0..3 {
            // Channel 3 included since #1750: both paths now write the src-over
            // coverage alpha. Both inputs are opaque here, so that channel alone
            // is not discriminating; the semi-transparent test below is.
            for ch in 0..4 {
                let i = px * 4 + ch;
                assert!(
                    close(gpu[i], cpu[i], 2),
                    "{mode:?} pixel {px} channel {ch}: GPU {} vs CPU {}",
                    gpu[i],
                    cpu[i]
                );
            }
        }
    }
}

/// The same shader-vs-Rust check where the alpha formula actually does something.
///
/// A transparent base and a half-transparent overlay give
/// `ao = ea + ba * (1 - ea) = 0.502`, so channel 3 is ~128 rather than the 255
/// both opaque inputs produce above. Before #1750 the GPU wrote the base alpha
/// (0) and the CPU left it untouched (also 0), so the two agreed by accident.
#[test]
fn blend_gpu_should_match_the_cpu_path_for_a_semi_transparent_overlay() {
    let Some(ctx) = gpu_ctx() else { return };

    let base = solid_rgba(40, 90, 200, 0, 2, 2);
    let overlay = solid_rgba(170, 60, 30, 128, 2, 2);

    for mode in ALL_BLEND_MODES {
        let node = BlendModeNode::new(mode, 1.0, overlay.clone(), 2, 2);
        let mut cpu = base.clone();
        node.process_cpu(&mut cpu, 2, 2);
        let gpu = run_gpu(&ctx, node, &base, 2, 2);

        for ch in 0..4 {
            assert!(
                close(gpu[ch], cpu[ch], 2),
                "{mode:?} channel {ch}: GPU {} vs CPU {}",
                gpu[ch],
                cpu[ch]
            );
        }
        assert!(
            close(cpu[3], 128, 2),
            "{mode:?}: the composited alpha must be ~128, not the base's 0; got {}",
            cpu[3]
        );
    }
}

/// Every Porter-Duff operator, shader against the Rust.
///
/// `blend_math.rs`'s table pins the Rust to the W3C `Fa`/`Fb` definitions; this
/// pins the shader to the Rust, so the two together tie the GPU output to the
/// spec (#1670).
///
/// Both alphas sit strictly inside `(0, 1)` and differ, or `In`, `Out` and
/// `Atop` degenerate into each other. The final assert is the load-bearing one:
/// the six operators must produce six distinct pixels, so a shader that ignored
/// `u.composite` and always composited `Over` would fail rather than agree with
/// a CPU path making the same mistake.
#[test]
fn composite_gpu_should_match_the_cpu_path_for_every_op() {
    let Some(ctx) = gpu_ctx() else { return };

    let base = solid_rgba(40, 90, 200, 102, 2, 2); // da = 0.4
    let overlay = solid_rgba(170, 60, 30, 153, 2, 2); // sa = 0.6

    let mut seen = Vec::new();
    for op in ALL_COMPOSITE_OPS {
        let mut node = BlendModeNode::new(BlendMode::Normal, 1.0, overlay.clone(), 2, 2);
        node.composite_op = op;
        let mut cpu = base.clone();
        node.process_cpu(&mut cpu, 2, 2);
        let gpu = run_gpu(&ctx, node, &base, 2, 2);

        for ch in 0..4 {
            assert!(
                close(gpu[ch], cpu[ch], 2),
                "{op:?} channel {ch}: GPU {} vs CPU {}",
                gpu[ch],
                cpu[ch]
            );
        }
        seen.push([cpu[0], cpu[1], cpu[2], cpu[3]]);
    }

    let mut distinct = seen.clone();
    distinct.sort_unstable();
    distinct.dedup();
    assert_eq!(
        distinct.len(),
        6,
        "the six operators must give distinct results; got {seen:?}"
    );
}

/// Kept in the same order as the `CompositeOp` discriminants; the enum's own
/// `composite_op_discriminants_should_match_the_shader_codes` pins those.
const ALL_COMPOSITE_OPS: [CompositeOp; 6] = [
    CompositeOp::Over,
    CompositeOp::Under,
    CompositeOp::In,
    CompositeOp::Out,
    CompositeOp::Atop,
    CompositeOp::Xor,
];

/// Kept in the same order as the `BlendMode` discriminants; the enum's own
/// `blend_mode_discriminants_should_match_the_shader_mode_codes` pins those.
const ALL_BLEND_MODES: [BlendMode; 44] = [
    BlendMode::Normal,
    BlendMode::Multiply,
    BlendMode::Screen,
    BlendMode::Overlay,
    BlendMode::SoftLight,
    BlendMode::HardLight,
    BlendMode::ColorDodge,
    BlendMode::ColorBurn,
    BlendMode::Difference,
    BlendMode::Exclusion,
    BlendMode::Add,
    BlendMode::Subtract,
    BlendMode::Darken,
    BlendMode::Lighten,
    BlendMode::Hue,
    BlendMode::Saturation,
    BlendMode::Color,
    BlendMode::Luminosity,
    BlendMode::And,
    BlendMode::Average,
    BlendMode::Bleach,
    BlendMode::Divide,
    BlendMode::Extremity,
    BlendMode::Freeze,
    BlendMode::Geometric,
    BlendMode::Glow,
    BlendMode::GrainExtract,
    BlendMode::GrainMerge,
    BlendMode::HardMix,
    BlendMode::HardOverlay,
    BlendMode::Harmonic,
    BlendMode::Heat,
    BlendMode::Interpolate,
    BlendMode::LinearLight,
    BlendMode::Multiply128,
    BlendMode::Negation,
    BlendMode::Or,
    BlendMode::Phoenix,
    BlendMode::PinLight,
    BlendMode::Reflect,
    BlendMode::SoftDifference,
    BlendMode::Stain,
    BlendMode::VividLight,
    BlendMode::Xor,
];

// LumaMaskNode

#[test]
fn luma_mask_gpu_bright_frame_should_preserve_alpha() {
    // The node masks by the luma of the source frame the executor binds as its
    // second input, so the frame under test is the mask.
    let Some(ctx) = gpu_ctx() else { return };
    let rgba = solid_rgba(255, 255, 255, 200, 2, 2);
    let out = run_gpu(&ctx, LumaMaskNode::new(false), &rgba, 2, 2);
    assert!(
        close(out[3], 200, 4),
        "a bright frame must keep alpha ~200; got {}",
        out[3]
    );
}

#[test]
fn luma_mask_gpu_dark_frame_should_zero_alpha() {
    let Some(ctx) = gpu_ctx() else { return };
    let rgba = solid_rgba(0, 0, 0, 255, 2, 2);
    let out = run_gpu(&ctx, LumaMaskNode::new(false), &rgba, 2, 2);
    assert!(
        close(out[3], 0, 4),
        "a dark frame must zero alpha; got {}",
        out[3]
    );
}

// ShapeMaskNode

#[test]
fn shape_mask_gpu_covering_rectangle_should_preserve_alpha() {
    let Some(ctx) = gpu_ctx() else { return };
    let rgba = solid_rgba(128, 64, 32, 200, 2, 2);
    let out = run_gpu(&ctx, ShapeMaskNode::new(0, 0, 2, 2, false), &rgba, 2, 2);
    assert!(
        close(out[3], 200, 4),
        "a covering rectangle must keep alpha ~200; got {}",
        out[3]
    );
}

#[test]
fn shape_mask_gpu_empty_rectangle_should_zero_alpha() {
    let Some(ctx) = gpu_ctx() else { return };
    let rgba = solid_rgba(128, 64, 32, 255, 2, 2);
    let out = run_gpu(&ctx, ShapeMaskNode::new(0, 0, 0, 0, false), &rgba, 2, 2);
    assert!(
        close(out[3], 0, 4),
        "an empty rectangle must zero alpha; got {}",
        out[3]
    );
}

// AlphaMatteNode

#[test]
fn alpha_matte_gpu_transparent_fg_should_reveal_background() {
    let Some(ctx) = gpu_ctx() else { return };
    let fg = solid_rgba(255, 0, 0, 0, 2, 2); // fully transparent red
    let bg = solid_rgba(0, 0, 255, 255, 2, 2); // opaque blue
    let out = run_gpu(&ctx, AlphaMatteNode::new(bg, 2, 2), &fg, 2, 2);
    assert!(
        out[2] > 200,
        "transparent fg must show the blue background; got B={}",
        out[2]
    );
}

// ChromaKeyNode

#[test]
fn chroma_key_gpu_pure_key_color_should_become_transparent() {
    let Some(ctx) = gpu_ctx() else { return };
    let rgba = solid_rgba(0, 255, 0, 255, 2, 2); // pure green
    let out = run_gpu(
        &ctx,
        ChromaKeyNode::new([0.0, 1.0, 0.0], 0.3, 0.0),
        &rgba,
        2,
        2,
    );
    assert!(
        close(out[3], 0, 4),
        "pure key colour must key out (alpha ~0); got {}",
        out[3]
    );
}

// YuvUploadNode

#[test]
fn yuv_upload_gpu_black_frame_should_produce_near_black_rgba() {
    let Some(ctx) = gpu_ctx() else { return };
    let mut node = YuvUploadNode::new(YuvFormat::Yuv420p, 4, 4);
    // BT.601 black: Y=16, Cb=Cr=128.
    node.set_planes(vec![16u8; 4 * 4], vec![128u8; 2 * 2], vec![128u8; 2 * 2]);
    let dummy = solid_rgba(0, 0, 0, 0, 4, 4);
    let out = run_gpu(&ctx, node, &dummy, 4, 4);
    assert!(
        out[0] < 25 && out[1] < 25 && out[2] < 25,
        "Y=16 must be near-black; got {} {} {}",
        out[0],
        out[1],
        out[2]
    );
    assert!(close(out[3], 255, 4), "alpha must be ~255; got {}", out[3]);
}

#[test]
fn yuv_upload_gpu_white_frame_should_produce_near_white_rgba() {
    let Some(ctx) = gpu_ctx() else { return };
    let mut node = YuvUploadNode::new(YuvFormat::Yuv420p, 4, 4);
    // BT.601 white: Y=235, Cb=Cr=128.
    node.set_planes(vec![235u8; 4 * 4], vec![128u8; 2 * 2], vec![128u8; 2 * 2]);
    let dummy = solid_rgba(0, 0, 0, 0, 4, 4);
    let out = run_gpu(&ctx, node, &dummy, 4, 4);
    assert!(
        out[0] > 225 && out[1] > 225 && out[2] > 225,
        "Y=235 must be near-white; got {} {} {}",
        out[0],
        out[1],
        out[2]
    );
}