From a3e820d8e86fcbd8322fee8e519f4b6593b076ce Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 25 Nov 2026 18:15:05 -0700 Subject: [PATCH] D3D12.X: a DispatchX with thread dimensions launches whole groups DispatchX(x, y, z, flags) dropped its flags. With USE_THREAD_DIMENSIONS (0x20; 0x8020 with the wave32 bit) x, y, z are thread counts: the console launches ceil(count / numthreads) groups and leaves the lanes past the count out of EXEC. XCL launched the counts as groups: 32-64x the groups for the retail title's BVH builder (its rehydration and reload passes pass node and primitive counts), past D3D12's 65,535 groups for a large mesh, and every extra thread of the unguarded Box32/Box16 rehydration shaders wrote outside its node array. The flags now reach the dispatch (also one put off to the submit). With 0x20 the counts become whole groups of the pipeline's console numthreads (kept with its user SGPRs at creation); a pipeline whose group shape is not known is not launched, skipped and counted as an untranslated one. The thread count of an axis that does not end on a whole group goes to io slots 114..116 (IO_THREAD_LIMIT), where the compute prologue will leave the lanes past it out of EXEC in a following change: until then the last group still runs whole. Each kind of flags is logged once. Part of a stack: EXEC masking of the last group follows. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/recomp/mod.rs | 5 ++ crates/xcl/src/gpu/recomp/pso.rs | 10 +++- crates/xcl/src/hle/d3d12x/bind.rs | 81 ++++++++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/list_x.rs | 9 ++-- crates/xcl/src/hle/d3d12x/real.rs | 26 ++++++--- 5 files changed, 116 insertions(+), 15 deletions(-) diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs index 528493d6e5..bef0c6572d 100644 --- a/crates/xcl/src/gpu/recomp/mod.rs +++ b/crates/xcl/src/gpu/recomp/mod.rs @@ -204,6 +204,11 @@ pub const GS_PRIMS_PER_GROUP: u32 = 21; pub const IO_GS_PRIMS: usize = 110; pub const IO_GS_VERTICES: usize = 111; pub const IO_UMA_BASE_HI: usize = 112; +/// io slots of a `DispatchX` with thread dimensions (flag 0x20): the thread count of each axis +/// when it is not a whole number of groups (0: the last group is whole), so the program can leave +/// the lanes past it out of EXEC. Written by D3D12.X now; read by the compute prologue later. +/// (113 is left for the group-limit slot of #1428.) +pub const IO_THREAD_LIMIT: [usize; 3] = [114, 115, 116]; impl GsInterface { /// bytes of one ES vertex in LDS (ESGS item: 16 per ES output register) diff --git a/crates/xcl/src/gpu/recomp/pso.rs b/crates/xcl/src/gpu/recomp/pso.rs index da4b25e5fb..317ccff879 100644 --- a/crates/xcl/src/gpu/recomp/pso.rs +++ b/crates/xcl/src/gpu/recomp/pso.rs @@ -920,13 +920,19 @@ fn cache_allowed(stage: Stage, present: impl Fn(&str) -> bool) -> bool { /// s0…s3 are the driver's, so what is above 4 is the root arguments its compiler passed in /// registers. None: not a console compute shader. pub fn compute_user_sgprs(blob: &[u8]) -> Option { + compute_interface(blob).map(|cs| cs.user_sgprs) +} + +/// The dispatch ABI of a console compute program: its user SGPRs and the console's group shape +/// (`numthreads`). None: not a console compute program. +pub fn compute_interface(blob: &[u8]) -> Option { if let Some(f) = fspx(blob) { - return Some(super::CsInterface::from_rsrc2(f.threads, f.rsrc2).user_sgprs); + return Some(super::CsInterface::from_rsrc2(f.threads, f.rsrc2)); } let sh = xbox::parse(blob)?; let f = sh.fssm.iter().find(|f| f.stages.first().is_some_and(|s| s.hw == 6))?; match interface(blob, &sh, f)? { - Mode::Compute(cs) => Some(cs.user_sgprs), + Mode::Compute(cs) => Some(cs), _ => None, } } diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index 3f31c48b43..9bb3e0302b 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -34,6 +34,9 @@ struct State { uma: Vec, /// where the next draw's io and argument table go (blocks in `uma`) arena: crate::gpu::uma::Arena, + /// the next dispatch's thread limits (`IO_THREAD_LIMIT`; a `DispatchX` with thread + /// dimensions), taken by its `bind_abi` + limits: [u32; 3], } static STATES: Mutex>> = Mutex::new(None); @@ -870,8 +873,30 @@ pub(super) unsafe extern "system" fn set_pipeline_state(this: *mut c_void, pso: /// #14 Dispatch: with a translated pipeline, on XCL's ABI with the kept root arguments pub(super) unsafe extern "system" fn dispatch(this: *mut c_void, x: u32, y: u32, z: u32) { + dispatch_flags(this, x, y, z, 0); +} + +/// `DispatchX` flag: x, y, z are thread counts, not group counts; the console launches whole +/// groups and leaves the lanes past the count out of EXEC (0x8000, wave32, needs nothing here) +const DISPATCH_THREAD_DIMENSIONS: u32 = 0x20; + +/// What a dispatch of `dims` with `flags` launches on pipeline `pso`: its groups and the thread +/// limits for `IO_THREAD_LIMIT` (0: that axis ends on a whole group). None: it cannot be run +/// as the console would (thread dimensions with a group shape not known). +fn dispatch_groups(pso: usize, dims: [u32; 3], flags: u32) -> Option<([u32; 3], [u32; 3])> { + if flags & DISPATCH_THREAD_DIMENSIONS == 0 { + return Some((dims, [0; 3])); + } + let threads = super::real::pso_threads(pso)?.map(|t| t.max(1)); + let groups = std::array::from_fn(|k| dims[k].div_ceil(threads[k])); + let limits = std::array::from_fn(|k| if dims[k] % threads[k] != 0 { dims[k] } else { 0 }); + Some((groups, limits)) +} + +/// `Dispatch` / `DispatchX(x, y, z, flags)`. +pub(super) unsafe fn dispatch_flags(this: *mut c_void, x: u32, y: u32, z: u32, flags: u32) { let pso = with(this, |s| s.pso); - if super::pso_async::is_pending(pso) && super::list_x::defer(this, super::list_x::Deferred::Dispatch(x, y, z)) { + if super::pso_async::is_pending(pso) && super::list_x::defer(this, super::list_x::Deferred::Dispatch(x, y, z, flags)) { return super::pso_async::need_by_submit(pso); } let pso = super::pso_async::resolve_wait(pso); @@ -886,7 +911,26 @@ pub(super) unsafe extern "system" fn dispatch(this: *mut c_void, x: u32, y: u32, count(DISPATCHES_SKIPPED); return; } - match translated_dispatch(this, list, pso, x, y, z) { + let Some((groups, limits)) = dispatch_groups(pso, [x, y, z], flags) else { + static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + if N.fetch_add(1, std::sync::atomic::Ordering::Relaxed) < 4 { + crate::xlog_warn!("D3D12.X: DispatchX of {x}x{y}x{z} threads (flags {flags:#x}) with a pipeline whose group shape is not known ({pso:#x}) skipped"); + } + count(DISPATCHES_SKIPPED); + return; + }; + if flags != 0 { + // each kind of flags once: what titles pass, and what it launches + static SEEN: Mutex> = Mutex::new(Vec::new()); + let first = { let mut g = SEEN.lock().unwrap(); !g.contains(&flags) && { g.push(flags); true } }; + if first { + crate::xlog_info!("D3D12.X: DispatchX with flags {flags:#x}: {x}x{y}x{z} launched as {}x{}x{} groups (thread limits {limits:?})", groups[0], groups[1], groups[2]); + } + } + with(this, |s| s.limits = limits); + let done = translated_dispatch(this, list, pso, groups[0], groups[1], groups[2]); + with(this, |s| s.limits = [0; 3]); + match done { Ok(()) => count(DISPATCHES), Err(e) => { count(DISPATCHES_SKIPPED); @@ -1039,6 +1083,11 @@ unsafe fn bind_abi(this: *mut c_void, list: &ID3D12GraphicsCommandList, pso: usi }; io.resize(crate::gpu::recomp::KERNEL_SGPRS, 0); io[crate::gpu::recomp::IO_UMA_BASE_HI] = (u.base() >> 32) as u32; + if !gfx { + for (k, l) in with(this, |s| std::mem::take(&mut s.limits)).into_iter().enumerate() { + io[crate::gpu::recomp::IO_THREAD_LIMIT[k]] = l; + } + } let io_at = copy(&io.iter().flat_map(|w| w.to_le_bytes()).collect::>())?; let res_at = copy(&[0u8; 256])?; let va = |p: usize| super::real::uma_va_of(p).unwrap_or(first); @@ -1403,6 +1452,34 @@ void main(uint t : SV_GroupIndex) { (*(this as *const FakeAdoptedList)).execute_indirect_calls.fetch_add(1, Ordering::Relaxed); } + /// `DispatchX` with thread dimensions (flag 0x20, as the BVH builder's rehydration passes + /// use it): the console launches whole groups of the pipeline's `numthreads` and leaves + /// the lanes past the count out; without the flag the counts are groups. With the flag + /// and a group shape not known, nothing is launched (the counts as groups was the bug). + #[test] + fn dispatch_x_with_thread_dimensions_launches_whole_groups() { + let pso = 0x7300_0001usize; + real::register_threads_for_test(pso, [64, 1, 1]); + // 640 threads in groups of 64: 10 groups, the last one whole + assert_eq!(dispatch_groups(pso, [640, 1, 1], 0x20), Some(([10, 1, 1], [0, 0, 0]))); + // 100 threads: 2 groups, and the lanes past 100 to leave out + assert_eq!(dispatch_groups(pso, [100, 1, 1], 0x20), Some(([2, 1, 1], [100, 0, 0]))); + // the wave32 bit (0x8000) changes nothing here + assert_eq!(dispatch_groups(pso, [100, 1, 1], 0x8020), Some(([2, 1, 1], [100, 0, 0]))); + // a 2-D group shape + let pso2 = 0x7300_0002usize; + real::register_threads_for_test(pso2, [8, 8, 1]); + assert_eq!(dispatch_groups(pso2, [20, 16, 3], 0x20), Some(([3, 2, 3], [20, 0, 0]))); + // group counts (Dispatch, DispatchX without the flag) are launched as they are + assert_eq!(dispatch_groups(pso, [7, 2, 1], 0), Some(([7, 2, 1], [0, 0, 0]))); + assert_eq!(dispatch_groups(pso, [7, 2, 1], 0x8000), Some(([7, 2, 1], [0, 0, 0]))); + // no threads: no groups + assert_eq!(dispatch_groups(pso, [0, 1, 1], 0x20), Some(([0, 1, 1], [0, 0, 0]))); + // thread dimensions on a pipeline whose group shape is not known: not launched + assert_eq!(dispatch_groups(0x7300_00FF, [100, 1, 1], 0x20), None); + assert_eq!(dispatch_groups(0x7300_00FF, [100, 1, 1], 0), Some(([100, 1, 1], [0, 0, 0]))); + } + #[test] fn async_compute_pso_success_and_failure_in_dispatch_and_execute_indirect() { let mut orig_vt = [0usize; 64]; diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index ee544dec22..6bb30cd8fa 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -657,7 +657,8 @@ struct ListState { /// A `Dispatch` or a dispatching `ExecuteIndirect` put off to the submit, with its arguments. pub(super) enum Deferred { - Dispatch(u32, u32, u32), + /// x, y, z and `DispatchX`'s flags (0 for `Dispatch`) + Dispatch(u32, u32, u32, u32), Indirect(usize, u32, usize, u64, usize, u64), } @@ -2707,7 +2708,7 @@ pub(super) unsafe fn record_deferred(wrapper: *mut c_void) { real::dispatch_chain::list_follows(last as usize, seg as usize); } match what { - Deferred::Dispatch(x, y, z) => super::bind::dispatch(seg, x, y, z), + Deferred::Dispatch(x, y, z, flags) => super::bind::dispatch_flags(seg, x, y, z, flags), Deferred::Indirect(sig, max, args, args_off, count_buf, count_off) => super::bind::execute_indirect(seg, sig, max, args, args_off, count_buf, count_off), } let ext = *(seg as *const usize); @@ -3064,8 +3065,8 @@ pub(super) unsafe extern "system" fn set_depth_bounds_x(this: *mut c_void, min: } /// command list #78 DispatchX(x, y, z, flags) -pub(super) unsafe extern "system" fn dispatch_x(this: *mut c_void, x: u32, y: u32, z: u32, _flags: u32) { - super::bind::dispatch(this, x, y, z); +pub(super) unsafe extern "system" fn dispatch_x(this: *mut c_void, x: u32, y: u32, z: u32, flags: u32) { + super::bind::dispatch_flags(this, x, y, z, flags); } /// command list #146 console compute/UAV barrier (used by `XboxBvhBuilder.dll` between BVH diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 91085d3933..ceecffee52 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3882,14 +3882,26 @@ fn note_pso_rs(pso: *mut c_void, rs: *mut c_void) { } } -/// user SGPRs of the console CS of a translated compute pipeline -static PSO_USER_SGPRS: Mutex>> = Mutex::new(None); +/// user SGPRs and the console's `numthreads` of the console CS of a translated compute pipeline +static PSO_USER_SGPRS: Mutex>> = Mutex::new(None); /// How many user SGPRs the console compute shader of a translated pipeline has (None: not /// known). What is above the driver's four holds root arguments (`console_list::inline_for`). pub fn pso_user_sgprs(pso: usize) -> Option { let pso = super::pso_async::resolve(pso); - PSO_USER_SGPRS.lock().unwrap().as_ref()?.get(&pso).copied() + PSO_USER_SGPRS.lock().unwrap().as_ref()?.get(&pso).map(|e| e.0) +} + +/// The console's group shape (`numthreads`) of a translated compute pipeline (None: not known). +pub fn pso_threads(pso: usize) -> Option<[u32; 3]> { + let pso = super::pso_async::resolve(pso); + PSO_USER_SGPRS.lock().unwrap().as_ref()?.get(&pso).map(|e| e.1) +} + +/// (tests) A translated compute pipeline with this console group shape. +#[cfg(test)] +pub(crate) fn register_threads_for_test(pso: usize, threads: [u32; 3]) { + PSO_USER_SGPRS.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso, (0, threads)); } /// The root signature a translated pipeline state was created with. @@ -4347,8 +4359,8 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const LAST_COMPUTE_RS.with(|c| c.set(rs)); note_pso_rs(*ppv, rs as *mut c_void); super::dred::label(*ppv as usize, &[("cs", Some(cs))]); - if let Some(n) = crate::gpu::recomp::pso::compute_user_sgprs(cs) { - PSO_USER_SGPRS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, n); + if let Some(i) = crate::gpu::recomp::pso::compute_interface(cs) { + PSO_USER_SGPRS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, (i.user_sgprs, i.threads)); } note_translated_compute(0, ppv); TRANSLATED_PSO.fetch_add(1, Ordering::Relaxed); @@ -4379,8 +4391,8 @@ unsafe fn translated_compute_pso_once(this: *mut c_void, f: PsoFn, desc: *const TRANSLATED_PSO.fetch_add(1, Ordering::Relaxed); note_pso_rs(*ppv, rs); super::dred::label(*ppv as usize, &[("cs", Some(cs))]); - if let Some(n) = crate::gpu::recomp::pso::compute_user_sgprs(cs) { - PSO_USER_SGPRS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, n); + if let Some(i) = crate::gpu::recomp::pso::compute_interface(cs) { + PSO_USER_SGPRS.lock().unwrap().get_or_insert_with(HashMap::new).insert(*ppv as usize, (i.user_sgprs, i.threads)); } note_translated_compute(hr, ppv); crate::xlog_info!(