diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index c325e65dd..dce9aacce 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -1372,10 +1372,13 @@ made, with no lock, and never rewritten for layouts. from the device copy. Back, where device work wrote the texture, before the next reader of the device copy (a binding that reaches the buffer, a blit from or into it, a copy into another texture over a range that meets its, a - `synchronize_resource`, a capture of a range over it, ahead of its copies): a - reader recorded after the write makes it in its own commands, and the upload that leads a - submission, which runs before the submission's commands, only once the write's segment was - submitted; that submission also owes each other texture over a range that meets its a copy + `synchronize_resource`, a capture of a range over it, ahead of its copies): a reader in the + write's own command buffer, recorded after the write, makes it in its own commands; any other + reader, whichever was encoded first, takes it in the upload that leads its submission, which + runs before the submission's commands, only once the write's segment was submitted, so a + reader committed before the write on its queue runs ahead of it and leaves it owed, and a + reader whose group held holds the write ahead of it leads its own group, the write submitted + first. The write's submission also owes each other texture over a range that meets its a copy in. A texture released while its copy back is owed leaves its image with the buffer until a reader takes it or the buffer is released, which drops it uncopied; `Gauges::textures` counts it meanwhile. A host write the program does not tell with `did_modify_range` reaches such a texture only diff --git a/crates/metal2vulkan-runtime/src/blit.rs b/crates/metal2vulkan-runtime/src/blit.rs index c6fe79ac1..fb427ed95 100644 --- a/crates/metal2vulkan-runtime/src/blit.rs +++ b/crates/metal2vulkan-runtime/src/blit.rs @@ -45,7 +45,7 @@ use crate::fence::Fence; use crate::hazard::Access; use crate::indirect::{reset_regions, IndirectCommandBuffer}; use crate::memory::Own; -use crate::over::{self, Reached}; +use crate::over::Reached; use crate::record::{memory_barrier, Reach}; use crate::surface::Surface; use crate::texture::{ @@ -441,8 +441,10 @@ impl BlitCommandEncoder { building.keep(destination); // The textures over either device copy go back to it first; the destination's owe their // copies in (`crate::over::ManagedOvers`). - building.managed_backs(over::backs(source, None, true))?; - building.managed_backs(over::backs(destination, None, true))?; + let mut backs = Vec::new(); + building.managed_reach(source, &mut backs); + building.managed_reach(destination, &mut backs); + building.managed_backs(backs)?; building.wrote_managed(destination); let overs = [ (Reached::Buffer(source.span()), Access::Read), @@ -536,7 +538,9 @@ impl BlitCommandEncoder { building.keep(buffer); // The device copy's textures' copies back go first; a copy into the buffer owes them // their copies in, and one into the texture its copy back (`crate::over::ManagedOvers`). - building.managed_backs(over::backs(buffer, None, true))?; + let mut backs = Vec::new(); + building.managed_reach(buffer, &mut backs); + building.managed_backs(backs)?; match into_image { true => building.wrote_texture_managed(texture), false => building.wrote_managed(buffer), @@ -663,7 +667,9 @@ impl BlitCommandEncoder { let (vk_buffer, base) = buffer.device_range(); let (offset, length) = fill_range(base, buffer.length(), range)?; building.keep(buffer); - building.managed_backs(over::backs(buffer, None, true))?; + let mut backs = Vec::new(); + building.managed_reach(buffer, &mut backs); + building.managed_backs(backs)?; building.wrote_managed(buffer); let overs = [(Reached::Buffer(buffer.span()), Access::Write)]; building.overs_before(overs, false)?; @@ -1028,7 +1034,9 @@ impl BlitCommandEncoder { }; building.keep(buffer); // The textures over the device copy go back to it first (`crate::over`). - building.managed_backs(over::backs(buffer, None, true))?; + let mut backs = Vec::new(); + building.managed_reach(buffer, &mut backs); + building.managed_backs(backs)?; let command = building.ordered_command()?; // Marked after `ordered_command`, as a copy into a Shared buffer is. building.wrote_host(); diff --git a/crates/metal2vulkan-runtime/src/capture.rs b/crates/metal2vulkan-runtime/src/capture.rs index b3d173ab2..85f68cdce 100644 --- a/crates/metal2vulkan-runtime/src/capture.rs +++ b/crates/metal2vulkan-runtime/src/capture.rs @@ -192,13 +192,13 @@ impl Captured { &self, after: u64, number: u64, - wrote: &[Arc], + wrote: &[(Arc, bool)], ) -> bool { (self.0.iter()) .filter(|&&(captured, _)| after < captured && captured <= number) .flat_map(|(_, copy_in)| ©_in.modified) .any(|modified| { - (wrote.iter()).any(|texture| { + (wrote.iter()).any(|(texture, _)| { (texture.managed_over()) .is_some_and(|buffer| Weak::as_ptr(&modified.buffer) == Arc::as_ptr(buffer)) }) diff --git a/crates/metal2vulkan-runtime/src/command.rs b/crates/metal2vulkan-runtime/src/command.rs index 52ef03fbe..b7b46f596 100644 --- a/crates/metal2vulkan-runtime/src/command.rs +++ b/crates/metal2vulkan-runtime/src/command.rs @@ -329,8 +329,13 @@ pub(crate) struct Segment { /// such texture owes its copy in from the next submission (`crate::over::ManagedOvers`). pub(crate) managed_written: Vec>, /// The textures over a Managed buffer's device copy in an image of their own that its - /// commands wrote: its submission settles their copies back (`crate::over::ManagedOvers`). - pub(crate) managed_wrote: Vec>, + /// commands wrote, each with whether its copy back is owed, no later command of the command + /// buffer having taken it: its submission settles them (`crate::over::ManagedOvers`). + pub(crate) managed_wrote: Vec<(Arc, bool)>, + /// The Managed buffers with textures over their device copies that its commands reach: the + /// upload that leads its submission takes the copies back owed to them by writes submitted + /// before it (`crate::over::ManagedOvers`). + pub(crate) managed_reached: Vec>, } pub(crate) struct Handlers { @@ -1427,8 +1432,8 @@ impl Building { } /// Before a command that reads or writes a Managed buffer's device copy: records the copies - /// back `backs` owed by textures over it (`crate::over::ManagedOvers`), ordered as a blit's - /// copy is, and keeps their images until the command buffer retires. + /// back `backs` owed by textures over it ([`managed_reach`](Self::managed_reach)), ordered as + /// a blit's copy is, and keeps their images until the command buffer retires. /// /// Err: [`Error::Vulkan`] where the command buffer cannot record. pub(crate) fn managed_backs(&mut self, backs: Vec) -> Result<(), Error> { @@ -1448,19 +1453,54 @@ impl Building { Ok(()) } + /// A command reaches `buffer`'s device copy: where it is a Managed buffer with textures over + /// it in images of their own, the copies back owed by this command buffer's writes of them + /// before the command, taken, into `backs`, for the command's own commands to make first; the + /// upload that leads the segment's submission takes those owed by other command buffers' + /// writes submitted before it (`crate::over::ManagedOvers`). + pub(crate) fn managed_reach( + &mut self, + buffer: &impl BufferObject, + backs: &mut Vec, + ) { + let Some(overs) = buffer.managed_overs() else { + return; + }; + let mut own = Vec::new(); + for segment in &mut self.gpu.segments { + for (texture, owes) in &mut segment.managed_wrote { + let over = texture + .managed_over() + .is_some_and(|over| over.id() == buffer.id()); + if *owes && over { + *owes = false; + own.push(texture.id()); + } + } + } + overs.own_backs(&own, backs); + let reached = &mut self.last().managed_reached; + if !reached.iter().any(|kept| kept.id() == buffer.id()) { + reached.push(Arc::clone(buffer.object())); + } + } + /// A command writes `texture`: where it is over a Managed buffer's device copy in an image of - /// its own, it owes its copy back to the readers recorded after the command, and from the - /// segment's submission to every later reader (`crate::over::ManagedOvers`). + /// its own, it owes its copy back to the later commands of the command buffer that reach the + /// buffer, and from the segment's submission, where none of them took it, to the readers + /// submitted after (`crate::over::ManagedOvers`). pub(crate) fn wrote_texture_managed(&mut self, texture: &impl TextureObject) { let Some(buffer) = texture.managed_over() else { return; }; let wrote = &mut self.last().managed_wrote; - let first = !wrote.iter().any(|kept| kept.id() == texture.id()); - if first { - wrote.push(Arc::clone(texture.object())); + match wrote.iter_mut().find(|(kept, _)| kept.id() == texture.id()) { + Some((_, owes)) => *owes = true, + None => { + wrote.push((Arc::clone(texture.object()), true)); + buffer.over_entries().pend(texture.id()); + } } - buffer.over_entries().pend(texture.id(), first); } /// A command writes `buffer`: where it is a Managed buffer with textures over its device copy, diff --git a/crates/metal2vulkan-runtime/src/encoder.rs b/crates/metal2vulkan-runtime/src/encoder.rs index 5125ea9b2..129d5e59f 100644 --- a/crates/metal2vulkan-runtime/src/encoder.rs +++ b/crates/metal2vulkan-runtime/src/encoder.rs @@ -1105,7 +1105,7 @@ impl ComputeCommandEncoder { for &(slot, access) in &interface.buffers { if let Some(Slot::Buffer { buffer, .. }) = self.slots.get(slot as usize) { if buffer.managed_overs().is_some() { - backs.extend(over::backs(&**buffer, None, true)); + building.managed_reach(&**buffer, &mut backs); if access.writes() { building.wrote_managed(&**buffer); } diff --git a/crates/metal2vulkan-runtime/src/over.rs b/crates/metal2vulkan-runtime/src/over.rs index a6af093ee..0d077040e 100644 --- a/crates/metal2vulkan-runtime/src/over.rs +++ b/crates/metal2vulkan-runtime/src/over.rs @@ -44,7 +44,6 @@ //! segment wrote is kept in sets by block ([`Written`]), so a write costs one lookup however many //! the segment made. -use crate::buffer::BufferObject; use crate::copy_out; use crate::counters::{Counters, Metric}; use crate::device::lock; @@ -110,7 +109,8 @@ impl Written { pub(crate) enum Reached<'a> { /// A texture: one in place over a buffer reaches the buffer. Texture(&'a Arc), - /// A buffer, by the bytes of memory it lies in ([`BufferObject::span`]). + /// A buffer, by the bytes of memory it lies in + /// ([`BufferObject::span`](crate::buffer::BufferObject::span)). Buffer(Span), } @@ -532,16 +532,13 @@ struct Entry { /// runs ahead of every command buffer numbered at or after it (`crate::capture`), which then /// takes none, while one numbered before it may run first and still takes its own. captured: Option, - /// Submitted device work wrote it since its last copy back: the upload that leads a - /// submission may take the copy. + /// Submitted device work wrote it since its last copy back: the upload that leads a later + /// submission takes the copy. back: bool, - /// Device work wrote it since a reader last took its copy back, submitted or not: a reader - /// recorded after the write takes the copy in its own commands. - fresh: bool, /// The segments that wrote it and are not yet submitted. The upload that leads a submission /// runs before them, so it takes no copy back for their writes; each one's submission makes - /// the copy back owed to it, unless a reader recorded after the write took it. One whose - /// command buffer is never committed leaves the entry until the buffer is released. + /// the copy back owed to it, unless a later command of its own command buffer took it. One + /// whose command buffer is never committed leaves the entry until the buffer is released. pending: u32, /// The texture is released: the entry lives for its copy back alone, and its id may name a /// texture made since, which a lookup by id finds instead. @@ -573,9 +570,13 @@ impl Entry { /// - **Back** where device work wrote the texture since the last copy back: before the next /// reader of the device copy (a binding whose interface reads the buffer, a blit from it, a copy /// into another texture over a range that meets its, a synchronize, a capture of a range over -/// it), not after each write. A reader recorded after the write takes it in its own commands; the -/// upload that leads a submission takes it only once the write's segment was submitted, as the -/// upload runs before the submission's commands. A capture's is taken by the upload that leads +/// it), not after each write. A reader in the write's own command buffer, recorded after it, +/// takes it in its own commands. Any other reader runs after the write only where the write was +/// submitted first, Metal running a queue's command buffers in the order they are committed, +/// whichever was encoded first: the upload that leads the reader's submission takes it once the +/// write's segment was submitted, as the upload runs before the submission's commands, and a +/// reader whose group held holds the write ahead of it leads its own group, the write submitted +/// first (`crate::submit`). A capture's is taken by the upload that leads /// the submission that takes its copy-in, a write in the same group held going first in a /// submission of its own (`crate::capture`). A texture released while its copy back is owed /// leaves its image with the buffer until a reader takes it or the buffer is released, which @@ -615,7 +616,6 @@ impl ManagedOvers { into: true, captured: None, back: false, - fresh: false, pending: 0, released: false, }); @@ -634,25 +634,24 @@ impl ManagedOvers { self.count.store(entries.len(), Relaxed); } - /// A segment not yet submitted wrote texture `texture`; `first` where it had not written it - /// before, which its submission settles ([`ManagedOvers::settle`]). - pub(crate) fn pend(&self, texture: u32, first: bool) { + /// A segment not yet submitted wrote texture `texture` for the first time, which its + /// submission settles ([`ManagedOvers::settle`]). + pub(crate) fn pend(&self, texture: u32) { if let Some(entry) = lock(&self.entries).iter_mut().find(|e| e.of(texture)) { - entry.pending += u32::from(first); - entry.fresh = true; + entry.pending += 1; } } /// A segment that wrote texture `texture` was submitted: the texture owes its copy back where - /// no reader recorded after the write took it, and each other texture whose range meets its - /// owes its copy in, which takes that copy back first. - pub(crate) fn settle(&self, texture: u32) { + /// `owes`, no later command of its command buffer having taken it, and each other texture + /// whose range meets its owes its copy in, which takes that copy back first. + pub(crate) fn settle(&self, texture: u32, owes: bool) { let mut entries = lock(&self.entries); let Some(range) = (entries.iter_mut()) .find(|entry| entry.of(texture)) .map(|entry| { entry.pending = entry.pending.saturating_sub(1); - entry.back |= entry.fresh; + entry.back |= owes; entry.range }) else { @@ -674,26 +673,35 @@ impl ManagedOvers { } } - /// A reader of the device copy comes, over `range` of the buffer (all of it where `None`): - /// each copy back owed by a texture whose range meets it, taken, into `backs`; with `pending`, - /// a reader recorded after the writes, also those of writes not yet submitted. A released - /// texture's entry goes once nothing is owed. - pub(crate) fn backs(&self, range: Option<(u64, u64)>, pending: bool, backs: &mut Vec) { + /// The upload that leads a submission that reads the device copy over `range` of the buffer + /// (all of it where `None`): each copy back owed by a submitted write of a texture whose range + /// meets it, taken, into `backs`. A released texture's entry goes once nothing is owed. + pub(crate) fn backs(&self, range: Option<(u64, u64)>, backs: &mut Vec) { let mut entries = lock(&self.entries); for entry in entries.iter_mut() { - let owed = entry.back || (pending && entry.fresh); - if owed && range.is_none_or(|range| meets(entry.range, range)) { + if entry.back && range.is_none_or(|range| meets(entry.range, range)) { backs.push((entry.texture, entry.copy, Arc::clone(&entry.image))); entry.back = false; - // A write not yet submitted stays fresh for the readers recorded after it, where - // the upload took the copy before it runs. - entry.fresh &= !pending && entry.pending > 0; } } entries.retain(|entry| !entry.released || entry.back || entry.pending > 0); self.count.store(entries.len(), Relaxed); } + /// A command of the command buffer that wrote `textures` and reads the device copy after + /// those writes: their copies back, into `backs`, for its own commands to make first. The + /// copies back owed by other command buffers' writes wait for its submission + /// ([`backs`](Self::backs)), where the order of the two is known. + pub(crate) fn own_backs(&self, textures: &[u32], backs: &mut Vec) { + let entries = lock(&self.entries); + for entry in entries + .iter() + .filter(|entry| textures.iter().any(|&t| entry.of(t))) + { + backs.push((entry.texture, entry.copy, Arc::clone(&entry.image))); + } + } + /// A submission takes the copy-in of a capture of `ranges` of the consumer's bytes into the /// device copy (`crate::capture`): the copies back owed by writes submitted before it of /// textures whose ranges meet one of `ranges`, or meet a live texture's that does, taken into @@ -712,8 +720,6 @@ impl ManagedOvers { if entry.back && (meets_any(ranges, entry.range) || meets_any(&targets, entry.range)) { backs.push((entry.texture, entry.copy, Arc::clone(&entry.image))); entry.back = false; - // A write not yet submitted stays fresh for the readers recorded after it. - entry.fresh &= entry.pending > 0; } } entries.retain(|entry| !entry.released || entry.back || entry.pending > 0); @@ -769,22 +775,7 @@ impl ManagedOvers { } (entry.range, entry.copy) }; - self.backs(Some(range), false, backs); + self.backs(Some(range), backs); Some(copy) } } - -/// The copies back owed to readers of `buffer`'s device copy over `range` (all of it where -/// `None`), where it has textures over it in images of their own; with `pending`, a reader -/// recorded after the writes, also those of writes not yet submitted. -pub(crate) fn backs( - buffer: &impl BufferObject, - range: Option<(u64, u64)>, - pending: bool, -) -> Vec { - let mut backs = Vec::new(); - if let Some(overs) = buffer.managed_overs() { - overs.backs(range, pending, &mut backs); - } - backs -} diff --git a/crates/metal2vulkan-runtime/src/render_encoder.rs b/crates/metal2vulkan-runtime/src/render_encoder.rs index 974794a2b..dde3d00f4 100644 --- a/crates/metal2vulkan-runtime/src/render_encoder.rs +++ b/crates/metal2vulkan-runtime/src/render_encoder.rs @@ -1492,7 +1492,8 @@ impl RenderCommandEncoder { }; let written = [(Reached::Buffer(buffer.span()), Access::Write)]; let copies = building.overs_owed(written, false); - let backs = over::backs(&**buffer, None, true); + let mut backs = Vec::new(); + building.managed_reach(&**buffer, &mut backs); if copies.is_empty() && backs.is_empty() { return Ok(()); } @@ -3082,7 +3083,7 @@ impl RenderCommandEncoder { for &(table, slot, access) in &draws.buffers { if let Some(buffer) = tables[table].buffer(slot) { if buffer.managed_overs().is_some() { - backs.extend(over::backs(buffer, None, true)); + building.managed_reach(buffer, &mut backs); if access.writes() { building.wrote_managed(buffer); } @@ -3090,9 +3091,7 @@ impl RenderCommandEncoder { } } for buffer in vertex_reads { - if buffer.managed_overs().is_some() { - backs.extend(over::backs(buffer, None, true)); - } + building.managed_reach(buffer, &mut backs); } for &(table, slot, access) in &draws.textures { if let (true, Some(texture)) = (access.writes(), tables[table].texture(slot)) { diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index b1b2bba20..4c347a4c4 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -480,8 +480,12 @@ struct Batch { /// copies in once it is submitted. written: Vec>, /// The textures over a Managed buffer's device copy its segments wrote, one for each segment - /// that wrote one, whose copies back its submission settles. - wrote: Vec>, + /// that wrote one, whose copies back its submission settles, each with whether its copy back + /// is owed (`crate::command::Segment::managed_wrote`). + wrote: Vec<(Arc, bool)>, + /// The Managed buffers with textures over their device copies its segments reach, to which + /// its upload takes the copies back owed by writes submitted before (`crate::over`). + reached: Vec>, } /// One part of a submit info: a segment's or an upload's commands and its signals, and for a @@ -1693,8 +1697,12 @@ impl WorkState { } let segment = &pending.gpu.segments[pending.next]; // A segment that leans on the leading copy for a buffer an earlier segment of the group - // wrote leads its group, so its copy follows the write (`crate::over`). - if (fifo.group.as_ref()).is_some_and(|group| group.leans.conflicts(&segment.overs)) { + // wrote leads its group, so its copy follows the write (`crate::over`); so does one that + // reaches a Managed buffer a texture over which an earlier segment of the group wrote, so + // its upload takes the copy back the write owes (`Batch::owes_over`). + if (fifo.group.as_ref()).is_some_and(|group| { + group.leans.conflicts(&segment.overs) || group.owes_over(&segment.managed_reached) + }) { return Step::Goes(index); } // A command buffer joins the group held only while the group's expected GPU runs, summed, @@ -1736,6 +1744,7 @@ impl WorkState { backs: Vec::new(), written: Vec::new(), wrote: Vec::new(), + reached: Vec::new(), }); if leads { group.run = group.run.saturating_add(run.unwrap_or(u64::MAX)); @@ -1754,6 +1763,11 @@ impl WorkState { } } group.wrote.extend(segment.managed_wrote.iter().cloned()); + for buffer in &segment.managed_reached { + if !group.reached.iter().any(|kept| kept.id() == buffer.id()) { + group.reached.push(buffer.clone()); + } + } for copy in segment.overs.leaning() { if !group.overs.iter().any(|kept| kept.image == copy.image) { group.overs.push(copy); @@ -1859,6 +1873,12 @@ impl WorkState { for (_, copy_in) in &taken { copy_in.backs(&mut batch.backs); } + // The copies back owed by writes submitted before it to the Managed buffers its segments + // reach, which its upload makes first: as it goes, the order of each write and its + // readers on the device's one `VkQueue` is known (`crate::over::ManagedOvers`). + for buffer in &batch.reached { + buffer.over_entries().backs(None, &mut batch.backs); + } batch.take(taken); self.progress += 1; let value = self.progress; @@ -1899,6 +1919,18 @@ impl Batch { let unseen = if self.seen == retired { self.unseen } else { 0 }; self.count < GROUP && joins(self.run, unseen, run) } + + /// Whether a texture over one of `reached`, Managed buffers, owes its copy back for a write of + /// one of its segments: a segment that reaches one of them after these leads its own group, + /// so the write is submitted, and owes its copy back, before the upload that leads that + /// segment's submission takes it (`crate::over::ManagedOvers`). + fn owes_over(&self, reached: &[Arc]) -> bool { + (self.wrote.iter()).any(|(texture, owes)| { + *owes + && (texture.managed_over()) + .is_some_and(|over| reached.iter().any(|buffer| buffer.id() == over.id())) + }) + } } impl Fifo { @@ -2205,6 +2237,7 @@ impl Batch { backs: Vec::new(), written: Vec::new(), wrote: Vec::new(), + reached: Vec::new(), } } } @@ -2486,9 +2519,9 @@ fn submit(core: &Core, value: u64, uploads: Vec, batch: Batch) { } // Its writes of textures over a Managed buffer's device copy are submitted: the upload that // leads a later submission may take their copies back. - for texture in wrote { + for (texture, owes) in wrote { if let Some(buffer) = texture.managed_over() { - buffer.over_entries().settle(texture.id()); + buffer.over_entries().settle(texture.id(), owes); } } } diff --git a/crates/metal2vulkan-runtime/src/tests/managed.rs b/crates/metal2vulkan-runtime/src/tests/managed.rs index 29a262274..b4f7573b3 100644 --- a/crates/metal2vulkan-runtime/src/tests/managed.rs +++ b/crates/metal2vulkan-runtime/src/tests/managed.rs @@ -34,7 +34,8 @@ //! committed before a capture of a range over it, in a group held or held for room, reads the //! buffer's bytes outside that range, and the one that takes the capture the new bytes. A reader //! of the buffer and a write of the texture on one queue run in the order they are committed, -//! whichever was encoded first. +//! whichever was encoded first, and a reader of another queue encoded before the write and +//! committed once it completed reads what it wrote. use crate::capture::{merge, taker, Captured}; use crate::submit::put_ahead; @@ -1798,17 +1799,19 @@ pub(crate) mod on_device { } /// A reader of a Managed buffer and a write of a texture over it in an image of its own - /// ([`over_texture`]), on one queue, run in the order they are committed, whichever was - /// encoded first: a queue is "a serial queue of command buffers" (`MTLCommandQueue.h`), and a - /// commit appends a command buffer not yet enqueued to its end, as `enqueue` does - /// (`MTLCommandBuffer.h`). P reads the texture and completes, so its image holds the buffer's - /// bytes. W copies a pattern into the whole texture and R copies the whole buffer into a - /// Shared buffer, W encoded first where `writer_first`, else R; the one encoded later is - /// committed first. Once both completed, L copies the whole buffer again. R reads the buffer's - /// bytes where it was committed first, else W's pattern in the texture's rows; L reads W's - /// pattern there; both read the buffer's bytes outside them. The form, the copies R recorded - /// as it was encoded, and what R and L read are printed. - fn over_read_in_commit_order(test: &str, writer_first: bool) { + /// ([`over_texture`]) run in the order Metal gives them, whichever was encoded first: on one + /// queue, the order they are committed, a queue being "a serial queue of command buffers" + /// (`MTLCommandQueue.h`) to whose end a commit appends a command buffer not yet enqueued, as + /// `enqueue` does (`MTLCommandBuffer.h`); on two, a reader committed once the write completed + /// runs after it. P reads the texture and completes, so its image holds the buffer's bytes. W + /// copies a pattern into the whole texture and R copies the whole buffer into a Shared buffer, + /// W encoded first where `writer_first`, else R; the one encoded later is committed first. + /// With `other_queue`, R is of another queue, committed once W completed. Once both completed, + /// L copies the whole buffer again. R reads the buffer's bytes where it was committed first, + /// else W's pattern in the texture's rows; L reads W's pattern there; both read the buffer's + /// bytes outside them. The form, the copies R recorded as it was encoded, those made as R and + /// W went, and what R and L read are printed. + fn over_read_in_commit_order(test: &str, writer_first: bool, other_queue: bool) { each_device(test, |name, _, device| { let counters = device.counters(); let cpu = (device.take_block(MemoryClass::Shared, 1)) @@ -1823,12 +1826,14 @@ pub(crate) mod on_device { }; let (r_read, l_read) = (whole(1), whole(2)); let queue = device.new_command_queue(16).expect("a queue"); + let other = device.new_command_queue(16).expect("a queue"); let filler = queue.command_buffer().expect("a command buffer"); read_over(&filler, &texture, &p_read); filler.commit().expect("a commit"); finished(name, &filler); let writer = queue.command_buffer().expect("a command buffer"); - let reader = queue.command_buffer().expect("a command buffer"); + let reads_on = if other_queue { &other } else { &queue }; + let reader = reads_on.command_buffer().expect("a command buffer"); if writer_first { write_over(&writer, &texture, &pattern); } @@ -1848,12 +1853,17 @@ pub(crate) mod on_device { ), false => (&writer, &reader, &written, "W first, R read W's pattern"), }; + let copies = counters.metrics().over_copies; first.commit().expect("a commit"); + if other_queue { + finished(name, first); + } second.commit().expect("a commit"); for command_buffer in [&writer, &reader] { finished(name, command_buffer); assert_eq!(command_buffer.error(), None, "{name}"); } + let went = counters.metrics().over_copies - copies; let later = queue.command_buffer().expect("a command buffer"); read_whole(&later, &managed, &l_read); later.commit().expect("a commit"); @@ -1861,9 +1871,10 @@ pub(crate) mod on_device { let len = OVER_LEN as usize; let (r_bytes, l_bytes) = (read(&r_read, len), read(&l_read, len)); println!( - "{test}: {name}: {form}; W encoded first {writer_first}; copies R recorded as it \ - was encoded {recorded}; committed {owed} {} (its first texels {:?}); L read W's \ - pattern {} (its first texels {:?})", + "{test}: {name}: {form}; W encoded first {writer_first}, R of another queue \ + {other_queue}; copies R recorded as it was encoded {recorded}, made as R and W \ + went {went}; committed {owed} {} (its first texels {:?}); L read W's pattern {} \ + (its first texels {:?})", r_bytes == *r_owed, &r_bytes[..8], l_bytes == written, @@ -1871,14 +1882,14 @@ pub(crate) mod on_device { ); assert!( r_bytes == *r_owed, - "{name}: R reads what Metal's commit order leaves" + "{name}: R reads what Metal's order leaves" ); assert!( l_bytes == written, "{name}: L reads W's pattern in the texture's rows" ); drop((texture, managed, pattern, p_read, r_read, l_read, arena)); - drop((filler, writer, reader, later, queue)); + drop((filler, writer, reader, later, queue, other)); (device.release_block(cpu)).unwrap_or_else(|error| panic!("{name}: {error}")); }); } @@ -1886,12 +1897,27 @@ pub(crate) mod on_device { /// [`over_read_in_commit_order`] with W encoded first and R committed first. #[test] fn every_device_reads_a_managed_buffer_ahead_of_a_write_over_it_encoded_before_it() { - over_read_in_commit_order("managed read ahead of a write encoded before it", true); + over_read_in_commit_order( + "managed read ahead of a write encoded before it", + true, + false, + ); } /// [`over_read_in_commit_order`] with R encoded first and W committed first. #[test] fn every_device_reads_a_managed_buffer_after_a_write_over_it_encoded_after_it() { - over_read_in_commit_order("managed read after a write encoded after it", false); + over_read_in_commit_order("managed read after a write encoded after it", false, false); + } + + /// [`over_read_in_commit_order`] with R encoded first on another queue and committed once W + /// completed. + #[test] + fn every_device_reads_a_managed_buffer_on_another_queue_after_a_completed_write_over_it() { + over_read_in_commit_order( + "managed read on another queue after a completed write", + false, + true, + ); } }