mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-25 00:41:00 +01:00
Filter tiled diagonal blur on a sheared grid
This commit is contained in:
1 parent
ab9a2c074a
commit
5a3b3364af
6 files changed
+94
-10
No files matched your search
@@ -4,6 +4,34 @@ use rayon::prelude::*;
|
||||
|
||||
mod mip_window;
|
||||
|
||||
/// A unit-slope shear on a square, periodic image has the same period as the source.
|
||||
pub(super) fn diagonal(input: &Image, reach: f32, horizontal: bool, slope: f32, source: OutputFormat, destination: OutputFormat) -> Option<Image> {
|
||||
let length = input.width();
|
||||
let radius = reach + 0.5;
|
||||
if length != input.height() || !length.is_power_of_two() || !radius.is_finite() || radius > length as f32 {
|
||||
return None;
|
||||
}
|
||||
// Shearing and reduction accumulate before normalization. Use the bounded fallback for HDR overflow.
|
||||
let limit = f32::MAX / (2.0 * radius + 2.0);
|
||||
if !input.data().iter().all(|v| v.abs() <= limit) {
|
||||
return None;
|
||||
}
|
||||
let intermediate = match destination {
|
||||
OutputFormat::U8 | OutputFormat::U16 | OutputFormat::Inherit => OutputFormat::U16,
|
||||
_ => destination,
|
||||
};
|
||||
let shear = |image: &Image, slope: f32, format| {
|
||||
Image::generate_uv(length, length, image.kind(), |u, v| {
|
||||
let uv = if horizontal { [u, v + slope * (u - 0.5)] } else { [u + slope * (v - 0.5), v] };
|
||||
crate::texture_sampler::sample(image, uv, [true; 2], format, None)
|
||||
})
|
||||
};
|
||||
let displaced = crate::output_precision::apply(shear(input, slope, source), intermediate);
|
||||
let filtered = mip_window::filter(&displaced, reach, horizontal, true, intermediate, intermediate, mip_window::Edges::Separate);
|
||||
let filtered = crate::output_precision::apply(filtered, intermediate);
|
||||
Some(shear(&filtered, -slope, intermediate))
|
||||
}
|
||||
|
||||
pub(super) fn short_line(input: &Image, reach: f32, step: [f32; 2], tile: [bool; 2], source_format: OutputFormat) -> Image {
|
||||
let center_weight = 0.5 / (0.5 + reach);
|
||||
let edge_weight = reach * center_weight;
|
||||
@@ -48,7 +76,7 @@ pub(super) fn axis(input: &Image, reach: f32, horizontal: bool, tile: bool, sour
|
||||
// behavior of the extended-precision path when those intermediate sums can overflow.
|
||||
let limit = f32::MAX / (2.0 * radius + 2.0);
|
||||
if length.is_power_of_two() && radius <= length as f32 && input.data().iter().all(|v| v.abs() <= limit) {
|
||||
return mip_window::filter(input, reach, horizontal, tile, source, destination);
|
||||
return mip_window::filter(input, reach, horizontal, tile, source, destination, mip_window::Edges::Paired);
|
||||
}
|
||||
let extent = (radius + 0.5).floor() as i64;
|
||||
let weight = 1.0 / (2.0 * f64::from(radius));
|
||||
|
||||
@@ -12,13 +12,19 @@ struct Tap {
|
||||
weight: f32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) enum Edges {
|
||||
Paired,
|
||||
Separate,
|
||||
}
|
||||
|
||||
impl Tap {
|
||||
fn texel(position: i64, width: u32, weight: f32) -> Self {
|
||||
Self { read: Read::Texel { level: width.ilog2() as usize, position: position.div_euclid(i64::from(width)) }, weight }
|
||||
}
|
||||
}
|
||||
|
||||
fn window(position: u32, length: u32, reach: f32, tile: bool) -> Vec<Tap> {
|
||||
fn window(position: u32, length: u32, reach: f32, tile: bool, edges: Edges) -> Vec<Tap> {
|
||||
let extent = reach.ceil() as i64;
|
||||
let fraction = reach - (extent - 1) as f32;
|
||||
let pair_weight = 1.0 + fraction;
|
||||
@@ -31,7 +37,7 @@ fn window(position: u32, length: u32, reach: f32, tile: bool) -> Vec<Tap> {
|
||||
// Clamped coverage contributes one weighted read of the original border texel.
|
||||
taps.push(Tap::texel(0, 1, fraction + (-left - 1) as f32));
|
||||
left = 0;
|
||||
} else if left % 2 == 0 {
|
||||
} else if matches!(edges, Edges::Paired) && left % 2 == 0 {
|
||||
taps.push(filtered(-extent as f32 + 1.0 / pair_weight));
|
||||
left += 2;
|
||||
} else {
|
||||
@@ -42,7 +48,7 @@ fn window(position: u32, length: u32, reach: f32, tile: bool) -> Vec<Tap> {
|
||||
let weight = fraction + (right - i64::from(length)) as f32;
|
||||
right = i64::from(length) - 1;
|
||||
Tap::texel(right, 1, weight)
|
||||
} else if right % 2 != 0 {
|
||||
} else if matches!(edges, Edges::Paired) && right % 2 != 0 {
|
||||
right -= 2;
|
||||
filtered((extent - 1) as f32 + fraction / pair_weight)
|
||||
} else {
|
||||
@@ -60,7 +66,7 @@ fn window(position: u32, length: u32, reach: f32, tile: bool) -> Vec<Tap> {
|
||||
taps
|
||||
}
|
||||
|
||||
pub(super) fn filter(input: &Image, reach: f32, horizontal: bool, tile: bool, source: OutputFormat, destination: OutputFormat) -> Image {
|
||||
pub(super) fn filter(input: &Image, reach: f32, horizontal: bool, tile: bool, source: OutputFormat, destination: OutputFormat, edges: Edges) -> Image {
|
||||
let radius = 0.5 + reach;
|
||||
let length = if horizontal { input.width() } else { input.height() };
|
||||
let mut levels = vec![input.clone()];
|
||||
@@ -77,7 +83,7 @@ pub(super) fn filter(input: &Image, reach: f32, horizontal: bool, tile: bool, so
|
||||
}
|
||||
let windows: Vec<_> = (0..length)
|
||||
.map(|position| {
|
||||
let mut taps = window(position, length, reach, tile);
|
||||
let mut taps = window(position, length, reach, tile, edges);
|
||||
if horizontal {
|
||||
taps.reverse();
|
||||
}
|
||||
|
||||
@@ -61,6 +61,15 @@ pub(super) fn blur(context: &CookContext, angle: f32) -> CookResult {
|
||||
let span = -u.abs().max(v.abs()).copysign(dominant);
|
||||
return Ok(vec![box_filter::short_line(&input, reach, [u / span, v / span], tile, source_format)]);
|
||||
}
|
||||
if tile == [true; 2] && context.w == input.width() && context.h == input.height() && (angle * 8.0).fract() == 0.0 {
|
||||
// Compare the components before rounding the direction to the sample grid.
|
||||
let (sine, cosine) = f64::from(angle * std::f32::consts::TAU).sin_cos();
|
||||
let horizontal = cosine.abs() > sine.abs();
|
||||
let slope = if sine.is_sign_negative() == cosine.is_sign_negative() { 1.0 } else { -1.0 };
|
||||
if let Some(image) = box_filter::diagonal(&input, reach, horizontal, slope, source_format, context.format) {
|
||||
return Ok(vec![image]);
|
||||
}
|
||||
}
|
||||
let projected_radius = 0.5 + reach;
|
||||
let half_samples = (projected_radius + 0.5).floor().clamp(1.0, MAX_HALF_SAMPLES as f32) as i32;
|
||||
|
||||
|
||||
@@ -309,6 +309,21 @@ fn signed_modular_field(width: u32, height: u32) -> Image {
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_diagonal_blur_filters_between_two_sheared_grids() {
|
||||
let input = [Some(impulse_and_stripes())];
|
||||
for (strength, angle, x, y, expected) in [
|
||||
(4.0, 0.125, 64, 64, [0x3e24087d, 0x3ea4087d, 0x3ea4087d, 0x3e891e8a]),
|
||||
(4.0, 0.125, 64, 65, [0x3da4087d, 0x3ea4087d, 0x3ea4087d, 0x3e532d50]),
|
||||
(10.0, 0.125, 63, 65, [0x00000000, 0x3e0b0e3c, 0x3e0b0e3c, 0x3e2adf33]),
|
||||
(4.75, -0.125, 64, 64, [0x3e0c35e8, 0x3e8c35e8, 0x3e8c35e8, 0x3e66fbdc]),
|
||||
] {
|
||||
let params = [("intensity", Value::Float(strength)), ("angle", Value::Float(angle))];
|
||||
let image = cook("directional_blur", &input, ¶ms, [128; 2], true);
|
||||
assert_eq!(image.get(x, y, false).map(f32::to_bits), expected, "strength {strength}, angle {angle}, ({x},{y})");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_oblique_blur_filters_fractional_taps_and_preserves_the_image_aspect() {
|
||||
let input = [Some(impulse_and_stripes())];
|
||||
|
||||
@@ -531,6 +531,32 @@ The four 64² F32 cube buffers contain 3,187 distinct position tuples, four norm
|
||||
|
||||
The line-light differences begin in its pixel processor. On the signed field, pixel (6,2) stores 1.98653376 in the three color channels in the reference and 1.98652911 in MaterialCraft. For the horizon, the input matches. Its pixel processor differs at two texels in the 64² A case. Pixel (39,49) has reference red .756740689 and MaterialCraft red .753627837. Both discrepancies were resolved in the [panorama seam comparisons](#panoramic-line-light-and-horizon-across-the-seam).
|
||||
|
||||
### Tiled diagonal directional blur
|
||||
|
||||
A centered shear moves a periodic square image onto an axis-aligned sample grid. The blur reduces full interior texel runs at the mip level for their width, with separate fractional endpoint reads. An inverse shear samples the stored result back on the output grid. Both shears filter at the precision of their input. The integer comparisons use U16 intermediates.
|
||||
|
||||
Across 96 original diagonal renders, exact RGBA pixels improved from 35,337 to 1,046,248 out of 1,351,680.
|
||||
|
||||
| Output | Renders | Previous exact | Current exact / compared | Previous maximum | Current maximum |
|
||||
|---|---:|---:|---:|---:|---:|
|
||||
| RGBA8 | 16 | 4,953 | 117,809/163,840 | 200 | 1 |
|
||||
| RGBA16 | 16 | 239 | 97,055/163,840 | 51479 | 1 |
|
||||
| RGBA16F | 16 | 12,188 | 163,827/163,840 | 0.811889648 | 0.00048828125 |
|
||||
| RGBA32F | 48 | 17,957 | 667,557/860,160 | 0.867096573 | 3.57627869e-07 |
|
||||
|
||||
Use a center impulse in R, a vertical stripe through the center in G and a horizontal stripe in B. For U8 and U16 set alpha to H=((97x+43y+7xy) mod 1024)/1023 and round the four channels to the nearest stored integer. Floating inputs use 3H−1.25 for alpha. In order, use turns .125, −.125, .375, .625 and .875 on F32, then .125 on U8, U16 and F16. For each, take (radius, square side) pairs (2,32), (2.375,128), (3.125,64), (5.375,128), (11.25,64), (7.5,256). Set intensity to 256×radius/side and seed to 479+37j+19k for zero-based angle/input index j and radius index k. Use all four output formats at radii 3.125 and 5.375, and F32 elsewhere.
|
||||
|
||||
At .125 and −.125 turns with F32 input and F16 output, all 40,960 pixels match. Differences in the other diagonal renders include fractional channel rounding and the projection of angles beyond the first quadrant. The untiled .125, tiled .0625, and tiled .21875 controls retain larger differences. Across these 36 renders the maxima are 192 U8 levels, 49,199 U16 levels, 1.095703125 in F16 and 1.09612465 in F32.
|
||||
|
||||
| Inventory root | Renders | Exact / compared | Maximum U8 / U16 | Maximum F16 / F32 |
|
||||
|---|---:|---:|---:|---:|
|
||||
| `anisotropic_blur.sbs/anisotropic_blur` | 8 | 112,190/115,712 | 1 / 0 | 0 / 0 |
|
||||
| `anisotropic_blur.sbs/anisotropic_blur_grayscale` | 8 | 112,563/115,712 | 1 / 0 | 0 / 0 |
|
||||
| `blur_hq.sbs/blur_hq` | 9 | 40,496/132,096 | 8 / 0 | 0 / 0 |
|
||||
| `blur_hq.sbs/blur_hq_grayscale` | 9 | 96,534/132,096 | 4 / 0 | 0 / 0 |
|
||||
|
||||
The [four-format inventory](#four-format-inventory-comparison) gives the changed inputs and settings. Each of the four roots matches on its independent F32 input and its changed 64² four-format matrix. The differences above are in archived U8 cases. Six previously exact roots with reachable directional filters also matched all 790,528 pixels in 52 renders, maximum zero.
|
||||
|
||||
### Short oblique directional blur
|
||||
|
||||
| Direct footprint | Renders | Exact pixels before | Exact pixels after / compared | Maximum error after |
|
||||
|
||||
@@ -72,8 +72,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
|
||||
| [3d_worley_noise.sbs/3D_worley_noise](../assets/sbs/3d_worley_noise.sbs) | 1 | No | [Parity · 34 renders, 312,832 exact pixels](parity-fixes.md#per-pixel-random-initialization) |
|
||||
| [alveolus.sbs/alveolus](../assets/sbs/alveolus.sbs) | 0 | Yes | [In progress · 300,544/302,336 pixels across 26 renders](parity-fixes.md#radial-stamp-profiles-and-pattern-generators) |
|
||||
| [angle_to_vector.sbs/angle_to_vector](../assets/sbs/angle_to_vector.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [anisotropic_blur.sbs/anisotropic_blur](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [In progress · 226,638/269,568 exact pixels, wide blur differs](parity-fixes.md#short-oblique-directional-blur) |
|
||||
| [anisotropic_blur.sbs/anisotropic_blur_grayscale](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [In progress · 232,976/269,568 exact pixels, wide blur differs](parity-fixes.md#short-oblique-directional-blur) |
|
||||
| [anisotropic_blur.sbs/anisotropic_blur](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [In progress · 112,190/115,712 pixels in 8 renders](parity-fixes.md#tiled-diagonal-directional-blur) |
|
||||
| [anisotropic_blur.sbs/anisotropic_blur_grayscale](../assets/sbs/anisotropic_blur.sbs) | 1 | No | [In progress · 112,563/115,712 pixels in 8 renders](parity-fixes.md#tiled-diagonal-directional-blur) |
|
||||
| [atlas_spliter.sbs/crop_and_transform_sub](../assets/sbs/atlas_spliter.sbs) | 7 | Yes | [Parity · 37 wired cases, 378,880 exact pixels](parity-fixes.md#runtime-pixel-sine-cosine-and-atlas-rotation) |
|
||||
| [atlas_spliter.sbs/max_ba](../assets/sbs/atlas_spliter.sbs) | 1 | Yes | [Parity: 3 cases](parity-fixes.md#verified-cases) |
|
||||
| [auto_crop.sbs/min_max](../assets/sbs/auto_crop.sbs) | 1 | Yes | [Parity: 3 cases](parity-fixes.md#verified-cases) |
|
||||
@@ -106,8 +106,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
|
||||
| [blend_switch.sbs/multi_switch_grayscale](../assets/sbs/blend_switch.sbs) | 20 | No | [Parity: 5 cases](parity-fixes.md#verified-cases) |
|
||||
| [blend_switch.sbs/switch](../assets/sbs/blend_switch.sbs) | 2 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [blend_switch.sbs/switch_grayscale](../assets/sbs/blend_switch.sbs) | 2 | No | [Parity: 6 cases](parity-fixes.md#verified-cases) |
|
||||
| [blur_hq.sbs/blur_hq](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 63,731/171,264 exact pixels, wide blur differs](parity-fixes.md#short-oblique-directional-blur) |
|
||||
| [blur_hq.sbs/blur_hq_grayscale](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 125,647/171,264 exact pixels, wide blur differs](parity-fixes.md#short-oblique-directional-blur) |
|
||||
| [blur_hq.sbs/blur_hq](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 40,496/132,096 pixels in 9 renders](parity-fixes.md#tiled-diagonal-directional-blur) |
|
||||
| [blur_hq.sbs/blur_hq_grayscale](../assets/sbs/blur_hq.sbs) | 1 | No | [In progress · 96,534/132,096 pixels in 9 renders](parity-fixes.md#tiled-diagonal-directional-blur) |
|
||||
| [bnw_spots_1.sbs/bnw_spots_1](../assets/sbs/bnw_spots_1.sbs) | 0 | Yes | [In progress · 19 renders, 131,316/172,288 exact pixels](parity-fixes.md#filtering-unequal-inputs-to-a-blend) |
|
||||
| [bnw_spots_3.sbs/bnw_spots_3](../assets/sbs/bnw_spots_3.sbs) | 0 | Yes | [In progress · 19 renders, 124,148/172,288 exact pixels](parity-fixes.md#filtering-unequal-inputs-to-a-blend) |
|
||||
| [brick_1.sbs/brick_1](../assets/sbs/brick_1.sbs) | 0 | Yes | [In progress · 170,632/172,288 pixels, edge coordinates before U16 storage](parity-fixes.md#brick-edge-distances-and-storage-precision) |
|
||||
|
||||
Reference in new issue
Block a user