From 284041433b45a4e4afc7cf8aa01090c8aa78df6f Mon Sep 17 00:00:00 2001 From: chasse20 Date: Wed, 18 Nov 2026 23:53:14 -0500 Subject: [PATCH] Preserve literal precision for values returned by functions --- crates/nodes/src/function/compile.rs | 1 + crates/nodes/src/function/tests.rs | 38 ++++++++++++++++++++++++++++ docs/parity-fixes.md | 15 +++++++++++ docs/sbs-root-nodes.md | 4 +-- 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/crates/nodes/src/function/compile.rs b/crates/nodes/src/function/compile.rs index d8efa31f..25c6fe25 100644 --- a/crates/nodes/src/function/compile.rs +++ b/crates/nodes/src/function/compile.rs @@ -210,6 +210,7 @@ fn mark_literals(program: &mut Program, mut arguments: BTreeSet<&str>) -> super: if let Some(call) = &mut op.call { let bound = call.inputs.iter().zip(&op.inputs).filter(|(_, index)| literal(index)).map(|(name, _)| name.as_str()).collect(); let lanes = mark_literals(&mut call.program, bound); + op.literal = call.program.output_is_literal(); // Returned products read the call's evaluated registers after its assignments have run. if lanes.iter().any(Option::is_some) { returned.insert(index, lanes); diff --git a/crates/nodes/src/function/tests.rs b/crates/nodes/src/function/tests.rs index 351c050f..4f1f425e 100644 --- a/crates/nodes/src/function/tests.rs +++ b/crates/nodes/src/function/tests.rs @@ -1173,6 +1173,44 @@ fn exposed_choices_are_integers_inside_functions() { assert!(evaluate(&graph, &BTreeMap::from([("choice".into(), Value::Enum(u32::MAX))])).is_err()); } +#[test] +fn a_constant_return_keeps_literal_precision_in_the_caller() { + let variables = BTreeMap::from([("angle".into(), Value::Float(std::f32::consts::PI))]); + let context = EvaluationContext { + scope: super::FunctionScope::PixelProcessor, + position: [0.5; 2], + size: [1.0; 2], + images: &[], + image_formats: &[], + tile: true, + variables: &variables, + }; + for (literal, expected) in [(true, 0x3c8efa35), (false, 0x3c8efa36)] { + let mut inner = FunctionGraph::default(); + inner.output = Some(if literal { + inner.add_constant(Value::Float(std::f32::consts::PI)) + } else { + inner.add("get_float1", &[], Some(Value::Text("angle".into()))) + }); + let mut graph = FunctionGraph::default(); + let result = graph.add("instance", &[], None); + let call = graph.nodes.last_mut().unwrap(); + call.instance = Some(materialcraft_core::GraphInstance { + label: String::new(), + package: String::new(), + graph: "angle_value".into(), + inputs: Vec::new(), + outputs: Vec::new(), + defaults: BTreeMap::new(), + }); + call.source = Some(Ok(std::sync::Arc::new(inner))); + let divisor = graph.add_constant(Value::Float(180.0)); + graph.output = Some(graph.add("div", &[result, divisor], None)); + let value = compile(&graph, 0).unwrap().evaluate_pixel(&context, &mut EvaluationState::default()).unwrap(); + assert_eq!(value, Value::Float(f32::from_bits(expected))); + } +} + #[test] fn nested_pixel_products_keep_the_callers_literal_arguments() { let mut inner = FunctionGraph::default(); diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 92ceece1..e68b601f 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -845,6 +845,21 @@ Both roots generate their own coordinates. Alongside the four archive renders an In both roots, at 32²/157 and 64²/317, the three-component directions for the texel and first point, their dot product and its quotient each match all 5,120 pixels when exported. Both constant linear RGB endpoints of the linear gradient also match. The next function adds one to the quotient and divides by two. Its unclamped output matches only 652/1,024 and 2,496/4,096 F32 pixels, maximum 1.19209290e−7 in both cases. Let D be the stored dot product and R the F32 reciprocal of 1−.3125, bits 3fba2e8c. Rounding D×R+1 once, then multiplying by .5, reconstructs both the unclamped and clamped linear weights at all 5,120 pixels. At (1,0) of the 32² case D has bits 3f01187e and the stored quotient is 3f3bc689. Separate rounding gives 3f5de344 for the weight and the reference gives 3f5de345. The same arithmetic followed by clamping and subtracting from one reconstructs the first two-point weight at all 5,120 pixels. The two-point weighted color sum still differs at 238/1,024 and 384/4,096 pixels in the measured implementation. +### Constants returned from pixel functions + +| Comparison | Renders | Exact pixels / compared | Maximum error | +|---|---:|---:|---:| +| Second color-selection root | 23 | 179,456/179,456 | 0 | +| Nine intermediate rotation values at 32² | 9 | 9,216/9,216 | 0 | +| Returned and inlined arithmetic with pure results | 84 | 247,296/247,296 | 0 | +| Literal and sampled division controls | 252 | 741,888/741,888 | 0 | + +The second color-selection root matches all 123,904 U8, 9,216 U16, 9,216 F16 and 37,120 F32 pixels in the [selection matrix](#scalar-arithmetic-across-a-call-argument). The 26 outstanding pixels used the perceptual distance. Its rotation helper multiplies the measured exponential by 60, then by π/180. π is returned by a zero-argument function. Dividing that constant by 180 gives bits 3c8efa35. The same numeric numerator read from a runtime variable gives 3c8efa36. Recognizing a pure constant result at the call boundary brings the radians from 17/1,024 to 1,024/1,024 and the complete perceptual distance from 998/1,024 to 1,024/1,024. The other intermediate comparisons are described under [shared scalar products](#two-sums-sharing-the-same-scalar-products). + +For the 84 independent renders, use the [signed field, six sizes/formats and seeds](#vector-products-in-the-direct-light-pass). Return a scalar V and store (R×(V/180), V/180, sin(V), sqrt(V)). Use π, 1.125×2.375+.4375, an explicitly passed π, an explicitly passed abs(G)+.625, π from a function with an unused varying argument, π through a second zero-argument call and π forwarded through a second single-argument call. Repeat each expression inline. Before the change the same graphs matched 194,555/247,296 pixels. Both the called and inlined varying case still match all 35,328 pixels. + +Two additional controls return an assignment of π or sequence an assignment of the varying argument before returning π. Inlined and called versions together match 32,768/70,656 in 24 renders, maximum 5.96046448e−8 in F16 and 8.74227766e−8 in F32. All U8 and U16 pixels match. These results were the same on both measured revisions. + ### Two sums sharing the same scalar products | Comparison | Renders | Exact pixels / compared | Maximum error | diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 8be32ff3..8514301e 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -35,7 +35,7 @@ A graph may accept input pins and still be a root under the dependency criterion Statuses: **Not checked**, **In progress**, **Parity** (exact pixels or typed values for the linked measured cases), or **Too hard** (reason recorded). Function calls do not count as regular graph dependencies. -The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **313 Parity**, **52 In progress** and **181 Too hard**. The November 17 and 18 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) +The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **314 Parity**, **51 In progress** and **181 Too hard**. The November 17 and 18 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) [Grayscale sampler measurements](parity-fixes.md#grayscale-sampling-of-color-inputs) · [Value sampling across 240 typed comparisons](parity-fixes.md#value-processor-texture-filtering) @@ -141,7 +141,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [color_space_conversion.sbs/srgb_to_linear_srgb_grayscale](../assets/sbs/color_space_conversion.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [color_temperature_adjustment.sbs/color_temperature_adjustment](../assets/sbs/color_temperature_adjustment.sbs) | 1 | No | [Parity · 18 cases, 171,264 exact RGBA pixels](parity-fixes.md#color-temperature-range-reductions-sky-parameters-and-polygon-paths) | | [color_to_mask.sbs/color_to_mask](../assets/sbs/color_to_mask.sbs) | 1 | Yes | [Parity · 18 cases, 168,192 exact pixels](parity-fixes.md#levels-endpoints-camera-tangents-and-uniform-exponentials) | -| [color_to_mask_v2.sbs/color_to_mask](../assets/sbs/color_to_mask_v2.sbs) | 2 | No | [In progress · 179,430/179,456 pixels, 26 F32 perceptual-distance pixels differ](parity-fixes.md#two-sums-sharing-the-same-scalar-products) | +| [color_to_mask_v2.sbs/color_to_mask](../assets/sbs/color_to_mask_v2.sbs) | 2 | No | [Parity · 23 renders, all 179,456 grayscale pixels exact](parity-fixes.md#constants-returned-from-pixel-functions) | | [composite.sbs/composite/background/uniform_color_float3](../assets/sbs/composite.sbs) | 0 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [composite.sbs/composite/blends/blend_content](../assets/sbs/composite.sbs) | 3 | Yes | [Parity · 171,264 exact pixels in 18 renders](parity-fixes.md#segment-mapping-and-normal-graph-rechecks) | | [composite.sbs/composite/blends/blend_content_grayscale](../assets/sbs/composite.sbs) | 3 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |