diff --git a/crates/eval/src/graph_instance.rs b/crates/eval/src/graph_instance.rs index 254b651a..0c7f7fad 100644 --- a/crates/eval/src/graph_instance.rs +++ b/crates/eval/src/graph_instance.rs @@ -88,7 +88,7 @@ pub(crate) fn cook_instance( source_options.overrides = params.iter().filter(|(k, _)| source_graph.exposed.iter().any(|e| &e.id == *k)).map(|(k, v)| (k.clone(), v.clone())).collect(); for exposed in &source_graph.exposed { let name = &exposed.id; - if fixed.contains(name) || (!params.contains_key(name) && matches!(source_graph.input_value(name), Some(Value::Int(_)))) { + if fixed.contains(name) || (!params.contains_key(name) && matches!(source_graph.input_value(name), Some(Value::Int(_) | Value::Float(_)))) { source_options.literal_inputs.push(name.clone()); } } diff --git a/crates/eval/src/parameter.rs b/crates/eval/src/parameter.rs index 61deec27..2a9fba47 100644 --- a/crates/eval/src/parameter.rs +++ b/crates/eval/src/parameter.rs @@ -123,7 +123,11 @@ pub(crate) fn resolve_parameters( } let fixed = if node.def == "graph_instance" { - parameters.iter().filter(|(name, value)| matches!(value, Value::Int(_)) && !dynamic.contains(*name)).map(|(name, _)| name.clone()).collect() + parameters + .iter() + .filter(|(name, value)| matches!(value, Value::Int(_) | Value::Float(_)) && !dynamic.contains(*name)) + .map(|(name, _)| name.clone()) + .collect() } else { Vec::new() }; diff --git a/crates/eval/src/tests.rs b/crates/eval/src/tests.rs index 615f4d6e..f3809ee5 100644 --- a/crates/eval/src/tests.rs +++ b/crates/eval/src/tests.rs @@ -278,6 +278,73 @@ fn cached_instances_distinguish_fixed_and_bound_integer_arguments() { } } +#[test] +fn instanced_float_angles_keep_their_precision_across_an_unrelated_write() { + use materialcraft_core::{ExposedParameter, FunctionGraph, GraphInstance, GraphPort, OutputFormat}; + let (parent, instance) = instancing("Parent", "Angle"); + let mut child = Graph::new("Angle"); + child.exposed.push(ExposedParameter { + id: "turn".into(), + label: "Turn".into(), + default: Value::Float(1.0), + connectable: false, + clamp: false, + description: String::new(), + group: String::new(), + editor: None, + step: None, + min: -2.0, + max: 2.0, + options: Vec::new(), + }); + let mut body = FunctionGraph::default(); + let one = body.add_constant(Value::Float(1.0)); + body.output = Some(body.add("set", &[one], Some(Value::Text("scratch".into())))); + let mut expression = FunctionGraph::default(); + let write = expression.add("instance", &[], None); + let call = expression.nodes.last_mut().unwrap(); + call.instance = Some(GraphInstance { + label: String::new(), + graph: "Remember".into(), + package: String::new(), + inputs: Vec::new(), + outputs: vec![GraphPort { identifier: "output".into(), type_code: 256 }], + defaults: Default::default(), + }); + call.source = Some(Ok(std::sync::Arc::new(body))); + let turn = expression.add("get_float1", &[], Some(Value::Text("turn".into()))); + let tau = expression.add_constant(Value::Float(std::f32::consts::TAU)); + let angle = expression.add("mul", &[turn, tau], None); + let sine = expression.add("sin", &[angle], None); + expression.output = Some(expression.add("sequence", &[write, sine], None)); + let processor = child.add_node("pixel_processor", [0.0; 2]); + let pixel = child.node_mut(processor).unwrap(); + pixel.base.format = OutputFormat::F32; + pixel.params.insert("function".into(), Value::Function(Box::new(expression))); + let output = child.add_node("output", [0.0; 2]); + child.connect(processor, "output", output, "input").unwrap(); + let mut package = Package::new("Fixed and bound turns"); + package.graphs.extend([parent, child]); + let mut options = Options::new(&no_res); + options.overrides.insert("driver".into(), Value::Float(1.0)); + let cache = Mutex::new(Cache::default()); + for (explicit, bound) in [(false, false), (true, false), (true, true), (true, false)] { + let node = package.graphs[0].node_mut(instance).unwrap(); + if explicit { + node.params.insert("turn".into(), Value::Float(1.0)); + } + if bound { + node.bindings.insert("turn".into(), "driver".into()); + } else { + node.bindings.remove("turn"); + } + let result = cook(&package, 0, &options, &cache); + assert_eq!(result.nodes[&instance].error, None); + let red = result.image(instance, 0).unwrap().get(0, 0, false)[0]; + assert_eq!(red.to_bits(), if bound { (-0.0_f32).to_bits() } else { 0x343b_bd2e }, "explicit {explicit}, bound {bound}"); + } +} + #[test] fn graph_instances_cook_their_subgraph() { let (mut p, ..) = sample(); diff --git a/crates/nodes/src/function/compile.rs b/crates/nodes/src/function/compile.rs index b0c88ff9..781d26cf 100644 --- a/crates/nodes/src/function/compile.rs +++ b/crates/nodes/src/function/compile.rs @@ -247,9 +247,14 @@ fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mu /// A write can replace an argument before its next read. pub(super) fn remove_written_arguments(program: &Program, arguments: &mut BTreeMap<&str, T>) { for op in &program.instructions { - if op.call.as_ref().is_some_and(|call| call.exports) { - arguments.clear(); - return; + if arguments.is_empty() { + break; + } + if let Some(call) = &op.call + && call.exports + { + // A callee's unrelated assignments leave the caller's known operands available. + remove_written_arguments(&call.program, arguments); } if op.definition.id == "set" && let Some(materialcraft_core::Value::Text(name)) = &op.value diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 42ced6a8..024faa53 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -59,6 +59,26 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas ## 2026-11-23 root comparisons +### Scalar float precision in nested graph instances + +A scalar float supplied by an instance default, an explicit constant or a constant expression retains literal precision inside the child graph. A value driven by an exposed parent parameter retains runtime precision. In an original Pixel Processor, take the sine of one full turn using the F32 constant 6.2831855. The fixed result has F32 bits `343bbd2e`. With the same turn supplied by an exposed parent input it has bits `80000000`. A called function writing a different variable preserves this distinction. MaterialCraft now tracks fixed scalar floats at graph boundaries and invalidates known operands by the names a called function writes. + +Use the signed procedural RGBA fields from the [pixel interpolation comparison](#packed-products-in-the-second-endpoint-of-a-pixel-interpolation). Use field indices 0, 1, 2, 0, 0 and 1 with seeds 435071 + 19j for j from 0 through 5. Read the red channel with nearest sampling. Set the exposed turns to 1, .1875, −.38157, .723561, 0 and .375. Output sine, cosine, the F32 angle in radians and the sampled red channel. Compare top-level reads, literal 1 and .375, nested defaults, explicit values, a .375 + .625 expression, a parent read and a parent-driven selection between 1 and .375. Repeat the phase calculation in a called function and after a call that writes a sampled value to an unrelated variable. + +| Comparison | Renders | Exact pixels / total | Maximum component error | +|---|---:|---:|---:| +| Fifteen F32 graphs, before | 90 | 129,024/215,040 | 1.74845553e−7 | +| Same F32 graphs, corrected | 90 | 215,040/215,040 | 0 | +| Six controls, U8, U16 and F16 at the first three settings | 54 | 49,152/49,152 per format | 0 | +| Eight MaterialCraft saves opened in both applications, F32 | 48 | 114,688/114,688 | 0 | +| Earlier returned-product, shared-product and quotient controls | 307 | 1,288,192/1,288,192 | 0 | + +The six format controls use top-level literal and exposed turns, a child default, an explicit child value, a parent read after an unrelated write, and the constant expression. The saves include two top-level graphs and six parent/child pairs with relative dependencies. Each saved reference image reproduced its original. The regression alternates default, explicit and parent-driven inputs on a shared cache. It failed on the preceding evaluator. All 470 evaluation and node release tests passed, along with formatting, strict workspace Clippy, dependency layers and WebAssembly. + +For the first renderer's eighth irradiance pass, repeat the [nine connected-index settings](#irradiance-with-a-connected-pass-index) with its existing SBS graph. All 4,608/4,608 F16 pixels and 9/9 float indices match, maximum zero. With the [four saved intermediate readouts](#whole-specular-outputs-and-wired-irradiance-stages), each processor, the first Switch and the final image match 2,560/2,560 F16 pixels. The four final images and index values reproduce the original reference outputs. At row 14 in the two 32² controls, the three rotation columns, world and rotated hits, sampling coordinates, samples and lighting terms match 896/896 F32 pixels over 14 readouts. The first two reference columns contain −1.74845553e−7 and +1.74845553e−7 off the diagonal. + +Separate probes also assign to the same name as the exposed angle. At turns 1, .1875 and 0, the top-level write, a parent-driven argument shadowed in a call, and an unrelated write through two calls match 18,432/18,432 F32 pixels. Four other read/write combinations in child graphs still differ at all 24,576 pixels. Writing the exposed name before another call reads it has maximum error 18.8408465 across the output channels. Shadowing a fixed input or reusing a getter evaluated before a write has maximum error 1.74845553e−7. These are independent controls for assignment order. + ### Rectangular image inputs on graph instances When a connected image crosses a graph boundary, axes more than one doubling or halving from their target move first. The final sampling pass adjusts both axes to the requested dimensions. Each pass stores its result at the input bridge's precision. A 32² F32 image feeding a 16 × 8 F16 bridge therefore goes through 32 × 16. MaterialCraft previously used 16² as the intermediate. The connected bridge now shares the atomic input filters' choice of intermediate dimensions. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 69e38777..cce4b874 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -41,7 +41,7 @@ Panorama Plane Light matches 212,736/212,736 pixels across 32 original settings The eighth specular pass in the second package (root 408) matches all 143,300 original pixels at 38 settings, all 16,704 pixels after reopening and all 81,920 intermediate F32 pixels in the environment and material controls. [Whole-root measurements](parity-fixes.md#whole-and-reopened-eighth-specular-passes) -In the first package, all 17 default-index irradiance settings match 120,320/120,320 pixels, with 4,603/4,608 exact across nine connected-index settings after correcting the rectangular environment bridge. All 26 next-index values match. For the first eighth specular pass, parameter-driven Switch Blend now reproduces the three differing green zero signs at 256² in a coordinate probe. [Rectangular image input and connected-index measurements](parity-fixes.md#rectangular-image-inputs-on-graph-instances) · [Default-index measurements](parity-fixes.md#eighth-irradiance-pass-with-default-and-connected-indices) · [Switch Blend controls and saved files](parity-fixes.md#switch-blend-with-parameter-driven-endpoint-opacity) +In the first package, all 17 default-index irradiance settings match 120,320/120,320 pixels, with 4,608/4,608 exact across nine connected-index settings after correcting input resizing and scalar angle precision. All 26 next-index values match. For the first eighth specular pass, parameter-driven Switch Blend now reproduces the three differing green zero signs at 256² in a coordinate probe. [Wired irradiance and rotation readouts](parity-fixes.md#scalar-float-precision-in-nested-graph-instances) · [Rectangular image input and connected-index measurements](parity-fixes.md#rectangular-image-inputs-on-graph-instances) · [Default-index measurements](parity-fixes.md#eighth-irradiance-pass-with-default-and-connected-indices) · [Switch Blend controls and saved files](parity-fixes.md#switch-blend-with-parameter-driven-endpoint-opacity) Eight original called-function graphs now match all 49,152 F32 pixels when a step is copied before a later assignment. Four saves reopened in both applications and matched another 24,576 pixels. The sampled-alpha assignment case remains open. [Copied steps and retained arithmetic comparisons](parity-fixes.md#copied-steps-in-called-pixel-sums) @@ -536,7 +536,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug | [pbr_render.sbs/ibl_specular_contrib_anisotropic/panoramic_lod](../assets/sbs/pbr_render.sbs) | 1 | Yes | [Parity · 18 cases, 186,624 exact pixels](parity-fixes.md#stored-precision-in-transform-mip-levels) | | [pbr_render.sbs/position_to_normal](../assets/sbs/pbr_render.sbs) | 2 | Yes | [Parity · 30 renders, all 515,328 RGBA pixels exact](parity-fixes.md#position-to-normal-outputs-in-both-renderers) | | [pbr_render.sbs/raytrace_position](../assets/sbs/pbr_render.sbs) | 4 | Yes | [Parity · 26 renders, all 142,340 RGBA pixels exact](parity-fixes.md#small-graph-parents-and-raytrace-positions) | -| [pbr_render.sbs/raytraced_irradiance_pass_8](../assets/sbs/pbr_render.sbs) | 10 | Yes | [In progress · 120,320/120,320 default-index pixels through 256², 4,603/4,608 wired, 26/26 pass indices](parity-fixes.md#rectangular-image-inputs-on-graph-instances) · [Default-index matrix](parity-fixes.md#eighth-irradiance-pass-with-default-and-connected-indices) · [Wired processor readouts](parity-fixes.md#whole-specular-outputs-and-wired-irradiance-stages) · [Stored product component and assignment-order controls](parity-fixes.md#component-reads-and-writes-across-pixel-function-calls) · [Earlier 26-setting matrix](parity-fixes.md#irradiance-with-a-connected-pass-index) · [First processor, 4,096/4,096 in both F16 and F32 across called ray steps](parity-fixes.md#assigned-ray-steps-inside-pixel-function-calls) · [Earlier first-processor comparison](parity-fixes.md#stored-products-in-pixel-expressions-and-irradiance-ray-steps) | +| [pbr_render.sbs/raytraced_irradiance_pass_8](../assets/sbs/pbr_render.sbs) | 10 | Yes | [In progress · 120,320/120,320 default-index pixels through 256², 4,608/4,608 wired, 26/26 pass indices](parity-fixes.md#scalar-float-precision-in-nested-graph-instances) · [Rectangular input measurements](parity-fixes.md#rectangular-image-inputs-on-graph-instances) · [Default-index matrix](parity-fixes.md#eighth-irradiance-pass-with-default-and-connected-indices) · [Wired processor readouts](parity-fixes.md#whole-specular-outputs-and-wired-irradiance-stages) · [Stored product component and assignment-order controls](parity-fixes.md#component-reads-and-writes-across-pixel-function-calls) · [Earlier 26-setting matrix](parity-fixes.md#irradiance-with-a-connected-pass-index) · [First processor, 4,096/4,096 in both F16 and F32 across called ray steps](parity-fixes.md#assigned-ray-steps-inside-pixel-function-calls) · [Earlier first-processor comparison](parity-fixes.md#stored-products-in-pixel-expressions-and-irradiance-ray-steps) | | [pbr_render.sbs/raytraced_specular_pass_8](../assets/sbs/pbr_render.sbs) | 9 | Yes | [In progress · corrected Switch matches 21/21 selected 256² pixels and 42/42 black-specular pixels](parity-fixes.md#switch-blend-with-parameter-driven-endpoint-opacity) · [Earlier whole outputs, 143,296/143,300 original and rectangular, 16,703/16,704 reopened](parity-fixes.md#whole-specular-outputs-and-wired-irradiance-stages) · [Switch precision measurements](parity-fixes.md#signed-zero-when-switch-blend-selects-a-floating-point-color-input) · [Earlier F16 comparisons](parity-fixes.md#eighth-raytraced-specular-passes) · [rectangular fields and saved packages](parity-fixes.md#rectangular-inputs-and-remaining-half-float-pixels-in-the-eighth-specular-passes) · [Environment controls and F32 boundary values](parity-fixes.md#environment-and-intermediate-precision-in-the-eighth-specular-passes) · [Reciprocal rounding and 128² recheck](parity-fixes.md#reciprocal-division-through-pixel-function-calls) | | [pbr_render2.sbs/PBR_render_mapping_color](../assets/sbs/pbr_render2.sbs) | 2 | No | [Parity · 13 cases, 163,840 exact pixels](parity-fixes.md#height-distance-texture-mapping-skew-and-weave) | | [pbr_render2.sbs/PBR_render_mapping_grayscale](../assets/sbs/pbr_render2.sbs) | 2 | No | [Parity · 13 cases, 163,840 exact pixels](parity-fixes.md#height-distance-texture-mapping-skew-and-weave) |