From 23d1aa8dfa38db7f468991d2850db0dfacbe79c7 Mon Sep 17 00:00:00 2001 From: upipibi Date: Tue, 24 Nov 2026 07:45:50 -0600 Subject: [PATCH] D3D12.X: a wait after a write into title memory in the same list is met by that write `wait_memory` drops a Wait32/64BitValueX on an address the same list already wrote (`has_prior_write`): the GPU reaches the write first, and polling the address before the list is submitted would only time out. The two writers that go to the CPU instead (`cpu_writes`: a Write32/64BitValueX into the title's own memory outside the unified-memory windows, and a timestamp there) did not record their range, so a wait on it later in the list stayed: it was polled before the list was submitted, while the CPU makes the write only after the submit, and it ran into the wait timeout every time. Both now record the range (`note_write`) as the GPU writers do. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/list_x.rs | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index cdf0c9e7ca..001af1bc57 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2270,6 +2270,10 @@ unsafe fn write(list: *mut c_void, va: u64, words: &[u32], mode: D3D12_WRITEBUFF if super::cpu_writes::writable(va as usize, n as usize) { if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(list as usize))) { s.cpu_writes.push(super::cpu_writes::Write::Immediate(va as usize, words.to_vec())); + // a wait on it later in this list is met by it, as for a GPU write: the CPU + // writes it only after the list is submitted, which a wait polled before the + // submit would never see + s.note_write(va, n); return; } } @@ -2380,6 +2384,7 @@ unsafe fn timestamp(list: *mut c_void, va: u64, what: &str) { let rb_raw = rb.as_raw() as usize; let off = i as u64 * 8; s.cpu_writes.push(super::cpu_writes::Write::ReadbackU64 { va: va as usize, src: base + off as usize, _buf: rb }); + s.note_write(va, 8); drop(g); pc::(list, PC_END_QUERY)(list, heap, D3D12_QUERY_TYPE_TIMESTAMP, i); pc::(list, PC_RESOLVE_QUERY_DATA)(list, heap, D3D12_QUERY_TYPE_TIMESTAMP, i, 1, rb_raw, off); @@ -4314,6 +4319,34 @@ mod tests { assert!(!s.has_prior_write(0x0FF8, 8)); } + /// A Write32/64BitValueX into the title's own memory is made by the CPU after the list is + /// submitted (`cpu_writes`); a wait on the same address later in the list is met by it, like + /// one after a GPU write, and must not be polled before the list that makes the write runs. + #[test] + fn a_wait_after_a_title_memory_write_in_the_same_list_is_met_by_it() { + let list = 0x7000_0101usize as *mut c_void; + let cell = Box::new(AtomicU32::new(0)); + let va = &*cell as *const AtomicU32 as u64; + let other = Box::new(AtomicU32::new(0)); + let other_va = &*other as *const AtomicU32 as u64; + unsafe { + on_create(list, 0x100, 0, D3D12_COMMAND_LIST_TYPE_DIRECT); + write(list, va, &[42], D3D12_WRITEBUFFERIMMEDIATE_MODE_MARKER_OUT, "Write32BitValueX"); + wait_memory(list, va, 4, D3D12_COMPARISON_FUNC_EQUAL.0 as u32, 42, u32::MAX as u64, "Wait32BitValueX"); + // another address: still a wait + wait_memory(list, other_va, 4, D3D12_COMPARISON_FUNC_EQUAL.0 as u32, 7, u32::MAX as u64, "Wait32BitValueX"); + } + { + let g = LISTS.lock().unwrap(); + let s = g.as_ref().unwrap().get(&(list as usize)).unwrap(); + assert_eq!(s.cpu_writes.len(), 1, "the write goes to the CPU"); + assert_eq!(s.pending_waits.len(), 1, "only the wait on the other address is left"); + assert_eq!(s.pending_waits[0].va, other_va); + } + on_release(list as usize); + assert_eq!(cell.load(Ordering::Acquire), 0, "nothing written before a submit"); + } + #[test] fn execute_with_waits_splits_batches_before_unsatisfied_list() { // Use synthetic non-zero list pointers distinct from real COM objects.