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
77 changes: 71 additions & 6 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3191,6 +3191,15 @@ fn page_span(a: usize, n: usize) -> (usize, usize) {
(a & !(PAGE - 1), a.saturating_add(n).saturating_add(PAGE - 1) & !(PAGE - 1))
}

/// A window's own tile `t` (`src == 0`) mapped with `holes`: into `runs` (watched) only once it
/// has a CPU page. One with video memory and none (`holes == 0xFFFF`) takes no CPU write, and
/// write-watch would only scan it; its first commit adds it (`commit_range_with_protection`).
fn watch_own_tile(runs: &mut BTreeMap<usize, usize>, t: usize, holes: u16) {
if holes != 0xFFFF {
early_add(runs, t, t + TILE);
}
}

/// Add the pages `[a, b)` to `early` (ranges as start -> end), merged with the ranges they overlap
/// or touch.
fn early_add(early: &mut BTreeMap<usize, usize>, mut a: usize, mut b: usize) {
Expand Down Expand Up @@ -3772,8 +3781,8 @@ impl State {
if self.parked.contains_key(&t) || self.kept.contains_key(&t) {
self.ensure_mapped(t, TILE, |_| (0xFFFF, 0));
} else {
// (not watched before its first CPU page: `commit_range_with_protection`)
self.mapped.insert(t, TileEntry::with_holes(LAZY, 0xFFFF));
early_add(&mut self.runs, t, t + TILE);
new = true;
}
}
Expand Down Expand Up @@ -3857,7 +3866,7 @@ impl State {
let mut entry = TileEntry::with_holes(tile, holes);
entry.not_rw = not_rw;
self.mapped.insert(t, entry);
early_add(&mut self.runs, t, t + TILE);
watch_own_tile(&mut self.runs, t, holes);
self.to_zero.insert((t, TILE));
unparked = true;
} else {
Expand All @@ -3866,7 +3875,7 @@ impl State {
let mut entry = TileEntry::with_holes(tile, holes);
entry.not_rw = not_rw;
self.mapped.insert(t, entry);
early_add(&mut self.runs, t, t + TILE);
watch_own_tile(&mut self.runs, t, holes);
if reused {
self.to_zero.insert((t, TILE));
}
Expand Down Expand Up @@ -5783,7 +5792,7 @@ impl State {
*e = TileEntry::with_holes(e.pool, 0xFFFF);
self.to_zero.insert((t, TILE));
self.mirrored -= 1;
early_add(&mut self.runs, t, t + TILE);
// (watched once the pages below are committed in it)
MAPPED_GEN.fetch_add(1, Ordering::Release);
}
}
Expand All @@ -5800,10 +5809,12 @@ impl State {
}
let mut pg = a & !(PAGE - 1);
let end = a.saturating_add(n);
let mut first_pages = false;
while pg < end {
let t = pg & !(TILE - 1);
let tile_end = t.saturating_add(TILE).min(end);
if let Some(entry) = self.mapped.get_mut(&t) {
let had_none = entry.src == 0 && entry.holes == 0xFFFF;
let mut p = pg;
while p < tile_end {
let bit = 1u16 << ((p - t) / PAGE);
Expand All @@ -5815,12 +5826,20 @@ impl State {
}
p += PAGE;
}
// its first CPU page: watched from now on
if had_none && entry.holes != 0xFFFF {
early_add(&mut self.runs, t, t + TILE);
first_pages = true;
}
}
if tile_end <= pg {
break;
}
pg = tile_end;
}
if first_pages {
MAPPED_GEN.fetch_add(1, Ordering::Release);
}
self.mapping_epoch = self.mapping_epoch.wrapping_add(1);
}

Expand Down Expand Up @@ -8458,9 +8477,9 @@ mod tests {
}

/// The runs of the tiles mapped, made from all of them (but those over a CPU view: their
/// own addresses are no CPU memory).
/// own addresses are no CPU memory; and those with no CPU page, `watch_own_tile`).
fn runs_of(s: &State) -> BTreeMap<usize, usize> {
let mut tiles: Vec<usize> = s.mapped.iter().filter(|(_, e)| e.src == 0).map(|(&t, _)| t).collect();
let mut tiles: Vec<usize> = s.mapped.iter().filter(|(_, e)| e.src == 0 && e.holes != 0xFFFF).map(|(&t, _)| t).collect();
tiles.sort_unstable();
let mut runs: Vec<(usize, usize)> = Vec::new();
for t in tiles {
Expand Down Expand Up @@ -11343,6 +11362,52 @@ mod tests {
drop(lazy);
}

/// A window's own tile is watched only from its first CPU page: one listed or mapped with
/// none (video memory only) is left out of the watched runs (#2754).
#[test]
fn a_tile_with_no_cpu_page_is_not_watched() {
let mut runs = BTreeMap::new();
watch_own_tile(&mut runs, 0x1_0000_0000, 0xFFFF);
assert!(runs.is_empty(), "video memory only: not watched");
watch_own_tile(&mut runs, 0x1_0000_0000, 0xFFFE);
assert_eq!(runs, BTreeMap::from([(0x1_0000_0000, 0x1_0000_0000 + TILE)]));
}

/// (`a_tile_with_no_cpu_page_is_not_watched` through the state's commits, buffers and lists)
#[test]
fn a_tile_with_no_cpu_page_is_watched_from_its_first_commit() {
unsafe {
let Some((base, mut s, _queue)) = lazy_state(2) else { return crate::gpu::recomp::runner::skip_test("no GPU with tiled resources tier 2") };
// (`lazy_state` committed the CPU pages: a placed buffer finds a tile with pages, and
// one decommitted again has none)
assert!(VirtualFree(base as *mut c_void, 2 * TILE, MEM_DECOMMIT) != 0);
let lazy = Lazy::on(true);
assert!(s.mapped.is_empty() && s.runs.is_empty());
// listed (lazy) with no CPU page: not watched
s.ensure_listed(base, 2 * TILE);
assert_eq!(s.mapped[&(base + TILE)].holes, 0xFFFF);
assert_eq!(s.runs, runs_of(&s));
assert!(!s.runs.contains_key(&(base + TILE)), "a listed tile with no CPU page is not watched");
drop(lazy);
// a placed buffer maps a tile with no CPU page: not watched
s.buffer_tiles(base + TILE, TILE);
assert_eq!(s.mapped[&(base + TILE)].holes, 0xFFFF);
assert!(s.runs.is_empty() && runs_of(&s).is_empty(), "video memory only: nothing to watch");
// its first CPU page: watched, and readers of the runs see it changed
let gen = mapped_gen();
assert!(!VirtualAlloc((base + TILE + PAGE) as *const c_void, PAGE, MEM_COMMIT, PAGE_READWRITE).is_null());
s.commit_range(base + TILE + PAGE, PAGE);
assert_eq!(s.mapped[&(base + TILE)].holes, 0xFFFF & !(1 << 1));
assert_eq!(s.runs, BTreeMap::from([(base + TILE, base + 2 * TILE)]));
assert_eq!(s.runs, runs_of(&s));
assert!(mapped_gen() > gen, "the first CPU page changes the watched runs");
let gen = mapped_gen();
assert!(!VirtualAlloc((base + TILE + 2 * PAGE) as *const c_void, PAGE, MEM_COMMIT, PAGE_READWRITE).is_null());
s.commit_range(base + TILE + 2 * PAGE, PAGE);
assert_eq!(mapped_gen(), gen, "another page of a watched tile changes nothing watched");
}
}

/// Bytes that found no video memory at an upload are owed and go up whole once there is some.
#[test]
fn bytes_that_found_no_memory_go_up_once_there_is_some() {
Expand Down
Loading
Loading