From ffc3f6c3d6a4a9ce8ed876e81b01494d4e2ba4b9 Mon Sep 17 00:00:00 2001 From: ChalanaGimhanaX Date: Tue, 24 Nov 2026 16:46:00 +0530 Subject: [PATCH] D3D12.X: track split and created lists on alloc_pool's active backing --- crates/xcl/src/hle/d3d12x/alloc_pool.rs | 319 +++++++++++++++++++++++- crates/xcl/src/hle/d3d12x/device_x.rs | 5 +- crates/xcl/src/hle/d3d12x/list_x.rs | 122 ++++++++- crates/xcl/src/hle/d3d12x/real.rs | 4 +- 4 files changed, 429 insertions(+), 21 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index 8b81b85913..a234eb8f02 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -98,6 +98,20 @@ pub(super) fn forget_list(list: usize) { } } +/// `list` was retired to a spare pool after its wrapper reset and will not be submitted again +/// with its previous recording: drop its backing pin (`of_list` / `users`) while preserving +/// `last_submit` and `recordings` so `in_flight(list)` still blocks spare reuse until the GPU +/// finishes any submitted work (and `b.marks` still protects the backing allocator). +pub(super) fn release_recording(list: usize) { + let mut g = STATE.lock().unwrap(); + let Some(s) = g.as_mut() else { return }; + if let Some(backing) = s.of_list.remove(&list) { + if let Some(b) = s.backings.get_mut(&backing) { + b.users.remove(&list); + } + } +} + /// Reset `list` on the title's allocator `alloc`, committing its new recording association /// only after native Reset succeeds. No pool lock is held across the driver call. pub fn reset_list(list: usize, alloc: usize, ty: i32, reset: impl FnOnce(usize) -> HRESULT) -> HRESULT { @@ -131,19 +145,28 @@ pub fn current(alloc: usize) -> usize { STATE.lock().unwrap().as_ref().and_then(|s| s.cur.get(&alloc).copied()).unwrap_or(alloc) } -/// `list` (of type `ty`) was made recording on the PC allocator `raw` itself -/// (CreateCommandList). -pub fn created_on(list: usize, raw: usize, ty: i32) { - if !enabled() || raw == 0 { +/// `list` (of type `ty`; negative: unknown) was made recording on `pc_alloc` for `title_alloc` +/// (`CreateCommandList`, a mid-list split segment, or a reset stand-in). +pub fn created_on_backing(list: usize, title_alloc: usize, pc_alloc: usize, ty: i32) { + if !enabled() || title_alloc == 0 || pc_alloc == 0 { return; } let mut g = STATE.lock().unwrap(); let s = g.get_or_insert_with(State::default); - s.types.entry(raw).or_insert(ty); - move_list(s, list, raw); + if ty >= 0 { + s.types.entry(title_alloc).or_insert(ty); + } + move_list(s, list, pc_alloc); *s.recordings.entry(list).or_default() += 1; } +/// `list` (of type `ty`) was made recording on the PC allocator `raw` itself +/// (CreateCommandList). +#[cfg(test)] +fn created_on(list: usize, raw: usize, ty: i32) { + created_on_backing(list, raw, raw, ty); +} + /// Diagnostic (#1452): `lists` are about to be submitted to `queue`. A PC list whose previous /// submit the GPU has not finished must not be submitted again on the PC (debug layer id 553, /// "the command queue fence has not advanced past previous executions of the command list"); @@ -550,4 +573,288 @@ mod tests { assert_eq!(after_forget, 0, "a forgotten list has no previous submit"); } } + + /// Retiring a split/stand-in segment into `title_list`'s spare pool must release its + /// allocator backing pin while keeping `in_flight` active until the GPU finishes its submit. + #[test] + fn release_recording_unpins_backing_while_preserving_in_flight() { + unsafe { + let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let mut ids = Box::new([0u8; 3]); + let base = ids.as_mut_ptr() as usize; + let (title_alloc, second, seg) = (base, base + 1, base + 2); + let queue = 0x20usize as *mut c_void; + created_on_backing(seg, title_alloc, second, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + { + let mut g = STATE.lock().unwrap(); + let s = g.as_mut().unwrap(); + let recording = s.recordings[&seg]; + s.last_submit.insert(seg, LastSubmit { queue: queue as usize, fence: fence.clone(), value: 1, recording }); + } + assert_eq!(recording_backing_for_test(seg), Some(second)); + assert!(in_flight(seg)); + + release_recording(seg); + assert_eq!(recording_backing_for_test(seg), None, "retired spare must release its backing pin"); + assert!(in_flight(seg), "retired spare must remain in_flight until the GPU finishes"); + assert_eq!(note_resubmits(queue, &[seg as *mut c_void]), 1); + + fence.Signal(1).unwrap(); + assert!(!in_flight(seg)); + forget_list(seg); + forget(title_alloc); + { + let mut g = STATE.lock().unwrap(); + if let Some(s) = g.as_mut() { + s.backings.remove(&second); + } + } + } + } + + /// Creating a new command list or resetting an adopted PC list after `alloc` swapped to a + /// substitute backing must record on `current(alloc)` so a later `alloc.Reset()` keeps the + /// live recording intact instead of resetting the active backing underneath it. + #[test] + fn cl_reset_and_new_list_after_swap_use_active_backing() { + unsafe { + let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let a = alloc.as_raw(); + if !super::super::real::is_adopted(a) { + super::super::real::adopt(a, super::super::real::Kind::CommandAllocator); + } + let list1: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); + created_on(list1.as_raw() as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + list1.Close().unwrap(); + + // First allocator reset puts `a` aside because `list1` still holds a recording on it. + alloc.Reset().unwrap(); + let second = current(a as usize); + assert_ne!(second, a as usize); + + // Resetting an adopted PC command list via `ID3D12GraphicsCommandList::Reset` with the + // title's `alloc` must redirect to `second = current(a)`, not record onto busy `a`. + let list2: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &alloc, None).unwrap(); + let l2 = list2.as_raw(); + if !super::super::real::is_adopted(l2) { + super::super::real::adopt(l2, super::super::real::Kind::CommandList); + } + list2.Close().unwrap(); + list2.Reset(&alloc, None).expect("cl_reset must resolve title allocator to active backing"); + assert_eq!(recording_backing_for_test(l2 as usize), Some(second), "cl_reset must track list2 on the active substitute backing"); + list2.Close().unwrap(); + + // A second allocator reset must keep `second` aside because `list2` is live on it. + alloc.Reset().unwrap(); + let third = current(a as usize); + assert_ne!(third, second, "active substitute backing holding list2 must not be reset"); + assert!(dev.GetDeviceRemovedReason().is_ok()); + + drop(list2); + drop(list1); + drop(alloc); + assert_eq!(current(a as usize), a as usize); + } + } + + #[test] + fn created_list_after_swap_tracks_active_backing() { + let (title_alloc, second, l1, l2) = (0x7E57_A000usize, 0x7E57_B000usize, 0x7E57_C000usize, 0x7E57_D000usize); + { + let mut g = STATE.lock().unwrap(); + let s = g.get_or_insert_with(State::default); + s.types.insert(title_alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + s.cur.insert(title_alloc, second); + s.aside.insert(title_alloc, vec![title_alloc]); + move_list(s, l1, title_alloc); + } + created_on_backing(l2, title_alloc, second, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + assert_eq!(recording_backing_for_test(l2), Some(second)); + { + let g = STATE.lock().unwrap(); + let s = g.as_ref().unwrap(); + assert!(!s.types.contains_key(&second)); + assert!(s.backings[&second].users.contains(&l2)); + assert!(!s.backings[&second].users.contains(&l1)); + assert_eq!(s.recordings.get(&l2).copied(), Some(1)); + } + forget_list(l2); + assert_eq!(recording_backing_for_test(l2), None); + { + let mut g = STATE.lock().unwrap(); + let s = g.as_mut().unwrap(); + assert!(s.backings[&second].users.is_empty()); + s.aside.remove(&title_alloc); + s.cur.remove(&title_alloc); + s.types.remove(&title_alloc); + s.backings.remove(&second); + s.backings.remove(&title_alloc); + s.of_list.remove(&l1); + } + } + + /// Concurrent allocator swaps (`title_reset_in`) racing with command-list resets (`reset_list`), + /// newly created segments (`created_on_backing`), and spare retirements (`release_recording`) + /// must keep `of_list` and `backings[b].users` consistent and always pin each list to the + /// exact backing its driver Reset received. + #[test] + fn concurrent_allocator_reset_and_list_reset_keep_backing_invariants() { + use std::sync::{ + atomic::{AtomicBool, AtomicUsize, Ordering}, + Arc, Barrier, + }; + + // Unique synthetic pointers outside any real heap/test ID range. + let mut ids = Box::new([0u8; 32]); + let base = ids.as_mut_ptr() as usize; + let title_alloc = base; + let spares: Vec = (1..=6).map(|i| base + i).collect(); + let all_backings: Vec = std::iter::once(title_alloc).chain(spares.iter().copied()).collect(); + let pin_list = base + 8; + let worker_lists: Vec = (0..4).map(|i| base + 10 + i).collect(); + + { + let mut g = STATE.lock().unwrap(); + let s = g.get_or_insert_with(State::default); + s.types.insert(title_alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + s.aside.insert(title_alloc, spares.clone()); + for &b in &spares { + s.backings.entry(b).or_default(); + } + move_list(s, pin_list, title_alloc); + } + + // 1. Deterministic interleaving: thread 1 reads `current(title_alloc)` = `title_alloc` inside + // `reset_list`, pauses before returning from the driver callback while thread 2 swaps + // `title_alloc` to a spare backing, then thread 1 commits. `worker_lists[0]` must stay + // pinned to `title_alloc` (the backing its Reset actually used), not the swapped spare. + let in_reset = Arc::new(Barrier::new(2)); + let swapped = Arc::new(Barrier::new(2)); + let l0 = worker_lists[0]; + let (in_reset_t, swapped_t) = (in_reset.clone(), swapped.clone()); + let observed = Arc::new(AtomicUsize::new(0)); + let observed_t = observed.clone(); + let t1 = std::thread::spawn(move || { + let hr = reset_list(l0, title_alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |pc_alloc| { + observed_t.store(pc_alloc, Ordering::SeqCst); + in_reset_t.wait(); + swapped_t.wait(); + 0 + }); + assert_eq!(hr, 0); + }); + in_reset.wait(); + let swap_hr = { + let mut g = STATE.lock().unwrap(); + let s = g.as_mut().unwrap(); + unsafe { title_reset_in(s, title_alloc as *mut c_void, |_| 0) } + }; + assert_eq!(swap_hr, Some(0)); + let after_swap = current(title_alloc); + assert_ne!(after_swap, title_alloc); + swapped.wait(); + t1.join().unwrap(); + assert_eq!(observed.load(Ordering::SeqCst), title_alloc); + assert_eq!(recording_backing_for_test(l0), Some(title_alloc), "reset_list must commit the exact backing passed to Reset"); + // Unpin `pin_list`; `l0` still pins `title_alloc`, so `title_alloc` is not free yet. + release_recording(pin_list); + assert!(!unsafe { STATE.lock().unwrap().as_ref().unwrap().backings[&title_alloc].free() }); + + // 2. Multi-threaded stress: allocator resets race with concurrent list resets, creations, + // and retirements across 4 worker threads. + let stop = Arc::new(AtomicBool::new(false)); + let mut handles = Vec::new(); + for (idx, &list_id) in worker_lists.iter().enumerate() { + let stop_w = stop.clone(); + handles.push(std::thread::spawn(move || { + for step in 0..200usize { + if stop_w.load(Ordering::Relaxed) { + break; + } + let chosen = AtomicUsize::new(0); + let hr = reset_list(list_id, title_alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |pc| { + chosen.store(pc, Ordering::Relaxed); + if (step + idx) % 11 == 0 { crate::util::E_FAIL } else { 0 } + }); + if hr >= 0 { + assert_eq!(recording_backing_for_test(list_id), Some(chosen.load(Ordering::Relaxed))); + } + if (step + idx) % 5 == 0 { + release_recording(list_id); + } else if (step + idx) % 7 == 0 { + let b = current(title_alloc); + created_on_backing(list_id, title_alloc, b, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + } + } + })); + } + let resetter = std::thread::spawn(move || { + for _ in 0..200usize { + let mut g = STATE.lock().unwrap(); + let s = g.as_mut().unwrap(); + // Keep at least one backing free in `aside` when all spares are briefly busy + // so `title_reset_in` never tries to call COM `GetDevice` on the synthetic pointer. + let cur_b = s.cur.get(&title_alloc).copied().unwrap_or(title_alloc); + let cur_free = s.backings.get(&cur_b).is_none_or(|b| unsafe { b.free() }); + let has_free_aside = s.aside.get(&title_alloc).is_some_and(|v| v.iter().any(|f| s.backings.get(f).is_none_or(|b| unsafe { b.free() }))); + let busy_now: HashSet = s.backings.iter().filter(|(_, b)| !b.users.is_empty()).map(|(&k, _)| k).collect(); + if cur_free || has_free_aside { + let hr = unsafe { + title_reset_in(s, title_alloc as *mut c_void, |raw| { + // Verify no active list user is pinned on a backing being reset. + assert!(!busy_now.contains(&(raw as usize)), "resetting busy backing {raw:?}"); + 0 + }) + }; + assert!(matches!(hr, None | Some(0))); + } + drop(g); + std::thread::yield_now(); + } + }); + for h in handles { + h.join().unwrap(); + } + stop.store(true, Ordering::Relaxed); + resetter.join().unwrap(); + + // Check `of_list` <-> `backings[b].users` bijection and clean up all synthetic IDs. + { + let g = STATE.lock().unwrap(); + let s = g.as_ref().unwrap(); + for &l in &worker_lists { + if let Some(&b) = s.of_list.get(&l) { + assert!(all_backings.contains(&b), "list mapped to unknown backing {b:#x}"); + assert!(s.backings[&b].users.contains(&l), "missing reverse user link for list {l:#x}"); + } + } + for &b in &all_backings { + if let Some(bk) = s.backings.get(&b) { + for &u in &bk.users { + assert_eq!(s.of_list.get(&u).copied(), Some(b), "stale user link {u:#x} on backing {b:#x}"); + } + } + } + } + forget_list(pin_list); + for &l in &worker_lists { + forget_list(l); + } + { + let mut g = STATE.lock().unwrap(); + let s = g.as_mut().unwrap(); + s.aside.remove(&title_alloc); + s.cur.remove(&title_alloc); + s.types.remove(&title_alloc); + for &b in &all_backings { + s.backings.remove(&b); + } + } + } } diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 57c30e0ffe..30888fdd7e 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -96,9 +96,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_for_bind(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 ee544dec22..972d8f1bfe 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -798,11 +798,11 @@ unsafe fn try_native_depth_bias(list: *mut c_void, bias: DepthBias) -> bool { true } -/// Record a newly created command list's allocator, initial pipeline, and type. +/// Record a newly created command list's title allocator, active PC backing, initial pipeline, and type. /// /// # 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) { +pub unsafe fn on_create_with_backing(list: *mut c_void, alloc: usize, pc_alloc: usize, pso: usize, ty: D3D12_COMMAND_LIST_TYPE) { if list.is_null() { return; } @@ -824,7 +824,7 @@ 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); + super::alloc_pool::created_on_backing(key, alloc, pc_alloc, ty.0); m.insert(key, ListState { allocator: alloc, list_type: Some(ty), bound_pso: pso, ..Default::default() }); old }; @@ -843,6 +843,14 @@ pub unsafe fn on_create(list: *mut c_void, alloc: usize, pso: usize, ty: D3D12_C } } +/// Record a newly created command list's allocator, initial pipeline, and type. +/// +/// # 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); +} + /// Clean up all tracked state when a command list is released for good. pub(super) fn on_release(list: usize) { super::alloc_pool::forget_list(list); @@ -870,6 +878,7 @@ pub(super) fn on_release(list: usize) { /// Clear per-recording sync/write tracking on a split segment moved into `title_list`'s spare pool. pub(super) fn clear_segment_state(list: usize) { + super::alloc_pool::release_recording(list); super::title_textures::list_reset(list); if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&list)) { s.occlusion = Occlusion::default(); @@ -2601,7 +2610,7 @@ pub(super) unsafe fn reset_stand_in(wrapper: *mut c_void, primary: *mut c_void, None => { let pc_alloc = super::alloc_pool::current(alloc); let made = real::create_raw_pc_command_list(device_of(primary)?.as_raw(), ty, pc_alloc, 0)?; - on_create(made, pc_alloc, 0, ty); + on_create_with_backing(made, alloc, pc_alloc, 0, ty); // (closed: it is reset below like a spare) let ext = *(made as *const usize); let hr = pc:: i32>(made, PC_CLOSE)(made); @@ -2816,14 +2825,20 @@ unsafe fn split(list: *mut c_void, dispatch: Option) -> Option<*mut c_ } let next_pc = match next_pc { Some(p) => p, - None => match real::create_raw_pc_command_list(dev.as_raw(), ty, super::alloc_pool::current(alloc), init_pso) { - Some(p) => p, - None => { - static CREATE_ERR: AtomicU64 = AtomicU64::new(0); - warn(&CREATE_ERR, "mid-list split", format_args!("CreateCommandList failed")); - return None; + None => { + let pc_alloc = super::alloc_pool::current(alloc); + match real::create_raw_pc_command_list(dev.as_raw(), ty, pc_alloc, init_pso) { + Some(p) => { + super::alloc_pool::created_on_backing(p as usize, alloc, pc_alloc, ty.0); + p + } + None => { + static CREATE_ERR: AtomicU64 = AtomicU64::new(0); + warn(&CREATE_ERR, "mid-list split", format_args!("CreateCommandList failed")); + return None; + } } - }, + } }; // the next segment is a recording of its own (reset or made here, not through `cl_reset`) @@ -2966,7 +2981,12 @@ unsafe fn deferred_segment(wrapper: *mut c_void, list: *mut c_void, ty: D3D12_CO }; let p = match super::title_list::pop_spare(wrapper).filter(emptied) { Some(p) => p, - None => real::create_raw_pc_command_list(dev.as_raw(), ty, super::alloc_pool::current(own), 0)?, + None => { + let pc_alloc = super::alloc_pool::current(own); + let p = real::create_raw_pc_command_list(dev.as_raw(), ty, pc_alloc, 0)?; + super::alloc_pool::created_on_backing(p as usize, own, pc_alloc, ty.0); + p + } }; let ext = *(p as *const usize); pc:: i32>(p, PC_CLOSE)(p); @@ -7233,6 +7253,84 @@ mod tests { } } + /// A newly created mid-list split segment must be tracked on `alloc_pool`'s active backing + /// (preventing premature allocator reset) and released from `of_list` once retired to spares. + #[test] + fn mid_list_split_tracks_new_segment_in_alloc_pool_and_releases_spare_on_reset() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let Ok(factory) = CreateDXGIFactory1::() else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + let Ok(warp) = factory.EnumWarpAdapter::() else { + return crate::gpu::recomp::runner::skip_test("no GPU"); + }; + if D3D12CreateDevice(&warp, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + return crate::gpu::recomp::runner::skip_test("no GPU"); + } + } + let dev = dev.unwrap(); + let a_comp: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE).unwrap(); + let a_raw = a_comp.as_raw(); + if !real::is_adopted(a_raw) { + real::adopt(a_raw, real::Kind::CommandAllocator); + } + let raw_c: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, &a_comp, None).unwrap(); + let pc_c0 = raw_c.into_raw(); + real::adopt(pc_c0, real::Kind::CommandList); + on_create(pc_c0, a_raw as usize, 0, D3D12_COMMAND_LIST_TYPE_COMPUTE); + super::super::bind::reset(pc_c0, a_raw as usize, 0); + let wrap_c = super::super::title_list::wrap(pc_c0); + + let cell_a = Box::new(AtomicU64::new(0)); + let cell_b = Box::new(AtomicU64::new(0)); + let va_a = &*cell_a as *const AtomicU64 as u64; + let va_b = &*cell_b as *const AtomicU64 as u64; + + note_write64_recorded(pc_c0, va_a, 1); + { + let mut g = SYNC_GRAPH.lock().unwrap(); + let entry = g.get_or_insert_with(SyncGraph::default).cells.entry(va_b).or_default(); + entry.recorded_max = entry.recorded_max.max(1); + entry.recorded_list_type = Some(D3D12_COMMAND_LIST_TYPE_DIRECT); + } + wait64_x(pc_c0, va_b, D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32, 1, 0, 0); + + let mut segs = Vec::new(); + assert!(super::super::title_list::push_segments(wrap_c, &mut segs)); + assert_eq!(segs.len(), 2); + let seg1 = segs[1]; + assert_eq!(super::super::alloc_pool::recording_backing_for_test(seg1 as usize), Some(a_raw as usize)); + + // Resetting the title's allocator while `seg1` is open must swap instead of resetting `a_raw`. + a_comp.Reset().unwrap(); + assert_ne!(super::super::alloc_pool::current(a_raw as usize), a_raw as usize); + + let close_fn: unsafe extern "system" fn(*mut c_void) -> i32 = + std::mem::transmute(*(*(wrap_c as *const *const usize)).add(9)); + assert_eq!(close_fn(wrap_c), 0); + + // Resetting the wrapper retires `seg1` into spares and must release its backing pin. + let reset_fn: unsafe extern "system" fn(*mut c_void, usize, usize) -> i32 = + std::mem::transmute(*(*(wrap_c as *const *const usize)).add(10)); + assert_eq!(reset_fn(wrap_c, a_raw as usize, 0), 0); + assert_eq!(super::super::alloc_pool::recording_backing_for_test(seg1 as usize), None); + + let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = + std::mem::transmute(*(*(wrap_c as *const *const usize)).add(2)); + assert_eq!(rel_fn(wrap_c), 0); + drop(a_comp); + if let Some(gr) = SYNC_GRAPH.lock().unwrap().as_mut() { + gr.cells.remove(&va_a); + gr.cells.remove(&va_b); + } + } + } + /// A buffer outside unified memory that has a mirror is bound at its mirror again without /// asking whether the title's memory is readable: the source made unreadable after the first /// bind still binds the same mirror (before, the bind asked `readable` first and gave the diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 91085d3933..cd8dc2b1a5 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -2983,7 +2983,9 @@ unsafe extern "system" fn cl_reset(this: *mut c_void, allocator: *mut c_void, in let f: unsafe extern "system" fn(*mut c_void, *mut c_void, *mut c_void) -> HRESULT = std::mem::transmute(orig_fn(this, slot_of(this, "Reset"))); let resolved = super::pso_async::resolve_for_bind(initial_state as usize) as *mut c_void; - let hr = f(this, allocator, resolved); + let hr = super::alloc_pool::reset_list(this as usize, allocator as usize, -1, |pc_alloc| { + f(this, pc_alloc as *mut c_void, resolved) + }); preserve_vtable(this, ext); if hr >= 0 { super::list_x::on_pc_reset(this);