diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index 165b6753c..8732c5948 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -2095,10 +2095,15 @@ degradations! { /// normalized vertex attribute's codes (the 8- and 16-bit normalized formats of either /// sign, `UChar4Normalized_BGRA`, `Int1010102Normalized`, `UInt1010102Normalized`) /// through its `UNORM` or `SNORM` vertex format, where Metal's fetch answers the code over - /// its maximum correctly rounded. Recorded bound: within one `f32` ulp where measured - /// (lavapipe's reciprocal product reads some 8-bit unsigned codes one ulp high; its 16-bit - /// and packed formats, RADV's unsigned ones and MoltenVK's on an Apple M3 Max Metal's); - /// the signed formats off MoltenVK are unmeasured. + /// its maximum correctly rounded (an `Int1010102Normalized` 10-bit field times + /// `float(1/511)`). Not recorded for a `float` attribute on a profile that takes + /// `DriverDeviation::MetalNormalizedVertexFetch` (MoltenVK on the Apple M3 Max's macOS 27 + /// identity, measured Metal's at every code). Recorded bound: within one `f32` ulp where + /// measured (lavapipe's reciprocal product reads some 8-bit unsigned codes one ulp high; + /// RADV's unsigned formats are Metal's), but lavapipe's per-instance fetch reads the most + /// negative code of an 8- or 16-bit signed format unclamped (-128/127, -32768/32767) where + /// Metal reads -1; a 16-bit format read as `half`, one `half` ulp at four codes; the signed + /// formats on RADV, ANV and NVIDIA are unmeasured. DeviceVertexConversion = "device-vertex-conversion"; /// Class: native half rounding. The device's own conversion of a `float` to 16 bits /// rounds the pipeline's `half` results, on a device with `shaderFloat16` whose float diff --git a/crates/metal2vulkan-validation/runtime-sources.sha256 b/crates/metal2vulkan-validation/runtime-sources.sha256 index 4fe4f0f16..af3a86a92 100644 --- a/crates/metal2vulkan-validation/runtime-sources.sha256 +++ b/crates/metal2vulkan-validation/runtime-sources.sha256 @@ -1 +1 @@ -809db10bac4766de916eb25e3895058fbfb2f414e7c1fe4b88b319349fe220ba +bbf9691cdd6c7686bff4111b21573c6f63040fcf262477798925ccc67fb94b18 diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 58f3d53a5..64696a049 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -60,7 +60,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 = 1889; +pub const SEMANTICS_VERSION: u32 = 1890; /// 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/lower/stage_io/fetch.rs b/crates/metal2vulkan/src/lower/stage_io/fetch.rs index afa8a4fda..98323eba4 100644 --- a/crates/metal2vulkan/src/lower/stage_io/fetch.rs +++ b/crates/metal2vulkan/src/lower/stage_io/fetch.rs @@ -665,7 +665,10 @@ fn field(fb: &mut FunctionBuilder<'_>, w: SValue, at: u32, width: u32) -> BResul /// `max(c / (2^(b-1) - 1), -1)`, each correctly rounded. The quotient is /// [`exact_division::div_exact`]'s integer sequence, not a device division, which may be an ULP /// off; a signed field divides its magnitude clamped to the scale (the most negative code is -/// `-1` exactly) and takes the code's sign, as `air.unpack`'s SNORM formats do. +/// `-1` exactly) and takes the code's sign, as `air.unpack`'s SNORM formats do. A signed 10-bit +/// field (`Int1010102Normalized`'s three) is Metal's product instead: the clamped magnitude times +/// `float(1/511)`, one rounding, which lies an ULP toward zero of the quotient at 96 of the 1,024 +/// codes (`runtime_lane::normalized_vertex_fetch` measures Metal's fetch so). fn normalized(fb: &mut FunctionBuilder<'_>, c: SValue, bits: u8, signed: bool) -> BResult { let u = u32_scalar(); if !signed { @@ -683,7 +686,14 @@ fn normalized(fb: &mut FunctionBuilder<'_>, c: SValue, bits: u8, signed: bool) - let mag = fb.select(neg, minus, v)?; let dk = k32(fb, d)?; let mag = fb.glsl(GlslOp::UMin, &[mag, dk])?; - let q = exact_division::div_exact(fb, u, mag, d)?; + let q = if bits == 10 { + let ft = ops::ty(fb, f32_scalar())?; + let m = fb.convert(ConvOp::ConvertUToF, mag, ft)?; + let r = ops::splat(fb, f32_scalar(), u64::from((1.0f32 / 511.0).to_bits()))?; + fb.binary_with(BinOp::FMul, m, r, Contraction::Forbidden)? + } else { + exact_division::div_exact(fb, u, mag, d)? + }; let nq = fb.unary(UnOp::FNegate, q)?; fb.select(neg, nq, q) } @@ -1031,7 +1041,15 @@ pub(crate) fn reference(encoding: Encoding, bytes: &[u8]) -> [f64; 4] { Encoding::Rgb10A2 { signed } => { let w = word(0); for (o, (at, width)) in out.iter_mut().zip([(0, 10), (10, 10), (20, 10), (30, 2)]) { - *o = norm((w >> at) & ((1 << width) - 1), width, signed); + let c = (w >> at) & ((1 << width) - 1); + *o = if signed && width == 10 { + // Metal's product: the clamped magnitude times `float(1/511)`. + let v = sx(c, width); + let q = (v.unsigned_abs().min(511) as f32) * (1.0f32 / 511.0); + f64::from(if v < 0 { -q } else { q }) + } else { + norm(c, width, signed) + }; } } Encoding::Rg11B10Float => { diff --git a/crates/metal2vulkan/src/lower/stage_io/fetch/tests.rs b/crates/metal2vulkan/src/lower/stage_io/fetch/tests.rs index cd5173b93..983a2a5b2 100644 --- a/crates/metal2vulkan/src/lower/stage_io/fetch/tests.rs +++ b/crates/metal2vulkan/src/lower/stage_io/fetch/tests.rs @@ -687,10 +687,12 @@ fn every_normalized_byte_is_correctly_rounded() { } } -/// Every 10-bit and 2-bit code of the packed 10:10:10:2 formats decodes to the correctly rounded -/// quotient, signed codes (the most negative one `-1`) and unsigned. +/// Every 10-bit and 2-bit code of the packed 10:10:10:2 formats decodes as Metal's fetch does: +/// the correctly rounded quotient, signed codes (the most negative one `-1`) and unsigned, but a +/// signed 10-bit field, which is its clamped magnitude times `float(1/511)` (an ULP toward zero +/// of the quotient at 96 of the 1,024 codes). #[test] -fn every_ten_and_two_bit_code_is_correctly_rounded() { +fn every_ten_and_two_bit_code_decodes_as_metals() { let n = 1024u32.div_ceil(3); let words: Vec = (0..n) .map(|i| { @@ -709,7 +711,12 @@ fn every_ten_and_two_bit_code_is_correctly_rounded() { let got = run(&m, &data, [i as u32, 0, 4, 0], 4); let want = [(0, 10u32), (10, 10), (20, 10), (30, 2)].map(|(at, bits)| { let c = (w >> at) & ((1 << bits) - 1); - if signed { + if signed && bits == 10 { + let s = 32 - bits; + let c = ((c << s) as i32) >> s; + let q = (c.unsigned_abs().min(511) as f32) * (1.0f32 / 511.0); + f64::from(if c < 0 { -q } else { q }) + } else if signed { let s = 32 - bits; let c = i64::from(((c << s) as i32) >> s); exact_quotient(c, (1 << (bits - 1)) - 1) @@ -721,6 +728,16 @@ fn every_ten_and_two_bit_code_is_correctly_rounded() { check_lanes(&format!("{format:?} element {i}"), float(4), &got, want); } } + let off = (-512i64..512) + .filter(|&c| { + let q = (c.unsigned_abs().min(511) as f32) * (1.0f32 / 511.0); + f64::from(if c < 0 { -q } else { q }) != exact_quotient(c, 511) + }) + .count(); + assert_eq!( + off, 96, + "the codes where Metal's product is not the quotient" + ); } /// A `half` field's widening to a `float` or `half` parameter, and a `FloatRG11B10` field's (a diff --git a/docs/API.md b/docs/API.md index a3dc47fc2..3f8238749 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2946,9 +2946,13 @@ bound. either sign, `UChar4Normalized_BGRA`, `Int1010102Normalized`, `UInt1010102Normalized`) to an attribute that fixed-function vertex input fetches, so the attribute is the device's `UNORM` or `SNORM` conversion of its codes, where Metal's fetch answers the code over its maximum correctly - rounded: within one `f32` ulp where measured (lavapipe reads some 8-bit unsigned codes one ulp - high). Recorded on the pipeline's vertex stage, on every device; one module serves every format - the device fetches. + rounded (an `Int1010102Normalized` 10-bit field times `float(1/511)`): within one `f32` ulp + where measured (lavapipe reads some 8-bit unsigned codes one ulp high), but lavapipe's + per-instance fetch, which reads the most negative code of an 8- or 16-bit signed format + unclamped (-128/127, -32768/32767) where Metal reads -1. Recorded on the pipeline's vertex stage + on every device but one whose profile takes `DriverDeviation::MetalNormalizedVertexFetch` + (MoltenVK on the Apple M3 Max's macOS 27 identity), where only a 16-bit format read as `half` + records it; one module serves every format the device fetches. - `metal2vulkan-degrade-native-half-conversion` (precision). `PrecisionNote::NativeHalfConversion`: Native half rounding (a conversion): the module rounds a `half` result it computes in `float` (a `half` operation's, a function's, a conversion from `float`'s, a texel's) by the device's diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index d48094d39..45438f55a 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -301,14 +301,6 @@ recorded here. Open differences from Metal that no note announces yet, and runtime costs still above the driver's. Each is open work. -- **A pulled `Int1010102Normalized` attribute's 10-bit fields are the correctly rounded - quotient `c / 511`, where Metal's fetch multiplies the code by `float(1 / 511)` in `float`:** - one `f32` ulp farther from zero at 96 of the 1,024 codes (48 of each sign; measured at every - code on the Apple M3 Max under macOS 27.0.1, per vertex and per instance). The shader's decode - (`lower::stage_io::fetch`) serves every attribute fixed-function input does not fetch: Metal's - constant step on MoltenVK, a device without buffer robustness, the indexed capture form, and a - kernel's stage input, whose Metal answer is unmeasured. Not announced. - - **A shadowed texture over an IOSurface plane's bytes is copied in whole for every command buffer that samples it**, where it is a shadow: one that only samples or reads is an image in place where its class's check passed (`in_place.rs`), so this holds for a plane that renders or