diff --git a/crates/metal2vulkan-validation/src/runtime_lane/driver_deviations.rs b/crates/metal2vulkan-validation/src/runtime_lane/driver_deviations.rs index c81490663..b61a9237c 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/driver_deviations.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/driver_deviations.rs @@ -979,14 +979,18 @@ fn float_denormals_read_as_metal_reads_them() { } /// The probe's cases ([`HALF_FMA_CASES`]): every word, `fma(a, b, c)`, `a * b + c`, the two `half` -/// results and the lone product `a * w`, is the Metal row's of the same chip where the profile -/// takes `DriverDeviation::MetalFloatDenormals`: the product answers Metal's flushed denormal -/// (2.2), and the fused pair Metal's canonical NaN where the profile takes -/// `DriverDeviation::MetalCanonicalNans`, a NaN of the device's bits elsewhere -/// (`PrecisionNote::NativeNanEncoding`, [`float_word_holds`]); the two `half` results within what -/// the stage's notes record ([`half_pair_within`]: the device's `half` `Fma` on the RTX 5080 -/// Laptop GPU, a unit in the last place from Metal's in 300 threads). Elsewhere the denormals are -/// the device's and the test prints how many words differ. +/// results and the lone product `a * w`, is the Metal row's of the same chip where the kernel +/// leaves its `float` denormals to the device as Metal's (it requires +/// `VkProperty::MetalFloatDenormals`): the product answers Metal's flushed denormal (2.2), and the +/// fused pair Metal's canonical NaN where the profile takes `DriverDeviation::MetalCanonicalNans`, +/// a NaN of the device's bits elsewhere (`PrecisionNote::NativeNanEncoding`, +/// [`float_word_holds`]); the two `half` results within what the stage's notes record +/// ([`half_pair_within`]: the device's `half` `Fma` on the RTX 5080 Laptop GPU, a unit in the last +/// place from Metal's in 300 threads). The kernels compute `half` in 16 bits, so their entries +/// declare the 16-bit float controls where the profile has them: there NVIDIA's driver keeps +/// `f32` denormals (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`), and the kernel +/// requires no such property. Elsewhere the denormals are the device's, the stage records +/// `PrecisionNote::DeviceDenormals`, and the test prints how many words differ. #[test] fn fused_pairs_answer_metals_nans_and_denormals() { let messages = Messages::default(); @@ -994,7 +998,6 @@ fn fused_pairs_answer_metals_nans_and_denormals() { { let device = validated_device_with(TEST_DEVICE, &messages, cache.path(), at_plan).unwrap(); let name = device.name().to_owned(); - let metals = takes(&device, DriverDeviation::MetalFloatDenormals); let canonical = takes(&device, DriverDeviation::MetalCanonicalNans); let word = |bytes: &[u8], i: usize| { u32::from_le_bytes(bytes[4 * i..4 * i + 4].try_into().unwrap()) @@ -1003,6 +1006,12 @@ fn fused_pairs_answer_metals_nans_and_denormals() { for case_name in HALF_FMA_CASES { let case = public_case(case_name).unwrap(); let notes = case_notes(&device, &case); + let metals = requires_property(&device, &case, VkProperty::MetalFloatDenormals); + assert!( + metals || notes.contains(&PrecisionNote::DeviceDenormals), + "{case_name} on {name}: the kernel leaves its denormals to the device unmeasured, \ + and the stage does not record it" + ); let run = run_case(&device, &case).unwrap(); let metal = metal_row(&case, METAL_ENVIRONMENT); assert_eq!(run.output.len(), metal.len(), "{case_name} on {name}"); @@ -1021,7 +1030,7 @@ fn fused_pairs_answer_metals_nans_and_denormals() { } } eprintln!( - "fused pairs {case_name} on {name} (the device's denormals, as Metal's: {metals}; \ + "fused pairs {case_name} on {name} (the kernel's denormals, as Metal's: {metals}; \ notes {notes:?}): {} of {} words differ from Metal, {within} more within what the \ notes record", differ.len(), diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 42ba29452..46ddccb94 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 = 2115; +pub const SEMANTICS_VERSION: u32 = 2116; /// 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/device/driver_facts.rs b/crates/metal2vulkan/src/device/driver_facts.rs index fa9afdcc7..0ac224cf1 100644 --- a/crates/metal2vulkan/src/device/driver_facts.rs +++ b/crates/metal2vulkan/src/device/driver_facts.rs @@ -138,7 +138,10 @@ pub(crate) fn among( /// the RTX 3090 likewise. lavapipe keeps them (Mesa 26.0.8 on LLVM 21.1.8, x86_64 and arm64: /// 1,249 and 1,032 words of the two probes differ), so it is not listed. NVIDIA's `Floor` and /// `Ceil` keep a denormal, which the deviation does not cover: every profile leaves their -/// denormals to the device, noted (`lower::arith::denormal`). One row per GPU, as measured. +/// denormals to the device, noted (`lower::arith::denormal`). The probes declare no float-controls +/// mode; in an entry that declares the 16-bit controls NVIDIA's driver keeps `f32` denormals +/// (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`, in `DEFECTS`), and there the +/// deviation is not taken. One row per GPU, as measured. /// /// `MetalCenterVaryings`: MoltenVK 1.4.1 hands Metal a smooth input decorated neither `Centroid` /// nor `Sample` with no interpolation qualifier (SPIRV-Cross `adec7acb`, `spirv_msl.cpp` @@ -865,6 +868,15 @@ const fn moltenvk_on_apple(fact: F) -> Measured { /// without, and its linear sampler's pipeline in 8.9 to 11.9 s against 14.8 to 14.9 s, drawing in /// 0.32 against 0.43 ms. Its rows name those two GPUs, the GPUs measured; another NVIDIA GPU /// takes it once measured. +/// - `FloatDenormalsKeptUnderHalfControls`: NVIDIA's 615.71.09 on the RTX 5080 Laptop GPU and +/// 620.70.06 on the RTX 3090 kept `f32` denormals in an entry declaring the 16-bit float +/// controls, where their `MetalFloatDenormals` rows' probes, which declare no float-controls +/// mode, read them as Metal does: the gate lane +/// `runtime_lane::driver_deviations::fused_pairs_answer_metals_nans_and_denormals` (2026-11-14, +/// at 400efe7932), 33 of 4,096 words of each of its two fixtures. RADV, Mesa 26.2.6 on the +/// Radeon 780M (Phoenix and Hawk Point), answered every word of both as Metal's under the same +/// modes, so it is not listed. One row per GPU, the GPUs NVIDIA's `MetalFloatDenormals` rows +/// name. pub(crate) const DEFECTS: &[Measured] = &[ Measured { fact: DriverWorkaround::BothBarycentricFormsRejected, @@ -926,6 +938,14 @@ pub(crate) const DEFECTS: &[Measured] = &[ DriverWorkaround::DontInlineKeepsCalls, NVIDIA_RTX_5080_LAPTOP, ), + nvidia_on( + DriverWorkaround::FloatDenormalsKeptUnderHalfControls, + NVIDIA_RTX_3090, + ), + nvidia_on( + DriverWorkaround::FloatDenormalsKeptUnderHalfControls, + NVIDIA_RTX_5080_LAPTOP, + ), ]; /// The defects translation works around on the driver `description` names, on its GPU. diff --git a/crates/metal2vulkan/src/device/profile.rs b/crates/metal2vulkan/src/device/profile.rs index c0a95c995..f95730051 100644 --- a/crates/metal2vulkan/src/device/profile.rs +++ b/crates/metal2vulkan/src/device/profile.rs @@ -1400,6 +1400,29 @@ pub enum DriverWorkaround { /// in a function holding such a write, where it is defined (`spirv::serialize::serialize_for`), /// which SPIRV-Cross declares there. LocalizedLoadsForwardedPastAtomicsAndCalls, + /// The driver keeps `f32` denormals, operands and results, in an entry that declares the + /// 16-bit float controls (`RoundingModeRTE`, `DenormPreserve` and `SignedZeroInfNanPreserve` + /// for 16 bits, `lower::arith::narrow::controlled_modes`) and no 32-bit denormal mode, where + /// it reads them as Metal does in an entry that declares no float-controls mode, the scope + /// `DriverDeviation::MetalFloatDenormals` was measured in (its probes compute no `half`). + /// Vulkan leaves the 32-bit denormals of such an entry to the implementation, so this is the + /// device's answer, not a defect. Measured by the gate lane + /// `runtime_lane::driver_deviations::fused_pairs_answer_metals_nans_and_denormals` (the owned + /// fixtures `fmx_fma_precise` and `fmx_fma_fast`, which compute `half` in 16 bits beside + /// `float` `fma(a, b, c)`, `a * b + c` and `a * w`) on NVIDIA's + /// (`VK_DRIVER_ID_NVIDIA_PROPRIETARY`) 615.71.09 on the RTX 5080 Laptop GPU and 620.70.06 on + /// the RTX 3090 (2026-11-14, at 400efe7932): in each fixture 33 of 4,096 words keep a + /// denormal where Metal answers zero, the lone product's alike (`1e-20 * 1e-20` answers + /// `0x000116c2`, the smallest denormal times `2^30` answers `0x04000000`). The driver states + /// no `shaderDenormFlushToZeroFloat32`, so no mode makes them Metal's. RADV, Mesa 26.2.6 on + /// the Radeon 780M (Phoenix and Hawk Point), declares the same entries' modes and answered + /// every word of both fixtures as Metal's; MoltenVK states no `shaderDenormPreserveFloat16`, + /// so its entries declare no 16-bit mode. Keyed on the GPUs whose + /// `DriverDeviation::MetalFloatDenormals` rows NVIDIA's driver has, in + /// `device::driver_facts`. There such an entry's `float` operations that the deviation covers + /// record `PrecisionNote::DeviceDenormals` and require no `VkProperty::MetalFloatDenormals` + /// (`pipeline::compile`); an entry that declares no such mode keeps the deviation. + FloatDenormalsKeptUnderHalfControls, /// The driver groups a draw's vertices into subgroups otherwise than Apple's GPUs group them /// into simdgroups: ANV (`VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA`) on Mesa 26.2.4 ran the /// vertex fixtures `rvc_simd_vertex` and `rvc_simd_vertex_fill` (forty points) in subgroups diff --git a/crates/metal2vulkan/src/device/requirements.rs b/crates/metal2vulkan/src/device/requirements.rs index 078648dcd..7411db28d 100644 --- a/crates/metal2vulkan/src/device/requirements.rs +++ b/crates/metal2vulkan/src/device/requirements.rs @@ -669,7 +669,10 @@ pub enum VkProperty { /// No Vulkan query reports it: `DenormFlushToZero` makes only the results' flush a /// requirement, the operands' remains the device's choice. A module that leaves such an /// operation's denormals to the device, recording no `PrecisionNote::DeviceDenormals` for it, - /// requires it; without it, they are the device's own, and the module records that note. + /// requires it; without it, they are the device's own, and the module records that note. So + /// does a module whose entry declares the 16-bit float controls on a driver measured to keep + /// `f32` denormals there (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`), which + /// requires no such property. MetalFloatDenormals, /// The device reads a center-sampled varying of a multisampled pipeline at the pixel center /// in every fragment invocation, a sample-rate one's included, as Metal does (the driver's diff --git a/crates/metal2vulkan/src/error/mod.rs b/crates/metal2vulkan/src/error/mod.rs index a71d9c338..9f3176334 100644 --- a/crates/metal2vulkan/src/error/mod.rs +++ b/crates/metal2vulkan/src/error/mod.rs @@ -624,8 +624,12 @@ pub enum DriverDeviation { /// The device's `float` arithmetic reads an `f32` denormal operand as the zero of its sign /// and flushes a denormal result to one, as Metal does (`VkProperty::MetalFloatDenormals`): /// `OpFAdd`, `OpFSub`, `OpFMul`, `OpFmaKHR`, `OpDot`, the comparisons, and `GLSL.std.450`'s - /// `Ldexp`, `Sqrt`, `InverseSqrt`, `Trunc` and `RoundEven`. Unmeasured, such operands and - /// results are the device's own, recorded as `PrecisionNote::DeviceDenormals`. + /// `Ldexp`, `Sqrt`, `InverseSqrt`, `Trunc` and `RoundEven`. Measured in entries that declare + /// no float-controls mode: in an entry that declares the 16-bit float controls, on a driver + /// measured to keep `f32` denormals there + /// (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`), they are the device's own. + /// Unmeasured, such operands and results are the device's own, recorded as + /// `PrecisionNote::DeviceDenormals`. MetalFloatDenormals, /// The device reads a center-sampled varying (a smooth input decorated neither `Centroid` /// nor `Sample`) of a multisampled pipeline at the pixel center in every fragment diff --git a/crates/metal2vulkan/src/lower/arith/mod.rs b/crates/metal2vulkan/src/lower/arith/mod.rs index 0889470d3..86e8ff9aa 100644 --- a/crates/metal2vulkan/src/lower/arith/mod.rs +++ b/crates/metal2vulkan/src/lower/arith/mod.rs @@ -737,7 +737,10 @@ pub struct ArithPlan { pub f16_fma: bool, /// Whether a `float` operation leaves its `f32` denormals to the device because the device /// reads them as Metal does ([`denormal::device_flushes`]), recorded at selection: the module - /// then requires `VkProperty::MetalFloatDenormals`. + /// then requires `VkProperty::MetalFloatDenormals`, or, where its entry declares the 16-bit + /// float controls on a driver measured to keep `f32` denormals there + /// (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`), records + /// `PrecisionNote::DeviceDenormals` (`pipeline::compile`). pub device_denormals: bool, /// Whether a `float` negation, absolute value, floor or ceiling leaves its `f32` denormals to /// the device because the device reads them as Metal does ([`denormal::unary_kept`]), diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 14a1e6aae..c01717f6b 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -714,7 +714,10 @@ pub enum PrecisionNote { /// operation's denormals to a device not measured to read them as Metal does: `+ - *`, the /// comparisons, `fma`, `dot`, `sqrt`, `rsqrt`, `trunc`, `rint` and `ldexp` on `float` (by the /// device's products) but on a device that takes - /// the measured `DriverDeviation::MetalFloatDenormals`; `fneg`, `fabs`, `floor` and `ceil` on + /// the measured `DriverDeviation::MetalFloatDenormals`, and those on such a device too in an + /// entry that declares the 16-bit float controls where its driver is measured to keep them + /// there (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`: NVIDIA's on the RTX 3090 and + /// the RTX 5080 Laptop GPU); `fneg`, `fabs`, `floor` and `ceil` on /// `float` but on a device that takes the measured `DriverDeviation::MetalUnaryDenormals`; and /// on every device the divisions and the remainder, the device's maths functions /// (`fast::fmod`'s fused multiply-add, `sign` and `fract` among them), `mix` by `FMix`, `fast::sqrt` diff --git a/crates/metal2vulkan/src/pipeline/compile.rs b/crates/metal2vulkan/src/pipeline/compile.rs index 9f51712b2..949ad3f8e 100644 --- a/crates/metal2vulkan/src/pipeline/compile.rs +++ b/crates/metal2vulkan/src/pipeline/compile.rs @@ -3032,12 +3032,12 @@ fn compile_bounded( // folds the round trip (`DriverWorkaround::HalfConversionPairFolded`, so `Converted`) the // pair's rounding is the device's whatever the entry declares, recorded below, and the rest // is Metal's under them as elsewhere. - if controls + let declares_controls = controls && (widens || crate::lower::arith::narrow::computes_in_16_bits(&module) || (quantize == crate::spirv::builder::QuantizeForm::Controlled - && crate::lower::arith::narrow::rounds_by_conversion(&module, m)?)) - { + && crate::lower::arith::narrow::rounds_by_conversion(&module, m)?)); + if declares_controls { let modes = crate::lower::arith::narrow::controlled_modes(p).unwrap_or_default(); module.declare_modes(&modes)?; } @@ -3184,11 +3184,23 @@ fn compile_bounded( .properties .insert(crate::device::requirements::VkProperty::MetalFullQuads); } - // `float` denormals left to the device because it reads them as Metal does. + // `float` denormals left to the device because it reads them as Metal does: measured in + // entries that declare no float-controls mode, so in one that declares the 16-bit controls, + // on a driver measured to keep them there + // (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`), they are the device's own, noted. if plan.families.arith.device_denormals { - requirements - .properties - .insert(crate::device::requirements::VkProperty::MetalFloatDenormals); + let kept_by_device = declares_controls + && p.workarounds().contains( + &crate::device::profile::DriverWorkaround::FloatDenormalsKeptUnderHalfControls, + ); + if !kept_by_device { + requirements + .properties + .insert(crate::device::requirements::VkProperty::MetalFloatDenormals); + } else if !precision.contains(&PrecisionNote::DeviceDenormals) { + precision.push(PrecisionNote::DeviceDenormals); + precision.sort_unstable(); + } } // The negations, absolute values, floors and ceilings whose `float` denormals the device // reads as Metal does. diff --git a/crates/metal2vulkan/src/tests/engine_metal_denormals.rs b/crates/metal2vulkan/src/tests/engine_metal_denormals.rs index 80086784a..6e4a730f0 100644 --- a/crates/metal2vulkan/src/tests/engine_metal_denormals.rs +++ b/crates/metal2vulkan/src/tests/engine_metal_denormals.rs @@ -353,3 +353,100 @@ fn the_default_forms_note_what_they_leave_to_the_device() { &wrong[..wrong.len().min(40)] ); } + +/// In an entry that declares the 16-bit float controls (`narrow::controlled_modes`: the owned +/// fixtures `fmx_fma_precise` and `fmx_fma_fast`, which compute `half` in 16 bits beside `float` +/// `fma`, `a * b + c` and `a * w`), the `float` denormals of a device whose driver keeps them +/// there (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`: NVIDIA's on the RTX 5080 Laptop +/// GPU and on the RTX 3090) are the device's own: the module records `DeviceDenormals` and +/// requires no `VkProperty::MetalFloatDenormals`. In an entry that declares no float-controls +/// mode (`nan_denormals`, the deviation's probe) the same profiles require the deviation's +/// property, and so does RADV on the Radeon 780M, which flushes them under the controls too. +/// Another NVIDIA GPU (the desktop RTX 5080) carries neither the deviation nor the workaround. +/// `llvm-as` assembles the fixtures; without it the test skips visibly. +#[test] +fn float_denormals_under_the_half_controls_are_the_devices_where_its_driver_keeps_them() { + use crate::device::driver_facts::{NVIDIA_RTX_3090, NVIDIA_RTX_5080_LAPTOP}; + use crate::device::profile::DriverWorkaround; + use crate::device::requirements::{FloatControl, FloatWidth, VkProperty}; + use crate::device::{Device, DeviceDescription}; + use crate::pipeline::testing::engine_dir; + let mut fixtures = Vec::new(); + for entry in ["fmx_fma_precise", "fmx_fma_fast", "nan_denormals"] { + match llvm_assemble(&engine_dir().join(format!("{entry}.ll"))) { + Ok(bitcode) => fixtures.push((entry, bitcode)), + Err(AssembleError::Missing(tool)) => { + eprintln!("SKIP float_denormals_under_the_half_controls: {tool}"); + return; + } + Err(AssembleError::Rejected(e)) => panic!("llvm-as rejected {entry}: {e}"), + } + } + let description = |stem: &str| { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../metal2vulkan-validation/profiles") + .join(format!("{stem}.json")); + let text = std::fs::read_to_string(&path).expect("profile"); + DeviceDescription::from_khronos_json(&text, None).expect("description") + }; + let laptop = description("nvidia-rtx5080-laptop"); + assert_eq!(laptop.device_id, NVIDIA_RTX_5080_LAPTOP); + let mut rtx_3090 = laptop.clone(); + rtx_3090.device_id = NVIDIA_RTX_3090; + let mut desktop = laptop.clone(); + desktop.device_id = 0x2c02; + // (name, description, whether its driver keeps the denormals under the controls, whether it + // takes the deviation). + let devices = [ + ("RTX 5080 Laptop GPU", laptop, true, true), + ("RTX 3090", rtx_3090, true, true), + ("RTX 5080 desktop", desktop, false, false), + ("Radeon 780M", description("amd-780m-radv"), false, true), + ]; + let mut kept_noted = 0; + for (name, desc, keeps, deviates) in devices { + let p = Device::new(desc, DevicePolicy::default()) + .expect("device") + .profile() + .clone(); + assert_eq!( + p.workarounds() + .contains(&DriverWorkaround::FloatDenormalsKeptUnderHalfControls), + keeps, + "{name}" + ); + assert_eq!( + p.takes_driver_deviation(DriverDeviation::MetalFloatDenormals), + deviates, + "{name}" + ); + for (entry, bitcode) in &fixtures { + let what = format!("{entry} on {name}"); + let a = compile(bitcode, entry, &p).unwrap_or_else(|e| panic!("{what}: {e:?}")); + link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{what}: L3: {e}")); + let properties = &a.requirements.properties; + let controls = properties.contains(&VkProperty::FloatControl( + FloatControl::DenormPreserve, + FloatWidth::F16, + )); + let requires = properties.contains(&VkProperty::MetalFloatDenormals); + let noted = a.precision.contains(&PrecisionNote::DeviceDenormals); + eprintln!( + "{what}: 16-bit controls {controls}, requires MetalFloatDenormals {requires}, \ + DeviceDenormals {noted}" + ); + assert_eq!( + controls, + *entry != "nan_denormals", + "{what}: the 16-bit controls" + ); + let kept = keeps && controls; + assert_eq!(requires, deviates && !kept, "{what}: MetalFloatDenormals"); + if kept { + assert!(noted, "{what}: DeviceDenormals"); + kept_noted += 1; + } + } + } + assert_eq!(kept_noted, 4, "the two fixtures on the two GPUs measured"); +} diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 5a53a9e6e..ead1d3cac 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -13747,6 +13747,7 @@ variant metal2vulkan::device::DriverWorkaround::CompositeInsertsWrittenInPlace C variant metal2vulkan::device::DriverWorkaround::DiscardDropsSimdgroupStores DiscardDropsSimdgroupStores variant metal2vulkan::device::DriverWorkaround::DontInlineKeepsCalls DontInlineKeepsCalls variant metal2vulkan::device::DriverWorkaround::EarlyTestsDropMultisampledImageWrites EarlyTestsDropMultisampledImageWrites +variant metal2vulkan::device::DriverWorkaround::FloatDenormalsKeptUnderHalfControls FloatDenormalsKeptUnderHalfControls variant metal2vulkan::device::DriverWorkaround::FunctionsInlinedAtEveryCall FunctionsInlinedAtEveryCall variant metal2vulkan::device::DriverWorkaround::HalfConversionFusedWithArithmetic HalfConversionFusedWithArithmetic variant metal2vulkan::device::DriverWorkaround::HalfConversionPairFolded HalfConversionPairFolded diff --git a/docs/API.md b/docs/API.md index 4703d7010..0b207b650 100644 --- a/docs/API.md +++ b/docs/API.md @@ -4249,7 +4249,11 @@ bound. measured to read them as Metal does: `+ - *`, the comparisons, `fma`, `dot`, `sqrt`, `rsqrt`, `trunc`, `rint` and `ldexp` on `float` (by the device's products) but on a device that takes the measured - `DriverDeviation::MetalFloatDenormals`; `fneg`, `fabs`, `floor` and `ceil` on `float` but on a + `DriverDeviation::MetalFloatDenormals`, and those on such a device too in an entry that declares + the 16-bit float controls where its driver is measured to keep them there + (`DriverWorkaround::FloatDenormalsKeptUnderHalfControls`: NVIDIA's on the RTX 3090 and the RTX + 5080 Laptop GPU, whose module then requires no `VkProperty::MetalFloatDenormals`); `fneg`, + `fabs`, `floor` and `ceil` on `float` but on a device that takes the measured `DriverDeviation::MetalUnaryDenormals` (MoltenVK on the Apple M3 Max's macOS 27 identity, whose module then requires `VkProperty::MetalUnaryDenormals`); and on every device the divisions and the remainder, the device's maths functions (`fast::fmod`'s fused diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 8b13acbf2..12c720317 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -70,7 +70,7 @@ known. The rest of the differences are docs/API.md section 9's precision notes. | `air.pack` and `air.unpack` of a normalized format are the device's conversion: `GLSL.std.450`'s `Pack`/`Unpack` `Unorm4x8`, `Snorm4x8`, `Unorm2x16` and `Snorm2x16`, and for `rgb10a2` and `rgb565` their definition by the device's `NClamp`, multiply, `RoundEven` (a pack) and multiply by the reciprocal (an unpack); `unorm4x8_srgb`'s colour is the MSL transfer functions by the device's `Pow` around the 8-bit instruction, as is an 8-bit sRGB storage texture's write through its UNORM alias. A `float` one of the four named formats on MoltenVK reaches Metal's own; on MoltenVK on the Apple M3 Max's macOS 27 identity (`DriverDeviation::MetalPackConversions`) so do `rgb565` in `float` and, in `half`, `unorm4x8`, `snorm4x8`, `rgb10a2` and `rgb565` both ways and `unorm2x16` and `snorm2x16` packed, measured to answer Metal's at every code and every `half` (`runtime_lane::pack_conversions`). The same conversions serve an implicit imageblock access of a normalized attachment (a `half` load rounding the unpacked `float` once more), the interlocked storage form's 8-bit sRGB attachment and sRGB word, and a snapshot's normalized codes (`c` times the `f32` nearest `1/D`). So does every conversion of an extended-range, padded 10-bit or bfloat texel, which no Vulkan format holds (`BGR10_XR`, `BGRA10_XR`, `RGB10A8_2P_XR10`, `R10Unorm_X6`, `RG10Unorm_X12`, their sRGB twins, `RGB10A2Unorm_sRGB`, the bfloat formats; every device): a read `float(c - 384)` (a padded code's `float(c)`) times the `f32` nearest `1/510` (`1/1023`), a write `RoundEven` of the clamped value's `f32` product plus 384, an sRGB twin's colour the MSL transfer by the device's `Pow` on the magnitude, a `half` result the device's narrowing, a bfloat NaN as it is. | The device's conversion is the contract. | A packed code within one of Metal's, apart only where `clamp(c)·scale` lies within its rounding of a half-integer (`Round`'s tie is the device's); an unpacked value within 2.5 ulp of the correctly rounded quotient; a decoded sRGB colour within half a twelve-bit step (`1/8190`) of Metal's table step `k/4095`, beyond its `Pow`'s bound; a NaN channel's code the device's (Metal's: 0). An extended-range or padded value within one `f32` ulp of Metal's quotient, a code within one of Metal's exact product, an sRGB twin's decoded value within half a `2^-13` step of Metal's table (`R10Unorm_X6_sRGB`'s and `RGB10A2Unorm_sRGB`'s colour 1.64 steps) and its code within one (theirs within two below `0.0031308`, where Metal rounds the value to `2^-13` before its table), a `half` result within one `half` ulp (Metal rounds ties away from zero, a bfloat sample's `half` toward zero), a bfloat NaN the device's (Metal's: `0x7fc00000`, a write `0x7fc0`). `DevicePackConversion` (`device-pack-conversion`) | | A tensor's 8-bit float element a `matmul2d` product reads (`MTLTensorDataTypeFloat8E4M3`, `Float8E5M2`) widens through the device's own 8-bit float on a device with `shaderFloat8` (`SPV_EXT_float8`'s `Float8E4M3EXT` or `Float8E5M2EXT`, OCP's encodings), as an `f8e4m3fn` or `f8e5m2` `air.convert` and simdgroup-matrix cell do there: an e4m3 `0x7F` or `0xFF` reads as a NaN, where Metal reads 480 and -480 (measured on the M3 Max), and an e5m2 NaN's bits are the device's, where Metal reads `0x7FC00000`. A device without `shaderFloat8` decodes the fields with integers, as Metal reads them. | The device's own 8-bit float is its native form (owner rule 1), the only form where it exists. | `NativeFloat8` (`native-float8`) | | Off MoltenVK a `float` multiply, a fused multiply-add by `OpFmaKHR`, and a `float` `ldexp` (whose result below `2^-126` is that multiply's product) round before Metal's flush: Metal flushes a result whose exact value lies below `2^-126` before rounding it, so a result whose exact value lies within half a unit below `2^-126` answers `2^-126` of its sign where Metal answers the zero of its sign. | Every device measured off MoltenVK (RADV, ANV, NVIDIA, lavapipe) rounds first; its multiply is the contract. | Announced once per device as it is made: `float-tininess-after-rounding` | -| `f32` denormals in a `float` operation on a device not measured to read them as Metal does (`DriverDeviation::MetalFloatDenormals`: lavapipe, and every unmeasured device), in `floor`, `ceil`, `fneg` and `fabs` on a device not measured to read them as Metal's do (`DriverDeviation::MetalUnaryDenormals`: every device but MoltenVK on the Apple M3 Max's macOS 27 identity), and on every device in a nearest pixel sample's texel `floor(x)` (which Metal's sampler reads from a subnormal `x` itself, a negative one as texel -1, where MoltenVK's `floor` reads it as the zero of its sign: recorded only where the `s` or `t` mode is repeat, clamp to zero or clamp to the border, which read texel -1 apart from texel 0, through a constexpr sampler of such a mode, through a runtime slot under a pipeline made for a nearest pixel state and announced where the bound state's mode is such, through an argument buffer as `argument-pixel-sampler-scaling`), in the maths functions (`sign` and `fract` among them, and the zero and sign tests of `pow`, `powr` and `atan2`) and the device's order of the rows above, and in a conversion to or from `double`. Metal reads them as zero and flushes a denormal result. | The device's reading is the contract: Vulkan lets each device keep or flush them, and Metal runs no `double`, so its reading of a conversion is not measured. | `DeviceDenormals` (`device-denormals`) | +| `f32` denormals in a `float` operation on a device not measured to read them as Metal does (`DriverDeviation::MetalFloatDenormals`: lavapipe, and every unmeasured device; and, in an entry that declares the 16-bit float controls, a device whose driver is measured to keep them there, `DriverWorkaround::FloatDenormalsKeptUnderHalfControls`: NVIDIA's on the RTX 3090 and the RTX 5080 Laptop GPU), in `floor`, `ceil`, `fneg` and `fabs` on a device not measured to read them as Metal's do (`DriverDeviation::MetalUnaryDenormals`: every device but MoltenVK on the Apple M3 Max's macOS 27 identity), and on every device in a nearest pixel sample's texel `floor(x)` (which Metal's sampler reads from a subnormal `x` itself, a negative one as texel -1, where MoltenVK's `floor` reads it as the zero of its sign: recorded only where the `s` or `t` mode is repeat, clamp to zero or clamp to the border, which read texel -1 apart from texel 0, through a constexpr sampler of such a mode, through a runtime slot under a pipeline made for a nearest pixel state and announced where the bound state's mode is such, through an argument buffer as `argument-pixel-sampler-scaling`), in the maths functions (`sign` and `fract` among them, and the zero and sign tests of `pow`, `powr` and `atan2`) and the device's order of the rows above, and in a conversion to or from `double`. Metal reads them as zero and flushes a denormal result. | The device's reading is the contract: Vulkan lets each device keep or flush them, and Metal runs no `double`, so its reading of a conversion is not measured. | `DeviceDenormals` (`device-denormals`) | | Samples, gathers, depth comparisons and level queries are the device's own sampler's on every device whose sampler is not Metal's (every device but MoltenVK): its filter, footprint, level, anisotropy, sRGB decode and comparison, its level clamp (the descriptor's as given, which Metal holds on a `1/64` grid) and its reading of a subnormal coordinate (Metal's sampler reads a subnormal normalized coordinate as the zero of its sign before addressing, measured on an Apple M3 Max on `R32Float`, and addresses a negative subnormal pixel coordinate as texel -1, as an Apple M3 Max with macOS 27.0.1 answers the conformance case `pixel-samplers-subnormal-border`). A sample's or gather's residency on a device with `shaderResourceResidency` is the device's `OpImageSparse*` word and non-resident value (`SparseResidency`); Metal's footprint rule answers it only where the device has no residency and the slot's tile map does. A two-plane YCbCr texture's linear sample is the device's filter of its texels (the device's own Y′CbCr conversion's where it converts the format, next row; Metal's reads exactly elsewhere); where the device's memory is the host's, a sample that reads the planes in place is Metal's filter exactly, and a gather Metal's taps. An 8-bit sRGB format the device cannot sample (`R8_SRGB` on lavapipe, `R8G8_SRGB` on lavapipe, ANV and NVIDIA) is held as its UNORM twin, the device lacking its decode: a read, a sample, a fetch, a gather and a bicubic tap decode what the twin's view reads (one texel's codes, or the device's filter of them) by the MSL transfer through the device's `Pow`. | The device's sampler is the contract. | Linear filter within one code (1/255) on the measured devices, sRGB within 1.83, a two-plane YCbCr texture's within 1.183; levels, anisotropy, cubes, 3D, gathers and comparisons unmeasured. A twin's texel: the transfer within the device's `Pow` (MoltenVK's within 1.83e-7 through a storage alias, `runtime_lane::srgb_storage`) where Metal's table rounds it to a twelve-bit step (up to 1/8190 apart); a filtered sample decodes the filtered codes, up to 0.286 off for a blend of codes 0 and 255 (`SrgbTexelDecode`). `DeviceSampling` (`device-sampling`) | | A pixel-coordinate sampler's linear sample, gather, comparison and level query is the device's normalized sampler's at the coordinate divided by the texture's level-0 size in the shader, on every device, MoltenVK included, where no other form takes it: through a runtime sampler slot or an argument buffer's sampler, whose state the function does not know, and through a constexpr one of an array, a 3D texture or a texture whose residency the function observes. The device reads its texels and weights from that coordinate in its own arithmetic, where Metal's sampler reads the coordinate as given on its own sub-texel grid. A constexpr pixel sampler's sample or gather of a 1D or 2D texture that is no array hands the coordinate itself, moved by its offset, to the device's unnormalized sampler at level 0 (a nearest sample with an offset its texel's centre `floor(x) + 0.5 + k`), which reads it as Metal's does on MoltenVK; a nearest sample elsewhere hands the device's normalized sampler its texel's centre `floor(x) + 0.5` over the size, where the device reads exactly Metal's texel `floor(x)` (a runtime slot's behind its `NearestPixelSampler` specialization, an argument buffer's by its state's word); a constexpr sampler that filters nearest and clamps to the edge fetches its texel (`sampler.pixel_fetch`). | The device's sampler is the contract. Vulkan's unnormalized samplers take no offset operand, comparison, array, 3D image, mip level or anisotropy, and a runtime or argument buffer's state is not known when the function is made, so the shader scales the coordinate there; no path reproduces Metal's sub-texel grid. | The device answers as its normalized sampler at a coordinate within one `f32` ulp of the quotient on MoltenVK (10 of 800 cells of each mirror-clamp linear configuration move by it) and at the quotient itself on lavapipe (`runtime_lane::pixel_samplers`: 800 coordinates at and around each texel edge of a 5x3 texture, every filter and address mode); on MoltenVK a linear sample through a runtime or argument buffer's sampler, or of an array, reads another weight step than Metal's at under 1% of the coordinates at and around each texel edge of textures 258, 21 and 37 texels wide, where `x - 0.5` lies at a half step of Metal's `1/256` grid (`runtime_lane::pixel_edges`); a linear downscale by 8 of textures 1000 to 1920 texels wide within 1/255 of Metal's on MoltenVK and 2/255 on lavapipe (`runtime_lane::pixel_downscales`); other devices unmeasured. Every nearest access of those edge probes reads Metal's texel on MoltenVK and on lavapipe, and every constexpr sample and gather of a 1D or 2D texture answers Metal's on MoltenVK (`runtime_lane::pixel_edges`); elsewhere a constexpr linear sample is the device's own filter in unnormalized coordinates (`device-sampling`), which on lavapipe differs from its normalized filter in the last bits of 79 to 107 of 800 cells of clamp to zero and to the border (`runtime_lane::pixel_samplers`). `PixelSamplerScaling` (`pixel-sampler-scaling`), `ArgumentPixelSamplerScaling` (`argument-pixel-sampler-scaling`) | | A two-plane YCbCr texture's texels are the device's own sampler Y′CbCr conversion of its planes where the device converts their format (`ycbcr_device.rs`): unpacked 8- or 10-bit codes of a 4:2:0, 4:2:2 or 4:4:4 layout over plane 0 that form one image (no chroma codes 0, no rows past the buffer; a 4:4:4 layout read as codes aside, whose identity conversion converts nothing), on a device that enables `samplerYcbcrConversion` (a 4:4:4 layout also needs Vulkan 1.3 or `VK_EXT_ycbcr_2plane_444_formats`), reports the multi-planar format's conversion and, for a 4:2:0 or 4:2:2 layout, Metal's reconstruction (midpoint chroma through a linear chroma filter), and has no defect in the format; the planes are copied into an image of it and sampled once per texel into the texture's image (`bind::ycbcr_sample_module`), through the refresh's indirect arguments. The conversion takes the surface's matrix and range (the identity model for the code-reading formats), and Metal's reconstruction of a subsampled layout; a device's cosited or nearest reconstruction would move what the codes mean to Metal (cosited, up to 1340/2048 of random planes on lavapipe), a capability it lacks, so such a layout converts through Metal's arithmetic. A render pass's load stays Metal's arithmetic, which our store inverts, so a loaded texel no draw changes keeps its codes, as `MTLLoadActionLoad` keeps the attachment's contents. lavapipe (Mesa 26.0.8) converts the 8-bit 4:4:4 layout (its 4:2:0 and 4:2:2 ones are cosited only, and it reads 10-bit codes as 16-bit words, `RuntimeDriverDefect::TenBitCodesReadAsWords`); MoltenVK 1.4.2 converts none (its 4:2:0 and 4:2:2 reconstruction is misplaced, `RuntimeDriverDefect::TwoPlaneChromaMisplaced`, and it reports no 4:4:4 two-plane format). RADV (Mesa 26.2.4) reads 10-bit codes as 16-bit words too (measured on a linear image of the 4:2:0 format on the Radeon 780M), so its 10-bit layouts convert through Metal's arithmetic; so do NVIDIA's (615.71.09, RTX 5080 Laptop GPU), whose conversion reads a 10-bit code's six unused low bits as its fraction, and which converts the 8-bit 4:2:0, 4:2:2 and 4:4:4 layouts. Elsewhere, and for packed codes, chroma codes 0 and plane 1, Metal's fixed-point conversion stays as capability (`WorkCounts::ycbcr_device_conversions`). | The device's conversion is the contract: Vulkan states the reconstruction's weights and the model's matrices, the precision of each step at least the format's, and the clamp of the result into `[0, 1]` the implementation's. | Each channel within 0.04/2048 of Vulkan's conversion past the `half` its image stores on lavapipe's 8-bit 4:4:4 layout, and within 0.034/2048 on NVIDIA's 8-bit layouts; of Metal's read within 6/2048 where the result lies in `[0, 1]` on lavapipe, 12.4/2048 on NVIDIA's 4:2:0 (19.1 through an `_sRGB` twin's curve; Metal rounds each reconstructed chroma to a code), and unclamped outside it; a code-reading format's codes the nearest to Vulkan's reconstruction, NVIDIA's Metal's (random planes, every texel, `tests/ycbcr_device.rs`). `YCbCrFilterNotExact` (`ycbcr-filter-not-exact`) |