From a14dca977fb507d56df6f69b601ebb3080f7ccae Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Thu, 12 Nov 2026 16:44:03 -0800 Subject: [PATCH] did_modify_range: a batch takes no upload of bytes a later commit took The 3090 run of the race fix (a392521bff) still read the wrong bytes, on one queue as on two: 48 rounds of a Managed buffer, each writing its byte, naming the range and committing a copy of it, read the last round of the buffer's bytes in rounds 1 and 2 of each four (3 read 7, 5 read 7; never the 0xee each round writes after its commit). Each commit staged its own bytes, but the scheduler submitted the earlier rounds after the CPU loop had committed the later ones, and an earlier round's batch took every upload of the buffer waiting, its own and the later commits', copying them in order before its blit. Each commit now takes a stamp under Modified's lock (take_at_commit returns it; the command buffer keeps it in State::commit). An upload taken at a commit carries its stamp (FirstFill::upload, register_upload's commit), and a batch takes an upload only where its command buffer's stamp is at least the upload's (FillTake::Owned and FillTake::Reached carry the batch's). An upload no commit took (a buffer's making, a rebind, a completion's undo) carries 0 and goes with any batch that reaches it, as before. Internal submissions take every upload, as before. Verified: first_move's unit test an_upload_goes_with_no_batch_committed_before_its_bytes_were_taken (an upload of commits 2, 3, 4: a batch of commit 1 takes none, of 2 the first, of 4 the other two, on devices with and without a fill queue); the 3090 before/after of ranges_named_ follows on this commit. --- crates/metal2vulkan-runtime/src/buffer.rs | 39 +++-- .../src/buffer_descriptor.rs | 2 +- .../src/command_buffer/mod.rs | 13 +- .../src/command_buffer/submit.rs | 4 + crates/metal2vulkan-runtime/src/managed.rs | 54 ++++--- .../metal2vulkan-runtime/src/managed_copy.rs | 36 +++-- .../src/texture/first_move.rs | 136 ++++++++++++++---- 7 files changed, 205 insertions(+), 79 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/buffer.rs b/crates/metal2vulkan-runtime/src/buffer.rs index 5f351dfc5..b01492a61 100644 --- a/crates/metal2vulkan-runtime/src/buffer.rs +++ b/crates/metal2vulkan-runtime/src/buffer.rs @@ -4305,15 +4305,17 @@ impl Buffer { /// (`texture/first_move.rs`'s `register_upload`), which the call does not wait for: every /// command buffer committed after the call reads the bytes, and none submitted before that /// submission on its `VkQueue` sees them land under it. The copy reads the host copy as that - /// submission runs, and the submission holds the host copy until it completed. + /// submission runs, and the submission holds the host copy until it completed. `commit` is + /// the stamp of the commit that took the range (`managed.rs`), which no batch committed before + /// it takes the copy for; 0 where none did, which any batch takes. /// Past the bound of what is staged, a submission waited for makes it now. Nothing for a /// buffer without a host copy, nor for an empty range. - pub(super) fn upload(&self, range: Range) -> Result<(), Error> { + pub(super) fn upload(&self, range: Range, commit: u64) -> Result<(), Error> { let s = self.s(); let mirror = match self.mirror() { Some(HostCopy::Own(mirror)) => mirror, // The caller's pages, through their import or staging (`managed_copy.rs`). - Some(HostCopy::Caller(caller)) => return caller.copy_in(self, range, true), + Some(HostCopy::Caller(caller)) => return caller.copy_in(self, range, true, commit), None => return Ok(()), }; if range.is_empty() { @@ -4329,6 +4331,7 @@ impl Buffer { (mirror.raw, range.start), range.end - range.start, Box::new(Arc::clone(mirror)), + commit, ); match past { true => device.submit_internal(|_, _| {}), @@ -4337,17 +4340,18 @@ impl Buffer { } /// Copy the bytes `range` of a buffer with a host copy ([`HostCopy`]) to its own as the host - /// copy holds them now, at a commit (`managed.rs`'s `Modified`): the CPU copies them into - /// staging buffers, which the device's next submission copies from, before any of its - /// commands (`managed_copy::stage_in`). Where the staging buffers find no memory, the bytes - /// are copied from the host copy as that submission runs ([`upload`](Self::upload)). Nothing - /// for a buffer without a host copy, nor for an empty range, nor for pages detached. - pub(super) fn upload_now(&self, range: Range) { + /// copy holds them now, at the commit stamped `commit` (`managed.rs`'s `Modified`): the CPU + /// copies them into staging buffers, which the device's next submission of a command buffer + /// committed at or after it that may reach the buffer copies from, before any of its commands + /// (`managed_copy::stage_in`). Where the staging buffers find no memory, the bytes are copied + /// from the host copy as that submission runs ([`upload`](Self::upload)). Nothing for a buffer + /// without a host copy, nor for an empty range, nor for pages detached. + pub(super) fn upload_now(&self, range: Range, commit: u64) { let staged = match self.mirror() { - Some(HostCopy::Caller(caller)) => caller.copy_in_now(self, range.clone()), + Some(HostCopy::Caller(caller)) => caller.copy_in_now(self, range.clone(), commit), Some(HostCopy::Own(_)) => match self.mapped_contents() { Some(contents) => { - super::managed_copy::stage_in(self, contents, range.clone(), true) + super::managed_copy::stage_in(self, contents, range.clone(), true, commit) } None => Ok(()), }, @@ -4356,7 +4360,7 @@ impl Buffer { // A failed submission past the bound fails the device, which the commit's own // submission reports. if staged.is_err() { - let _ = self.upload(range); + let _ = self.upload(range, commit); } } @@ -4366,14 +4370,15 @@ impl Buffer { /// commands before it on its `VkQueue`, as [`upload`](Self::upload) copies, the submission holding `held` until it completed: a /// two-copy buffer's copy in from the caller's pages (`managed_copy.rs`), counted /// (`WorkCounts::managed_bytes_copied_in`), or from staging buffers of a host copy's bytes - /// ([`upload_now`](Self::upload_now)). Where `bounded`, past the bound of what is staged, a + /// ([`upload_now`](Self::upload_now)), for the commit stamped `commit` (0: none, + /// [`upload`](Self::upload)). Where `bounded`, past the bound of what is staged, a /// submission waited for makes it now; else it waits for the device's next. pub(super) fn copy_in_from( &self, source: (vk::Buffer, u64), range: Range, held: Box, - bounded: bool, + (bounded, commit): (bool, u64), ) -> Result<(), Error> { let s = self.s(); let device = &s.device; @@ -4387,6 +4392,7 @@ impl Buffer { source, bytes, held, + commit, ); if self.caller_copy().is_some() { device.shared().counts.managed_copied(bytes, true); @@ -5255,7 +5261,10 @@ impl Buffer { // A two-copy buffer's device copy takes the pages' bytes, its GPU writes not synchronized // lost with the `VkDevice` (`managed_copy.rs`). if let Some(caller) = carried.caller_copy() { - if caller.copy_in(&carried, 0..caller.reach(), true).is_err() { + if caller + .copy_in(&carried, 0..caller.reach(), true, 0) + .is_err() + { if let Bytes::Own(parts) = &carried.0.bytes { parts.range_token.hand_to(&own.range_token); } diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs index c492ae5fc..b09037691 100644 --- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs @@ -674,7 +674,7 @@ impl Device { }; if buffer.storage_mode() != StorageMode::Private { fill(&buffer)?; - buffer.upload(0..length)?; + buffer.upload(0..length, 0)?; return Ok(buffer); } let staging = self.own_buffer(length, ResourceOptions::shared())?; diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs index 8e4c19ebd..9a5b5ecea 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs @@ -1023,6 +1023,10 @@ pub(super) struct State { /// reaches buffers through addresses), a root table of work on the GPU alone, or work its /// host steps resolve from records (`Deferred::reaches_unnamed`). pub(super) reaches_unnamed: bool, + /// The stamp of its commit (`managed.rs`'s `Modified::take_at_commit`): its batches take no + /// `Managed` buffer's upload of bytes a later commit took (`texture/first_move.rs`, + /// `FirstFill::upload`). 0 until it is committed. + pub(super) commit: u64, /// A command of it may write memory no use it noted names: one that reaches it as above /// whose functions may write memory (stores or atomic updates, /// `StageInterface::writes_memory`; the root table of a draw whose vertex and fragment @@ -2424,6 +2428,7 @@ impl CommandBuffer { release: None, replay_tables: Vec::new(), reaches_unnamed: false, + commit: 0, writes_unnamed: false, unrunnable: BTreeSet::new(), submitted_holds: Vec::new(), @@ -3369,10 +3374,12 @@ impl CommandBuffer { hand: Hand, ) -> Result<(), Error> { // The bytes of the ranges `did_modify_range` noted of the buffers this command buffer may - // use, as the host copies hold them now, for its submission, or one before it, that may - // use them (`managed.rs`). + // use, as the host copies hold them now, for its submission that may use them, or a later + // commit's, never an earlier one's (`managed.rs`); and its commit's stamp, which bounds + // the uploads its batches take. let modified = &self.0.device.shared().modified; - modified.take_at_commit(|buffer| self.may_use(buffer)); + let commit = modified.take_at_commit(|buffer| self.may_use(buffer)); + self.state().commit = commit; // Left to wait for the last render encoder's end, where its call left it to the device's // lowering thread and the thread has not lowered it, or for the commits left so. if failure.is_none() && self.queue_commit(call, hand)? { diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs index ff07741b7..66e0f97e7 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs @@ -686,14 +686,18 @@ impl CommandBufferShared { // Either way it learns what owns the memory of the work it takes. let owner_of = |key: ResourceKey| crate::used_resources::owner_of(shared, key); let reaches = |owner: ObjectId| state.parts_reach(shared, start..end, &[owner]); + // An upload of bytes a later commit took waits for that commit's batches. + let commit = state.commit; let fill_take = match device.has_fill_queue() { true => FillTake::Reached { owner_of: &owner_of, reaches: &reaches, + commit, }, false => FillTake::Owned { owner_of: &owner_of, reaches: &reaches, + commit, }, }; let hold = || crate::heap_sets::hold(device); diff --git a/crates/metal2vulkan-runtime/src/managed.rs b/crates/metal2vulkan-runtime/src/managed.rs index 0eba229b9..c35c4a585 100644 --- a/crates/metal2vulkan-runtime/src/managed.rs +++ b/crates/metal2vulkan-runtime/src/managed.rs @@ -19,9 +19,12 @@ //! the buffer (its encoders' uses name it, `use_resource` made it resident, or a command may //! reach memory no use names), of any of the device's queues, takes the range's bytes as the //! host copy holds them then, before that call returns, and copies them to the device's own by -//! the next submission whose commands may use the buffer, before any of them, after the -//! command buffers before it on its `VkQueue` (`texture/first_move.rs`'s `register_upload`): -//! Metal's shape, the upload made before the GPU uses the buffer, on the user's own queue. A +//! the next submission whose commands may use the buffer of a command buffer committed at or +//! after that commit, before any of them, after the command buffers before it on its +//! `VkQueue` (`texture/first_move.rs`'s `register_upload`): Metal's shape, the upload made +//! before the GPU uses the buffer, on the user's own queue. A command buffer committed before, +//! whose submission the scheduler makes after the later commit, never takes it, so it reads +//! the bytes of its own commit and none a later commit took. A //! commit that uses none of it leaves the range noted, and its submission leaves the copy //! waiting, so the copy never lands under a command buffer of another queue still to read the //! bytes it replaces (`synchronize_resource` copies the buffer back where the command buffer @@ -103,37 +106,54 @@ impl Buffer { /// The ranges of buffers with a host copy the CPU wrote and named (`did_modify_range`, a CPU /// write through a texture over the buffer) that no commit of a command buffer that may use /// their buffer took yet: the next such commit copies each in, as the host copy holds it then -/// (the module docs). A buffer gone meanwhile takes its ranges with it. +/// (the module docs), and the commits' stamps. A buffer gone meanwhile takes its ranges with it. #[derive(Default)] -pub(super) struct Modified(Mutex>); +pub(super) struct Modified(Mutex); + +#[derive(Default)] +struct Ranges { + noted: Noted, + /// The commits so far: the stamp of the last ([`Modified::take_at_commit`]). + commits: u64, +} impl Modified { /// Note `range` of `buffer`: once, where a range noted of it already holds it. fn note(&self, buffer: &Buffer, range: Range) { let same = |of: &WeakBuffer| of.upgrade().is_some_and(|of| of == *buffer); - lock(&self.0).note(buffer.downgrade(), range, same); + lock(&self.0).noted.note(buffer.downgrade(), range, same); } /// A commit of a command buffer whose commands may use a buffer as `uses` answers: take the /// ranges noted of those it may use, and copy their bytes in as their host copies hold them - /// now (staged, `Buffer::upload_now`), before the command buffer is submitted, so its own - /// submission that may use the buffer, or one before it that may, copies them in. The ranges - /// of the rest stay noted, in their order. - pub(crate) fn take_at_commit(&self, uses: impl Fn(&Buffer) -> bool) { + /// now (staged, `Buffer::upload_now`), before the command buffer is submitted. The commit's + /// stamp, which the command buffer keeps: one past every commit's taken before it, under the + /// same lock. Each upload carries it, and only a batch of a command buffer whose stamp is at + /// least as large takes it (`texture/first_move.rs`'s `register_upload`): its own submission + /// that may use the buffer copies them in, or a later commit's, never one committed before, + /// which reads the bytes its own commit took. The ranges of the rest stay noted, in their + /// order. + pub(crate) fn take_at_commit(&self, uses: impl Fn(&Buffer) -> bool) -> u64 { // Each buffer read here, let go of once the lock is free. let mut read = Vec::new(); - let taken = lock(&self.0).take(|of| { - let buffer = of.upgrade()?; - let used = uses(&buffer); - read.push(buffer); - Some(used) - }); + let (commit, taken) = { + let mut ranges = lock(&self.0); + ranges.commits += 1; + let taken = ranges.noted.take(|of| { + let buffer = of.upgrade()?; + let used = uses(&buffer); + read.push(buffer); + Some(used) + }); + (ranges.commits, taken) + }; for (buffer, range) in taken { if let Some(buffer) = buffer.upgrade() { - buffer.upload_now(range); + buffer.upload_now(range, commit); } } drop(read); + commit } } diff --git a/crates/metal2vulkan-runtime/src/managed_copy.rs b/crates/metal2vulkan-runtime/src/managed_copy.rs index 4bafd9314..7de154caa 100644 --- a/crates/metal2vulkan-runtime/src/managed_copy.rs +++ b/crates/metal2vulkan-runtime/src/managed_copy.rs @@ -263,7 +263,7 @@ impl AtCompletion { if let Some(caller) = buffer.caller_copy() { buffer.device().shared().counts.managed_write_undone(); // Unbounded: the completion makes no submission (the next one takes it). - let _ = caller.copy_in(&buffer, 0..caller.reach, false); + let _ = caller.copy_in(&buffer, 0..caller.reach, false, 0); } } } @@ -387,13 +387,15 @@ impl CallerCopy { /// Copy `range` of the pages into `buffer`'s device copy (the module docs, **Copies in**), /// clamped to the pages' reach. Where `bounded`, past the bound of what is staged, a /// submission waited for makes the copies now; else (a command buffer's completion, which - /// makes no submission) they wait for the device's next. Nothing while the pages are - /// detached. + /// makes no submission) they wait for the device's next. `commit` is the stamp of the commit + /// that took the range (`managed.rs`), 0 where none did (`Buffer::upload`). Nothing while the + /// pages are detached. pub(super) fn copy_in( &self, buffer: &Buffer, range: Range, bounded: bool, + commit: u64, ) -> Result<(), Error> { let range = range.start..range.end.min(self.reach); if range.is_empty() { @@ -415,15 +417,15 @@ impl CallerCopy { let pointer = import .mapped_contents() .ok_or_else(|| internal("a copied host buffer without contents"))?; - return stage_in(buffer, pointer, range, bounded); + return stage_in(buffer, pointer, range, bounded, commit); } let ((source, at), hold) = import.blit_at(range.start)?; let held: Box = Box::new((import, hold)); - buffer.copy_in_from((source, at), range, held, bounded) + buffer.copy_in_from((source, at), range, held, (bounded, commit)) } Side::Pages(pages, false) => { let range = range.start..range.end.min(pages.reach); - stage_in(buffer, pages.pointer, range, bounded) + stage_in(buffer, pages.pointer, range, bounded, commit) } Side::Pages(_, true) => Ok(()), } @@ -432,8 +434,14 @@ impl CallerCopy { /// Copy `range` of the pages into `buffer`'s device copy as they hold it now, at a commit /// after a `did_modify_range` (`managed.rs`): the CPU copies the bytes into staging buffers /// ([`stage_in`]), whatever reaches the pages, so a CPU write after the commit reaches no - /// command buffer committed before it. Nothing while the pages are detached. - pub(super) fn copy_in_now(&self, buffer: &Buffer, range: Range) -> Result<(), Error> { + /// command buffer committed before it, and for that commit's stamp, `commit`. Nothing while + /// the pages are detached. + pub(super) fn copy_in_now( + &self, + buffer: &Buffer, + range: Range, + commit: u64, + ) -> Result<(), Error> { let range = range.start..range.end.min(self.reach); if range.is_empty() { return Ok(()); @@ -441,7 +449,7 @@ impl CallerCopy { let Some(pointer) = self.mapped_contents() else { return Ok(()); }; - stage_in(buffer, pointer, range, true) + stage_in(buffer, pointer, range, true, commit) } /// `detach_backing` of the buffer: the host copy's (the module docs). An import's detach @@ -489,7 +497,7 @@ impl CallerCopy { // call where nothing does, as Metal runs it at the replace. let old = std::mem::replace(&mut *lock(&self.side), side); drop(old); - self.copy_in(buffer, 0..self.reach, true) + self.copy_in(buffer, 0..self.reach, true, 0) } } @@ -500,12 +508,14 @@ impl Drop for CallerCopy { } /// Copy `range` of the bytes at `pointer` (the buffer's byte 0) into `buffer`'s device copy -/// through staging buffers of [`STAGED_CHUNK`] bytes at most, read now. +/// through staging buffers of [`STAGED_CHUNK`] bytes at most, read now, for the commit stamped +/// `commit` (0: none, [`Buffer::upload`]). pub(super) fn stage_in( buffer: &Buffer, pointer: NonNull, range: Range, bounded: bool, + commit: u64, ) -> Result<(), Error> { let device = buffer.device(); let mut start = range.start; @@ -525,7 +535,7 @@ pub(super) fn stage_in( } let source = staging.buffer.at(0); let held: Box = Box::new(staging); - buffer.copy_in_from(source, start..start + bytes, held, bounded)?; + buffer.copy_in_from(source, start..start + bytes, held, (bounded, commit))?; start += bytes; } Ok(()) @@ -723,7 +733,7 @@ impl Device { naming, }); // The pages' bytes reach every command buffer committed from now on. - caller.copy_in(&buffer, 0..reach, true)?; + caller.copy_in(&buffer, 0..reach, true, 0)?; Ok(buffer) } diff --git a/crates/metal2vulkan-runtime/src/texture/first_move.rs b/crates/metal2vulkan-runtime/src/texture/first_move.rs index 5802880cf..70d5756c2 100644 --- a/crates/metal2vulkan-runtime/src/texture/first_move.rs +++ b/crates/metal2vulkan-runtime/src/texture/first_move.rs @@ -765,11 +765,14 @@ pub(crate) struct FirstFill { /// it ([`FillTake::Reached`]); `None` where none did, or none owns it. owner: Option, /// An upload into a buffer the program's command buffers may have read already - /// ([`FirstMoves::register_upload`]): a batch takes it only where its commands may reach the - /// memory, so it lands on that batch's `VkQueue`, after the command buffers before it there, - /// never because the batch takes every fill ([`Taken::every_fill`]); an internal - /// submission's take ([`FillTake::All`]) takes it too. - reached_only: bool, + /// ([`FirstMoves::register_upload`]), with the stamp of the commit that took its bytes + /// (`managed.rs`'s `Modified`; 0 where none did): a batch takes it only where its commands + /// may reach the memory and its command buffer was committed at or after that commit, so it + /// lands on that batch's `VkQueue`, after the command buffers before it there, and under no + /// command buffer committed before the bytes were taken; never because the batch takes every + /// fill ([`Taken::every_fill`]). An internal submission's take ([`FillTake::All`]) takes it + /// too. `None` for every other fill. + upload: Option, } impl FirstFill { @@ -800,7 +803,7 @@ impl FirstFill { acquire: None, key: None, owner: None, - reached_only: false, + upload: None, } } @@ -829,7 +832,7 @@ impl FirstFill { acquire: None, key: None, owner: None, - reached_only: false, + upload: None, }); } let zeros = moves.zeros(device, 0)?; @@ -863,7 +866,7 @@ impl FirstFill { acquire: None, key: None, owner: None, - reached_only: false, + upload: None, } } @@ -880,7 +883,7 @@ impl FirstFill { acquire: Some((source, target)), key: None, owner: None, - reached_only: false, + upload: None, } } @@ -1020,24 +1023,36 @@ pub(crate) enum FillTake<'r> { All, /// Every one, knowing what owns the memory of what it takes by `owner_of` (the owner under a /// key, `used_resources::owner_of`), but an upload whose memory its commands do not reach, as - /// `reaches` answers for what owns it (`FirstFill::reached_only`): a batch's on a device - /// without a fill queue. + /// `reaches` answers for what owns it, or of a commit after `commit`, its command buffer's + /// (`FirstFill::upload`): a batch's on a device without a fill queue. Owned { owner_of: &'r dyn Fn(ResourceKey) -> Option, reaches: &'r dyn Fn(ObjectId) -> bool, + commit: u64, }, /// A batch's on a device with a fill queue (the module docs): the fills of memory its commands /// may reach, as `reaches` answers for what owns it (`owner_of` naming the owner under a /// key, `used_resources::owner_of`); every fill of memory no use of the program's names; and /// every fill where it takes a move of an image in memory another resource owns, or a staged - /// copy ([`Taken::every_fill`]). + /// copy ([`Taken::every_fill`]); but an upload of a commit after `commit`, its command + /// buffer's (`FirstFill::upload`). Reached { owner_of: &'r dyn Fn(ResourceKey) -> Option, reaches: &'r dyn Fn(ObjectId) -> bool, + commit: u64, }, } impl FillTake<'_> { + /// The stamp of the commit of the batch's command buffer, where the take is a batch's: it + /// takes no upload of a later commit (`FirstFill::upload`). + fn commit(&self) -> Option { + match self { + FillTake::All => None, + FillTake::Owned { commit, .. } | FillTake::Reached { commit, .. } => Some(*commit), + } + } + /// What names the owner of the memory under a key, where the take knows it. fn owner_of(&self) -> Option<&dyn Fn(ResourceKey) -> Option> { match self { @@ -1327,11 +1342,14 @@ impl FirstMoves { } /// As [`register_bytes`](Self::register_bytes), of an upload into a buffer the program's - /// command buffers may have read already (a `Managed` buffer's copy in, `managed.rs`): a - /// batch takes it only where its commands may reach the buffer's memory - /// (`FirstFill::reached_only`), so it lands after the command buffers before that batch on - /// its `VkQueue`, which may still read the bytes it replaces, and before that batch's - /// commands. A batch of another queue that reaches none of the buffer leaves it waiting. + /// command buffers may have read already (a `Managed` buffer's copy in, `managed.rs`), of + /// bytes taken at the commit stamped `commit` (0: at none): a batch takes it only where its + /// commands may reach the buffer's memory and its command buffer was committed at or after + /// that commit (`FirstFill::upload`), so it lands after the command buffers before that batch + /// on its `VkQueue`, which may still read the bytes it replaces, and before that batch's + /// commands, and a command buffer committed before the bytes were taken, submitted after, + /// reads the bytes of its own commit, not these. A batch of another queue that reaches none + /// of the buffer leaves it waiting. pub(crate) fn register_upload( &self, keeper: Weak, @@ -1339,10 +1357,11 @@ impl FirstMoves { source: (vk::Buffer, u64), length: u64, staging: Box, + commit: u64, ) -> bool { let fill = FirstFill::bytes(buffer, start, source, length, staging).keyed(&keeper); let fill = FirstFill { - reached_only: true, + upload: Some(commit), ..fill }; self.register_fill_of_bytes(keeper, fill) @@ -1517,14 +1536,21 @@ impl FirstMoves { (fills.owner_of()).and_then(|owner_of| fill.key.and_then(owner_of)); let reached = match (&fills, fill.owner) { (FillTake::Reached { reaches, .. }, Some(owner)) => reaches(owner), - (FillTake::Owned { reaches, .. }, Some(owner)) if fill.reached_only => { + (FillTake::Owned { reaches, .. }, Some(owner)) + if fill.upload.is_some() => + { reaches(owner) } _ => true, }; // An upload goes only with a batch that may reach its memory, whatever - // else the batch takes. - let taken = reached || (every_fill && !fill.reached_only); + // else the batch takes, of a command buffer committed at or after the + // commit that took its bytes. + let in_time = match (fill.upload, fills.commit()) { + (Some(upload), Some(commit)) => upload <= commit, + _ => true, + }; + let taken = in_time && (reached || (every_fill && fill.upload.is_none())); match taken { // The submission holds the buffer's memory; the buffer goes as the // program lets go of it, once the lock is free. @@ -1554,13 +1580,13 @@ impl FirstMoves { /// Whether a batch whose commands name nothing takes no work waiting: none waits but fills /// of memory whose key `named_only` says only a batch that may reach it takes /// ([`FillTake::Reached`]), and uploads, which only such a batch takes on any device - /// (`FirstFill::reached_only`). A command buffer that needs no GPU work then goes with no + /// (`FirstFill::upload`). A command buffer that needs no GPU work then goes with no /// submission, and one that finds work waiting carries it as before /// (`command_buffer/submit.rs`). pub(crate) fn none_waiting_but(&self, named_only: impl Fn(ResourceKey) -> bool) -> bool { lock(&self.waiting).iter().all(|entry| { matches!(entry, Waiting::Fill(_, fill) - if fill.reached_only || fill.key.is_some_and(&named_only)) + if fill.upload.is_some() || fill.key.is_some_and(&named_only)) }) } @@ -2534,6 +2560,7 @@ mod tests { let fills = FillTake::Reached { owner_of: &owner_of, reaches: &reaches, + commit: 0, }; let taken = moves.take_with(|_| true, fills, || None, |_| true); assert!(!taken.every_fill()); @@ -2548,6 +2575,7 @@ mod tests { let fills = FillTake::Reached { owner_of: &owner_of, reaches: &none, + commit: 0, }; let taken = moves.take_with(|_| true, fills, || None, |_| true); assert!(taken.every_fill()); @@ -2564,6 +2592,7 @@ mod tests { let fills = FillTake::Reached { owner_of: &owner_of, reaches: &none, + commit: 0, }; let taken = moves.take_with(|_| true, fills, || None, |_| true); assert!(taken.every_fill()); @@ -2572,12 +2601,12 @@ mod tests { assert!(moves.none_waiting_but(|_| false)); } - /// As [`keyed_fill`], an upload into a buffer the program may have read already - /// (`FirstMoves::register_upload`). - fn upload(moves: &FirstMoves, raw: u64, key: ResourceKey) -> Arc { + /// As [`keyed_fill`], an upload into a buffer the program may have read already, of bytes + /// taken at the commit stamped `commit` (`FirstMoves::register_upload`). + fn upload(moves: &FirstMoves, raw: u64, key: ResourceKey, commit: u64) -> Arc { let buffer = keyed_fill(moves, raw, Some(key)); if let Some(Waiting::Fill(_, fill)) = lock(&moves.waiting).last_mut() { - fill.reached_only = true; + fill.upload = Some(commit); } buffer } @@ -2601,13 +2630,14 @@ mod tests { // Without a fill queue: a batch that reaches the other buffer takes every fill but the // upload. let _buffers = [ - upload(&moves, 1, unused), + upload(&moves, 1, unused, 1), keyed_fill(&moves, 2, Some(used)), - upload(&moves, 3, used), + upload(&moves, 3, used, 1), ]; let fills = FillTake::Owned { owner_of: &owner_of, reaches: &reaches, + commit: 1, }; let taken = moves.take_with(|_| true, fills, || None, |_| true); assert!(taken.every_fill()); @@ -2622,6 +2652,7 @@ mod tests { let fills = FillTake::Reached { owner_of: &owner_of, reaches: &none, + commit: 1, }; let taken = moves.take_with(|_| true, fills, || None, |_| true); assert!(taken.every_fill()); @@ -2632,18 +2663,62 @@ mod tests { let fills = FillTake::Owned { owner_of: &owner_of, reaches: &reaches_unused, + commit: 1, }; let taken = moves.take_with(|_| true, fills, || None, |_| true); assert_eq!(filled(&taken), [1]); drop(taken.submitted(None)); // An internal submission takes one whatever reaches it. - let _later = upload(&moves, 5, unused); + let _later = upload(&moves, 5, unused, 1); let taken = moves.take(|_| true); assert_eq!(filled(&taken), [5]); drop(taken.submitted(None)); assert!(moves.none_waiting_but(|_| false)); } + /// An upload goes with no batch of a command buffer committed before the commit that took its + /// bytes, though the batch reaches its buffer and is submitted after that commit: the batch of + /// the commit takes its own upload and leaves the later ones, in their order, to the batches + /// of their commits, on a device with a fill queue or without one. An upload no commit took + /// (stamped 0) goes with any batch that reaches it. + #[test] + fn an_upload_goes_with_no_batch_committed_before_its_bytes_were_taken() { + let moves = FirstMoves::default(); + let used = ResourceKey::new(); + let used_id = ObjectId::new(); + let owner_of = |key: ResourceKey| (key == used).then_some(used_id); + let reaches = |of: ObjectId| of == used_id; + let _buffers = [ + upload(&moves, 1, used, 2), + upload(&moves, 2, used, 3), + upload(&moves, 3, used, 4), + ]; + let owned = |commit: u64| FillTake::Owned { + owner_of: &owner_of, + reaches: &reaches, + commit, + }; + let reached = |commit: u64| FillTake::Reached { + owner_of: &owner_of, + reaches: &reaches, + commit, + }; + let taken = moves.take_with(|_| true, owned(1), || None, |_| true); + assert!(filled(&taken).is_empty(), "committed before every upload"); + drop(taken.submitted(None)); + let taken = moves.take_with(|_| true, reached(2), || None, |_| true); + assert_eq!(filled(&taken), [1]); + drop(taken.submitted(None)); + let taken = moves.take_with(|_| true, owned(4), || None, |_| true); + assert_eq!(filled(&taken), [2, 3]); + drop(taken.submitted(None)); + let _made = upload(&moves, 4, used, 0); + let taken = moves.take_with(|_| true, reached(1), || None, |_| true); + assert_eq!(filled(&taken), [4]); + drop(taken.submitted(None)); + assert!(moves.none_waiting_but(|_| false)); + } + /// An image of the program's whose uses name its key, holding its move. struct Owned(Mutex>, ResourceKey); @@ -2688,6 +2763,7 @@ mod tests { let fills = FillTake::Owned { owner_of: &owner_of, reaches: &|_| true, + commit: 0, }; moves.take_with(|_| true, fills, || None, |_| true) };