diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 169acdc..0897d3c 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -8,7 +8,7 @@ use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::{Mutex, OnceLock}; -use windows::Win32::Foundation::{HANDLE, HWND, LPARAM, LRESULT, RECT, WPARAM}; +use windows::Win32::Foundation::{HANDLE, HWND, LPARAM, LRESULT, RECT, WAIT_OBJECT_0, WPARAM}; use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_12_0; use windows::Win32::Graphics::Direct3D12::*; use windows::Win32::Graphics::Dxgi::Common::*; @@ -3722,6 +3722,49 @@ fn land() { } } +#[cfg(test)] +#[test] +fn host_fence_completion_rejects_removed_device() { + assert!(!fence_reached(u64::MAX, 1)); + assert!(!fence_reached(6, 7)); + assert!(fence_reached(7, 7)); + assert!(fence_reached(8, 7)); +} + +/// Whether a fence's completed value shows `target` run. `u64::MAX` is not +/// a value the card reaches: it is what `GetCompletedValue` answers when the +/// device is gone. +fn fence_reached(completed: u64, target: u64) -> bool { + completed != u64::MAX && completed >= target +} + +/// Waits for `target` on `fence` and reports what went wrong instead of +/// carrying on: a failed wait used to fall through to the landing step with +/// nothing run, settling flights off an unsent fence value, and a lost +/// device (a completed value of `u64::MAX`) made every wait pass at once. +/// +/// # Safety +/// +/// `event` is the calling thread's own, never closed while it waits. +unsafe fn wait_host_fence(fence: &ID3D12Fence, target: u64, event: HANDLE, context: &str) { + // SAFETY: the caller's fence and event, alive for the whole wait. + if let Err(e) = unsafe { fence.SetEventOnCompletion(target, event) } { + fail(format_args!( + "{context}: SetEventOnCompletion({target}) failed: {e}" + )); + } + let status = unsafe { WaitForSingleObject(event, INFINITE) }; + if status != WAIT_OBJECT_0 { + fail(format_args!("{context}: fence wait failed: {status:?}")); + } + let completed = unsafe { fence.GetCompletedValue() }; + if !fence_reached(completed, target) { + fail(format_args!( + "{context}: fence {completed} did not reach {target}" + )); + } +} + /// The thread that settles the flights once the card has run them all /// ([`late`]): without it a bottom-of-pipe write would reach the title only /// at the next call that settles, and a title thread that polls such a @@ -3761,14 +3804,10 @@ fn late_helper() { }; // The card first, without the submission lock: the submitting // thread prepares and sends meanwhile. + #[cfg(feature = "diagnostics")] + let _card = crate::timeline::card_wait(Some(target)); // SAFETY: our fence and event. - unsafe { - if host.fence.SetEventOnCompletion(target, event).is_ok() { - #[cfg(feature = "diagnostics")] - let _card = crate::timeline::card_wait(Some(target)); - WaitForSingleObject(event, INFINITE); - } - } + unsafe { wait_host_fence(&host.fence, target, event, "late helper") }; { let mut submitted = host.submitted.lock().unwrap_or_else(|e| e.into_inner()); // Something newer went up meanwhile: the card is waited for @@ -3926,14 +3965,7 @@ fn relay_worker() { #[cfg(feature = "diagnostics")] let _card = crate::timeline::card_wait(Some(target)); // SAFETY: the host's fence and this thread's event. - let waited = unsafe { - host.fence.SetEventOnCompletion(target, event).map(|()| { - WaitForSingleObject(event, INFINITE); - }) - }; - if waited.is_err() { - std::thread::sleep(std::time::Duration::from_millis(1)); - } + unsafe { wait_host_fence(&host.fence, target, event, "relay") }; RELAY_STATS[2].fetch_add(waiting.elapsed().as_micros() as u64, Ordering::Relaxed); } }