From 4243a96a787a63cdc4ac943c88867fbc0631f12c Mon Sep 17 00:00:00 2001 From: Marvis Date: Wed, 25 Nov 2026 04:45:10 +0100 Subject: [PATCH] Shader: 16-bit typed buffer accesses pass bits through integer views On GFX10 a typed buffer access converts by the buffer format of its descriptor, and `_d16` only packs the 16-bit results: through a UINT or SINT view, `buffer_load_format_d16_*` and `buffer_store_format_d16_*` move the 16-bit integers as they are. The lowering always converted them as halves (f32 to f16 on loads, f16 to f32 on stores) through a float-typed handle, so through an integer view a store wrote a float's bit pattern into the 16-bit element and a load turned the integer's bits into a denormal and then to 0. Grand Theft Auto VI's compute graph (the 65 compute_graph_ops programs: the face network's matrix products, activations, reductions, copies; the only users of these accesses in the title's world set) keeps its halves in R16G16B16A16_UINT and R16_UINT views. Its output was bit soup that changed from frame to frame (shards6: 0x0011 0x0001 0x0015 0x006D 0x80EC ... at frame 1658, 0xD8A8 0xA388 0x2F30 0x0472 ... at 1670 in the same place), the blendshape extract turned that into the head's translations, and the main character's head came out as a flat slab or not at all. The view's format kind now rides in the view-stride table, beside the stride: bit 31 an integer format, bit 30 a signed one (VIEW_INTEGER, VIEW_SIGNED; stride readers mask them off). The host sets them for typed buffer views of UINT and SINT formats. A d16 typed load through such a view keeps each channel's low 16 bits; a d16 typed store zero- or sign-extends the 16-bit integer. Float, normalized, raw and structured views keep the conversion they had. The integer path still uses the float-typed handle and relies on the driver passing a mismatched component type's bits through (shortcut comment; checked on the RX 6700 XT and WARP by the new tests). RESULTS Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/descriptor/host.rs | 71 ++++++- crates/victory-shader/src/emit/host.rs | 8 +- crates/victory-shader/src/emit/lower.rs | 78 +++++++- crates/victory-shader/src/emit/mod.rs | 3 +- crates/victory-shader/src/emit/shader.rs | 16 +- .../victory-shader/src/emit/stride_tests.rs | 174 +++++++++++++++++- 6 files changed, 333 insertions(+), 17 deletions(-) diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 0a816f4..8eb4c15 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -468,9 +468,10 @@ impl HostHeap { } /// Enters the byte stride of `view` (0 for none or not a buffer view) - /// for slot `index` in the view-stride table. + /// for slot `index` in the view-stride table, with the integer-format + /// bits of a typed buffer view ([`view_kind`]). fn set_stride(&self, index: u32, view: Option<&View>) { - self.write_stride(index, view.map_or(0, view_stride)); + self.write_stride(index, view.map_or(0, |v| view_stride(v) | view_kind(v))); } fn write_stride(&self, index: u32, stride: u32) { @@ -830,6 +831,51 @@ fn view_stride(view: &View) -> u32 { element.max(1) as u32 } +/// The view-stride table's format-kind bits of a buffer view: a typed view +/// (no structure stride, not raw) of an integer format gets +/// `VIEW_INTEGER`, a signed one also `VIEW_SIGNED`; anything else none. +/// Translated shaders pass 16-bit typed data of such views as bits, not as +/// halves (`victory_shader::emit`, "d16"). +fn view_kind(view: &View) -> u32 { + use victory_shader::emit::{VIEW_INTEGER, VIEW_SIGNED}; + let (unordered, words): (bool, &[u32; 10]) = match view { + View::Shader(_, Some(desc)) => (false, desc), + View::Unordered(_, _, Some(desc)) => (true, desc), + View::Placed(unordered, resource, desc) if resource[0] == 1 => match desc[..10].try_into() { + Ok(words) => (*unordered, words), + Err(_) => return 0, + }, + _ => return 0, + }; + // Buffer views only (dimension 1); a structured or raw one is no typed view. + let (format, typed) = if unordered { + let desc = uav_desc(words); + // SAFETY: a buffer view's union member. + let b = unsafe { desc.Anonymous.Buffer }; + (desc.Format.0 as u32, b.StructureByteStride == 0 && b.Flags.0 & D3D12_BUFFER_UAV_FLAG_RAW.0 == 0) + } else { + let desc = srv_desc(words); + // SAFETY: a buffer view's union member. + let b = unsafe { desc.Anonymous.Buffer }; + (desc.Format.0 as u32, b.StructureByteStride == 0 && b.Flags.0 & D3D12_BUFFER_SRV_FLAG_RAW.0 == 0) + }; + match (words[1] == 1 && typed).then(|| integer_format(format)).flatten() { + Some(true) => VIEW_INTEGER | VIEW_SIGNED, + Some(false) => VIEW_INTEGER, + None => 0, + } +} + +/// Whether a DXGI format is an integer one, and if so whether signed: +/// `Some(false)` for `UINT`, `Some(true)` for `SINT`, `None` otherwise. +fn integer_format(format: u32) -> Option { + match format { + 3 | 7 | 12 | 17 | 25 | 30 | 36 | 42 | 50 | 57 | 62 => Some(false), + 4 | 8 | 14 | 18 | 32 | 38 | 43 | 52 | 59 | 64 => Some(true), + _ => None, + } +} + /// A buffer view of title memory at `address` on its host copy: the first /// element moves by the copy's offset. /// Bytes per element of a buffer view. @@ -1472,6 +1518,27 @@ fn null_view(at: D3D12_CPU_DESCRIPTOR_HANDLE) { mod tests { use super::*; + /// Typed buffer views of integer formats carry their kind in the + /// view-stride table; float, normalized, raw and structured ones none. + #[test] + fn integer_typed_buffer_views_are_marked_in_the_stride_table() { + use victory_shader::emit::{VIEW_INTEGER, VIEW_SIGNED}; + // Format, dimension (buffer), first element, count, stride, counter, flags. + let uav = |format, stride, flags| View::Unordered(0, 0, Some([format, 1, 0, 0, 64, stride, 0, 0, flags, 0])); + assert_eq!(view_kind(&uav(12, 0, 0)), VIEW_INTEGER, "R16G16B16A16_UINT"); + assert_eq!(view_kind(&uav(57, 0, 0)), VIEW_INTEGER, "R16_UINT"); + assert_eq!(view_kind(&uav(59, 0, 0)), VIEW_INTEGER | VIEW_SIGNED, "R16_SINT"); + assert_eq!(view_kind(&uav(64, 0, 0)), VIEW_INTEGER | VIEW_SIGNED, "R8_SINT"); + assert_eq!(view_kind(&uav(10, 0, 0)), 0, "R16G16B16A16_FLOAT"); + assert_eq!(view_kind(&uav(58, 0, 0)), 0, "R16_SNORM"); + assert_eq!(view_kind(&uav(39, 0, 1)), 0, "raw"); + assert_eq!(view_kind(&uav(0, 16, 0)), 0, "structured"); + // Format, dimension, mapping, first element, count, stride, flags. + let srv = View::Shader(0, Some([62, 1, 5768, 0, 0, 0, 64, 0, 0, 0])); + assert_eq!(view_kind(&srv), VIEW_INTEGER, "R8_UINT"); + assert_eq!(view_stride(&uav(12, 0, 0)) | view_kind(&uav(12, 0, 0)), 8 | VIEW_INTEGER); + } + /// A placed texture's view the title writes into a heap is left null /// and noted once: heap syncs (every submission) pass it by, a draw /// reading it through a table makes it. diff --git a/crates/victory-shader/src/emit/host.rs b/crates/victory-shader/src/emit/host.rs index 76f2455..5ab0a9e 100644 --- a/crates/victory-shader/src/emit/host.rs +++ b/crates/victory-shader/src/emit/host.rs @@ -1239,6 +1239,9 @@ pub enum View<'a> { Uint2TextureUav(u32, u32), /// A typed RGBA16F buffer UAV of this many elements, zeroed. HalfUav(u32), + /// A typed buffer UAV in this format, of this many elements of this many + /// bytes, zeroed. + TypedUav(DXGI_FORMAT, u32, u32), /// A constant buffer of these dwords. Cbv(&'a [u32]), /// A buffer SRV over these dwords. @@ -1543,7 +1546,7 @@ impl Host { dev.CreateShaderResourceView(&tex, None, at); keep.push(tex); } - View::RawUav(_) | View::HalfUav(_) => { + View::RawUav(_) | View::HalfUav(_) | View::TypedUav(..) => { // (dwords, format, elements, flags) let (n, format, elements, flags) = match *v { View::HalfUav(e) => ( @@ -1552,6 +1555,9 @@ impl Host { e, D3D12_BUFFER_UAV_FLAG_NONE, ), + View::TypedUav(format, e, bytes) => { + ((e * bytes).div_ceil(4), format, e, D3D12_BUFFER_UAV_FLAG_NONE) + } View::RawUav(n) => { (n, DXGI_FORMAT_R32_TYPELESS, n, D3D12_BUFFER_UAV_FLAG_RAW) } diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index d014a85..ad88685 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -11,7 +11,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; use super::UserData; use super::bitcode::{Attr, Bin, Cast, Term, V, pred}; use super::container::SigElement; -use super::shader::{Shader, VIEW_STRIDES, flags}; +use super::shader::{Shader, VIEW_INTEGER, VIEW_SIGNED, VIEW_STRIDE_BITS, VIEW_STRIDES, flags}; use crate::ShaderError; use crate::gfx10::{Dpp, Fields, NULL, Operand}; use crate::io::{Barycentric, Elsewhere, Export, Input, Interp, Io, Target}; @@ -4378,6 +4378,14 @@ impl<'a> Lower<'a> { /// The byte stride of the buffer view at heap index `index`, from the /// view-stride table (`t`[`VIEW_STRIDES`]). fn stride_at(&mut self, index: V) -> V { + let word = self.view_word(index); + let bits = self.s.m.i32(VIEW_STRIDE_BITS); + self.s.m.bin(self.cur, Bin::And, word, bits) + } + + /// Dword `index` of the view-stride table: the stride and the format + /// kind bits ([`VIEW_INTEGER`], [`VIEW_SIGNED`]). + fn view_word(&mut self, index: V) -> V { let c = self.cur; let four = self.s.m.i32(4); let at = self.s.m.bin(c, Bin::Mul, index, four); @@ -4385,6 +4393,22 @@ impl<'a> Lower<'a> { self.raw_dword(table, at) } + /// Whether the typed buffer view a handle names has an integer format, + /// and whether a signed one, from the view-stride table; `None` if the + /// handle's heap index is not known. + fn integer_view(&mut self, h: V) -> Option<(V, V)> { + let index = *self.s.view_index.get(&h)?; + let word = self.view_word(index); + let c = self.cur; + let zero = self.s.m.i32(0); + let [int, signed] = [VIEW_INTEGER, VIEW_SIGNED].map(|bit| { + let bit = self.s.m.i32(bit); + let x = self.s.m.bin(c, Bin::And, word, bit); + self.s.m.cmp(c, pred::NE, x, zero) + }); + Some((int, signed)) + } + /// Title memory at a raw GPU address (decision 0005: the unified-memory /// item). Every access to an address the program computes goes through /// here: scalar and global loads, stores and atomics, the BVH node @@ -4834,7 +4858,9 @@ impl<'a> Lower<'a> { &[table, at, iu, mask, four], ); let x = self.s.m.extract(c, r, 0); - Ok(self.as_int(x)) + let word = self.as_int(x); + let bits = self.s.m.i32(VIEW_STRIDE_BITS); + Ok(self.s.m.bin(c, Bin::And, word, bits)) } /// The used-resource entry a descriptor loaded from a table falls in. @@ -8961,9 +8987,19 @@ impl<'a> Lower<'a> { let regs = if d16 { count.div_ceil(2) } else { count }; let mut vals = self.vgprs(inst, data.as_ref().ok_or("buffer store data")?, regs as u8)?; + let typed_h = if typed { + Some(self.handle(site, src, false, (10 | u, 0x409))?) + } else { + None + }; if d16 { - // shortcut: d16 data is taken as f16 (integer formats would - // keep 16 bits), widened for the 32-bit float store. + // d16 data is f16 for float and normalized formats, widened + // for the 32-bit float store; for integer formats (the view's + // kind, from the view-stride table) the 16-bit integer, zero- + // or sign-extended (stored through the float-typed handle as + // loads read: see there). + let integer = typed_h.and_then(|h| self.integer_view(h)); + let c = self.cur; let mut wide = Vec::new(); for k in 0..count { let mut x = vals[(k / 2) as usize]; @@ -8975,7 +9011,16 @@ impl<'a> Lower<'a> { let f = self .s .op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[x]); - wide.push(self.as_int(f)); + let mut w = self.as_int(f); + if let Some((int, signed)) = integer { + let (k16, low) = (self.s.m.i32(16), self.s.m.i32(0xFFFF)); + let zero_ext = self.s.m.bin(c, Bin::And, x, low); + let up = self.s.m.bin(c, Bin::Shl, x, k16); + let sign_ext = self.s.m.bin(c, Bin::AShr, up, k16); + let n = self.s.m.select(c, signed, sign_ext, zero_ext); + w = self.s.m.select(c, int, n, w); + } + wide.push(w); } vals = wide; } @@ -8989,7 +9034,7 @@ impl<'a> Lower<'a> { if count != 4 || *offen || *offset != 0 { return Err("a typed buffer store of some channels or at an offset"); } - let h = self.handle(site, src, false, (10 | u, 0x409))?; + let h = typed_h.ok_or("a typed buffer store")?; let element = index.unwrap_or(self.s.m.i32(0)); args.push(h); args.extend([element, iu]); @@ -9094,6 +9139,7 @@ impl<'a> Lower<'a> { self.write(inst, Reg::V(reg), x, guard); return Ok(()); } + let mut typed_h = None; let r = if typed { if *offen || matches!(off, V::Inst(_)) || *offset != 0 { return Err("a typed buffer load at an offset"); @@ -9102,6 +9148,7 @@ impl<'a> Lower<'a> { self.s.flags |= flags::TYPED_UAV_LOADS; } let h = self.handle(site, src, false, (10 | u, 0x409))?; + typed_h = Some(h); let index = index.unwrap_or(self.s.m.i32(0)); self.s.op( c, @@ -9150,16 +9197,31 @@ impl<'a> Lower<'a> { return Err("a buffer result"); }; if d16 { - // Two f16 channels per VGPR (an odd last high half 0). + // Two 16-bit channels per VGPR (an odd last high half 0): f16 for + // float and normalized formats; for integer formats (the view's + // kind, from the view-stride table) the integer's low 16 bits. + // shortcut: an integer view is read and written through the + // float-typed handle, so its channels' bits travel as the driver + // passes a mismatched component type (unchanged on the RX 6700 + // XT and WARP, `stride_tests`); an integer-typed second handle + // for the slot would not rely on it. + let integer = typed_h.and_then(|h| self.integer_view(h)); + let c = self.cur; let k16 = self.s.m.i32(16); let i32t = self.s.t.i32; for k in 0..count.div_ceil(2) { let mut x = None; for ch in (2 * k..(2 * k + 2).min(count)).rev() { let f = self.s.m.extract(c, r, ch); - let h = self + let mut h = self .s .op(c, "legacyF32ToF16", Attr::ReadNone, i32t, 130, &[f]); + if let Some((int, _)) = integer { + let bits = self.as_int(f); + let low = self.s.m.i32(0xFFFF); + let low = self.s.m.bin(c, Bin::And, bits, low); + h = self.s.m.select(c, int, low, h); + } x = Some(match x { Some(hi) => { let hi = self.s.m.bin(c, Bin::Shl, hi, k16); diff --git a/crates/victory-shader/src/emit/mod.rs b/crates/victory-shader/src/emit/mod.rs index 43397e6..87d43e7 100644 --- a/crates/victory-shader/src/emit/mod.rs +++ b/crates/victory-shader/src/emit/mod.rs @@ -220,7 +220,8 @@ use crate::{ShaderError, io, ir, resource, structure}; pub use sampler::StaticSampler; pub use shader::{ HEAP_SAMPLERS, HEAP_VIEWS, MEMORY_SPACE, NULL_BUFFER_VIEW, ROOT_CBV_SPACE, SAMPLER_SPACE, - USER_DATA_SPACE, VIEW_MARK_NONE, VIEW_MARKS, VIEW_STRIDES, VIEW_WRITE_BITS, VIEW_WRITES, + USER_DATA_SPACE, VIEW_INTEGER, VIEW_MARK_NONE, VIEW_MARKS, VIEW_SIGNED, VIEW_STRIDE_BITS, VIEW_STRIDES, + VIEW_WRITE_BITS, VIEW_WRITES, }; /// The loop limit without an override: 2^20. diff --git a/crates/victory-shader/src/emit/shader.rs b/crates/victory-shader/src/emit/shader.rs index e68e262..1e8cf3c 100644 --- a/crates/victory-shader/src/emit/shader.rs +++ b/crates/victory-shader/src/emit/shader.rs @@ -14,8 +14,16 @@ pub const SAMPLER_SPACE: u32 = 0; pub const ROOT_CBV_SPACE: u32 = 1; /// The root SRV slot (`t255` in [`ROOT_CBV_SPACE`], past every title root /// parameter) of the view-stride table: dword `i` is the byte stride of -/// the buffer view at view-heap index `i`. +/// the buffer view at view-heap index `i` ([`VIEW_STRIDE_BITS`]), with +/// [`VIEW_INTEGER`] and [`VIEW_SIGNED`] for typed views of integer formats. pub const VIEW_STRIDES: u8 = 255; +/// The bits of a view-stride table dword holding the stride. +pub const VIEW_STRIDE_BITS: u32 = (1 << 30) - 1; +/// In a view-stride table dword: a typed buffer view of an integer format +/// (`UINT` or `SINT`), whose 16-bit typed accesses (`_d16`) pass bits. +pub const VIEW_INTEGER: u32 = 1 << 31; +/// With [`VIEW_INTEGER`]: a signed (`SINT`) format. +pub const VIEW_SIGNED: u32 = 1 << 30; /// Where the title-memory page table is bound: `t0` (a raw root SRV) in /// this space (see `emit` "Title memory"). pub const MEMORY_SPACE: u32 = 2; @@ -533,8 +541,10 @@ impl Shader { &[index, s, n], ); let h = self.annotate(block, h, props.0, props.1); - // Unordered typed, raw and structured buffers (kinds 10-12). - if !sampler && props.0 & 0x1000 != 0 && (10..=12).contains(&(props.0 & 0xFF)) { + // Unordered typed, raw and structured buffers (kinds 10-12), and + // typed buffers read (their format kind, `Lower::view_word`). + let kind = props.0 & 0xFF; + if !sampler && (kind == 10 || props.0 & 0x1000 != 0 && (11..=12).contains(&kind)) { self.view_index.insert(h, index); } h diff --git a/crates/victory-shader/src/emit/stride_tests.rs b/crates/victory-shader/src/emit/stride_tests.rs index fc160f5..20615b2 100644 --- a/crates/victory-shader/src/emit/stride_tests.rs +++ b/crates/victory-shader/src/emit/stride_tests.rs @@ -7,8 +7,13 @@ use windows::Win32::Graphics::Direct3D12::D3D12_SAMPLER_DESC; +use windows::Win32::Graphics::Dxgi::Common::{ + DXGI_FORMAT, DXGI_FORMAT_R16_FLOAT, DXGI_FORMAT_R16_UINT, DXGI_FORMAT_R16G16B16A16_FLOAT, + DXGI_FORMAT_R16G16B16A16_UINT, +}; + use super::host::{Dispatch, Host, Root, Stride, View}; -use super::{HostShader, StaticSampler, VIEW_STRIDES, lower, validator}; +use super::{HostShader, StaticSampler, VIEW_INTEGER, VIEW_STRIDES, lower, validator}; use crate::native::{ ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource, }; @@ -573,4 +578,169 @@ fn a_sampler_chosen_by_a_uniform_branch_samples_as_chosen() { ran += 1; } assert!(ran > 0, "neither hardware nor WARP SM 6.6 device available"); -} \ No newline at end of file +} +/// User data: s0 a table with a typed buffer UAV, s1 a table with a raw +/// buffer of 2 dwords a lane, s2 a table with a raw UAV. Each lane stores +/// its 4 halves to element `lane` of the typed view (`_d16`), loads them +/// back the same way and stores what it read to dwords `2 lane, 2 lane + 1` +/// of the raw UAV. +const D16_ROUND_TRIP_WAVE32: &[u32] = &[ + 0xBE84_0300, // s_mov_b32 s4, s0 + 0xBE85_0380, // s_mov_b32 s5, 0 + 0xF408_0202, + 0xFA00_0000, // s_load_dwordx4 s[8:11], s[4:5], 0x0 + 0xBE86_0301, // s_mov_b32 s6, s1 + 0xBE87_0380, // s_mov_b32 s7, 0 + 0xF408_0303, + 0xFA00_0000, // s_load_dwordx4 s[12:15], s[6:7], 0x0 + 0xBE94_0302, // s_mov_b32 s20, s2 + 0xBE95_0380, // s_mov_b32 s21, 0 + 0xF408_040A, + 0xFA00_0000, // s_load_dwordx4 s[16:19], s[20:21], 0x0 + 0x3406_0083, // v_lshlrev_b32 v3, 3, v0 + 0xBF8C_C07F, // s_waitcnt lgkmcnt(0) + 0xE034_1000, + 0x8003_0103, // buffer_load_dwordx2 v[1:2], v3, s[12:15], 0 offen + 0xBF8C_3F70, // s_waitcnt vmcnt(0) + 0xE21C_2000, + 0x8002_0100, // buffer_store_format_d16_xyzw v[1:2], v0, s[8:11], 0 idxen + 0xE20C_2000, + 0x8002_0400, // buffer_load_format_d16_xyzw v[4:5], v0, s[8:11], 0 idxen + 0xBF8C_3F70, // s_waitcnt vmcnt(0) + 0xE074_1000, + 0x8004_0403, // buffer_store_dwordx2 v[4:5], v3, s[16:19], 0 offen + 0xBF81_0000, // s_endpgm +]; + +/// User data: s0 a table with a typed buffer SRV, s1 a table with a raw UAV. +/// Each lane loads element `lane` (`_d16_x`) and stores the dword it got to +/// dword `lane` of the raw UAV. +const D16_X_LOAD_WAVE32: &[u32] = &[ + 0xBE84_0300, // s_mov_b32 s4, s0 + 0xBE85_0380, // s_mov_b32 s5, 0 + 0xF408_0202, + 0xFA00_0000, // s_load_dwordx4 s[8:11], s[4:5], 0x0 + 0xBE86_0301, // s_mov_b32 s6, s1 + 0xBE87_0380, // s_mov_b32 s7, 0 + 0xF408_0403, + 0xFA00_0000, // s_load_dwordx4 s[16:19], s[6:7], 0x0 + 0x3406_0082, // v_lshlrev_b32 v3, 2, v0 + 0x7E08_0280, // v_mov_b32 v4, 0 + 0xBF8C_C07F, // s_waitcnt lgkmcnt(0) + 0xE200_2000, + 0x8002_0400, // buffer_load_format_d16_x v4, v0, s[8:11], 0 idxen + 0xBF8C_3F70, // s_waitcnt vmcnt(0) + 0xE070_1000, + 0x8004_0403, // buffer_store_dword v4, v3, s[16:19], 0 offen + 0xBF81_0000, // s_endpgm +]; + +/// Runs `shader` (user data: tables at heap indices 0, 1, 2 in order) on +/// hardware and WARP with `views` and the view-stride table `strides`; +/// each adapter's UAV contents. +fn run_tables(shader: &HostShader, views: &[View], strides: Vec) -> Vec<(&'static str, Vec>)> { + assert_eq!(shader.root_srvs, [VIEW_STRIDES]); + let _one = super::valu_tests::ONE_AT_A_TIME + .lock() + .unwrap_or_else(|e| e.into_inner()); + let mut out = Vec::new(); + for (label, warp) in [("hardware", false), ("warp", true)] { + let Ok(host) = Host::new(warp) else { + eprintln!("no SM 6.6 {label} device; skipped"); + continue; + }; + let (root, pso) = host + .compute_pipeline(shader) + .unwrap_or_else(|e| panic!("{label}: compute_pipeline failed: {e}")); + let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize]; + for u in &shader.user_data { + // Table k is the view at heap index k (32 bytes a view). + ud[u.index as usize] = 32 * u32::from(u.slot); + } + let words = host + .dispatch( + &root, + &pso, + &Dispatch { + roots: vec![(0, Root::Constants(ud)), (1, Root::View { srv: true, data: strides.clone() })], + views, + samplers: &[], + groups: [1, 1, 1], + }, + ) + .unwrap_or_else(|e| panic!("{label}: dispatch failed: {e}")); + out.push((label, words)); + } + assert!(!out.is_empty(), "neither hardware nor WARP SM 6.6 device available"); + out +} + +/// 16-bit typed stores and loads (`_d16`) through views of integer formats +/// pass the bits (the console's buffer format decides the conversion; d16 +/// only packs 16-bit results), and through float views convert as halves: +/// Grand Theft Auto VI's face network keeps its halves in UINT views. +#[test] +fn d16_typed_buffer_accesses_pass_bits_through_integer_views() { + let shader = build( + D16_ROUND_TRIP_WAVE32, + vec![ + param(RootType::Table, 0), + param(RootType::Table, 1), + param(RootType::Table, 2), + ], + vec![ + entry(ResourceType::Uav, 4, 0), + entry(ResourceType::Srv, 4, 1), + entry(ResourceType::Uav, 4, 2), + ], + 32, + ); + // Lane l: negative zero and small, signalling NaN, denormal and + // all-ones patterns as bits; normal halves for the float view. + let bits: Vec = (0..32u32) + .flat_map(|l| [(0x8000 | l) | (0x7C01 + l) << 16, (1 + l) | (0xFFFF - l) << 16]) + .collect(); + let halves: Vec = (0..32u32) + .flat_map(|l| [(0x3C00 + l) | (0xC000 + 3 * l) << 16, (0x1400 + l) | (0x5BFF - l) << 16]) + .collect(); + let cases: [(DXGI_FORMAT, u32, &[u32]); 2] = [ + (DXGI_FORMAT_R16G16B16A16_UINT, 8 | VIEW_INTEGER, &bits), + (DXGI_FORMAT_R16G16B16A16_FLOAT, 8, &halves), + ]; + for (format, stride, data) in cases { + let views = [ + View::TypedUav(format, 32, 8), + View::Buffer { data, stride: Stride::Raw }, + View::RawUav(64), + ]; + for (label, words) in run_tables(&shader, &views, vec![stride, 4, 4]) { + assert_eq!(words[0], data, "{label} {format:?}: the typed view's bytes"); + assert_eq!(words[1], data, "{label} {format:?}: what the loads read"); + } + } +} + +/// A one-channel `_d16_x` load from a typed SRV: an integer view's 16 bits +/// as they are, a float view's half as it is. +#[test] +fn d16_x_loads_read_r16_uint_and_r16_float_views() { + let shader = build( + D16_X_LOAD_WAVE32, + vec![param(RootType::Table, 0), param(RootType::Table, 1)], + vec![entry(ResourceType::Srv, 4, 0), entry(ResourceType::Uav, 4, 1)], + 32, + ); + let pack = |h: &dyn Fn(u32) -> u32| -> Vec { (0..16u32).map(|k| h(2 * k) | h(2 * k + 1) << 16).collect() }; + let uint = |l: u32| 0xF000 | l; + let half = |l: u32| 0x3C00 + 5 * l; + let cases: [(DXGI_FORMAT, u32, Vec, Vec); 2] = [ + (DXGI_FORMAT_R16_UINT, 2 | VIEW_INTEGER, pack(&uint), (0..32).map(uint).collect()), + (DXGI_FORMAT_R16_FLOAT, 2, pack(&half), (0..32).map(half).collect()), + ]; + for (format, stride, data, want) in cases { + let views = [View::Buffer { data: &data, stride: Stride::Format(format, 2) }, View::RawUav(32)]; + for (label, words) in run_tables(&shader, &views, vec![stride, 4]) { + assert_eq!(words[0], want, "{label} {format:?}"); + } + } +}