Skip to content
Merged
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
62 changes: 52 additions & 10 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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;

Expand Down
Loading