authoravatarclover caruso<git@paperclover.net>2026-12-01 00:08:34-08:00
committeravatarclover caruso<git@paperclover.net>2026-12-01 01:05:25-08:00
logb3d875495c4f804d43885dd8faae35c7175eecc5
tree4b3a0f37114cbd8c48ec4e03f180a8e8fabab4dc
parentd0c875aa773e5d28656a545f701099d66106098c
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Fix ColorCurves HDR, WandMask falloff, ChannelBoolean ops, and archive color/composite parity


5 files changed, 353 insertions(+), 50 deletions(-)

crates/engine/src/eval/color.rs+114-1
......@@ -593,6 +593,31 @@ impl Engine {
593593 lut_tex
594594 }
595595
596 /// Returns `(y0, y1, slope0, slope1)` for RGBA `ColorCurves` linear tangent extrapolation
597 /// outside `[0, 1]`, matching Fusion 21.1's `ColorCurves` HDR behavior.
598 fn color_curves_extrap_params(comp: &Comp, node: &Node) -> ([f32; 4], [f32; 4], [f32; 4], [f32; 4]) {
599 const EPS: f64 = 1e-4;
600 let mut y0 = [0.0_f32; 4];
601 let mut y1 = [1.0_f32; 4];
602 let mut s0 = [1.0_f32; 4];
603 let mut s1 = [1.0_f32; 4];
604 for (i, port) in ["Red", "Green", "Blue", "Alpha"].iter().enumerate() {
605 if let Some(sp) = node
606 .connected_node(port)
607 .and_then(|s| comp.splines.get(s))
608 .filter(|s| !s.keyframes.is_empty())
609 {
610 let v0 = sp.eval(0.0);
611 let v1 = sp.eval(1.0);
612 y0[i] = v0 as f32;
613 y1[i] = v1 as f32;
614 s0[i] = ((sp.eval(EPS) - v0) / EPS) as f32;
615 s1[i] = ((v1 - sp.eval(1.0 - EPS)) / EPS) as f32;
616 }
617 }
618 (y0, y1, s0, s1)
619 }
620
596621 pub(super) fn eval_color_curves(
597622 &mut self,
598623 comp: &Comp,
......@@ -619,17 +644,22 @@ impl Engine {
619644 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
620645
621646 let (lut_f16, lut_hash, apply_a) = Self::build_color_curves_lut_f16(comp, node);
647 let (y0, y1, s0, s1) = Self::color_curves_extrap_params(comp, node);
622648
623649 let mut u = UniformParams::default();
624650 u.p[0] = [prediv, ignore_trans, blend, if mask.is_some() { 1.0 } else { 0.0 }];
625651 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];
652 u.p[2] = y0;
653 u.p[3] = y1;
654 u.p[4] = s0;
655 u.p[5] = s1;
626656
627657 let key = self.make_key(
628658 node,
629659 "ColorCurves",
630660 src.size,
631661 &[src.content_hash, mask_hash, lut_hash],
632 bytemuck::cast_slice(&u.p[..2]),
662 bytemuck::cast_slice(&u.p[..6]),
633663 );
634664 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
635665 stats.cache_hits += 1;
......@@ -1368,5 +1398,88 @@ mod tests {
13681398 let (m_tex, _) = eng.evaluate(&comp, comp.find_node("M").unwrap(), 0.0);
13691399 assert!((m_tex.tex_scale - 1.0).abs() < 1e-4, "Merge over 128x128 BG should stay 1.0");
13701400 }
1401
1402 #[test]
1403 fn color_curves_hdr_tangent_extrapolation_and_prediv() {
1404 // Probe HDR > 1.0 and < 0.0 on Float32:
1405 // 1) Identity preserves -0.5 and 2.5
1406 // 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
1407 // 3) PreDividePostMultiply=1 unpremultiplies rgb/alpha > 1.0 without clamping
1408 let comp_src = r#"{
1409 Tools = ordered() {
1410 LutLin = LUTBezier {
1411 KeyColorSplines = {
1412 [0] = {
1413 [0] = { 0.2, Flags = { Linear = true } },
1414 [1] = { 0.8, Flags = { Linear = true } },
1415 },
1416 },
1417 },
1418 HdrNeg = Background {
1419 Inputs = {
1420 Width = Input { Value = 16, },
1421 Height = Input { Value = 16, },
1422 Depth = Input { Value = 4, },
1423 TopLeftRed = Input { Value = -0.5, },
1424 TopLeftGreen = Input { Value = 2.5, },
1425 TopLeftBlue = Input { Value = 1.0, },
1426 TopLeftAlpha = Input { Value = 0.5, },
1427 },
1428 },
1429 CcId = ColorCurves {
1430 Inputs = {
1431 Input = Input { SourceOp = "HdrNeg", Source = "Output", },
1432 },
1433 },
1434 EncodeId = BrightnessContrast {
1435 Inputs = {
1436 Gain = Input { Value = 0.2, },
1437 Brightness = Input { Value = 0.2, },
1438 Input = Input { SourceOp = "CcId", Source = "Output", },
1439 },
1440 },
1441 CcLin = ColorCurves {
1442 Inputs = {
1443 Red = Input { SourceOp = "LutLin", Source = "Value", },
1444 Green = Input { SourceOp = "LutLin", Source = "Value", },
1445 Input = Input { SourceOp = "HdrNeg", Source = "Output", },
1446 },
1447 },
1448 EncodeLin = BrightnessContrast {
1449 Inputs = {
1450 Gain = Input { Value = 0.2, },
1451 Brightness = Input { Value = 0.2, },
1452 Input = Input { SourceOp = "CcLin", Source = "Output", },
1453 },
1454 },
1455 CcPreDiv = ColorCurves {
1456 Inputs = {
1457 PreDividePostMultiply = Input { Value = 1, },
1458 Green = Input { SourceOp = "LutLin", Source = "Value", },
1459 Input = Input { SourceOp = "HdrNeg", Source = "Output", },
1460 },
1461 },
1462 EncodePreDiv = BrightnessContrast {
1463 Inputs = {
1464 Gain = Input { Value = 0.2, },
1465 Brightness = Input { Value = 0.2, },
1466 Input = Input { SourceOp = "CcPreDiv", Source = "Output", },
1467 },
1468 },
1469 }
1470 }"#;
1471 // Encoded as v * 0.2 + 0.2:
1472 // Id: R=-0.5 -> 0.1 (26), G=2.5 -> 0.7 (179)
1473 let pid = eval_node_px(comp_src, "EncodeId");
1474 assert!((pid[0] as i32 - 26).abs() <= 2, "Id HDR neg R={pid:?}");
1475 assert!((pid[1] as i32 - 179).abs() <= 2, "Id HDR >1 G={pid:?}");
1476 // Lin: R=-0.1 -> 0.18 (46), G=1.7 -> 0.54 (138)
1477 let plin = eval_node_px(comp_src, "EncodeLin");
1478 assert!((plin[0] as i32 - 46).abs() <= 2, "Lin HDR neg R={plin:?}");
1479 assert!((plin[1] as i32 - 138).abs() <= 2, "Lin HDR >1 G={plin:?}");
1480 // 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)
1481 let ppd = eval_node_px(comp_src, "EncodePreDiv");
1482 assert!((ppd[1] as i32 - 133).abs() <= 2, "PreDiv HDR >1 G={ppd:?}");
1483 }
13711484}
13721485
crates/engine/src/eval/composite.rs+128-6
......@@ -1,6 +1,26 @@
11use super::*;
22
33impl Engine {
4 fn flattened_fg_edge_mode(
5 comp: &Comp,
6 fg_id: NodeId,
7 time: f64,
8 expr: &ExprEngine,
9 fg_tex: &CachedTexture,
10 ) -> Option<f32> {
11 let n = comp.node(fg_id);
12 if n.kind == "Transform" {
13 if comp.eval_number(n, "FlattenTransform", time, 0.0, expr) <= 0.5 {
14 return None;
15 }
16 let e = comp.eval_number(n, "Edges", time, 0.0, expr).round() as i32;
17 if e == 1 || e == 3 {
18 return Some(e as f32);
19 }
20 }
21 fg_tex.edge_mode.map(f32::from)
22 }
23
424 pub(super) fn eval_merge(
525 &mut self,
626 comp: &Comp,
......@@ -24,7 +44,8 @@ impl Engine {
2444 return bg;
2545 }
2646
27 let fg = match node.connected_node("Foreground").and_then(|s| comp.find_node(s)) {
47 let fg_node = node.connected_node("Foreground").and_then(|s| comp.find_node(s));
48 let fg = match fg_node {
2849 Some(fid) => self.eval_node(comp, fid, time, enc, visiting, stats),
2950 None => return bg,
3051 };
......@@ -35,7 +56,14 @@ impl Engine {
3556 let gain = (comp.eval_number(node, "Gain", time, 1.0, &self.expr) as f32).clamp(0.0, 1.0);
3657 let burn_in = comp.eval_number(node, "BurnIn", time, 0.0, &self.expr) as f32;
3758 let sub_add = comp.eval_number(node, "SubtractiveAdditive", time, 1.0, &self.expr) as f32;
38 let edges = comp.eval_number(node, "Edges", time, 0.0, &self.expr) as f32;
59 let raw_edges = comp.eval_number(node, "Edges", time, 0.0, &self.expr) as f32;
60 let edges = if raw_edges < 0.5 {
61 fg_node
62 .and_then(|fid| Self::flattened_fg_edge_mode(comp, fid, time, &self.expr, &fg))
63 .unwrap_or(raw_edges)
64 } else {
65 raw_edges
66 };
3967 let flip_h = comp.eval_number(node, "FlipHoriz", time, 0.0, &self.expr) as f32;
4068 let flip_v = comp.eval_number(node, "FlipVert", time, 0.0, &self.expr) as f32;
4169
......@@ -241,8 +269,10 @@ impl Engine {
241269 if blend <= 1e-6 {
242270 return bg;
243271 }
244 // Fast path: Copy BG R/G/B/A onto itself (e.g. ZBuffer-only ChannelBoolean)
245 if op == 0 && to_r == 5 && to_g == 6 && to_b == 7 && to_a == 8 {
272 // Fast path: Copy BG R/G/B/A onto itself (e.g. ZBuffer-only ChannelBoolean) or all Do Nothing
273 if (op == 0 && to_r == 5 && to_g == 6 && to_b == 7 && to_a == 8)
274 || (op == 0 && to_r == 4 && to_g == 4 && to_b == 4 && to_a == 4)
275 {
246276 return bg;
247277 }
248278 // When Foreground is unconnected, Fusion uses Background as Foreground
......@@ -253,6 +283,11 @@ impl Engine {
253283 };
254284 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(bg.canvas_size), enc, visiting, stats);
255285 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
286 let expand_canvas = bg.has_dod()
287 && (op == 10 || op == 11 || [to_r, to_g, to_b, to_a].contains(&16));
288 let dst_size = if expand_canvas { bg.canvas_size } else { bg.size };
289 let dst_origin = if expand_canvas { [0, 0] } else { bg.origin };
290
256291 let mut u = UniformParams::default();
257292 u.p[0] = [op as f32, to_r as f32, to_g as f32, to_b as f32];
258293 u.p[1] = [
......@@ -267,14 +302,19 @@ impl Engine {
267302 if bg.has_dod() {
268303 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];
269304 }
270 let key = self.make_key(node, "ChannelBoolean", bg.size, &[bg.content_hash, fg.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..4]));
305 u.p[4] = [if expand_canvas { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0];
306 let key = self.make_key(node, "ChannelBoolean", dst_size, &[bg.content_hash, fg.content_hash, mask_hash], bytemuck::cast_slice(&u.p[..5]));
271307 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
272308 stats.cache_hits += 1;
273309 return hit;
274310 }
275311 stats.cache_misses += 1;
276312 stats.nodes_evaluated += 1;
277 let dst = self.alloc_tex_like(&bg, bg.depth.max(fg.depth), key);
313 let mut dst = self.alloc_tex_dod(dst_size, dst_origin, bg.canvas_size, bg.depth.max(fg.depth), key);
314 dst.tex_scale = bg.tex_scale;
315 if op == 11 && mask.is_none() && ![to_r, to_g, to_b, to_a].contains(&4) {
316 dst.edge_mode = Some(2);
317 }
278318 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
279319 self.dispatch_op(enc, ShaderOp::ChannelBoolean, &bg.view, &fg.view, mv, &dst, &u);
280320 self.cache.insert(key, dst.clone(), node.id, frame_i);
......@@ -927,5 +967,87 @@ mod tests {
927967 assert!((m50[1] as i32 - 128).abs() <= 4 && (m50[2] as i32 - 128).abs() <= 4, "MultiStop 0.5={m50:?}");
928968 assert!(m75[2] >= 250 && m75[0] <= 5, "MultiStop 0.75={m75:?}");
929969 }
970
971 #[test]
972 fn channel_boolean_and_or_xor_signed_add_and_solid_border() {
973 let comp = r#"{
974 Tools = ordered() {
975 Bg = Background {
976 Inputs = {
977 Width = Input { Value = 32, },
978 Height = Input { Value = 32, },
979 TopLeftRed = Input { Value = 0.7, },
980 TopLeftGreen = Input { Value = 0.5, },
981 TopLeftBlue = Input { Value = 0.3, },
982 TopLeftAlpha = Input { Value = 0.8, },
983 },
984 },
985 Fg = Background {
986 Inputs = {
987 Width = Input { Value = 32, },
988 Height = Input { Value = 32, },
989 TopLeftRed = Input { Value = 0.2, },
990 TopLeftGreen = Input { Value = 0.4, },
991 TopLeftBlue = Input { Value = 0.8, },
992 TopLeftAlpha = Input { Value = 0.5, },
993 },
994 },
995 CbAnd = ChannelBoolean { Inputs = { Operation = Input { Value = 3, }, Background = Input { SourceOp = "Bg", Source = "Output", }, Foreground = Input { SourceOp = "Fg", Source = "Output", }, }, },
996 CbOr = ChannelBoolean { Inputs = { Operation = Input { Value = 4, }, Background = Input { SourceOp = "Bg", Source = "Output", }, Foreground = Input { SourceOp = "Fg", Source = "Output", }, }, },
997 CbXor = ChannelBoolean { Inputs = { Operation = Input { Value = 5, }, Background = Input { SourceOp = "Bg", Source = "Output", }, Foreground = Input { SourceOp = "Fg", Source = "Output", }, }, },
998 CbSAdd = ChannelBoolean { Inputs = { Operation = Input { Value = 14, }, Background = Input { SourceOp = "Bg", Source = "Output", }, Foreground = Input { SourceOp = "Fg", Source = "Output", }, }, },
999 CbSolid = ChannelBoolean { Inputs = { Operation = Input { Value = 11, }, Background = Input { SourceOp = "Bg", Source = "Output", }, }, },
1000 SolidZoom = Transform { Inputs = { Size = Input { Value = 0.5, }, Input = Input { SourceOp = "CbSolid", Source = "Output", }, }, },
1001 }
1002 }"#;
1003 let (p3, w, _) = eval_node_px(comp, "CbAnd");
1004 assert!((px_at(&p3, w, 16, 16)[0] as i32 - 36).abs() <= 2, "And={:?}", px_at(&p3, w, 16, 16));
1005 let (p4, _, _) = eval_node_px(comp, "CbOr");
1006 assert!((px_at(&p4, w, 16, 16)[0] as i32 - 194).abs() <= 2, "Or={:?}", px_at(&p4, w, 16, 16));
1007 let (p5, _, _) = eval_node_px(comp, "CbXor");
1008 assert!((px_at(&p5, w, 16, 16)[0] as i32 - 158).abs() <= 2, "Xor={:?}", px_at(&p5, w, 16, 16));
1009 let (p14, _, _) = eval_node_px(comp, "CbSAdd");
1010 assert!((px_at(&p14, w, 16, 16)[2] as i32 - 153).abs() <= 2, "SignedAdd={:?}", px_at(&p14, w, 16, 16));
1011 // Solid (op=11) infinite white border stays (255,255,255,255) outside zoomed region.
1012 let (psz, _, _) = eval_node_px(comp, "SolidZoom");
1013 assert_eq!(px_at(&psz, w, 2, 2), [255, 255, 255, 255]);
1014 }
1015
1016 #[test]
1017 fn merge_inherits_flattened_transform_wrap_edge_mode() {
1018 let comp = r#"{
1019 Tools = ordered() {
1020 Black = Background { Inputs = { Width = Input { Value = 64, }, Height = Input { Value = 64, }, TopLeftAlpha = Input { Value = 1, }, }, },
1021 Dot = Background {
1022 Inputs = {
1023 Width = Input { Value = 64, },
1024 Height = Input { Value = 64, },
1025 TopLeftRed = Input { Value = 1, },
1026 TopLeftAlpha = Input { Value = 1, },
1027 EffectMask = Input { SourceOp = "R", Source = "Mask", },
1028 },
1029 },
1030 R = RectangleMask { Inputs = { MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, }, Width = Input { Value = 0.5, }, Height = Input { Value = 0.5, }, }, },
1031 XfFlat = Transform {
1032 Inputs = {
1033 Size = Input { Value = 0.25, },
1034 Edges = Input { Value = 1, },
1035 FlattenTransform = Input { Value = 1, },
1036 Input = Input { SourceOp = "Dot", Source = "Output", },
1037 },
1038 },
1039 M = Merge {
1040 Inputs = {
1041 Background = Input { SourceOp = "Black", Source = "Output", },
1042 Foreground = Input { SourceOp = "XfFlat", Source = "Output", },
1043 Center = Input { Value = { 0.2, 0.5 }, },
1044 },
1045 },
1046 }
1047 }"#;
1048 let (px, w, _) = eval_node_px(comp, "M");
1049 // Right side (x=60, y=32) wraps into tiled red instead of clipping to black.
1050 assert!(px_at(&px, w, 60, 32)[0] > 200, "expected wrapped red at x=60, got {:?}", px_at(&px, w, 60, 32));
1051 }
9301052}
9311053
crates/engine/src/eval/masks.rs+64-24
......@@ -440,7 +440,7 @@ impl Engine {
440440 let range_default = comp.eval_number(node, "SelectionRange", time, 0.1, &self.expr);
441441 let range = comp.eval_number(node, "Range", time, range_default, &self.expr).clamp(0.0, 1.0) as f32;
442442 let range_soft = comp.eval_number(node, "RangeSoftEdge", time, 0.0, &self.expr).clamp(0.0, 1.0) as f32;
443 let color_space = comp.eval_number(node, "ColorSpace", time, 0.0, &self.expr).round() as i32;
443 let channel = comp.eval_number(node, "Channel", time, 0.0, &self.expr).round() as i32;
444444 let soft = comp.eval_number(node, "SoftEdge", time, 0.0, &self.expr).max(0.0) as f32;
445445 let invert = comp.eval_number(node, "Invert", time, 0.0, &self.expr) as f32;
446446 let level = comp.eval_number(node, "Level", time, 1.0, &self.expr) as f32;
......@@ -460,7 +460,7 @@ impl Engine {
460460 seed_pt[1] as f32,
461461 range,
462462 range_soft,
463 color_space as f32,
463 channel as f32,
464464 soft,
465465 invert,
466466 level,
......@@ -491,7 +491,7 @@ impl Engine {
491491 Ok(src_rgba) if src_rgba.len() >= w * h * 4 => {
492492 let sx = (seed_pt[0].clamp(0.0, 1.0) * (w - 1) as f64).round() as usize;
493493 let sy = ((1.0 - seed_pt[1].clamp(0.0, 1.0)) * (h - 1) as f64).round() as usize;
494 wand_flood_fill(&src_rgba, w, h, [sx, sy], range, range_soft, color_space == 1)
494 wand_flood_fill(&src_rgba, w, h, [sx, sy], range, range_soft, channel)
495495 }
496496 _ => vec![0; w * h * 4],
497497 };
......@@ -536,34 +536,60 @@ impl Engine {
536536
537537}
538538
539/// Flood fill from `seed` over RGBA8 `src`: pixels within `range` of the seed color are selected at
540/// 1.0, falling off linearly to 0.0 at `range + range_soft`; only the connected region is selected.
541fn wand_flood_fill(src: &[u8], w: usize, h: usize, seed: [usize; 2], range: f32, range_soft: f32, yuv: bool) -> Vec<u8> {
542 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];
543 let [sr, sg, sb] = rgb(seed[1] * w + seed[0]);
544 let sl = 0.299 * sr + 0.587 * sg + 0.114 * sb;
539/// Flood fill from `seed` over RGBA8 `src`: Fusion 21.1 `WandMask` halves `Range` and
540/// `RangeSoftEdge` (`r = 0.5 * range`, `s = 0.5 * range_soft`), selects `Channel`
541/// (`0=Color` Euclidean RGB, `1=Red`, `2=Green`, `3=Blue`, `4=Alpha`), and falls off linearly
542/// from `1.0` at `d <= r` to `0.0` at `r + s`.
543fn wand_flood_fill(src: &[u8], w: usize, h: usize, seed: [usize; 2], range: f32, range_soft: f32, channel: i32) -> Vec<u8> {
544 let rgba = |i: usize| [
545 src[i * 4] as f32 / 255.0,
546 src[i * 4 + 1] as f32 / 255.0,
547 src[i * 4 + 2] as f32 / 255.0,
548 src[i * 4 + 3] as f32 / 255.0,
549 ];
550 let start = seed[1] * w + seed[0];
551 let [sr, sg, sb, sa] = rgba(start);
545552 let dist = |i: usize| {
546 let [r, g, b] = rgb(i);
547 if yuv {
548 let l = 0.299 * r + 0.587 * g + 0.114 * b;
549 (l - sl).abs().max(((b - l) - (sb - sl)).abs()).max(((r - l) - (sr - sl)).abs())
550 } else {
551 (r - sr).abs().max((g - sg).abs()).max((b - sb).abs())
553 let [r, g, b, a] = rgba(i);
554 match channel {
555 0 => {
556 let (dr, dg, db) = (r - sr, g - sg, b - sb);
557 (dr * dr + dg * dg + db * db).sqrt()
558 }
559 1 => (r - sr).abs(),
560 2 => (g - sg).abs(),
561 3 => (b - sb).abs(),
562 4 => (a - sa).abs(),
563 _ => {
564 if src[i * 4..i * 4 + 4] == src[start * 4..start * 4 + 4] {
565 0.0
566 } else {
567 f32::INFINITY
568 }
569 }
552570 }
553571 };
572 let r_half = range * 0.5;
573 let soft_half = range_soft * 0.5;
574 let max_d = r_half + soft_half;
554575 let mut out = vec![0u8; w * h * 4];
555576 let mut visited = vec![false; w * h];
556 let start = seed[1] * w + seed[0];
557577 visited[start] = true;
558578 let mut queue = std::collections::VecDeque::from([start]);
559579 while let Some(i) = queue.pop_front() {
560580 let d = dist(i);
561 let a = if d <= range { 1.0 } else { 1.0 - (d - range) / range_soft };
581 let a = if d <= r_half {
582 1.0
583 } else if soft_half <= 1e-6 {
584 0.0
585 } else {
586 (1.0 - (d - r_half) / soft_half).clamp(0.0, 1.0)
587 };
562588 out[i * 4..i * 4 + 4].fill((a * 255.0).round() as u8);
563589 let (x, y) = (i % w, i / w);
564590 let nbrs = [(x > 0).then(|| i - 1), (x + 1 < w).then(|| i + 1), (y > 0).then(|| i - w), (y + 1 < h).then(|| i + w)];
565591 for n in nbrs.into_iter().flatten() {
566 if !visited[n] && dist(n) <= range + range_soft {
592 if !visited[n] && dist(n) <= max_d {
567593 visited[n] = true;
568594 queue.push_back(n);
569595 }
......@@ -639,21 +665,35 @@ mod tests {
639665
640666 #[test]
641667 fn wand_range_soft_edge_falls_off_linearly() {
642 // Distances from the seed: 0, 0.1 (range), 0.2, 0.3 (range + soft), 0.4.
668 // Channel 1 (Red) compares |r - sr| against half-scale range (0.1) and soft (0.2).
669 // Distances from seed 0: 0, 25/255 (~0.098 <= 0.1), 51/255 (0.2), 76/255 (~0.298), 102/255 (0.4).
643670 let src = gray_row(&[0, 25, 51, 76, 102]);
644 let hard = wand_flood_fill(&src, 5, 1, [0, 0], 0.1, 0.0, false);
671 let hard = wand_flood_fill(&src, 5, 1, [0, 0], 0.2, 0.0, 1);
645672 assert_eq!(alphas(&hard), [255, 255, 0, 0, 0]);
646 let soft = wand_flood_fill(&src, 5, 1, [0, 0], 0.1, 0.2, false);
673 let soft = wand_flood_fill(&src, 5, 1, [0, 0], 0.2, 0.4, 1);
647674 let a = alphas(&soft);
648675 assert_eq!((a[0], a[1], a[2], a[4]), (255, 255, 128, 0));
649 assert!(a[3] <= 3, "falloff reaches 0 at range + soft, got {a:?}");
676 assert!(a[3] <= 3, "falloff reaches 0 at 0.5*(range + soft), got {a:?}");
677 }
678
679 #[test]
680 fn wand_channel_modes_match_fusion_euclidean_and_per_channel() {
681 // Seed=(0, 128, 255, 255); px1=(51, 128, 255, 255) -> dr=0.2; px2=(0, 179, 255, 255) -> dg=0.2.
682 let src = vec![0, 128, 255, 255, 51, 128, 255, 255, 0, 179, 255, 255];
683 assert_eq!(alphas(&wand_flood_fill(&src, 3, 1, [0, 0], 0.2, 0.4, 1)), [255, 128, 255]);
684 assert_eq!(alphas(&wand_flood_fill(&src, 3, 1, [0, 0], 0.2, 0.4, 2)), [255, 255, 128]);
685 // Color mode (Channel=0): Euclidean RGB distance for gray step v=26 (sqrt(3)*0.102=0.177).
686 // With r_half=0.1, soft_half=0.2: 1 - (0.177 - 0.1) / 0.2 = 0.617 (~157).
687 let gray = gray_row(&[0, 26]);
688 let a = alphas(&wand_flood_fill(&gray, 2, 1, [0, 0], 0.2, 0.4, 0));
689 assert!((a[1] as i32 - 157).abs() <= 2, "Euclidean RGB gray falloff got {a:?}");
650690 }
651691
652692 #[test]
653693 fn wand_soft_edge_stays_connected() {
654 // The second 0-distance pixel is cut off from the seed by a pixel beyond range + soft.
694 // The second 0-distance pixel is cut off from the seed by a pixel beyond 0.5*(range + soft).
655695 let src = gray_row(&[0, 255, 0]);
656 assert_eq!(alphas(&wand_flood_fill(&src, 3, 1, [0, 0], 0.1, 0.2, false)), [255, 0, 0]);
696 assert_eq!(alphas(&wand_flood_fill(&src, 3, 1, [0, 0], 0.2, 0.4, 1)), [255, 0, 0]);
657697 }
658698
659699 #[test]
crates/engine/src/shaders/color.wgsl+21-9
......@@ -439,8 +439,22 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) {
439439// ============================================================================
440440// 20. ColorCurves (per-channel 256x1 LUT texture in src_b)
441441// p[0] = (prediv, ignore_transparent, blend, has_mask)
442// p[1] = (mask_invert, apply_alpha, 0, 0)
442// p[1] = (mask_invert, apply_alpha, multiply_by_mask, 0)
443// p[2] = y0 (rgba at x=0), p[3] = y1 (rgba at x=1)
444// p[4] = slope0 (rgba tangent at x=0), p[5] = slope1 (rgba tangent at x=1)
443445// ============================================================================
446fn color_curves_eval_channel(v: f32, ch: i32, lut_n: f32) -> f32 {
447 if (v < 0.0) {
448 return u.p[2][ch] + v * u.p[4][ch];
449 }
450 if (v > 1.0) {
451 return u.p[3][ch] + (v - 1.0) * u.p[5][ch];
452 }
453 let lut_u = (0.5 + v * (lut_n - 1.0)) / lut_n;
454 let s = textureSampleLevel(src_b, samp_lin, vec2<f32>(lut_u, 0.5), 0.0);
455 return s[ch];
456}
457
444458@compute @workgroup_size(8, 8)
445459fn color_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) {
446460 let dims = textureDimensions(dst_tex);
......@@ -458,20 +472,18 @@ fn color_curves_op(@builtin(global_invocation_id) gid: vec3<u32>) {
458472 }
459473
460474 var rgb = src.rgb;
461 let unpre = (prediv || ignore_trans) && src.a > 1e-5;
475 let unpre = prediv || ignore_trans;
462476 if (unpre) {
463 rgb = clamp(rgb / src.a, vec3<f32>(0.0), vec3<f32>(1.0));
477 rgb = select(vec3<f32>(0.0), rgb / src.a, src.a > 1e-5);
464478 }
465479
466480 let lut_n = f32(textureDimensions(src_b).x);
467 let lut_u = (vec3<f32>(0.5) + clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0)) * (lut_n - 1.0)) / lut_n;
468 let r = textureSampleLevel(src_b, samp_lin, vec2<f32>(lut_u.r, 0.5), 0.0).r;
469 let g = textureSampleLevel(src_b, samp_lin, vec2<f32>(lut_u.g, 0.5), 0.0).g;
470 let b = textureSampleLevel(src_b, samp_lin, vec2<f32>(lut_u.b, 0.5), 0.0).b;
481 let r = color_curves_eval_channel(rgb.r, 0, lut_n);
482 let g = color_curves_eval_channel(rgb.g, 1, lut_n);
483 let b = color_curves_eval_channel(rgb.b, 2, lut_n);
471484 var a = src.a;
472485 if (apply_a) {
473 let a_u = (0.5 + clamp(src.a, 0.0, 1.0) * (lut_n - 1.0)) / lut_n;
474 a = textureSampleLevel(src_b, samp_lin, vec2<f32>(a_u, 0.5), 0.0).a;
486 a = color_curves_eval_channel(src.a, 3, lut_n);
475487 }
476488
477489 var out_rgb = vec3<f32>(r, g, b);
crates/engine/src/shaders/composite.wgsl+26-10
......@@ -566,6 +566,9 @@ fn cb_apply_op(op: i32, bg_v: f32, fg_v: f32) -> f32 {
566566 case 0: { return fg_v; } // Copy
567567 case 1: { return bg_v + fg_v; } // Add
568568 case 2: { return bg_v - fg_v; } // Subtract
569 case 3: { return bg_v * fg_v; } // And
570 case 4: { return bg_v + fg_v - bg_v * fg_v; } // Or
571 case 5: { return bg_v + fg_v - 2.0 * bg_v * fg_v; } // Exclusive Or
569572 case 6: { return bg_v * fg_v; } // Multiply
570573 case 7: { return select(bg_v / fg_v, 0.0, abs(fg_v) <= 1e-5); } // Divide
571574 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 {
574577 case 11: { return 1.0; } // Solid
575578 case 12: { return 0.0; } // Clear
576579 case 13: { return abs(bg_v - fg_v); } // Difference
577 default: { return fg_v; }
580 case 14: { return bg_v + fg_v - 0.5; } // Signed Add
581 default: { return select(fg_v, bg_v + fg_v - 0.5, op >= 14); }
578582 }
579583}
580584
......@@ -582,16 +586,28 @@ fn cb_apply_op(op: i32, bg_v: f32, fg_v: f32) -> f32 {
582586fn channel_boolean_op(@builtin(global_invocation_id) gid: vec3<u32>) {
583587 let dims = textureDimensions(dst_tex);
584588 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
585 let bg = textureLoad(src_a, vec2<i32>(gid.xy), 0);
586589 let bg_dod = u.p[3];
587 let has_bg_dod = bg_dod.z > 0.5 && bg_dod.w > 0.5;
588 let bg_dims = select(vec2<f32>(dims), bg_dod.zw, has_bg_dod);
589 let canvas_px = select(
590 vec2<f32>(gid.xy) + vec2<f32>(0.5),
591 vec2<f32>(gid.xy) + vec2<f32>(0.5) + bg_dod.xy,
592 has_bg_dod,
593 );
594 let fu = vec2<f32>(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y);
590 let expand_canvas = u.p[4].x > 0.5;
591 var bg: vec4<f32>;
592 var bg_dims: vec2<f32>;
593 var canvas_px: vec2<f32>;
594 var fu: vec2<f32>;
595 if (expand_canvas) {
596 bg_dims = vec2<f32>(dims);
597 canvas_px = vec2<f32>(gid.xy) + vec2<f32>(0.5);
598 fu = vec2<f32>(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y);
599 bg = sample_dod_edges_mode(src_a, fu, 0, bg_dod, false);
600 } else {
601 bg = textureLoad(src_a, vec2<i32>(gid.xy), 0);
602 let has_bg_dod = bg_dod.z > 0.5 && bg_dod.w > 0.5;
603 bg_dims = select(vec2<f32>(dims), bg_dod.zw, has_bg_dod);
604 canvas_px = select(
605 vec2<f32>(gid.xy) + vec2<f32>(0.5),
606 vec2<f32>(gid.xy) + vec2<f32>(0.5) + bg_dod.xy,
607 has_bg_dod,
608 );
609 fu = vec2<f32>(canvas_px.x / bg_dims.x, 1.0 - canvas_px.y / bg_dims.y);
610 }
595611 let fg = sample_dod_edges_mode(src_b, fu, 0, u.p[2], false);
596612
597613 let op = i32(u.p[0].x + 0.5);