mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-25 00:41:00 +01:00
Preserve signed Sharpen lobes and accumulate stored texels in order
This commit is contained in:
1 parent
73f82262be
commit
3b5ec60ec0
4 files changed
+85
-28
No files matched your search
@@ -1,4 +1,5 @@
|
||||
use super::ATOMIC;
|
||||
use super::input_resize::{self, InputResize};
|
||||
use crate::{CookContext, CookResult, NodeDefinition, node};
|
||||
use craft_texture::{Image, Kind};
|
||||
|
||||
@@ -13,12 +14,13 @@ pub(super) fn definition() -> NodeDefinition {
|
||||
}
|
||||
|
||||
fn sharpen(context: &CookContext) -> CookResult {
|
||||
let input = context.any_or(0, Kind::Gray, [0.0; 4]);
|
||||
let input = input_resize::resized_input(context, 0, InputResize::Filter).unwrap_or_else(|| Image::filled(context.w, context.h, Kind::Gray, [0.0; 4]));
|
||||
let intensity = context.f("intensity");
|
||||
let tile = context.tiling_axes();
|
||||
let short_side = context.w.min(context.h) as f32;
|
||||
let horizontal = intensity * short_side / context.w.max(1) as f32;
|
||||
let vertical = intensity * short_side / context.h.max(1) as f32;
|
||||
let center_weight = 1.0 + 2.0 * (horizontal + vertical);
|
||||
|
||||
Ok(vec![Image::generate(context.w, context.h, input.kind(), |x, y| {
|
||||
let (x, y) = (i64::from(x), i64::from(y));
|
||||
@@ -28,23 +30,19 @@ fn sharpen(context: &CookContext) -> CookResult {
|
||||
let top = input.get_axes(x, y - 1, tile);
|
||||
let bottom = input.get_axes(x, y + 1, tile);
|
||||
std::array::from_fn(|channel| {
|
||||
// Keep the axial differences separate to preserve Float32 cancellation order
|
||||
let twice_center = 2.0 * center[channel];
|
||||
let across = axial_difference(center[channel], twice_center, left[channel], right[channel]);
|
||||
let down = axial_difference(center[channel], twice_center, top[channel], bottom[channel]);
|
||||
|
||||
// Float outputs retain HDR values but discard negative sharpening lobes
|
||||
(center[channel] + (across * horizontal + down * vertical)).max(0.0)
|
||||
let [center, left, right, top, bottom] = [center[channel], left[channel], right[channel], top[channel], bottom[channel]];
|
||||
let sum = center.mul_add(center_weight, -horizontal * left);
|
||||
let sum = right.mul_add(-horizontal, sum);
|
||||
let sum = top.mul_add(-vertical, sum);
|
||||
let result = bottom.mul_add(-vertical, sum);
|
||||
if result.is_finite() || ![center, left, right, top, bottom].iter().all(|v| v.is_finite()) {
|
||||
return result;
|
||||
}
|
||||
// A finite HDR plateau can overflow the weighted center before its neighbors cancel.
|
||||
let center = f64::from(center);
|
||||
let across = 2.0 * center - (f64::from(left) + f64::from(right));
|
||||
let down = 2.0 * center - (f64::from(top) + f64::from(bottom));
|
||||
(center + across * f64::from(horizontal) + down * f64::from(vertical)) as f32
|
||||
})
|
||||
})])
|
||||
}
|
||||
|
||||
fn axial_difference(center: f32, twice_center: f32, first: f32, second: f32) -> f32 {
|
||||
let difference = twice_center - (first + second);
|
||||
if difference.is_finite() || ![center, first, second].iter().all(|value| value.is_finite()) {
|
||||
return difference;
|
||||
}
|
||||
|
||||
// Finite HDR inputs can overflow both Float32 sums and turn their difference into NaN
|
||||
(2.0 * f64::from(center) - (f64::from(first) + f64::from(second))) as f32
|
||||
}
|
||||
@@ -327,12 +327,27 @@ fn sharpen_preserves_float_order_on_ramps() {
|
||||
|
||||
let output = cook("sharpen", &[Some(input)], &[("intensity", Value::Float(1.0))], [16, 16], true);
|
||||
|
||||
// Float32 ramps distinguish the Laplacian's arithmetic order
|
||||
for (x, bits) in [(1, 0x3d88_8889), (3, 0x3e4c_cccb), (7, 0x3eee_eeed), (13, 0x3f5d_ddde)] {
|
||||
for (x, bits) in [(1, 0x3d88_8887), (3, 0x3e4c_cccf), (7, 0x3eee_eef1), (13, 0x3f5d_dddc)] {
|
||||
assert_eq!(output.get_gray(x, 7, false).to_bits(), bits, "column {x}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sharpen_accumulates_all_four_weighted_neighbors_in_order() {
|
||||
let input = Image::gray(16, 16, |x, y| {
|
||||
f32::from_bits(match (x, y) {
|
||||
(9, 8) => 0x3d9a_e7d9,
|
||||
(8, 8) => 0xbf0b_3f1e,
|
||||
(10, 8) => 0x3f49_f914,
|
||||
(9, 7) => 0xbfe3_2f70,
|
||||
(9, 9) => 0x3ff6_8c6b,
|
||||
_ => 0,
|
||||
})
|
||||
});
|
||||
let output = cook("sharpen", &[Some(input)], &[("intensity", Value::Float(0.8125))], [16, 16], true);
|
||||
assert_eq!(output.get_gray(9, 8, false).to_bits(), 0xba0c_0f80);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sharpen_preserves_finite_hdr_constant() {
|
||||
let constant = 2.0e38;
|
||||
@@ -566,13 +581,27 @@ fn directional_blur_uses_full_radius_and_fractional_endpoints() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sharpen_has_center_gain_and_clamps_negative_lobes() {
|
||||
fn sharpen_preserves_the_signed_lobes_of_an_impulse_in_float_outputs() {
|
||||
let input = Image::gray(16, 16, |x, y| f32::from(x == 8 && y == 8));
|
||||
let inputs = [Some(input)];
|
||||
for format in [OutputFormat::F16, OutputFormat::F32] {
|
||||
let output = cook_format("sharpen", &inputs, &[("intensity", Value::Float(1.0))], [16, 16], true, format);
|
||||
assert_eq!(output.get_gray(8, 8, false), 5.0);
|
||||
for (x, y) in [(7, 8), (9, 8), (8, 7), (8, 9)] {
|
||||
assert_eq!(output.get_gray(x, y, false), -1.0);
|
||||
}
|
||||
assert_eq!(output.get_gray(7, 7, false), 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
let output = cook("sharpen", &[Some(input)], &[("intensity", Value::Float(1.0))], [16, 16], true);
|
||||
|
||||
assert_eq!(output.get_gray(8, 8, false), 5.0);
|
||||
assert!(output.data().iter().all(|value| *value >= 0.0));
|
||||
#[test]
|
||||
fn sharpen_resizes_stored_levels_before_computing_contrast() {
|
||||
let levels = [58997, 1380, 14934, 23956];
|
||||
let input = Image::gray(2, 2, |x, y| levels[(2 * y + x) as usize] as f32 / 65535.0).with_integer_sampling(true);
|
||||
let output = cook("sharpen", &[Some(input)], &[("intensity", Value::Float(0.0))], [4, 4], true);
|
||||
for (x, y, bits) in [(0, 0, 0x3f136e93), (3, 0, 0x3e86de87), (0, 3, 0x3ebbd0bc), (3, 3, 0x3e9dfa9e)] {
|
||||
assert_eq!(output.get_gray(x, y, false).to_bits(), bits);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -358,6 +358,36 @@ To isolate the pattern stages, connect the identified stage directly to the outp
|
||||
|
||||
The 120 authored single-branch renders matched 70,015/72,664 pixels. The 120 disc controls matched 72,249/72,664. A disc still differing leaves placement, coverage or evaluation upstream of the original shape open. At indices 441, 466, 522 and 523 the native runtime rejected the isolated packages. Whole-root and connected-stage counts are available for all four. Sphere displacement and K-means first differ in their pixel processors. Wood fibers 2 still needs a bounded whole-graph cook.
|
||||
|
||||
### Signed Sharpen stencils and stored inputs
|
||||
|
||||
Sharpen preserves negative lobes in floating-point output and uses the input's working storage when resizing. Its four-neighbor stencil starts with the weighted left texel. It adds the center, right, top and bottom terms in that order, fusing the product and addition at each step. With horizontal weight H and vertical weight V, the center weight is 1+2(H+V). H and V scale intensity by the shorter output side divided by the respective axis length. The two earlier sign and resizing regressions and two further accumulation checks failed on the preceding implementations. All 256 release node tests, formatting, strict workspace Clippy, dependency layers and WebAssembly passed.
|
||||
|
||||
| Original probe | Renders | Exact / compared | Maximum error |
|
||||
|---|---:|---:|---|
|
||||
| Impulse, signed ramp and constant, U16 and F32 sources | 16 | 5,632/5,632 | 0 in all four output formats |
|
||||
| Grayscale and RGBA with output size inherited from the input | 108 | 10,512/10,512 | 0 in all four output formats |
|
||||
| Output size set by parent | 108 | 38,549/38,832 comparable | 0 / 0 / 0 / 1.87000686 in U8 / U16 / F16 / F32 |
|
||||
| Independent signed field with five nonzero strengths and changed sizes | 80 | 16,417/16,704 | 4.39762998 F32 |
|
||||
| Dirt 4 and its second version, shared 2² sources | 8 | 136/136 | 0 F32 |
|
||||
|
||||
For the impulse matrix, place a single white texel at (8,8) in a black 16² image. Render at intensity one in all four formats and at 32² F32. At 16² the floating-point output is 5 at the center, −1 at its four axial neighbors and zero at the diagonals. In U8 and U16 the center is white and the neighbors black. Add the 16² signed field (((x+3y) mod 17)−5)/8 at intensities .375 and 0, and a flat .28125 at intensity 1. Use F32 inputs or clip and round to U16, and seeds 138559+11j in the order F32, F16, U8, U16, enlarged F32, two ramps and flat.
|
||||
|
||||
For the input-size and parent-size matrices, use source extents 2², 8×4 and 16². Test grayscale and four-channel inputs in U16 and F32. In channel c use level Q=(7919x+21473y+1103xy+2927c+139m+58997) mod 65536 for m=0 or 2. For m=1 use 59841−223c where (x+y+c) mod 3=0, and 3431+125c elsewhere. Write Q for U16 or Q/19001−.8125 for F32. Use the following cases with seed 134381+19j+i, where i is 1 for U16 and 3 for F32. The parent-size matrix has four additional size discrepancies at a requested 1². The reference returned 8×4. The pixel totals exclude those four outputs.
|
||||
|
||||
| Requested extent relative to source | Strength | Field m | Stored output |
|
||||
|---|---:|---:|---|
|
||||
| Same | 1 | 0 | F32 |
|
||||
| Double both axes | 1, then 0 | 0 | F32 |
|
||||
| Double both axes | .375 | 1 | F32, U8, U16, F16 |
|
||||
| Four times width, twice height | 1.25 | 2 | F32 |
|
||||
| Half both axes | 1 | 2 | F32 |
|
||||
|
||||
For the independent field use Q=(269x+1723y+257xy+9587c+7919((x+3y+5c) mod 13)+7537) mod 65536 at 8×4 or 16². Write Q for U16 or (Q−31007)/15019+.03125(x mod 3) for F32. Use F32 output. At the source extent use strengths 0, 1, .375, .8125, 1.34375 and 2.71875, then double both axes at 0 and .8125, and halve both axes at 0 and 1.34375. Use seed 142867+19j. The 287 differing pixels were in the reduced nonzero-strength cases. At 16² on an independent x/15 F32 ramp, strength one gives bits 3d888887, 3e4ccccf, 3eeeeef1 and 3f5ddddc at columns 1, 3, 7 and 13. Three finite ramp controls match 768/768 pixels. A separate constant 2×10³⁸ input produces positive infinity in all 256 reference pixels. MaterialCraft preserves that finite HDR plateau.
|
||||
|
||||
The two 4×4 reference outputs in the [deep traversal experiment](#bounded-traversal-of-deep-patterns) reproduce exactly when Sharpen alone receives the measured sources. Use U16 levels (30680,17968 / 0,24181) for dirt_4 and U8 levels (89,103 / 90,43) for its second version, in row order. Test 4² at strengths 1 and 0, 2² at 1, and 8×4 at .375. Use seeds 103307+13i at inventory indices i=266 and 267. All 136 F32 pixels match, including both whole-reference 4² outputs. Their full pattern traversal remains open. Dirt 6 improved from 117,826 to 125,252/129,280 exact pixels across 16 whole renders. Its U8 / U16 / F16 / F32 maxima are 0 / 8 / .000244140625 / .0000927746296.
|
||||
|
||||
A MaterialCraft-saved U16 input graph and an independently saved RGBA32F graph reopened in the reference. The first six cases of each reproduced all 1,620 stored pixels byte for byte.
|
||||
|
||||
## 2026-11-19 root comparisons
|
||||
|
||||
### Texture sampling in pattern functions
|
||||
|
||||
@@ -202,7 +202,7 @@ Reading inherited variables directly reduced cook time on six Dirt 1 and BNW Spo
|
||||
| [dirt_1.sbs/dirt_1](../assets/sbs/dirt_1.sbs) | 0 | Yes | [In progress · 145,661/145,664 exact pixels, overlapping disc additions isolated](parity-fixes.md#overlapping-disc-additions-in-dirt-1) |
|
||||
| [dirt_3.sbs/dirt_3](../assets/sbs/dirt_3.sbs) | 0 | Yes | [In progress · 145,359/145,664 exact pixels in 17 renders](parity-fixes.md#pattern-tint-coverage-and-grayscale-conversion) |
|
||||
| [dirt_5.sbs/dirt_5](../assets/sbs/dirt_5.sbs) | 0 | Yes | [In progress · 129,226/129,280 exact pixels in 16 renders](parity-fixes.md#stored-opacity-across-pattern-depths) |
|
||||
| [dirt_6.sbs/dirt_6](../assets/sbs/dirt_6.sbs) | 0 | Yes | [In progress · 117,826/129,280 exact pixels in 16 renders](parity-fixes.md#pattern-tint-coverage-and-grayscale-conversion) |
|
||||
| [dirt_6.sbs/dirt_6](../assets/sbs/dirt_6.sbs) | 0 | Yes | [In progress · 125,252/129,280 exact pixels in 16 renders](parity-fixes.md#signed-sharpen-stencils-and-stored-inputs) |
|
||||
| [dirt_gradient.sbs/dirt_gradient](../assets/sbs/dirt_gradient.sbs) | 0 | Yes | [In progress · 128,396/129,280 exact pixels in 16 renders](parity-fixes.md#pattern-tint-coverage-and-grayscale-conversion) |
|
||||
| [exposure.sbs/exposure](../assets/sbs/exposure.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [extend_shape.sbs/extend_shape_color](../assets/sbs/extend_shape.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
@@ -337,8 +337,8 @@ Reading inherited variables directly reduced cook time on six Dirt 1 and BNW Spo
|
||||
| [noise_dirt_2.sbs/dirt_2](../assets/sbs/noise_dirt_2.sbs) | 0 | Yes | [In progress · 11/19 whole outputs, 107,184/107,776 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) |
|
||||
| [noise_dirt_3.sbs/dirt_3](../assets/sbs/noise_dirt_3.sbs) | 0 | Yes | [In progress · 19/19 whole outputs, 135,236/142,592 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) |
|
||||
| [noise_dirt_3_v2.sbs/dirt_3](../assets/sbs/noise_dirt_3_v2.sbs) | 0 | No | [In progress · 17/19 whole outputs, 136,738/141,568 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) |
|
||||
| [noise_dirt_4.sbs/dirt_4](../assets/sbs/noise_dirt_4.sbs) | 0 | Yes | [In progress · whole graph reaches its execution limit in all 19 cases, 2,112 hard-shape pixels per shape](parity-fixes.md#dirt-noise-sources-and-downstream-stages) · [4×4 bounded trial, 0/16 exact](parity-fixes.md#bounded-traversal-of-deep-patterns) |
|
||||
| [noise_dirt_4_v2.sbs/dirt_4](../assets/sbs/noise_dirt_4_v2.sbs) | 0 | No | [In progress · whole graph reaches its execution limit in all 19 cases, 2,112 hard-shape pixels per shape](parity-fixes.md#dirt-noise-sources-and-downstream-stages) · [4×4 bounded trial, 0/16 exact](parity-fixes.md#bounded-traversal-of-deep-patterns) |
|
||||
| [noise_dirt_4.sbs/dirt_4](../assets/sbs/noise_dirt_4.sbs) | 0 | Yes | [In progress · whole graph reaches its execution limit in all 19 cases, 2,112 hard-shape pixels per shape](parity-fixes.md#dirt-noise-sources-and-downstream-stages) · [4×4 bounded trial, 0/16 exact](parity-fixes.md#bounded-traversal-of-deep-patterns) · [4×4 Sharpen matches all 16 pixels on the measured source](parity-fixes.md#signed-sharpen-stencils-and-stored-inputs) |
|
||||
| [noise_dirt_4_v2.sbs/dirt_4](../assets/sbs/noise_dirt_4_v2.sbs) | 0 | No | [In progress · whole graph reaches its execution limit in all 19 cases, 2,112 hard-shape pixels per shape](parity-fixes.md#dirt-noise-sources-and-downstream-stages) · [4×4 bounded trial, 0/16 exact](parity-fixes.md#bounded-traversal-of-deep-patterns) · [4×4 Sharpen matches all 16 pixels on the measured source](parity-fixes.md#signed-sharpen-stencils-and-stored-inputs) |
|
||||
| [noise_dirt_5.sbs/dirt_5](../assets/sbs/noise_dirt_5.sbs) | 0 | Yes | [In progress · 11/19 whole outputs, 107,751/107,776 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) |
|
||||
| [noise_dirt_5_v2.sbs/dirt_5](../assets/sbs/noise_dirt_5_v2.sbs) | 0 | No | [In progress · 11/19 whole outputs, 107,341/107,776 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) |
|
||||
| [noise_dirt_gradient.sbs/dirt_gradient](../assets/sbs/noise_dirt_gradient.sbs) | 0 | Yes | [In progress · 19/19 whole outputs, 141,554/142,592 pixels](parity-fixes.md#dirt-noise-sources-and-downstream-stages) |
|
||||
|
||||
Reference in new issue
Block a user