Preserve scalar quadrant luminosity when saving pattern graphs

This commit is contained in:
chasse20 committed 2026-11-22 21:02:24 -05:00
1 parent 201fc24d42
commit dc2a113246
3 files changed
+57 -1

No files matched your search

+37 -1
View File
@@ -1,4 +1,4 @@
use materialcraft_core::{PatternGraph, PatternNode, PatternNodeKind, Value};
use materialcraft_core::{FunctionGraph, PatternGraph, PatternNode, PatternNodeKind, Value};
use roxmltree::Node as XmlNode;
use std::collections::BTreeMap;
use std::fmt::Write;
@@ -87,6 +87,13 @@ pub(super) fn write(graph: &PatternGraph, writer: &mut SbsWriter) -> Result<Stri
let definition = materialcraft_nodes::pattern::definition(node.kind).ok_or("missing pattern definition")?;
let mut parameters: BTreeMap<_, _> = definition.params.iter().map(|parameter| (parameter.id.to_string(), parameter.default.clone())).collect();
parameters.extend(node.parameters.clone());
if node.kind == PatternNodeKind::Quadrant
&& let Some(luminosity @ Value::Float(_)) = parameters.get_mut("opacity")
{
// The literal swatch has four components. A scalar expression preserves the
// luminosity in both grayscale and color patterns when the package is reopened.
*luminosity = Value::Function(Box::new(FunctionGraph::constant(luminosity.clone())));
}
for (name, value) in &parameters {
if let Value::Function(function) = value {
let function = function_graph::write(function, writer)?;
@@ -122,3 +129,32 @@ pub(super) fn write(graph: &PatternGraph, writer: &mut SbsWriter) -> Result<Stri
output.push_str("</paramsGraph></paramsGraphs>");
Ok(output)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn quadrant_scalar_luminosity_uses_a_scalar_function_on_save() {
let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant);
let original_swatch = Value::Float4([0.25, 0.5, 0.75, 0.875]);
let child = graph.add(PatternNodeKind::Quadrant).unwrap();
graph.nodes[0].children[1] = Some(child);
graph.nodes[0].parameters.insert("opacity".into(), Value::Float(-0.3125));
graph.nodes[1].parameters.insert("opacity".into(), original_swatch.clone());
let mut writer = SbsWriter { last_uid: 10, external_packages: BTreeMap::new() };
let xml = write(&graph, &mut writer).unwrap();
let document = roxmltree::Document::parse(&xml).unwrap();
let stored = read(document.root_element().first_element_child().unwrap()).unwrap();
// A literal swatch accepts four components. A one-component function keeps scalar
// luminosity semantics in both color modes, including values outside the unit range.
let Value::Function(function) = &stored.nodes[0].parameters["opacity"] else { panic!("expected a scalar function") };
let scalar = function.node(function.output.unwrap()).unwrap();
assert_eq!(scalar.op, "const_float1");
assert_eq!(scalar.type_code, 256);
assert_eq!(scalar.value, Some(Value::Float(-0.3125)));
assert_eq!(function.nodes.len(), 1);
assert_eq!(stored.nodes[1].parameters["opacity"], original_swatch);
}
}
+18
View File
@@ -59,6 +59,24 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas
## 2026-11-22 root comparisons
### Scalar Quadrant luminosity in saved packages
A Quadrant accepts a scalar luminosity in MaterialCraft. Saving that value as a one-component literal swatch produced an SBS that the reference renderer could not compile. The native swatch takes four components. A single constant scalar function is also a valid property representation, and retains the scalar’s meaning in both grayscale and color. The writer now uses that representation for a scalar literal on a Quadrant. Existing four-component swatches and authored functions retain their representations. [Writer and regression](../crates/formats/src/sbs/pattern_graph.rs)
Use one additive square covering the canvas with pattern size (4,4), zero roughness, opacity multiplier one and tiling off. Use black for the grayscale background and (.125,.25,.5,.375) for the color background. For each color mode and each luminosity in (.375,−.3125,1.5,0,1), compare three graphs. The first holds the scalar directly in MaterialCraft. The second uses a function with that scalar as its only constant. The third uses the literal swatch (.1875,.5,.8125,luminosity). Save each with the ordinary file command and reopen it. The earlier writer’s ten scalar saves were all rejected by the reference compiler. All ten corrected scalar saves, ten constant-function saves and ten swatch saves compiled.
Render 16 × 8 F32, 8 × 16 U8, 32 × 16 U16 and 16 × 32 F16, then return to the original F32 setting. The seed is 352001 + 37i + 17j for zero-based luminosity and setting indices, with the original seed on return. For RGBA, count a matching pixel only when all four component bit patterns agree.
| Comparison across both color modes | Renders | U8 exact / total | U16 exact / total | F16 exact / total | F32 exact / total | Maximum error |
|---|---:|---:|---:|---:|---:|---:|
| Original MaterialCraft scalar vs. the reference scalar expression | 50 | 1,280/1,280 | 5,120/5,120 | 5,120/5,120 | 2,560/2,560 | 0 |
| Corrected scalar save reopened in both applications | 50 | 1,280/1,280 | 5,120/5,120 | 5,120/5,120 | 2,560/2,560 | 0 |
| Reference scalar expression vs. the reopened scalar save | 50 | 1,280/1,280 | 5,120/5,120 | 5,120/5,120 | 2,560/2,560 | 0 |
| Authored constant expression, reopened in both applications | 50 | 1,280/1,280 | 5,120/5,120 | 5,120/5,120 | 2,560/2,560 | 0 |
| Authored literal swatch, reopened in both applications | 50 | 1,280/1,280 | 5,120/5,120 | 5,120/5,120 | 2,560/2,560 | 0 |
The 549 tracked SBS packages contain 396 function-valued and 59 four-component literal Quadrant opacity properties. None contains a one-component literal in that field. This corrects saved user graphs while the measured root count stays at 475/546. The regression failed on the previous writer and passed after the change. All 127 format-crate release tests passed, along with formatting, strict workspace Clippy, dependency layers and the WebAssembly checks.
### Position, depth and iteration in pattern expressions
Iterate supplies the zero-based repetition number. Quadrant subdivision supplies the branch position, depth and extent. Expressions on descendants can read those values. A Switch or Iterate at the root read the starting position (.5,.5). After Quadrant slots 1 and 2, each read (.625,.375). An Iterate’s initial branch keeps the enclosing number and its repeated branch starts a new counter. [Nested counter measurements](#initial-branch-of-a-nested-pattern-iterator)
+2
View File
@@ -37,6 +37,8 @@ Statuses: **Not checked**, **In progress**, **Parity** (exact pixels or typed va
The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **475 Parity**, **71 In progress** and **0 Too hard**. The November 17–22 comparisons include raw counts, output formats and remaining differences. [November 17 measurements](parity-fixes.md#2026-11-17-root-comparisons) · [November 18 measurements](parity-fixes.md#2026-11-18-root-comparisons) · [November 19 measurements](parity-fixes.md#2026-11-19-root-comparisons) · [November 20 measurements](parity-fixes.md#2026-11-20-root-comparisons) · [November 21 measurements](parity-fixes.md#2026-11-21-root-comparisons) · [November 22 measurements](parity-fixes.md#2026-11-22-root-comparisons)
Each of ten corrected scalar-luminosity saves opened in the reference app. The two applications matched all 14,080 reopened grayscale and color pixels. [Scalar Quadrant luminosity in saved packages](parity-fixes.md#scalar-quadrant-luminosity-in-saved-packages)
The shared pattern-variable checks match **55,680/55,680 pixels**, including saved files, direct predicates and branch offsets. Two controlled F32 overlap graphs also produced different images on repeated eight-worker reference renders. [Position, depth and iteration](parity-fixes.md#position-depth-and-iteration-in-pattern-expressions) · [Worker count and repeated renders](parity-fixes.md#worker-count-and-repeatability-of-overlapping-patterns)
[Pattern tiling axes and copy bounds across 215 renders](parity-fixes.md#tiling-axes-and-pattern-copy-bounds) · [Grayscale sampler measurements](parity-fixes.md#grayscale-sampling-of-color-inputs) · [Value sampling across 240 typed comparisons](parity-fixes.md#value-processor-texture-filtering) · [Five saved graphs reopened in the reference](parity-fixes.md#saved-filter-graphs-and-scalar-levels-controls)