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(