From 4adce8371227e026e55b1780990b00a2348f8a47 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Sat, 21 Nov 2026 04:03:19 -0500 Subject: [PATCH] Group grayscale bitmap stamps by slot and filtering mode --- crates/nodes/src/pattern/raster.rs | 35 +++++++++++++++--- crates/nodes/src/pattern/tests.rs | 59 ++++++++++++++++++++++++++++++ docs/parity-fixes.md | 27 ++++++++++++++ docs/sbs-root-nodes.md | 2 + 4 files changed, 117 insertions(+), 6 deletions(-) diff --git a/crates/nodes/src/pattern/raster.rs b/crates/nodes/src/pattern/raster.rs index 30fa4206..7d7f5209 100644 --- a/crates/nodes/src/pattern/raster.rs +++ b/crates/nodes/src/pattern/raster.rs @@ -53,12 +53,35 @@ pub(super) fn render( let mut traversal = groups.into_iter().flatten(); let mut ordered = Vec::new(); - let draw_order = |stamp: &PatternStamp| (stamp.pattern, stamp.blend != 0); - if kind == Kind::Gray && !traversal.clone().map(draw_order).is_sorted() && traversal.clone().all(|stamp| (2..=18).contains(&stamp.pattern)) { - // Procedural grayscale stamps draw by shape, with additive stamps first. - // Keep traversal order among equal keys, including depth-six sibling groups. - ordered.extend(traversal.by_ref()); - ordered.sort_by_key(|stamp| draw_order(stamp)); + if kind == Kind::Gray { + let mut bitmap_ranks = [0; crate::MAX_IMAGE_INPUTS]; + let mut next_rank = 256; + let mut draw_order = |stamp: &PatternStamp| { + let mut rank = stamp.pattern; + let mut filtering = 0; + if rank >= 256 + && let Some(slot) = bitmap_ranks.get_mut((rank - 256) as usize) + { + // Bitmap slots follow the procedural shapes, in the order each slot first drew. + if *slot == 0 { + *slot = next_rank; + next_rank += 1; + } + rank = *slot; + filtering = stamp.filtering; + } + (rank, filtering, stamp.blend != 0) + }; + if !traversal.clone().map(&mut draw_order).is_sorted() { + // Assign the remaining ranks in traversal order before the sort compares stamps. + for stamp in traversal.by_ref() { + draw_order(stamp); + ordered.push(stamp); + } + // Each bitmap slot draws by filtering mode, then additive before maximum. + // Equal keys keep their traversal order. + ordered.sort_by_key(|stamp| draw_order(stamp)); + } } for stamp in traversal.chain(ordered) { if !stamp.center.iter().chain(&stamp.size).chain(&stamp.color).chain([&stamp.rotation, &stamp.variation]).all(|value| value.is_finite()) { diff --git a/crates/nodes/src/pattern/tests.rs b/crates/nodes/src/pattern/tests.rs index 050eacc7..f582958d 100644 --- a/crates/nodes/src/pattern/tests.rs +++ b/crates/nodes/src/pattern/tests.rs @@ -333,6 +333,65 @@ fn grayscale_stamps_draw_in_shape_then_blend_order() { } } +#[test] +fn grayscale_bitmap_stamps_group_repeated_slots_in_first_draw_order() { + use craft_texture::{Image, Kind}; + use materialcraft_core::OutputFormat; + + let definition = find("fx_map").unwrap(); + let level = 128.0 / 255.0; + let tiny = 2.0_f32.powi(-27); + // The input bridges have already resized both constant fields to the graph dimensions. + let inputs = [None, Some(Image::filled(32, 16, Kind::Gray, [level; 4])), Some(Image::filled(32, 16, Kind::Gray, [1.0; 4]))]; + for (slots, filters, modes, tints, expected) in [ + ([1, 0, 1], [2; 3], [0, 0, 0], [0.75, tiny, -0.75], [tiny * level, 0.75]), + ([1, 0, 1], [2; 3], [1, 0, 1], [0.125, 0.25, 0.875], [0.875 + 0.25 * level, 0.125]), + ([1, 1, 1], [2; 3], [1, 0, 1], [0.875, 0.25, 0.75], [0.875, 0.875]), + ([1; 3], [0, 1, 0], [1, 0, 1], [0.75, 0.3125, 0.1875], [1.0625, 0.75]), + ([1; 3], [1, 2, 1], [0; 3], [0.75, tiny, -0.75], [tiny, 0.75]), + ([1; 3], [2, 1, 2], [0; 3], [0.75, tiny, -0.75], [0.0, 0.75]), + ] { + let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant); + let first = graph.add(PatternNodeKind::Quadrant).unwrap(); + let second = graph.add(PatternNodeKind::Quadrant).unwrap(); + graph.nodes[0].children[0] = Some(first); + graph.nodes[1].children[1] = Some(second); + for (i, (node, size)) in graph.nodes.iter_mut().zip([1.0, 2.0, 4.0]).enumerate() { + let tint = tints[i]; + node.parameters = BTreeMap::from([ + ("patterntype".into(), Value::Int(1)), + ("imageindex".into(), Value::Int(slots[i])), + ("imagefiltering".into(), Value::Int(filters[i])), + ("blendingmode".into(), Value::Int(modes[i])), + ("opacity".into(), Value::Float4([tint, tint, tint, 1.0])), + ("patternsize".into(), Value::Float2([size; 2])), + ]); + } + let params = resolve_params( + definition, + &BTreeMap::from([ + ("pattern".into(), Value::PatternGraph(Box::new(graph))), + ("roughness".into(), Value::Float(0.0)), + ("color_mode".into(), Value::Bool(false)), + ]), + ); + let context = CookContext { + w: 32, + h: 16, + seed: 471817, + format: OutputFormat::F32, + tile: false, + input_formats: &[OutputFormat::U8; 3], + inputs: &inputs, + params: ¶ms, + resource: &|_| None, + vars: &BTreeMap::from([("$tiling".into(), Value::Int(0))]), + }; + let image = (definition.cook)(&context).unwrap().remove(0); + assert_eq!([image.get_gray(0, 0, false), image.get_gray(31, 15, false)], expected); + } +} + #[test] fn six_quadrant_levels_composite_overlapping_subtrees_in_reverse_slot_order() { let small = f32::EPSILON * 0.5; diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 6f8b10ee..559e1dff 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -418,6 +418,33 @@ Across all 48 renders, **100,352/100,352** pixels match, maximum zero. Convertin These measurements cover power-of-two dimensions and an explicit linear data read. The separate comparisons for odd-sized color resources and file-default color interpretation remain open for this evaluator. +### Grayscale bitmap stamp ordering across slots and filters + +In a grayscale pattern graph, bitmap slots draw after the procedural shapes. Their first traversal use determines the order of the slots. Within a slot the measured order is automatic mip filtering (0), bilinear (1), then nearest (2). At each filter mode additive stamps draw before maximum stamps. Equal keys keep traversal order. + +Use a three-node quadrant chain with the first child connected at quadrant 0 and the next at quadrant 1. Give the three stamps sizes 1, 2 and 4 and use zero roughness, zero background, tiling off, unit opacity multiplier and F32 grayscale output. Supply constant byte images of 128 and 255 at slots 0 and 1. At 32 × 16 and 16 × 32, seeds 471817 and 471818, the following results distinguish the ordering. + +| Slots | Filters | Blend modes | Signed grayscale tints | Top-left F32 value | +|---|---|---|---|---:| +| 1,0,1 | 2,2,2 | add,add,add | .75, 2⁻²⁷, −.75 | 3.7398995e−9 | +| 1,0,1 | 2,2,2 | max,add,max | .125, .25, .875 | 1.00049019 | +| 1,1,1 | 0,1,0 | max,add,max | .75, .3125, .1875 | 1.0625 | +| 1,1,1 | 1,0,1 | max,add,max | .75, .3125, .1875 | .75 | + +The independent filter sweep uses each ordered pair of distinct filters a,b from 0,1,2 with the stamps set to a,b,a. It repeats the maximum/additive case and the signed three-addition case. For the five-stamp controls, vary the slot sequence across 2,1,2,0,1 and 0,0,0,0,0 as well as mixed shapes and slots. Use filters (i+j) mod 3 for zero-based stamp i and case j, sizes 1,2,4,8,16, and exercise additions, both maximum mode values, positive and negative gains. Reversing the serialized node list in alternating cases preserves the connected traversal order. The tested sizes are 32 × 16, 16 × 32, 32² and 16². + +| F32 comparison | Renders | Exact before / total · maximum | Exact after / total · maximum | +|---|---:|---:|---:| +| Three stamps with two slots | 40 | 20,184/23,040 · 0.212009817 | 23,040/23,040 · 0 | +| Five stamps with up to three slots | 24 | 9,335/13,824 · 0.593627483 | 13,823/13,824 · 0 | +| Constant fields | 8 | 1,984/4,096 · 0.25 | 4,096/4,096 · 0 | +| Procedural shapes with a bitmap | 24 | 11,879/16,384 · 0.375 | 16,384/16,384 · 0 | +| Filter pairs on one slot | 24 | 9,120/12,288 · 0.3125 | 12,288/12,288 · 0 | + +The 120 composite renders matched **69,631/69,632** pixels. Rendering the individual stamps separately matched **187,390/187,392** across 312 renders. Starting with the same independently authored graph serialized by MaterialCraft, saving it again and reopening it in both applications matched **33,791/33,792** across 60 renders. Each application reproduced its own originals. The maximum numerical error is zero. At (0,0) on the 16 × 32 five-stamp, one-slot control with negative gain, the reference stored negative zero and MaterialCraft stored positive zero. The same sign difference occurred in two isolated-stamp renders. + +The four 64² U8, U16, F16 and F32 root controls stayed exact on Arc Pavement, Tile Generator, Tile Random and Weave 2 in the first group of rechecks. Paths Trace and Splatter Circular still differ. The larger root matrices retain their existing status. The ordering regression reproduced the earlier error. The release node suite, formatting, strict workspace Clippy, dependency layers and WebAssembly 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 a73cb03d..9c69191f 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -49,6 +49,8 @@ Odd-sized grayscale bitmap resources match 116,736/116,736 pixels directly and 9 Linear RGBA bitmap resources match 108,544/108,544 pixels on two procedural fields. [Exact counts by format and saved-package results](parity-fixes.md#stored-levels-of-linear-color-bitmap-resources) +The 120 mixed-stamp renders matched 69,631/69,632 F32 pixels across slot, filtering and blend changes. [Measurements and saved packages](parity-fixes.md#grayscale-bitmap-stamp-ordering-across-slots-and-filters) + Grayscale output from normalized RGB bitmaps matched 100,352/100,352 pixels across 48 renders. [Measurements and saved packages](parity-fixes.md#grayscale-conversion-on-the-first-bitmap-filter-pass) Enlarged RGBA pattern backgrounds now match 71,680/71,680 pixels across 280 independent renders. The earlier source-storage tests still differ at 121 grayscale zero signs. [Background sign measurements](parity-fixes.md#signs-of-zero-in-enlarged-color-backgrounds)