D3D12.X: a full write-watch buffer goes on after its last page

refresh_once asked GetWriteWatch again from the range's start whenever its
16,384-entry buffer came back full. The pages already reported had been reset,
but each call walked them again. A range with many written pages therefore
cost more than one pass: 4 GiB with every page written took 105 ms a look,
and 47 ms when each call starts after the last page reported.

take_written does the look for one range and continues from there.

Test: written_pages_past_a_full_buffer_are_all_found_once. A 64-entry buffer
over 1,000 pages, with 857 and 128 pages written: each page is found once, in
order, and the look resets them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kalmarv
2026-11-23 17:05:18 -05:00
parent 85bdf2193e
commit d6cbfcd31c
+52 -10
View File
@@ -382,6 +382,26 @@ pub(super) unsafe fn refresh(queue: *mut c_void) {
own_work(|| refresh_once(queue));
}
/// The pages of `[base, base + size)` written since the last look, added to `dirty` and reset
/// (`page`: their size). A full `buf` goes on after the last page it reported: starting again at
/// `base` walked the pages already reset each time (4 GiB with every page written: 105 ms a
/// look, 47 ms going on).
unsafe fn take_written(base: usize, size: usize, buf: &mut [*mut c_void], dirty: &mut Vec<usize>, page: &mut u32) {
let end = base + size;
let mut at = base;
while at < end {
let mut n = buf.len();
if GetWriteWatch(WRITE_WATCH_FLAG_RESET, at as *const c_void, end - at, buf.as_mut_ptr(), &mut n, page) != 0 {
break;
}
dirty.extend(buf[..n].iter().map(|&p| p as usize));
if n == 0 || n < buf.len() {
break;
}
at = buf[n - 1] as usize + *page as usize;
}
}
unsafe fn refresh_once(queue: *mut c_void) {
let Some(u) = crate::gpu::uma::get() else { return };
let sparse = u.has_sparse();
@@ -401,16 +421,7 @@ unsafe fn refresh_once(queue: *mut c_void) {
buf.resize(16384, std::ptr::null_mut());
}
for &(base, size) in ranges.iter() {
loop {
let mut n = buf.len();
if GetWriteWatch(WRITE_WATCH_FLAG_RESET, base as *const c_void, size, buf.as_mut_ptr(), &mut n, &mut page) != 0 {
break;
}
dirty.extend(buf[..n].iter().map(|&p| p as usize));
if n < buf.len() {
break;
}
}
take_written(base, size, &mut buf, &mut dirty, &mut page);
}
});
dirty.sort_unstable();
@@ -787,6 +798,37 @@ mod tests {
}
}
/// More written pages than one call reports: each is found once, in order, and the look
/// resets them – a buffer that fills in the middle of the range and one that fills exactly.
#[test]
fn written_pages_past_a_full_buffer_are_all_found_once() {
use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE};
const MEM_WRITE_WATCH: u32 = 0x20_0000;
const PAGES: usize = 1000;
const PAGE: usize = 0x1000;
unsafe {
let base = VirtualAlloc(std::ptr::null(), PAGES * PAGE, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE) as usize;
assert!(base != 0);
let mut buf = vec![std::ptr::null_mut::<c_void>(); 64];
let (mut dirty, mut page) = (Vec::new(), 0u32);
take_written(base, PAGES * PAGE, &mut buf, &mut dirty, &mut page);
for written in [(0..PAGES).filter(|i| i % 7 != 3).collect::<Vec<_>>(), (0..128).map(|i| i * 5).collect()] {
let want: Vec<usize> = written.iter().map(|i| base + i * PAGE).collect();
for &a in &want {
(a as *mut u8).write_volatile(1);
}
dirty.clear();
take_written(base, PAGES * PAGE, &mut buf, &mut dirty, &mut page);
assert_eq!(page as usize, PAGE);
assert_eq!(dirty, want, "{} pages written", want.len());
dirty.clear();
take_written(base, PAGES * PAGE, &mut buf, &mut dirty, &mut page);
assert!(dirty.is_empty(), "the look reset what it reported");
}
VirtualFree(base as *mut c_void, 0, MEM_RELEASE);
}
}
const SRV: D3D12_RESOURCE_STATES = D3D12_RESOURCE_STATES(D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE.0 | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE.0);
const ALL: u32 = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;