From 4e445b1722d755ddbf5b229a8b392d70275ccfdf Mon Sep 17 00:00:00 2001 From: chasse20 Date: Mon, 23 Nov 2026 07:42:21 -0500 Subject: [PATCH] Respect typed arguments on graph instance input bridges --- crates/eval/src/graph_instance.rs | 12 +++++ crates/eval/src/node_execution.rs | 10 +--- crates/eval/src/port.rs | 3 ++ crates/eval/src/ports.rs | 69 +++++++++++------------- crates/eval/src/tests.rs | 39 ++++++++++++++ crates/nodes/src/parameter_descriptor.rs | 22 +++++++- docs/parity-fixes.md | 18 +++++++ docs/sbs-root-nodes.md | 2 +- 8 files changed, 124 insertions(+), 51 deletions(-) diff --git a/crates/eval/src/graph_instance.rs b/crates/eval/src/graph_instance.rs index 0c7f7fad..92bff191 100644 --- a/crates/eval/src/graph_instance.rs +++ b/crates/eval/src/graph_instance.rs @@ -93,6 +93,18 @@ pub(crate) fn cook_instance( } } for (port, input) in input_ports.iter().zip(inputs) { + // A stored argument drives its bridge even when the same port has a wire. + if let Some(expected) = port.value_type + && (node.params.contains_key(&port.id) || node.bindings.contains_key(&port.id) || node.functions.contains_key(&port.id)) + && let Some(value) = params.get(&port.id) + { + if materialcraft_core::ValueType::of(value) != Some(expected) { + return Err(format!("input `{}` requires {}", port.id, expected.label())); + } + source_options.overrides.insert(port.id.clone(), value.clone()); + source_options.literal_inputs.retain(|name| name != &port.id); + continue; + } let Some((output, format)) = input else { continue }; if let Some((image, texture_format)) = output.texture(*format) { source_options.inputs.insert(port.id.clone(), image.clone()); diff --git a/crates/eval/src/node_execution.rs b/crates/eval/src/node_execution.rs index 15822be7..aebb61aa 100644 --- a/crates/eval/src/node_execution.rs +++ b/crates/eval/src/node_execution.rs @@ -71,15 +71,7 @@ pub(crate) fn cook_node( input_keys.push(source.as_ref().map_or(0, |source| source.1)); let output = source.map(|source| source.0); - let expected = node.input_values.get(&port.id).copied().or_else(|| { - node.instance - .as_ref()? - .inputs - .iter() - .find(|input| input.identifier == port.id) - .and_then(|input| materialcraft_core::ValueType::from_code(input.type_code)) - }); - if let Some(expected) = expected { + if let Some(expected) = port.value_type { let value = match output.as_ref() { Some(NodeOutput::Value(value)) if materialcraft_core::ValueType::of(value) == Some(expected) => value.clone(), Some(_) => { diff --git a/crates/eval/src/port.rs b/crates/eval/src/port.rs index 2c35bbda..345da7fb 100644 --- a/crates/eval/src/port.rs +++ b/crates/eval/src/port.rs @@ -1,3 +1,4 @@ +use materialcraft_core::ValueType; use materialcraft_nodes::PortKind; #[derive(Clone, Debug, PartialEq)] @@ -5,4 +6,6 @@ pub struct Port { pub id: String, pub label: String, pub kind: PortKind, + /// Concrete type of a declared value port. + pub value_type: Option, } diff --git a/crates/eval/src/ports.rs b/crates/eval/src/ports.rs index f141c8af..7981f15c 100644 --- a/crates/eval/src/ports.rs +++ b/crates/eval/src/ports.rs @@ -14,53 +14,38 @@ pub fn ports_with_connections<'a>( incoming: impl IntoIterator, ) -> (Vec, Vec) { if node.def == "graph_instance" { + let ports = |source: &[materialcraft_core::GraphPort]| { + source + .iter() + .map(|port| Port { + id: port.identifier.clone(), + label: port.identifier.clone(), + kind: match port.type_code { + 1 => PortKind::Color, + 2 => PortKind::Gray, + _ => PortKind::Value, + }, + value_type: materialcraft_core::ValueType::from_code(port.type_code), + }) + .collect() + }; if let Some(instance) = &node.instance { - let ports = |ports: &[materialcraft_core::GraphPort]| { - ports - .iter() - .map(|port| Port { - id: port.identifier.clone(), - label: port.identifier.clone(), - kind: match port.type_code { - 1 => PortKind::Color, - 2 => PortKind::Gray, - _ => PortKind::Value, - }, - }) - .collect() - }; return (ports(&instance.inputs), ports(&instance.outputs)); } let Some((_, graph)) = instance_graph(package, node) else { return (Vec::new(), Vec::new()); }; - - let graph_ports = |definition: &str| -> Vec { - graph - .nodes - .iter() - .filter(|node| node.def == definition || (definition == "input" && node.def.starts_with("input_"))) - .map(|node| { - let id = identifier(node); - let kind = match node.def.as_str() { - "input_grayscale" => PortKind::Gray, - "input_color" => PortKind::Color, - _ => PortKind::Any, - }; - - Port { label: id.clone(), id, kind } - }) - .collect() - }; - - return (graph_ports("input"), graph_ports("output")); + let (inputs, outputs) = materialcraft_nodes::graph_interface::graph_ports(graph); + return (ports(&inputs), ports(&outputs)); } if node.def == materialcraft_core::MISSING_NODE { let (inputs, outputs) = materialcraft_core::missing_ports(node); - let inputs = inputs.into_iter().map(|id| Port { label: id.clone(), id, kind: PortKind::Any }).collect(); - let outputs = - outputs.into_iter().map(|(id, grayscale)| Port { label: id.clone(), id, kind: if grayscale { PortKind::Gray } else { PortKind::Color } }).collect(); + let inputs = inputs.into_iter().map(|id| Port { label: id.clone(), id, kind: PortKind::Any, value_type: None }).collect(); + let outputs = outputs + .into_iter() + .map(|(id, grayscale)| Port { label: id.clone(), id, kind: if grayscale { PortKind::Gray } else { PortKind::Color }, value_type: None }) + .collect(); return (inputs, outputs); } @@ -69,7 +54,7 @@ pub fn ports_with_connections<'a>( return (Vec::new(), Vec::new()); }; - let catalog_port = |port: &materialcraft_nodes::PortDefinition| Port { id: port.id.into(), label: port.label.into(), kind: port.kind }; + let catalog_port = |port: &materialcraft_nodes::PortDefinition| Port { id: port.id.into(), label: port.label.into(), kind: port.kind, value_type: None }; let mut inputs: Vec = node .input_values .iter() @@ -77,6 +62,7 @@ pub fn ports_with_connections<'a>( id: identifier.clone(), label: format!("{} ({})", crate::value_input_label(identifier), value_type.label()), kind: PortKind::Value, + value_type: Some(*value_type), }) .collect(); inputs.extend(definition.inputs.iter().map(catalog_port)); @@ -89,7 +75,12 @@ pub fn ports_with_connections<'a>( .max() .unwrap_or(1) .min(materialcraft_nodes::MAX_IMAGE_INPUTS); - inputs.extend((1..count).map(|index| Port { id: format!("{prefix}{index}"), label: format!("Input image {index}"), kind: PortKind::Any })); + inputs.extend((1..count).map(|index| Port { + id: format!("{prefix}{index}"), + label: format!("Input image {index}"), + kind: PortKind::Any, + value_type: None, + })); } (inputs, definition.outputs.iter().map(catalog_port).collect()) diff --git a/crates/eval/src/tests.rs b/crates/eval/src/tests.rs index 66bcb1fb..9c624270 100644 --- a/crates/eval/src/tests.rs +++ b/crates/eval/src/tests.rs @@ -235,6 +235,45 @@ fn preview_cap_and_unknown_nodes() { assert!(c.nodes[&bad].error.is_some()); } +#[test] +fn typed_instance_ports_preserve_explicit_arguments_and_wire_fallbacks() { + use materialcraft_core::{FunctionGraph, ValueType}; + + let (mut parent, instance) = instancing("Caller", "Echo"); + let wire = parent.add_node("input_value", [0.0; 2]); + parent.node_mut(wire).unwrap().params.insert("default".into(), Value::Int(9)); + let mut child = Graph::new("Echo"); + let bridge = child.add_node("input_value", [0.0; 2]); + child.node_mut(bridge).unwrap().params.extend([("identifier".into(), Value::Text("count".into())), ("default".into(), Value::Int(5))]); + let output = child.add_node("output", [0.0; 2]); + child.connect(bridge, "output", output, "input").unwrap(); + + let mut package = Package::new("Typed arguments"); + package.graphs.extend([parent, child]); + let cache = Mutex::new(Cache::default()); + let mut options = Options::new(&no_res); + options.overrides.insert("control".into(), Value::Int(-12)); + for wired in [false, true] { + if wired { + package.graphs[0].connect(wire, "output", instance, "count").unwrap(); + } + for (literal, function, expected) in [(None, false, if wired { 9 } else { 5 }), (Some(0), false, 0), (Some(-3), false, -3), (None, true, -12)] { + let node = package.graphs[0].node_mut(instance).unwrap(); + node.params.remove("count"); + node.functions.remove("count"); + if let Some(value) = literal { + node.params.insert("count".into(), Value::Int(value)); + } + if function { + node.functions.insert("count".into(), FunctionGraph::input("control", ValueType::Integer)); + } + let cooked = cook(&package, 0, &options, &cache); + assert_eq!(cooked.nodes[&instance].error, None); + assert_eq!(cooked.nodes[&instance].outputs[0].as_value(), Some(&Value::Int(expected)), "wire {wired}, stored {literal:?}, function {function}"); + } + } +} + #[test] fn cached_instances_distinguish_fixed_and_bound_integer_arguments() { use materialcraft_core::{ExposedParameter, FunctionGraph, ValueType}; diff --git a/crates/nodes/src/parameter_descriptor.rs b/crates/nodes/src/parameter_descriptor.rs index ab00d836..ad29209c 100644 --- a/crates/nodes/src/parameter_descriptor.rs +++ b/crates/nodes/src/parameter_descriptor.rs @@ -57,7 +57,8 @@ impl ParameterDescriptor { Some(Value::Text(name)) => Some(name.as_str()), _ => None, }); - if let Some((_, graph)) = source.zip(name).and_then(|(source, name)| source.graph_by_name(name)) + let graph = source.zip(name).and_then(|(source, name)| source.graph_by_name(name)).map(|(_, graph)| graph); + if let Some(graph) = graph && let Some(input) = graph.exposed.iter().find(|input| input.id == parameter) { return Some(Self { @@ -71,7 +72,24 @@ impl ParameterDescriptor { }); } - let default = node.instance.as_ref()?.defaults.get(parameter)?.clone(); + let bridge = graph.and_then(|graph| { + graph.nodes.iter().find(|bridge| { + bridge.def == "input_value" + && match bridge.params.get("identifier") { + Some(Value::Text(name)) if !name.is_empty() => name == parameter, + _ => parameter == "input", + } + }) + }); + let default = if let Some(bridge) = bridge { + bridge.params.get("default").cloned().unwrap_or(Value::Float(0.0)) + } else { + let interface = node.instance.as_ref()?; + interface.defaults.get(parameter).cloned().or_else(|| { + let port = interface.inputs.iter().find(|port| port.identifier == parameter)?; + Some(ValueType::from_code(port.type_code)?.default_value()) + })? + }; return Some(Self { id: parameter.into(), label: parameter.into(), diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 73774372..4f0608b3 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -59,6 +59,24 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas ## 2026-11-23 root comparisons +### Stored and wired values at graph instance inputs + +Connect a typed graph input directly to an output. In the caller place an instance with an open input, an explicit argument, a wire from a Value Processor, or both an argument and a wire. The reference uses the declared default when the port is open. A wire supplies the value when the argument is absent. An explicit constant or parameter function supplies the value even with a wire present. MaterialCraft now resolves the argument using the bridge's declared type. The same-package and linked-package paths use the same port information. + +For an integer bridge with default 5, try an open port, stored 0, stored −3, wired 9, wired 9 with stored −3, and wired 9 with stored 9. The results in that order are 5, 0, −3, 9, −3 and 9. Run all six for same-file and linked instances, then save and reopen each caller in both applications. Agreement improved from 12/24 to **24/24 integer values**, maximum error 0. Repeat the linked cases with integer, Boolean and float2 types, testing both a constant argument and a parameter function on an open or wired port. For Boolean use default and wire true and argument false. For float2 use default (.125,.875), wire (2.5,−4.25) and argument (−.75,.375). These 18 original and 18 reopened outputs improved from 12/36 to **36/36 typed values**, maximum component error 0. + +In Points Preview, keep the same generated two-row ellipse strip, 32² canvas, point size .0625 and labels off. Connect point counts 1, 4 or 8. For each count test an open offset, constants 0 and 1, wires 0, 1 and 2, and a wire of 1 with constant 2. All **21,504/21,504** grayscale pixels match, maximum error 0. Repeat the [28 earlier root settings](#package-font-outlines-and-point-preview), including counts 4, 8 and 12 at offset 0, 1 and 2. All **25,088/25,088** pixels match the desktop captures in their stored formats. + +For a second whole-root comparison, use counts 4, 8 and 12 with the same offsets. Hide the first point for count 8 and the last for count 12. Use point sizes .03125, .0625 and .09375 and label sizes .0625, .09375 and .125 respectively. Test points only, labels only and both. Also test count zero with both enabled. For each view use 16² seed 0, 32² seed 42 and 32 × 16 seed 507031. Compare the one-worker F32 readback of the original instance and the instance referring to the complete saved spline package with its licensed fallback font. Errors below are normalized grayscale differences. + +| Visible elements | Original exact / total · max | Full save exact / total · max | Reference original versus full save · max | +|---|---:|---:|---:| +| Points | 5,376/5,376 · 0 | 5,376/5,376 · 0 | 5,376/5,376 · 0 | +| Labels | 5,088/5,376 · 0.513725515 | 5,087/5,376 · 0.494117673 | 5,105/5,376 · 0.380392157 | +| Both, including the empty input | 7,063/7,168 · 0.411764707 | 7,063/7,168 · 0.419607846 | 7,067/7,168 · 0.356862746 | + +The complete saved library reopens in the reference renderer. The reference catalog and the full save select different font files. Point-only output agrees across both paths. Visible labels also differ when both applications open the same saved font, leaving the root in progress. + ### Package font outlines and point preview The Text primitive reads the selected package font. Shaping and outline drawing use the shared font catalog and the CPU rasterizer. Editing the font bytes invalidates both the Text node and its parent graph instance. The release regression constructs a two-glyph TrueType face in memory, halves the cap height and checks the complete 64 × 32 raster on both cooks. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 76bebd81..0d1a1840 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -652,7 +652,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug | [spline_tools.sbs/internal_tools/get_spline_point_data](../assets/sbs/spline_tools.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [spline_tools.sbs/internal_tools/modify_spline_property_sub](../assets/sbs/spline_tools.sbs) | 4 | Yes | [Parity · 28,928 exact pixels in 19 renders](parity-fixes.md#clamped-texture-reads-in-spline-graphs) | | [spline_tools.sbs/internal_tools/normalize_spline_distance](../assets/sbs/spline_tools.sbs) | 1 | Yes | [Parity · 16 cases, 52,480 exact pixels](parity-fixes.md#tile-edge-masks-and-normalized-spline-distances) | -| [spline_tools.sbs/internal_tools/points_preview](../assets/sbs/spline_tools.sbs) | 3 | Yes | [In progress · linked fonts cook, four points exact over 4,864 pixels, eight and twelve points differ](parity-fixes.md#package-font-outlines-and-point-preview) | +| [spline_tools.sbs/internal_tools/points_preview](../assets/sbs/spline_tools.sbs) | 3 | Yes | [In progress · all 25,088 empty and point-only pixels exact, visible labels differ](parity-fixes.md#stored-and-wired-values-at-graph-instance-inputs) | | [spline_tools.sbs/internal_tools/resample_spline_data](../assets/sbs/spline_tools.sbs) | 3 | Yes | [Parity · 29,952 exact pixels in 19 renders](parity-fixes.md#clamped-texture-reads-in-spline-graphs) | | [spline_tools.sbs/internal_tools/scatter_splines_on_splines_preprocess](../assets/sbs/spline_tools.sbs) | 5 | Yes | [Parity · 622,592/622,592 pixels and 16/16 counts, both full-library saves exact](parity-fixes.md#spline-preprocessing-with-empty-inputs-and-complete-library-saves) · [Four output formats](parity-fixes.md#spline-preprocessing-with-wired-counts) | | [spline_tools.sbs/internal_tools/select_spline_sub](../assets/sbs/spline_tools.sbs) | 3 | Yes | [Parity · 622,592 exact pixels in 19 renders](parity-fixes.md#clamped-texture-reads-in-spline-graphs) |