diff --git a/crates/eval/src/node_execution.rs b/crates/eval/src/node_execution.rs index 28f16eda..5a40b7db 100644 --- a/crates/eval/src/node_execution.rs +++ b/crates/eval/src/node_execution.rs @@ -115,7 +115,13 @@ pub(crate) fn cook_node( } let source = options.input_bases.get(&identifier(node))?; // A bridge has its own working dimensions and precision. Its sampling properties follow the wire. - Some(crate::ResolvedBaseParameters { pixel_size: source.pixel_size, pixel_ratio: source.pixel_ratio, tiling: source.tiling, ..prepared.parent_base }) + Some(crate::ResolvedBaseParameters { + pixel_size: source.pixel_size, + pixel_ratio: source.pixel_ratio, + tiling: source.tiling, + bound_image: source.bound_image, + ..prepared.parent_base + }) }); let (mut base, base_values, base_error) = match crate::resolved_base_parameters::evaluate(&node.base, input_base, prepared.parent_base, &mut variables, &fixed) { @@ -172,12 +178,22 @@ pub(crate) fn cook_node( } let seed = base.seed; - let ResolvedParameters { values: mut parameters, instance_literals, dynamic_blend_opacity, bound_pattern_property } = + let ResolvedParameters { values: mut parameters, instance_literals, dynamic_blend_opacity, bound_image_property } = match resolve_parameters(package, node, definition, &mut variables, base, &fixed) { Ok(parameters) => parameters, Err(error) => return fail(error, 0), }; - let bound_pattern_property = bound_pattern_property || (node.def == "fx_map" && (base.bound_seed || base.bound_size)); + let bound_image = if node.disabled { + base.bound_image + } else { + base.bound_size + || bound_image_property + || dynamic_blend_opacity + || (node.def == "fx_map" && base.bound_seed) + || input_base.is_some_and(|source| source.bound_image) + || incoming.iter().flatten().any(|(_, source)| source.bound_image) + }; + let base = crate::ResolvedBaseParameters { bound_image, ..base }; if matches!(node.def.as_str(), "blur" | "emboss") { variables.to_mut().insert("$pixelsize".into(), Value::Float2(base.pixel_size)); } @@ -215,7 +231,7 @@ pub(crate) fn cook_node( let key = hash_of(&( &node.def, serde_json::to_string(¶meters).unwrap_or_default(), - (dynamic_blend_opacity, bound_pattern_property), + (dynamic_blend_opacity, bound_image), ( w, h, @@ -325,7 +341,13 @@ pub(crate) fn cook_node( Ok(vec![output]) } else { let result = if node.def == "fx_map" { - materialcraft_nodes::pattern::cook_with_bound_properties(&context, &base_level_only, bound_pattern_property) + materialcraft_nodes::pattern::cook_with_image_dependencies(&context, &base_level_only, bound_image).map(|(image, reads_input)| { + // A cached pattern also supplies its drawing inputs to the next image on the wire. + if reads_input != bound_image { + output_bases = Some(Arc::from([crate::ResolvedBaseParameters { bound_image: reads_input, ..base }])); + } + vec![image] + }) } else if node.def == "text" && !context.text("fontdata").is_empty() && let Some(font) = package.resource(context.text("fontdata")) diff --git a/crates/eval/src/parameter.rs b/crates/eval/src/parameter.rs index ecacdd85..241c3d6e 100644 --- a/crates/eval/src/parameter.rs +++ b/crates/eval/src/parameter.rs @@ -8,7 +8,7 @@ pub(crate) struct ResolvedParameters { pub values: BTreeMap, pub instance_literals: Vec, pub dynamic_blend_opacity: bool, - pub bound_pattern_property: bool, + pub bound_image_property: bool, } pub(crate) fn supports_literal_argument(value: &Value) -> bool { @@ -51,8 +51,13 @@ pub(crate) fn resolve_parameters( let mut dynamic = std::collections::BTreeSet::new(); let mut dynamic_blend_opacity = false; - let mut bound_pattern_property = false; - let pattern_property = |name: &str| node.def == "fx_map" && matches!(name, "opacity" | "roughness" | "background" | "render_region" | "tiling_region"); + let mut bound_image_property = false; + let image_property = |name: &str| { + matches!( + (node.def.as_str(), name), + ("uniform", "color") | ("blur", "intensity") | ("fx_map", "opacity" | "roughness" | "background" | "render_region" | "tiling_region") + ) + }; let mut uniform_color = None; for (parameter, exposed) in &node.bindings { if node.def == "uniform" && parameter == "color" { @@ -67,8 +72,8 @@ pub(crate) fn resolve_parameters( if node.def == "blend" && parameter == "opacity" { dynamic_blend_opacity = !literal_inputs.contains(exposed); } - if pattern_property(parameter) && !node.functions.contains_key(parameter) { - bound_pattern_property |= !literal_inputs.contains(exposed); + if image_property(parameter) && !node.functions.contains_key(parameter) { + bound_image_property |= !literal_inputs.contains(exposed); } parameters.insert(parameter.clone(), coerced); } @@ -92,8 +97,8 @@ pub(crate) fn resolve_parameters( if node.def == "blend" && parameter == "opacity" { dynamic_blend_opacity = program.reads_graph_parameter(variables); } - if pattern_property(parameter) { - bound_pattern_property |= program.reads_graph_parameter(variables); + if image_property(parameter) { + bound_image_property |= program.reads_graph_parameter(variables); } crate::parameter_function::evaluate(&program, expected, variables.to_mut(), base.size, mode, Some(current)) }) @@ -140,7 +145,7 @@ pub(crate) fn resolve_parameters( } else { Vec::new() }; - Ok(ResolvedParameters { values: parameters, instance_literals: fixed, dynamic_blend_opacity, bound_pattern_property }) + Ok(ResolvedParameters { values: parameters, instance_literals: fixed, dynamic_blend_opacity, bound_image_property }) } pub(crate) fn inspection_parameters(node: &Node, parameters: &mut BTreeMap) { diff --git a/crates/eval/src/prepared_graph.rs b/crates/eval/src/prepared_graph.rs index 4edec9dd..ffce1974 100644 --- a/crates/eval/src/prepared_graph.rs +++ b/crates/eval/src/prepared_graph.rs @@ -46,6 +46,7 @@ impl<'a> PreparedGraph<'a> { base.size = self.parent_base.size; base.size_underflow = self.parent_base.size_underflow; base.dynamic_size = self.parent_base.dynamic_size; + base.bound_size = self.parent_base.bound_size; } Some(base) diff --git a/crates/eval/src/resolved_base_parameters.rs b/crates/eval/src/resolved_base_parameters.rs index b41a35b1..d72d3ca0 100644 --- a/crates/eval/src/resolved_base_parameters.rs +++ b/crates/eval/src/resolved_base_parameters.rs @@ -15,6 +15,9 @@ pub struct ResolvedBaseParameters { /// This node's seed expression reads a live graph input. #[serde(skip)] pub bound_seed: bool, + /// A drawing property on this image or a connected source reads a live graph input. + #[serde(skip)] + pub bound_image: bool, /// Negative requested exponents on axes whose allocated texture is one texel. #[serde(skip)] pub size_underflow: [i32; 2], @@ -40,6 +43,7 @@ impl ResolvedBaseParameters { dynamic_size: graph.absolute_output_size.is_none() && parent.is_some_and(|base| base.dynamic_size), bound_size: graph.output_size_setting().0 != materialcraft_core::SizeMode::Absolute && parent.is_some_and(|base| base.bound_size), bound_seed: false, + bound_image: false, size_underflow, seed: resolve_seed(graph.seed_mode, graph.seed, parent.map_or(0, |base| base.seed)), format: match graph.format { @@ -119,6 +123,7 @@ impl ResolvedBaseParameters { dynamic_size: size_mode.resolve(false, input.map(|base| base.dynamic_size), parent.dynamic_size), bound_size: size_mode.resolve(false, input.map(|base| base.bound_size), parent.bound_size), bound_seed: false, + bound_image: false, seed: resolve_seed(base.seed_mode, base.seed, parent.seed), format: BaseParameter::Format.inheritance(base).resolve(base.format, input.map(|base| base.format), parent.format), tiling: base.tiling_mode.resolve(base.tiling, input.map(|base| base.tiling), parent.tiling), diff --git a/crates/eval/tests/pattern_opacity.rs b/crates/eval/tests/pattern_opacity.rs index a9a42891..82c5b03a 100644 --- a/crates/eval/tests/pattern_opacity.rs +++ b/crates/eval/tests/pattern_opacity.rs @@ -2,8 +2,7 @@ use materialcraft_core::{FunctionGraph, Graph, OutputFormat, Package, PatternGra use materialcraft_eval::{Cache, Options, cook}; use std::sync::Mutex; -#[test] -fn bound_pattern_property_changes_the_first_local_read_at_equal_opacity() { +fn first_local_probe() -> (Package, materialcraft_core::NodeId) { let mut graph = Graph::new("First quadrant read"); graph.size = [4; 2]; graph.format = OutputFormat::F32; @@ -26,6 +25,12 @@ fn bound_pattern_property_changes_the_first_local_read_at_equal_opacity() { let mut package = Package::new("Quadrant parameter origin"); package.graphs.push(graph); + (package, stamp) +} + +#[test] +fn bound_pattern_property_changes_the_first_local_read_at_equal_opacity() { + let (mut package, stamp) = first_local_probe(); let mut options = Options::new(&|_| None); options.overrides.insert("gain".into(), Value::Float(1.0)); let cache = Mutex::new(Cache::default()); @@ -60,6 +65,47 @@ fn bound_pattern_property_changes_the_first_local_read_at_equal_opacity() { } } +#[test] +fn connected_image_properties_reach_writable_quadrants() { + let (mut package, stamp) = first_local_probe(); + let graph = &mut package.graphs[0]; + let uniform = graph.add_node("uniform", [0.0; 2]); + let source = graph.node_mut(uniform).unwrap(); + source.params.insert("color_mode".into(), Value::Bool(false)); + source.functions.insert("color".into(), FunctionGraph::input("swatch", ValueType::Float4)); + let blur = graph.add_node("blur", [0.0; 2]); + graph.node_mut(blur).unwrap().params.insert("intensity".into(), Value::Float(0.0)); + graph.connect(uniform, "output", blur, "input").unwrap(); + let drawing = graph.add_node("fx_map", [0.0; 2]); + let mut square = PatternGraph::with_root(PatternNodeKind::Quadrant); + square.nodes[0].parameters.insert("patterntype".into(), Value::Int(2)); + square.nodes[0].parameters.insert("opacity".into(), Value::Function(Box::new(FunctionGraph::input("swatch", ValueType::Float4)))); + let stage = graph.node_mut(drawing).unwrap(); + stage.params.insert("color_mode".into(), Value::Bool(false)); + stage.params.insert("pattern".into(), Value::PatternGraph(Box::new(square))); + let mut options = Options::new(&|_| None); + options.overrides.insert("swatch".into(), Value::Float4([1.0; 4])); + let cache = Mutex::new(Cache::default()); + for port in ["pattern0", "background"] { + for source in [uniform, blur, drawing] { + for (connected, fixed) in [(false, false), (true, true), (true, false), (true, true)] { + let graph = &mut package.graphs[0]; + graph.disconnect(stamp, port); + if connected { + graph.connect(source, "output", stamp, port).unwrap(); + } + options.literal_inputs = if fixed { vec!["swatch".into()] } else { Vec::new() }; + let result = cook(&package, 0, &options, &cache); + assert_eq!(result.nodes[&stamp].error, None); + let background = if connected && port == "background" { 1.0 } else { 0.0 }; + let first = if connected && !fixed { 0.625 } else { 0.375 }; + assert_eq!(result.image(stamp, 0).unwrap().get_gray(8, 8, false), background + first, "{port}, {source:?}, {connected}, {fixed}"); + } + } + package.graphs[0].disconnect(stamp, port); + } +} + #[test] fn gray_pattern_opacity_keeps_signed_and_undefined_contributions_until_blending() { let mut graph = Graph::new("Signed stamp opacity"); diff --git a/crates/nodes/src/pattern/evaluation.rs b/crates/nodes/src/pattern/evaluation.rs index 26cf7838..71466eab 100644 --- a/crates/nodes/src/pattern/evaluation.rs +++ b/crates/nodes/src/pattern/evaluation.rs @@ -184,7 +184,16 @@ pub fn cook_with_bound_properties(context: &CookContext, base_level_only: &[bool cook_with_visit_limit(context, base_level_only, MAX_VISITS, bound_property) } +/// Cook the image and report live drawing inputs for a downstream pattern. +pub fn cook_with_image_dependencies(context: &CookContext, base_level_only: &[bool], bound_property: bool) -> Result<(craft_texture::Image, bool), String> { + evaluate(context, base_level_only, MAX_VISITS, bound_property) +} + pub(super) fn cook_with_visit_limit(context: &CookContext, base_level_only: &[bool], visit_limit: usize, bound_property: bool) -> CookResult { + evaluate(context, base_level_only, visit_limit, bound_property).map(|(image, _)| vec![image]) +} + +fn evaluate(context: &CookContext, base_level_only: &[bool], visit_limit: usize, bound_property: bool) -> Result<(craft_texture::Image, bool), String> { let Some(Value::PatternGraph(graph)) = context.params.get("pattern") else { return Err("missing pattern graph".into()); }; @@ -238,7 +247,8 @@ pub(super) fn cook_with_visit_limit(context: &CookContext, base_level_only: &[bo let mut variables = PatternVariables::new(context.vars.graph_values()); let has_locals = functions.values().any(|(_, names)| !names.is_empty()); - variables.bound_draw = has_locals && (bound_property || has_bound_draw(graph.root, &nodes, &functions, context, &variables.modes)); + variables.bound_draw = + bound_property || (context.vars.keys().any(|name| !name.starts_with('$')) && has_bound_draw(graph.root, &nodes, &functions, context, &variables.modes)); let separate_child_variables = opacity_depth == 6 && has_locals && graph.root.is_some_and(|root| has_uniform_quadrant_leaves(root, opacity_depth, &nodes)); let mut max_depth = 0; let mut state = EvaluationState::default(); @@ -608,5 +618,7 @@ pub(super) fn cook_with_visit_limit(context: &CookContext, base_level_only: &[bo .or_else(|| underfilled.and_then(|runs| runs.finish(&stamps))) .or_else(|| repeated_six_level.and_then(|runs| runs.finish(&stamps, repeated_roots.unwrap_or_default()))) .or_else(|| repeated_roots.and_then(|repeats| super::repeated_quadrants::runs(context, &stamps, repeats, max_depth))); - Ok(vec![raster::render(context, mixed_groups.as_deref().unwrap_or(&groups), opacities, nonfinite_gray_opacity, base_level_only, runtime_image_selector)?]) + let image = + raster::render(context, mixed_groups.as_deref().unwrap_or(&groups), opacities, nonfinite_gray_opacity, base_level_only, runtime_image_selector)?; + Ok((image, variables.bound_draw)) } diff --git a/crates/nodes/src/pattern/mod.rs b/crates/nodes/src/pattern/mod.rs index 26fe259d..5595a361 100644 --- a/crates/nodes/src/pattern/mod.rs +++ b/crates/nodes/src/pattern/mod.rs @@ -19,7 +19,7 @@ mod variables; pub use catalog::{definition, definitions}; pub(crate) use evaluation::cook; -pub use evaluation::{cook_with_base_level_inputs, cook_with_bound_properties}; +pub use evaluation::{cook_with_base_level_inputs, cook_with_bound_properties, cook_with_image_dependencies}; #[cfg(test)] mod tests; diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 24d20362..c94e8faa 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-25 root comparisons +### Connected image properties and the first quadrant draw + +Connect a white Uniform to the pattern socket of the [four-square writable-float graph](#first-reads-of-writable-quadrant-floats). Use square mode so the draw does not sample this socket. Give each component of the Uniform the value one plus zero times the published scalar. The first square contributes .625, and the other three contribute .375. Replacing the Uniform with literal white, computing white entirely from constants or disconnecting it gives four .375 contributions. A Uniform with a published output-size offset of zero reproduces the first result. The evaluator carries this dependency through connected image outputs and includes it in the cook cache. [Graph evaluator](../crates/eval/src/node_execution.rs) · [Cache regression](../crates/eval/tests/pattern_opacity.rs) + +Use eight original graphs. Connect the live Uniform to the pattern and background sockets in turn. Use the literal and disconnected controls on the pattern socket. Insert a zero-intensity Blur after the live Uniform, then test a Blend whose opacity evaluates to one from a published input and whose source is fixed. For the last two graphs, connect another FX-Map whose square luminosity or outer opacity evaluates to one from that input. Render all eight with default .8125 at 16 × 32 F32 and 32² F16, and changed .3125 at 32 × 16 U8 and 16² U16. The respective seeds in table order are 41671, 41684, 41697 and 41710. Save each graph normally in MaterialCraft and repeat the measurements on the reopened graph in the reference desktop. + +| Storage | Previous exact pixels | Previous maximum error | Current original exact | Current reopened exact | Current maximum error | +|---|---:|---:|---:|---:|---:| +| F32 | 3,328/4,096 | .25 | 4,096/4,096 | 4,096/4,096 | 0 | +| U8 | 3,456/4,096 | 63 codes | 4,096/4,096 | 4,096/4,096 | 0 | +| F16 | 6,656/8,192 | .25 | 8,192/8,192 | 8,192/8,192 | 0 | +| U16 | 1,728/2,048 | 16,383 codes | 2,048/2,048 | 2,048/2,048 | 0 | + +At 16² and 32 × 16 F32, seeds 41301 and 41309, the direct input and background cases match 4,608/4,608 pixels. The nine literal, constant-expression and disconnected controls match 6,912/6,912. The six Blur and Blend chains match 4,608/4,608. Another 5,376/5,376 pixels distinguish an upstream pattern's drawing properties from the Uniform's seed, format, tiling and color selector. Connecting the live Uniform to the foreground, background or mask of a Blend gives the same first draw at fixed opacity zero and one, including when the selected color is fixed. All 4,608 pixels in those twelve settings match. The maximum error is zero in each group. + +Eight whole Noise BNW Spots 1 v2 settings retain every previous output bit and match 25,546/25,600 reference pixels, maximum one U16 code. The Position Debug and Bevel Smooth Debug Position controls match all 768 and 4,096 RGBA8 pixels respectively. The Tile helper matches 49,152/49,152 U16 pixels across defaults, seed 42 and changed parameters. The original and reopened spline preprocess controls each match 294,912/294,912 image pixels and six of six typed counts. The dense noise roots and the [remaining writable-variable widths](#first-reads-of-writable-quadrant-floats) stay open. + +The new cache regression first failed with .375 in the live square and now passes. All 505 release tests for the evaluator and node library, formatting, strict workspace Clippy, dependency layers and WebAssembly passed. + ### First reads of writable quadrant floats The first read of a writable scalar, float2 or float3 at a quadrant depth can produce undefined components when a drawing property reads a live graph input. The next visit at that depth sees the previous assignment. To distinguish these cases, connect four child slots to one square. In the square’s luminosity function, assign .375 if the local equals itself and .625 otherwise. With a live parent branch offset the squares read .625, .375, .375, .375 in traversal order. Substituting a literal for the graph input gives four .375 squares in this comparison. [Pattern evaluator](../crates/nodes/src/pattern/evaluation.rs) · [Variable scope](../crates/nodes/src/pattern/variables.rs) @@ -78,7 +97,7 @@ For the component-width control, store the local plus a zero vector and start lu Connect a second square to a switch and drive its luminosity from a published input. The first square reads .625 even with the switch fixed to the other branch. Disconnecting the second square or setting its shape to zero gives .375. A published branch offset or seed still affects a shapeless quadrant. A published image index affects an image stamp with no texture connected. Image filtering and seed inheritance each leave the first square at .375. A graph-size function that returns (0,0) from a published scalar affects both relative size modes. With an absolute 16² size on the pattern node the first square reads .375. The six graph size and seed inheritance controls match 4,352/4,352 F32 pixels. [Connected-branch regression](../crates/nodes/src/pattern/tests.rs) -Across ten scalar first-read expressions with fixed and live parents, 14,976/15,360 F32 pixels match, up from 14,016. The 384 remaining differences occur on the fixed-parent 32 × 16 captures, maximum .25. Thirty other binding and assignment variants improve from 12,288/23,040 to 19,200/23,040, maximum 1.5. A separate reference control with a live upstream Uniform color and literal properties on the pattern node also changes the first square. Disconnecting the Uniform or replacing its color with the same literal restores the four .375 readings. Propagation from the upstream image remains open. +Across ten scalar first-read expressions with fixed and live parents, 14,976/15,360 F32 pixels match, up from 14,016. The 384 remaining differences occur on the fixed-parent 32 × 16 captures, maximum .25. Thirty other binding and assignment variants improve from 12,288/23,040 to 19,200/23,040, maximum 1.5. A separate reference control with a live upstream Uniform color and literal properties on the pattern node also changes the first square. Disconnecting the Uniform or replacing its color with the same literal restores the four .375 readings. [Connected-image measurements and fix](#connected-image-properties-and-the-first-quadrant-draw) Seven original and four reopened settings of each Noise BNW Spots 1 version retain their previous MaterialCraft buffers bit for bit. Version one matches 20,618/20,992 original pixels on one processor and 20,455/20,992 on eight, with maximum one U16 code. Its ordinary saves match 2,799/2,816, maximum 5.96046448e−8. Version two matches 20,904/20,992 on both processor counts and 4,273/4,352 on saves, with respective maxima 1.19209290e−7 and 5.96046448e−8. Both whole roots remain in progress. [Original settings and previous buffers](#undefined-components-in-a-computed-quadrant-tint) diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 0c64c2ab..9c0e1c89 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -37,7 +37,7 @@ 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 **480 Parity**, **66 In progress** and **0 Too hard**. The November 17–25 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) · [November 23 measurements](parity-fixes.md#2026-11-23-root-comparisons) · [November 24 measurements](parity-fixes.md#2026-11-24-root-comparisons) · [November 25 measurements](parity-fixes.md#2026-11-25-root-comparisons) -Writable quadrant tints now match 16,896/16,896 pixels in 44 settings across four output formats, up from 4,608. Fixed and published inputs through a graph instance match 3,840/3,840 original and 3,840/3,840 reopened pixels. Live float4 and integer initial values remain open. The earlier explicit undefined-tint controls covered 70,656 pixels across original squares, ordinary saves, Gaussian and ramp shapes and connected images. [November 25 measurements](parity-fixes.md#first-reads-of-writable-quadrant-floats) · [Explicit undefined tints](parity-fixes.md#undefined-components-in-a-computed-quadrant-tint) +Writable quadrant tints now match 16,896/16,896 pixels in 44 settings across four output formats, up from 4,608. Fixed and published inputs through a graph instance match 3,840/3,840 original and 3,840/3,840 reopened pixels. Connected image properties match all 18,432 original and all 18,432 reopened pixels across 32 settings, up from 15,168 on each side. Live float4 and integer initial values remain open. The earlier explicit undefined-tint controls covered 70,656 pixels across original squares, ordinary saves, Gaussian and ramp shapes and connected images. [Connected images](parity-fixes.md#connected-image-properties-and-the-first-quadrant-draw) · [Writable floats](parity-fixes.md#first-reads-of-writable-quadrant-floats) · [Explicit undefined tints](parity-fixes.md#undefined-components-in-a-computed-quadrant-tint) Noise BNW Spots 1 still matches 403/512 pixels in the repeatable 16 × 32 F32 setting. Signed-square measurements reproduce the final differing pixel of a 511/512 diagnostic replay. The dense draw order remains open. [Draw-order measurements](parity-fixes.md#the-remaining-sample-in-a-dense-draw-order-replay)