diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index 8b81b85913..41b90c8b24 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -331,6 +331,108 @@ unsafe fn title_reset_in(s: &mut State, alloc: *mut c_void, mut reset: impl FnMu mod tests { use super::*; + /// When title allocator `A` is already rotated to `B` at `CreateCommandList`, + /// `on_create_with_backing` retains `A` as `ListState.allocator` while pinning the first + /// recording (and its `bind` state) to `B`. After `A` rotates again to `C`, a continuation + /// resolved through `ListState.allocator` selects `C` while the first recording stays on `B`. + #[test] + fn created_on_rotated_backing_keeps_title_allocator_across_second_rotation() { + let mut ids = Box::new([0u8; 6]); + let base = ids.as_mut_ptr() as usize; + let (alloc_a, backing_b, backing_c) = (base, base + 1, base + 2); + let (seed_list, list, cont) = (base + 3, base + 4, base + 5); + let ty = D3D12_COMMAND_LIST_TYPE_DIRECT; + + // Seed `A` with an active recording on `A` and free spares `B`, `C`, then rotate `A -> B`. + created_on(seed_list, alloc_a, ty.0); + let rot1_hr = { + let mut g = STATE.lock().unwrap(); + let s = g.as_mut().unwrap(); + s.aside.insert(alloc_a, vec![backing_b, backing_c]); + s.backings.insert(backing_b, Backing::default()); + s.backings.insert(backing_c, Backing::default()); + unsafe { title_reset_in(s, alloc_a as *mut c_void, |_| 0) } + }; + let pc_at_create = current(alloc_a); + + // Mirror `device_x::create_list`: create on `pc_at_create` (`B`), retain title `A`, + // then initialize `bind` state against `A` (which pins to `recording_backing(list)` = `B`). + unsafe { + super::super::list_x::on_create_with_backing(list as *mut c_void, alloc_a, pc_at_create, 0, ty); + } + super::super::bind::reset(list as *mut c_void, alloc_a, 0); + + let tracked_after_create = super::super::list_x::tracked_allocator_for_test(list); + let backing_after_create = recording_backing(list); + let bind_alloc_after_create = super::super::bind::reset_state_for_test(list as *mut c_void).0; + + // Second rotation (`A -> C`) while `list` still holds its initial recording on `B`. + let rot2_hr = { + let mut g = STATE.lock().unwrap(); + let s = g.as_mut().unwrap(); + unsafe { title_reset_in(s, alloc_a as *mut c_void, |_| 0) } + }; + let cur_after_rot2 = current(alloc_a); + + // Continuation resolves through `ListState.allocator` (`A`) and must select `C`, + // while the initial recording remains pinned to `B`. + let title_alloc = super::super::list_x::tracked_allocator_for_test(list); + let mut cont_pc_alloc = 0usize; + let cont_hr = title_alloc.map(|a| { + let hr = reset_list(cont, a, ty.0, |pc_alloc| { + cont_pc_alloc = pc_alloc; + 0 + }); + super::super::bind::inherit_for_split(list as *mut c_void, cont as *mut c_void, a); + hr + }); + + let backing_after_rot2 = recording_backing(list); + let cont_backing = recording_backing(cont); + let bind_alloc_first = super::super::bind::reset_state_for_test(list as *mut c_void).0; + let bind_alloc_cont = super::super::bind::reset_state_for_test(cont as *mut c_void).0; + let backings_pinned = { + let g = STATE.lock().unwrap(); + let s = g.as_ref().unwrap(); + s.backings.get(&backing_b).is_some_and(|b| b.users.contains(&list) && !b.users.contains(&cont)) + && s.backings.get(&backing_c).is_some_and(|b| b.users.contains(&cont)) + }; + + // Clean up synthetic addresses before assertions without dropping fake COM pointers. + super::super::list_x::on_release(list); + forget_list(cont); + forget_list(seed_list); + super::super::bind::remove_state_for_test(list as *mut c_void); + super::super::bind::remove_state_for_test(cont as *mut c_void); + { + let mut g = STATE.lock().unwrap(); + if let Some(s) = g.as_mut() { + s.cur.remove(&alloc_a); + s.aside.remove(&alloc_a); + for a in [alloc_a, backing_b, backing_c] { + s.types.remove(&a); + s.backings.remove(&a); + } + } + } + + assert_eq!(rot1_hr, Some(0)); + assert_eq!(pc_at_create, backing_b, "first rotation must move A -> B"); + assert_eq!(tracked_after_create, Some(alloc_a), "ListState.allocator must retain title allocator A"); + assert_eq!(backing_after_create, Some(backing_b), "initial recording must pin to B"); + assert_eq!(bind_alloc_after_create, backing_b, "bind::State.allocator for initial recording must pin to B"); + assert_eq!(rot2_hr, Some(0)); + assert_eq!(cur_after_rot2, backing_c, "second rotation must move A -> C"); + assert_eq!(title_alloc, Some(alloc_a), "ListState.allocator must stay A after second rotation"); + assert_eq!(cont_hr, Some(0)); + assert_eq!(cont_pc_alloc, backing_c, "continuation reset_list(ListState.allocator) must select C"); + assert_eq!(cont_backing, Some(backing_c), "continuation recording must pin to C"); + assert_eq!(bind_alloc_cont, backing_c, "continuation bind::State.allocator must pin to C"); + assert_eq!(backing_after_rot2, Some(backing_b), "first recording must remain pinned to B"); + assert_eq!(bind_alloc_first, backing_b, "first recording bind::State.allocator must remain pinned to B"); + assert!(backings_pinned, "B must still hold list (not cont), and C must hold cont"); + } + #[test] fn a_spare_whose_reset_fails_stays_available_for_retry() { let (alloc, spare, list) = (0x1000usize, 0x2000usize, 0x3000usize); diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index 6d2bc04f0b..5b7c72a0a6 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -362,7 +362,7 @@ pub(super) fn release_allocator_res(allocator: usize) { /// Whether `FREE_RES`, holding `count` buffers of `held` bytes, keeps one of `bytes` more. fn pool_keeps(count: usize, held: u64, bytes: u64) -> bool { - count < 16 && held + bytes <= FREE_RES_CAP + count < 256 && held + bytes <= FREE_RES_CAP } /// Whether `FREE_IO`, holding `held` bytes of VRAM `io` slabs, keeps one of `bytes` more. @@ -652,14 +652,11 @@ pub(super) fn clear_state(list: *mut c_void, pso: usize) { pub(super) fn rebind_allocator(list: *mut c_void, allocator: usize) { let backing = backing_allocator(list, allocator); let (old, blocks, res_bufs, io_blocks, prelude) = with(list, |s| { - if s.allocator == backing { - return (0, Vec::new(), Vec::new(), Vec::new(), None); - } - let old = std::mem::replace(&mut s.allocator, backing); - s.arena = Default::default(); - s.scratch = Default::default(); - s.scratch_next = 0; - s.scratch_taken = 0; + s.heaps_ensured = false; + s.pc_abi_gfx = AbiBound::default(); + s.pc_abi_cs = AbiBound::default(); + s.arg_order_clean = false; + s.inline_guard = None; s.prelude_scratch = Default::default(); s.prelude_bufs.clear(); s.prelude_guards.clear(); @@ -667,7 +664,17 @@ pub(super) fn rebind_allocator(list: *mut c_void, allocator: usize) { s.prelude_draws.clear(); let prelude = s.prelude_owned.take(); s.prelude_built = None; - s.inline_guard = None; + if s.allocator == backing { + for sl in &mut s.scratch { + sl.state = D3D12_RESOURCE_STATE_COMMON; + } + return (backing, Vec::new(), Vec::new(), Vec::new(), prelude); + } + let old = std::mem::replace(&mut s.allocator, backing); + s.arena = Default::default(); + s.scratch = Default::default(); + s.scratch_next = 0; + s.scratch_taken = 0; s.dummy_res_va = 0; s.dummy_table_p = 0; s.io_cache_gfx = None; @@ -738,8 +745,12 @@ pub(super) fn inherit_for_split(from: *mut c_void, to: *mut c_void, allocator: u s.io_cache_cs = None; s.io_cache_tp_cs = None; s.io_cache_tp_ms = None; + let mut res_bufs = std::mem::take(&mut s.res_retired); + if let Some(b) = s.res_buf.take() { + res_bufs.push(b); + } let io_blocks = std::mem::take(&mut s.io_blocks); - (old, std::mem::take(&mut s.uma), std::mem::take(&mut s.res_retired), io_blocks, prelude) + (old, std::mem::take(&mut s.uma), res_bufs, io_blocks, prelude) }); console_list::hold_until_reset(old, blocks); hold_res_until_reset(old, res_bufs); @@ -1174,7 +1185,11 @@ unsafe fn default_uav_buffer(dev: &ID3D12Device, bytes: u64, what: &str) -> Resu &mut res, ) .map_err(|e| format!("{what} ({bytes:#x} B): {e}"))?; - res.ok_or_else(|| format!("null {what}")) + let res = res.ok_or_else(|| format!("null {what}"))?; + if super::real::dred_on() { + super::real::name_resource(res.as_raw(), what); + } + Ok(res) } /// Groups of a two-pass dispatch that have a record: one record a group in `cap_bytes` of @@ -5701,7 +5716,8 @@ void main(uint t : SV_GroupIndex) { assert!(pool_keeps(0, 0, 128 << 20)); assert!(pool_keeps(1, 128 << 20, 128 << 20)); assert!(!pool_keeps(2, 256 << 20, 4 << 20)); - assert!(!pool_keeps(16, 0, 1 << 20)); + assert!(pool_keeps(16, 0, 1 << 20)); + assert!(!pool_keeps(256, 0, 1 << 20)); } /// A second queue for two-pass draws is told from the first one. @@ -6470,6 +6486,49 @@ void ms(uint3 g : SV_GroupID) { o.Store((v * 16u + g.x) * 4u, rec.Load(g.x * 4u) super::super::alloc_pool::forget_list(list as usize); } + #[test] + fn rebind_allocator_clears_per_recording_state_even_on_same_backing() { + let list = 0x7E57_9101usize as *mut c_void; + let title_alloc = 0x7E57_9110usize; + let backing_a = 0x7E57_9120usize; + let backing_b = 0x7E57_9130usize; + super::super::alloc_pool::created_on(list as usize, backing_a, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + reset(list, title_alloc, 0); + with(list, |s| { + s.heaps_ensured = true; + s.arg_order_clean = true; + s.pc_abi_gfx.rs = 0x1111; + s.pc_abi_cs.rs = 0x2222; + s.scratch[0].state = GUARDED_ARGS_STATE; + s.uma.push(0xBEEF_1000); + }); + + // Same backing: per-recording native-reset state must clear while UMA blocks stay pinned. + rebind_allocator(list, title_alloc); + with(list, |s| { + assert_eq!(s.allocator, backing_a); + assert!(!s.heaps_ensured); + assert!(!s.arg_order_clean); + assert_eq!(s.pc_abi_gfx.rs, 0); + assert_eq!(s.pc_abi_cs.rs, 0); + assert_eq!(s.scratch[0].state, D3D12_RESOURCE_STATE_COMMON); + assert_eq!(s.uma, vec![0xBEEF_1000]); + }); + + // Rotated backing: per-recording state clears and prior UMA blocks retire onto `backing_a`. + with(list, |s| s.heaps_ensured = true); + super::super::alloc_pool::created_on(list as usize, backing_b, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + rebind_allocator(list, title_alloc); + with(list, |s| { + assert_eq!(s.allocator, backing_b); + assert!(!s.heaps_ensured); + assert!(s.uma.is_empty()); + }); + assert_eq!(console_list::take_pending_for_test(backing_a), vec![0xBEEF_1000]); + forget(list); + super::super::alloc_pool::forget_list(list as usize); + } + /// `SCALE_ARGS_HEAP_HLSL` and `GUARD_ARGS_HEAP_HLSL` read from a real 4 GiB reserved buffer's /// raw UAV in `ResourceDescriptorHeap`: a mapped tile produces exact scaled/folded outputs, /// while an unmapped NULL tile reads safely as zero with no GPU page fault. diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 20f4ad06e6..aecb7276a3 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -97,9 +97,10 @@ unsafe fn create_list(this: *mut c_void, node: u32, ty: u32, alloc: usize, pso: real::dev_create_command_list_x(this, node, ty, alloc, pso, riid, ppv) } else { let resolved_pso = super::pso_async::resolve(pso); - let hr = real::dev_create_command_list(this, node, ty, alloc, resolved_pso, riid, ppv); + let pc_alloc = super::alloc_pool::current(alloc); + let hr = real::dev_create_command_list(this, node, ty, pc_alloc, resolved_pso, riid, ppv); if hr >= 0 && !ppv.is_null() && !(*ppv).is_null() { - super::list_x::on_create(*ppv, alloc, pso, D3D12_COMMAND_LIST_TYPE(ty as i32)); + super::list_x::on_create_with_backing(*ppv, alloc, pc_alloc, pso, D3D12_COMMAND_LIST_TYPE(ty as i32)); super::dred::reset(*ppv); if pso != 0 { super::dred::mark_pipeline(*ppv, pso, resolved_pso); diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 3b4667a184..f74bebd903 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -1152,6 +1152,16 @@ unsafe fn try_native_depth_bias(list: *mut c_void, bias: DepthBias) -> bool { /// # Safety /// `list` must be null or a valid `ID3D12GraphicsCommandList` COM pointer. pub unsafe fn on_create(list: *mut c_void, alloc: usize, pso: usize, ty: D3D12_COMMAND_LIST_TYPE) { + on_create_with_backing(list, alloc, alloc, pso, ty); +} + +pub unsafe fn on_create_with_backing( + list: *mut c_void, + title_alloc: usize, + pc_alloc: usize, + pso: usize, + ty: D3D12_COMMAND_LIST_TYPE, +) { if list.is_null() { return; } @@ -1174,8 +1184,8 @@ pub unsafe fn on_create(list: *mut c_void, alloc: usize, pso: usize, ty: D3D12_C WRITE64_SIGNAL_LISTS.fetch_sub(1, Ordering::AcqRel); } } - super::alloc_pool::created_on(key, alloc, ty.0); - m.insert(key, ListState { allocator: alloc, list_type: Some(ty), bound_pso: pso, ..Default::default() }); + super::alloc_pool::created_on(key, pc_alloc, ty.0); + m.insert(key, ListState { allocator: title_alloc, list_type: Some(ty), bound_pso: pso, ..Default::default() }); old }; drop(old); @@ -1246,6 +1256,11 @@ pub(super) fn has_list_state(list: usize) -> bool { LISTS.lock().unwrap().as_ref().is_some_and(|m| m.contains_key(&list)) } +#[cfg(test)] +pub(super) fn tracked_allocator_for_test(list: usize) -> Option { + LISTS.lock().unwrap().as_ref()?.get(&list).map(|s| s.allocator) +} + /// Forget a released command allocator so `KickoffX` will not reset a list with a dead allocator. pub(super) fn on_allocator_release(alloc: usize) { if let Some(m) = LISTS.lock().unwrap().as_mut() {