From 5db3215260bf37f06248e49e911d71bb347031c9 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Fri, 20 Nov 2026 22:06:31 -0800 Subject: [PATCH] Rotated blends: every blend not exact by construction announces its rounding The planner's ruling on the rotated blend: the note no longer follows a measured table. A blend on an attachment held with its channels rotated (ARGB8Unorm) raises PrecisionNote::RotatedBlendRounding unless it is exact by construction: a minimum or a maximum, which takes no factor, or a blend whose chosen Vulkan factors are all ZERO or ONE, whose result is the source, the destination, zero, or their sum or difference, a whole code half a code from any tie. rotated_blend's MEASURED table and measured() go; unmeasured() becomes inexact(), which reads the state rotated_blend() chooses: a Synthesized state (SRC1 factors) is inexact, and a Direct one is inexact unless its four factors are ZERO or ONE. Newly announced, the nine states of the old table that are not exact by construction, each Metal's color pair and alpha pair under the one operation: - DestinationColor over Zero, DestinationAlpha over Zero, Add; - SourceColor over OneMinusSourceColor, SourceAlpha over OneMinusSourceAlpha, Add; - BlendAlpha over OneMinusBlendAlpha for both, Add; - the two straight-alpha pairs: SourceAlpha over OneMinusSourceAlpha with the alpha the same, and with the alpha One over OneMinusSourceAlpha, Add; - One over OneMinusSourceAlpha for both, Add and Subtract; - SourceAlpha over One with the alpha Zero over One, Add; - SourceAlpha over One for both, ReverseSubtract. One over Zero and One over One (Add) stay unannounced, now as exact by construction. Newly unannounced: blends outside the old table whose Vulkan factors are all ZERO or ONE, which Metal gives with one Zero or One factor for both its color and alpha sources and one for both its destinations, under Add, Subtract or ReverseSubtract (One over One under ReverseSubtract, Zero over One, and the like). Minimum and maximum stay unannounced. StageIo::rotated_blends and LoweringInputs::rotated_blends stay: they are now the attachments whose blend is not exact by construction. The stage raises the note from the latter, and color_writes::check reads the former to refuse a fragment compiled for another set than the pipeline's (NeedsInput(ColorWrites)), so the set still has a reader; its docs lose "measured". The docs/API.md catalogue entry, PrecisionNote::RotatedBlendRounding's doc, LoweringInputs' and StageIo's docs, rotated_blend's module doc, and validation's c3 deferral doc, its bound string and comparison.rs's allowance doc state the one rule. The validation files use the note to defer a row in C3 (c3.rs) and give it an unbounded allowance (comparison.rs); both follow the note, so more rows on rotated attachments are deferred, by the note alone. Tests: rotated_blend's measured-forms test becomes the_lane_s_blends_store_metal_s_rotated, over the old table kept as test data (LANE): every state stores Metal's blend rotated alike on MoltenVK and lavapipe, and all but one over zero and one over one are inexact. inexact_blends_are_announced adds a difference by ones and a held destination, exact. engine_rotated_colors expects the note on its product and its two second-output blends, and not on its minimum or its reverse subtraction of ones. SEMANTICS_VERSION 2237 (placeholder; main renumbers): the rotated blends' set, which the key carries, and the stage's notes change for the same descriptor. ARTIFACT_FORMAT_VERSION and RUNTIME_VERSION stay. api_signatures, docs/REFUSALS.md and the coverage are unchanged. Consumer check: unmeasured and MEASURED were pub(crate); no public name changes. Verified: m3-max-61259 at this tree (a5396ea291 before the colour commit's message was filled, msi-laptop): fmt, clippy (all features, --cfg m2v_gpu and --cfg m2v_cpu_icd) and Rustdoc rc 0; nextest over metal2vulkan and metal2vulkan-validation, 4719 run, 4718 passed, 23 skipped, 1 failed: the render key test (the_old_render_descriptor_key_keeps_its_value), left unpinned until the next named pin. Rebased onto development d10a14d5af: the SEMANTICS placeholder is one above its 2235 in stack order (ARTIFACT_FORMAT_VERSION is already above its 574). The checks above ran at the old base acc706ca47; the rebased stack's tip is checked by m3-max-61756 (msi-laptop), at the stack rebased on d6d56b8a7c (tip 5c74fad38d, whose diff from its base is this stack's diff from d10a14d5af): fmt, clippy (all features, --cfg m2v_gpu, --cfg m2v_cpu_icd) and Rustdoc green; nextest of metal2vulkan, metal2vulkan-validation, metal2vulkan-conformance and metal2vulkan-runtime ran 5877 tests, 5870 passed. Of the 7 failures, the old render descriptor key's test is unpinned; golden_hashes and engine inventory parts 4, 5, 7 and 8 are bless due (the profiles' fingerprints changed); and metal::state's encoder_raster_state_needs_its_features fails the same way at development d10a14d5af itself (m3-max-61802, where runtime-floor gained depthBiasClamp in a529fb658e), as inventory parts 3, 4, 5 and 8 do there. The stack as rebased on 72e1754218 (tip 090d544208: the same product changes, without the test-only push sweep fix) played 36 distinct streams over 61 runs on five nodes with 0 refused (player m3-max-61537; plays m3-max-61548, 61549, 61550, 61559 and 61569). --- crates/metal2vulkan-validation/src/c3.rs | 20 +-- .../metal2vulkan-validation/src/c3/tests.rs | 4 +- .../src/observation_contract/comparison.rs | 6 +- crates/metal2vulkan/src/cache/mod.rs | 2 +- .../metal2vulkan/src/lower/stage_io/plan.rs | 2 +- crates/metal2vulkan/src/pipeline/artifact.rs | 12 +- .../metal2vulkan/src/pipeline/color_writes.rs | 15 +- .../metal2vulkan/src/pipeline/lower_inputs.rs | 12 +- .../src/pipeline/rotated_blend.rs | 142 ++++-------------- .../src/pipeline/rotated_blend/tests.rs | 113 ++++++++++++-- crates/metal2vulkan/src/pipeline/stage_io.rs | 4 +- .../src/tests/engine_rotated_colors.rs | 13 +- docs/API.md | 8 +- 13 files changed, 181 insertions(+), 172 deletions(-) diff --git a/crates/metal2vulkan-validation/src/c3.rs b/crates/metal2vulkan-validation/src/c3.rs index 59eaed831..fe8f25f5f 100644 --- a/crates/metal2vulkan-validation/src/c3.rs +++ b/crates/metal2vulkan-validation/src/c3.rs @@ -747,10 +747,11 @@ pub struct SlotRow { /// another sample, and the stored samples are ordinary data later words use. Open until Metal's /// coverage pattern is computed in the function, on a device whose coverage is Metal's /// (elsewhere the device's coverage is its native answer, `DeviceAlphaCoverage`). -/// - An `ARGB8Unorm` attachment blended in none of the measured forms -/// (`RotatedBlendRounding`): Apple's blend computes a factor pair apart in its color and alpha -/// arithmetic, which the rotation swaps for Metal's alpha and blue, and the stored code is -/// ordinary data later words use. Open until the blend state has a measured form. +/// - An `ARGB8Unorm` attachment whose blend is not exact by construction +/// (`RotatedBlendRounding`): a blend may compute a factor pair apart in its color and alpha +/// arithmetic, as Apple's does, which the rotation swaps for Metal's alpha and blue, and the +/// stored code is ordinary data later words use. Open: no query states how a device's blend +/// rounds. /// /// A varying the device interpolates (`DeviceInterpolation`) is not deferred: every fragment /// function that reads one records it on every device but MoltenVK's, so a row-level deferral @@ -875,11 +876,12 @@ fn near_equivalent_bound(note: PrecisionNote) -> &'static str { where the device covers as Metal does" } PrecisionNote::RotatedBlendRounding => { - "an ARGB8Unorm attachment blended in none of the measured forms may store a channel \ - one code apart from Metal's: Apple's blend computes a factor pair apart in its color \ - and alpha arithmetic, and the rotation moves Metal's blue into the image's alpha \ - and Metal's alpha into its red (up to 1,767 of 65,536 tie-heavy texels over 440 \ - swept states); exact path: a measured form for the state" + "an ARGB8Unorm attachment whose blend is not exact by construction (all but a \ + minimum, a maximum, and Vulkan factors of zero and one) may store a channel one code \ + apart from Metal's: a blend may compute a factor pair apart in its color and alpha \ + arithmetic, as Apple's does, and the rotation moves Metal's blue into the image's \ + alpha and Metal's alpha into its red (up to 1,767 of 65,536 tie-heavy texels over \ + 440 swept states); no exact path: no query states how a device's blend rounds" } _ => "unbounded", } diff --git a/crates/metal2vulkan-validation/src/c3/tests.rs b/crates/metal2vulkan-validation/src/c3/tests.rs index 091b858c2..1f4a817f6 100644 --- a/crates/metal2vulkan-validation/src/c3/tests.rs +++ b/crates/metal2vulkan-validation/src/c3/tests.rs @@ -1470,8 +1470,8 @@ fn a_coverage_reads_rounded_alpha_row_is_deferred_and_never_a_reviewed_reading() ); } -/// An `ARGB8Unorm` attachment blended in none of the measured forms (`RotatedBlendRounding`) is -/// deferred the same way. +/// An `ARGB8Unorm` attachment whose blend is not exact by construction (`RotatedBlendRounding`) +/// is deferred the same way. #[test] fn a_rotated_blend_rounding_row_is_deferred_and_never_a_reviewed_reading() { deferred_and_never_reviewed( diff --git a/crates/metal2vulkan-validation/src/observation_contract/comparison.rs b/crates/metal2vulkan-validation/src/observation_contract/comparison.rs index 893a849e9..fbfac49fb 100644 --- a/crates/metal2vulkan-validation/src/observation_contract/comparison.rs +++ b/crates/metal2vulkan-validation/src/observation_contract/comparison.rs @@ -226,9 +226,9 @@ impl UlpTable { /// - `DeviceAlphaCoverage`: unbounded: the device's alpha to coverage may cover other samples /// than Metal's, a whole fragment stored or dropped at one sample, which moves every channel a /// resolve or a later blend reads there. -/// - `RotatedBlendRounding`: unbounded: an `ARGB8Unorm` attachment blended in none of the measured -/// forms may store a channel one code apart from Metal's, which moves every later word that reads -/// it. +/// - `RotatedBlendRounding`: unbounded: an `ARGB8Unorm` attachment whose blend is not exact by +/// construction may store a channel one code apart from Metal's, which moves every later word +/// that reads it. /// - `DeviceDenormals`, `DeviceNanEncoding`: [`Allowance::Recorded`]. Metal flushes `float` /// denormals and canonicalizes NaNs, a device need not, and every `float` operation the device /// performs is exposed to both; a `half` texel's NaN is the device's decode where Metal hands diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 97df4146d..0822d9b5c 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -61,7 +61,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 2236; +pub const SEMANTICS_VERSION: u32 = 2237; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/lower/stage_io/plan.rs b/crates/metal2vulkan/src/lower/stage_io/plan.rs index 04727c769..080d154b6 100644 --- a/crates/metal2vulkan/src/lower/stage_io/plan.rs +++ b/crates/metal2vulkan/src/lower/stage_io/plan.rs @@ -2510,7 +2510,7 @@ pub fn entry(cx: &mut SelectCx<'_>) -> Result<(), Error> { if let (0, Some(&lanes)) = (index, cx.inputs.rotated_factors.get(&attachment)) { seconds.push((attachment, o.field, link.form, lanes)); } - // A blend in none of the measured forms may round apart from Apple's + // A blend not exact by construction may round apart from Metal's // (`LoweringInputs::rotated_blends`). if index == 0 && cx.inputs.rotated_blends.contains(&attachment) { cx.precision.insert(PrecisionNote::RotatedBlendRounding); diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 8ce528943..9b5b0f393 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -1265,12 +1265,12 @@ pub enum PrecisionNote { /// mismatch for review, deferred in C3 until an exact path exists. BlendedWriteRounding, /// A fragment function writes an attachment held with its channels rotated (`ARGB8Unorm`) - /// whose blend is none of the measured forms (`LoweringInputs::rotated_blends`): Apple's blend - /// computes a factor pair apart in its color and alpha arithmetic, and the rotation moves - /// Metal's blue into the image's alpha and Metal's alpha into its red, so a channel may store - /// one code apart from Metal's (up to 1,767 of 65,536 tie-heavy texels over 440 swept blend - /// states on an M4). The exact path for a blend state is its measured form - /// (`pipeline::rotated_blend`'s table). + /// whose blend is not exact by construction (`LoweringInputs::rotated_blends`): every blend but + /// a minimum, a maximum, and one whose Vulkan factors are all `ZERO` or `ONE`. A blend may + /// compute a factor pair apart in its color and alpha arithmetic, as Apple's does, and the + /// rotation moves Metal's blue into the image's alpha and Metal's alpha into its red, so a + /// channel may store one code apart from Metal's (up to 1,767 of 65,536 tie-heavy texels over + /// 440 swept blend states on an M4). No query states how a device's blend rounds. /// /// Validation derives no tolerance and no explanation from it: a difference stays a /// mismatch for review, deferred in C3 until an exact path exists. diff --git a/crates/metal2vulkan/src/pipeline/color_writes.rs b/crates/metal2vulkan/src/pipeline/color_writes.rs index 0b7ec6790..6e7f00022 100644 --- a/crates/metal2vulkan/src/pipeline/color_writes.rs +++ b/crates/metal2vulkan/src/pipeline/color_writes.rs @@ -58,7 +58,7 @@ use crate::pipeline::descriptor::ColorAttachmentDescriptor; use crate::pipeline::facts::RuntimeFact; use crate::pipeline::graphics::ColorAttachmentState; use crate::pipeline::lower_inputs::{ChannelWrite, ExtendedRangeColor, FactorLane}; -use crate::pipeline::rotated_blend::{rotated_blend, unmeasured, RotatedBlend}; +use crate::pipeline::rotated_blend::{inexact, rotated_blend, RotatedBlend}; use crate::pipeline::stage_io::IoRole; /// The color writes of a fragment function writing the attachments `written` (dual-source index @@ -271,7 +271,8 @@ fn nearest_even( /// ([`wanted_extended_range`]), and every color it stores nowhere (`StageIo::unattached_colors`) /// has no attachment and is not color 0 under alpha to coverage, and the second outputs it writes /// for rotated attachments are the pipeline's ([`wanted_rotated_factors`]), and so are those of -/// their blends in none of the measured forms ([`wanted_rotated_blends`]), and the attachments holding Metal's alpha in red it +/// their blends not exact by construction ([`wanted_rotated_blends`]), and the attachments holding +/// Metal's alpha in red it /// writes or reads are the pipeline's ([`wanted_alpha_in_red`]), and its texture writes /// that Metal's mode 2 stores otherwise compiled under the pipeline's `textureWriteRoundingMode` /// (`StageIo::texture_write_rounding`), else `NeedsInput(ColorWrites)`. @@ -710,8 +711,8 @@ pub(crate) fn wanted_rotated_factors( out } -/// The attachments of `colors` held with their channels rotated whose blend is in none of the -/// measured forms on `p` (`LoweringInputs::rotated_blends`, `rotated_blend::unmeasured`). +/// The attachments of `colors` held with their channels rotated whose blend is not exact by +/// construction on `p` (`LoweringInputs::rotated_blends`, `rotated_blend::inexact`). pub(crate) fn rotated_blends( colors: &[Option], p: &DeviceProfile, @@ -722,13 +723,13 @@ pub(crate) fn rotated_blends( .filter_map(|(i, c)| { let c = c.as_ref()?; let rotated = metal::format::rotated_channels(c.pixel_format); - (rotated && unmeasured(c, p)).then_some(i as u32) + (rotated && inexact(c, p)).then_some(i as u32) }) .collect() } -/// The attachments held with their channels rotated whose blends in none of the measured forms -/// the fragment stage `fragment` compiles under in a render pipeline whose color attachments are +/// The attachments held with their channels rotated whose blends not exact by construction the +/// fragment stage `fragment` compiles under in a render pipeline whose color attachments are /// `colors` on `p`: [`rotated_blends`]'s, for the attachments it writes through a first /// (dual-source index 0) output. pub(crate) fn wanted_rotated_blends( diff --git a/crates/metal2vulkan/src/pipeline/lower_inputs.rs b/crates/metal2vulkan/src/pipeline/lower_inputs.rs index 65fe78b0e..6d4fd0053 100644 --- a/crates/metal2vulkan/src/pipeline/lower_inputs.rs +++ b/crates/metal2vulkan/src/pipeline/lower_inputs.rs @@ -715,12 +715,12 @@ pub struct LoweringInputs { serde(default, with = "crate::pipeline::serde_pairs") )] pub cubic_samplers: BTreeMap, - /// The attachments of `rotated_colors` blended in none of the measured forms, by color index - /// (`pipeline::rotated_blend`): Vulkan's factors store Metal's channels rotated, which Apple's - /// blend may round one code apart from Metal's (up to 1,767 of 65,536 tie-heavy texels in our - /// probe's sweep), and the function records `PrecisionNote::RotatedBlendRounding`. An - /// attachment blended in a measured form, under a minimum or a maximum, or not blended, is not - /// listed. `Device::compile_render_pipeline` sets it for a `Function` fragment stage from the + /// The attachments of `rotated_colors` whose blend is not exact by construction, by color + /// index (`pipeline::rotated_blend`): Vulkan's factors store Metal's channels rotated, which a + /// blend may round one code apart from Metal's (up to 1,767 of 65,536 tie-heavy texels on + /// Apple's in our probe's sweep), and the function records + /// `PrecisionNote::RotatedBlendRounding`. An attachment blended under a minimum or a maximum, or + /// by Vulkan factors that are all `ZERO` or `ONE`, or not blended, is not listed. `Device::compile_render_pipeline` sets it for a `Function` fragment stage from the /// pipeline's color attachments, and link refuses a fragment compiled under another set as /// `NeedsInput(ColorWrites)`. Empty for any other function. #[cfg_attr(feature = "serde", serde(default))] diff --git a/crates/metal2vulkan/src/pipeline/rotated_blend.rs b/crates/metal2vulkan/src/pipeline/rotated_blend.rs index 27ef00a3d..fe158abad 100644 --- a/crates/metal2vulkan/src/pipeline/rotated_blend.rs +++ b/crates/metal2vulkan/src/pipeline/rotated_blend.rs @@ -35,17 +35,17 @@ //! `NotImplemented(Format)`. A one minus a value the function would compute is not offered //! among Vulkan's factors: it would round apart from the blend's own. //! -//! **Measured forms.** Apple's blend does not compute every factor pair alike in its color and -//! alpha arithmetic, and the rotation moves Metal's blue into the image's alpha and Metal's alpha -//! into its red: under `SourceAlpha` over `OneMinusSourceAlpha` Metal's color is `fma(d, 1 - a, s * +//! **Rounding.** Apple's blend does not compute every factor pair alike in its color and alpha +//! arithmetic, and the rotation moves Metal's blue into the image's alpha and Metal's alpha into +//! its red: under `SourceAlpha` over `OneMinusSourceAlpha` Metal's color is `fma(d, 1 - a, s * //! a)`, where the image's alpha blends a factor and one minus it as `fma(f, s - d, d)`, which //! rounds one code apart near half-code ties. A swept 440 blend states held 1 to 1,767 differing -//! texels of 65,536 in 40 of them. The blend states [`MEASURED`] lists store Metal's bytes rotated -//! in the forms this module gives them on every lane device at the rotated-targets lane's texels, -//! and all but the two straight-alpha pairs (`SourceAlpha` over `OneMinusSourceAlpha` for color) -//! on the M4 and the M3 Max at 65,536 tie-heavy texels: under Apple's own blend those two may round -//! as above, unannounced. Every other blend state is the search's, announced -//! (`LoweringInputs::rotated_blends`, `PrecisionNote::RotatedBlendRounding`). +//! texels of 65,536 in 40 of them, and no query states how a device's blend rounds. So every blend +//! the rotation gives is announced (`LoweringInputs::rotated_blends`, +//! `PrecisionNote::RotatedBlendRounding`) but one exact by construction ([`inexact`]): a minimum +//! or a maximum, which takes no factor, and a blend whose Vulkan factors are all `ZERO` or `ONE`, +//! whose result is the source, the destination, zero, or their sum or difference, a whole code +//! half a code from any tie. use crate::device::requirements::VkFeature; use crate::device::DeviceProfile; @@ -71,112 +71,30 @@ pub(crate) enum RotatedBlend { Synthesized(BlendState, [FactorLane; 4]), } -/// The measured blend states (the module docs): the rotated-targets lane's blended cases, and -/// one over zero and one over one, whose arithmetic is the one channel's or one sum: Metal's -/// color source and destination factors, its alpha source and destination factors, and its one -/// operation. -const MEASURED: [([BlendFactor; 4], BlendOperation); 11] = { - use BlendFactor as F; - use BlendOperation as O; - [ - ([F::One, F::Zero, F::One, F::Zero], O::Add), - ([F::One, F::One, F::One, F::One], O::Add), - ( - [F::DestinationColor, F::Zero, F::DestinationAlpha, F::Zero], - O::Add, - ), - ( - [ - F::SourceColor, - F::OneMinusSourceColor, - F::SourceAlpha, - F::OneMinusSourceAlpha, - ], - O::Add, - ), - ( - [ - F::BlendAlpha, - F::OneMinusBlendAlpha, - F::BlendAlpha, - F::OneMinusBlendAlpha, - ], - O::Add, - ), - // Straight alpha. - ( - [ - F::SourceAlpha, - F::OneMinusSourceAlpha, - F::SourceAlpha, - F::OneMinusSourceAlpha, - ], - O::Add, - ), - // Straight alpha, Metal's alpha accumulated. - ( - [ - F::SourceAlpha, - F::OneMinusSourceAlpha, - F::One, - F::OneMinusSourceAlpha, - ], - O::Add, - ), - ( - [ - F::One, - F::OneMinusSourceAlpha, - F::One, - F::OneMinusSourceAlpha, - ], - O::Add, - ), - ([F::SourceAlpha, F::One, F::Zero, F::One], O::Add), - ( - [F::SourceAlpha, F::One, F::SourceAlpha, F::One], - O::ReverseSubtract, - ), - ( - [ - F::One, - F::OneMinusSourceAlpha, - F::One, - F::OneMinusSourceAlpha, - ], - O::Subtract, - ), - ] -}; - -/// Whether `a` blends by a measured blend state ([`MEASURED`]). -fn measured(a: &ColorAttachmentDescriptor) -> bool { - let factors = [ - a.source_rgb_blend_factor, - a.destination_rgb_blend_factor, - a.source_alpha_blend_factor, - a.destination_alpha_blend_factor, - ]; - a.rgb_blend_operation == a.alpha_blend_operation - && MEASURED.contains(&(factors, a.rgb_blend_operation)) -} - -/// Whether the blend of attachment `a`, held with its channels rotated, on `p` is in none of the -/// measured forms (`LoweringInputs::rotated_blends`, the module docs): a blend [`rotated_blend`] -/// admits, under neither a minimum nor a maximum (which takes no factor), that [`MEASURED`] does -/// not list. A blend that is off, and one no Vulkan state gives (which link refuses), is not. -pub(crate) fn unmeasured(a: &ColorAttachmentDescriptor, p: &DeviceProfile) -> bool { - if !matches!(rotated_blend(a, p), Ok(Some(_))) { - return false; - } - // The same resolution [`rotated_blend`] makes, so the form of a blend holding a placeholder is - // the form of the blend Metal draws with. - let a = &a.specialized(); +/// Whether the blend of attachment `a`, held with its channels rotated, on `p` is not exact by +/// construction (`LoweringInputs::rotated_blends`, the module docs): a blend [`rotated_blend`] +/// admits but a minimum or a maximum, which takes no factor, and one whose Vulkan factors are all +/// `ZERO` or `ONE`. A blend that is off, and one no Vulkan state gives (which link refuses), is not. +pub(crate) fn inexact(a: &ColorAttachmentDescriptor, p: &DeviceProfile) -> bool { + use vk::BlendFactor as B; + let state = match rotated_blend(a, p) { + Ok(Some(RotatedBlend::Direct(state))) => state, + Ok(Some(RotatedBlend::Synthesized(..))) => return true, + _ => return false, + }; + // The same resolution [`rotated_blend`] makes, so a blend holding a placeholder is the blend + // Metal draws with. let factorless = matches!( - a.rgb_blend_operation, + a.specialized().rgb_blend_operation, BlendOperation::Min | BlendOperation::Max ); - !factorless && !measured(a) + let factors = [ + state.src_color, + state.dst_color, + state.src_alpha, + state.dst_alpha, + ]; + !factorless && !factors.iter().all(|f| matches!(*f, B::ZERO | B::ONE)) } /// The value a blend factor takes at one channel, in Metal's channels (0 to 3). diff --git a/crates/metal2vulkan/src/pipeline/rotated_blend/tests.rs b/crates/metal2vulkan/src/pipeline/rotated_blend/tests.rs index fd57a9dd7..262d63eff 100644 --- a/crates/metal2vulkan/src/pipeline/rotated_blend/tests.rs +++ b/crates/metal2vulkan/src/pipeline/rotated_blend/tests.rs @@ -414,29 +414,105 @@ fn a_second_output_needs_dual_source_blending() { )); } -/// Every measured blend state is admitted on MoltenVK and on lavapipe and stores Metal's blend -/// rotated; it takes the search's form, alike on both devices, unannounced. +/// The rotated-targets lane's blended states, and one over zero and one over one: Metal's color +/// source and destination factors, its alpha source and destination factors, and its one +/// operation. +const LANE: [([BlendFactor; 4], BlendOperation); 11] = { + use BlendFactor as F; + use BlendOperation as O; + [ + ([F::One, F::Zero, F::One, F::Zero], O::Add), + ([F::One, F::One, F::One, F::One], O::Add), + ( + [F::DestinationColor, F::Zero, F::DestinationAlpha, F::Zero], + O::Add, + ), + ( + [ + F::SourceColor, + F::OneMinusSourceColor, + F::SourceAlpha, + F::OneMinusSourceAlpha, + ], + O::Add, + ), + ( + [ + F::BlendAlpha, + F::OneMinusBlendAlpha, + F::BlendAlpha, + F::OneMinusBlendAlpha, + ], + O::Add, + ), + ( + [ + F::SourceAlpha, + F::OneMinusSourceAlpha, + F::SourceAlpha, + F::OneMinusSourceAlpha, + ], + O::Add, + ), + ( + [ + F::SourceAlpha, + F::OneMinusSourceAlpha, + F::One, + F::OneMinusSourceAlpha, + ], + O::Add, + ), + ( + [ + F::One, + F::OneMinusSourceAlpha, + F::One, + F::OneMinusSourceAlpha, + ], + O::Add, + ), + ([F::SourceAlpha, F::One, F::Zero, F::One], O::Add), + ( + [F::SourceAlpha, F::One, F::SourceAlpha, F::One], + O::ReverseSubtract, + ), + ( + [ + F::One, + F::OneMinusSourceAlpha, + F::One, + F::OneMinusSourceAlpha, + ], + O::Subtract, + ), + ] +}; + +/// Every state of the rotated-targets lane is admitted on MoltenVK and on lavapipe and stores +/// Metal's blend rotated, alike on both devices; all but one over zero and one over one, exact by +/// construction, are announced, the straight-alpha pairs included. #[test] -fn measured_blends_take_their_measured_forms() { +fn the_lane_s_blends_store_metal_s_rotated() { let mvk = moltenvk(); let lvp = profile("lavapipe", DevicePolicy::default()).expect("profile"); - for &([sr, dr, sa, da], op) in &MEASURED { + for (i, &([sr, dr, sa, da], op)) in LANE.iter().enumerate() { let a = attachment((sr, dr, op), (sa, da, op)); let on_mvk = rotated_blend(&a, &mvk).expect("admitted").expect("blended"); let on_lvp = rotated_blend(&a, &lvp).expect("admitted").expect("blended"); check(&a, &on_mvk); check(&a, &on_lvp); - assert!(!unmeasured(&a, &lvp), "{a:?}"); - assert!(!unmeasured(&a, &mvk), "{a:?}"); + assert_eq!(inexact(&a, &lvp), i > 1, "{a:?}"); + assert_eq!(inexact(&a, &mvk), i > 1, "{a:?}"); assert_eq!(on_mvk, on_lvp, "{a:?}"); } } -/// A blend admitted in none of the measured forms is announced on every device; a minimum or -/// maximum (which takes no factor), a blend that is off, and a blend no Vulkan state gives are -/// not. +/// A blend admitted that is not exact by construction is announced on every device; a minimum or +/// maximum (which takes no factor), a blend by Vulkan factors of zero and one, a blend that is +/// off, and a blend no Vulkan state gives are not. #[test] -fn unmeasured_blends_are_announced() { +fn inexact_blends_are_announced() { use BlendFactor as F; use BlendOperation as O; let mvk = moltenvk(); @@ -449,16 +525,25 @@ fn unmeasured_blends_are_announced() { off.blending_enabled = false; for p in [&mvk, &lvp] { assert!(matches!(rotated_blend(&kept, p), Ok(Some(_)))); - assert!(unmeasured(&kept, p), "{p:?}"); + assert!(inexact(&kept, p), "{p:?}"); let max = attachment((F::One, F::One, O::Max), (F::One, F::One, O::Max)); assert!(matches!(rotated_blend(&max, p), Ok(Some(_)))); - assert!(!unmeasured(&max, p)); - assert!(!unmeasured(&off, p)); + assert!(!inexact(&max, p)); + let difference = attachment( + (F::One, F::One, O::ReverseSubtract), + (F::One, F::One, O::ReverseSubtract), + ); + let held = attachment((F::Zero, F::One, O::Add), (F::Zero, F::One, O::Add)); + for exact in [difference, held] { + assert!(matches!(rotated_blend(&exact, p), Ok(Some(_)))); + assert!(!inexact(&exact, p), "{exact:?}"); + } + assert!(!inexact(&off, p)); let refused = attachment( (F::DestinationAlpha, F::OneMinusDestinationAlpha, O::Add), (F::One, F::Zero, O::Add), ); - assert!(!unmeasured(&refused, p)); + assert!(!inexact(&refused, p)); } } diff --git a/crates/metal2vulkan/src/pipeline/stage_io.rs b/crates/metal2vulkan/src/pipeline/stage_io.rs index 450143d31..a2d474df6 100644 --- a/crates/metal2vulkan/src/pipeline/stage_io.rs +++ b/crates/metal2vulkan/src/pipeline/stage_io.rs @@ -252,8 +252,8 @@ pub struct StageIo { /// for another map than the pipeline's (`NeedsInput(ColorWrites)`). #[cfg_attr(feature = "serde", serde(default))] pub rotated_factors: BTreeMap, - /// The attachments held with their channels rotated whose blends in none of the measured - /// forms the function compiled under (`LoweringInputs::rotated_blends`), by color index: those + /// The attachments held with their channels rotated whose blends not exact by construction + /// the function compiled under (`LoweringInputs::rotated_blends`), by color index: those /// of the attachments it writes through a first output. Link refuses a stage compiled for /// another set than the pipeline's (`NeedsInput(ColorWrites)`). #[cfg_attr(feature = "serde", serde(default))] diff --git a/crates/metal2vulkan/src/tests/engine_rotated_colors.rs b/crates/metal2vulkan/src/tests/engine_rotated_colors.rs index ffaf7a2d4..8939f9a22 100644 --- a/crates/metal2vulkan/src/tests/engine_rotated_colors.rs +++ b/crates/metal2vulkan/src/tests/engine_rotated_colors.rs @@ -16,7 +16,7 @@ //! `RotatedInputBlend`), noting the device's conversion, and a device that orders it in neither //! form refuses naming both orderings; alpha to coverage over the image is refused //! `NotImplemented(Format)` at the attachment; -//! - a blend in none of the measured forms records `PrecisionNote::RotatedBlendRounding`. +//! - a blend not exact by construction records `PrecisionNote::RotatedBlendRounding`. //! //! No GPU is used. `llvm-as` assembles the fixture; a missing tool skips visibly. @@ -229,9 +229,10 @@ fn argb8_attachments_take_the_output_mask_and_blend_rotated() { let blend = p.graphics.color_attachments[0].and_then(|s| s.blend); assert!(blend.is_some_and(|b| !b.reads_second_output()), "{stem}"); assert!(!second_output(&p), "{stem}: {c:?}"); - // In none of the measured forms but a minimum's: announced. - let unmeasured = c.rgb_blend_operation != O::Min; - assert_eq!(announces_rounding(&p), unmeasured, "{stem}: {c:?}"); + // A product is announced; a minimum, and a difference by factors of one, + // are exact by construction. + let inexact = c.source_rgb_blend_factor == F::SourceColor; + assert_eq!(announces_rounding(&p), inexact, "{stem}: {c:?}"); } Err(Error::Unsupported(u)) if u.needs != Needs::NotImplemented => {} Err(e) => panic!("{stem}: {c:?}: {e}"), @@ -249,9 +250,9 @@ fn argb8_attachments_take_the_output_mask_and_blend_rotated() { let blend = p.graphics.color_attachments[0].and_then(|s| s.blend); assert!(blend.is_some_and(|b| b.reads_second_output()), "{stem}"); assert!(second_output(&p) && rotates(&p), "{stem}: {c:?}"); - // Copies of Metal's source alpha. Measured: no notice. + // Copies of Metal's source alpha, under factors that round: announced. assert!(shuffles(&p, [3, 3, 3, 3]), "{stem}: {c:?}"); - assert!(!announces_rounding(&p), "{stem}: {c:?}"); + assert!(announces_rounding(&p), "{stem}: {c:?}"); } Err(Error::Unsupported(u)) if !dual => { assert_eq!(u.feature, Feature::DualSourceBlending, "{stem}"); diff --git a/docs/API.md b/docs/API.md index 10af36b72..f06d92ce8 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2316,9 +2316,11 @@ bound. every other device and count is unmeasured. - `metal2vulkan-degrade-rotated-blend-rounding` (precision). `PrecisionNote::RotatedBlendRounding`: A fragment function writes an attachment held with its channels rotated (`ARGB8Unorm`) whose blend - is none of the measured forms (`LoweringInputs::rotated_blends`): Apple's blend computes a factor pair apart in its color - and alpha arithmetic, and the rotation moves Metal's blue into the image's alpha and Metal's alpha - into its red, so a channel may store one code apart from Metal's. + is not exact by construction (`LoweringInputs::rotated_blends`): every blend but a minimum, a + maximum, and one whose Vulkan factors are all `ZERO` or `ONE`. A blend may compute a factor pair + apart in its color and alpha arithmetic, as Apple's does, and the rotation moves Metal's blue into + the image's alpha and Metal's alpha into its red, so a channel may store one code apart from + Metal's. - `metal2vulkan-degrade-linked-sampler-numbering` (precision). `PrecisionNote::LinkedSamplerNumbering`: The sampler resource IDs a pipeline state takes for the constexpr samplers of the visible functions its stage links may be other than Metal's: two or more