diff --git a/crates/metal2vulkan-validation/src/runtime_lane/half_storage_writes.rs b/crates/metal2vulkan-validation/src/runtime_lane/half_storage_writes.rs index 8bc49d337..5f8268775 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/half_storage_writes.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/half_storage_writes.rs @@ -450,3 +450,73 @@ fn kernel_writes_store_metals_small_floats() { "kernel small-float writes not Metal's: {differ:#?}" ); } + +/// Every NaN class of a `float` that a kernel writes to an `RG11B10Float` storage texture, through +/// every form: each of the 8,192 patterns of a NaN's top 13 payload bits, its 10 low bits clear and +/// set (pattern 0 with them set alone, which is no infinity), of both signs, every channel of a +/// texel the same NaN. Metal stores each field as its canonical NaN (`0x7e0`, `0x3f0`), whatever +/// the NaN's sign and payload (`DriverDeviation::MetalSmallFloatNans`'s measurement on the M3 +/// Max). On a device that takes that deviation every field is Metal's and nothing announces +/// `device-nan-encoding`: the deviation's gate on each driver it lists. Elsewhere the fields +/// stored as another code are counted and printed, and where any is, the case announces +/// `device-nan-encoding`. +#[test] +fn every_nan_a_kernel_writes_to_a_small_float_texture_is_metal_s_where_measured() { + let mut values = Vec::new(); + for sign in [0, 0x8000_0000u32] { + for top in 0..1u32 << 13 { + for low in [0, 0x3ff] { + if top << 10 | low != 0 { + values.push(f32::from_bits(sign | 0x7f80_0000 | top << 10 | low)); + } + } + } + } + let mut failed = Vec::new(); + for form in Form::ALL { + on_device(|device, messages| { + let name = device.name().to_owned(); + let label = format!("RG11B10Float by {}", form.kernel()); + messages.take_notices(); + let Some(stored) = written(device, form, PixelFormat::RG11B10Float, 4, &values) else { + return; + }; + let noted = messages.take_notices().iter().any(|m| { + m.degradation + .as_ref() + .is_some_and(|n| n.degradation == Degradation::DeviceNanEncoding) + }); + let measured = device + .compile_device() + .profile() + .takes_driver_deviation(DriverDeviation::MetalSmallFloatNans); + // Each code stored in place of the field's canonical NaN, and how many times. + let mut other = BTreeMap::new(); + for (i, x) in values.iter().enumerate() { + assert!(x.is_nan()); + let texel = u32::from_le_bytes(stored[i * 4..i * 4 + 4].try_into().unwrap()); + for (offset, mantissa) in SMALL_FLOAT_FIELDS { + let got = (texel >> offset) & ((1 << (mantissa + 5)) - 1); + if got != (0x1f << mantissa | 1 << (mantissa - 1)) { + *other.entry((offset, got)).or_insert(0usize) += 1; + } + } + } + let differ: usize = other.values().sum(); + let fields = values.len() * SMALL_FLOAT_FIELDS.len(); + eprintln!( + "every small-float NaN {label} on {name}: {differ} of {fields} NaN fields not \ + Metal's {other:x?}; MetalSmallFloatNans {measured}, device-nan-encoding {noted}" + ); + if (measured && (differ > 0 || noted)) || (differ > 0 && !noted) { + failed.push(format!( + "{label}: {differ} of {fields}, measured {measured}, announced {noted}" + )); + } + }); + } + assert!( + failed.is_empty(), + "small-float NaN writes against MetalSmallFloatNans: {failed:#?}" + ); +} diff --git a/crates/metal2vulkan-validation/src/runtime_lane/half_write_nans.rs b/crates/metal2vulkan-validation/src/runtime_lane/half_write_nans.rs index 3438b50ce..1cc4b1ede 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/half_write_nans.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/half_write_nans.rs @@ -24,8 +24,11 @@ //! Metal's on a device with `VkProperty::MetalSmallFloatNans`; elsewhere the NaN is the device's, //! and a pipeline whose stores differ announces `device-nan-encoding`. On a device with those //! properties a write into such a texture, through a texture slot or an argument buffer whose -//! command makes no texture of another family resident (`SpecKey::HeapNanFamilies`), announces -//! neither `device-nan-encoding` nor `native-nan-encoding`. +//! command makes no texture of another family resident (`SpecKey::HeapNanFamilies`), announces no +//! `device-nan-encoding`, nor `native-nan-encoding` where the device's widening is measured +//! (`VkProperty::MetalNanMoves`). Where it is not (lavapipe), the kernel's making announces +//! `native-nan-encoding` for its widening of the half, whatever texture it writes: a packed small +//! float store of Metal's hides that NaN's bits, a 32-bit float one does not. use super::degrade_notices::{counts, on_device_with}; use super::{at_plan, buffer_with, layer_reported_nothing, read_buffer, WAIT}; @@ -250,22 +253,28 @@ fn nan_word(f: &Format, w: u32) -> bool { } } -/// The words texel by texel that `state` (of `access`) stored into a `Private` texture of `f`. +/// The words texel by texel that `state` (of `access`) stored into a `Private` texture of `f`, one +/// texel a thread for each of `texels`: at most 64 of them, or a whole number of threadgroups of 64. fn stored( device: &Device, (state, access): (&ComputePipelineState, &str), f: &Format, + texels: &[[u16; 4]], ) -> Vec> { - let mut d = TextureDescriptor::texture_2d(f.format, N, 1, false); + let n = texels.len() as u64; + let group = n.min(64); + assert_eq!(n % group, 0, "{n} texels in threadgroups of {group}"); + let mut d = TextureDescriptor::texture_2d(f.format, n, 1, false); d.usage = TextureUsage::SHADER_READ | TextureUsage::SHADER_WRITE; d.resource_options = ResourceOptions::private(); let texture = device.new_texture(&d).unwrap(); - let row = N * f.bytes; + let row = n * f.bytes; let fill = buffer_with(device, &vec![FILL; row as usize]).unwrap(); let out = buffer_with(device, &vec![0; row as usize]).unwrap(); - let bits: Vec = (0..V.len()) - .flat_map(texel) - .flat_map(u16::to_le_bytes) + let bits: Vec = texels + .iter() + .flatten() + .flat_map(|h| h.to_le_bytes()) .collect(); let input = buffer_with(device, &bits).unwrap(); let queue = device.new_command_queue().unwrap(); @@ -276,7 +285,7 @@ fn stored( 0, row, row, - Size::new(N, 1, 1), + Size::new(n, 1, 1), &texture, 0, 0, @@ -298,7 +307,7 @@ fn stored( encoder.set_texture(Some(&texture), 0).unwrap(); } encoder - .dispatch_threads(Size::new(N, 1, 1), Size::new(N, 1, 1)) + .dispatch_threads(Size::new(n, 1, 1), Size::new(group, 1, 1)) .unwrap(); encoder.end_encoding().unwrap(); let mut blit = command_buffer.blit_command_encoder().unwrap(); @@ -307,7 +316,7 @@ fn stored( 0, 0, Origin::new(0, 0, 0), - Size::new(N, 1, 1), + Size::new(n, 1, 1), &out, 0, row, @@ -364,7 +373,8 @@ fn half_writes_store_their_nans_as_metal_or_announce() { for f in &FORMATS { { let state = kernel(device, access); - let texels = stored(device, (&state, access), f); + let lanes: Vec<[u16; 4]> = (0..N as usize).map(texel).collect(); + let texels = stored(device, (&state, access), f, &lanes); let announced = counts(&messages.take_notices()); let properties = &device.compile_device().profile().enabled().properties; let keeps = properties.contains(&VkProperty::MetalHalfTexelWrites); @@ -421,9 +431,12 @@ fn half_writes_store_their_nans_as_metal_or_announce() { f.name, differ.len() ); + // The widening's note, the making's whatever texture the kernel writes, + // stands where the device's widening is not measured to be Metal's. + let moves = properties.contains(&VkProperty::MetalNanMoves); if measured { assert!( - differ.is_empty() && !noted && !widened_own, + differ.is_empty() && !noted && !(widened_own && moves), "{access} {}: {} lanes differ from Metal's, device-nan-encoding \ {noted}, native-nan-encoding {widened_own}", f.name, @@ -454,3 +467,66 @@ fn half_writes_store_their_nans_as_metal_or_announce() { ); eprintln!("half write NaNs: {report:#?}"); } + +/// Every `half` NaN (each payload of both signs: 2,046) that a kernel writes, in all four lanes of +/// a texel, to an `RG11B10Float` storage texture through `write`, `read_write` and `held`. Metal +/// stores each field as its canonical NaN (`0x7e0`, `0x3f0`: [`METAL_RG11B10`]'s first texel, whose +/// lanes are all NaNs); on a device that takes `DriverDeviation::MetalSmallFloatNans` every texel +/// is so and no write announces `device-nan-encoding`, the deviation's gate on each driver it +/// lists. Elsewhere the texels stored otherwise are printed, and a write that stores one announces +/// `device-nan-encoding`. +#[test] +fn every_half_nan_written_to_a_small_float_texture_is_metal_s_where_measured() { + let mut nans: Vec = (0..=u16::MAX) + .filter(|h| h & 0x7c00 == 0x7c00 && h & 0x3ff != 0) + .collect(); + assert_eq!(nans.len(), 2046); + // Whole threadgroups of 64, the first NaN again past the last. + nans.resize(nans.len().div_ceil(64) * 64, nans[0]); + let lanes: Vec<[u16; 4]> = nans.iter().map(|&h| [h; 4]).collect(); + let rg11b10f = FORMATS + .iter() + .find(|f| f.format == PixelFormat::RG11B10Float) + .unwrap(); + let mut failed = Vec::new(); + on_device_with( + |options| { + options.announce_each.insert(Degradation::DeviceNanEncoding); + at_plan(options) + }, + |device, messages| { + let properties = &device.compile_device().profile().enabled().properties; + let measured = properties.contains(&VkProperty::MetalSmallFloatNans); + for access in ["write", "read_write", "held"] { + let state = kernel(device, access); + let texels = stored(device, (&state, access), rg11b10f, &lanes); + let noted = + counts(&messages.take_notices()).contains_key(&Degradation::DeviceNanEncoding); + let mut other: Vec = texels + .iter() + .zip(&nans) + .filter(|(words, _)| words[0] != METAL_RG11B10[0]) + .map(|(words, h)| format!("{h:04x}->{:x}", words[0])) + .collect(); + let differ = other.len(); + other.truncate(8); + eprintln!( + "every half NaN {access} rg11b10f on {}: {differ} of {} texels not Metal's \ + {other:?}; MetalSmallFloatNans {measured}, device-nan-encoding {noted}", + device.name(), + nans.len() + ); + if (measured && (differ > 0 || noted)) || (differ > 0 && !noted) { + failed.push(format!( + "{access}: {differ} texels, measured {measured}, announced {noted}" + )); + } + layer_reported_nothing(messages, &format!("every half NaN {access} rg11b10f")); + } + }, + ); + assert!( + failed.is_empty(), + "half NaN writes to RG11B10Float against MetalSmallFloatNans: {failed:#?}" + ); +} diff --git a/crates/metal2vulkan/src/device/driver_facts.rs b/crates/metal2vulkan/src/device/driver_facts.rs index 8d6c42df8..03e503260 100644 --- a/crates/metal2vulkan/src/device/driver_facts.rs +++ b/crates/metal2vulkan/src/device/driver_facts.rs @@ -489,7 +489,18 @@ pub(crate) fn among( /// Metal's bytes in every texel, as the same writes do into `RGBA32Float`, `RG32Float` and /// `R32Float` (a `half` widened by Metal's own `float(half)`, `MetalNanMoves`). Into `R16Float` /// and `RGBA16Float` a `half` NaN's payload differs (the device stores `0x7e00`), which -/// `MetalHalfTexelWrites` would state, so outside it. One row per GPU, as measured. +/// `MetalHalfTexelWrites` would state, so outside it. One row per GPU, as measured. lavapipe +/// (`MESA_LLVMPIPE`) packs a small float in its shader code, which writes every NaN as the +/// field's quiet NaN with its sign dropped, Metal's: Mesa 26.2.4 on LLVM 23.1.1, x86_64 (an +/// Ultra 9 275HX), through a kernel's `write`, `read_write`, texture buffer and argument buffer +/// forms, stored Metal's field for each of 32,766 `float` NaNs (every pattern of the top 13 +/// payload bits, its 10 low bits clear and set, both signs) and each of the 2,046 `half` NaNs +/// (the gate lanes +/// `runtime_lane::half_storage_writes::every_nan_a_kernel_writes_to_a_small_float_texture_is_metal_s_where_measured` +/// and +/// `runtime_lane::half_write_nans::every_half_nan_written_to_a_small_float_texture_is_metal_s_where_measured`). +/// One row, of the driver on every CPU. Measured to keep a NaN's payload bits, so not listed: RADV +/// on the Radeon 780M and NVIDIA's 615.71.09 on the RTX 5080 Laptop GPU. /// /// `MetalUnaryDenormals`: MoltenVK 1.4.2's `OpFNegate`, `FAbs`, `Floor` and `Ceil` on 32-bit /// floats, which SPIRV-Cross writes as Metal's `-x`, `fabs`, `floor` and `ceil`. On the M3 Max @@ -705,6 +716,12 @@ pub(crate) const DEVIATIONS: &[Measured] = &[ device: Some(APPLE9), ..moltenvk_on_apple(DriverDeviation::MetalSmallFloatNans) }, + Measured { + fact: DriverDeviation::MetalSmallFloatNans, + driver: DriverId::MESA_LLVMPIPE, + vendor: None, + device: None, + }, Measured { device: Some(APPLE9), ..moltenvk_on_apple(DriverDeviation::MetalUnaryDenormals) diff --git a/crates/metal2vulkan/src/device/tests/driver_facts.rs b/crates/metal2vulkan/src/device/tests/driver_facts.rs index 013f5cd9d..54b0ac478 100644 --- a/crates/metal2vulkan/src/device/tests/driver_facts.rs +++ b/crates/metal2vulkan/src/device/tests/driver_facts.rs @@ -183,7 +183,8 @@ fn taken(p: &crate::device::DeviceProfile) -> Vec /// sampler, compute cube route and the rest of the seventeen), with the graphics cube route and the /// per-sample depth of macOS 27's Apple9 identity, that identity's normalized /// storage writes, full quads, normalized vertex fetch, alpha to coverage, moved NaNs, pack conversions, half conversions, small-float NaN stores and unary denormals, and every Apple GPU's 16-bit unorm depth stores, interpolation -/// and first-vertex indices; lavapipe's `float` color NaNs and half-float storage writes; RADV's +/// and first-vertex indices; lavapipe's `float` color NaNs, half-float storage writes and +/// small-float NaN stores; RADV's /// half-float storage writes on every GPU and its others on the Radeon 780M, ANV's on the Iris Xe /// of RPL-P and the UHD 770 of RPL-S, and NVIDIA's on the RTX 3090 and the RTX 5080 Laptop GPU, as /// measured there; the 8-bit linear filter's bound where measured. Every other committed profile @@ -277,7 +278,11 @@ fn the_profile_takes_the_deviations_its_driver_is_measured_with() { (true, _) => want_all, (false, "nvidia-rtx5080-laptop") => nvidia_measured.clone(), (false, "lavapipe" | "gate-lavapipe" | "gate-lavapipe-x86_64") => { - vec![D::MetalFloatColorNans, D::MetalHalfStorageWrites] + vec![ + D::MetalFloatColorNans, + D::MetalHalfStorageWrites, + D::MetalSmallFloatNans, + ] } // The per-sample `FragCoord.z` last, in `taken`'s order. The 780M under Phoenix's // `deviceID` and Hawk Point's alike. diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 355df25c8..f93a22212 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -964,10 +964,11 @@ pub enum PrecisionNote { /// plain color fill to a NaN of a half or small-float texture the device's clear writes: the /// device converts the NaN as it likes, where Metal's fill keeps a half NaN's sign and payload /// and makes a small float's all ones. Not recorded by a texture write of a constant texel none - /// of whose lanes is a NaN, nor where the write's stored NaN is measured to be Metal's (MoltenVK - /// on the Apple M3 Max's macOS 27 identity): a `half` texel's write to a 32-bit float format on - /// a profile that takes `DriverDeviation::MetalNanMoves`, and a `float` or `half` texel's write - /// to `RG11B10Float` on one that takes `DriverDeviation::MetalSmallFloatNans` (the module then + /// of whose lanes is a NaN, nor where the write's stored NaN is measured to be Metal's: a + /// `half` texel's write to a 32-bit float format on a profile that takes + /// `DriverDeviation::MetalNanMoves` (MoltenVK on the Apple M3 Max's macOS 27 identity), and a + /// `float` or `half` texel's write to `RG11B10Float` on one that takes + /// `DriverDeviation::MetalSmallFloatNans` (that identity and lavapipe) (the module then /// requires the matching `VkProperty`). Validation derives no tolerance and no explanation from /// it, as for `DeviceDenormals`. DeviceNanEncoding, diff --git a/docs/API.md b/docs/API.md index fb5b71b10..e34d1f072 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3706,9 +3706,10 @@ bound. `DriverDeviation::MetalNanMoves` (the image stores the widened `float`'s bits, Metal's `0x7fc00000`; the module requires `VkProperty::MetalNanMoves`), and a `float` or `half` texel's write to a packed small float one (`RG11B10Float`) on a profile that takes - `DriverDeviation::MetalSmallFloatNans`, MoltenVK on Apple's GPUs of the Apple M3 Max's macOS 27 + `DriverDeviation::MetalSmallFloatNans`: MoltenVK on Apple's GPUs of the Apple M3 Max's macOS 27 identity, measured byte for byte against plain Metal over every `half` and 33,792 `float` words, - 16,446 NaNs among them (the module requires `VkProperty::MetalSmallFloatNans`); and a `half` + 16,446 NaNs among them, and lavapipe, whose stores of 32,766 `float` NaNs and every `half` NaN + are Metal's canonical field NaN (the module requires `VkProperty::MetalSmallFloatNans`); and a `half` read, sample or gather of a texture slot bound to a 32-bit float or packed small float format (`RG11B10Float`, `RGB9E5Float`) on a profile that takes `DriverDeviation::MetalHalfConversion`, the same identity, whose `half` of such a texel is Metal's, its NaN `0x7e00` (the module