From dbd332e087a094121bda36c4aff890ee714ef433 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 26 Nov 2026 22:46:01 -0800 Subject: [PATCH] Runtime tests: each part of a group sent apart hands its own Direct3D 11 planes over A group sent member by member is one vkQueueSubmit2 a part, each part signalling a progress value of its own, and each command buffer retires at its own part's value. The Direct3D 11 planes' hand-over is still per group: one acquire ahead of the first part and one release at the end of the last, so a member ahead of the last that used a plane is seen complete before that plane's release has run, and the consumer's precondition (rt's command buffers that use a plane have completed) lets Direct3D 11 take the plane while rt still owns it (Vulkan 1.4, section 12.10.1: "Ensure the release operation has completed"). Found by reading; whether any run met it is not known. These tests state the per-part form and fail on this tree: - plane_parts (no device, every leg): the part builder, now its own function (`submit::parts`) with the per-group placement unchanged, given a group of an upload and two leads and the planes each part uses. Each plane-using part must acquire at its head and release at its end, the release taking the part's signals and progress value; a part using no plane takes neither; the uploads' planes go with the part ahead of the first lead. The negative control, a group sent as one part, hands over once ahead of everything and once after, as today, and passes here. - every_device_hands_direct3d_11_planes_over_in_each_part_of_a_group_sent_apart (Windows, m2v_gpu): behind a queue's busy work, a read of the planes and a second command buffer join one group; a run of 1 s seen before it goes (QueueInner::ran, the state retirements set) sends it member by member. Two reads must hand over twice each way and make two vkQueueSubmit2 calls; a read and a buffer copy once each way in two calls. Sent as one part (send_now), the two reads hand over once: the control. The extraction moves no command buffer: the acquire still leads the uploads and the release still takes the last part's last signals. --- crates/metal2vulkan-runtime/src/submit.rs | 176 +++++++++++------- .../metal2vulkan-runtime/src/tests/d3d11.rs | 124 +++++++++++- crates/metal2vulkan-runtime/src/tests/mod.rs | 1 + .../src/tests/plane_parts.rs | 170 +++++++++++++++++ 4 files changed, 401 insertions(+), 70 deletions(-) create mode 100644 crates/metal2vulkan-runtime/src/tests/plane_parts.rs diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index 2f3bbec73..6f32a544e 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -2548,6 +2548,90 @@ pub(crate) fn put_ahead( kept } +/// A submission's parts, each one `vkQueueSubmit2` with the progress value it signals, from its +/// `segments`, the first `ahead` of them its uploads': one part, or, where the group goes member +/// by member, `leads` holding where each command buffer that leads in it begins among the +/// segments past the uploads, one ahead of the first lead's, with the uploads, and one for each +/// lead (`WorkState::close`). The last part's value is `progress`'s `value`, which also covers +/// the parts left empty, and each part's one above the one before. A segment with no commands +/// and no signals is left out, and so is a part left empty. +/// +/// `planes[p]` holds the Direct3D 11 planes part `p` uses, and `hand` the command buffer that +/// acquires (`true`) or releases (`false`) such planes ([`Upload::hand_over`]), None where its +/// make failed: the planes of every part are acquired ahead of everything and released after +/// everything, the release taking the last part's last signals, so it adds no submit info +/// ([`infos`]). +pub(crate) fn parts( + mut segments: Vec, + ahead: usize, + leads: &[usize], + (progress, value): (vk::Semaphore, u64), + planes: &[Vec

], + mut hand: impl FnMut(&[P], bool) -> Option, +) -> Vec<(u64, Vec)> { + let staged = |command: vk::CommandBuffer| Staged { + command: Some(vk::CommandBufferSubmitInfo::default().command_buffer(command)), + ..Staged::default() + }; + let all: Vec

= planes.iter().flatten().cloned().collect(); + let (head, tail) = match all.is_empty() { + true => (None, None), + false => (hand(&all, true), hand(&all, false)), + }; + let mut ahead = ahead; + if let Some(head) = head { + segments.insert(0, staged(head)); + ahead += 1; + } + // Each part's progress value: the part ahead of the first lead's `value` less the leads, + // each lead's one above the one before. + let count = leads.len(); + let mut parts: Vec<(u64, Vec)> = Vec::with_capacity(1); + for (lead, &start) in leads.iter().enumerate().rev() { + let at = value - (count - 1 - lead) as u64; + parts.push((at, segments.split_off(start + ahead))); + } + parts.push((value - count as u64, segments)); + parts.reverse(); + for (_, part) in &mut parts { + part.retain(|staged| staged.command.is_some() || !staged.signals.is_empty()); + } + parts.retain(|(_, part)| !part.is_empty()); + // The release goes after every segment and takes its last signals, so it adds no submit info + // (`infos`). + if let Some(tail) = tail { + let last = (parts.last_mut()) + .and_then(|(_, part)| part.pop()) + .unwrap_or_default(); + let release = Staged { + signals: last.signals, + ..staged(tail) + }; + let last = Staged { + signals: Vec::new(), + ..last + }; + match parts.last_mut() { + Some((_, part)) => part.extend([last, release]), + None => parts.push((value, vec![last, release])), + } + } + match parts.last_mut() { + Some((last, _)) => *last = value, + None => parts.push((value, vec![Staged::default()])), + } + for (at, part) in &mut parts { + let signal = vk::SemaphoreSubmitInfo::default() + .semaphore(progress) + .value(*at) + .stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS); + if let Some(last) = part.last_mut() { + last.signals.push(signal); + } + } + parts +} + /// Orders the zero fills of the uploads one submission carries, oldest first: where a fill an /// upload records before any barrier of its own meets bytes a fill of an earlier one wrote since /// the last barrier, a barrier ends the upload before it, so the two do not write the same bytes @@ -2676,20 +2760,6 @@ fn submit(core: &Core, value: u64, uploads: Vec, batch: Batch) { } } } - let (head, tail) = match planes.is_empty() { - true => (None, None), - false => { - let (head, began) = Upload::hand_over(core, &planes, true); - let (tail, ended) = Upload::hand_over(core, &planes, false); - ok = ok.and(began).and(ended); - (head, tail) - } - }; - if let Some(head) = head { - uploads.insert(0, head); - core.counters.count(Metric::D3d11PlaneSubmissions); - } - let ahead = uploads.len(); segments.splice( 0..0, uploads.iter().map(|upload| Staged { @@ -2697,60 +2767,34 @@ fn submit(core: &Core, value: u64, uploads: Vec, batch: Batch) { ..Staged::default() }), ); - // One part, or, where the group goes member by member, one for each command buffer that leads - // in it, the uploads with the first; each with its progress value (`WorkState::close`): the - // part ahead of the first lead's `value` less the leads, each lead's one above the one before. let leads = match apart { - true => starts.len(), - false => 0, + true => &starts[..], + false => &[][..], }; - let mut parts: Vec<(u64, Vec)> = Vec::with_capacity(1); - for (lead, &start) in starts[..leads].iter().enumerate().rev() { - let at = value - (leads - 1 - lead) as u64; - parts.push((at, segments.split_off(start + ahead))); - } - parts.push((value - leads as u64, segments)); - parts.reverse(); - for (_, part) in &mut parts { - part.retain(|staged| staged.command.is_some() || !staged.signals.is_empty()); - } - parts.retain(|(_, part)| !part.is_empty()); - // The release goes after every segment and takes its last signals, so it adds no submit info - // (`infos`). - if let Some(mut tail) = tail { - let last = (parts.last_mut()) - .and_then(|(_, part)| part.pop()) - .unwrap_or_default(); - let release = Staged { - waits: Vec::new(), - command: Some(vk::CommandBufferSubmitInfo::default().command_buffer(tail.command)), - signals: last.signals, - }; - let last = Staged { - signals: Vec::new(), - ..last - }; - match parts.last_mut() { - Some((_, part)) => part.extend([last, release]), - None => parts.push((value, vec![last, release])), - } - tail.held.extend(planes.into_iter().map(Held::Texture)); - uploads.push(tail); - } - // Each part signals its value, the last `value`, which also covers the parts left empty. - match parts.last_mut() { - Some((last, _)) => *last = value, - None => parts.push((value, vec![Staged::default()])), - } - for (at, part) in &mut parts { - let progress = vk::SemaphoreSubmitInfo::default() - .semaphore(core.done.progress) - .value(*at) - .stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS); - if let Some(last) = part.last_mut() { - last.signals.push(progress); - } - } + let mut handed = Vec::new(); + let planes = [planes]; + let mut parts = self::parts( + segments, + uploads.len(), + leads, + (core.done.progress, value), + &planes, + |planes, acquire| { + let (upload, done) = Upload::hand_over(core, planes, acquire); + if let (true, Err(loss)) = (ok.is_ok(), done) { + ok = Err(loss); + } + let mut upload = upload?; + match acquire { + true => core.counters.count(Metric::D3d11PlaneSubmissions), + false => (upload.held).extend(planes.iter().cloned().map(Held::Texture)), + } + let command = upload.command; + handed.push(upload); + Some(command) + }, + ); + uploads.extend(handed); if ok.is_ok() { // The host's writes to the Shared memory its command buffers and copy-ins reach are the // GPU's before it runs, where the Shared type is not host-coherent (`crate::coherence`). diff --git a/crates/metal2vulkan-runtime/src/tests/d3d11.rs b/crates/metal2vulkan-runtime/src/tests/d3d11.rs index 0e4caee78..a37573104 100644 --- a/crates/metal2vulkan-runtime/src/tests/d3d11.rs +++ b/crates/metal2vulkan-runtime/src/tests/d3d11.rs @@ -124,12 +124,15 @@ mod on_device { mod picture { use super::d3d::{self, D3d, SharedTexture}; use super::{RENDER_TARGET, SHADER_RESOURCE}; - use crate::tests::command::on_device::{each_device, finished, on_each_device, within}; + use crate::submit::IN_FLIGHT; + use crate::tests::command::on_device::{ + behind_busy, each_device, finished, on_each_device, within, + }; use crate::texture::TextureObject; use crate::{ - Buffer, BufferLayout, CommandBuffer, D3d11Plane, Device, Error, Gap, IndexClass, - MemoryClass, PixelFormat, Surface, TextureDescriptor, TexturePlace, TextureSwizzle, - TextureSwizzleChannels, TextureType, TextureUsage, TextureViewKind, + Buffer, BufferLayout, CommandBuffer, CommandBufferStatus, D3d11Plane, Device, Error, + Gap, IndexClass, MemoryClass, PixelFormat, Surface, TextureDescriptor, TexturePlace, + TextureSwizzle, TextureSwizzleChannels, TextureType, TextureUsage, TextureViewKind, }; use ash::vk; use metal2vulkan::metal::format::srgb_sibling; @@ -546,6 +549,119 @@ mod on_device { }); } + /// The planes' hand-over in each part of a group sent member by member. On one queue, + /// behind as many busy submissions as a queue keeps in flight, a read of the planes + /// Direct3D 11 drew (A) and a second command buffer join one group: a second read (B), or + /// a copy between two buffers that uses no plane (F). Sent at once at the queue's run + /// (`CommandQueue::send_now`), A and B go as one part: one `vkQueueSubmit2`, one acquire + /// and one release. Sent once a run of 1 s was seen (`QueueInner::ran`, the state the + /// completion thread sets as a command buffer retires), the group goes member by member, + /// one `vkQueueSubmit2` a member and none more: A and B each acquire the planes at their + /// part's head and release them at its end, two of each; A and F acquire and release + /// once, in A's part, so A, seen complete at its own part's value, is seen complete after + /// the planes' release. Every read is picture 1's exactly. The order within each part is + /// the part builder's (`plane_parts`). + #[test] + fn every_device_hands_direct3d_11_planes_over_in_each_part_of_a_group_sent_apart() { + each_device("import_d3d11_surface_apart", |name, _, device| { + let luid = luid(&device).unwrap_or_else(|| panic!("{name}: no valid LUID")); + let d3d = D3d::on_luid(luid) + .unwrap_or_else(|| panic!("{name}: no DXGI adapter of LUID {luid:02x?}")); + let textures = PLANES + .each_ref() + .map(|p| d3d.shared_texture(p.width, p.height, p.dxgi, BOUND)); + let surface = import(name, &device, &textures); + d3d_draw(&d3d, &textures, 1); + surface.external_writes_done().expect("the acquire"); + let one = picture(1); + let ahead = IN_FLIGHT as u64; + let counters = device.counters(); + // Each case: its name, whether the second member reads the planes, whether the + // group goes member by member, and the hand-overs and submissions it makes. + let cases = [ + ("A and B as one part", true, false, 1, 1), + ("A and B apart", true, true, 2, 2), + ("A and F apart", false, true, 1, 2), + ]; + for (case, reads, apart, hands, parts) in cases { + let outs = [picture_buffer(&device), picture_buffer(&device)]; + let from = picture_buffer(&device); + let queue = device.new_command_queue(64).expect("a queue"); + behind_busy(name, &device, &queue, |busy, submitted, _| { + let before = counters.metrics(); + let a = queue.command_buffer().expect("a command buffer"); + encode_read(name, &a, &surface, &outs[0]); + a.commit().expect("a commit"); + let second = queue.command_buffer().expect("a command buffer"); + match reads { + true => encode_read(name, &second, &surface, &outs[1]), + false => { + let mut blit = second.blit_command_encoder().expect("an encoder"); + (blit.copy_from_buffer_to_buffer(&from, 0, &outs[1], 0, 256)) + .expect("a copy"); + blit.end_encoding(); + } + } + second.commit().expect("a commit"); + for command_buffer in [&a, &second] { + within(name, || { + command_buffer.status() != CommandBufferStatus::Committed + }); + } + let sent = || counters.metrics().submissions - submitted; + assert_eq!(sent(), ahead, "{name}: {case}: held in one group"); + match apart { + true => second.queue().ran(1_000_000_000), + false => { + queue.send_now(&second); + within(name, || sent() > ahead); + } + } + busy.open(); + let got = [ + read_bytes(name, (a.clone(), outs[0].clone())), + read_bytes(name, (second.clone(), outs[1].clone())), + ]; + let metrics = counters.metrics(); + let handed = [ + metrics.d3d11_plane_submissions - before.d3d11_plane_submissions, + metrics.external_acquires - before.external_acquires, + metrics.external_releases - before.external_releases, + ]; + let group = sent() - ahead; + println!( + "d3d11-apart: {name}: {case}: the group's vkQueueSubmit2 calls {group}; \ + plane-keeping submissions, acquires, releases {handed:?}" + ); + assert_eq!( + compare(&one, &got[0]).0, + None, + "{name}: {case}: A read picture 1" + ); + if reads { + assert_eq!( + compare(&one, &got[1]).0, + None, + "{name}: {case}: B read picture 1" + ); + } + if apart { + assert!(group >= 2, "{name}: {case}: went member by member"); + } + assert_eq!( + handed, [hands; 3], + "{name}: {case}: one acquire and one release in each part that uses \ + the planes" + ); + assert_eq!( + group, parts, + "{name}: {case}: one vkQueueSubmit2 a part, and no more" + ); + }); + } + }); + } + /// How many reads [`every_device_times_back_to_back_reads_of_direct3d_11_planes`] commits /// in a run. const READS: u32 = 64; diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 0b99665ef..fad06b2b3 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -91,6 +91,7 @@ mod patch; mod pixel_format_info; mod pixel_interlock; mod places; +mod plane_parts; mod pool_reach; mod present; mod private_arena; diff --git a/crates/metal2vulkan-runtime/src/tests/plane_parts.rs b/crates/metal2vulkan-runtime/src/tests/plane_parts.rs new file mode 100644 index 000000000..3d8e02f60 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/plane_parts.rs @@ -0,0 +1,170 @@ +//! A submission's parts and the Direct3D 11 planes' hand-over, without a device: where a group +//! goes member by member, each part whose segments or uploads use a plane acquires those planes +//! at its head and releases them at its end, the release taking that part's last signals, its +//! progress value among them, so it adds no submit info; a part that uses none takes neither, +//! and a group sent as one part acquires once ahead of everything and releases once after +//! everything. + +use crate::submit::{infos, parts, Staged}; +use ash::vk::{self, Handle}; + +/// The progress semaphore's raw handle. +const PROGRESS: u64 = 50; + +fn staged(raw: u64, signals: &[u64]) -> Staged { + Staged { + waits: Vec::new(), + command: Some( + vk::CommandBufferSubmitInfo::default().command_buffer(vk::CommandBuffer::from_raw(raw)), + ), + signals: (signals.iter()) + .map(|&s| { + vk::SemaphoreSubmitInfo::default() + .semaphore(vk::Semaphore::from_raw(s)) + .value(1) + }) + .collect(), + } +} + +/// An info as its command buffers and its signals, each signal as its semaphore and value. +type Info = (Vec, Vec<(u64, u64)>); + +/// The parts of `segments`, the first `ahead` of them uploads, at the last value 10, each as its +/// value and its infos; with each hand-over asked for, as its planes and whether it acquires. +/// Each hand-over's command buffer is numbered from 100 in the order asked. +fn shape( + segments: Vec, + ahead: usize, + leads: &[usize], + planes: &[Vec], +) -> (Vec<(u64, Vec)>, Vec<(Vec, bool)>) { + let mut asked = Vec::new(); + let made = parts( + segments, + ahead, + leads, + (vk::Semaphore::from_raw(PROGRESS), 10), + planes, + |planes, acquire| { + asked.push((planes.to_vec(), acquire)); + Some(vk::CommandBuffer::from_raw(99 + asked.len() as u64)) + }, + ); + let shaped = (made.iter()) + .map(|(value, part)| { + let infos = (infos(part).iter()) + .map(|info| { + let commands = (info.commands.iter()) + .map(|command| command.command_buffer.as_raw()) + .collect(); + let signals = (info.signals.iter()) + .map(|signal| (signal.semaphore.as_raw(), signal.value)) + .collect(); + (commands, signals) + }) + .collect(); + (*value, infos) + }) + .collect(); + (shaped, asked) +} + +/// An upload (90), a segment of a command buffer begun in an earlier group (1), and two command +/// buffers that lead: the first's two segments (2, and 3 signalling event 7), the second's one +/// (4). +fn group() -> Vec { + vec![ + staged(90, &[]), + staged(1, &[]), + staged(2, &[]), + staged(3, &[7]), + staged(4, &[]), + ] +} + +/// Where the two leads begin among the segments past the upload. +const LEADS: [usize; 2] = [1, 3]; + +#[test] +fn each_part_of_a_group_sent_apart_hands_its_own_planes_over() { + // The first lead's part uses plane a; the second's a and b. + let planes = [vec![], vec!['a'], vec!['a', 'b']]; + let (parts, asked) = shape(group(), 1, &LEADS, &planes); + assert_eq!( + parts, + [ + (8, vec![(vec![90, 1], vec![(PROGRESS, 8)])]), + (9, vec![(vec![100, 2, 3, 101], vec![(7, 1), (PROGRESS, 9)])]), + (10, vec![(vec![102, 4, 103], vec![(PROGRESS, 10)])]), + ], + "each plane-using part acquires at its head and releases at its end, the release taking \ + the part's signals and its progress value" + ); + assert_eq!( + asked, + [ + (vec!['a'], true), + (vec!['a'], false), + (vec!['a', 'b'], true), + (vec!['a', 'b'], false), + ] + ); +} + +#[test] +fn a_plane_used_only_ahead_of_a_group_s_last_member_is_released_in_its_own_part() { + // Only the first lead's part uses a plane: the last part, which completes last, hands none + // over, and the first lead's release is ahead of its own progress value. + let planes = [vec![], vec!['a'], vec![]]; + let (parts, asked) = shape(group(), 1, &LEADS, &planes); + assert_eq!( + parts, + [ + (8, vec![(vec![90, 1], vec![(PROGRESS, 8)])]), + (9, vec![(vec![100, 2, 3, 101], vec![(7, 1), (PROGRESS, 9)])]), + (10, vec![(vec![4], vec![(PROGRESS, 10)])]), + ] + ); + assert_eq!(asked, [(vec!['a'], true), (vec!['a'], false)]); + // The uploads' planes go with the part ahead of the first lead's. + let planes = [vec!['u'], vec![], vec![]]; + let (parts, _) = shape(group(), 1, &LEADS, &planes); + assert_eq!( + parts, + [ + (8, vec![(vec![100, 90, 1, 101], vec![(PROGRESS, 8)])]), + (9, vec![(vec![2, 3], vec![(7, 1), (PROGRESS, 9)])]), + (10, vec![(vec![4], vec![(PROGRESS, 10)])]), + ] + ); +} + +/// The negative control: a group sent as one part hands its planes over once, ahead of its +/// uploads and after its last segment, the release taking the last signals; and parts that use +/// no plane are as they are without the hand-over. +#[test] +fn a_group_sent_as_one_part_hands_its_planes_over_once() { + let (parts, asked) = shape(group(), 1, &[], &[vec!['a', 'b']]); + assert_eq!( + parts, + [( + 10, + vec![ + (vec![100, 90, 1, 2, 3], vec![(7, 1)]), + (vec![4, 101], vec![(PROGRESS, 10)]), + ] + )] + ); + assert_eq!(asked, [(vec!['a', 'b'], true), (vec!['a', 'b'], false)]); + let (parts, asked) = shape(group(), 1, &LEADS, &[]); + assert_eq!( + parts, + [ + (8, vec![(vec![90, 1], vec![(PROGRESS, 8)])]), + (9, vec![(vec![2, 3], vec![(7, 1), (PROGRESS, 9)])]), + (10, vec![(vec![4], vec![(PROGRESS, 10)])]), + ] + ); + assert!(asked.is_empty()); +}