mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-28 17:19:17 +01:00
Match diagonal mip selection for rotated rectangular bitmap stamps
This commit is contained in:
1 parent
558f5a5cb2
commit
7ff64fa5d0
4 files changed
+116
-13
No files matched your search
@@ -71,25 +71,55 @@ impl<'a> Sampler<'a> {
|
||||
}
|
||||
|
||||
fn mip_level(image: &Image, gradients: [[f32; 2]; 2]) -> usize {
|
||||
let [dx, dy] = gradients.map(|[u, v]| [u * image.width() as f32, v * image.height() as f32].map(|g| f32::from_bits(g.to_bits() & !0x3fff)));
|
||||
// The footprint uses nine fraction bits per derivative. The smaller component
|
||||
// contributes 11/32 at its own exponent before alignment with the larger one.
|
||||
let [dx, dy] = gradients.map(|[u, v]| [u * image.width() as f32, v * image.height() as f32].map(truncate_derivative));
|
||||
let footprint = |[x, y]: [f32; 2]| {
|
||||
let minor = x.abs().min(y.abs()).to_bits();
|
||||
let mantissa = 512 + ((minor >> 14) & 511);
|
||||
let weighted = (11 * mantissa + 4) >> 5;
|
||||
let major = f32::from_bits(x.abs().max(y.abs()).to_bits() & !0x3fff);
|
||||
major + f32::from_bits(minor & 0x7f80_0000) * (weighted as f32 / 512.0)
|
||||
let (mantissa, unit) = derivative_parts(x.abs().min(y.abs()));
|
||||
x.abs().max(y.abs()) + ((11 * mantissa + 4) >> 5) as f32 * unit
|
||||
};
|
||||
let add = [dx[0] + dy[0], dx[1] + dy[1]];
|
||||
let sub = [dx[0] - dy[0], dx[1] - dy[1]];
|
||||
let diagonal = footprint(add).max(footprint(sub)) * (11.0 / 16.0);
|
||||
let [u_sum, u_diff] = diagonal_components(dx[0], dy[0]);
|
||||
let [v_sum, v_diff] = diagonal_components(dx[1], dy[1]);
|
||||
let diagonal = diagonal_footprint(u_sum, v_sum).max(diagonal_footprint(u_diff, v_diff));
|
||||
let bits = footprint(dx).max(footprint(dy)).max(diagonal).max(1.0).to_bits();
|
||||
// The first measured half-level boundary is 1 + 53/128 and repeats at each power of two.
|
||||
let exponent = (bits >> 23).saturating_sub(127);
|
||||
(exponent + u32::from(bits & 0x7f_ffff >= 53 << 16)).min(13) as usize
|
||||
}
|
||||
|
||||
fn truncate_derivative(value: f32) -> f32 {
|
||||
f32::from_bits(value.to_bits() & !0x3fff)
|
||||
}
|
||||
|
||||
// The stored derivative has nine fraction bits. The unit belongs to the input exponent.
|
||||
fn derivative_parts(magnitude: f32) -> (u32, f32) {
|
||||
let bits = magnitude.to_bits();
|
||||
(512 + ((bits >> 14) & 511), f32::from_bits(bits & 0x7f80_0000) / 512.0)
|
||||
}
|
||||
|
||||
fn diagonal_components(a: f32, b: f32) -> [f32; 2] {
|
||||
let exponent = |v: f32| (v.to_bits() >> 23) & 255;
|
||||
let common = exponent(a).max(exponent(b));
|
||||
// Keep one guard bit below the larger component's nine fraction bits before
|
||||
// adding or subtracting. Cancellation can expose that bit in the diagonal.
|
||||
let align = |v| match common - exponent(v) {
|
||||
0 | 1 => v,
|
||||
gap @ 2..=10 => f32::from_bits(v.to_bits() & (u32::MAX << (13 + gap))),
|
||||
_ => 0.0,
|
||||
};
|
||||
let (a, b) = (align(a), align(b));
|
||||
[a + b, a - b]
|
||||
}
|
||||
|
||||
fn diagonal_footprint(u: f32, v: f32) -> f32 {
|
||||
let major = u.abs().max(v.abs());
|
||||
let (_, major_unit) = derivative_parts(major);
|
||||
let (minor_mantissa, minor_unit) = derivative_parts(u.abs().min(v.abs()));
|
||||
// Scale each component before summing. The major rounds down at the weighted
|
||||
// exponent after a quarter input unit of bias. The minor also includes the 11/32 axis weight.
|
||||
let major = truncate_derivative(truncate_derivative(major) * (11.0 / 16.0) + major_unit * 0.25);
|
||||
let minor = ((121 * minor_mantissa + 32) >> 9) as f32 * minor_unit;
|
||||
major + minor
|
||||
}
|
||||
|
||||
fn sample(image: &Image, local: [f32; 2], format: OutputFormat, nearest: bool) -> [f32; 4] {
|
||||
let uv = local.map(|axis| axis * 0.5 + 0.5);
|
||||
if nearest {
|
||||
@@ -98,3 +128,17 @@ fn sample(image: &Image, local: [f32; 2], format: OutputFormat, nearest: bool) -
|
||||
texture_sampler::sample(image, uv, [false; 2], format, Some([0.0; 4]))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bitmap_diagonal_gradients_cross_half_levels_in_both_directions() {
|
||||
let image = Image::gray(32, 32, |_, _| 0.0);
|
||||
for (texel_steps, expected) in [([[1864, -2248], [1874, 861]], 1), ([[1884, 649], [1884, 649]], 0), ([[1814, -841], [-2192, -1450]], 0)] {
|
||||
let gradients = texel_steps.map(|pair| pair.map(|v| v as f32 / (2048.0 * 32.0)));
|
||||
assert_eq!(mip_level(&image, gradients), expected, "{texel_steps:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1764,6 +1764,22 @@ fn automatic_bitmap_mips_use_texel_gradients_from_neighboring_pixels() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rectangular_bitmap_rotation_crosses_the_first_mip_boundary() {
|
||||
use materialcraft_core::OutputFormat;
|
||||
|
||||
let input = craft_texture::Image::gray(64, 32, |x, y| {
|
||||
let byte = (17 * x + 59 * y + 9 * x * y + 7) % 251 + 4 * u32::from((x + 3 * y) % 7 < 3);
|
||||
byte as f32 / 255.0
|
||||
})
|
||||
.with_integer_sampling(true);
|
||||
for (numerator, expected) in [(40, 0x3f1a_eb9b), (41, 0x3f1c_699c), (42, 0x3f0e_ca8f), (-41, 0x3eff_d300)] {
|
||||
let stamp = [("patternsize", Value::Float2([1.0; 2])), ("patternrotation", Value::Float(numerator as f32 / 512.0))];
|
||||
let image = render_bitmap(input.clone(), OutputFormat::U8, [64, 32], &stamp, &[("color_mode", Value::Bool(false))]);
|
||||
assert_eq!(image.get_gray(32, 16, false).to_bits(), expected, "rotation {numerator}/512");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bitmap_stamp_filters_include_the_last_interpolated_texel() {
|
||||
use materialcraft_core::OutputFormat;
|
||||
|
||||
@@ -1102,6 +1102,49 @@ For the drawing-depth controls, keep traversal, connections and expressions that
|
||||
|
||||
The individual depths 4, 5 and 6 each match all 7,680 U16 pixels. F32 distinguishes differences within each of those depths. At the 256² default final output, the five reference renders vary at 240 pixels and agree on 57 additional pixels that differ from MaterialCraft. Both effects remain open.
|
||||
|
||||
### Diagonal mip selection on rotated rectangular bitmaps
|
||||
|
||||
The first different stage of Radial Blur Grayscale was the automatic bitmap filter in its first pattern graph. A 32 × 64 modular-and-impulse input at default parameters gave 345/2,048 exact U16 pattern pixels, maximum 405 levels. The downstream blend gave 527/2,048, maximum 138 U16 levels. At the F32 output 527/2,048 matched, maximum .00210574269. The input and all three transformed fields already matched.
|
||||
|
||||
Retain the graph’s original sample count and per-stamp opacity, limiting only the number of visits. With 64 samples, .125 turns and the center offset (0,0), the first 41 draws matched 2,048/2,048 pixels. Including the 42nd draw, rotated by 41/512 turns, matched 338/2,048 with maximum 404 U16 levels. With the new filter both prefixes and the complete draw match. A separate one-stamp graph reproduces the transition at either orientation of a 2:1 rectangle. At ±41/512 turns the reference automatic filter selects the same level as bilinear sampling. At 42/512 it selects a coarser level.
|
||||
|
||||
For a 64 × 32 original byte image use ((17x+59y+9xy+7) mod 251) + 4[(x+3y) mod 7 < 3]. Use one full-size, centered grayscale bitmap stamp, tiling off, opacity one, and read F32 at (32,16). At rotations 40/512, 41/512, 42/512 and −41/512 the stored bits are `3f1aeb9b`, `3f1c699c`, `3f0eca8f` and `3effd300`. For the wider sweep use 32 × 64, 64 × 32, 64² and 32 × 128. At the first size use the matching byte output of the first transform in the modular-and-impulse graph. At the three other sizes replace 7 in the byte field with 7j, with j = 1, 2, 3. At all four sizes render turns (39,40,41,42,43)/512, 41/512 ± 2⁻¹⁵, 41/512 ± 2⁻¹², −41/512, 0, .125 and .25−41/512. Automatic and bilinear each match **159,744/159,744** F32 pixels across 52 settings.
|
||||
|
||||
The [two-by-two pixel group](#automatic-bitmap-stamp-levels-at-neighboring-pixels) supplies the two screen derivatives in texels. Truncate each signed component to nine fraction bits. For each texture coordinate, align the two screen derivatives with one extra fraction bit at the larger exponent before taking their sum and difference. Evaluate both diagonals. Weight the larger magnitude by 11/16, add a quarter of its input unit and truncate to nine fraction bits at the resulting exponent. For the smaller magnitude with nine-bit integer significand m (including its implicit leading bit, scaled by 512), use floor((121m+32)/512) times its input unit. The maximum of both axial and both diagonal footprints selects the level at the existing half-level thresholds.
|
||||
|
||||
Generate a mip chain with a constant value equal to its level number and read the selected level using explicit gradients. The three fixed regression matrices, written in texels divided by 2048, are ((1864,−2248),(1874,861)), ((1884,649),(1884,649)) and ((1814,−841),(−2192,−1450)). Their measured levels are 1, 0 and 0, respectively. In the independent dense series draw four F32 components uniformly from −1 to 1 with random seed 763451. Use 131,072 matrices in order. For the first 32,768 make the two screen derivatives equal, and for the next 32,768 make them opposites. For the following 16,384 set both second components to zero. For the next 16,384 set the second screen derivative to zero. Scale each matrix by 2ᵉ × 1.4140625 × u divided by its approximate unquantized footprint, with e a uniform integer from 0 through 6 and u uniform from .997 to 1.003. The unquantized footprint uses max + 11/32 min for each axis and 11/16 of that norm for both diagonals.
|
||||
|
||||
| Graphics-device level choices | Exact before / compared | Exact now / compared | Maximum level error now |
|
||||
|---|---:|---:|---:|
|
||||
| Four derivative families from the earlier stress test | 64,127/65,536 | 65,536/65,536 | 0 |
|
||||
| Independent near-threshold perturbations | 16,182/16,384 | 16,384/16,384 | 0 |
|
||||
| Independent signed and collinear matrices | 124,877/131,072 | 131,072/131,072 | 0 |
|
||||
|
||||
The 139 earlier automatic stamp settings retained 2,105,344/2,105,344 exact pixels and the independent 180-setting boundary series retained 2,457,600/2,457,600. All 506,880/506,880 stored pixels at the ten observed stages across 23 radial graph renders matched, maximum zero.
|
||||
|
||||
For the whole root, repeat the archived defaults and controls, the four 64² formats, and the [seven-size modular input series](#modifying-a-256-entry-color-palette). At the five nondefault sizes in that series, use (samples, angle, center) in this order. Use (7,−.21875,(−.21875,.13671875)), (15,.15625,(.15625,−.09765625)), (11,.078125,(.078125,−.048828125)), (19,−.34375,(−.34375,.21484375)) and (13,.3046875,(.3046875,−.1904296875)). For the 32 × 64 modular-and-impulse field write 1 at (2,43), .625 at (18,2), and .3125 where x mod 13 = 0 and y mod 3 ≠ 0. Elsewhere write ((11x+29y+7xy) mod 241)/960. Use defaults, then (samples,angle) = (1,.375), (2,−.21875), (3,.25), (7,.28125), (11,−.1875), (16,.5625), (29,−.421875), (47,.15625). Use center (−.21875,.15625) for the first four changes and (.3125,−.09375) for the last four. Use seed 733460 plus the setting index and F32 throughout, with U8, U16 and F16 also measured at 2 and 16 samples.
|
||||
|
||||
For the independent ten-size series below, use seed S = 763819 + 61j and a procedural F32 input. For source dimensions W × H write .5[((41x+59y+13xy+S) mod 257)/256] + .3125[(floor(x/5)+floor(y/7)) mod 2] + .1875[(3x+13y+S) mod 17 < 4]. Set the texel at x = (floor(W/3)+2y) mod W to one in every row. Use a 32 × 16 source at j = 4 and 7, and equal source and output dimensions elsewhere. Render U16 and F32 throughout, also U8 and F16 at j = 1, 6 and 9.
|
||||
|
||||
| j | Output dimensions | Samples | Angle in turns | Center offset |
|
||||
|---:|---|---:|---:|---|
|
||||
| 0 | 16 × 32 | 64 | 0.125 | (0, 0) |
|
||||
| 1 | 32 × 64 | 64 | -0.125 | (-0.1875, 0.28125) |
|
||||
| 2 | 64 × 32 | 64 | 0.125 | (0.34375, -0.125) |
|
||||
| 3 | 128 × 64 | 128 | -0.125 | (0, 0) |
|
||||
| 4 | 64 × 128 | 48 | 0.15625 | (-0.1875, 0.28125) |
|
||||
| 5 | 128 × 128 | 64 | 0.125 | (0, 0) |
|
||||
| 6 | 128 × 256 | 128 | 0.125 | (0, 0) |
|
||||
| 7 | 256 × 64 | 63 | 0.304688 | (-0.1875, 0.28125) |
|
||||
| 8 | 32 × 16 | 65 | 0.125 | (0.34375, -0.125) |
|
||||
| 9 | 64 × 256 | 97 | -0.421875 | (0, 0) |
|
||||
|
||||
| Root (inventory index) | Distinct settings | U8 exact / total · max | U16 exact / total · max | F16 exact / total · max | F32 exact / total · max |
|
||||
|---|---:|---:|---:|---:|---:|
|
||||
| Radial Blur Grayscale (441) | 80 | 82,944/82,944 · 0 | 259,840/259,840 · 0 | 84,992/84,992 · 0 | 163,840/163,840 · 0 |
|
||||
|
||||
All **591,616/591,616** pixels matched across 80 distinct whole-root settings. Save the seven-size graph and repeat all 20 renders. Save it a second time and repeat all seven F32 settings and the other three formats at 64². Reopen the modular-and-impulse graph for all 15 settings. Reopen the independent ten-size graph for all ten F32 outputs plus the other three formats at 128 × 256. All **385,792/385,792** pixels matched across 58 reopened renders, maximum zero. Each application reproduced all its original buffers. Both new regressions failed before the change and passed afterward. All 342 release node tests, formatting, strict workspace Clippy, dependency layers and WebAssembly passed. Across seven alternating single-core release runs, median cook time for the default 32 × 64 field went from 168.7 to 166.5 ms. The 128 × 256 field at 128 samples went from 5038 to 5070 ms.
|
||||
|
||||
## 2026-11-20 root comparisons
|
||||
|
||||
### Hue, saturation, lightness and Replace Color
|
||||
|
||||
@@ -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 **438 Parity**, **108 In progress** and **0 Too hard**. The November 17–21 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) · [November 19 measurements](parity-fixes.md#2026-11-19-root-comparisons) · [November 20 measurements](parity-fixes.md#2026-11-20-root-comparisons) · [November 21 measurements](parity-fixes.md#2026-11-21-root-comparisons)
|
||||
The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **439 Parity**, **107 In progress** and **0 Too hard**. The November 17–21 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) · [November 19 measurements](parity-fixes.md#2026-11-19-root-comparisons) · [November 20 measurements](parity-fixes.md#2026-11-20-root-comparisons) · [November 21 measurements](parity-fixes.md#2026-11-21-root-comparisons)
|
||||
|
||||
[Pattern tiling axes and copy bounds across 215 renders](parity-fixes.md#tiling-axes-and-pattern-copy-bounds) · [Grayscale sampler measurements](parity-fixes.md#grayscale-sampling-of-color-inputs) · [Value sampling across 240 typed comparisons](parity-fixes.md#value-processor-texture-filtering) · [Five saved graphs reopened in the reference](parity-fixes.md#saved-filter-graphs-and-scalar-levels-controls)
|
||||
|
||||
@@ -558,7 +558,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug
|
||||
| [quantize_color.sbs/kmeans_iteration](../assets/sbs/quantize_color.sbs) | 2 | Yes | [In progress · 77,776/77,824 whole pixels, first differing stages measured](parity-fixes.md#tile-weave-splatter-and-fiber-stage-comparisons) |
|
||||
| [quantize_color.sbs/modify_color_palette_sub](../assets/sbs/quantize_color.sbs) | 2 | Yes | [Parity · 15,104/15,104 palette entries across 59 distinct settings](parity-fixes.md#modifying-a-256-entry-color-palette) · [Earlier palette measurement](parity-fixes.md#height-distance-texture-mapping-skew-and-weave) |
|
||||
| [quantize_color.sbs/view_color_palette_sub](../assets/sbs/quantize_color.sbs) | 2 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [radial_blur.sbs/radial_blur_grayscale](../assets/sbs/radial_blur.sbs) | 1 | No | [In progress · 121,514/142,592 whole pixels, first differing stages measured](parity-fixes.md#tile-weave-splatter-and-fiber-stage-comparisons) · [both isolated branches match in all six cases per shape](parity-fixes.md#tile-weave-splatter-and-fiber-stage-comparisons) |
|
||||
| [radial_blur.sbs/radial_blur_grayscale](../assets/sbs/radial_blur.sbs) | 1 | No | [Parity · 591,616/591,616 pixels across 80 distinct settings](parity-fixes.md#diagonal-mip-selection-on-rotated-rectangular-bitmaps) · [Earlier radial measurements](parity-fixes.md#tile-weave-splatter-and-fiber-stage-comparisons) · [both isolated branches match in all six cases per shape](parity-fixes.md#tile-weave-splatter-and-fiber-stage-comparisons) |
|
||||
| [replace_color.sbs/replace_color](../assets/sbs/replace_color.sbs) | 1 | No | [In progress · 136,480/142,592 exact pixels in 19 whole renders](parity-fixes.md#hue-saturation-lightness-and-replace-color) |
|
||||
| [rgb-a_merge.sbs/rgb-a_merge](../assets/sbs/rgb-a_merge.sbs) | 2 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [rgb-a_split.sbs/rgb-a_split](../assets/sbs/rgb-a_split.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
|
||||
Reference in new issue
Block a user