From 971298dd9811150eca16bfd68879dce58cfb50d9 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 09:33:09 -0800 Subject: [PATCH] Rank a color lane's one minus the destination alpha as the source alpha's in the 8-bit unorm blend The fragment function's 8-bit unorm blend (extended_range::Blend::unorm8) picks which product of an added pair it rounds first by factor rank. For DestinationAlpha over OneMinusDestinationAlpha, both ranked 0, so it rounded the destination's product first. Plain Metal on the M3 Max rounds the source's first: in the argb8-targets-destination-alpha recording (RGBA8Unorm, a float4 output under fast math), f(fma(d, f(1 - da), f(s * da))) gives Metal's code in every color channel of 8,192 texels, and the destination-first form misses 10 of them. Those are exactly the 10 elements the shader-blended ARGB8Unorm target read differently on MoltenVK (m3-max-4198). A color lane's one minus the destination alpha now ranks 1, as the one minus the source alpha does. No pair the blend8 tests or the validation lane measured contains it in a color factor, so no measured blend moves. The same rank in half arithmetic is not measured, and the doc says so. Test: blend8's fetched-8-bit-unorm state table gains this blend (alpha as source alpha over), checked on the interpreter against the measured form in both arithmetics and both maths. The fit was checked offline against the recording's texels. (cherry picked from commit 75e73605c7b6aa28857b24847bbe5b0ca8d34bc3) --- .../src/lower/arith/extended_range/blend.rs | 17 ++++++++-- .../src/lower/arith/tests/blend8.rs | 32 ++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs b/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs index 3730412d3..7b41904af 100644 --- a/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs +++ b/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs @@ -630,6 +630,14 @@ fn alpha_rank(f: BlendFactor) -> u8 { /// An 8-bit unorm attachment's factor rank, of a color factor or alpha's where `alpha`, in the /// arithmetic `arith`: of an added pair, the destination's product is rounded first where the /// source factor's rank is at least the destination factor's, else the source's. +/// +/// In a color lane the one minus the destination alpha ranks as the one minus the source alpha +/// does. Plain Metal on the M3 Max under macOS 27.0.1 blends `DestinationAlpha` over +/// `OneMinusDestinationAlpha` with the source's product rounded first, into `RGBA8Unorm` from a +/// `float4` output under fast math: `f(fma(d, f(1 - da), f(s ยท da)))` gives Metal's code in every +/// color channel of the 8,192 texels of the `argb8-targets-destination-alpha` recording, and the +/// destination's product rounded first misses 10 of them. The same rank in `half` arithmetic is +/// not measured. fn unorm8_rank(f: BlendFactor, alpha: bool, arith: BlendArith) -> u8 { use BlendFactor as F; let float = arith == BlendArith::Float; @@ -638,10 +646,13 @@ fn unorm8_rank(f: BlendFactor, alpha: bool, arith: BlendArith) -> u8 { | F::SourceAlpha | F::DestinationColor | F::OneMinusDestinationColor - | F::DestinationAlpha - | F::OneMinusDestinationAlpha => 0, + | F::DestinationAlpha => 0, + F::OneMinusDestinationAlpha if alpha => 0, F::OneMinusSourceColor | F::OneMinusSourceAlpha if alpha && !float => 0, - F::OneMinusSourceColor | F::OneMinusSourceAlpha | F::SourceAlphaSaturated => 1, + F::OneMinusSourceColor + | F::OneMinusSourceAlpha + | F::OneMinusDestinationAlpha + | F::SourceAlphaSaturated => 1, F::BlendColor | F::BlendAlpha if float => 1, F::BlendColor | F::BlendAlpha => 2, F::OneMinusBlendColor | F::OneMinusBlendAlpha => 3, diff --git a/crates/metal2vulkan/src/lower/arith/tests/blend8.rs b/crates/metal2vulkan/src/lower/arith/tests/blend8.rs index 16f14cc65..7a65b8016 100644 --- a/crates/metal2vulkan/src/lower/arith/tests/blend8.rs +++ b/crates/metal2vulkan/src/lower/arith/tests/blend8.rs @@ -484,7 +484,7 @@ fn the_factor_set_masks_and_blend_color_store_metals_code() { ColorWriteMask, Option
, Option, - ); 4] = [ + ); 5] = [ // Source alpha over: the color's source product rounded first; alpha a lerp of the // source alpha from the destination's. ( @@ -513,6 +513,36 @@ fn the_factor_set_masks_and_blend_color_store_metals_code() { } }), ), + // The destination alpha and its one minus: the color's source product rounded first, as + // its one minus ranks as the source alpha's does (`argb8-targets-destination-alpha`'s + // `RGBA8Unorm` texels, a `float4` output under fast math: every color channel of 8,192; + // `half` arithmetic takes the same rank, not measured); alpha as source alpha over's. + ( + eq( + (F::DestinationAlpha, F::OneMinusDestinationAlpha, O::Add), + (F::SourceAlpha, F::OneMinusSourceAlpha, O::Add), + ), + PixelFormat::BGRA8Unorm, + ColorWriteMask::ALL, + Some(|ar, math, l| { + let m = rounded(ar, 1.0 - l.da); + match math { + BlendMath::Fast => fused(ar, (l.d, m, rounded(ar, l.s * l.da))), + BlendMath::Rounded => { + rounded(ar, rounded(ar, l.s * l.da) + rounded(ar, l.d * m)) + } + } + }), + Some(|ar, math, l| { + let m = rounded(ar, 1.0 - l.sa); + match math { + BlendMath::Fast => fused(ar, (l.sa, rounded(ar, l.sa - l.da), l.da)), + BlendMath::Rounded => { + rounded(ar, rounded(ar, l.sa * l.sa) + rounded(ar, l.da * m)) + } + } + }), + ), // The blend color and its one minus: lerps of it under fast math. ( eq(