authoravatarclover caruso<git@paperclover.net>2026-12-02 03:58:04-08:00
committeravatarclover caruso<git@paperclover.net>2026-12-02 04:21:29-08:00
logd1417f4de7446f1bd31dd9b7feedad5e464bc949
treebaa1aa0ecb0fe087b3e3aa0fd75dcc6992cbc787
parent8424e1111e8ac1d6bd1a49a8a2618309fa504018
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(engine): push core 2D tools toward 100% parity (mask FitMask/channels, Merge Conjoint/Process*, CC OutputLevels, Blur/ColorGain/Sharpen)


8 files changed, 595 insertions(+), 80 deletions(-)

crates/engine/src/eval/blur_glow.rs+177-19
...@@ -39,9 +39,13 @@ impl Engine {...@@ -39,9 +39,13 @@ impl Engine {
39 let ry = y_raw * scale_ref * gauss_k;39 let ry = y_raw * scale_ref * gauss_k;
40 let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32;40 let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32;
41 let clamp_edges = if node.static_str("ClippingMode").unwrap_or("Frame") == "None" { 0.0 } else { 1.0 };41 let clamp_edges = if node.static_str("ClippingMode").unwrap_or("Frame") == "None" { 0.0 } else { 1.0 };
42 let mbm = self.multiplies_by_mask(comp, node, time);
43 let skip = self.process_skip(comp, node, time);
44 let off = self.channel_off(comp, node, time);
45 let all_noop = (0..4).all(|c| skip[c] > 0.5 || off[c] > 0.5);
4246
43 let max_r = rx.max(ry);47 let max_r = rx.max(ry);
44 if max_r < 0.25 || blend <= 1e-6 {48 if !mbm && (max_r < 0.25 || blend <= 1e-6 || all_noop) {
45 return src;49 return src;
46 }50 }
47 let has_mask = Self::resolve_effect_mask_id(comp, node).is_some();51 let has_mask = Self::resolve_effect_mask_id(comp, node).is_some();
...@@ -53,7 +57,19 @@ impl Engine {...@@ -53,7 +57,19 @@ impl Engine {
53 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);57 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);
54 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);58 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
5559
56 let key = self.make_key(node, "Blur", src.size, &[src.content_hash, mask_hash], &[rx, ry, blend, mode, clamp_edges, if mask_inv { 1.0 } else { 0.0 }]);60 let key = self.make_key(
61 node,
62 "Blur",
63 src.size,
64 &[src.content_hash, mask_hash],
65 &[
66 rx, ry, blend, mode, clamp_edges,
67 if mask_inv { 1.0 } else { 0.0 },
68 if mbm { 1.0 } else { 0.0 },
69 skip[0], skip[1], skip[2], skip[3],
70 off[0], off[1], off[2], off[3],
71 ],
72 );
57 if let Some(hit) = self.cache.get(key, node.id, frame_i) {73 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
58 stats.cache_hits += 1;74 stats.cache_hits += 1;
59 return hit;75 return hit;
...@@ -63,7 +79,12 @@ impl Engine {...@@ -63,7 +79,12 @@ impl Engine {
6379
64 // When ClippingMode = "None" on an unmasked full-blend Blur, Fusion expands the DoD so the80 // When ClippingMode = "None" on an unmasked full-blend Blur, Fusion expands the DoD so the
65 // outer half of the Gaussian/Box falloff survives beyond the frame border for downstream Merges!81 // outer half of the Gaussian/Box falloff survives beyond the frame border for downstream Merges!
66 let expand_dod = clamp_edges < 0.5 && mask.is_none() && blend >= 0.999;82 let expand_dod = clamp_edges < 0.5
83 && mask.is_none()
84 && blend >= 0.999
85 && !mbm
86 && skip == [0.0; 4]
87 && off == [0.0; 4];
67 let (work_src, out_origin) = if expand_dod {88 let (work_src, out_origin) = if expand_dod {
68 let pad_x = if rx >= 0.25 { (rx * 1.5).ceil().min(384.0) as u32 } else { 0 };89 let pad_x = if rx >= 0.25 { (rx * 1.5).ceil().min(384.0) as u32 } else { 0 };
69 let pad_y = if ry >= 0.25 { (ry * 1.5).ceil().min(384.0) as u32 } else { 0 };90 let pad_y = if ry >= 0.25 { (ry * 1.5).ceil().min(384.0) as u32 } else { 0 };
...@@ -100,6 +121,7 @@ impl Engine {...@@ -100,6 +121,7 @@ impl Engine {
100 1u32121 1u32
101 };122 };
102123
124 let mbm_f = if mbm { 1.0 } else { 0.0 };
103 if down > 1 {125 if down > 1 {
104 let work_size = [(work_src.size[0] / down).max(1), (work_src.size[1] / down).max(1)];126 let work_size = [(work_src.size[0] / down).max(1), (work_src.size[1] / down).max(1)];
105 let small = self.alloc_tex(work_size, src.depth, 0);127 let small = self.alloc_tex(work_size, src.depth, 0);
...@@ -120,28 +142,35 @@ impl Engine {...@@ -120,28 +142,35 @@ impl Engine {
120 uv.p[1] = [1.0, 0.0, 0.0, clamp_edges];142 uv.p[1] = [1.0, 0.0, 0.0, clamp_edges];
121 self.dispatch_op(enc, ShaderOp::BlurV, &tmp.view, &self.dummy.view, &self.dummy.view, &small_blur, &uv);143 self.dispatch_op(enc, ShaderOp::BlurV, &tmp.view, &self.dummy.view, &self.dummy.view, &small_blur, &uv);
122144
123 if blend >= 0.999 && mask.is_none() {145 if blend >= 0.999 && mask.is_none() && skip == [0.0; 4] && off == [0.0; 4] {
124 self.dispatch_op(enc, ShaderOp::DisplayBlit, &small_blur.view, &self.dummy.view, &self.dummy.view, &dst, &u_blit);146 self.dispatch_op(enc, ShaderOp::DisplayBlit, &small_blur.view, &self.dummy.view, &self.dummy.view, &dst, &u_blit);
125 } else {147 } else {
126 let mut um = UniformParams::default();148 let up = self.alloc_tex(work_src.size, src.depth, 0);
127 um.p[0] = [0.5, 0.5, 1.0, 0.0];149 self.dispatch_op(enc, ShaderOp::DisplayBlit, &small_blur.view, &self.dummy.view, &self.dummy.view, &up, &u_blit);
128 um.p[1] = [blend, 1.0, 0.0, 0.0];150 let mut ug = UniformParams::default();
129 um.p[3] = [if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, work_src.size[0] as f32, work_src.size[1] as f32];151 ug.p[0] = [1.0, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }];
152 ug.p[1] = [4.0, 0.0, 0.0, mbm_f];
153 ug.p[3] = skip;
154 ug.p[4] = off;
130 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);155 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
131 self.dispatch_op(enc, ShaderOp::Merge, &work_src.view, &small_blur.view, mv, &dst, &um);156 self.dispatch_op(enc, ShaderOp::GlowAdd, &work_src.view, &up.view, mv, &dst, &ug);
132 }157 }
133 } else if rx < 0.25 {158 } else if rx < 0.25 {
134 let mut uv = UniformParams::default();159 let mut uv = UniformParams::default();
135 uv.p[0] = [0.0, 1.0, ry, mode];160 uv.p[0] = [0.0, 1.0, ry, mode];
136 uv.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges];161 uv.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges];
137 uv.p[2] = [1.0, 0.0, 0.0, 0.0];162 uv.p[2] = [1.0, mbm_f, 0.0, 0.0];
163 uv.p[3] = skip;
164 uv.p[4] = off;
138 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);165 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
139 self.dispatch_op(enc, ShaderOp::BlurV, &work_src.view, &work_src.view, mv, &dst, &uv);166 self.dispatch_op(enc, ShaderOp::BlurV, &work_src.view, &work_src.view, mv, &dst, &uv);
140 } else if ry < 0.25 {167 } else if ry < 0.25 {
141 let mut uh = UniformParams::default();168 let mut uh = UniformParams::default();
142 uh.p[0] = [1.0, 0.0, rx, mode];169 uh.p[0] = [1.0, 0.0, rx, mode];
143 uh.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges];170 uh.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges];
144 uh.p[2] = [1.0, 0.0, 0.0, 0.0];171 uh.p[2] = [1.0, mbm_f, 0.0, 0.0];
172 uh.p[3] = skip;
173 uh.p[4] = off;
145 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);174 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
146 self.dispatch_op(enc, ShaderOp::BlurH, &work_src.view, &work_src.view, mv, &dst, &uh);175 self.dispatch_op(enc, ShaderOp::BlurH, &work_src.view, &work_src.view, mv, &dst, &uh);
147 } else {176 } else {
...@@ -154,7 +183,9 @@ impl Engine {...@@ -154,7 +183,9 @@ impl Engine {
154 let mut uv = UniformParams::default();183 let mut uv = UniformParams::default();
155 uv.p[0] = [0.0, 1.0, ry, mode];184 uv.p[0] = [0.0, 1.0, ry, mode];
156 uv.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges];185 uv.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges];
157 uv.p[2] = [1.0, 0.0, 0.0, 0.0]; // blend against original unblurred src in src_b!186 uv.p[2] = [1.0, mbm_f, 0.0, 0.0]; // blend against original unblurred src in src_b!
187 uv.p[3] = skip;
188 uv.p[4] = off;
158 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);189 let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view);
159 self.dispatch_op(enc, ShaderOp::BlurV, &tmp.view, &work_src.view, mv, &dst, &uv);190 self.dispatch_op(enc, ShaderOp::BlurV, &tmp.view, &work_src.view, mv, &dst, &uv);
160 }191 }
...@@ -497,7 +528,7 @@ impl Engine {...@@ -497,7 +528,7 @@ impl Engine {
497 let sy = if lock_xy {528 let sy = if lock_xy {
498 sx529 sx
499 } else {530 } else {
500 comp.eval_number(node, "YGlowSize", time, sx as f64, &self.expr) as f32531 comp.eval_number(node, "YGlowSize", time, 10.0, &self.expr) as f32
501 };532 };
502 let g = comp.eval_number(node, "Gain", time, 2.0, &self.expr) as f32;533 let g = comp.eval_number(node, "Gain", time, 2.0, &self.expr) as f32;
503 let th = comp.eval_number(node, "Threshold", time, 0.0, &self.expr).clamp(0.0, 0.999) as f32;534 let th = comp.eval_number(node, "Threshold", time, 0.0, &self.expr).clamp(0.0, 0.999) as f32;
...@@ -510,7 +541,7 @@ impl Engine {...@@ -510,7 +541,7 @@ impl Engine {
510 let sy = if lock_xy {541 let sy = if lock_xy {
511 sx542 sx
512 } else {543 } else {
513 comp.eval_number(node, "YGlowSize", time, sx as f64, &self.expr) as f32544 comp.eval_number(node, "YGlowSize", time, glow_fallback, &self.expr) as f32
514 };545 };
515 let glow_p = comp.eval_number(node, "Glow", time, 0.8, &self.expr).clamp(0.0, 0.999) as f32;546 let glow_p = comp.eval_number(node, "Glow", time, 0.8, &self.expr).clamp(0.0, 0.999) as f32;
516 let g = 1.0 / (1.0 - glow_p).max(1e-3);547 let g = 1.0 / (1.0 - glow_p).max(1e-3);
...@@ -1290,12 +1321,21 @@ impl Engine {...@@ -1290,12 +1321,21 @@ impl Engine {
1290 };1321 };
1291 let src = self.eval_node(comp, src_id, time, enc, visiting, stats);1322 let src = self.eval_node(comp, src_id, time, enc, visiting, stats);
1292 let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32;1323 let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32;
1293 let lock_xy = comp.eval_number(node, "LockAmountXY", time, 1.0, &self.expr) > 0.5;1324 let lock_xy = if node.inputs.contains_key("LockXY") {
1325 comp.eval_number(node, "LockXY", time, 1.0, &self.expr) > 0.5
1326 } else {
1327 comp.eval_number(node, "LockAmountXY", time, 1.0, &self.expr) > 0.5
1328 };
1294 let x_amt = comp.eval_number(node, "XAmount", time, 1.0, &self.expr) as f32;1329 let x_amt = comp.eval_number(node, "XAmount", time, 1.0, &self.expr) as f32;
1295 let y_amt = if lock_xy { x_amt } else { comp.eval_number(node, "YAmount", time, x_amt as f64, &self.expr) as f32 };1330 let y_amt = if lock_xy {
1331 x_amt
1332 } else {
1333 comp.eval_number(node, "YAmount", time, 1.0, &self.expr) as f32
1334 };
1296 let scale_ref = src.canvas_size[0] as f32 / 720.0 * 1.14;1335 let scale_ref = src.canvas_size[0] as f32 / 720.0 * 1.14;
1297 let rx = (x_amt * scale_ref).max(0.0);1336 let rx = (x_amt * scale_ref).max(0.0);
1298 let ry = (y_amt * scale_ref).max(0.0);1337 let ry = (y_amt * scale_ref).max(0.0);
1338 let clamp_edges = if node.static_str("ClippingMode").unwrap_or("Frame") == "None" { 0.0 } else { 1.0 };
1299 let mbm = self.multiplies_by_mask(comp, node, time);1339 let mbm = self.multiplies_by_mask(comp, node, time);
1300 let skip = self.process_skip(comp, node, time);1340 let skip = self.process_skip(comp, node, time);
1301 let off = self.channel_off(comp, node, time);1341 let off = self.channel_off(comp, node, time);
...@@ -1311,7 +1351,19 @@ impl Engine {...@@ -1311,7 +1351,19 @@ impl Engine {
1311 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);1351 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats);
1312 let mask = mask.map(|m| self.crop_to_canvas(enc, m));1352 let mask = mask.map(|m| self.crop_to_canvas(enc, m));
1313 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);1353 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
1314 let key = self.make_key(node, "Sharpen", src.size, &[src.content_hash, mask_hash], &[rx, ry, blend, if mask_inv { 1.0 } else { 0.0 }, mbm as u8 as f32, skip[0], skip[1], skip[2], skip[3], off[0], off[1], off[2], off[3]]);1354 let key = self.make_key(
1355 node,
1356 "Sharpen",
1357 src.size,
1358 &[src.content_hash, mask_hash],
1359 &[
1360 rx, ry, blend, clamp_edges,
1361 if mask_inv { 1.0 } else { 0.0 },
1362 mbm as u8 as f32,
1363 skip[0], skip[1], skip[2], skip[3],
1364 off[0], off[1], off[2], off[3],
1365 ],
1366 );
1315 if let Some(hit) = self.cache.get(key, node.id, frame_i) {1367 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
1316 stats.cache_hits += 1;1368 stats.cache_hits += 1;
1317 return hit;1369 return hit;
...@@ -1323,11 +1375,11 @@ impl Engine {...@@ -1323,11 +1375,11 @@ impl Engine {
1323 let tmp2 = self.alloc_tex(src.size, src.depth, 0);1375 let tmp2 = self.alloc_tex(src.size, src.depth, 0);
1324 let mut uh = UniformParams::default();1376 let mut uh = UniformParams::default();
1325 uh.p[0] = [1.0, 0.0, rx, 0.0];1377 uh.p[0] = [1.0, 0.0, rx, 0.0];
1326 uh.p[1] = [1.0, 0.0, 0.0, 0.0];1378 uh.p[1] = [1.0, 0.0, 0.0, clamp_edges];
1327 self.dispatch_op(enc, ShaderOp::BlurH, &src.view, &self.dummy.view, &self.dummy.view, &tmp1, &uh);1379 self.dispatch_op(enc, ShaderOp::BlurH, &src.view, &self.dummy.view, &self.dummy.view, &tmp1, &uh);
1328 let mut uv = UniformParams::default();1380 let mut uv = UniformParams::default();
1329 uv.p[0] = [0.0, 1.0, ry, 0.0];1381 uv.p[0] = [0.0, 1.0, ry, 0.0];
1330 uv.p[1] = [1.0, 0.0, 0.0, 0.0];1382 uv.p[1] = [1.0, 0.0, 0.0, clamp_edges];
1331 self.dispatch_op(enc, ShaderOp::BlurV, &tmp1.view, &self.dummy.view, &self.dummy.view, &tmp2, &uv);1383 self.dispatch_op(enc, ShaderOp::BlurV, &tmp1.view, &self.dummy.view, &self.dummy.view, &tmp2, &uv);
13321384
1333 let dst = self.alloc_tex_like(&src, src.depth, key);1385 let dst = self.alloc_tex_like(&src, src.depth, key);
...@@ -2627,4 +2679,110 @@ mod tests {...@@ -2627,4 +2679,110 @@ mod tests {
2627 let got_tuned = eng.readback_rgba(&dst_tuned).expect("readback tuned");2679 let got_tuned = eng.readback_rgba(&dst_tuned).expect("readback tuned");
2628 assert_eq!(got_ref, got_tuned);2680 assert_eq!(got_ref, got_tuned);
2629 }2681 }
2682
2683 #[test]
2684 fn blur_process_toggles_channel_off_and_multiply_by_mask() {
2685 let comp_src = r#"{
2686 Tools = ordered() {
2687 Src = Background {
2688 Inputs = {
2689 Width = Input { Value = 64, }, Height = Input { Value = 64, },
2690 Type = Input { Value = FuID { "Horizontal" }, },
2691 TopLeftRed = Input { Value = 1.0, }, TopLeftGreen = Input { Value = 1.0, }, TopLeftBlue = Input { Value = 1.0, }, TopLeftAlpha = Input { Value = 1.0, },
2692 TopRightRed = Input { Value = 0.0, }, TopRightGreen = Input { Value = 0.0, }, TopRightBlue = Input { Value = 0.0, }, TopRightAlpha = Input { Value = 1.0, },
2693 },
2694 },
2695 Mask = RectangleMask {
2696 Inputs = { MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, }, Width = Input { Value = 0.5, }, Height = Input { Value = 0.5, }, },
2697 },
2698 BlurSkip = Blur {
2699 Inputs = {
2700 Input = Input { SourceOp = "Src", Source = "Output", },
2701 XBlurSize = Input { Value = 200.0, },
2702 ProcessRed = Input { Value = 0, },
2703 Blue = Input { Value = 0, },
2704 },
2705 },
2706 BlurMbm = Blur {
2707 Inputs = {
2708 Input = Input { SourceOp = "Src", Source = "Output", },
2709 EffectMask = Input { SourceOp = "Mask", Source = "Mask", },
2710 MultiplyByMask = Input { Value = 1, },
2711 XBlurSize = Input { Value = 200.0, },
2712 ProcessRed = Input { Value = 0, },
2713 Blue = Input { Value = 0, },
2714 },
2715 },
2716 }
2717 }"#;
2718 let comp = Comp::from_fusion_str(comp_src).expect("parse Blur toggles comp");
2719 let Ok(mut eng) = Engine::new_headless() else { return };
2720 let (ts, _) = eng.evaluate(&comp, comp.find_node("Src").unwrap(), 0.0);
2721 let ps = eng.readback_rgba(&ts).unwrap();
2722 let (tb, _) = eng.evaluate(&comp, comp.find_node("BlurSkip").unwrap(), 0.0);
2723 let pb = eng.readback_rgba(&tb).unwrap();
2724 // Left edge (x=0, y=32): R (ProcessRed=0) and B (Blue=0) keep source; G is blurred down significantly.
2725 let left = (32 * 64) * 4;
2726 assert_eq!(pb[left], ps[left], "ProcessRed=0 must keep unblurred R");
2727 assert_eq!(pb[left + 2], ps[left + 2], "Blue=0 must keep unblurred B");
2728 assert!(u16::from(pb[left + 1]) + 10 < u16::from(ps[left + 1]), "G must be blurred ({} vs {})", pb[left + 1], ps[left + 1]);
2729
2730 let (tm, _) = eng.evaluate(&comp, comp.find_node("BlurMbm").unwrap(), 0.0);
2731 let pm = eng.readback_rgba(&tm).unwrap();
2732 let center = (32 * 64 + 32) * 4;
2733 assert_eq!(pm[center], 0, "MultiplyByMask=1 + ProcessRed=0 clears R to 0");
2734 assert!(pm[center + 2] > 100, "MultiplyByMask=1 + Blue=0 preserves source B inside mask: {}", pm[center + 2]);
2735 assert_eq!(&pm[left..left + 4], &[0, 0, 0, 0], "MultiplyByMask=1 zeroes outside mask");
2736 }
2737
2738 #[test]
2739 fn sharpen_lock_xy_unlocked_and_border_clamping() {
2740 let comp_src = r#"{
2741 Tools = ordered() {
2742 HStep = Background {
2743 Inputs = {
2744 Width = Input { Value = 64, }, Height = Input { Value = 64, },
2745 Type = Input { Value = FuID { "Horizontal" }, },
2746 TopLeftRed = Input { Value = 0.8, }, TopLeftGreen = Input { Value = 0.8, }, TopLeftBlue = Input { Value = 0.8, }, TopLeftAlpha = Input { Value = 1.0, },
2747 TopRightRed = Input { Value = 0.2, }, TopRightGreen = Input { Value = 0.2, }, TopRightBlue = Input { Value = 0.2, }, TopRightAlpha = Input { Value = 1.0, },
2748 },
2749 },
2750 VStep = Background {
2751 Inputs = {
2752 Width = Input { Value = 64, }, Height = Input { Value = 64, },
2753 Type = Input { Value = FuID { "Vertical" }, },
2754 TopLeftRed = Input { Value = 0.8, }, TopLeftGreen = Input { Value = 0.8, }, TopLeftBlue = Input { Value = 0.8, }, TopLeftAlpha = Input { Value = 1.0, },
2755 BottomLeftRed = Input { Value = 0.2, }, BottomLeftGreen = Input { Value = 0.2, }, BottomLeftBlue = Input { Value = 0.2, }, BottomLeftAlpha = Input { Value = 1.0, },
2756 },
2757 },
2758 ShH = Sharpen {
2759 Inputs = {
2760 Input = Input { SourceOp = "HStep", Source = "Output", },
2761 LockXY = Input { Value = 0, },
2762 XAmount = Input { Value = 60.0, },
2763 },
2764 },
2765 ShV = Sharpen {
2766 Inputs = {
2767 Input = Input { SourceOp = "VStep", Source = "Output", },
2768 LockXY = Input { Value = 0, },
2769 XAmount = Input { Value = 60.0, },
2770 },
2771 },
2772 }
2773 }"#;
2774 let comp = Comp::from_fusion_str(comp_src).expect("parse Sharpen LockXY comp");
2775 let Ok(mut eng) = Engine::new_headless() else { return };
2776 let (th, _) = eng.evaluate(&comp, comp.find_node("ShH").unwrap(), 0.0);
2777 let ph = eng.readback_rgba(&th).unwrap();
2778 let (tv, _) = eng.evaluate(&comp, comp.find_node("ShV").unwrap(), 0.0);
2779 let pv = eng.readback_rgba(&tv).unwrap();
2780 // Unlocked LockXY=0 with XAmount=60 (YAmount defaults to 1.0): horizontal gradient at x=0 overshoots,
2781 // while vertical gradient at y=0 stays unsharpened (and Frame clamping avoids black-border halo).
2782 let h_left = ph[(32 * 64) * 4];
2783 let v_top = pv[32 * 4];
2784 assert!(h_left >= 210, "large XAmount should sharpen horizontal gradient (got {h_left})");
2785 assert!(h_left > v_top + 4, "unlocked LockXY=0 must sharpen X ({h_left}) more than default Y ({v_top})");
2786 assert!((v_top as i32 - 203).abs() <= 2, "default YAmount=1.0 with LockXY=0 should keep vertical edge near 203 (got {v_top})");
2787 }
2630}2788}
crates/engine/src/eval/color.rs+105
...@@ -58,6 +58,16 @@ impl CcLevels {...@@ -58,6 +58,16 @@ impl CcLevels {
58 out_scale *= out_span;58 out_scale *= out_span;
59 out_offset = out_offset * out_span + l.out_low;59 out_offset = out_offset * out_span + l.out_low;
60 }60 }
61 // Curve table is uploaded as Rgba16Float (max 65504); when `low >= high` collapses `span` to
62 // `1e-5`, `in_scale` reaches 1e5+ and overflows f16 to Inf (producing Inf - Inf = NaN in the
63 // shader). Rescaling `(in_scale, in_offset)` together keeps the exact step threshold
64 // `-in_offset / in_scale` while staying finite in f16.
65 let max_in = in_scale.abs().max(in_offset.abs());
66 if max_in > 4096.0 {
67 let s = 4096.0 / max_in;
68 in_scale *= s;
69 in_offset *= s;
70 }
61 [[contrast, slope, offset, bright], [in_scale, in_offset, gamma, 0.0], [out_scale, out_offset, 0.0, 0.0]]71 [[contrast, slope, offset, bright], [in_scale, in_offset, gamma, 0.0], [out_scale, out_offset, 0.0, 0.0]]
62 }72 }
63}73}
...@@ -2125,5 +2135,100 @@ mod tests {...@@ -2125,5 +2135,100 @@ mod tests {
2125 }2135 }
2126 assert!(eng.pipelines.borrow().lut_misses <= 83);2136 assert!(eng.pipelines.borrow().lut_misses <= 83);
2127 }2137 }
2138
2139 #[test]
2140 fn color_corrector_inverted_low_high_levels_does_not_overflow_f16() {
2141 // When MasterGreenLow (0.675) > MasterGreenHigh (0.08), span collapses without overflowing
2142 // Rgba16Float curve storage: green above Low maps to 255, green below Low maps to 0.
2143 let comp_src = r#"{
2144 Tools = ordered() {
2145 Bright = Background {
2146 Inputs = {
2147 Width = Input { Value = 16, }, Height = Input { Value = 16, },
2148 TopLeftRed = Input { Value = 0.8, },
2149 TopLeftGreen = Input { Value = 0.85, },
2150 TopLeftBlue = Input { Value = 0.8, },
2151 TopLeftAlpha = Input { Value = 1.0, },
2152 },
2153 },
2154 Dark = Background {
2155 Inputs = {
2156 Width = Input { Value = 16, }, Height = Input { Value = 16, },
2157 TopLeftRed = Input { Value = 0.8, },
2158 TopLeftGreen = Input { Value = 0.25, },
2159 TopLeftBlue = Input { Value = 0.8, },
2160 TopLeftAlpha = Input { Value = 1.0, },
2161 },
2162 },
2163 CCBright = ColorCorrector {
2164 Inputs = {
2165 Input = Input { SourceOp = "Bright", Source = "Output", },
2166 MasterGreenLow = Input { Value = 0.67557, },
2167 MasterGreenHigh = Input { Value = 0.0827, },
2168 MasterBlueOutputHigh = Input { Value = 0.5, },
2169 },
2170 },
2171 CCDark = ColorCorrector {
2172 Inputs = {
2173 Input = Input { SourceOp = "Dark", Source = "Output", },
2174 MasterGreenLow = Input { Value = 0.67557, },
2175 MasterGreenHigh = Input { Value = 0.0827, },
2176 MasterBlueOutputHigh = Input { Value = 0.5, },
2177 },
2178 },
2179 }
2180 }"#;
2181 let comp = Comp::from_fusion_str(comp_src).expect("parse CC inverted levels comp");
2182 let Ok(mut eng) = Engine::new_headless() else { return };
2183 let (tb, _) = eng.evaluate(&comp, comp.find_node("CCBright").unwrap(), 0.0);
2184 let pb = eng.readback_rgba(&tb).unwrap();
2185 assert_eq!(pb[1], 255, "green above MasterGreenLow must saturate to 255, not NaN->0: {:?}", &pb[..4]);
2186 assert!((pb[2] as i32 - 102).abs() <= 2, "MasterBlueOutputHigh=0.5 scales 0.8->0.4 (~102), got {}", pb[2]);
2187
2188 let (td, _) = eng.evaluate(&comp, comp.find_node("CCDark").unwrap(), 0.0);
2189 let pd = eng.readback_rgba(&td).unwrap();
2190 assert_eq!(pd[1], 0, "green below MasterGreenLow must map to 0: {:?}", &pd[..4]);
2191 }
2192
2193 #[test]
2194 fn color_gain_balance_before_additive_tonal_hue_and_process_channels() {
2195 // Matches Fusion ColorGain f11 behavior: MidRedCyan balance runs before additive tonal
2196 // DarkHue/MidHue/HighHue shifts, while ProcessRed=0 and ProcessBlue=0 keep source R/B.
2197 let comp_src = r#"{
2198 Tools = ordered() {
2199 Src = Background {
2200 Inputs = {
2201 Width = Input { Value = 16, }, Height = Input { Value = 16, },
2202 TopLeftRed = Input { Value = 0.6157, },
2203 TopLeftGreen = Input { Value = 0.902, },
2204 TopLeftBlue = Input { Value = 0.3373, },
2205 TopLeftAlpha = Input { Value = 1.0, },
2206 },
2207 },
2208 CG = ColorGain {
2209 Inputs = {
2210 Input = Input { SourceOp = "Src", Source = "Output", },
2211 ProcessRed = Input { Value = 0, },
2212 ProcessBlue = Input { Value = 0, },
2213 GainGreen = Input { Value = 3.26722, },
2214 GammaGreen = Input { Value = 4.43987, },
2215 LiftRed = Input { Value = -0.26756, },
2216 MidRedCyan = Input { Value = -0.6691, },
2217 DarkHue = Input { Value = 0.70687, },
2218 MidHue = Input { Value = -0.5539, },
2219 HighHue = Input { Value = -0.51854, },
2220 },
2221 },
2222 }
2223 }"#;
2224 let comp = Comp::from_fusion_str(comp_src).expect("parse ColorGain balance+hue comp");
2225 let Ok(mut eng) = Engine::new_headless() else { return };
2226 let (t, _) = eng.evaluate(&comp, comp.find_node("CG").unwrap(), 0.0);
2227 let px = eng.readback_rgba(&t).unwrap();
2228 assert_eq!(px[0], 157, "ProcessRed=0 keeps source R=157");
2229 assert_eq!(px[2], 86, "ProcessBlue=0 keeps source B=86");
2230 // Fusion 21.1 GT at PlateA (64,64) = (157,230,86) gives G=46 (vs old broken G=105).
2231 assert!((px[1] as i32 - 46).abs() <= 3, "expected Fusion ColorGain G~46, got {}", px[1]);
2232 }
2128}2233}
21292234
crates/engine/src/eval/composite.rs+101-5
...@@ -986,15 +986,22 @@ impl Engine {...@@ -986,15 +986,22 @@ impl Engine {
986 "Luminosity" => 24.0,986 "Luminosity" => 24.0,
987 _ => 0.0,987 _ => 0.0,
988 };988 };
989 let nearest = if comp.eval_number(node, "FilterMethod", time, 2.0, &self.expr) < 0.5 { 1.0 } else { 0.0 };989 let filter_method = comp.eval_number(node, "FilterMethod", time, 2.0, &self.expr).round() as i32;
990 let nearest = if filter_method == 0 { 1.0 } else { 0.0 };
991 let cubic_filter = if filter_method == 4 { 1.0 } else { 0.0 };
990 let op_id = match node.static_str("Operator").unwrap_or("Over") {992 let op_id = match node.static_str("Operator").unwrap_or("Over") {
991 "Under" => 1.0,993 "Under" => 1.0,
992 "In" => 2.0,994 "In" => 2.0,
993 "Held Out" | "HeldOut" => 3.0,995 "Held Out" | "HeldOut" => 3.0,
994 "Atop" => 4.0,996 "Atop" => 4.0,
995 "XOr" => 5.0,997 "XOr" => 5.0,
998 "Conjoint" => 6.0,
999 "Disjoint" => 7.0,
1000 "Mask" => 8.0,
1001 "Stencil" => 9.0,
996 _ => 0.0, // Over1002 _ => 0.0, // Over
997 };1003 };
1004 let skip = self.process_skip(comp, node, time);
9981005
999 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(bg.canvas_size), enc, visiting, stats);1006 let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(bg.canvas_size), enc, visiting, stats);
1000 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);1007 let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash);
...@@ -1030,7 +1037,7 @@ impl Engine {...@@ -1030,7 +1037,7 @@ impl Engine {
1030 if edges < 0.51037 if edges < 0.5
1031 && mask.is_none()1038 && mask.is_none()
1032 && mode_id == 0.01039 && mode_id == 0.0
1033 && (op_id == 0.0 || op_id == 1.0 || op_id == 5.0)1040 && (op_id == 0.0 || op_id == 1.0 || op_id == 5.0 || op_id == 6.0 || op_id == 7.0)
1034 && bg.tex_scale >= 0.991041 && bg.tex_scale >= 0.99
1035 && eff_size > 1e-41042 && eff_size > 1e-4
1036 {1043 {
...@@ -1134,6 +1141,8 @@ impl Engine {...@@ -1134,6 +1141,8 @@ impl Engine {
1134 (bg.origin[1] - dst_origin[1]) as f32,1141 (bg.origin[1] - dst_origin[1]) as f32,
1135 ];1142 ];
1136 u.p[7] = [if mbm { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0];1143 u.p[7] = [if mbm { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0];
1144 u.p[8] = [0.0, cubic_filter, 0.0, 0.0];
1145 u.p[9] = skip;
11371146
1138 let bg_has_z = bg.aux.as_ref().and_then(|a| a.z.as_ref()).is_some();1147 let bg_has_z = bg.aux.as_ref().and_then(|a| a.z.as_ref()).is_some();
1139 let fg_has_z = fg.aux.as_ref().and_then(|a| a.z.as_ref()).is_some();1148 let fg_has_z = fg.aux.as_ref().and_then(|a| a.z.as_ref()).is_some();
...@@ -1145,7 +1154,7 @@ impl Engine {...@@ -1145,7 +1154,7 @@ impl Engine {
1145 "Merge",1154 "Merge",
1146 dst_size,1155 dst_size,
1147 &[bg.content_hash, fg.content_hash, mask_hash],1156 &[bg.content_hash, fg.content_hash, mask_hash],
1148 bytemuck::cast_slice(&u.p[..8]),1157 bytemuck::cast_slice(&u.p[..10]),
1149 );1158 );
1150 if let Some(hit) = self.cache.get(key, node.id, frame_i) {1159 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
1151 stats.cache_hits += 1;1160 stats.cache_hits += 1;
...@@ -1182,14 +1191,15 @@ impl Engine {...@@ -1182,14 +1191,15 @@ impl Engine {
1182 if bg_has_z { 1.0 } else { 0.0 },1191 if bg_has_z { 1.0 } else { 0.0 },
1183 if fg_has_z { 1.0 } else { 0.0 },1192 if fg_has_z { 1.0 } else { 0.0 },
1184 ];1193 ];
1185 u.p[8] = [fg_z_offset, 0.0, 0.0, 0.0];1194 u.p[8] = [fg_z_offset, cubic_filter, 0.0, 0.0];
1195 u.p[9] = skip;
11861196
1187 let key = self.make_key(1197 let key = self.make_key(
1188 node,1198 node,
1189 "MergeDepth",1199 "MergeDepth",
1190 dst_size,1200 dst_size,
1191 &[bg.content_hash, fg.content_hash, mask_hash],1201 &[bg.content_hash, fg.content_hash, mask_hash],
1192 bytemuck::cast_slice(&u.p[..9]),1202 bytemuck::cast_slice(&u.p[..10]),
1193 );1203 );
1194 if let Some(hit) = self.cache.get(key, node.id, frame_i) {1204 if let Some(hit) = self.cache.get(key, node.id, frame_i) {
1195 stats.cache_hits += 1;1205 stats.cache_hits += 1;
...@@ -2413,4 +2423,90 @@ mod tests {...@@ -2413,4 +2423,90 @@ mod tests {
2413 assert_eq!(mc_tex.size, [260, 260], "Fuse.XfChroma must preserve full upstream DoD");2423 assert_eq!(mc_tex.size, [260, 260], "Fuse.XfChroma must preserve full upstream DoD");
2414 eng.queue.submit(Some(enc.finish()));2424 eng.queue.submit(Some(enc.finish()));
2415 }2425 }
2426
2427 #[test]
2428 fn merge_conjoint_disjoint_mask_stencil_and_process_channels() {
2429 // BG = premult (0.4, 0.2, 0.8, 0.8), FG = premult (0.6, 0.3, 0.0, 0.6)
2430 // - Conjoint: k = 1 - 0.6/0.8 = 0.25 -> (0.7, 0.35, 0.2, 0.8) = (179, 89, 51, 204)
2431 // - Disjoint: k = min(1, (1 - 0.6)/0.8) = 0.5 -> (0.8, 0.4, 0.4, 1.0) = (204, 102, 102, 255)
2432 // - Mask: bg * fg.a = (0.24, 0.12, 0.48, 0.48) = (61, 31, 122, 122)
2433 // - Stencil: bg * (1 - fg.a) = (0.16, 0.08, 0.32, 0.32) = (41, 20, 82, 82)
2434 // - ProcessBlue=0, ProcessAlpha=0 on Conjoint keeps BG blue (204) and BG alpha (204)
2435 let comp_src = r#"{
2436 Tools = ordered() {
2437 Bg = Background {
2438 Inputs = {
2439 Width = Input { Value = 16, }, Height = Input { Value = 16, },
2440 TopLeftRed = Input { Value = 0.4, },
2441 TopLeftGreen = Input { Value = 0.2, },
2442 TopLeftBlue = Input { Value = 0.8, },
2443 TopLeftAlpha = Input { Value = 0.8, },
2444 },
2445 },
2446 Fg = Background {
2447 Inputs = {
2448 Width = Input { Value = 16, }, Height = Input { Value = 16, },
2449 TopLeftRed = Input { Value = 0.6, },
2450 TopLeftGreen = Input { Value = 0.3, },
2451 TopLeftBlue = Input { Value = 0.0, },
2452 TopLeftAlpha = Input { Value = 0.6, },
2453 },
2454 },
2455 MConj = Merge {
2456 Inputs = {
2457 Operator = Input { Value = FuID { "Conjoint" }, },
2458 Background = Input { SourceOp = "Bg", Source = "Output", },
2459 Foreground = Input { SourceOp = "Fg", Source = "Output", },
2460 },
2461 },
2462 MDisj = Merge {
2463 Inputs = {
2464 Operator = Input { Value = FuID { "Disjoint" }, },
2465 Background = Input { SourceOp = "Bg", Source = "Output", },
2466 Foreground = Input { SourceOp = "Fg", Source = "Output", },
2467 },
2468 },
2469 MMask = Merge {
2470 Inputs = {
2471 Operator = Input { Value = FuID { "Mask" }, },
2472 Background = Input { SourceOp = "Bg", Source = "Output", },
2473 Foreground = Input { SourceOp = "Fg", Source = "Output", },
2474 },
2475 },
2476 MSten = Merge {
2477 Inputs = {
2478 Operator = Input { Value = FuID { "Stencil" }, },
2479 Background = Input { SourceOp = "Bg", Source = "Output", },
2480 Foreground = Input { SourceOp = "Fg", Source = "Output", },
2481 },
2482 },
2483 MProc = Merge {
2484 Inputs = {
2485 Operator = Input { Value = FuID { "Conjoint" }, },
2486 ProcessBlue = Input { Value = 0, },
2487 ProcessAlpha = Input { Value = 0, },
2488 Background = Input { SourceOp = "Bg", Source = "Output", },
2489 Foreground = Input { SourceOp = "Fg", Source = "Output", },
2490 },
2491 },
2492 }
2493 }"#;
2494 let comp = Comp::from_fusion_str(comp_src).expect("parse merge ops comp");
2495 let Ok(mut eng) = Engine::new_headless() else { return };
2496 let px_of = |eng: &mut Engine, name: &str| -> [u8; 4] {
2497 let (tex, _) = eng.evaluate(&comp, comp.find_node(name).unwrap(), 0.0);
2498 let px = eng.readback_rgba(&tex).unwrap();
2499 [px[0], px[1], px[2], px[3]]
2500 };
2501 let close = |got: [u8; 4], want: [u8; 4], label: &str| {
2502 for c in 0..4 {
2503 assert!(got[c].abs_diff(want[c]) <= 2, "{label} ch {c}: got {got:?}, want {want:?}");
2504 }
2505 };
2506 close(px_of(&mut eng, "MConj"), [179, 89, 51, 204], "Conjoint");
2507 close(px_of(&mut eng, "MDisj"), [204, 102, 102, 255], "Disjoint");
2508 close(px_of(&mut eng, "MMask"), [61, 31, 122, 122], "Mask");
2509 close(px_of(&mut eng, "MSten"), [41, 20, 82, 82], "Stencil");
2510 close(px_of(&mut eng, "MProc"), [179, 89, 204, 204], "Conjoint+ProcessBlue/Alpha=0");
2511 }
2416}2512}
crates/engine/src/eval/masks.rs+104-5
...@@ -12,6 +12,23 @@ pub(super) struct MaskPost<'a> {...@@ -12,6 +12,23 @@ pub(super) struct MaskPost<'a> {
12}12}
1313
14impl Engine {14impl Engine {
15 /// Maps Fusion `FitMask` / `FitInput` FuID or numeric enum to shader mode id:
16 /// `0=Crop, 1=Stretch, 2=Inside, 3=Width, 4=Height, 5=Outside`.
17 pub(super) fn fit_mask_mode(comp: &Comp, node: &Node, input: &str, time: f64, expr: &ExprEngine) -> f32 {
18 if let Some(s) = node.static_str(input) {
19 return match s {
20 "Stretch" => 1.0,
21 "Inside" => 2.0,
22 "Width" => 3.0,
23 "Height" => 4.0,
24 "Outside" => 5.0,
25 _ => 0.0, // Crop
26 };
27 }
28 let n = comp.eval_number(node, input, time, 0.0, expr).round().clamp(0.0, 5.0);
29 n as f32
30 }
31
15 pub(super) fn paint_mode_id(node: &Node) -> f32 {32 pub(super) fn paint_mode_id(node: &Node) -> f32 {
16 match node.static_str("PaintMode").unwrap_or("Merge") {33 match node.static_str("PaintMode").unwrap_or("Merge") {
17 "Add" => 1.0,34 "Add" => 1.0,
...@@ -497,15 +514,20 @@ impl Engine {...@@ -497,15 +514,20 @@ impl Engine {
497 "Saturation" => 5.0,514 "Saturation" => 5.0,
498 "Lightness" => 6.0,515 "Lightness" => 6.0,
499 "Hue" => 7.0,516 "Hue" => 7.0,
517 "Coverage" => 8.0,
500 _ => 0.0, // Alpha (Fusion default)518 _ => 0.0, // Alpha (Fusion default)
501 };519 };
502 let low = comp.eval_number(node, "Low", time, 0.0, &self.expr) as f32;520 let low = comp.eval_number(node, "Low", time, 0.0, &self.expr) as f32;
503 let high = comp.eval_number(node, "High", time, 1.0, &self.expr) as f32;521 let high = comp.eval_number(node, "High", time, 1.0, &self.expr) as f32;
522 let clip_black = comp.eval_number(node, "ClipBlack", time, 1.0, &self.expr) > 0.5;
523 let clip_white = comp.eval_number(node, "ClipWhite", time, 1.0, &self.expr) > 0.5;
524 let no_clip_black = if clip_black { 0.0 } else { 1.0 };
525 let no_clip_white = if clip_white { 0.0 } else { 1.0 };
504 let soft = comp.eval_number(node, "SoftEdge", time, 0.0, &self.expr).max(0.0) as f32;526 let soft = comp.eval_number(node, "SoftEdge", time, 0.0, &self.expr).max(0.0) as f32;
505 let invert = if comp.eval_number(node, "Invert", time, 0.0, &self.expr) > 0.5 { 1.0 } else { 0.0 };527 let invert = if comp.eval_number(node, "Invert", time, 0.0, &self.expr) > 0.5 { 1.0 } else { 0.0 };
506 let level = comp.eval_number(node, "Level", time, 1.0, &self.expr) as f32;528 let level = comp.eval_number(node, "Level", time, 1.0, &self.expr) as f32;
507 let center = comp.eval_point(node, "Center", time, [0.5, 0.5], &self.expr);529 let center = comp.eval_point(node, "Center", time, [0.5, 0.5], &self.expr);
508 let fit_stretch = if comp.eval_number(node, "FitInput", time, 0.0, &self.expr) > 0.5 { 1.0 } else { 0.0 };530 let fit_mode = Self::fit_mask_mode(comp, node, "FitInput", time, &self.expr);
509 let is_box = Self::mask_filter_is_box(node);531 let is_box = Self::mask_filter_is_box(node);
510 let clamp_edges = Self::mask_clamp_edges(node);532 let clamp_edges = Self::mask_clamp_edges(node);
511 let prev_mask = self.bitmap_or_wand_prev_mask(comp, node, time, size, enc, visiting, stats);533 let prev_mask = self.bitmap_or_wand_prev_mask(comp, node, time, size, enc, visiting, stats);
...@@ -521,8 +543,8 @@ impl Engine {...@@ -521,8 +543,8 @@ impl Engine {
521 size,543 size,
522 &[src.content_hash, prev_hash],544 &[src.content_hash, prev_hash],
523 &[545 &[
524 ch_id, low, high, soft, invert, level,546 ch_id, low, high, no_clip_black, no_clip_white, soft, invert, level,
525 center[0] as f32, center[1] as f32, fit_stretch,547 center[0] as f32, center[1] as f32, fit_mode,
526 if is_box { 1.0 } else { 0.0 }, clamp_edges, paint_mode,548 if is_box { 1.0 } else { 0.0 }, clamp_edges, paint_mode,
527 ],549 ],
528 );550 );
...@@ -538,9 +560,9 @@ impl Engine {...@@ -538,9 +560,9 @@ impl Engine {
538560
539 let raw_tex = create_texture(&self.device, size, if need_post { 0 } else { key });561 let raw_tex = create_texture(&self.device, size, if need_post { 0 } else { key });
540 let mut u = UniformParams::default();562 let mut u = UniformParams::default();
541 u.p[0] = [ch_id, low, high, fit_stretch];563 u.p[0] = [ch_id, low, high, fit_mode];
542 u.p[1] = [center[0] as f32, center[1] as f32, src.origin[0] as f32, src.origin[1] as f32];564 u.p[1] = [center[0] as f32, center[1] as f32, src.origin[0] as f32, src.origin[1] as f32];
543 u.p[2] = [src.canvas_size[0] as f32, src.canvas_size[1] as f32, 0.0, 0.0];565 u.p[2] = [src.canvas_size[0] as f32, src.canvas_size[1] as f32, no_clip_black, no_clip_white];
544 self.dispatch_op(enc, ShaderOp::BitmapMask, &src.view, &self.dummy.view, &self.dummy.view, &raw_tex, &u);566 self.dispatch_op(enc, ShaderOp::BitmapMask, &src.view, &self.dummy.view, &self.dummy.view, &raw_tex, &u);
545567
546 let out_tex = if need_post {568 let out_tex = if need_post {
...@@ -1367,4 +1389,81 @@ mod tests {...@@ -1367,4 +1389,81 @@ mod tests {
1367 let px = eng.readback_rgba(&tex).unwrap();1389 let px = eng.readback_rgba(&tex).unwrap();
1368 assert!(px.chunks_exact(4).any(|c| c[3] > 200), "clamped PolylineMask must rasterize cleanly instead of clearing to transparent");1390 assert!(px.chunks_exact(4).any(|c| c[3] > 200), "clamped PolylineMask must rasterize cleanly instead of clearing to transparent");
1369 }1391 }
1392
1393 #[test]
1394 fn bitmap_mask_fit_input_modes_and_coverage_channel() {
1395 // Source is 16x32 solid white; destination mask is 32x16:
1396 // - Crop: 16px wide centered -> x=2 is 0, x=16 is 255
1397 // - Stretch / Width / Outside: fills full width -> x=2 is 255
1398 // - Inside / Height: uniform scale = 16/32 = 0.5 -> 8px wide centered (x=12..20) -> x=10 is 0, x=16 is 255
1399 // - Coverage channel: returns 0 mask cleanly
1400 let comp_src = r#"{
1401 Tools = ordered() {
1402 Src = Background {
1403 Inputs = {
1404 Width = Input { Value = 16, },
1405 Height = Input { Value = 32, },
1406 TopLeftRed = Input { Value = 1, },
1407 TopLeftGreen = Input { Value = 1, },
1408 TopLeftBlue = Input { Value = 1, },
1409 TopLeftAlpha = Input { Value = 1, },
1410 },
1411 },
1412 BCrop = BitmapMask {
1413 Inputs = {
1414 OutputSize = Input { Value = FuID { "Custom" }, },
1415 MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, },
1416 FitInput = Input { Value = FuID { "Crop" }, },
1417 Image = Input { SourceOp = "Src", Source = "Output", },
1418 },
1419 },
1420 BStretch = BitmapMask {
1421 Inputs = {
1422 OutputSize = Input { Value = FuID { "Custom" }, },
1423 MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, },
1424 FitInput = Input { Value = FuID { "Stretch" }, },
1425 Image = Input { SourceOp = "Src", Source = "Output", },
1426 },
1427 },
1428 BInside = BitmapMask {
1429 Inputs = {
1430 OutputSize = Input { Value = FuID { "Custom" }, },
1431 MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, },
1432 FitInput = Input { Value = FuID { "Inside" }, },
1433 Image = Input { SourceOp = "Src", Source = "Output", },
1434 },
1435 },
1436 BOutside = BitmapMask {
1437 Inputs = {
1438 OutputSize = Input { Value = FuID { "Custom" }, },
1439 MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, },
1440 FitInput = Input { Value = FuID { "Outside" }, },
1441 Image = Input { SourceOp = "Src", Source = "Output", },
1442 },
1443 },
1444 BCov = BitmapMask {
1445 Inputs = {
1446 OutputSize = Input { Value = FuID { "Custom" }, },
1447 MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, },
1448 Channel = Input { Value = FuID { "Coverage" }, },
1449 Image = Input { SourceOp = "Src", Source = "Output", },
1450 },
1451 },
1452 }
1453 }"#;
1454 let comp = Comp::from_fusion_str(comp_src).expect("parse BitmapMask FitInput comp");
1455 let Ok(mut eng) = Engine::new_headless() else { return };
1456 let sample = |eng: &mut Engine, name: &str, x: usize, y: usize| -> u8 {
1457 let (tex, _) = eng.evaluate(&comp, comp.find_node(name).unwrap(), 0.0);
1458 let px = eng.readback_rgba(&tex).unwrap();
1459 px[(y * 32 + x) * 4 + 3]
1460 };
1461 assert_eq!(sample(&mut eng, "BCrop", 2, 8), 0);
1462 assert_eq!(sample(&mut eng, "BCrop", 10, 8), 255);
1463 assert_eq!(sample(&mut eng, "BStretch", 2, 8), 255);
1464 assert_eq!(sample(&mut eng, "BInside", 10, 8), 0);
1465 assert_eq!(sample(&mut eng, "BInside", 16, 8), 255);
1466 assert_eq!(sample(&mut eng, "BOutside", 2, 8), 255);
1467 assert_eq!(sample(&mut eng, "BCov", 16, 8), 0);
1468 }
1370}1469}
crates/engine/src/shaders/blur_glow.wgsl+16-6
...@@ -132,7 +132,8 @@ fn run_blur_pass(gid: vec2<u32>) {...@@ -132,7 +132,8 @@ fn run_blur_pass(gid: vec2<u32>) {
132 store_dst(vec2<i32>(gid.xy), out_col);132 store_dst(vec2<i32>(gid.xy), out_col);
133 return;133 return;
134 }134 }
135 if (has_mask && m <= 1e-6) {135 let mbm = u.p[2].y > 0.5;
136 if (has_mask && !mbm && m <= 1e-6) {
136 var orig: vec4<f32>;137 var orig: vec4<f32>;
137 if (u.p[2].x > 0.5) {138 if (u.p[2].x > 0.5) {
138 orig = textureLoad(src_b, vec2<i32>(gid.xy), 0);139 orig = textureLoad(src_b, vec2<i32>(gid.xy), 0);
...@@ -275,7 +276,9 @@ fn run_blur_pass(gid: vec2<u32>) {...@@ -275,7 +276,9 @@ fn run_blur_pass(gid: vec2<u32>) {
275 }276 }
276 }277 }
277278
278 if (u.p[1].y < 0.5 && u.p[1].x >= 1.0) {279 let skip = u.p[3] > vec4<f32>(0.5);
280 let off = u.p[4] > vec4<f32>(0.5);
281 if (u.p[1].y < 0.5 && u.p[1].x >= 1.0 && !any(skip) && !any(off)) {
279 store_dst(vec2<i32>(gid.xy), acc);282 store_dst(vec2<i32>(gid.xy), acc);
280 return;283 return;
281 }284 }
...@@ -283,8 +286,16 @@ fn run_blur_pass(gid: vec2<u32>) {...@@ -283,8 +286,16 @@ fn run_blur_pass(gid: vec2<u32>) {
283 if (u.p[2].x > 0.5) {286 if (u.p[2].x > 0.5) {
284 orig = textureLoad(src_b, vec2<i32>(gid.xy), 0);287 orig = textureLoad(src_b, vec2<i32>(gid.xy), 0);
285 }288 }
286 let out_col = mix(orig, acc, m);289 let eff = select(acc, orig, off);
287 store_dst(vec2<i32>(gid.xy), out_col);290 if (has_mask && mbm) {
291 let raw_mask = read_mask(gid.xy, dims, u.p[1].y, u.p[1].z);
292 let mixed = mix(orig, eff, clamp(u.p[1].x, 0.0, 1.0));
293 let clamped = select(mixed, clamp(mixed, vec4<f32>(0.0), vec4<f32>(1.0)), !HDR_FLOAT);
294 store_dst(vec2<i32>(gid.xy), select(clamped * raw_mask, vec4<f32>(0.0), skip));
295 return;
296 }
297 let out_col = mix(orig, eff, m);
298 store_dst(vec2<i32>(gid.xy), select(out_col, orig, skip));
288}299}
289300
290@compute @workgroup_size(8, 8)301@compute @workgroup_size(8, 8)
...@@ -328,8 +339,7 @@ fn glow_add_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -328,8 +339,7 @@ fn glow_add_op(@builtin(global_invocation_id) gid: vec3<u32>) {
328 let gain_rgb = cscale.xyz * gain;339 let gain_rgb = cscale.xyz * gain;
329 let gain_a = cscale.w * gain;340 let gain_a = cscale.w * gain;
330 let out_rgb = orig.rgb * (1.0 - gm_blur) + blurred.rgb * gain_rgb;341 let out_rgb = orig.rgb * (1.0 - gm_blur) + blurred.rgb * gain_rgb;
331 let raw_a = orig.a * (1.0 - gm_blur) + blurred.a * gain_a;342 let out_a = orig.a * (1.0 - gm_blur) + blurred.a * gain_a;
332 let out_a = select(clamp(raw_a, 0.0, 1.0), raw_a, HDR_FLOAT);
333 added = vec4<f32>(out_rgb, out_a);343 added = vec4<f32>(out_rgb, out_a);
334 if (u.p[1].z > 0.5) {344 if (u.p[1].z > 0.5) {
335 added = orig + added * (1.0 - orig.a);345 added = orig + added * (1.0 - orig.a);
crates/engine/src/shaders/color.wgsl+35-29
...@@ -674,8 +674,39 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -674,8 +674,39 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) {
674 rgb = mix(vec3<f32>(luma), rgb, sat);674 rgb = mix(vec3<f32>(luma), rgb, sat);
675 }675 }
676676
677 // Dark/Mid/High brightness shifts all channels by a Rec.601-luma-weighted amount; colour shifts weigh each channel by its own value.
678 // Fusion applies Balance (Brightness + RedCyan/GreenMagenta/BlueYellow) before tonal HSV Hue shifts.
679 let any_dmb = abs(u.p[7].w) > 1e-6 || abs(u.p[6].w) > 1e-6 || abs(u.p[5].w) > 1e-6;
680 if (any_dmb) {
681 let luma_v = clamp(dot(rgb, vec3<f32>(0.299, 0.587, 0.114)), 0.0, 1.0);
682 let wd = max(1.0 - 2.0 * luma_v, 0.0);
683 let wh = max(2.0 * luma_v - 1.0, 0.0);
684 rgb = rgb + vec3<f32>(u.p[7].w * wd + u.p[6].w * (1.0 - wd - wh) + u.p[5].w * wh);
685 }
686 let any_dmc = any(abs(u.p[7].xyz) > vec3<f32>(1e-6))
687 || any(abs(u.p[6].xyz) > vec3<f32>(1e-6))
688 || any(abs(u.p[5].xyz) > vec3<f32>(1e-6));
689 if (any_dmc) {
690 let sd = cg_axis_shift(u.p[7].xyz);
691 let sm = cg_axis_shift(u.p[6].xyz);
692 let sh = cg_axis_shift(u.p[5].xyz);
693 let rc = clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0));
694 let wr = cg_range_weights(rc.r);
695 let wg = cg_range_weights(rc.g);
696 let wb = cg_range_weights(rc.b);
697 rgb = rgb + vec3<f32>(
698 wr.x * sd.x + wr.y * sm.x + wr.z * sh.x,
699 wg.x * sd.y + wg.y * sm.y + wg.z * sh.y,
700 wb.x * sd.z + wb.y * sm.z + wb.z * sh.z,
701 );
702 if (!HDR_FLOAT) {
703 rgb = clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0));
704 }
705 }
706
677 // Tonal range weights using HSV Value max(r,g,b):707 // Tonal range weights using HSV Value max(r,g,b):
678 // Fusion ColorGain uses quadratic falloff u^2 on Dark/High and linear 1 - u_dark - u_high on Mid.708 // Fusion ColorGain uses quadratic falloff u^2 on Dark/High and linear 1 - u_dark - u_high on Mid,
709 // combining each tonal range's HSV shift additively from the base color: c + sum_i w_i * (hsv(h + dh_i) - c).
679 let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5;710 let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5;
680 if (any_hsv) {711 if (any_hsv) {
681 let c_pos = max(rgb, vec3<f32>(0.0));712 let c_pos = max(rgb, vec3<f32>(0.0));
...@@ -689,44 +720,19 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -689,44 +720,19 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3<u32>) {
689 var cur = c_pos;720 var cur = c_pos;
690 if (abs(dark_hue) > 1e-5 && w_dark > 1e-5) {721 if (abs(dark_hue) > 1e-5 && w_dark > 1e-5) {
691 let h_d = vec3<f32>(fract(base_hsv.x + dark_hue), base_hsv.y, base_hsv.z);722 let h_d = vec3<f32>(fract(base_hsv.x + dark_hue), base_hsv.y, base_hsv.z);
692 cur = mix(cur, hsv_to_rgb(h_d), w_dark);723 cur = cur + (hsv_to_rgb(h_d) - c_pos) * w_dark;
693 }724 }
694 if (abs(high_hue) > 1e-5 && w_high > 1e-5) {725 if (abs(high_hue) > 1e-5 && w_high > 1e-5) {
695 let h_h = vec3<f32>(fract(base_hsv.x + high_hue), base_hsv.y, base_hsv.z);726 let h_h = vec3<f32>(fract(base_hsv.x + high_hue), base_hsv.y, base_hsv.z);
696 cur = mix(cur, hsv_to_rgb(h_h), w_high);727 cur = cur + (hsv_to_rgb(h_h) - c_pos) * w_high;
697 }728 }
698 if (abs(mid_hue) > 1e-5 && w_mid > 1e-5) {729 if (abs(mid_hue) > 1e-5 && w_mid > 1e-5) {
699 let h_m = vec3<f32>(fract(base_hsv.x + mid_hue), base_hsv.y, base_hsv.z);730 let h_m = vec3<f32>(fract(base_hsv.x + mid_hue), base_hsv.y, base_hsv.z);
700 cur = mix(cur, hsv_to_rgb(h_m), w_mid);731 cur = cur + (hsv_to_rgb(h_m) - c_pos) * w_mid;
701 }732 }
702 rgb = cur;733 rgb = cur;
703 }734 }
704735
705 // Dark/Mid/High brightness shifts all channels by a Rec.601-luma-weighted amount; colour shifts weigh each channel by its own value.
706 let any_dmb = abs(u.p[7].w) > 1e-6 || abs(u.p[6].w) > 1e-6 || abs(u.p[5].w) > 1e-6;
707 if (any_dmb) {
708 let luma_v = clamp(dot(rgb, vec3<f32>(0.299, 0.587, 0.114)), 0.0, 1.0);
709 let wd = max(1.0 - 2.0 * luma_v, 0.0);
710 let wh = max(2.0 * luma_v - 1.0, 0.0);
711 rgb = rgb + vec3<f32>(u.p[7].w * wd + u.p[6].w * (1.0 - wd - wh) + u.p[5].w * wh);
712 }
713 let any_dmc = any(abs(u.p[7].xyz) > vec3<f32>(1e-6))
714 || any(abs(u.p[6].xyz) > vec3<f32>(1e-6))
715 || any(abs(u.p[5].xyz) > vec3<f32>(1e-6));
716 if (any_dmc) {
717 let sd = cg_axis_shift(u.p[7].xyz);
718 let sm = cg_axis_shift(u.p[6].xyz);
719 let sh = cg_axis_shift(u.p[5].xyz);
720 let wr = cg_range_weights(rgb.r);
721 let wg = cg_range_weights(rgb.g);
722 let wb = cg_range_weights(rgb.b);
723 rgb = rgb + vec3<f32>(
724 wr.x * sd.x + wr.y * sm.x + wr.z * sh.x,
725 wg.x * sd.y + wg.y * sm.y + wg.z * sh.y,
726 wb.x * sd.z + wb.y * sm.z + wb.z * sh.z,
727 );
728 }
729
730 if (prediv) {736 if (prediv) {
731 rgb = rgb * a_new;737 rgb = rgb * a_new;
732 }738 }
crates/engine/src/shaders/composite.wgsl+29-6
...@@ -251,8 +251,8 @@ fn merge_fetch_edges(tex: texture_2d<f32>, ix: i32, iy: i32, dims: vec2<i32>, ed...@@ -251,8 +251,8 @@ fn merge_fetch_edges(tex: texture_2d<f32>, ix: i32, iy: i32, dims: vec2<i32>, ed
251 }251 }
252}252}
253253
254fn merge_sample_fg(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32, dod: vec4<f32>, nearest: bool, size: f32) -> vec4<f32> {254fn merge_sample_fg(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32, dod: vec4<f32>, nearest: bool, size: f32, force_cubic: bool) -> vec4<f32> {
255 if (nearest || size >= 0.999) {255 if (nearest || (size >= 0.999 && !force_cubic)) {
256 return sample_dod_edges_mode(tex, fu, edges, dod, nearest);256 return sample_dod_edges_mode(tex, fu, edges, dod, nearest);
257 }257 }
258 let itdims = vec2<i32>(textureDimensions(tex));258 let itdims = vec2<i32>(textureDimensions(tex));
...@@ -359,7 +359,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -359,7 +359,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {
359 let mode = i32(u.p[1].z + 0.5);359 let mode = i32(u.p[1].z + 0.5);
360 let edges = i32(u.p[1].w + 0.5);360 let edges = i32(u.p[1].w + 0.5);
361 let nearest = u.p[2].z > 0.5;361 let nearest = u.p[2].z > 0.5;
362 let op = i32(u.p[2].w + 0.5); // 0=Over, 1=Under, 2=In, 3=HeldOut, 4=Atop, 5=XOr362 let cubic_filter = u.p[8].y > 0.5;
363 let skip = u.p[9];
364 let has_skip = any(skip > vec4<f32>(0.5));
365 let op = i32(u.p[2].w + 0.5); // 0=Over, 1=Under, 2=In, 3=HeldOut, 4=Atop, 5=XOr, 6=Conjoint, 7=Disjoint, 8=Mask, 9=Stencil
363 let has_mask = u.p[3].x;366 let has_mask = u.p[3].x;
364 let burn_in = u.p[6].x;367 let burn_in = u.p[6].x;
365 let sub_add = u.p[6].y;368 let sub_add = u.p[6].y;
...@@ -379,10 +382,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -379,10 +382,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {
379 if (u.p[2].x > 0.5) { fg_fu.x = 1.0 - fg_fu.x; }382 if (u.p[2].x > 0.5) { fg_fu.x = 1.0 - fg_fu.x; }
380 if (u.p[2].y > 0.5) { fg_fu.y = 1.0 - fg_fu.y; }383 if (u.p[2].y > 0.5) { fg_fu.y = 1.0 - fg_fu.y; }
381384
382 let fg = merge_sample_fg(src_b, fg_fu, edges, u.p[4], nearest, size);385 let fg = merge_sample_fg(src_b, fg_fu, edges, u.p[4], nearest, size, cubic_filter);
383386
384 // Fast path for the common Normal + Over + Gain=1.0 + no mask + Blend=1.0 case.387 // Fast path for the common Normal + Over + Gain=1.0 + no mask + Blend=1.0 + all channels processed case.
385 if (mode == 0 && op == 0 && gain >= 1.0 && has_mask < 0.5 && blend >= 1.0 && !mbm && burn_in == 0.0) {388 if (mode == 0 && op == 0 && gain >= 1.0 && has_mask < 0.5 && blend >= 1.0 && !mbm && burn_in == 0.0 && !has_skip) {
386 let fg_rgb = select(fg.rgb * mix(fg.a, 1.0, sub_add), fg.rgb, sub_add >= 1.0);389 let fg_rgb = select(fg.rgb * mix(fg.a, 1.0, sub_add), fg.rgb, sub_add >= 1.0);
387 let out_col = vec4<f32>(fg_rgb + bg.rgb * (1.0 - fg.a), fg.a + bg.a * (1.0 - fg.a));390 let out_col = vec4<f32>(fg_rgb + bg.rgb * (1.0 - fg.a), fg.a + bg.a * (1.0 - fg.a));
388 store_dst(vec2<i32>(gid.xy), out_col);391 store_dst(vec2<i32>(gid.xy), out_col);
...@@ -414,6 +417,22 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -414,6 +417,22 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {
414 } else if (op == 5) {417 } else if (op == 5) {
415 // XOr418 // XOr
416 comp = vec4<f32>(fg_rgb, fg_a_eff) * (1.0 - bg.a) + bg * (1.0 - fg_occ);419 comp = vec4<f32>(fg_rgb, fg_a_eff) * (1.0 - bg.a) + bg * (1.0 - fg_occ);
420 } else if (op == 6) {
421 // Conjoint Over: fg + bg * max(0, 1 - fg.a / bg.a)
422 let k_rgb = select(0.0, max(0.0, 1.0 - fg_occ / bg.a), bg.a > 1e-6);
423 let k_a = select(0.0, max(0.0, 1.0 - fg_a_eff / bg.a), bg.a > 1e-6);
424 comp = vec4<f32>(fg_rgb + bg.rgb * k_rgb, fg_a_eff + bg.a * k_a);
425 } else if (op == 7) {
426 // Disjoint Over: fg + bg * min(1, (1 - fg.a) / bg.a)
427 let k_rgb = select(1.0, min(1.0, (1.0 - fg_occ) / bg.a), bg.a > 1e-6);
428 let k_a = select(1.0, min(1.0, (1.0 - fg_a_eff) / bg.a), bg.a > 1e-6);
429 comp = vec4<f32>(fg_rgb + bg.rgb * k_rgb, fg_a_eff + bg.a * k_a);
430 } else if (op == 8) {
431 // Mask (Porter-Duff dst-in): BG masked by FG alpha
432 comp = bg * fg_a_eff;
433 } else if (op == 9) {
434 // Stencil (Porter-Duff dst-out): BG outside FG alpha
435 comp = vec4<f32>(bg.rgb * (1.0 - fg_occ), bg.a * (1.0 - fg_a_eff));
417 } else {436 } else {
418 // Over (default) with Fusion Alpha Gain, Subtractive/Additive, and BurnIn437 // Over (default) with Fusion Alpha Gain, Subtractive/Additive, and BurnIn
419 let over_rgb = fg_rgb + bg.rgb * (1.0 - fg_occ);438 let over_rgb = fg_rgb + bg.rgb * (1.0 - fg_occ);
...@@ -493,6 +512,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -493,6 +512,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3<u32>) {
493 if (mbm) {512 if (mbm) {
494 out_col = out_col * mask;513 out_col = out_col * mask;
495 }514 }
515 if (has_skip) {
516 let unprocessed = select(bg, vec4<f32>(0.0), has_mask > 0.5 && mbm);
517 out_col = select(out_col, unprocessed, skip > vec4<f32>(0.5));
518 }
496 store_dst(vec2<i32>(gid.xy), out_col);519 store_dst(vec2<i32>(gid.xy), out_col);
497}520}
498// ============================================================================521// ============================================================================
crates/engine/src/shaders/masks.wgsl+28-10
...@@ -145,10 +145,10 @@ fn mask_apply_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -145,10 +145,10 @@ fn mask_apply_op(@builtin(global_invocation_id) gid: vec3<u32>) {
145 textureStore(dst_tex, p, src * m);145 textureStore(dst_tex, p, src * m);
146}146}
147147
148// 10b. BitmapMask channel extraction + Low/High clip + centered 1:1 pixel alignment148// 10b. BitmapMask channel extraction + Low/High clip + FitInput/FitMask alignment
149// p[0] = (channel: 0=A,1=Luma,2=R,3=G,4=B,5=Sat,6=Lightness,7=Hue, low, high, fit_stretch)149// p[0] = (channel: 0=A,1=Luma,2=R,3=G,4=B,5=Sat,6=Lightness,7=Hue,8=Coverage, low, high, fit_mode: 0=Crop,1=Stretch,2=Inside,3=Width,4=Height,5=Outside)
150// p[1] = (center.x, center.y, src_origin.x, src_origin.y)150// p[1] = (center.x, center.y, src_origin.x, src_origin.y)
151// p[2] = (src_canvas.x, src_canvas.y, 0, 0)151// p[2] = (src_canvas.x, src_canvas.y, no_clip_black, no_clip_white)
152@compute @workgroup_size(8, 8)152@compute @workgroup_size(8, 8)
153fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {153fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {
154 let dims = textureDimensions(dst_tex);154 let dims = textureDimensions(dst_tex);
...@@ -162,20 +162,36 @@ fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -162,20 +162,36 @@ fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {
162 if (ch != 8) {162 if (ch != 8) {
163 let fdims = vec2<f32>(dims);163 let fdims = vec2<f32>(dims);
164 let sdims = vec2<f32>(textureDimensions(src_a));164 let sdims = vec2<f32>(textureDimensions(src_a));
165 let fit_stretch = u.p[0].w > 0.5;165 let fit_mode = i32(u.p[0].w + 0.5);
166166
167 var col = vec4<f32>(0.0);167 var col = vec4<f32>(0.0);
168 var valid = true;168 var valid = true;
169 if (fit_stretch) {169 if (fit_mode == 1) {
170 // Stretch: maps full source to full destination.
170 let uv = (vec2<f32>(gid.xy) + 0.5) / fdims;171 let uv = (vec2<f32>(gid.xy) + 0.5) / fdims;
171 col = textureSampleLevel(src_a, samp_lin, uv, 0.0);172 col = textureSampleLevel(src_a, samp_lin, uv, 0.0);
172 } else {173 } else {
173 // Fusion BitmapMask places Image centered at 1:1 pixel scale inside the mask canvas174 // Crop(0), Inside(2), Width(3), Height(4), Outside(5): centered at `center` with per-mode scale.
174 let center = u.p[1].xy;175 let center = u.p[1].xy;
175 let src_origin = u.p[1].zw;176 let src_origin = u.p[1].zw;
176 let src_canvas = max(u.p[2].xy, vec2<f32>(1.0));177 let src_canvas = select(sdims, u.p[2].xy, u.p[2].x > 0.5 && u.p[2].y > 0.5);
178 let sx = fdims.x / max(src_canvas.x, 1.0);
179 let sy = fdims.y / max(src_canvas.y, 1.0);
180 var scale = vec2<f32>(1.0, 1.0);
181 if (fit_mode == 2) {
182 let s = min(sx, sy);
183 scale = vec2<f32>(s, s);
184 } else if (fit_mode == 3) {
185 scale = vec2<f32>(sx, sx);
186 } else if (fit_mode == 4) {
187 scale = vec2<f32>(sy, sy);
188 } else if (fit_mode == 5) {
189 let s = max(sx, sy);
190 scale = vec2<f32>(s, s);
191 }
177 let shift_px = vec2<f32>((center.x - 0.5) * fdims.x, -(center.y - 0.5) * fdims.y);192 let shift_px = vec2<f32>((center.x - 0.5) * fdims.x, -(center.y - 0.5) * fdims.y);
178 let canvas_px = (vec2<f32>(gid.xy) + 0.5) - 0.5 * fdims - shift_px + 0.5 * src_canvas;193 let rel_dst = (vec2<f32>(gid.xy) + 0.5) - 0.5 * fdims - shift_px;
194 let canvas_px = rel_dst / max(scale, vec2<f32>(1e-5)) + 0.5 * src_canvas;
179 let tex_px = canvas_px - src_origin;195 let tex_px = canvas_px - src_origin;
180 if (tex_px.x >= 0.0 && tex_px.x <= sdims.x && tex_px.y >= 0.0 && tex_px.y <= sdims.y) {196 if (tex_px.x >= 0.0 && tex_px.x <= sdims.x && tex_px.y >= 0.0 && tex_px.y <= sdims.y) {
181 col = textureSampleLevel(src_a, samp_lin, tex_px / sdims, 0.0);197 col = textureSampleLevel(src_a, samp_lin, tex_px / sdims, 0.0);
...@@ -227,7 +243,9 @@ fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {...@@ -227,7 +243,9 @@ fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {
227 if (abs(span) < 1e-6) {243 if (abs(span) < 1e-6) {
228 v = select(0.0, 1.0, v >= lo);244 v = select(0.0, 1.0, v >= lo);
229 } else {245 } else {
230 v = clamp((v - lo) / span, 0.0, 1.0);246 v = (v - lo) / span;
247 if (u.p[2].z < 0.5) { v = max(v, 0.0); }
248 if (u.p[2].w < 0.5) { v = min(v, 1.0); }
231 }249 }
232 textureStore(dst_tex, p, vec4<f32>(v, v, v, v));250 store_dst(p, vec4<f32>(v, v, v, v));
233}251}