Skip to content
Draft
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ next steps:
console upscaler runs a whole frame on a PC GPU, the GDK HTTP library initialises.
- **Console textures decode exactly** – the GFX10 tiling and mip layout reproduce 909 of 912
textures of the game's texture archive byte for byte, its volume textures included.
Texture uploads can read the paired CPU view of a GPU-only 64 KiB page reservation, including
when the CPU view is in a sparse window. A texture registered in a paired view follows its
new CPU backing if the title releases and reuses the GPU address.
- **370+ automated tests**, including GPU tests that compare translated shaders against expected
results instruction by instruction.

Expand Down
16 changes: 12 additions & 4 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,8 @@ fn mirror_source(va: usize) -> usize {
if mapped { va } else { src }
}

/// Physical pages were mapped or unmapped in a GPU view since the last look: textures there
/// whose source changed (a page now mapped under one, or gone from it) read from the new one,
/// uploaded again at this submit.
/// A GPU view's backing reservation or physical page mappings changed since the last look.
/// Textures whose source changed read from the new one and are uploaded again at this submit.
fn recheck_gpu_view_sources() {
static SEEN: AtomicU64 = AtomicU64::new(0);
let gen = crate::hle::kernelx::gpu_view_map_gen();
Expand Down Expand Up @@ -1228,7 +1227,8 @@ mod tests {
}
// Larger than all four UMA windows together. These public allocations
// must use the watched Windows fallback, reserving space rather than RAM.
const SIZE: usize = 24usize << 30;
// Distinct from the 24 GiB reservation in the parallel kernelx bookkeeping test.
const SIZE: usize = (24usize << 30) + PAGE;
const OFFSET: usize = 0x2_0000;
const PAGE: usize = 0x1_0000;
struct Reservation(usize);
Expand Down Expand Up @@ -1345,6 +1345,14 @@ mod tests {
refresh(queue.as_raw());
assert_eq!(read_r8(dev, &queue, &texture), vec![0x9A; 64 * 64], "unmapping restores the earlier CPU view");
assert_eq!(address(key), Some(va));
// Keep the texture alive while its GPU address loses and regains its CPU view.
kernelx::mirrors::released(gpu.0);
recheck_gpu_view_sources();
assert_eq!(TEXTURES.lock().unwrap().by_res[&key].src, va);
kernelx::mirrors::reserved(gpu.0, SIZE, 0x2040_2000, 0x801);
recheck_gpu_view_sources();
let src_after_reuse = TEXTURES.lock().unwrap().by_res[&key].src;
assert_eq!(src_after_reuse, src);
dev.GetDeviceRemovedReason().unwrap();
}
}
Expand Down
19 changes: 14 additions & 5 deletions crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ pub mod mirrors {
let taken: Vec<usize> = s.gpu.values().map(|&(_, c)| c).collect();
if let Some(&(cpu, _)) = s.cpu.iter().rev().find(|&&(b, n)| n == size && !taken.contains(&b)) {
s.gpu.insert(base, (size, cpu));
super::GPU_VIEW_MAP_GEN.fetch_add(1, std::sync::atomic::Ordering::Release);
crate::xlog_info!("kernelx: reservation {base:#x} ({size:#x} bytes, no CPU access) is the GPU view of {cpu:#x}");
}
} else if p & 0xFF != super::mem::PAGE_NOACCESS {
Expand All @@ -203,8 +204,11 @@ pub mod mirrors {
pub fn released(base: usize) {
let mut s = STATE.lock().unwrap();
s.cpu.retain(|&(b, _)| b != base);
s.gpu.remove(&base);
s.gpu.retain(|_, &mut (_, c)| c != base);
let paired = s.gpu.len();
s.gpu.retain(|&g, &mut (_, c)| g != base && c != base);
if s.gpu.len() != paired {
super::GPU_VIEW_MAP_GEN.fetch_add(1, std::sync::atomic::Ordering::Release);
}
}

/// The CPU-view address behind `va` when `va` lies in a GPU view.
Expand Down Expand Up @@ -251,6 +255,7 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t:
uma.map(|u| u.sparse_release(q));
return std::ptr::null_mut();
}
mirrors::reserved(q, size, t, p);
trace_mem("VirtualAlloc (64K pages, sparse)", 0, size, t as u64, p, q);
return q as *mut c_void;
}
Expand Down Expand Up @@ -284,17 +289,20 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t:
uma.and_then(|u| u.sparse_reserve(size))
};
if let Some(q) = reserved {
mirrors::reserved(q, size, t, p);
trace_mem("VirtualAlloc (graphics reserve, sparse)", 0, size, t as u64, p, q);
return q as *mut c_void;
}
if let Some(q) = uma.and_then(|u| u.reserve(size, 0x1_0000)) {
mirrors::reserved(q as usize, size, t, p);
trace_mem("VirtualAlloc (graphics reserve, UMA)", 0, size, t as u64, p, q as usize);
return q as *mut c_void;
}
let q = mem::VirtualAlloc(addr, size, mem::MEM_RESERVE | MEM_WRITE_WATCH, sanitize_protect(p));
if !q.is_null() {
GRAPHICS_RESERVATIONS.lock().unwrap().insert(q as usize, size);
GRAPHICS_RESERVATIONS_GEN.fetch_add(1, Ordering::Release);
mirrors::reserved(q as usize, size, t, p);
return q;
}
}
Expand Down Expand Up @@ -429,12 +437,14 @@ pub unsafe extern "system" fn VirtualFree(addr: *mut c_void, size: usize, t: u32
}
}
if release {
mirrors::released(a);
u.sparse_release(a);
}
return 1;
}
if let Some(u) = crate::gpu::uma::get().filter(|u| u.contains(addr as usize)) {
if t & 0x8000 != 0 {
mirrors::released(addr as usize);
return u.free(addr as usize) as BOOL;
}
if t & 0x4000 != 0 && size > 0 {
Expand Down Expand Up @@ -819,11 +829,10 @@ pub fn physical_page_at(va: usize) -> bool {
AWAY.lock().unwrap().as_ref().is_some_and(|a| a.at.contains_key(&(va & !(PHYS_PAGE - 1))))
}

/// Counts the calls that map or unmap physical pages in a GPU view (`mirrors`): what
/// `physical_page_at` says there may have changed.
/// Changes when a GPU view gains or loses its backing reservation or physical page mappings.
static GPU_VIEW_MAP_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Generation of the physical-page mappings inside GPU views.
/// Generation of the backing memory seen by textures in GPU views.
pub fn gpu_view_map_gen() -> u64 {
GPU_VIEW_MAP_GEN.load(Ordering::Acquire)
}
Expand Down
95 changes: 95 additions & 0 deletions crates/xcl/tests/sparse_mirrors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use windows_sys::Win32::System::Memory::{MEM_DECOMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE};
use xcl::hle::kernelx::{gpu_view_map_gen, in_graphics_reservation, mirrors, VirtualAlloc, VirtualFree};

// Only these tests run in this process and change the generation.
static TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

#[test]
fn reusing_a_gpu_view_with_new_backing_notifies_its_textures() {
let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
// Bookkeeping only. Supply the same GPU address on both reservations.
let (first, second, gpu, size) = (0x10_0000, 0x20_0000, 0x30_0000, 0x2_0000);
let reserve = |base, p| mirrors::reserved(base, size, MEM_RESERVE | 0x2040_0000, p);
reserve(first, PAGE_READWRITE);
reserve(gpu, PAGE_NOACCESS | 0x800);
assert_eq!(mirrors::source(gpu + 65536), Some(first + 65536));
mirrors::released(gpu);
mirrors::released(first);
let unpaired = gpu_view_map_gen();
assert_eq!(mirrors::source(gpu + 65536), None);
reserve(second, PAGE_READWRITE);
assert_eq!(gpu_view_map_gen(), unpaired);
reserve(gpu, PAGE_NOACCESS | 0x800);
assert_eq!(mirrors::source(gpu + 65536), Some(second + 65536));
assert_eq!(gpu_view_map_gen(), unpaired + 1, "notify a surviving texture of the new backing");
mirrors::released(second);
mirrors::released(gpu);
}

#[test]
fn sparse_64k_views_pair_and_forget_released_addresses() {
let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let Some(uma) = xcl::gpu::uma::get().filter(|u| u.has_sparse()) else {
xcl::gpu::recomp::runner::skip_test("no sparse UMA address space");
return;
};
let size = 3 * 65536;
let reserve = |size, protection| {
let p = unsafe { VirtualAlloc(std::ptr::null(), size, MEM_RESERVE | 0x2040_0000, protection) };
assert!(!p.is_null(), "failed to reserve {size:#x} bytes with protection {protection:#x}");
p
};
let gpu_protection = PAGE_NOACCESS | 0x800; // PAGE_GRAPHICS_NOACCESS
let before = gpu_view_map_gen();
let cpu = reserve(size, PAGE_READWRITE);
assert_eq!(gpu_view_map_gen(), before);
let gpu = reserve(size, gpu_protection);
assert!(uma.is_sparse(cpu as usize) && uma.is_sparse(gpu as usize));
let sample = gpu as usize + 65536;
let backing = cpu as usize + 65536;
assert_eq!(mirrors::source(sample), Some(backing));

unsafe {
let paired = gpu_view_map_gen();
assert_eq!(paired, before + 1, "notify when the sparse views first pair");
assert_eq!(VirtualFree(gpu, size, MEM_RELEASE), 0);
assert_eq!(VirtualFree(gpu, size, MEM_DECOMMIT), 1);
assert_eq!(gpu_view_map_gen(), paired);
assert_eq!(mirrors::source(sample), Some(backing));

assert_eq!(VirtualFree(gpu, 0, MEM_RELEASE), 1);
assert_eq!(mirrors::source(sample), None);
assert_eq!(gpu_view_map_gen(), paired + 1);
let replacement = reserve(size, PAGE_READWRITE);
assert_eq!(mirrors::source(replacement as usize + 65536), None);
assert_eq!(VirtualFree(replacement, 0, MEM_RELEASE), 1);
assert_eq!(gpu_view_map_gen(), paired + 1);

let gpu_again = reserve(size, gpu_protection);
let sample = gpu_again as usize + 65536;
assert_eq!(mirrors::source(sample), Some(backing));
assert_eq!(gpu_view_map_gen(), paired + 2);
let paired = gpu_view_map_gen();
assert_eq!(VirtualFree(cpu, 0, MEM_RELEASE), 1);
assert_eq!(mirrors::source(sample), None);
assert_eq!(gpu_view_map_gen(), paired + 1);
assert_eq!(VirtualFree(gpu_again, 0, MEM_RELEASE), 1);
assert_eq!(gpu_view_map_gen(), paired + 1);

// Two reservations this large exceed all sparse windows together. These flags contain
// neither MEM_GRAPHICS nor MEM_COMMIT, so there is no coherent-window fallback.
let sparse_bytes: usize = uma.watch_regions().into_iter()
.filter(|&(base, _)| uma.is_sparse(base)).map(|(_, len)| len).sum();
let size = sparse_bytes / 2 + 65536;
let cpu = reserve(size, PAGE_READWRITE);
let gpu = reserve(size, gpu_protection);
assert!(uma.is_sparse(cpu as usize));
assert!(!uma.contains(gpu as usize) && in_graphics_reservation(gpu as usize));
assert_eq!(mirrors::source(gpu as usize + 65536), Some(cpu as usize + 65536));
let paired = gpu_view_map_gen();
assert_eq!(VirtualFree(gpu, 0, MEM_RELEASE), 1);
assert_eq!(mirrors::source(gpu as usize + 65536), None);
assert_eq!(gpu_view_map_gen(), paired + 1);
assert_eq!(VirtualFree(cpu, 0, MEM_RELEASE), 1);
}
}
Loading