From 87f2b26dbeafada62fedb6d10b6f9add5575771a Mon Sep 17 00:00:00 2001 From: chasse20 Date: Wed, 18 Nov 2026 15:22:44 -0500 Subject: [PATCH] Filter paired texels in five-texel blur footprints --- crates/nodes/src/atomic/blur.rs | 7 +++--- crates/nodes/src/atomic/box_filter.rs | 24 +++++++++++++++++--- crates/nodes/src/atomic/directional_blur.rs | 6 +++-- crates/nodes/tests/atomic_filter_parity.rs | 14 ++++++++++++ docs/parity-fixes.md | 25 +++++++++++++++++++++ docs/sbs-root-nodes.md | 2 +- 6 files changed, 69 insertions(+), 9 deletions(-) diff --git a/crates/nodes/src/atomic/blur.rs b/crates/nodes/src/atomic/blur.rs index a6c8c4d..a78e493 100644 --- a/crates/nodes/src/atomic/blur.rs +++ b/crates/nodes/src/atomic/blur.rs @@ -18,10 +18,11 @@ fn blur(context: &CookContext) -> CookResult { let radius = context.f("intensity").max(0.0) * context.w.min(context.h) as f32 / BASE_RESOLUTION; let tile = context.tiling_axes(); - if radius > 0.5 && radius <= 1.5 { - let horizontal = box_filter::three_taps(&input, radius, [1, 0], tile); + if radius > 0.5 && radius <= 2.5 { + let source_format = context.input_formats.first().copied().unwrap_or(materialcraft_core::OutputFormat::F32); + let horizontal = box_filter::short_axis(&input, radius, [1, 0], tile, source_format); let stored = crate::output_precision::apply(horizontal, context.format); - return Ok(vec![box_filter::three_taps(&stored, radius, [0, -1], tile)]); + return Ok(vec![box_filter::short_axis(&stored, radius, [0, -1], tile, context.format)]); } Ok(vec![box_filter::axis(&box_filter::axis(&input, radius, true, tile[0]), radius, false, tile[1])]) } diff --git a/crates/nodes/src/atomic/box_filter.rs b/crates/nodes/src/atomic/box_filter.rs index 13338c2..4a13a80 100644 --- a/crates/nodes/src/atomic/box_filter.rs +++ b/crates/nodes/src/atomic/box_filter.rs @@ -1,16 +1,34 @@ use craft_texture::{Image, Limits, MAX_SIDE}; +use materialcraft_core::OutputFormat; use rayon::prelude::*; -pub(super) fn three_taps(input: &Image, radius: f32, step: [i64; 2], tile: [bool; 2]) -> Image { +pub(super) fn short_axis(input: &Image, radius: f32, step: [i64; 2], tile: [bool; 2], source_format: OutputFormat) -> Image { let center_weight = 0.5 / radius; let edge_weight = (radius - 0.5) * center_weight; + // The two passes visit samples in different directions. Cancellation exposes their order. + let combine = |first: [f32; 4], center: [f32; 4], last: [f32; 4]| { + std::array::from_fn(|c| last[c].mul_add(edge_weight, center[c].mul_add(center_weight, first[c] * edge_weight))) + }; + if radius > 1.5 { + // Each side combines its two texels in one filtered read at the source storage precision. + let offset = 2.0 - 1.0 / (radius - 0.5); + let du = step[0] as f32 * offset / input.width() as f32; + let dv = step[1] as f32 * offset / input.height() as f32; + return Image::generate(input.width(), input.height(), input.kind(), |x, y| { + let u = (x as f32 + 0.5) / input.width() as f32; + let v = (y as f32 + 0.5) / input.height() as f32; + let first = crate::texture_sampler::sample(input, [u - du, v - dv], tile, source_format, None); + let center = input.get(i64::from(x), i64::from(y), false); + let last = crate::texture_sampler::sample(input, [u + du, v + dv], tile, source_format, None); + combine(first, center, last) + }); + } Image::generate(input.width(), input.height(), input.kind(), |x, y| { let (x, y) = (i64::from(x), i64::from(y)); let first = input.get_axes(x - step[0], y - step[1], tile); let center = input.get_axes(x, y, tile); let last = input.get_axes(x + step[0], y + step[1], tile); - // The two passes visit samples in different directions. Cancellation exposes their order. - std::array::from_fn(|c| last[c].mul_add(edge_weight, center[c].mul_add(center_weight, first[c] * edge_weight))) + combine(first, center, last) }) } diff --git a/crates/nodes/src/atomic/directional_blur.rs b/crates/nodes/src/atomic/directional_blur.rs index 32dc59c..fa44566 100644 --- a/crates/nodes/src/atomic/directional_blur.rs +++ b/crates/nodes/src/atomic/directional_blur.rs @@ -32,8 +32,10 @@ pub(super) fn blur(context: &CookContext, angle: f32) -> CookResult { let dy = -dy; let tile = context.tiling_axes(); if dy == 0.0 { - if radius <= 1.5 { - return Ok(vec![box_filter::three_taps(&input, radius, [-1, 0], tile)]); + if radius <= 2.5 { + let source_format = context.input_formats.first().copied().unwrap_or(materialcraft_core::OutputFormat::F32); + let step = if radius <= 1.5 { [-1, 0] } else { [1, 0] }; + return Ok(vec![box_filter::short_axis(&input, radius, step, tile, source_format)]); } return Ok(vec![box_filter::axis(&input, radius, true, tile[0])]); } diff --git a/crates/nodes/tests/atomic_filter_parity.rs b/crates/nodes/tests/atomic_filter_parity.rs index 5220750..7cb48cd 100644 --- a/crates/nodes/tests/atomic_filter_parity.rs +++ b/crates/nodes/tests/atomic_filter_parity.rs @@ -226,6 +226,20 @@ fn three_texel_blurs_keep_tap_order_and_intermediate_storage() { assert_eq!(directional.get_gray(32, 32, true).to_bits(), 0xbdfdbf73); } +#[test] +fn five_texel_blurs_filter_the_outer_pairs_before_accumulating() { + let field = Image::generate(128, 128, Kind::Color, |x, y| { + let value = f64::from((97 * x + 43 * y + 7 * x * y) % 1024) / 1023.0; + [f32::from(x == 64 && y == 64), f32::from(x == 64), f32::from(y == 64), (3.0 * value - 1.25) as f32] + }); + let input = [Some(field)]; + let params = [("intensity", Value::Float(4.0))]; + let directional = cook("directional_blur", &input, ¶ms, [128; 2], true); + assert_eq!(directional.get(62, 64, false).map(f32::to_bits), [0x3dff0000, 0x3dff0000, 0x3f800000, 0x3d6739cc]); + let separable = cook("blur", &input, ¶ms, [128; 2], true); + assert_eq!(separable.get(63, 64, false).map(f32::to_bits), [0x3d804000, 0x3e804000, 0x3e800000, 0x3e1e66da]); +} + #[test] fn blur_preserves_finite_hdr_constant() { let input = Image::filled(16, 16, Kind::Gray, [1.0e38; 4]); diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 9682608..c02a142 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -253,6 +253,31 @@ 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). +### Five-texel blur kernels + +| Original controls with 1.5 < radius ≤ 2.5 | Renders | Exact RGBA pixels / compared | Maximum error | +|---|---:|---:|---:| +| Separable impulse, stripes and signed field | 40 | 851,968/851,968 | 0 | +| Zero-angle directional blur on the same fields | 12 | 147,456/147,456 | 0 | +| Separable four-channel field | 80 | 253,952/253,952 | 0 | +| Zero-angle directional four-channel field | 40 | 126,976/126,976 | 0 | +| Shifted stripes, separable and directional | 36 | 1,032,192/1,032,192 | 0 | + +At this radius, sample the center directly and combine the texels on each side with a filtered read at offset 2−1/(radius−.5) texels. The center weight is .5/radius. Each side has weight (radius−.5)×center weight. Filter at the source storage precision, then accumulate in F32 from left to right horizontally or bottom to top vertically. The separable blur stores its horizontal image at the destination precision. On the 128² stripe at radius 2, the outer and inner samples are .12451171875 and .25048828125. The regression also checks the raw F32 channels on the signed field. + +Use the [three-texel inputs](#three-texel-blur-kernels) for the first two rows. For the next two, define H=((33x+87y+5xy+71c+17cx+13cy) mod 1021)/1020 per channel. Use H rounded to the nearest integer level for normalized inputs and 3H−1.375 for floating inputs. Run the separable filter both tiled and untiled. Use radius and seed (1.5009765625,97), (1.71875,137) and (2.375,571) at 32². At 64² use (1.5625,109), (1.9375,211), (2.09375,389) and (2.4990234375,683). Output F32 except at radius 1.9375, which uses all four formats. For the last row, fill the four F32 channels with vertical stripes at x=1, N/8+1, N/2 and N−3. Use 64², 128² and 256², seed 239, and radii 1.50390625, 1.75, 2, 2.0625, 2.25 and 2.5. + +The bevel blur now matches all 154,880 pixels of the original 17 renders, including 128² at intensity 9. Its three blur stages each have radius 2.25 at that setting. Nine further renders extend the range. Define H=((37x+29y+11xy) mod 509)/508. At 32² use intensity 0/seed 53. At 64² use intensity 16/137 with 2.5H−.75. Both match exactly. At 128² run intensity 12.5/307 in all formats and repeat F32 at seed 797. At 256² use intensity 10.75/487. For a 64×32 source at 64² use intensity 24.5/653. + +| Bevel blur with the extended range | Exact pixels / compared | Maximum error | +|---|---:|---:| +| LUM8 | 106,545/122,880 | 3 | +| LUM16 | 8,192/24,576 | 356 | +| LUM16F | 8,192/24,576 | 0.005859375 | +| LUM32F | 37,120/139,520 | 0.00733691454 | + +At radius 3.125, shifting a single vertical impulse by one column exchanges the two measured edge weights .09953125 and .099999994. Those larger footprints still differ. The maximum F32 difference on the extended bevel matrix occurs at 256². + ### Volume density, dithering and color-temperature controls | Root | Renders | Exact RGBA pixels / compared | Maximum U8 / U16 levels | Maximum F16 / F32 error | diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 24cba66..c026092 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -77,7 +77,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [auto_crop.sbs/min_max](../assets/sbs/auto_crop.sbs) | 1 | Yes | [Parity: 3 cases](parity-fixes.md#verified-cases) | | [auto_levels.sbs/auto_levels](../assets/sbs/auto_levels.sbs) | 1 | No | [Parity: 3 cases](parity-fixes.md#verified-cases) | | [auto_levels.sbs/min_max](../assets/sbs/auto_levels.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) | -| [bevel_smooth.sbs/blur_gaussian_grayscale](../assets/sbs/bevel_smooth.sbs) | 1 | Yes | [In progress · 138,496/154,880 pixels, wide F32 kernel open](parity-fixes.md#three-texel-blur-kernels) | +| [bevel_smooth.sbs/blur_gaussian_grayscale](../assets/sbs/bevel_smooth.sbs) | 1 | Yes | [In progress · 160,049/311,552 pixels, higher intensities differ](parity-fixes.md#five-texel-blur-kernels) | | [bevel_smooth.sbs/debug/bevel_smooth_debug_position](../assets/sbs/bevel_smooth.sbs) | 1 | Yes | [In progress · 280,307/285,952 exact pixels](parity-fixes.md#segment-mapping-and-normal-graph-rechecks) | | [bevel_smooth.sbs/jump_flood_segment_iteration](../assets/sbs/bevel_smooth.sbs) | 1 | Yes | [Parity · 171,264 exact pixels in 18 renders](parity-fixes.md#segment-mapping-and-normal-graph-rechecks) | | [blackbody.sbs/blackbody](../assets/sbs/blackbody.sbs) | 0 | No | [In progress · 34,048/42,496 pixels across 19 renders](parity-fixes.md#volume-density-dithering-and-color-temperature-controls) |