From 988e9a66e889aac9f85aa165adb09abab70e84e0 Mon Sep 17 00:00:00 2001 From: Lewandox <108379891+Lewandox@users.noreply.github.com> Date: Wed, 25 Nov 2026 19:42:50 +0000 Subject: [PATCH] gpu: settle reader before readback and serialize Gate tests In raw_views_a_submission_in_flight_may_read_stay_until_it_has_run, gpu_caught_up checks only the D3D12 GPU fence value and returns without draining when the fence has already advanced, racing against xtp-gpu-finisher copying dst back to title memory. Settle reader explicitly before asserting on dst.words(). Also serialize the Gate::close tests in descriptor/host.rs and resource/pool.rs with host::submissions::serial(), matching host::submissions and draw.rs. --- crates/xtp-gpu/src/descriptor/host.rs | 10 ++++++++-- crates/xtp-gpu/src/host/submissions.rs | 8 ++++---- crates/xtp-gpu/src/resource/pool.rs | 1 + 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/xtp-gpu/src/descriptor/host.rs b/crates/xtp-gpu/src/descriptor/host.rs index f569ff7..c1db57a 100644 --- a/crates/xtp-gpu/src/descriptor/host.rs +++ b/crates/xtp-gpu/src/descriptor/host.rs @@ -6556,6 +6556,7 @@ void main(uint3 id : SV_DispatchThreadID) { #[test] fn raw_views_a_submission_in_flight_may_read_stay_until_it_has_run() { use crate::testgpu::{TitleBuffer, pattern}; + let _serial = crate::host::submissions::serial(); let test = "descriptor::host::tests::raw_views_a_submission_in_flight_may_read_stay_until_it_has_run"; let Some(gpu) = crate::testgpu::device(test) else { return; @@ -6596,7 +6597,7 @@ void main(uint3 id : SV_DispatchThreadID) { heap.make_at(0, &buffer_view(false, &src, structured(20), (0, 4))); assert_eq!(heap.pool_state(), (2, 1)); gate.open(); - gpu_caught_up(host); + reader.settle(); let old: Vec = (0..64) .map(|i| if i < 24 { pattern(i) } else { 0 }) .collect(); @@ -6616,7 +6617,9 @@ void main(uint3 id : SV_DispatchThreadID) { /// Waits until the GPU has run what was handed to the queue so far, /// other tests' submissions too (one handed over but not yet in the - /// timeline is not waited for by a drain). + /// timeline is not waited for by a drain). Waits for the GPU fence + /// only, not for host write-back: call `HostList::settle` before + /// reading back what a submission wrote. fn gpu_caught_up(host: &crate::host::Host) { let ticket = host.executing(); let started = std::time::Instant::now(); @@ -7360,6 +7363,7 @@ void main() { #[test] fn retired_raw_views_go_back_when_the_fence_reaches_them() { use crate::testgpu::TitleBuffer; + let _serial = crate::host::submissions::serial(); let test = "descriptor::host::tests::retired_raw_views_go_back_when_the_fence_reaches_them"; let Some(gpu) = crate::testgpu::device(test) else { return; @@ -7407,6 +7411,7 @@ void main() { #[test] fn a_remapped_slot_gets_a_raw_view_once_one_comes_back() { use crate::testgpu::{TitleBuffer, TitleList}; + let _serial = crate::host::submissions::serial(); let test = "descriptor::host::tests::a_remapped_slot_gets_a_raw_view_once_one_comes_back"; let Some(gpu) = crate::testgpu::device(test) else { return; @@ -7910,6 +7915,7 @@ void main() { #[test] fn slots_viewing_the_same_window_share_one_raw_view() { use crate::testgpu::TitleBuffer; + let _serial = crate::host::submissions::serial(); let test = "descriptor::host::tests::slots_viewing_the_same_window_share_one_raw_view"; let Some(gpu) = crate::testgpu::device(test) else { return; diff --git a/crates/xtp-gpu/src/host/submissions.rs b/crates/xtp-gpu/src/host/submissions.rs index 33d5c01..89fdfd8 100644 --- a/crates/xtp-gpu/src/host/submissions.rs +++ b/crates/xtp-gpu/src/host/submissions.rs @@ -6,10 +6,10 @@ use super::*; use crate::queue::Written; use crate::testgpu::{Kernel, Pass, TitleBuffer, device, pattern}; -/// The tests here one at a time: a test that holds the host queue -/// ([`Gate`]) would stop one that waits inside a submission (which holds -/// the submission lock) until the gate's timer opens it. Tests elsewhere -/// wait for the GPU only after submitting, so they are only delayed. +/// Tests that hold the host queue ([`Gate`]) or wait inside a submission +/// run one at a time: a held queue would stop one waiting under the +/// submission lock until the gate's timer opens it. Tests elsewhere wait +/// for the GPU only after submitting, so they are only delayed. pub(crate) fn serial() -> std::sync::MutexGuard<'static, ()> { static SERIAL: Mutex<()> = Mutex::new(()); SERIAL.lock().unwrap_or_else(|e| e.into_inner()) diff --git a/crates/xtp-gpu/src/resource/pool.rs b/crates/xtp-gpu/src/resource/pool.rs index fdff79d..abe4b40 100644 --- a/crates/xtp-gpu/src/resource/pool.rs +++ b/crates/xtp-gpu/src/resource/pool.rs @@ -2271,6 +2271,7 @@ pub(crate) mod tests { /// registry. #[test] fn reserved_textures_map_and_give_back_their_tiles() { + let _serial = crate::host::submissions::serial(); let test = "resource::pool::tests::reserved_textures_map_and_give_back_their_tiles"; let Some(gpu) = crate::testgpu::device(test) else { return;