From 3e10052cbe1df6aceba600da2d31cc71ffc75ec4 Mon Sep 17 00:00:00 2001 From: Marvis Date: Wed, 25 Nov 2026 03:12:31 +0100 Subject: [PATCH] GPU: D3D12 objects freed after a submission are released on a thread of their own Releasing a resource is a kernel call. In run perf1 (intro cutscene) ~5 % of the title's submitting thread went into releases inside the submission: resources retired while the GPU ran, host copies whose last holder was the submission's own bookkeeping, and (diagnostics) a read-back buffer made and freed at every present for the black-frame probe. Retired resources and the submission's batch, copy-back list and heap list now go to a `victory-release` thread once the submission has run (nothing in flight uses them); the probe keeps one buffer (presents are waited for one by one). Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/src/capture.rs | 35 ++++++++++++++--------- crates/victory-gpu/src/descriptor/host.rs | 5 ++++ crates/victory-gpu/src/host.rs | 25 ++++++++++++++-- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/crates/victory-gpu/src/capture.rs b/crates/victory-gpu/src/capture.rs index cd56afd..4e5d228 100644 --- a/crates/victory-gpu/src/capture.rs +++ b/crates/victory-gpu/src/capture.rs @@ -747,20 +747,29 @@ pub(crate) fn probe( Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() }; - let mut buffer: Option = None; - // SAFETY: plain creation; an open list; locations naming live resources. - unsafe { - device - .CreateCommittedResource( - &heap, - D3D12_HEAP_FLAG_NONE, - &desc, - D3D12_RESOURCE_STATE_COPY_DEST, - None, - &mut buffer, - ) + // One buffer for every present: each is waited for before the next + // (made and released per present, it cost a kernel call per frame). + static BUFFER: std::sync::OnceLock> = std::sync::OnceLock::new(); + let buffer = BUFFER + .get_or_init(|| { + let mut buffer: Option = None; + // SAFETY: plain creation. + unsafe { + device.CreateCommittedResource( + &heap, + D3D12_HEAP_FLAG_NONE, + &desc, + D3D12_RESOURCE_STATE_COPY_DEST, + None, + &mut buffer, + ) + } .ok()?; - let buffer = buffer?; + buffer + }) + .clone()?; + // SAFETY: an open list; locations naming live resources. + unsafe { for i in 0..GRID * GRID { let (x, y) = ( (2 * (i % GRID) + 1) * d.Width as u32 / (2 * GRID), diff --git a/crates/victory-gpu/src/descriptor/host.rs b/crates/victory-gpu/src/descriptor/host.rs index 8b1867a..aa93ec9 100644 --- a/crates/victory-gpu/src/descriptor/host.rs +++ b/crates/victory-gpu/src/descriptor/host.rs @@ -1525,6 +1525,11 @@ mod tests { heap.make_null(slot); assert!(references(&buffer) >= 2, "the slot's, retired"); crate::host::release_retired(); + // Released on a thread of its own. + let since = std::time::Instant::now(); + while references(&buffer) > 1 && since.elapsed() < std::time::Duration::from_secs(5) { + std::thread::yield_now(); + } assert_eq!(references(&buffer), 1, "ours"); } let a = heap.dynamic(heap.dynamic - 100).unwrap(); diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index c64e18d..357ef40 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -1092,8 +1092,10 @@ impl Host { what.write(at as *mut u8, bytes); } } - // The submission has run and the next waits for our lock. + // The submission has run and the next waits for our lock. Host + // copies the title gave up meanwhile may have their last holder here. release_retired(); + release_later(Box::new((batch, back, withheld, heaps))); self.log_messages(); // A listed frame: what this submission's lists drew, as it is now // (the dumps submit lists of their own). @@ -2801,7 +2803,26 @@ pub(crate) fn retire(resource: ID3D12Resource) { /// After a submission has run, with none running: the retired resources go. pub(crate) fn release_retired() { let retired = std::mem::take(&mut *RETIRED.lock().unwrap_or_else(|e| e.into_inner())); - drop(retired); + if !retired.is_empty() { + release_later(Box::new(retired)); + } +} + +/// Drops `what` on a thread of its own: releasing a D3D12 resource is a +/// kernel call (~5 % of a world frame on the title's submitting thread, +/// run perf1). Only for objects no GPU work in flight uses. +fn release_later(what: Box) { + type Release = std::sync::mpsc::Sender>; + static RELEASE: OnceLock = OnceLock::new(); + let release = RELEASE.get_or_init(|| { + let (tx, rx) = std::sync::mpsc::channel::>(); + // Without the thread the receiver is gone: sends fail and drop here. + let _ = std::thread::Builder::new() + .name("victory-release".into()) + .spawn(move || rx.into_iter().for_each(drop)); + tx + }); + let _ = release.send(what); } /// Diagnostics: a noted command running a shader `VICTORY_TRACE_SHADERS`