From 196bdf813568bbfd0fdfb47c6d383b7356d2fd30 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 2 Nov 2026 16:00:47 -0800 Subject: [PATCH] Fix forward the expectations the half-conversion and NaN-encoding carve-outs moved What: - tests::engine_extended_range::half_float_storage_slots_leave_their_writes_to_the_device: a formatless half-float storage slot's float write no longer records DeviceNanEncoding for the packed small float format on a profile that takes MetalSmallFloatNans (moltenvk-m3), and requires VkProperty::MetalSmallFloatNans there; the test and its doc now say so. - runtime_lane::native_maths::half_rounding_answers_within_its_recorded_bound: a module whose conversion is measured to be Metal's (VkProperty::MetalHalfConversion) announces nothing and must answer Metal's exactly, as under the float controls. On MoltenVK it does: 8192 of 8192 answers Metal's exactly. - runtime_lane::ycbcr_textures::a_four_four_four_texture_s_writes_store_metal_s_codes: the half write's YCbCrWrite variant bitcasts the widened half's lanes to words for the runtime's encode, which records NativeNanEncoding on every device (as before). The kernel's own stage no longer records it on MoltenVK, so the variant's dispatch announces it there; the lane expects it exactly where the write's stage does not record it (Kernels::half_write_nans). - runtime_lane::ycbcr_textures::srgb_twins_read_what_metal_reads: a half nearest sample of an sRGB twin through a runtime sampler slot is the device's rounding of the float it reads (MoltenVK: toward zero, 8,409 channels over the six surfaces and three matrices, each within one half ulp of Metal's ties-away step), which HalfTexelConversion announces at the bind. The lane licensed it by the stage's NativeHalfConversion, which the read no longer records; it now asserts the bound and that HalfTexelConversion was announced (assert_half_samples). - golden_hashes.tsv: the moltenvk-m3 profile's hash (the two new deviations). Verified on this M3 Max (MoltenVK 1.4.2, macOS 27.0.1): the core test passes; the three lanes pass. The diagnosis of the 4:4:4 note used a temporary dump of the variant's instructions (not committed): StorageImageClass::YCbCrWrite, R32G32B32A32_UINT, a Bitcast of each widened lane. (cherry picked from commit 0baa910d6acff2838ffb801f608ad29bffa0331c) --- .../src/runtime_lane/native_maths.rs | 17 ++-- .../src/runtime_lane/ycbcr_textures.rs | 80 ++++++++++++++++--- .../src/tests/engine_extended_range.rs | 15 +++- crates/metal2vulkan/tests/golden_hashes.tsv | 2 +- 4 files changed, 93 insertions(+), 21 deletions(-) diff --git a/crates/metal2vulkan-validation/src/runtime_lane/native_maths.rs b/crates/metal2vulkan-validation/src/runtime_lane/native_maths.rs index 5b991d0fc..afffe378f 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/native_maths.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/native_maths.rs @@ -583,7 +583,10 @@ fn half_operands() -> Vec<[f32; 2]> { /// operands rounded, or rounded itself), exactly. CI's lavapipe (Mesa 26.0.8 with LLVM 21.1.8) /// rounds here (8192 of 8192). A device whose float controls make its conversion Metal's (the /// module declares `RoundingModeRTE`, `DenormPreserve` and `SignedZeroInfNanPreserve` for 16 -/// bits: ANV and RADV) records neither and answers Metal's exactly. +/// bits: ANV and RADV) records neither and answers Metal's exactly, and so does one whose +/// conversion is measured to be Metal's (MoltenVK on the Apple M3 Max's macOS 27 identity, +/// `DriverDeviation::MetalHalfConversion`: the module requires its property), whose `half` sum +/// is its own 16-bit addition. #[test] fn half_rounding_answers_within_its_recorded_bound() { let bitcode = llvm_assemble_text(&half_kernel()).unwrap(); @@ -626,10 +629,14 @@ fn half_rounding_answers_within_its_recorded_bound() { .filter_map(|kind| kind.precision_note()) .filter(|note| kinds.contains(note)) .collect(); - // A rounding by the bare quantize is the device's: only the float controls answer - // Metal's. - let metal_s = controlled; - eprintln!("{name}: float controls {controlled}; announced {announced:?}"); + // A rounding by the bare quantize is the device's: only the float controls, or a + // conversion measured to be Metal's, answer Metal's. + let measured = required.contains(&VkProperty::MetalHalfConversion); + let metal_s = controlled || measured; + eprintln!( + "{name}: float controls {controlled}; measured conversion {measured}; announced \ + {announced:?}" + ); assert_eq!(announced.len(), usize::from(!metal_s), "{name}"); let operands = half_operands(); let bytes: Vec = operands diff --git a/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_textures.rs b/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_textures.rs index 123c2e38d..dd7dbb567 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_textures.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/ycbcr_textures.rs @@ -463,6 +463,10 @@ struct Kernels { /// Whether `read_half`'s stage rounds a `half` by the device's conversion /// (`PrecisionNote::NativeHalfConversion`), not as Metal does. native_half: bool, + /// Whether `write_masked_half`'s stage records `PrecisionNote::NativeNanEncoding`, announced + /// as its state is made: not where the device's moves of a `half` NaN are Metal's + /// (`DriverDeviation::MetalNanMoves`) and its write's store accounts for the texel's NaN. + half_write_nans: bool, } impl Kernels { @@ -498,6 +502,8 @@ impl Kernels { exact: metal_sampler(device), native_half: stage_notes(device, &bitcode, "read_half") .contains(&PrecisionNote::NativeHalfConversion), + half_write_nans: stage_notes(device, &bitcode, "write_masked_half") + .contains(&PrecisionNote::NativeNanEncoding), } } } @@ -2615,8 +2621,9 @@ fn write_then_read( /// read the planes as written (where the device converts the texture, its conversion of them: /// `ycbcr_device_reads`). A dispatch writing no texel keeps every byte. Nothing is announced /// past the lane's pipeline states' own notes, on a device without unified memory the first -/// `Shared` buffer's `shared-in-system-memory`, and where the device converts a texture itself -/// `ycbcr-filter-not-exact`. +/// `Shared` buffer's `shared-in-system-memory`, where the device converts a texture itself +/// `ycbcr-filter-not-exact`, and where the `half` write's own stage does not record it +/// (`Kernels::half_write_nans`) its variant's `native-nan-encoding`. #[test] fn a_four_four_four_texture_s_writes_store_metal_s_codes() { use PixelFormat as P; @@ -2738,6 +2745,13 @@ fn a_four_four_four_texture_s_writes_store_metal_s_codes() { if by_devices { expected.push(Degradation::YCbCrFilterNotExact); } + // The write's variant for such a texture (`StorageImageClass::YCbCrWrite`) hands the + // widened `half`'s bits to the runtime's encode as words, which records + // `native-nan-encoding` on every device: announced at its first dispatch where the + // kernel's own stage did not record it. + if !kernels.half_write_nans { + expected.push(Degradation::NativeNanEncoding); + } let expected = &expected[..]; let notices = assert_once(&messages.take_notices(), expected); assert!( @@ -3513,6 +3527,38 @@ fn assert_half_reads(kernels: &Kernels, texels: &[u8], want: &[[u16; 3]], case: assert_half_steps(kernels, texels, want, 8192, case); } +/// A `half` nearest sample's `float4`s `texels` through a runtime sampler slot against Metal's +/// `half` read of the steps `want` (of `1 / 8192`): each channel [`half_away`] of its step, or +/// within one `half` ulp of it, where the slot's word reads one texel and the device rounds its +/// `float` (`PrecisionNote::HalfTexelConversion`, which the runtime announces at the dispatch that +/// binds such a texture there; MoltenVK on the Apple M3 Max rounds toward zero), alpha 1. The +/// channels so rounded are counted, printed and returned. +fn assert_half_samples(texels: &[u8], want: &[[u16; 3]], case: &str) -> usize { + let mut rounded = 0usize; + for (i, (t, w)) in texels.as_chunks::<16>().0.iter().zip(want).enumerate() { + let c = |i: usize| f32::from_le_bytes(t[4 * i..4 * i + 4].try_into().unwrap()); + assert_eq!(c(3), 1.0, "{case}: texel {i}'s alpha"); + for (k, &step) in w.iter().enumerate() { + let (got, metal) = (c(k), half_away(step)); + if got.to_bits() == metal.to_bits() { + continue; + } + // The step's binade's `half` ulp: the device's `half` lies within one of Metal's. + let v = f64::from(step) / 8192.0; + let ulp = 2f64.powi(v.log2().floor() as i32 - 10); + assert!( + (f64::from(got) - f64::from(metal)).abs() <= ulp, + "{case}: texel {i} channel {k} (step {step}): {got} against Metal's {metal}" + ); + rounded += 1; + } + } + if rounded > 0 { + println!("YCBCR-HALF-SAMPLE {case}: {rounded} channels the device's rounding"); + } + rounded +} + /// A `half` read's `float4`s `texels` against `want`'s steps of `1 / grid`, alpha 1: each step's /// `half`, rounded with ties away from zero on the grid `8192` ([`half_away`]) and exact on the /// grid `2048`. Where the read's stage rounds by the device's conversion (`Kernels::native_half`, @@ -3548,10 +3594,11 @@ fn assert_half_steps(kernels: &Kernels, texels: &[u8], want: &[[u16; 3]], grid: /// The `_sRGB` twins over their surfaces read what Metal reads (`ycbcr_read` states it): /// `YCBCR8_420_2P_sRGB` over `420v` and `420f`, and `YCBCR10_420_2P_sRGB` over `x420`, `xf20`, /// `x422` and `xf44`, without a matrix, with BT.709 and with BT.2020. Each texel's `float` `read()` -/// and a linear sample at its centre are its step of `1/8192`; a `half` read and a `half` nearest -/// sample that step rounded with ties away from zero; and a linear sample between the texels the -/// filter of the kept image, whose planes no function reads in place ([`Between`], Metal's step -/// on the grid `8192` where read in place). The filter notices are [`assert_filter_notices`]'s. A +/// and a linear sample at its centre are its step of `1/8192`; a `half` read that step rounded with +/// ties away from zero, and a `half` nearest sample through the runtime's sampler slot that or, +/// announced, the device's rounding of it ([`assert_half_samples`]); and a linear sample between +/// the texels the filter of the kept image, whose planes no function reads in place +/// ([`Between`], Metal's step on the grid `8192` where read in place). The filter notices are [`assert_filter_notices`]'s. A /// twin the device converts itself reads Vulkan's conversion of the same codes through the curve /// instead, within the device's bound (`ycbcr_device_reads`), and its linear sample between the /// texels the filter of those. @@ -3563,6 +3610,7 @@ fn srgb_twins_read_what_metal_reads() { let nearest = sampler(device, SamplerMinMagFilter::Nearest); let mut steps = Between::new(device); let mut seed = 70; + let mut rounded = 0; let surfaces: [(PixelFormat, &[u8; 4], YCbCrRange); 6] = [ (PixelFormat::YCBCR84202PSRGB, b"420v", YCbCrRange::Video), (PixelFormat::YCBCR84202PSRGB, b"420f", YCbCrRange::Full), @@ -3617,24 +3665,28 @@ fn srgb_twins_read_what_metal_reads() { let half = texels_of(device, &kernels.read_half, &texture, None); let nearest_half = texels_of(device, &kernels.sample_half, &texture, Some(&nearest)); + // Each result, whether it is a `half`, and whether a sampler slot sampled it. let checks = [ ( &sampled, - false, + (false, true), format!("{case}: a linear sample at the centres"), ), - (&half, true, format!("{case}: a half read()")), + (&half, (true, false), format!("{case}: a half read()")), ( &nearest_half, - true, + (true, true), format!("{case}: a half nearest sample"), ), ]; - for (texels, half, what) in checks { + for (texels, (half, slot), what) in checks { match (by_device, half) { (true, _) => { assert_device_reads(texels, &vulkan, (codes.output, half), &what) } + (false, true) if slot => { + rounded += assert_half_samples(texels, &want, &what) + } (false, true) => assert_half_reads(&kernels, texels, &want, &what), (false, false) => assert_eq!(grid_of(texels, 8192), want, "{what}"), } @@ -3643,7 +3695,13 @@ fn srgb_twins_read_what_metal_reads() { steps.texels(&between, &read, in_place, &case); } } - steps.finish(device, &counts(&messages.take_notices()), "sRGB twins"); + let notices = counts(&messages.take_notices()); + // A `half` the device's rounding answered was announced. + assert!( + rounded == 0 || notices.contains_key(&Degradation::HalfTexelConversion), + "{rounded} half sample channels the device's rounding, unannounced: {notices:?}" + ); + steps.finish(device, ¬ices, "sRGB twins"); }); } diff --git a/crates/metal2vulkan/src/tests/engine_extended_range.rs b/crates/metal2vulkan/src/tests/engine_extended_range.rs index bdc0608e1..c278c1d35 100644 --- a/crates/metal2vulkan/src/tests/engine_extended_range.rs +++ b/crates/metal2vulkan/src/tests/engine_extended_range.rs @@ -648,9 +648,10 @@ fn argb8_storage_slots_rotate_their_bytes() { /// NaN is Metal's `0x7e00` where it takes `DriverDeviation::MetalHalfStorageNans` (MoltenVK's: the /// module requires its property), else the device's (`PrecisionNote::DeviceNanEncoding`); a /// formatless slot's notes hold under the format bound there (`tests::engine_half_storage_notes`). -/// A formatless slot may also reach a packed small float image, whose NaN is the device's on every -/// profile (noted too), or a normalized one, whose conversion is the device's on every profile -/// (noted too). The held lanes cover every tie between neighbouring halves and its +/// A formatless slot may also reach a packed small float image, whose NaN is the device's (noted +/// too) but where the profile takes `DriverDeviation::MetalSmallFloatNans` (the module requires +/// its property), or a normalized one, whose conversion is the device's on every profile (noted +/// too). The held lanes cover every tie between neighbouring halves and its /// neighbours, past the largest, the infinities and NaNs. #[test] fn half_float_storage_slots_leave_their_writes_to_the_device() { @@ -756,9 +757,15 @@ fn half_float_storage_slots_leave_their_writes_to_the_device() { nans, "{what}" ); + let small = p.takes_driver_deviation(DriverDeviation::MetalSmallFloatNans); + assert_eq!( + relied.contains(&VkProperty::MetalSmallFloatNans), + formatless && small, + "{what}" + ); assert_eq!( a.precision.contains(&PrecisionNote::DeviceNanEncoding), - !nans || formatless, + !nans || (formatless && !small), "{what}: {:?}", a.precision ); diff --git a/crates/metal2vulkan/tests/golden_hashes.tsv b/crates/metal2vulkan/tests/golden_hashes.tsv index 725a7b5b0..fcc2f53ee 100644 --- a/crates/metal2vulkan/tests/golden_hashes.tsv +++ b/crates/metal2vulkan/tests/golden_hashes.tsv @@ -16,7 +16,7 @@ profile intel-irisxe-rplp-anv c57b380689e33e05 profile intel-uhd770-rpls-anv c57b380689e33e05 profile lavapipe 8b0e38e4b195da6b profile lunarg-desktop-baseline-2023 a4079dbb79661db5 -profile moltenvk-m3 b82837f9130215bf +profile moltenvk-m3 0a8291d5ecbf3f75 profile nvidia-pascal-linux ffe884de52decdda profile nvidia-rtx50-linux 55dfd90c715d7263 profile nvidia-rtx5080-laptop c017cadc61d61b42