From b3d875495c4f804d43885dd8faae35c7175eecc5 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Tue, 1 Dec 2026 00:08:34 -0800 Subject: [PATCH] Fix ColorCurves HDR, WandMask falloff, ChannelBoolean ops, and archive color/composite parity --- crates/engine/src/eval/color.rs | 115 ++++++++++++++++++- crates/engine/src/eval/composite.rs | 134 ++++++++++++++++++++++- crates/engine/src/eval/masks.rs | 88 +++++++++++---- crates/engine/src/shaders/color.wgsl | 30 +++-- crates/engine/src/shaders/composite.wgsl | 36 ++++-- 5 files changed, 353 insertions(+), 50 deletions(-) diff --git a/crates/engine/src/eval/color.rs b/crates/engine/src/eval/color.rs index 81b5ebae812a234461c774a8bdebec4b008ade8a..9e1590d85d92be1daad075ce221cd8d764eabdb9 100644 --- a/crates/engine/src/eval/color.rs +++ b/crates/engine/src/eval/color.rs @@ -593,6 +593,31 @@ impl Engine { lut_tex } + /// Returns `(y0, y1, slope0, slope1)` for RGBA `ColorCurves` linear tangent extrapolation + /// outside `[0, 1]`, matching Fusion 21.1's `ColorCurves` HDR behavior. + fn color_curves_extrap_params(comp: &Comp, node: &Node) -> ([f32; 4], [f32; 4], [f32; 4], [f32; 4]) { + const EPS: f64 = 1e-4; + let mut y0 = [0.0_f32; 4]; + let mut y1 = [1.0_f32; 4]; + let mut s0 = [1.0_f32; 4]; + let mut s1 = [1.0_f32; 4]; + for (i, port) in ["Red", "Green", "Blue", "Alpha"].iter().enumerate() { + if let Some(sp) = node + .connected_node(port) + .and_then(|s| comp.splines.get(s)) + .filter(|s| !s.keyframes.is_empty()) + { + let v0 = sp.eval(0.0); + let v1 = sp.eval(1.0); + y0[i] = v0 as f32; + y1[i] = v1 as f32; + s0[i] = ((sp.eval(EPS) - v0) / EPS) as f32; + s1[i] = ((v1 - sp.eval(1.0 - EPS)) / EPS) as f32; + } + } + (y0, y1, s0, s1) + } + pub(super) fn eval_color_curves( &mut self, comp: &Comp, @@ -619,17 +644,22 @@ impl Engine { let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); let (lut_f16, lut_hash, apply_a) = Self::build_color_curves_lut_f16(comp, node); + let (y0, y1, s0, s1) = Self::color_curves_extrap_params(comp, node); let mut u = UniformParams::default(); u.p[0] = [prediv, ignore_trans, blend, if mask.is_some() { 1.0 } else { 0.0 }]; u.p[1] = [if mask_inv { 1.0 } else { 0.0 }, if apply_a { 1.0 } else { 0.0 }, if mbm { 1.0 } else { 0.0 }, 0.0]; + u.p[2] = y0; + u.p[3] = y1; + u.p[4] = s0; + u.p[5] = s1; let key = self.make_key( node, "ColorCurves", src.size, &[src.content_hash, mask_hash, lut_hash], - bytemuck::cast_slice(&u.p[..2]), + bytemuck::cast_slice(&u.p[..6]), ); if let Some(hit) = self.cache.get(key, node.id, frame_i) { stats.cache_hits += 1; @@ -1368,5 +1398,88 @@ mod tests { let (m_tex, _) = eng.evaluate(&comp, comp.find_node("M").unwrap(), 0.0); assert!((m_tex.tex_scale - 1.0).abs() < 1e-4, "Merge over 128x128 BG should stay 1.0"); } + + #[test] + fn color_curves_hdr_tangent_extrapolation_and_prediv() { + // Probe HDR > 1.0 and < 0.0 on Float32: + // 1) Identity preserves -0.5 and 2.5 + // 2) Linear LUT (0, 0.2) -> (1, 0.8) extrapolates to 0.2 + 0.6*(-0.5) = -0.1 and 0.2 + 0.6*2.5 = 1.7 + // 3) PreDividePostMultiply=1 unpremultiplies rgb/alpha > 1.0 without clamping + let comp_src = r#"{ + Tools = ordered() { + LutLin = LUTBezier { + KeyColorSplines = { + [0] = { + [0] = { 0.2, Flags = { Linear = true } }, + [1] = { 0.8, Flags = { Linear = true } }, + }, + }, + }, + HdrNeg = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 16, }, + Depth = Input { Value = 4, }, + TopLeftRed = Input { Value = -0.5, }, + TopLeftGreen = Input { Value = 2.5, }, + TopLeftBlue = Input { Value = 1.0, }, + TopLeftAlpha = Input { Value = 0.5, }, + }, + }, + CcId = ColorCurves { + Inputs = { + Input = Input { SourceOp = "HdrNeg", Source = "Output", }, + }, + }, + EncodeId = BrightnessContrast { + Inputs = { + Gain = Input { Value = 0.2, }, + Brightness = Input { Value = 0.2, }, + Input = Input { SourceOp = "CcId", Source = "Output", }, + }, + }, + CcLin = ColorCurves { + Inputs = { + Red = Input { SourceOp = "LutLin", Source = "Value", }, + Green = Input { SourceOp = "LutLin", Source = "Value", }, + Input = Input { SourceOp = "HdrNeg", Source = "Output", }, + }, + }, + EncodeLin = BrightnessContrast { + Inputs = { + Gain = Input { Value = 0.2, }, + Brightness = Input { Value = 0.2, }, + Input = Input { SourceOp = "CcLin", Source = "Output", }, + }, + }, + CcPreDiv = ColorCurves { + Inputs = { + PreDividePostMultiply = Input { Value = 1, }, + Green = Input { SourceOp = "LutLin", Source = "Value", }, + Input = Input { SourceOp = "HdrNeg", Source = "Output", }, + }, + }, + EncodePreDiv = BrightnessContrast { + Inputs = { + Gain = Input { Value = 0.2, }, + Brightness = Input { Value = 0.2, }, + Input = Input { SourceOp = "CcPreDiv", Source = "Output", }, + }, + }, + } + }"#; + // Encoded as v * 0.2 + 0.2: + // Id: R=-0.5 -> 0.1 (26), G=2.5 -> 0.7 (179) + let pid = eval_node_px(comp_src, "EncodeId"); + assert!((pid[0] as i32 - 26).abs() <= 2, "Id HDR neg R={pid:?}"); + assert!((pid[1] as i32 - 179).abs() <= 2, "Id HDR >1 G={pid:?}"); + // Lin: R=-0.1 -> 0.18 (46), G=1.7 -> 0.54 (138) + let plin = eval_node_px(comp_src, "EncodeLin"); + assert!((plin[0] as i32 - 46).abs() <= 2, "Lin HDR neg R={plin:?}"); + assert!((plin[1] as i32 - 138).abs() <= 2, "Lin HDR >1 G={plin:?}"); + // PreDiv: G_straight = 2.5 / 0.5 = 5.0 -> 0.2 + 0.6*5.0 = 3.2 -> * 0.5 = 1.6 -> enc 0.52 (133) + let ppd = eval_node_px(comp_src, "EncodePreDiv"); + assert!((ppd[1] as i32 - 133).abs() <= 2, "PreDiv HDR >1 G={ppd:?}"); + } } diff --git a/crates/engine/src/eval/composite.rs b/crates/engine/src/eval/composite.rs index 688cc4c8f0231ad4e11c1fdfd5de577880d11fb6..9130784469063ff7eba6dd826f21e4b595905c0a 100644 --- a/crates/engine/src/eval/composite.rs +++ b/crates/engine/src/eval/composite.rs @@ -1,6 +1,26 @@ use super::*; impl Engine { + fn flattened_fg_edge_mode( + comp: &Comp, + fg_id: NodeId, + time: f64, + expr: &ExprEngine, + fg_tex: &CachedTexture, + ) -> Option { + let n = comp.node(fg_id); + if n.kind == "Transform" { + if comp.eval_number(n, "FlattenTransform", time, 0.0, expr) <= 0.5 { + return None; + } + let e = comp.eval_number(n, "Edges", time, 0.0, expr).round() as i32; + if e == 1 || e == 3 { + return Some(e as f32); + } + } + fg_tex.edge_mode.map(f32::from) + } + pub(super) fn eval_merge( &mut self, comp: &Comp, @@ -24,7 +44,8 @@ impl Engine { return bg; } - let fg = match node.connected_node("Foreground").and_then(|s| comp.find_node(s)) { + let fg_node = node.connected_node("Foreground").and_then(|s| comp.find_node(s)); + let fg = match fg_node { Some(fid) => self.eval_node(comp, fid, time, enc, visiting, stats), None => return bg, }; @@ -35,7 +56,14 @@ impl Engine { let gain = (comp.eval_number(node, "Gain", time, 1.0, &self.expr) as f32).clamp(0.0, 1.0); let burn_in = comp.eval_number(node, "BurnIn", time, 0.0, &self.expr) as f32; let sub_add = comp.eval_number(node, "SubtractiveAdditive", time, 1.0, &self.expr) as f32; - let edges = comp.eval_number(node, "Edges", time, 0.0, &self.expr) as f32; + let raw_edges = comp.eval_number(node, "Edges", time, 0.0, &self.expr) as f32; + let edges = if raw_edges < 0.5 { + fg_node + .and_then(|fid| Self::flattened_fg_edge_mode(comp, fid, time, &self.expr, &fg)) + .unwrap_or(raw_edges) + } else { + raw_edges + }; let flip_h = comp.eval_number(node, "FlipHoriz", time, 0.0, &self.expr) as f32; let flip_v = comp.eval_number(node, "FlipVert", time, 0.0, &self.expr) as f32; @@ -241,8 +269,10 @@ impl Engine { if blend <= 1e-6 { return bg; } - // Fast path: Copy BG R/G/B/A onto itself (e.g. ZBuffer-only ChannelBoolean) - if op == 0 && to_r == 5 && to_g == 6 && to_b == 7 && to_a == 8 { + // Fast path: Copy BG R/G/B/A onto itself (e.g. ZBuffer-only ChannelBoolean) or all Do Nothing + if (op == 0 && to_r == 5 && to_g == 6 && to_b == 7 && to_a == 8) + || (op == 0 && to_r == 4 && to_g == 4 && to_b == 4 && to_a == 4) + { return bg; } // When Foreground is unconnected, Fusion uses Background as Foreground @@ -253,6 +283,11 @@ impl Engine { }; let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(bg.canvas_size), enc, visiting, stats); let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); + let expand_canvas = bg.has_dod() + && (op == 10 || op == 11 || [to_r, to_g, to_b, to_a].contains(&16)); + let dst_size = if expand_canvas { bg.canvas_size } else { bg.size }; + let dst_origin = if expand_canvas { [0, 0] } else { bg.origin }; + let mut u = UniformParams::default(); u.p[0] = [op as f32, to_r as f32, to_g as f32, to_b as f32]; u.p[1] = [ @@ -267,14 +302,19 @@ impl Engine { if bg.has_dod() { u.p[3] = [bg.origin[0] as f32, bg.origin[1] as f32, bg.canvas_size[0] as f32, bg.canvas_size[1] as f32]; } - let key = self.make_key(node, "ChannelBoolean", bg.size, &[bg.content_hash, fg.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..4])); + u.p[4] = [if expand_canvas { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0]; + let key = self.make_key(node, "ChannelBoolean", dst_size, &[bg.content_hash, fg.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5])); if let Some(hit) = self.cache.get(key, node.id, frame_i) { stats.cache_hits += 1; return hit; } stats.cache_misses += 1; stats.nodes_evaluated += 1; - let dst = self.alloc_tex_like(&bg, bg.depth.max(fg.depth), key); + let mut dst = self.alloc_tex_dod(dst_size, dst_origin, bg.canvas_size, bg.depth.max(fg.depth), key); + dst.tex_scale = bg.tex_scale; + if op == 11 && mask.is_none() && ![to_r, to_g, to_b, to_a].contains(&4) { + dst.edge_mode = Some(2); + } let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); self.dispatch_op(enc, ShaderOp::ChannelBoolean, &bg.view, &fg.view, mv, &dst, &u); self.cache.insert(key, dst.clone(), node.id, frame_i); @@ -927,5 +967,87 @@ mod tests { assert!((m50[1] as i32 - 128).abs() <= 4 && (m50[2] as i32 - 128).abs() <= 4, "MultiStop 0.5={m50:?}"); assert!(m75[2] >= 250 && m75[0] <= 5, "MultiStop 0.75={m75:?}"); } + + #[test] + fn channel_boolean_and_or_xor_signed_add_and_solid_border() { + let comp = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 32, }, + Height = Input { Value = 32, }, + TopLeftRed = Input { Value = 0.7, }, + TopLeftGreen = Input { Value = 0.5, }, + TopLeftBlue = Input { Value = 0.3, }, + TopLeftAlpha = Input { Value = 0.8, }, + }, + }, + Fg = Background { + Inputs = { + Width = Input { Value = 32, }, + Height = Input { Value = 32, }, + TopLeftRed = Input { Value = 0.2, }, + TopLeftGreen = Input { Value = 0.4, }, + TopLeftBlue = Input { Value = 0.8, }, + TopLeftAlpha = Input { Value = 0.5, }, + }, + }, + CbAnd = ChannelBoolean { Inputs = { Operation = Input { Value = 3, }, Background = Input { SourceOp = "Bg", Source = "Output", }, Foreground = Input { SourceOp = "Fg", Source = "Output", }, }, }, + CbOr = ChannelBoolean { Inputs = { Operation = Input { Value = 4, }, Background = Input { SourceOp = "Bg", Source = "Output", }, Foreground = Input { SourceOp = "Fg", Source = "Output", }, }, }, + CbXor = ChannelBoolean { Inputs = { Operation = Input { Value = 5, }, Background = Input { SourceOp = "Bg", Source = "Output", }, Foreground = Input { SourceOp = "Fg", Source = "Output", }, }, }, + CbSAdd = ChannelBoolean { Inputs = { Operation = Input { Value = 14, }, Background = Input { SourceOp = "Bg", Source = "Output", }, Foreground = Input { SourceOp = "Fg", Source = "Output", }, }, }, + CbSolid = ChannelBoolean { Inputs = { Operation = Input { Value = 11, }, Background = Input { SourceOp = "Bg", Source = "Output", }, }, }, + SolidZoom = Transform { Inputs = { Size = Input { Value = 0.5, }, Input = Input { SourceOp = "CbSolid", Source = "Output", }, }, }, + } + }"#; + let (p3, w, _) = eval_node_px(comp, "CbAnd"); + assert!((px_at(&p3, w, 16, 16)[0] as i32 - 36).abs() <= 2, "And={:?}", px_at(&p3, w, 16, 16)); + let (p4, _, _) = eval_node_px(comp, "CbOr"); + assert!((px_at(&p4, w, 16, 16)[0] as i32 - 194).abs() <= 2, "Or={:?}", px_at(&p4, w, 16, 16)); + let (p5, _, _) = eval_node_px(comp, "CbXor"); + assert!((px_at(&p5, w, 16, 16)[0] as i32 - 158).abs() <= 2, "Xor={:?}", px_at(&p5, w, 16, 16)); + let (p14, _, _) = eval_node_px(comp, "CbSAdd"); + assert!((px_at(&p14, w, 16, 16)[2] as i32 - 153).abs() <= 2, "SignedAdd={:?}", px_at(&p14, w, 16, 16)); + // Solid (op=11) infinite white border stays (255,255,255,255) outside zoomed region. + let (psz, _, _) = eval_node_px(comp, "SolidZoom"); + assert_eq!(px_at(&psz, w, 2, 2), [255, 255, 255, 255]); + } + + #[test] + fn merge_inherits_flattened_transform_wrap_edge_mode() { + let comp = r#"{ + Tools = ordered() { + Black = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftAlpha = Input { Value = 1, }, }, }, + Dot = Background { + Inputs = { + Width = Input { Value = 64, }, + Height = Input { Value = 64, }, + TopLeftRed = Input { Value = 1, }, + TopLeftAlpha = Input { Value = 1, }, + EffectMask = Input { SourceOp = "R", Source = "Mask", }, + }, + }, + R = RectangleMask { Inputs = { MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, }, Width = Input { Value = 0.5, }, Height = Input { Value = 0.5, }, }, }, + XfFlat = Transform { + Inputs = { + Size = Input { Value = 0.25, }, + Edges = Input { Value = 1, }, + FlattenTransform = Input { Value = 1, }, + Input = Input { SourceOp = "Dot", Source = "Output", }, + }, + }, + M = Merge { + Inputs = { + Background = Input { SourceOp = "Black", Source = "Output", }, + Foreground = Input { SourceOp = "XfFlat", Source = "Output", }, + Center = Input { Value = { 0.2, 0.5 }, }, + }, + }, + } + }"#; + let (px, w, _) = eval_node_px(comp, "M"); + // Right side (x=60, y=32) wraps into tiled red instead of clipping to black. + assert!(px_at(&px, w, 60, 32)[0] > 200, "expected wrapped red at x=60, got {:?}", px_at(&px, w, 60, 32)); + } } diff --git a/crates/engine/src/eval/masks.rs b/crates/engine/src/eval/masks.rs index b08e2d32bb47bc95d1a1f900a3185807f22d43d2..771a01dab1ed93f9a34aa505841e0e8dcd110e68 100644 --- a/crates/engine/src/eval/masks.rs +++ b/crates/engine/src/eval/masks.rs @@ -440,7 +440,7 @@ impl Engine { let range_default = comp.eval_number(node, "SelectionRange", time, 0.1, &self.expr); let range = comp.eval_number(node, "Range", time, range_default, &self.expr).clamp(0.0, 1.0) as f32; let range_soft = comp.eval_number(node, "RangeSoftEdge", time, 0.0, &self.expr).clamp(0.0, 1.0) as f32; - let color_space = comp.eval_number(node, "ColorSpace", time, 0.0, &self.expr).round() as i32; + let channel = comp.eval_number(node, "Channel", time, 0.0, &self.expr).round() as i32; let soft = comp.eval_number(node, "SoftEdge", time, 0.0, &self.expr).max(0.0) as f32; let invert = comp.eval_number(node, "Invert", time, 0.0, &self.expr) as f32; let level = comp.eval_number(node, "Level", time, 1.0, &self.expr) as f32; @@ -460,7 +460,7 @@ impl Engine { seed_pt[1] as f32, range, range_soft, - color_space as f32, + channel as f32, soft, invert, level, @@ -491,7 +491,7 @@ impl Engine { Ok(src_rgba) if src_rgba.len() >= w * h * 4 => { let sx = (seed_pt[0].clamp(0.0, 1.0) * (w - 1) as f64).round() as usize; let sy = ((1.0 - seed_pt[1].clamp(0.0, 1.0)) * (h - 1) as f64).round() as usize; - wand_flood_fill(&src_rgba, w, h, [sx, sy], range, range_soft, color_space == 1) + wand_flood_fill(&src_rgba, w, h, [sx, sy], range, range_soft, channel) } _ => vec![0; w * h * 4], }; @@ -536,34 +536,60 @@ impl Engine { } -/// Flood fill from `seed` over RGBA8 `src`: pixels within `range` of the seed color are selected at -/// 1.0, falling off linearly to 0.0 at `range + range_soft`; only the connected region is selected. -fn wand_flood_fill(src: &[u8], w: usize, h: usize, seed: [usize; 2], range: f32, range_soft: f32, yuv: bool) -> Vec { - let rgb = |i: usize| [src[i * 4] as f32 / 255.0, src[i * 4 + 1] as f32 / 255.0, src[i * 4 + 2] as f32 / 255.0]; - let [sr, sg, sb] = rgb(seed[1] * w + seed[0]); - let sl = 0.299 * sr + 0.587 * sg + 0.114 * sb; +/// Flood fill from `seed` over RGBA8 `src`: Fusion 21.1 `WandMask` halves `Range` and +/// `RangeSoftEdge` (`r = 0.5 * range`, `s = 0.5 * range_soft`), selects `Channel` +/// (`0=Color` Euclidean RGB, `1=Red`, `2=Green`, `3=Blue`, `4=Alpha`), and falls off linearly +/// from `1.0` at `d <= r` to `0.0` at `r + s`. +fn wand_flood_fill(src: &[u8], w: usize, h: usize, seed: [usize; 2], range: f32, range_soft: f32, channel: i32) -> Vec { + let rgba = |i: usize| [ + src[i * 4] as f32 / 255.0, + src[i * 4 + 1] as f32 / 255.0, + src[i * 4 + 2] as f32 / 255.0, + src[i * 4 + 3] as f32 / 255.0, + ]; + let start = seed[1] * w + seed[0]; + let [sr, sg, sb, sa] = rgba(start); let dist = |i: usize| { - let [r, g, b] = rgb(i); - if yuv { - let l = 0.299 * r + 0.587 * g + 0.114 * b; - (l - sl).abs().max(((b - l) - (sb - sl)).abs()).max(((r - l) - (sr - sl)).abs()) - } else { - (r - sr).abs().max((g - sg).abs()).max((b - sb).abs()) + let [r, g, b, a] = rgba(i); + match channel { + 0 => { + let (dr, dg, db) = (r - sr, g - sg, b - sb); + (dr * dr + dg * dg + db * db).sqrt() + } + 1 => (r - sr).abs(), + 2 => (g - sg).abs(), + 3 => (b - sb).abs(), + 4 => (a - sa).abs(), + _ => { + if src[i * 4..i * 4 + 4] == src[start * 4..start * 4 + 4] { + 0.0 + } else { + f32::INFINITY + } + } } }; + let r_half = range * 0.5; + let soft_half = range_soft * 0.5; + let max_d = r_half + soft_half; let mut out = vec![0u8; w * h * 4]; let mut visited = vec![false; w * h]; - let start = seed[1] * w + seed[0]; visited[start] = true; let mut queue = std::collections::VecDeque::from([start]); while let Some(i) = queue.pop_front() { let d = dist(i); - let a = if d <= range { 1.0 } else { 1.0 - (d - range) / range_soft }; + let a = if d <= r_half { + 1.0 + } else if soft_half <= 1e-6 { + 0.0 + } else { + (1.0 - (d - r_half) / soft_half).clamp(0.0, 1.0) + }; out[i * 4..i * 4 + 4].fill((a * 255.0).round() as u8); let (x, y) = (i % w, i / w); let nbrs = [(x > 0).then(|| i - 1), (x + 1 < w).then(|| i + 1), (y > 0).then(|| i - w), (y + 1 < h).then(|| i + w)]; for n in nbrs.into_iter().flatten() { - if !visited[n] && dist(n) <= range + range_soft { + if !visited[n] && dist(n) <= max_d { visited[n] = true; queue.push_back(n); } @@ -639,21 +665,35 @@ mod tests { #[test] fn wand_range_soft_edge_falls_off_linearly() { - // Distances from the seed: 0, 0.1 (range), 0.2, 0.3 (range + soft), 0.4. + // Channel 1 (Red) compares |r - sr| against half-scale range (0.1) and soft (0.2). + // Distances from seed 0: 0, 25/255 (~0.098 <= 0.1), 51/255 (0.2), 76/255 (~0.298), 102/255 (0.4). let src = gray_row(&[0, 25, 51, 76, 102]); - let hard = wand_flood_fill(&src, 5, 1, [0, 0], 0.1, 0.0, false); + let hard = wand_flood_fill(&src, 5, 1, [0, 0], 0.2, 0.0, 1); assert_eq!(alphas(&hard), [255, 255, 0, 0, 0]); - let soft = wand_flood_fill(&src, 5, 1, [0, 0], 0.1, 0.2, false); + let soft = wand_flood_fill(&src, 5, 1, [0, 0], 0.2, 0.4, 1); let a = alphas(&soft); assert_eq!((a[0], a[1], a[2], a[4]), (255, 255, 128, 0)); - assert!(a[3] <= 3, "falloff reaches 0 at range + soft, got {a:?}"); + assert!(a[3] <= 3, "falloff reaches 0 at 0.5*(range + soft), got {a:?}"); + } + + #[test] + fn wand_channel_modes_match_fusion_euclidean_and_per_channel() { + // Seed=(0, 128, 255, 255); px1=(51, 128, 255, 255) -> dr=0.2; px2=(0, 179, 255, 255) -> dg=0.2. + let src = vec![0, 128, 255, 255, 51, 128, 255, 255, 0, 179, 255, 255]; + assert_eq!(alphas(&wand_flood_fill(&src, 3, 1, [0, 0], 0.2, 0.4, 1)), [255, 128, 255]); + assert_eq!(alphas(&wand_flood_fill(&src, 3, 1, [0, 0], 0.2, 0.4, 2)), [255, 255, 128]); + // Color mode (Channel=0): Euclidean RGB distance for gray step v=26 (sqrt(3)*0.102=0.177). + // With r_half=0.1, soft_half=0.2: 1 - (0.177 - 0.1) / 0.2 = 0.617 (~157). + let gray = gray_row(&[0, 26]); + let a = alphas(&wand_flood_fill(&gray, 2, 1, [0, 0], 0.2, 0.4, 0)); + assert!((a[1] as i32 - 157).abs() <= 2, "Euclidean RGB gray falloff got {a:?}"); } #[test] fn wand_soft_edge_stays_connected() { - // The second 0-distance pixel is cut off from the seed by a pixel beyond range + soft. + // The second 0-distance pixel is cut off from the seed by a pixel beyond 0.5*(range + soft). let src = gray_row(&[0, 255, 0]); - assert_eq!(alphas(&wand_flood_fill(&src, 3, 1, [0, 0], 0.1, 0.2, false)), [255, 0, 0]); + assert_eq!(alphas(&wand_flood_fill(&src, 3, 1, [0, 0], 0.2, 0.4, 1)), [255, 0, 0]); } #[test] diff --git a/crates/engine/src/shaders/color.wgsl b/crates/engine/src/shaders/color.wgsl index 8a539629a4554c73bf4baa7fea406a241ebf6b37..a5d525569a548806a2edabade1cc5603ebee20c4 100644 --- a/crates/engine/src/shaders/color.wgsl +++ b/crates/engine/src/shaders/color.wgsl @@ -439,8 +439,22 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3) { // ============================================================================ // 20. ColorCurves (per-channel 256x1 LUT texture in src_b) // p[0] = (prediv, ignore_transparent, blend, has_mask) -// p[1] = (mask_invert, apply_alpha, 0, 0) +// p[1] = (mask_invert, apply_alpha, multiply_by_mask, 0) +// p[2] = y0 (rgba at x=0), p[3] = y1 (rgba at x=1) +// p[4] = slope0 (rgba tangent at x=0), p[5] = slope1 (rgba tangent at x=1) // ============================================================================ +fn color_curves_eval_channel(v: f32, ch: i32, lut_n: f32) -> f32 { + if (v < 0.0) { + return u.p[2][ch] + v * u.p[4][ch]; + } + if (v > 1.0) { + return u.p[3][ch] + (v - 1.0) * u.p[5][ch]; + } + let lut_u = (0.5 + v * (lut_n - 1.0)) / lut_n; + let s = textureSampleLevel(src_b, samp_lin, vec2(lut_u, 0.5), 0.0); + return s[ch]; +} + @compute @workgroup_size(8, 8) fn color_curves_op(@builtin(global_invocation_id) gid: vec3) { let dims = textureDimensions(dst_tex); @@ -458,20 +472,18 @@ fn color_curves_op(@builtin(global_invocation_id) gid: vec3) { } var rgb = src.rgb; - let unpre = (prediv || ignore_trans) && src.a > 1e-5; + let unpre = prediv || ignore_trans; if (unpre) { - rgb = clamp(rgb / src.a, vec3(0.0), vec3(1.0)); + rgb = select(vec3(0.0), rgb / src.a, src.a > 1e-5); } let lut_n = f32(textureDimensions(src_b).x); - let lut_u = (vec3(0.5) + clamp(rgb, vec3(0.0), vec3(1.0)) * (lut_n - 1.0)) / lut_n; - let r = textureSampleLevel(src_b, samp_lin, vec2(lut_u.r, 0.5), 0.0).r; - let g = textureSampleLevel(src_b, samp_lin, vec2(lut_u.g, 0.5), 0.0).g; - let b = textureSampleLevel(src_b, samp_lin, vec2(lut_u.b, 0.5), 0.0).b; + let r = color_curves_eval_channel(rgb.r, 0, lut_n); + let g = color_curves_eval_channel(rgb.g, 1, lut_n); + let b = color_curves_eval_channel(rgb.b, 2, lut_n); var a = src.a; if (apply_a) { - let a_u = (0.5 + clamp(src.a, 0.0, 1.0) * (lut_n - 1.0)) / lut_n; - a = textureSampleLevel(src_b, samp_lin, vec2(a_u, 0.5), 0.0).a; + a = color_curves_eval_channel(src.a, 3, lut_n); } var out_rgb = vec3(r, g, b); diff --git a/crates/engine/src/shaders/composite.wgsl b/crates/engine/src/shaders/composite.wgsl index a50bc0b7ce126dfc1b0975a338705f57e31b6d28..eff9ab9827c83089938e5b6341ad7d7341c5c80d 100644 --- a/crates/engine/src/shaders/composite.wgsl +++ b/crates/engine/src/shaders/composite.wgsl @@ -566,6 +566,9 @@ fn cb_apply_op(op: i32, bg_v: f32, fg_v: f32) -> f32 { case 0: { return fg_v; } // Copy case 1: { return bg_v + fg_v; } // Add case 2: { return bg_v - fg_v; } // Subtract + case 3: { return bg_v * fg_v; } // And + case 4: { return bg_v + fg_v - bg_v * fg_v; } // Or + case 5: { return bg_v + fg_v - 2.0 * bg_v * fg_v; } // Exclusive Or case 6: { return bg_v * fg_v; } // Multiply case 7: { return select(bg_v / fg_v, 0.0, abs(fg_v) <= 1e-5); } // Divide case 8: { return max(bg_v, fg_v); } // Maximum @@ -574,7 +577,8 @@ fn cb_apply_op(op: i32, bg_v: f32, fg_v: f32) -> f32 { case 11: { return 1.0; } // Solid case 12: { return 0.0; } // Clear case 13: { return abs(bg_v - fg_v); } // Difference - default: { return fg_v; } + case 14: { return bg_v + fg_v - 0.5; } // Signed Add + default: { return select(fg_v, bg_v + fg_v - 0.5, op >= 14); } } } @@ -582,16 +586,28 @@ fn cb_apply_op(op: i32, bg_v: f32, fg_v: f32) -> f32 { fn channel_boolean_op(@builtin(global_invocation_id) gid: vec3) { let dims = textureDimensions(dst_tex); if (gid.x >= dims.x || gid.y >= dims.y) { return; } - let bg = textureLoad(src_a, vec2(gid.xy), 0); let bg_dod = u.p[3]; - let has_bg_dod = bg_dod.z > 0.5 && bg_dod.w > 0.5; - let bg_dims = select(vec2(dims), bg_dod.zw, has_bg_dod); - let canvas_px = select( - vec2(gid.xy) + vec2(0.5), - vec2(gid.xy) + vec2(0.5) + bg_dod.xy, - has_bg_dod, - ); - let fu = vec2(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y); + let expand_canvas = u.p[4].x > 0.5; + var bg: vec4; + var bg_dims: vec2; + var canvas_px: vec2; + var fu: vec2; + if (expand_canvas) { + bg_dims = vec2(dims); + canvas_px = vec2(gid.xy) + vec2(0.5); + fu = vec2(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y); + bg = sample_dod_edges_mode(src_a, fu, 0, bg_dod, false); + } else { + bg = textureLoad(src_a, vec2(gid.xy), 0); + let has_bg_dod = bg_dod.z > 0.5 && bg_dod.w > 0.5; + bg_dims = select(vec2(dims), bg_dod.zw, has_bg_dod); + canvas_px = select( + vec2(gid.xy) + vec2(0.5), + vec2(gid.xy) + vec2(0.5) + bg_dod.xy, + has_bg_dod, + ); + fu = vec2(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y); + } let fg = sample_dod_edges_mode(src_b, fu, 0, u.p[2], false); let op = i32(u.p[0].x + 0.5); -- 2.54.1