Respect typed arguments on graph instance input bridges

This commit is contained in:
chasse20 committed 2026-11-23 07:42:21 -05:00
1 parent 3ac2f1b61f
commit 4e445b1722
8 files changed
+124 -51

No files matched your search

+12
View File
@@ -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());
+1 -9
View File
@@ -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(_) => {
+3
View File
@@ -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<ValueType>,
}
+30 -39
View File
@@ -14,53 +14,38 @@ pub fn ports_with_connections<'a>(
incoming: impl IntoIterator<Item = &'a materialcraft_core::Connection>,
) -> (Vec<Port>, Vec<Port>) {
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<Port> {
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<Port> = 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())
+39
View File
@@ -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};
+20 -2
View File
@@ -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(),
+18
View File
@@ -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.
+1 -1
View File
@@ -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) |