From 9fe73ba9774cc6d81d221fe74aaf870b68cfe43c Mon Sep 17 00:00:00 2001 From: artor410 <62668404+artor410@users.noreply.github.com> Date: Mon, 23 Nov 2026 20:15:06 -0300 Subject: [PATCH] shader: decode the packed typed-buffer formats Typed buffer loads in the 10_11_11, 11_11_10 and 10_10_10_2 families (GFX10 unified formats 30-49) were refused, failing the shader. Fill in their channel layouts and decode the unsigned 11- and 10-bit floats by shifting them into half position for legacyF16ToF32, which is exact for denormals, infinities and NaNs. Format numbers from LLVM's UfmtGFX10, channel order from Mesa's ac_formats.c. Co-Authored-By: Claude Opus 5.5 --- crates/victory-shader/src/emit/lower.rs | 143 +++++++++++++++++++----- 1 file changed, 117 insertions(+), 26 deletions(-) diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index ca5e898..67415f3 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -101,33 +101,46 @@ fn buffer_format(format: u8) -> Option<(&'static [u32], Numeric)> { const NORM: &[Numeric] = &[Unorm, Snorm, Uscaled, Sscaled, Uint, Sint]; const ALL: &[Numeric] = &[Unorm, Snorm, Uscaled, Sscaled, Uint, Sint, Float]; const WIDE: &[Numeric] = &[Uint, Sint, Float]; - // `None`: a family not decoded (packed floats, 10_10_10_2). - const FAMILIES: [(Option<&[u32]>, &[Numeric]); 14] = [ - (Some(&[8]), NORM), - (Some(&[16]), ALL), - (Some(&[8, 8]), NORM), - (Some(&[32]), WIDE), - (Some(&[16, 16]), ALL), - (None, ALL), // 10_11_11 - (None, ALL), // 11_11_10 - (None, NORM), // 10_10_10_2 - (Some(&[10, 10, 10, 2]), NORM), // 2_10_10_10 - (Some(&[8, 8, 8, 8]), NORM), - (Some(&[32, 32]), WIDE), - (Some(&[16, 16, 16, 16]), ALL), - (Some(&[32, 32, 32]), WIDE), - (Some(&[32, 32, 32, 32]), WIDE), + // Numbers and order from LLVM's `UfmtGFX10` (`SIDefines.h`); the GFX10 + // buffer formats have no 5_9_9_9. Channel order from Mesa + // (`ac_formats.c`): R11G11B10_FLOAT is 10_11_11, and 10_10_10_2 has + // its 2-bit channel in the lowest bits. + const FAMILIES: [(&[u32], &[Numeric]); 14] = [ + (&[8], NORM), + (&[16], ALL), + (&[8, 8], NORM), + (&[32], WIDE), + (&[16, 16], ALL), + (&[11, 11, 10], ALL), // 10_11_11 + (&[10, 11, 11], ALL), // 11_11_10 + (&[2, 10, 10, 10], NORM), // 10_10_10_2 + (&[10, 10, 10, 2], NORM), // 2_10_10_10 + (&[8, 8, 8, 8], NORM), + (&[32, 32], WIDE), + (&[16, 16, 16, 16], ALL), + (&[32, 32, 32], WIDE), + (&[32, 32, 32, 32], WIDE), ]; let mut n = usize::from(format.checked_sub(1)?); for (bits, numerics) in FAMILIES { if n < numerics.len() { - return Some((bits?, numerics[n])); + return Some((bits, numerics[n])); } n -= numerics.len(); } None } +/// How far a float channel of `width` bits (16, or the unsigned 11- and +/// 10-bit floats of the packed formats) shifts up to be a half: +/// the small floats are a half's exponent (5 bits, biased 15) and the top +/// of its mantissa, without the sign, so `legacyF16ToF32` widens them +/// exactly, denormals, infinities and NaNs included. +fn half_shift(width: u32) -> u32 { + debug_assert!(matches!(width, 10 | 11 | 16), "{width}-bit float"); + if width == 16 { 0 } else { 15 - width } +} + struct Lower<'a> { s: Shader, x: &'a Parts<'a>, @@ -3041,11 +3054,10 @@ impl<'a> Lower<'a> { /// typed loads do (channels past the format's read 0, 0, 0, 1). // shortcut: the descriptor's channel swizzle is taken as the identity // (it is for raw views and the descriptors code builds), elements of 4 - // bytes or more as dword-aligned, and the packed float and 10_10_10_2 - // layouts are refused; decode them when a program needs them. Norm - // formats divide, which the AMD driver does 1 ULP off the hardware's - // correctly rounded conversion for some values; correct it with an - // integer check if a title compares converted values exactly. + // bytes or more as dword-aligned. Norm formats divide, which the AMD + // driver does 1 ULP off the hardware's correctly rounded conversion for + // some values; correct it with an integer check if a title compares + // converted values exactly. fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result, Why> { let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?; let c = self.cur; @@ -3132,9 +3144,16 @@ impl<'a> Lower<'a> { Numeric::Sint => signed(self), Numeric::Float if w == 32 => field, Numeric::Float => { + let h = match half_shift(w) { + 0 => field, + k => { + let sh = self.s.m.i32(k); + self.s.m.bin(c, Bin::Shl, field, sh) + } + }; let f = self .s - .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[field]); + .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[h]); float(self, f) } }; @@ -6559,7 +6578,7 @@ impl<'a> Lower<'a> { #[cfg(test)] mod tests { - use super::{Numeric, Parts, buffer_format, lower}; + use super::{Numeric, Parts, buffer_format, half_shift, lower}; use crate::native::{ComputeIo, HwStage, NativeProgram, StageIo}; use crate::{io, ir, resource, structure}; @@ -6645,9 +6664,81 @@ mod tests { buffer_format(77), Some((&[32, 32, 32, 32][..], Numeric::Float)) ); - // Invalid, a packed float, past the table. + // Invalid, past the table. assert_eq!(buffer_format(0), None); - assert_eq!(buffer_format(36), None); assert_eq!(buffer_format(78), None); } + + #[test] + fn packed_buffer_formats_by_number() { + // LLVM's `UfmtGFX10`: 10_11_11 is 30-36, 11_11_10 37-43, + // 10_10_10_2 44-49. Channels from the lowest bits: R11G11B10_FLOAT + // is 10_11_11 (Mesa), 10_10_10_2 has the 2-bit channel first. + assert_eq!(buffer_format(30), Some((&[11, 11, 10][..], Numeric::Unorm))); + assert_eq!(buffer_format(36), Some((&[11, 11, 10][..], Numeric::Float))); + assert_eq!(buffer_format(37), Some((&[10, 11, 11][..], Numeric::Unorm))); + assert_eq!(buffer_format(43), Some((&[10, 11, 11][..], Numeric::Float))); + assert_eq!( + buffer_format(44), + Some((&[2, 10, 10, 10][..], Numeric::Unorm)) + ); + assert_eq!( + buffer_format(45), + Some((&[2, 10, 10, 10][..], Numeric::Snorm)) + ); + assert_eq!( + buffer_format(46), + Some((&[2, 10, 10, 10][..], Numeric::Uscaled)) + ); + assert_eq!( + buffer_format(47), + Some((&[2, 10, 10, 10][..], Numeric::Sscaled)) + ); + assert_eq!( + buffer_format(48), + Some((&[2, 10, 10, 10][..], Numeric::Uint)) + ); + assert_eq!( + buffer_format(49), + Some((&[2, 10, 10, 10][..], Numeric::Sint)) + ); + } + + /// A half's value (sign, 5-bit exponent biased 15, 10-bit mantissa), as + /// `legacyF16ToF32` widens it. + fn half(h: u32) -> f32 { + let (s, e, m) = ((h >> 15) & 1, (h >> 10) & 31, h & 1023); + let v = match e { + 0 => m as f32 * 2f32.powi(-24), + 31 if m == 0 => f32::INFINITY, + 31 => f32::NAN, + _ => (1024 + m) as f32 * 2f32.powi(e as i32 - 25), + }; + if s == 1 { -v } else { v } + } + + #[test] + fn small_floats_widen_as_halves() { + // Unsigned 11- and 10-bit floats: a 5-bit exponent biased 15 over a + // 6- or 5-bit mantissa, no sign; values from the D3D/GL spec. + let f11 = |x: u32| half(x << half_shift(11)); + let f10 = |x: u32| half(x << half_shift(10)); + assert_eq!(f11(0), 0.0); + assert_eq!(f11(0x001), 2f32.powi(-20)); // smallest denormal + assert_eq!(f11(0x03F), 63.0 * 2f32.powi(-20)); // largest denormal + assert_eq!(f11(0x040), 2f32.powi(-14)); // smallest normal + assert_eq!(f11(0x3C0), 1.0); + assert_eq!(f11(0x3E0), 1.5); + assert_eq!(f11(0x7BF), 65024.0); // largest + assert_eq!(f11(0x7C0), f32::INFINITY); + assert!(f11(0x7C1).is_nan()); + assert_eq!(f10(0x001), 2f32.powi(-19)); + assert_eq!(f10(0x020), 2f32.powi(-14)); + assert_eq!(f10(0x1E0), 1.0); + assert_eq!(f10(0x3DF), 64512.0); + assert_eq!(f10(0x3E0), f32::INFINITY); + assert!(f10(0x3FF).is_nan()); + // A 16-bit float is a half already, sign included. + assert_eq!(half(0xBC00 << half_shift(16)), -1.0); + } }