Skip to content
Draft
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
6 changes: 5 additions & 1 deletion crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,7 @@ unsafe fn note_superseded_in_tables(u: &crate::gpu::uma::Uma, layout: Option<&La
let Some(layout) = layout else { return };
const TABLE_SLOTS: usize = 256;
let mut table = [0u8; TABLE_SLOTS * 32];
let mut named = Vec::new();
for p in &layout.params {
let Param::Table { at } = *p else { continue };
let at = at as usize;
Expand All @@ -1330,10 +1331,13 @@ unsafe fn note_superseded_in_tables(u: &crate::gpu::uma::Uma, layout: Option<&La
}
let res = super::console_heap::named_by_index(index);
if res != 0 && res != super::console_heap::GONE {
super::placements::note_use(res);
named.push(res);
}
}
}
named.sort_unstable();
named.dedup();
super::placements::note_uses(&named);
}

/// `XCL_DUMP_TARGETS`: the resources the first 16 slots of each descriptor table of `this`'s
Expand Down
91 changes: 86 additions & 5 deletions crates/xcl/src/hle/d3d12x/placements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
//! before (`late_release::hold_unmap`), and come back when the title uses it again (a barrier or a
//! copy of it: `note_use`) or releases the newer ones over it while it still holds it.

use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::ffi::c_void;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Mutex;
use std::sync::{Mutex, RwLock};
use std::time::Instant;

use windows::core::Interface;
Expand Down Expand Up @@ -186,6 +186,9 @@ struct Registry {
seq: u64,
/// the superseded ones the title holds (`SUPERSEDED`)
unmapped: usize,
/// `(res, superseded)`: changes to the superseded ones since `SUPERSEDED_IDS` last took them
/// (`sync_superseded`)
superseded_delta: Vec<(usize, bool)>,
}

/// What a placement found (`Registry::place`).
Expand Down Expand Up @@ -256,6 +259,7 @@ impl Registry {
old.under.push(res);
p.over.push(o);
self.unmapped += 1;
self.superseded_delta.push((o, true));
out.victims.push(o);
}
}
Expand All @@ -277,6 +281,7 @@ impl Registry {
if !p.mapped {
// (its tiles go with it: `vram::forget`)
self.unmapped -= 1;
self.superseded_delta.push((res, false));
}
let (over, under) = (std::mem::take(&mut p.over), std::mem::take(&mut p.under));
for n in under {
Expand Down Expand Up @@ -307,6 +312,7 @@ impl Registry {
if newer.is_empty() {
old.mapped = true;
self.unmapped -= 1;
self.superseded_delta.push((o, false));
remap.push(o);
} else {
old.under = newer;
Expand Down Expand Up @@ -337,6 +343,7 @@ impl Registry {
let Some(p) = self.live.get_mut(&res).filter(|p| !p.mapped && !p.released) else { return Vec::new() };
p.mapped = true;
self.unmapped -= 1;
self.superseded_delta.push((res, false));
let under = std::mem::take(&mut p.under);
for n in &under {
if let Some(q) = self.live.get_mut(n) {
Expand Down Expand Up @@ -482,6 +489,33 @@ unsafe fn map_again(remap: &[usize]) -> Vec<ID3D12Resource> {
held
}

/// The superseded ones the title holds (`Registry::superseded`), written under the registry's
/// lock: `note_use`, called for every barrier, copy and bound descriptor table, finds a texture is
/// not one of them under a shared lock instead of waiting for `REGISTRY`.
static SUPERSEDED_IDS: RwLock<Option<HashSet<usize>>> = RwLock::new(None);

/// `Registry::superseded_delta` into `set`.
fn drain_superseded(r: &mut Registry, set: &mut HashSet<usize>) {
for (res, superseded) in r.superseded_delta.drain(..) {
if superseded {
set.insert(res);
} else {
set.remove(&res);
}
}
}

/// `SUPERSEDED` and `SUPERSEDED_IDS` as the registry `r` has them (its lock held).
fn sync_superseded(r: &mut Registry) {
SUPERSEDED.store(r.unmapped, Ordering::Relaxed);
if !r.superseded_delta.is_empty() {
let mut g = SUPERSEDED_IDS.write().unwrap_or_else(|e| e.into_inner());
let set = g.get_or_insert_with(HashSet::new);
drain_superseded(r, set);
debug_assert_eq!(set.len(), r.unmapped, "SUPERSEDED_IDS missed a change of the registry");
}
}

/// Texture `res` placed at title address `va` on `pages` (`[start, end)`: its own title pages),
/// its tiles mapped. Returns the older targets it supersedes: their tiles are to leave the shared
/// pages once the queues are past the work submitted before (`late_release::hold_unmap`).
Expand All @@ -492,7 +526,7 @@ pub(super) fn placed(res: usize, va: usize, pages: (usize, usize), backing: Back
let mut g = REGISTRY.lock().unwrap();
let r = g.get_or_insert_with(Registry::default);
let out = r.place(res, va, pages, backing, kind, desc, Instant::now(), look, retire);
SUPERSEDED.store(r.unmapped, Ordering::Relaxed);
sync_superseded(r);
let victims: Vec<ID3D12Resource> = out.victims.iter().filter_map(|&o| unsafe { resource(o) }).collect();
let held = unsafe { map_again(&out.remap) };
(out, victims, held)
Expand All @@ -510,7 +544,7 @@ fn settle(f: impl FnOnce(&mut Registry) -> Vec<usize>) {
let mut g = REGISTRY.lock().unwrap();
let Some(r) = g.as_mut() else { return };
let remap = f(r);
SUPERSEDED.store(r.unmapped, Ordering::Relaxed);
sync_superseded(r);
unsafe { map_again(&remap) }
};
drop(held);
Expand Down Expand Up @@ -543,6 +577,22 @@ pub(super) fn any_superseded() -> bool {
SUPERSEDED.load(Ordering::Relaxed) != 0
}

/// `note_use` for each of `res` (the textures a draw's descriptor tables name), looking them up
/// under one shared lock.
pub(super) fn note_uses(res: &[usize]) {
if res.is_empty() || SUPERSEDED.load(Ordering::Relaxed) == 0 {
return;
}
let hits: Vec<usize> = {
let g = SUPERSEDED_IDS.read().unwrap_or_else(|e| e.into_inner());
let Some(set) = g.as_ref() else { return };
res.iter().copied().filter(|r| *r != 0 && set.contains(r)).collect()
};
for r in hits {
note_use(r);
}
}

/// The title uses `res` (a barrier, a copy, or a descriptor table): a superseded target goes back on the shared pages –
/// on the console the title may use both over one memory when it knows they share it. Logged
/// once for each pair (the first 64). The work recorded now waits for the mapping
Expand All @@ -551,12 +601,15 @@ pub(super) fn note_use(res: usize) {
if res == 0 || SUPERSEDED.load(Ordering::Relaxed) == 0 {
return;
}
if !SUPERSEDED_IDS.read().unwrap_or_else(|e| e.into_inner()).as_ref().is_some_and(|set| set.contains(&res)) {
return;
}
static LOGGED: Mutex<Option<std::collections::HashSet<(u64, u64)>>> = Mutex::new(None);
let held = {
let mut g = REGISTRY.lock().unwrap();
let Some(r) = g.as_mut() else { return };
let under = r.use_again(res);
SUPERSEDED.store(r.unmapped, Ordering::Relaxed);
sync_superseded(r);
if under.is_empty() {
return;
}
Expand Down Expand Up @@ -776,6 +829,34 @@ mod tests {
assert!(superseded(&r).is_empty());
}

/// `SUPERSEDED_IDS`, which `note_use` checks before it takes the registry's lock, follows
/// `Registry::superseded_delta`: through placements, uses again, releases and removals it
/// holds the superseded ones the registry counts.
#[test]
fn the_superseded_set_follows_the_registry() {
let mut r = Registry::default();
let mut set = HashSet::new();
let mut synced = |r: &mut Registry| {
drain_superseded(r, &mut set);
let mut v: Vec<usize> = set.iter().copied().collect();
v.sort_unstable();
assert_eq!(v, superseded(r));
};
let x = 0xA0_0000_0000usize;
for k in 0..3 {
retiring(&mut r, 1 + k, x + k * 4 * MIB, 4 * MIB, Kind::Target);
}
assert_eq!(retiring(&mut r, 9, x + MIB, 10 * MIB, Kind::Target).0, [1, 2, 3]);
synced(&mut r);
assert_eq!(r.use_again(1), [9]);
synced(&mut r);
assert!(r.release(2).is_empty());
synced(&mut r);
assert_eq!(r.remove(9), Some(vec![3]));
synced(&mut r);
assert!(superseded(&r).is_empty());
}

/// A superseded target the title uses again goes back on the shared pages, and the newer
/// one's release then has nothing to map again.
#[test]
Expand Down
Loading