diff --git a/crates/eval/src/external_image.rs b/crates/eval/src/external_image.rs index e4c1631d..4e52f62b 100644 --- a/crates/eval/src/external_image.rs +++ b/crates/eval/src/external_image.rs @@ -17,9 +17,7 @@ pub(super) fn resize(image: &Image, size: (u32, u32), kind: Kind, source: Output let output_levels = match output { OutputFormat::U8 => Some(255.0), OutputFormat::Inherit | OutputFormat::U16 => Some(65535.0), - OutputFormat::F32 => None, - OutputFormat::F16 if input_levels.is_none() => None, - OutputFormat::F16 => return None, + OutputFormat::F32 | OutputFormat::F16 => None, }; // Keep the lower channel depth through both filter passes. A byte raster sent to // a word or float output still filters at byte precision. @@ -38,11 +36,15 @@ pub(super) fn resize(image: &Image, size: (u32, u32), kind: Kind, source: Output filtered = filter_axis(filtered, horizontal, end, maximum.is_some()); } if output == OutputFormat::F16 { - // Float rasters are filtered at full precision before the half writer diffuses its rounding error. - return diffuse(&filtered, |original, received| { - let stored = nearest_positive_half(original + received); - (stored, original - stored) - }); + return match source { + OutputFormat::U8 => Some(filtered.map(kind, |p| p.map(|v| crate::output_precision::store_half(v / 255.0))).with_integer_sampling(false)), + OutputFormat::U16 => word_to_half(&filtered), + // Float rasters are filtered at full precision before the half writer diffuses its rounding error. + _ => diffuse(&filtered, |original, received| { + let stored = nearest_positive_half(original + received); + (stored, original - stored) + }), + }; } Some(if let Some(maximum) = maximum { filtered.map(kind, |p| p.map(|v| v / maximum)).with_integer_sampling(output_levels.is_some()) } else { filtered }) } @@ -71,7 +73,8 @@ fn filter_axis(image: Image, horizontal: bool, end: u32, integer: bool) -> Image let radius = 1.0 / filter_scale; let taps: Vec<_> = (0..end) .map(|index| { - let center = (f64::from(index) + 0.5) / scale; + // On enlargement the mapped origin and half-texel offset round independently. + let center = if end > start { f64::from(index) / scale + 0.5 / scale } else { (f64::from(index) + 0.5) / scale }; let first = (center - 0.5 - radius).ceil().max(0.0) as u32; let last = ((center - 0.5 + radius).floor() as u32).min(start - 1); // Keep the downsampling factor through normalization. Cancelling it changes rounding at close texels. @@ -102,13 +105,35 @@ fn filter_axis(image: Image, horizontal: bool, end: u32, integer: bool) -> Image }) } +fn next_fraction(state: &mut u64) -> f32 { + *state = (48_271 * *state) % 2_147_483_647; + (*state as f64 / 2_147_483_647.0) as f32 +} + +fn word_to_half(image: &Image) -> Option { + let mut state = 1_u64; + diffuse(image, |original, received| { + let original = f32::from(original as u16); + let word = (original + received + (next_fraction(&mut state) - 0.5) * 16.0) as u16; + let (stored, reconstructed) = match word { + // The low-end conversion has distinct readback and error values at word three. + 0..=2 => (0.0, 0.0), + 3 => (2.0 / 65536.0, 3.0), + _ => { + let stored = crate::output_precision::store_half(f32::from(word) / 65535.0); + (stored, (stored * 65536.0).min(65535.0)) + } + }; + (stored, original - reconstructed) + }) +} + fn dither(image: &Image, maximum: f32, word_to_byte: bool) -> Option { let mut state = 1_u64; let unit = maximum.recip(); let limit = f32::MAX / (4.0 * maximum); diffuse(image, |value, received| { - state = (48_271 * state) % 2_147_483_647; - let random = (state as f64 / 2_147_483_647.0) as f32; + let random = next_fraction(&mut state); let original = if value.is_finite() { value.clamp(-limit, limit) } else { f32::from(value > 0.0) }; let stored = if word_to_byte { // One full byte level expands to 257 word levels. The noise spans one high-byte step. diff --git a/crates/eval/src/tests.rs b/crates/eval/src/tests.rs index d3fdac10..a0aa9f49 100644 --- a/crates/eval/src/tests.rs +++ b/crates/eval/src/tests.rs @@ -433,6 +433,51 @@ fn emboss_uses_the_resolved_pixel_size_on_each_axis() { } } +#[test] +fn bitmap_growth_maps_the_origin_and_half_texel_offset_separately() { + let mut graph = Graph::new("Column"); + graph.size = [0, 6]; + graph.format = materialcraft_core::OutputFormat::F32; + let input = graph.add_node("input_grayscale", [0.0; 2]); + let mut package = Package::new("Column"); + package.graphs.push(graph); + let mut options = Options::new(&no_res); + let column = Image::gray(1, 49, |_, y| { + f32::from_bits(match y { + 5 => 0xbeae_712e, + 6 => 0x3f90_4fe4, + _ => 0, + }) + }); + options.inputs.insert("input".into(), column); + let result = cook(&package, 0, &options, &Mutex::new(Cache::default())); + assert_eq!(result.image(input, 0).unwrap().get_gray(0, 7, false).to_bits(), 0x3c73_7400); +} + +#[test] +fn bitmap_half_storage_uses_the_original_integer_channel_depth() { + use materialcraft_core::OutputFormat::{F16, U8, U16}; + + let mut graph = Graph::new("Byte and word"); + graph.size = [5, 5]; + graph.format = F16; + let input = graph.add_node("input_grayscale", [0.0; 2]); + let mut package = Package::new("Integer source"); + package.graphs.push(graph); + for (depth, level, expected) in [ + (U8, 9.0 / 255.0, [0x3d10_8000; 3]), + (U16, 2313.0 / 65535.0, [0x3d10_0000, 0x3d10_2000, 0x3d10_c000]), + (U16, 1.0, [0x3f7f_e000, 0x3f7f_e000, 0x3f80_0000]), + ] { + let mut options = Options::new(&no_res); + options.bitmap_formats.insert("input".into(), depth); + options.inputs.insert("input".into(), Image::filled(32, 17, Kind::Gray, [level; 4]).with_integer_sampling(true)); + let result = cook(&package, 0, &options, &Mutex::new(Cache::default())); + let image = result.image(input, 0).unwrap(); + assert_eq!([0, 1, 2].map(|x| image.get_gray(x, 0, false).to_bits()), expected); + } +} + #[test] fn bound_float_bitmap_reduces_close_texels_at_the_measured_filter_precision() { let mut graph = Graph::new("Row reduction"); diff --git a/crates/nodes/src/output_precision.rs b/crates/nodes/src/output_precision.rs index 281eeb01..7495c908 100644 --- a/crates/nodes/src/output_precision.rs +++ b/crates/nodes/src/output_precision.rs @@ -2,7 +2,8 @@ use craft_texture::{Image, Limits}; use materialcraft_core::OutputFormat; use std::sync::Arc; -fn store_half(pixel: f32) -> f32 { +/// Round to the half value used when storing a cooked channel. +pub fn store_half(pixel: f32) -> f32 { if !pixel.is_finite() { return if pixel.is_nan() { half::f16::from_bits(0x7fff).to_f32() } else { pixel }; } diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 47f24dc1..669d5396 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -1167,6 +1167,31 @@ For Worley, Dissolve, the four diffusion passes, six flood-fill outputs and both For Worley also set size (.75,1.25,1.5), Minkowski number .6875, border width .1875 and roundness .3125. Set optional diffusion controls where exposed. For Flood Fill 2 color use HSL adjustment (.125,−.1875,.25) and random range (.375,.625,.1875). For its grayscale equivalent use luminance adjustment −.1875 and random range .625. The random flood-fill outputs and fast white noise have no additional exposed numeric changes in these controls. Each residual flood-fill difference is at most one U16 level or .0000152587891 in F32. Uniform noise selects the measured per-texel stream. The later [conditional draw comparison](#conditional-pixel-draws-and-white-noise-v2) resolves the Gaussian differences in White Noise v2. +### Enlarging bitmap rows and converting integer images to half + +| External source and destination | Renders | Exact bridge pixels / compared | Exact F32 output pixels / compared | Maximum F32 output error | +|---|---:|---:|---:|---:| +| Signed F32 fields, original sizes | 78 | 79,872/79,872 | 239,616/239,616 | 0 | +| Signed F32 fields, 17 additional growth ratios | 51 | 147,456/147,456 | 466,944/466,944 | 0 | +| Six byte PNG fields read through half | 6 | 6,144/6,144 | 1/6,144 | .000486373901 | +| Thirty word PNG fields read through half | 30 | 30,720/30,720 | 30,720/30,720 | 0 | +| Twenty byte PNG constants read through half | 20 | 20,480/20,480 | 4,096/20,480 | .000411689281 | +| Fifty word PNG constants read through half | 50 | 51,200/51,200 | 51,200/51,200 | 0 | + +A one-column input reproduces the previous [float growth difference](#float-bitmap-reduction-and-half-output). Resize 49 rows to 64. Put F32 words beae712e and 3f904fe4 in rows 5 and 6, with zeros elsewhere. Row 7 has bits 3c737400. During enlargement, map the output index and half-texel offset separately in F64, as i/s + .5/s for s=destination/source. The original 35×49 grayscale and RGBA fields now match at the bridge and at the square output. + +For the 51 additional controls, the source width and bridge width are 32. Use source heights 21, 35, 43, 45, 47, 49, 51, 55, 57 and 61 with destination height 64. Use heights 67, 71, 87, 91, 93, 97 and 105 with destination height 128. Render at the destination height squared, sampling the bridge at normalized texel centers. At each height generate three original RGBA32F images, with zero-based x, y and channel c. + +- At seed 709, use (((17x+53y+31xy+19c) mod 2047)/1024−1) × 2^((3x+7y+c) mod 8−4). +- At seed 710, reinterpret 3f800001 plus (37x+53y+13xy+31c) mod 256 as F32 bits. Negate where x+3y+c is odd. +- At seed 711, use (((21x+29y+11c+13xy) mod 1021)/1020−.5) × 2^((x+y) mod 13−5). + +For the PNG fields use the [byte and word recipes](#source-image-depth-and-the-three-noise-upscalers). For constants, bind 32×17 grayscale and repeated RGBA images to a 32² half bridge, seed 881. Byte levels are 0, 1, 9, 31, 127, 128, 143, 196, 250 and 255. Word levels are 0, 1, 3, 7, 9, 13, 16, 127, 128, 129, 251, 513, 1023, 2117, 2313, 4118, 8193, 15427, 22001, 32079, 32767, 39114, 49197, 65529 and 65535. + +Filter both axes at the source integer precision. A byte value b reports the half of b/255 rounded toward zero at the bridge. At byte level 9, that is 3d108000 expanded to F32. A word source uses the [three-row diffusion weights](#non-power-of-two-float-image-inputs-and-curvature) in word units with one new random sample per interleaved channel. Start the generator at 1, multiply by 48271 modulo 2147483647, and divide by 2147483647 for a unit draw r. Truncate original word + received error + 16(r−.5) and clamp to the word range. Values 0 through 2 read and reconstruct as zero. Value 3 reads as 2/65536 and reconstructs as 3. For larger values q, store the half of q/65535 rounded toward zero, and reconstruct as min(65535,65536×stored). Propagate original minus reconstructed. For word level 2313 the first three expanded channel words are 3d100000, 3d102000 and 3d10c000. + +The reference F32 consumer of these byte sources reads the unrounded filtered byte divided by 255. Its half bridge readback and the consumer value are distinct. This discrepancy accounts for all 22,527 differing downstream pixels in the two byte rows. The float-input to U8 and U16 controls retain 18,414/18,432 and 18,418/18,432 exact pixels, with maximum errors of one stored level. Both curvature roots retain 176,384/176,384 across 20 renders each. Each of the three noise upscalers retains 697,344/697,344 across 21. + ### Float bitmap reduction and half output | Bound input and bridge storage, read through F32 | Renders | Exact pixels before | Exact pixels after / compared | Maximum F32 error after | diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index ff3cbe20..60a1ab07 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -170,8 +170,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [crop.sbs/crop_grayscale](../assets/sbs/crop.sbs) | 1 | No | [Parity · 48 renders, all 377,344 pixels across both outputs exact](parity-fixes.md#half-precision-crop-area-and-input-sizes) | | [crystal_1.sbs/crystal_1](../assets/sbs/crystal_1.sbs) | 0 | Yes | [In progress · 19 renders, 99,700/172,288 exact pixels](parity-fixes.md#filtering-unequal-inputs-to-a-blend) | | [crystal_2.sbs/crystal_2](../assets/sbs/crystal_2.sbs) | 0 | Yes | [In progress · 19 renders, 113/172,288 exact pixels](parity-fixes.md#random-draws-in-pattern-properties-and-iterations) | -| [curvature.sbs/curvature](../assets/sbs/curvature.sbs) | 1 | No | [Parity · 20 renders, 176,384 exact grayscale pixels](parity-fixes.md#float-bitmap-reduction-and-half-output) | -| [curvature_smooth.sbs/curvature_smooth](../assets/sbs/curvature_smooth.sbs) | 1 | Yes | [Parity · 20 renders, 176,384 exact grayscale pixels](parity-fixes.md#float-bitmap-reduction-and-half-output) | +| [curvature.sbs/curvature](../assets/sbs/curvature.sbs) | 1 | No | [Parity · 20 renders, 176,384 exact pixels](parity-fixes.md#enlarging-bitmap-rows-and-converting-integer-images-to-half) | +| [curvature_smooth.sbs/curvature_smooth](../assets/sbs/curvature_smooth.sbs) | 1 | Yes | [Parity · 20 renders, 176,384 exact pixels](parity-fixes.md#enlarging-bitmap-rows-and-converting-integer-images-to-half) | | [curvature_smooth_v2.sbs/compute_curvature_v2](../assets/sbs/curvature_smooth_v2.sbs) | 2 | Yes | [Parity: 6 cases](parity-fixes.md#verified-cases) | | [diffusion.sbs/impl/pass_RGBA](../assets/sbs/diffusion.sbs) | 4 | Yes | [Parity · 19 renders, 143,872 exact pixels](parity-fixes.md#per-pixel-random-initialization) | | [diffusion.sbs/impl/pass_UV](../assets/sbs/diffusion.sbs) | 1 | Yes | [Parity · 19 renders, 143,872 exact pixels](parity-fixes.md#per-pixel-random-initialization) | @@ -352,9 +352,9 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [noise_perlin_noise.sbs/perlin_noise](../assets/sbs/noise_perlin_noise.sbs) | 0 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) | | [noise_perlin_noise_v2.sbs/perlin_noise](../assets/sbs/noise_perlin_noise_v2.sbs) | 0 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) | | [noise_plasma.sbs/plasma](../assets/sbs/noise_plasma.sbs) | 0 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) | -| [noise_upscale_1.sbs/noise_upscale_1](../assets/sbs/noise_upscale_1.sbs) | 1 | No | [Parity · 21 renders, 697,344 exact grayscale pixels](parity-fixes.md#float-bitmap-reduction-and-half-output) | -| [noise_upscale_2.sbs/noise_upscale_2](../assets/sbs/noise_upscale_2.sbs) | 1 | No | [Parity · 21 renders, 697,344 exact grayscale pixels](parity-fixes.md#float-bitmap-reduction-and-half-output) | -| [noise_upscale_3.sbs/noise_upscale_3](../assets/sbs/noise_upscale_3.sbs) | 2 | No | [Parity · 21 renders, 697,344 exact grayscale pixels](parity-fixes.md#float-bitmap-reduction-and-half-output) | +| [noise_upscale_1.sbs/noise_upscale_1](../assets/sbs/noise_upscale_1.sbs) | 1 | No | [Parity · 21 renders, 697,344 exact pixels](parity-fixes.md#enlarging-bitmap-rows-and-converting-integer-images-to-half) | +| [noise_upscale_2.sbs/noise_upscale_2](../assets/sbs/noise_upscale_2.sbs) | 1 | No | [Parity · 21 renders, 697,344 exact pixels](parity-fixes.md#enlarging-bitmap-rows-and-converting-integer-images-to-half) | +| [noise_upscale_3.sbs/noise_upscale_3](../assets/sbs/noise_upscale_3.sbs) | 2 | No | [Parity · 21 renders, 697,344 exact pixels](parity-fixes.md#enlarging-bitmap-rows-and-converting-integer-images-to-half) | | [noise_voronoi.sbs/3d_voronoi](../assets/sbs/noise_voronoi.sbs) | 1 | No | [In progress · 37 renders, 152,320/155,392 exact pixels with stock and shared tables](parity-fixes.md#voronoi-distortion-and-shared-noise-inputs) | | [noise_voronoi.sbs/3d_voronoi_fractal](../assets/sbs/noise_voronoi.sbs) | 1 | No | [In progress · 37 renders, 152,163/155,392 exact pixels with stock and shared tables](parity-fixes.md#voronoi-distortion-and-shared-noise-inputs) | | [noise_voronoi.sbs/voronoi](../assets/sbs/noise_voronoi.sbs) | 0 | No | [In progress · 37 renders, 152,320/155,392 exact pixels with stock and shared tables](parity-fixes.md#voronoi-distortion-and-shared-noise-inputs) |