mirror of
https://github.com/storytold/materialcraft.git
synced 2026-12-01 10:34:01 +01:00
Reduce aligned texel runs in wide blur windows
This commit is contained in:
1 parent
11a3ac3c6e
commit
aec776f142
7 files changed
+177
-10
No files matched your search
@@ -17,12 +17,17 @@ fn blur(context: &CookContext) -> CookResult {
|
||||
const BASE_RESOLUTION: f32 = 256.0;
|
||||
let radius = context.f("intensity").max(0.0) * context.w.min(context.h) as f32 / BASE_RESOLUTION;
|
||||
let tile = context.tiling_axes();
|
||||
let source_format = context.input_formats.first().copied().unwrap_or(materialcraft_core::OutputFormat::F32);
|
||||
|
||||
if radius > 0.5 && radius <= 2.5 {
|
||||
let source_format = context.input_formats.first().copied().unwrap_or(materialcraft_core::OutputFormat::F32);
|
||||
let horizontal = box_filter::short_axis(&input, radius, [1, 0], tile, source_format);
|
||||
let stored = crate::output_precision::apply(horizontal, context.format);
|
||||
return Ok(vec![box_filter::short_axis(&stored, radius, [0, -1], tile, context.format)]);
|
||||
}
|
||||
Ok(vec![box_filter::axis(&box_filter::axis(&input, radius, true, tile[0]), radius, false, tile[1])])
|
||||
if radius <= 0.5 || !radius.is_finite() {
|
||||
return Ok(vec![input]);
|
||||
}
|
||||
let horizontal = box_filter::axis(&input, radius, true, tile[0], source_format, context.format);
|
||||
let stored = crate::output_precision::apply(horizontal, context.format);
|
||||
Ok(vec![box_filter::axis(&stored, radius, false, tile[1], context.format, context.format)])
|
||||
}
|
||||
@@ -2,6 +2,8 @@ use craft_texture::{Image, Limits, MAX_SIDE};
|
||||
use materialcraft_core::OutputFormat;
|
||||
use rayon::prelude::*;
|
||||
|
||||
mod mip_window;
|
||||
|
||||
pub(super) fn short_axis(input: &Image, radius: f32, step: [i64; 2], tile: [bool; 2], source_format: OutputFormat) -> Image {
|
||||
let center_weight = 0.5 / radius;
|
||||
let edge_weight = (radius - 0.5) * center_weight;
|
||||
@@ -32,13 +34,20 @@ pub(super) fn short_axis(input: &Image, radius: f32, step: [i64; 2], tile: [bool
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn axis(input: &Image, radius: f32, horizontal: bool, tile: bool) -> Image {
|
||||
pub(super) fn axis(input: &Image, radius: f32, horizontal: bool, tile: bool, source: OutputFormat, destination: OutputFormat) -> Image {
|
||||
if !radius.is_finite() || radius <= 0.5 {
|
||||
return input.clone();
|
||||
}
|
||||
|
||||
// Bound work for hostile strengths to the largest supported image dimension
|
||||
let radius = radius.min(MAX_SIDE as f32);
|
||||
let length = if horizontal { input.width() } else { input.height() };
|
||||
// Wide windows sum before normalization. Keep the finite HDR and non-finite
|
||||
// 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, radius, horizontal, tile, source, destination);
|
||||
}
|
||||
let extent = (radius + 0.5).floor() as i64;
|
||||
let weight = 1.0 / (2.0 * f64::from(radius));
|
||||
let edge_weight = (0.5 - (extent as f64 - 0.5) * weight).max(0.0);
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
use crate::{output_precision, texture_sampler};
|
||||
use craft_texture::Image;
|
||||
use materialcraft_core::OutputFormat;
|
||||
|
||||
enum Read {
|
||||
Texel { level: usize, position: i64 },
|
||||
Filtered(f32),
|
||||
}
|
||||
|
||||
struct Tap {
|
||||
read: Read,
|
||||
weight: f32,
|
||||
}
|
||||
|
||||
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, radius: f32, tile: bool) -> Vec<Tap> {
|
||||
let extent = (radius - 0.5).ceil() as i64;
|
||||
let fraction = radius - (extent as f32 - 0.5);
|
||||
let pair_weight = 1.0 + fraction;
|
||||
let center = (position as f32 + 0.5) / length as f32;
|
||||
let filtered = |offset| Tap { read: Read::Filtered(center + offset / length as f32), weight: pair_weight };
|
||||
let mut left = i64::from(position) - extent;
|
||||
let mut right = i64::from(position) + extent;
|
||||
let mut taps = Vec::new();
|
||||
if !tile && left < 0 {
|
||||
// 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 {
|
||||
taps.push(filtered(-extent as f32 + 1.0 / pair_weight));
|
||||
left += 2;
|
||||
} else {
|
||||
taps.push(Tap::texel(left, 1, fraction));
|
||||
left += 1;
|
||||
}
|
||||
let last = if !tile && right >= i64::from(length) {
|
||||
let weight = fraction + (right - i64::from(length)) as f32;
|
||||
right = i64::from(length) - 1;
|
||||
Tap::texel(right, 1, weight)
|
||||
} else if right % 2 != 0 {
|
||||
right -= 2;
|
||||
filtered((extent - 1) as f32 + fraction / pair_weight)
|
||||
} else {
|
||||
right -= 1;
|
||||
Tap::texel(right + 1, 1, fraction)
|
||||
};
|
||||
while left <= right {
|
||||
// An aligned run of 2^k texels is a single stored texel at level k.
|
||||
let available = (right - left + 1).min(i64::from(length)) as u32;
|
||||
let width = 1 << available.ilog2().min(left.trailing_zeros());
|
||||
taps.push(Tap::texel(left, width, width as f32));
|
||||
left += i64::from(width);
|
||||
}
|
||||
taps.push(last);
|
||||
taps
|
||||
}
|
||||
|
||||
pub(super) fn filter(input: &Image, radius: f32, horizontal: bool, tile: bool, source: OutputFormat, destination: OutputFormat) -> Image {
|
||||
let length = if horizontal { input.width() } else { input.height() };
|
||||
let mut levels = vec![input.clone()];
|
||||
let mut format = source;
|
||||
let mut size = length;
|
||||
let max_width = ((radius * 2.0).ceil() as u32).min(length);
|
||||
for _ in 0..max_width.ilog2() {
|
||||
let Some(previous) = levels.last() else { break };
|
||||
size /= 2;
|
||||
let (w, h) = if horizontal { (size, input.height()) } else { (input.width(), size) };
|
||||
let reduced = texture_sampler::resize_input_level(previous, w, h, format, [true; 2]);
|
||||
levels.push(output_precision::apply(reduced, destination));
|
||||
format = destination;
|
||||
}
|
||||
let windows: Vec<_> = (0..length)
|
||||
.map(|position| {
|
||||
let mut taps = window(position, length, radius, tile);
|
||||
if horizontal {
|
||||
taps.reverse();
|
||||
}
|
||||
taps
|
||||
})
|
||||
.collect();
|
||||
let scale = 0.5 / radius;
|
||||
Image::generate(input.width(), input.height(), input.kind(), |x, y| {
|
||||
let (position, across, other_size) = if horizontal { (x, y, input.height()) } else { (y, x, input.width()) };
|
||||
let other_uv = (across as f32 + 0.5) / other_size as f32;
|
||||
let read = |tap: &Tap| match tap.read {
|
||||
Read::Texel { level, position } => {
|
||||
let (x, y) = if horizontal { (position, i64::from(across)) } else { (i64::from(across), position) };
|
||||
levels.get(level).unwrap_or(input).get(x, y, true)
|
||||
}
|
||||
Read::Filtered(uv) => {
|
||||
let xy = if horizontal { [uv, other_uv] } else { [other_uv, uv] };
|
||||
texture_sampler::sample(input, xy, [tile; 2], source, None)
|
||||
}
|
||||
};
|
||||
let Some((first, rest)) = windows.get(position as usize).and_then(|taps| taps.split_first()) else {
|
||||
return input.get(i64::from(x), i64::from(y), false);
|
||||
};
|
||||
let mut sum = read(first).map(|v| v * first.weight);
|
||||
for tap in rest {
|
||||
for (total, v) in sum.iter_mut().zip(read(tap)) {
|
||||
*total = v.mul_add(tap.weight, *total);
|
||||
}
|
||||
}
|
||||
sum.map(|v| v * scale)
|
||||
})
|
||||
}
|
||||
@@ -32,12 +32,12 @@ pub(super) fn blur(context: &CookContext, angle: f32) -> CookResult {
|
||||
let dy = -dy;
|
||||
let tile = context.tiling_axes();
|
||||
if dy == 0.0 {
|
||||
let source_format = context.input_formats.first().copied().unwrap_or(materialcraft_core::OutputFormat::F32);
|
||||
if radius <= 2.5 {
|
||||
let source_format = context.input_formats.first().copied().unwrap_or(materialcraft_core::OutputFormat::F32);
|
||||
let step = if radius <= 1.5 { [-1, 0] } else { [1, 0] };
|
||||
return Ok(vec![box_filter::short_axis(&input, radius, step, tile, source_format)]);
|
||||
}
|
||||
return Ok(vec![box_filter::axis(&input, radius, true, tile[0])]);
|
||||
return Ok(vec![box_filter::axis(&input, radius, true, tile[0], source_format, context.format)]);
|
||||
}
|
||||
|
||||
let major = dx.abs().max(dy.abs());
|
||||
|
||||
@@ -240,6 +240,30 @@ fn five_texel_blurs_filter_the_outer_pairs_before_accumulating() {
|
||||
assert_eq!(separable.get(63, 64, false).map(f32::to_bits), [0x3d804000, 0x3e804000, 0x3e800000, 0x3e1e66da]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wide_blur_reduces_aligned_runs_at_the_destination_precision() {
|
||||
let field = Image::generate(64, 128, Kind::Color, |x, y| {
|
||||
std::array::from_fn(|c| {
|
||||
let c = c as u32;
|
||||
let level = (37 * x + 71 * y + 11 * x * y + 47 * c + 3 * x * c) % 1021;
|
||||
(3.0 * (f64::from(level) / 1020.0) - 1.125) as f32
|
||||
})
|
||||
});
|
||||
let input = [Some(field)];
|
||||
let params = [("intensity", Value::Float(17.5))];
|
||||
for (format, expected) in [
|
||||
(OutputFormat::U8, [0x3ec0c0c1, 0x3edadadb, 0x3ec8c8c9, 0x3ec2c2c3]),
|
||||
(OutputFormat::U16, [0x3ec2e0c3, 0x3edb88dc, 0x3ec9f4ca, 0x3ec3f0c4]),
|
||||
(OutputFormat::F16, [0x3ebbc000, 0x3ed24000, 0x3ec30000, 0x3eb66000]),
|
||||
(OutputFormat::F32, [0x3ebbdad5, 0x3ed25b4e, 0x3ec322fc, 0x3eb6834f]),
|
||||
] {
|
||||
let output = cook_format("blur", &input, ¶ms, [64, 128], true, format);
|
||||
assert_eq!(materialcraft_nodes::output_precision::apply(output, format).get(11, 37, false).map(f32::to_bits), expected);
|
||||
}
|
||||
let clamped = cook("blur", &input, ¶ms, [64, 128], false);
|
||||
assert_eq!(clamped.get(0, 1, false).map(f32::to_bits), [0xbf1b2df1, 0xbeeaad75, 0xbe9eff07, 0xbe26a135]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blur_preserves_finite_hdr_constant() {
|
||||
let input = Image::filled(16, 16, Kind::Gray, [1.0e38; 4]);
|
||||
@@ -255,9 +279,7 @@ fn directional_blur_uses_full_radius_and_fractional_endpoints() {
|
||||
|
||||
let output = cook("directional_blur", &[Some(input)], &[("intensity", Value::Float(4.0))], [256, 256], true);
|
||||
|
||||
for offset in -4..=4 {
|
||||
let expected = if offset == -4 || offset == 4 { 0.0625 } else { 0.125 };
|
||||
|
||||
for (offset, expected) in (-4..=4).zip([0.0625, 513.0 / 4096.0, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 255.0 / 4096.0]) {
|
||||
assert_eq!(output.get_gray(128 + offset, 128, false), expected, "offset {offset}");
|
||||
}
|
||||
assert_eq!(output.get_gray(128, 129, false), 0.0);
|
||||
|
||||
@@ -289,6 +289,26 @@ When a and b are literals, their product rounds first and the other product can
|
||||
|
||||
Omitting a connection is a separate case. With the same declared defaults and both first-pair ports left open, the hash-field sum/difference agrees on 0/35,328 pixels, maximum .234375238. The four modular-field expressions agree on 0/70,656 pixels, maximum 1.67397642. The measured color-temperature root uses explicit connections for its fit.
|
||||
|
||||
### Wide blur windows and bevel blur
|
||||
|
||||
The bevel blur matches 311,552/311,552 grayscale pixels across all 26 renders, maximum error zero. The [bevel intensity controls](#five-texel-blur-kernels) previously matched 160,049 pixels. The current totals are 122,880 U8, 24,576 U16, 24,576 F16 and 139,520 F32. These include all three successive blur stages at the larger intensities.
|
||||
|
||||
| Original controls with radius above 2.5 texels | Renders | Exact RGBA pixels / compared | Maximum error |
|
||||
|---|---:|---:|---:|
|
||||
| Separable impulse, stripes and signed channel | 48 | 1,081,344/1,081,344 | 0 |
|
||||
| Zero-angle directional impulse and stripes | 16 | 262,144/262,144 | 0 |
|
||||
| Separable modular four-channel fields | 48 | 172,032/172,032 | 0 |
|
||||
| Zero-angle directional modular fields | 24 | 86,016/86,016 | 0 |
|
||||
| Four shifted impulses | 42 | 1,204,224/1,204,224 | 0 |
|
||||
| Rectangular and square RGBA fields, separable | 128 | 3,801,088/3,801,088 | 0 |
|
||||
| Rectangular and square RGBA fields, directional | 64 | 1,900,544/1,900,544 | 0 |
|
||||
|
||||
Interior runs aligned to a power of two read a single filtered texel from the corresponding one-dimensional mip level. Reduce adjacent pairs at the source precision, storing each level at the destination precision. At the boundary of a footprint with radius r, let e=ceil(r−.5) and f=r−(e−.5). An endpoint aligned with the start of a pair combines the fractional texel with its full neighbor. For example, the left sample lies 1/(1+f) texels from that endpoint and has weight 1+f. A lone endpoint has weight f. Accumulate the weighted runs from right to left horizontally and top to bottom vertically, then multiply by .5/r. On a clamped axis, coverage beyond the image contributes one weighted read of the original border texel. A 256² impulse at column 128 and radius 4 reads .0625 at column 124, 513/4096 at 125 and 255/4096 at 132.
|
||||
|
||||
Use the [three-texel impulse and stripe inputs](#three-texel-blur-kernels) and the [four-channel and shifted-stripe inputs](#five-texel-blur-kernels). At seed 347 run the separable blur at intensity 17.5 on 64², 6.25/10/16.5/24.75 on 128² and 5.25 on 256². Run the zero-angle directional filter on the four 128² cases. For the modular fields use radius 2.875 at 64²/seed 797, 3.0625 at 32²/839 and 3.375 at 64²/877, with all four destination formats at 3.375. For the shifted stripes use radii 2.75, 3, 3.125, 3.5, 4, 5.25 and 8.25, seed 239, at 64², 128² and 256².
|
||||
|
||||
For the final two rows use 64×128, 128×64 and 256×256. Channel c is H=((37x+71y+11xy+47c+3xc) mod 1021)/1020. Supply rounded U8 levels or F32 values 3H−1.125. In order, use intensities 6.25, 9, 13, 17.5, 23.75, 30, 31.5, 48 and 62.75 at seeds 109+58j with j starting at zero. Output F32 except at 17.5, which uses U8, U16, F16 and F32. Run the separable filter with both axes tiled and both clamped, and the horizontal directional filter tiled. The radius scales from the shorter dimension on both axes. The table counts only radii above 2.5. Oblique directional kernels are outside these measurements.
|
||||
|
||||
### Five-texel blur kernels
|
||||
|
||||
| Original controls with 1.5 < radius ≤ 2.5 | Renders | Exact RGBA pixels / compared | Maximum error |
|
||||
|
||||
@@ -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 **260 Parity**, **34 In progress** and **252 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 **261 Parity**, **33 In progress** and **252 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)
|
||||
|
||||
@@ -77,7 +77,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
|
||||
| [auto_crop.sbs/min_max](../assets/sbs/auto_crop.sbs) | 1 | Yes | [Parity: 3 cases](parity-fixes.md#verified-cases) |
|
||||
| [auto_levels.sbs/auto_levels](../assets/sbs/auto_levels.sbs) | 1 | No | [Parity: 3 cases](parity-fixes.md#verified-cases) |
|
||||
| [auto_levels.sbs/min_max](../assets/sbs/auto_levels.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [bevel_smooth.sbs/blur_gaussian_grayscale](../assets/sbs/bevel_smooth.sbs) | 1 | Yes | [In progress · 160,049/311,552 pixels, higher intensities differ](parity-fixes.md#five-texel-blur-kernels) |
|
||||
| [bevel_smooth.sbs/blur_gaussian_grayscale](../assets/sbs/bevel_smooth.sbs) | 1 | Yes | [Parity · 26 renders, 311,552 exact grayscale pixels](parity-fixes.md#wide-blur-windows-and-bevel-blur) |
|
||||
| [bevel_smooth.sbs/debug/bevel_smooth_debug_position](../assets/sbs/bevel_smooth.sbs) | 1 | Yes | [In progress · 280,307/285,952 exact pixels](parity-fixes.md#segment-mapping-and-normal-graph-rechecks) |
|
||||
| [bevel_smooth.sbs/jump_flood_segment_iteration](../assets/sbs/bevel_smooth.sbs) | 1 | Yes | [Parity · 171,264 exact pixels in 18 renders](parity-fixes.md#segment-mapping-and-normal-graph-rechecks) |
|
||||
| [blackbody.sbs/blackbody](../assets/sbs/blackbody.sbs) | 0 | No | [Parity · 19 renders, 42,496 exact RGBA pixels](parity-fixes.md#literal-arguments-inside-pixel-function-calls) |
|
||||
|
||||
Reference in new issue
Block a user