diff --git a/README.md b/README.md index 81fccca878..a72e06ec72 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ XCL works only on unpacked game files you already own. It does not download, dec | **Ray tracing** | ✅ | BLAS / TLAS built on the GPU by the game's own XboxBvhBuilder; RDNA2 BVH node format; BVH intersection instructions emulated in shaders | GPU traversal = brute force reference; the game's builder output checked on the GPU | | **Upscaling** | ✅ | the game's AMD FSR 4 console DLL (`amd_fidelityfx_upscaler_xbox.dll`): context, the 29 pipelines in the console driver's own container (FSPX) translated, a frame's 20 passes recorded and run on the GPU | end-to-end with the real DLL: a 1280×720 → 1920×1080 frame on an RX 9070, the input's gradient in the output | +If command-list Reset fails, its earlier recording keeps holding its original command allocator backing. + ### Media and tools | Feature | State | What it does | Verified by | diff --git a/crates/xcl/src/hle/d3d12x/alloc_pool.rs b/crates/xcl/src/hle/d3d12x/alloc_pool.rs index 4815766b33..dc13bc8d6f 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -72,20 +72,26 @@ fn move_list(s: &mut State, list: usize, backing: usize) { s.backings.entry(backing).or_default().users.insert(list); } -/// `list` (of type `ty`; negative: not known) is reset on the title's allocator `alloc`: the -/// PC allocator to give the PC list. -pub fn for_list(list: usize, alloc: usize, ty: i32) -> usize { - if !enabled() || alloc == 0 { - return alloc; +/// 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 { + let backing = current(alloc); + let hr = reset(backing); + if hr < 0 || !enabled() || alloc == 0 { + return hr; } let mut g = STATE.lock().unwrap(); let s = g.get_or_insert_with(State::default); if ty >= 0 { s.types.insert(alloc, ty); } - let backing = s.cur.get(&alloc).copied().unwrap_or(alloc); move_list(s, list, backing); - backing + hr +} + +#[cfg(test)] +pub(super) fn recording_backing_for_test(list: usize) -> Option { + STATE.lock().unwrap().as_ref()?.of_list.get(&list).copied() } /// The PC allocator the title's allocator `alloc` stands for now. @@ -283,10 +289,11 @@ mod tests { assert!(dev.GetDeviceRemovedReason().is_ok(), "the recording was still there"); // the list is reset on the title's allocator: it records on the PC allocator in use - let pc = for_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0); - assert_eq!(pc, second); let second_alloc = ID3D12CommandAllocator::from_raw_borrowed(&(second as *mut c_void)).unwrap().clone(); - list.Reset(&second_alloc, None).unwrap(); + assert_eq!(reset_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |pc| { + assert_eq!(pc, second); + list.Reset(&second_alloc, None).map_or_else(|e| e.code().0, |_| 0) + }), 0); list.Close().unwrap(); queue.ExecuteCommandLists(&lists); executed(queue.as_raw(), &[l]); @@ -299,8 +306,10 @@ mod tests { // the title's allocator goes while another PC allocator stands for it: nothing of // it stays (the next allocator at its address is its own) - list.Reset(&alloc, None).unwrap(); - for_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + assert_eq!(reset_list(l as usize, a as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0, |pc| { + assert_eq!(pc, a as usize); + list.Reset(&alloc, None).map_or_else(|e| e.code().0, |_| 0) + }), 0); list.Close().unwrap(); alloc.Reset().unwrap(); assert_ne!(current(a as usize), a as usize); diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 3778fbc570..52ecc81ecf 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2293,8 +2293,9 @@ unsafe fn try_split_for_wait(list: *mut c_void) -> Option<*mut c_void> { let mut next_pc: Option<*mut c_void> = None; if let Some(spare) = super::title_list::pop_spare(wrapper) { let ext_spare = *(spare as *const usize); - let pc_alloc = super::alloc_pool::for_list(spare as usize, alloc, ty.0); - let reset_hr = pc:: i32>(spare, PC_RESET)(spare, pc_alloc, init_pso); + let reset_hr = super::alloc_pool::reset_list(spare as usize, alloc, ty.0, |pc_alloc| { + pc:: i32>(spare, PC_RESET)(spare, pc_alloc, init_pso) + }); real::preserve_vtable(spare, ext_spare); if reset_hr >= 0 { next_pc = Some(spare); @@ -2833,8 +2834,9 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let ext_closed = *(target as *const usize); let init_pso = super::pso_async::resolve_for_bind(bound_pso); - let pc_alloc = super::alloc_pool::for_list(target as usize, alloc, list_type(target).0); - let reset_hr = pc:: i32>(target, PC_RESET)(target, pc_alloc, init_pso); + let reset_hr = super::alloc_pool::reset_list(target as usize, alloc, list_type(target).0, |pc_alloc| { + pc:: i32>(target, PC_RESET)(target, pc_alloc, init_pso) + }); real::preserve_vtable(target, ext_closed); if reset_hr < 0 { return warn(&ERR, "KickoffX", format_args!("Reset after kickoff failed ({reset_hr:#x})")); @@ -3383,8 +3385,9 @@ pub unsafe extern "system" fn reset_x(this: *mut c_void, alloc: usize, pso: usiz let resolved = super::pso_async::resolve_for_bind(pso); // (the PC allocator the title's allocator stands for now: `alloc_pool`) let ty = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(this as usize))).and_then(|s| s.list_type).map_or(-1, |t| t.0); - let pc_alloc = super::alloc_pool::for_list(this as usize, alloc, ty); - let hr = pc:: i32>(this, PC_RESET)(this, pc_alloc, resolved); + let hr = super::alloc_pool::reset_list(this as usize, alloc, ty, |pc_alloc| { + pc:: i32>(this, PC_RESET)(this, pc_alloc, resolved) + }); real::preserve_vtable(this, ext); if hr < 0 { return hr; @@ -3886,7 +3889,7 @@ mod tests { let list = object.as_mut_ptr() as *mut c_void; let key = list as usize; - unsafe { on_create(list, 0x100, 0, D3D12_COMMAND_LIST_TYPE_DIRECT) }; + unsafe { on_create(list, 0x111, 0, D3D12_COMMAND_LIST_TYPE_DIRECT) }; { let mut g = LISTS.lock().unwrap(); let s = g.as_mut().unwrap().get_mut(&key).unwrap(); @@ -3909,6 +3912,7 @@ mod tests { RESET_TEST_HR.store(0x8000_4005, Ordering::SeqCst); assert!(unsafe { reset_x(list, 0x333, 0x555) } < 0); + let failed_backing = super::super::alloc_pool::recording_backing_for_test(key); assert_eq!(object[0], ext); { let g = LISTS.lock().unwrap(); @@ -3929,6 +3933,7 @@ mod tests { RESET_TEST_HR.store(0, Ordering::SeqCst); assert_eq!(unsafe { reset_x(list, 0x333, 0x555) }, 0); + let successful_backing = super::super::alloc_pool::recording_backing_for_test(key); assert_eq!(object[0], ext); { let g = LISTS.lock().unwrap(); @@ -3945,6 +3950,12 @@ mod tests { LISTS.lock().unwrap().as_mut().unwrap().remove(&key); super::super::bind::remove_state_for_test(list); + unsafe { + super::super::alloc_pool::forget(0x111); + super::super::alloc_pool::forget(0x333); + } + assert_eq!(failed_backing, Some(0x111), "failed Reset must keep holding the old recording's allocator"); + assert_eq!(successful_backing, Some(0x333), "only successful Reset moves the recording to its new allocator"); } #[test]