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 <noreply@anthropic.com>
This commit is contained in:
1 parent
19533398f1
commit
3e10052cbe
3 files changed
+50
-15
No files matched your search
@@ -747,20 +747,29 @@ pub(crate) fn probe(
|
||||
Type: D3D12_HEAP_TYPE_READBACK,
|
||||
..Default::default()
|
||||
};
|
||||
let mut buffer: Option<ID3D12Resource> = 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<Option<ID3D12Resource>> = std::sync::OnceLock::new();
|
||||
let buffer = BUFFER
|
||||
.get_or_init(|| {
|
||||
let mut buffer: Option<ID3D12Resource> = 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),
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<dyn std::any::Any + Send>) {
|
||||
type Release = std::sync::mpsc::Sender<Box<dyn std::any::Any + Send>>;
|
||||
static RELEASE: OnceLock<Release> = OnceLock::new();
|
||||
let release = RELEASE.get_or_init(|| {
|
||||
let (tx, rx) = std::sync::mpsc::channel::<Box<dyn std::any::Any + Send>>();
|
||||
// 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`
|
||||
|
||||
Reference in new issue
Block a user