diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index cae13913f2..500c77fea4 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -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, 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, mut a: usize, mut b: usize) { @@ -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; } } @@ -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 { @@ -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)); } @@ -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); } } @@ -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); @@ -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); } @@ -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 { - let mut tiles: Vec = s.mapped.iter().filter(|(_, e)| e.src == 0).map(|(&t, _)| t).collect(); + let mut tiles: Vec = 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 { @@ -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() { diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index d809b1c2a2..d8d41b9a8a 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -189,10 +189,21 @@ impl Table { } } - fn link_src(&mut self, res: usize, src: usize, size: usize) { + /// An active texture's source `[src, src + size)`; `watched` (not `Entry::gpu_written`): its + /// blocks go into `coarse` too. + fn link_src(&mut self, res: usize, src: usize, size: usize, watched: bool) { for p in (src >> SRC_PIECE_SHIFT)..=((src + size.max(1) - 1) >> SRC_PIECE_SHIFT) { self.src_pieces.entry(p).or_default().push(res); } + if watched { + self.watch_src(src, size); + } + } + + /// The blocks of an active texture's source into `coarse` (write-watched). A texture the GPU + /// wrote is never uploaded from its source (`texture_needs_upload`): it counts only while + /// not `gpu_written`, and goes in and out where that changes (`rewatch`). + fn watch_src(&mut self, src: usize, size: usize) { if in_view_window(src) { return; } @@ -205,7 +216,8 @@ impl Table { } } - fn unlink_src(&mut self, res: usize, src: usize, size: usize) { + /// `link_src` undone, with the `watched` it was linked with. + fn unlink_src(&mut self, res: usize, src: usize, size: usize, watched: bool) { for p in (src >> SRC_PIECE_SHIFT)..=((src + size.max(1) - 1) >> SRC_PIECE_SHIFT) { if let Some(v) = self.src_pieces.get_mut(&p) { v.retain(|&r| r != res); @@ -214,6 +226,25 @@ impl Table { } } } + if watched { + self.unwatch_src(src, size); + } + } + + /// After `res`'s `gpu_written` may have changed (`was_watched`: it was not GPU-written + /// before): an active texture's source goes into or out of `coarse` with it. + fn rewatch(&mut self, res: usize, was_watched: bool) { + let Some(e) = self.by_res.get(&res) else { return }; + let (src, size, watched) = (e.src, e.tex.size() as usize, !e.gpu_written); + match (was_watched, watched) { + (false, true) => self.watch_src(src, size), + (true, false) => self.unwatch_src(src, size), + _ => {} + } + } + + /// `watch_src` undone. + fn unwatch_src(&mut self, src: usize, size: usize) { if in_view_window(src) { return; } @@ -294,7 +325,7 @@ impl Table { for (ova, ores, _) in self.active_overlaps(va, end) { self.by_va.remove(&(ova, ores)); if let Some(old) = self.by_res.remove(&ores) { - self.unlink_src(ores, old.src, old.tex.size() as usize); + self.unlink_src(ores, old.src, old.tex.size() as usize, !old.gpu_written); if old.pending { pending_delta -= 1; } @@ -330,7 +361,7 @@ impl Table { let mut pending_delta = 0isize; let mut to_unmap = Vec::new(); if let Some(old) = self.by_res.remove(&res) { - self.unlink_src(res, old.src, old.tex.size() as usize); + self.unlink_src(res, old.src, old.tex.size() as usize, !old.gpu_written); self.by_va.remove(&(old.va, res)); if old.pending { pending_delta -= 1; @@ -345,7 +376,7 @@ impl Table { } self.by_va.remove(&(ova, ores)); if let Some(mut old) = self.by_res.remove(&ores) { - self.unlink_src(ores, old.src, old.tex.size() as usize); + self.unlink_src(ores, old.src, old.tex.size() as usize, !old.gpu_written); if old.pending { pending_delta -= 1; } @@ -380,8 +411,8 @@ impl Table { self.pending_res.insert(res); } self.by_va.insert((va, res), end); - let (src, size) = (entry.src, entry.tex.size() as usize); - self.link_src(res, src, size); + let (src, size, watched) = (entry.src, entry.tex.size() as usize, !entry.gpu_written); + self.link_src(res, src, size, watched); self.by_res.insert(res, entry); (pending_delta, to_unmap) } @@ -398,7 +429,7 @@ impl Table { return (true, 0, Vec::new(), st); } let Some(old) = self.by_res.remove(&res) else { return (false, 0, Vec::new(), None) }; - self.unlink_src(res, old.src, old.tex.size() as usize); + self.unlink_src(res, old.src, old.tex.size() as usize, !old.gpu_written); let st = (old.states.len() == 1).then(|| old.states[0]); self.by_va.remove(&(old.va, res)); let mut pending_delta = if old.pending { -1 } else { 0 }; @@ -430,8 +461,8 @@ impl Table { pending_delta += 1; } self.by_va.insert((sva, sres), send); - let (src, size) = (e.src, e.tex.size() as usize); - self.link_src(sres, src, size); + let (src, size, watched) = (e.src, e.tex.size() as usize, !e.gpu_written); + self.link_src(sres, src, size, watched); if e.pending { self.pending_res.insert(sres); } @@ -812,22 +843,25 @@ fn recheck_gpu_view_sources() { s.extend(remapped.keys().copied()); s }); - let mut moved: Vec<(usize, usize, usize, usize, bool)> = Vec::new(); + // (res, old source, new source, size, pending, watched before, watched now): a source that + // moved, or a GPU write that gave way (`mapping_changed`), of an active texture + let mut moved: Vec<(usize, usize, usize, usize, bool, bool, bool)> = Vec::new(); let mut again: Vec = Vec::new(); let Table { by_res, shadowed, .. } = t; let mut look = |res: usize, e: &mut Entry, active: bool| { - let (old, size) = (e.src, e.tex.size() as usize); - if recheck_source(e) { + let (old, size, was_watched) = (e.src, e.tex.size() as usize, !e.gpu_written); + let (moved_src, pending) = (recheck_source(e), e.pending); + if moved_src { changed = true; - if active { - moved.push((res, old, e.src, size, e.pending)); - } } if let Some((call, parts)) = remapped.remove(&res) { if mapping_changed(e, call, parts, active) { again.push(res); } } + if active && (moved_src || was_watched != !e.gpu_written) { + moved.push((res, old, e.src, size, moved_src && pending, was_watched, !e.gpu_written)); + } }; match &names { Some(s) => { @@ -848,9 +882,9 @@ fn recheck_gpu_view_sources() { } } } - for (res, old, new, size, pending) in moved { - t.unlink_src(res, old, size); - t.link_src(res, new, size); + for (res, old, new, size, pending, was_watched, watched) in moved { + t.unlink_src(res, old, size, was_watched); + t.link_src(res, new, size, watched); if pending { t.pending_res.insert(res); } @@ -957,6 +991,8 @@ pub(super) fn publish_submission_effects(queue: Option, writes: & e.gpu_written = true; GPU_WRITTEN.fetch_add(1, Ordering::Relaxed); newly_written.push(write.res); + // (never uploaded from its source now: out of the watched blocks) + t.rewatch(write.res, true); } } } @@ -3166,10 +3202,12 @@ unsafe fn upload_queue(dev: &ID3D12Device, device: usize) -> Result Result { } if entry.gpu_writers.iter().all(Option::is_none) { entry.gpu_writers.clear(); + let was_watched = !entry.gpu_written; entry.gpu_written = false; + // (the CPU's copy is current again: its writes are watched) + table.rewatch(download.res, was_watched); } Ok(true) } @@ -5646,6 +5687,10 @@ pub(super) mod tests { let past_limit = (first.1 + WATCH_GAP + 1, first.1 + WATCH_GAP + 0x1001); assert_eq!(watch_ranges(®ions, vec![past_limit, first]), vec![(base, 0x1000), (past_limit.0, 0x1000)]); + + // (#2891) ranges 6 MiB apart are two scans: the gap is not walked + let six_away = (first.1 + (6 << 20), first.1 + (6 << 20) + 0x1000); + assert_eq!(watch_ranges(&[(base, 64 << 20)], vec![first, six_away]), vec![(base, 0x1000), (six_away.0, 0x1000)]); } #[test] @@ -6516,6 +6561,62 @@ pub(super) mod tests { assert!(t.by_res.get(&1).is_some_and(|e| e.cpu_backed && e.generation == cpu_generation)); } + /// A texture the GPU wrote is never uploaded from its source: its blocks are not watched + /// while it is `gpu_written`, and are again once the CPU's copy is current (#2754). + #[test] + fn gpu_written_textures_leave_the_watched_blocks_until_they_are_current_again() { + let tex = Arc::new(xg::Texture::new(&console_desc(3, 64, 64, 1, 1, 28, 0x105)).unwrap()); + let entry = |src: usize, gpu_written: bool| Entry { + device: 0, + committed_bytes: 0, + va: src, + src, + in_gpu_view: false, + tex: Arc::clone(&tex), + states: vec![D3D12_RESOURCE_STATE_COMMON], + pending: false, + gpu_written, + gpu_writers: Vec::new(), + cpu_backed: true, + placed: false, + unmapped: false, + generation: 0, + paged: false, + written_call: 0, + known: Default::default(), + target: false, + }; + let (a, b) = (0x4000_0000usize, 0x5000_0000usize); + let blocks = |t: &Table| t.coarse.keys().map(|&k| k << SRC_COARSE_SHIFT).collect::>(); + let mut t = Table::new(); + t.insert(1, entry(a, false)); + t.insert(2, entry(b, true)); + assert_eq!(blocks(&t), vec![a], "a GPU-written texture's source is not watched"); + assert_eq!(t.touched_by(&[b]), std::collections::BTreeSet::from([2]), "but is still found under a written page"); + + // the GPU writes the first one: its block goes + t.by_res.get_mut(&1).unwrap().gpu_written = true; + t.rewatch(1, true); + assert!(blocks(&t).is_empty()); + // downloaded (the CPU's copy current again): watched again + t.by_res.get_mut(&1).unwrap().gpu_written = false; + t.rewatch(1, false); + assert_eq!(blocks(&t), vec![a]); + t.rewatch(1, true); + assert_eq!(blocks(&t), vec![a], "no change, no count"); + + // a shadowed texture never counts; it does once it is uncovered, by its own state + t.insert(3, entry(a + 0x1000, true)); + assert!(t.shadowed.contains_key(&1) && blocks(&t).is_empty(), "the newer GPU-written one covers it"); + t.rewatch(1, true); + assert!(blocks(&t).is_empty(), "a shadowed entry is not counted"); + t.remove(3); + assert_eq!(blocks(&t), vec![a], "uncovered: watched again"); + t.remove(1); + t.remove(2); + assert!(t.coarse.is_empty() && t.src_pieces.is_empty()); + } + #[test] fn stream_recheck_schedule_catches_late_writes_and_resets_milestones() { // (STREAM_RECHECK is shared with the other stream recheck test)