did_modify_range: a commit takes the ranges of the buffers its command buffer uses, copied in on its queue

What: Modified::take_at_commit takes, at a commit, only the noted ranges of the buffers the
command buffer may use (CommandBuffer::may_use: its encoders' uses name the buffer or made it
resident, a command may reach memory no use names, its last encoder's end is still with the
lowering thread, or nothing owns the buffer's key); the rest stay noted, in their order, for a
commit that uses them (managed.rs's Noted). Their copies in, and every other upload into a
buffer the program may have read (Buffer::upload, copy_in_from), register as uploads
(FirstMoves::register_upload, FirstFill::reached_only): a batch takes one only where its
commands may reach the buffer, on a device with a fill queue or without one, never because it
takes every other fill; an internal submission takes it too, and a batch that needs no GPU work
leaves it waiting. FillTake::Owned carries the batch's reach for that. So the copy lands on the
user's queue, after the command buffers before it there, before the commands of the first that
uses it: Metal's shape. Docs: managed.rs, managed_copy.rs (the 'as on Metal' line now says what
holds: only two queues' unordered uses of the buffer race the copy), first_move.rs, API.md, the
runtime README and FAST-PATHS.md's managed row.

Why: any queue's commit took every noted range, and any queue's next submission took its copy in
(before f62966f4f0 the call registered it, with the same take), so a range a thread named after
committing a command buffer that reads the buffer could land, on another queue's submission,
before that command buffer ran: on an RTX 3090 round 0 of a buffer read round 2's bytes, a silent
wrong result on every discrete GPU.

Tests: managed.rs's take rule (a_commit_takes_the_noted_ranges_of_the_buffers_its_command_buffer_uses)
and first_move.rs's (an_upload_goes_only_with_a_batch_that_reaches_its_buffer), on every device;
the parent's named rounds on two queues and on one, on the RTX 3090 before and after (job ids in
the report). RUNTIME_VERSION wants a bump at landing.
This commit is contained in:
Anees Iqbal committed 2026-11-12 17:18:01 -08:00
1 parent 4ce008a6fc
commit 56a8aa5283
10 files changed
+313 -71

No files matched your search

+4 -1
View File
@@ -450,7 +450,10 @@ after it, and 0 of 80 with the device dropped first
command reads and writes, and the caller's pages, which `contents` answers. The pages are
copied in as the buffer is made and by a rebind (whole), before any command of a command
buffer committed after the call, and a `did_modify_range`'s range as the pages hold it at the
device's next commit (copied there on the CPU into staging buffers); a CPU write no
next commit of a command buffer that uses the buffer (copied there on the CPU into staging
buffers), copied in on that command buffer's queue after the command buffers before it there,
so a later range never lands under an earlier command buffer still to read the buffer; a CPU
write no
`did_modify_range` names is not seen by the GPU, as on a Mac with a discrete GPU. A blit's
`synchronize_resource` copies the GPU's copy out to the pages in its command buffer's order.
The copies run from an import of the pages where the device makes one, else (the gate refused
+11 -9
View File
@@ -4300,10 +4300,12 @@ impl Buffer {
}
/// Copy the bytes `range` of a buffer with a host copy ([`Mirror`]) from that copy to the
/// buffer's own, by the device's next submission, before any of its commands, after the
/// commands before it on its `VkQueue` (`texture/first_move.rs`), which the call does not wait
/// for: every command buffer committed after the call reads the bytes. The copy reads the host
/// copy as that submission runs, and the submission holds the host copy until it completed.
/// buffer's own, by the device's next submission whose commands may reach the buffer (or an
/// internal one), before any of its commands, after the commands before it on its `VkQueue`
/// (`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.
/// 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<u64>) -> Result<(), Error> {
@@ -4321,7 +4323,7 @@ impl Buffer {
s.staged.store(true, Ordering::Relaxed);
let weak: Weak<BufferShared> = Arc::downgrade(s);
let keeper: Weak<dyn Filled> = weak;
let past = (device.shared().textures.first_moves).register_bytes(
let past = (device.shared().textures.first_moves).register_upload(
keeper,
self.at(range.start),
(mirror.raw, range.start),
@@ -4359,9 +4361,9 @@ impl Buffer {
}
/// Copy `range` of the bytes of `source` (a `VkBuffer`, and where the buffer's byte
/// `range.start` lies in it) into the buffer's own, by the device's next submission, before
/// any of its commands, after the commands before it on its `VkQueue`, as
/// [`upload`](Self::upload) copies, the submission holding `held` until it completed: a
/// `range.start` lies in it) into the buffer's own, by the device's next submission whose
/// commands may reach the buffer (or an internal one), before any of its commands, after the
/// 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
@@ -4379,7 +4381,7 @@ impl Buffer {
s.staged.store(true, Ordering::Relaxed);
let weak: Weak<BufferShared> = Arc::downgrade(s);
let keeper: Weak<dyn Filled> = weak;
let past = (device.shared().textures.first_moves).register_bytes(
let past = (device.shared().textures.first_moves).register_upload(
keeper,
self.at(range.start),
source,
@@ -3344,6 +3344,22 @@ impl CommandBuffer {
self.commit_as("commit_failed", Some(failure), Hand::Wake)
}
/// Whether its commands may use `buffer`'s memory, for the copies in of a `Managed` buffer's
/// ranges at its commit (`managed.rs`): where a command may reach memory no use names, where
/// its last encoder's end is still with the device's lowering thread (its uses not read yet),
/// where nothing owns the buffer's key, or where its encoders' uses name the buffer or made it
/// resident (`State::uses`).
fn may_use(&self, buffer: &Buffer) -> bool {
let shared = self.0.device.shared();
let Some(owner) = crate::used_resources::owner_of(shared, buffer.identity().key) else {
return true;
};
let state = self.state();
state.reaches_unnamed
|| (state.ending.is_some() && !state.end_lowered)
|| state.uses(shared, None, &[owner])
}
/// [`commit`](Self::commit) for `call`, or, with `failure`,
/// [`commit_failed`](Self::commit_failed), its batches handed to the scheduler as `hand` says.
fn commit_as(
@@ -3352,10 +3368,11 @@ impl CommandBuffer {
failure: Option<CommitFailure>,
hand: Hand,
) -> Result<(), Error> {
// The bytes of the ranges `did_modify_range` noted since the last commit, as the host
// copies hold them now, for this command buffer's submission or one before it
// (`managed.rs`).
self.0.device.shared().modified.take_at_commit();
// 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`).
let modified = &self.0.device.shared().modified;
modified.take_at_commit(|buffer| self.may_use(buffer));
// 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)? {
@@ -693,6 +693,7 @@ impl CommandBufferShared {
},
false => FillTake::Owned {
owner_of: &owner_of,
reaches: &reaches,
},
};
let hold = || crate::heap_sets::hold(device);
+82 -30
View File
@@ -15,17 +15,25 @@
//! - **Without unified memory** (a discrete GPU), it has two copies, as Metal's managed resources
//! have on such a device: its own bytes in device-local memory, which the GPU uses, and a host
//! copy in such cached memory, which `contents` maps (`buffer.rs`'s `Mirror`). `did_modify_range`
//! notes the range ([`Modified`]); the device's next commit, of a command buffer of any of its
//! 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, before any of its commands
//! (`synchronize_resource` copies the buffer back where the command buffer runs it). A program
//! notes the range ([`Modified`]); the next commit of a command buffer whose commands may use
//! 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
//! 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
//! runs it). A program
//! that skips them sees stale bytes there, as on a Mac with a discrete GPU. A GPU writing
//! host-cached memory across the bus ran a kernel's writes at 0.03 to 0.1 GB/s on an RTX 3090
//! and an RTX 5080, where a device-local write and a copy back ran at several GB/s. A `Managed`
//! no-copy buffer is two copies the same way, its host copy the caller's pages
//! (`managed_copy.rs`).
//! - **The bytes a command buffer reads** are the host copy's at the commit after the
//! `did_modify_range`, Metal's answer on a Mac with a discrete GPU: on an AMD Radeon Pro 5300M
//! - **The bytes a command buffer reads** are the host copy's at the commit, after the
//! `did_modify_range`, of the first command buffer that may use the buffer, Metal's answer on a
//! Mac with a discrete GPU: on an AMD Radeon Pro 5300M
//! (macOS 26.7.1), a kernel copying a 4 KiB `Managed` buffer behind a spin kernel read the CPU's
//! write made before the commit (after the call, and with the command buffer encoded before the
//! write), and not one made 30 ms after the commit while the GPU still spun. On an Intel UHD
@@ -47,8 +55,9 @@ use std::sync::Mutex;
impl Buffer {
/// `didModifyRange:`: the CPU wrote `range` of a managed buffer; command buffers committed
/// after this call see it. Where the buffer has a host copy (a device without unified memory),
/// the range's bytes are taken from it at the device's next commit and copied to its own bytes
/// by the submission after it, before any of its commands (the module docs); the call does not
/// the range's bytes are taken from it at the next commit of a command buffer that may use the
/// buffer and copied to its own bytes by that command buffer's submission, or one before it
/// that may use the buffer, before any of its commands (the module docs); the call does not
/// wait for the GPU. On a buffer of another storage mode it does nothing, whatever the range,
/// as plain Metal does (RT2 `managed.didmodify.shared`: `Shared` and `Private` buffers, in
/// range and past it; Metal's validation stops it). Refused for a managed buffer's range past
@@ -72,7 +81,7 @@ impl Buffer {
));
}
// A two-copy buffer over the caller's pages (`managed_copy.rs`): the range the program
// asked, which the next commit copies in.
// asked, which the next commit that may use the buffer copies in.
if self.caller_copy().is_some() && !range.is_empty() {
let counts = &self.device().shared().counts;
counts.managed_asked(range.end - range.start, true);
@@ -81,8 +90,8 @@ impl Buffer {
Ok(())
}
/// The CPU wrote `range` of the buffer's host copy, where it has one: the device's next
/// commit takes the bytes and copies them in ([`Modified`]).
/// The CPU wrote `range` of the buffer's host copy, where it has one: the next commit of a
/// command buffer that may use the buffer takes the bytes and copies them in ([`Modified`]).
pub(super) fn modified(&self, range: Range<u64>) {
if self.mirror().is_none() || range.is_empty() {
return;
@@ -92,35 +101,78 @@ 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) since the device's last commit: the next commit
/// copies each in, as the host copy holds it then (the module docs). A buffer gone meanwhile
/// takes its ranges with it.
/// 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.
#[derive(Default)]
pub(super) struct Modified(Mutex<Vec<(WeakBuffer, Range<u64>)>>);
pub(super) struct Modified(Mutex<Noted<WeakBuffer>>);
impl Modified {
/// Note `range` of `buffer`: once, where a range noted of it already holds it.
fn note(&self, buffer: &Buffer, range: Range<u64>) {
let mut noted = lock(&self.0);
let held = noted.iter().any(|(of, at)| {
at.start <= range.start
&& range.end <= at.end
&& of.upgrade().is_some_and(|of| of == *buffer)
});
if !held {
noted.push((buffer.downgrade(), range));
}
let same = |of: &WeakBuffer| of.upgrade().is_some_and(|of| of == *buffer);
lock(&self.0).note(buffer.downgrade(), range, same);
}
/// A commit: take every range noted so far and copy its bytes in as its host copy holds them
/// now (staged, `Buffer::upload_now`), before the commit's command buffer is submitted, so the
/// submission that carries its work, or one before it, copies them in.
pub(crate) fn take_at_commit(&self) {
let noted: Vec<(WeakBuffer, Range<u64>)> = lock(&self.0).drain(..).collect();
for (buffer, range) in noted {
/// 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) {
// 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)
});
for (buffer, range) in taken {
if let Some(buffer) = buffer.upgrade() {
buffer.upload_now(range);
}
}
drop(read);
}
}
/// Ranges of buffers named by `B`, in the order they were noted ([`Modified`]).
pub(super) struct Noted<B>(Vec<(B, Range<u64>)>);
impl<B> Default for Noted<B> {
fn default() -> Self {
Noted(Vec::new())
}
}
impl<B> Noted<B> {
/// Note `range` of `of`: once, where a range noted of a buffer `same` says is `of` already
/// holds it.
pub(super) fn note(&mut self, of: B, range: Range<u64>, same: impl Fn(&B) -> bool) {
let held = (self.0.iter())
.any(|(noted, at)| at.start <= range.start && range.end <= at.end && same(noted));
if !held {
self.0.push((of, range));
}
}
/// The ranges whose buffer `taken` answers true for, in their order; those it answers
/// `None` for (their buffer gone) go, and the rest stay, in their order.
pub(super) fn take(
&mut self,
mut taken: impl FnMut(&B) -> Option<bool>,
) -> Vec<(B, Range<u64>)> {
let mut out = Vec::new();
let mut kept = Vec::new();
for (of, range) in self.0.drain(..) {
match taken(&of) {
Some(true) => out.push((of, range)),
Some(false) => kept.push((of, range)),
None => {}
}
}
self.0 = kept;
out
}
}
@@ -23,15 +23,21 @@
//! (`copied.rs`), which would be a third. `contents` is the caller's pointer, and the
//! deallocator and keep-alive go as a no-copy buffer's do.
//! - **Copies in.** The buffer's bytes and the rest of their last page ([`CallerCopy::reach`])
//! are copied in as it is made, a `did_modify_range`'s range at the device's next commit, as
//! the pages hold it then (`managed.rs`: staged by the CPU, [`CallerCopy::copy_in_now`]), and a
//! rebind copies the new pages in whole: each by the device's next submission, before any of its
//! commands, after the commands before it on its `VkQueue` (`texture/first_move.rs`), which the
//! call does not wait for, so every command buffer committed after the call reads them, and none
//! in flight on that queue sees the copy land under it. A command buffer in flight on another queue that
//! reads the buffer meanwhile races the copy, as on Metal. On a device with a fill queue (a copy
//! engine's second queue, `device/staging.rs`), a batch's copies in run there, before the batch,
//! which waits for them, after the batches before it on its `VkQueue`. Not on the staging queue,
//! are copied in as it is made, a `did_modify_range`'s range at the next commit of a command
//! buffer that may use the buffer, as the pages hold it then (`managed.rs`: staged by the CPU,
//! [`CallerCopy::copy_in_now`]), and a rebind copies the new pages in whole: each by the
//! device's next submission whose commands may use the buffer (or an internal one), before any
//! of its commands, after the commands before it on its `VkQueue`
//! (`texture/first_move.rs`'s `register_upload`), which the call does not wait for, so every
//! command buffer committed after the call that uses the buffer reads them, and none submitted
//! before that submission on its `VkQueue` sees the copy land under it. A command buffer of
//! another queue that uses none of the buffer neither takes the copy nor waits for it, so the
//! copy never lands under the program's command buffers still to read the bytes on the queue
//! it uses; one of another queue that uses the buffer meanwhile, unordered with the first by
//! the program, races it, as two queues' unordered uses of a resource do on Metal. On a device
//! with a fill queue (a copy engine's second queue, `device/staging.rs`), a batch's copies in
//! run there, before the batch, which waits for them, after the batches before it on its
//! `VkQueue`. Not on the staging queue,
//! which waits for no work in flight. From an import, the copy's source is
//! the import's `VkBuffer`, held (imported again where the device released it) until the
//! submission completed; from pages no import maps, the call copies the bytes on the CPU into
@@ -32,3 +32,35 @@ fn managed_memory_is_host_coherent_and_cached_first() {
[1, 3]
);
}
/// The take rule of the ranges `did_modify_range` notes (`managed.rs`'s `Modified`): a commit
/// takes the ranges of the buffers its command buffer may use, in their order, and leaves the
/// rest noted in theirs, before any noted later, for the commit of a command buffer that uses
/// them; a range a noted one of the same buffer holds is noted once, and a buffer gone takes its
/// ranges with it. Buffers here are named by number.
#[test]
fn a_commit_takes_the_noted_ranges_of_the_buffers_its_command_buffer_uses() {
use crate::managed::Noted;
let mut noted: Noted<u32> = Noted::default();
let note = |noted: &mut Noted<u32>, of: u32, range: std::ops::Range<u64>| {
noted.note(of, range, |noted| *noted == of);
};
note(&mut noted, 1, 0..64);
note(&mut noted, 2, 0..16);
note(&mut noted, 1, 128..256);
// Held by the first range of buffer 1: noted once. Buffer 2's own range is not buffer 1's.
note(&mut noted, 1, 16..32);
note(&mut noted, 3, 0..8);
// A commit of a command buffer that uses buffer 1 alone takes buffer 1's ranges.
let taken = noted.take(|of| Some(*of == 1));
assert_eq!(taken, [(1, 0..64), (1, 128..256)]);
// A range noted after it, of buffer 2, comes after the ones left.
note(&mut noted, 2, 32..48);
// Buffer 3 went: its range goes; one that uses nothing takes nothing.
let taken = noted.take(|of| (*of != 3).then_some(false));
assert!(taken.is_empty());
// One that uses buffer 2 takes both of its ranges, in their order.
let taken = noted.take(|of| Some(*of == 2));
assert_eq!(taken, [(2, 0..16), (2, 32..48)]);
assert!(noted.take(|_| Some(true)).is_empty());
}
@@ -59,7 +59,11 @@
//! its start, after them. There a batch takes only the fills of memory its commands may reach,
//! of the device's own buffers, and every fill where it takes a move of an image in memory
//! another resource owns, or a staged copy ([`FillTake::Reached`]); the rest wait for a batch
//! that may reach them (`device/staging.rs`).
//! that may reach them (`device/staging.rs`). On every device an upload into a buffer the
//! program's command buffers may have read already (a `Managed` buffer's copy in,
//! [`FirstMoves::register_upload`]) goes only with a batch whose commands may reach the buffer,
//! or an internal submission: so it lands after the command buffers before that batch on its
//! `VkQueue`, never under one of another queue still to read the bytes it replaces.
//! - **Staged copies**. `replace_region` of a texture whose `VkImage` is its own for good (in
//! memory of its own, or a heap's own pages) and takes no host or staging-queue copies (those
//! are queued for the staging queue instead, whose submission of them every submission of the
@@ -760,6 +764,12 @@ pub(crate) struct FirstFill {
/// What owns that memory (`used_resources::owner_of`), as the batch that took it last found
/// it ([`FillTake::Reached`]); `None` where none did, or none owns it.
owner: Option<ObjectId>,
/// 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,
}
impl FirstFill {
@@ -851,6 +861,7 @@ impl FirstFill {
acquire: None,
key: None,
owner: None,
reached_only: false,
}
}
@@ -1005,9 +1016,12 @@ pub(crate) enum FillTake<'r> {
/// handle's.
All,
/// Every one, knowing what owns the memory of what it takes by `owner_of` (the owner under a
/// key, `used_resources::owner_of`): a batch's on a device without a fill queue.
/// 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.
Owned {
owner_of: &'r dyn Fn(ResourceKey) -> Option<ObjectId>,
reaches: &'r dyn Fn(ObjectId) -> bool,
},
/// 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
@@ -1025,7 +1039,9 @@ impl FillTake<'_> {
fn owner_of(&self) -> Option<&dyn Fn(ResourceKey) -> Option<ObjectId>> {
match self {
FillTake::All => None,
FillTake::Owned { owner_of } | FillTake::Reached { owner_of, .. } => Some(*owner_of),
FillTake::Owned { owner_of, .. } | FillTake::Reached { owner_of, .. } => {
Some(*owner_of)
}
}
}
}
@@ -1304,6 +1320,34 @@ impl FirstMoves {
staging: Box<dyn Any + Send + Sync>,
) -> bool {
let fill = FirstFill::bytes(buffer, start, source, length, staging).keyed(&keeper);
self.register_fill_of_bytes(keeper, fill)
}
/// 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.
pub(crate) fn register_upload(
&self,
keeper: Weak<dyn Filled>,
(buffer, start): (vk::Buffer, u64),
source: (vk::Buffer, u64),
length: u64,
staging: Box<dyn Any + Send + Sync>,
) -> bool {
let fill = FirstFill::bytes(buffer, start, source, length, staging).keyed(&keeper);
let fill = FirstFill {
reached_only: true,
..fill
};
self.register_fill_of_bytes(keeper, fill)
}
/// Register `fill`, a copy of staged bytes into `keeper`'s buffer: whether the copies staged
/// now passed their bound.
fn register_fill_of_bytes(&self, keeper: Weak<dyn Filled>, fill: FirstFill) -> bool {
let _order = self.registration_place();
let mut waiting = lock(&self.waiting);
waiting.push(Waiting::Fill(keeper, fill));
@@ -1468,11 +1512,16 @@ impl FirstMoves {
Sorted::Fill(weak, mut fill, buffer) => {
fill.owner =
(fills.owner_of()).and_then(|owner_of| fill.key.and_then(owner_of));
let taken = every_fill
|| match (&fills, fill.owner) {
(FillTake::Reached { reaches, .. }, Some(owner)) => reaches(owner),
_ => true,
};
let reached = match (&fills, fill.owner) {
(FillTake::Reached { reaches, .. }, Some(owner)) => reaches(owner),
(FillTake::Owned { reaches, .. }, Some(owner)) if fill.reached_only => {
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);
match taken {
// The submission holds the buffer's memory; the buffer goes as the
// program lets go of it, once the lock is free.
@@ -1501,13 +1550,15 @@ 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`]). A command buffer that needs no GPU work then goes with no
/// ([`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
/// 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.key.is_some_and(&named_only)),
)
lock(&self.waiting).iter().all(|entry| {
matches!(entry, Waiting::Fill(_, fill)
if fill.reached_only || fill.key.is_some_and(&named_only))
})
}
/// Let go of the fills and copies that wait for what is at `keeper` (the address of what
@@ -2518,6 +2569,78 @@ 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<Unit> {
let buffer = keyed_fill(moves, raw, Some(key));
if let Some(Waiting::Fill(_, fill)) = lock(&moves.waiting).last_mut() {
fill.reached_only = true;
}
buffer
}
/// An upload goes only with a batch whose commands may reach its memory, on a device with a
/// fill queue or without one, even where the batch takes every other fill (a move of an
/// image in another resource's memory, or a device without a fill queue); a batch that
/// reaches none of it leaves it waiting, and so does one that needs no GPU work; an internal
/// submission takes it.
#[test]
fn an_upload_goes_only_with_a_batch_that_reaches_its_buffer() {
let moves = FirstMoves::default();
let (used, unused) = (ResourceKey::new(), ResourceKey::new());
let (used_id, unused_id) = (ObjectId::new(), ObjectId::new());
let owner_of = |key: ResourceKey| match key == used {
true => Some(used_id),
false => Some(unused_id),
};
let reaches = |of: ObjectId| of == used_id;
let none = |_: ObjectId| false;
// Without a fill queue: a batch that reaches the other buffer takes every fill but the
// upload.
let _buffers = [
upload(&moves, 1, unused),
keyed_fill(&moves, 2, Some(used)),
upload(&moves, 3, used),
];
let fills = FillTake::Owned {
owner_of: &owner_of,
reaches: &reaches,
};
let taken = moves.take_with(|_| true, fills, || None, |_| true);
assert!(taken.every_fill());
assert_eq!(filled(&taken), [2, 3]);
drop(taken.submitted(None));
assert!(moves.none_waiting_but(|_| false), "only an upload waits");
// With a fill queue, a move of an image in another resource's memory takes every fill but
// the upload no command of the batch reaches.
let placed = Arc::new(Placed(Mutex::new(vec![first(4, false)])));
let weak: Weak<Placed> = Arc::downgrade(&placed);
moves.register(weak);
let fills = FillTake::Reached {
owner_of: &owner_of,
reaches: &none,
};
let taken = moves.take_with(|_| true, fills, || None, |_| true);
assert!(taken.every_fill());
assert!(filled(&taken).is_empty());
drop(taken.submitted(None));
// A batch that reaches it takes it.
let reaches_unused = |of: ObjectId| of == unused_id;
let fills = FillTake::Owned {
owner_of: &owner_of,
reaches: &reaches_unused,
};
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 taken = moves.take(|_| true);
assert_eq!(filled(&taken), [5]);
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<Vec<FirstMove>>, ResourceKey);
@@ -2561,6 +2684,7 @@ mod tests {
let take = || {
let fills = FillTake::Owned {
owner_of: &owner_of,
reaches: &|_| true,
};
moves.take_with(|_| true, fills, || None, |_| true)
};
+9 -5
View File
@@ -524,11 +524,15 @@ On a discrete GPU (no unified memory), a `Managed` no-copy buffer is two copies,
device-local memory, which every command reads and writes, at the buffer's `gpu_address`, and the
caller's pages, which `contents` answers. The pages reach the GPU's copy as the buffer is made
and at a rebind (whole), each before any command of a command buffer committed after the call,
and a `did_modify_range`'s range as the pages hold it at the device's next commit, before any
command of that command buffer: a CPU write of the range after that commit reaches no command
buffer committed before it, however late the GPU reaches it, as on an AMD Radeon Pro 5300M
(macOS 26.7.1). That commit copies each range on the CPU into staging buffers, a cost of the
range's bytes. A CPU write that no `did_modify_range` names is not seen by the GPU. A blit encoder's `synchronize_resource` copies the GPU's copy out to the pages in its
and a `did_modify_range`'s range as the pages hold it at the next commit of a command buffer
that uses the buffer (its encoders' uses or `use_resource` name it, or a command reaches memory
no use names), before any command of that command buffer, on its queue after the command buffers
before it there: a CPU write of the range after that commit reaches no command buffer committed
before it, however late the GPU reaches it, as on an AMD Radeon Pro 5300M (macOS 26.7.1), and the
copy in of a later range never lands under an earlier command buffer still to read the buffer.
A commit of a command buffer that uses none of the buffer leaves the range for the next that
does. That commit copies each range on the CPU into staging buffers, a cost of the range's
bytes. A CPU write that no `did_modify_range` names is not seen by the GPU. A blit encoder's `synchronize_resource` copies the GPU's copy out to the pages in its
command buffer's order, which the CPU sees once the command buffer completed. The copies run
from an import of the pages where the device makes one (never a copy of them), else through
staging buffers the runtime frees once their work completed, at most 256 MiB staged ahead
+2 -1
View File
@@ -496,7 +496,8 @@ recorded here.
(`WorkCounts::managed_device_copy_bytes`), and copies across the bus, as GPU transfers from the
pages' import, else through staging buffers of at most 32 MiB, 256 MiB staged at most before the
call submits them and waits: the pages whole at the making and at each rebind, a
`did_modify_range`'s range as the next commit finds it (staged by the CPU there), the buffer
`did_modify_range`'s range as the next commit of a command buffer that uses the buffer finds
it (staged by the CPU there), the buffer
whole at each
`synchronize_resource` of it (of a texture over it, or its `synchronize_texture`, the texels'
bytes)