From deec316912b1bb391ef17152cb72c1af6a3988db Mon Sep 17 00:00:00 2001 From: chasse20 Date: Sat, 21 Nov 2026 02:46:29 -0500 Subject: [PATCH] Use projected direction components in short blur kernels --- crates/nodes/src/atomic/directional_blur.rs | 57 ++++++++++----------- crates/nodes/tests/atomic_filter_parity.rs | 21 ++++++++ docs/parity-fixes.md | 38 ++++++++++++++ docs/sbs-root-nodes.md | 4 +- 4 files changed, 88 insertions(+), 32 deletions(-) diff --git a/crates/nodes/src/atomic/directional_blur.rs b/crates/nodes/src/atomic/directional_blur.rs index 5691c2b7..ab511468 100644 --- a/crates/nodes/src/atomic/directional_blur.rs +++ b/crates/nodes/src/atomic/directional_blur.rs @@ -47,40 +47,37 @@ pub(super) fn blur(context: &CookContext, angle: f32) -> CookResult { return Ok(vec![box_filter::axis(&input, reach, horizontal, tile[usize::from(!horizontal)], source_format, context.format)]); } + let radians = angle * std::f32::consts::TAU; + let sine = projected_sine(radians.abs()) * radians.signum(); + let cosine = projected_sine(radians.abs() + std::f32::consts::FRAC_PI_2); + let short_reach = (radius - 0.5) * sine.abs().max(cosine.abs()); + if short_reach <= 1.0 { + // Normalize the offsets in texture coordinates so the longer side keeps its texel scale. + let u = cosine / context.w as f32; + let v = sine / context.h as f32; + // Tap order affects the fused sum. A rectangular image can change which UV axis dominates. + let dominant = if u.abs() > v.abs() { u } else { v }; + let span = -dominant; + return Ok(vec![box_filter::short_line(&input, short_reach, [u / span, v / span], tile, source_format)]); + } + if tile == [true; 2] && context.w == input.width() && context.h == input.height() { + // Compare the components before rounding the direction to the sample grid. + let (line_y, line_x) = f64::from(radians).sin_cos(); + let horizontal = line_x.abs() / f64::from(context.w) > line_y.abs() / f64::from(context.h); + // An authored eighth turn has an exact unit slope even after radian conversion. + // On a rectangle the dominant UV direction can have the smaller texel + // component. The window spans the projection along the chosen texel axis. + let (across, along) = if horizontal { (sine, cosine) } else { (cosine, sine) }; + let slope = if (angle * 8.0).fract() == 0.0 { (line_y * line_x).signum() as f32 } else { across / along }; + if let Some(image) = box_filter::sheared(&input, (radius - 0.5) * along.abs(), horizontal, slope, source_format, context.format) { + return Ok(vec![image]); + } + } let (dx, dy) = direction(angle); // Image-space rotation follows increasing rows. let dy = -dy; let major = dx.abs().max(dy.abs()); let reach = (radius - 0.5) * major; - if reach <= 1.0 { - // Normalize the offsets in texture coordinates so the longer side keeps its texel scale. - let u = dx / context.w as f32; - let v = dy / context.h as f32; - // Tap order affects the fused sum. A rectangular image can change which UV axis dominates. - let dominant = if u.abs() > v.abs() { u } else { v }; - let span = -dominant; - return Ok(vec![box_filter::short_line(&input, reach, [u / span, v / span], tile, source_format)]); - } - if tile == [true; 2] && context.w == input.width() && context.h == input.height() { - // Compare the components before rounding the direction to the sample grid. - let radians = angle * std::f32::consts::TAU; - let (sine, cosine) = f64::from(radians).sin_cos(); - let horizontal = cosine.abs() / f64::from(context.w) > sine.abs() / f64::from(context.h); - // An authored eighth turn has an exact unit slope even after radian conversion. - // On a rectangle the dominant UV direction can have the smaller texel - // component. The window spans the projection along the chosen texel axis. - let (slope, along) = if (angle * 8.0).fract() == 0.0 { - ((sine * cosine).signum() as f32, major) - } else { - let sine = projected_sine(radians.abs()) * radians.signum(); - let cosine = projected_sine(radians.abs() + std::f32::consts::FRAC_PI_2); - let (across, along) = if horizontal { (sine, cosine) } else { (cosine, sine) }; - (across / along, along.abs()) - }; - if let Some(image) = box_filter::sheared(&input, (radius - 0.5) * along, horizontal, slope, source_format, context.format) { - return Ok(vec![image]); - } - } let projected_radius = 0.5 + reach; let half_samples = (projected_radius + 0.5).floor().clamp(1.0, MAX_HALF_SAMPLES as f32) as i32; @@ -108,7 +105,7 @@ pub(super) fn blur(context: &CookContext, angle: f32) -> CookResult { })]) } -/// The wide filter stores both direction components before deriving the window and shear. +/// The filter stores both direction components before deriving sample weights and coordinates. fn projected_sine(radians: f32) -> f32 { use std::f64::consts::{FRAC_PI_2, PI, TAU}; diff --git a/crates/nodes/tests/atomic_filter_parity.rs b/crates/nodes/tests/atomic_filter_parity.rs index 89a85023..875a6721 100644 --- a/crates/nodes/tests/atomic_filter_parity.rs +++ b/crates/nodes/tests/atomic_filter_parity.rs @@ -490,6 +490,26 @@ fn short_diagonal_blur_orders_the_taps_in_texture_coordinates() { } } +#[test] +fn short_oblique_blur_rounds_the_projected_center_and_edge_weights() { + for (size, angle, intensity, point, expected) in [ + ([128, 64], 0.4375, 3.625, [1, 1], 0x3f12_3b30), + ([128, 64], 0.4375, 6.3125, [2, 0], 0x3e8d_258d), + ([128, 64], 0.08203125, 3.625, [1, 1], 0x3f15_f9ba), + ([64, 128], -139.0 / 512.0, 3.625, [1, 0], 0x3e25_ead0), + ] { + let source = Image::gray(size[0], size[1], |x, y| if (x, y) == (1, 1) { 1.0 } else { 0.0 }).with_integer_sampling(true); + let params = [("angle", Value::Float(angle)), ("intensity", Value::Float(intensity))]; + let output = cook("directional_blur", &[Some(source)], ¶ms, size, true); + assert_eq!(output.get_gray(point[0], point[1], false).to_bits(), expected, "{angle}, {intensity}"); + } + + let square = [64; 2]; + let params = [("angle", Value::Float(0.375)), ("intensity", Value::Float(2.64))]; + let output = cook("directional_blur", &[Some(modular_blur_field(square))], ¶ms, square, true); + assert_eq!(output.get_gray(2, 0, false).to_bits(), 0x3eb3_e6a6); +} + #[test] fn diagonal_blur_on_rectangles_carries_the_shear_across_tile_edges() { let intensity = Value::Float(f32::from_bits(0x416b_a592)); @@ -525,6 +545,7 @@ fn oblique_rectangular_blur_projects_the_window_onto_its_filtering_axis() { fn long_oblique_blur_keeps_the_projected_stripe_weight() { for (size, angle, strength, vertical_stripe, expected) in [ ([16, 128], 0.1, 41.96875, true, 0x3e66_e1d7), + ([16, 128], 0.375, 41.96875, true, 0x3e7f_d802), ([128, 16], 0.265625, 46.6875, false, 0x3e30_2ae0), ([128, 16], -139.0 / 512.0, 34.89026, false, 0x3e6c_7382), ] { diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index c5990994..a29eacb9 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -234,6 +234,44 @@ The 2D fractal results also match **32,256/32,256** across 38 supplemental setti The [stock-table comparison](#voronoi-distortion-and-shared-noise-inputs) used different resources in the two applications. All 27 undistorted renders matched 146,688/146,688 pixels per root. The three distorted 32² stock renders differed throughout, with F32 maxima .175076187 for the 2D fractal and .205578864 for the 3D fractal. Those cases remain distinct from the shared-table results. +### Projected weights and tap order in short directional blurs + +The short oblique blur measures its reach using the stored horizontal and vertical direction components. It derives the first and last tap from the dominant component in texture coordinates. The same stored component supplies the projected width of a long diagonal window. On 16 × 128, the vertical white stripe at .375 turns and intensity 41.96875 produces center F32 bits 0x3e7fd802. A single U16 impulse at (1,1) on 128 × 64, angle .4375 and intensity 3.625, produces 0x3f123b30 at that pixel. The previous result was 0x3f123b31. + +The 46 three-tap F32 cases in the [rectangular blur matrix](#oblique-directional-blur-on-rectangular-images) improved from **384,404/442,368** to **442,368/442,368**, maximum zero. The 217 original signed and axial field controls from the [short-line comparison](#short-directional-blur-tap-order-on-rectangular-inputs) also match exactly, including square inputs where ordering the positive and negative taps affects the stored result. + +For an independent field use U16 levels (19x² + 313y + 3137xy + 47(j+3) + 26213(x mod 3)) mod 65536. Cycle dimensions 32 × 16, 16 × 32, 32² and 64 × 16 in zero-based angle order. Use turns (37,−119,303,−431)/1024, (183,−377,693,−1043)/2048, (987,−645,1213,−1515)/1024, then .06250012, .43749989, .213798523, .394229889, .125, −.125, .375, −.375, .625, .875, .08203125 and −.271484375. Set intensity to 256 times the radius divided by the shorter side. Use radii .5009765625, .671875, .8984375, 1.0703125, 1.3515625, 1.49 and 1.595703125 with seed 469193+29j+k for radius index k. Render F32 throughout and all four formats at indices 2 and 4. + +| Original primitive series | Renders | Exact pixels / total | Maximum error | +|---|---:|---:|---:| +| Earlier short-field controls · RGBA16 | 31 | 708,608/708,608 | 0 | +| Earlier short-field controls · RGBA16F | 31 | 708,608/708,608 | 0 | +| Earlier short-field controls · RGBA32F | 124 | 2,834,432/2,834,432 | 0 | +| Earlier short-field controls · RGBA8 | 31 | 708,608/708,608 | 0 | +| Earlier oblique matrix · RGBA16 | 36 | 342,561/344,064 | 63 | +| Earlier oblique matrix · RGBA16F | 36 | 343,693/344,064 | 0.0009765625 | +| Earlier oblique matrix · RGBA32F | 204 | 1,922,736/1,949,696 | 0.000969707966 | +| Earlier oblique matrix · RGBA8 | 36 | 344,030/344,064 | 1 | +| Independent field · RGBA16 | 48 | 36,864/36,864 | 0 | +| Independent field · RGBA16F | 48 | 36,864/36,864 | 0 | +| Independent field · RGBA32F | 168 | 128,991/129,024 | 0.00146079063 | +| Independent field · RGBA8 | 48 | 36,864/36,864 | 0 | +| Original stripe angles · RGBA32F | 792 | 1,608,192/1,622,016 | 1.1920929e-07 | +| Additional stripe angles · RGBA32F | 576 | 1,179,648/1,179,648 | 0 | +| Signed eighth-turn stripes · RGBA32F | 240 | 491,520/491,520 | 0 | +| Single impulses · RGBA32F | 30 | 2,752,512/2,752,512 | 0 | +| Sparse periodic impulses · RGBA32F | 36 | 2,948,850/2,949,120 | 0.000616334379 | +| Sparse periodic impulses · RGBA8 | 12 | 983,040/983,040 | 0 | + +For the eighth-turn controls orient the same 16-bit stripes perpendicular to each filter axis at 16 × 128 and 128 × 16. Use the first, second, fourth, sixth, seventh, ninth, thirteenth, fifteenth, seventeenth and eighteenth radii in the [long-stripe series](#direction-and-sample-coordinates-of-long-oblique-blurs). Use turns 0.125, 0.375, 0.625, 0.875, -0.125, -0.375, -0.625, -0.875, 1.125, -1.375, 1.625, -1.875 and seed 471329+71j+17h+i for angle j, orientation h and zero-based original radius index i. + +| Root (inventory index) | Distinct settings | U8 exact / total · max | U16 exact / total · max | F16 exact / total · max | F32 exact / total · max | +|---|---:|---:|---:|---:|---:| +| Blur HQ (55) | 78 | 706,504/706,560 · 1 | 1,169,141/1,169,408 · 257 | 591,816/591,872 · 0.00439453125 | 1,192,949/1,193,216 · 0.0039215982 | +| Blur HQ Grayscale (56) | 78 | 706,545/706,560 · 1 | 1,169,358/1,169,408 · 257 | 591,857/591,872 · 0.00390625 | 1,193,166/1,193,216 · 0.0039215982 | + +In the two complete roots, 646 color and 130 grayscale pixels still differ across 78 distinct settings per root. All 3,489,792 original pixels in each application reproduced after reopening 60 saved settings per root, maximum zero. The residuals include longer oblique passes. The new release regression failed before this change and passed afterward. All 323 node release tests, formatting, strict workspace Clippy, dependency layers and WebAssembly checks passed. + ## 2026-11-20 root comparisons ### Hue, saturation, lightness and Replace Color diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 42b470d5..00037f1c 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -154,8 +154,8 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug | [blend_switch.sbs/multi_switch_grayscale](../assets/sbs/blend_switch.sbs) | 20 | No | [Parity: 5 cases](parity-fixes.md#verified-cases) | | [blend_switch.sbs/switch](../assets/sbs/blend_switch.sbs) | 2 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [blend_switch.sbs/switch_grayscale](../assets/sbs/blend_switch.sbs) | 2 | No | [Parity: 6 cases](parity-fixes.md#verified-cases) | -| [blur_hq.sbs/blur_hq](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 3,660,410/3,661,056 pixels across 78 distinct settings](parity-fixes.md#direction-and-sample-coordinates-of-long-oblique-blurs) | -| [blur_hq.sbs/blur_hq_grayscale](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 3,660,926/3,661,056 pixels across 78 distinct settings](parity-fixes.md#direction-and-sample-coordinates-of-long-oblique-blurs) | +| [blur_hq.sbs/blur_hq](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 3,660,410/3,661,056 pixels across 78 distinct settings](parity-fixes.md#direction-and-sample-coordinates-of-long-oblique-blurs) · [Three-tap and eighth-turn probes](parity-fixes.md#projected-weights-and-tap-order-in-short-directional-blurs) | +| [blur_hq.sbs/blur_hq_grayscale](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 3,660,926/3,661,056 pixels across 78 distinct settings](parity-fixes.md#direction-and-sample-coordinates-of-long-oblique-blurs) · [Three-tap and eighth-turn probes](parity-fixes.md#projected-weights-and-tap-order-in-short-directional-blurs) | | [bnw_spots_1.sbs/bnw_spots_1](../assets/sbs/bnw_spots_1.sbs) | 0 | Yes | [In progress · 3,177,940/3,177,984 pixels across 125 settings](parity-fixes.md#pixel-center-rounding-at-the-near-stamp-vertex) · [64,000/64,000 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) · [Circular stamp and opacity controls](parity-fixes.md#circular-stamp-coverage-and-zero-stamp-opacity) · [One U16 texel accounts for nine default output pixels](parity-fixes.md#one-differing-generator-texel-in-the-basic-spots-root) | | [bnw_spots_3.sbs/bnw_spots_3](../assets/sbs/bnw_spots_3.sbs) | 0 | Yes | [Parity · 118 settings, 3,159,552/3,159,552 final pixels](parity-fixes.md#pixel-center-rounding-at-the-near-stamp-vertex) · [27,136/27,136 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) · [Circular stamp and opacity controls](parity-fixes.md#circular-stamp-coverage-and-zero-stamp-opacity) · [Intermediate recheck](parity-fixes.md#interpolated-coordinates-on-clipped-squares-and-quarter-turns) | | [brick_1.sbs/brick_1](../assets/sbs/brick_1.sbs) | 0 | Yes | [Parity · 131 renders, 3,194,368/3,194,368 pixels](parity-fixes.md#fractional-stamp-vertices-and-the-last-covered-pixel) · [64,000/64,000 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) · [Earlier generator settings](parity-fixes.md#square-and-rectangular-pattern-generators) |