authoravatarclover caruso<git@paperclover.net>2026-11-30 18:54:20-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-30 19:14:04-08:00
log2f0d9d6103df077ffa2f3d6d1919e43c86055a69
treea041026c67d8de7a3c658f25f79e0518098a1952
parent41eb98669920b6fa00658188349211442e0beac4
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Fix 2D composite and color divergences across 2019-2026 archive comps (#38)


4 files changed, 549 insertions(+), 132 deletions(-)

crates/engine/src/eval/color.rs+148-11
...@@ -498,29 +498,30 @@ impl Engine {...@@ -498,29 +498,30 @@ impl Engine {
498 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);498 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);
499 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);499 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
500500
501 let lock_rgb = comp.eval_number(node, "LockRGB", time, 0.0, &self.expr) > 0.5;
501 let high_hue = comp.eval_number(node, "HighHue", time, 0.0, &self.expr) as f32;502 let high_hue = comp.eval_number(node, "HighHue", time, 0.0, &self.expr) as f32;
502 let mid_hue = comp.eval_number(node, "MidHue", time, 0.0, &self.expr) as f32;503 let mid_hue = comp.eval_number(node, "MidHue", time, 0.0, &self.expr) as f32;
503 let dark_hue = comp.eval_number(node, "DarkHue", time, 0.0, &self.expr) as f32;504 let dark_hue = comp.eval_number(node, "DarkHue", time, 0.0, &self.expr) as f32;
504 let sat_dark = comp.eval_number(node, "DarkSaturation", time, 1.0, &self.expr) as f32;
505 let sat_mid = comp.eval_number(node, "MidSaturation", time, 1.0, &self.expr) as f32;
506 let sat_high = comp.eval_number(node, "HighSaturation", time, 1.0, &self.expr) as f32;
507 let gain_r = comp.eval_number(node, "GainRed", time, 1.0, &self.expr) as f32;505 let gain_r = comp.eval_number(node, "GainRed", time, 1.0, &self.expr) as f32;
508 let gain_g = comp.eval_number(node, "GainGreen", time, 1.0, &self.expr) as f32;506 let gain_g = if lock_rgb { gain_r } else { comp.eval_number(node, "GainGreen", time, 1.0, &self.expr) as f32 };
509 let gain_b = comp.eval_number(node, "GainBlue", time, 1.0, &self.expr) as f32;507 let gain_b = if lock_rgb { gain_r } else { comp.eval_number(node, "GainBlue", time, 1.0, &self.expr) as f32 };
510 let lift_r = comp.eval_number(node, "LiftRed", time, 0.0, &self.expr) as f32;508 let lift_r = comp.eval_number(node, "LiftRed", time, 0.0, &self.expr) as f32;
511 let lift_g = comp.eval_number(node, "LiftGreen", time, 0.0, &self.expr) as f32;509 let lift_g = if lock_rgb { lift_r } else { comp.eval_number(node, "LiftGreen", time, 0.0, &self.expr) as f32 };
512 let lift_b = comp.eval_number(node, "LiftBlue", time, 0.0, &self.expr) as f32;510 let lift_b = if lock_rgb { lift_r } else { comp.eval_number(node, "LiftBlue", time, 0.0, &self.expr) as f32 };
513 let gamma_r = comp.eval_number(node, "GammaRed", time, 1.0, &self.expr) as f32;511 let gamma_r = comp.eval_number(node, "GammaRed", time, 1.0, &self.expr) as f32;
514 let gamma_g = comp.eval_number(node, "GammaGreen", time, 1.0, &self.expr) as f32;512 let gamma_g = if lock_rgb { gamma_r } else { comp.eval_number(node, "GammaGreen", time, 1.0, &self.expr) as f32 };
515 let gamma_b = comp.eval_number(node, "GammaBlue", time, 1.0, &self.expr) as f32;513 let gamma_b = if lock_rgb { gamma_r } else { comp.eval_number(node, "GammaBlue", time, 1.0, &self.expr) as f32 };
514 let sat_r = comp.eval_number(node, "SaturationRed", time, 1.0, &self.expr) as f32;
515 let sat_g = if lock_rgb { sat_r } else { comp.eval_number(node, "SaturationGreen", time, 1.0, &self.expr) as f32 };
516 let sat_b = if lock_rgb { sat_r } else { comp.eval_number(node, "SaturationBlue", time, 1.0, &self.expr) as f32 };
516 let prediv = comp.eval_number(node, "PreDividePostMultiply", time, 0.0, &self.expr) as f32;517 let prediv = comp.eval_number(node, "PreDividePostMultiply", time, 0.0, &self.expr) as f32;
517518
518 let mut u = UniformParams::default();519 let mut u = UniformParams::default();
519 u.p[0] = [gain_r, gain_g, gain_b, prediv];520 u.p[0] = [gain_r, gain_g, gain_b, prediv];
520 u.p[1] = [lift_r, lift_g, lift_b, blend];521 u.p[1] = [lift_r, lift_g, lift_b, blend];
521 u.p[2] = [gamma_r, gamma_g, gamma_b, sat_mid];522 u.p[2] = [gamma_r, gamma_g, gamma_b, sat_r];
522 u.p[3] = [dark_hue, mid_hue, high_hue, if mask.is_some() { 1.0 } else { 0.0 }];523 u.p[3] = [dark_hue, mid_hue, high_hue, if mask.is_some() { 1.0 } else { 0.0 }];
523 u.p[4] = [if mask_inv { 1.0 } else { 0.0 }, sat_dark, sat_high, 0.0];524 u.p[4] = [if mask_inv { 1.0 } else { 0.0 }, sat_g, sat_b, 0.0];
524525
525 let key = self.make_key(node, "ColorGain", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5]));526 let key = self.make_key(node, "ColorGain", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5]));
526 if let Some(hit) = self.cache.get(key, node.id, frame_i) {527 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
...@@ -807,3 +808,139 @@ impl Engine {...@@ -807,3 +808,139 @@ impl Engine {
807 }808 }
808809
809}810}
811
812#[cfg(test)]
813mod tests {
814 use super::{Comp, Engine};
815
816 fn eval_node_px(comp_src: &str, node_name: &str) -> [u8; 4] {
817 let comp = Comp::from_fusion_str(comp_src).expect("parse comp");
818 let id = comp.find_node(node_name).expect("find node");
819 let mut engine = Engine::new_headless().expect("headless engine");
820 let (tex, _) = engine.evaluate(&comp, id, 0.0);
821 let px = engine.readback_rgba(&tex).expect("readback");
822 let w = tex.size[0];
823 let i = ((tex.size[1] / 2 * w + w / 2) * 4) as usize;
824 [px[i], px[i + 1], px[i + 2], px[i + 3]]
825 }
826
827 #[test]
828 fn color_corrector_wheel_hue_and_tint_length_blend_linearly() {
829 // Pure gray (0.5, 0.5, 0.5) with WheelHue1=0.25 and WheelTintLength1=0.5 (TintMode=0):
830 // rgb_hue is neutral (0.5, 0.5, 0.5); TM0 tilt at angle 0.25*2pi + 27deg and length 0.5
831 // shifts toward warm green/yellow, blended linearly with mix(..., 0.5).
832 let comp = r#"{
833 Tools = ordered() {
834 Bg = Background {
835 Inputs = {
836 Width = Input { Value = 16, },
837 Height = Input { Value = 16, },
838 TopLeftRed = Input { Value = 0.5, },
839 TopLeftGreen = Input { Value = 0.5, },
840 TopLeftBlue = Input { Value = 0.5, },
841 },
842 },
843 CC = ColorCorrector {
844 Inputs = {
845 Input = Input { SourceOp = "Bg", Source = "Output", },
846 WheelHue1 = Input { Value = 0.25, },
847 WheelTintLength1 = Input { Value = 0.5, },
848 },
849 },
850 }
851 }"#;
852 let p = eval_node_px(comp, "CC");
853 // Distinctly red-shifted from neutral gray (128,128,128) at WheelHue1=0.25 with
854 // TintMode=0 linear mix(rgb_hue, rgb_tilt, 0.5) (~[217, 90, 90, 255]).
855 assert!(p[0] > 200 && p[1] == p[2] && p[1] < 100, "expected red-shifted tint, got {p:?}");
856 assert!((p[0] as i32 - 217).abs() <= 5 && (p[1] as i32 - 90).abs() <= 5, "p={p:?}");
857 }
858
859 #[test]
860 fn color_gain_lock_rgb_and_saturation_red() {
861 // With LockRGB = 1, GainRed=0.5 and SaturationRed=0.0 broadcast to all 3 channels.
862 let lock_on = r#"{
863 Tools = ordered() {
864 Bg = Background {
865 Inputs = {
866 Width = Input { Value = 16, },
867 Height = Input { Value = 16, },
868 TopLeftRed = Input { Value = 0.8, },
869 TopLeftGreen = Input { Value = 0.4, },
870 TopLeftBlue = Input { Value = 0.2, },
871 },
872 },
873 CG = ColorGain {
874 Inputs = {
875 Input = Input { SourceOp = "Bg", Source = "Output", },
876 LockRGB = Input { Value = 1, },
877 GainRed = Input { Value = 0.5, },
878 SaturationRed = Input { Value = 0.0, },
879 },
880 },
881 }
882 }"#;
883 let p_on = eval_node_px(lock_on, "CG");
884 // All channels scaled by 0.5 -> (0.4, 0.2, 0.1), then desaturated with Sat=0 -> uniform gray.
885 assert_eq!(p_on[0], p_on[1]);
886 assert_eq!(p_on[1], p_on[2]);
887
888 // With LockRGB omitted (defaults to 0 matching Render Node), GainRed=0.5 only scales Red,
889 // and SaturationRed=0.0 only desaturates Red toward Rec.601 luma.
890 let lock_off = r#"{
891 Tools = ordered() {
892 Bg = Background {
893 Inputs = {
894 Width = Input { Value = 16, },
895 Height = Input { Value = 16, },
896 TopLeftRed = Input { Value = 0.8, },
897 TopLeftGreen = Input { Value = 0.4, },
898 TopLeftBlue = Input { Value = 0.2, },
899 },
900 },
901 CG = ColorGain {
902 Inputs = {
903 Input = Input { SourceOp = "Bg", Source = "Output", },
904 GainRed = Input { Value = 0.5, },
905 SaturationRed = Input { Value = 0.0, },
906 },
907 },
908 }
909 }"#;
910 let p_off = eval_node_px(lock_off, "CG");
911 // After GainRed=0.5: (0.4, 0.4, 0.2), luma = 0.299*0.4 + 0.587*0.4 + 0.114*0.2 = 0.3772
912 // Red desaturated -> ~96, Green stays 0.4 -> ~102, Blue stays 0.2 -> ~51.
913 assert!((p_off[0] as i32 - 96).abs() <= 2, "p_off={p_off:?}");
914 assert!((p_off[1] as i32 - 102).abs() <= 2, "p_off={p_off:?}");
915 assert!((p_off[2] as i32 - 51).abs() <= 2, "p_off={p_off:?}");
916 }
917
918 #[test]
919 fn color_gain_quadratic_dark_and_high_hue_weights() {
920 // Pure red with Value=0.25 (u_dark = 1 - 0.25*2 = 0.5) and DarkHue=1/3 (+120 deg toward green):
921 // quadratic weight w_dark = 0.5^2 = 0.25 -> (0.1875, 0.0625, 0) ~ (48, 16, 0),
922 // rather than linear w = 0.5 -> (32, 32, 0).
923 let comp = r#"{
924 Tools = ordered() {
925 Bg = Background {
926 Inputs = {
927 Width = Input { Value = 16, },
928 Height = Input { Value = 16, },
929 TopLeftRed = Input { Value = 0.25, },
930 TopLeftGreen = Input { Value = 0.0, },
931 TopLeftBlue = Input { Value = 0.0, },
932 },
933 },
934 CG = ColorGain {
935 Inputs = {
936 Input = Input { SourceOp = "Bg", Source = "Output", },
937 DarkHue = Input { Value = 0.3333333, },
938 },
939 },
940 }
941 }"#;
942 let p = eval_node_px(comp, "CG");
943 assert!((p[0] as i32 - 48).abs() <= 2 && (p[1] as i32 - 16).abs() <= 2 && p[2] == 0, "p={p:?}");
944 }
945}
946
crates/engine/src/eval/composite.rs+245-1
...@@ -52,7 +52,7 @@ impl Engine {...@@ -52,7 +52,7 @@ impl Engine {
52 "Soft Light" => 9.0,52 "Soft Light" => 9.0,
53 "LinearLight" | "Linear Light" => 10.0,53 "LinearLight" | "Linear Light" => 10.0,
54 "Color Dodge" => 11.0,54 "Color Dodge" => 11.0,
55 "LinearDodge" | "Linear Dodge" | "Add" => 12.0,55 "LinearDodge" => 12.0,
56 "VividLight" | "Vivid Light" => 13.0,56 "VividLight" | "Vivid Light" => 13.0,
57 "Hypotenuse" => 14.0,57 "Hypotenuse" => 14.0,
58 "DarkerColor" | "Darker Color" => 15.0,58 "DarkerColor" | "Darker Color" => 15.0,
...@@ -411,3 +411,247 @@ impl Engine {...@@ -411,3 +411,247 @@ impl Engine {
411 }411 }
412412
413}413}
414
415#[cfg(test)]
416mod tests {
417 use super::{Comp, Engine};
418
419 fn eval_node_px(comp_src: &str, node_name: &str) -> (Vec<u8>, u32, u32) {
420 let comp = Comp::from_fusion_str(comp_src).expect("parse comp");
421 let id = comp.find_node(node_name).expect("find node");
422 let mut engine = Engine::new_headless().expect("headless engine");
423 let (tex, _) = engine.evaluate(&comp, id, 0.0);
424 let px = engine.readback_rgba(&tex).expect("readback");
425 (px, tex.size[0], tex.size[1])
426 }
427
428 fn px_at(px: &[u8], w: u32, x: u32, y: u32) -> [u8; 4] {
429 let i = ((y * w + x) * 4) as usize;
430 [px[i], px[i + 1], px[i + 2], px[i + 3]]
431 }
432
433 #[test]
434 fn background_gradients_use_pixel_aspect_space() {
435 // 16:9 canvas: diagonal Linear gradient normal must be perpendicular to the
436 // Start->End vector in pixel space (not skewed UV space), and Radial gradient
437 // distance must be circular in pixels rather than elliptical in UV.
438 let linear_comp = r#"{
439 Tools = ordered() {
440 BgLin = Background {
441 Inputs = {
442 Width = Input { Value = 320, },
443 Height = Input { Value = 180, },
444 Type = Input { Value = FuID { "Gradient" }, },
445 Start = Input { Value = { 0.0, 0.0 }, },
446 End = Input { Value = { 1.0, 1.0 }, },
447 Gradient = Input {
448 Value = Gradient {
449 Colors = {
450 [0] = { 0, 0, 0, 1, },
451 [1] = { 1, 0, 0, 1, },
452 },
453 },
454 },
455 },
456 },
457 }
458 }"#;
459 let (px, w, _) = eval_node_px(linear_comp, "BgLin");
460 // At pixel (x=160, y=90) -> exact midpoint t=0.5 (~128).
461 // At pixel (x=240, y=0) -> top-right? No, top-down y_img=0 => fu.y ~ 1.0, fu.x ~ 0.75:
462 // in pixel space pos=(240,180), end=(320,180):
463 // dot((240,180), (320,180)) / (320^2 + 180^2) = 109200 / 134800 = 0.810 -> ~207 (vs UV 0.875 -> 223).
464 let mid = px_at(&px, w, 160, 90);
465 assert!((mid[0] as i32 - 128).abs() <= 2, "mid={mid:?}");
466 let off = px_at(&px, w, 240, 0);
467 assert!((off[0] as i32 - 207).abs() <= 3, "expected ~207 in pixel space, got {off:?}");
468
469 let radial_comp = r#"{
470 Tools = ordered() {
471 BgRad = Background {
472 Inputs = {
473 Width = Input { Value = 320, },
474 Height = Input { Value = 180, },
475 Type = Input { Value = FuID { "Gradient" }, },
476 GradientType = Input { Value = FuID { "Radial" }, },
477 Start = Input { Value = { 0.5, 0.5 }, },
478 End = Input { Value = { 1.0, 0.5 }, },
479 Gradient = Input {
480 Value = Gradient {
481 Colors = {
482 [0] = { 0, 0, 0, 1, },
483 [1] = { 1, 0, 0, 1, },
484 },
485 },
486 },
487 },
488 },
489 }
490 }"#;
491 let (rpx, rw, _) = eval_node_px(radial_comp, "BgRad");
492 // Radius = 160 px horizontally. At 80 px right of center (240, 90) AND 80 px
493 // vertically from center (160, 10): both must be t=0.5 (~128) for circular radial.
494 let horiz = px_at(&rpx, rw, 240, 90);
495 let vert = px_at(&rpx, rw, 160, 10);
496 assert!((horiz[0] as i32 - 128).abs() <= 3, "horiz={horiz:?}");
497 assert!((vert[0] as i32 - 128).abs() <= 3, "vert={vert:?} (circular radial failed)");
498 }
499
500 #[test]
501 fn merge_linear_dodge_and_hsl_luma_modes() {
502 // LinearDodge must scale semi-transparent fg by Gain=0.5 just like Normal over bg.
503 let ld_comp = r#"{
504 Tools = ordered() {
505 Bg = Background {
506 Inputs = {
507 Width = Input { Value = 16, },
508 Height = Input { Value = 16, },
509 TopLeftRed = Input { Value = 0.4, },
510 TopLeftGreen = Input { Value = 0.2, },
511 TopLeftBlue = Input { Value = 0.1, },
512 TopLeftAlpha = Input { Value = 1.0, },
513 },
514 },
515 Fg = Background {
516 Inputs = {
517 Width = Input { Value = 16, },
518 Height = Input { Value = 16, },
519 TopLeftRed = Input { Value = 0.2, },
520 TopLeftGreen = Input { Value = 0.3, },
521 TopLeftBlue = Input { Value = 0.4, },
522 TopLeftAlpha = Input { Value = 0.5, },
523 },
524 },
525 M = Merge {
526 Inputs = {
527 Background = Input { SourceOp = "Bg", Source = "Output", },
528 Foreground = Input { SourceOp = "Fg", Source = "Output", },
529 ApplyMode = Input { Value = FuID { "LinearDodge" }, },
530 Gain = Input { Value = 0.5, },
531 },
532 },
533 }
534 }"#;
535 let (ld_px, ld_w, _) = eval_node_px(ld_comp, "M");
536 let p = px_at(&ld_px, ld_w, 8, 8);
537 // Background outputs fg=(0.2, 0.3, 0.4, 0.5); LinearDodge scales by Gain=0.5 directly ->
538 // bg (0.4, 0.2, 0.1) + fg * 0.5 (0.1, 0.15, 0.2) = (0.50, 0.35, 0.30) -> (128, 90, 77).
539 assert!((p[0] as i32 - 128).abs() <= 2, "LinearDodge R={p:?}");
540 assert!((p[1] as i32 - 90).abs() <= 2, "LinearDodge G={p:?}");
541 assert!((p[2] as i32 - 77).abs() <= 2, "LinearDodge B={p:?}");
542
543 // Geometric with fg=(0,0,0,1) over opaque bg must evaluate to 0, not > 0.
544 let geom_comp = r#"{
545 Tools = ordered() {
546 Bg = Background {
547 Inputs = {
548 Width = Input { Value = 16, },
549 Height = Input { Value = 16, },
550 TopLeftRed = Input { Value = 0.8, },
551 TopLeftGreen = Input { Value = 0.5, },
552 TopLeftBlue = Input { Value = 0.2, },
553 },
554 },
555 Fg = Background {
556 Inputs = {
557 Width = Input { Value = 16, },
558 Height = Input { Value = 16, },
559 TopLeftRed = Input { Value = 0.0, },
560 TopLeftGreen = Input { Value = 0.0, },
561 TopLeftBlue = Input { Value = 0.0, },
562 },
563 },
564 M = Merge {
565 Inputs = {
566 Background = Input { SourceOp = "Bg", Source = "Output", },
567 Foreground = Input { SourceOp = "Fg", Source = "Output", },
568 ApplyMode = Input { Value = FuID { "Geometric" }, },
569 },
570 },
571 }
572 }"#;
573 let (g_px, g_w, _) = eval_node_px(geom_comp, "M");
574 let gp = px_at(&g_px, g_w, 8, 8);
575 assert_eq!([gp[0], gp[1], gp[2]], [0, 0, 0]);
576
577 // Luminosity must transfer fg Rec.601 luma to bg without W3C clip_color desaturation.
578 // bg = (0.0, 0.4, 0.8) [lum = 0.3264], fg = (0.9, 0.9, 0.9) [lum = 0.9]
579 // bg + (0.9 - 0.3264) = (0.5736, 0.9736, 1.3736 -> clamped 1.0) -> (~146, ~248, 255)
580 let lum_comp = r#"{
581 Tools = ordered() {
582 Bg = Background {
583 Inputs = {
584 Width = Input { Value = 16, },
585 Height = Input { Value = 16, },
586 TopLeftRed = Input { Value = 0.0, },
587 TopLeftGreen = Input { Value = 0.4, },
588 TopLeftBlue = Input { Value = 0.8, },
589 },
590 },
591 Fg = Background {
592 Inputs = {
593 Width = Input { Value = 16, },
594 Height = Input { Value = 16, },
595 TopLeftRed = Input { Value = 0.9, },
596 TopLeftGreen = Input { Value = 0.9, },
597 TopLeftBlue = Input { Value = 0.9, },
598 },
599 },
600 M = Merge {
601 Inputs = {
602 Background = Input { SourceOp = "Bg", Source = "Output", },
603 Foreground = Input { SourceOp = "Fg", Source = "Output", },
604 ApplyMode = Input { Value = FuID { "Luminosity" }, },
605 },
606 },
607 }
608 }"#;
609 let (l_px, l_w, _) = eval_node_px(lum_comp, "M");
610 let lp = px_at(&l_px, l_w, 8, 8);
611 assert!((lp[0] as i32 - 146).abs() <= 3, "Luminosity={lp:?}");
612 assert!((lp[1] as i32 - 248).abs() <= 3, "Luminosity={lp:?}");
613 assert_eq!(lp[2], 255);
614 }
615
616 #[test]
617 fn merge_darker_and_lighter_color_respect_semi_transparent_bg() {
618 // DarkerColor over semi-transparent bg=(0.2,0.2,0.2,0.5) with fg=(0.8,0.8,0.8,1.0):
619 // over_rgb = 0.8 > bg.rgb = 0.2, so bg wins and produces bg.rgb + fg.rgb*(1-bg.a) = 0.6 (~153).
620 let comp = r#"{
621 Tools = ordered() {
622 Bg = Background {
623 Inputs = {
624 Width = Input { Value = 16, },
625 Height = Input { Value = 16, },
626 TopLeftRed = Input { Value = 0.2, },
627 TopLeftGreen = Input { Value = 0.2, },
628 TopLeftBlue = Input { Value = 0.2, },
629 TopLeftAlpha = Input { Value = 0.5, },
630 },
631 },
632 Fg = Background {
633 Inputs = {
634 Width = Input { Value = 16, },
635 Height = Input { Value = 16, },
636 TopLeftRed = Input { Value = 0.8, },
637 TopLeftGreen = Input { Value = 0.8, },
638 TopLeftBlue = Input { Value = 0.8, },
639 TopLeftAlpha = Input { Value = 1.0, },
640 },
641 },
642 MDark = Merge {
643 Inputs = {
644 Background = Input { SourceOp = "Bg", Source = "Output", },
645 Foreground = Input { SourceOp = "Fg", Source = "Output", },
646 ApplyMode = Input { Value = FuID { "Darker Color" }, },
647 },
648 },
649 }
650 }"#;
651 let (px, w, _) = eval_node_px(comp, "MDark");
652 let p = px_at(&px, w, 8, 8);
653 assert!((p[0] as i32 - 153).abs() <= 2, "DarkerColor={p:?}");
654 assert_eq!(p[3], 255);
655 }
656}
657
crates/engine/src/shaders/color.wgsl+76-69
...@@ -123,51 +123,56 @@ fn apply_cc_wheel_and_lgg(...@@ -123,51 +123,56 @@ fn apply_cc_wheel_and_lgg(
123 let gamma = max(gamma_in, 1e-4);123 let gamma = max(gamma_in, 1e-4);
124 var rgb = rgb_in;124 var rgb = rgb_in;
125125
126 // 1. Hue rotation (verified 0.00/255 vs Fusion 21.1)126 // 1 & 2. Hue rotation + Fusion Color Wheel Tint
127 if (abs(hue_turns) > 1e-5) {127 if (tint_len > 1e-5 && tint_mode < 0.5) {
128 rgb = rotate_hue_preserve_luma(rgb, -hue_turns * 6.28318530718);128 // TintMode = 0: linear blend between Hue-rotated color and RGB cube tilt (27 deg * len, verified 0/255!)
129 }129 let rgb_hue = select(rgb, rotate_hue_preserve_luma(rgb, -hue_turns * 6.28318530718), abs(hue_turns) > 1e-5);
130
131 // 2. Fusion Color Wheel Tint
132 if (tint_len > 1e-5) {
133 let l = clamp(tint_len, 0.0, 1.0);130 let l = clamp(tint_len, 0.0, 1.0);
134 if (tint_mode < 0.5) {131 let s3 = 0.5773502691896258;
135 // TintMode = 0 (RGB cube tilt + perpendicular chroma squeeze, verified 0.00/255 at len=1.0!)132 let e1 = vec3<f32>(s3, s3, s3);
136 let s3 = 0.5773502691896258;133 let a = tint_turns * 6.28318530718;
137 let e1 = vec3<f32>(s3, s3, s3);134 let u_ch = normalize(vec3<f32>(cos(a), cos(a - 2.09439510239), cos(a + 2.09439510239)));
138 let a = tint_turns * 6.28318530718;135 let u_pp = normalize(cross(e1, u_ch));
139 let u_ch = normalize(vec3<f32>(cos(a), cos(a - 2.09439510239), cos(a + 2.09439510239)));136 let th = 0.471238898038 * l; // 27.0 deg * len
140 let u_pp = normalize(cross(e1, u_ch));137 let c_th = cos(th);
141 let t = l * l;138 let s_th = sin(th);
142 let th = 0.471238898038 * t; // 27.0 deg * len^2139 let x1 = dot(rgb, e1);
143 let c_th = cos(th);140 let x2 = dot(rgb, u_ch);
144 let s_th = sin(th);141 let x3 = dot(rgb, u_pp);
145 let x1 = dot(rgb, e1);142 let y1 = c_th * x1 - s_th * x2;
146 let x2 = dot(rgb, u_ch);143 let y2 = s_th * x1 + c_th * x2;
147 let x3 = dot(rgb, u_pp);144 let y3 = (1.0 - l) * x3;
148 let y1 = c_th * x1 - s_th * x2;145 let rgb_tilt = y1 * e1 + y2 * u_ch + y3 * u_pp;
149 let y2 = s_th * x1 + c_th * x2;146 rgb = mix(rgb_hue, rgb_tilt, l);
150 let y3 = (1.0 - t) * x3;147 } else {
151 rgb = y1 * e1 + y2 * u_ch + y3 * u_pp;148 // Hue rotation (verified 0.00/255 vs Fusion 21.1)
152 } else if (tint_mode < 1.5) {149 if (abs(hue_turns) > 1e-5) {
153 // TintMode = 1: luma-preserving chroma blend + amp = l*((1-l) + l*2*min(y,1-y)), verified 0.00/255!150 rgb = rotate_hue_preserve_luma(rgb, -hue_turns * 6.28318530718);
154 let y_t = dot(rgb, k_luma);151 }
155 let pure = hsv_pure_hue(tint_turns);152
156 let pure_y = dot(pure, k_luma);153 // Fusion Color Wheel Tint (TintMode 1 & 2)
157 let tc = pure - vec3<f32>(pure_y);154 if (tint_len > 1e-5) {
158 let yc = clamp(y_t, 0.0, 1.0);155 let l = clamp(tint_len, 0.0, 1.0);
159 let amp = l * ((1.0 - l) + l * 2.0 * min(yc, 1.0 - yc));156 if (tint_mode < 1.5) {
160 rgb = vec3<f32>(y_t) + (rgb - vec3<f32>(y_t)) * (1.0 - l) + tc * amp;157 // TintMode = 1: luma-preserving chroma blend + amp = l*((1-l) + l*2*min(y,1-y)), verified 0.00/255!
161 } else {158 let y_t = dot(rgb, k_luma);
162 // TintMode = 2 (Fusion 21.1 default when omitted): lerp toward pure HSV chroma * 2*min(y,1-y)159 let pure = hsv_pure_hue(tint_turns);
163 let y_t = dot(rgb, k_luma);160 let pure_y = dot(pure, k_luma);
164 let pure = hsv_pure_hue(tint_turns);161 let tc = pure - vec3<f32>(pure_y);
165 let pure_y = dot(pure, k_luma);162 let yc = clamp(y_t, 0.0, 1.0);
166 let tc = pure - vec3<f32>(pure_y);163 let amp = l * ((1.0 - l) + l * 2.0 * min(yc, 1.0 - yc));
167 let yc = clamp(y_t, 0.0, 1.0);164 rgb = vec3<f32>(y_t) + (rgb - vec3<f32>(y_t)) * (1.0 - l) + tc * amp;
168 let amp = 2.0 * min(yc, 1.0 - yc);165 } else {
169 let tint_rgb = vec3<f32>(y_t) + tc * amp;166 // TintMode = 2 (Fusion 21.1 default when omitted): lerp toward pure HSV chroma * 2*min(y,1-y)
170 rgb = mix(rgb, tint_rgb, l);167 let y_t = dot(rgb, k_luma);
168 let pure = hsv_pure_hue(tint_turns);
169 let pure_y = dot(pure, k_luma);
170 let tc = pure - vec3<f32>(pure_y);
171 let yc = clamp(y_t, 0.0, 1.0);
172 let amp = 2.0 * min(yc, 1.0 - yc);
173 let tint_rgb = vec3<f32>(y_t) + tc * amp;
174 rgb = mix(rgb, tint_rgb, l);
175 }
171 }176 }
172 }177 }
173178
...@@ -290,9 +295,9 @@ fn invert_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -290,9 +295,9 @@ fn invert_op(@builtin(global_invocation_id) gid: vec3<u32>) {
290// 19. ColorGain (Non-luma-preserving HSV Hue shift across Dark/Mid/High + per-channel Gain/Lift/Gamma/Sat)295// 19. ColorGain (Non-luma-preserving HSV Hue shift across Dark/Mid/High + per-channel Gain/Lift/Gamma/Sat)
291// p[0] = (gain_r, gain_g, gain_b, prediv)296// p[0] = (gain_r, gain_g, gain_b, prediv)
292// p[1] = (lift_r, lift_g, lift_b, blend)297// p[1] = (lift_r, lift_g, lift_b, blend)
293// p[2] = (gamma_r, gamma_g, gamma_b, sat_mid)298// p[2] = (gamma_r, gamma_g, gamma_b, sat_r)
294// p[3] = (dark_hue, mid_hue, high_hue, has_mask)299// p[3] = (dark_hue, mid_hue, high_hue, has_mask)
295// p[4] = (mask_invert, sat_dark, sat_high, 0)300// p[4] = (mask_invert, sat_g, sat_b, 0)
296// ============================================================================301// ============================================================================
297fn rgb_to_hsv(c: vec3<f32>) -> vec3<f32> {302fn rgb_to_hsv(c: vec3<f32>) -> vec3<f32> {
298 let k = vec4<f32>(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);303 let k = vec4<f32>(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
...@@ -319,49 +324,51 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -319,49 +324,51 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) {
319 let lift = u.p[1].xyz;324 let lift = u.p[1].xyz;
320 let blend = u.p[1].w;325 let blend = u.p[1].w;
321 let gamma = max(u.p[2].xyz, vec3<f32>(1e-4));326 let gamma = max(u.p[2].xyz, vec3<f32>(1e-4));
322 let sat_mid = u.p[2].w;
323 let dark_hue = u.p[3].x;327 let dark_hue = u.p[3].x;
324 let mid_hue = u.p[3].y;328 let mid_hue = u.p[3].y;
325 let high_hue = u.p[3].z;329 let high_hue = u.p[3].z;
326 let sat_dark = u.p[4].y;330 let sat = vec3<f32>(u.p[2].w, u.p[4].y, u.p[4].z);
327 let sat_high = u.p[4].z;
328331
329 var rgb = src.rgb;332 var rgb = src.rgb;
330 if (prediv && src.a > 1e-5) {333 if (prediv && src.a > 1e-5) {
331 rgb = rgb / src.a;334 rgb = rgb / src.a;
332 }335 }
333336
334 // Per-channel Gain -> Lift -> Gamma337 // Per-channel Gain/Lift -> Gamma -> Saturation (Rec.601 luma mix)
335 rgb = rgb * gain;338 rgb = rgb * (gain - lift) + lift;
336 rgb = rgb + lift * (1.0 - rgb);339 if (!HDR_FLOAT) {
340 rgb = clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0));
341 }
337 rgb = pow(max(rgb, vec3<f32>(0.0)), vec3<f32>(1.0) / gamma);342 rgb = pow(max(rgb, vec3<f32>(0.0)), vec3<f32>(1.0) / gamma);
343 if (any(abs(sat - vec3<f32>(1.0)) > vec3<f32>(1e-4))) {
344 let luma = dot(rgb, vec3<f32>(0.299, 0.587, 0.114));
345 rgb = mix(vec3<f32>(luma), rgb, sat);
346 }
338347
339 // Tonal range weights using HSV Value max(r,g,b) so saturated primaries & outer anti-aliased348 // Tonal range weights using HSV Value max(r,g,b):
340 // outline edges (>= 0.5) land cleanly in Mid without reddish DarkHue halos, while RGB band blending349 // Fusion ColorGain uses quadratic falloff u^2 on Dark/High and linear 1 - u_dark - u_high on Mid.
341 // completely prevents green hue-interpolation fringes!
342 let c_pos = max(rgb, vec3<f32>(0.0));350 let c_pos = max(rgb, vec3<f32>(0.0));
343 let lum = clamp(max(c_pos.r, max(c_pos.g, c_pos.b)), 0.0, 1.0);351 let lum = clamp(max(c_pos.r, max(c_pos.g, c_pos.b)), 0.0, 1.0);
344 let w_dark = clamp(1.0 - lum * 2.0, 0.0, 1.0);352 let u_dark = clamp(1.0 - lum * 2.0, 0.0, 1.0);
345 let w_high = clamp((lum - 0.5) * 2.0, 0.0, 1.0);353 let u_high = clamp((lum - 0.5) * 2.0, 0.0, 1.0);
346 let w_mid = clamp(1.0 - w_dark - w_high, 0.0, 1.0);354 let w_dark = u_dark * u_dark;
347355 let w_high = u_high * u_high;
348 // Fusion ColorGain evaluates Dark, High, then Mid sequentially as RGB mixes against base_hsv356 let w_mid = clamp(1.0 - u_dark - u_high, 0.0, 1.0);
349 // using V = max(r,g,b) for the range curve — matches Fusion 21.1 to ~0.002 and stays green-fringe-free!357
350 let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5358 let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5;
351 || abs(sat_dark - 1.0) > 1e-4 || abs(sat_mid - 1.0) > 1e-4 || abs(sat_high - 1.0) > 1e-4;
352 if (any_hsv) {359 if (any_hsv) {
353 let base_hsv = rgb_to_hsv(c_pos);360 let base_hsv = rgb_to_hsv(c_pos);
354 var cur = c_pos;361 var cur = c_pos;
355 if ((abs(dark_hue) > 1e-5 || abs(sat_dark - 1.0) > 1e-4) && w_dark > 1e-5) {362 if (abs(dark_hue) > 1e-5 && w_dark > 1e-5) {
356 let h_d = vec3<f32>(fract(base_hsv.x + dark_hue), clamp(base_hsv.y * sat_dark, 0.0, 1.0), base_hsv.z);363 let h_d = vec3<f32>(fract(base_hsv.x + dark_hue), base_hsv.y, base_hsv.z);
357 cur = mix(cur, hsv_to_rgb(h_d), w_dark);364 cur = mix(cur, hsv_to_rgb(h_d), w_dark);
358 }365 }
359 if ((abs(high_hue) > 1e-5 || abs(sat_high - 1.0) > 1e-4) && w_high > 1e-5) {366 if (abs(high_hue) > 1e-5 && w_high > 1e-5) {
360 let h_h = vec3<f32>(fract(base_hsv.x + high_hue), clamp(base_hsv.y * sat_high, 0.0, 1.0), base_hsv.z);367 let h_h = vec3<f32>(fract(base_hsv.x + high_hue), base_hsv.y, base_hsv.z);
361 cur = mix(cur, hsv_to_rgb(h_h), w_high);368 cur = mix(cur, hsv_to_rgb(h_h), w_high);
362 }369 }
363 if ((abs(mid_hue) > 1e-5 || abs(sat_mid - 1.0) > 1e-4) && w_mid > 1e-5) {370 if (abs(mid_hue) > 1e-5 && w_mid > 1e-5) {
364 let h_m = vec3<f32>(fract(base_hsv.x + mid_hue), clamp(base_hsv.y * sat_mid, 0.0, 1.0), base_hsv.z);371 let h_m = vec3<f32>(fract(base_hsv.x + mid_hue), base_hsv.y, base_hsv.z);
365 cur = mix(cur, hsv_to_rgb(h_m), w_mid);372 cur = mix(cur, hsv_to_rgb(h_m), w_mid);
366 }373 }
367 rgb = cur;374 rgb = cur;
crates/engine/src/shaders/composite.wgsl+80-51
...@@ -20,16 +20,18 @@ fn bg_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -20,16 +20,18 @@ fn bg_op(@builtin(global_invocation_id) gid: vec3<u32>) {
20 let bot = mix(u.p[2], u.p[3], fu.x);20 let bot = mix(u.p[2], u.p[3], fu.x);
21 col = mix(bot, top, fu.y);21 col = mix(bot, top, fu.y);
22 } else if (kind == 4 || kind == 5) {22 } else if (kind == 4 || kind == 5) {
23 let s = u.p[4].zw;23 let fdims = vec2<f32>(dims);
24 let e = u.p[5].xy;24 let p_px = fu * fdims;
25 let d = e - s;25 let s_px = u.p[4].zw * fdims;
26 let e_px = u.p[5].xy * fdims;
27 let d_px = e_px - s_px;
26 var t = 0.0;28 var t = 0.0;
27 if (kind == 5) {29 if (kind == 5) {
28 let r = max(length(d), 1e-5);30 let r = max(length(d_px), 1e-5);
29 t = clamp(length(fu - s) / r, 0.0, 1.0);31 t = clamp(length(p_px - s_px) / r, 0.0, 1.0);
30 } else {32 } else {
31 let len2 = max(dot(d, d), 1e-6);33 let len2 = max(dot(d_px, d_px), 1e-6);
32 t = clamp(dot(fu - s, d) / len2, 0.0, 1.0);34 t = clamp(dot(p_px - s_px, d_px) / len2, 0.0, 1.0);
33 }35 }
34 col = mix(u.p[0], u.p[1], t);36 col = mix(u.p[0], u.p[1], t);
35 }37 }
...@@ -50,37 +52,41 @@ fn blend_lum(c: vec3<f32>) -> f32 {...@@ -50,37 +52,41 @@ fn blend_lum(c: vec3<f32>) -> f32 {
50 return dot(c, vec3<f32>(0.299, 0.587, 0.114));52 return dot(c, vec3<f32>(0.299, 0.587, 0.114));
51}53}
5254
53fn clip_color(col_in: vec3<f32>) -> vec3<f32> {55fn merge_rgb_to_hsl(c: vec3<f32>) -> vec3<f32> {
54 var c = col_in;56 let mx = max(c.r, max(c.g, c.b));
55 let l = blend_lum(c);57 let mn = min(c.r, min(c.g, c.b));
56 let n = min(c.r, min(c.g, c.b));58 let l = 0.5 * (mx + mn);
57 let x = max(c.r, max(c.g, c.b));59 let d = mx - mn;
58 if (n < 0.0) {60 if (d <= 1e-6) {
59 c = vec3<f32>(l) + ((c - vec3<f32>(l)) * l) / max(l - n, 1e-5);61 return vec3<f32>(0.0, 0.0, l);
60 }62 }
61 if (x > 1.0) {63 let denom = max(1.0 - abs(2.0 * l - 1.0), 1e-5);
62 c = vec3<f32>(l) + ((c - vec3<f32>(l)) * (1.0 - l)) / max(x - l, 1e-5);64 let s = d / denom;
65 var h = 0.0;
66 if (mx == c.r) {
67 h = (c.g - c.b) / d + select(0.0, 6.0, c.g < c.b);
68 } else if (mx == c.g) {
69 h = (c.b - c.r) / d + 2.0;
70 } else {
71 h = (c.r - c.g) / d + 4.0;
63 }72 }
64 return c;73 return vec3<f32>(h / 6.0, s, l);
65}74}
6675
67fn set_lum(c: vec3<f32>, l: f32) -> vec3<f32> {76fn merge_hsl_to_rgb(hsl: vec3<f32>) -> vec3<f32> {
68 let d = l - blend_lum(c);77 let c = (1.0 - abs(2.0 * hsl.z - 1.0)) * hsl.y;
69 return clip_color(c + vec3<f32>(d));78 let h6 = fract(hsl.x) * 6.0;
70}79 let h2 = h6 - 2.0 * floor(h6 * 0.5);
7180 let x = c * (1.0 - abs(h2 - 1.0));
72fn sat_val(c: vec3<f32>) -> f32 {81 let m = hsl.z - 0.5 * c;
73 return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b));82 var rgb = vec3<f32>(0.0);
74}83 if (h6 < 1.0) { rgb = vec3<f32>(c, x, 0.0); }
7584 else if (h6 < 2.0) { rgb = vec3<f32>(x, c, 0.0); }
76fn set_sat(c_in: vec3<f32>, s: f32) -> vec3<f32> {85 else if (h6 < 3.0) { rgb = vec3<f32>(0.0, c, x); }
77 let mn = min(c_in.r, min(c_in.g, c_in.b));86 else if (h6 < 4.0) { rgb = vec3<f32>(0.0, x, c); }
78 let mx = max(c_in.r, max(c_in.g, c_in.b));87 else if (h6 < 5.0) { rgb = vec3<f32>(x, 0.0, c); }
79 let span = mx - mn;88 else { rgb = vec3<f32>(c, 0.0, x); }
80 if (span <= 1e-5) {89 return rgb + vec3<f32>(m);
81 return vec3<f32>(0.0);
82 }
83 return (c_in - vec3<f32>(mn)) * (s / span);
84}90}
8591
86fn apply_blend_mode(bg: vec3<f32>, fg: vec3<f32>, mode: i32) -> vec3<f32> {92fn apply_blend_mode(bg: vec3<f32>, fg: vec3<f32>, mode: i32) -> vec3<f32> {
...@@ -162,17 +168,22 @@ fn apply_blend_mode(bg: vec3<f32>, fg: vec3<f32>, mode: i32) -> vec3<f32> {...@@ -162,17 +168,22 @@ fn apply_blend_mode(bg: vec3<f32>, fg: vec3<f32>, mode: i32) -> vec3<f32> {
162 case 20: { // Geometric168 case 20: { // Geometric
163 return (2.0 * fg * bg) / max(fg + bg, vec3<f32>(1e-5));169 return (2.0 * fg * bg) / max(fg + bg, vec3<f32>(1e-5));
164 }170 }
165 case 21: { // Hue171 case 21: { // Hue (Fusion HSL)
166 return set_lum(set_sat(fg, sat_val(bg)), blend_lum(bg));172 let h_fg = merge_rgb_to_hsl(fg).x;
173 let hsl_bg = merge_rgb_to_hsl(bg);
174 return merge_hsl_to_rgb(vec3<f32>(h_fg, hsl_bg.y, hsl_bg.z));
167 }175 }
168 case 22: { // Saturation176 case 22: { // Saturation (Fusion HSL; BG hue evaluated in BGR channel order to match Fusion 21.1)
169 return set_lum(set_sat(bg, sat_val(fg)), blend_lum(bg));177 let h_bg = merge_rgb_to_hsl(bg.bgr).x;
178 let s_fg = merge_rgb_to_hsl(fg).y;
179 let l_bg = merge_rgb_to_hsl(bg).z;
180 return merge_hsl_to_rgb(vec3<f32>(h_bg, s_fg, l_bg));
170 }181 }
171 case 23: { // Color182 case 23: { // Color (Fusion Rec.601 luma transfer, no gamut clipping)
172 return set_lum(fg, blend_lum(bg));183 return fg + vec3<f32>(blend_lum(bg) - blend_lum(fg));
173 }184 }
174 case 24: { // Luminosity185 case 24: { // Luminosity (Fusion Rec.601 luma transfer, no gamut clipping)
175 return set_lum(bg, blend_lum(fg));186 return bg + vec3<f32>(blend_lum(fg) - blend_lum(bg));
176 }187 }
177 default: { // 0: Normal188 default: { // 0: Normal
178 return fg;189 return fg;
...@@ -355,23 +366,41 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -355,23 +366,41 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {
355 let comp_rgb = clamp(1.0 - (1.0 - bg.rgb) / max(1.0 - fg_ap + fg_p, vec3<f32>(1e-5)), vec3<f32>(0.0), vec3<f32>(1.0));366 let comp_rgb = clamp(1.0 - (1.0 - bg.rgb) / max(1.0 - fg_ap + fg_p, vec3<f32>(1e-5)), vec3<f32>(0.0), vec3<f32>(1.0));
356 let comp_a = bg.a + fg_ap * (1.0 - bg.a);367 let comp_a = bg.a + fg_ap * (1.0 - bg.a);
357 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);368 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
369 } else if (mode == 14) {
370 // Fusion Hypotenuse operates directly on premultiplied RGB: sqrt(bg^2 + (fg * gain)^2)
371 let fg_p = fg.rgb * gain;
372 let fg_ap = fg.a * gain;
373 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);
375 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
376 } else if (mode == 20) {
377 // Fusion Geometric operates directly on premultiplied RGB: 2*bg*fg / (bg + fg)
378 let fg_p = fg.rgb * gain;
379 let fg_ap = fg.a * gain;
380 let denom = bg.rgb + fg_p;
381 let safe_d = max(denom, vec3<f32>(1e-5));
382 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);
384 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
358 } else if (mode == 15 || mode == 16) {385 } else if (mode == 15 || mode == 16) {
359 // Fusion DarkerColor/LighterColor with AlphaGain: candidate is FG + BG*(1 - fg.a*gain),386 // Fusion DarkerColor/LighterColor compare Over candidate against BG by Rec.601 luma
360 // compared against BG by Rec.601 luma! (Verified bit-exact on probe_merge f09 & f10!)
361 let fg_a_eff = fg.a * gain;387 let fg_a_eff = fg.a * gain;
362 let bg_u = select(vec3<f32>(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5);388 let over_rgb = fg.rgb + bg.rgb * (1.0 - fg_a_eff);
363 let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5);389 let bg_branch = bg.rgb + fg.rgb * (1.0 - bg.a);
364 let cand = fg_u + bg_u * (1.0 - fg_a_eff);390 let pick_over = select(
365 let blended = apply_blend_mode(bg_u, cand, mode);391 blend_lum(over_rgb) > blend_lum(bg.rgb),
366 let comp_rgb = bg.a * fg.a * blended + fg.rgb * (1.0 - bg.a) + (1.0 - fg.a) * bg.rgb;392 blend_lum(over_rgb) < blend_lum(bg.rgb),
393 mode == 15,
394 );
395 let comp_rgb = select(bg_branch, over_rgb, pick_over);
367 let comp_a = bg.a + fg_a_eff * (1.0 - bg.a);396 let comp_a = bg.a + fg_a_eff * (1.0 - bg.a);
368 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);397 out_col = mix(bg, vec4<f32>(comp_rgb, comp_a), eff_blend);
369 } else {398 } else {
370 let bg_u = select(vec3<f32>(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5);399 let bg_u = select(vec3<f32>(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5);
371 let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5);400 let fg_u = select(vec3<f32>(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5);
372 // In Fusion's special fill-opacity modes (Exclusion, Screen, LinearDodge),401 // In Fusion's special fill-opacity modes (Screen, Exclusion),
373 // Gain scales FG color intensity BEFORE blending.402 // Gain scales FG color intensity BEFORE blending.
374 let scale_fg_rgb = (mode == 6 || mode == 1 || mode == 12);403 let scale_fg_rgb = (mode == 6 || mode == 1);
375 let fg_blend_in = select(fg_u, fg_u * gain, scale_fg_rgb);404 let fg_blend_in = select(fg_u, fg_u * gain, scale_fg_rgb);
376 let blended = apply_blend_mode(bg_u, fg_blend_in, mode);405 let blended = apply_blend_mode(bg_u, fg_blend_in, mode);
377 let fg_eff = select(fg.a * gain, fg.a, scale_fg_rgb);406 let fg_eff = select(fg.a * gain, fg.a, scale_fg_rgb);