From 1b638d4db24af822012e08758cfc442da325c4a6 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Fri, 20 Nov 2026 09:12:27 -0500 Subject: [PATCH] Retain raster precision at the origin of cropped pattern stamps --- crates/nodes/src/pattern/coordinates.rs | 37 +++++++++++++++++++++---- crates/nodes/src/pattern/tests.rs | 28 +++++++++++++++++++ docs/parity-fixes.md | 32 +++++++++++++++++++++ docs/sbs-root-nodes.md | 2 +- 4 files changed, 92 insertions(+), 7 deletions(-) diff --git a/crates/nodes/src/pattern/coordinates.rs b/crates/nodes/src/pattern/coordinates.rs index 6f4fdd7..238af60 100644 --- a/crates/nodes/src/pattern/coordinates.rs +++ b/crates/nodes/src/pattern/coordinates.rs @@ -10,6 +10,7 @@ pub(super) struct PixelPlanes { struct Axis { base: f64, step: f64, + negative_bias: f64, } impl PixelPlanes { @@ -33,6 +34,12 @@ impl PixelPlanes { let far: [f64; 2] = std::array::from_fn(|i| (bounds[i][0] + 2.0 * extents[i] + 0.5).floor() + 1.0); let padded: [f32; 2] = std::array::from_fn(|i| ((far[i] - near[i]) * 0.5) as f32); let reciprocal = area_reciprocal(truncate_float(f64::from(padded[0]) * f64::from(padded[1]))); + let origin = near.map(|n| (n.max(0.0) * 0.25).floor() * 4.0); + let near_offset: [i32; 2] = std::array::from_fn(|i| (origin[i] - near[i]) as i32); + let far_offset: [i32; 2] = std::array::from_fn(|i| (origin[i] - far[i]) as i32); + // The triangle has only one far vertex on each axis. When both offsets fit the same + // number of bits, the vertical far vertex has priority, then the horizontal one. + let far_axis = (0..2).rev().find(|&i| far_offset[i].unsigned_abs().leading_zeros() >= near_offset[i].unsigned_abs().leading_zeros()); let axes = std::array::from_fn(|axis| { let basis = truncate_float(f64::from(padded[1 - axis]) * f64::from(reciprocal)); let center = (bounds[axis][0] + bounds[axis][1]) * 0.5; @@ -40,11 +47,13 @@ impl PixelPlanes { let low = ((near[axis] - center) * scale) as f32; let high = ((far[axis] - center) * scale) as f32; let span = truncate_float(f64::from(high) - f64::from(low)) * 0.5; - let step = f64::from(gradient_product(basis, span)); - let origin = (near[axis].max(0.0) * 0.25).floor() * 4.0; - let base = truncate_float((origin - near[axis]).mul_add(step, f64::from(low))); + let gradient = gradient_product(basis, span); + let unit = f64::from(gradient.next_up() - gradient); + let (offset, vertex) = if far_axis == Some(axis) { (far_offset[axis], high) } else { (near_offset[axis], low) }; + let base = origin_coordinate(offset, gradient, unit, vertex); let sign = f64::from(signs[axis]); - Axis { base: ((0.5 - origin) * step + f64::from(base)) * sign, step: step * sign } + let step = f64::from(gradient); + Axis { base: ((0.5 - origin[axis]) * step + f64::from(base)) * sign, step: step * sign, negative_bias: unit / 64.0 } }); Some(Self { axes, swap }) } @@ -61,11 +70,27 @@ impl PixelPlanes { impl Axis { fn sample(&self, pixel: u32) -> f32 { let value = f64::from(pixel) * self.step + self.base; - let stored = truncate_float(value); - if value < 0.0 && f64::from(stored) == value { stored.next_up() } else { stored } + truncate_float(if value < 0.0 { value + self.negative_bias } else { value }) } } +fn origin_coordinate(offset: i32, gradient: f32, unit: f64, vertex: f32) -> f32 { + let magnitude = offset.unsigned_abs(); + // The integer offset times the 24-bit gradient retains 29 product columns. Each + // partial row loses its low columns before the remaining rows are added. + let low_columns = (u32::BITS - magnitude.leading_zeros()).saturating_sub(5); + let significand = (gradient.to_bits() & 0x7f_ffff) | 0x80_0000; + let mask = (1_u32 << low_columns) - 1; + let mut discarded = 0_u64; + for bit in 0..low_columns { + if magnitude & (1 << bit) != 0 { + discarded += u64::from(significand & (mask >> bit)) << bit; + } + } + let product = f64::from(magnitude) * f64::from(gradient) - discarded as f64 * unit; + truncate_float(f64::from(vertex) + if offset < 0 { -product } else { product }) +} + // Interpolate 1/x from its two endpoints and midpoint in each normalized mantissa interval. // The three samples use quarter units. The squared interpolation fraction retains sixteen bits. fn area_reciprocal(area: f32) -> f32 { diff --git a/crates/nodes/src/pattern/tests.rs b/crates/nodes/src/pattern/tests.rs index 953a9b0..1933dbe 100644 --- a/crates/nodes/src/pattern/tests.rs +++ b/crates/nodes/src/pattern/tests.rs @@ -290,6 +290,34 @@ fn fractional_stamp_edges_set_both_coordinate_gradients() { } } +#[test] +fn wrapped_beveled_stamp_retains_its_coordinate_at_the_crop() { + let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant); + graph.nodes[0].parameters = BTreeMap::from([ + ("patterntype".into(), Value::Int(9)), + ("patternsuppl".into(), Value::Float(0.7265625)), + ("patternsize".into(), Value::Float2([0.984375, 0.484375])), + ("frameoffset".into(), Value::Float2([0.5, 0.25])), + ]); + let image = cook_at_size(graph, &BTreeMap::new(), 261312, 128).unwrap().remove(0); + assert_eq!(image.get_gray(48, 88, false).to_bits(), 0x3e73_2b04); +} + +#[test] +fn clipped_stamp_gradients_use_one_triangle_vertex_for_both_axes() { + let region = super::region::Region::new([0.0, 1.0, 0.0, 1.0]).unwrap(); + for (canvas, center, size, expected) in [ + ([128, 64], [-0.2734375, 0.3125], [0.734375_f32, 0.734375], 0x3f60_ae4e), + ([128, 64], [-0.2734375, 0.3125], [-0.734375, 0.734375], 0x3dfa_8d8c), + ([128, 128], [-0.3046875, -0.265625], [0.7578125, 0.6953125], 0x3f68_3f56), + ] { + let frame = super::footprint::Footprint { sine: 0.0, cosine: 1.0, radius: size.map(|s| s.abs() * 0.5) }; + let planes = super::coordinates::PixelPlanes::new(&frame, ®ion, center, size, canvas).unwrap(); + let [x, _] = planes.row(0)(0); + assert_eq!(((x + 1.0) * 0.5).to_bits(), expected, "center {center:?}, size {size:?}"); + } +} + #[test] fn gaussian_stamp_coordinates_follow_the_pixel_footprint() { let definition = find("fx_map").unwrap(); diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index bb8ae30..f0b5535 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -534,6 +534,38 @@ For each root’s 25 follow-up renders, the 5,120 U8, 553,728 U16, 78,336 F16 an In a single-worker release run, four interleaved measurements of the 128² default, 256² default and 512² filled cases gave median whole-graph cook times of 46, 259 and 1,138 ms for Alveolus (previously 45, 247 and 1,068 ms). Pattern Alveolus took 45, 253 and 1,083 ms (previously 43, 238 and 1,045 ms). +### Cropped origins of axis-aligned stamps + +Brick 1 at 256², tiling 2, edge smoothness .7265625 and interstice width .03125 differed at 64 output positions in each of U16 and F32. Two independently authored bevel stamps reproduce the entire 128² source in each app. For the wrapped stamp use shape 9, size (.984375,.484375), center (0,.75), unit opacity and the same smoothness. At (48,88) its F32 value was `3e732afe` in MaterialCraft and `3e732b04` in the reference. The combined U16 source there was 15,562 versus 15,563. Replacing the bevel by a horizontal ramp isolated the difference to the horizontal coordinate. + +At the four-pixel origin, choose the far vertical triangle vertex when its integer distance fits in as few bits as the vertical distance to the near vertex. Otherwise apply the same comparison horizontally, then use the near vertex if neither qualifies. For each axis, multiply the unsigned offset by the 24-bit gradient significand. Drop the low max(0, bit length of the offset − 5) columns in each partial product before adding the rows. Apply the sign, add the vertex attribute and truncate to F32. For a negative interpolated value add 1/64 of a gradient unit in the last place before the final truncation. The wrapped bevel and signed, doubly clipped gradient assertions exercise the original discrepancy. + +| Original control | Renders | Exact pixels or scalar coordinates | Maximum error | +|---|---:|---:|---:| +| Horizontal and vertical ramps at fractional extents | 669 | 5,050,368/5,050,368 | 0 | +| Seeded independent triangle interpolation, two signed coordinates | 1,024 | 251,364/251,364 | 0 | +| Brick 1, original 68 runs and first 31 follow-ups | 99 | 1,283,584/1,283,584 | 0 in all four formats | +| Brick 1, first 31 follow-ups reopened from a MaterialCraft save | 31 | 856,064/856,064 | 0 in all four formats | +| Brick 1, 12 further parameter settings | 32 | 1,906,474/1,910,784 | 0 U8, 1 U16, 0 F16, .0000153183937 F32 | +| Previously verified roots at their recorded settings | 167 | 3,139,328/3,139,328 | 0 | + +To reproduce the triangle check, start a 32-bit state at `0x31d57b09` and advance with 1,664,525 × state + 1,013,904,223 modulo 2³² before each draw. For j = 0 through 1,023 take W = 19 + draw mod 493, H = 17 + draw mod 389. For K and J use (draw shifted right eight) mod (floor(dimension/2) + 4) − 3. Take two more draws for near attributes u and v, each .5 + ((draw shifted right 16) mod 64)/64. Negate u unless j is divisible by three and v unless j is divisible by five. The far attributes are F32(u × (1 − W/(max(K,3) + t))) and its vertical counterpart, with t = .5 on odd j and zero otherwise. Draw the right triangle with corners (−K,−J), (W−K,−J) and (−K,H−J) onto 16×8. Compare pixel centers with nonnegative barycentric coordinates and normalized horizontal plus vertical distance below .997. + +For the 32 new Brick 1 renders, read each row as width × height, tiling, smoothness and gap. Run F32 and U16 for every row, then U8 and F16 for rows 0, 1, 3 and 7. In that order, seed each format with 286049 + 43 × row + format index. + +| Row | Dimensions, tiling, smoothness, gap | Row | Dimensions, tiling, smoothness, gap | +|---:|---|---:|---| +| 0 | 16×32, 2, .113, .071 | 6 | 256², 5, .917, .287 | +| 1 | 32×64, 1, .199, .183 | 7 | 512×128, 3, .577, .253 | +| 2 | 64×128, 3, .317, .097 | 8 | 512², 1, .431, .107 | +| 3 | 128×32, 4, .489, .139 | 9 | 1024×256, 2, .737, .149 | +| 4 | 128×256, 3, .697, .193 | 10 | 256×512, 3, .349, .039 | +| 5 | 256×128, 2, .811, .063 | 11 | 128², 2, .999, .013 | + +Across the 32, U8 and F16 each match all 72,192 pixels. U16 matches 881,552/883,200 and F32 matches 880,538/883,200. Brick 1 remains in progress. Separately, 200 original F32 ramps with arbitrary fractional centers match 1,346,264/1,392,640 pixels, maximum .000000178813934. For 72 deliberately clipped signed ramps per orientation, 509,590/509,952 pixels match horizontally and vertically, with maxima 1 and 1.00000024. [Earlier sizes, root settings and saved packages](#fractional-edges-of-axis-aligned-pattern-stamps) + +On the recorded set of 17 Crystal 1 renders, 144,967/145,664 pixels match. Directional Noise matches 146,377/147,712 on its 23 renders. Four interleaved single-worker release runs gave median 512² cook times of 1,355 ms for Alveolus and 1,050 ms for Pattern Alveolus, compared with 1,238 and 1,169 ms respectively before this change. All 270 release node tests passed. + ### Fractional edges of axis-aligned pattern stamps Changing only the height of a horizontal ramp changes its left-to-right coordinate step. For a 64² grayscale F32 quadrant, use shape 10, center (.5,.5), size (.375,.23046875), zero rotation and zero roughness. At pixel (21,32) the reference returns `3d800004`. The previous calculation returned `3d800000`. A second control uses size (.23828125,.375), center (.5,.501953125) and a quarter turn. Pixel (20,34) reads `3f27de6c`. The five raw sample assertions fail on the previous cooker and pass with the change. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 8d6586c..ca7ff0c 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -138,7 +138,7 @@ The reference app reopened both intact Alveolus graphs saved with the corrected | [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 · 197,288/204,288 pixels across 34 renders](parity-fixes.md#individual-stamps-in-bnw-spots-1) · [64,000/64,000 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) | | [bnw_spots_3.sbs/bnw_spots_3](../assets/sbs/bnw_spots_3.sbs) | 0 | Yes | [In progress · 180,719/185,856 pixels across 27 renders](parity-fixes.md#fractional-edges-of-axis-aligned-pattern-stamps) · [27,136/27,136 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) | -| [brick_1.sbs/brick_1](../assets/sbs/brick_1.sbs) | 0 | Yes | [In progress · 1,283,456/1,283,584 pixels across 99 renders](parity-fixes.md#fractional-edges-of-axis-aligned-pattern-stamps) · [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) | +| [brick_1.sbs/brick_1](../assets/sbs/brick_1.sbs) | 0 | Yes | [In progress · 3,190,058/3,194,368 pixels across 131 renders](parity-fixes.md#cropped-origins-of-axis-aligned-stamps) · [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) | | [brick_2.sbs/brick_2](../assets/sbs/brick_2.sbs) | 0 | Yes | [In progress · 1,263,116/1,265,152 pixels across 92 renders](parity-fixes.md#fractional-edges-of-axis-aligned-pattern-stamps) · [27,136/27,136 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) · [Earlier generator settings](parity-fixes.md#square-and-rectangular-pattern-generators) | | [brick_3.sbs/brick_3](../assets/sbs/brick_3.sbs) | 0 | Yes | [In progress · 424,442/427,520 pixels across 68 renders](parity-fixes.md#fractional-edges-of-axis-aligned-pattern-stamps) · [64,000/64,000 downstream pixels](parity-fixes.md#isolating-the-generators-in-spots-bricks-and-cells) · [Depth opacity measurements](parity-fixes.md#stored-opacity-across-pattern-depths) | | [brick_generator.sbs/resources/Tile](../assets/sbs/brick_generator.sbs) | 1 | Yes | [In progress · 149,508/150,528 pixels across 74 renders](parity-fixes.md#fractional-edges-of-axis-aligned-pattern-stamps) · [Stamp-width recheck](parity-fixes.md#pixel-reciprocals-for-stamp-edge-widths) |