diff --git a/README.md b/README.md index 83a6798152..909324cec9 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,10 @@ A gamepad is recommended ([Controls](#controls)). The game folder is never modif Far-jump address calculations are resolved to fixed targets only when branches enter at the start of the calculation. An entry partway through it keeps the jump indirect and subject to the recompiler's existing indirect-branch limits. +CPU-emulated bottom-of-pipe writes wait for confirmed GPU completion. A stalled or removed +producer keeps its writes and readback resources held; a timeout does not publish progress. +Other producer queues can continue. Resources held after device loss remain retained until exit. +
diff --git a/crates/xcl/src/hle/d3d12x/cpu_writes.rs b/crates/xcl/src/hle/d3d12x/cpu_writes.rs index f672e42f0a..3d850d27df 100644 --- a/crates/xcl/src/hle/d3d12x/cpu_writes.rs +++ b/crates/xcl/src/hle/d3d12x/cpu_writes.rs @@ -8,15 +8,14 @@ //! queue signals it, and a worker thread does the writes, in submission order, when the GPU is //! there – what bottom of pipe means. -use std::collections::HashMap; +use std::collections::{HashMap, VecDeque}; use std::ffi::c_void; use std::sync::atomic::{AtomicU32, AtomicU64, Ordering}; use std::sync::{mpsc, Mutex, OnceLock}; +use std::time::{Duration, Instant}; use windows::core::Interface; -use windows::Win32::Foundation::HANDLE; use windows::Win32::Graphics::Direct3D12::*; -use windows_sys::Win32::System::Threading as th; /// A bottom-of-pipe write into memory outside unified memory, applied by the CPU once the /// queue's fence reaches the submission that recorded it. @@ -32,9 +31,14 @@ pub(super) enum Write { struct Job { fence: ID3D12Fence, + // Capture the diagnostic number while the queue is alive; the worker never looks it up + // through a queue address that may have been released or reused. + queue_id: usize, value: u64, writes: Vec, done: Option>, + queued: Instant, + warned: bool, } // SAFETY: the fence is a free-threaded COM object; the writes are plain data @@ -44,6 +48,7 @@ unsafe impl Send for Job {} struct QueueFence { fence: ID3D12Fence, value: u64, + failed: bool, } unsafe impl Send for QueueFence {} @@ -60,38 +65,107 @@ pub(super) fn forget_queue(id: super::present::QueueId) { drop(gone); } -/// how long the worker waits for one submission before writing anyway (a removed device, a -/// queue that never gets there) -const WAIT_MS: u32 = 10_000; +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum ProducerStatus { Pending, Complete, Removed } + +fn completion(completed: u64, wanted: u64) -> ProducerStatus { + if completed == u64::MAX { ProducerStatus::Removed } + else if completed >= wanted { ProducerStatus::Complete } + else { ProducerStatus::Pending } +} + +struct PendingQueue { + jobs: VecDeque, + removed: bool, +} + +/// The key names an owned producer fence, not a reusable command queue address. Keep that +/// fence and every ordered suffix alive while pending; unrelated fences can still progress. +fn publish_ready(pending: &mut HashMap>, mut poll: impl FnMut(&mut T) -> ProducerStatus, mut publish: impl FnMut(T)) { + pending.retain(|_, queue| { + for _ in 0..64 { + if queue.removed { break; } + let Some(job) = queue.jobs.front_mut() else { break }; + match poll(job) { + ProducerStatus::Complete => publish(queue.jobs.pop_front().unwrap()), + ProducerStatus::Pending => break, + ProducerStatus::Removed => { queue.removed = true; break; } + } + } + !queue.jobs.is_empty() + }); +} + +/// A failed signal gives us no safe retirement point for a submitted readback resource. +fn keep_if_marked(work: T, marked: bool) -> Option { + if marked { Some(work) } else { std::mem::forget(work); None } +} + +fn send_or_retain(sender: &mpsc::Sender, work: T) -> bool { + match sender.send(work) { + Ok(()) => true, + Err(e) => { std::mem::forget(e.0); false } + } +} fn worker() -> &'static Mutex> { static W: OnceLock>> = OnceLock::new(); W.get_or_init(|| { let (tx, rx) = mpsc::channel::(); let spawned = std::thread::Builder::new().name("xcl-cpu-writes".into()).spawn(move || unsafe { - let event = th::CreateEventW(std::ptr::null(), 0, 0, std::ptr::null()); - for job in rx { - if super::real::fence_completed(&job.fence) < job.value && job.fence.SetEventOnCompletion(job.value, HANDLE(event as *mut c_void)).is_ok() { - th::WaitForSingleObject(event, WAIT_MS); - if super::real::fence_completed(&job.fence) < job.value { - static LATE: AtomicU64 = AtomicU64::new(0); - let k = LATE.fetch_add(1, Ordering::Relaxed) + 1; - if k.is_power_of_two() { - crate::xlog_warn!("D3D12.X: {k} writes after the GPU so far done although their queue had not got there after {WAIT_MS} ms (fence at {}, wanted {}; {} writes)", super::real::fence_completed(&job.fence), job.value, job.writes.len()); - } + // Keep the pending map's resources on unwind. This is not a worker-panic recovery + // policy: the channel receiver still has its ordinary unwinding behavior. + let mut pending = std::mem::ManuallyDrop::new(HashMap::>::new()); + loop { + let next = if pending.is_empty() { + rx.recv().map_err(|_| mpsc::RecvTimeoutError::Disconnected) + } else { + // No reusable event or blocking fence wait: stale notifications and failed + // event registration cannot publish a write or stall another producer. + rx.recv_timeout(Duration::from_millis(2)) + }; + match next { + Ok(job) => { + pending.entry(job.fence.as_raw() as usize) + .or_insert_with(|| PendingQueue { jobs: VecDeque::new(), removed: false }).jobs.push_back(job); + } + Err(mpsc::RecvTimeoutError::Timeout) => {} + Err(mpsc::RecvTimeoutError::Disconnected) => { + // The receiver is stopping, not evidence that the GPU finished. + std::mem::forget(pending); + break; } } - for w in &job.writes { - match w { - Write::Immediate(va, words) => write_now(*va, words), - Write::ReadbackU64 { va, src, .. } => { - let value = (*(*src as *const AtomicU64)).load(Ordering::Acquire); - write_u64_now(*va, value); + publish_ready(&mut pending, |job| { + let done = super::real::fence_completed(&job.fence); + let state = completion(done, job.value); + if state != ProducerStatus::Complete && !job.warned + && (state == ProducerStatus::Removed || job.queued.elapsed() >= Duration::from_secs(10)) { + job.warned = true; + crate::xlog_warn!("D3D12.X: holding {} CPU writes after {} ms: producer {:?}, fence at {}, wanted {} (queue q{}, producer fence {:#x}, Signal accepted)", job.writes.len(), job.queued.elapsed().as_millis(), state, done, job.value, job.queue_id, job.fence.as_raw() as usize); + } + state + }, |job| { + for w in &job.writes { + match w { + Write::Immediate(va, words) => write_now(*va, words), + Write::ReadbackU64 { va, src, .. } => { + let value = (*(*src as *const AtomicU64)).load(Ordering::Acquire); + write_u64_now(*va, value); + } } } - } - if let Some(done) = job.done { - let _ = done.send(()); + // (the receipt of `after_with_completion`: every store of the submission is made) + if let Some(done) = job.done { + let _ = done.send(()); + } + }); + // A removed producer never publishes. Its waiters get no receipt (the sender + // goes); its writes and their resources stay held. + for queue in pending.values_mut().filter(|queue| queue.removed) { + for job in queue.jobs.iter_mut() { + job.done = None; + } } } }); @@ -240,18 +314,22 @@ pub(super) fn completion_pair_for_test() -> (Completion, mpsc::Sender<()>) { } /// Schedule `writes` from lists just submitted to `queue` after the GPU has finished them. +/// No receipt: nothing was scheduled, and what was submitted stays held without being published. unsafe fn schedule(queue: *mut c_void, writes: Vec, completion: bool) -> Option { if writes.is_empty() || queue.is_null() { return None; } let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&queue) else { return None }; + let queue_id = super::list_x::queue_id(queue as usize); let (done, receipt) = if completion { let (tx, rx) = mpsc::channel(); (Some(tx), Some(Completion(rx))) } else { (None, None) }; - let job = { + let sender = worker(); + let count = writes.len(); + let sent = { let mut g = FENCES.lock().unwrap(); let m = g.get_or_insert_with(HashMap::new); let entry = match m.entry(queue as usize) { @@ -259,22 +337,37 @@ unsafe fn schedule(queue: *mut c_void, writes: Vec, completion: bool) -> std::collections::hash_map::Entry::Vacant(v) => { let mut dev: Option = None; if q.GetDevice(&mut dev).is_err() { + std::mem::forget(writes); return None; } - let Some(dev) = dev else { return None }; - let Ok(fence) = dev.CreateFence::(0, D3D12_FENCE_FLAG_NONE) else { return None }; - v.insert(QueueFence { fence, value: 0 }) + let Some(dev) = dev else { std::mem::forget(writes); return None }; + let Ok(fence) = dev.CreateFence::(0, D3D12_FENCE_FLAG_NONE) else { std::mem::forget(writes); return None }; + v.insert(QueueFence { fence, value: 0, failed: false }) } }; - entry.value += 1; - if super::real::native_queue_signal(q, &entry.fence, entry.value).is_err() { + let marked = !entry.failed && entry.value.checked_add(1).filter(|v| *v != u64::MAX).is_some_and(|value| { + entry.value = value; + super::real::native_queue_signal(q, &entry.fence, value).is_ok() + }); + let Some(writes) = keep_if_marked(writes, marked) else { + if !entry.failed { + crate::xlog_error!("D3D12.X: cannot signal CPU-write producer on q{queue_id} (fence {:#x}, last mark {}); retaining in-flight resources without publishing", entry.fence.as_raw() as usize, entry.value); + } + entry.failed = true; return None; - } - Job { fence: entry.fence.clone(), value: entry.value, writes, done } + }; + let job = Job { fence: entry.fence.clone(), queue_id, value: entry.value, writes, done, queued: Instant::now(), warned: false }; + // Enqueue under the signal-order lock: concurrent submitters must not publish value N+1 + // before the worker has received N. The unbounded send does not wait for GPU/title work. + send_or_retain(&sender.lock().unwrap(), job) }; static FIRST: std::sync::Once = std::sync::Once::new(); - FIRST.call_once(|| crate::xlog_info!("D3D12.X: bottom-of-pipe writes outside unified memory are done by the CPU after the GPU ({} in the first submission)", job.writes.len())); - worker().lock().unwrap().send(job).ok()?; + FIRST.call_once(|| crate::xlog_info!("D3D12.X: bottom-of-pipe writes outside unified memory are done by the CPU after the GPU ({} in the first submission)", count)); + if !sent { + crate::xlog_error!("D3D12.X: CPU-write worker unavailable for q{queue_id}; retaining in-flight resources without publishing"); + // (the retained job keeps its sender, so nobody may wait for its receipt) + return None; + } receipt } @@ -289,9 +382,221 @@ pub(super) unsafe fn after_with_completion(queue: *mut c_void, writes: Vec>, + wanted: u64, + word: u32, + dropped: Rc>, + } + + impl Drop for FakeJob { + fn drop(&mut self) { self.dropped.set(self.dropped.get() + 1); } + } + + fn fake_job(fence: &Rc>, wanted: u64, word: u32, dropped: &Rc>) -> FakeJob { + FakeJob { fence: fence.clone(), wanted, word, dropped: dropped.clone() } + } + + fn fake_queue(jobs: Vec) -> PendingQueue { + PendingQueue { jobs: jobs.into(), removed: false } + } + + fn poll_fake(job: &mut FakeJob) -> ProducerStatus { completion(job.fence.get(), job.wanted) } + + #[test] + fn pending_cpu_writes_retain_resources_until_confirmed_completion() { + let fence = Rc::new(Cell::new(2)); + let dropped = Rc::new(Cell::new(0)); + let mut pending = HashMap::from([(1, fake_queue(vec![fake_job(&fence, 3, 17, &dropped)]))]); + let destination = Cell::new(0); + // Arbitrarily many timeout/wakeup cycles cannot substitute for producer completion. + for _ in 0..100 { + publish_ready(&mut pending, poll_fake, |j| destination.set(j.word)); + assert_eq!(destination.get(), 0); + assert_eq!(dropped.get(), 0, "readback ownership must survive pending polls"); + } + fence.set(3); + publish_ready(&mut pending, poll_fake, |j| destination.set(j.word)); + assert_eq!(destination.get(), 17); + assert_eq!(dropped.get(), 1); + assert!(pending.is_empty()); + publish_ready(&mut pending, poll_fake, |_| panic!("published twice")); + } + + #[test] + fn pending_cpu_writes_preserve_suffix_without_blocking_other_fences() { + let slow = Rc::new(Cell::new(0)); + let fast = Rc::new(Cell::new(2)); + let dropped = Rc::new(Cell::new(0)); + let mut pending = HashMap::from([ + (1, fake_queue(vec![fake_job(&slow, 1, 11, &dropped), fake_job(&slow, 2, 12, &dropped)])), + (2, fake_queue(vec![fake_job(&fast, 2, 21, &dropped)])), + ]); + let mut writes = Vec::new(); + publish_ready(&mut pending, poll_fake, |j| writes.push(j.word)); + assert_eq!(writes, [21]); + assert_eq!(dropped.get(), 1); + slow.set(2); + publish_ready(&mut pending, poll_fake, |j| writes.push(j.word)); + assert_eq!(writes, [21, 11, 12]); + assert_eq!(dropped.get(), 3); + } + + #[test] + fn a_ready_suffix_never_overtakes_an_unconfirmed_cpu_write() { + let first = Rc::new(Cell::new(0)); + let later = Rc::new(Cell::new(2)); + let dropped = Rc::new(Cell::new(0)); + let mut pending = HashMap::from([(1, fake_queue(vec![fake_job(&first, 1, 1, &dropped), fake_job(&later, 2, 2, &dropped)]))]); + publish_ready(&mut pending, poll_fake, |_| panic!("unconfirmed head must hold its suffix")); + assert_eq!(dropped.get(), 0); + first.set(1); + let mut writes = Vec::new(); + publish_ready(&mut pending, poll_fake, |j| writes.push(j.word)); + assert_eq!(writes, [1, 2]); + } + + #[test] + fn device_removal_never_publishes_or_releases_cpu_writes() { + let removed = Rc::new(Cell::new(u64::MAX)); + let healthy = Rc::new(Cell::new(1)); + let dropped = Rc::new(Cell::new(0)); + let mut pending = HashMap::from([ + (1, fake_queue(vec![fake_job(&removed, 1, 11, &dropped)])), + (2, fake_queue(vec![fake_job(&healthy, 1, 21, &dropped)])), + ]); + let mut writes = Vec::new(); + publish_ready(&mut pending, poll_fake, |j| writes.push(j.word)); + assert_eq!(writes, [21]); + assert_eq!(dropped.get(), 1); + // Even a later inconsistent observation must not revive a removed producer. + removed.set(10); + pending.get_mut(&1).unwrap().jobs.push_back(fake_job(&removed, 2, 12, &dropped)); + publish_ready(&mut pending, |_| panic!("removed producer was polled again"), |_| panic!("removed producer published")); + assert_eq!(dropped.get(), 1); + assert_eq!(pending[&1].jobs.len(), 2); + // This fixture's references are CPU-only; releasing them here is not GPU teardown. + } + + #[test] + fn failed_producer_signal_retains_inflight_cpu_write_resources() { + let fence = Rc::new(Cell::new(0)); + let dropped = Rc::new(Cell::new(0)); + assert!(keep_if_marked(fake_job(&fence, 1, 1, &dropped), false).is_none()); + assert_eq!(dropped.get(), 0, "failed signal cannot retire an in-flight readback"); + drop(keep_if_marked(fake_job(&fence, 1, 1, &dropped), true).unwrap()); + assert_eq!(dropped.get(), 1, "successful mark transfers ownership normally"); + } + + #[test] + fn failed_worker_send_retains_inflight_cpu_write_resources() { + let fence = Rc::new(Cell::new(0)); + let dropped = Rc::new(Cell::new(0)); + let (tx, rx) = mpsc::channel(); + drop(rx); + assert!(!send_or_retain(&tx, fake_job(&fence, 1, 1, &dropped))); + assert_eq!(dropped.get(), 0, "lost worker cannot retire an in-flight readback"); + let (tx, rx) = mpsc::channel(); + assert!(send_or_retain(&tx, fake_job(&fence, 1, 1, &dropped))); + assert_eq!(dropped.get(), 0); + drop(rx.recv().unwrap()); + assert_eq!(dropped.get(), 1); + } + + /// Uses the actual worker unchanged on the baseline: a GPU copy is gated beyond its old + /// ten-second fallback, then released. No spinning shader or intentional device removal. + #[test] + fn gpu_readback_is_not_published_after_an_uncompleted_ten_second_wait() { + use std::time::{Duration, Instant}; + const EXPECTED: u64 = 0x1234_5678_9abc_def0; + const SENTINEL: u64 = 0xface_cafe_dead_beef; + struct Cleanup { + gate: ID3D12Fence, + done: ID3D12Fence, + // Keep the CPU destination as well: a delayed worker can still write to it. + held: Option<(ID3D12CommandQueue, ID3D12CommandAllocator, ID3D12GraphicsCommandList, ID3D12Resource, ID3D12Resource, Box)>, + } + impl Drop for Cleanup { + fn drop(&mut self) { + unsafe { + let _ = self.gate.Signal(1); + let deadline = Instant::now() + Duration::from_secs(3); + while Instant::now() < deadline { + let done = self.done.GetCompletedValue(); + if done != u64::MAX && done >= 1 + && self.held.as_ref().unwrap().5.load(Ordering::Acquire) == EXPECTED { + return; + } + std::thread::sleep(Duration::from_millis(2)); + } + // A failed/removed device or delayed worker cannot safely retire these. + std::mem::forget((self.gate.clone(), self.done.clone(), self.held.take())); + } + } + } + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + return crate::gpu::recomp::runner::skip_test("no hardware D3D12 device for gated readback"); + } + let dev = dev.unwrap(); + let queue: ID3D12CommandQueue = dev.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_DIRECT, ..Default::default() }).unwrap(); + let allocator: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT).unwrap(); + let list: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, &allocator, None).unwrap(); + let desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, Width: 8, Height: 1, + DepthOrArraySize: 1, MipLevels: 1, + SampleDesc: windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, ..Default::default() + }; + let mut upload: Option = None; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_UPLOAD, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, None, &mut upload).unwrap(); + let upload = upload.unwrap(); + let mut readback: Option = None; + dev.CreateCommittedResource(&D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_READBACK, ..Default::default() }, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COPY_DEST, None, &mut readback).unwrap(); + let readback = readback.unwrap(); + let mut src = std::ptr::null_mut(); + upload.Map(0, None, Some(&mut src)).unwrap(); + (src as *mut u64).write(EXPECTED); + upload.Unmap(0, None); + let mut rb = std::ptr::null_mut(); + readback.Map(0, None, Some(&mut rb)).unwrap(); + (rb as *mut u64).write(0); // distinguish an early read from the GPU's copy + list.CopyBufferRegion(&readback, 0, &upload, 0, 8); + list.Close().unwrap(); + let gate: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let done: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let destination = Box::new(AtomicU64::new(SENTINEL)); + let va = destination.as_ref() as *const AtomicU64 as usize; + let cleanup = Cleanup { gate, done, held: Some((queue, allocator, list, upload, readback, destination)) }; + let (queue, _, list, _, readback, destination) = cleanup.held.as_ref().unwrap(); + queue.Wait(&cleanup.gate, 1).unwrap(); + queue.ExecuteCommandLists(&[Some(list.cast().unwrap())]); + after(queue.as_raw(), vec![Write::ReadbackU64 { va, src: rb as usize, _buf: readback.clone() }]); + queue.Signal(&cleanup.done, 1).unwrap(); + let hold_until = Instant::now() + Duration::from_secs(11); + let mut early = None; + while Instant::now() < hold_until { + let value = destination.load(Ordering::Acquire); + if value != SENTINEL { early = Some(value); break; } + std::thread::sleep(Duration::from_millis(2)); + } + // Always release the gate before reporting a failure; Cleanup also covers panics. + cleanup.gate.Signal(1).unwrap(); + let deadline = Instant::now() + Duration::from_secs(3); + while destination.load(Ordering::Acquire) != EXPECTED && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(2)); + } + assert_eq!(early, None, "CPU published a readback while its GPU producer was gated"); + assert_eq!(destination.load(Ordering::Acquire), EXPECTED, "GPU-produced readback never reached the destination"); + } + } + /// The value lands after the queue has done what came before it. #[test] fn writes_land_after_the_queue() { diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 0210c522f1..224a7fc772 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -2540,7 +2540,7 @@ fn dump_sync_trace(why: &str, n: &AtomicU64, times: u64) { } } -fn queue_id(q: usize) -> usize { +pub(super) fn queue_id(q: usize) -> usize { let mut g = QUEUE_IDS.lock().unwrap(); if let Some(pos) = g.iter().position(|&x| x == q) { pos