From ff3182f9e37fc51d28b83feb581a402b1975c677 Mon Sep 17 00:00:00 2001 From: nova-aryan <81568644+nova-aryan@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:34:23 +0530 Subject: [PATCH] D3D12.X: preserve busy allocators when replacement fails Propagate replacement GetDevice/CreateCommandAllocator failures instead of letting the Reset hook fall back to the original backing. Add synthetic COM coverage for failure and retry paths; execution remains pending at the user's request. Fixes #989. --- README.md | 2 + crates/xcl/src/hle/d3d12x/alloc_pool.rs | 92 ++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81fccca87..482bda13f 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 a busy command allocator needs a replacement and its creation fails, Reset returns the error and keeps the existing recordings. + ### 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 4815766b3..21e62392d 100644 --- a/crates/xcl/src/hle/d3d12x/alloc_pool.rs +++ b/crates/xcl/src/hle/d3d12x/alloc_pool.rs @@ -209,8 +209,16 @@ pub unsafe fn title_reset(alloc: *mut c_void) -> Option { None => { let this = ID3D12CommandAllocator::from_raw_borrowed(&alloc)?; let mut dev: Option = None; - this.GetDevice(&mut dev).ok()?; - let made: ID3D12CommandAllocator = dev?.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE(ty)).ok()?; + // None lets alloc_reset reset the original allocator, whose recordings are still + // held here. A failed replacement must instead fail Reset and keep that backing. + if let Err(e) = this.GetDevice(&mut dev) { + return Some(e.code().0); + } + let Some(dev) = dev else { return Some(crate::util::E_FAIL) }; + let made: ID3D12CommandAllocator = match dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE(ty)) { + Ok(made) => made, + Err(e) => return Some(e.code().0), + }; let raw = made.into_raw(); // (made through the title's device it is adopted already) if !super::real::is_adopted(raw) { @@ -238,6 +246,86 @@ pub unsafe fn title_reset(alloc: *mut c_void) -> Option { mod tests { use super::*; + /// Failure to get a replacement must not tell alloc_reset to reset a busy allocator. + /// Mock COM objects inject the failures without allocating a GPU resource or exhausting RAM. + #[test] + fn a_failed_replacement_keeps_the_busy_allocator() { + use std::cell::Cell; + use std::mem::{offset_of, size_of}; + use windows::core::GUID; + + #[repr(C)] + struct Device { + vtable: *const usize, + references: Cell, + creates: Cell, + } + #[repr(C)] + struct Allocator { + vtable: *const usize, + device: *mut Device, + error: HRESULT, + } + unsafe extern "system" fn get_device(this: *mut c_void, _iid: *const GUID, out: *mut *mut c_void) -> HRESULT { + let a = &*this.cast::(); + *out = std::ptr::null_mut(); + if a.error < 0 { return a.error; } + let d = &*a.device; + d.references.set(d.references.get() + 1); + *out = a.device.cast(); + 0 + } + unsafe extern "system" fn create(this: *mut c_void, _ty: D3D12_COMMAND_LIST_TYPE, _iid: *const GUID, out: *mut *mut c_void) -> HRESULT { + let d = &*this.cast::(); + d.creates.set(d.creates.get() + 1); + *out = std::ptr::null_mut(); + crate::util::E_OUTOFMEMORY + } + unsafe extern "system" fn release(this: *mut c_void) -> u32 { + let d = &*this.cast::(); + let n = d.references.get() - 1; + d.references.set(n); + n + } + extern "system" fn unexpected_call() { std::process::abort(); } + + const SLOT: usize = size_of::(); + let mut device_vtable = [unexpected_call as usize; size_of::() / SLOT]; + device_vtable[offset_of!(ID3D12Device_Vtbl, base__.base__.Release) / SLOT] = release as usize; + device_vtable[offset_of!(ID3D12Device_Vtbl, CreateCommandAllocator) / SLOT] = create as usize; + let mut allocator_vtable = [unexpected_call as usize; size_of::() / SLOT]; + allocator_vtable[offset_of!(ID3D12CommandAllocator_Vtbl, base__.base__.GetDevice) / SLOT] = get_device as usize; + let mut device = Device { vtable: device_vtable.as_ptr(), references: Cell::new(1), creates: Cell::new(0) }; + let mut allocator = Allocator { vtable: allocator_vtable.as_ptr(), device: &mut device, error: crate::util::E_FAIL }; + let raw = std::ptr::from_mut(&mut allocator).cast::(); + let list = std::ptr::from_mut(&mut device) as usize; + created_on(list, raw as usize, D3D12_COMMAND_LIST_TYPE_DIRECT.0); + let mut results = Vec::new(); + let mut preserved = Vec::new(); + unsafe { + for error in [crate::util::E_FAIL, 0] { + allocator.error = error; + // Retry too: a failure must leave enough state to attempt a replacement again. + for _ in 0..2 { + results.push(title_reset(raw)); + let g = STATE.lock().unwrap(); + let s = g.as_ref().unwrap(); + preserved.push( + !s.cur.contains_key(&(raw as usize)) && !s.aside.contains_key(&(raw as usize)) + && s.of_list.get(&list) == Some(&(raw as usize)) + && s.backings.get(&(raw as usize)).is_some_and(|b| b.users.contains(&list)) + ); + } + } + // Remove the stack-backed COM pointers before asserting (also on the old code). + forget(raw as usize); + } + assert_eq!(results, [Some(crate::util::E_FAIL), Some(crate::util::E_FAIL), Some(crate::util::E_OUTOFMEMORY), Some(crate::util::E_OUTOFMEMORY)]); + assert!(preserved.into_iter().all(|p| p), "failed Reset must keep every recording association"); + assert_eq!(device.creates.get(), 2); + assert_eq!(device.references.get(), 1, "temporary device references must be released"); + } + /// A list recorded once and submitted again after the title reset its allocator: the /// recording is still there (the device is not removed), the title's allocator goes on with /// another PC allocator, and the first one is used again once the list has moved on.