diff --git a/crates/nodes/src/function/operation.rs b/crates/nodes/src/function/operation.rs index a8402d0e..adf2bc53 100644 --- a/crates/nodes/src/function/operation.rs +++ b/crates/nodes/src/function/operation.rs @@ -147,7 +147,7 @@ pub(crate) fn evaluate( } Some(image) if context.scope == super::FunctionScope::PixelProcessor => { let format = context.image_formats.get(index).copied().unwrap_or(materialcraft_core::OutputFormat::F32); - crate::texture_sampler::sample(image, [position[0], position[1]], [true; 2], format, None) + crate::texture_sampler::sample_pixel(image, [position[0], position[1]], format) } Some(image) => image.sample_stored(position[0], position[1], context.tile), None => [0.0; 4], diff --git a/crates/nodes/src/function/tests.rs b/crates/nodes/src/function/tests.rs index a44a0d3b..3e97b11d 100644 --- a/crates/nodes/src/function/tests.rs +++ b/crates/nodes/src/function/tests.rs @@ -797,6 +797,30 @@ fn pixel_gray_sampler_combines_stored_color_channels_after_reading() { } } +#[test] +fn filtered_pixel_reads_flush_single_precision_subnormals_before_and_after_filtering() { + let normal = f32::MIN_POSITIVE; + let subnormal = f32::from_bits(normal.to_bits() - 1); + let left = [subnormal, -subnormal, -normal, normal * 2.0]; + let image = Image::generate(2, 1, Kind::Color, |x, _| if x == 0 { left } else { [normal, -normal, 0.0, subnormal] }); + let context = EvaluationContext { + scope: super::FunctionScope::PixelProcessor, + position: [0.0; 2], + size: [2.0, 1.0], + images: &[Some(image)], + image_formats: &[materialcraft_core::OutputFormat::F32], + tile: true, + variables: &BTreeMap::new(), + }; + for (filter, u, expected) in [(0, 0.25, left), (1, 0.25, [0.0, -0.0, -normal, normal * 2.0]), (1, 0.5, [0.0, -0.0, -0.0, normal])] { + let mut graph = FunctionGraph::default(); + let uv = graph.add_constant(Value::Float2([u, 0.5])); + graph.output = Some(graph.add("samplecol", &[uv], Some(Value::Int2([0, filter])))); + let value = compile(&graph, 0).unwrap().evaluate_pixel(&context, &mut EvaluationState::default()).unwrap(); + assert_eq!(value.as_vec4().unwrap().map(f32::to_bits), expected.map(f32::to_bits)); + } +} + #[test] fn pixel_dot_rounds_in_channel_order() { // One sampled texel distinguishes the accumulation orders in all three widths. diff --git a/crates/nodes/src/texture_sampler.rs b/crates/nodes/src/texture_sampler.rs index e1256e78..4248d1fd 100644 --- a/crates/nodes/src/texture_sampler.rs +++ b/crates/nodes/src/texture_sampler.rs @@ -54,7 +54,15 @@ pub(crate) fn sample(image: &Image, uv: [f32; 2], tiling: [bool; 2], format: Out if !uv.iter().all(|v| v.is_finite()) { return matte.unwrap_or([0.0, 0.0, 0.0, 1.0]); } - filter_taps(image, taps(image, uv, tiling), tiling, format, matte) + filter_taps(image, taps(image, uv, tiling), tiling, format, matte, std::convert::identity) +} + +/// A filtered pixel read flushes F32 subnormals at the texels and again at the filter result. +pub(crate) fn sample_pixel(image: &Image, uv: [f32; 2], format: OutputFormat) -> [f32; 4] { + if format != OutputFormat::F32 || !uv.iter().all(|v| v.is_finite()) { + return sample(image, uv, [true; 2], format, None); + } + filter_taps(image, taps(image, uv, [true; 2]), [true; 2], format, None, |pixel| pixel.map(crate::function::flush_subnormal)) } /// Average each clamped 2×2 footprint at source precision, then store the reduced level. @@ -62,20 +70,28 @@ pub(crate) fn reduce_level(image: &Image, source: OutputFormat, destination: Out let level = Image::generate(image.width().div_ceil(2), image.height().div_ceil(2), image.kind(), |x, y| { let (x, y) = (i64::from(x) * 2, i64::from(y) * 2); let taps = [(x, y, 64), (x + 1, y, 64), (x, y + 1, 64), (x + 1, y + 1, 64)]; - filter_taps(image, taps, [false; 2], source, None) + filter_taps(image, taps, [false; 2], source, None, std::convert::identity) }); crate::output_precision::apply(level, destination) } -fn filter_taps(image: &Image, taps: [(i64, i64, u32); 4], tiling: [bool; 2], format: OutputFormat, matte: Option<[f32; 4]>) -> [f32; 4] { +fn filter_taps( + image: &Image, + taps: [(i64, i64, u32); 4], + tiling: [bool; 2], + format: OutputFormat, + matte: Option<[f32; 4]>, + prepare: impl Fn([f32; 4]) -> [f32; 4], +) -> [f32; 4] { if image.integer_sampling() { return sample_normalized(image, taps, tiling, matte); } + let read = |x, y| prepare(texel(image, x, y, tiling, matte)); if let Some((x, y, _)) = taps.iter().find(|(_, _, weight)| *weight == 256) { - return texel(image, *x, *y, tiling, matte); + return read(*x, *y); } - let pixels = taps.map(|(x, y, weight)| (texel(image, x, y, tiling, matte), weight)); - std::array::from_fn(|channel| filter_float(&pixels, channel, format)) + let pixels = taps.map(|(x, y, weight)| (read(x, y), weight)); + prepare(std::array::from_fn(|channel| filter_float(&pixels, channel, format))) } fn taps(image: &Image, [u, v]: [f32; 2], tiling: [bool; 2]) -> [(i64, i64, u32); 4] { diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 97f4f445..3ece7ee5 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -232,6 +232,22 @@ The remaining F32 gradient measurements separate coverage from the tint arithmet Six current-header saves reopened in the reference app and matched 34,816/34,816 pixels across 58 renders, maximum error zero. The 19 previously verified pattern roots retained 3,211,776/3,211,776 exact pixels in 159 renders, maximum error zero. +### Subnormal values in filtered pixel reads + +The F32 bilinear Pixel Processor sampler flushes subnormal input components to signed zero before filtering. It applies the same rule to the filtered result. Nearest sampling and directly wired images preserve their input bit patterns. The 150 original renders below match **41,664/41,664 RGBA32F pixels**, maximum error zero. The first 60 previously matched 14,489/15,360, maximum 1.10202595e−38. + +| Sample mode and field | Renders | Exact pixels / compared | +|---|---:|---:| +| U16, F16 and F32 fields sampled at output pixel centers | 60 | 15,360/15,360 | +| Mixed signed F32 fields at six input sizes, bilinear at output centers | 18 | 4,608/4,608 | +| Same fields, nearest at output centers or at (.390625,.34375) | 36 | 9,216/9,216 | +| Same fields, bilinear at (.390625,.34375) | 18 | 4,608/4,608 | +| Direct wire at the six original input sizes | 18 | 7,872/7,872 | + +Use the [three-storage sampler fields](#signed-zero-in-floating-point-texture-filtering) for the first row. For the other rows use a 16² output and scale the source width and height by (1,1), (2,2), (½,½), (2,1), (1,2) or (½,2). At relative size j generate three fields with seeds 100151+19j+i. Use F32 bit patterns 00000000, 80000000, 00371245, 80371245, 007fffff, 807fffff, 00800000, 80800000, 00800001, 80800001, 009fffff, 80bfffff, 01000000, 81000000, 017fffff, 817fffff, 02000001 and 82000001. The first field samples this list uniformly with a seeded PCG64 generator in row, column and channel order. For the second field take index (5x+9y+3c) mod 18, with index 12+(c mod 2) where (floor(x/2)+3floor(y/2)+c) mod 7 is zero. For the last field take index (x+7y+c) mod 18, with −.25 where (x+y+c) mod 3 is zero. At output centers the native nearest reads preserve all 3,547 subnormal components. The filtered reads return signed zeros there when the result falls below the smallest positive normal. + +Eight small capture graphs also tested the cost of loading the node library. Use the first row at size/storage pairs (0,U16), (1,F16), (2,F32), (3,U16), (4,F16), (3,F32), (0,F32) and (4,U16), including all four field variants for each pair. Running eight independent sessions in separate processes took 26.12 and 24.70 seconds. Running the same sessions in one process with a shared read-only library took 3.34 and 3.29 seconds. Test the two orders on one worker. Each run produced 96 identical images, 30,976/30,976 captured pixels, identical node values and errors, and zero maximum difference. + ### Signed zero in floating-point texture filtering A bilinear footprint containing only zeros retains a negative sign in a channel when every contributing texel has a negative sign. Texels with zero weight have no effect. On 20 original color Pixel Processor renders, all **20,480/20,480 F16- and F32-source component signs** match this rule. The full 60-render matrix improved from 10,741 to 14,489/15,360 bit-exact RGBA32F pixels. The remaining maximum absolute error is 1.10202595e−38. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 4c9a9064..e607edc8 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -41,6 +41,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard Pattern center culling matched all 356,352 pixels across 204 renders. Background-only resizing matched all 150,528 pixels across 84 renders. The mixed-size square controls now match all 188,160 pixels, and the 288 independent depth controls match all 73,728 pixels. [Opacity and accumulator measurements](parity-fixes.md#pattern-opacity-near-zero-and-color-accumulation) [Pattern backgrounds, saved graphs and affected roots](parity-fixes.md#pattern-backgrounds-and-center-culling) +Filtered F32 pixel reads now match 41,664/41,664 pixels across 150 renders. The nearest and directly wired controls preserve subnormal inputs. [Sampler measurements and capture timings](parity-fixes.md#subnormal-values-in-filtered-pixel-reads) + The 32²-to-16² signed threshold controls match 2,048/2,048 pixels. [Floating texture filter measurements](parity-fixes.md#signed-zero-in-floating-point-texture-filtering) Connected F32 backgrounds now match all 2,048 same-size pixels. Across 132 further renders with changed input sizes, the maximum absolute error is zero. Some RGBA zeros still differ in sign. [Connected background counts and file checks](parity-fixes.md#connected-floating-point-pattern-backgrounds)