diff --git a/crates/nodes/src/function/arithmetic.rs b/crates/nodes/src/function/arithmetic.rs index 5b0fff3..458b7a5 100644 --- a/crates/nodes/src/function/arithmetic.rs +++ b/crates/nodes/src/function/arithmetic.rs @@ -1,7 +1,9 @@ use super::value_type::{dimensions, floats, integer_value, integers, intermediate_float_value as float_value}; use materialcraft_core::Value; -pub(crate) fn evaluate(op: &str, args: &[Value], random: &mut u32, scope: super::FunctionScope) -> Result { +pub(crate) fn evaluate(instruction: &super::instruction::Instruction, args: &[Value], random: &mut u32, scope: super::FunctionScope) -> Result { + let op = instruction.definition.id; + let pixel_math = scope == super::FunctionScope::PixelProcessor && !instruction.literal; let first = args.first().ok_or("missing arithmetic operand")?; let count = dimensions(first); let integer = matches!(first, Value::Int(_) | Value::Int2(_) | Value::Int3(_) | Value::Int4(_)); @@ -44,6 +46,12 @@ pub(crate) fn evaluate(op: &str, args: &[Value], random: &mut u32, scope: super: } if op == "dot" { + if pixel_math && count >= 2 { + // The second product rounds first. Each remaining channel contributes one fused operation. + let xy = left[0].mul_add(right[0], left[1] * right[1]); + let sum = left.iter().zip(right).take(count).skip(2).fold(xy, |sum, (a, b)| a.mul_add(b, sum)); + return float_value(&[sum]); + } return float_value(&[left.iter().zip(right).take(count).map(|(a, b)| a * b).sum()]); } @@ -69,6 +77,8 @@ pub(crate) fn evaluate(op: &str, args: &[Value], random: &mut u32, scope: super: "add" => left + right, "sub" => left - right, "mul" | "mulscalar" => left * right, + // Outside the normal reciprocal range, an intermediate overflow or underflow can lose a finite quotient. + "div" if pixel_math && right.recip().is_normal() => left * right.recip(), "div" => left / right, // Floored modulo wraps negative coordinates; remainder keeps their sign "mod" => left - right * (left / right).floor(), @@ -87,6 +97,7 @@ pub(crate) fn evaluate(op: &str, args: &[Value], random: &mut u32, scope: super: "sin" => left.sin(), "cos" => left.cos(), "tan" => left.tan(), + "lerp" if pixel_math => (right - left).mul_add(factor, left), "lerp" => left + (right - left) * factor, "rand" if scope == super::FunctionScope::Node => crate::random::node_function(random) * left, "rand" if scope == super::FunctionScope::FxMap => crate::random::pattern_function(random) * left, diff --git a/crates/nodes/src/function/compile.rs b/crates/nodes/src/function/compile.rs index 133510f..ffb6289 100644 --- a/crates/nodes/src/function/compile.rs +++ b/crates/nodes/src/function/compile.rs @@ -107,6 +107,7 @@ fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mu inputs: Vec::new(), value: Some(value), type_code, + literal: true, repeated: Vec::new(), call: None, }); @@ -146,8 +147,13 @@ fn compile_nested(graph: &FunctionGraph, seed: u32, depth: usize, remaining: &mu let input_type = |index: usize| inputs.get(index).and_then(|index| instructions.get(*index)).map_or(256, |instruction| instruction.type_code); let inferred = super::inferred_type::inferred_type(&node.op, value.as_ref(), input_type); let type_code = call.as_ref().map_or_else(|| if node.type_code == 0 { inferred } else { node.type_code }, |call| call.program.output_type()); + let literal = definition.id.starts_with("const_") + || (call.is_none() + && !inputs.is_empty() + && !matches!(definition.id, "rand" | "samplelum" | "samplecol" | "set" | "while") + && inputs.iter().all(|index| instructions.get(*index).is_some_and(|input| input.literal))); indices.insert(id, instructions.len()); - instructions.push(Instruction { id, definition, inputs, value, type_code, repeated: Vec::new(), call }); + instructions.push(Instruction { id, definition, inputs, value, type_code, literal, repeated: Vec::new(), call }); } // Instructions come after their inputs, so walking back from a loop reaches its whole condition and body diff --git a/crates/nodes/src/function/instruction.rs b/crates/nodes/src/function/instruction.rs index e4d40d9..907ac85 100644 --- a/crates/nodes/src/function/instruction.rs +++ b/crates/nodes/src/function/instruction.rs @@ -7,6 +7,8 @@ pub(crate) struct Instruction { pub inputs: Vec, pub value: Option, pub type_code: u32, + /// Pure expression with only literal inputs. Its arithmetic rounds before pixel evaluation. + pub literal: bool, /// For a loop: the instructions of its condition and body, evaluated again on every iteration pub repeated: Vec, pub call: Option, diff --git a/crates/nodes/src/function/operation.rs b/crates/nodes/src/function/operation.rs index f4f14ad..6716731 100644 --- a/crates/nodes/src/function/operation.rs +++ b/crates/nodes/src/function/operation.rs @@ -131,6 +131,6 @@ pub(crate) fn evaluate( if op == "samplelum" { Value::Float(pixel[0]) } else { Value::Float4(pixel) } } - _ => return super::arithmetic::evaluate(op, arguments, random, context.scope), + _ => return super::arithmetic::evaluate(instruction, arguments, random, context.scope), }) } diff --git a/crates/nodes/src/function/tests.rs b/crates/nodes/src/function/tests.rs index bfea4f8..d35805c 100644 --- a/crates/nodes/src/function/tests.rs +++ b/crates/nodes/src/function/tests.rs @@ -17,6 +17,58 @@ fn operation(name: &str, arguments: &[Value], setting: Option) -> Result< evaluate(&graph, &BTreeMap::new()) } +fn pixel_operation(op: &str, operands: &[Value], literal: bool) -> Value { + let mut graph = FunctionGraph::default(); + let mut variables = BTreeMap::new(); + let mut inputs = Vec::new(); + for (i, operand) in operands.iter().enumerate() { + let input = if literal { + graph.add_constant(operand.clone()) + } else { + let name = format!("input{i}"); + variables.insert(name.clone(), operand.clone()); + graph.add(&format!("get_float{}", super::value_type::dimensions(operand)), &[], Some(Value::Text(name))) + }; + inputs.push(input); + } + graph.output = Some(graph.add(op, &inputs, None)); + let context = + EvaluationContext { scope: super::FunctionScope::PixelProcessor, position: [0.5; 2], size: [1.0; 2], images: &[], tile: true, variables: &variables }; + compile(&graph, 0).unwrap().evaluate_pixel(&context, &mut EvaluationState::default()).unwrap() +} + +#[test] +fn pixel_dot_rounds_in_channel_order() { + // One sampled texel distinguishes the accumulation orders in all three widths. + let [x, y, z, w] = [0xc07089a0, 0x3fa2fba9, 0xbe30266f, 0xbcf10944].map(f32::from_bits); + for (left, right, dynamic_bits, literal_bits) in [ + (Value::Float2([x, y]), Value::Float2([0.2126, 0.7152]), 0x3de49f4e, 0x3de49f50), + (Value::Float3([x, y, z]), Value::Float3([0.2126, 0.7152, 0.0722]), 0x3dcb2fab, 0x3dcb2fad), + (Value::Float4([x, y, z, w]), Value::Float4([0.2126, 0.7152, 0.0722, -0.39]), 0x3de2afee, 0x3de2aff0), + ] { + let operands = [left, right]; + assert_eq!(pixel_operation("dot", &operands, false), Value::Float(f32::from_bits(dynamic_bits))); + assert_eq!(pixel_operation("dot", &operands, true), Value::Float(f32::from_bits(literal_bits))); + } +} + +#[test] +fn pixel_division_multiplies_by_the_reciprocal() { + let operands = [Value::Float(-2.7353475), Value::Float(1.7320508)]; + assert_eq!(pixel_operation("div", &operands, false), Value::Float(f32::from_bits(0xbfca24fb))); + assert_eq!(pixel_operation("div", &operands, true), Value::Float(f32::from_bits(0xbfca24fc))); + // Taking a reciprocal of this subnormal would overflow before the quotient can be computed. + let tiny = Value::Float(f32::from_bits(1)); + assert_eq!(pixel_operation("div", &[tiny.clone(), tiny], false), Value::Float(1.0)); +} + +#[test] +fn pixel_lerp_fuses_the_scaled_difference() { + let operands = [0xc04928a6, 0xbfc9a1db, 0xbefff6a8].map(|bits| Value::Float(f32::from_bits(bits))); + assert_eq!(pixel_operation("lerp", &operands, false), Value::Float(f32::from_bits(0xc07b52ad))); + assert_eq!(pixel_operation("lerp", &operands, true), Value::Float(f32::from_bits(0xc07b52ae))); +} + #[test] fn system_variables_resolve_with_their_declared_types() { for (operation, name, expected) in [ diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index c32f4bf..5dad14a 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -57,8 +57,49 @@ The first pass classifies all 546 roots: 166 Parity and 380 Too hard. These meas - **2026-11-13 - Unconnected output images:** Unconnected output bridges now produce transparent RGBA instead of grayscale. The importer separately retains each output's declared image type so saving an unconnected output preserves the graph interface. Both material templates match all 1,179,648 RGBA8 pixels across six recorded cases. All 2,560 direct grayscale/color declaration probe pixels also match, and both declared image types survive save/reload. +## 2026-11-17 root comparisons + +The clean-room comparisons below use the 16.0.7 reference app with the Direct3D 11 backend and a release MaterialCraft CPU cook. They compare the raw output bridge at its stored precision. A pixel counts only when every channel agrees. Integer errors are in stored levels. Floating errors are absolute. Each case starts from the graph defaults. Where a row selects another storage format, set the final processor's format to absolute in both apps before cooking. + +### 3D linear gradient + +Bind a linear RGBA8 position map at the output size `N`. At zero-based texel `(x,y)`, set R and G to the nearest integers to `255*x/(N-1)` and `255*y/(N-1)`, with ties to even. Set B to 128 and A to 255. Use the default UV points, or set `points_position_mode=1` for world mode. The oblique case sets the world points to `(.2,.1,.3)` and `(.8,.9,.6)` and contrast to .35. The float output includes values outside the unit interval. + +| Case | Size | Seed | Format | Exact pixels / total | Maximum error | +|---|---:|---:|---|---:|---:| +| Default UV | 256² / 512² | 0 | LUM32F | 65,536/65,536 and 262,144/262,144 | 0 | +| Default world points | 256² / 512² | 0 | LUM32F | 65,536/65,536 and 262,144/262,144 | 0 | +| Default UV | 128² | 42 | LUM32F | 16,384/16,384 | 0 | +| UV, contrast .6 | 256² / 512² | 0 | LUM32F | 26,043/65,536 and 104,172/262,144 | 2.384186e-7 | +| Oblique | 256² / 512² | 0 | LUM32F | 38,634/65,536 and 154,536/262,144 | 1.192093e-7 | +| Oblique | 128² | 42 | LUM8 | 16,384/16,384 | 0 | +| Oblique | 128² | 42 | LUM16 | 16,379/16,384 | 1 | +| Oblique | 128² | 42 | LUM16F | 16,367/16,384 | 2.44140625e-4 | + +At 256² the two default cases previously matched 46,763 and 32,755 float pixels. A separate pair of 128² probes for each of float2, float3 and float4 isolated the dot product. Use a varying signed RGBA32F input and dot its leading lanes with themselves or with the corresponding prefix of `(.2126,.7152,.0722,-.39)`. The measured accumulation starts with the rounded Y product and fuses X, then Z and W when present. All 98,304 sampled dot and squared-length results now agree exactly. Literal-only dot, division and interpolation expressions retain their separately measured rounding. + +For the signed test image, number RGBA components in row-major order starting at zero. Compute `v = i*747796405 + 2891336453`, then `v = ((v >> ((v >> 28)+4)) ^ v)*277803737` with unsigned 32-bit wrap at both steps. Store `8*((v ^ (v >> 22))/2^32)-4` as float32. Interpolating R to G with B as the factor and dividing R by the float32 value 1.7320508 each match 16,384/16,384 pixels with zero error. With G as the divisor, 14,372/16,384 match and the maximum error is 4.8828125e-4. Multiplying R by a separately captured reference reciprocal of G reproduces all 16,384 reference quotients. A standalone `R*G-B` probe matches 11,721/16,384 with a maximum error of 1.907349e-6. Fusing that product and subtraction reproduces every reference pixel. At 256² the contrast-.6 reference output is exactly a fused `gradient*2.5-.75` on the zero-contrast reference image. Separate graph operations and reciprocal approximation remain open. The unconnected Position case from the first pass also remains open. + +### Basic and ridged 3D Perlin + +Use the same position ramp. For a controlled comparison, bind an original 1024×16 grayscale lookup to the internal bitmap in both apps. With one Python `random.Random(104729)` instance, shuffle a fresh list of integers 0 through 1023 for each successive row. Write each entry as the nearest integer to `65535*entry/1023` in a linear 16-bit PNG. Both internal lookup outputs matched all 16,384 levels with zero error. The changed parameter is `scale=6` for basic Perlin and `roughness=.7` for ridged Perlin. Use an 8-bit parent format. + +| Case | Size | Seed | Format | Basic exact / total | Basic max | Ridged exact / total | Ridged max | +|---|---:|---:|---|---:|---:|---:|---:| +| Default | 128² | 0 | LUM8 | 16,383/16,384 | 1 | 16,384/16,384 | 0 | +| Default | 256² | 0 | LUM8 | 65,534/65,536 | 1 | 65,536/65,536 | 0 | +| Default | 128² | 42 | LUM8 | 16,384/16,384 | 0 | 16,384/16,384 | 0 | +| Changed | 128² | 0 | LUM8 | 16,384/16,384 | 0 | 16,384/16,384 | 0 | +| Changed | 128² | 42 | LUM16 | 16,360/16,384 | 1 | 16,304/16,384 | 1 | +| Changed | 128² | 42 | LUM16F | 16,384/16,384 | 0 | 16,378/16,384 | 4.8828125e-4 | +| Changed | 128² | 42 | LUM32F | 12,105/16,384 | 5.066395e-7 | 3,119/16,384 | 4.172326e-7 | + +The changed float case previously matched 10,399 pixels for basic Perlin and 3,926 for ridged Perlin. Whole-graph rounding remains visible in both. A separate seed-zero comparison used each app's shipped lookup, with MaterialCraft's tracked CC0 image read from Git. Basic Perlin matched 173/16,384 at 128² and 662/65,536 at 256², with maximum errors of 203 levels at both sizes. Ridged Perlin matched 176/16,384 and 768/65,536, with maximum errors of 139 and 154 levels. The shared lookup isolates an asset-dependent difference from the arithmetic. + ## Remaining differences +The following entries record the first pass. The dated comparisons above give the newer results for revisited roots. + - **`3d_gradient.sbs/3d_linear_gradient` - Too hard for this pass:** Corrected the wrong channel mapping and normalized-integer bilinear sampling offset. Compared generated position maps in UV and world modes, with zero and nonzero contrast, at 256 and 512 pixels (eight cases). Remaining 32-bit differences reach 7.153e-7; some values straddle a 16-bit rounding boundary by one level. The default world-mode case matches every pixel after 16-bit conversion at both sizes, but full float parity is not established. Further work needs matching the reference shader arithmetic rounding. An unconnected Position input yields NaNs in the reference app; MaterialCraft retains its actionable non-finite arithmetic error. Moving to the next node. - **`3d_perlin_noise.sbs/3D_perlin_noise` - Too hard for this pass:** Exact at 128x128 for defaults, seed 42, and scale 6. At 256x256, 3 of 65,536 eight-bit pixels remain one level different: (x=246,y=155), (25,179), and (154,227). Input maps are generated linear XY ramps with Z=128/255 and alpha=1. The output-format error is fixed; residual arithmetic/conversion rounding still prevents a parity mark. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 0066be8..58bfba7 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -35,16 +35,16 @@ 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 covers all **546 roots**: **166 Parity** and **380 Too hard**. Parity applies only to the recorded cases; it is not a claim of complete functional parity. +The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **166 Parity**, **3 In progress** and **377 Too hard**. Exact counts, output formats and remaining differences for the reopened roots are in the [November 17 comparisons](parity-fixes.md#2026-11-17-root-comparisons). ## Complete inventory | SBS package / graph path | Input pins | Hidden in library | Parity status | |---|---:|---|---| -| [3d_gradient.sbs/3d_linear_gradient](../assets/sbs/3d_gradient.sbs) | 1 | No | [Too hard: remaining float rounding](parity-fixes.md#remaining-differences) | -| [3d_perlin_noise.sbs/3D_perlin_noise](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [Too hard: 3 pixels remain](parity-fixes.md#remaining-differences) | +| [3d_gradient.sbs/3d_linear_gradient](../assets/sbs/3d_gradient.sbs) | 1 | No | [In progress · default UV and world exact, contrast differs](parity-fixes.md#3d-linear-gradient) | +| [3d_perlin_noise.sbs/3D_perlin_noise](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · lookup and arithmetic differences measured](parity-fixes.md#basic-and-ridged-3d-perlin) | | [3d_perlin_noise.sbs/3D_perlin_noise_fractal](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | -| [3d_perlin_noise.sbs/3D_ridged_noise_fractal](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [Too hard: 3 pixels remain](parity-fixes.md#remaining-differences) | +| [3d_perlin_noise.sbs/3D_ridged_noise_fractal](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · shared lookup exact in LUM8, higher precision differs](parity-fixes.md#basic-and-ridged-3d-perlin) | | [3d_planar_projection.sbs/3d_planar_projection](../assets/sbs/3d_planar_projection.sbs) | 3 | No | [Too hard: sampling boundaries](parity-fixes.md#remaining-differences) | | [3d_planar_projection.sbs/3d_planar_projection_color](../assets/sbs/3d_planar_projection.sbs) | 3 | No | [Too hard: sampling boundaries](parity-fixes.md#remaining-differences) | | [3d_simplex_noise.sbs/3d_simplex_noise](../assets/sbs/3d_simplex_noise.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |