diff --git a/crates/nodes/src/pattern/footprint.rs b/crates/nodes/src/pattern/footprint.rs index 67b40a5e..275f5934 100644 --- a/crates/nodes/src/pattern/footprint.rs +++ b/crates/nodes/src/pattern/footprint.rs @@ -49,46 +49,60 @@ impl Footprint { } pub(super) struct SampleGrid { - region: Region, + render: Region, + tiling: Region, dimensions: [u32; 2], axes: [bool; 2], + cull_outside: bool, } impl SampleGrid { pub fn new(context: &CookContext, graph_nodes: usize) -> Option { - let unit = [0.0, 1.0, 0.0, 1.0]; let roughness = context.f("roughness"); - // Retain ordinary raster validation for nonunit regions and depth weights that might overflow. - if context.v4("render_region") != unit - || context.v4("tiling_region") != unit - || !(roughness >= 0.0 && roughness.is_finite() && context.f("opacity").abs() <= 1.0) + // Retain ordinary raster validation for depth weights that might overflow. + if !(roughness >= 0.0 && roughness.is_finite() && context.f("opacity").abs() <= 1.0) || (roughness > 1.0 && !roughness.powf(graph_nodes as f32).is_finite()) { return None; } let (width, height) = craft_texture::clamp_size(context.w, context.h); - Some(Self { region: Region::new(unit).ok()?, dimensions: [width, height], axes: context.tiling_axes() }) + let axes = context.tiling_axes(); + let cull_outside = context.b("cull_outside"); + let render = Region::new(context.v4("render_region")).ok()?; + let tiling = Region::tiling(context.v4("tiling_region"), axes, cull_outside).ok()?; + Some(Self { render, tiling, dimensions: [width, height], axes, cull_outside }) } pub fn misses(&self, stamp: &PatternStamp) -> bool { if !(2..=18).contains(&stamp.pattern) || !(0..=2).contains(&stamp.blend) - || !stamp.center.iter().chain(&stamp.size).chain(&stamp.color).chain([&stamp.variation]).all(|v| v.is_finite()) + || !stamp.center.iter().chain(&stamp.size).chain(&stamp.color).chain([&stamp.rotation, &stamp.variation]).all(|v| v.is_finite()) { return false; } + if self.cull_outside && !self.tiling.contains(stamp.center) { + return true; + } let Ok(frame) = Footprint::new(stamp) else { return false }; // Limit this check to footprints spanning at most two sample columns and rows per copy. - if frame.radius.iter().zip(self.dimensions).any(|(&r, n)| r * n as f32 > 0.5) { + if (0..2).any(|i| frame.radius[i] * self.dimensions[i] as f32 > 0.5 * self.render.period()[i]) { + return false; + } + let center = self.tiling.copy_origin(stamp.center, self.axes); + let Ok(repeats) = self.tiling.repeats(&self.render, center, frame.radius, self.axes) else { return false }; + let copies = repeats[0].count.saturating_mul(repeats[1].count); + if copies == 0 { + return true; + } + // A small tiling period can place many copies in a single pixel. Leave those to the raster budget. + if copies > 4 { return false; } - let center = self.region.copy_origin(stamp.center, self.axes); - let Ok(repeats) = self.region.repeats(&self.region, center, frame.radius, self.axes) else { return false }; let columns = repeats[0].centers(); for y_center in repeats[1].centers() { for x_center in columns.clone() { let center = [x_center, y_center]; - let bounds = self.region.bounds(center, frame.radius, self.dimensions); + let bounds = self.render.bounds(center, frame.radius, self.dimensions); let [[left, right], [top, bottom]] = bounds; if left == right || top == bottom { continue; @@ -100,12 +114,13 @@ impl SampleGrid { let low = center[axis] - frame.radius[axis]; let end = bounds[axis][1]; let pixels = self.dimensions[axis]; - f64::from(low * pixels as f32) + 0.5 >= f64::from(end) && self.region.pixel_center(axis, end - 1, pixels) <= low + let near = self.render.stamp_vertices(axis, center[axis], frame.radius[axis], pixels)[0]; + f64::from(near) + 0.5 >= f64::from(end) && self.render.pixel_center(axis, end - 1, pixels) <= low }) { continue; } - if let Some(planes) = PixelPlanes::new(&frame, &self.region, center, stamp.size, self.dimensions) { + if let Some(planes) = PixelPlanes::new(&frame, &self.render, center, stamp.size, self.dimensions) { if planes.covers_any(bounds) { return false; } @@ -113,7 +128,7 @@ impl SampleGrid { } for y in top..bottom { for x in left..right { - if frame.local(self.region.point([x, y], self.dimensions), center, stamp.size).is_some() { + if frame.local(self.render.point([x, y], self.dimensions), center, stamp.size).is_some() { return false; } } diff --git a/crates/nodes/src/pattern/raster.rs b/crates/nodes/src/pattern/raster.rs index 726bc6c8..57d72152 100644 --- a/crates/nodes/src/pattern/raster.rs +++ b/crates/nodes/src/pattern/raster.rs @@ -43,13 +43,7 @@ pub(super) fn render(context: &CookContext, groups: [&[PatternStamp]; 5], max_de let pixel_size = [span[0] / width as f32, span[1] / height as f32]; let axes = context.tiling_axes(); let cull_outside = context.b("cull_outside"); - let mut tiling = context.v4("tiling_region"); - for (interval, enabled) in tiling.as_chunks_mut::<2>().0.iter_mut().zip(axes) { - if !enabled && !cull_outside { - *interval = [0.0, 1.0]; - } - } - let tile_region = Region::new(tiling)?; + let tile_region = Region::tiling(context.v4("tiling_region"), axes, cull_outside)?; let mut mipmaps = bitmap::Mipmaps::new(); for stamp in groups.into_iter().flatten() { diff --git a/crates/nodes/src/pattern/region.rs b/crates/nodes/src/pattern/region.rs index 34d8be52..b768f453 100644 --- a/crates/nodes/src/pattern/region.rs +++ b/crates/nodes/src/pattern/region.rs @@ -23,6 +23,15 @@ impl Copies { } impl Region { + pub fn tiling(mut value: [f32; 4], axes: [bool; 2], cull_outside: bool) -> Result { + for (interval, enabled) in value.as_chunks_mut::<2>().0.iter_mut().zip(axes) { + if !enabled && !cull_outside { + *interval = [0.0, 1.0]; + } + } + Self::new(value) + } + pub fn new(value: [f32; 4]) -> Result { let mut start = [value[0], value[2]]; let mut end = [value[1], value[3]]; diff --git a/crates/nodes/src/pattern/tests.rs b/crates/nodes/src/pattern/tests.rs index 51f1b1bd..012c30c4 100644 --- a/crates/nodes/src/pattern/tests.rs +++ b/crates/nodes/src/pattern/tests.rs @@ -1383,8 +1383,11 @@ fn deep_quadrants_draw_only_the_leaves_covering_output_samples() { } let increment = 1.0 / 1_048_576.0; graph.nodes[10].parameters = BTreeMap::from([("patterntype".into(), Value::Int(2)), ("opacity".into(), Value::Float(increment))]); - let image = cook_at_size(graph, &BTreeMap::new(), 107185, 4).unwrap().remove(0); + let image = cook_at_size(graph.clone(), &BTreeMap::new(), 107185, 4).unwrap().remove(0); assert_eq!(image.data(), &[increment; 16]); + let expanded = [-0.5, 1.5, -0.5, 1.5]; + let image = cook_in_regions(graph, &BTreeMap::new(), 417341, [4; 2], [expanded; 2], 0.0).unwrap().remove(0); + assert_eq!(image.data(), &[0.0, 0.0, 0.0, 0.0, 0.0, increment, increment, 0.0, 0.0, increment, increment, 0.0, 0.0, 0.0, 0.0, 0.0]); } #[test] diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 4207c4fb..a2ae0045 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -161,6 +161,25 @@ Four roots also have downstream stages. Replace the source patterns with the ori | mesh_2 | 8,984/10,496 | 0 / 1 / 0.25 / 1.52587891e-05 | | polygon_2 | 10,496/10,496 | 0 / 0 / 0 / 0 | +### Deep pattern samples in custom render and tiling regions + +The FX-Map traversal checks tiny procedural stamps against the actual render and tiling rectangles. A discarded stamp must miss every candidate output sample of every visible copy, or have its center culled by the tiling region. The raster and traversal use the same region rules, including reversed intervals and disabled tiling axes. A stamp with more than four candidate copies goes through ordinary raster accounting. The visit, discard, copy and pixel limits stay in effect. + +Generate ten levels of empty quadrants. At each level connect all four slots to the next quadrant. Draw an additive axis-aligned gray square of local size (1,1) at each leaf, opacity 2⁻²⁰, zero roughness, global opacity one, over zero. Repeat with nine levels. For each table row j, render 4² F32, 8×4 U16, 4×8 F32 and 16² F32 with seed 417341+43j+k at format index k=0…3. + +| j | Depth | Render region (left, right, top, bottom) | Tiling region | Tiled axes | Cull outside | Previous result | Exact / total after | Maximum error | +|---:|---:|---|---|---|---|---|---:|---:| +| 0 | 10 | (-0.5, 1.5, -0.5, 1.5) | (-0.5, 1.5, -0.5, 1.5) | both | off | 4 visit errors | 336/336 | 0 | +| 1 | 10 | (0.125, 0.625, -0.25, 0.75) | (0, 1, 0, 1) | both | off | 4 visit errors | 336/336 | 0 | +| 2 | 10 | (-0.25, 0.75, 0.25, 1.25) | (-0.25, 0.75, 0.25, 1.25) | x | off | 4 visit errors | 336/336 | 0 | +| 3 | 9 | (0.125, 0.625, -0.25, 0.75) | (-0.25, 0.75, 0, 0.5) | both | on | 336/336 | 336/336 | 0 | +| 4 | 9 | (0.625, 0.125, 0.875, 0.25) | (0, 1, 0, 1) | y | off | 336/336 | 336/336 | 0 | +| 5 | 9 | (0, 1, 0, 1) | (0.25, 0.75, 0.125, 0.625) | none | on | 336/336 | 336/336 | 0 | + +In the expanded window the four central samples of the 4×4 F32 image equal 2⁻²⁰ and the twelve outer samples are zero. Before the change this regression returned the traversal-budget error. The two unit-window controls matched 32/32. Another 696 original rectangle, tiling, ramp, square and falloff controls match **1,966,080/1,966,080 F32 pixels**, maximum zero, before and after the change. [Original traversal and budget controls](#bounded-traversal-of-deep-patterns) + +At 32² F32, seed 113, scale 2 and disorder .37, the first BnW Spots 3 and first Dirt 5 noise packages (inventory indices 238 and 268) now finish with 1,024/1,024 exact pixels each, maximum zero. Both previously returned the visit error. At 64², seed 42, BnW Spots 3 also finishes in all four output formats with the same scale and disorder. U8 matches 4,096/4,096, U16 4,094/4,096 (maximum 1), F16 4,096/4,096 and F32 4,093/4,096 (maximum 1.06692314e−5). The companion packages at 239 and 269 still reach the visit guard for their independent 32² cases with disorder speed .6, anisotropy .3, anisotropy angle .125 and tile offset (.17,.29). Both Directional Noise 3, Dirt 1 and Dirt 4 variants also reach the guard in their independent 32² cases. + ### Bounded traversal of deep patterns Four roots reached the traversal guard even at 4×4. An instrumented release cook raised the visit ceiling from 1,048,576 to 2,097,152 and the raster copy ceiling from 1,048,576 to 8,388,608 for this experiment. The 256,000,000-pixel-work guard remained active. Use defaults, F32, and seeds 106622, 106635, 106765 and 106778 in table order. Each process cooked one graph on one worker. Peak resident memory includes graph loading. The visit and stamp counts are for the most expensive pattern stage. Times are single observations. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index ece54cde..c6b997b9 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -337,7 +337,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug | [noise_anisotropic_noise_v2.sbs/anisotropic_noise_v2](../assets/sbs/noise_anisotropic_noise_v2.sbs) | 0 | No | [Parity · 18 renders, all 171,264 pixels exact](parity-fixes.md#high-pass-anisotropic-noise-and-triangle-grid-controls) | | [noise_bnw_spots_1.sbs/bnw_spots_1](../assets/sbs/noise_bnw_spots_1.sbs) | 0 | Yes | [In progress · all nine renders cook, 3,216/132,096 exact pixels](parity-fixes.md#empty-size-dependent-pattern-branches) | | [noise_bnw_spots_1_v2.sbs/bnw_spots_1](../assets/sbs/noise_bnw_spots_1_v2.sbs) | 0 | No | [In progress · nine renders, 41,745/132,096 exact pixels](parity-fixes.md#pattern-backgrounds-and-center-culling) | -| [noise_bnw_spots_3.sbs/bnw_spots_3](../assets/sbs/noise_bnw_spots_3.sbs) | 0 | Yes | [In progress · 104,343/107,776 pixels, 13 execution-limit cases](parity-fixes.md#bnw-spots-3-and-cells-1-pattern-sources) | +| [noise_bnw_spots_3.sbs/bnw_spots_3](../assets/sbs/noise_bnw_spots_3.sbs) | 0 | Yes | [In progress · 32² now exact, 64² precision differences](parity-fixes.md#deep-pattern-samples-in-custom-render-and-tiling-regions) · [Earlier matrix](parity-fixes.md#bnw-spots-3-and-cells-1-pattern-sources) | | [noise_bnw_spots_3_v2.sbs/bnw_spots_3](../assets/sbs/noise_bnw_spots_3_v2.sbs) | 0 | No | [In progress · 106,559/107,776 pixels, 13 execution-limit cases](parity-fixes.md#bnw-spots-3-and-cells-1-pattern-sources) | | [noise_cells_1.sbs/cells_1](../assets/sbs/noise_cells_1.sbs) | 0 | Yes | [In progress · 143,321/153,856 pixels in 19 renders](parity-fixes.md#bnw-spots-3-and-cells-1-pattern-sources) | | [noise_cells_1_v2.sbs/cells_1](../assets/sbs/noise_cells_1_v2.sbs) | 0 | No | [In progress · 20 renders, 158,728/173,312 exact pixels](parity-fixes.md#wire-presence-inside-a-called-function) · [Stamp-width recheck](parity-fixes.md#pixel-reciprocals-for-stamp-edge-widths) | @@ -367,7 +367,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug | [noise_dirt_3_v2.sbs/dirt_3](../assets/sbs/noise_dirt_3_v2.sbs) | 0 | No | [In progress · 17/19 whole outputs, 136,738/141,568 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) | | [noise_dirt_4.sbs/dirt_4](../assets/sbs/noise_dirt_4.sbs) | 0 | Yes | [In progress · 10/19 whole renders completed, 89,052/105,728 exact pixels](parity-fixes.md#discarding-pattern-stamps-that-miss-every-output-sample) · [Original stage comparisons](parity-fixes.md#dirt-noise-sources-and-downstream-stages) | | [noise_dirt_4_v2.sbs/dirt_4](../assets/sbs/noise_dirt_4_v2.sbs) | 0 | No | [In progress · 5/19 whole renders completed, 99,535/99,584 exact pixels](parity-fixes.md#discarding-pattern-stamps-that-miss-every-output-sample) · [Original stage comparisons](parity-fixes.md#dirt-noise-sources-and-downstream-stages) | -| [noise_dirt_5.sbs/dirt_5](../assets/sbs/noise_dirt_5.sbs) | 0 | Yes | [In progress · 11/19 whole outputs, 107,751/107,776 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) | +| [noise_dirt_5.sbs/dirt_5](../assets/sbs/noise_dirt_5.sbs) | 0 | Yes | [In progress · 32² now exact](parity-fixes.md#deep-pattern-samples-in-custom-render-and-tiling-regions) · [Earlier matrix](parity-fixes.md#dirt-noise-sources-and-downstream-stages) | | [noise_dirt_5_v2.sbs/dirt_5](../assets/sbs/noise_dirt_5_v2.sbs) | 0 | No | [In progress · 11/19 whole outputs, 107,341/107,776 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) | | [noise_dirt_gradient.sbs/dirt_gradient](../assets/sbs/noise_dirt_gradient.sbs) | 0 | Yes | [In progress · 19/19 whole outputs, 141,554/142,592 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) | | [noise_dirt_gradient_v2.sbs/dirt_gradient](../assets/sbs/noise_dirt_gradient_v2.sbs) | 0 | No | [In progress · 6/19 whole outputs, 98,423/101,632 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) |