Preserve image socket selection when cooking and saving patterns

This commit is contained in:
chasse20 committed 2026-11-19 07:34:05 -05:00
1 parent dc03ad9aac
commit 67da2d0b8b
6 files changed
+78 -9

No files matched your search

+8 -4
View File
@@ -133,6 +133,9 @@ fn fx_map_starts_with_a_quadrant_and_grows_image_inputs_as_connected() {
let source = add(&mut session, "uniform");
execute(&mut session, "graph.node.param", &json!({"id": source.0, "param": "color", "value": [0.5, 0.5, 0.5, 1.0]})).unwrap();
execute(&mut session, "graph.node.param", &json!({"id": source.0, "param": "color_mode", "value": false})).unwrap();
let sixth = add(&mut session, "uniform");
execute(&mut session, "graph.node.param", &json!({"id": sixth.0, "param": "color", "value": [0.75, 0.75, 0.75, 1.0]})).unwrap();
execute(&mut session, "graph.node.param", &json!({"id": sixth.0, "param": "color_mode", "value": false})).unwrap();
let owner = add(&mut session, "fx_map");
let Value::PatternGraph(pattern) = &session.graph().unwrap().node(owner).unwrap().params["pattern"] else { panic!("missing default pattern") };
@@ -154,7 +157,8 @@ fn fx_map_starts_with_a_quadrant_and_grows_image_inputs_as_connected() {
assert_eq!(loaded_pattern.root, Some(loaded_pattern.nodes[0].id));
for index in 0..6 {
execute(&mut session, "graph.connect", &json!({"from": source.0, "to": owner.0, "toPort": format!("pattern{index}")})).unwrap();
let input = if index == 5 { sixth } else { source };
execute(&mut session, "graph.connect", &json!({"from": input.0, "to": owner.0, "toPort": format!("pattern{index}")})).unwrap();
assert_eq!(session.input_ports(owner).last(), Some(&format!("pattern{}", index + 1)));
}
@@ -163,12 +167,12 @@ fn fx_map_starts_with_a_quadrant_and_grows_image_inputs_as_connected() {
let mut pattern = (**pattern).clone();
pattern.nodes[0].parameters.insert("patterntype".into(), Value::Int(1));
pattern.nodes[0].parameters.insert("imageindex".into(), Value::Int(6));
pattern.nodes[0].parameters.insert("imageindex".into(), Value::Int(5));
execute(&mut session, "graph.pattern.set", &json!({"id": owner.0, "pattern": pattern})).unwrap();
session.cook();
assert!(session.cooked.nodes[&owner].error.is_none(), "{:?}", session.cooked.nodes[&owner].error);
assert!((session.cooked.image(owner, 0).unwrap().mean()[0] - 0.5).abs() < 1e-5);
assert!((session.cooked.image(owner, 0).unwrap().mean()[0] - 0.75).abs() < 1e-5);
let bytes = write_sbs(&session.pkg).unwrap();
@@ -186,7 +190,7 @@ fn fx_map_starts_with_a_quadrant_and_grows_image_inputs_as_connected() {
loaded.cook();
assert!((loaded.cooked.image(loaded_owner, 0).unwrap().mean()[0] - 0.5).abs() < 1e-5);
assert!((loaded.cooked.image(loaded_owner, 0).unwrap().mean()[0] - 0.75).abs() < 1e-5);
execute(&mut session, "graph.disconnect", &json!({"to": owner.0, "toPort": "pattern5"})).unwrap();
+1 -1
View File
@@ -19,7 +19,7 @@ pub fn definitions() -> &'static [NodeDefinition] {
.int("blendingmode", "Blending", 0, 0, 2)
.int("imagefiltering", "Image filtering", 1, 0, 1)
.boolean("imagepremul", "Premultiplied image", false)
.int("imageindex", "Image input", 1, 1, crate::MAX_IMAGE_INPUTS as i32)
.int("imageindex", "Image input", 0, 0, crate::MAX_IMAGE_INPUTS as i32 - 1)
.int("randomseed", "Random seed", 0, 0, 10000)
.boolean("randominherited", "Inherit random seed", true)
.cook(crate::node_definition_builder::no_cook),
+9 -2
View File
@@ -103,8 +103,15 @@ pub(crate) fn cook(context: &CookContext) -> CookResult {
PatternNodeKind::Quadrant => {
let offset = vector("branchoffset");
visit.center = [visit.center[0] + offset[0], visit.center[1] + offset[1]];
let pattern = integer("patterntype");
let pattern = if pattern == 1 { integer("imageindex").max(1).saturating_add(255) } else { pattern };
let mut pattern = integer("patterntype");
if pattern == 1 || (256..256 + crate::MAX_IMAGE_INPUTS as i32).contains(&pattern) {
// Zero uses the slot encoded in the pattern. The generic image pattern starts at slot zero.
pattern = match integer("imageindex") {
0 => pattern.max(256),
index if index > 0 => index.saturating_add(256),
_ => 0,
};
}
if pattern != 0 {
let offset = vector("frameoffset");
let color = match parameters.get("opacity") {
+41
View File
@@ -445,6 +445,47 @@ fn patterns_reject_cycles_and_bound_exponential_traversal() {
assert!(cook(graph).unwrap_err().contains("budget"));
}
#[test]
fn image_selectors_use_zero_based_slots_and_override_packed_patterns() {
use craft_texture::{Image, Kind};
let inputs = [None, Some(Image::filled(4, 4, Kind::Gray, [0.25; 4])), Some(Image::filled(4, 4, Kind::Gray, [0.75; 4]))];
let definition = find("fx_map").unwrap();
for (pattern, slot, expected) in [
(1, None, 0.25),
(1, Some(0), 0.25),
(1, Some(1), 0.75),
(1, Some(2), 0.0),
(256, Some(1), 0.75),
(257, None, 0.75),
(258, Some(1), 0.75),
(257, Some(-1), 0.0),
] {
let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant);
let node = &mut graph.nodes[0];
node.parameters.insert("patterntype".into(), Value::Int(pattern));
if let Some(slot) = slot {
node.parameters.insert("imageindex".into(), Value::Int(slot));
}
let parameters =
resolve_params(definition, &BTreeMap::from([("pattern".into(), Value::PatternGraph(Box::new(graph))), ("color_mode".into(), Value::Bool(false))]));
let context = CookContext {
input_formats: &[],
format: materialcraft_core::OutputFormat::F32,
w: 4,
h: 4,
seed: 0,
tile: false,
inputs: &inputs,
params: &parameters,
resource: &|_| None,
vars: &BTreeMap::new(),
};
let image = (definition.cook)(&context).unwrap().remove(0);
assert_eq!(image.get(2, 2, false)[0], expected, "pattern {pattern}, slot {slot:?}");
}
}
#[test]
fn image_indices_outside_the_supported_range_return_an_error() {
let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant);
+17
View File
@@ -1806,6 +1806,23 @@ These measurements follow the [uniform arc-tangent correction](#uniform-arc-tang
The remaining hard edge on Pattern Stripes is (23,59) at 128². MaterialCraft returns zero and the reference returns one at that pixel in each of the four output formats. Standalone Stripes matches every hard-edge control and retains 28 differences in the archived U16 renders. Both Dirt 4 versions reach the pattern traversal budget before producing an image in all nine cases. The additional [wired irradiance controls](#environment-rotation-and-wired-irradiance-samples) include nonfinite stamp parameters and remain open.
### Image selection in saved pattern graphs
When opening Arc Pavement, the first image socket is `pattern0`. An omitted `imageindex` selects that socket. MaterialCraft previously wrote an explicit 1 when saving such a graph. The reference app then sampled an empty second socket. MaterialCraft now defaults the index to zero, and its numeric image selector and SBS writer use the same slot numbers. For automation that sets `imageindex` explicitly, use 5 for the sixth connected image. A nonzero positive selector also overrides the slot embedded in pattern codes 256 through 511. A negative selector draws nothing in the measured controls.
Use one full-frame grayscale image stamp. With one 64² pin, bind a constant byte 173 or a checker alternating bytes 0 and 255. Test a white square, generic image type 1 with selectors 0, 1, 2 and omitted, packed types 256 and 257, and a graph-controlled image type with selectors 0 or 1. All 73,728/73,728 F32 pixels match over 18 renders. With two inputs, bind the same constant to the first slot and the checker to the second. Test omitted, negative, zero, one and two, three packed forms, then a variable selector reading 0, 1 and 2. For each configuration compare 16² and 32² F32 and 64² U8, U16, F16 and F32, seed 859+13j+i.
| How the slot was specified | Renders | Exact pixels / compared | Maximum error |
|---|---:|---:|---|
| Two pins, fixed selections and runtime slots zero or one | 60 | 176,640/176,640 | 0 |
| Two pins, runtime slot two | 6 | 8,832/17,664 | 255 U8, 65,535 U16, 1 F16/F32 |
| Two pins, additional packed selections at 16² F32 | 12 | 3,072/3,072 | 0 |
| Four distinct constant pins at 16² F32 | 30 | 5,632/7,680 | 2.98023224e−8 |
For the four-pin case use byte levels 31, 79, 151 and 229. At seed 885, cross pattern types 1, 256, 257, 258 and 259 with indices 0, 1, 2, 3, 4 and −1. Rounding both float outputs back to bytes gives 7,680/7,680 identical socket selections. The remaining raw float differences are in the sampled value. With two pins a runtime request for slot two samples the last pin in the reference app, while MaterialCraft produces black. This explains all differences in the 66 two-pin renders.
For saved-file fidelity, start at format version `1.1.0.202502`, save and reopen through MaterialCraft's ordinary `file.saveAs`, then open the resulting file directly in the reference app and compare it with the original in that same app. Arc Pavement matches all 112,896/112,896 pixels across the six [original handoff cases](#integer-defaults-in-saved-graph-interfaces) and all 18 [expanded root cases](#uniform-arc-tangents-and-pavement-rotations), maximum error zero. This includes the three 64² F32 image inputs that previously returned black and the independent 32² U16 input. Spot Emboss again matches all 33,792/33,792 pixels across six cases. The two files saved with their original `0.1.3` header still have the documented load failure. The separate MaterialCraft cook comparison for Arc Pavement remains at 202,429/211,200 matching pixels.
### Integer defaults in saved graph interfaces
Two packages with a `0.1.3` format header failed to reopen in the reference app after MaterialCraft Save As. Changing only that field to `1.1.0.202502` in a temporary copy let the reference app open both packages. Spot Emboss then reproduced all six original renders. Arc Pavement reproduced its default render, but six scalar integer settings had no readable default on the saved graph. The [earlier round trips](#vector-equality-lab-to-rgb-and-sbs-round-trips) cover a separate function library and a package that failed to load.
+2 -2
View File
@@ -405,7 +405,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
| [paths_tools.sbs/vertex_processor](../assets/sbs/paths_tools.sbs) | 9 | No | [Parity · 18 renders, all 57,600 pixels exact](parity-fixes.md#marked-variable-reads-in-path-and-mipmap-graphs) |
| [paths_tools.sbs/vertex_processor_simple](../assets/sbs/paths_tools.sbs) | 5 | No | [Parity · 18 renders, all 57,600 pixels exact](parity-fixes.md#marked-variable-reads-in-path-and-mipmap-graphs) |
| [pattern_alveolus.sbs/alveolus](../assets/sbs/pattern_alveolus.sbs) | 0 | No | [In progress · 170,440/171,264 pixels across 18 renders](parity-fixes.md#radial-stamp-profiles-and-pattern-generators) |
| [pattern_arc_pavement.sbs/arc_pavement](../assets/sbs/pattern_arc_pavement.sbs) | 1 | No | [In progress · 132,096 baseline pixels exact, 202,429/211,200 across all 27 renders](parity-fixes.md#uniform-arc-tangents-and-pavement-rotations) |
| [pattern_arc_pavement.sbs/arc_pavement](../assets/sbs/pattern_arc_pavement.sbs) | 1 | No | [In progress · 202,429/211,200 cook pixels and 24 exact saved-file renders](parity-fixes.md#image-selection-in-saved-pattern-graphs) |
| [pattern_brick_1.sbs/brick_1](../assets/sbs/pattern_brick_1.sbs) | 0 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [pattern_brick_2.sbs/brick_2](../assets/sbs/pattern_brick_2.sbs) | 0 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [pattern_brick_generator.sbs/resources/Tile](../assets/sbs/pattern_brick_generator.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
@@ -559,7 +559,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
| [spline_tools.sbs/internal_tools/scatter_splines_on_splines_preprocess](../assets/sbs/spline_tools.sbs) | 5 | Yes | [In progress · nonzero counts and images exact, zero count timed out](parity-fixes.md#spline-preprocessing-with-wired-counts) |
| [spline_tools.sbs/internal_tools/select_spline_sub](../assets/sbs/spline_tools.sbs) | 3 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [spline_tools.sbs/internal_tools/spline_warp_sub](../assets/sbs/spline_tools.sbs) | 6 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [spot_emboss.sbs/spot_emboss](../assets/sbs/spot_emboss.sbs) | 2 | No | [Parity · 218,368 exact cook pixels and six exact saved-file renders](parity-fixes.md#integer-defaults-in-saved-graph-interfaces) |
| [spot_emboss.sbs/spot_emboss](../assets/sbs/spot_emboss.sbs) | 2 | No | [Parity · 218,368 exact cook pixels and six exact saved-file renders](parity-fixes.md#image-selection-in-saved-pattern-graphs) |
| [starburst.sbs/starburst](../assets/sbs/starburst.sbs) | 0 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [stripes.sbs/stripes](../assets/sbs/stripes.sbs) | 0 | Yes | [In progress · 215,012/215,040 exact pixels in 18 renders](parity-fixes.md#other-roots-using-uniform-arc-tangents) |
| [summed_area_table.sbs/summed_area_table_iteration](../assets/sbs/summed_area_table.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |