From 03912e8de71cd43700fc3fa4857fd51fa2a0e552 Mon Sep 17 00:00:00 2001 From: Enrique Villa Date: Tue, 24 Nov 2026 10:58:59 +0800 Subject: [PATCH] D3D12.X: a buffer with a mirror is bound again without VirtualQuery gpu_va asked gpu::shader::readable() on every bind of a buffer outside unified memory before it looked for the buffer's mirror. On a title's memory that is not resident, readable() falls back to a VirtualQuery walk of the whole region: in a retail title past its account prompt the render thread spent 97 % in NtQueryVirtualMemory, 70.9 % of it via IASetVertexBuffers -> gpu_va -> readable and 7.8 % via IASetIndexBuffer. A range that has a mirror was readable when the mirror was made, and sync_title_buffers checks readable() itself before each copy, so the bind now looks the mirror up first and asks readable() only for the first bind of a range. The window check stays first, so a PC resource's GPU address that falls in a window by number is still passed through. AI-assisted. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/list_x.rs | 42 ++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index fb17d4d2d..dcfb4a9a9 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -3059,7 +3059,16 @@ unsafe fn gpu_va(list: *mut c_void, va: u64, bytes: usize, what: &str, mirror: b return t; } // in a window by number: a PC resource's GPU address - if crate::gpu::uma::get().is_none_or(|u| u.contains(va as usize)) || !crate::gpu::shader::readable(va as usize, bytes) { + if crate::gpu::uma::get().is_none_or(|u| u.contains(va as usize)) { + return t; + } + // a buffer bound before has its mirror: no `readable` (a `VirtualQuery` walk of the title's + // memory when it is not resident – most of a retail title's render thread at a bind per + // draw); `sync_title_buffers` checks what it reads + if let Some(p) = mirror.then(|| mirrored(va, bytes)).flatten() { + return pc_va(p as u64); + } + if !crate::gpu::shader::readable(va as usize, bytes) { return t; } if mirror { @@ -5810,4 +5819,35 @@ mod tests { } } } + + /// 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 + /// title's address back, which the PC's GPU has no address for). + #[test] + fn a_mirrored_buffer_binds_its_mirror_without_asking_readable() { + use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualProtect, MEM_COMMIT, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE}; + if crate::gpu::uma::get().is_none() { + crate::gpu::recomp::runner::skip_test("no unified memory"); + return; + } + unsafe { + let len = 0x1_0000usize; + let src = VirtualAlloc(std::ptr::null(), len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE) as *mut u8; + assert!(!src.is_null()); + for k in 0..len { + *src.add(k) = k as u8; + } + let (va, bytes) = (src as u64 + 0x100, 0x400usize); + let first = gpu_va(std::ptr::null_mut(), va, bytes, "test vertex buffer", true); + assert_ne!(first, va, "the first bind makes a mirror"); + assert!(mirrored(va, bytes).is_some()); + let mut old = 0u32; + assert_ne!(VirtualProtect(src as *const _, len, PAGE_NOACCESS, &mut old), 0); + assert!(!crate::gpu::shader::readable(va as usize, bytes)); + assert_eq!(gpu_va(std::ptr::null_mut(), va, bytes, "test vertex buffer", true), first); + // the range stays reserved (and unreadable): the mirror table is never emptied, so its + // address must not come back for another test's memory + } + } }