Skip to content
Merged
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
96 changes: 92 additions & 4 deletions crates/xcl/src/hle/d3d12x/alloc_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ struct State {
fences: HashMap<usize, (ID3D12Fence, u64)>,
made: u64,
swaps: u64,
/// PC list → its last submit (queue, fence, value after it, recording number then)
last_submit: HashMap<usize, LastSubmit>,
/// PC list → recordings so far (a Reset or creation starts the next one)
recordings: HashMap<usize, u64>,
resubmits: u64,
}

struct LastSubmit {
queue: usize,
fence: ID3D12Fence,
value: u64,
recording: u64,
}

unsafe impl Send for State {}
Expand Down Expand Up @@ -77,6 +89,8 @@ fn move_list(s: &mut State, list: usize, backing: usize) {
pub(super) fn forget_list(list: usize) {
let mut g = STATE.lock().unwrap();
let Some(s) = g.as_mut() else { return };
s.last_submit.remove(&list);
s.recordings.remove(&list);
if let Some(backing) = s.of_list.remove(&list) {
if let Some(b) = s.backings.get_mut(&backing) {
b.users.remove(&list);
Expand All @@ -98,6 +112,7 @@ pub fn reset_list(list: usize, alloc: usize, ty: i32, reset: impl FnOnce(usize)
s.types.insert(alloc, ty);
}
move_list(s, list, backing);
*s.recordings.entry(list).or_default() += 1;
hr
}

Expand All @@ -124,18 +139,52 @@ pub fn created_on(list: usize, raw: usize, ty: i32) {
let s = g.get_or_insert_with(State::default);
s.types.entry(raw).or_insert(ty);
move_list(s, list, raw);
*s.recordings.entry(list).or_default() += 1;
}

/// Diagnostic (#1452): `lists` are about to be submitted to `queue`. A PC list whose previous
/// submit the GPU has not finished must not be submitted again on the PC (debug layer id 553,
/// "the command queue fence has not advanced past previous executions of the command list");
/// a console lets a title do it. Logged (the first 8, then at powers of two) with whether it is
/// the same recording and the same queue. Every submitted PC list is seen (`executed` marks them all).
pub unsafe fn note_resubmits(queue: *mut c_void, lists: &[*mut c_void]) -> usize {
if !enabled() {
return 0;
}
let mut g = STATE.lock().unwrap();
let Some(s) = g.as_mut() else { return 0 };
let mut found = 0;
for &l in lists {
let l = l as usize;
let Some(last) = s.last_submit.get(&l) else { continue };
if last.fence.GetCompletedValue() >= last.value {
continue;
}
let same_recording = s.recordings.get(&l).copied().unwrap_or(0) == last.recording;
let same_queue = last.queue == queue as usize;
found += 1;
s.resubmits += 1;
let n = s.resubmits;
if n <= 8 || n.is_power_of_two() {
crate::xlog_warn!(
"D3D12.X: a command list is submitted while its previous submit is still on the GPU ({}, {}; #{n} so far) – the PC does not allow it (debug layer id 553)",
if same_recording { "the same recording" } else { "a new recording" },
if same_queue { "the same queue" } else { "another queue" },
);
}
}
found
}

/// `lists` were just submitted to `queue`.
pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) {
if !enabled() || queue.is_null() {
return;
}
// (every submit is marked, also of lists this module does not track – the PC lists behind
// console lists – so note_resubmits sees them all)
let mut g = STATE.lock().unwrap();
let Some(s) = g.as_mut() else { return };
if !lists.iter().any(|l| s.of_list.contains_key(&(*l as usize))) {
return;
}
let s = g.get_or_insert_with(State::default);
let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&queue) else { return };
let key = queue as usize;
if !s.fences.contains_key(&key) {
Expand All @@ -155,6 +204,8 @@ pub unsafe fn executed(queue: *mut c_void, lists: &[*mut c_void]) {
if let Some(&b) = s.of_list.get(&(l as usize)) {
s.backings.entry(b).or_default().marks.insert(key, (fence.clone(), value));
}
let recording = s.recordings.get(&(l as usize)).copied().unwrap_or(0);
s.last_submit.insert(l as usize, LastSubmit { queue: key, fence: fence.clone(), value, recording });
}
}

Expand Down Expand Up @@ -444,4 +495,41 @@ mod tests {
assert!(!STATE.lock().unwrap().as_ref().unwrap().types.contains_key(&(a as usize)));
}
}

/// A list submitted again while its previous submit is still on the GPU is counted (and
/// logged); once that submit is done, or after the list is forgotten, it is not.
#[test]
fn a_resubmit_while_the_previous_submit_runs_is_noticed() {
unsafe {
let Ok(dev) = crate::gpu::agility::create_device(None, windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0) else {
return crate::gpu::recomp::runner::skip_test("no GPU");
};
// a fence the GPU has not reached stands for a submit still running
let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap();
let mut ids = Box::new([0u8; 2]);
let (alloc, list) = (ids.as_mut_ptr() as usize, ids.as_mut_ptr() as usize + 1);
let queue = 0x10usize as *mut c_void;
created_on(list, alloc, D3D12_COMMAND_LIST_TYPE_DIRECT.0);
{
let mut g = STATE.lock().unwrap();
let s = g.as_mut().unwrap();
let recording = s.recordings[&list];
s.last_submit.insert(list, LastSubmit { queue: queue as usize, fence: fence.clone(), value: 1, recording });
}
let while_running = note_resubmits(queue, &[list as *mut c_void]);
fence.Signal(1).unwrap();
let after_done = note_resubmits(queue, &[list as *mut c_void]);
{
// running again, then the list goes: nothing is left to compare with
let mut g = STATE.lock().unwrap();
g.as_mut().unwrap().last_submit.get_mut(&list).unwrap().value = 2;
}
forget_list(list);
let after_forget = note_resubmits(queue, &[list as *mut c_void]);
forget(alloc);
assert_eq!(while_running, 1, "a submit while the previous one runs must be noticed");
assert_eq!(after_done, 0, "a finished previous submit is not a resubmit");
assert_eq!(after_forget, 0, "a forgotten list has no previous submit");
}
}
}
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6215,6 +6215,7 @@ unsafe extern "system" fn q_execute_command_lists(this: *mut c_void, n: u32, lis
}
super::list_x::note_queue_for_lists(this as usize, &mapped);
super::list_x::execute_with_waits_on(this, &mapped, |chunk| {
let _ = super::alloc_pool::note_resubmits(this, chunk);
f(this, chunk.len() as u32, chunk.as_ptr());
super::list_x::copy_scratch_submitted(this, chunk);
super::alloc_pool::executed(this, chunk);
Expand Down
Loading