Skip to content
Open
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
23 changes: 23 additions & 0 deletions crates/xcl/src/eh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ unsafe fn bounded_cstr(p: usize, max: usize) -> String {

fn is_writable(va: usize, bytes: usize) -> bool {
use windows_sys::Win32::System::Memory::{VirtualQuery, MEMORY_BASIC_INFORMATION, MEM_COMMIT, PAGE_EXECUTE_READWRITE, PAGE_READWRITE};
// sparse tiles made read-only for the write-watch look (#582) are the title's writable
// memory: the emulated CLZERO writes them as the CPU would after the fault
crate::hle::d3d12x::sparse_cool::warm(va, bytes);
// a committed window of unified memory is read-write as a whole and is not asked about, as in
// `gpu::shader::readable`: `VirtualQuery` walks a region to its end to size it, and a title's
// memset there asked at every emulated CLZERO loop
Expand Down Expand Up @@ -974,6 +977,26 @@ pub fn start_probes() {
mod tests {
use super::*;

/// A sparse tile made read-only for the write-watch look is writable to the emulated CLZERO
/// (it is warmed first), so the title gets the zeroed line, not the illegal instruction.
#[test]
fn is_writable_warms_a_cooled_sparse_tile() {
use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE};
const TILE: usize = 0x1_0000;
unsafe {
crate::hle::d3d12x::sparse_cool::enable_on_this_thread_for_test();
let p = VirtualAlloc(std::ptr::null(), TILE, MEM_RESERVE | MEM_COMMIT | 0x0020_0000, PAGE_READWRITE) as usize;
assert_ne!(p, 0);
let mut buf = [std::ptr::null_mut::<std::ffi::c_void>(); 16];
let (mut n, mut gran) = (buf.len(), 0u32);
windows_sys::Win32::System::Memory::GetWriteWatch(1, p as *const _, TILE, buf.as_mut_ptr(), &mut n, &mut gran);
assert!(crate::hle::d3d12x::sparse_cool::cool_for_test(p));
assert!(is_writable(p + 128, 64));
std::ptr::write_volatile((p + 128) as *mut u64, 0);
VirtualFree(p as *mut std::ffi::c_void, 0, MEM_RELEASE);
}
}

#[test]
fn bounded_cstr_stops_at_nul_limit_and_unreadable_memory() {
unsafe {
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod present;
pub mod pso_async;
pub mod real;
pub mod sparse;
pub mod sparse_cool;
pub mod title_list;
pub mod title_textures;
pub mod vram;
Expand Down
30 changes: 27 additions & 3 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,7 @@ pub unsafe fn committed(a: usize, n: usize) {
if n == 0 {
return;
}
super::sparse_cool::warm(a, n);
let mut g = STATE.lock().unwrap();
let Some(s) = g.as_mut() else {
let (start, end) = page_span(a, n);
Expand All @@ -1318,6 +1319,7 @@ pub unsafe fn decommitted(a: usize, n: usize) {
if n == 0 {
return;
}
super::sparse_cool::warm(a, n);
let mut g = STATE.lock().unwrap();
let Some(s) = g.as_mut() else {
let (start, end) = page_span(a, n);
Expand Down Expand Up @@ -1433,14 +1435,17 @@ pub fn mapped_ranges() -> Vec<(usize, usize)> {
s.runs.clone().unwrap_or_default()
}

/// time spent and pages found by the write-watch scans (`refresh_once`), for the timing line
static WATCH: [AtomicU64; 2] = [AtomicU64::new(0), AtomicU64::new(0)];
/// time spent, pages found, looks made and bytes looked at by the write-watch scans
/// (`refresh_once`), for the timing line
static WATCH: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4];
/// [lock wait, chunk scan, upload] in µs, runs uploaded
static PHASES: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4];

pub fn note_watch(t: std::time::Duration, pages: usize) {
pub fn note_watch(t: std::time::Duration, pages: usize, bytes: usize) {
WATCH[0].fetch_add(t.as_micros() as u64, Ordering::Relaxed);
WATCH[1].fetch_add(pages as u64, Ordering::Relaxed);
WATCH[2].fetch_add(1, Ordering::Relaxed);
WATCH[3].fetch_add(bytes as u64, Ordering::Relaxed);
}

/// Before the title's ExecuteCommandLists on `queue`: the pages the CPU wrote (`dirty`, 4 KiB
Expand Down Expand Up @@ -1490,6 +1495,9 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page:
x = end;
}
}
super::sparse_cool::after_look(dirty, MAPPED_GEN.load(Ordering::Acquire), || {
s.mapped.iter().filter(|(_, e)| e.holes == 0).map(|(&t, _)| t).collect()
});
let t_scanned = std::time::Instant::now();
PHASES[1].fetch_add((t_scanned - t_locked).as_micros() as u64, Ordering::Relaxed);
PHASES[3].fetch_add((zero_runs.len() + dirty_runs.len()) as u64, Ordering::Relaxed);
Expand Down Expand Up @@ -1521,6 +1529,22 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page:
ms(&PHASES[2]),
PHASES[3].load(Ordering::Relaxed)
);
let looks = WATCH[2].load(Ordering::Relaxed).max(1);
let (cool, cooled, warmed, pass_ms) = super::sparse_cool::counts();
let made = super::title_textures::ranges_made();
crate::xlog_info!(
"write-watch look: {:.3} ms and {} MB a look over {} looks (ranges made again {}, filtered again {} in {} ms); sparse tiles read-only {} (cooled {}, warmed {}, cooling pass {:.3} ms a look)",
WATCH[0].load(Ordering::Relaxed) as f64 / 1000.0 / looks as f64,
WATCH[3].load(Ordering::Relaxed) / looks >> 20,
looks,
made.0,
made.1,
made.2 / 1000,
cool,
cooled,
warmed,
pass_ms / looks as f64
);
}
}
dirty_runs.clear();
Expand Down
Loading
Loading