diff --git a/crates/engine/src/eval/blur_glow.rs b/crates/engine/src/eval/blur_glow.rs index 0d40e555414603367e542e9b1afd1694e669d125..f9a20a3b2415cf304fefa298219fd72398783c81 100644 --- a/crates/engine/src/eval/blur_glow.rs +++ b/crates/engine/src/eval/blur_glow.rs @@ -39,9 +39,13 @@ impl Engine { let ry = y_raw * scale_ref * gauss_k; let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; let clamp_edges = if node.static_str("ClippingMode").unwrap_or("Frame") == "None" { 0.0 } else { 1.0 }; + let mbm = self.multiplies_by_mask(comp, node, time); + let skip = self.process_skip(comp, node, time); + let off = self.channel_off(comp, node, time); + let all_noop = (0..4).all(|c| skip[c] > 0.5 || off[c] > 0.5); let max_r = rx.max(ry); - if max_r < 0.25 || blend <= 1e-6 { + if !mbm && (max_r < 0.25 || blend <= 1e-6 || all_noop) { return src; } let has_mask = Self::resolve_effect_mask_id(comp, node).is_some(); @@ -53,7 +57,19 @@ impl Engine { let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); - let 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 }]); + 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 }, + if mbm { 1.0 } else { 0.0 }, + skip[0], skip[1], skip[2], skip[3], + off[0], off[1], off[2], off[3], + ], + ); if let Some(hit) = self.cache.get(key, node.id, frame_i) { stats.cache_hits += 1; return hit; @@ -63,7 +79,12 @@ impl Engine { // When ClippingMode = "None" on an unmasked full-blend Blur, Fusion expands the DoD so the // outer half of the Gaussian/Box falloff survives beyond the frame border for downstream Merges! - let expand_dod = clamp_edges < 0.5 && mask.is_none() && blend >= 0.999; + let expand_dod = clamp_edges < 0.5 + && mask.is_none() + && blend >= 0.999 + && !mbm + && skip == [0.0; 4] + && off == [0.0; 4]; let (work_src, out_origin) = if expand_dod { let pad_x = if rx >= 0.25 { (rx * 1.5).ceil().min(384.0) as u32 } else { 0 }; let pad_y = if ry >= 0.25 { (ry * 1.5).ceil().min(384.0) as u32 } else { 0 }; @@ -100,6 +121,7 @@ impl Engine { 1u32 }; + let mbm_f = if mbm { 1.0 } else { 0.0 }; if down > 1 { let work_size = [(work_src.size[0] / down).max(1), (work_src.size[1] / down).max(1)]; let small = self.alloc_tex(work_size, src.depth, 0); @@ -120,28 +142,35 @@ impl Engine { uv.p[1] = [1.0, 0.0, 0.0, clamp_edges]; self.dispatch_op(enc, ShaderOp::BlurV, &tmp.view, &self.dummy.view, &self.dummy.view, &small_blur, &uv); - if blend >= 0.999 && mask.is_none() { + if blend >= 0.999 && mask.is_none() && skip == [0.0; 4] && off == [0.0; 4] { self.dispatch_op(enc, ShaderOp::DisplayBlit, &small_blur.view, &self.dummy.view, &self.dummy.view, &dst, &u_blit); } else { - let mut um = UniformParams::default(); - um.p[0] = [0.5, 0.5, 1.0, 0.0]; - um.p[1] = [blend, 1.0, 0.0, 0.0]; - 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]; + let up = self.alloc_tex(work_src.size, src.depth, 0); + self.dispatch_op(enc, ShaderOp::DisplayBlit, &small_blur.view, &self.dummy.view, &self.dummy.view, &up, &u_blit); + let mut ug = UniformParams::default(); + ug.p[0] = [1.0, blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }]; + ug.p[1] = [4.0, 0.0, 0.0, mbm_f]; + ug.p[3] = skip; + ug.p[4] = off; let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); - self.dispatch_op(enc, ShaderOp::Merge, &work_src.view, &small_blur.view, mv, &dst, &um); + self.dispatch_op(enc, ShaderOp::GlowAdd, &work_src.view, &up.view, mv, &dst, &ug); } } else if rx < 0.25 { let mut uv = UniformParams::default(); uv.p[0] = [0.0, 1.0, ry, mode]; uv.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges]; - uv.p[2] = [1.0, 0.0, 0.0, 0.0]; + uv.p[2] = [1.0, mbm_f, 0.0, 0.0]; + uv.p[3] = skip; + uv.p[4] = off; let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); self.dispatch_op(enc, ShaderOp::BlurV, &work_src.view, &work_src.view, mv, &dst, &uv); } else if ry < 0.25 { let mut uh = UniformParams::default(); uh.p[0] = [1.0, 0.0, rx, mode]; uh.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges]; - uh.p[2] = [1.0, 0.0, 0.0, 0.0]; + uh.p[2] = [1.0, mbm_f, 0.0, 0.0]; + uh.p[3] = skip; + uh.p[4] = off; let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); self.dispatch_op(enc, ShaderOp::BlurH, &work_src.view, &work_src.view, mv, &dst, &uh); } else { @@ -154,7 +183,9 @@ impl Engine { let mut uv = UniformParams::default(); uv.p[0] = [0.0, 1.0, ry, mode]; uv.p[1] = [blend, if mask.is_some() { 1.0 } else { 0.0 }, if mask_inv { 1.0 } else { 0.0 }, clamp_edges]; - uv.p[2] = [1.0, 0.0, 0.0, 0.0]; // blend against original unblurred src in src_b! + uv.p[2] = [1.0, mbm_f, 0.0, 0.0]; // blend against original unblurred src in src_b! + uv.p[3] = skip; + uv.p[4] = off; let mv = mask.as_ref().map_or(&self.dummy.view, |m| &m.view); self.dispatch_op(enc, ShaderOp::BlurV, &tmp.view, &work_src.view, mv, &dst, &uv); } @@ -497,7 +528,7 @@ impl Engine { let sy = if lock_xy { sx } else { - comp.eval_number(node, "YGlowSize", time, sx as f64, &self.expr) as f32 + comp.eval_number(node, "YGlowSize", time, 10.0, &self.expr) as f32 }; let g = comp.eval_number(node, "Gain", time, 2.0, &self.expr) as f32; 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 { let sy = if lock_xy { sx } else { - comp.eval_number(node, "YGlowSize", time, sx as f64, &self.expr) as f32 + comp.eval_number(node, "YGlowSize", time, glow_fallback, &self.expr) as f32 }; let glow_p = comp.eval_number(node, "Glow", time, 0.8, &self.expr).clamp(0.0, 0.999) as f32; let g = 1.0 / (1.0 - glow_p).max(1e-3); @@ -1290,12 +1321,21 @@ impl Engine { }; let src = self.eval_node(comp, src_id, time, enc, visiting, stats); let blend = comp.eval_number(node, "Blend", time, 1.0, &self.expr) as f32; - let lock_xy = comp.eval_number(node, "LockAmountXY", time, 1.0, &self.expr) > 0.5; + let lock_xy = if node.inputs.contains_key("LockXY") { + comp.eval_number(node, "LockXY", time, 1.0, &self.expr) > 0.5 + } else { + comp.eval_number(node, "LockAmountXY", time, 1.0, &self.expr) > 0.5 + }; let x_amt = comp.eval_number(node, "XAmount", time, 1.0, &self.expr) as f32; - let y_amt = if lock_xy { x_amt } else { comp.eval_number(node, "YAmount", time, x_amt as f64, &self.expr) as f32 }; + let y_amt = if lock_xy { + x_amt + } else { + comp.eval_number(node, "YAmount", time, 1.0, &self.expr) as f32 + }; let scale_ref = src.canvas_size[0] as f32 / 720.0 * 1.14; let rx = (x_amt * scale_ref).max(0.0); let ry = (y_amt * scale_ref).max(0.0); + let clamp_edges = if node.static_str("ClippingMode").unwrap_or("Frame") == "None" { 0.0 } else { 1.0 }; let mbm = self.multiplies_by_mask(comp, node, time); let skip = self.process_skip(comp, node, time); let off = self.channel_off(comp, node, time); @@ -1311,7 +1351,19 @@ impl Engine { let (mask, mask_inv) = self.eval_mask_input_sized(comp, node, time, Some(src.size), enc, visiting, stats); let mask = mask.map(|m| self.crop_to_canvas(enc, m)); let mask_hash = mask.as_ref().map_or(0, |m| m.content_hash); - 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]]); + let key = self.make_key( + node, + "Sharpen", + src.size, + &[src.content_hash, mask_hash], + &[ + rx, ry, blend, clamp_edges, + 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], + ], + ); if let Some(hit) = self.cache.get(key, node.id, frame_i) { stats.cache_hits += 1; return hit; @@ -1323,11 +1375,11 @@ impl Engine { let tmp2 = self.alloc_tex(src.size, src.depth, 0); let mut uh = UniformParams::default(); uh.p[0] = [1.0, 0.0, rx, 0.0]; - uh.p[1] = [1.0, 0.0, 0.0, 0.0]; + uh.p[1] = [1.0, 0.0, 0.0, clamp_edges]; self.dispatch_op(enc, ShaderOp::BlurH, &src.view, &self.dummy.view, &self.dummy.view, &tmp1, &uh); let mut uv = UniformParams::default(); uv.p[0] = [0.0, 1.0, ry, 0.0]; - uv.p[1] = [1.0, 0.0, 0.0, 0.0]; + uv.p[1] = [1.0, 0.0, 0.0, clamp_edges]; self.dispatch_op(enc, ShaderOp::BlurV, &tmp1.view, &self.dummy.view, &self.dummy.view, &tmp2, &uv); let dst = self.alloc_tex_like(&src, src.depth, key); @@ -2627,4 +2679,110 @@ mod tests { let got_tuned = eng.readback_rgba(&dst_tuned).expect("readback tuned"); assert_eq!(got_ref, got_tuned); } + + #[test] + fn blur_process_toggles_channel_off_and_multiply_by_mask() { + let comp_src = r#"{ + Tools = ordered() { + Src = Background { + Inputs = { + Width = Input { Value = 64, }, Height = Input { Value = 64, }, + Type = Input { Value = FuID { "Horizontal" }, }, + TopLeftRed = Input { Value = 1.0, }, TopLeftGreen = Input { Value = 1.0, }, TopLeftBlue = Input { Value = 1.0, }, TopLeftAlpha = Input { Value = 1.0, }, + TopRightRed = Input { Value = 0.0, }, TopRightGreen = Input { Value = 0.0, }, TopRightBlue = Input { Value = 0.0, }, TopRightAlpha = Input { Value = 1.0, }, + }, + }, + Mask = RectangleMask { + Inputs = { MaskWidth = Input { Value = 64, }, MaskHeight = Input { Value = 64, }, Width = Input { Value = 0.5, }, Height = Input { Value = 0.5, }, }, + }, + BlurSkip = Blur { + Inputs = { + Input = Input { SourceOp = "Src", Source = "Output", }, + XBlurSize = Input { Value = 200.0, }, + ProcessRed = Input { Value = 0, }, + Blue = Input { Value = 0, }, + }, + }, + BlurMbm = Blur { + Inputs = { + Input = Input { SourceOp = "Src", Source = "Output", }, + EffectMask = Input { SourceOp = "Mask", Source = "Mask", }, + MultiplyByMask = Input { Value = 1, }, + XBlurSize = Input { Value = 200.0, }, + ProcessRed = Input { Value = 0, }, + Blue = Input { Value = 0, }, + }, + }, + } + }"#; + let comp = Comp::from_fusion_str(comp_src).expect("parse Blur toggles comp"); + let Ok(mut eng) = Engine::new_headless() else { return }; + let (ts, _) = eng.evaluate(&comp, comp.find_node("Src").unwrap(), 0.0); + let ps = eng.readback_rgba(&ts).unwrap(); + let (tb, _) = eng.evaluate(&comp, comp.find_node("BlurSkip").unwrap(), 0.0); + let pb = eng.readback_rgba(&tb).unwrap(); + // Left edge (x=0, y=32): R (ProcessRed=0) and B (Blue=0) keep source; G is blurred down significantly. + let left = (32 * 64) * 4; + assert_eq!(pb[left], ps[left], "ProcessRed=0 must keep unblurred R"); + assert_eq!(pb[left + 2], ps[left + 2], "Blue=0 must keep unblurred B"); + assert!(u16::from(pb[left + 1]) + 10 < u16::from(ps[left + 1]), "G must be blurred ({} vs {})", pb[left + 1], ps[left + 1]); + + let (tm, _) = eng.evaluate(&comp, comp.find_node("BlurMbm").unwrap(), 0.0); + let pm = eng.readback_rgba(&tm).unwrap(); + let center = (32 * 64 + 32) * 4; + assert_eq!(pm[center], 0, "MultiplyByMask=1 + ProcessRed=0 clears R to 0"); + assert!(pm[center + 2] > 100, "MultiplyByMask=1 + Blue=0 preserves source B inside mask: {}", pm[center + 2]); + assert_eq!(&pm[left..left + 4], &[0, 0, 0, 0], "MultiplyByMask=1 zeroes outside mask"); + } + + #[test] + fn sharpen_lock_xy_unlocked_and_border_clamping() { + let comp_src = r#"{ + Tools = ordered() { + HStep = Background { + Inputs = { + Width = Input { Value = 64, }, Height = Input { Value = 64, }, + Type = Input { Value = FuID { "Horizontal" }, }, + TopLeftRed = Input { Value = 0.8, }, TopLeftGreen = Input { Value = 0.8, }, TopLeftBlue = Input { Value = 0.8, }, TopLeftAlpha = Input { Value = 1.0, }, + TopRightRed = Input { Value = 0.2, }, TopRightGreen = Input { Value = 0.2, }, TopRightBlue = Input { Value = 0.2, }, TopRightAlpha = Input { Value = 1.0, }, + }, + }, + VStep = Background { + Inputs = { + Width = Input { Value = 64, }, Height = Input { Value = 64, }, + Type = Input { Value = FuID { "Vertical" }, }, + TopLeftRed = Input { Value = 0.8, }, TopLeftGreen = Input { Value = 0.8, }, TopLeftBlue = Input { Value = 0.8, }, TopLeftAlpha = Input { Value = 1.0, }, + BottomLeftRed = Input { Value = 0.2, }, BottomLeftGreen = Input { Value = 0.2, }, BottomLeftBlue = Input { Value = 0.2, }, BottomLeftAlpha = Input { Value = 1.0, }, + }, + }, + ShH = Sharpen { + Inputs = { + Input = Input { SourceOp = "HStep", Source = "Output", }, + LockXY = Input { Value = 0, }, + XAmount = Input { Value = 60.0, }, + }, + }, + ShV = Sharpen { + Inputs = { + Input = Input { SourceOp = "VStep", Source = "Output", }, + LockXY = Input { Value = 0, }, + XAmount = Input { Value = 60.0, }, + }, + }, + } + }"#; + let comp = Comp::from_fusion_str(comp_src).expect("parse Sharpen LockXY comp"); + let Ok(mut eng) = Engine::new_headless() else { return }; + let (th, _) = eng.evaluate(&comp, comp.find_node("ShH").unwrap(), 0.0); + let ph = eng.readback_rgba(&th).unwrap(); + let (tv, _) = eng.evaluate(&comp, comp.find_node("ShV").unwrap(), 0.0); + let pv = eng.readback_rgba(&tv).unwrap(); + // Unlocked LockXY=0 with XAmount=60 (YAmount defaults to 1.0): horizontal gradient at x=0 overshoots, + // while vertical gradient at y=0 stays unsharpened (and Frame clamping avoids black-border halo). + let h_left = ph[(32 * 64) * 4]; + let v_top = pv[32 * 4]; + assert!(h_left >= 210, "large XAmount should sharpen horizontal gradient (got {h_left})"); + assert!(h_left > v_top + 4, "unlocked LockXY=0 must sharpen X ({h_left}) more than default Y ({v_top})"); + assert!((v_top as i32 - 203).abs() <= 2, "default YAmount=1.0 with LockXY=0 should keep vertical edge near 203 (got {v_top})"); + } } diff --git a/crates/engine/src/eval/color.rs b/crates/engine/src/eval/color.rs index 39209691612c40f03cfe13289957b108e126158c..9fc5299f05cec75ef03d3a41f5f7a115126ea8a6 100644 --- a/crates/engine/src/eval/color.rs +++ b/crates/engine/src/eval/color.rs @@ -58,6 +58,16 @@ impl CcLevels { out_scale *= out_span; out_offset = out_offset * out_span + l.out_low; } + // Curve table is uploaded as Rgba16Float (max 65504); when `low >= high` collapses `span` to + // `1e-5`, `in_scale` reaches 1e5+ and overflows f16 to Inf (producing Inf - Inf = NaN in the + // shader). Rescaling `(in_scale, in_offset)` together keeps the exact step threshold + // `-in_offset / in_scale` while staying finite in f16. + let max_in = in_scale.abs().max(in_offset.abs()); + if max_in > 4096.0 { + let s = 4096.0 / max_in; + in_scale *= s; + in_offset *= s; + } [[contrast, slope, offset, bright], [in_scale, in_offset, gamma, 0.0], [out_scale, out_offset, 0.0, 0.0]] } } @@ -2125,5 +2135,100 @@ mod tests { } assert!(eng.pipelines.borrow().lut_misses <= 83); } + + #[test] + fn color_corrector_inverted_low_high_levels_does_not_overflow_f16() { + // When MasterGreenLow (0.675) > MasterGreenHigh (0.08), span collapses without overflowing + // Rgba16Float curve storage: green above Low maps to 255, green below Low maps to 0. + let comp_src = r#"{ + Tools = ordered() { + Bright = Background { + Inputs = { + Width = Input { Value = 16, }, Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.8, }, + TopLeftGreen = Input { Value = 0.85, }, + TopLeftBlue = Input { Value = 0.8, }, + TopLeftAlpha = Input { Value = 1.0, }, + }, + }, + Dark = Background { + Inputs = { + Width = Input { Value = 16, }, Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.8, }, + TopLeftGreen = Input { Value = 0.25, }, + TopLeftBlue = Input { Value = 0.8, }, + TopLeftAlpha = Input { Value = 1.0, }, + }, + }, + CCBright = ColorCorrector { + Inputs = { + Input = Input { SourceOp = "Bright", Source = "Output", }, + MasterGreenLow = Input { Value = 0.67557, }, + MasterGreenHigh = Input { Value = 0.0827, }, + MasterBlueOutputHigh = Input { Value = 0.5, }, + }, + }, + CCDark = ColorCorrector { + Inputs = { + Input = Input { SourceOp = "Dark", Source = "Output", }, + MasterGreenLow = Input { Value = 0.67557, }, + MasterGreenHigh = Input { Value = 0.0827, }, + MasterBlueOutputHigh = Input { Value = 0.5, }, + }, + }, + } + }"#; + let comp = Comp::from_fusion_str(comp_src).expect("parse CC inverted levels comp"); + let Ok(mut eng) = Engine::new_headless() else { return }; + let (tb, _) = eng.evaluate(&comp, comp.find_node("CCBright").unwrap(), 0.0); + let pb = eng.readback_rgba(&tb).unwrap(); + assert_eq!(pb[1], 255, "green above MasterGreenLow must saturate to 255, not NaN->0: {:?}", &pb[..4]); + assert!((pb[2] as i32 - 102).abs() <= 2, "MasterBlueOutputHigh=0.5 scales 0.8->0.4 (~102), got {}", pb[2]); + + let (td, _) = eng.evaluate(&comp, comp.find_node("CCDark").unwrap(), 0.0); + let pd = eng.readback_rgba(&td).unwrap(); + assert_eq!(pd[1], 0, "green below MasterGreenLow must map to 0: {:?}", &pd[..4]); + } + + #[test] + fn color_gain_balance_before_additive_tonal_hue_and_process_channels() { + // Matches Fusion ColorGain f11 behavior: MidRedCyan balance runs before additive tonal + // DarkHue/MidHue/HighHue shifts, while ProcessRed=0 and ProcessBlue=0 keep source R/B. + let comp_src = r#"{ + Tools = ordered() { + Src = Background { + Inputs = { + Width = Input { Value = 16, }, Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.6157, }, + TopLeftGreen = Input { Value = 0.902, }, + TopLeftBlue = Input { Value = 0.3373, }, + TopLeftAlpha = Input { Value = 1.0, }, + }, + }, + CG = ColorGain { + Inputs = { + Input = Input { SourceOp = "Src", Source = "Output", }, + ProcessRed = Input { Value = 0, }, + ProcessBlue = Input { Value = 0, }, + GainGreen = Input { Value = 3.26722, }, + GammaGreen = Input { Value = 4.43987, }, + LiftRed = Input { Value = -0.26756, }, + MidRedCyan = Input { Value = -0.6691, }, + DarkHue = Input { Value = 0.70687, }, + MidHue = Input { Value = -0.5539, }, + HighHue = Input { Value = -0.51854, }, + }, + }, + } + }"#; + let comp = Comp::from_fusion_str(comp_src).expect("parse ColorGain balance+hue comp"); + let Ok(mut eng) = Engine::new_headless() else { return }; + let (t, _) = eng.evaluate(&comp, comp.find_node("CG").unwrap(), 0.0); + let px = eng.readback_rgba(&t).unwrap(); + assert_eq!(px[0], 157, "ProcessRed=0 keeps source R=157"); + assert_eq!(px[2], 86, "ProcessBlue=0 keeps source B=86"); + // Fusion 21.1 GT at PlateA (64,64) = (157,230,86) gives G=46 (vs old broken G=105). + assert!((px[1] as i32 - 46).abs() <= 3, "expected Fusion ColorGain G~46, got {}", px[1]); + } } diff --git a/crates/engine/src/eval/composite.rs b/crates/engine/src/eval/composite.rs index 7b67d459fef10cbf2a13451a31e30a6a941aeb42..33ec283a354228b8bff5ed917f0fd6dde9b91358 100644 --- a/crates/engine/src/eval/composite.rs +++ b/crates/engine/src/eval/composite.rs @@ -986,15 +986,22 @@ impl Engine { "Luminosity" => 24.0, _ => 0.0, }; - let nearest = if comp.eval_number(node, "FilterMethod", time, 2.0, &self.expr) < 0.5 { 1.0 } else { 0.0 }; + let filter_method = comp.eval_number(node, "FilterMethod", time, 2.0, &self.expr).round() as i32; + let nearest = if filter_method == 0 { 1.0 } else { 0.0 }; + let cubic_filter = if filter_method == 4 { 1.0 } else { 0.0 }; let op_id = match node.static_str("Operator").unwrap_or("Over") { "Under" => 1.0, "In" => 2.0, "Held Out" | "HeldOut" => 3.0, "Atop" => 4.0, "XOr" => 5.0, + "Conjoint" => 6.0, + "Disjoint" => 7.0, + "Mask" => 8.0, + "Stencil" => 9.0, _ => 0.0, // Over }; + let skip = self.process_skip(comp, node, time); 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); @@ -1030,7 +1037,7 @@ impl Engine { if edges < 0.5 && mask.is_none() && mode_id == 0.0 - && (op_id == 0.0 || op_id == 1.0 || op_id == 5.0) + && (op_id == 0.0 || op_id == 1.0 || op_id == 5.0 || op_id == 6.0 || op_id == 7.0) && bg.tex_scale >= 0.99 && eff_size > 1e-4 { @@ -1134,6 +1141,8 @@ impl Engine { (bg.origin[1] - dst_origin[1]) as f32, ]; u.p[7] = [if mbm { 1.0 } else { 0.0 }, 0.0, 0.0, 0.0]; + u.p[8] = [0.0, cubic_filter, 0.0, 0.0]; + u.p[9] = skip; let bg_has_z = bg.aux.as_ref().and_then(|a| a.z.as_ref()).is_some(); let fg_has_z = fg.aux.as_ref().and_then(|a| a.z.as_ref()).is_some(); @@ -1145,7 +1154,7 @@ impl Engine { "Merge", dst_size, &[bg.content_hash, fg.content_hash, mask_hash], - bytemuck::cast_slice(&u.p[..8]), + bytemuck::cast_slice(&u.p[..10]), ); if let Some(hit) = self.cache.get(key, node.id, frame_i) { stats.cache_hits += 1; @@ -1182,14 +1191,15 @@ impl Engine { if bg_has_z { 1.0 } else { 0.0 }, if fg_has_z { 1.0 } else { 0.0 }, ]; - u.p[8] = [fg_z_offset, 0.0, 0.0, 0.0]; + u.p[8] = [fg_z_offset, cubic_filter, 0.0, 0.0]; + u.p[9] = skip; let key = self.make_key( node, "MergeDepth", dst_size, &[bg.content_hash, fg.content_hash, mask_hash], - bytemuck::cast_slice(&u.p[..9]), + bytemuck::cast_slice(&u.p[..10]), ); if let Some(hit) = self.cache.get(key, node.id, frame_i) { stats.cache_hits += 1; @@ -2413,4 +2423,90 @@ mod tests { assert_eq!(mc_tex.size, [260, 260], "Fuse.XfChroma must preserve full upstream DoD"); eng.queue.submit(Some(enc.finish())); } + + #[test] + fn merge_conjoint_disjoint_mask_stencil_and_process_channels() { + // BG = premult (0.4, 0.2, 0.8, 0.8), FG = premult (0.6, 0.3, 0.0, 0.6) + // - Conjoint: k = 1 - 0.6/0.8 = 0.25 -> (0.7, 0.35, 0.2, 0.8) = (179, 89, 51, 204) + // - Disjoint: k = min(1, (1 - 0.6)/0.8) = 0.5 -> (0.8, 0.4, 0.4, 1.0) = (204, 102, 102, 255) + // - Mask: bg * fg.a = (0.24, 0.12, 0.48, 0.48) = (61, 31, 122, 122) + // - Stencil: bg * (1 - fg.a) = (0.16, 0.08, 0.32, 0.32) = (41, 20, 82, 82) + // - ProcessBlue=0, ProcessAlpha=0 on Conjoint keeps BG blue (204) and BG alpha (204) + let comp_src = r#"{ + Tools = ordered() { + Bg = Background { + Inputs = { + Width = Input { Value = 16, }, Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.4, }, + TopLeftGreen = Input { Value = 0.2, }, + TopLeftBlue = Input { Value = 0.8, }, + TopLeftAlpha = Input { Value = 0.8, }, + }, + }, + Fg = Background { + Inputs = { + Width = Input { Value = 16, }, Height = Input { Value = 16, }, + TopLeftRed = Input { Value = 0.6, }, + TopLeftGreen = Input { Value = 0.3, }, + TopLeftBlue = Input { Value = 0.0, }, + TopLeftAlpha = Input { Value = 0.6, }, + }, + }, + MConj = Merge { + Inputs = { + Operator = Input { Value = FuID { "Conjoint" }, }, + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + }, + }, + MDisj = Merge { + Inputs = { + Operator = Input { Value = FuID { "Disjoint" }, }, + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + }, + }, + MMask = Merge { + Inputs = { + Operator = Input { Value = FuID { "Mask" }, }, + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + }, + }, + MSten = Merge { + Inputs = { + Operator = Input { Value = FuID { "Stencil" }, }, + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + }, + }, + MProc = Merge { + Inputs = { + Operator = Input { Value = FuID { "Conjoint" }, }, + ProcessBlue = Input { Value = 0, }, + ProcessAlpha = Input { Value = 0, }, + Background = Input { SourceOp = "Bg", Source = "Output", }, + Foreground = Input { SourceOp = "Fg", Source = "Output", }, + }, + }, + } + }"#; + let comp = Comp::from_fusion_str(comp_src).expect("parse merge ops comp"); + let Ok(mut eng) = Engine::new_headless() else { return }; + let px_of = |eng: &mut Engine, name: &str| -> [u8; 4] { + let (tex, _) = eng.evaluate(&comp, comp.find_node(name).unwrap(), 0.0); + let px = eng.readback_rgba(&tex).unwrap(); + [px[0], px[1], px[2], px[3]] + }; + let close = |got: [u8; 4], want: [u8; 4], label: &str| { + for c in 0..4 { + assert!(got[c].abs_diff(want[c]) <= 2, "{label} ch {c}: got {got:?}, want {want:?}"); + } + }; + close(px_of(&mut eng, "MConj"), [179, 89, 51, 204], "Conjoint"); + close(px_of(&mut eng, "MDisj"), [204, 102, 102, 255], "Disjoint"); + close(px_of(&mut eng, "MMask"), [61, 31, 122, 122], "Mask"); + close(px_of(&mut eng, "MSten"), [41, 20, 82, 82], "Stencil"); + close(px_of(&mut eng, "MProc"), [179, 89, 204, 204], "Conjoint+ProcessBlue/Alpha=0"); + } } diff --git a/crates/engine/src/eval/masks.rs b/crates/engine/src/eval/masks.rs index 2cd9b2a16c18f40bdad3e4bd3308ce390f7c2b70..3233df695ff2b1f5e47dd908b3fba618927c167f 100644 --- a/crates/engine/src/eval/masks.rs +++ b/crates/engine/src/eval/masks.rs @@ -12,6 +12,23 @@ pub(super) struct MaskPost<'a> { } impl Engine { + /// Maps Fusion `FitMask` / `FitInput` FuID or numeric enum to shader mode id: + /// `0=Crop, 1=Stretch, 2=Inside, 3=Width, 4=Height, 5=Outside`. + pub(super) fn fit_mask_mode(comp: &Comp, node: &Node, input: &str, time: f64, expr: &ExprEngine) -> f32 { + if let Some(s) = node.static_str(input) { + return match s { + "Stretch" => 1.0, + "Inside" => 2.0, + "Width" => 3.0, + "Height" => 4.0, + "Outside" => 5.0, + _ => 0.0, // Crop + }; + } + let n = comp.eval_number(node, input, time, 0.0, expr).round().clamp(0.0, 5.0); + n as f32 + } + pub(super) fn paint_mode_id(node: &Node) -> f32 { match node.static_str("PaintMode").unwrap_or("Merge") { "Add" => 1.0, @@ -497,15 +514,20 @@ impl Engine { "Saturation" => 5.0, "Lightness" => 6.0, "Hue" => 7.0, + "Coverage" => 8.0, _ => 0.0, // Alpha (Fusion default) }; let low = comp.eval_number(node, "Low", time, 0.0, &self.expr) as f32; let high = comp.eval_number(node, "High", time, 1.0, &self.expr) as f32; + let clip_black = comp.eval_number(node, "ClipBlack", time, 1.0, &self.expr) > 0.5; + let clip_white = comp.eval_number(node, "ClipWhite", time, 1.0, &self.expr) > 0.5; + let no_clip_black = if clip_black { 0.0 } else { 1.0 }; + let no_clip_white = if clip_white { 0.0 } else { 1.0 }; let soft = comp.eval_number(node, "SoftEdge", time, 0.0, &self.expr).max(0.0) as f32; let invert = if comp.eval_number(node, "Invert", time, 0.0, &self.expr) > 0.5 { 1.0 } else { 0.0 }; let level = comp.eval_number(node, "Level", time, 1.0, &self.expr) as f32; let center = comp.eval_point(node, "Center", time, [0.5, 0.5], &self.expr); - let fit_stretch = if comp.eval_number(node, "FitInput", time, 0.0, &self.expr) > 0.5 { 1.0 } else { 0.0 }; + let fit_mode = Self::fit_mask_mode(comp, node, "FitInput", time, &self.expr); let is_box = Self::mask_filter_is_box(node); let clamp_edges = Self::mask_clamp_edges(node); let prev_mask = self.bitmap_or_wand_prev_mask(comp, node, time, size, enc, visiting, stats); @@ -521,8 +543,8 @@ impl Engine { size, &[src.content_hash, prev_hash], &[ - ch_id, low, high, soft, invert, level, - center[0] as f32, center[1] as f32, fit_stretch, + ch_id, low, high, no_clip_black, no_clip_white, soft, invert, level, + center[0] as f32, center[1] as f32, fit_mode, if is_box { 1.0 } else { 0.0 }, clamp_edges, paint_mode, ], ); @@ -538,9 +560,9 @@ impl Engine { let raw_tex = create_texture(&self.device, size, if need_post { 0 } else { key }); let mut u = UniformParams::default(); - u.p[0] = [ch_id, low, high, fit_stretch]; + u.p[0] = [ch_id, low, high, fit_mode]; u.p[1] = [center[0] as f32, center[1] as f32, src.origin[0] as f32, src.origin[1] as f32]; - u.p[2] = [src.canvas_size[0] as f32, src.canvas_size[1] as f32, 0.0, 0.0]; + u.p[2] = [src.canvas_size[0] as f32, src.canvas_size[1] as f32, no_clip_black, no_clip_white]; self.dispatch_op(enc, ShaderOp::BitmapMask, &src.view, &self.dummy.view, &self.dummy.view, &raw_tex, &u); let out_tex = if need_post { @@ -1367,4 +1389,81 @@ mod tests { let px = eng.readback_rgba(&tex).unwrap(); assert!(px.chunks_exact(4).any(|c| c[3] > 200), "clamped PolylineMask must rasterize cleanly instead of clearing to transparent"); } + + #[test] + fn bitmap_mask_fit_input_modes_and_coverage_channel() { + // Source is 16x32 solid white; destination mask is 32x16: + // - Crop: 16px wide centered -> x=2 is 0, x=16 is 255 + // - Stretch / Width / Outside: fills full width -> x=2 is 255 + // - Inside / Height: uniform scale = 16/32 = 0.5 -> 8px wide centered (x=12..20) -> x=10 is 0, x=16 is 255 + // - Coverage channel: returns 0 mask cleanly + let comp_src = r#"{ + Tools = ordered() { + Src = Background { + Inputs = { + Width = Input { Value = 16, }, + Height = Input { Value = 32, }, + TopLeftRed = Input { Value = 1, }, + TopLeftGreen = Input { Value = 1, }, + TopLeftBlue = Input { Value = 1, }, + TopLeftAlpha = Input { Value = 1, }, + }, + }, + BCrop = BitmapMask { + Inputs = { + OutputSize = Input { Value = FuID { "Custom" }, }, + MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, }, + FitInput = Input { Value = FuID { "Crop" }, }, + Image = Input { SourceOp = "Src", Source = "Output", }, + }, + }, + BStretch = BitmapMask { + Inputs = { + OutputSize = Input { Value = FuID { "Custom" }, }, + MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, }, + FitInput = Input { Value = FuID { "Stretch" }, }, + Image = Input { SourceOp = "Src", Source = "Output", }, + }, + }, + BInside = BitmapMask { + Inputs = { + OutputSize = Input { Value = FuID { "Custom" }, }, + MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, }, + FitInput = Input { Value = FuID { "Inside" }, }, + Image = Input { SourceOp = "Src", Source = "Output", }, + }, + }, + BOutside = BitmapMask { + Inputs = { + OutputSize = Input { Value = FuID { "Custom" }, }, + MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, }, + FitInput = Input { Value = FuID { "Outside" }, }, + Image = Input { SourceOp = "Src", Source = "Output", }, + }, + }, + BCov = BitmapMask { + Inputs = { + OutputSize = Input { Value = FuID { "Custom" }, }, + MaskWidth = Input { Value = 32, }, MaskHeight = Input { Value = 16, }, + Channel = Input { Value = FuID { "Coverage" }, }, + Image = Input { SourceOp = "Src", Source = "Output", }, + }, + }, + } + }"#; + let comp = Comp::from_fusion_str(comp_src).expect("parse BitmapMask FitInput comp"); + let Ok(mut eng) = Engine::new_headless() else { return }; + let sample = |eng: &mut Engine, name: &str, x: usize, y: usize| -> u8 { + let (tex, _) = eng.evaluate(&comp, comp.find_node(name).unwrap(), 0.0); + let px = eng.readback_rgba(&tex).unwrap(); + px[(y * 32 + x) * 4 + 3] + }; + assert_eq!(sample(&mut eng, "BCrop", 2, 8), 0); + assert_eq!(sample(&mut eng, "BCrop", 10, 8), 255); + assert_eq!(sample(&mut eng, "BStretch", 2, 8), 255); + assert_eq!(sample(&mut eng, "BInside", 10, 8), 0); + assert_eq!(sample(&mut eng, "BInside", 16, 8), 255); + assert_eq!(sample(&mut eng, "BOutside", 2, 8), 255); + assert_eq!(sample(&mut eng, "BCov", 16, 8), 0); + } } diff --git a/crates/engine/src/shaders/blur_glow.wgsl b/crates/engine/src/shaders/blur_glow.wgsl index 9a1d67df86a7af44f1e8ee00c06f8633aff76a47..b5694e250163f7f32225a447e1ebf29422bc42df 100644 --- a/crates/engine/src/shaders/blur_glow.wgsl +++ b/crates/engine/src/shaders/blur_glow.wgsl @@ -132,7 +132,8 @@ fn run_blur_pass(gid: vec2) { store_dst(vec2(gid.xy), out_col); return; } - if (has_mask && m <= 1e-6) { + let mbm = u.p[2].y > 0.5; + if (has_mask && !mbm && m <= 1e-6) { var orig: vec4; if (u.p[2].x > 0.5) { orig = textureLoad(src_b, vec2(gid.xy), 0); @@ -275,7 +276,9 @@ fn run_blur_pass(gid: vec2) { } } - if (u.p[1].y < 0.5 && u.p[1].x >= 1.0) { + let skip = u.p[3] > vec4(0.5); + let off = u.p[4] > vec4(0.5); + if (u.p[1].y < 0.5 && u.p[1].x >= 1.0 && !any(skip) && !any(off)) { store_dst(vec2(gid.xy), acc); return; } @@ -283,8 +286,16 @@ fn run_blur_pass(gid: vec2) { if (u.p[2].x > 0.5) { orig = textureLoad(src_b, vec2(gid.xy), 0); } - let out_col = mix(orig, acc, m); - store_dst(vec2(gid.xy), out_col); + let eff = select(acc, orig, off); + if (has_mask && mbm) { + let raw_mask = read_mask(gid.xy, dims, u.p[1].y, u.p[1].z); + let mixed = mix(orig, eff, clamp(u.p[1].x, 0.0, 1.0)); + let clamped = select(mixed, clamp(mixed, vec4(0.0), vec4(1.0)), !HDR_FLOAT); + store_dst(vec2(gid.xy), select(clamped * raw_mask, vec4(0.0), skip)); + return; + } + let out_col = mix(orig, eff, m); + store_dst(vec2(gid.xy), select(out_col, orig, skip)); } @compute @workgroup_size(8, 8) @@ -328,8 +339,7 @@ fn glow_add_op(@builtin(global_invocation_id) gid: vec3) { let gain_rgb = cscale.xyz * gain; let gain_a = cscale.w * gain; let out_rgb = orig.rgb * (1.0 - gm_blur) + blurred.rgb * gain_rgb; - let raw_a = orig.a * (1.0 - gm_blur) + blurred.a * gain_a; - let out_a = select(clamp(raw_a, 0.0, 1.0), raw_a, HDR_FLOAT); + let out_a = orig.a * (1.0 - gm_blur) + blurred.a * gain_a; added = vec4(out_rgb, out_a); if (u.p[1].z > 0.5) { added = orig + added * (1.0 - orig.a); diff --git a/crates/engine/src/shaders/color.wgsl b/crates/engine/src/shaders/color.wgsl index 9250d50906c631a31be50e9c7415156be59d623e..2c009b3e2c2fdcc75b5af843b7b4945cd9242cf9 100644 --- a/crates/engine/src/shaders/color.wgsl +++ b/crates/engine/src/shaders/color.wgsl @@ -674,8 +674,39 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3) { rgb = mix(vec3(luma), rgb, sat); } + // Dark/Mid/High brightness shifts all channels by a Rec.601-luma-weighted amount; colour shifts weigh each channel by its own value. + // Fusion applies Balance (Brightness + RedCyan/GreenMagenta/BlueYellow) before tonal HSV Hue shifts. + let any_dmb = abs(u.p[7].w) > 1e-6 || abs(u.p[6].w) > 1e-6 || abs(u.p[5].w) > 1e-6; + if (any_dmb) { + let luma_v = clamp(dot(rgb, vec3(0.299, 0.587, 0.114)), 0.0, 1.0); + let wd = max(1.0 - 2.0 * luma_v, 0.0); + let wh = max(2.0 * luma_v - 1.0, 0.0); + rgb = rgb + vec3(u.p[7].w * wd + u.p[6].w * (1.0 - wd - wh) + u.p[5].w * wh); + } + let any_dmc = any(abs(u.p[7].xyz) > vec3(1e-6)) + || any(abs(u.p[6].xyz) > vec3(1e-6)) + || any(abs(u.p[5].xyz) > vec3(1e-6)); + if (any_dmc) { + let sd = cg_axis_shift(u.p[7].xyz); + let sm = cg_axis_shift(u.p[6].xyz); + let sh = cg_axis_shift(u.p[5].xyz); + let rc = clamp(rgb, vec3(0.0), vec3(1.0)); + let wr = cg_range_weights(rc.r); + let wg = cg_range_weights(rc.g); + let wb = cg_range_weights(rc.b); + rgb = rgb + vec3( + wr.x * sd.x + wr.y * sm.x + wr.z * sh.x, + wg.x * sd.y + wg.y * sm.y + wg.z * sh.y, + wb.x * sd.z + wb.y * sm.z + wb.z * sh.z, + ); + if (!HDR_FLOAT) { + rgb = clamp(rgb, vec3(0.0), vec3(1.0)); + } + } + // Tonal range weights using HSV Value max(r,g,b): - // Fusion ColorGain uses quadratic falloff u^2 on Dark/High and linear 1 - u_dark - u_high on Mid. + // Fusion ColorGain uses quadratic falloff u^2 on Dark/High and linear 1 - u_dark - u_high on Mid, + // combining each tonal range's HSV shift additively from the base color: c + sum_i w_i * (hsv(h + dh_i) - c). let any_hsv = abs(dark_hue) > 1e-5 || abs(mid_hue) > 1e-5 || abs(high_hue) > 1e-5; if (any_hsv) { let c_pos = max(rgb, vec3(0.0)); @@ -689,44 +720,19 @@ fn color_gain_op(@builtin(global_invocation_id) gid: vec3) { var cur = c_pos; if (abs(dark_hue) > 1e-5 && w_dark > 1e-5) { let h_d = vec3(fract(base_hsv.x + dark_hue), base_hsv.y, base_hsv.z); - cur = mix(cur, hsv_to_rgb(h_d), w_dark); + cur = cur + (hsv_to_rgb(h_d) - c_pos) * w_dark; } if (abs(high_hue) > 1e-5 && w_high > 1e-5) { let h_h = vec3(fract(base_hsv.x + high_hue), base_hsv.y, base_hsv.z); - cur = mix(cur, hsv_to_rgb(h_h), w_high); + cur = cur + (hsv_to_rgb(h_h) - c_pos) * w_high; } if (abs(mid_hue) > 1e-5 && w_mid > 1e-5) { let h_m = vec3(fract(base_hsv.x + mid_hue), base_hsv.y, base_hsv.z); - cur = mix(cur, hsv_to_rgb(h_m), w_mid); + cur = cur + (hsv_to_rgb(h_m) - c_pos) * w_mid; } rgb = cur; } - // Dark/Mid/High brightness shifts all channels by a Rec.601-luma-weighted amount; colour shifts weigh each channel by its own value. - let any_dmb = abs(u.p[7].w) > 1e-6 || abs(u.p[6].w) > 1e-6 || abs(u.p[5].w) > 1e-6; - if (any_dmb) { - let luma_v = clamp(dot(rgb, vec3(0.299, 0.587, 0.114)), 0.0, 1.0); - let wd = max(1.0 - 2.0 * luma_v, 0.0); - let wh = max(2.0 * luma_v - 1.0, 0.0); - rgb = rgb + vec3(u.p[7].w * wd + u.p[6].w * (1.0 - wd - wh) + u.p[5].w * wh); - } - let any_dmc = any(abs(u.p[7].xyz) > vec3(1e-6)) - || any(abs(u.p[6].xyz) > vec3(1e-6)) - || any(abs(u.p[5].xyz) > vec3(1e-6)); - if (any_dmc) { - let sd = cg_axis_shift(u.p[7].xyz); - let sm = cg_axis_shift(u.p[6].xyz); - let sh = cg_axis_shift(u.p[5].xyz); - let wr = cg_range_weights(rgb.r); - let wg = cg_range_weights(rgb.g); - let wb = cg_range_weights(rgb.b); - rgb = rgb + vec3( - wr.x * sd.x + wr.y * sm.x + wr.z * sh.x, - wg.x * sd.y + wg.y * sm.y + wg.z * sh.y, - wb.x * sd.z + wb.y * sm.z + wb.z * sh.z, - ); - } - if (prediv) { rgb = rgb * a_new; } diff --git a/crates/engine/src/shaders/composite.wgsl b/crates/engine/src/shaders/composite.wgsl index e693ba127a99b2c03bdd19b44470fa62987e5f98..b4640696516fe4df843fecc7f8aaf6d6250be375 100644 --- a/crates/engine/src/shaders/composite.wgsl +++ b/crates/engine/src/shaders/composite.wgsl @@ -251,8 +251,8 @@ fn merge_fetch_edges(tex: texture_2d, ix: i32, iy: i32, dims: vec2, ed } } -fn merge_sample_fg(tex: texture_2d, fu: vec2, edges: i32, dod: vec4, nearest: bool, size: f32) -> vec4 { - if (nearest || size >= 0.999) { +fn merge_sample_fg(tex: texture_2d, fu: vec2, edges: i32, dod: vec4, nearest: bool, size: f32, force_cubic: bool) -> vec4 { + if (nearest || (size >= 0.999 && !force_cubic)) { return sample_dod_edges_mode(tex, fu, edges, dod, nearest); } let itdims = vec2(textureDimensions(tex)); @@ -359,7 +359,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3) { let mode = i32(u.p[1].z + 0.5); let edges = i32(u.p[1].w + 0.5); let nearest = u.p[2].z > 0.5; - let op = i32(u.p[2].w + 0.5); // 0=Over, 1=Under, 2=In, 3=HeldOut, 4=Atop, 5=XOr + let cubic_filter = u.p[8].y > 0.5; + let skip = u.p[9]; + let has_skip = any(skip > vec4(0.5)); + 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 let has_mask = u.p[3].x; let burn_in = u.p[6].x; let sub_add = u.p[6].y; @@ -379,10 +382,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3) { if (u.p[2].x > 0.5) { fg_fu.x = 1.0 - fg_fu.x; } if (u.p[2].y > 0.5) { fg_fu.y = 1.0 - fg_fu.y; } - let fg = merge_sample_fg(src_b, fg_fu, edges, u.p[4], nearest, size); + let fg = merge_sample_fg(src_b, fg_fu, edges, u.p[4], nearest, size, cubic_filter); - // Fast path for the common Normal + Over + Gain=1.0 + no mask + Blend=1.0 case. - if (mode == 0 && op == 0 && gain >= 1.0 && has_mask < 0.5 && blend >= 1.0 && !mbm && burn_in == 0.0) { + // Fast path for the common Normal + Over + Gain=1.0 + no mask + Blend=1.0 + all channels processed case. + if (mode == 0 && op == 0 && gain >= 1.0 && has_mask < 0.5 && blend >= 1.0 && !mbm && burn_in == 0.0 && !has_skip) { let fg_rgb = select(fg.rgb * mix(fg.a, 1.0, sub_add), fg.rgb, sub_add >= 1.0); let out_col = vec4(fg_rgb + bg.rgb * (1.0 - fg.a), fg.a + bg.a * (1.0 - fg.a)); store_dst(vec2(gid.xy), out_col); @@ -414,6 +417,22 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3) { } else if (op == 5) { // XOr comp = vec4(fg_rgb, fg_a_eff) * (1.0 - bg.a) + bg * (1.0 - fg_occ); + } else if (op == 6) { + // Conjoint Over: fg + bg * max(0, 1 - fg.a / bg.a) + let k_rgb = select(0.0, max(0.0, 1.0 - fg_occ / bg.a), bg.a > 1e-6); + let k_a = select(0.0, max(0.0, 1.0 - fg_a_eff / bg.a), bg.a > 1e-6); + comp = vec4(fg_rgb + bg.rgb * k_rgb, fg_a_eff + bg.a * k_a); + } else if (op == 7) { + // Disjoint Over: fg + bg * min(1, (1 - fg.a) / bg.a) + let k_rgb = select(1.0, min(1.0, (1.0 - fg_occ) / bg.a), bg.a > 1e-6); + let k_a = select(1.0, min(1.0, (1.0 - fg_a_eff) / bg.a), bg.a > 1e-6); + comp = vec4(fg_rgb + bg.rgb * k_rgb, fg_a_eff + bg.a * k_a); + } else if (op == 8) { + // Mask (Porter-Duff dst-in): BG masked by FG alpha + comp = bg * fg_a_eff; + } else if (op == 9) { + // Stencil (Porter-Duff dst-out): BG outside FG alpha + comp = vec4(bg.rgb * (1.0 - fg_occ), bg.a * (1.0 - fg_a_eff)); } else { // Over (default) with Fusion Alpha Gain, Subtractive/Additive, and BurnIn let over_rgb = fg_rgb + bg.rgb * (1.0 - fg_occ); @@ -493,6 +512,10 @@ fn merge_op(@builtin(global_invocation_id) gid: vec3) { if (mbm) { out_col = out_col * mask; } + if (has_skip) { + let unprocessed = select(bg, vec4(0.0), has_mask > 0.5 && mbm); + out_col = select(out_col, unprocessed, skip > vec4(0.5)); + } store_dst(vec2(gid.xy), out_col); } // ============================================================================ diff --git a/crates/engine/src/shaders/masks.wgsl b/crates/engine/src/shaders/masks.wgsl index 26b5b713d26521617f9e0d5c0c09a2b7164bb76b..d3c886172d1e47d2aaa8cd19337cc6350ddf0dcf 100644 --- a/crates/engine/src/shaders/masks.wgsl +++ b/crates/engine/src/shaders/masks.wgsl @@ -145,10 +145,10 @@ fn mask_apply_op(@builtin(global_invocation_id) gid: vec3) { textureStore(dst_tex, p, src * m); } -// 10b. BitmapMask channel extraction + Low/High clip + centered 1:1 pixel alignment -// p[0] = (channel: 0=A,1=Luma,2=R,3=G,4=B,5=Sat,6=Lightness,7=Hue, low, high, fit_stretch) +// 10b. BitmapMask channel extraction + Low/High clip + FitInput/FitMask alignment +// 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) // p[1] = (center.x, center.y, src_origin.x, src_origin.y) -// p[2] = (src_canvas.x, src_canvas.y, 0, 0) +// p[2] = (src_canvas.x, src_canvas.y, no_clip_black, no_clip_white) @compute @workgroup_size(8, 8) fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3) { let dims = textureDimensions(dst_tex); @@ -162,20 +162,36 @@ fn bitmap_mask_op(@builtin(global_invocation_id) gid: vec3) { if (ch != 8) { let fdims = vec2(dims); let sdims = vec2(textureDimensions(src_a)); - let fit_stretch = u.p[0].w > 0.5; + let fit_mode = i32(u.p[0].w + 0.5); var col = vec4(0.0); var valid = true; - if (fit_stretch) { + if (fit_mode == 1) { + // Stretch: maps full source to full destination. let uv = (vec2(gid.xy) + 0.5) / fdims; col = textureSampleLevel(src_a, samp_lin, uv, 0.0); } else { - // Fusion BitmapMask places Image centered at 1:1 pixel scale inside the mask canvas + // Crop(0), Inside(2), Width(3), Height(4), Outside(5): centered at `center` with per-mode scale. let center = u.p[1].xy; let src_origin = u.p[1].zw; - let src_canvas = max(u.p[2].xy, vec2(1.0)); + let src_canvas = select(sdims, u.p[2].xy, u.p[2].x > 0.5 && u.p[2].y > 0.5); + let sx = fdims.x / max(src_canvas.x, 1.0); + let sy = fdims.y / max(src_canvas.y, 1.0); + var scale = vec2(1.0, 1.0); + if (fit_mode == 2) { + let s = min(sx, sy); + scale = vec2(s, s); + } else if (fit_mode == 3) { + scale = vec2(sx, sx); + } else if (fit_mode == 4) { + scale = vec2(sy, sy); + } else if (fit_mode == 5) { + let s = max(sx, sy); + scale = vec2(s, s); + } let shift_px = vec2((center.x - 0.5) * fdims.x, -(center.y - 0.5) * fdims.y); - let canvas_px = (vec2(gid.xy) + 0.5) - 0.5 * fdims - shift_px + 0.5 * src_canvas; + let rel_dst = (vec2(gid.xy) + 0.5) - 0.5 * fdims - shift_px; + let canvas_px = rel_dst / max(scale, vec2(1e-5)) + 0.5 * src_canvas; let tex_px = canvas_px - src_origin; if (tex_px.x >= 0.0 && tex_px.x <= sdims.x && tex_px.y >= 0.0 && tex_px.y <= sdims.y) { 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) { if (abs(span) < 1e-6) { v = select(0.0, 1.0, v >= lo); } else { - v = clamp((v - lo) / span, 0.0, 1.0); + v = (v - lo) / span; + if (u.p[2].z < 0.5) { v = max(v, 0.0); } + if (u.p[2].w < 0.5) { v = min(v, 1.0); } } - textureStore(dst_tex, p, vec4(v, v, v, v)); + store_dst(p, vec4(v, v, v, v)); }