Match normal-copy mask and coverage rounding

This commit is contained in:
chasse20 committed 2026-11-18 04:28:05 -05:00
1 parent 3e0d3a3b64
commit a26a4c8f3a
4 files changed
+66 -2

No files matched your search

+36
View File
@@ -34,6 +34,42 @@ fn blend(context: &CookContext) -> CookResult {
Ok(vec![Image::generate(context.w, context.h, kind, |x, y| {
let (x, y) = (x as i64, y as i64);
let m = mask.as_ref().map_or(1.0, |m| m.get_gray(x, y, false));
// Normal copy reads the mask through its transparency before applying the global opacity.
let m = if mode == 0 && color_blending == 0 { 1.0 - (1.0 - m) } else { m };
ops::blend_px(mode, background.get(x, y, false), foreground.get(x, y, false), opacity * m, color_blending, blend_alpha, clamp)
})])
}
#[cfg(test)]
mod tests {
use super::*;
use crate::resolve_params;
use materialcraft_core::{OutputFormat, Value};
use std::collections::BTreeMap;
#[test]
fn normal_copy_rounds_mask_color_and_coverage_independently() {
let foreground = [116.0, 38.0, 60.0, 36.0].map(|v| v / 255.0);
let background = [158.0, 163.0, 10.0, 68.0].map(|v| v / 255.0);
let mask = 44.0 / 255.0;
for (opacity, alpha, hdr, expected) in [
(1.0, true, false, [0x3f1d97ad, 0x3f209511, 0x3d343117, 0x3e91adfe]),
(0.07, true, false, [0x3f1e8c37, 0x3f236cdd, 0x3d21ff39, 0x3e892c70]),
(0.3, false, false, [0x3f1e4fbd, 0x3f22b8df, 0x3d267f2b, 0x3e888889]),
(0.3, true, true, [0x3f514e8e, 0x3f4b8353, 0xbf55520b, 0xbdcc9712]),
] {
let level = |v: f32| if hdr { v * 3.0 - 1.0 } else { v };
let inputs = [
Some(Image::filled(1, 1, Kind::Color, foreground.map(level))),
Some(Image::filled(1, 1, Kind::Color, background.map(level))),
Some(Image::gray(1, 1, |_, _| level(mask))),
];
let vars = BTreeMap::new();
let params = resolve_params(&definition(), &BTreeMap::from([("opacity".into(), Value::Float(opacity)), ("alpha".into(), Value::Bool(alpha))]));
let context =
CookContext { format: OutputFormat::F32, w: 1, h: 1, seed: 0, tile: true, inputs: &inputs, params: &params, resource: &|_| None, vars: &vars };
let result = blend(&context).unwrap();
assert_eq!(result[0].get(0, 0, false).map(f32::to_bits), expected, "opacity {opacity}, alpha {alpha}, HDR {hdr}");
}
}
}
+16
View File
@@ -366,6 +366,22 @@ pub fn blend_channel(mode: i32, b: f32, f: f32) -> f32 {
/// `blend_alpha`: composite coverage; false preserves background alpha.
#[inline]
pub fn blend_px(mode: i32, bg: [f32; 4], fg: [f32; 4], opacity: f32, color_blending: i32, blend_alpha: bool, clamp: bool) -> [f32; 4] {
if mode == 0 && color_blending == 0 {
// The color weights use the complement of transparency. Coverage uses the original alpha.
let alpha = 1.0 - (1.0 - fg[3]);
let source_weight = alpha * opacity;
let background_weight = (-alpha).mul_add(opacity, 1.0);
let mut out = bg;
for k in 0..3 {
let color = bg[k].mul_add(background_weight, fg[k] * source_weight);
out[k] = if clamp { color.clamp(0.0, 1.0) } else { color };
}
if blend_alpha {
out[3] = (1.0 - bg[3]).mul_add(fg[3] * opacity, bg[3]);
}
return out;
}
// Switch already interpolates RGBA; another alpha weight composites twice
let t = opacity * if mode != 7 { fg[3] } else { 1.0 };
let coverage = bg[3] * (1.0 - t) + t;
+12
View File
@@ -95,6 +95,18 @@ Two other roots match the original four eight-bit cases at 128²/256², includin
Each format column compares all channels of 4,096 pixels. Clone uses 128² input fields in the 64² cases.
### Clone mask and alpha rounding
In the 64² Clone graph the transformed foreground, source image and blurred mask each matched all 4,096 source pixels. The final normal-copy Blend produced the entire U16 and F32 discrepancy. After correcting that operation, all eight cases listed above match **131,072/131,072 pixels**, maximum error zero. This includes 4,096/4,096 each in RGBA8, RGBA16, RGBA16F and RGBA32F at 64².
For an independent 64² Blend control, form byte-valued foreground `(17*x+31*y, 7*x+3*y, 11*x+5*y, 5*x+11*y)`, background `(29*x+13*y, 128+7*x, 5+5*y, 13*x+3*y)` and mask `7*x+9*y`, each modulo 256. Use default normal-copy blending and seed zero. Test each as linear U8 and as float32 normalized by 255. The grayscale float fields may be encoded as equal RGB channels with an opaque alpha and connected to a grayscale input.
For each input format, compare the varying fields, an opaque foreground, a white mask, white and black foreground RGB, white and black background RGB, zero background alpha, and constant foreground alpha 127 with mask 159. Repeat the first three controls with grayscale source and background. Each of these 24 float-output cases matches 4,096/4,096 pixels, maximum error zero.
With float inputs, repeat the varying field, black background RGB and zero background alpha at global opacity .3 and .625. Repeat at .3 with alpha blending disabled. For signed fields replace each normalized input component `v` with `3*v-1` and run at opacity .3 and 1. Finally test opacity .07 with and without the mask, and opacity 1e-8 with the mask. All 24 controls match 4,096/4,096 pixels each, maximum error zero.
The measured operation first takes the complement of mask transparency in float32, then multiplies by the global opacity. Color weights take the complement of foreground transparency. Output coverage uses the original foreground alpha. Fusing the background contribution into the weighted foreground reproduces the measured RGB rounding, including negative inputs and non-opaque coverage.
### Height to normal in world units
Both roots in `height_to_normal_world_units.sbs` match **131,072/131,072 RGBA pixels each** across eight cases. For a square grayscale height input, store `(17*x+31*y) mod 256` at each integer coordinate. Use default settings at 128² and 256², repeat 128² with seed 42, then set `surface_size=100`, `height_depth=8`, `normal_format=0` and `sampling=1` at 128² with seed zero. Each root matches 16,384, 65,536, 16,384 and 16,384 RGBA8 pixels respectively. Keep those changed settings and the 128² input, then cook at 64² with seed 42. U8, U16, F16 and F32 each match 4,096/4,096 pixels for both roots. Every maximum error is zero. Each 64² output contains 49 distinct normals.
+2 -2
View File
@@ -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 **170 Parity**, **21 In progress** and **355 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 **171 Parity**, **20 In progress** and **355 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)
## Complete inventory
@@ -115,7 +115,7 @@ The first pass covered all **546 roots**, with **166 Parity** and **380 Too hard
| [chrominance_extract.sbs/chrominance_extract](../assets/sbs/chrominance_extract.sbs) | 1 | No | [Too hard: remaining pixel differences](parity-fixes.md#remaining-differences) |
| [clamp.sbs/clamp](../assets/sbs/clamp.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
| [clamp.sbs/clamp_grayscale](../assets/sbs/clamp.sbs) | 1 | No | [Parity: 4 cases](parity-fixes.md#verified-cases) |
| [clone.sbs/clone](../assets/sbs/clone.sbs) | 2 | No | [In progress · U16 and F32 differ at 64²](parity-fixes.md#color-conversion-checker-and-clone) |
| [clone.sbs/clone](../assets/sbs/clone.sbs) | 2 | No | [Parity · eight cases, four formats](parity-fixes.md#clone-mask-and-alpha-rounding) |
| [clone_patch.sbs/transform_from_center](../assets/sbs/clone_patch.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
| [clone_patch.sbs/transform_to_center](../assets/sbs/clone_patch.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |
| [clone_patch.sbs/transform_to_center_grayscale](../assets/sbs/clone_patch.sbs) | 1 | Yes | [Parity: 4 cases](parity-fixes.md#verified-cases) |