rt: a commit takes the Managed ranges named since the last, with commands or none

Before this change, a commit captured the Managed buffers' named ranges only if its command buffer
had commands. A command buffer with nothing for the GPU (no commands, or only dispatches the
runtime elides: a kernel that writes no memory, or a grid with no thread) took no submission. So
its copy-in waited for the next command buffer that did submit, and a program's "upload now"
commit uploaded nothing: the upload slid into its next, timed commit, and the wait on the
upload commit covered no copy.

Now every commit captures. In the submit thread, a command buffer with nothing for the GPU that a
copy-in waits for (Captured::waits_for: the oldest captured at or before its commit number, the
taker rule) leads a group as one with commands does. The copy-in becomes its submission, and it
completes after it. Elided dispatches stay elided. One with nothing named still takes no
submission.

Its other answers keep Metal's empty shape:
- It is scheduled as it joins, with its scheduled handlers run before the submission.
- It retires in flight order, with its completed handlers, Completed status and the wait.
- It writes no timestamp, so its GPU start and end are one time (command::retire's empty branch).
- The copy-in counts in runtime_command_buffers with its own timestamps.

This is within documented Metal: a named range may be taken at any commit after the naming. On an
AMD Radeon Pro 5300M, Metal takes it at the first commit whose encoder binds the buffer. The
runtime takes it at the same commit where that commit binds the buffer, an earlier one where it
binds nothing, and never a later one.

Docs: did_modify_range, the runtime README (the Managed section, the empty command buffer, GPU
times) and docs/API.md's accepted difference. The internal docs in capture.rs and submit.rs are
updated too.

Tests: the two device tests of the previous commit, and a host check of waits_for.
This commit is contained in:
Anees Iqbal committed 2026-11-21 01:49:57 -08:00
1 parent 492ac7b946
commit 52ee4de509
7 files changed
+72 -39

No files matched your search

+14 -8
View File
@@ -415,14 +415,18 @@ in its memory.
named as `did_modify_range` names a range.
`Buffer::did_modify_range(start, length)` names a range changed (`Refusal::ModifiedRange` past
the buffer), merged with the buffer's others where they overlap or touch, and submits and
allocates nothing. The next commit of a command buffer with commands, on any queue, takes every
range named since, once each: GPU copies from the consumer's block into the device copy,
recorded at the commit into a command buffer of their own. It goes in the submission of the
first command buffer committed at or after that commit that the device submits, whichever queue
and whichever waits held the committing one, as a part of its own ahead of that command
allocates nothing. The next commit of a command buffer, with commands or none, on any queue,
takes every range named since, once each: GPU copies from the consumer's block into the device
copy, recorded at the commit into a command buffer of their own. It goes in the submission of
the first command buffer committed at or after that commit that the device submits, whichever
queue and whichever waits held the committing one, as a part of its own ahead of that command
buffer's commands and outside its waits, after every earlier command on the device, which the
barrier ending each earlier command buffer orders; the copies record one barrier, after them.
A command buffer committed before it never takes it. The copy runs when that submission
A command buffer with nothing for the GPU (no commands, or only dispatches that record none: a
kernel that writes no memory, or a grid with no thread) that reaches the submit thread while
the copies wait for it takes a submission of them alone, and completes once they have run, its
GPU start and end one time as for any command buffer with nothing for the GPU. A command buffer
committed before it never takes it. The copy runs when that submission
reaches the GPU, so every command buffer committed after a range is named reads the bytes the
consumer wrote before the naming, as Metal's `didModifyRange:` asks; a write made after the
commit may reach it, and so may a command buffer committed before that runs after a later one,
@@ -1673,7 +1677,9 @@ made, with no lock, and never rewritten for layouts.
segments hold no commands, no event wait and no event signal takes no submission: the submit
thread schedules it as it reaches it, and it completes with its queue's command buffer before
it, in flight, or at once where that one has completed. Where that one is in a group held, it
joins the group, which goes at once, so its completion waits only on submitted work.
joins the group, which goes at once, so its completion waits only on submitted work. One that
a Managed buffer's copy-in waits for as it reaches it ("Memory blocks and buffers" above) takes
the copy-in as its submission instead, and completes after it.
- A command buffer's scheduled handlers run on the submit thread, once it is scheduled and before
its submission goes to the driver (one that presents a drawable: after, before the completion
thread learns of it; one that ends without a submission: once it ends, before it retires), with
@@ -1729,7 +1735,7 @@ made, with no lock, and never rewritten for layouts.
two timestamps' own span, and later command buffers keep their order. Mesa's drivers read the
two clocks in turn, in the order asked, the GPU's first, so a calibration's host time trails
its GPU tick by the time between the reads, and each completion bounds it from above. A command
buffer that took no submission writes no
buffer with nothing for the GPU, whether it took no submission or a Managed copy-in's, writes no
query: its `gpu_start_time` and `gpu_end_time` are one time, never 0: the latest `gpu_end_time`
of its queue's command buffers before it, or its `kernel_end_time` where that is later, the shape Metal
reports for a command buffer that does no work. A command buffer's GPU times so fall between
+12 -8
View File
@@ -499,14 +499,18 @@ impl Buffer {
/// `didModifyRange:`: the consumer changed `length` bytes from `start` of the buffer's bytes.
/// Of a Managed buffer with a device copy, the range is named, and the next commit of a command
/// buffer with commands, on any queue, takes every range named since, once each, merged where
/// they overlap or touch: GPU copies from the consumer's block into the device copy, and into
/// the images of its textures over the range in images of their own, go ahead of that command
/// buffer's commands in its submission. So a command buffer committed after this call reads
/// the bytes the consumer wrote before its commit, as Metal owes. The copy reads the
/// consumer's bytes as it runs, so a write made after the commit may reach it, where Metal
/// leaves the range undefined for the GPU. Of any other buffer it does nothing, as the GPU
/// reads the consumer's bytes. It allocates nothing.
/// buffer, with commands or none, on any queue, takes every range named since, once each,
/// merged where they overlap or touch: GPU copies from the consumer's block into the device
/// copy, and into the images of its textures over the range in images of their own, go ahead
/// of that command buffer's commands in its submission. One with nothing for the GPU (no
/// commands, or only dispatches that record none) submits the copies alone and completes after
/// them. So a command buffer committed after this call reads the bytes the consumer wrote
/// before its commit, as Metal owes. Metal on a discrete Mac GPU (an AMD Radeon Pro 5300M)
/// takes a named range at the first commit whose encoder binds its buffer; this takes it at
/// the next commit of any command buffer, the same commit where that one binds the buffer, and
/// never a later one. The copy reads the consumer's bytes as it runs, so a write made after the
/// commit may reach it, where Metal leaves the range undefined for the GPU. Of any other
/// buffer it does nothing, as the GPU reads the consumer's bytes. It allocates nothing.
///
/// Err: [`Refusal::ModifiedRange`] for a range that leaves the buffer; for a copy,
/// [`Error::Quiesced`].
+14 -6
View File
@@ -1,7 +1,7 @@
//! A Managed buffer's changed ranges, copied in at the commit: where a Managed buffer has a device
//! copy (a device whose Vulkan device type is neither integrated nor CPU), `didModifyRange:` only
//! names a range ([`Changes::add`]), and the next commit on any queue of a command buffer with
//! commands takes every range named since the last such commit, once each
//! names a range ([`Changes::add`]), and the next commit on any queue, of a command buffer with
//! commands or none, takes every range named since the last commit, once each
//! ([`Work::capture`](crate::submit::Work::capture)). Its copy-in ([`copy_in`]) is a command
//! buffer of its own, [`CopyIn`]: GPU copies of each range from the consumer's block into the
//! device copy, after the copies back owed to the textures over the ranges in images of their own
@@ -9,10 +9,12 @@
//! [`Captured`] at the capturing commit's number, a point in the commit order, and the first
//! command buffer submitted whose commit number is at or after it takes it ([`taker`]): it goes in
//! that command buffer's submission, as a part of its own ahead of its commands and outside its
//! waits. A command buffer committed before the capture leaves it pending, so it never goes ahead
//! of older work of the queue that may write the range. The submission keeps each consumer's
//! block and device copy until it retires. A copy-in records no barrier before its copies: the one
//! that ends every earlier segment, upload, fill and copy-in on the queue puts every earlier
//! waits. A command buffer with nothing for the GPU that it waits for ([`Captured::waits_for`])
//! takes a submission for it alone, and completes once the copies have run. A command buffer
//! committed before the capture leaves it pending, so it never goes ahead of older work of the
//! queue that may write the range. The submission keeps each consumer's block and device copy
//! until it retires. A copy-in records no barrier before its copies: the one that ends every
//! earlier segment, upload, fill and copy-in on the queue puts every earlier
//! command first, as it does for a segment (`record::end`), and the submission that carries it
//! makes the host's writes before it visible to the copies (Vulkan's host write ordering
//! guarantee).
@@ -163,6 +165,12 @@ impl<C> Captured<C> {
}
taken
}
/// Whether a copy-in waits for the command buffer numbered `number`: the oldest, captured at
/// or before its commit, goes in that command buffer's submission ([`taker`]).
pub(crate) fn waits_for(&self, number: u64) -> bool {
(self.0.front()).is_some_and(|&(captured, _)| taker(captured, &[number]).is_some())
}
}
/// Which command buffer of a submission takes a copy-in captured by the commit numbered `captured`,
+7 -6
View File
@@ -280,7 +280,8 @@ pub(crate) struct Gpu {
impl Gpu {
/// Nothing for the GPU: no segment holds commands, a wait or a signal. Such a command buffer
/// records no timestamps, takes no submission and captures nothing.
/// records no timestamps, and takes no submission unless a copy-in waits for it
/// (`crate::capture`): the copy-in is then its submission.
pub(crate) fn is_empty(&self) -> bool {
(self.segments.iter()).all(|segment| {
segment.command.is_none()
@@ -626,11 +627,11 @@ impl CommandBuffer {
if work.closed() {
return Err(Error::Quiesced);
}
// With commands, it captures the Managed buffers' ranges named since the last capture,
// at its number in the commit order (`crate::capture`); one with none leaves them for the
// next. A commit that fails after its capture leaves the copy-in at that number.
let empty = self.state().open()?.nothing_for_gpu();
let mut work = match empty || work.lost() {
// With commands or none, it captures the Managed buffers' ranges named since the last
// capture, at its number in the commit order (`crate::capture`). A commit that fails
// after its capture leaves the copy-in at that number.
self.state().open()?;
let mut work = match work.lost() {
true => work,
false => core.work.capture(&core, work)?,
};
+10 -8
View File
@@ -23,7 +23,8 @@
//! signals an event ends its group, which goes at once; one that waits on a semaphore leads a new
//! group, the one held going first. A group is one submission with one signal of the progress
//! semaphore, which the completion thread follows in commit order. A command buffer that takes no
//! submission of its own (one with nothing for the GPU, or one ended with an error) and finds a
//! submission of its own (one with nothing for the GPU that no copy-in waits for, or one ended
//! with an error) and finds a
//! group held joins it, and the group goes at once: what it retires behind is then submitted, so
//! its completion waits on nothing that only a later completion or wait would send.
//!
@@ -660,9 +661,9 @@ impl Work {
/// `didModifyRange:` of a Managed buffer with a device copy: the consumer's bytes
/// `start..end` of `buffer` changed. The range is only named, merged with the buffer's others
/// where they overlap or touch, holding the buffer weakly; the next commit of a command buffer
/// with commands copies it in ([`capture`](Self::capture)) where the buffer still lives. It
/// allocates nothing.
/// where they overlap or touch, holding the buffer weakly; the next commit, of a command
/// buffer with commands or none, copies it in ([`capture`](Self::capture)) where the buffer
/// still lives. It allocates nothing.
///
/// Err: [`Error::Quiesced`].
pub(crate) fn modify(
@@ -679,8 +680,8 @@ impl Work {
Ok(())
}
/// At a commit of a command buffer with commands, on the committing thread, under the work
/// lock `state`, which it keeps: takes every Managed buffer's range named since the last
/// At every commit, with commands or none, on the committing thread, under the work lock
/// `state`, which it keeps: takes every Managed buffer's range named since the last
/// capture (`crate::capture`) into one copy-in, GPU copies from the consumers' blocks into the
/// device copies, recorded into a command buffer of its own ([`capture::copy_in`]); none
/// where every buffer named is gone. It waits at the committing command buffer's number, the
@@ -1512,8 +1513,9 @@ impl WorkState {
return Step::Idle;
};
// A command buffer with nothing for the GPU takes no submission: it is scheduled now, and
// completes with its queue's work before it.
if pending.gpu.is_empty() {
// completes with its queue's work before it. One that a copy-in waits for goes on below,
// as one with commands does, so the copy-in is its submission and it completes after it.
if pending.gpu.is_empty() && !self.changes.captured.waits_for(pending.seq) {
if let Some(Entry {
cb,
pending: Some(mut pending),
@@ -102,6 +102,13 @@ fn a_copy_in_goes_ahead_of_the_first_command_buffer_committed_at_or_after_its_ca
"the first at or after it"
);
assert!(captured.take(&[12]).is_empty(), "each goes once");
// A command buffer with nothing for the GPU takes a submission only where one waits for it.
assert!(!captured.waits_for(12), "none waits");
captured.push(13, "thirteen");
assert!(!captured.waits_for(12), "not for an older one");
assert!(captured.waits_for(13), "for its capturer");
assert!(captured.waits_for(15), "for a later one, its capturer held");
assert_eq!(captured.take(&[13]), [(0, "thirteen")]);
// Two pending go ahead of the same command buffer, in their order.
captured.push(13, "thirteen");
captured.push(14, "fourteen");
+8 -3
View File
@@ -2759,13 +2759,18 @@ named states the rule in full.
- **The runtime's Managed copies read the consumer's bytes when the GPU reaches them**
(`metal2vulkan-runtime`'s `Buffer::did_modify_range` and `Texture::upload_region`). A Managed
buffer with a device copy (on a device that is neither integrated nor a CPU) takes its named
ranges into that copy at the next commit of a command buffer with commands, on any queue, by
device copies ahead of that command buffer's commands; a Managed texture's uploaded region is one
ranges into that copy at the next commit of a command buffer, with commands or none, on any
queue, by device copies ahead of that command buffer's commands; a Managed texture's uploaded region is one
device copy from its named bytes into its image, ahead of every command buffer committed after the
call on every queue. The copy reads the bytes as they are when the GPU runs it, so a write the
consumer makes after the commit and before then may be seen. Metal leaves it undefined: an Apple
M3 and an Intel UHD 630 take the bytes when the GPU reads them, an AMD Radeon Pro 5300M at the
commit.
commit. That 5300M takes a named range at the first commit whose encoder binds its buffer (a
dispatch of a zero grid included) and none at a commit that binds nothing; the runtime takes it
at the next commit of any command buffer: the same commit where that one binds the buffer, an
earlier one where it binds nothing, and never a later one. Only a write made after that earlier
commit without naming it again can tell them apart, and Metal leaves its bytes undefined for
the GPU.
**Function constants**