diff --git a/crates/metal2vulkan-runtime/src/device/staging.rs b/crates/metal2vulkan-runtime/src/device/staging.rs index 453bd7859..a180b852a 100644 --- a/crates/metal2vulkan-runtime/src/device/staging.rs +++ b/crates/metal2vulkan-runtime/src/device/staging.rs @@ -670,6 +670,13 @@ impl Device { } } + /// The staging queue's value the host saw complete (0 on a device without one), read without + /// the wait of no time a [`staging_wait`](Self::staging_wait) makes: whether a wait for a + /// submission would reach the driver. + pub(crate) fn staging_seen(&self) -> u64 { + (self.shared().staging.as_ref()).map_or(0, |staging| staging.done.value()) + } + /// The fill queue's family and that family's queue flags, where the device has one. pub(crate) fn fill_queue_family(&self) -> Option<(u32, vk::QueueFlags)> { (self.shared().fill_queue.as_ref()).map(|queue| (queue.family, queue.flags)) diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs index c74787a07..1b3c15762 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer.rs @@ -128,6 +128,8 @@ mod set_each; mod set_states; mod writers; +#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] +pub(crate) use home::RUNTIME_PLACING_WAITS; use home::{Home, Reach}; pub use set_each::{IndirectComputeSet, IndirectRenderSet}; use set_states::{Keyed, SetKeys, SetStates, StateKey}; diff --git a/crates/metal2vulkan-runtime/src/indirect_command_buffer/home.rs b/crates/metal2vulkan-runtime/src/indirect_command_buffer/home.rs index a01e1e688..0c484b05f 100644 --- a/crates/metal2vulkan-runtime/src/indirect_command_buffer/home.rs +++ b/crates/metal2vulkan-runtime/src/indirect_command_buffer/home.rs @@ -45,6 +45,7 @@ use crate::degradation::Degradation; use crate::device::lock; use crate::encoder::EncoderCore; use crate::error::{internal, Error}; +use crate::exit_guard; use crate::texture::transfer_barrier; use crate::{Device, StorageMode}; use ash::vk; @@ -137,13 +138,34 @@ impl Home { } } +/// The drops of homes that waited for the submission that placed their records, which the host +/// had not seen complete, on one of the runtime's threads (a command buffer's completion that let +/// go of the buffer last), in the device tests' builds: a test's evidence that the wait ran there +/// (`tests::icb_records_home`). +#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] +pub(crate) static RUNTIME_PLACING_WAITS: std::sync::atomic::AtomicU64 = + std::sync::atomic::AtomicU64::new(0); + impl Drop for Home { fn drop(&mut self) { // The submission that set the device-local records' bytes holds nothing: the records go - // once it completed. + // once it completed. A section of the exit guard, wherever the last hold lets go (a + // command buffer's completion, on its queue's waiter or a handler thread): while the + // process exits, the records stay to its end, as their buffer's release leaves them + // (`exit_guard`, `release.rs`). let state = lock(&self.state); if let (Some(records), false) = (self.device.get(), state.init_seen) { - let _ = records.generation().wait_staging(state.init); + let Some(_busy) = exit_guard::enter() else { + return; + }; + let device = records.generation(); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + let unseen = device.staging_seen() < state.init && exit_guard::on_runtime_thread(); + let _ = device.wait_staging(state.init); + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + if unseen { + RUNTIME_PLACING_WAITS.fetch_add(1, Ordering::Relaxed); + } } } } diff --git a/crates/metal2vulkan-runtime/src/tests/icb_records_home.rs b/crates/metal2vulkan-runtime/src/tests/icb_records_home.rs index df0d3a661..3fe9e80dc 100644 --- a/crates/metal2vulkan-runtime/src/tests/icb_records_home.rs +++ b/crates/metal2vulkan-runtime/src/tests/icb_records_home.rs @@ -23,6 +23,7 @@ use crate::{ RenderPipelineState, ResourceOptions, Size, StoreAction, Texture, TextureDescriptor, WorkCounts, }; +use ash::vk; use metal2vulkan::bind::indirect_commands::written_flag_offset; use metal2vulkan::metal::{ IndirectCommandType, PixelFormat, PrimitiveType, RenderStages, ResourceUsage, TextureUsage, @@ -30,6 +31,7 @@ use metal2vulkan::metal::{ use metal2vulkan::pipeline::{ ColorAttachmentDescriptor, ComputePipelineDescriptor, RenderPipelineDescriptor, StageSource, }; +use std::sync::atomic::Ordering; use std::sync::{Arc, Mutex}; /// The commands of a buffer, and the cells of the target: a command draws the lower half of the @@ -623,3 +625,69 @@ fn a_gpu_reset_then_an_encode_then_an_execution_are_exact() { } } } + +/// A test timeline of `device`'s at value 0, which [`Device::hold_staging_submissions`] holds the +/// staging submissions on until the test signals it. +fn hold_timeline(device: &Device) -> vk::Semaphore { + let mut kind = vk::SemaphoreTypeCreateInfo::default() + .semaphore_type(vk::SemaphoreType::TIMELINE) + .initial_value(0); + let info = vk::SemaphoreCreateInfo::default().push_next(&mut kind); + // SAFETY: the device is live and enables `timelineSemaphore`; `info` is live for the call. + unsafe { device.shared().vk().create_semaphore(&info, None) }.unwrap() +} + +/// A buffer the program lets go of while a command buffer that uses it is in flight goes as +/// that command buffer completes, on one of the runtime's threads (its queue's waiter, or a +/// handler thread): its home waits there for the staging submission that placed its records in +/// device-local memory, which the host has not seen complete, inside a section of the exit guard +/// (`home.rs`), as every Vulkan call of the runtime's threads (`exit_guard::check`, which in these +/// builds ends the process at one made outside a section). The placing submission is held on the +/// GPU until the command buffer was committed and the buffer let go of +/// ([`Device::hold_staging_submissions`]), so the command buffer completes after it, and nothing +/// waits for it on the host before the drop. A device that keeps every buffer's records in the +/// host's memory places none and waits for none. +#[test] +fn a_buffer_its_command_buffer_lets_go_of_last_waits_for_its_placing_inside_a_section() { + let ours = Ours::new("icb-home-last-hold"); + let name = ours.device.name().to_owned(); + if !ours.placing() { + println!("{name}: keeps every buffer's records in the host's memory: nothing placed"); + return; + } + let hold = hold_timeline(&ours.device); + ours.device.hold_staging_submissions(Some((hold, 1))); + let icb = ours.icb(ResourceOptions::private()); + // Its residency in a command buffer: the GPU's first use, which places the records, held + // until the command buffer completes. + let queue = ours.device.new_command_queue().unwrap(); + let command_buffer = queue.command_buffer().unwrap(); + let mut encoder = command_buffer.compute_command_encoder().unwrap(); + encoder + .use_resource(icb.as_resource(), ResourceUsage::READ) + .unwrap(); + encoder.end_encoding().unwrap(); + ours.device.hold_staging_submissions(None); + command_buffer.commit().unwrap(); + let waits = || crate::indirect_command_buffer::RUNTIME_PLACING_WAITS.load(Ordering::Relaxed); + let before = (ours.device.work_counts(), waits()); + drop(icb); + let signal = vk::SemaphoreSignalInfo::default().semaphore(hold).value(1); + // SAFETY: a host signal of the test's timeline, above its value. + unsafe { ours.device.shared().vk().signal_semaphore(&signal) }.unwrap(); + wait(&command_buffer); + let after = (ours.device.work_counts(), waits()); + let where_seen = after.0.completed_where_seen - before.0.completed_where_seen; + println!( + "{name}: the command buffer's completion let go of the buffer last ({where_seen} \ + completed where seen); {} drops waited for the placing submission on a runtime thread", + after.1 - before.1 + ); + assert_eq!( + after.1 - before.1, + 1, + "{name}: the drop waited for the placing submission on one of the runtime's threads" + ); + // SAFETY: the semaphore is the test's, and no submission waits on it any more. + unsafe { ours.device.shared().vk().destroy_semaphore(hold, None) }; +} diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 16c970e78..bf4312a2d 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2320; +pub const RUNTIME_VERSION: u32 = 2321; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles