From a5c9b6882eb2a315bcb6b9ffd5aa86945f5ab033 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Wed, 18 Nov 2026 06:33:18 -0500 Subject: [PATCH] Average filtered color channels in grayscale pixel reads --- crates/nodes/src/function/operation.rs | 11 +++++++++-- crates/nodes/src/function/tests.rs | 21 +++++++++++++++++++++ docs/parity-fixes.md | 12 ++++++++++++ docs/sbs-root-nodes.md | 8 +++++--- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/nodes/src/function/operation.rs b/crates/nodes/src/function/operation.rs index f15b485..99bfc93 100644 --- a/crates/nodes/src/function/operation.rs +++ b/crates/nodes/src/function/operation.rs @@ -123,7 +123,8 @@ pub(crate) fn evaluate( let index = usize::try_from(setting[0]).map_err(|_| "negative image index")?; // Pixel samplers repeat both texture axes independently of the processor's output tiling. let repeat = context.scope == super::FunctionScope::PixelProcessor || context.tile; - let pixel = match context.images.get(index).and_then(Option::as_ref) { + let image = context.images.get(index).and_then(Option::as_ref); + let pixel = match image { 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, repeat) } @@ -135,7 +136,13 @@ pub(crate) fn evaluate( None => [0.0; 4], }; - if op == "samplelum" { Value::Float(pixel[0]) } else { Value::Float4(pixel) } + if op == "samplecol" { + Value::Float4(pixel) + } else if context.scope == super::FunctionScope::PixelProcessor && image.is_some_and(|texture| texture.kind() == craft_texture::Kind::Color) { + Value::Float(((pixel[1] + pixel[2]) + pixel[0]) * (1.0 / 3.0)) + } else { + Value::Float(pixel[0]) + } } _ => return super::arithmetic::evaluate(instruction, arguments, random, context.scope), }) diff --git a/crates/nodes/src/function/tests.rs b/crates/nodes/src/function/tests.rs index 0ea065d..efd5ea2 100644 --- a/crates/nodes/src/function/tests.rs +++ b/crates/nodes/src/function/tests.rs @@ -90,6 +90,27 @@ fn pixel_product_sums_round_once_unless_the_product_is_also_read() { assert_eq!(compile(&graph, 0).unwrap().evaluate(&context, &mut EvaluationState::default()).unwrap(), Value::Float(f32::from_bits(0x3df49f20))); } +#[test] +fn pixel_gray_sampler_combines_stored_color_channels_after_reading() { + let mut graph = FunctionGraph::default(); + let position = graph.add_constant(Value::Float2([0.5; 2])); + graph.output = Some(graph.add("samplelum", &[position], Some(Value::Int2([0, 0])))); + let program = compile(&graph, 0).unwrap(); + let rgba = [0x40521096, 0xbfccf206, 0xbfa48635, 0x3f800000].map(f32::from_bits); + for (kind, expected) in [(Kind::Color, 0x3e0717d6), (Kind::Gray, rgba[0].to_bits())] { + let context = EvaluationContext { + scope: super::FunctionScope::PixelProcessor, + position: [0.5; 2], + size: [1.0; 2], + images: &[Some(Image::filled(1, 1, kind, rgba))], + image_formats: &[], + tile: true, + variables: &BTreeMap::new(), + }; + assert_eq!(program.evaluate_pixel(&context, &mut EvaluationState::default()).unwrap(), Value::Float(f32::from_bits(expected))); + } +} + #[test] fn pixel_dot_rounds_in_channel_order() { // One sampled texel distinguishes the accumulation orders in all three widths. diff --git a/docs/parity-fixes.md b/docs/parity-fixes.md index 1063e35..17b74d9 100644 --- a/docs/parity-fixes.md +++ b/docs/parity-fixes.md @@ -133,6 +133,18 @@ In the [atlas crop wrapper](#atlas-crop-with-wired-value-inputs), default rotati For the [spline distance controls](#height-distance-texture-mapping-skew-and-weave), both wired counts now match 1,024/1,024 at 32² seed 42 and 4,096/4,096 at 64² seed 137 in LUM32F. At 16² seed zero, count 1 matches 256/256 and count 2 matches 252/256, maximum 5.96046448e-8. +### Grayscale sampling of color inputs + +Feed an original 128² image to a Pixel Processor and read input zero with the grayscale sampler at pixel-center UV plus (.1875,.5625) texels. Use seed 42 and a LUM32F output. With nearest and bilinear filtering, RGBA8, RGBA16, RGBA16F and RGBA32F each match 16,384/16,384 pixels per mode. On the signed F32 input, reading only red previously matched zero pixels in both modes. Maximum errors were 4.95148039 and 3.83905315. Adding green and blue first, then red, and multiplying by the float32 reciprocal of three after sampling matches all eight color cases exactly. + +For RGBA8 use ((17x+31y) mod 256, floor(255x/127), 255−floor(255y/127), (7x+13y) mod 256). For the floating formats use the signed four-channel input under [3D linear gradient](#3d-linear-gradient). For RGBA16 encode round((channel+4)×65535/8) from that field. The eight grayscale-bridge controls also match all pixels. Their U8 input is (61x+137y+7xy) mod 256. Their U16 input is q=(2029x+4889y+193xy) mod 65536. For floating storage, supply a linear RGB image with the same float32 value q/8191−3.25 in each channel and alpha one, and select a grayscale bridge. The single stored channel is sampled directly. The sixteen comparisons match **262,144/262,144 pixels**, maximum error zero. A separate luminance-only float EXR was readable in the reference app and the current MaterialCraft decoder returned an error for that file. + +### Crop output and area + +The color and grayscale Crop roots each match **229,376/229,376** pixels across both outputs in the four original cases. Use the default graph at 128² and 256² with seed zero, repeat 128² with seed 42, then disable Show Crop Area, set Input Size to (128,128), Matrix to (1.2,.25,−.125,.8), Offset to (.1,−.15), and Filtering to 1 at 128². The inputs are the original foreground or grayscale ramp from the first pass. At 64² seed 42, retaining the 128² original input, these altered settings match a further 32,768/32,768 pixels per root across the two outputs and all four storage formats. Every maximum is zero. + +For the independent case, enable Show Crop Area, set Input Size to (64,128), Matrix to (.75,−.125,.375,1.25), Offset to (−.1875,.3125) and Filtering to 0. Use the original color or height field under [height distance, texture mapping and skew](#height-distance-texture-mapping-skew-and-weave) at the output dimensions. At 128² seed 71 F32 and 64² seed 137 in all four storage formats, each root’s Output matches 32,768/32,768 pixels. Area matches in U8, U16 and F32. At 64² F16, color Area matches 4,054/4,096 and grayscale Area matches 4,087/4,096. Both maxima are .00048828125. The last operation on both Area outputs is a blend. + ### Runtime angles and circular gradient The Circular Gradient root matches **163,840/163,840 pixels** in thirteen cases, maximum error zero. Its default outputs at 128² and 256² with seed zero and 128² with seed 42 match all 98,304 pixels. Set the two points to (.3,.6) and (.8,.7), angle .2 and non-square expansion off. At 128² seed zero the authored output matches 16,384/16,384. At 64² seed 42, U8, U16, F16 and F32 each match 4,096/4,096. For an independent rotation, use (.71875,.21875), (.0625,.8125), angle −.15625 and enable non-square expansion. The 128² F32 output at seed 71 and all four formats at 64² seed 137 match another 32,768/32,768 pixels. This root takes no image input. diff --git a/docs/sbs-root-nodes.md b/docs/sbs-root-nodes.md index 0c5aef6..e8cdd50 100644 --- a/docs/sbs-root-nodes.md +++ b/docs/sbs-root-nodes.md @@ -35,7 +35,9 @@ 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 **190 Parity**, **22 In progress** and **334 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 **190 Parity**, **24 In progress** and **332 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) ## Complete inventory @@ -156,8 +158,8 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard | [contrast_luminosity.sbs/contrast_luminosity](../assets/sbs/contrast_luminosity.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [contrast_luminosity.sbs/contrast_luminosity_grayscale](../assets/sbs/contrast_luminosity.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) | | [creased.sbs/creased](../assets/sbs/creased.sbs) | 0 | Yes | [Too hard: noise field](parity-fixes.md#remaining-differences) | -| [crop.sbs/crop](../assets/sbs/crop.sbs) | 1 | No | [Too hard: pixel differences](parity-fixes.md#remaining-differences) | -| [crop.sbs/crop_grayscale](../assets/sbs/crop.sbs) | 1 | No | [Too hard: pixel differences](parity-fixes.md#remaining-differences) | +| [crop.sbs/crop](../assets/sbs/crop.sbs) | 1 | No | [In progress · output exact, color area differs in half precision](parity-fixes.md#crop-output-and-area) | +| [crop.sbs/crop_grayscale](../assets/sbs/crop.sbs) | 1 | No | [In progress · output exact, grayscale area differs in half precision](parity-fixes.md#crop-output-and-area) | | [crystal_1.sbs/crystal_1](../assets/sbs/crystal_1.sbs) | 0 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) | | [crystal_2.sbs/crystal_2](../assets/sbs/crystal_2.sbs) | 0 | Yes | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) | | [curvature.sbs/curvature](../assets/sbs/curvature.sbs) | 1 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |