diff --git a/crates/metal2vulkan-runtime/src/command_buffer/group.rs b/crates/metal2vulkan-runtime/src/command_buffer/group.rs index f2dbae3f4..bc972174f 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/group.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/group.rs @@ -1,12 +1,13 @@ //! Batches that go to the driver in one kernel submission (`CompletionPolicy::Grouped`). //! -//! On a driver whose queue is Mesa's common one, each `vkQueueSubmit`'s `VkSubmitInfo`s that -//! signal nothing merge into the next one's kernel submission (Mesa 26.2.4, -//! `vulkan/runtime/vk_queue.c`, `vk_queue_submits_merge`: "Don't merge if there are signals in -//! between"), and ANV chains the merged command buffers into one batch buffer -//! (`anv_queue_submit_cmd_buffers_locked`). Each kernel submission costs CPU time for every -//! `VkDeviceMemory` of the device: ANV on i915 lists each in every execbuffer -//! (`i915/anv_batch_chain.c`, `setup_execbuf_for_cmd_buffers`), RADV each in every CS. +//! On a driver measured to merge each `vkQueueSubmit`'s `VkSubmitInfo`s that signal nothing into +//! the next one's kernel submission ([`DriverFact::SubmissionsWithoutSignalsMerge`], whose +//! documentation holds what was measured where), batches that signal nothing but their queue's +//! timeline may wait and go in one call for the cost of one kernel submission. The question is +//! the driver's, and the fact answers it, never the driver's name: a driver without the fact +//! takes the ungrouped path, each batch its own `vkQueueSubmit`, which is what every batch took +//! before grouping and is exact alike -- a group changes when a `vkQueueSubmit` is made and +//! nothing it submits. //! //! - **What waits.** A batch the scheduler submits waits here, submitted by nothing yet, where it //! signals nothing but its queue's timeline and its events' semaphores: no binary semaphore, @@ -87,8 +88,8 @@ use crate::exit_guard::{self, Call}; use crate::gpu_time; use crate::Device; use ash::vk; -use metal2vulkan::device::CompletionPolicy; -use metal2vulkan::vk::DriverId; +use metal2vulkan::device::{CompletionPolicy, DriverFact}; +use std::collections::BTreeSet; use std::sync::atomic::Ordering; use std::sync::Arc; @@ -104,11 +105,12 @@ const BATCH_INFOS: usize = 2; const MOST_INFOS: usize = GROUP_BATCHES * BATCH_INFOS + 1 + BATCH_INFOS; /// Whether `device`'s batches on `VkQueue` `queue` may wait in a group: its policy groups them, -/// its driver merges a submission that signals nothing into the next (the module docs), and the -/// queue's failures are not relayed (each relayed batch takes a fence, `device/relay.rs`). +/// its driver is measured to merge a submission that signals nothing into the next (the module +/// docs), and the queue's failures are not relayed (each relayed batch takes a fence, +/// `device/relay.rs`). pub(crate) fn groups(device: &Device, queue: usize) -> bool { device.compile_device().policy().completion == CompletionPolicy::Grouped - && merges_submissions(device.properties().description.driver_id) + && merges_submissions(&device.properties().driver_facts) && device.shared().flights(queue).is_none() } @@ -151,23 +153,12 @@ const IN_GROUP: u8 = 1; /// A command buffer's `group_hold` bit: a thread waits for its completion. const WAITED_FOR: u8 = 2; -/// Whether the driver's queue is Mesa's common one with kernel submissions of their own: the -/// drivers that merge a `VkSubmitInfo` that signals nothing into the next. -fn merges_submissions(driver: Option) -> bool { - matches!( - driver, - Some( - DriverId::MESA_RADV - | DriverId::INTEL_OPEN_SOURCE_MESA - | DriverId::MESA_LLVMPIPE - | DriverId::MESA_TURNIP - | DriverId::MESA_V3DV - | DriverId::MESA_PANVK - | DriverId::MESA_NVK - | DriverId::MESA_HONEYKRISP - | DriverId::IMAGINATION_OPEN_SOURCE_MESA - ) - ) +/// Whether the driver merges a `VkSubmitInfo` that signals nothing into the next one's kernel +/// submission ([`DriverFact::SubmissionsWithoutSignalsMerge`]): the driver's facts alone answer +/// it, and nothing of the driver's identity, so a test can hand it the facts of each committed +/// profile (`tests::batches::only_drivers_measured_to_merge_their_submissions_group_their_batches`). +pub(crate) fn merges_submissions(facts: &BTreeSet) -> bool { + facts.contains(&DriverFact::SubmissionsWithoutSignalsMerge) } /// What sent a group held (`WorkCounts::group_ends_full` and kin). diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index 92b82e45f..7ef84e652 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -50,6 +50,11 @@ mod submit; pub(super) use completion::Completion; pub(super) use first_submission::{NotedTable, ReplayTable}; +/// Whether a driver's facts merge a submission that signals nothing into the next, for the test +/// that holds every committed profile to the fact +/// (`tests::batches::only_drivers_measured_to_merge_their_submissions_group_their_batches`). +#[cfg(test)] +pub(crate) use group::merges_submissions; pub(super) use group::{Group, GroupEnd, GROUP_BATCHES}; use rebound_use::{ReboundUse, Unlogged}; pub(super) use submit::{BatchLists, Submitting}; diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs index c3c01f0c5..2a90eafb8 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs @@ -115,8 +115,10 @@ //! of the batch, whether a use names the resource or not (a command may reach a texture through its //! resource ID, a buffer through its address), and the batch holds what it worked on until it //! completes. On a device of several `VkQueue`s every batch on another one waits for it until it -//! completed. It is a command buffer of its own, first in the batch's work's `VkSubmitInfo` (in -//! a batch with no work, its end's), so it waits for what the work waits for: Vulkan's +//! completed. It is recorded first in the command buffer that leads the batch's work's +//! `VkSubmitInfo` (in a batch with no work, its end's) -- its own, or, where the batch also has +//! host calls' staged copies, the one that holds the copies after it -- so it waits for what the +//! work waits for: Vulkan's //! submission order puts it before every command of the batch ("the order in which command //! buffers are specified in the pCommandBuffers member of VkSubmitInfo ... determines their //! submission order", the Vulkan specification, Submission Order), and its last barrier, @@ -681,26 +683,47 @@ impl CommandBufferShared { work.extend(&state.recorded[start + offset].raws); } // The first work goes first in the batch's work, or in a batch with no work in its end - // (the module docs). Host calls' staged copies go at the batch's start, after it. - let lead = match first_work.has_first() { - true => Some(self.record_raw(&mut state, |_, buffer| { - // SAFETY: the command buffer is recording, outside a render pass; it is - // submitted first in the batch, before any later one, and every batch on - // another `VkQueue` waits for the batch (`FirstMoves::waits`). - unsafe { first_work.record(raw, buffer, shared.queue_flags_of(queue)) }; - Ok(()) - })?), - false => None, + // (the module docs). Host calls' staged copies go at the batch's start, after it, in the + // same command buffer: the two went in two command buffers of one `VkSubmitInfo`, which + // puts them in the same submission order one command buffer's recording order does ("the + // order in which command buffers are specified in the pCommandBuffers member of + // VkSubmitInfo ... determines their submission order", the Vulkan specification, + // Submission Order; inside one command buffer, the order they were recorded), and a + // command buffer boundary is no dependency of its own, so the copies' own leading barrier + // (`FirstMoves::record_staged`) covers the moves recorded before it either way. One + // command buffer where the batch has both (counted: + // `WorkCounts::command_buffers_begun_batch_work`). + let has_first = first_work.has_first(); + let has_staged = first_work.has_staged(); + let recorded = (has_first || has_staged) + .then(|| { + self.record_raw(&mut state, |_, buffer| { + if has_first { + // SAFETY: the command buffer is recording, outside a render pass; it is + // submitted first in the batch, before any later one, and every batch on + // another `VkQueue` waits for the batch (`FirstMoves::waits`). + unsafe { first_work.record(raw, buffer, shared.queue_flags_of(queue)) }; + } + if has_staged { + // SAFETY: the command buffer is recording, outside a render pass; every + // image's move is in this command buffer, before it, or a submission + // before. + unsafe { first_work.record_staged(raw, buffer) }; + } + Ok(()) + }) + }) + .transpose()?; + // Where it holds staged copies it goes at the batch's start, as they did, so the batch + // has work; the first work alone goes first in the work, or in the end where the batch + // has none (below). + let lead = match has_staged { + true => { + work.insert(0, recorded.expect("recorded where there are staged copies")); + None + } + false => recorded, }; - if first_work.has_staged() { - let staged = self.record_raw(&mut state, |_, buffer| { - // SAFETY: the command buffer is recording, outside a render pass; every image's - // move is in the first work's command buffer, before it, or a submission before. - unsafe { first_work.record_staged(raw, buffer) }; - Ok(()) - })?; - work.insert(0, staged); - } let BatchWaits { semaphores: wait_semaphores, values: wait_values, diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs index 31c3b819c..c10797059 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -1605,11 +1605,18 @@ impl Device { .collect(); exit_guard::register_device(identity); let in_process = properties.kind == DeviceKind::Cpu; - // A device on MoltenVK whose instance has the relay has its queues' failures relayed - // to the command buffers that failed (`relay.rs`), from its first batch on. + // A device whose driver reports a command buffer's execution failure through the + // instance's messenger alone, and whose instance has the relay, has its queues' failures + // relayed to the command buffers that failed (`relay.rs`), from its first batch on. The + // question is the driver's, and the fact answers it, never the driver's name: a defect + // fact, so a driver with a row takes it at every release and on every GPU. let lost = Arc::new(AtomicBool::new(false)); + let relays = (properties.description.driver_facts()) + .contains(&metal2vulkan::device::DriverFact::QueueFailuresReportedByMessenger); + // Read before the properties move into the device's shared state (below): still keyed on + // the driver's name, for the scheduler's wait alone. let moltenvk = properties.description.driver_id == Some(DriverId::MOLTENVK); - if let (Some(relay), true) = (instance.relay(), moltenvk) { + if let (Some(relay), true) = (instance.relay(), relays) { for slot in &queues { let queue = slot.with(|queue| queue); // SAFETY: the queue is the device's, live, and its word was put back just now. @@ -1756,7 +1763,9 @@ impl Device { } // The scheduler's thread waits for the GPU itself where heads wait for signals to land, // but on MoltenVK, whose wait for any of several semaphores loses a signal that lands - // while it registers a later one (`command_buffer/completion.rs`). + // while it registers a later one (`command_buffer/completion.rs`). Still keyed on the + // driver's name: its home is a `RuntimeDriverDefect` beside + // `ReadsCommandBuffersAfterTheirSignal`, which this commit does not open. if !moltenvk { (shared.scheduler).wait_on_gpu(Functions::clone(&shared.functions), shared.nudge); } diff --git a/crates/metal2vulkan-runtime/src/device/submit.rs b/crates/metal2vulkan-runtime/src/device/submit.rs index 6c0a7c741..c72b4d5ea 100644 --- a/crates/metal2vulkan-runtime/src/device/submit.rs +++ b/crates/metal2vulkan-runtime/src/device/submit.rs @@ -8,8 +8,8 @@ //! instead (`command_buffer/submit.rs`). Each first does the //! new resources' first GPU work that waits (`texture/first_move.rs`), after the batches on other //! `VkQueue`s that did such work: no new resource waits for one, so making one waits for no GPU -//! work. That work goes in a command buffer of its own, first in the work's `VkSubmitInfo`, as a -//! batch's does (`command_buffer/submit.rs`). +//! work. That work is recorded first in the submission's one command buffer, before the host +//! calls' staged copies and the work, as a batch's is in its own (`command_buffer/submit.rs`). //! //! The one submission no one waits for is a no-copy buffer's sparse binding of its dma-bufs //! (`dma_buf.rs`), on the same queue and timeline: it would wait behind the queue's work in @@ -71,9 +71,9 @@ impl OwnTimeline { /// The command pool, command buffers and fence of internal submissions, made on first use. pub(super) struct Internal { pool: vk::CommandPool, + /// The one command buffer a submission records: its first work, the host calls' staged + /// copies and its work, in that order (`submit_internal_naming`). buffer: vk::CommandBuffer, - /// The command buffer of a submission's first work, first in the work's `VkSubmitInfo`. - first: vk::CommandBuffer, fence: vk::Fence, /// The timeline each submission signals, and the value the last one signalled: a batch on /// another `VkQueue` waits for one that did first work (`texture/first_move.rs`). @@ -96,9 +96,9 @@ impl Internal { let allocate = vk::CommandBufferAllocateInfo::default() .command_pool(pool) .level(vk::CommandBufferLevel::PRIMARY) - .command_buffer_count(2); + .command_buffer_count(1); let buffers = device.allocate_command_buffers(&allocate)?; - let (buffer, first) = (buffers[0], buffers[1]); + let buffer = buffers[0]; // SAFETY: the device is live. let fence = unsafe { raw.create_fence(&vk::FenceCreateInfo::default(), None) } .map_err(vulkan)?; @@ -119,7 +119,6 @@ impl Internal { Ok(Internal { pool, buffer, - first, fence, timeline, signalled: 0, @@ -183,36 +182,35 @@ impl Device { let waits = firsts.waits(0, |point| { shared.queues[point.queue].completed_value() >= point.value }); - // The first work goes in a command buffer of its own, first in the work's `VkSubmitInfo` - // (`command_buffer/submit.rs`: MoltenVK runs each `VkSubmitInfo` as a Metal command - // buffer of its own). - let lead = taken.has_first(); - if lead { - // SAFETY: as below, for the first work's command buffer. - unsafe { raw.begin_command_buffer(objects.first, &begin) }.map_err(vulkan)?; - shared.counts.command_buffer_begun(Begun::Internal); - // SAFETY: recording, outside a render pass, for the queue family; it is submitted - // first in the work's submission, which is waited for before anything that uses the - // resources is submitted after it. - unsafe { taken.record(raw, objects.first, shared.queue_flags_of(0)) }; - // SAFETY: as above. - unsafe { raw.end_command_buffer(objects.first) }.map_err(vulkan)?; - } // SAFETY: the command buffer is not pending (every submission is waited for below) and // the internal lock is held; beginning it resets it (the pool resets command buffers). unsafe { raw.begin_command_buffer(objects.buffer, &begin) }.map_err(vulkan)?; shared.counts.command_buffer_begun(Begun::Internal); - // Host calls' staged copies, after the first work's command buffer, before the work. - // SAFETY: recording, outside a render pass; every image's move is in the first work's - // submission or one before. + // The new resources' first work goes first in this command buffer, then the host calls' + // staged copies, then the work. The first work had a command buffer of its own, first in + // the same `VkSubmitInfo`, which put it in the same submission order recording it first + // here does ("the order in which command buffers are specified in the pCommandBuffers + // member of VkSubmitInfo ... determines their submission order", the Vulkan + // specification, Submission Order; inside one command buffer, the order they were + // recorded), and a command buffer boundary is no dependency of its own, so what each + // records and the barriers between them are unchanged. Counted: + // `WorkCounts::command_buffers_begun_internal`. + if taken.has_first() { + // SAFETY: recording, outside a render pass, for the queue family; it is submitted + // first in the work's submission, which is waited for before anything that uses the + // resources is submitted after it. + unsafe { taken.record(raw, objects.buffer, shared.queue_flags_of(0)) }; + } + // Host calls' staged copies, after the first work, before the work. + // SAFETY: recording, outside a render pass; every image's move is recorded before it + // here, or in a submission before. unsafe { taken.record_staged(raw, objects.buffer) }; record(raw, objects.buffer); // SAFETY: as above. unsafe { raw.end_command_buffer(objects.buffer) }.map_err(vulkan)?; // SAFETY: the fence is unsignaled or signaled and not pending. unsafe { raw.reset_fences(&[objects.fence]) }.map_err(vulkan)?; - let buffers = [objects.first, objects.buffer]; - let buffers = &buffers[usize::from(!lead)..]; + let buffers = [objects.buffer]; // And the last binding of a no-copy buffer's dma-bufs, until it completed. let waits: Vec<(vk::Semaphore, u64)> = (waits.iter()) .map(|point| (shared.queues[point.queue].timeline, point.value)) @@ -232,7 +230,7 @@ impl Device { let submit = vk::SubmitInfo::default() .wait_semaphores(&wait_semaphores) .wait_dst_stage_mask(&wait_stages) - .command_buffers(buffers) + .command_buffers(&buffers) .signal_semaphores(&signal_semaphores) .push_next(&mut timeline); // The first work waits as the work's does (a heap's fill another queue did comes before diff --git a/crates/metal2vulkan-runtime/src/tests/batches.rs b/crates/metal2vulkan-runtime/src/tests/batches.rs index 89b802696..23be71990 100644 --- a/crates/metal2vulkan-runtime/src/tests/batches.rs +++ b/crates/metal2vulkan-runtime/src/tests/batches.rs @@ -1,7 +1,12 @@ //! A command buffer's batches (`command_buffer/batches.rs`): where event waits and signals, and -//! binary semaphore waits and signals, split its encoders. +//! binary semaphore waits and signals, split its encoders; which drivers' batches may wait in +//! a group (`command_buffer/group.rs`); and which drivers' queue failures are relayed +//! (`device/relay.rs`). use crate::command_buffer::batches::{Batch, Batches}; +use crate::command_buffer::merges_submissions; +use metal2vulkan::device::{DeviceDescription, DriverFact}; +use metal2vulkan::vk::DriverId; fn batch(waits: &[(char, u64)], end: usize, signals: &[(char, u64)]) -> Batch { Batch { @@ -238,3 +243,106 @@ fn signals_after_a_failing_encoder_are_dropped() { ] ); } + +/// Whether a driver's batches may wait in one kernel submission is the driver's **fact**, never +/// its name (`command_buffer/group.rs`): a driver measured to merge a `vkQueueSubmit`'s +/// `VkSubmitInfo`s that signal nothing into the next one's kernel submission +/// (`DriverFact::SubmissionsWithoutSignalsMerge`) groups its batches; every other driver takes the +/// ungrouped path, each batch its own call, which is exact alike. +/// +/// The check over every committed profile: a profile groups only where its driver is one the fact +/// is measured of, so a decision that read the driver's name -- the Mesa drivers no lane measured +/// read alike there -- would answer otherwise for one of them. A `MEASURED` row written without a +/// measurement would show here as a MoltenVK, NVIDIA or SwiftShader profile that groups. +#[test] +fn only_drivers_measured_to_merge_their_submissions_group_their_batches() { + let dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../metal2vulkan-validation/profiles"); + let mut read = 0; + let mut grouped = 0; + for entry in std::fs::read_dir(&dir).expect("validation/profiles is readable") { + let path = entry.expect("directory entry").path(); + if path.extension().is_none_or(|x| x != "json") { + continue; + } + let text = std::fs::read_to_string(&path).expect("profile is readable"); + let description = + DeviceDescription::from_khronos_json(&text, None).expect("profile parses"); + let merges = merges_submissions(&description.driver_facts()); + if merges { + // Only a driver the fact is measured of: Mesa's RADV, ANV and llvmpipe. + assert!( + matches!( + description.driver_id, + Some( + DriverId::MESA_RADV + | DriverId::INTEL_OPEN_SOURCE_MESA + | DriverId::MESA_LLVMPIPE + ) + ), + "{} groups its batches with no row of its driver", + path.display() + ); + grouped += 1; + } + read += 1; + } + assert!(read > 1, "profiles read: {read}"); + // The grouped form is reachable, and the fact alone chooses it. + assert!(grouped > 0, "profiles that group: {grouped}"); + assert!(merges_submissions( + &[DriverFact::SubmissionsWithoutSignalsMerge] + .into_iter() + .collect() + )); + assert!(!merges_submissions(&Default::default())); +} + +/// Whether a device's queue failures are relayed to the command buffers that failed is the +/// driver's **fact**, never its name (`device/mod.rs`, `device/relay.rs`): a driver that reports +/// a command buffer's execution failure through the instance's messenger alone, and loses the +/// whole device for it, takes the relay +/// (`DriverFact::QueueFailuresReportedByMessenger`); every other driver takes none, and its +/// batches take a fence only where their command buffer ends at one. +/// +/// It is a **defect** fact, so a driver with a row takes it at every release and on every GPU +/// (`DriverFact::assumed_where_unmeasured`): that is what makes the fact answer exactly as the +/// driver's name did, and the check is that MoltenVK's profiles take it while every other +/// committed profile -- RADV's, ANV's, NVIDIA's, lavapipe's and SwiftShader's -- does not, +/// including at a release and on a GPU no lane measured. +#[test] +fn only_drivers_that_report_their_failures_by_messenger_relay_them() { + let dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../metal2vulkan-validation/profiles"); + let mut moltenvk = 0; + let mut other = 0; + for entry in std::fs::read_dir(&dir).expect("validation/profiles is readable") { + let path = entry.expect("directory entry").path(); + if path.extension().is_none_or(|x| x != "json") { + continue; + } + let text = std::fs::read_to_string(&path).expect("profile is readable"); + let mut description = + DeviceDescription::from_khronos_json(&text, None).expect("profile parses"); + let is_moltenvk = description.driver_id == Some(DriverId::MOLTENVK); + for release in [description.driver_version, 0x4242] { + description.driver_version = release; + let relays = (description.driver_facts()) + .contains(&DriverFact::QueueFailuresReportedByMessenger); + assert_eq!( + relays, + is_moltenvk, + "{} relays {relays} at {release:#x}", + path.display() + ); + } + match is_moltenvk { + true => moltenvk += 1, + false => other += 1, + } + } + assert!( + moltenvk > 0 && other > 1, + "{moltenvk} MoltenVK, {other} other" + ); +} diff --git a/crates/metal2vulkan-runtime/src/texture/first_move.rs b/crates/metal2vulkan-runtime/src/texture/first_move.rs index db7d604ed..3163d2b95 100644 --- a/crates/metal2vulkan-runtime/src/texture/first_move.rs +++ b/crates/metal2vulkan-runtime/src/texture/first_move.rs @@ -41,10 +41,11 @@ //! takes such a move whatever its //! commands name, so no command reads the image before its move; an internal submission, whose //! commands are the runtime's own, takes it only where they name the image's key (a host call -//! on its texture, whose contract orders the program's GPU work on it). The work goes in -//! a command buffer of its own, first in the submission's `VkSubmitInfo`, waiting for what -//! the submission waits for: MoltenVK runs each `VkSubmitInfo` as a Metal command buffer of its -//! own, so it adds none (`command_buffer/submit.rs`). +//! on its texture, whose contract orders the program's GPU work on it). The work is recorded +//! first in the command buffer that leads the submission's `VkSubmitInfo` -- a batch's own, or +//! the one that holds the staged copies after it; an internal submission's one command buffer +//! -- waiting for what the submission waits for: MoltenVK runs each `VkSubmitInfo` as a Metal +//! command buffer of its own, so it adds none (`command_buffer/submit.rs`). //! - **Staged copies**. `replace_region` of a texture whose `VkImage` is its own for good (in //! memory of its own, or a heap's own pages) and takes no host copies stages the bytes //! ([`TexelCopy`], [`FirstMoves::register_texels`]): the next submission copies them in at @@ -1367,13 +1368,14 @@ impl Taken<'_> { } /// Record the host calls' staged copies it took into `command_buffer`, each after every - /// write before it (the first work, which a command buffer submitted before it records; the - /// copy before), then the barrier that makes them visible to every command after and to the - /// host. + /// write before it (the first work, which [`record`](Self::record) records into this command + /// buffer before it or a submission before records; the copy before), then the barrier that + /// makes them visible to every command after and to the host. /// /// # Safety /// - /// As [`record`](Self::record)'s; every image's first move was submitted before. + /// As [`record`](Self::record)'s; every image's first move is recorded before it in + /// submission order -- earlier in this command buffer, or in a submission before. pub(crate) unsafe fn record_staged(&self, device: &Vk, command_buffer: vk::CommandBuffer) { let staged = self.held.iter().filter_map(|held| match held { Held::Texels(_, copy) => Some(copy), diff --git a/crates/metal2vulkan-validation/src/runtime_lane/grouped_submissions.rs b/crates/metal2vulkan-validation/src/runtime_lane/grouped_submissions.rs index 75e02474c..a42931e6a 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/grouped_submissions.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/grouped_submissions.rs @@ -4,9 +4,10 @@ //! [`BATCHES`] command buffers committed on one queue, each filling a buffer of its own with a //! value of its own, the first after a wait for an event the host has not raised, all wait behind //! that wait; once the host raises it the scheduler finds them ready together. Under the default -//! `CompletionPolicy::Grouped`, on a driver whose queue merges a submission that signals nothing -//! into the next one (Mesa's: ANV, RADV, lavapipe and kin), they go in `vkQueueSubmit` calls of up -//! to four batches each, only the last of each signalling (`WorkCounts::grouped_submissions`, +//! `CompletionPolicy::Grouped`, on a driver measured to merge a submission that signals nothing +//! into the next one (`DriverFact::SubmissionsWithoutSignalsMerge`), they go in `vkQueueSubmit` +//! calls of up to four batches each, only the last of each signalling +//! (`WorkCounts::grouped_submissions`, //! `WorkCounts::batches_grouped`), announced once ([`GROUPED`]); under `CompletionPolicy::Exact`, //! and on every other driver, each alone, announcing nothing. Either way every command buffer //! completes without error, each buffer holds its own value, and the layer reports nothing. Each @@ -43,8 +44,7 @@ use super::degrade_notices::counts; use super::{loader, open_device, validated_device_with, Messages, ScratchCache, WAIT}; use crate::gpu::TEST_DEVICE; -use metal2vulkan::device::CompletionPolicy; -use metal2vulkan::vk::DriverId; +use metal2vulkan::device::{CompletionPolicy, DeviceDescription, DriverFact}; use metal2vulkan_runtime::{ Buffer, CommandBuffer, CommandBufferStatus, Degradation, DeviceOptions, EventRef, ResourceOptions, ShaderCacheOptions, WorkCounts, @@ -79,23 +79,12 @@ fn group_ends(before: &WorkCounts, after: &WorkCounts) -> [u64; 7] { std::array::from_fn(|at| after[at] - before[at]) } -/// Whether the device's driver merges a submission that signals nothing into the next: its queue -/// is Mesa's common one (the runtime's `group.rs`). -fn merges(driver: Option) -> bool { - matches!( - driver, - Some( - DriverId::MESA_RADV - | DriverId::INTEL_OPEN_SOURCE_MESA - | DriverId::MESA_LLVMPIPE - | DriverId::MESA_TURNIP - | DriverId::MESA_V3DV - | DriverId::MESA_PANVK - | DriverId::MESA_NVK - | DriverId::MESA_HONEYKRISP - | DriverId::IMAGINATION_OPEN_SOURCE_MESA - ) - ) +/// Whether the device's driver merges a submission that signals nothing into the next: the +/// driver's fact answers it, never its name, and the runtime reads the same fact +/// (`DriverFact::SubmissionsWithoutSignalsMerge`, the runtime's `group.rs`), so a device whose +/// identity no lane measured takes the ungrouped path and this lane expects it. +fn merges(description: &DeviceDescription) -> bool { + (description.driver_facts()).contains(&DriverFact::SubmissionsWithoutSignalsMerge) } /// Wait until the scheduler's thread has taken in every command buffer committed before the call: @@ -132,7 +121,7 @@ fn run(completion: CompletionPolicy, checked: bool) -> (u64, u64, u64, usize, bo options.policy.completion = completion; }) .unwrap(); - let merging = merges(device.properties().description.driver_id); + let merging = merges(&device.properties().description); let queue = device.new_command_queue().unwrap(); let event = device.new_shared_event().unwrap(); let buffers: Vec = (0..BATCHES) @@ -394,7 +383,7 @@ fn event_scenario(completion: CompletionPolicy, steps: &[Step], last: u64) -> Sc options.policy.completion = completion; }) .unwrap(); - let merging = merges(device.properties().description.driver_id); + let merging = merges(&device.properties().description); let queues = [ device.new_command_queue().unwrap(), device.new_command_queue().unwrap(), @@ -863,7 +852,7 @@ fn behind_running_work(completion: CompletionPolicy, stretch: usize, carried: Ca options.policy.completion = completion; }) .unwrap(); - let merging = merges(device.properties().description.driver_id); + let merging = merges(&device.properties().description); let queue = device.new_command_queue().unwrap(); let large = device .new_buffer(RUNNING_FILL, ResourceOptions::private()) diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 66ce2428e..17a76131a 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -93,7 +93,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 448; /// 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 = 1552; +pub const RUNTIME_VERSION: u32 = 1554; /// 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 diff --git a/crates/metal2vulkan/src/device/driver_facts.rs b/crates/metal2vulkan/src/device/driver_facts.rs index 5f76ae8db..5afc64cce 100644 --- a/crates/metal2vulkan/src/device/driver_facts.rs +++ b/crates/metal2vulkan/src/device/driver_facts.rs @@ -1543,6 +1543,57 @@ pub enum DriverFact { /// with a 1 made a second `VkPipeline` there (Metal's specialization and pipeline compile, /// 5.6 to 24.5 ms). ConstantOffsetUniformReadsPreloaded, + /// A cost fact (the module docs): the driver's queue is Mesa's common one, where each + /// `vkQueueSubmit`'s `VkSubmitInfo`s that signal nothing merge into the next one's kernel + /// submission, so batches that signal nothing but their queue's timeline may wait and go in + /// one call for the cost of one kernel submission (Mesa 26.2.4, `vulkan/runtime/vk_queue.c`, + /// `vk_queue_submits_merge`: "Don't merge if there are signals in between"; ANV chains the + /// merged command buffers into one batch buffer, + /// `anv_queue_submit_cmd_buffers_locked`). Each kernel submission costs CPU time for every + /// `VkDeviceMemory` of the device there: ANV on i915 lists each in every execbuffer + /// (`i915/anv_batch_chain.c`, `setup_execbuf_for_cmd_buffers`), RADV each in every CS. On a + /// driver without it each batch takes its own `vkQueueSubmit`, which is the exact path and + /// the one every batch took before grouping: a group changes when a batch's `vkQueueSubmit` + /// is made and nothing it submits (`command_buffer/group.rs` in the runtime). + /// + /// Counted where the merge pays, on Mesa 26.2.4's ANV on the Intel UHD 770: 24 kernel + /// submissions a frame grouped against 30 ungrouped, at 67 command buffers a frame committed + /// 1 ms apart behind 1.3 ms of GPU work each, the GPU as idle either way (0.62 ms a frame). + /// + /// Measured without it, so not listed: MoltenVK, whose queue is its own and merges nothing + /// -- it commits a Metal command buffer for each `VkSubmitInfo` of a call and one for a call + /// of none with a fence (`MVKQueue::submit`), about 65 us of a thread of Metal's on an Apple + /// M3 Max whether or not the info holds work -- and NVIDIA's proprietary driver, which has + /// no Mesa queue. The Mesa drivers no lane measures (Turnip, V3DV, PanVK, NVK, Honeykrisp, + /// PowerVR) take no row: the merge is in the common queue they share, but no identity of + /// theirs was measured, so each takes the ungrouped exact path. + SubmissionsWithoutSignalsMerge, + /// A defect fact (the module docs): the driver reports a command buffer's execution failure + /// only to the instance's debug-utils messengers, naming the `VkQueue` and not the + /// submission, and by default marks the whole `VkDevice` lost for it, where Metal fails the + /// command buffer it struck and runs the queue on. The runtime's workaround is the relay + /// (`device/relay.rs`): its instance asks the driver not to lose the device + /// (`MVK_CONFIG_RESUME_LOST_DEVICE`), each `VkQueue` keeps its submissions in flight in + /// submission order with a fence each, and a failure heard on a queue is given to the first + /// submission whose fence is unsignaled, so the command buffer whose batch it was finishes + /// with that error and the queue's other command buffers complete, as Metal's do. + /// + /// Measured on MoltenVK 1.4.1: `handleMTLCommandBufferError` in `MVKQueue.mm` reports + /// `": Lost|Resumed VkDevice after MTLCommandBuffer \"