From f0e897d625c15bc1cbec18a60b04dad49591fbbc Mon Sep 17 00:00:00 2001 From: chasse20 Date: Thu, 19 Nov 2026 11:23:23 -0500 Subject: [PATCH] Filter tiled oblique blur across stored mip seams --- crates/nodes/src/atomic/box_filter.rs | 11 ++--- .../nodes/src/atomic/box_filter/mip_window.rs | 40 +++++++++++++++- crates/nodes/src/atomic/directional_blur.rs | 8 ++-- crates/nodes/tests/atomic_filter_parity.rs | 24 ++++++++++ docs/parity-fixes.md | 47 +++++++++++++++++++ docs/sbs-root-nodes.md | 10 ++-- 6 files changed, 123 insertions(+), 17 deletions(-) diff --git a/crates/nodes/src/atomic/box_filter.rs b/crates/nodes/src/atomic/box_filter.rs index 5a4c5b9..4b0c4cb 100644 --- a/crates/nodes/src/atomic/box_filter.rs +++ b/crates/nodes/src/atomic/box_filter.rs @@ -4,8 +4,8 @@ use rayon::prelude::*; mod mip_window; -/// A unit-slope shear on a square, periodic image has the same period as the source. -pub(super) fn diagonal(input: &Image, reach: f32, horizontal: bool, slope: f32, source: OutputFormat, destination: OutputFormat) -> Option { +/// Filter a periodic square on a sheared grid, accounting for the transverse shift at a seam. +pub(super) fn sheared(input: &Image, reach: f32, horizontal: bool, slope: f32, source: OutputFormat, destination: OutputFormat) -> Option { let length = input.width(); let radius = reach + 0.5; if length != input.height() || !length.is_power_of_two() || !radius.is_finite() || radius > length as f32 { @@ -16,10 +16,7 @@ pub(super) fn diagonal(input: &Image, reach: f32, horizontal: bool, slope: f32, if !input.data().iter().all(|v| v.abs() <= limit) { return None; } - let intermediate = match destination { - OutputFormat::U8 | OutputFormat::U16 | OutputFormat::Inherit => OutputFormat::U16, - _ => destination, - }; + let intermediate = if destination == OutputFormat::Inherit { OutputFormat::U16 } else { destination }; let shear = |image: &Image, slope: f32, format| { Image::generate_uv(length, length, image.kind(), |u, v| { let uv = if horizontal { [u, v + slope * (u - 0.5)] } else { [u + slope * (v - 0.5), v] }; @@ -27,7 +24,7 @@ pub(super) fn diagonal(input: &Image, reach: f32, horizontal: bool, slope: f32, }) }; let displaced = crate::output_precision::apply(shear(input, slope, source), intermediate); - let filtered = mip_window::filter(&displaced, reach, horizontal, true, intermediate, intermediate, mip_window::Edges::Separate); + let filtered = mip_window::filter(&displaced, reach, horizontal, true, intermediate, intermediate, mip_window::Edges::Sheared(slope)); let filtered = crate::output_precision::apply(filtered, intermediate); Some(shear(&filtered, -slope, intermediate)) } diff --git a/crates/nodes/src/atomic/box_filter/mip_window.rs b/crates/nodes/src/atomic/box_filter/mip_window.rs index 10526c1..bb0b7a2 100644 --- a/crates/nodes/src/atomic/box_filter/mip_window.rs +++ b/crates/nodes/src/atomic/box_filter/mip_window.rs @@ -5,6 +5,7 @@ use materialcraft_core::OutputFormat; enum Read { Texel { level: usize, position: i64 }, Filtered(f32), + AcrossSeam { level: usize, along: f32, shift: f32 }, } struct Tap { @@ -15,13 +16,25 @@ struct Tap { #[derive(Clone, Copy)] pub(super) enum Edges { Paired, - Separate, + Sheared(f32), } impl Tap { fn texel(position: i64, width: u32, weight: f32) -> Self { Self { read: Read::Texel { level: width.ilog2() as usize, position: position.div_euclid(i64::from(width)) }, weight } } + + fn wrap_sheared(&mut self, length: u32, slope: f32) { + let Read::Texel { level, position } = self.read else { return }; + let count = i64::from(length >> level); + let turns = position.div_euclid(count); + if turns != 0 { + // Crossing a period on the sheared grid shifts the other coordinate. + // Interpolate the already stored mip at that shift, including for endpoint taps. + let along = (position.rem_euclid(count) as f32 + 0.5) / count as f32; + self.read = Read::AcrossSeam { level, along, shift: turns as f32 * slope }; + } + } } fn window(position: u32, length: u32, reach: f32, tile: bool, edges: Edges) -> Vec { @@ -84,6 +97,13 @@ pub(super) fn filter(input: &Image, reach: f32, horizontal: bool, tile: bool, so let windows: Vec<_> = (0..length) .map(|position| { let mut taps = window(position, length, reach, tile, edges); + if let Edges::Sheared(slope) = edges + && slope.fract() != 0.0 + { + for tap in &mut taps { + tap.wrap_sheared(length, slope); + } + } if horizontal { taps.reverse(); } @@ -91,6 +111,13 @@ pub(super) fn filter(input: &Image, reach: f32, horizontal: bool, tile: bool, so }) .collect(); let scale = 0.5 / radius; + // On a sheared grid the normalized integer window includes a positive storage offset. + // A one-level pulse exposes it before the inverse shear averages neighboring texels. + let storage_offset = match (edges, destination) { + (Edges::Sheared(_), OutputFormat::U8) => Some(0.5 / 256.0), + (Edges::Sheared(_), OutputFormat::U16 | OutputFormat::Inherit) => Some(0.4 / 65535.0), + _ => None, + }; Image::generate(input.width(), input.height(), input.kind(), |x, y| { let (position, across, other_size) = if horizontal { (x, y, input.height()) } else { (y, x, input.width()) }; let other_uv = (across as f32 + 0.5) / other_size as f32; @@ -103,6 +130,12 @@ pub(super) fn filter(input: &Image, reach: f32, horizontal: bool, tile: bool, so let xy = if horizontal { [uv, other_uv] } else { [other_uv, uv] }; texture_sampler::sample(input, xy, [tile; 2], source, None) } + Read::AcrossSeam { level, along, shift } => { + let across = other_uv + shift; + let xy = if horizontal { [along, across] } else { [across, along] }; + let format = if level == 0 { source } else { destination }; + texture_sampler::sample(levels.get(level).unwrap_or(input), xy, [true; 2], format, None) + } }; let Some((first, rest)) = windows.get(position as usize).and_then(|taps| taps.split_first()) else { return input.get(i64::from(x), i64::from(y), false); @@ -113,6 +146,9 @@ pub(super) fn filter(input: &Image, reach: f32, horizontal: bool, tile: bool, so *total = v.mul_add(tap.weight, *total); } } - sum.map(|v| v * scale) + match storage_offset { + Some(offset) => sum.map(|v| v.mul_add(scale, offset)), + None => sum.map(|v| v * scale), + } }) } diff --git a/crates/nodes/src/atomic/directional_blur.rs b/crates/nodes/src/atomic/directional_blur.rs index bdf3029..7bcbc62 100644 --- a/crates/nodes/src/atomic/directional_blur.rs +++ b/crates/nodes/src/atomic/directional_blur.rs @@ -61,12 +61,14 @@ pub(super) fn blur(context: &CookContext, angle: f32) -> CookResult { let span = -u.abs().max(v.abs()).copysign(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() && (angle * 8.0).fract() == 0.0 { + if tile == [true; 2] && context.w == input.width() && context.h == input.height() { // Compare the components before rounding the direction to the sample grid. let (sine, cosine) = f64::from(angle * std::f32::consts::TAU).sin_cos(); let horizontal = cosine.abs() > sine.abs(); - let slope = if sine.is_sign_negative() == cosine.is_sign_negative() { 1.0 } else { -1.0 }; - if let Some(image) = box_filter::diagonal(&input, reach, horizontal, slope, source_format, context.format) { + let slope = if horizontal { sine / cosine } else { cosine / sine }; + // An authored eighth turn has an exact unit slope even after radian conversion. + let slope = if (angle * 8.0).fract() == 0.0 { slope.signum() as f32 } else { slope as f32 }; + if let Some(image) = box_filter::sheared(&input, reach, horizontal, slope, source_format, context.format) { return Ok(vec![image]); } } diff --git a/crates/nodes/tests/atomic_filter_parity.rs b/crates/nodes/tests/atomic_filter_parity.rs index 092da1c..734e3fa 100644 --- a/crates/nodes/tests/atomic_filter_parity.rs +++ b/crates/nodes/tests/atomic_filter_parity.rs @@ -324,6 +324,30 @@ fn wide_diagonal_blur_filters_between_two_sheared_grids() { } } +#[test] +fn diagonal_blur_stores_the_integer_window_before_resampling() { + let input = [Some(Image::gray(32, 32, |_, y| if y % 8 == 0 { 1.0 / 255.0 } else { 0.0 }))]; + for (format, strength, maximum, expected) in + [(OutputFormat::U8, 19.0, 255.0, [0, 0, 1, 0, 0, 0, 1, 1]), (OutputFormat::U16, 48.0, 65535.0, [30, 30, 30, 30, 52, 30, 30, 30])] + { + let settings = [("intensity", Value::Float(strength)), ("angle", Value::Float(0.125))]; + let image = cook_format("directional_blur", &input, &settings, [32; 2], true, format); + let levels: [u32; 8] = std::array::from_fn(|y| (image.get_gray(0, y as i64, false) * maximum).round() as u32); + assert_eq!(levels, expected, "{format:?}"); + } +} + +#[test] +fn oblique_blur_samples_stored_levels_across_the_tile_seam() { + let input = [Some(impulse_and_stripes())]; + let params = [("intensity", Value::Float(10.75)), ("angle", Value::Float(0.0625))]; + let blurred = cook_format("directional_blur", &input, ¶ms, [128; 2], true, OutputFormat::U8); + let stored = materialcraft_nodes::output_precision::apply(blurred, OutputFormat::U8); + for (x, y, expected) in [(0, 65, [0, 0, 57, 116]), (127, 52, [0, 0, 0, 88]), (64, 64, [17, 26, 61, 99])] { + assert_eq!(stored.get(x, y, false).map(|v| (v * 255.0).round() as u8), expected, "({x}, {y})"); + } +} + #[test] fn short_oblique_blur_filters_fractional_taps_and_preserves_the_image_aspect() { let input = [Some(impulse_and_stripes())]; diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 75ac845..d3adc7d 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -59,6 +59,53 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas ## 2026-11-19 root comparisons +### Integer and oblique directional blur + +| Root | Renders | Previous exact pixels | Exact / compared | Maximum error | +|---|---:|---:|---:|---:| +| `anisotropic_blur.sbs/anisotropic_blur` | 29 | 145,109 | 200,192/200,192 | 0 | +| `anisotropic_blur.sbs/anisotropic_blur_grayscale` | 29 | 165,764 | 200,192/200,192 | 0 | +| `blur_hq.sbs/blur_hq` | 18 | 65,482 | 171,264/171,264 | 0 | +| `blur_hq.sbs/blur_hq_grayscale` | 18 | 128,880 | 171,264/171,264 | 0 | + +On the 128² anisotropic color and grayscale inputs at angle .0625, intensity 17.5, anisotropy .625 and high quality, the first directional pass operates at intensity 10.5. Look at the raw U8 output of that pass. Before the change, 191/16,384 color and 2,125/16,384 grayscale pixels matched. On both inputs all 16,384 now match. A sheared sampling grid crosses the image boundary at a different position on the other axis. Wrap each tap at its own stored mip level and bilinearly sample the transverse shift. Recomputing the padded edge from the original bitmap still misses 675 color and 290 grayscale pixels, with a maximum error of two byte levels. + +An integer diagonal blur stores the first shear and each reduction at the destination depth. In the window normalization, adding half of 1/256 before the U8 store reproduces the measured byte controls. The measured U16 offset is approximately .4 of one stored level. To isolate the rounding, generate a 32² image whose rows divisible by eight contain 1/255, with all other rows zero. Set angle .125 and both axes to repeat. At intensity 19 the U8 output at x=0 for the first eight rows is `0,0,1,0,0,0,1,1`. At intensity 48 the U16 levels are `30,30,30,30,52,30,30,30`. For the edge test use the 128² signed [impulse-and-stripes field](#tiled-diagonal-directional-blur), angle .0625 and intensity 10.75. Pixel (0,65) is RGBA8 (0,0,57,116), (127,52) is (0,0,0,88), and (64,64) is (17,26,61,99). + +All four roots include the [archive and four-format comparisons](#tiled-diagonal-directional-blur), the independent 32² input and nine further fields. For those nine use `H=((37x+41y+7xy+61c+5xc) mod 509)/508` for color channel c. Use c=0 for grayscale RGB with opaque alpha. Render defaults at 16²/43. At 64²/157 render all four formats at intensity 5.25 with high quality. At 128²/293 use intensity 18.5 with high quality off. At 32²/463 use signed input `2.5H−.75` and intensity 10.25 with high quality. Apply the signed transformation to every channel. Repeat the 128² settings at 32²/599 with a 32×16 input and the 64² settings at seed 751. On the anisotropic roots the three changed intensity settings use anisotropy/angle .6875/.0625, .3125/.15625 and 1/−.21875. + +The anisotropic roots include twelve additional fields. Use `H=((73x+37y+11xy+157+43c+3cx) mod 1021)/1020`, with c=0 for the grayscale image. Columns for each case below give square side, seed, intensity, anisotropy, angle in turns, quality and output. Apply `2.5H−.875` to both signed inputs. + +| Side | Seed | Intensity | Anisotropy | Angle | High quality | Output | +|---:|---:|---:|---:|---:|---|---| +| 16 | 2833 | 10 | .5 | 0 | Off | F32 | +| 32 | 2953 | 10 | .5 | 0 | Off | U8 | +| 64 | 3001 | 24 | .8125 | .125 | Off | U8, U16, F16, F32 | +| 128 | 3049 | 17.5 | .625 | .0625 | On | F32, signed | +| 32 | 3119 | 5.375 | .375 | −.1875 | Off | F32, signed | +| 32 | 3203 | 0 | 1 | .03125 | On | F32 | +| 64 | 3251 | 27 | 0 | −.375 | On | F32 | +| 64 | 3271 | 24 | .8125 | .125 | Off | F32 | +| 32 | 3313 | 13 | .75 | .1875 | On | F32 | + +### Direct blur controls and remaining differences + +| Procedural control | Renders | Previous exact pixels | Current exact / compared | Maximum U8 / U16 / F16 / F32 error | +|---|---:|---:|---:|---| +| Tiled eighth turns | 96 | 1,046,248 | 1,158,974/1,351,680 | 0 / 1 / 0.00048828125 / 3.57627869e-07 | +| Tiled .0625 and .21875 turns | 24 | 2,625 | 280,184/337,920 | 1 / 74 / 0.00146484375 / 0.00263102353 | +| Untiled .125 turns | 12 | 5,523 | 5,523/168,960 | 192 / 49,199 / 1.09570312 / 1.09612465 | +| Independent stripes and projected axial controls | 64 | 134,690 | 160,571/163,840 | 0 / 0 / 3.81469727e-06 / 5.96046448e-08 | +| Code patches, low pulses and translated row fields | 80 | 1,200,293 | 1,454,692/1,495,040 | 0 / 1 / — / 1.78813934e-07 | + +The [impulse and stripes controls](#tiled-diagonal-directional-blur) specify the input, strength and seed for the first three rows. In the 96 tiled eighth-turn renders, all 163,840 RGBA8 pixels match. Of 163,840 RGBA16 pixels, 90 still differ by one level. Off-axis filtering has further differences at .21875 turns. Untiled boundaries and non-square processing grids still use the direct sampling path. + +For the independent stripes use U8 inputs at (side, radius) (32,2.375), (64,3.125), (64,6) and (32,5.375). Set R=(53x+29x²+19) mod 256, G=(31y+7y²+17) mod 256, B to 208 or 23 according to the parity of x+y, and A to 127. The second field uses 1 or 2 by x parity, 9 or 2 by y parity, (x+3y) mod 17 and 127+(x(y+1) mod 3). Set intensity to 256×radius/side at .125 turns. For the first field also render horizontal and vertical controls with radius .5+(radius−.5)/√2, across all four formats at seeds 3359 through 3362. + +For the code patches, assign k=16⌊y/16⌋+⌊x/16⌋ on a 256² field and store channels (k,k+1,k+67,k+129) modulo 256. The other inputs are 64² row fields or a 32² one-level pulse across four base levels. One row field uses `((31y+7y²+17) mod 128)+32+c`. The other uses `U=((117y+53y²+17) mod 251)`, with channels U, (U+12) mod 256 and the transposes of both. In the pulse field add one to (0,4,45,184) every eighth row. For the U16 inputs, scale each code by 257 and add 29c below code 255. Use the same four radii at .125 turns and seeds 3433–3436 in field order. Store each U8 source in U8, U16 and F32, and each U16 source in U16 and F32. All 299,008 U8 output pixels in this set match. + +The 38 instrumented color and grayscale anisotropic renders compare 304 graph images, including the input and output bridges. 2,560,000/2,560,000 pixels match, maximum stored-component error 0. Six other roots with a reachable directional blur retain all 790,528 exact pixels in 52 renders. + ### Bitmap stamp filtering and compositing A quadrant with an image pattern uses automatic bilinear mip filtering when `imagefiltering` is omitted or zero. Mode 1 selects the original level with bilinear filtering. Mode 2 selects the nearest texel. Filter taps outside the bitmap use transparent black. Intermediate reductions store each level at the source precision, which matters even when the final output is F32. A grayscale stamp needs a grayscale image and a color stamp needs a color image. Wiring the opposite kind leaves the background intact. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 947ee7d..9402a33 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 **347 Parity**, **69 In progress** and **130 Too hard**. The November 17–19 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) · [November 19 measurements](parity-fixes.md#2026-11-19-root-comparisons) +The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **351 Parity**, **65 In progress** and **130 Too hard**. The November 17–19 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) · [November 19 measurements](parity-fixes.md#2026-11-19-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) @@ -72,8 +72,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [3d_worley_noise.sbs/3D_worley_noise](../assets/sbs/3d_worley_noise.sbs) | 1 | No | [Parity · 34 renders, 312,832 exact pixels](parity-fixes.md#per-pixel-random-initialization) | | [alveolus.sbs/alveolus](../assets/sbs/alveolus.sbs) | 0 | Yes | [In progress · 300,544/302,336 pixels across 26 renders](parity-fixes.md#radial-stamp-profiles-and-pattern-generators) | | [angle_to_vector.sbs/angle_to_vector](../assets/sbs/angle_to_vector.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | -| [anisotropic_blur.sbs/anisotropic_blur](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [In progress · 112,190/115,712 pixels in 8 renders](parity-fixes.md#tiled-diagonal-directional-blur) | -| [anisotropic_blur.sbs/anisotropic_blur_grayscale](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [In progress · 112,563/115,712 pixels in 8 renders](parity-fixes.md#tiled-diagonal-directional-blur) | +| [anisotropic_blur.sbs/anisotropic_blur](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [Parity · 29 renders, 200,192 exact pixels across four formats](parity-fixes.md#integer-and-oblique-directional-blur) | +| [anisotropic_blur.sbs/anisotropic_blur_grayscale](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [Parity · 29 renders, 200,192 exact pixels across four formats](parity-fixes.md#integer-and-oblique-directional-blur) | | [atlas_spliter.sbs/crop_and_transform_sub](../assets/sbs/atlas_spliter.sbs) | 7 | Yes | [Parity · 37 wired cases, 378,880 exact pixels](parity-fixes.md#runtime-pixel-sine-cosine-and-atlas-rotation) | | [atlas_spliter.sbs/max_ba](../assets/sbs/atlas_spliter.sbs) | 1 | Yes | [Parity: 3 cases](parity-fixes.md#verified-cases) | | [auto_crop.sbs/min_max](../assets/sbs/auto_crop.sbs) | 1 | Yes | [Parity: 3 cases](parity-fixes.md#verified-cases) | @@ -106,8 +106,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [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 · 40,496/132,096 pixels in 9 renders](parity-fixes.md#tiled-diagonal-directional-blur) | -| [blur_hq.sbs/blur_hq_grayscale](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 96,534/132,096 pixels in 9 renders](parity-fixes.md#tiled-diagonal-directional-blur) | +| [blur_hq.sbs/blur_hq](../assets/sbs/blur_hq.sbs) | 1 | No | [Parity · 18 renders, 171,264 exact pixels across four formats](parity-fixes.md#integer-and-oblique-directional-blur) | +| [blur_hq.sbs/blur_hq_grayscale](../assets/sbs/blur_hq.sbs) | 1 | No | [Parity · 18 renders, 171,264 exact pixels across four formats](parity-fixes.md#integer-and-oblique-directional-blur) | | [bnw_spots_1.sbs/bnw_spots_1](../assets/sbs/bnw_spots_1.sbs) | 0 | Yes | [In progress · 19 renders, 131,316/172,288 exact pixels](parity-fixes.md#filtering-unequal-inputs-to-a-blend) | | [bnw_spots_3.sbs/bnw_spots_3](../assets/sbs/bnw_spots_3.sbs) | 0 | Yes | [In progress · 19 renders, 124,148/172,288 exact pixels](parity-fixes.md#filtering-unequal-inputs-to-a-blend) | | [brick_1.sbs/brick_1](../assets/sbs/brick_1.sbs) | 0 | Yes | [In progress · 170,632/172,288 pixels, edge coordinates before U16 storage](parity-fixes.md#brick-edge-distances-and-storage-precision) |