mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-26 09:20:56 +01:00
Preserve measured zero signs in enlarged pattern backgrounds
This commit is contained in:
1 parent
a810085f6d
commit
5c95d89ac8
4 files changed
+59
-16
No files matched your search
@@ -25,23 +25,32 @@ pub(super) fn warp_input(context: &CookContext, port: usize) -> (Image, OutputFo
|
||||
resized_texture(context, port, InputResize::Warp).unwrap_or_else(|| (Image::filled(context.w, context.h, Kind::Gray, [0.0; 4]), context.format))
|
||||
}
|
||||
|
||||
fn prepare_pattern_background(image: Image, mode: InputResize, source: OutputFormat) -> Image {
|
||||
if mode != InputResize::PatternBackground || source != OutputFormat::F32 {
|
||||
fn prepare_pattern_background(image: Image, mode: InputResize, source: OutputFormat, enlarged: bool) -> Image {
|
||||
if mode != InputResize::PatternBackground {
|
||||
return image;
|
||||
}
|
||||
// Connected gray backgrounds clear the sign of zero. Color backgrounds retain it.
|
||||
let gray = image.kind() == Kind::Gray;
|
||||
let prepare = |v| {
|
||||
let v = crate::function::flush_subnormal(v);
|
||||
if gray && v == 0.0 { 0.0 } else { v }
|
||||
};
|
||||
if image.data().iter().any(|&v| prepare(v).to_bits() != v.to_bits()) { image.map(image.kind(), |pixel| pixel.map(prepare)) } else { image }
|
||||
let flush = source == OutputFormat::F32;
|
||||
let enlarged_color = enlarged && !gray && matches!(source, OutputFormat::F16 | OutputFormat::F32);
|
||||
if !flush && !enlarged_color {
|
||||
return image;
|
||||
}
|
||||
let needs_change = |&v: &f32| (flush && v.is_subnormal()) || ((gray || enlarged_color) && v.to_bits() == (-0.0_f32).to_bits());
|
||||
if !image.data().iter().any(needs_change) {
|
||||
return image;
|
||||
}
|
||||
image.map(image.kind(), |pixel| {
|
||||
let pixel = if flush { pixel.map(crate::function::flush_subnormal) } else { pixel };
|
||||
// In an enlarged color background a negative zero survives only when all three RGB signs are negative.
|
||||
let clear_zero_sign = gray || (enlarged_color && pixel[..3].iter().any(|v| v.is_sign_positive()));
|
||||
if clear_zero_sign { pixel.map(|v| if v == 0.0 { 0.0 } else { v }) } else { pixel }
|
||||
})
|
||||
}
|
||||
|
||||
fn resized_texture(context: &CookContext, port: usize, mode: InputResize) -> Option<(Image, OutputFormat)> {
|
||||
let mut image = context.input(port)?.clone();
|
||||
let mut source = context.input_formats.get(port).copied().unwrap_or(if image.integer_sampling() { OutputFormat::U16 } else { OutputFormat::F32 });
|
||||
image = prepare_pattern_background(image, mode, source);
|
||||
image = prepare_pattern_background(image, mode, source, false);
|
||||
let target = craft_texture::clamp_size(context.w, context.h);
|
||||
let size = (image.width(), image.height());
|
||||
// A warp can take its final taps directly from an input up to one level larger on either axis.
|
||||
@@ -76,7 +85,7 @@ fn resized_texture(context: &CookContext, port: usize, mode: InputResize) -> Opt
|
||||
let intermediate = (advance(current.0, target.0), advance(current.1, target.1));
|
||||
let (w, h) = if intermediate == current { target } else { intermediate };
|
||||
image = crate::texture_sampler::resize_input_level(&image, w, h, source, context.tiling_axes());
|
||||
image = prepare_pattern_background(image, mode, source);
|
||||
image = prepare_pattern_background(image, mode, source, (w, h) == target && (w > size.0 || h > size.1));
|
||||
// A final reduction can feed the Levels curve directly. An enlargement stores the
|
||||
// filtered texels first, which matters when the working image uses half precision.
|
||||
if mode == InputResize::Levels && (w, h) == target && w <= current.0 && h <= current.1 {
|
||||
|
||||
@@ -749,11 +749,22 @@ fn connected_pattern_backgrounds_prepare_small_samples_before_filtering() {
|
||||
let largest_subnormal = f32::from_bits(0x007f_ffff);
|
||||
let gray = Image::gray(2, 2, |x, _| if x == 0 { largest_subnormal } else { 2.0 * smallest });
|
||||
let underflow = Image::gray(2, 2, |x, _| if x == 0 { smallest } else { 0.0 });
|
||||
for (input, size, expected) in [
|
||||
(gray, 1, [smallest.to_bits(); 4]),
|
||||
(underflow, 1, [0; 4]),
|
||||
(Image::filled(2, 2, Kind::Gray, [-largest_subnormal; 4]), 2, [0; 4]),
|
||||
(Image::filled(2, 2, Kind::Color, [-largest_subnormal, -0.0, largest_subnormal, -smallest]), 2, [0x8000_0000, 0x8000_0000, 0, 0x8080_0000]),
|
||||
let mixed = [-0.0_f32, -0.375, 0.5, -0.0];
|
||||
let negative = [-0.0_f32, -0.375, -0.5, -0.0];
|
||||
for (input, size, source, expected) in [
|
||||
(gray, 1, OutputFormat::F32, [smallest.to_bits(); 4]),
|
||||
(underflow, 1, OutputFormat::F32, [0; 4]),
|
||||
(Image::filled(2, 2, Kind::Gray, [-largest_subnormal; 4]), 2, OutputFormat::F32, [0; 4]),
|
||||
(
|
||||
Image::filled(2, 2, Kind::Color, [-largest_subnormal, -0.0, largest_subnormal, -smallest]),
|
||||
2,
|
||||
OutputFormat::F32,
|
||||
[0x8000_0000, 0x8000_0000, 0, 0x8080_0000],
|
||||
),
|
||||
(Image::filled(2, 2, Kind::Color, mixed), 2, OutputFormat::F32, mixed.map(f32::to_bits)),
|
||||
(Image::filled(2, 2, Kind::Color, mixed), 4, OutputFormat::F32, [0, mixed[1].to_bits(), mixed[2].to_bits(), 0]),
|
||||
(Image::filled(2, 2, Kind::Color, mixed), 4, OutputFormat::F16, [0, mixed[1].to_bits(), mixed[2].to_bits(), 0]),
|
||||
(Image::filled(2, 2, Kind::Color, negative), 4, OutputFormat::F32, negative.map(f32::to_bits)),
|
||||
] {
|
||||
let kind = input.kind();
|
||||
let graph = PatternGraph::with_root(PatternNodeKind::Quadrant);
|
||||
@@ -768,7 +779,7 @@ fn connected_pattern_backgrounds_prepare_small_samples_before_filtering() {
|
||||
seed: 100237,
|
||||
format: OutputFormat::F32,
|
||||
tile: true,
|
||||
input_formats: &[OutputFormat::F32],
|
||||
input_formats: &[source],
|
||||
inputs: &[Some(input)],
|
||||
params: ¶ms,
|
||||
resource: &|_| None,
|
||||
|
||||
@@ -248,6 +248,27 @@ Use the [three-storage sampler fields](#signed-zero-in-floating-point-texture-fi
|
||||
|
||||
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.
|
||||
|
||||
### Signs of zero in enlarged color backgrounds
|
||||
|
||||
MaterialCraft now preserves a negative zero in an enlarged connected RGBA pattern background when all three filtered RGB channels have their sign bit set. A positive sign in any RGB channel changes zero components to positive zero, including an alpha of negative zero. The rule applies at the final enlargement before output storage. A 2× enlargement of the constant (−0,−.375,.5,−0) gives (+0,−.375,.5,+0). With blue set to −.5 both negative zeros survive. Same-size controls preserve the original RGBA signs.
|
||||
|
||||
| Original control | Renders | Previous exact | Exact pixels / compared | Maximum numerical error |
|
||||
|---|---:|---:|---:|---:|
|
||||
| Vary RGB and alpha signs in independent spatial fields | 80 | 14,027 | 20,480/20,480 | 0 |
|
||||
| Flat fields with mixed zero signs and ordinary values | 80 | 12,800 | 20,480/20,480 | 0 |
|
||||
| Both sides of the first nonzero half value, four output formats | 120 | 30,131 | 30,720/30,720 | 0 |
|
||||
| Earlier signed F32 inputs at six relative sizes | 72 | 17,764 | 18,432/18,432 | 0 |
|
||||
| Earlier subnormal and normal threshold fields | 60 | 14,575 | 15,360/15,360 | 0 |
|
||||
| Earlier U8, U16 and F16 sources | 120 | 30,559 | 30,599/30,720 | 0 |
|
||||
|
||||
For the first row render an empty color pattern at 16² F32. Use F32 and F16 RGBA sources at 16², 32², 8², 8×16, 16×8, 8×32, 4² and 32×4. Number those sizes j from zero, and the five fields i from zero. For the first three fields use sign mask (floor(x/2)+3floor(y/2)+5i) mod 16. For the last two use (3x+5y+3xy+i) mod 16. Bit c sets the sign of channel c, starting with red. Use zero magnitude in field 0. In field 1 use .375+.125c where (floor(x/2)+floor(y/2)+c) mod 3 is zero. In field 2 use .1875+.0625c where (5x+3y+c) mod 3 is zero. Elsewhere these two fields use zero. In fields 3 and 4 choose magnitude at index (x+5y+3c+i) mod 4 from (0,2⁻¹²⁶,2⁻¹²⁵,2⁻¹²⁷) for F32 and (0,2⁻²⁴,2⁻²⁰,.75) for F16. Use seed 102389+13j+i.
|
||||
|
||||
For the second row use both source formats, sizes 8², 16², 32², 4² and 8×32, and seed 102991+16j+i. Use the two constant examples above followed by (−0,−0,−0,+0), (+0,−0,−0,−0), (.25,−0,−0,−0), (−.25,−0,−0,.75), (−0,.25,−0,−.75) and (−0,−0,.25,−.75).
|
||||
|
||||
For the half threshold row use source sizes 16², 32², 8², 4² and 8×32. Draw three fields with sign mask (5x+3y+7xy+i) mod 16 and magnitudes (0,2⁻²⁴,2⁻²⁵,2⁻²³,2⁻²⁶,2⁻²¹,2⁻²⁴,.3125). For field 0 index the magnitudes by (floor(x/2)+5floor(y/2)+2c) mod 8. For fields 1 and 2 use (3x+y+3c+i) mod 8. Use seed 103109+7j+i and render both source formats to F32, F16, U16 and U8.
|
||||
|
||||
All 15,360 RGBA pixels in the earlier mixed-storage controls now match. The remaining 121 unequal grayscale pixels differ only in their zero sign. The reference input bridge already clears negative zero in the measured half-gray source. [Earlier texture filtering measurements](#signed-zero-in-floating-point-texture-filtering) · [Original background inputs](#connected-floating-point-pattern-backgrounds)
|
||||
|
||||
### 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.
|
||||
|
||||
@@ -43,6 +43,8 @@ Pattern center culling matched all 356,352 pixels across 204 renders. Background
|
||||
|
||||
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)
|
||||
|
||||
Enlarged RGBA pattern backgrounds now match 71,680/71,680 pixels across 280 independent renders. The earlier source-storage tests still differ at 121 grayscale zero signs. [Background sign measurements](parity-fixes.md#signs-of-zero-in-enlarged-color-backgrounds)
|
||||
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user