diff --git a/crates/nodes/src/atomic/blend.rs b/crates/nodes/src/atomic/blend.rs index a09c965..37e35fc 100644 --- a/crates/nodes/src/atomic/blend.rs +++ b/crates/nodes/src/atomic/blend.rs @@ -34,8 +34,8 @@ fn blend(context: &CookContext) -> CookResult { Ok(vec![Image::generate(context.w, context.h, kind, |x, y| { let (x, y) = (x as i64, y as i64); let m = mask.as_ref().map_or(1.0, |m| m.get_gray(x, y, false)); - // Normal copy reads the mask through its transparency before applying the global opacity. - let m = if mode == 0 && color_blending == 0 { 1.0 - (1.0 - m) } else { m }; + // These modes read the mask through its transparency before applying global opacity. + let m = if (mode == 0 && color_blending == 0) || matches!(mode, 7 | 8) { 1.0 - (1.0 - m) } else { m }; ops::blend_px(mode, background.get(x, y, false), foreground.get(x, y, false), opacity * m, color_blending, blend_alpha, clamp) })]) } @@ -48,15 +48,18 @@ mod tests { use std::collections::BTreeMap; #[test] - fn normal_copy_rounds_mask_color_and_coverage_independently() { + fn blend_rounds_mask_color_and_coverage_independently() { let foreground = [116.0, 38.0, 60.0, 36.0].map(|v| v / 255.0); let background = [158.0, 163.0, 10.0, 68.0].map(|v| v / 255.0); let mask = 44.0 / 255.0; - for (opacity, alpha, hdr, expected) in [ - (1.0, true, false, [0x3f1d97ad, 0x3f209511, 0x3d343117, 0x3e91adfe]), - (0.07, true, false, [0x3f1e8c37, 0x3f236cdd, 0x3d21ff39, 0x3e892c70]), - (0.3, false, false, [0x3f1e4fbd, 0x3f22b8df, 0x3d267f2b, 0x3e888889]), - (0.3, true, true, [0x3f514e8e, 0x3f4b8353, 0xbf55520b, 0xbdcc9712]), + for (mode, interpretation, opacity, alpha, hdr, expected) in [ + (0, 0, 1.0, true, false, [0x3f1d97ad, 0x3f209511, 0x3d343117, 0x3e91adfe]), + (0, 0, 0.07, true, false, [0x3f1e8c37, 0x3f236cdd, 0x3d21ff39, 0x3e892c70]), + (0, 0, 0.3, false, false, [0x3f1e4fbd, 0x3f22b8df, 0x3d267f2b, 0x3e888889]), + (0, 0, 0.3, true, true, [0x3f514e8e, 0x3f4b8353, 0xbf55520b, 0xbdcc9712]), + (7, 0, 0.6137, true, false, [0x3f1a2797, 0x3f1659c4, 0x3d75aca1, 0x3e81bac6]), + (8, 0, 0.6137, true, false, [0x3f21760a, 0x3f319bf2, 0x3d286e88, 0x3e8e257d]), + (8, 1, 0.6137, false, false, [0x3f32bf2c, 0x3f834be5, 0x3d57e86e, 0x3e888889]), ] { let level = |v: f32| if hdr { v * 3.0 - 1.0 } else { v }; let inputs = [ @@ -65,7 +68,15 @@ mod tests { Some(Image::gray(1, 1, |_, _| level(mask))), ]; let vars = BTreeMap::new(); - let params = resolve_params(&definition(), &BTreeMap::from([("opacity".into(), Value::Float(opacity)), ("alpha".into(), Value::Bool(alpha))])); + let params = resolve_params( + &definition(), + &BTreeMap::from([ + ("mode".into(), Value::Enum(mode)), + ("color_blending".into(), Value::Enum(interpretation)), + ("opacity".into(), Value::Float(opacity)), + ("alpha".into(), Value::Bool(alpha)), + ]), + ); let context = CookContext { input_formats: &[], format: OutputFormat::F32, @@ -79,7 +90,7 @@ mod tests { vars: &vars, }; let result = blend(&context).unwrap(); - assert_eq!(result[0].get(0, 0, false).map(f32::to_bits), expected, "opacity {opacity}, alpha {alpha}, HDR {hdr}"); + assert_eq!(result[0].get(0, 0, false).map(f32::to_bits), expected, "mode {mode}, interpretation {interpretation}, opacity {opacity}"); } } } diff --git a/crates/nodes/src/function/mod.rs b/crates/nodes/src/function/mod.rs index 2cdbedf..8f32ce7 100644 --- a/crates/nodes/src/function/mod.rs +++ b/crates/nodes/src/function/mod.rs @@ -51,6 +51,7 @@ pub use node_types::node_types; pub use output::{can_be_output, output_node}; pub(crate) use processor::pixel_processor; pub use program::Program; +pub(crate) use reciprocal::divide as pixel_divide; pub use resolve_instances::resolve_instances; pub use system_variable::SystemVariable; pub use warnings::warnings; diff --git a/crates/nodes/src/function/reciprocal.rs b/crates/nodes/src/function/reciprocal.rs index 799c9ae..d544a3f 100644 --- a/crates/nodes/src/function/reciprocal.rs +++ b/crates/nodes/src/function/reciprocal.rs @@ -52,7 +52,7 @@ const fn polynomials() -> [Polynomial; 128] { const POLYNOMIALS: [Polynomial; 128] = polynomials(); -pub(super) fn divide(numerator: f32, denominator: f32) -> f32 { +pub(crate) fn divide(numerator: f32, denominator: f32) -> f32 { let bits = denominator.to_bits(); let exponent = (bits >> 23) & 255; if !(1..=252).contains(&exponent) { diff --git a/crates/nodes/src/ops.rs b/crates/nodes/src/ops.rs index 84151e9..ea1d64d 100644 --- a/crates/nodes/src/ops.rs +++ b/crates/nodes/src/ops.rs @@ -319,11 +319,8 @@ pub fn blend_channel(mode: i32, b: f32, f: f32) -> f32 { 5 => b.max(f), 6 => b.min(f), 8 => { - if f.abs() < 1e-6 { - if b > 0.0 { 1.0 } else { 0.0 } - } else { - b / f - } + // A zero divisor produces this finite HDR value even when the numerator is zero. + if f == 0.0 { 16_777_216.0 } else { crate::function::pixel_divide(b, f) } } 9 => { if b < 0.5 { @@ -366,14 +363,24 @@ pub fn blend_channel(mode: i32, b: f32, f: f32) -> f32 { /// `blend_alpha`: composite coverage; false preserves background alpha. #[inline] pub fn blend_px(mode: i32, bg: [f32; 4], fg: [f32; 4], opacity: f32, color_blending: i32, blend_alpha: bool, clamp: bool) -> [f32; 4] { - if mode == 0 && color_blending == 0 { + if mode == 7 { + // Switch interpolates each channel directly, including alpha. + let mut out = std::array::from_fn(|k| bg[k].mul_add(1.0 - opacity, fg[k] * opacity)); + if clamp { + for c in &mut out[..3] { + *c = c.clamp(0.0, 1.0); + } + } + return out; + } + if (mode == 0 && color_blending == 0) || (mode == 8 && color_blending != 2) { // The color weights use the complement of transparency. Coverage uses the original alpha. - let alpha = 1.0 - (1.0 - fg[3]); + let alpha = if color_blending == 1 { 1.0 } else { 1.0 - (1.0 - fg[3]) }; let source_weight = alpha * opacity; let background_weight = (-alpha).mul_add(opacity, 1.0); let mut out = bg; for k in 0..3 { - let color = bg[k].mul_add(background_weight, fg[k] * source_weight); + let color = bg[k].mul_add(background_weight, blend_channel(mode, bg[k], fg[k]) * source_weight); out[k] = if clamp { color.clamp(0.0, 1.0) } else { color }; } if blend_alpha { @@ -382,8 +389,7 @@ pub fn blend_px(mode: i32, bg: [f32; 4], fg: [f32; 4], opacity: f32, color_blend return out; } - // Switch already interpolates RGBA; another alpha weight composites twice - let t = opacity * if mode != 7 { fg[3] } else { 1.0 }; + let t = opacity * fg[3]; let coverage = bg[3] * (1.0 - t) + t; let mut o = [0.0; 4]; for k in 0..3 { @@ -391,12 +397,12 @@ pub fn blend_px(mode: i32, bg: [f32; 4], fg: [f32; 4], opacity: f32, color_blend // Weight before adding to preserve integer-boundary rounding o[k] = match color_blending { - 1 if mode != 7 => bg[k] * (1.0 - opacity) + v * opacity, - 2 if mode != 7 && coverage > 0.0 => { + 1 => bg[k] * (1.0 - opacity) + v * opacity, + 2 if coverage > 0.0 => { let v = if clamp { v.clamp(0.0, 1.0) } else { v }; (bg[k] * bg[3] * (1.0 - t) + fg[k] * t * (1.0 - bg[3]) + v * bg[3] * t) / coverage } - 2 if mode != 7 => bg[k], + 2 => bg[k], 3 if mode == 0 => bg[k] * (1.0 - t) + fg[k] * opacity, _ => bg[k] * (1.0 - t) + v * t, }; @@ -405,13 +411,7 @@ pub fn blend_px(mode: i32, bg: [f32; 4], fg: [f32; 4], opacity: f32, color_blend } } - o[3] = if mode == 7 { - bg[3] * (1.0 - opacity) + fg[3] * opacity - } else if blend_alpha || color_blending == 2 || (color_blending == 3 && mode == 0) { - coverage - } else { - bg[3] - }; + o[3] = if blend_alpha || color_blending == 2 || (color_blending == 3 && mode == 0) { coverage } else { bg[3] }; o } @@ -426,6 +426,15 @@ pub fn gray_from(w: u32, h: u32, v: Vec) -> Result { mod tests { use super::*; + #[test] + fn divide_blend_handles_zero_and_small_nonzero_divisors() { + for numerator in [-8.0, 0.0, 0.5] { + assert_eq!(blend_channel(8, numerator, 0.0), 16_777_216.0); + assert_eq!(blend_channel(8, numerator, -0.0), 16_777_216.0); + } + assert_eq!(blend_channel(8, -8.0, 1e-10), -80_000_000_000.0); + } + #[test] fn color_dodge_and_burn_keep_black_and_white_backgrounds() { const DODGE: i32 = 13; diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index a28c496..b4a3b26 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -247,6 +247,16 @@ Literal-only pixel graphs at 16², seed 23, give 512/768 exact pixels for each l The shared-lookup ridged root still matches 5,160/16,384 at changed roughness in F32, maximum 1.78813934e−7. Its one-octave .7 control still matches 1,847/4,096, maximum 2.38418579e−7. For positive normal x on the signed field, the reference's general power agrees with exp(y×ln(x)) on all 32,614 pixels. On .7 with exponent 1 it returns `3f333334`. Further work on general power and natural exponential is tracked with the [octave controls](#ridged-noise-octave-isolation). +### Color burn and color dodge + +Color burn and color dodge each match **171,264/171,264 RGBA pixels in 18 cases**, maximum error zero. Each root passed the four original 128²/256² cases (114,688 pixels), the [four-format and independent comparisons](#four-format-inventory-comparison) (17,408), and nine additional cases (39,168). In the independent 32² color-burn field, four pixels had blue foreground 0 and blue background 255. The divide stage differed by 255 levels at precisely those pixels. In color dodge the final switch previously matched 524/1,024 on that field with a maximum of 5.96046448e−8. + +For the nine cases, use byte channels modulo 256 with foreground index `t=1`, background 2 and mask 3. Set `V=11x+17y+3xy+31t`, and set RGB to `(V,41x−19y+11t,3x+79y+t)`. Alpha is `42×((13x+7y+31t) mod 7)`. Use V alone for the mask. The 16²/seed 17 and 128²/137 cases use opacity .375 with alpha blending enabled. The 64²/71 cases use opacity .8125 with alpha blending disabled and cover U8, U16, F16 and F32. Three more cases use a signed 32²/211 F32 field with opacity .375 (RGBA bytes divided by 100 minus .625, mask divided by 255), a 32²/43 U8 field with alternating blue endpoints, and a 64×32 input at 64²/53 F32. For alternating endpoints set foreground blue to zero where `(x+3y) mod 4=0` and to 255 elsewhere, then reverse the two values for background blue. The last two cases use opacity .8125 with alpha blending disabled. + +Independent atomic switch and divide probes each match **1,122,304/1,122,304 pixels in 34 cases** with byte inputs. Sweep all 65,536 byte pairs in both input orders at opacity 1 and .6137, comparing all four stored formats. The other cases use 64² varying RGBA, mask and alpha fields, including opaque, constant-mask and transparent-background controls. For switch, the mask is read through its transparency and the background's weighted contribution is fused into the rounded foreground product for all four channels. For divide, either signed zero divisor produces 16,777,216 before opacity, including zero divided by zero. Small nonzero divisors retain their sign and use the runtime pixel reciprocal. + +The separate straight-alpha divide probe still differs, including 443/4,096 exact RGBA32F pixels on its varying field, maximum .5. On equal-channel float colors bound to grayscale inputs, some outputs differ in the sign of zero. Divide matches 63,488/65,536 pixels at F16 and F32 for both opacities. Switch at full opacity matches 63,744/65,536 at F16 and 63,232/65,536 at F32. At .6137 opacity it matches 65,280/65,536 in both formats. All four signed endpoint fields have zero maximum absolute error. + ### Natural exponential and ridged noise Ridged noise with the [original shared lookup](#basic-and-ridged-3d-perlin) matches **253,184/253,184 pixels across 29 cases**, maximum error zero. The seven original cases contribute 163,840 pixels, the [thirteen octave controls](#ridged-noise-octave-isolation) contribute 53,248, and the nine cases below contribute 36,096. The octave with roughness .7 and levels 1–4 now matches all 4,096 LUM32F pixels. The runtime power of .7 to the fourth has bits `3e75dccb` in both apps. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 39d0a2d..044960d 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -35,7 +35,7 @@ A graph may accept input pins and still be a root under the dependency criterion Statuses: **Not checked**, **In progress**, **Parity** (exact pixels or typed values for the linked measured cases), or **Too hard** (reason recorded). Function calls do not count as regular graph dependencies. -The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **206 Parity**, **23 In progress** and **317 Too hard**. The November 17 and 18 comparisons include raw counts, output formats and remaining differences. [November 17 measurements](parity-fixes.md#2026-11-17-root-comparisons) · [November 18 measurements](parity-fixes.md#2026-11-18-root-comparisons) +The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **208 Parity**, **23 In progress** and **315 Too hard**. The November 17 and 18 comparisons include raw counts, output formats and remaining differences. [November 17 measurements](parity-fixes.md#2026-11-17-root-comparisons) · [November 18 measurements](parity-fixes.md#2026-11-18-root-comparisons) [Grayscale sampler measurements](parity-fixes.md#grayscale-sampling-of-color-inputs) · [Value sampling across 240 typed comparisons](parity-fixes.md#value-processor-texture-filtering) @@ -80,8 +80,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [blend.sbs/add](../assets/sbs/blend.sbs) | 3 | Yes | [Parity: 5 cases](parity-fixes.md#verified-cases) | | [blend.sbs/add_sub](../assets/sbs/blend.sbs) | 3 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [blend.sbs/color](../assets/sbs/blend.sbs) | 3 | No | [Too hard: color conversion](parity-fixes.md#remaining-differences) | -| [blend.sbs/color_burn](../assets/sbs/blend.sbs) | 3 | No | [Too hard: 7 pixels remain](parity-fixes.md#remaining-differences) | -| [blend.sbs/color_dodge](../assets/sbs/blend.sbs) | 3 | No | [Too hard: 7 pixels remain](parity-fixes.md#remaining-differences) | +| [blend.sbs/color_burn](../assets/sbs/blend.sbs) | 3 | No | [Parity · 18 cases, 171,264 exact RGBA pixels](parity-fixes.md#color-burn-and-color-dodge) | +| [blend.sbs/color_dodge](../assets/sbs/blend.sbs) | 3 | No | [Parity · 18 cases, 171,264 exact RGBA pixels](parity-fixes.md#color-burn-and-color-dodge) | | [blend.sbs/copy](../assets/sbs/blend.sbs) | 3 | Yes | [Parity: 5 cases](parity-fixes.md#verified-cases) | | [blend.sbs/difference](../assets/sbs/blend.sbs) | 3 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [blend.sbs/dissolve_2](../assets/sbs/blend.sbs) | 3 | No | [Too hard: stochastic pixels](parity-fixes.md#remaining-differences) |