From 2f0d9d6103df077ffa2f3d6d1919e43c86055a69 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 30 Nov 2026 18:54:20 -0800 Subject: [PATCH] Fix 2D composite and color divergences across 2019-2026 archive comps (#38) --- crates/engine/src/eval/color.rs | 159 ++++++++++++++- crates/engine/src/eval/composite.rs | 246 ++++++++++++++++++++++- crates/engine/src/shaders/color.wgsl | 143 ++++++------- crates/engine/src/shaders/composite.wgsl | 131 +++++++----- 4 files changed, 548 insertions(+), 131 deletions(-) diff --git a/crates/engine/src/eval/color.rs b/crates/engine/src/eval/color.rs index 3479f60dd98c7a06a9be788cd7916774d77fe683..5ed889315b24e49d2a65199f473f1f84c8401035 100644 --- a/crates/engine/src/eval/color.rs +++ b/crates/engine/src/eval/color.rs @@ -498,29 +498,30 @@ impl Engine { let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); + let lock_rgb = comp.eval_number(node, "LockRGB", time, 0.0, &self.expr) > 0.5; let high_hue = comp.eval_number(node, "HighHue", time, 0.0, &self.expr) as f32; let mid_hue = comp.eval_number(node, "MidHue", time, 0.0, &self.expr) as f32; let dark_hue = comp.eval_number(node, "DarkHue", time, 0.0, &self.expr) as f32; - let sat_dark = comp.eval_number(node, "DarkSaturation", time, 1.0, &self.expr) as f32; - let sat_mid = comp.eval_number(node, "MidSaturation", time, 1.0, &self.expr) as f32; - let sat_high = comp.eval_number(node, "HighSaturation", time, 1.0, &self.expr) as f32; let gain_r = comp.eval_number(node, "GainRed", time, 1.0, &self.expr) as f32; - let gain_g = comp.eval_number(node, "GainGreen", time, 1.0, &self.expr) as f32; - let gain_b = comp.eval_number(node, "GainBlue", time, 1.0, &self.expr) as f32; + let gain_g = if lock_rgb { gain_r } else { comp.eval_number(node, "GainGreen", time, 1.0, &self.expr) as f32 }; + let gain_b = if lock_rgb { gain_r } else { comp.eval_number(node, "GainBlue", time, 1.0, &self.expr) as f32 }; let lift_r = comp.eval_number(node, "LiftRed", time, 0.0, &self.expr) as f32; - let lift_g = comp.eval_number(node, "LiftGreen", time, 0.0, &self.expr) as f32; - let lift_b = comp.eval_number(node, "LiftBlue", time, 0.0, &self.expr) as f32; + let lift_g = if lock_rgb { lift_r } else { comp.eval_number(node, "LiftGreen", time, 0.0, &self.expr) as f32 }; + let lift_b = if lock_rgb { lift_r } else { comp.eval_number(node, "LiftBlue", time, 0.0, &self.expr) as f32 }; let gamma_r = comp.eval_number(node, "GammaRed", time, 1.0, &self.expr) as f32; - let gamma_g = comp.eval_number(node, "GammaGreen", time, 1.0, &self.expr) as f32; - let gamma_b = comp.eval_number(node, "GammaBlue", time, 1.0, &self.expr) as f32; + let gamma_g = if lock_rgb { gamma_r } else { comp.eval_number(node, "GammaGreen", time, 1.0, &self.expr) as f32 }; + let gamma_b = if lock_rgb { gamma_r } else { comp.eval_number(node, "GammaBlue", time, 1.0, &self.expr) as f32 }; + let sat_r = comp.eval_number(node, "SaturationRed", time, 1.0, &self.expr) as f32; + let sat_g = if lock_rgb { sat_r } else { comp.eval_number(node, "SaturationGreen", time, 1.0, &self.expr) as f32 }; + let sat_b = if lock_rgb { sat_r } else { comp.eval_number(node, "SaturationBlue", time, 1.0, &self.expr) as f32 }; let prediv = comp.eval_number(node, "PreDividePostMultiply", time, 0.0, &self.expr) as f32; let mut u = UniformParams::default(); u.p[0] = [gain_r, gain_g, gain_b, prediv]; u.p[1] = [lift_r, lift_g, lift_b, blend]; - u.p[2] = [gamma_r, gamma_g, gamma_b, sat_mid]; + u.p[2] = [gamma_r, gamma_g, gamma_b, sat_r]; u.p[3] = [dark_hue, mid_hue, high_hue, if mask.is_some() { 1.0 } else { 0.0 }]; - u.p[4] = [if mask_inv { 1.0 } else { 0.0 }, sat_dark, sat_high, 0.0]; + u.p[4] = [if mask_inv { 1.0 } else { 0.0 }, sat_g, sat_b, 0.0]; let key = self.make_key(node, "ColorGain", src.size, &[src.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5])); if let Some(hit) = self.cache.get(key, node.id, frame_i) { @@ -807,3 +808,139 @@ impl Engine { } } + +#[cfg(test)] +mod tests { + use super::{Comp, Engine}; + + fn eval_node_px(comp_src: &str, node_name: &str) -> [u8; 4] { + let comp = Comp::from_fusion_str(comp_src).expect("parse comp"); + let id = comp.find_node(node_name).expect("find node"); + let mut engine = Engine::new_headless().expect("headless engine"); + let (tex, _) = engine.evaluate(&comp, id, 0.0); + let px = engine.readback_rgba(&tex).expect("readback"); + let w = tex.size[0]; + let i = ((tex.size[1] / 2 * w + w / 2) * 4) as usize; + [px[i], px[i + 1], px[i + 2], px[i + 3]] + } + + #[test] + fn color_corrector_wheel_hue_and_tint_length_blend_linearly() { + // Pure gray (0.5, 0.5, 0.5) with WheelHue1=0.25 and WheelTintLength1=0.5 (TintMode=0): + // rgb_hue is neutral (0.5, 0.5, 0.5); TM0 tilt at angle 0.25*2pi + 27deg and length 0.5 + // shifts toward warm green/yellow, blended linearly with mix(..., 0.5). + let comp = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.5, }, + TopLeftGreen = Input { Value = 0.5, }, + TopLeftBlue = Input { Value = 0.5, }, + }, + }, + CC = ColorCorrector { + Inputs = { + Input = Input { SourceOp = "Bg", Source = "Output", }, + WheelHue1 = Input { Value = 0.25, }, + WheelTintLength1 = Input { Value = 0.5, }, + }, + }, + } + }"#; + let p = eval_node_px(comp, "CC"); + // Distinctly red-shifted from neutral gray (128,128,128) at WheelHue1=0.25 with + // TintMode=0 linear mix(rgb_hue, rgb_tilt, 0.5) (~[217, 90, 90, 255]). + assert!(p[0] > 200 && p[1] == p[2] && p[1] < 100, "expected red-shifted tint, got {p:?}"); + assert!((p[0] as i32 - 217).abs() <= 5 && (p[1] as i32 - 90).abs() <= 5, "p={p:?}"); + } + + #[test] + fn color_gain_lock_rgb_and_saturation_red() { + // With LockRGB = 1, GainRed=0.5 and SaturationRed=0.0 broadcast to all 3 channels. + let lock_on = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.8, }, + TopLeftGreen = Input { Value = 0.4, }, + TopLeftBlue = Input { Value = 0.2, }, + }, + }, + CG = ColorGain { + Inputs = { + Input = Input { SourceOp = "Bg", Source = "Output", }, + LockRGB = Input { Value = 1, }, + GainRed = Input { Value = 0.5, }, + SaturationRed = Input { Value = 0.0, }, + }, + }, + } + }"#; + let p_on = eval_node_px(lock_on, "CG"); + // All channels scaled by 0.5 -> (0.4, 0.2, 0.1), then desaturated with Sat=0 -> uniform gray. + assert_eq!(p_on[0], p_on[1]); + assert_eq!(p_on[1], p_on[2]); + + // With LockRGB omitted (defaults to 0 matching Render Node), GainRed=0.5 only scales Red, + // and SaturationRed=0.0 only desaturates Red toward Rec.601 luma. + let lock_off = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.8, }, + TopLeftGreen = Input { Value = 0.4, }, + TopLeftBlue = Input { Value = 0.2, }, + }, + }, + CG = ColorGain { + Inputs = { + Input = Input { SourceOp = "Bg", Source = "Output", }, + GainRed = Input { Value = 0.5, }, + SaturationRed = Input { Value = 0.0, }, + }, + }, + } + }"#; + let p_off = eval_node_px(lock_off, "CG"); + // 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 + // Red desaturated -> ~96, Green stays 0.4 -> ~102, Blue stays 0.2 -> ~51. + assert!((p_off[0] as i32 - 96).abs() <= 2, "p_off={p_off:?}"); + assert!((p_off[1] as i32 - 102).abs() <= 2, "p_off={p_off:?}"); + assert!((p_off[2] as i32 - 51).abs() <= 2, "p_off={p_off:?}"); + } + + #[test] + fn color_gain_quadratic_dark_and_high_hue_weights() { + // Pure red with Value=0.25 (u_dark = 1 - 0.25*2 = 0.5) and DarkHue=1/3 (+120 deg toward green): + // quadratic weight w_dark = 0.5^2 = 0.25 -> (0.1875, 0.0625, 0) ~ (48, 16, 0), + // rather than linear w = 0.5 -> (32, 32, 0). + let comp = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.25, }, + TopLeftGreen = Input { Value = 0.0, }, + TopLeftBlue = Input { Value = 0.0, }, + }, + }, + CG = ColorGain { + Inputs = { + Input = Input { SourceOp = "Bg", Source = "Output", }, + DarkHue = Input { Value = 0.3333333, }, + }, + }, + } + }"#; + let p = eval_node_px(comp, "CG"); + assert!((p[0] as i32 - 48).abs() <= 2 && (p[1] as i32 - 16).abs() <= 2 && p[2] == 0, "p={p:?}"); + } +} + diff --git a/crates/engine/src/eval/composite.rs b/crates/engine/src/eval/composite.rs index 8ada9af52be4c98b0c64ac21cff89589f2985223..93bc69f5df36c278325aa3d74208d7458051f2e7 100644 --- a/crates/engine/src/eval/composite.rs +++ b/crates/engine/src/eval/composite.rs @@ -52,7 +52,7 @@ impl Engine { "Soft Light" => 9.0, "LinearLight" | "Linear Light" => 10.0, "Color Dodge" => 11.0, - "LinearDodge" | "Linear Dodge" | "Add" => 12.0, + "LinearDodge" => 12.0, "VividLight" | "Vivid Light" => 13.0, "Hypotenuse" => 14.0, "DarkerColor" | "Darker Color" => 15.0, @@ -411,3 +411,247 @@ impl Engine { } } + +#[cfg(test)] +mod tests { + use super::{Comp, Engine}; + + fn eval_node_px(comp_src: &str, node_name: &str) -> (Vec, u32, u32) { + let comp = Comp::from_fusion_str(comp_src).expect("parse comp"); + let id = comp.find_node(node_name).expect("find node"); + let mut engine = Engine::new_headless().expect("headless engine"); + let (tex, _) = engine.evaluate(&comp, id, 0.0); + let px = engine.readback_rgba(&tex).expect("readback"); + (px, tex.size[0], tex.size[1]) + } + + fn px_at(px: &[u8], w: u32, x: u32, y: u32) -> [u8; 4] { + let i = ((y * w + x) * 4) as usize; + [px[i], px[i + 1], px[i + 2], px[i + 3]] + } + + #[test] + fn background_gradients_use_pixel_aspect_space() { + // 16:9 canvas: diagonal Linear gradient normal must be perpendicular to the + // Start->End vector in pixel space (not skewed UV space), and Radial gradient + // distance must be circular in pixels rather than elliptical in UV. + let linear_comp = r#"{ + Tools = ordered() { + BgLin = Background { + Inputs = { + Width = Input { Value = 320, }, + Height = Input { Value = 180, }, + Type = Input { Value = FuID { "Gradient" }, }, + Start = Input { Value = { 0.0, 0.0 }, }, + End = Input { Value = { 1.0, 1.0 }, }, + Gradient = Input { + Value = Gradient { + Colors = { + [0] = { 0, 0, 0, 1, }, + [1] = { 1, 0, 0, 1, }, + }, + }, + }, + }, + }, + } + }"#; + let (px, w, _) = eval_node_px(linear_comp, "BgLin"); + // At pixel (x=160, y=90) -> exact midpoint t=0.5 (~128). + // At pixel (x=240, y=0) -> top-right? No, top-down y_img=0 => fu.y ~ 1.0, fu.x ~ 0.75: + // in pixel space pos=(240,180), end=(320,180): + // dot((240,180), (320,180)) / (320^2 + 180^2) = 109200 / 134800 = 0.810 -> ~207 (vs UV 0.875 -> 223). + let mid = px_at(&px, w, 160, 90); + assert!((mid[0] as i32 - 128).abs() <= 2, "mid={mid:?}"); + let off = px_at(&px, w, 240, 0); + assert!((off[0] as i32 - 207).abs() <= 3, "expected ~207 in pixel space, got {off:?}"); + + let radial_comp = r#"{ + Tools = ordered() { + BgRad = Background { + Inputs = { + Width = Input { Value = 320, }, + Height = Input { Value = 180, }, + Type = Input { Value = FuID { "Gradient" }, }, + GradientType = Input { Value = FuID { "Radial" }, }, + Start = Input { Value = { 0.5, 0.5 }, }, + End = Input { Value = { 1.0, 0.5 }, }, + Gradient = Input { + Value = Gradient { + Colors = { + [0] = { 0, 0, 0, 1, }, + [1] = { 1, 0, 0, 1, }, + }, + }, + }, + }, + }, + } + }"#; + let (rpx, rw, _) = eval_node_px(radial_comp, "BgRad"); + // Radius = 160 px horizontally. At 80 px right of center (240, 90) AND 80 px + // vertically from center (160, 10): both must be t=0.5 (~128) for circular radial. + let horiz = px_at(&rpx, rw, 240, 90); + let vert = px_at(&rpx, rw, 160, 10); + assert!((horiz[0] as i32 - 128).abs() <= 3, "horiz={horiz:?}"); + assert!((vert[0] as i32 - 128).abs() <= 3, "vert={vert:?} (circular radial failed)"); + } + + #[test] + fn merge_linear_dodge_and_hsl_luma_modes() { + // LinearDodge must scale semi-transparent fg by Gain=0.5 just like Normal over bg. + let ld_comp = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.4, }, + TopLeftGreen = Input { Value = 0.2, }, + TopLeftBlue = Input { Value = 0.1, }, + TopLeftAlpha = Input { Value = 1.0, }, + }, + }, + Fg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.2, }, + TopLeftGreen = Input { Value = 0.3, }, + TopLeftBlue = Input { Value = 0.4, }, + TopLeftAlpha = Input { Value = 0.5, }, + }, + }, + M = Merge { + Inputs = { + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + ApplyMode = Input { Value = FuID { "LinearDodge" }, }, + Gain = Input { Value = 0.5, }, + }, + }, + } + }"#; + let (ld_px, ld_w, _) = eval_node_px(ld_comp, "M"); + let p = px_at(&ld_px, ld_w, 8, 8); + // Background outputs fg=(0.2, 0.3, 0.4, 0.5); LinearDodge scales by Gain=0.5 directly -> + // 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). + assert!((p[0] as i32 - 128).abs() <= 2, "LinearDodge R={p:?}"); + assert!((p[1] as i32 - 90).abs() <= 2, "LinearDodge G={p:?}"); + assert!((p[2] as i32 - 77).abs() <= 2, "LinearDodge B={p:?}"); + + // Geometric with fg=(0,0,0,1) over opaque bg must evaluate to 0, not > 0. + let geom_comp = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.8, }, + TopLeftGreen = Input { Value = 0.5, }, + TopLeftBlue = Input { Value = 0.2, }, + }, + }, + Fg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.0, }, + TopLeftGreen = Input { Value = 0.0, }, + TopLeftBlue = Input { Value = 0.0, }, + }, + }, + M = Merge { + Inputs = { + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + ApplyMode = Input { Value = FuID { "Geometric" }, }, + }, + }, + } + }"#; + let (g_px, g_w, _) = eval_node_px(geom_comp, "M"); + let gp = px_at(&g_px, g_w, 8, 8); + assert_eq!([gp[0], gp[1], gp[2]], [0, 0, 0]); + + // Luminosity must transfer fg Rec.601 luma to bg without W3C clip_color desaturation. + // bg = (0.0, 0.4, 0.8) [lum = 0.3264], fg = (0.9, 0.9, 0.9) [lum = 0.9] + // bg + (0.9 - 0.3264) = (0.5736, 0.9736, 1.3736 -> clamped 1.0) -> (~146, ~248, 255) + let lum_comp = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.0, }, + TopLeftGreen = Input { Value = 0.4, }, + TopLeftBlue = Input { Value = 0.8, }, + }, + }, + Fg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.9, }, + TopLeftGreen = Input { Value = 0.9, }, + TopLeftBlue = Input { Value = 0.9, }, + }, + }, + M = Merge { + Inputs = { + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + ApplyMode = Input { Value = FuID { "Luminosity" }, }, + }, + }, + } + }"#; + let (l_px, l_w, _) = eval_node_px(lum_comp, "M"); + let lp = px_at(&l_px, l_w, 8, 8); + assert!((lp[0] as i32 - 146).abs() <= 3, "Luminosity={lp:?}"); + assert!((lp[1] as i32 - 248).abs() <= 3, "Luminosity={lp:?}"); + assert_eq!(lp[2], 255); + } + + #[test] + fn merge_darker_and_lighter_color_respect_semi_transparent_bg() { + // DarkerColor over semi-transparent bg=(0.2,0.2,0.2,0.5) with fg=(0.8,0.8,0.8,1.0): + // over_rgb = 0.8 > bg.rgb = 0.2, so bg wins and produces bg.rgb + fg.rgb*(1-bg.a) = 0.6 (~153). + let comp = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.2, }, + TopLeftGreen = Input { Value = 0.2, }, + TopLeftBlue = Input { Value = 0.2, }, + TopLeftAlpha = Input { Value = 0.5, }, + }, + }, + Fg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.8, }, + TopLeftGreen = Input { Value = 0.8, }, + TopLeftBlue = Input { Value = 0.8, }, + TopLeftAlpha = Input { Value = 1.0, }, + }, + }, + MDark = Merge { + Inputs = { + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + ApplyMode = Input { Value = FuID { "Darker Color" }, }, + }, + }, + } + }"#; + let (px, w, _) = eval_node_px(comp, "MDark"); + let p = px_at(&px, w, 8, 8); + assert!((p[0] as i32 - 153).abs() <= 2, "DarkerColor={p:?}"); + assert_eq!(p[3], 255); + } +} + diff --git a/crates/engine/src/shaders/color.wgsl b/crates/engine/src/shaders/color.wgsl index 3696e07b9c59321a7b65b52a5f42f446fa7ac308..6e40f345e69414fa8251e660474cd38becb29e8b 100644 --- a/crates/engine/src/shaders/color.wgsl +++ b/crates/engine/src/shaders/color.wgsl @@ -123,51 +123,56 @@ fn apply_cc_wheel_and_lgg( let gamma = max(gamma_in, 1e-4); var rgb = rgb_in; - // 1. Hue rotation (verified 0.00/255 vs Fusion 21.1) - if (abs(hue_turns) > 1e-5) { - rgb = rotate_hue_preserve_luma(rgb, -hue_turns * 6.28318530718); - } - - // 2. Fusion Color Wheel Tint - if (tint_len > 1e-5) { + // 1 & 2. Hue rotation + Fusion Color Wheel Tint + if (tint_len > 1e-5 && tint_mode < 0.5) { + // TintMode = 0: linear blend between Hue-rotated color and RGB cube tilt (27 deg * len, verified 0/255!) + let rgb_hue = select(rgb, rotate_hue_preserve_luma(rgb, -hue_turns * 6.28318530718), abs(hue_turns) > 1e-5); let l = clamp(tint_len, 0.0, 1.0); - if (tint_mode < 0.5) { - // TintMode = 0 (RGB cube tilt + perpendicular chroma squeeze, verified 0.00/255 at len=1.0!) - let s3 = 0.5773502691896258; - let e1 = vec3(s3, s3, s3); - let a = tint_turns * 6.28318530718; - let u_ch = normalize(vec3(cos(a), cos(a - 2.09439510239), cos(a + 2.09439510239))); - let u_pp = normalize(cross(e1, u_ch)); - let t = l * l; - let th = 0.471238898038 * t; // 27.0 deg * len^2 - let c_th = cos(th); - let s_th = sin(th); - let x1 = dot(rgb, e1); - let x2 = dot(rgb, u_ch); - let x3 = dot(rgb, u_pp); - let y1 = c_th * x1 - s_th * x2; - let y2 = s_th * x1 + c_th * x2; - let y3 = (1.0 - t) * x3; - rgb = y1 * e1 + y2 * u_ch + y3 * u_pp; - } else if (tint_mode < 1.5) { - // TintMode = 1: luma-preserving chroma blend + amp = l*((1-l) + l*2*min(y,1-y)), verified 0.00/255! - let y_t = dot(rgb, k_luma); - let pure = hsv_pure_hue(tint_turns); - let pure_y = dot(pure, k_luma); - let tc = pure - vec3(pure_y); - let yc = clamp(y_t, 0.0, 1.0); - let amp = l * ((1.0 - l) + l * 2.0 * min(yc, 1.0 - yc)); - rgb = vec3(y_t) + (rgb - vec3(y_t)) * (1.0 - l) + tc * amp; - } else { - // TintMode = 2 (Fusion 21.1 default when omitted): lerp toward pure HSV chroma * 2*min(y,1-y) - let y_t = dot(rgb, k_luma); - let pure = hsv_pure_hue(tint_turns); - let pure_y = dot(pure, k_luma); - let tc = pure - vec3(pure_y); - let yc = clamp(y_t, 0.0, 1.0); - let amp = 2.0 * min(yc, 1.0 - yc); - let tint_rgb = vec3(y_t) + tc * amp; - rgb = mix(rgb, tint_rgb, l); + let s3 = 0.5773502691896258; + let e1 = vec3(s3, s3, s3); + let a = tint_turns * 6.28318530718; + let u_ch = normalize(vec3(cos(a), cos(a - 2.09439510239), cos(a + 2.09439510239))); + let u_pp = normalize(cross(e1, u_ch)); + let th = 0.471238898038 * l; // 27.0 deg * len + let c_th = cos(th); + let s_th = sin(th); + let x1 = dot(rgb, e1); + let x2 = dot(rgb, u_ch); + let x3 = dot(rgb, u_pp); + let y1 = c_th * x1 - s_th * x2; + let y2 = s_th * x1 + c_th * x2; + let y3 = (1.0 - l) * x3; + let rgb_tilt = y1 * e1 + y2 * u_ch + y3 * u_pp; + rgb = mix(rgb_hue, rgb_tilt, l); + } else { + // Hue rotation (verified 0.00/255 vs Fusion 21.1) + if (abs(hue_turns) > 1e-5) { + rgb = rotate_hue_preserve_luma(rgb, -hue_turns * 6.28318530718); + } + + // Fusion Color Wheel Tint (TintMode 1 & 2) + if (tint_len > 1e-5) { + let l = clamp(tint_len, 0.0, 1.0); + if (tint_mode < 1.5) { + // TintMode = 1: luma-preserving chroma blend + amp = l*((1-l) + l*2*min(y,1-y)), verified 0.00/255! + let y_t = dot(rgb, k_luma); + let pure = hsv_pure_hue(tint_turns); + let pure_y = dot(pure, k_luma); + let tc = pure - vec3(pure_y); + let yc = clamp(y_t, 0.0, 1.0); + let amp = l * ((1.0 - l) + l * 2.0 * min(yc, 1.0 - yc)); + rgb = vec3(y_t) + (rgb - vec3(y_t)) * (1.0 - l) + tc * amp; + } else { + // TintMode = 2 (Fusion 21.1 default when omitted): lerp toward pure HSV chroma * 2*min(y,1-y) + let y_t = dot(rgb, k_luma); + let pure = hsv_pure_hue(tint_turns); + let pure_y = dot(pure, k_luma); + let tc = pure - vec3(pure_y); + let yc = clamp(y_t, 0.0, 1.0); + let amp = 2.0 * min(yc, 1.0 - yc); + let tint_rgb = vec3(y_t) + tc * amp; + rgb = mix(rgb, tint_rgb, l); + } } } @@ -290,9 +295,9 @@ fn invert_op(@builtin(global_invocation_id) gid: vec3) { // 19. ColorGain (Non-luma-preserving HSV Hue shift across Dark/Mid/High + per-channel Gain/Lift/Gamma/Sat) // p[0] = (gain_r, gain_g, gain_b, prediv) // p[1] = (lift_r, lift_g, lift_b, blend) -// p[2] = (gamma_r, gamma_g, gamma_b, sat_mid) +// p[2] = (gamma_r, gamma_g, gamma_b, sat_r) // p[3] = (dark_hue, mid_hue, high_hue, has_mask) -// p[4] = (mask_invert, sat_dark, sat_high, 0) +// p[4] = (mask_invert, sat_g, sat_b, 0) // ============================================================================ fn rgb_to_hsv(c: vec3) -> vec3 { let k = vec4(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) { let lift = u.p[1].xyz; let blend = u.p[1].w; let gamma = max(u.p[2].xyz, vec3(1e-4)); - let sat_mid = u.p[2].w; let dark_hue = u.p[3].x; let mid_hue = u.p[3].y; let high_hue = u.p[3].z; - let sat_dark = u.p[4].y; - let sat_high = u.p[4].z; + let sat = vec3(u.p[2].w, u.p[4].y, u.p[4].z); var rgb = src.rgb; if (prediv && src.a > 1e-5) { rgb = rgb / src.a; } - // Per-channel Gain -> Lift -> Gamma - rgb = rgb * gain; - rgb = rgb + lift * (1.0 - rgb); + // Per-channel Gain/Lift -> Gamma -> Saturation (Rec.601 luma mix) + rgb = rgb * (gain - lift) + lift; + if (!HDR_FLOAT) { + rgb = clamp(rgb, vec3(0.0), vec3(1.0)); + } rgb = pow(max(rgb, vec3(0.0)), vec3(1.0) / gamma); + if (any(abs(sat - vec3(1.0)) > vec3(1e-4))) { + let luma = dot(rgb, vec3(0.299, 0.587, 0.114)); + rgb = mix(vec3(luma), rgb, sat); + } - // Tonal range weights using HSV Value max(r,g,b) so saturated primaries & outer anti-aliased - // outline edges (>= 0.5) land cleanly in Mid without reddish DarkHue halos, while RGB band blending - // completely prevents green hue-interpolation fringes! + // Tonal range weights using HSV Value max(r,g,b): + // Fusion ColorGain uses quadratic falloff u^2 on Dark/High and linear 1 - u_dark - u_high on Mid. let c_pos = max(rgb, vec3(0.0)); let lum = clamp(max(c_pos.r, max(c_pos.g, c_pos.b)), 0.0, 1.0); - let w_dark = clamp(1.0 - lum * 2.0, 0.0, 1.0); - let w_high = clamp((lum - 0.5) * 2.0, 0.0, 1.0); - let w_mid = clamp(1.0 - w_dark - w_high, 0.0, 1.0); + let u_dark = clamp(1.0 - lum * 2.0, 0.0, 1.0); + let u_high = clamp((lum - 0.5) * 2.0, 0.0, 1.0); + let w_dark = u_dark * u_dark; + let w_high = u_high * u_high; + let w_mid = clamp(1.0 - u_dark - u_high, 0.0, 1.0); - // Fusion ColorGain evaluates Dark, High, then Mid sequentially as RGB mixes against base_hsv - // using V = max(r,g,b) for the range curve — matches Fusion 21.1 to ~0.002 and stays green-fringe-free! - let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5 - || abs(sat_dark - 1.0) > 1e-4 || abs(sat_mid - 1.0) > 1e-4 || abs(sat_high - 1.0) > 1e-4; + let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5; if (any_hsv) { let base_hsv = rgb_to_hsv(c_pos); var cur = c_pos; - if ((abs(dark_hue) > 1e-5 || abs(sat_dark - 1.0) > 1e-4) && w_dark > 1e-5) { - let h_d = vec3(fract(base_hsv.x + dark_hue), clamp(base_hsv.y * sat_dark, 0.0, 1.0), base_hsv.z); + if (abs(dark_hue) > 1e-5 && w_dark > 1e-5) { + let h_d = vec3(fract(base_hsv.x + dark_hue), base_hsv.y, base_hsv.z); cur = mix(cur, hsv_to_rgb(h_d), w_dark); } - if ((abs(high_hue) > 1e-5 || abs(sat_high - 1.0) > 1e-4) && w_high > 1e-5) { - let h_h = vec3(fract(base_hsv.x + high_hue), clamp(base_hsv.y * sat_high, 0.0, 1.0), base_hsv.z); + if (abs(high_hue) > 1e-5 && w_high > 1e-5) { + let h_h = vec3(fract(base_hsv.x + high_hue), base_hsv.y, base_hsv.z); cur = mix(cur, hsv_to_rgb(h_h), w_high); } - if ((abs(mid_hue) > 1e-5 || abs(sat_mid - 1.0) > 1e-4) && w_mid > 1e-5) { - let h_m = vec3(fract(base_hsv.x + mid_hue), clamp(base_hsv.y * sat_mid, 0.0, 1.0), base_hsv.z); + if (abs(mid_hue) > 1e-5 && w_mid > 1e-5) { + let h_m = vec3(fract(base_hsv.x + mid_hue), base_hsv.y, base_hsv.z); cur = mix(cur, hsv_to_rgb(h_m), w_mid); } rgb = cur; diff --git a/crates/engine/src/shaders/composite.wgsl b/crates/engine/src/shaders/composite.wgsl index b2cf06dfcc927d4ff35e212bd5d807e3c9e21fca..3e02953ce08cf547c00b2a1392ec000145b85030 100644 --- a/crates/engine/src/shaders/composite.wgsl +++ b/crates/engine/src/shaders/composite.wgsl @@ -20,16 +20,18 @@ fn bg_op(@builtin(global_invocation_id) gid: vec3) { let bot = mix(u.p[2], u.p[3], fu.x); col = mix(bot, top, fu.y); } else if (kind == 4 || kind == 5) { - let s = u.p[4].zw; - let e = u.p[5].xy; - let d = e - s; + let fdims = vec2(dims); + let p_px = fu * fdims; + let s_px = u.p[4].zw * fdims; + let e_px = u.p[5].xy * fdims; + let d_px = e_px - s_px; var t = 0.0; if (kind == 5) { - let r = max(length(d), 1e-5); - t = clamp(length(fu - s) / r, 0.0, 1.0); + let r = max(length(d_px), 1e-5); + t = clamp(length(p_px - s_px) / r, 0.0, 1.0); } else { - let len2 = max(dot(d, d), 1e-6); - t = clamp(dot(fu - s, d) / len2, 0.0, 1.0); + let len2 = max(dot(d_px, d_px), 1e-6); + t = clamp(dot(p_px - s_px, d_px) / len2, 0.0, 1.0); } col = mix(u.p[0], u.p[1], t); } @@ -50,37 +52,41 @@ fn blend_lum(c: vec3) -> f32 { return dot(c, vec3(0.299, 0.587, 0.114)); } -fn clip_color(col_in: vec3) -> vec3 { - var c = col_in; - let l = blend_lum(c); - let n = min(c.r, min(c.g, c.b)); - let x = max(c.r, max(c.g, c.b)); - if (n < 0.0) { - c = vec3(l) + ((c - vec3(l)) * l) / max(l - n, 1e-5); +fn merge_rgb_to_hsl(c: vec3) -> vec3 { + let mx = max(c.r, max(c.g, c.b)); + let mn = min(c.r, min(c.g, c.b)); + let l = 0.5 * (mx + mn); + let d = mx - mn; + if (d <= 1e-6) { + return vec3(0.0, 0.0, l); } - if (x > 1.0) { - c = vec3(l) + ((c - vec3(l)) * (1.0 - l)) / max(x - l, 1e-5); + let denom = max(1.0 - abs(2.0 * l - 1.0), 1e-5); + let s = d / denom; + var h = 0.0; + if (mx == c.r) { + h = (c.g - c.b) / d + select(0.0, 6.0, c.g < c.b); + } else if (mx == c.g) { + h = (c.b - c.r) / d + 2.0; + } else { + h = (c.r - c.g) / d + 4.0; } - return c; + return vec3(h / 6.0, s, l); } -fn set_lum(c: vec3, l: f32) -> vec3 { - let d = l - blend_lum(c); - return clip_color(c + vec3(d)); -} - -fn sat_val(c: vec3) -> f32 { - return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b)); -} - -fn set_sat(c_in: vec3, s: f32) -> vec3 { - let mn = min(c_in.r, min(c_in.g, c_in.b)); - let mx = max(c_in.r, max(c_in.g, c_in.b)); - let span = mx - mn; - if (span <= 1e-5) { - return vec3(0.0); - } - return (c_in - vec3(mn)) * (s / span); +fn merge_hsl_to_rgb(hsl: vec3) -> vec3 { + let c = (1.0 - abs(2.0 * hsl.z - 1.0)) * hsl.y; + let h6 = fract(hsl.x) * 6.0; + let h2 = h6 - 2.0 * floor(h6 * 0.5); + let x = c * (1.0 - abs(h2 - 1.0)); + let m = hsl.z - 0.5 * c; + var rgb = vec3(0.0); + if (h6 < 1.0) { rgb = vec3(c, x, 0.0); } + else if (h6 < 2.0) { rgb = vec3(x, c, 0.0); } + else if (h6 < 3.0) { rgb = vec3(0.0, c, x); } + else if (h6 < 4.0) { rgb = vec3(0.0, x, c); } + else if (h6 < 5.0) { rgb = vec3(x, 0.0, c); } + else { rgb = vec3(c, 0.0, x); } + return rgb + vec3(m); } fn apply_blend_mode(bg: vec3, fg: vec3, mode: i32) -> vec3 { @@ -162,17 +168,22 @@ fn apply_blend_mode(bg: vec3, fg: vec3, mode: i32) -> vec3 { case 20: { // Geometric return (2.0 * fg * bg) / max(fg + bg, vec3(1e-5)); } - case 21: { // Hue - return set_lum(set_sat(fg, sat_val(bg)), blend_lum(bg)); + case 21: { // Hue (Fusion HSL) + let h_fg = merge_rgb_to_hsl(fg).x; + let hsl_bg = merge_rgb_to_hsl(bg); + return merge_hsl_to_rgb(vec3(h_fg, hsl_bg.y, hsl_bg.z)); } - case 22: { // Saturation - return set_lum(set_sat(bg, sat_val(fg)), blend_lum(bg)); + case 22: { // Saturation (Fusion HSL; BG hue evaluated in BGR channel order to match Fusion 21.1) + let h_bg = merge_rgb_to_hsl(bg.bgr).x; + let s_fg = merge_rgb_to_hsl(fg).y; + let l_bg = merge_rgb_to_hsl(bg).z; + return merge_hsl_to_rgb(vec3(h_bg, s_fg, l_bg)); } - case 23: { // Color - return set_lum(fg, blend_lum(bg)); + case 23: { // Color (Fusion Rec.601 luma transfer, no gamut clipping) + return fg + vec3(blend_lum(bg) - blend_lum(fg)); } - case 24: { // Luminosity - return set_lum(bg, blend_lum(fg)); + case 24: { // Luminosity (Fusion Rec.601 luma transfer, no gamut clipping) + return bg + vec3(blend_lum(fg) - blend_lum(bg)); } default: { // 0: Normal return fg; @@ -355,23 +366,41 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3) { let comp_rgb = clamp(1.0 - (1.0 - bg.rgb) / max(1.0 - fg_ap + fg_p, vec3(1e-5)), vec3(0.0), vec3(1.0)); let comp_a = bg.a + fg_ap * (1.0 - bg.a); out_col = mix(bg, vec4(comp_rgb, comp_a), eff_blend); + } else if (mode == 14) { + // Fusion Hypotenuse operates directly on premultiplied RGB: sqrt(bg^2 + (fg * gain)^2) + let fg_p = fg.rgb * gain; + let fg_ap = fg.a * gain; + let comp_rgb = sqrt(max(bg.rgb * bg.rgb + fg_p * fg_p, vec3(0.0))); + let comp_a = bg.a + fg_ap * (1.0 - bg.a); + out_col = mix(bg, vec4(comp_rgb, comp_a), eff_blend); + } else if (mode == 20) { + // Fusion Geometric operates directly on premultiplied RGB: 2*bg*fg / (bg + fg) + let fg_p = fg.rgb * gain; + let fg_ap = fg.a * gain; + let denom = bg.rgb + fg_p; + let safe_d = max(denom, vec3(1e-5)); + let comp_rgb = select(vec3(0.0), (2.0 * bg.rgb * fg_p) / safe_d, denom > vec3(1e-5)); + let comp_a = bg.a + fg_ap * (1.0 - bg.a); + out_col = mix(bg, vec4(comp_rgb, comp_a), eff_blend); } else if (mode == 15 || mode == 16) { - // Fusion DarkerColor/LighterColor with AlphaGain: candidate is FG + BG*(1 - fg.a*gain), - // compared against BG by Rec.601 luma! (Verified bit-exact on probe_merge f09 & f10!) + // Fusion DarkerColor/LighterColor compare Over candidate against BG by Rec.601 luma let fg_a_eff = fg.a * gain; - let bg_u = select(vec3(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5); - let fg_u = select(vec3(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5); - let cand = fg_u + bg_u * (1.0 - fg_a_eff); - let blended = apply_blend_mode(bg_u, cand, mode); - let comp_rgb = bg.a * fg.a * blended + fg.rgb * (1.0 - bg.a) + (1.0 - fg.a) * bg.rgb; + let over_rgb = fg.rgb + bg.rgb * (1.0 - fg_a_eff); + let bg_branch = bg.rgb + fg.rgb * (1.0 - bg.a); + let pick_over = select( + blend_lum(over_rgb) > blend_lum(bg.rgb), + blend_lum(over_rgb) < blend_lum(bg.rgb), + mode == 15, + ); + let comp_rgb = select(bg_branch, over_rgb, pick_over); let comp_a = bg.a + fg_a_eff * (1.0 - bg.a); out_col = mix(bg, vec4(comp_rgb, comp_a), eff_blend); } else { let bg_u = select(vec3(0.0), bg.rgb / max(bg.a, 1e-5), bg.a > 1e-5); let fg_u = select(vec3(0.0), fg.rgb / max(fg.a, 1e-5), fg.a > 1e-5); - // In Fusion's special fill-opacity modes (Exclusion, Screen, LinearDodge), + // In Fusion's special fill-opacity modes (Screen, Exclusion), // Gain scales FG color intensity BEFORE blending. - let scale_fg_rgb = (mode == 6 || mode == 1 || mode == 12); + let scale_fg_rgb = (mode == 6 || mode == 1); let fg_blend_in = select(fg_u, fg_u * gain, scale_fg_rgb); let blended = apply_blend_mode(bg_u, fg_blend_in, mode); let fg_eff = select(fg.a * gain, fg.a, scale_fg_rgb); -- 2.54.1