From 7cdacdc30bcd13ddbf47b359843e7958b8cabaf9 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Sat, 21 Nov 2026 02:48:09 -0500 Subject: [PATCH] Resize grayscale bitmap resources through stored levels --- crates/eval/src/node_execution.rs | 28 +------------------------ crates/nodes/src/atomic/bitmap.rs | 11 ++++++++++ crates/nodes/src/lib.rs | 2 +- crates/nodes/src/tests.rs | 29 ++++++++++++++++++++++++++ crates/nodes/src/texture_sampler.rs | 30 ++++++++++++++++++++++++++- docs/parity-fixes.md | 32 +++++++++++++++++++++++++++++ docs/sbs-root-nodes.md | 2 +- 7 files changed, 104 insertions(+), 30 deletions(-) diff --git a/crates/eval/src/node_execution.rs b/crates/eval/src/node_execution.rs index 9ad45f91..a1375c06 100644 --- a/crates/eval/src/node_execution.rs +++ b/crates/eval/src/node_execution.rs @@ -261,7 +261,7 @@ pub(crate) fn cook_node( let format = upstream.unwrap_or(OutputFormat::F32); let external = if wired { None } else { crate::external_image::resize(&image, (w, h), kind, bitmap.unwrap_or(format), base.format) }; let tiling = if wired { base.tiling } else { [false; 2] }; - let output = external.unwrap_or_else(|| NodeOutput::Image(cook_bound_image(image, (w, h), kind, format, base.format, tiling))); + let output = external.unwrap_or_else(|| NodeOutput::Image(materialcraft_nodes::resize_bound_image(image, (w, h), kind, format, base.format, tiling))); Ok(vec![output]) } else if node.def == materialcraft_core::MISSING_NODE { let name = if node.label.is_empty() { "this node" } else { node.label.as_str() }; @@ -337,29 +337,3 @@ fn wired_texture<'a>(options: &'a Options<'_>, bridge: &Node) -> Option<(&'a Ima let image = options.inputs.get(&name)?; (!bridge.disabled && image.kind() == kind).then_some((image, *options.input_formats.get(&name)?)) } - -fn cook_bound_image(mut image: Image, size: (u32, u32), kind: Kind, mut source_format: OutputFormat, output_format: OutputFormat, tiling: [bool; 2]) -> Image { - let next = |current: u32, target: u32| { - if current < target { current.saturating_mul(2).min(target) } else { current.div_ceil(2).max(target) } - }; - loop { - if (image.width(), image.height()) != size { - image = materialcraft_nodes::resize_input_level(&image, next(image.width(), size.0), next(image.height(), size.1), source_format, tiling); - } - // The first filtered level supplies RGB for conversion. Later levels read the stored gray texels. - if image.kind() != kind { - image = match kind { - Kind::Gray => { - let weight = 1.0 / 3.0; - image.map(Kind::Gray, |p| [p[2].mul_add(weight, p[1].mul_add(weight, p[0] * weight)), 0.0, 0.0, 0.0]) - } - Kind::Color => image.to_color(), - }; - } - image = crate::output_precision::apply(image, output_format); - if (image.width(), image.height()) == size { - return image; - } - source_format = output_format; - } -} diff --git a/crates/nodes/src/atomic/bitmap.rs b/crates/nodes/src/atomic/bitmap.rs index ab07409f..aa32d4be 100644 --- a/crates/nodes/src/atomic/bitmap.rs +++ b/crates/nodes/src/atomic/bitmap.rs @@ -1,5 +1,7 @@ use super::ATOMIC; use crate::{CookContext, CookResult, NodeDefinition, node}; +use craft_texture::Kind; +use materialcraft_core::OutputFormat; pub(super) fn definition() -> NodeDefinition { node("bitmap", "Bitmap", ATOMIC) @@ -16,6 +18,15 @@ pub(super) fn definition() -> NodeDefinition { fn bitmap(context: &CookContext) -> CookResult { let name = context.text("resource"); let image = match (context.resource)(&craft_texture::resource_key(name, context.i("colour_space") == 1)) { + Some(image) + if image.kind() == Kind::Gray + && image.integer_sampling() + && (image.width(), image.height()) != (context.w, context.h) + && [image.width(), image.height(), context.w, context.h].into_iter().all(u32::is_power_of_two) => + { + // Bitmap filtering clamps at the resource edge and stores each level at the node precision. + crate::resize_bound_image(image, (context.w, context.h), Kind::Gray, OutputFormat::U16, context.format, [false; 2]) + } Some(image) => image.resize(context.w, context.h, context.tile), None if name.is_empty() => context.gen_color(|u, v| { let c = if ((u * 8.0) as i32 + (v * 8.0) as i32) % 2 == 0 { 0.8 } else { 0.2 }; diff --git a/crates/nodes/src/lib.rs b/crates/nodes/src/lib.rs index cc264058..f125245b 100644 --- a/crates/nodes/src/lib.rs +++ b/crates/nodes/src/lib.rs @@ -34,7 +34,7 @@ pub use parameter_definition::ParameterDefinition; pub use parameter_descriptor::ParameterDescriptor; pub use port_definition::PortDefinition; pub use port_kind::PortKind; -pub use texture_sampler::resize_input_level; +pub use texture_sampler::{resize_bound_image, resize_input_level}; pub type CookResult = Result, String>; pub type Cook = fn(&CookContext<'_>) -> CookResult; diff --git a/crates/nodes/src/tests.rs b/crates/nodes/src/tests.rs index 524f9c6a..7e757c14 100644 --- a/crates/nodes/src/tests.rs +++ b/crates/nodes/src/tests.rs @@ -88,6 +88,35 @@ fn bilinear_transform_selects_and_stores_the_requested_mip_level() { } } +#[test] +fn grayscale_bitmap_halving_stores_each_level_before_the_next_filter() { + use materialcraft_core::OutputFormat; + + let definition = find("bitmap").unwrap(); + let params = resolve_params(definition, &BTreeMap::from([("color_mode".into(), Value::Bool(false)), ("resource".into(), Value::Text("field".into()))])); + let source = Image::gray(2, 4, |x, y| { + let level = 1000 * x + u32::from(y == 3); + level as f32 / 65535.0 + }) + .with_integer_sampling(true); + let vars = BTreeMap::new(); + let context = CookContext { + input_formats: &[], + format: OutputFormat::U16, + w: 2, + h: 1, + seed: 0, + tile: false, + inputs: &[], + params: ¶ms, + resource: &|_| Some(source.clone()), + vars: &vars, + }; + let result = (definition.cook)(&context).unwrap(); + let stored = crate::output_precision::apply(result[0].clone(), context.format); + assert_eq!(stored.gray_data().unwrap(), [1.0 / 65535.0, 1001.0 / 65535.0]); +} + #[test] fn nearest_transform_mip_keeps_the_last_column_of_an_odd_width_input() { let definition = find("transformation").unwrap(); diff --git a/crates/nodes/src/texture_sampler.rs b/crates/nodes/src/texture_sampler.rs index b07ffaa5..e4223714 100644 --- a/crates/nodes/src/texture_sampler.rs +++ b/crates/nodes/src/texture_sampler.rs @@ -1,4 +1,4 @@ -use craft_texture::Image; +use craft_texture::{Image, Kind}; use materialcraft_core::OutputFormat; /// Resample one stored input level at destination pixel centers. @@ -6,6 +6,34 @@ pub fn resize_input_level(image: &Image, width: u32, height: u32, format: Output Image::generate_uv(width, height, image.kind(), |u, v| sample(image, [u, v], tiling, format, None)) } +/// Resize a raster through stored levels, converting its channels after the first sampling pass. +pub fn resize_bound_image(mut image: Image, size: (u32, u32), kind: Kind, mut source: OutputFormat, output: OutputFormat, tiling: [bool; 2]) -> Image { + let size = craft_texture::clamp_size(size.0, size.1); + let next = |current: u32, target: u32| { + if current < target { current.saturating_mul(2).min(target) } else { current.div_ceil(2).max(target) } + }; + loop { + if (image.width(), image.height()) != size { + image = resize_input_level(&image, next(image.width(), size.0), next(image.height(), size.1), source, tiling); + } + // Later levels read the stored channels, including any RGB-to-gray conversion. + if image.kind() != kind { + image = match kind { + Kind::Gray => { + let weight = 1.0 / 3.0; + image.map(Kind::Gray, |p| [p[2].mul_add(weight, p[1].mul_add(weight, p[0] * weight)), 0.0, 0.0, 0.0]) + } + Kind::Color => image.to_color(), + }; + } + image = crate::output_precision::apply(image, output); + if (image.width(), image.height()) == size { + return image; + } + source = output; + } +} + /// Value and pattern functions filter stored channel levels using full-precision texel fractions. pub(crate) fn sample_uniform(image: &Image, uv: [f32; 2], format: OutputFormat, bilinear: bool, repeat: bool) -> [f32; 4] { if !uv.iter().all(|v| v.is_finite()) { diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index a29eacb9..14bb4218 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -272,6 +272,38 @@ For the eighth-turn controls orient the same 16-bit stripes perpendicular to eac In the two complete roots, 646 color and 130 grayscale pixels still differ across 78 distinct settings per root. All 3,489,792 original pixels in each application reproduced after reopening 60 saved settings per root, maximum zero. The residuals include longer oblique passes. The new release regression failed before this change and passed afterward. All 323 node release tests, formatting, strict workspace Clippy, dependency layers and WebAssembly checks passed. +### Stored levels when resizing a grayscale bitmap resource + +The Bitmap node now filters power-of-two normalized grayscale resources at each factor-of-two step. It clamps at the resource edge and stores each intermediate level at the node's destination format. Bitmap and graph-input paths use the same level sampler. The previous Bitmap cook averaged the entire footprint in one pass for integer-ratio reductions and sampled the original image directly for enlargements. + +The 3D fractal Voronoi graph requests its lookup at 1024 × 1. Its original procedural input has 1024 columns and 16 rows. For U16, take successive vertical pairs, compute (a+b+1) div 2, and repeat until one row remains. On the shuffled table, 70/1,024 stored texels previously matched, maximum two levels. On the affine table, 95/1,024 matched, also within two levels. The earlier graph-output tests masked the affine-table differences. + +Use the [two procedural tables](#package-ownership-of-saved-function-calls), a second row shuffle using seed 1299709, and tables filled with zero or 32768. From the first shuffled table, also replace zeros with 64, clamp the maximum to 65471, take the 65535 complement, and replace each of the 16 rows in turn with the matching affine row. All 24 directly captured Bitmap outputs now match **24,576/24,576** U16 texels, maximum zero. For an independent check, generate a 1024 × 1 PNG from the repeated pair calculation and bind it in both applications. The preceding evaluator then reproduces every final pixel across the seven shuffled and three affine settings of each fractal Voronoi root, **7,168/7,168 per root**, maximum zero. + +For a two-column regression use four rows. Fill the columns with 0 and 1000 respectively and add one at the bottom of each column. Resizing to 2 × 1 U16 stores 1 and 1001. The previous evaluator stored 0 and 1000. + +For an independent Bitmap graph, generate U16 levels (573x+149y+97xy+317) mod 65536 at 64 × 32 and 32 × 64, and U8 levels (83x+151y+11xy+29) mod 256 at 64 × 32. A fourth 64 × 32 U16 image uses 514 floor(x/8), plus one where x mod 4 and y mod 4 are both 3. With grayscale output, render each at 64 × 32, 32², 64 × 16, 32 × 16, 16², 16 × 32, 16 × 64, 64², 128 × 16 and 32 × 64. Use all four formats. The seeds are 497107 + 41j + k, with field indices 0 for U16, 1 for U8, 2 for the transposed U16 field and 3 for the tie field, and zero-based resolution index k. + +| Output | Renders | Exact before / total · maximum | Exact after / total · maximum | +|---|---:|---:|---:| +| LUM8 | 40 | 55,322/58,368 · 85 | 58,368/58,368 · 0 | +| LUM16 | 40 | 48,598/58,368 · 21884 | 58,368/58,368 · 0 | +| LUM16F | 40 | 53,644/58,368 · 0.333984375 | 58,368/58,368 · 0 | +| LUM32F | 40 | 23,985/58,368 · 0.33392936 | 58,368/58,368 · 0 | + +All **233,472/233,472** pixels match. The 3D fractal Voronoi root also now matches all **5,632/5,632** pixels on the seven shuffled-table settings, up from 5,475. The affine settings still match 1,536/1,536, and the 28 additional style, color-seed and distance controls match 25,088/25,088. All maxima are zero. Capturing the three displacement calls separately before this fix showed that their nine inputs (position, scale and offset for each call) already matched 46,080/46,080 F32 pixels across six settings. + +Repeat the two modular fields at 37 × 23, with field indices 4 and 5. These non-power-of-two Bitmap outputs have the following remaining differences. A graph input bound to the same PNG matched the complete reference Bitmap output in all 80 settings, 116,736/116,736 pixels across both files, maximum zero. + +| Output | Renders | Exact pixels / total · maximum | +|---|---:|---:| +| LUM8 | 20 | 15,093/29,184 · 85 | +| LUM16 | 20 | 4,548/29,184 · 21878 | +| LUM16F | 20 | 5,057/29,184 · 0.333740234 | +| LUM32F | 20 | 16/29,184 · 0.333839174 | + +The new release regression failed before this change and passed afterward. All 324 node and 55 evaluator release tests passed, along with formatting, strict workspace Clippy, dependency layers and WebAssembly checks. + ## 2026-11-20 root comparisons ### Hue, saturation, lightness and Replace Color diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 00037f1c..530ac931 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -404,7 +404,7 @@ Creased matches 10,947,840/10,947,840 pixels across 100 distinct settings throug | [noise_upscale_2.sbs/noise_upscale_2](../assets/sbs/noise_upscale_2.sbs) | 1 | No | [Parity · 21 renders, 697,344 exact pixels](parity-fixes.md#sampling-integer-bitmap-inputs-through-wider-bridges) | | [noise_upscale_3.sbs/noise_upscale_3](../assets/sbs/noise_upscale_3.sbs) | 2 | No | [Parity · 21 renders, 697,344 exact pixels](parity-fixes.md#sampling-integer-bitmap-inputs-through-wider-bridges) | | [noise_voronoi.sbs/3d_voronoi](../assets/sbs/noise_voronoi.sbs) | 1 | No | [Parity · 174,080 exact pixels in 82 shared-lookup renders, bundled defaults differ](parity-fixes.md#shared-pixel-assignments-in-voronoi-color-selection) · [Saved graph comparisons](parity-fixes.md#package-ownership-of-saved-function-calls) · [Stock tables](parity-fixes.md#voronoi-distortion-and-shared-noise-inputs) | -| [noise_voronoi.sbs/3d_voronoi_fractal](../assets/sbs/noise_voronoi.sbs) | 1 | No | [In progress · 37 renders, 152,163/155,392 exact pixels with stock and shared tables](parity-fixes.md#voronoi-distortion-and-shared-noise-inputs) · [Saved function calls and linked noise packages](parity-fixes.md#package-ownership-of-saved-function-calls) · [Random-color assignments](parity-fixes.md#shared-pixel-assignments-in-voronoi-color-selection) · [82 original and 24 saved comparisons](parity-fixes.md#fractal-voronoi-roots-with-shared-lookup-tables) | +| [noise_voronoi.sbs/3d_voronoi_fractal](../assets/sbs/noise_voronoi.sbs) | 1 | No | [In progress · 37 renders, 152,163/155,392 exact pixels with stock and shared tables](parity-fixes.md#voronoi-distortion-and-shared-noise-inputs) · [Saved function calls and linked noise packages](parity-fixes.md#package-ownership-of-saved-function-calls) · [Random-color assignments](parity-fixes.md#shared-pixel-assignments-in-voronoi-color-selection) · [82 original and 24 saved comparisons](parity-fixes.md#fractal-voronoi-roots-with-shared-lookup-tables) · [7 shuffled-table cases now exact after Bitmap resizing](parity-fixes.md#stored-levels-when-resizing-a-grayscale-bitmap-resource) | | [noise_voronoi.sbs/voronoi](../assets/sbs/noise_voronoi.sbs) | 0 | No | [Parity · 174,080 exact pixels in 82 shared-lookup renders, bundled defaults differ](parity-fixes.md#shared-pixel-assignments-in-voronoi-color-selection) · [Saved graph comparisons](parity-fixes.md#package-ownership-of-saved-function-calls) · [Stock tables](parity-fixes.md#voronoi-distortion-and-shared-noise-inputs) | | [noise_voronoi.sbs/voronoi_fractal](../assets/sbs/noise_voronoi.sbs) | 0 | No | [Parity · 174,080 exact pixels in 82 shared-lookup renders, bundled defaults differ](parity-fixes.md#fractal-voronoi-roots-with-shared-lookup-tables) · [Saved graph comparisons](parity-fixes.md#package-ownership-of-saved-function-calls) · [Stock tables](parity-fixes.md#voronoi-distortion-and-shared-noise-inputs) | | [non_square_transform.sbs/non_square_transform](../assets/sbs/non_square_transform.sbs) | 1 | No | [Parity · 204,032 exact pixels in 23 renders](parity-fixes.md#rotated-transforms-and-fractional-texture-coordinates) |