diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 2fe0c9e12e..49477083d7 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -3973,33 +3973,68 @@ fn publish_download(download: &ReadyDownload) -> Result { Ok(true) } +/// Coalesce contiguous changed 4 KiB page slices in `old`/`new` anchored at `at`, +/// returning `(start_offset, end_offset)` runs within `0..new.len()`. +fn changed_page_runs(at: usize, old: &[u8], new: &[u8]) -> Vec<(usize, usize)> { + let mut runs: Vec<(usize, usize)> = Vec::new(); + let mut offset = 0; + while offset < new.len() { + let page_rem = 4096 - (at.wrapping_add(offset) & 4095); + let end = offset.saturating_add(page_rem).min(new.len()); + if old[offset..end] != new[offset..end] { + if let Some(last) = runs.last_mut() { + if last.1 == offset { + last.1 = end; + offset = end; + continue; + } + } + runs.push((offset, end)); + } + offset = end; + } + runs +} + /// Write changed 4 KiB pages without faulting if the title concurrently unmaps one. Every /// changed page must be committed and writable; otherwise its fence remains hidden and retries. +/// Contiguous changed pages within a VM region are coalesced into one `WriteProcessMemory` call +/// so publishing large GPU-written textures does not pay a per-4 KiB syscall and instruction-cache +/// flush while holding `TEXTURES`. unsafe fn write_changed_pages(at: usize, old: &[u8], new: &[u8]) -> bool { use windows_sys::Win32::System::Diagnostics::Debug::WriteProcessMemory; use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_GUARD}; use windows_sys::Win32::System::Threading::GetCurrentProcess; if old.len() != new.len() { return false } - let mut offset = 0; - while offset < new.len() { - let end = (((at + offset) & !4095) + 4096).saturating_sub(at).min(new.len()); - if old[offset..end] == new[offset..end] { offset = end; continue } - let address = at + offset; - let mut info: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); - if VirtualQuery(address as *const c_void, &mut info, std::mem::size_of_val(&info)) == 0 - || info.State != MEM_COMMIT - || info.Protect & 0xCC == 0 - || info.Protect & PAGE_GUARD != 0 - || (info.BaseAddress as usize).saturating_add(info.RegionSize) < at + end - { - return false; - } - let mut written = 0; - if WriteProcessMemory(GetCurrentProcess(), address as *const c_void, new[offset..end].as_ptr() as *const c_void, end - offset, &mut written) == 0 - || written != end - offset { - return false; + let proc = GetCurrentProcess(); + let mut region_end = 0usize; + for (mut offset, run_end) in changed_page_runs(at, old, new) { + while offset < run_end { + let Some(address) = at.checked_add(offset) else { return false }; + if address >= region_end { + let mut info: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); + if VirtualQuery(address as *const c_void, &mut info, std::mem::size_of_val(&info)) == 0 + || info.State != MEM_COMMIT + || info.Protect & 0xCC == 0 + || info.Protect & PAGE_GUARD != 0 + { + return false; + } + region_end = (info.BaseAddress as usize).saturating_add(info.RegionSize); + if region_end <= address { + return false; + } + } + let chunk_end = run_end.min(region_end.saturating_sub(at)); + let len = chunk_end - offset; + let mut written = 0; + if WriteProcessMemory(proc, address as *const c_void, new[offset..chunk_end].as_ptr() as *const c_void, len, &mut written) == 0 + || written != len + { + return false; + } + offset = chunk_end; } - offset = end; } true } @@ -7167,4 +7202,82 @@ pub(super) mod tests { ON_QUEUE.lock().unwrap().remove(&first_key); ON_QUEUE.lock().unwrap().remove(&second_key); } + + #[test] + fn changed_page_runs_coalesces_contiguous_and_skips_unchanged() { + assert!(changed_page_runs(0x10000, &[], &[]).is_empty()); + let old = vec![0u8; 4 << 12]; + assert!(changed_page_runs(0x10000, &old, &old).is_empty()); + + // Pages 0, 1, 3 changed -> two coalesced runs: [0..8192) and [12288..16384). + let mut new = old.clone(); + new[10] = 1; + new[(1 << 12) + 5] = 2; + new[(3 << 12) + 99] = 3; + assert_eq!(changed_page_runs(0x10000, &old, &new), vec![(0, 2 << 12), (3 << 12, 4 << 12)]); + + // Unaligned base (0x10200): first page slice is 0xE00 bytes, second is 0x1000, third is 0x200. + let old_u = vec![0u8; 2 << 12]; + let mut new_u = old_u.clone(); + new_u[0] = 0xAA; + new_u[0xE00] = 0xBB; + assert_eq!(changed_page_runs(0x10200, &old_u, &new_u), vec![(0, 0x1E00)]); + new_u[0] = 0; + assert_eq!(changed_page_runs(0x10200, &old_u, &new_u), vec![(0xE00, 0x1E00)]); + } + + #[test] + fn write_changed_pages_coalesces_writes_and_respects_vm_protections() { + use windows_sys::Win32::System::Memory::{ + GetWriteWatch, ResetWriteWatch, VirtualAlloc, VirtualFree, VirtualProtect, VirtualQuery, + MEMORY_BASIC_INFORMATION, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, + PAGE_GUARD, PAGE_READONLY, PAGE_READWRITE, + }; + const MEM_WRITE_WATCH: u32 = 0x0020_0000; + unsafe { + let pages = 4usize; + let len = pages << 12; + let base = VirtualAlloc(std::ptr::null(), len, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE) as usize; + assert_ne!(base, 0); + std::ptr::write_bytes(base as *mut u8, 0x11, len); + assert_eq!(ResetWriteWatch(base as *const c_void, len), 0); + + let old = vec![0x11u8; len]; + let mut new = old.clone(); + // Modify pages 0, 1, and 3; leave page 2 untouched. + new[0] = 0x22; + new[1 << 12] = 0x33; + new[3 << 12] = 0x44; + assert!(write_changed_pages(base, &old, &new)); + + let got = std::slice::from_raw_parts(base as *const u8, len); + assert_eq!(got, new.as_slice()); + + let mut dirty = [std::ptr::null_mut::(); 8]; + let mut count = dirty.len(); + let mut gran = 0u32; + assert_eq!(GetWriteWatch(0, base as *const c_void, len, dirty.as_mut_ptr(), &mut count, &mut gran), 0); + let mut addrs: Vec = dirty[..count].iter().map(|&p| p as usize).collect(); + addrs.sort_unstable(); + assert_eq!(addrs, vec![base, base + (1 << 12), base + (3 << 12)], "unchanged page 2 must not be dirtied"); + + // A read-only or guard page inside a changed run must fail closed without clearing PAGE_GUARD. + let mut prev = 0; + assert_ne!(VirtualProtect((base + (1 << 12)) as *const c_void, 1 << 12, PAGE_READONLY, &mut prev), 0); + assert_ne!(VirtualProtect((base + (2 << 12)) as *const c_void, 1 << 12, PAGE_READWRITE | PAGE_GUARD, &mut prev), 0); + let mut new2 = new.clone(); + new2[0] = 0x55; + new2[1 << 12] = 0x66; + assert!(!write_changed_pages(base, &new, &new2), "read-only page in changed run must fail closed"); + + let mut new3 = new.clone(); + new3[2 << 12] = 0x77; + assert!(!write_changed_pages(base, &new, &new3), "guard page in changed run must fail closed"); + let mut m: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); + VirtualQuery((base + (2 << 12)) as *const c_void, &mut m, std::mem::size_of_val(&m)); + assert_ne!(m.Protect & PAGE_GUARD, 0, "guard page must remain armed"); + + VirtualFree(base as *mut c_void, 0, MEM_RELEASE); + } + } }