GPU: pipelines off the render thread and cached; indirect execution on
With indirect execution the world's first frame asked for over 1,200 pipelines at once and took more than 30 s, so the title's own GPU watchdog ended the run. Host pipelines are now made by worker threads: a first use waits at most 0.5 s per title frame, and that pipeline's draws are dropped until it is ready. The world presents frames again, and indirect execution is the default (the switch is gone). Pipelines are cached across runs: - every host pipeline goes into a pipeline library saved under the cache folder (named by a hash of the translated code, our root signature and the state; a blob the device rejects starts an empty library); - the pipelines drawn with are remembered and started during loading on the next run. One cold and one warm run: the last pipeline was ready 38 s after the world's first frame cold, 1.4 s warm; the first world frame took 3.2 s cold, 0.37 s warm. Also: - Copy-backs of shader-written ranges return only the bytes the GPU changed; they used to overwrite what the title's CPU wrote meanwhile, which crashed the title. - Indirect dispatch records are bounded (group counts were not), and the console's single-dispatch argument type runs as a dispatch. - Per-shader counts of dropped draws in the diagnostics log; a switch that submits and times each list alone. Not working yet: the world shows no geometry, and runs end on a GPU page fault after at most about 55 s. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
1 parent
97c78d1087
commit
35b054ec03
13 files changed
+1018
-101
No files matched your search
@@ -40,6 +40,39 @@ pub(crate) struct Shadow {
|
||||
/// Ranges shaders of a submission not yet copied back may be writing:
|
||||
/// title memory there is older than the copy.
|
||||
pending: Mutex<Vec<(usize, usize)>>,
|
||||
/// The copy's bytes before the submission ran, from an offset on,
|
||||
/// covering the ranges its shaders write: what the GPU wrote differs.
|
||||
before: Mutex<Option<(usize, Vec<u8>)>>,
|
||||
}
|
||||
|
||||
/// Snapshot memory kept for reuse (already touched, so no page faults).
|
||||
static POOL: Mutex<Vec<Vec<u8>>> = Mutex::new(Vec::new());
|
||||
|
||||
/// An empty buffer with room for `n` bytes, from the pool if it has one.
|
||||
fn pooled(n: usize) -> Vec<u8> {
|
||||
let mut pool = POOL.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let fit = pool
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, v)| v.capacity() >= n)
|
||||
.min_by_key(|(_, v)| v.capacity())
|
||||
.map(|(i, _)| i);
|
||||
let mut v = match fit {
|
||||
Some(i) => pool.swap_remove(i),
|
||||
None => Vec::with_capacity(n),
|
||||
};
|
||||
v.clear();
|
||||
v
|
||||
}
|
||||
|
||||
/// Returns a snapshot buffer to the pool (at most 8 kept, the largest).
|
||||
fn give_back(v: Vec<u8>) {
|
||||
let mut pool = POOL.lock().unwrap_or_else(|e| e.into_inner());
|
||||
pool.push(v);
|
||||
if pool.len() > 8 {
|
||||
pool.sort_by_key(|v| std::cmp::Reverse(v.capacity()));
|
||||
pool.truncate(8);
|
||||
}
|
||||
}
|
||||
|
||||
const NOT_WATCHED: u8 = 2;
|
||||
@@ -84,72 +117,145 @@ impl Shadow {
|
||||
copied: Mutex::new(vec![0; pages as usize]),
|
||||
watched: AtomicU8::new(0),
|
||||
pending: Mutex::default(),
|
||||
before: Mutex::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// `[address, address + len)` cut to the buffer, as offsets in the copy.
|
||||
fn inside(&self, address: usize, len: usize) -> (usize, usize) {
|
||||
let (start, end) = (self.start as usize, (self.start + self.len) as usize);
|
||||
let from = address.clamp(start, end);
|
||||
(from - start, (address + len).clamp(from, end) - start)
|
||||
}
|
||||
|
||||
/// Shaders of `list` may write `[address, address + len)` of the copy
|
||||
/// (unordered access): after the list's submission has run, the range
|
||||
/// is copied back into title memory, where the title reads it; until
|
||||
/// then no refresh copies title memory over it. Call after the
|
||||
/// [`Shadow::refresh`] that gives the shaders the title's data.
|
||||
///
|
||||
/// shortcut: the whole range goes back after every submission that
|
||||
/// declares it; copy back only what shaders wrote (a GPU-side dirty
|
||||
/// mask) if big unordered-access buffers make it slow.
|
||||
/// shortcut: what shaders wrote is found by keeping the range's bytes
|
||||
/// before the submission (a copy per declared range and submission);
|
||||
/// a GPU-side dirty mask would save that if big ranges make it slow.
|
||||
pub(crate) fn gpu_writes(self: &Arc<Self>, list: &HostList, address: u64, len: u64) {
|
||||
let len = len.min(self.left(address));
|
||||
list.defer_gpu_write(self, address as usize, len as usize);
|
||||
}
|
||||
|
||||
/// At submission: a submitted list's shaders write this range.
|
||||
/// At submission, after the range's refresh: a submitted list's shaders
|
||||
/// write this range. Its bytes are kept to tell later what the GPU wrote:
|
||||
/// one snapshot per copy and submission, grown to cover every range the
|
||||
/// submission declares (the world declares overlapping views of its
|
||||
/// 174 MB heap dozens of times), in reused memory (fresh snapshots cost
|
||||
/// ~2.3 million page faults per world frame).
|
||||
pub(crate) fn writing(&self, address: usize, len: usize) {
|
||||
self.pending
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.push((address, address + len));
|
||||
let (from, to) = self.inside(address, len);
|
||||
// SAFETY: inside the mapping.
|
||||
let host = |a: usize, b: usize| unsafe { std::slice::from_raw_parts(self.data.add(a), b - a) };
|
||||
let mut before = self.before.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let (start, end) = match &*before {
|
||||
Some((at, bytes)) => ((*at).min(from), (at + bytes.len()).max(to)),
|
||||
None => (from, to),
|
||||
};
|
||||
if let Some((at, bytes)) = &*before
|
||||
&& (*at, at + bytes.len()) == (start, end)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Parts newly covered from the copy, the rest as it was taken.
|
||||
let mut grown = pooled(end - start);
|
||||
match before.take() {
|
||||
Some((at, bytes)) => {
|
||||
grown.extend_from_slice(host(start, at));
|
||||
grown.extend_from_slice(&bytes);
|
||||
grown.extend_from_slice(host(at + bytes.len(), end));
|
||||
give_back(bytes);
|
||||
}
|
||||
None => grown.extend_from_slice(host(start, end)),
|
||||
}
|
||||
*before = Some((start, grown));
|
||||
}
|
||||
|
||||
/// After the submission ran: copies what its shaders may have written
|
||||
/// back into title memory (unmapped pages left alone). Pages wholly
|
||||
/// written are current in this copy, so our own write is not taken for
|
||||
/// the title's.
|
||||
/// Returns the bytes written.
|
||||
/// After the submission ran: copies the bytes its shaders wrote back
|
||||
/// into title memory (unmapped pages left alone), and only those: the
|
||||
/// title's threads write the same buffers while the GPU runs (its
|
||||
/// command memory shares the big GPU heap with what shaders write), and
|
||||
/// putting back the copy's older bytes there broke the title's own
|
||||
/// records (a fence list read as -1, title+0x4e9b54a). Returns the bytes
|
||||
/// written.
|
||||
pub(crate) fn copy_back(&self, address: usize, len: usize) -> usize {
|
||||
let mut snapshot = self.before.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut written = 0;
|
||||
let (start, end) = (self.start as usize, (self.start + self.len) as usize);
|
||||
let (from, to) = (address.max(start), (address + len).min(end));
|
||||
if from < to {
|
||||
// Only the pieces that differ: shaders rewrite part of what they
|
||||
// may write, and writing title memory through the kernel (and
|
||||
// opening guarded blocks) was the world's biggest cost (~1 s per
|
||||
// frame for 450 MB).
|
||||
let (from, to) = self.inside(address, len);
|
||||
let start = self.start as usize;
|
||||
if let Some((taken, before)) = &*snapshot
|
||||
&& *taken <= from
|
||||
&& to <= taken + before.len()
|
||||
&& from < to
|
||||
{
|
||||
let before = &before[from - taken..to - taken];
|
||||
// By 64 KiB piece: the copy compared with its bytes before the
|
||||
// submission, title memory read only where the GPU wrote.
|
||||
const PIECE: usize = 64 << 10;
|
||||
let mut title = vec![0u8; PIECE];
|
||||
let mut synced = Vec::new();
|
||||
let mut at = from;
|
||||
while at < to {
|
||||
let n = (PIECE - at % PIECE).min(to - at);
|
||||
// SAFETY: inside the mapping.
|
||||
let host = unsafe { std::slice::from_raw_parts(self.data.add(at - start), n) };
|
||||
if !crate::host::read_into(at as *const u8, &mut title[..n]) || title[..n] != *host {
|
||||
crate::watch::writing(at, n);
|
||||
crate::host::write_mapped(at as *mut u8, host.as_ptr(), n);
|
||||
written += n;
|
||||
let n = (PIECE - (start + at) % PIECE).min(to - at);
|
||||
// SAFETY: inside the mapping; only the submitting thread
|
||||
// writes it.
|
||||
let host = unsafe { std::slice::from_raw_parts_mut(self.data.add(at), n) };
|
||||
let old = &before[at - from..at - from + n];
|
||||
let title = &mut title[..n];
|
||||
if host != old && crate::host::read_into((start + at) as *const u8, title) {
|
||||
// Title memory with the GPU's bytes put in; the copy
|
||||
// takes the title's bytes in turn.
|
||||
let mut changed = false;
|
||||
for ((t, h), o) in title.iter_mut().zip(host.iter_mut()).zip(old) {
|
||||
if *h != *o && *t != *h {
|
||||
*t = *h;
|
||||
changed = true;
|
||||
}
|
||||
*h = *t;
|
||||
}
|
||||
if changed {
|
||||
crate::watch::writing(start + at, n);
|
||||
crate::host::write_mapped((start + at) as *mut u8, title.as_ptr(), n);
|
||||
written += n;
|
||||
}
|
||||
synced.push((start + at) / PAGE);
|
||||
}
|
||||
at += n;
|
||||
}
|
||||
let (first, last) = (from.div_ceil(PAGE), to / PAGE);
|
||||
// Pages made equal to title memory are current in the copy, so
|
||||
// our own write is not taken for the title's; others keep what
|
||||
// they had (a title write there shows at the next refresh).
|
||||
let (first, last) = ((start + from).div_ceil(PAGE), (start + to) / PAGE);
|
||||
if first < last
|
||||
&& !synced.is_empty()
|
||||
&& self.watched.load(Ordering::Relaxed) != NOT_WATCHED
|
||||
&& let Some((now, _)) = crate::watch::writes(first * PAGE, last * PAGE, true)
|
||||
{
|
||||
let base = start / PAGE;
|
||||
let mut copied = self.copied.lock().unwrap_or_else(|e| e.into_inner());
|
||||
copied[first - base..last - base].fill(now);
|
||||
for page in synced.into_iter().filter(|p| (first..last).contains(p)) {
|
||||
copied[page - base] = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Every submitted step that declared the range is done with it.
|
||||
// Every submitted step that declared the range is done with it (one
|
||||
// submission runs at a time), and the snapshot with the last range.
|
||||
let mut pending = self.pending.lock().unwrap_or_else(|e| e.into_inner());
|
||||
pending.retain(|&r| r != (address, address + len));
|
||||
if pending.is_empty()
|
||||
&& let Some((_, bytes)) = snapshot.take()
|
||||
{
|
||||
give_back(bytes);
|
||||
}
|
||||
written
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user