mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-23 07:50:58 +01:00
Round normalized Pixel Processor filter weights together
This commit is contained in:
1 parent
289d4f50d5
commit
62cf397283
5 files changed
+102
-4
No files matched your search
@@ -0,0 +1,71 @@
|
||||
use craft_texture::Image;
|
||||
|
||||
/// Bilinear reads from normalized integer textures in a Pixel Processor.
|
||||
pub(super) fn sample(image: &Image, [u, v]: [f32; 2], tile: bool) -> [f32; 4] {
|
||||
if !u.is_finite() || !v.is_finite() {
|
||||
return [0.0, 0.0, 0.0, 1.0];
|
||||
}
|
||||
let (x, dx) = axis(u, image.width(), tile);
|
||||
let (y, dy) = axis(v, image.height(), tile);
|
||||
// Round the diagonal coefficient first. The four tap weights must still sum to 256.
|
||||
let diagonal = (dx * dy + 128) / 256;
|
||||
let bottom = dy - diagonal;
|
||||
let taps = [(x, y, 256 - dx - bottom), (x + 1, y, dx - diagonal), (x, y + 1, bottom), (x + 1, y + 1, diagonal)];
|
||||
let mut sum = [0.0; 4];
|
||||
for (x, y, weight) in taps {
|
||||
if weight != 0 {
|
||||
for (total, channel) in sum.iter_mut().zip(image.get(x, y, tile)) {
|
||||
// Stored 16-bit levels times the eight-bit weights sum exactly in f32.
|
||||
*total += (channel.clamp(0.0, 1.0) * 65535.0).round() * weight as f32;
|
||||
}
|
||||
}
|
||||
}
|
||||
sum.map(|value| (value / 256.0).round() / 65535.0)
|
||||
}
|
||||
|
||||
fn axis(uv: f32, size: u32, tile: bool) -> (i64, u32) {
|
||||
// Reduce before converting to a texel so even extreme finite coordinates have bounded neighbors.
|
||||
let uv = if tile { uv.rem_euclid(1.0) } else { uv.clamp(0.0, 1.0) };
|
||||
let texel = uv * size as f32 - 0.5;
|
||||
let first = texel.floor();
|
||||
(first as i64, ((texel - first) * 256.0).round() as u32)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::function::{EvaluationContext, EvaluationState, FunctionScope, compile};
|
||||
use craft_texture::{Image, Kind};
|
||||
use materialcraft_core::{FunctionGraph, Value};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[test]
|
||||
fn pixel_sampler_rounds_the_four_integer_texel_weights_together() {
|
||||
// RGBA identifies the four corners independently. At (1,128)/256 the diagonal weight is halfway to one.
|
||||
let image = Image::generate(2, 2, Kind::Color, |x, y| {
|
||||
let mut pixel = [0.0; 4];
|
||||
pixel[(2 * y + x) as usize] = 1.0;
|
||||
pixel
|
||||
})
|
||||
.with_integer_sampling(true);
|
||||
let variables = BTreeMap::new();
|
||||
let mut context = EvaluationContext {
|
||||
scope: FunctionScope::PixelProcessor,
|
||||
position: [0.25 + 1.0 / 512.0, 0.5],
|
||||
size: [2.0; 2],
|
||||
images: &[Some(image)],
|
||||
tile: true,
|
||||
variables: &variables,
|
||||
};
|
||||
let mut graph = FunctionGraph::default();
|
||||
let pos = graph.add("get_float2", &[], Some(Value::Text("$pos".into())));
|
||||
graph.output = Some(graph.add("samplecol", &[pos], Some(Value::Int2([0, 1]))));
|
||||
let program = compile(&graph, 0).unwrap();
|
||||
let pixel = |context: &EvaluationContext<'_>| program.evaluate_pixel(context, &mut EvaluationState::default()).unwrap();
|
||||
let expected = Value::Float4([32768.0, 0.0, 32512.0, 256.0].map(|n| n / 65535.0));
|
||||
assert_eq!(pixel(&context), expected);
|
||||
context.position[0] -= 1.0;
|
||||
assert_eq!(pixel(&context), expected);
|
||||
context.tile = false;
|
||||
assert_eq!(pixel(&context), Value::Float4([32768.0 / 65535.0, 0.0, 32768.0 / 65535.0, 0.0]));
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ mod function_definition;
|
||||
mod function_scope;
|
||||
mod inferred_type;
|
||||
mod instruction;
|
||||
mod integer_sampler;
|
||||
mod node_interface;
|
||||
mod node_types;
|
||||
mod operation;
|
||||
|
||||
@@ -125,6 +125,9 @@ pub(crate) fn evaluate(
|
||||
Some(image) if setting[1] == 0 => {
|
||||
image.get((position[0] * image.width() as f32).floor() as i64, (position[1] * image.height() as f32).floor() as i64, context.tile)
|
||||
}
|
||||
Some(image) if context.scope == super::FunctionScope::PixelProcessor && image.integer_sampling() => {
|
||||
super::integer_sampler::sample(image, [position[0], position[1]], context.tile)
|
||||
}
|
||||
Some(image) => image.sample_stored(position[0], position[1], context.tile),
|
||||
None => [0.0; 4],
|
||||
};
|
||||
|
||||
+24
-1
@@ -96,6 +96,29 @@ Use the same position ramp. For a controlled comparison, bind an original 1024×
|
||||
|
||||
The changed float case previously matched 10,399 pixels for basic Perlin and 3,926 for ridged Perlin. Whole-graph rounding remains visible in both. A separate seed-zero comparison used each app's shipped lookup, with MaterialCraft's tracked CC0 image read from Git. Basic Perlin matched 173/16,384 at 128² and 662/65,536 at 256², with maximum errors of 203 levels at both sizes. Ridged Perlin matched 176/16,384 and 768/65,536, with maximum errors of 139 and 154 levels. The shared lookup isolates an asset-dependent difference from the arithmetic.
|
||||
|
||||
### Grayscale and color 3D planar projection
|
||||
|
||||
Bind the position ramp above and a constant world-space normal of RGBA8 `(128,128,255,255)`. The grayscale texture is `T=(17*x+31*y) mod 256`. The color texture is RGBA8 `(T,R,255-G,(251+3*x+5*y) mod 256)`, where R and G are the position ramp bytes. Keep all input maps linear and the same size as the graph. The rotated case sets `rotation=.125` and `scale=.38`. Each format pair below gives the grayscale and color output formats respectively.
|
||||
|
||||
| Case | Size | Seed | Formats | Grayscale exact / total | Gray max | Color exact / total | Color max |
|
||||
|---|---:|---:|---|---:|---:|---:|---:|
|
||||
| Default | 128² | 0 | LUM16, RGBA8 | 16,384/16,384 | 0 | 16,384/16,384 | 0 |
|
||||
| Default | 256² | 0 | LUM16, RGBA8 | 65,536/65,536 | 0 | 65,536/65,536 | 0 |
|
||||
| Default | 128² | 42 | LUM16, RGBA8 | 16,384/16,384 | 0 | 16,384/16,384 | 0 |
|
||||
| Rotated | 128² | 0 | LUM16, RGBA8 | 16,334/16,384 | 226 | 16,378/16,384 | 1 |
|
||||
| Rotated | 64² | 42 | LUM8, RGBA8 | 4,094/4,096 | 1 | 4,094/4,096 | 1 |
|
||||
| Rotated | 64² | 42 | LUM16, RGBA16 | 4,093/4,096 | 32 | 4,093/4,096 | 32 |
|
||||
| Rotated | 64² | 42 | LUM16F, RGBA16F | 4,093/4,096 | 4.8828125e-4 | 4,093/4,096 | 4.8828125e-4 |
|
||||
| Rotated | 64² | 42 | LUM32F, RGBA32F | 4,093/4,096 | 4.882888e-4 | 4,093/4,096 | 4.882888e-4 |
|
||||
|
||||
Before the sampler change the default 256² cases matched 65,048 grayscale pixels and 65,492 RGBA pixels. An independent one-processor graph isolates the integer texture read. In a 256² source, use the 2×2 block starting at (12,15). Light one corner at a time, then repeat with corner levels `(.37,.13,.91,.68)` rounded to the input precision, in row-major order. For output pixel (x,y), sample at `((12.5+x/256)/256,(15.5+y/256)/256)`. Run all five patterns with RGBA8 and RGBA16 input and read the red channel into LUM32F. All 655,360 pixels match exactly.
|
||||
|
||||
In units of 1/256 texel, the rounded horizontal and vertical fractions are `dx` and `dy`. The bottom-right weight is `q=floor((dx*dy+128)/256)`. The other three weights in row-major order are `256-dx-dy+q`, `dx-q` and `dy-q`. Accumulate the four normalized 16-bit levels with these integer weights, divide by 256 and round half up. A separate 128² high-frequency texture sweep at seven fixed offsets matched all 114,688 pixels, including wrapped edges. The rotated graph cases still differ with this sampler. Floating-source filtering has separate rounding behavior and remains open.
|
||||
|
||||
### November 17 validation
|
||||
|
||||
All four new regressions failed with the corresponding behavior disabled or absent. The full release node suite passed all 108 tests. Workspace formatting, strict all-target Clippy, dependency layers and WebAssembly checks passed. Raw captures and generated input images were kept outside the repository.
|
||||
|
||||
## Remaining differences
|
||||
|
||||
The following entries record the first pass. The dated comparisons above give the newer results for revisited roots.
|
||||
@@ -1142,7 +1165,7 @@ The following entries record the first pass. The dated comparisons above give th
|
||||
|
||||
- **`pbr_render2.sbs/height_adjust` - Parity:** All 98,304 output pixels (LUM8) match across 3 recorded cases: defaults at 128x128/256x256 and seed 42. Connectable value pins retain their defaults; nondefault values require a wired reference fixture and remain unverified.
|
||||
|
||||
## Validation
|
||||
## First-pass validation
|
||||
|
||||
- Final release CLI and native desktop builds pass, including the unassigned-variable, grayscale Pixel Processor, integer-conversion, and output-bridge changes. Formatting, dependency layers, and all ten WebAssembly crate checks pass. An independent inventory rescan agrees with all 546 roots, and all 320 package links resolve.
|
||||
- Workspace Clippy is blocked by an existing `chunks_exact_to_as_chunks` lint in the text primitive. Clippy for the four changed library crates passes with that existing lint suppressed.
|
||||
|
||||
@@ -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 **166 Parity**, **3 In progress** and **377 Too hard**. Exact counts, output formats and remaining differences for the reopened roots are in the [November 17 comparisons](parity-fixes.md#2026-11-17-root-comparisons).
|
||||
The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard**. The current inventory has **166 Parity**, **5 In progress** and **375 Too hard**. Exact counts, output formats and remaining differences for the reopened roots are in the [November 17 comparisons](parity-fixes.md#2026-11-17-root-comparisons).
|
||||
|
||||
## Complete inventory
|
||||
|
||||
@@ -45,8 +45,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
|
||||
| [3d_perlin_noise.sbs/3D_perlin_noise](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · lookup and arithmetic differences measured](parity-fixes.md#basic-and-ridged-3d-perlin) |
|
||||
| [3d_perlin_noise.sbs/3D_perlin_noise_fractal](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [3d_perlin_noise.sbs/3D_ridged_noise_fractal](../assets/sbs/3d_perlin_noise.sbs) | 1 | No | [In progress · shared lookup exact in LUM8, higher precision differs](parity-fixes.md#basic-and-ridged-3d-perlin) |
|
||||
| [3d_planar_projection.sbs/3d_planar_projection](../assets/sbs/3d_planar_projection.sbs) | 3 | No | [Too hard: sampling boundaries](parity-fixes.md#remaining-differences) |
|
||||
| [3d_planar_projection.sbs/3d_planar_projection_color](../assets/sbs/3d_planar_projection.sbs) | 3 | No | [Too hard: sampling boundaries](parity-fixes.md#remaining-differences) |
|
||||
| [3d_planar_projection.sbs/3d_planar_projection](../assets/sbs/3d_planar_projection.sbs) | 3 | No | [In progress · default exact, rotated samples differ](parity-fixes.md#grayscale-and-color-3d-planar-projection) |
|
||||
| [3d_planar_projection.sbs/3d_planar_projection_color](../assets/sbs/3d_planar_projection.sbs) | 3 | No | [In progress · default RGBA exact, rotated samples differ](parity-fixes.md#grayscale-and-color-3d-planar-projection) |
|
||||
| [3d_simplex_noise.sbs/3d_simplex_noise](../assets/sbs/3d_simplex_noise.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
|
||||
| [3d_texture_jump_flood.sbs/3d_texture_jump_flood_iteration](../assets/sbs/3d_texture_jump_flood.sbs) | 1 | Yes | [Parity: 5 cases](parity-fixes.md#verified-cases) |
|
||||
| [3d_texture_position.sbs/3d_texture_position](../assets/sbs/3d_texture_position.sbs) | 0 | No | [Parity: native 4K atlas](parity-fixes.md#verified-cases) |
|
||||
|
||||
Reference in new issue
Block a user