From 24d2ada6af7fddfde48e7841db6558733bef0657 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Tue, 3 Nov 2026 02:26:06 -0800 Subject: [PATCH] Answer Metal's extended-range read and write models publicly The validation lanes that hold extended-range texels to Metal compare the device's conversion with Metal's answer within PrecisionNote::DevicePackConversion's bound (a code within one of Metal's, a read within its bound of Metal's). That needs Metal's code-to-value mapping and its write's rounding, which the runtime's clear model already holds as the private `code_value` and `code`. ExtendedRange gains two thin public wrappers: - `read_value(code, alpha) -> Option`: Metal's float read of a code of an extended-range form, or None for the padded and bfloat forms and a code past 1023. - `write_code(v, alpha) -> Option`: Metal's kernel write's code of a float of an extended-range or padded form, or None for the bfloat forms. Each doc comment states Metal's mapping and rounding, and says that a shader's conversion is the device's, bounded against this one. api_signatures.tsv gains the two method rows. The api_surface table lists items, not methods, and ExtendedRange's row is unchanged. No behaviour changes and no version bump. The pin needs recomputing, since metal/format/mod.rs is a hashed translator file. Verified: cargo nextest run -p metal2vulkan (api_signatures, api_surface and metal::format tests) 64 passed; clippy -D warnings plain and --cfg m2v_gpu; rustdoc -D warnings for metal2vulkan. (cherry picked from commit 753e6ac604e2f4be8150d787e50574ee880798a6) (cherry picked from commit 99e4bd12fd251ee97615d0ab535980089e5e0a71) --- crates/metal2vulkan/src/metal/format/mod.rs | 27 ++++++++++++++++++++ crates/metal2vulkan/tests/api_signatures.tsv | 2 ++ 2 files changed, 29 insertions(+) diff --git a/crates/metal2vulkan/src/metal/format/mod.rs b/crates/metal2vulkan/src/metal/format/mod.rs index 8ba35c994..39c02cc3e 100644 --- a/crates/metal2vulkan/src/metal/format/mod.rs +++ b/crates/metal2vulkan/src/metal/format/mod.rs @@ -639,6 +639,33 @@ impl ExtendedRange { })) } + /// The `float` value Metal reads from the code `code` (below 1024) of a color channel, or of + /// alpha where `alpha`, of a `BGR10_XR`, `BGRA10_XR` or two-plane format or an `_sRGB` twin + /// (an Apple M3 Max and an M4): `(code - 384) / 510` correctly rounded, an `_sRGB` color + /// channel's step of Metal's table over 8192, the two-plane formats' alpha byte over 255 + /// correctly rounded. `None` for the padded and bfloat forms and a larger code. A shader's read + /// of such a texel is the device's conversion, not this value + /// ([`PrecisionNote::DevicePackConversion`](crate::pipeline::PrecisionNote::DevicePackConversion) + /// bounds it against this one). + pub fn read_value(self, code: u32, alpha: bool) -> Option { + (!self.padded() && !self.bfloat() && code < 1024).then(|| self.code_value(code, alpha)) + } + + /// The code Metal's kernel write stores for the `float` value `v` of a color channel, or of + /// alpha where `alpha`, of an extended-range or padded form (an Apple M3 Max and an M4): a + /// NaN as 0; a color channel's `384 ± RoundHalfEven(min(|v|, 1.5) · 510)` of the exact + /// product, clamped to `[0, 1023]`, an `_sRGB` twin's by Metal's own encode table; alpha's + /// clamped to `[0, 1]`, `384 + RoundHalfEven(a · 510)`, the two-plane formats' byte + /// `RoundHalfEven(a · 255)`; a padded form's the exact product of the value clamped to + /// `[0, 1]` with 1023 rounded to nearest even, an `_sRGB` twin's by Metal's tables, + /// `RGB10A2Unorm_sRGB`'s alpha the product with 3. `None` for the bfloat forms. A shader's + /// write of such a texel is the device's conversion, not this code + /// ([`PrecisionNote::DevicePackConversion`](crate::pipeline::PrecisionNote::DevicePackConversion) + /// bounds it against this one). + pub fn write_code(self, v: f32, alpha: bool) -> Option { + (!self.bfloat()).then(|| self.code(v, alpha)) + } + /// The value Metal reads from the code `code` of a color channel, or of alpha where `alpha`, /// of a `BGR10_XR`, `BGRA10_XR` or two-plane format or an `_sRGB` twin: `(code - 384) / 510` /// correctly rounded, an `_sRGB` color channel's table step, the two-plane formats' alpha byte diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 64018db9e..00abbe4ad 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -9003,7 +9003,9 @@ method metal2vulkan::metal::format::ExtendedRange::padded pub fn padded(self) -> method metal2vulkan::metal::format::ExtendedRange::padded_srgb pub fn padded_srgb(self) -> bool method metal2vulkan::metal::format::ExtendedRange::raw_components pub fn raw_components(self) -> u32 method metal2vulkan::metal::format::ExtendedRange::raw_format pub fn raw_format(self) -> vk::Format +method metal2vulkan::metal::format::ExtendedRange::read_value pub fn read_value(self, code: u32, alpha: bool) -> Option method metal2vulkan::metal::format::ExtendedRange::srgb pub fn srgb(self) -> bool +method metal2vulkan::metal::format::ExtendedRange::write_code pub fn write_code(self, v: f32, alpha: bool) -> Option method metal2vulkan::metal::format::FormatMapping::direct pub fn direct(format: vk::Format, aspects: vk::ImageAspectFlags) -> FormatMapping method metal2vulkan::metal::format::WordAttachment::fields pub fn fields(self) -> [Option; 4] method metal2vulkan::metal::format::WordAttachment::layout pub fn layout(pf: PixelFormat) -> Option