From bc78f8e74748d843fd6c4cb595954eded39d5b41 Mon Sep 17 00:00:00 2001 From: yoriswind <164435053+yoriswind@users.noreply.github.com> Date: Sat, 31 Oct 2026 00:09:43 -0700 Subject: [PATCH 1/3] Fix GPU Blend If gray rounding at whole channel levels --- crates/gpu/src/compose.wgsl | 6 ++++- crates/gpu/tests/it/parity.rs | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl index 09dd4cc415..0e7f3eadb4 100644 --- a/crates/gpu/src/compose.wgsl +++ b/crates/gpu/src/compose.wgsl @@ -1105,7 +1105,11 @@ fn blend_if_weight(this_px: vec4, under: vec4) -> f32 { } else { // compose::blend_if_gray: (30 R + 59 G + 11 B) / 100, rounded half down on whole levels. let sum = 30.0 * v.r + 59.0 * v.g + 11.0 * v.b; - let gray = select(sum / 100.0, floor((sum + 49.0) / 100.0), all(v == round(v))); + var gray = sum / 100.0; + if (all(v == round(v))) { + // Float division can undershoot an integer before floor, e.g. 9700 / 100. + gray = f32((u32(sum) + 49u) / 100u); + } k = k * bi_weight(0u, s, gray) * bi_weight(1u, s, v.r) * bi_weight(2u, s, v.g) * bi_weight(3u, s, v.b); } if (k <= 0.0) { diff --git a/crates/gpu/tests/it/parity.rs b/crates/gpu/tests/it/parity.rs index d22a4515d1..1516acde3f 100644 --- a/crates/gpu/tests/it/parity.rs +++ b/crates/gpu/tests/it/parity.rs @@ -2202,6 +2202,54 @@ fn stroke_effects_on_filled_and_stroked_shapes() { } } +#[test] +fn blend_if_gray_rounding_at_whole_levels() { + use photocraft_doc::BlendRange; + let Some(mut g) = gpu() else { return }; + // Exact .51, half-down ties, and nearby values from the CPU gray-rounding oracle. + for (rgb, gray) in [ + ([29u8, 141, 42], 97u8), + ([65, 0, 0], 19), + ([66, 0, 0], 20), + ([0, 0, 250], 27), + ([0, 0, 251], 28), + ([123, 126, 151], 128), + ([120, 129, 149], 128), + ([121, 129, 149], 129), + ] { + let c = rgb.map(|v| f32::from(v) / 255.0); + for side in 0..2 { + for cut in [gray, gray + 1] { + let color = [c[0], c[1], c[2], 1.0]; + let other = [1.0, 0.0, 1.0, 1.0]; + let pixels = if side == 0 { [other, color] } else { [color, other] }; + let mut d = Document::new("gray rounding", Size::new(1, 1), ColorMode::Rgb, SampleType::U8); + for px in pixels { + let mut l = Layer::raster("pixel", PixelFormat::RGBA8); + l.surface_mut().unwrap().write_region(d.bounds(), &px); + d.layers.push(l); + } + let mut ranges = [BlendRange::FULL; 2]; + ranges[side] = BlendRange { black: [cut, cut], white: [255, 255] }; + d.layers[1].blend_if.set(0, ranges); + let expected = pixels[usize::from(cut == gray)]; + assert_eq!(photocraft_compose::render(&d, d.bounds()).px[0], expected); + check(&mut g, &d, &format!("gray {rgb:?}, side {side}, black {cut}")); + } + } + } + + // Deeper colors between whole levels retain fractional gray: 115.3875, not 115. + let mut d = Document::new("fractional gray", Size::new(1, 1), ColorMode::Rgb, SampleType::F32); + for px in [[0.25, 0.5, 0.75, 1.0], [1.0; 4]] { + let mut l = Layer::raster("pixel", PixelFormat::RGBA32F); + l.surface_mut().unwrap().write_region(d.bounds(), &px); + d.layers.push(l); + } + d.layers[1].blend_if.set(0, [BlendRange::FULL, BlendRange { black: [115, 116], white: [255, 255] }]); + check(&mut g, &d, "fractional gray"); +} + /// Blend If on the GPU matches `compose::apply_blend_if` (#4395): this-layer and underlying /// ranges, hard and split points, per channel, on raster, masked, adjustment and clipped layers, /// with channel restrictions, and in grayscale. Hard points are compared on 8-bit documents only: From cd0a3da4c5c45909bccd311c4e9b6da374c88e12 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Sat, 31 Oct 2026 05:51:54 -0700 Subject: [PATCH 2/3] Document Blend If gray rounding regression coverage --- crates/gpu/tests/it/parity.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/gpu/tests/it/parity.rs b/crates/gpu/tests/it/parity.rs index 1516acde3f..45e03aba4c 100644 --- a/crates/gpu/tests/it/parity.rs +++ b/crates/gpu/tests/it/parity.rs @@ -2202,6 +2202,7 @@ fn stroke_effects_on_filled_and_stroked_shapes() { } } +/// Whole-level gray rounds half down at Blend If cutoffs; fractional gray retains its precision. #[test] fn blend_if_gray_rounding_at_whole_levels() { use photocraft_doc::BlendRange; From 029b0fc4c70f20ffa64afb992280b8ee90339657 Mon Sep 17 00:00:00 2001 From: yoriswind <164435053+yoriswind@users.noreply.github.com> Date: Mon, 2 Nov 2026 22:48:28 -0800 Subject: [PATCH 3/3] ci: retrigger checks after contributor approval