diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs index 3b09e747a..af713240f 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs @@ -51,12 +51,19 @@ //! and the RTX 3090, about 2 ms of CPU in one `vkQueueSubmit` in about every thousand waits and //! signals) takes no such wait: each batch with work starts instead with a command buffer of //! barriers whose first is [`Barrier::after_completed`], which makes what completed batches -//! wrote visible to every later command. Every batch, on every driver, waits for the staging -//! queue's last submission while the host has not seen it complete (`Device::staging_wait`): -//! one that carries the copies a burst of host calls queued is made just before the batch and -//! waited for on the GPU, not the host (`texture/staging_copy.rs`). -//! Where every batch waited, about twice as many of its submissions took that long as where -//! batches only signal. +//! wrote visible to every later command. Where every batch waited, about twice as many of its +//! submissions took that long as where batches only signal. +//! +//! **The staging queue's copies.** On every driver, a batch waits on the GPU for the staging +//! queue's last submission while the host has not seen it complete (`Device::staging_wait`), +//! unless a batch of its `VkQueue` already waited for that submission: the wait's second +//! synchronization scope holds every command later in submission order there, so one wait per +//! `VkQueue` makes the copies visible to every later batch of it, as one wait per value does for +//! completed work above. A submission that carries the copies a burst of host calls queued is +//! made just before the batch and waited for on the GPU, not the host +//! (`texture/staging_copy.rs`). The batches after the one that waits may then join a group +//! (**Grouped**, above), which a batch with a wait for work the host has not seen complete only +//! leads. Counted: `WorkCounts::staging_gpu_waits`. //! //! Each barrier goes in a command buffer of barriers alone, which the scheduler records once per //! list of barriers and submits again once the batch that last carried it completed @@ -830,9 +837,16 @@ impl CommandBufferShared { wait_values.push(value); wait_stages.push(vk::PipelineStageFlags::ALL_COMMANDS); } - // And the staging queue's last submission, on every `VkQueue`, until the host saw it - // complete: what it copied may be among what the work reads (`device/staging.rs`). - if let Some((timeline, value)) = device.staging_wait() { + // And the staging queue's last submission, until the host saw it complete: what it + // copied may be among what the work reads (`device/staging.rs`). Where a batch of this + // `VkQueue` already waited for it, every later command there follows that wait in + // submission order, this batch's too (the module docs): one wait serves them all. The + // staging queue's place among the timelines follows the `VkQueue`s' and the internal + // submissions' (`Device::own_completed`). + let staging_place = shared.queues.len() + 1; + let staging_wait = (device.staging_wait()) + .filter(|&(_, value)| value > scheduler.waited[queue][staging_place]); + if let Some((timeline, value)) = staging_wait { wait_semaphores.push(timeline); wait_values.push(value); wait_stages.push(vk::PipelineStageFlags::ALL_COMMANDS); @@ -1080,6 +1094,11 @@ impl CommandBufferShared { for &(source, value) in waited.iter() { scheduler.waited[queue][source] = value; } + // Its staging wait, made or held: a held group goes to its `VkQueue` before any later + // batch there, and one whose submission fails puts the values back. + if let Some((_, value)) = staging_wait { + scheduler.waited[queue][staging_place] = value; + } shared.counts.completed_waited(waited.len()); if !single && !started { shared.counts.bare_start(); diff --git a/crates/metal2vulkan-runtime/src/device/staging.rs b/crates/metal2vulkan-runtime/src/device/staging.rs index b620f51f4..d53bc7ee7 100644 --- a/crates/metal2vulkan-runtime/src/device/staging.rs +++ b/crates/metal2vulkan-runtime/src/device/staging.rs @@ -22,8 +22,10 @@ //! that needs what it did on the CPU (`get_bytes`, a texture's `VkImage` handed out, the //! program's own use of a `VkQueue`) waits for it on the host. Every other submission of the //! device's, a batch or an internal one on any `VkQueue`, waits for it on the GPU until the host -//! saw it complete ([`Device::staging_wait`]): the copies it made are visible to the commands -//! after that wait, which never waits for work of the program's. A submission is not waited for +//! saw it complete ([`Device::staging_wait`]), but a batch of a `VkQueue` where an earlier +//! batch waited for it (`command_buffer/submit.rs`): the copies it made are visible to the +//! commands after that wait, in submission order there, which never waits for work of the +//! program's. A submission is not waited for //! before the next one is recorded: its command buffer is one of up to [`BUFFERS`], each reused //! once the host saw the value it signalled reached. //! - **Who uses it.** A texture made to copy through it (`texture/staging_copy.rs`) has its first @@ -353,8 +355,8 @@ impl Device { } /// The staging queue's last submission while the host has not seen it complete: what every - /// other submission of the device's waits for on the GPU (the module docs), as what it copied - /// may be among what it uses. The host sees it complete once a wait of no time returned for + /// other submission of the device's waits for on the GPU, or follows on its `VkQueue` a wait + /// for (the module docs), as what it copied may be among what it uses. The host sees it complete once a wait of no time returned for /// it. pub(crate) fn staging_wait(&self) -> Option<(vk::Semaphore, u64)> { let shared = self.shared(); diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index a94cedd9d..76b3499c2 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -4898,7 +4898,8 @@ pub struct WorkCounts { pub staging_flushes: u64, /// Batches and internal submissions that waited on the GPU for a staging submission the host /// had not seen complete (`Device::staging_wait`, `device/staging.rs`), in place of a wait on - /// the host. + /// the host: of the batches, at most one per `VkQueue` per staging submission, as every later + /// batch there follows its wait (`command_buffer/submit.rs`). pub staging_gpu_waits: u64, /// New `Shared` and `Managed` textures' images with host-transfer usage placed in memory the /// host does not cache, where the device has none the image may take diff --git a/crates/metal2vulkan-validation/src/runtime_lane/staging_batches.rs b/crates/metal2vulkan-validation/src/runtime_lane/staging_batches.rs index 9540d313d..04b5c3c3c 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/staging_batches.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/staging_batches.rs @@ -211,6 +211,97 @@ fn a_burst_of_replace_regions_shares_one_staging_submission() { }); } +/// One `replace_region` of a 64 MiB `Managed` texture, then `BATCHES` command buffers committed +/// back to back on one queue, each a blit of a 64-texel square of it of its own into a buffer: +/// every blit reads the call's bytes. Where the device copies on its staging queue, the first +/// batch carried the copy and waited for it on the GPU, and the batches after it on the queue, +/// committed while the copy may still run, took no wait of their own: they follow that wait in +/// the `VkQueue`'s submission order, which orders them after the copy (`command_buffer/submit.rs`). +/// So the staging waits are one per staging submission, not one per batch the host submitted +/// before it saw the copy complete. +#[test] +fn batches_after_one_that_waited_for_a_staging_copy_follow_its_wait() { + const SIDE: u64 = 4096; + const SQUARE: u64 = 64; + const BATCHES: u64 = 32; + on_device(|device| { + let name = device.name(); + let queue = device.new_command_queue().unwrap(); + let texture = texture(device, PixelFormat::RGBA8Unorm, SIDE, managed()); + let written = texels(SIDE, 3); + let before = device.work_counts(); + let region = Region::new_2d(0, 0, SIDE, SIDE); + (texture.replace_region(region, 0, 0, &written, 4 * SIDE, 0)).unwrap(); + let burst = Path::between(&before, &device.work_counts()); + let squares: Vec<(u64, u64)> = (0..BATCHES) + .map(|at| (SQUARE * (at % 8), SQUARE * (at / 8))) + .collect(); + let mut last = None; + let outs: Vec = (squares.iter()) + .map(|&(x, y)| { + let command_buffer = queue.command_buffer().unwrap(); + let out = device + .new_buffer(4 * SQUARE * SQUARE, ResourceOptions::shared()) + .unwrap(); + let mut blit = command_buffer.blit_command_encoder().unwrap(); + blit.copy_from_texture_to_buffer( + &texture, + 0, + 0, + Origin::new(x, y, 0), + Size::new(SQUARE, SQUARE, 1), + &out, + 0, + 4 * SQUARE, + 0, + BlitOption::None, + ) + .unwrap(); + blit.end_encoding().unwrap(); + command_buffer.commit().unwrap(); + last = Some(command_buffer); + out + }) + .collect(); + let last = last.unwrap(); + last.wait_until_completed_for(WAIT).unwrap(); + assert_eq!(last.error(), None, "{name}: the blits"); + let read = Path::between(&before, &device.work_counts()); + for (at, (out, &(x, y))) in outs.iter().zip(&squares).enumerate() { + let want: Vec = (0..SQUARE) + .flat_map(|row| { + let from = (4 * ((y + row) * SIDE + x)) as usize; + written[from..from + (4 * SQUARE) as usize].iter().copied() + }) + .collect(); + assert!( + read_buffer(out, 0, want.len() as u64).unwrap() == want, + "{name}: batch {at}'s blit reads the replace_region's bytes" + ); + } + println!( + "STAGING-BATCH {name}: one replace_region of {} MiB {}, then {BATCHES} batches on one \ + queue that read it: the call {burst:?}, with the batches {read:?}", + (4 * SIDE * SIDE) >> 20, + burst.said(1) + ); + if burst.queued > 0 { + assert_eq!( + (read.staging, read.flushes), + (1, 1), + "{name}: the first batch carried the copy in one staging submission" + ); + assert!( + (1..=1 + read.internal).contains(&read.gpu_waits), + "{name}: one batch on the queue waited for the staging submission on the GPU, \ + and the batches after it none (internal submissions wait for it themselves): \ + {} waits", + read.gpu_waits + ); + } + }); +} + /// Overlapping writes into one texture keep their order, whether read by `get_bytes` or by a later /// command buffer, through the texture or a view of it: the whole level written twice, then a /// part of it, then a part through a view; the reads see the last whole write with the two parts