diff --git a/crates/xcl/src/hle/d3d12x/null.rs b/crates/xcl/src/hle/d3d12x/null.rs index 39334047f6..86760b711d 100644 --- a/crates/xcl/src/hle/d3d12x/null.rs +++ b/crates/xcl/src/hle/d3d12x/null.rs @@ -150,7 +150,10 @@ fn vtable(kind: Kind) -> *const usize { v[17] = Queue_GetClockCalibration as usize; v[18] = Hidden_ZeroDesc16 as usize; // GetDesc } - Kind::CommandAllocator => v[8] = Ret_S_OK as usize, // Reset + Kind::CommandAllocator => { + v[8] = Ret_S_OK as usize; // Reset + v[9] = super::real::alloc_memory_callback_x as usize; + } Kind::CommandList => { v[8] = Ret_Zero as usize; // GetType → DIRECT v[9] = Ret_S_OK as usize; // Close @@ -780,6 +783,12 @@ mod tests { } } + /// The null device's allocators take the title's command-memory callback (#9) as the real ones do. + #[test] + fn allocator_9_is_the_memory_callback_slot() { + assert_eq!(unsafe { *vtable(Kind::CommandAllocator).add(9) }, super::super::real::alloc_memory_callback_x as usize); + } + #[test] fn device_queue_fence_resource_roundtrip() { unsafe { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 014dfa4d95..a4794efa44 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -886,6 +886,7 @@ fn console_extensions(kind: Kind) -> Option { /// * device #76: `CreateCommittedResource` (heap properties, flags, desc, state, clear value, /// riid, ppv) as on PC #27 (PC #76 = CreateCommittedResource3); the console resource desc /// carries more after the 52 PC bytes (amd_fidelityfx_upscaler_xbox.dll writes 3 at +52). +/// * allocator #9: the title registers its command-memory callback (`alloc_memory_callback_x`). /// * descriptor heap #9 / #10: the heap start handles come back in rax on the console. /// * resource #11 GetGPUVirtualAddress: console buffers live in unified memory; their console /// address (= CPU address, low 32 bits = offset in `mem`) is what console code gets. @@ -958,6 +959,7 @@ fn console_slots(kind: Kind) -> Vec<(usize, usize)> { Kind::DescriptorHeap => vec![(9, heap_cpu_start_x as usize), (10, heap_gpu_start_x as usize)], Kind::Resource => vec![(11, res_gpu_va_x as usize)], Kind::Fence => vec![(12, super::fence_x::completed_value_address as usize)], + Kind::CommandAllocator => vec![(9, alloc_memory_callback_x as usize)], // the title's calls from #14 on: Signal / Wait / … / SignalX / PresentX at the slots of // its runtime's layout (`queue_dispatch`) Kind::CommandQueue => QUEUE_SLOTS.iter().enumerate().map(|(k, &f)| (QUEUE_FIRST + k, f as usize)).collect(), @@ -1037,6 +1039,19 @@ fn console_slots(kind: Kind) -> Vec<(usize, usize)> { } } +/// allocator #9 (callback, context): the title gives the allocator a function that hands it +/// command memory. The retail title calls it each time it takes an allocator from its pool, +/// and ignores the result; its callback takes a size and an alignment (4th and 5th arguments) +/// and the context (7th) and returns a block of the title's memory. A PC allocator takes its +/// memory from the driver, so the callback is kept nowhere and never called. +pub(super) unsafe extern "system" fn alloc_memory_callback_x(_this: *mut c_void, callback: usize, context: usize) -> HRESULT { + static LOGGED: AtomicBool = AtomicBool::new(false); + if !LOGGED.swap(true, Ordering::Relaxed) { + crate::xlog_info!("D3D12.X: allocator #9 – the title registers a command-memory callback ({callback:#x}, context {context:#x}); PC allocators take their memory from the driver, so it is not called"); + } + S_OK +} + /// descriptor heap #9 / #10 GetCPU / GetGPUDescriptorHandleForHeapStart: console code /// takes the handle in rax (only `this` passed); the PC methods return it through a hidden /// pointer @@ -6704,6 +6719,28 @@ mod srv_tests { } } +#[cfg(test)] +mod alloc_memory_callback_tests { + use super::*; + + static CALLED: AtomicBool = AtomicBool::new(false); + + unsafe extern "system" fn callback() -> usize { + CALLED.store(true, Ordering::Relaxed); + 0 + } + + /// Allocator #9 takes the title's callback and context, answers S_OK and calls nothing. + #[test] + fn allocator_9_accepts_the_callback_without_calling_it() { + assert_eq!(console_slots(Kind::CommandAllocator), vec![(9, alloc_memory_callback_x as usize)]); + for _ in 0..2 { + assert_eq!(unsafe { alloc_memory_callback_x(std::ptr::null_mut(), callback as usize, 0x1234) }, S_OK); + } + assert!(!CALLED.load(Ordering::Relaxed)); + } +} + #[cfg(test)] mod console_format_query_tests { use super::*;