Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion crates/xcl/src/hle/d3d12x/console_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ unsafe extern "system" fn cl_release(this: *mut c_void) -> u32 {
})
.unwrap_or(0);
if left == 0 {
super::title_textures::forget_list_closed(obj);
if let Some(st) = LISTS.lock().unwrap().as_mut().and_then(|m| m.remove(&obj)) {
free_uma(&st.uma);
let release: unsafe extern "system" fn(usize) -> u32 = std::mem::transmute(pc_vtable_call(st.pc, 2));
Expand Down Expand Up @@ -481,6 +482,7 @@ unsafe extern "system" fn cl_reset(this: *mut c_void, allocator: *mut c_void, ps
}) else {
return crate::util::E_FAIL;
};
super::title_textures::forget_list_closed(obj);
// what the previous recording left in unified memory lives until its allocator resets
if !blocks.is_empty() {
PENDING.lock().unwrap().get_or_insert_with(HashMap::new).entry(old_alloc).or_default().extend(blocks);
Expand Down Expand Up @@ -672,6 +674,15 @@ pub fn pc_list(obj: *mut c_void) -> Option<*mut c_void> {
with_list(obj as usize, |st| st.pc as *mut c_void)
}

/// Whether `pc` is the internal PC command list behind an active `console_list` wrapper.
pub(super) fn is_backing(pc: *mut c_void) -> bool {
if !enabled() || pc.is_null() {
return false;
}
let p = pc as usize;
LISTS.lock().unwrap().as_ref().is_some_and(|m| m.values().any(|st| st.pc == p))
}

/// The PC pipeline state behind a console-shaped one (others unchanged).
pub fn pc_pso(pso: *mut c_void) -> *mut c_void {
if pso.is_null() || unsafe { *(pso as *const usize) } != pso_vtable() {
Expand Down Expand Up @@ -785,7 +796,6 @@ unsafe fn ensure_dispatch_heaps(list: &ID3D12GraphicsCommandList, bound: &mut Ve
/// Turn the recorded console commands into commands on the PC list and close it.
pub unsafe fn close(obj: *mut c_void) -> Result<Translation, String> {
let obj = obj as usize;
super::title_textures::note_list_closed(obj);
let mut guard = LISTS.lock().unwrap();
let st = guard.as_mut().and_then(|m| m.get_mut(&obj)).ok_or("not an XCL console list")?;
if let Some(last) = st.chunks.last_mut() {
Expand Down Expand Up @@ -1059,6 +1069,7 @@ pub unsafe fn close(obj: *mut c_void) -> Result<Translation, String> {
crate::xlog_warn!("console list: unknown record {w:#010x} (word {at}) – rest skipped");
}
list.Close().map_err(|e| format!("Close: {e}"))?;
super::title_textures::note_list_closed(obj);
Ok(out)
}

Expand Down
14 changes: 13 additions & 1 deletion crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3838,7 +3838,8 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
if close_hr < 0 {
return warn(&ERR, "KickoffX", format_args!("Close failed ({close_hr:#x})"));
}
if let Some(wrapper) = super::title_list::wrapper_of(this) {
let kickoff_wrapper = super::title_list::wrapper_of(this);
if let Some(wrapper) = kickoff_wrapper {
record_deferred(wrapper);
}
super::capture::kickoff(this, queue);
Expand Down Expand Up @@ -3884,6 +3885,9 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) {
// what ran is in the textures' states; the list records anew after its Reset below
super::title_textures::submitted(&one);
super::title_textures::list_reset(this as usize);
if let Some(w) = kickoff_wrapper {
super::title_textures::forget_list_closed(w as usize);
}
if !sparse_writes.0.is_empty() || !sparse_writes.1.is_empty() {
if let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&q_raw) {
super::sparse::readback_after_execute(q, &sparse_writes.0, &sparse_writes.1);
Expand Down Expand Up @@ -4866,6 +4870,14 @@ pub(super) unsafe extern "system" fn dma_close_x(this: *mut c_void) -> i32 {
let ext = *(this as *const usize);
let hr = pc::<unsafe extern "system" fn(*mut c_void) -> i32>(this, 9)(this);
real::preserve_vtable(this, ext);
if hr >= 0
&& !super::title_textures::is_own_work()
&& !super::present::presenting()
&& super::title_list::wrapper_of(this).is_none()
&& !super::console_list::is_backing(this)
{
super::title_textures::note_list_closed(this as usize);
}
if hr < 0 {
let ty = pc::<unsafe extern "system" fn(*mut c_void) -> i32>(this, PC_GET_TYPE)(this);
crate::xlog_warn!("D3D12.X: DMA list Close → {:#x} (PC list type {ty})", hr as u32);
Expand Down
11 changes: 11 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3416,6 +3416,14 @@ unsafe extern "system" fn cl_close(this: *mut c_void) -> HRESULT {
let f: RefFn = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12GraphicsCommandList", "Close")));
let hr = f(this) as HRESULT;
preserve_vtable(this, ext);
if hr >= 0
&& !super::title_textures::is_own_work()
&& !super::present::presenting()
&& super::title_list::wrapper_of(this).is_none()
&& !super::console_list::is_backing(this)
{
super::title_textures::note_list_closed(this as usize);
}
if hr < 0 {
let ty: RefFn = std::mem::transmute(orig_fn(this, pc_slot!("ID3D12GraphicsCommandList", "GetType")));
crate::xlog_warn!("D3D12.X: command list {this:p} (type {}) Close → {:#x}", ty(this), hr as u32);
Expand All @@ -3432,6 +3440,9 @@ unsafe extern "system" fn cl_reset(this: *mut c_void, allocator: *mut c_void, in
let hr = f(this, allocator, resolved);
preserve_vtable(this, ext);
if hr >= 0 {
if !super::title_textures::is_own_work() && !super::present::presenting() {
super::title_textures::forget_list_closed(this as usize);
}
super::list_x::on_pc_reset(this);
super::dred::reset(this);
if !initial_state.is_null() {
Expand Down
70 changes: 69 additions & 1 deletion crates/xcl/src/hle/d3d12x/title_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ unsafe extern "system" fn close(this: *mut c_void) -> HRESULT {
let f: unsafe extern "system" fn(*mut c_void) -> HRESULT = std::mem::transmute(pc_slot(pc, 9));
let hr = f(pc);
super::capture::list_end(this as usize);
super::title_textures::note_list_closed(this as usize);
if hr >= 0 {
super::title_textures::note_list_closed(this as usize);
}
let n = *((this as usize + F_RING_AT) as *const usize);
if hr < 0 && n > 0 {
let first = n.saturating_sub(RING);
Expand Down Expand Up @@ -170,6 +172,7 @@ pub(super) unsafe extern "system" fn reset(this: *mut c_void, alloc: usize, pso:
f(primary, alloc, pso)
};
if hr >= 0 {
super::title_textures::forget_list_closed(this as usize);
let active = stand_in.unwrap_or(primary as usize);
*((this as usize + F_BACKING) as *mut usize) = active;
let moved = {
Expand Down Expand Up @@ -420,6 +423,7 @@ unsafe extern "system" fn release(this: *mut c_void) -> u32 {
let n = f(pc);
if n == 0 {
super::capture::forget(this as usize);
super::title_textures::forget_list_closed(this as usize);
let book = SEGMENTS.lock().unwrap().as_mut().and_then(|m| m.remove(&(this as usize)));
{
let mut wg = WRAPPER_OF.lock().unwrap();
Expand Down Expand Up @@ -740,4 +744,68 @@ mod tests {
drop(Box::from_raw(seg1));
}
}

/// End-to-end `title_list` lifecycle for `title_textures::CLOSED`:
/// failed Close does not record, successful Close records the wrapper pointer, failed Reset
/// keeps it, successful Reset clears it, non-final Release keeps it, and final Release clears
/// it before freeing the wrapper address.
#[test]
fn title_list_close_reset_and_release_maintain_closed_tracking() {
#[repr(C)]
struct LifecycleFake {
vt: *const usize,
refs: u32,
close_hr: HRESULT,
reset_hr: HRESULT,
}
unsafe extern "system" fn lf_add_ref(this: *mut c_void) -> u32 {
let f = &mut *(this as *mut LifecycleFake);
f.refs += 1;
f.refs
}
unsafe extern "system" fn lf_release(this: *mut c_void) -> u32 {
let f = &mut *(this as *mut LifecycleFake);
f.refs -= 1;
f.refs
}
unsafe extern "system" fn lf_close(this: *mut c_void) -> HRESULT {
(*(this as *mut LifecycleFake)).close_hr
}
unsafe extern "system" fn lf_reset(this: *mut c_void, _alloc: usize, _pso: usize) -> HRESULT {
(*(this as *mut LifecycleFake)).reset_hr
}
let mut vt = vec![0usize; LIST_SLOTS];
vt[1] = lf_add_ref as usize;
vt[2] = lf_release as usize;
vt[9] = lf_close as usize;
vt[10] = lf_reset as usize;
let fake = Box::into_raw(Box::new(LifecycleFake { vt: vt.as_ptr(), refs: 1, close_hr: -1, reset_hr: -1 }));
unsafe {
let w = wrap(fake as *mut c_void);
assert!(!super::super::title_textures::is_list_closed_for_test(w as usize));
// Failed Close must not mark the list closed.
assert_eq!(close(w), -1);
assert!(!super::super::title_textures::is_list_closed_for_test(w as usize));
// Successful Close marks the wrapper pointer closed.
(*fake).close_hr = 0;
assert_eq!(close(w), 0);
assert!(super::super::title_textures::is_list_closed_for_test(w as usize));
// Failed Reset leaves the old closed recording intact.
assert_eq!(reset(w, 0, 0), -1);
assert!(super::super::title_textures::is_list_closed_for_test(w as usize));
// Successful Reset clears CLOSED so the reopened list does not keep a stale timestamp.
(*fake).reset_hr = 0;
assert_eq!(reset(w, 0, 0), 0);
assert!(!super::super::title_textures::is_list_closed_for_test(w as usize));
// Re-close, AddRef, non-final Release keeps CLOSED; final Release clears it.
assert_eq!(close(w), 0);
assert!(super::super::title_textures::is_list_closed_for_test(w as usize));
assert_eq!(add_ref(w), 2);
assert_eq!(release(w), 1);
assert!(super::super::title_textures::is_list_closed_for_test(w as usize));
assert_eq!(release(w), 0);
assert!(!super::super::title_textures::is_list_closed_for_test(w as usize));
drop(Box::from_raw(fake));
}
}
}
Loading
Loading