From 782da3582be303f9a40e2bde37dbb9cdd9d29cc9 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 10 Nov 2026 02:24:20 -0800 Subject: [PATCH] A half read of a 32-bit or small float texel records no NaN note where the device's half is Metal's On MoltenVK on the M3 Max (m3-max-13447, default policy) half-texel-nans-r32f, -rgba32f, -rg11b10f and -rgb9e5f answered every value as Metal does and still announced device-nan-encoding; runtime_lane::half_texel_nans read 0 differing half lanes. The profile takes DriverDeviation::MetalHalfConversion, whose measurement covers the half result of a read, sample and gather of every float word of RGBA32Float (NaNs among them) and 16,384 random words each of RG11B10Float and RGB9E5Float. half_texel_nans now records DeviceNanEncoding on such a profile only where the bound class is a half-float one (packed field 0) or of no family, and requires VkProperty::MetalHalfConversion; elsewhere it keeps the one clause of TexelFormat::NAN_FREE. The 16-bit float formats keep the note: no deviation records their NaN payloads as measured. Test: engine_texture's half-read test checks both forms per committed profile (clauses, formats that hold, the property) and that a profile of the deviation ran. Docs: the note's rustdoc, API.md's exemptions, the storage_properties doc. Under the branch's SEMANTICS_VERSION (2015). (cherry picked from commit b03555f8c9f05e382f744593cfaa01f2dc3bbb20) --- crates/metal2vulkan/src/lower/texture/mod.rs | 33 +++++++-- crates/metal2vulkan/src/pipeline/artifact.rs | 6 +- .../metal2vulkan/src/tests/engine_texture.rs | 72 ++++++++++++------- docs/API.md | 6 +- 4 files changed, 83 insertions(+), 34 deletions(-) diff --git a/crates/metal2vulkan/src/lower/texture/mod.rs b/crates/metal2vulkan/src/lower/texture/mod.rs index 66a65f4e0..e8c4c1c46 100644 --- a/crates/metal2vulkan/src/lower/texture/mod.rs +++ b/crates/metal2vulkan/src/lower/texture/mod.rs @@ -1003,7 +1003,8 @@ pub struct TexturePlan { /// conversion because it stores Metal's half (`MetalHalfStorageWrites`) and its NaN /// (`MetalHalfStorageNans`), a write that may reach a normalized format, because it stores /// Metal's codes (`MetalNormStorageWrites`), and a `half` texel's NaN to a 16-bit float format - /// (`MetalHalfTexelWrites`). The module requires them. + /// (`MetalHalfTexelWrites`); and a `half` read's NaN of a 32-bit float or packed small float + /// texel (`MetalHalfConversion`, `half_texel_nans`). The module requires them. pub storage_properties: BTreeSet, } @@ -1671,8 +1672,11 @@ pub(crate) fn half_texels( /// only where the class of the format bound there may hold a NaN (none of `TexelFormat::NAN_FREE`'s /// families): a test of `SpecKey::BoundTexelFormat`, which the runtime reads from the bound texture /// and no pipeline's key carries. A normalized, sRGB or extended-range texel decodes to no NaN, so -/// the device's encoding of one cannot show. Of a heap's element or a depth texture, wherever it -/// reads. +/// the device's encoding of one cannot show. On a profile that takes +/// `DriverDeviation::MetalHalfConversion` (the module then requires its property), not where the +/// class is a 32-bit float or packed small float one either: the `half` the device makes of such a +/// texel is measured to be Metal's, its NaN `0x7e00` (`device::driver_facts`). Of a heap's element +/// or a depth texture, wherever it reads. pub(crate) fn half_texel_nans( cx: &mut SelectCx<'_>, image: ImageRef, @@ -1681,8 +1685,27 @@ pub(crate) fn half_texel_nans( ) -> Result<(), Error> { match image { ImageRef::Slot(index) if !shape.depth => { - let free = crate::metal::texel_format::TexelFormat::NAN_FREE; - let when = vec![[NoteTest::equal(NoteOn::Bound(index), free, 0)].into()]; + use crate::metal::bicubic::BicubicTexels as B; + use crate::metal::texel_format::TexelFormat; + let bound = |mask, bits| NoteTest::equal(NoteOn::Bound(index), mask, bits); + let when = match cx + .profile + .takes_driver_deviation(DriverDeviation::MetalHalfConversion) + { + // A half-float class, and a class of no family. + true => { + cx.families + .texture + .storage_properties + .insert(crate::device::requirements::VkProperty::MetalHalfConversion); + let family = B::LINEAR | B::HALF | TexelFormat::NAN_FREE; + vec![ + [bound(family, B::HALF), bound(TexelFormat::PACKED_MASK, 0)].into(), + [bound(family, 0)].into(), + ] + } + false => vec![[bound(TexelFormat::NAN_FREE, 0)].into()], + }; cx.note_when(PrecisionNote::DeviceNanEncoding, when, m) } _ => { diff --git a/crates/metal2vulkan/src/pipeline/artifact.rs b/crates/metal2vulkan/src/pipeline/artifact.rs index 5b0c4462a..55007fda1 100644 --- a/crates/metal2vulkan/src/pipeline/artifact.rs +++ b/crates/metal2vulkan/src/pipeline/artifact.rs @@ -919,7 +919,11 @@ pub enum PrecisionNote { /// pixel fetch of a `half` texel: Metal hands back the texel's own NaN, sign and payload, and /// Vulkan states no encoding for a 16-bit float texel's decode (MoltenVK answers the canonical /// NaN for every one); on a device that rounds a `half` through its conversion under the float - /// controls (ANV, RADV), the NaN that conversion makes of the device's decode. Also recorded + /// controls (ANV, RADV), the NaN that conversion makes of the device's decode. Not by such an + /// access at a texture slot bound to a 32-bit float or packed small float format on a profile + /// that takes [`DriverDeviation::MetalHalfConversion`](crate::error::DriverDeviation::MetalHalfConversion), + /// whose `half` of such a texel is measured to be Metal's (`0x7e00` for its NaN); the module + /// then requires `VkProperty::MetalHalfConversion`. Also recorded /// by a `half` texel's write to a storage texture that may hold a float format, a slot whose /// storage choice declares none or a heap element: Metal stores a 16-bit float texel's NaN as /// the half's own bits and a 32-bit or packed small float one's as its canonical NaN, and the diff --git a/crates/metal2vulkan/src/tests/engine_texture.rs b/crates/metal2vulkan/src/tests/engine_texture.rs index bda684adf..f89ad4d1d 100644 --- a/crates/metal2vulkan/src/tests/engine_texture.rs +++ b/crates/metal2vulkan/src/tests/engine_texture.rs @@ -350,23 +350,26 @@ fn a_half_texel_read_records_the_device_nan_encoding() { /// A `half` read of a color texture slot records `DeviceNanEncoding` only where the format bound /// there may hold a NaN: `short_texels`' read of texture 2, its one `half` source, which no -/// arithmetic follows, specializes the note on one clause, a test of the class of the format -/// bound at that slot (`SpecKey::BoundTexelFormat`) against `TexelFormat::NAN_FREE`, which no -/// pipeline's key carries. It holds of a half-float, 32-bit float, small-float or unknown format -/// (an Apple M4 hands back their NaNs), and not of a normalized or sRGB one, whose +/// arithmetic follows, specializes the note on tests of the class of the format bound at that +/// slot (`SpecKey::BoundTexelFormat`), which no pipeline's key carries. It holds of a half-float or +/// unknown format, and of a 32-bit float or small-float one (an Apple M4 hands back their NaNs) +/// but on a profile that takes `DriverDeviation::MetalHalfConversion`, whose `half` of such a +/// texel is Metal's and which the module then requires; and not of a normalized or sRGB one, whose /// texels decode to no NaN. #[test] fn a_half_texel_read_notes_the_device_nan_encoding_where_the_bound_format_may_hold_one() { + use crate::device::requirements::VkProperty; + use crate::error::DriverDeviation; use crate::metal::format::bound_texel_format; - use crate::metal::texel_format::TexelFormat; let Some(bc) = fixture("texture_ops") else { return; }; - let mut half = 0; + let (mut half, mut measured) = (0, 0); for (stem, p) in committed_profiles().expect("profiles") { if !p.fields().images.storage_write_without_format { continue; } + let takes = p.takes_driver_deviation(DriverDeviation::MetalHalfConversion); let h = ok(compile(&bc, "short_texels", &p), &p, &stem); let entries: Vec<_> = h .specialized @@ -374,33 +377,34 @@ fn a_half_texel_read_notes_the_device_nan_encoding_where_the_bound_format_may_ho .filter(|s| s.note == PrecisionNote::DeviceNanEncoding) .collect(); assert_eq!(entries.len(), 1, "{stem}: {:?}", h.specialized); - let [clause] = entries[0].when.as_slice() else { - panic!("{stem}: {:?}", entries[0].when); - }; - assert!(clause.tests_bound_formats(), "{stem}: {clause:?}"); - let [test] = clause.all.as_slice() else { - panic!("{stem}: {clause:?}"); - }; - assert_eq!(test.key, SpecKey::BoundTexelFormat(texture_slot(2))); - assert_eq!( - (test.mask, test.bits, test.equal), - (TexelFormat::NAN_FREE, 0, true) - ); + let when = &entries[0].when; + assert_eq!(when.len(), if takes { 2 } else { 1 }, "{stem}: {when:?}"); + for clause in when { + assert!(clause.tests_bound_formats(), "{stem}: {clause:?}"); + for test in &clause.all { + assert_eq!(test.key, SpecKey::BoundTexelFormat(texture_slot(2))); + } + } let holds = |pf: Option| { - clause.holds_of_bound(&|slot| { - assert_eq!(slot, texture_slot(2)); - pf.map(bound_texel_format) + when.iter().any(|clause| { + clause.holds_of_bound(&|slot| { + assert_eq!(slot, texture_slot(2)); + pf.map(bound_texel_format) + }) }) }; - for pf in [ - PixelFormat::RGBA16Float, - PixelFormat::R16Float, - PixelFormat::RGBA32Float, - PixelFormat::RG11B10Float, - ] { + for pf in [PixelFormat::RGBA16Float, PixelFormat::R16Float] { assert!(holds(Some(pf)), "{stem}: {pf:?}"); } assert!(holds(None), "{stem}: no format known"); + for pf in [ + PixelFormat::RGBA32Float, + PixelFormat::R32Float, + PixelFormat::RG11B10Float, + PixelFormat::RGB9E5Float, + ] { + assert_eq!(holds(Some(pf)), !takes, "{stem}: {pf:?}"); + } for pf in [ PixelFormat::RGBA8Unorm, PixelFormat::R16Unorm, @@ -409,12 +413,26 @@ fn a_half_texel_read_notes_the_device_nan_encoding_where_the_bound_format_may_ho ] { assert!(!holds(Some(pf)), "{stem}: {pf:?}"); } + if takes { + assert!( + h.requirements + .properties + .contains(&VkProperty::MetalHalfConversion), + "{stem}: {:?}", + h.requirements.properties + ); + measured += 1; + } half += 1; } assert!( half > 0, "no committed profile compiles short_texels without inputs" ); + assert!( + measured > 0, + "no committed profile that takes MetalHalfConversion compiles short_texels" + ); } /// OpIsNan. diff --git a/docs/API.md b/docs/API.md index 49e2624ab..fbe9e9bb0 100644 --- a/docs/API.md +++ b/docs/API.md @@ -3693,7 +3693,11 @@ bound. 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 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`). + 16,446 NaNs among them (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 + requires `VkProperty::MetalHalfConversion`). - `metal2vulkan-degrade-restricted-depth-range` (precision). `PrecisionNote::RestrictedDepthRange`: The device compares a depth texture's texels on a device without `VK_EXT_depth_range_unrestricted` enabled (MoltenVK on the M3 Max, ANV on the Iris Xe, RADV), where Vulkan gives a depth image no