mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-22 15:30:55 +01:00
Match runtime pixel reciprocals and random draw progression
This commit is contained in:
1 parent
a95edb51d7
commit
8ce0d07ffc
7 files changed
+158
-12
No files matched your search
@@ -77,8 +77,7 @@ pub(crate) fn evaluate(instruction: &super::instruction::Instruction, args: &[Va
|
||||
"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" if pixel_math => super::reciprocal::divide(left, right),
|
||||
"div" => left / right,
|
||||
// Floored modulo wraps negative coordinates; remainder keeps their sign
|
||||
"mod" => left - right * (left / right).floor(),
|
||||
@@ -100,11 +99,7 @@ pub(crate) fn evaluate(instruction: &super::instruction::Instruction, args: &[Va
|
||||
"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,
|
||||
"rand" => {
|
||||
*random = crate::random::hash(random.wrapping_add(1));
|
||||
(*random >> 8) as f32 / 16777216.0 * left
|
||||
}
|
||||
"rand" => crate::random::image_function(random) * left,
|
||||
_ => return Err(format!("unknown arithmetic operation `{op}`")),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ mod output;
|
||||
mod processor;
|
||||
mod product_sum;
|
||||
mod program;
|
||||
mod reciprocal;
|
||||
mod resolve_instances;
|
||||
mod system_variable;
|
||||
mod unconnected_input;
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
//! Fixed-point reciprocal used by runtime pixel division.
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct Polynomial {
|
||||
quarter_constant: i32,
|
||||
half_slope: i32,
|
||||
curvature: i32,
|
||||
}
|
||||
|
||||
const fn nearest(value: f64) -> i32 {
|
||||
(value + if value < 0.0 { -0.5 } else { 0.5 }) as i32
|
||||
}
|
||||
|
||||
// Derive the coefficients from 1/x. The normalized mantissa spans 128 equal intervals.
|
||||
// In units of the output significand, the function on interval i is 2^31 / (128+i+t).
|
||||
const fn polynomials() -> [Polynomial; 128] {
|
||||
const SCALE: f64 = 2_147_483_648.0;
|
||||
let mut result = [Polynomial { quarter_constant: 0, half_slope: 0, curvature: 0 }; 128];
|
||||
let mut i = 0;
|
||||
while i < result.len() {
|
||||
let start = 128.0 + i as f64;
|
||||
let mid = start + 0.5;
|
||||
let mid_squared = mid * mid;
|
||||
// On [-1/2,1/2], the linear minimax approximation of t³ is 3t/16.
|
||||
let center_slope = -SCALE / mid_squared - (3.0 / 16.0) * SCALE / (mid_squared * mid_squared);
|
||||
let half_slope = nearest((center_slope - SCALE / (mid_squared * mid)) * 0.5);
|
||||
let slope = 2 * half_slope;
|
||||
let curvature = nearest(center_slope) - slope;
|
||||
|
||||
// Center the quarter-unit constant between the extrema of the residual.
|
||||
let mut low = SCALE;
|
||||
let mut high = 0.0;
|
||||
let mut step = 0;
|
||||
while step <= 64 {
|
||||
let t = step as f64 / 64.0;
|
||||
let residual = SCALE / (start + t) - slope as f64 * t - curvature as f64 * t * t;
|
||||
if residual < low {
|
||||
low = residual;
|
||||
}
|
||||
if residual > high {
|
||||
high = residual;
|
||||
}
|
||||
step += 1;
|
||||
}
|
||||
result[i] = Polynomial { quarter_constant: nearest(2.0 * (low + high)), half_slope, curvature };
|
||||
i += 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
const POLYNOMIALS: [Polynomial; 128] = polynomials();
|
||||
|
||||
// Square a 16-bit fraction, retaining partial products in the top 15 fractional columns.
|
||||
// A pair of distinct bits contributes twice. A diagonal bit contributes once.
|
||||
fn square_fraction(fraction: u32) -> u32 {
|
||||
let mut square = 0;
|
||||
for bit in 1..=7 {
|
||||
if fraction & (1 << (16 - bit)) != 0 {
|
||||
let width = 16 - 2 * bit;
|
||||
square += (1 << (width - 1)) + ((fraction >> bit) & ((1 << width) - 1));
|
||||
}
|
||||
}
|
||||
square
|
||||
}
|
||||
|
||||
pub(super) fn divide(numerator: f32, denominator: f32) -> f32 {
|
||||
let bits = denominator.to_bits();
|
||||
let exponent = (bits >> 23) & 255;
|
||||
if !(1..=252).contains(&exponent) {
|
||||
// At the limits, an intermediate reciprocal can overflow or underflow a finite quotient.
|
||||
let reciprocal = denominator.recip();
|
||||
return if reciprocal.is_normal() { numerator * reciprocal } else { numerator / denominator };
|
||||
}
|
||||
let Some(bin) = POLYNOMIALS.get(((bits >> 16) & 127) as usize) else {
|
||||
return numerator / denominator;
|
||||
};
|
||||
let fraction = bits & 0xffff;
|
||||
let polynomial = (i64::from(bin.quarter_constant) << 13)
|
||||
+ i64::from(bin.half_slope) * i64::from(fraction)
|
||||
+ i64::from(bin.curvature) * i64::from(square_fraction(fraction));
|
||||
// The observed rounding threshold includes a bias of 2023/32768 of an output unit.
|
||||
let significand = ((polynomial + 16_384 + 2_023) >> 15) as u32;
|
||||
let magnitude = ((252 - exponent) << 23) + significand;
|
||||
numerator * f32::from_bits((bits & 0x8000_0000) | magnitude)
|
||||
}
|
||||
@@ -115,6 +115,46 @@ fn pixel_division_multiplies_by_the_reciprocal() {
|
||||
assert_eq!(pixel_operation("div", &[tiny.clone(), tiny], false), Value::Float(1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn measured_pixel_reciprocals_preserve_runtime_rounding() {
|
||||
for (input, expected) in [(0x3ecccccc, 0x40200000), (0x3ecc4ccd, 0x4020643f), (0xc2ff6f11, 0xbc0048a0), (0x038537b1, 0x7b75f93f)] {
|
||||
let x = f32::from_bits(input);
|
||||
let operands = [Value::Float(1.0), Value::Float(x)];
|
||||
assert_eq!(pixel_operation("div", &operands, false), Value::Float(f32::from_bits(expected)));
|
||||
assert_eq!(pixel_operation("div", &operands, true), Value::Float(1.0 / x));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn measured_pixel_random_draws_share_one_stream() {
|
||||
let mut graph = FunctionGraph::default();
|
||||
let one = graph.add_constant(Value::Float(1.0));
|
||||
let negative = graph.add_constant(Value::Float(-2.0));
|
||||
let factor = graph.add_constant(Value::Float(0.375));
|
||||
let first = graph.add("rand", &[one], None);
|
||||
let second = graph.add("rand", &[one], None);
|
||||
let third = graph.add("rand", &[negative], None);
|
||||
let fourth = graph.add("rand", &[factor], None);
|
||||
let pair1 = graph.add("vector2", &[first, second], None);
|
||||
let pair2 = graph.add("vector2", &[third, fourth], None);
|
||||
graph.output = Some(graph.add("vector4", &[pair1, pair2], None));
|
||||
let variables = BTreeMap::new();
|
||||
let context = EvaluationContext {
|
||||
scope: super::FunctionScope::PixelProcessor,
|
||||
position: [0.5; 2],
|
||||
size: [32.0; 2],
|
||||
images: &[],
|
||||
image_formats: &[],
|
||||
tile: true,
|
||||
variables: &variables,
|
||||
};
|
||||
let program = compile(&graph, 0).unwrap();
|
||||
for (mut state, bits) in [(9_091_073, [1060669180, 1057720276, 1034667712, 1054479120]), (3_342_178, [1058664790, 1044635448, 1061348922, 1062659162])] {
|
||||
let [a, b, c, d] = bits.map(f32::from_bits);
|
||||
assert_eq!(program.evaluate_with_random(&context, &mut EvaluationState::default(), &mut state).unwrap(), Value::Float4([a, b, -2.0 * c, 0.375 * d]));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pixel_lerp_fuses_the_scaled_difference() {
|
||||
let operands = [0xc04928a6, 0xbfc9a1db, 0xbefff6a8].map(|bits| Value::Float(f32::from_bits(bits)));
|
||||
|
||||
@@ -15,8 +15,8 @@ pub(crate) fn node_function(state: &mut u32) -> f32 {
|
||||
*state as f32 / 16_777_216.0
|
||||
}
|
||||
|
||||
/// The measured scalar sequence for functions evaluated inside a pattern graph.
|
||||
pub(crate) fn pattern_function(state: &mut u32) -> f32 {
|
||||
/// Scalar draws in pixel and pattern functions use the upper 23 bits of this stream.
|
||||
pub(crate) fn image_function(state: &mut u32) -> f32 {
|
||||
*state = state.wrapping_mul(3_626_811).wrapping_add(51_137) & 0x00ff_ffff;
|
||||
(*state >> 1) as f32 / 8_388_608.0
|
||||
}
|
||||
|
||||
@@ -76,6 +76,29 @@ Repeat the November 17 **3D linear gradient** cases with that change. At 256² a
|
||||
|
||||
With the original shared lookup described under [basic and ridged 3D Perlin](#basic-and-ridged-3d-perlin), **basic 3D Perlin** now matches 65,536/65,536 pixels at 256² and 16,384/16,384 in each of the other six cases, including changed LUM16, LUM16F and LUM32F. All maximum errors are zero. With the applications' respective stock lookups, only 173/16,384 and 663/65,536 LUM8 pixels match. Both maxima are 203 levels.
|
||||
|
||||
### Runtime reciprocal and 3D gradient contrast
|
||||
|
||||
The twelve wired 3D linear gradient cases listed under [3D linear gradient](#3d-linear-gradient) now match **1,376,256/1,376,256 pixels**, maximum error zero. This includes all 65,536 and 262,144 LUM32F pixels at contrast .6 and all four measured output formats. An exposed contrast of .6 gives the float32 denominator `0x3ecccccc`. Its runtime reciprocal is 2.5. With a literal expression, the reciprocal is the next float above 2.5.
|
||||
|
||||
For a standalone Pixel Processor, nearest-sample a linear RGBA32F input and output the reciprocal of one channel at its original dimensions. Five fields match **671,744/671,744 LUM32F pixels**, maximum error zero. Use seed zero. For the 128² case take G from the signed field under [3D linear gradient](#3d-linear-gradient). Construct the remaining four fields from float32 bits at row-major pixel index `i`, repeated in RGB.
|
||||
|
||||
| Dimensions | Input bits | Exact pixels / total |
|
||||
|---|---|---:|
|
||||
| 256² | `0x3f800000 + 128*i` | 65,536/65,536 |
|
||||
| 256² | `(125 << 23) + floor(3*2^23/5) - 32768 + i` | 65,536/65,536 |
|
||||
| 512² | Use `h=(747796405*i+2891336453) mod 2^32` as below | 262,144/262,144 |
|
||||
| 512² | Use `h=(1013904223*i+341937) mod 2^32` as below | 262,144/262,144 |
|
||||
|
||||
For the 512² fields, use the low 23 bits of `h` as the fraction, `7 + (37*((h >> 22) XOR i)) mod 241` as the biased exponent, and bit 2 of `i` as the sign. MaterialCraft derives a quadratic for each of 128 mantissa intervals from the mathematical reciprocal. The measured fixed-point square retains partial products through fractional bit 15. The literal division path retains its original rounding. A separate 32² RGBA32F probe emits contrast, denominator, reciprocal and half the reciprocal minus one half. All nine runs match 1,024/1,024 pixels each, including exposed values .3, .35, .51, .6, .625, .875 and .99, an image input and a literal.
|
||||
|
||||
With Position disconnected, the reference app returns zero in U8 and U16 and quiet NaN in floating storage. At 32² with seed zero, the 1,024 raw samples are respectively `0x00`, `0x0000`, `0x7fff` and `0x7fffffff`. At 64² with seed 42 the F32 result has the same NaN bits at all 4,096 pixels. MaterialCraft still reports a non-finite float output for this case.
|
||||
|
||||
### Pixel Processor random draws
|
||||
|
||||
Emit four independent calls to `rand(1)` as RGBA32F. In the reference app the four values recover a unique initial 24-bit state at each of **27,968/27,968 pixels**. Advance the state with `s=(3626811*s+51137) mod 2^24` and return `floor(s/2)/2^23`. The four outputs then match at every pixel, maximum error zero. Use square canvases of sides 8, 16, 32, 64 and 128 at seed zero, also 32² at seeds 1, 2, 42 and 137, and 32×16 and 16×32 at seeds zero and 42. The first 32² pixel starts from 9,091,073 at seed zero and 3,342,178 at seed 42.
|
||||
|
||||
Four additional original graphs replicate a single draw to RGBA, reuse two draws in alternating channels, double a reused draw, or apply ranges 1, 1, −2 and .375 to successive draws. At 16², 32² and 64² with seed zero and 32² with seed 42, all **25,600/25,600 reference pixels** agree with the corresponding channel expressions, maximum error zero. The MaterialCraft evaluator now matches the progression given the recovered initial state. Its mapping from pixel position and graph seed to that state remains open. For example, the native and CPU four-draw images at 32² still match 0/1,024 entire pixels at seeds zero and 42, with maximum component errors .995849967 and .989675045.
|
||||
|
||||
### Color conversion, checker and clone
|
||||
|
||||
The ACEScg-to-linear-sRGB root matches **131,072/131,072 RGBA pixels across eight cases**, maximum component error zero. Bind the following original linear RGBA8 fields directly to its image input. At integer texel `(x,y)` on an `N×N` field, let `U=floor(255*x/(N-1))` and `V=floor(255*y/(N-1))`. Arithmetic in the four stored byte channels wraps modulo 256.
|
||||
@@ -173,6 +196,8 @@ Each root matches **131,072/131,072 pixels in eight cases**, maximum component e
|
||||
- `gradient_linear_2` generates its own input. The first four outputs are LUM16. For the changed cases set `Tiling=3` and `rotation=1`.
|
||||
- `multiangle_to_albedo` uses the foreground field on input 1 and the background field on inputs 2 through 8, as specified under [color conversion, checker and clone](#color-conversion-checker-and-clone). The first four outputs are RGBA8. For the changed cases set `samples_amount=4` and keep the 128² inputs for the 64² format sweep.
|
||||
|
||||
Six additional albedo cases use a different original image on each of the eight pins. For input number `j=1..8` the RGBA bytes are `((17+j)*x+(11+2*j)*y+29*j, (7*j+1)*x+5*y+13*j, 3*x+(13*j+1)*y+41*j, (5+j)*x+(11-j)*y+23*j)` modulo 256. Match the input and output dimensions. The default at 64² with seed zero matches 4,096/4,096 RGBA8 pixels. Four samples at 128² and seed 137 match 16,384/16,384 RGBA8 pixels. At 32² and seed 42 the same setting matches 1,024/1,024 pixels in each of U8, U16, F16 and F32. Maximum error is zero throughout.
|
||||
|
||||
### Atlas crop with wired value inputs
|
||||
|
||||
For `atlas_spliter.sbs/crop_and_transform_sub`, connect constant value processors to `shape_count` and `shape_data_table_size`. Use count 1, table size (1,1), and a single RGBA32F table texel (0,0,1,1). Bind a transparent 16² background and solid white 16² shape-index and opacity images. For the 128² color image, use linear RGBA8 texels `((17*x+31*y)%256, floor(255*x/127), 255-floor(255*y/127), (7*x+13*y)%256)`. Give the instance a parent-relative (0,0) size. The reference app returns RGBA32F at its authored precision.
|
||||
|
||||
@@ -41,7 +41,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
|
||||
|
||||
| SBS package / graph path | Input pins | Hidden in library | Parity status |
|
||||
|---|---:|---|---|
|
||||
| [3d_gradient.sbs/3d_linear_gradient](../assets/sbs/3d_gradient.sbs) | 1 | No | [In progress · default and oblique gradients exact, contrast .6 differs](parity-fixes.md#pixel-processor-products-and-sums) |
|
||||
| [3d_gradient.sbs/3d_linear_gradient](../assets/sbs/3d_gradient.sbs) | 1 | No | [In progress · all 12 wired cases exact, disconnected float output differs](parity-fixes.md#runtime-reciprocal-and-3d-gradient-contrast) |
|
||||
| [3d_perlin_noise.sbs/3D_perlin_noise](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · shared lookup exact across seven cases, stock lookup differs](parity-fixes.md#pixel-processor-products-and-sums) |
|
||||
| [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 | [In progress · shared lookup exact in LUM8, higher precision differs](parity-fixes.md#basic-and-ridged-3d-perlin) |
|
||||
@@ -59,7 +59,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
|
||||
| [3d_texture_transform.sbs/3d_texture_offset_grayscale](../assets/sbs/3d_texture_transform.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [3d_viewer.sbs/ibl_specular_contrib_anisotropic/panoramic_lod](../assets/sbs/3d_viewer.sbs) | 1 | Yes | [In progress · input resizing and transform filtering measured](parity-fixes.md#panoramic-lod) |
|
||||
| [3d_volume_mask.sbs/3d_volume_mask](../assets/sbs/3d_volume_mask.sbs) | 1 | No | [In progress · box rotation and feathering differ](parity-fixes.md#3d-volume-mask-and-worley-noise) |
|
||||
| [3d_worley_noise.sbs/3D_worley_noise](../assets/sbs/3d_worley_noise.sbs) | 1 | No | [In progress · random sequence differs before distance evaluation](parity-fixes.md#3d-volume-mask-and-worley-noise) |
|
||||
| [3d_worley_noise.sbs/3D_worley_noise](../assets/sbs/3d_worley_noise.sbs) | 1 | No | [In progress · random draws measured, per-pixel starting state differs](parity-fixes.md#pixel-processor-random-draws) |
|
||||
| [alveolus.sbs/alveolus](../assets/sbs/alveolus.sbs) | 0 | Yes | [In progress · 64² format sweep exact, larger fields differ](parity-fixes.md#alveolus-quadrant-placement-and-falloff) |
|
||||
| [angle_to_vector.sbs/angle_to_vector](../assets/sbs/angle_to_vector.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [anisotropic_blur.sbs/anisotropic_blur](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [In progress · first directional pass differs](parity-fixes.md#anisotropic-blur) |
|
||||
@@ -272,7 +272,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
|
||||
| [mosaic.sbs/mosaic_grayscale](../assets/sbs/mosaic.sbs) | 2 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
|
||||
| [multi_directional_warp.sbs/multi_directional_warp_color](../assets/sbs/multi_directional_warp.sbs) | 2 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
|
||||
| [multi_directional_warp.sbs/multi_directional_warp_grayscale](../assets/sbs/multi_directional_warp.sbs) | 2 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
|
||||
| [multiangle_to_albedo.sbs/multiangle_to_albedo](../assets/sbs/multiangle_to_albedo.sbs) | 8 | No | [Parity · 8 cases, RGBA in four formats](parity-fixes.md#linear-gradient-2-and-multiangle-albedo) |
|
||||
| [multiangle_to_albedo.sbs/multiangle_to_albedo](../assets/sbs/multiangle_to_albedo.sbs) | 8 | No | [Parity · 14 cases, including eight distinct input images](parity-fixes.md#linear-gradient-2-and-multiangle-albedo) |
|
||||
| [multiangle_to_normal.sbs/angle_to_normal](../assets/sbs/multiangle_to_normal.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [multiangle_to_normal.sbs/angle_weight](../assets/sbs/multiangle_to_normal.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [noise_anisotropic_noise.sbs/anisotropic_noise](../assets/sbs/noise_anisotropic_noise.sbs) | 0 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
|
||||
|
||||
Reference in new issue
Block a user