Skip to content
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper.
fills into its GPU views) are applied: a GPU-view page shows the physical page mapped into it.
`XCL_PAGE_MAPPINGS=0` turns them off (the call is ignored and a GPU-view page shows its CPU view at
the same offset, as before).
- Deferred resource releases keep their objects held after device removal; the fence's
removal sentinel does not count as completed GPU work.
- Sparse tile reuse and heap retirement require valid completed queue fences. Failed retirement
signals or device removal keep the resources held instead of treating removal as completion.
- Every run writes `report.json` (called stubs, unknown D3D12.X slots and interfaces, XGameRuntime
classes asked for, shader statistics, audio formats, crash).
`d3d12.presents` counts presentations, not visible content. With `XCL_FRAME_DUMP=N` (`N > 1`),
Expand Down
41 changes: 37 additions & 4 deletions crates/xcl/src/hle/d3d12x/late_release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ struct Held {
// (the interface pointers are only used under the lock)
unsafe impl Send for Held {}

fn next_mark(value: u64) -> Option<u64> {
value.checked_add(1).filter(|&next| next != u64::MAX)
}

fn marks_completed<F>(marks: &[(F, u64)], mut completed: impl FnMut(&F) -> u64) -> bool {
marks.iter().all(|(f, value)| {
let done = completed(f);
done != u64::MAX && done >= *value
})
}

impl Held {
fn is_empty(&self) -> bool {
self.open.is_empty() && self.sealed.is_empty()
Expand All @@ -77,22 +88,26 @@ impl Held {
self.fences.insert(key, (f, 0));
}
let (fence, value) = self.fences.get_mut(&key).unwrap();
if super::real::native_queue_signal(q, &*fence, *value + 1).is_err() {
let Some(next) = next_mark(*value) else {
ok = false;
break;
};
if super::real::native_queue_signal(q, &*fence, next).is_err() {
ok = false;
break;
}
*value += 1;
*value = next;
marks.push((fence.clone(), *value));
}
// (no fence or no signal: the device is gone – the batch stays open, and held)
// Without a valid signalled mark, the batch stays open and held.
if ok {
self.last_seal = Some(Instant::now());
let dead = std::mem::take(&mut self.open);
self.sealed.push_back(Batch { dead, marks });
}
}
let mut gone = Vec::new();
while self.sealed.front().is_some_and(|b| b.marks.iter().all(|(f, v)| super::real::fence_completed(&f) >= *v)) {
while self.sealed.front().is_some_and(|b| marks_completed(&b.marks, |f| super::real::fence_completed(f))) {
gone.extend(self.sealed.pop_front().unwrap().dead);
}
gone
Expand Down Expand Up @@ -277,6 +292,24 @@ mod tests {
use super::*;
use windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC;

#[test]
fn removal_does_not_complete_a_held_batch() {
let complete = |marks: &[(u64, u64)]| marks_completed(marks, |f| *f);
assert!(complete(&[]));
assert!(complete(&[(3, 3), (8, 7)]));
assert!(!complete(&[(3, 3), (6, 7)]));
assert!(!complete(&[(3, 3), (u64::MAX, 7)]));
assert!(!complete(&[(u64::MAX, 0)]), "removal is not completion even for zero");
}

#[test]
fn late_release_marks_never_signal_the_removal_sentinel_or_wrap() {
assert_eq!(next_mark(0), Some(1));
assert_eq!(next_mark(u64::MAX - 2), Some(u64::MAX - 1));
assert_eq!(next_mark(u64::MAX - 1), None);
assert_eq!(next_mark(u64::MAX), None);
}

/// A held resource stays while a queue has not got past the work submitted before it was
/// released, and goes once the queue has.
#[test]
Expand Down
53 changes: 49 additions & 4 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,18 @@ fn retire_due(cooling: &VecDeque<((u32, u32), std::time::Instant)>, waiting: boo
(waiting || cooled(cooling, now)) && now.duration_since(tried) >= COOL
}

fn retirement_reached(done: u64, value: u64) -> bool {
done != u64::MAX && done >= value
}

fn next_retirement_value(value: u64) -> Option<u64> {
value.checked_add(1).filter(|&next| next != u64::MAX)
}

fn retirement_marks_reached<F>(marks: &[(F, u64)], mut completed: impl FnMut(&F) -> u64) -> bool {
!marks.is_empty() && marks.iter().all(|(f, v)| retirement_reached(completed(f), *v))
}

struct UploadSlot {
alloc: ID3D12CommandAllocator,
list: ID3D12GraphicsCommandList,
Expand Down Expand Up @@ -3516,6 +3528,9 @@ impl State {
if tests::NO_TILES.with(|c| c.get()) {
return None;
}
if super::real::fence_completed(&self.fence) == u64::MAX {
return None;
}
self.drain_quarantined();
if let Some(t) = reuse_tile(&mut self.free, &mut self.cooling, std::time::Instant::now()) {
return Some((t, true));
Expand Down Expand Up @@ -3572,7 +3587,10 @@ impl State {
fn retire_free_pools_with(&mut self, done: u64) {
let now = std::time::Instant::now();
self.retire_tried = now;
self.retired.retain(|(_, v, at)| !(done >= *v && now.duration_since(*at).as_secs() >= 2));
if done == u64::MAX {
return;
}
self.retired.retain(|(_, v, at)| !(retirement_reached(done, *v) && now.duration_since(*at).as_secs() >= 2));
self.free.drain_cooling(&mut self.cooling, now);
let gone: Vec<usize> = free_heaps(&self.free, &self.cooling, self.pools.len(), now).into_iter().filter(|&h| self.pools[h].is_some()).collect();
if gone.is_empty() {
Expand Down Expand Up @@ -5931,8 +5949,19 @@ impl State {
if tiles.is_empty() {
return;
}
let mut marks = initial_quarantine_marks((self.fence.clone(), self.value), retiring, blocked).unwrap_or_default();
let mut marks = Vec::new();
let mut ok = !blocked;
if ok {
// Establish a checked mark after the unmaps. An earlier unchecked signal cannot
// authorize reuse, and a removed fence's UINT64_MAX is never normal completion.
match next_retirement_value(self.value) {
Some(next) if super::real::native_queue_signal(&self.queue, &self.fence, next).is_ok() => {
self.value = next;
marks = initial_quarantine_marks((self.fence.clone(), next), retiring, blocked).unwrap_or_default();
}
_ => ok = false,
}
}
let mut seen = Vec::with_capacity(queues.len());
marks.reserve(queues.len());
for q in queues.iter().filter(|_| ok) {
Expand All @@ -5955,7 +5984,10 @@ impl State {
}
},
};
let next = entry.1 + 1;
let Some(next) = next_retirement_value(entry.1) else {
ok = false;
break;
};
if super::real::native_queue_signal(&q, &entry.0, next).is_ok() {
entry.1 = next;
marks.push((entry.0.clone(), next));
Expand All @@ -5972,7 +6004,7 @@ impl State {
unsafe fn drain_quarantined_with(&mut self, mut completed: impl FnMut(&ID3D12Fence) -> u64) -> bool {
let mut freed = false;
while let Some(front) = self.quarantined.front() {
let ready = front.marks.as_ref().is_some_and(|marks| marks.iter().all(|(f, v)| completed(f) >= *v));
let ready = front.marks.as_ref().is_some_and(|marks| retirement_marks_reached(marks, &mut completed));
if !ready {
break;
}
Expand Down Expand Up @@ -8439,6 +8471,19 @@ fn forget_in(s: &mut State, res: usize) {
mod tests {
use super::*;

#[test]
fn sparse_retirement_requires_real_completion_on_every_queue() {
let reached = |marks: &[(u64, u64)]| retirement_marks_reached(marks, |f| *f);
assert!(!reached(&[]), "the sparse queue needs its own signalled mark");
assert!(reached(&[(3, 3), (8, 7)]));
assert!(!reached(&[(3, 3), (6, 7)]));
assert!(!reached(&[(3, 3), (u64::MAX, 7)]));
assert!(!retirement_reached(u64::MAX, 0), "removal never releases a retired heap");
assert_eq!(next_retirement_value(u64::MAX - 2), Some(u64::MAX - 1));
assert_eq!(next_retirement_value(u64::MAX - 1), None);
assert_eq!(next_retirement_value(u64::MAX), None);
}

thread_local! {
/// The VirtualQuery calls `source_holes` made on this thread.
pub(super) static VIRTUAL_QUERIES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
Expand Down
Loading