mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-28 17:19:17 +01:00
Preserve the measured draw order for three-child terminal quadrants
This commit is contained in:
1 parent
2347ad179c
commit
d31c1ee1ae
4 files changed
+65
-16
No files matched your search
@@ -10,9 +10,9 @@ use crate::{CookContext, CookResult};
|
||||
// up to this same limit again. Retained stamps, raster copies and pixel work stay bounded.
|
||||
const MAX_VISITS: usize = 1_048_576;
|
||||
|
||||
// A complete seven-level tree has sixteen grandchildren with 4^5 leaves each.
|
||||
// Pairwise overlapping squares establish this compositing order in single-worker renders.
|
||||
const LEAVES_PER_RUN: usize = 1024;
|
||||
// Each grandchild of a regular seven-level tree contains 4^4 terminal quadrants.
|
||||
// Overlapping squares establish the same run order with three or four leaves per terminal quadrant.
|
||||
const TERMINAL_QUADRANTS_PER_RUN: usize = 256;
|
||||
const SEVEN_LEVEL_RUN_ORDER: [usize; 16] = [12, 13, 8, 9, 14, 0, 1, 15, 2, 11, 6, 7, 10, 3, 4, 5];
|
||||
|
||||
pub(crate) fn cook(context: &CookContext) -> CookResult {
|
||||
@@ -68,7 +68,8 @@ pub fn cook_with_base_level_inputs(context: &CookContext, base_level_only: &[boo
|
||||
let mut remaining = MAX_VISITS;
|
||||
let mut discard_allowance = MAX_VISITS;
|
||||
let grayscale = !context.b("color_mode");
|
||||
let mut leaf_count = (grayscale && opacity_depth == 7 && graph.nodes.iter().all(|node| node.kind == PatternNodeKind::Quadrant)).then_some(0_usize);
|
||||
let mut leaf_count = (grayscale && opacity_depth == 7).then_some(0_usize);
|
||||
let mut leaves_per_run = 0;
|
||||
let mut run_starts = [0; SEVEN_LEVEL_RUN_ORDER.len() + 1];
|
||||
// A grayscale opacity expression can produce an infinity or NaN which participates in the blend.
|
||||
let opacity = context.value("opacity").and_then(Value::as_f32).unwrap_or(0.0);
|
||||
@@ -97,6 +98,19 @@ pub fn cook_with_base_level_inputs(context: &CookContext, base_level_only: &[boo
|
||||
pending.push((next, repeats_left - 1));
|
||||
}
|
||||
let &(node, literal_geometry) = nodes.get(&visit.node).ok_or("missing pattern node")?;
|
||||
if leaf_count.is_some() {
|
||||
if node.kind != PatternNodeKind::Quadrant {
|
||||
leaf_count = None;
|
||||
} else if visit.depth == 6 {
|
||||
let child_count = node.children.iter().flatten().count();
|
||||
let length = TERMINAL_QUADRANTS_PER_RUN * child_count;
|
||||
if (3..=4).contains(&child_count) && (leaves_per_run == 0 || leaves_per_run == length) {
|
||||
leaves_per_run = length;
|
||||
} else {
|
||||
leaf_count = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
if node.kind == PatternNodeKind::Dot {
|
||||
if let Some(Some(child)) = node.children.first() {
|
||||
visit.node = *child;
|
||||
@@ -224,10 +238,10 @@ pub fn cook_with_base_level_inputs(context: &CookContext, base_level_only: &[boo
|
||||
premultiplied: field("imagepremul").and_then(Value::as_bool).unwrap_or(false),
|
||||
};
|
||||
if let Some(count) = leaf_count.as_mut() {
|
||||
if stamp.depth == 7 && (1..256).contains(&stamp.pattern) && stamp.blend == 0 {
|
||||
if stamp.depth == 7 && leaves_per_run != 0 && (1..256).contains(&stamp.pattern) && stamp.blend == 0 {
|
||||
// A leaf that misses the sample grid still occupies its place in the tree.
|
||||
if count.is_multiple_of(LEAVES_PER_RUN)
|
||||
&& let Some(start) = run_starts.get_mut(*count / LEAVES_PER_RUN)
|
||||
if count.is_multiple_of(leaves_per_run)
|
||||
&& let Some(start) = run_starts.get_mut(*count / leaves_per_run)
|
||||
{
|
||||
*start = stamps.len();
|
||||
}
|
||||
@@ -319,7 +333,7 @@ pub fn cook_with_base_level_inputs(context: &CookContext, base_level_only: &[boo
|
||||
}
|
||||
|
||||
let mut groups = [&[][..]; SEVEN_LEVEL_RUN_ORDER.len()];
|
||||
if leaf_count == Some(LEAVES_PER_RUN * groups.len()) {
|
||||
if leaves_per_run != 0 && leaf_count == Some(leaves_per_run * groups.len()) {
|
||||
run_starts[groups.len()] = stamps.len();
|
||||
for (group, run) in groups.iter_mut().zip(SEVEN_LEVEL_RUN_ORDER) {
|
||||
*group = stamps.get(run_starts[run]..run_starts[run + 1]).ok_or("invalid pattern draw range")?;
|
||||
|
||||
@@ -473,7 +473,9 @@ fn six_quadrant_levels_composite_overlapping_subtrees_in_reverse_slot_order() {
|
||||
#[test]
|
||||
fn seven_quadrant_levels_order_leaf_runs_before_discarding_missed_stamps() {
|
||||
let tiny = f32::EPSILON * 0.25;
|
||||
for (negative, positive, miss_other_stamps, expected) in [(12, 0, false, tiny), (12, 0, true, tiny), (0, 14, true, 0.0)] {
|
||||
for (negative, positive, miss_other_stamps, missing_slot, expected) in
|
||||
[(12, 0, false, None, tiny), (12, 0, true, None, tiny), (0, 14, true, None, 0.0), (12, 0, false, Some(2), tiny), (0, 14, true, Some(2), 0.0)]
|
||||
{
|
||||
let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant);
|
||||
for slot in 0..4 {
|
||||
let parent = graph.add(PatternNodeKind::Quadrant).unwrap();
|
||||
@@ -482,9 +484,14 @@ fn seven_quadrant_levels_order_leaf_runs_before_discarding_missed_stamps() {
|
||||
let group = slot * 4 + child_slot;
|
||||
let mut leaf = graph.add(PatternNodeKind::Quadrant).unwrap();
|
||||
graph.nodes[(parent - 1) as usize].children[child_slot] = Some(leaf);
|
||||
for _ in 2..7 {
|
||||
for depth in 2..7 {
|
||||
let child = graph.add(PatternNodeKind::Quadrant).unwrap();
|
||||
graph.nodes[(leaf - 1) as usize].children.fill(Some(child));
|
||||
if depth == 6
|
||||
&& let Some(slot) = missing_slot
|
||||
{
|
||||
graph.nodes[(leaf - 1) as usize].children[slot] = None;
|
||||
}
|
||||
leaf = child;
|
||||
}
|
||||
let offset = [(slot & 1) * 2 + (child_slot & 1), (slot >> 1) * 2 + (child_slot >> 1)].map(|quarter| -0.25 * quarter as f32);
|
||||
@@ -507,11 +514,21 @@ fn seven_quadrant_levels_order_leaf_runs_before_discarding_missed_stamps() {
|
||||
]);
|
||||
}
|
||||
}
|
||||
let image = cook_in_regions(graph, &BTreeMap::new(), 136404, [128; 2], [[0.0, 1.0, 0.0, 1.0]; 2], 1.0).unwrap().remove(0);
|
||||
for y in 0..128 {
|
||||
for x in 0..128 {
|
||||
let value = if x < 32 && y < 32 { expected } else { 1.0 };
|
||||
assert_eq!(image.get_gray(x, y, false).to_bits(), value.to_bits(), "groups {negative} and {positive} at ({x},{y})");
|
||||
for detached_dot in [false, true] {
|
||||
if detached_dot {
|
||||
graph.add(PatternNodeKind::Dot).unwrap();
|
||||
}
|
||||
let image = cook_in_regions(graph.clone(), &BTreeMap::new(), 136404, [128; 2], [[0.0, 1.0, 0.0, 1.0]; 2], 1.0).unwrap().remove(0);
|
||||
for y in 0..128 {
|
||||
for x in 0..128 {
|
||||
let slot = ((x & 1) + 2 * (y & 1)) as usize;
|
||||
let value = if x < 32 && y < 32 && missing_slot != Some(slot) { expected } else { 1.0 };
|
||||
assert_eq!(
|
||||
image.get_gray(x, y, false).to_bits(),
|
||||
value.to_bits(),
|
||||
"groups {negative} and {positive} at ({x},{y}), missing {missing_slot:?}, detached {detached_dot}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2098,6 +2098,24 @@ Each application reproduced every pixel of its own original output on these reop
|
||||
|
||||
In rounds 0, 19 and 41, moving the parent frames right and down by half a texel changed 5, 10 and 10 of the 8,192 upper-half odd-coordinate samples. The sampled pixels distinguish which parent draws first. With tiling disabled, all 24,576 samples reproduced the original order. Reducing the shifted squares to size (.375,.375) also reproduced all 24,576. A fixed order derived on the unshifted squares left 1,592 differences among 65,536 F32 texels in the 256-square Clouds 2 generator at seed 0 and defaults. This leaves the mixed-depth accumulation order open.
|
||||
|
||||
### Three terminal children in seven-level quadrant trees
|
||||
|
||||
Dirt 3 draws 12,288 additive leaves at depth seven. Each depth-six quadrant has children in slots 0, 1 and 3. The previous renderer ordered the complete four-child case and visited the three-child case in sequence. On a white F32 background, repeat the [seven-level square experiment](#compositing-complete-seven-level-additive-quadrant-trees), disconnecting slot 2 from every depth-six quadrant. The 120 pair comparisons at 128² improved from 1,910,016/1,966,080 exact pixels to **1,966,080/1,966,080**, maximum F32 error from 2⁻²⁵ to zero. Separately omitting each of slots 0, 1, 2 and 3 produced the same ordering in all 92,160 overlapping samples per omission. An unconnected routing dot on the complete four-child graph also retained that order. Across its 120 renders MaterialCraft improved from 1,891,328/1,966,080 to **1,966,080/1,966,080**, maximum error zero.
|
||||
|
||||
An independent comparison labelled all 64 subtrees reached after the third subdivision. Pair them in 63 round-robin rounds with the same white background, −1 and 2⁻²⁵ square opacities and zero roughness. Translate each pair into one of 32 upper-half tiles of the 8 × 8 grid. Across all 2,016 comparisons the four consecutive subtrees of each grandchild stayed together. The sixteen groups of 768 leaves followed the order measured in the four-child case. MaterialCraft uses the connected terminal child count to find group boundaries before dropping stamps that miss the sample grid. It checks the node kinds that the renderer actually visits.
|
||||
|
||||
For the Dirt 3 generator itself, read F32 at (width, height, seed, disorder) of (128,512,398765,.984375), (128,64,142327,.53125) and (256,128,142453,.921875). Compare the original generator directly, its output from the package saved in MaterialCraft, and a diagnostic output connected directly to that generator. The reference reproduced every generator pixel after reopening. The group order reduced the maximum F32 error from 5.96046448e-8 to zero. The comparison ran against the normal MaterialCraft renderer as well as the independently reordered contributions.
|
||||
|
||||
| Dirt 3 capture | Renders | Earlier exact / total | Current exact / total | Current maximum by storage |
|
||||
|---|---:|---:|---:|---|
|
||||
| Generator, single processor | 3 | 105,609/106,496 | 106,496/106,496 | F32 0 |
|
||||
| Whole root, single processor | 68 | 2,316,511/2,316,544 | 2,316,544/2,316,544 | U8 0, U16 0, F16 0, F32 0 |
|
||||
| Saved whole root, single processor | 12 | 461,041/461,056 | 461,056/461,056 | U8 0, U16 0, F16 0, F32 0 |
|
||||
| Whole root, eight processors | 68 | 2,316,521/2,316,544 | 2,316,526/2,316,544 | U8 0, U16 1, F16 0.00048828125, F32 1.52587891e-05 |
|
||||
| Second capture, eight processors | 12 | 461,048/461,056 | 461,047/461,056 | U8 0, U16 1, F16 0, F32 0 |
|
||||
|
||||
Use the [68 dirt settings and twelve reopens](#single-processor-comparisons-for-clouds-2-and-five-dirt-fields). Both applications preserved every original pixel on their own single-processor reopens. With eight processors the reference reproduced 461,056/461,056 of its own pixels on the twelve repeated settings, with maximum differences of U8 0, U16 0, F16 0, F32 0. Dirt 3 remains in progress for this configuration. The regression exercises three- and four-child trees, culled neutral squares and a disconnected dot. All 426 node and evaluator release tests passed, along with formatting, strict workspace Clippy, dependency layers and WebAssembly.
|
||||
|
||||
## 2026-11-20 root comparisons
|
||||
|
||||
### Hue, saturation, lightness and Replace Color
|
||||
|
||||
@@ -252,7 +252,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug
|
||||
| [directional_distance.sbs/directional_distance](../assets/sbs/directional_distance.sbs) | 3 | No | [Parity · 32 renders, both outputs exact across 342,528 pixels](parity-fixes.md#distance-output-channels-axis-wrapping-and-cutoffs) |
|
||||
| [directionnal_noise.sbs/directionnal_noise](../assets/sbs/directionnal_noise.sbs) | 0 | Yes | [Parity · 136 settings, 40,572,160/40,572,160 pixels](parity-fixes.md#interpolated-coordinates-on-clipped-squares-and-quarter-turns) · [Earlier measurements](parity-fixes.md#draw-order-at-six-quadrant-subdivisions) |
|
||||
| [dirt_1.sbs/dirt_1](../assets/sbs/dirt_1.sbs) | 0 | Yes | [In progress · 2,005,987/2,006,016 pixels across 40 new renders](parity-fixes.md#wave-frequency-and-phase-in-directional-noise) · [Earlier measurements](parity-fixes.md#overlapping-disc-additions-in-dirt-1) · [Six reference repeats](parity-fixes.md#repeated-dirt-pattern-stages) · [2,316,507/2,316,544 pixels across 68 single-processor settings](parity-fixes.md#single-processor-comparisons-for-clouds-2-and-five-dirt-fields) |
|
||||
| [dirt_3.sbs/dirt_3](../assets/sbs/dirt_3.sbs) | 0 | Yes | [In progress · 2,086,117/2,086,144 pixels across 56 distinct settings](parity-fixes.md#original-and-reopened-dirt-moisture-and-cellular-noise-fields) · [Earlier measurements](parity-fixes.md#wave-frequency-and-phase-in-directional-noise) · [Earlier isolation](parity-fixes.md#bounds-and-pixel-coordinates-of-rotated-pattern-stamps) · [Six reference repeats](parity-fixes.md#repeated-dirt-pattern-stages) · [2,316,511/2,316,544 pixels across 68 single-processor settings](parity-fixes.md#single-processor-comparisons-for-clouds-2-and-five-dirt-fields) |
|
||||
| [dirt_3.sbs/dirt_3](../assets/sbs/dirt_3.sbs) | 0 | Yes | [In progress · 68 single-processor settings exact, 2,316,526/2,316,544 pixels on eight processors](parity-fixes.md#three-terminal-children-in-seven-level-quadrant-trees) · [Earlier distinct settings](parity-fixes.md#original-and-reopened-dirt-moisture-and-cellular-noise-fields) · [Earlier measurements](parity-fixes.md#wave-frequency-and-phase-in-directional-noise) · [Earlier isolation](parity-fixes.md#bounds-and-pixel-coordinates-of-rotated-pattern-stamps) · [Six reference repeats](parity-fixes.md#repeated-dirt-pattern-stages) · [2,316,511/2,316,544 pixels across 68 single-processor settings](parity-fixes.md#single-processor-comparisons-for-clouds-2-and-five-dirt-fields) |
|
||||
| [dirt_5.sbs/dirt_5](../assets/sbs/dirt_5.sbs) | 0 | Yes | [In progress · 2,005,576/2,006,016 pixels across 40 new renders](parity-fixes.md#wave-frequency-and-phase-in-directional-noise) · [Earlier measurements](parity-fixes.md#stored-opacity-across-pattern-depths) · [Six reference repeats](parity-fixes.md#repeated-dirt-pattern-stages) · [2,299,232/2,300,160 pixels across 67 single-processor settings](parity-fixes.md#single-processor-comparisons-for-clouds-2-and-five-dirt-fields) |
|
||||
| [dirt_6.sbs/dirt_6](../assets/sbs/dirt_6.sbs) | 0 | Yes | [In progress · 2,068,729/2,069,760 pixels across 55 distinct settings](parity-fixes.md#original-and-reopened-dirt-moisture-and-cellular-noise-fields) · [Earlier measurements](parity-fixes.md#wave-frequency-and-phase-in-directional-noise) · [Earlier isolation](parity-fixes.md#drawing-axis-aligned-stamps-smaller-than-one-pixel) · [Six reference repeats](parity-fixes.md#repeated-dirt-pattern-stages) · [2,298,882/2,300,160 pixels across 67 single-processor settings](parity-fixes.md#single-processor-comparisons-for-clouds-2-and-five-dirt-fields) |
|
||||
| [dirt_gradient.sbs/dirt_gradient](../assets/sbs/dirt_gradient.sbs) | 0 | Yes | [In progress · 2,005,640/2,006,016 pixels across 40 new renders](parity-fixes.md#wave-frequency-and-phase-in-directional-noise) · [Earlier measurements](parity-fixes.md#draw-order-at-six-quadrant-subdivisions) · [Six reference repeats](parity-fixes.md#repeated-dirt-pattern-stages) · [2,299,615/2,300,160 pixels across 67 single-processor settings](parity-fixes.md#single-processor-comparisons-for-clouds-2-and-five-dirt-fields) |
|
||||
|
||||
Reference in new issue
Block a user