From 52436c9a3e0b355eadd9d0e596f4662bbc38f3cf Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 13:10:52 -0800 Subject: [PATCH] D3D12.X: keep selected pipeline metadata exact Several command paths selected and pinned one native pipeline, then passed its pointer through metadata helpers that resolved it again as a title-visible address. Address reuse could redirect thread shape, ABI, remap, root-signature, or two-pass decisions to another build. Add explicit exact metadata accessors and use them after pipeline selection in dispatch, draw, indirect, dispatch-chain, and console-list paths. Legacy title-address helpers retain their existing one-resolution behavior. Windows tests were cross-compiled but not executed on this Linux host. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/bind.rs | 56 +++++++++++-------- crates/xcl/src/hle/d3d12x/console_list.rs | 4 +- crates/xcl/src/hle/d3d12x/real.rs | 34 +++++++++++ .../xcl/src/hle/d3d12x/real/dispatch_chain.rs | 4 +- 4 files changed, 72 insertions(+), 26 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index 87e92cb4da..d3819af693 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -590,7 +590,7 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us // TWO_PASS rather than TRANSLATED_GRAPHICS. Falling through to raw PC ExecuteIndirect // with the title's graphics root signature (e.g. a non-DispatchMesh indirect draw or a // root-changing signature) invalidates the command list (Close → E_INVALIDARG). - if let Some(tp) = super::real::two_pass_of(pso) { + if let Some(tp) = super::real::two_pass_of_exact(pso) { count(DRAWS_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -633,7 +633,7 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us if compute && max == 1 && count_buf == 0 && DISPATCH_ONLY.lock().unwrap().as_ref().is_some_and(|m| m.contains_key(&sig)) { let chained = indirect_address(args, args_off_title, args_off).map_or(Ok(false), |address| { super::real::dispatch_chain::execute_indirect(list, pso, with(this, |s| s.allocator), address, - |root| bind_abi_with_root(this, list, pso, false, root)) + |root| bind_abi_with_root_resolved(this, list, pso, false, root, None, [0; 3]).map(|_| ())) }); match chained { Ok(true) => { @@ -651,11 +651,11 @@ pub(super) unsafe extern "system" fn execute_indirect(this: *mut c_void, sig: us } } } - match bind_abi(this, list, pso, gfx) { + match bind_abi_resolved(this, list, pso, gfx) { Ok(()) => { if compute { - if super::real::pso_dispatch_remap(pso).is_some() { - match super::real::remap_fallback::whole(pso) { + if super::real::pso_dispatch_remap_exact(pso).is_some() { + match super::real::remap_fallback::whole_exact(pso) { Ok(fallback) => list.SetPipelineState(&fallback), Err(e) => { static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); @@ -924,7 +924,7 @@ unsafe fn graphics_bound(this: *mut c_void) -> Option<(ID3D12GraphicsCommandList // TWO_PASS rather than TRANSLATED_GRAPHICS. Binding the title's graphics RS and issuing // DrawInstanced / DrawIndexedInstanced against a mesh PSO invalidates the command list // (Close → E_INVALIDARG). - if let Some(tp) = super::real::two_pass_of(real) { + if let Some(tp) = super::real::two_pass_of_exact(real) { count(DRAWS_SKIPPED); static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); if N.fetch_add(1, Ordering::Relaxed) < 4 { @@ -945,7 +945,7 @@ unsafe fn graphics_bound(this: *mut c_void) -> Option<(ID3D12GraphicsCommandList crate::xlog_info!( "frame trace: draw with an untranslated pipeline {pso:#x} (resolves to {real:#x}; pending {}, two-pass {}, RT0 {:?})", super::pso_async::is_pending(pso), - super::real::two_pass_of(real).is_some(), + super::real::two_pass_of_exact(real).is_some(), super::present::rt0_resource(this as usize).map(|r| ID3D12Resource::from_raw_borrowed(&(r as *mut c_void)).map(|x| { let d = x.GetDesc(); (d.Width, d.Height, d.Format.0) })) ); } @@ -1277,7 +1277,7 @@ fn dispatch_groups(pso: usize, dims: [u32; 3], flags: u32) -> Option<([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 threads = super::real::pso_threads_exact(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)) @@ -1530,18 +1530,6 @@ fn prepared_abi_io( io } -unsafe fn bind_abi(this: *mut c_void, list: &ID3D12GraphicsCommandList, pso: usize, gfx: bool) -> Result<(), String> { - let pso = super::list_x::resolve_and_record_pipeline_wait(list.as_raw() as usize, pso); - bind_abi_resolved(this, list, pso, gfx) -} - -unsafe fn bind_abi_with_root( - this: *mut c_void, list: &ID3D12GraphicsCommandList, pso: usize, gfx: bool, root: usize, -) -> Result<(), String> { - let pso = super::list_x::resolve_and_record_pipeline_wait(list.as_raw() as usize, pso); - bind_abi_with_root_resolved(this, list, pso, gfx, root, None, [0; 3]).map(|_| ()) -} - unsafe fn bind_abi_resolved(this: *mut c_void, list: &ID3D12GraphicsCommandList, pso: usize, gfx: bool) -> Result<(), String> { bind_abi_with_root_resolved(this, list, pso, gfx, 0, None, [0; 3]).map(|_| ()) } @@ -1572,7 +1560,7 @@ unsafe fn bind_abi_with_root_resolved( let windows = super::real::uma_window_vas(); let first = *windows.first().ok_or("unified memory is not open on the GPU")?; // a compute shader takes no more arguments from registers than it has (`inline_for`) - let user_sgprs = if gfx { None } else { super::real::pso_user_sgprs(pso) }; + let user_sgprs = if gfx { None } else { super::real::pso_user_sgprs_exact(pso) }; let prepared = prepare_abi_state(this, u, gfx, user_sgprs)?; let PreparedAbiState { layout, args, inline, table_at, io_at, res_at, allocator } = prepared; std::ptr::copy_nonoverlapping(args[inline..].as_ptr(), table_at as *mut u8, args.len() - inline); @@ -1582,7 +1570,7 @@ unsafe fn bind_abi_with_root_resolved( std::ptr::write_bytes(res_at as *mut u8, 0, 256); let va = |p: usize| super::real::uma_va_of(p).unwrap_or(first); let rs = (root != 0).then_some(root) - .or_else(|| super::real::pso_root_signature(pso)) + .or_else(|| super::real::pso_root_signature_exact(pso)) .or_else(|| super::real::abi_rs_cached().map(|p| p as usize)) .ok_or("no XCL ABI root signature")?; let rs = rs as *mut c_void; @@ -2728,6 +2716,30 @@ void main(uint t : SV_GroupIndex) { assert_eq!([graphics[slots[0]], graphics[slots[1]], graphics[slots[2]]], [0; 3]); } + #[test] + fn resolved_compute_metadata_does_not_follow_a_reused_native_address() { + let exact = 0x7301_3000usize; + let replacement = 0x7301_4000usize; + real::register_compute_metadata_for_test(exact, 6, [4, 2, 1], (2, 0), 0x7301_5000); + real::register_compute_metadata_for_test(replacement, 9, [8, 1, 1], (3, 1), 0x7301_6000); + pso_async::submit_lazy(exact, true, Box::new(move || replacement), 0); + assert_eq!(pso_async::resolve_wait(exact), replacement); + + assert_eq!(real::pso_threads(exact), Some([8, 1, 1]), + "the title-address accessor should still resolve an active stand-in"); + assert_eq!(real::pso_threads_exact(exact), Some([4, 2, 1])); + assert_eq!(real::pso_user_sgprs_exact(exact), Some(6)); + assert_eq!(real::pso_dispatch_remap_exact(exact), Some((2, 0))); + assert_eq!(real::pso_root_signature_exact(exact), Some(0x7301_5000)); + assert_eq!(dispatch_groups(exact, [8, 4, 1], DISPATCH_THREAD_DIMENSIONS), + Some(([2, 2, 1], [0; 3])), + "an already-selected native generation was redirected through PENDING"); + + assert_eq!(pso_async::released(exact), replacement); + real::forget_pso_for_test(exact); + real::forget_pso_for_test(replacement); + } + #[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/console_list.rs b/crates/xcl/src/hle/d3d12x/console_list.rs index 75a597eee0..8834313e13 100644 --- a/crates/xcl/src/hle/d3d12x/console_list.rs +++ b/crates/xcl/src/hle/d3d12x/console_list.rs @@ -906,7 +906,7 @@ pub unsafe fn close(obj: *mut c_void) -> Result { out.skipped_dispatches += 1; continue; } - let user_sgprs = super::real::pso_user_sgprs(real); + let user_sgprs = super::real::pso_user_sgprs_exact(real); let inline = layout.as_ref().map_or(0, |l| inline_for(l, user_sgprs) as usize).min(table.len()); let table_at = uma_copy(st, &table[inline..]).ok_or("UMA: argument table")?; let mut io = match layout.as_ref() { @@ -918,7 +918,7 @@ pub unsafe fn close(obj: *mut c_void) -> Result { let io_at = uma_copy(st, bytemuck_u32(&io)).ok_or("UMA: io")?; let res_at = uma_copy(st, &[0u8; 256]).ok_or("UMA: res")?; // the pipeline's own root signature when it has static samplers - let own = super::real::pso_root_signature(real).or(((*p).rs != 0).then_some((*p).rs)).map(|r| r as *mut c_void); + let own = super::real::pso_root_signature_exact(real).or(((*p).rs != 0).then_some((*p).rs)).map(|r| r as *mut c_void); match own.as_ref().and_then(|r| ID3D12RootSignature::from_raw_borrowed(r)) { Some(r) => list.SetComputeRootSignature(r), None => list.SetComputeRootSignature(rs.as_ref().ok_or("no XCL ABI root signature")?), diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 281f299251..4975cf3a80 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4407,12 +4407,20 @@ static PSO_DISPATCH_REMAP: Mutex>> = Mutex::ne /// 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_exact(pso) +} + +pub(super) fn pso_user_sgprs_exact(pso: usize) -> Option { 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_threads_exact(pso) +} + +pub(super) fn pso_threads_exact(pso: usize) -> Option<[u32; 3]> { PSO_USER_SGPRS.lock().unwrap().as_ref()?.get(&pso).map(|e| e.1) } @@ -4422,8 +4430,21 @@ 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)); } +#[cfg(test)] +pub(super) fn register_compute_metadata_for_test( + pso: usize, user_sgprs: u32, threads: [u32; 3], remap: (u32, u32), root: usize, +) { + PSO_USER_SGPRS.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso, (user_sgprs, threads)); + PSO_DISPATCH_REMAP.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso, remap); + PSO_RS.lock().unwrap().get_or_insert_with(HashMap::new).insert(pso, root); +} + pub fn pso_dispatch_remap(pso: usize) -> Option<(u32, u32)> { let pso = super::pso_async::resolve(pso); + pso_dispatch_remap_exact(pso) +} + +pub(super) fn pso_dispatch_remap_exact(pso: usize) -> Option<(u32, u32)> { PSO_DISPATCH_REMAP.lock().unwrap().as_ref()?.get(&pso).copied() } @@ -4443,6 +4464,10 @@ fn note_pso_dispatch_remap(pso: *mut c_void, remap: Option<(u32, u32)>) { /// The root signature a translated pipeline state was created with. pub fn pso_root_signature(pso: usize) -> Option { let pso = super::pso_async::resolve(pso); + pso_root_signature_exact(pso) +} + +pub(super) fn pso_root_signature_exact(pso: usize) -> Option { PSO_RS.lock().unwrap().as_ref()?.get(&pso).copied() } @@ -5129,8 +5154,13 @@ pub(super) mod remap_fallback { } } + #[allow(dead_code)] // title-address counterpart to whole_exact pub(in crate::hle::d3d12x) unsafe fn whole(pso: usize) -> Result { let pso = super::super::pso_async::resolve(pso); + whole_exact(pso) + } + + pub(in crate::hle::d3d12x) unsafe fn whole_exact(pso: usize) -> Result { let fb = FALLBACKS.lock().unwrap().get(&pso).cloned().ok_or("no remap fallback")?; fb.whole.get_or_build(std::time::Instant::now(), || { let status = Arc::new(std::sync::atomic::AtomicI32::new(E_INVALIDARG)); @@ -7525,6 +7555,10 @@ static TWO_PASS: Mutex>>> = Mut /// The compute pass of a pipeline state XCL built from console MS / GS code. pub fn two_pass_of(pso: usize) -> Option> { let pso = super::pso_async::resolve(pso); + two_pass_of_exact(pso) +} + +pub(super) fn two_pass_of_exact(pso: usize) -> Option> { TWO_PASS.lock().unwrap().as_ref()?.get(&pso).cloned() } diff --git a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs index e980ee6ca3..eea0a809ac 100644 --- a/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs +++ b/crates/xcl/src/hle/d3d12x/real/dispatch_chain.rs @@ -519,10 +519,10 @@ pub(in crate::hle::d3d12x) unsafe fn dispatch(list: &ID3D12GraphicsCommandList, let [x, y, z] = grid; let chain = CHAINS.lock().unwrap().get(&pso).cloned(); let Some(chain) = chain else { - match remapped_grid(grid, super::pso_dispatch_remap(pso)) { + match remapped_grid(grid, super::pso_dispatch_remap_exact(pso)) { Some([x, y, z]) => list.Dispatch(x, y, z), None => { - list.SetPipelineState(&super::remap_fallback::whole(pso)?); + list.SetPipelineState(&super::remap_fallback::whole_exact(pso)?); list.Dispatch(x, y, z); } }