From e51c5ca01fdbf364ca6d7eb34ba7cf205acb40c3 Mon Sep 17 00:00:00 2001 From: Nick Stallman Date: Fri, 27 Nov 2026 13:06:21 +1100 Subject: [PATCH] xshader: keep a compute root's layout when the runtime appends its private root SRVs The runtime appends the heap index map and the stride table to a root as root SRVs in space 32766. Two places matched a compute root by its exact parameter count: the root-constant layouts (`table, constants` and `table, table, constants`) and the registers that hold the dispatch IDs. With an SRV appended the constants were no longer declared and their registers stayed zero. That already affects programs translated with the stride table: 127 of the recorded ProjectScarlett requests gain their root constants back, and nothing else in 25,397 requests changes. Co-Authored-By: Claude Fable 5.2 --- .../src/hlsl/emitter/output.rs | 5 +- .../shader-compiler/src/hlsl/sampler_heap.rs | 16 +++- .../src/tests/hlsl/sampler_heap.rs | 77 +++++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/tools/shader-compiler/src/hlsl/emitter/output.rs b/tools/shader-compiler/src/hlsl/emitter/output.rs index b18d6e80..e1a6c998 100644 --- a/tools/shader-compiler/src/hlsl/emitter/output.rs +++ b/tools/shader-compiler/src/hlsl/emitter/output.rs @@ -364,14 +364,15 @@ uint tb_fmac_half(uint a, uint b, uint c) { if self.paired { for r in ["v0", "v1", "v2"] { self.regs.insert(r.to_string()); } } - let nparams = ex.root_sig.as_ref().map(|r| r.params.len()).unwrap_or(4); + let guest_params = ex.root_sig.as_ref().map(sampler_heap::guest_params); + let nparams = guest_params.map_or(4, <[_]>::len); let g = match self.packed.as_ref().map(|p| &p.abi) { None if self.resolve_wave32 || lone_lane::on() && ray_resolve::applies(ex) => ray_resolve::group_x_register(ex) .ok_or("unverified resolve compute user-data layout")?, Some(RootAbi::RegisterWave32 | RootAbi::RegisterWave64) => 6 + 2 * (nparams - 2), // two tables followed by root CBV pairs Some(RootAbi::ArgumentBlock) => 4, Some(RootAbi::Ps09Raster) => 11, - None if ex.root_sig.as_ref().is_some_and(|r| r.flags & 0x400 != 0 && r.params.len() == 2 && r.params.iter().all(|p| matches!(p.kind, crate::meta::RootParamKind::Table(_)))) + None if ex.root_sig.as_ref().is_some_and(|r| r.flags & 0x400 != 0) && guest_params.is_some_and(|params| params.len() == 2 && params.iter().all(|p| matches!(p.kind, crate::meta::RootParamKind::Table(_)))) && tuxbox_dxbc::compute_table::two_tables_and_heap(&ex.fssm) => 7, None if sampler_heap::user_data_end(ex).is_some() => sampler_heap::user_data_end(ex).unwrap(), diff --git a/tools/shader-compiler/src/hlsl/sampler_heap.rs b/tools/shader-compiler/src/hlsl/sampler_heap.rs index d22b0051..a2771c10 100644 --- a/tools/shader-compiler/src/hlsl/sampler_heap.rs +++ b/tools/shader-compiler/src/hlsl/sampler_heap.rs @@ -9,10 +9,24 @@ pub(super) struct DirectSampler { pub component: u32, } +/// The title's own root parameters. The runtime appends its private root SRVs +/// (space 32766: the heap index map and the stride table) after them. The +/// console program was compiled without those, so they take no launch register +/// and are no part of the layout its registers follow. +pub(super) fn guest_params(root: &crate::meta::RootSig) -> &[crate::meta::RootParam] { + let private = root.params.iter().rev() + .take_while(|p| matches!(p.kind, RootParamKind::Descriptor { kind: 3, space: 32766, .. })) + .count(); + &root.params[..root.params.len() - private] +} + pub(super) fn constants(ex: &ShaderExtract) -> Option<(u8, DirectSampler)> { use tuxbox_dxbc::compute_table::records; let root = ex.root_sig.as_ref()?; - let (index, start) = match (ex.stage, root.params.as_slice()) { + // The pixel form names the index map itself; compute roots are matched as + // the title declared them. + let params = if ex.stage == Stage::Compute { guest_params(root) } else { root.params.as_slice() }; + let (index, start) = match (ex.stage, params) { (Stage::Pixel, [table, _, rest @ ..]) if matches!(table.kind, RootParamKind::Table(_)) && (rest.is_empty() diff --git a/tools/shader-compiler/src/tests/hlsl/sampler_heap.rs b/tools/shader-compiler/src/tests/hlsl/sampler_heap.rs index ec6653a0..ffbd7836 100644 --- a/tools/shader-compiler/src/tests/hlsl/sampler_heap.rs +++ b/tools/shader-compiler/src/tests/hlsl/sampler_heap.rs @@ -445,3 +445,80 @@ fn cube_sample_with_derivatives_and_a_lod_clamp_lowers() { let expected = format!("{}, {}, asfloat(v11));", along(4, 5), along(6, 7)); assert!(call.ends_with(&expected), "{call}"); } + +/// The Lanes `bindless-table` program under its root: one table with two raw +/// UAVs (u0 and u1, space1), one root constant (b0) and the direct-indexing +/// flag, followed by `private` root SRVs of the runtime (registers in space 32766). +fn table_constant_and_heap(private: &[u32]) -> ShaderExtract { + use crate::meta::{RootParam, RootRange}; + let words = [ + 0xbe800304u32, 0xbe810380, // s[0:1] = table 0 + 0xf4080200, 0xfa000020, // s[8:11] = table[s0:1] + 0x20 + 0x8f028505, // s2 = s5 << 5, the root constant + 0xf4080303, 0x04000000, // s[12:15] = heap[s6:7][s2] + 0x7e000281, // v0 = 1 + 0xe0700000, 0x80020000, // table UAV store + 0x7e0202ff, 0x7ab1e232, // v1 + 0xe0700000, 0x80030100, // heap UAV store + 0xbf810000, + ]; + let isa: Vec = words.iter().flat_map(|w| w.to_le_bytes()).collect(); + let mut ex = super::super::mesh::heap_extract(&[], 0x400, &isa); + ex.stage = Stage::Compute; + ex.shader_model = (6, 6); + ex.psv_threads = Some([64, 1, 1]); + ex.fssm = fssm( + &[[3, 8, 1, 1, 0x4], [0, 0, 1, 0, 0x105], [5, 0, 1, 0, 6]], + &[[0x303, 8, 1, 1, 0x404], [2, 0, 0, 0, 0x1_0504]], + ); + ex.resources = vec![ + PsvResource { res_type: 7, space: 1, lower: 1, upper: 1 }, + PsvResource { res_type: 2, space: 0, lower: 0, upper: 0 }, + ]; + let root = ex.root_sig.as_mut().unwrap(); + root.version = 2; + root.params = vec![ + RootParam { visibility: 0, kind: RootParamKind::Table(vec![ + RootRange { range_type: 1, num: 2, base_register: 0, space: 1, offset_in_table: u32::MAX }]) }, + RootParam { visibility: 0, kind: RootParamKind::Constants { register: 0, space: 0, num32: 1 } }, + ]; + root.params.extend(private.iter().map(|®ister| RootParam { + visibility: 0, kind: RootParamKind::Descriptor { kind: 3, register, space: 32766 }, + })); + ex +} + +#[test] +fn the_runtimes_private_root_srvs_leave_a_compute_roots_layout_alone() { + let guest = super::super::translate(&table_constant_and_heap(&[])).unwrap().hlsl; + for line in [ + "cbuffer TBIndex0_0 : register(b0, space0) { uint tb_index0_0; };", + "s5 = tb_index0_0.x;", + "s12 = tb_heap_index(s2 >> 5);", + "uint tb_heap_index(uint i) { return i; }", + "RWByteAddressBuffer raw_ubuf1 : register(u1, space1);", + ] { + assert!(guest.contains(line), "{line}\n{guest}"); + } + // With the index map appended, the root constant and every register stay + // where the title's root put them. Only the heap index goes through the map. + let mapped = super::super::translate(&table_constant_and_heap(&[0])).unwrap().hlsl; + let expected = guest.replace( + "uint tb_heap_index(uint i) { return i; }", + "uint tb_heap_index(uint i) { return tb_heap_map.Load(0) == 0u ? i : tb_heap_map.Load(4u+i*4u); }", + ); + let declared: String = mapped.lines() + .filter(|line| *line != "ByteAddressBuffer tb_heap_map : register(t0, space32766);") + .flat_map(|line| [line, "\n"]).collect(); + assert_ne!(declared, mapped, "{mapped}"); + assert_eq!(declared, expected); + // The stride table alone, or after the map, changes how the heap descriptor + // is loaded, not where the constant is. A root SRV of the title's own is a + // parameter like any other: it does change the root's shape. + for private in [&[1][..], &[0, 1]] { + let strided = super::super::translate(&table_constant_and_heap(private)).unwrap().hlsl; + for line in ["cbuffer TBIndex0_0 : register(b0, space0) { uint tb_index0_0; };", "s5 = tb_index0_0.x;", "tb_heap_strides.Load"] { + assert!(strided.contains(line), "{private:?}: {line}\n{strided}"); + } + } +}