authoravatarclover caruso<git@paperclover.net>2026-11-30 19:34:47-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-30 19:55:45-08:00
log4877d940860eb3654b239b37a9bf28c882c860fb
tree230a8f5c5a7a4e60b16007d125fc5f5e84ccde4a
parenta9e4b2c177b6285819eb8cc1b1e92ff29b4e94d8
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Fix Merge Under/VividLight, CB Negative HDR, and Background Angle/Reflect/FuID{} gradients (#41)


3 files changed, 399 insertions(+), 50 deletions(-)

crates/engine/src/eval/composite.rs+274
......@@ -653,5 +653,279 @@ mod tests {
653653 assert!((p[0] as i32 - 153).abs() <= 2, "DarkerColor={p:?}");
654654 assert_eq!(p[3], 255);
655655 }
656
657 #[test]
658 fn merge_vivid_light_and_under_gain_parity() {
659 let comp = r#"{
660 Tools = ordered() {
661 TransBg = Background {
662 Inputs = {
663 Width = Input { Value = 16, },
664 Height = Input { Value = 16, },
665 TopLeftAlpha = Input { Value = 0.0, },
666 },
667 },
668 SemiBg = Background {
669 Inputs = {
670 Width = Input { Value = 16, },
671 Height = Input { Value = 16, },
672 TopLeftRed = Input { Value = 0.2, },
673 TopLeftGreen = Input { Value = 0.3, },
674 TopLeftBlue = Input { Value = 0.1, },
675 TopLeftAlpha = Input { Value = 0.5, },
676 },
677 },
678 Fg = Background {
679 Inputs = {
680 Width = Input { Value = 16, },
681 Height = Input { Value = 16, },
682 TopLeftRed = Input { Value = 0.64, },
683 TopLeftGreen = Input { Value = 0.24, },
684 TopLeftBlue = Input { Value = 0.48, },
685 TopLeftAlpha = Input { Value = 0.8, },
686 },
687 },
688 VividTrans = Merge {
689 Inputs = {
690 Background = Input { SourceOp = "TransBg", Source = "Output", },
691 Foreground = Input { SourceOp = "Fg", Source = "Output", },
692 ApplyMode = Input { Value = FuID { "VividLight" }, },
693 Operator = Input { Value = FuID { "Under" }, },
694 Gain = Input { Value = 0.74, },
695 },
696 },
697 VividSemi = Merge {
698 Inputs = {
699 Background = Input { SourceOp = "SemiBg", Source = "Output", },
700 Foreground = Input { SourceOp = "Fg", Source = "Output", },
701 ApplyMode = Input { Value = FuID { "VividLight" }, },
702 Operator = Input { Value = FuID { "Under" }, },
703 Gain = Input { Value = 0.74, },
704 },
705 },
706 NormalUnder = Merge {
707 Inputs = {
708 Background = Input { SourceOp = "SemiBg", Source = "Output", },
709 Foreground = Input { SourceOp = "Fg", Source = "Output", },
710 Operator = Input { Value = FuID { "Under" }, },
711 Gain = Input { Value = 0.74, },
712 },
713 },
714 ScreenGain = Merge {
715 Inputs = {
716 Background = Input { SourceOp = "SemiBg", Source = "Output", },
717 Foreground = Input { SourceOp = "Fg", Source = "Output", },
718 ApplyMode = Input { Value = FuID { "Screen" }, },
719 Gain = Input { Value = 0.74, },
720 },
721 },
722 HypotGain = Merge {
723 Inputs = {
724 Background = Input { SourceOp = "SemiBg", Source = "Output", },
725 Foreground = Input { SourceOp = "Fg", Source = "Output", },
726 ApplyMode = Input { Value = FuID { "Hypotenuse" }, },
727 Gain = Input { Value = 0.74, },
728 },
729 },
730 GeomGain = Merge {
731 Inputs = {
732 Background = Input { SourceOp = "SemiBg", Source = "Output", },
733 Foreground = Input { SourceOp = "Fg", Source = "Output", },
734 ApplyMode = Input { Value = FuID { "Geometric" }, },
735 Gain = Input { Value = 0.74, },
736 },
737 },
738 }
739 }"#;
740 // VividLight over transparent BG evaluates to (0,0,0) with alpha = fg.a * gain (~151).
741 let (vt_px, vt_w, _) = eval_node_px(comp, "VividTrans");
742 let vt = px_at(&vt_px, vt_w, 8, 8);
743 assert_eq!([vt[0], vt[1], vt[2]], [0, 0, 0]);
744 assert!((vt[3] as i32 - 151).abs() <= 2, "VividTrans alpha={vt:?}");
745
746 // VividLight over semi-transparent BG=(0.2,0.3,0.1,0.5) with Gain=0.74 -> ~(96, 31, 30, 203).
747 let (vs_px, vs_w, _) = eval_node_px(comp, "VividSemi");
748 let vs = px_at(&vs_px, vs_w, 8, 8);
749 assert!((vs[0] as i32 - 96).abs() <= 2, "VividSemi={vs:?}");
750 assert!((vs[1] as i32 - 31).abs() <= 2, "VividSemi={vs:?}");
751 assert!((vs[2] as i32 - 30).abs() <= 2, "VividSemi={vs:?}");
752 assert!((vs[3] as i32 - 203).abs() <= 2, "VividSemi alpha={vs:?}");
753
754 // Normal Under scales fg alpha by Gain=0.74 -> (133, 107, 87, 203).
755 let (nu_px, nu_w, _) = eval_node_px(comp, "NormalUnder");
756 let nu = px_at(&nu_px, nu_w, 8, 8);
757 assert!((nu[0] as i32 - 133).abs() <= 2, "NormalUnder={nu:?}");
758 assert!((nu[3] as i32 - 203).abs() <= 2, "NormalUnder alpha={nu:?}");
759
760 // Screen scales alpha by fg.a * gain (~203).
761 let (sc_px, sc_w, _) = eval_node_px(comp, "ScreenGain");
762 let sc = px_at(&sc_px, sc_w, 8, 8);
763 assert!((sc[0] as i32 - 147).abs() <= 2, "ScreenGain={sc:?}");
764 assert!((sc[3] as i32 - 203).abs() <= 2, "ScreenGain alpha={sc:?}");
765
766 // Hypotenuse alpha = sqrt(0.5^2 + 0.592^2) ~ 0.775 (~198).
767 let (hy_px, hy_w, _) = eval_node_px(comp, "HypotGain");
768 let hy = px_at(&hy_px, hy_w, 8, 8);
769 assert!((hy[3] as i32 - 198).abs() <= 2, "HypotGain alpha={hy:?}");
770
771 // Geometric alpha = 2*0.5*0.592 / (0.5 + 0.592) ~ 0.542 (~138).
772 let (ge_px, ge_w, _) = eval_node_px(comp, "GeomGain");
773 let ge = px_at(&ge_px, ge_w, 8, 8);
774 assert!((ge[3] as i32 - 138).abs() <= 2, "GeomGain alpha={ge:?}");
775 }
776
777 #[test]
778 fn channel_boolean_negative_preserves_unclamped_hdr() {
779 let comp = r#"{
780 Tools = ordered() {
781 HdrBg = Background {
782 Inputs = {
783 Width = Input { Value = 16, },
784 Height = Input { Value = 16, },
785 Depth = Input { Value = 3, },
786 TopLeftRed = Input { Value = 0.25, },
787 TopLeftGreen = Input { Value = 0.75, },
788 TopLeftBlue = Input { Value = 1.5, },
789 TopLeftAlpha = Input { Value = 1.0, },
790 },
791 },
792 CbNeg = ChannelBoolean {
793 Inputs = {
794 Background = Input { SourceOp = "HdrBg", Source = "Output", },
795 Foreground = Input { SourceOp = "HdrBg", Source = "Output", },
796 Operation = Input { Value = 10, },
797 ToRed = Input { Value = 0, },
798 ToGreen = Input { Value = 1, },
799 ToBlue = Input { Value = 2, },
800 ToAlpha = Input { Value = 3, },
801 },
802 },
803 Lift = BrightnessContrast {
804 Inputs = {
805 Input = Input { SourceOp = "CbNeg", Source = "Output", },
806 Brightness = Input { Value = 0.6, },
807 },
808 },
809 }
810 }"#;
811 // Negative(1.5) = -0.5 in Depth=3; +0.6 brightness yields 0.1 (~26), not 0.6 (~153).
812 let (px, w, _) = eval_node_px(comp, "Lift");
813 let p = px_at(&px, w, 8, 8);
814 assert_eq!(p[0], 255);
815 assert!((p[1] as i32 - 217).abs() <= 2, "CB Negative G={p:?}");
816 assert!((p[2] as i32 - 26).abs() <= 2, "CB Negative HDR B={p:?}");
817 }
818
819 #[test]
820 fn background_empty_fuid_corner_defaults_angle_reflect_and_multistop() {
821 let comp = r#"{
822 Tools = ordered() {
823 EmptyType = Background {
824 Inputs = {
825 Width = Input { Value = 16, },
826 Height = Input { Value = 16, },
827 Type = Input { Value = FuID {}, },
828 TopLeftRed = Input { Value = 0.661, },
829 TopLeftAlpha = Input { Value = 1.0, },
830 },
831 },
832 Horiz = Background {
833 Inputs = {
834 Width = Input { Value = 64, },
835 Height = Input { Value = 16, },
836 Type = Input { Value = FuID { "Horizontal" }, },
837 TopLeftRed = Input { Value = 0.8, },
838 TopLeftGreen = Input { Value = 0.4, },
839 TopLeftBlue = Input { Value = 0.2, },
840 },
841 },
842 AngleGrad = Background {
843 Inputs = {
844 Width = Input { Value = 320, },
845 Height = Input { Value = 180, },
846 Type = Input { Value = FuID { "Gradient" }, },
847 GradientType = Input { Value = FuID { "Angle" }, },
848 Start = Input { Value = { 0.5, 0.5 }, },
849 End = Input { Value = { 0.8, 0.6 }, },
850 Gradient = Input {
851 Value = Gradient {
852 Colors = {
853 [0] = { 1, 0, 0, 1, },
854 [1] = { 0, 1, 0, 1, },
855 },
856 },
857 },
858 },
859 },
860 ReflectGrad = Background {
861 Inputs = {
862 Width = Input { Value = 320, },
863 Height = Input { Value = 180, },
864 Type = Input { Value = FuID { "Gradient" }, },
865 GradientType = Input { Value = FuID { "Reflect" }, },
866 Start = Input { Value = { 0.5, 0.5 }, },
867 End = Input { Value = { 0.8, 0.5 }, },
868 Gradient = Input {
869 Value = Gradient {
870 Colors = {
871 [0] = { 1, 0, 0, 1, },
872 [1] = { 0, 1, 0, 1, },
873 },
874 },
875 },
876 },
877 },
878 MultiStop = Background {
879 Inputs = {
880 Width = Input { Value = 320, },
881 Height = Input { Value = 16, },
882 Type = Input { Value = FuID { "Gradient" }, },
883 Start = Input { Value = { 0.0, 0.5 }, },
884 End = Input { Value = { 1.0, 0.5 }, },
885 Gradient = Input {
886 Value = Gradient {
887 Colors = {
888 [0] = { 1, 0, 0, 1, },
889 [0.25] = { 0, 1, 0, 1, },
890 [0.75] = { 0, 0, 1, 1, },
891 [1] = { 1, 1, 0, 1, },
892 },
893 },
894 },
895 },
896 },
897 }
898 }"#;
899 let (epx, ew, _) = eval_node_px(comp, "EmptyType");
900 assert_eq!(px_at(&epx, ew, 8, 8), [0, 0, 0, 0]);
901
902 // Omitted TopRight defaults to (0,0,0,1), so midpoint x=32 is ~(102, 51, 25, 255).
903 let (hpx, hw, _) = eval_node_px(comp, "Horiz");
904 let hm = px_at(&hpx, hw, 32, 8);
905 assert!((hm[0] as i32 - 102).abs() <= 4, "Horiz mid={hm:?}");
906 assert_eq!(hm[3], 255);
907
908 // Angle gradient: CCW from End=(0.8, 0.6) around Start=(0.5, 0.5) in pixel space.
909 let (apx, aw, _) = eval_node_px(comp, "AngleGrad");
910 let a_top = px_at(&apx, aw, 160, 10); // t ~ 0.219 -> R~199, G~56
911 assert!((a_top[0] as i32 - 199).abs() <= 3, "Angle top={a_top:?}");
912 assert!((a_top[1] as i32 - 56).abs() <= 3, "Angle top={a_top:?}");
913
914 // Reflect gradient: symmetric around x=0.5 (x=10 and x=310 both saturate to green).
915 let (rpx, rw, _) = eval_node_px(comp, "ReflectGrad");
916 assert_eq!(px_at(&rpx, rw, 10, 90), [0, 255, 0, 255]);
917 assert_eq!(px_at(&rpx, rw, 310, 90), [0, 255, 0, 255]);
918 let r_mid = px_at(&rpx, rw, 160, 90);
919 assert!(r_mid[0] >= 250 && r_mid[1] <= 5, "Reflect center={r_mid:?}");
920
921 // Multi-stop Linear: t=0.25 -> green (x=80), t=0.5 -> cyan-ish (0, 128, 128), t=0.75 -> blue (x=240).
922 let (mpx, mw, _) = eval_node_px(comp, "MultiStop");
923 let m25 = px_at(&mpx, mw, 80, 8);
924 let m50 = px_at(&mpx, mw, 160, 8);
925 let m75 = px_at(&mpx, mw, 240, 8);
926 assert!(m25[1] >= 250 && m25[0] <= 5, "MultiStop 0.25={m25:?}");
927 assert!((m50[1] as i32 - 128).abs() <= 4 && (m50[2] as i32 - 128).abs() <= 4, "MultiStop 0.5={m50:?}");
928 assert!(m75[2] >= 250 && m75[0] <= 5, "MultiStop 0.75={m75:?}");
929 }
656930}
657931
crates/engine/src/eval/io.rs+76-25
......@@ -427,40 +427,63 @@ impl Engine {
427427 comp.eval_number(node, "TopLeftAlpha", time, 1.0, &self.expr) as f32,
428428 ];
429429 let tr = [
430 comp.eval_number(node, "TopRightRed", time, tl[0] as f64, &self.expr) as f32,
431 comp.eval_number(node, "TopRightGreen", time, tl[1] as f64, &self.expr) as f32,
432 comp.eval_number(node, "TopRightBlue", time, tl[2] as f64, &self.expr) as f32,
433 comp.eval_number(node, "TopRightAlpha", time, tl[3] as f64, &self.expr) as f32,
430 comp.eval_number(node, "TopRightRed", time, 0.0, &self.expr) as f32,
431 comp.eval_number(node, "TopRightGreen", time, 0.0, &self.expr) as f32,
432 comp.eval_number(node, "TopRightBlue", time, 0.0, &self.expr) as f32,
433 comp.eval_number(node, "TopRightAlpha", time, 1.0, &self.expr) as f32,
434434 ];
435435 let bl = [
436 comp.eval_number(node, "BottomLeftRed", time, tl[0] as f64, &self.expr) as f32,
437 comp.eval_number(node, "BottomLeftGreen", time, tl[1] as f64, &self.expr) as f32,
438 comp.eval_number(node, "BottomLeftBlue", time, tl[2] as f64, &self.expr) as f32,
439 comp.eval_number(node, "BottomLeftAlpha", time, tl[3] as f64, &self.expr) as f32,
436 comp.eval_number(node, "BottomLeftRed", time, 0.0, &self.expr) as f32,
437 comp.eval_number(node, "BottomLeftGreen", time, 0.0, &self.expr) as f32,
438 comp.eval_number(node, "BottomLeftBlue", time, 0.0, &self.expr) as f32,
439 comp.eval_number(node, "BottomLeftAlpha", time, 1.0, &self.expr) as f32,
440440 ];
441441 let br = [
442 comp.eval_number(node, "BottomRightRed", time, tr[0] as f64, &self.expr) as f32,
443 comp.eval_number(node, "BottomRightGreen", time, tr[1] as f64, &self.expr) as f32,
444 comp.eval_number(node, "BottomRightBlue", time, tr[2] as f64, &self.expr) as f32,
445 comp.eval_number(node, "BottomRightAlpha", time, tr[3] as f64, &self.expr) as f32,
442 comp.eval_number(node, "BottomRightRed", time, 0.0, &self.expr) as f32,
443 comp.eval_number(node, "BottomRightGreen", time, 0.0, &self.expr) as f32,
444 comp.eval_number(node, "BottomRightBlue", time, 0.0, &self.expr) as f32,
445 comp.eval_number(node, "BottomRightAlpha", time, 1.0, &self.expr) as f32,
446446 ];
447447
448 let mut kind = match node.static_str("Type").unwrap_or("Solid") {
449 "Horizontal" => 1.0,
450 "Vertical" => 2.0,
451 "Corner" => 3.0,
452 "Gradient" => match node.static_str("GradientType").unwrap_or("Linear") {
453 "Radial" => 5.0,
454 _ => 4.0,
455 },
456 _ => 0.0,
448 let type_str = node.static_str("Type");
449 let empty_fuid = matches!(type_str, Some(""));
450 let mut kind = if empty_fuid {
451 0.0
452 } else {
453 match type_str.unwrap_or("Solid") {
454 "Horizontal" => 1.0,
455 "Vertical" => 2.0,
456 "Corner" => 3.0,
457 "Gradient" => match node.static_str("GradientType").unwrap_or("Linear") {
458 "Radial" => 5.0,
459 "Angle" => 6.0,
460 "Reflect" => 7.0,
461 _ => 4.0,
462 },
463 _ => 0.0,
464 }
457465 };
458 let mut c0 = tl;
466 let mut c0 = if empty_fuid { [0.0, 0.0, 0.0, 0.0] } else { tl };
459467 let mut c1 = tr;
468 let mut grad_lut_f16: Option<[u16; 512 * 4]> = None;
469 let mut grad_hash = 0u64;
460470 if kind >= 4.0 {
461471 if let Some(grad) = comp.eval_gradient(node, "Gradient", time) {
462472 c0 = grad.sample(0.0);
463473 c1 = grad.sample(1.0);
474 let mut lut = [0u16; 512 * 4];
475 for i in 0..512 {
476 let t = i as f64 / 511.0;
477 let c = grad.sample(t);
478 lut[i * 4] = crate::media::f32_to_f16_bits(c[0]);
479 lut[i * 4 + 1] = crate::media::f32_to_f16_bits(c[1]);
480 lut[i * 4 + 2] = crate::media::f32_to_f16_bits(c[2]);
481 lut[i * 4 + 3] = crate::media::f32_to_f16_bits(c[3]);
482 }
483 let mut gh = DefaultHasher::new();
484 lut.hash(&mut gh);
485 grad_hash = gh.finish();
486 grad_lut_f16 = Some(lut);
464487 } else {
465488 kind = 0.0;
466489 }
......@@ -475,7 +498,7 @@ impl Engine {
475498 u.p[1] = c1;
476499 u.p[2] = bl;
477500 u.p[3] = br;
478 u.p[4] = [kind, 0.0, start[0] as f32, start[1] as f32];
501 u.p[4] = [kind, if grad_lut_f16.is_some() { 1.0 } else { 0.0 }, start[0] as f32, start[1] as f32];
479502 u.p[5] = [
480503 end[0] as f32,
481504 end[1] as f32,
......@@ -484,7 +507,7 @@ impl Engine {
484507 ];
485508
486509 let dst_depth = self.node_depth(comp, node, time, None);
487 let key = self.make_key(node, "Background", size, &[mask_hash, dst_depth as u64], bytemuck::cast_slice(&u.p[..6]));
510 let key = self.make_key(node, "Background", size, &[mask_hash, dst_depth as u64, grad_hash], bytemuck::cast_slice(&u.p[..6]));
488511 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
489512 stats.cache_hits += 1;
490513 return hit;
......@@ -492,9 +515,37 @@ impl Engine {
492515 stats.cache_misses += 1;
493516 stats.nodes_evaluated += 1;
494517
518 let lut_tex = grad_lut_f16.map(|lut| {
519 let t = create_texture_dod_fmt(
520 &self.device,
521 [512, 1],
522 [0, 0],
523 [512, 1],
524 BitDepth::Float16,
525 wgpu::TextureFormat::Rgba16Float,
526 0,
527 );
528 self.queue.write_texture(
529 t.texture.as_image_copy(),
530 bytemuck::cast_slice(&lut),
531 wgpu::TexelCopyBufferLayout {
532 offset: 0,
533 bytes_per_row: Some(512 * 8),
534 rows_per_image: Some(1),
535 },
536 wgpu::Extent3d {
537 width: 512,
538 height: 1,
539 depth_or_array_layers: 1,
540 },
541 );
542 t
543 });
544
495545 let dst = self.alloc_tex(size, dst_depth, key);
546 let lut_view = lut_tex.as_ref().map_or(&self.dummy.view, |t| &t.view);
496547 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
497 self.dispatch_op(enc, ShaderOp::Bg, &self.dummy.view, &self.dummy.view, mv, &dst, &u);
548 self.dispatch_op(enc, ShaderOp::Bg, &self.dummy.view, lut_view, mv, &dst, &u);
498549 self.cache.insert(key, dst.clone(), node.id, frame_i);
499550 dst
500551 }
crates/engine/src/shaders/composite.wgsl+49-25
......@@ -1,8 +1,8 @@
11// ============================================================================
22// 1. Background (Solid + Gradients)
33// p[0] = top_left rgba, p[1] = top_right rgba, p[2] = bottom_left rgba, p[3] = bottom_right rgba
4// p[4] = (type: 0=Solid, 1=Horiz, 2=Vert, 3=FourCorner, 4=Gradient, 5=Radial, sub_type, start_x, start_y)
5// p[5] = (end_x, end_y, 0, 0)
4// p[4] = (type: 0=Solid, 1=Horiz, 2=Vert, 3=FourCorner, 4=Linear, 5=Radial, 6=Angle, 7=Reflect, has_lut, start_x, start_y)
5// p[5] = (end_x, end_y, has_mask, mask_invert)
66// ============================================================================
77@compute @workgroup_size(8, 8)
88fn bg_op(@builtin(global_invocation_id) gid: vec3<u32>) {
......@@ -10,6 +10,7 @@ fn bg_op(@builtin(global_invocation_id) gid: vec3<u32>) {
1010 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
1111 let fu = pix_to_fu(gid.xy, dims);
1212 let kind = i32(u.p[4].x + 0.5);
13 let has_lut = u.p[4].y > 0.5;
1314 var col = u.p[0];
1415 if (kind == 1) {
1516 col = mix(u.p[0], u.p[1], fu.x);
......@@ -19,7 +20,7 @@ fn bg_op(@builtin(global_invocation_id) gid: vec3<u32>) {
1920 let top = mix(u.p[0], u.p[1], fu.x);
2021 let bot = mix(u.p[2], u.p[3], fu.x);
2122 col = mix(bot, top, fu.y);
22 } else if (kind == 4 || kind == 5) {
23 } else if (kind >= 4) {
2324 let fdims = vec2<f32>(dims);
2425 let p_px = fu * fdims;
2526 let s_px = u.p[4].zw * fdims;
......@@ -29,11 +30,25 @@ fn bg_op(@builtin(global_invocation_id) gid: vec3<u32>) {
2930 if (kind == 5) {
3031 let r = max(length(d_px), 1e-5);
3132 t = clamp(length(p_px - s_px) / r, 0.0, 1.0);
33 } else if (kind == 6) {
34 let rel = p_px - s_px;
35 let a_p = select(0.0, atan2(rel.y, rel.x), dot(rel, rel) > 1e-12);
36 let a_d = select(0.0, atan2(d_px.y, d_px.x), dot(d_px, d_px) > 1e-12);
37 t = fract((a_p - a_d) / 6.28318530718);
38 } else if (kind == 7) {
39 let len2 = max(dot(d_px, d_px), 1e-6);
40 t = clamp(abs(dot(p_px - s_px, d_px) / len2), 0.0, 1.0);
3241 } else {
3342 let len2 = max(dot(d_px, d_px), 1e-6);
3443 t = clamp(dot(p_px - s_px, d_px) / len2, 0.0, 1.0);
3544 }
36 col = mix(u.p[0], u.p[1], t);
45 if (has_lut) {
46 let lut_w = max(f32(textureDimensions(src_b).x), 1.0);
47 let lut_u = (0.5 + t * (lut_w - 1.0)) / lut_w;
48 col = textureSampleLevel(src_b, samp_lin, vec2<f32>(lut_u, 0.5), 0.0);
49 } else {
50 col = mix(u.p[0], u.p[1], t);
51 }
3752 }
3853 let m = read_mask(gid.xy, dims, u.p[5].z, u.p[5].w);
3954 col = col * m;
......@@ -322,41 +337,43 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {
322337 var out_col: vec4<f32>;
323338 if (mode == 0) {
324339 let fg_rgb = fg.rgb * mix(fg.a, 1.0, sub_add);
325 let fg_occ = fg.a * gain * (1.0 - burn_in);
340 let fg_a_eff = fg.a * gain;
341 let fg_occ = fg_a_eff * (1.0 - burn_in);
326342 var comp: vec4<f32>;
327343 if (op == 1) {
328344 // Under: FG behind BG
329345 let bg_occ = bg.a;
330 comp = vec4<f32>(bg.rgb + fg_rgb * (1.0 - bg_occ), bg.a + fg.a * (1.0 - bg_occ));
346 comp = vec4<f32>(bg.rgb + fg_rgb * (1.0 - bg_occ), bg.a + fg_a_eff * (1.0 - bg_occ));
331347 } else if (op == 2) {
332348 // In: FG masked by BG alpha
333 comp = vec4<f32>(fg_rgb, fg.a) * bg.a;
349 comp = vec4<f32>(fg_rgb, fg_a_eff) * bg.a;
334350 } else if (op == 3) {
335351 // HeldOut: FG masked by (1 - BG alpha)
336 comp = vec4<f32>(fg_rgb, fg.a) * (1.0 - bg.a);
352 comp = vec4<f32>(fg_rgb, fg_a_eff) * (1.0 - bg.a);
337353 } else if (op == 4) {
338354 // Atop: FG inside BG + BG outside FG
339355 comp = vec4<f32>(fg_rgb * bg.a + bg.rgb * (1.0 - fg_occ), bg.a);
340356 } else if (op == 5) {
341357 // XOr
342 comp = vec4<f32>(fg_rgb, fg.a) * (1.0 - bg.a) + bg * (1.0 - fg_occ);
358 comp = vec4<f32>(fg_rgb, fg_a_eff) * (1.0 - bg.a) + bg * (1.0 - fg_occ);
343359 } else {
344360 // Over (default) with Fusion Alpha Gain, Subtractive/Additive, and BurnIn
345361 let over_rgb = fg_rgb + bg.rgb * (1.0 - fg_occ);
346 let over_a = fg.a * gain + bg.a * (1.0 - fg.a * gain);
362 let over_a = fg_a_eff + bg.a * (1.0 - fg_a_eff);
347363 comp = vec4<f32>(over_rgb, over_a);
348364 }
349365 out_col = mix(bg, comp, eff_blend);
350366 } else if (mode == 5) {
351367 // Fusion Difference operates directly on premultiplied RGB: |bg - fg * gain|
368 let fg_ap = fg.a * gain;
352369 let comp_rgb = abs(bg.rgb - fg.rgb * gain);
353 let comp_a = bg.a + fg.a * (1.0 - bg.a);
370 let comp_a = bg.a + fg_ap * (1.0 - bg.a);
354371 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
355372 } else if (mode == 11) {
356373 // Fusion Color Dodge operates in premultiplied RGB
357374 let fg_p = fg.rgb * gain;
358375 let fg_ap = fg.a * gain;
359 let comp_rgb = clamp(bg.rgb / max(1.0 - fg_p, vec3<f32>(1e-5)), vec3<f32>(0.0), vec3<f32>(1.0)) + fg_p * (1.0 - bg.a);
376 let comp_rgb = clamp(bg.rgb / max(1.0 - fg_p, vec3<f32>(1e-5)), vec3<f32>(0.0), vec3<f32>(1.0));
360377 let comp_a = bg.a + fg_ap * (1.0 - bg.a);
361378 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
362379 } else if (mode == 18) {
......@@ -367,20 +384,21 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {
367384 let comp_a = bg.a + fg_ap * (1.0 - bg.a);
368385 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
369386 } else if (mode == 14) {
370 // Fusion Hypotenuse operates directly on premultiplied RGB: sqrt(bg^2 + (fg * gain)^2)
387 // Fusion Hypotenuse operates directly on premultiplied RGBA: sqrt(bg^2 + (fg * gain)^2)
371388 let fg_p = fg.rgb * gain;
372389 let fg_ap = fg.a * gain;
373390 let comp_rgb = sqrt(max(bg.rgb * bg.rgb + fg_p * fg_p, vec3<f32>(0.0)));
374 let comp_a = bg.a + fg_ap * (1.0 - bg.a);
391 let comp_a = sqrt(max(bg.a * bg.a + fg_ap * fg_ap, 0.0));
375392 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
376393 } else if (mode == 20) {
377 // Fusion Geometric operates directly on premultiplied RGB: 2*bg*fg / (bg + fg)
394 // Fusion Geometric operates directly on premultiplied RGBA: 2*bg*fg / (bg + fg)
378395 let fg_p = fg.rgb * gain;
379396 let fg_ap = fg.a * gain;
380397 let denom = bg.rgb + fg_p;
381398 let safe_d = max(denom, vec3<f32>(1e-5));
382399 let comp_rgb = select(vec3<f32>(0.0), (2.0 * bg.rgb * fg_p) / safe_d, denom > vec3<f32>(1e-5));
383 let comp_a = bg.a + fg_ap * (1.0 - bg.a);
400 let denom_a = bg.a + fg_ap;
401 let comp_a = select(0.0, (2.0 * bg.a * fg_ap) / max(denom_a, 1e-5), denom_a > 1e-5);
384402 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
385403 } else if (mode == 15 || mode == 16) {
386404 // Fusion DarkerColor/LighterColor compare Over candidate against BG by Rec.601 luma
......@@ -395,16 +413,22 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {
395413 let comp_rgb = select(bg_branch, over_rgb, pick_over);
396414 let comp_a = bg.a + fg_a_eff * (1.0 - bg.a);
397415 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
398 } else {
416 } else if (mode == 19) {
417 // Fusion LinearBurn preserves unpremultiplied BG/FG coverage over transparent BG.
399418 let bg_u = select(vec3<f32>(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5);
400419 let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5);
401 // In Fusion's special fill-opacity modes (Screen, Exclusion),
402 // Gain scales FG color intensity BEFORE blending.
403 let scale_fg_rgb = (mode == 6 || mode == 1);
404 let fg_blend_in = select(fg_u, fg_u * gain, scale_fg_rgb);
405 let blended = apply_blend_mode(bg_u, fg_blend_in, mode);
406 let fg_eff = select(fg.a * gain, fg.a, scale_fg_rgb);
407 let comp_rgb = fg_eff * bg.a * blended + fg_eff * (1.0 - bg.a) * fg_blend_in + (1.0 - fg_eff) * bg.rgb;
420 let blended = apply_blend_mode(bg_u, fg_u, mode);
421 let fg_eff = fg.a * gain;
422 let comp_rgb = fg_eff * bg.a * blended + fg_eff * (1.0 - bg.a) * fg_u + (1.0 - fg_eff) * bg.rgb;
423 let comp_a = bg.a + fg_eff * (1.0 - bg.a);
424 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
425 } else {
426 // Fusion 21.1 ApplyMode != Normal blends premultiplied BG RGB with unpremultiplied FG RGB
427 // using FG effective alpha (fg.a * gain) regardless of Operator.
428 let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5);
429 let blended = apply_blend_mode(bg.rgb, fg_u, mode);
430 let fg_eff = fg.a * gain;
431 let comp_rgb = fg_eff * blended + (1.0 - fg_eff) * bg.rgb;
408432 let comp_a = bg.a + fg_eff * (1.0 - bg.a);
409433 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
410434 }
......@@ -575,7 +599,7 @@ fn channel_boolean_op(@builtin(global_invocation_id) gid: vec3<u32>) {
575599 let vb = select(cb_apply_op(op, bg.b, cb_pick(to_b, bg, fg, bg.b)), bg.b, to_b == 4);
576600 let va = select(cb_apply_op(op, bg.a, cb_pick(to_a, bg, fg, bg.a)), bg.a, to_a == 4);
577601
578 let res = clamp(vec4<f32>(vr, vg, vb, va), vec4<f32>(0.0), vec4<f32>(1.0));
602 let res = vec4<f32>(vr, vg, vb, va);
579603 let m = read_mask_uv(canvas_px / bg_dims, u.p[1].z, u.p[1].w) * u.p[1].y;
580604 let out_col = mix(bg, res, clamp(m, 0.0, 1.0));
581605 store_dst(vec2<i32>(gid.xy), out_col);