From 968488b4bebd2e142272eda65ff53237b048c616 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Thu, 19 Nov 2026 22:56:04 -0500 Subject: [PATCH] Use pattern field defaults for unbound direct inputs --- crates/nodes/src/pattern/evaluation.rs | 4 ++++ crates/nodes/src/pattern/tests.rs | 32 ++++++++++++++++++++++++++ docs/parity-fixes.md | 19 +++++++++++++++ docs/sbs-root-nodes.md | 2 ++ 4 files changed, 57 insertions(+) diff --git a/crates/nodes/src/pattern/evaluation.rs b/crates/nodes/src/pattern/evaluation.rs index b0902f9..a863e27 100644 --- a/crates/nodes/src/pattern/evaluation.rs +++ b/crates/nodes/src/pattern/evaluation.rs @@ -72,6 +72,10 @@ pub(crate) fn cook(context: &CookContext) -> CookResult { let mut evaluate = |id: &str, random: &mut FunctionRandom| -> Result, String> { let Some(program) = functions.get(&(node.id, id)) else { return Ok(node.parameters.get(id).cloned()) }; + // An unbound direct link leaves the field at its default. A getter inside an expression still evaluates to its typed zero. + if program.input_identifier().is_some_and(|name| !name.starts_with('$') && !variables.contains_key(name)) { + return Ok(None); + } let evaluation = EvaluationContext { image_formats: context.input_formats.get(1..).unwrap_or(&[]), scope: crate::function::FunctionScope::FxMap, diff --git a/crates/nodes/src/pattern/tests.rs b/crates/nodes/src/pattern/tests.rs index 4cfde47..fa11a44 100644 --- a/crates/nodes/src/pattern/tests.rs +++ b/crates/nodes/src/pattern/tests.rs @@ -391,6 +391,38 @@ fn empty_switches_at_later_depths_still_affect_roughness() { assert_eq!((definition.cook)(&context).unwrap()[0].get_gray(8, 8, false), 1.0 / 3.0); } +#[test] +fn missing_direct_pattern_inputs_use_the_field_default() { + use materialcraft_core::ValueType; + + let direct = FunctionGraph::input("strength", ValueType::Float); + let mut derived = direct.clone(); + let read = derived.output.unwrap(); + let offset = derived.add_constant(Value::Float(0.375)); + derived.output = Some(derived.add("add", &[read, offset], None)); + for (expression, binding, expected) in [ + (direct.clone(), None, 1.0), + (direct.clone(), Some(0.0), 0.0), + (direct, Some(0.375), 0.375), + (derived, None, 0.375), + (FunctionGraph::input("tint", ValueType::Float4), None, 1.0), + ] { + let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant); + graph.nodes[0].parameters = BTreeMap::from([("patterntype".into(), Value::Int(2)), ("opacity".into(), Value::Function(Box::new(expression)))]); + let vars = binding.map(|v| ("strength".into(), Value::Float(v))).into_iter().collect(); + let image = cook_with_variables(graph, &vars, 7).unwrap().remove(0); + assert_eq!(image.get_gray(8, 8, false), expected); + } + + let mut graph = PatternGraph::with_root(PatternNodeKind::Iterate); + let square = graph.add(PatternNodeKind::Quadrant).unwrap(); + graph.nodes[0].children[1] = Some(square); + graph.nodes[0].parameters.insert("numberadded".into(), Value::Function(Box::new(FunctionGraph::input("count", ValueType::Integer)))); + graph.nodes[1].parameters.insert("patterntype".into(), Value::Int(2)); + graph.nodes[1].parameters.insert("patternsize".into(), Value::Function(Box::new(FunctionGraph::input("extent", ValueType::Float2)))); + assert!(cook(graph).unwrap()[0].data().iter().all(|&value| value == 1.0)); +} + #[test] fn a_quadrant_only_publishes_writes_from_active_stamp_properties() { for (pattern, property, result, expected) in [ diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index c0c30f4..73aea8f 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -387,6 +387,25 @@ Isolate the shortest connected path from the pattern root to a drawing quadrant | noise_crystal_1 | 4 | 35,328 | 30,215/35,328 | 0 / 1 / .00048828125 / .00000101327896 | | noise_crystal_2 | 12 | 17,664 | 17,512/17,664 | 0 / 1 / .000244140625 / .00000238418579 | +### Direct pattern links with an unbound variable + +An unbound user variable connected directly to a pattern property leaves that property at its default. Within an arithmetic expression the same getter contributes its typed zero. A bound zero supplies an actual zero. MaterialCraft now distinguishes these cases when evaluating a pattern graph. + +| Original control | Renders | Exact pixels / compared | Maximum F32 / U16 error | +|---|---:|---:|---| +| Grayscale and RGBA square, scalar and Float4 opacity | 84 | 35,840/35,840 | 0 / 0 | +| Square size from a Float2 link | 21 | 8,960/8,960 | 0 / 0 | +| Iterate count from an integer link | 21 | 8,960/8,960 | 0 / 0 | +| Seed inheritance from a Boolean link | 21 | 8,310/8,960 | .694821596 / 40631 | + +For the first row, render a centered square of size (.75,.75). In each output kind and for each opacity type, compare seven settings. Omit opacity, read an unbound user variable directly, bind that variable to zero or a second value in an outer Iterate, add the second value to an unbound read, or author either value directly. Use .375 for the scalar and (.25,.5,.75,1) for the vector. Author the scalar controls as constant-return expressions and the vector controls as literal colors. In grayscale, the vector expression supplies its alpha and the literal vector supplies its mean RGB. Use 16² F32, 32×16 F32 and 32×16 U16 at seeds 100801 through 100803. Before the change 32,960/35,840 pixels matched, with maxima of 1 F32 and 65535 U16 levels. + +For the next three rows start with an outer Iterate's initial child. Use a square of size (.8125,.625) and RGBA (.125,.25,.375,1). Compare omission, an unbound read, explicit constant-return expressions for zero and a second value, matching assignments in the outer Iterate, and a derived expression. Use (.375,.625) for the second size. To measure count, insert a second Iterate and draw (.125,.25,.375,.25) on its repeated branch with a changed count of 2. For seed inheritance, use a seed offset of 43, a random stamp color, true as the second value and logical negation for the derived expression. For property index j in the displayed order use the same sizes and formats at seed 101621+11j+i, with i from 0 through 2. Together these rows improved from 23,650 to 26,230/26,880. The remaining 650 pixels occur when the outer Iterate assigns false to the seed-inheritance variable. The reference uses the field default there. [Quadrant mode reads and writes](#assignments-in-quadrant-mode-expressions) + +Six additional original files at format version 1.1.0.202502 were saved by MaterialCraft, reopened in the reference app and compared against their source-file renders. The four pattern files exercise writes from empty and drawing squares, an image-index expression and an exposed bilinear filter. The two pixel-read files exercise an 8² source and a 32×16 source feeding a 16² output. All 19 stored-image comparisons matched, **6,912/6,912 pixels**, maximum error zero. [Pattern properties](#active-properties-of-empty-and-drawing-quadrants) · [Filtered pixel reads](#subnormal-values-in-filtered-pixel-reads) + +The 19 previously verified roots also match all 3,211,776 pixels in 159 renders after the direct-link change, maximum error zero. + ### Assignments in quadrant mode expressions Local assignments inside the pattern type, blend mode, image filter, premultiplication and seed-inheritance expressions affect that expression's result. Later properties and children retain their incoming variables. Across the [117 child-color controls](#active-properties-of-empty-and-drawing-quadrants), the count improved from 49,120 to **49,920/49,920 exact RGBA pixels**, maximum error zero. The image-index expression still publishes its assignment on a bound image stamp. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index f6eb723..95ebf0c 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -47,6 +47,8 @@ The 32²-to-16² signed threshold controls match 2,048/2,048 pixels. [Floating t Connected F32 backgrounds now match all 2,048 same-size pixels. Across 132 further renders with changed input sizes, the maximum absolute error is zero. Some RGBA zeros still differ in sign. [Connected background counts and file checks](parity-fixes.md#connected-floating-point-pattern-backgrounds) +Direct links to missing pattern inputs now use the field default. All 53,760 opacity, size and iteration-count pixels match. In the separate seed-inheritance controls 8,310/8,960 pixels match. [Raw counts and six saved-file checks](parity-fixes.md#direct-pattern-links-with-an-unbound-variable) + Assignments in quadrant mode expressions now stay within the expression. All 49,920 child-color pixels match. [Mode writes and remaining input-scope differences](parity-fixes.md#assignments-in-quadrant-mode-expressions) Empty quadrants now skip inactive stamp properties. The variable-writing controls match 49,120/49,920 pixels over 117 renders. [Measured property scopes](parity-fixes.md#active-properties-of-empty-and-drawing-quadrants)