Filter transform mip levels at their stored precision

This commit is contained in:
chasse20 committed 2026-11-18 13:34:28 -05:00
1 parent 3652c61559
commit e9c74b8e21
4 files changed
+56 -31

No files matched your search

+20 -26
View File
@@ -32,8 +32,8 @@ fn transformation(context: &CookContext) -> CookResult {
let nearest = context.i("filtering") == 1;
let levels = mip_level(&input, matrix, context);
let source_format = context.input_formats.first().copied().unwrap_or(if input.integer_sampling() { OutputFormat::U16 } else { OutputFormat::F32 });
let sample_format = if !nearest && levels > 0 && (input.width() > 1 || input.height() > 1) { context.format } else { source_format };
let input = reduce_mip(input, levels, nearest, context.format);
let sample_format = if levels > 0 && (input.width() > 1 || input.height() > 1) { context.format } else { source_format };
let input = reduce_mip(input, levels, source_format, context.format);
let matte = crate::output_precision::store_pixel(context.v4("matte"), sample_format);
// Keep independent wrap axes for panoramic images
@@ -71,37 +71,18 @@ fn mip_level(input: &Image, matrix: [f32; 4], context: &CookContext) -> u32 {
dx.max(dy).max(1.0).log2().round().clamp(0.0, 13.0) as u32
}
fn reduce_mip(mut input: Image, levels: u32, nearest: bool, format: OutputFormat) -> Image {
let nearest_maximum = match format {
OutputFormat::U8 => Some(255.0),
OutputFormat::Inherit | OutputFormat::U16 => Some(65535.0),
OutputFormat::F16 | OutputFormat::F32 => None,
};
fn reduce_mip(mut input: Image, levels: u32, mut source_format: OutputFormat, format: OutputFormat) -> Image {
for _ in 0..levels {
if input.width() <= 1 && input.height() <= 1 {
break;
}
// Bilinear samples of either normalized source depth return a 16-bit value.
let maximum = if nearest { nearest_maximum } else { input.integer_sampling().then_some(65535.0) };
// Both final filters read the same mip chain. Each reduction samples at the preceding level's precision.
input = Image::generate(input.width().div_ceil(2), input.height().div_ceil(2), input.kind(), |x, y| {
let (x, y) = (i64::from(x) * 2, i64::from(y) * 2);
let pixels = [input.get(x, y, false), input.get(x + 1, y, false), input.get(x, y + 1, false), input.get(x + 1, y + 1, false)];
std::array::from_fn(|channel| {
if let Some(maximum) = maximum {
let sum: u32 = pixels.iter().map(|pixel| (pixel[channel].clamp(0.0, 1.0) * maximum).round() as u32).sum();
((sum + 2) / 4) as f32 / maximum
} else {
// Average in f64 so finite HDR texels cannot overflow the sum
(pixels.iter().map(|pixel| f64::from(pixel[channel])).sum::<f64>() * 0.25) as f32
}
})
crate::texture_sampler::mip_texel(&input, x, y, source_format)
});
if !nearest {
// Each reduction is stored at the transform's output precision before the next read.
input = crate::output_precision::apply(input, format);
}
input = crate::output_precision::apply(input, format);
source_format = format;
}
input
@@ -114,6 +95,19 @@ mod tests {
use materialcraft_core::Value;
use std::collections::BTreeMap;
#[test]
fn mip_reductions_use_the_precision_of_each_source_level() {
for (format, bits, expected) in [
(OutputFormat::F16, [0x3fa2e000, 0x3cd0c000, 0x404e0000, 0xc0180000], 0x3f092000),
(OutputFormat::F32, [0x4012ab5d, 0xbf9f3b55, 0x4075821b, 0xc02f817b], 0x3f090e53),
(OutputFormat::U8, [0x3f62e2e3, 0x3ec8c8c9, 0x3f46c6c7, 0x3f068687], 0x3f2525a5),
] {
let input = Image::gray(2, 2, |x, y| f32::from_bits(bits[(2 * y + x) as usize])).with_integer_sampling(format == OutputFormat::U8);
let output = reduce_mip(input, 1, format, OutputFormat::F32);
assert_eq!(output.get_gray(0, 0, false).to_bits(), expected);
}
}
#[test]
fn large_affine_offsets_preserve_the_last_visible_mip_texels() {
let color = [23.0, 139.0, 211.0, 255.0].map(|v| v / 255.0);
+11 -1
View File
@@ -52,7 +52,17 @@ 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]);
}
let taps = taps(image, uv, tiling);
filter_taps(image, taps(image, uv, tiling), tiling, format, matte)
}
/// Reduce a 2×2 source footprint with equal weights and clamped edges.
pub(crate) fn mip_texel(image: &Image, x: u32, y: u32, format: OutputFormat) -> [f32; 4] {
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], format, None)
}
fn filter_taps(image: &Image, taps: [(i64, i64, u32); 4], tiling: [bool; 2], format: OutputFormat, matte: Option<[f32; 4]>) -> [f32; 4] {
if image.integer_sampling() {
return sample_normalized(image, taps, tiling, matte);
}
+21
View File
@@ -253,6 +253,27 @@ Literal-only pixel graphs at 16², seed 23, give 512/768 exact pixels for each l
The shared-lookup ridged root still matches 5,160/16,384 at changed roughness in F32, maximum 1.78813934e−7. Its one-octave .7 control still matches 1,847/4,096, maximum 2.38418579e−7. For positive normal x on the signed field, the reference's general power agrees with exp(y×ln(x)) on all 32,614 pixels. On .7 with exponent 1 it returns `3f333334`. Further work on general power and natural exponential is tracked with the [octave controls](#ridged-noise-octave-isolation).
### Stored precision in transform mip levels
| Root | Cases | Exact RGBA pixels / compared | Maximum error |
|---|---:|---:|---:|
| Composite transform filter | 18 | 171,264/171,264 | 0 |
| First PBR renderer, panoramic LOD | 18 | 186,624/186,624 | 0 |
| Second PBR renderer, LOD | 18 | 171,264/171,264 | 0 |
The first PBR panorama matches all four archived renders, all four changed output formats, the independent 32² input and nine additional color fields. All measured intermediate images now match on the full, large and signed fields. Its 18 cases include 162,048 F32 pixels and 8,192 pixels in each other format. The earlier large-offset controls now match 688,128/688,128 pixels across 42 transforms. The [panorama comparison](#large-transform-offsets-and-additive-blends) gives the inputs, matrices, wrapping and seeds.
An equal-weight 2×2 reduction is enough to reproduce the previous discrepancy. Each mip level filters at the preceding level's storage precision, then stores the reduced image at the transform's output precision. Both nearest and bilinear final sampling use those same levels. For a float input, align the tap significands to the largest participating exponent before summing. The [floating texture sampling controls](#transform-edges-and-floating-point-filtering) describe the filter. For example, four F32 texels with bits 4012ab5d, bf9f3b55, 4075821b and c02f817b reduce to 3f090e53 in row-major order. A half source with F32 bits 3fa2e000, 3cd0c000, 404e0000 and c0180000 reduces to 3f092000.
| Direct transform controls | Cases | Exact RGBA pixels / compared | Maximum error |
|---|---:|---:|---:|
| One, two or three mip levels | 120 | 215,040/215,040 | 0 |
| Untiled matte after reduction | 24 | 98,304/98,304 | 0 |
For the 120 cases, use an identity matrix, zero offset, both axes tiled and a fixed 128² RGBA source. Run nearest and bilinear transforms at 64², 32² and 16². For each combination of four source and four destination formats, take the [signed-field hash](#3d-linear-gradient) per channel. Use the low eight or sixteen bits for integer inputs, or 8H/2³²−4 for F16 and F32, seed 557. For both floating-point source and destination formats also use ((13x+31y+7xy+47c) mod 1024 / 128−3) × 2^((x+3y) mod 13−6), seed 659. The first four texels are (−0,0,−0,0), (.25,1,−4,−.5), (1,−2,.125,.875) and (−1,4,−.25,.75). For the 24 matte cases, use the 64² output with U8, F16 and F32 sources, both filters, offset (4,−4), no tiling and matte (.5,.123456,−.312345,1.144445). Once a mip level has been generated, the transform reads its border at destination precision.
For the composite transform and the second renderer's LOD, the original four renders provide 114,688 pixels per root and the previous four-format and independent inputs add 17,408. For nine further cases, define channel c as ((97x+43y+7xy+143c+9xc) mod 1024)/1023 in an original RGBA32F image. Use 16² F32/seed 43, 64² in all four formats/73, 128² F32/197, 32×16 input at 32² F32/239, signed 32² F32/311 with 3H−1.25, and a repeat of 64² F32 at seed 401. The LOD graph has no exposed numeric control. For the composite filter use defaults at 16², and otherwise matrix (1.25,.375,−.125,.875), offset (.1875,−.4375), bilinear. At 128² use (−1,0,0,.5), offset (−.0625,.25), nearest. For signed input use (2.5,−.75,.25,1.125), offset (.25,−.125), bilinear. Each graph matches all 39,168 additional pixels.
### Large transform offsets and additive blends
| Root | Measured cases | Exact pixels (maximum error zero) |
+4 -4
View File
@@ -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 **224 Parity**, **25 In progress** and **297 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 **227 Parity**, **24 In progress** and **295 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)
@@ -150,7 +150,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
| [composite.sbs/composite/blends/blend_matte_fg_passthrough](../assets/sbs/composite.sbs) | 2 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
| [composite.sbs/composite/filters/filter_mask](../assets/sbs/composite.sbs) | 2 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
| [composite.sbs/composite/filters/filter_split_rgb_a](../assets/sbs/composite.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
| [composite.sbs/composite/filters/filter_transformation](../assets/sbs/composite.sbs) | 1 | Yes | [Too hard: pixel differences](parity-fixes.md#remaining-differences) |
| [composite.sbs/composite/filters/filter_transformation](../assets/sbs/composite.sbs) | 1 | Yes | [Parity · 18 cases, 171,264 exact pixels](parity-fixes.md#stored-precision-in-transform-mip-levels) |
| [composite.sbs/composite/filters/filter_transformation_grayscale](../assets/sbs/composite.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
| [constant_values.sbs/constant_boolean](../assets/sbs/constant_values.sbs) | 0 | No | [Parity: 3 typed values](parity-fixes.md#verified-cases) |
| [constant_values.sbs/float/constant_float](../assets/sbs/constant_values.sbs) | 0 | No | [Parity: 3 typed values](parity-fixes.md#verified-cases) |
@@ -441,7 +441,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
| [pbr_render.sbs/chroma_shift](../assets/sbs/pbr_render.sbs) | 1 | Yes | [Parity · 13 cases, 163,840 exact pixels](parity-fixes.md#height-distance-texture-mapping-skew-and-weave) |
| [pbr_render.sbs/environment_map_rescale](../assets/sbs/pbr_render.sbs) | 1 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [pbr_render.sbs/get_max_coords/get_max_coord](../assets/sbs/pbr_render.sbs) | 1 | Yes | [Parity · 12 typed coordinate comparisons](parity-fixes.md#value-processor-coordinates) |
| [pbr_render.sbs/ibl_specular_contrib_anisotropic/panoramic_lod](../assets/sbs/pbr_render.sbs) | 1 | Yes | [In progress · first float transform differs after mip reduction](parity-fixes.md#large-transform-offsets-and-additive-blends) |
| [pbr_render.sbs/ibl_specular_contrib_anisotropic/panoramic_lod](../assets/sbs/pbr_render.sbs) | 1 | Yes | [Parity · 18 cases, 186,624 exact pixels](parity-fixes.md#stored-precision-in-transform-mip-levels) |
| [pbr_render.sbs/position_to_normal](../assets/sbs/pbr_render.sbs) | 2 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [pbr_render.sbs/raytrace_position](../assets/sbs/pbr_render.sbs) | 4 | Yes | [Too hard: cook timeout](parity-fixes.md#remaining-differences) |
| [pbr_render.sbs/raytraced_irradiance_pass_8](../assets/sbs/pbr_render.sbs) | 10 | Yes | [Too hard: cook timeout](parity-fixes.md#remaining-differences) |
@@ -453,7 +453,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
| [pbr_render2.sbs/get_max_coords/get_max_coord](../assets/sbs/pbr_render2.sbs) | 1 | Yes | [Parity · 12 typed coordinate comparisons](parity-fixes.md#value-processor-coordinates) |
| [pbr_render2.sbs/height_adjust](../assets/sbs/pbr_render2.sbs) | 3 | Yes | [Parity: 3 cases](parity-fixes.md#verified-cases) |
| [pbr_render2.sbs/ibl_specular_contrib_anisotropic/panoramic_lod](../assets/sbs/pbr_render2.sbs) | 1 | Yes | [Parity · 18 cases, 186,624 exact pixels](parity-fixes.md#large-transform-offsets-and-additive-blends) |
| [pbr_render2.sbs/lod](../assets/sbs/pbr_render2.sbs) | 1 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [pbr_render2.sbs/lod](../assets/sbs/pbr_render2.sbs) | 1 | Yes | [Parity · 18 cases, 171,264 exact pixels](parity-fixes.md#stored-precision-in-transform-mip-levels) |
| [pbr_render2.sbs/position_to_normal](../assets/sbs/pbr_render2.sbs) | 2 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [pbr_render2.sbs/raytrace_position](../assets/sbs/pbr_render2.sbs) | 4 | Yes | [Too hard: cook timeout](parity-fixes.md#remaining-differences) |
| [pbr_render2.sbs/raytraced_irradiance_is_pass_8](../assets/sbs/pbr_render2.sbs) | 9 | Yes | [Too hard: cook timeout](parity-fixes.md#remaining-differences) |