From 29210929d8d84915975e46209c5e84066ebdc76c Mon Sep 17 00:00:00 2001 From: Marvis Date: Wed, 25 Nov 2026 09:49:41 +0100 Subject: [PATCH] HLE: writes into physical pages are reported where the pages show when the write ends DirectStorage writes streamed data into physical pages through views of its own and then tells the title views showing those pages, so that the GPU side's host copies of them take the new bytes. The list of addresses was looked up when the write started. A page window's decode (zlib or BCPack) takes milliseconds, and the title maps streamed pages into its GPU heap meanwhile (CopyPageMappingsBatchX): the remap marks the copies there, a refresh can take the page before the bytes land (zeros, or part of them), and the report at the end named only the old addresses, so the copy at the new one stayed stale with a clean tracker for good. The addresses are now looked up when the write ends (PageWindow::drop, after the copy in write_physical). An address mapped during the write is reported; one that stopped showing the page during the write is not, and needs not be: the unmap itself marked the copies there, and they read the address's new contents at their next refresh. A mapping made after the report is marked by its own remap. Test page_windows_report_where_the_pages_show_when_done: a page shown at A is mapped at B and unmapped from A while a window writes it; the drop reports B only (and write_physical the same). On the previous code it reports A. Found with the meshlet culling's copies: in title runs 5-8 % of culled batches read meshlet buffers whose host copies (4 MiB windows over the GPU heap's mapped pages) held zeros where title memory held the buffer, with the tracker clean: whole batches culled, run-dependent. Co-Authored-By: Claude Opus 5.5 --- crates/victory-hle/src/modules/xmem.rs | 54 +++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/crates/victory-hle/src/modules/xmem.rs b/crates/victory-hle/src/modules/xmem.rs index 495da28..5b515a0 100644 --- a/crates/victory-hle/src/modules/xmem.rs +++ b/crates/victory-hle/src/modules/xmem.rs @@ -441,10 +441,18 @@ fn shown_at(pages: &[usize]) -> Vec { /// view showing them (DirectStorage's page destinations). fn report_written(shown: &[usize]) { for &page in shown { + #[cfg(test)] + REPORTED.with(|r| r.borrow_mut().push(page)); drop(victory_gpu::kernel_will_write(page, PAGE)); } } +#[cfg(test)] +thread_local! { + /// The title addresses this thread's writes into physical pages reported. + static REPORTED: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; +} + static PHYSICAL: LazyLock> = LazyLock::new(|| { Mutex::new(Physical { // Page numbers are opaque to titles; 0 stays unused. @@ -622,7 +630,6 @@ pub(crate) fn write_physical(pages: &[usize], offset: usize, data: &[u8]) -> boo let Some(runs) = runs(pages, offset, data.len()) else { return false; }; - let shown = shown_at(pages.get(offset / PAGE..(offset + data.len()).div_ceil(PAGE)).unwrap_or(&[])); let (mut within, mut done) = (offset % PAGE, 0); for (slice, n) in &runs { let Some(mut view) = View::map(slice, *n) else { @@ -632,7 +639,8 @@ pub(crate) fn write_physical(pages: &[usize], offset: usize, data: &[u8]) -> boo view.bytes()[within..within + k].copy_from_slice(&data[done..done + k]); (within, done) = (0, done + k); } - report_written(&shown); + // Where the pages show once written (see `PageWindow`). + report_written(&shown_at(pages.get(offset / PAGE..(offset + data.len()).div_ceil(PAGE)).unwrap_or(&[]))); true } @@ -643,13 +651,18 @@ pub(crate) struct PageWindow { view: View, within: usize, len: usize, - /// Title views showing the pages, told when the window is done. - shown: Vec, + /// The pages (page numbers) the bytes reach: the title views showing + /// them when the window is done are told. Looked up then, not when the + /// window opens: a decode takes milliseconds, and the title may map a + /// page at another address meanwhile (its GPU heap); a copy that took + /// the page there before the bytes landed would never hear of them. An + /// address that stops showing a page meanwhile was told by the unmap. + pages: Vec, } impl Drop for PageWindow { fn drop(&mut self) { - report_written(&self.shown); + report_written(&shown_at(&self.pages)); } } @@ -674,7 +687,7 @@ pub(crate) fn page_window(pages: &[usize], offset: usize, len: usize) -> Option< view: View::map(slice, *n)?, within: offset % PAGE, len, - shown: shown_at(pages.get(offset / PAGE..(offset + len).div_ceil(PAGE)).unwrap_or(&[])), + pages: pages.get(offset / PAGE..(offset + len).div_ceil(PAGE)).unwrap_or(&[]).to_vec(), }) } @@ -1350,6 +1363,35 @@ mod tests { assert_eq!(XMemFreePhysicalPages(1, b.as_ptr()), 1); } + /// A page mapped at a second address while a window writes it is + /// reported there too when the window is done (the title maps streamed + /// pages into its GPU heap while their reads decode); an address that + /// stopped showing it meanwhile is not. + #[test] + fn page_windows_report_where_the_pages_show_when_done() { + let pages = allocate(1); + let reserve = || XMemVirtualAlloc(std::ptr::null_mut(), PAGE, MEM_RESERVE | PAGES_64K, 0, PAGE_READWRITE); + let (a, b) = (reserve(), reserve()); + assert!(!a.is_null() && !b.is_null()); + assert_eq!(XMemMapPhysicalPages(a, 1, pages.as_ptr()), a); + REPORTED.with(|r| r.borrow_mut().clear()); + let mut window = page_window(&pages, 0, 16).expect("one run"); + // Mapped at `b`, unmapped from `a`, while the bytes are written. + assert_eq!(XMemMapPhysicalPages(b, 1, pages.as_ptr()), b); + assert_eq!(XMemMapPhysicalPages(a, 1, std::ptr::null()), a); + window.bytes().fill(3); + drop(window); + assert_eq!(REPORTED.with(|r| r.borrow().clone()), [b as usize]); + // SAFETY: mapped read-write. + assert_eq!(unsafe { b.cast::().read() }, 3); + // `write_physical` the same way. + REPORTED.with(|r| r.borrow_mut().clear()); + assert!(write_physical(&pages, 0, &[4])); + assert_eq!(REPORTED.with(|r| r.borrow().clone()), [b as usize]); + assert_eq!(XMemMapPhysicalPages(b, 1, std::ptr::null()), b); + assert_eq!(XMemFreePhysicalPages(1, pages.as_ptr()), 1); + } + #[test] fn page_windows_cover_one_run_in_place() { let (a, b) = (allocate(3), allocate(1));