From fcff6c25a47d6be5c055259e71c2b32a87c8b3be Mon Sep 17 00:00:00 2001 From: chasse20 Date: Mon, 23 Nov 2026 23:29:50 -0500 Subject: [PATCH] Isolate writable quadrant values across six-level root children --- crates/nodes/src/pattern/evaluation.rs | 22 +++++++++++++++++++ crates/nodes/src/pattern/tests.rs | 29 ++++++++++++++++++++++++++ crates/nodes/src/pattern/variables.rs | 8 +++++++ docs/parity-fixes.md | 19 +++++++++++++++++ docs/sbs-root-nodes.md | 2 ++ 5 files changed, 80 insertions(+) diff --git a/crates/nodes/src/pattern/evaluation.rs b/crates/nodes/src/pattern/evaluation.rs index c2bf4f30..db13bf3a 100644 --- a/crates/nodes/src/pattern/evaluation.rs +++ b/crates/nodes/src/pattern/evaluation.rs @@ -23,6 +23,22 @@ enum QuadrantChildren { Other, } +fn has_uniform_quadrant_leaves(root: u32, depth: u32, nodes: &BTreeMap) -> bool { + let mut level = BTreeSet::from([root]); + for current in 0..=depth { + let mut next = BTreeSet::new(); + for id in level { + let Some(&(node, _, children)) = nodes.get(&id) else { return false }; + if node.kind != PatternNodeKind::Quadrant || (children == QuadrantChildren::None) != (current == depth) { + return false; + } + next.extend(node.children.iter().flatten().copied()); + } + level = next; + } + true +} + #[derive(Default)] struct SevenLevelRuns { seen_root: bool, @@ -148,6 +164,9 @@ pub(super) fn cook_with_visit_limit(context: &CookContext, base_level_only: &[bo let mut branch_starts = [None; 4]; let mut variables = PatternVariables::new(context.vars.graph_values()); + let separate_child_variables = opacity_depth == 6 + && functions.values().any(|(_, names)| !names.is_empty()) + && graph.root.is_some_and(|root| has_uniform_quadrant_leaves(root, opacity_depth, &nodes)); let mut max_depth = 0; let mut state = EvaluationState::default(); let mut terminal_siblings: Option<(usize, bool)> = None; @@ -181,6 +200,9 @@ pub(super) fn cook_with_visit_limit(context: &CookContext, base_level_only: &[bo terminal_siblings = None; } variables.resume(pending.len()); + if separate_child_variables && visit.depth == 1 { + variables.start_root_child(); + } max_depth = max_depth.max(visit.depth); if empty_switches.contains(&visit.node) { continue; diff --git a/crates/nodes/src/pattern/tests.rs b/crates/nodes/src/pattern/tests.rs index 16a2a524..c945ec76 100644 --- a/crates/nodes/src/pattern/tests.rs +++ b/crates/nodes/src/pattern/tests.rs @@ -1973,6 +1973,35 @@ fn quadrant_children_accumulate_in_their_own_depth() { } } +#[test] +fn six_level_quadrant_children_start_independent_counters() { + for depth in [5, 6] { + let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant); + for level in 0..depth { + let next = graph.add(PatternNodeKind::Quadrant).unwrap(); + graph.nodes[level].children.fill(Some(next)); + } + square(&mut graph, depth); + let mut counter = FunctionGraph::input("count", materialcraft_core::ValueType::Float); + let previous = counter.output.unwrap(); + let step = counter.add_constant(Value::Float(1.0 / 4096.0)); + let next = counter.add("add", &[previous, step], None); + counter.output = Some(counter.add("set", &[next], Some(Value::Text("count".into())))); + graph.nodes[depth].parameters.insert("opacity".into(), Value::Function(Box::new(counter))); + + let side = 1 << depth; + let image = cook_at_size(graph, &BTreeMap::new(), 598241, side).unwrap().remove(0); + let half = i64::from(side / 2); + for slot in 0..4 { + let x = slot % 2 * half; + let y = slot / 2 * half; + let first = if depth == 5 { 256 * slot + 1 } else { 1 }; + assert_eq!(image.get_gray(x, y, false), first as f32 / 4096.0, "depth {depth}, child {slot}"); + assert_eq!(image.get_gray(x + 1, y, false), (first + 1) as f32 / 4096.0); + } + } +} + #[test] fn quadrant_modes_read_published_inputs_and_shadow_outer_pattern_bindings() { use materialcraft_core::ValueType; diff --git a/crates/nodes/src/pattern/variables.rs b/crates/nodes/src/pattern/variables.rs index 1f66a70a..20ac281d 100644 --- a/crates/nodes/src/pattern/variables.rs +++ b/crates/nodes/src/pattern/variables.rs @@ -71,6 +71,14 @@ impl<'a> PatternVariables<'a> { } } + pub(super) fn start_root_child(&mut self) { + // A six-level quadrant tree starts a separate evaluation task per root child. + // Active declarations on the root still supply values to read-only descendants. + for local in self.locals.values_mut() { + local.saved.clear(); + } + } + pub(super) fn resume(&mut self, pending_after_pop: usize) { // Once a waiting sibling is popped, the declaring node and all its descendants have finished. while self.scopes.last().is_some_and(|(pending, _)| pending_after_pop < *pending) { diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 5d4df25a..3fc39eff 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -59,6 +59,25 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas ## 2026-11-23 root comparisons +### Independent writable variables in six-level quadrant trees + +In a plain quadrant tree with six subdivision levels, each connected child of the root starts its own writable variable scope. Successive visits within that child retain the value at the declaring depth. With five full levels a counter spans all four root children. The evaluator checks the reachable nodes and terminal depths before starting separate scopes. An active declaration on the root remains available to descendants that only read it. [Scope handling](../crates/nodes/src/pattern/variables.rs) · [Traversal](../crates/nodes/src/pattern/evaluation.rs) · [Five- and six-level regression](../crates/nodes/src/pattern/tests.rs) + +Use a full six-level tree, a tree with all four root children followed by five single-child levels, and one with only opposite child slots at every level. For a five-level control, use all four slots throughout. At the leaves, draw a unit square and keep the first visited position in a local float. When the value is zero, write .125 + .5 × (1 + x + sy) / s², where s is the number of cells along an axis and (x,y) is the zero-based cell. Otherwise write the existing value. In the 64² complete tree the first positions have linear indices 0, 32, 2,048 and 2,080. There are four counters even when each root child contains only one leaf. + +For two other original fields, add 1/4,096 to a local counter at depth six or depth one in a complete six-level tree. Read the F32 field in a Pixel Processor using nearest sampling. Subtract the first pixel and add .125. For the depth-one field give the leaves a positive gain of 1e−30. Subtracting the first pixel removes an initial offset observed across separate reference renders of uninitialized variables. At 64² the depth-six field records 1,024 increments per root child. For both counter fields and all four first-position fields render s², 2s × s, s × s/2 and s × 2s. Use s = 64 for six levels and 32 for five. Use seed 598241 + 283i + j for size index i and format index j in the table order. Repeat after an ordinary save and reopen. + +| Output | Previous exact / 118,272 · max | Current original | Current ordinary save | Current max | +|---|---:|---:|---:|---:| +| U8 | 84,286 · 192 | 118,272/118,272 | 118,272/118,272 | 0 | +| U16 | 67,390 · 49,151 | 118,272/118,272 | 118,272/118,272 | 0 | +| F16 | 67,390 · .75 | 118,272/118,272 | 118,272/118,272 | 0 | +| F32 | 67,390 · .75 | 118,272/118,272 | 118,272/118,272 | 0 | + +Across the inventory, ten roots contain eleven pure quadrant trees ending at depth six. Their reachable quadrant properties contain no direct Set. Compare the two 256² and 128 × 64 desktop controls for each of BNW Spots 1 and Clouds 1 and 2, then four 64² changed-format captures each for BNW Spots 3, Directional Noise, Dirt Gradient, Fluid, Microscope View, Scratches 4 and Wood Fibers 2. All 34 MaterialCraft buffers retain their earlier bits. The reference comparisons give 28,672/28,672 U8, 249,844/249,856 U16, 28,672/28,672 F16 and 28,659/28,672 F32, with maxima of 0, 2, 0 and 2.59876251e−5 respectively. The thirteen differing F32 pixels are one in Dirt Gradient and twelve in Wood Fibers 2. [Early noise roots and their desktop settings](#live-desktop-and-standalone-rendering-of-three-noise-roots) + +A separate read/write discrepancy already occurs with a single level of four sibling squares. In the root branch offset, assign a published input to a local float and return (0,0). Reading the local in a child gives the published .8125 or .3125. Adding .0625 and assigning the result to a different name gives .875 or .375. Assigning that same sum back to the name just read returns zero in the reference and accumulates across the four squares in MaterialCraft. The 16² default gives 0/256, maximum .25. Replacing that child expression with a literal assignment of .25 restores 256/256 agreement. In the six-level version using an increment of 1/4,096, the 64² default still gives 0/4,096 F32 pixels, maximum .25, on both the original and ordinary save. The read-only version matches 4,096/4,096 on both. Uninitialized first-position fields can also retain a value from an earlier capture. Only the specified measurements are marked exact. + ### A full copy batch in the last root quadrant In a complete eight-level additive tree with two full batches per neighboring pair of depth-two quadrants, the next 5,460 physical copies come from three parts of the first tree. In zero-based child-slot traversal order, take the depth-two draw at 15, then the depth-three subtrees 62 and 63, then 48 and 49. Stop after 5,460 copies when these parts together contain one full batch and a smaller remainder. A cut within one wrapped stamp can submit some of its copies now and the others later. The shared evaluator uses the observed batch counts and the ordered spans. [Evaluator and shape conditions](../crates/nodes/src/pattern/underfilled_quadrants.rs) · [Two-square regression](../crates/nodes/src/pattern/tests/underfilled_quadrants.rs) diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index cf8aabd3..afb4a77b 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -39,6 +39,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard Nested Curvature, Curvature Smooth and Normal Sobel each match 2,304/2,304 original and 2,304/2,304 reopened pixels. Panorama 3D Position now reopens in the reference desktop and matches 2,304/2,304 pixels across six renders of an ordinary SBS save. Panorama Shape, Shape 2 and Spot each match 2,304/2,304 original and 2,304/2,304 reopened pixels in the same six settings. [Fixed vector arguments and whole-root counts](parity-fixes.md#float-vector-arguments-in-nested-functions) [Save, reopen and desktop comparisons](parity-fixes.md#ordinary-sbs-saves-with-verified-library-dependencies) [Working precision, blur inputs and whole-root counts](parity-fixes.md#output-precision-in-nested-generators-and-blurs) +Six-level quadrant counters and first-position fields match 473,088/473,088 typed pixels on originals and again on ordinary saves. The 34 measurements across ten catalog roots with the same tree topology retain their earlier outputs. A self-referential assignment driven by a published parent value still differs. [Variable scopes, raw counts and a one-level isolate](parity-fixes.md#independent-writable-variables-in-six-level-quadrant-trees) + Ordinary SBS saves preserve graph seed inheritance across 22 nested graphs. The original and reopened random fields each match 22,528/22,528 pixels. BNW Spots 1 and Clouds 1 and 2 each match 9,216/9,216 pixels across four output formats in the two nested settings. [Seed settings, whole graphs and renderer repeats](parity-fixes.md#graph-random-seed-inheritance-on-save-and-in-nested-graphs) An image wired through a graph instance retains its per-pin pixel size, tiling and pixel ratio. All 331,776/331,776 U16 pixels match on original and reopened flat and nested graphs. U8, F16 and F32 each match a further 73,728/73,728 pixels on both passes. [Sampling settings, overrides and raw comparisons](parity-fixes.md#sampling-properties-on-graph-inputs-and-outputs)