did_modify_range's bytes are taken at the next commit, as Metal takes them
What: did_modify_range (and a CPU write through a texture over a buffer) notes the range on the device (managed.rs Modified); every commit, at its start and before it returns, copies each noted range of a buffer with a host copy on the CPU into staging buffers as the host copy holds it then (Buffer::upload_now, CallerCopy::copy_in_now, managed_copy::stage_in), which the next submission copies into the device copy before any of its commands. A range the staging buffers find no memory for falls back to the copy from the host copy as the submission runs. Why: the copy in read the host copy (the caller's pages through their import, or the runtime's own host copy) when the GPU ran it, so a CPU write made after the commit, before the GPU reached the command buffer, was what the command buffer read. Metal on a Radeon Pro 5300M (macOS 26.7.1, reims's oracle) takes the bytes at the commit after didModifyRange: a write before the commit (after the call, or with the command buffer encoded before it) is read, one made 30 ms after the commit while the GPU spun is not. Capturing at the call would be wrong (steps 2 and 3 read the later write there). An RX 7900 XTX computed frame 0 of a benchmark on frame 1's input this way. Docs: managed.rs and managed_copy.rs state the rule and its cost (a CPU copy of the range's bytes at the commit); API.md, the runtime README and FAST-PATHS.md's managed row follow. managed_bytes_copied_in counts a caller copy's copies alone, as before. Verified: the lane test of the parent commit on an RTX 3090 (job ids in the report).
This commit is contained in:
1 parent
f7b5429a8a
commit
f62966f4f0
9 files changed
+151
-33
No files matched your search
@@ -448,8 +448,9 @@ after it, and 0 of 80 with the device dropped first
|
||||
Without unified memory a `Managed` no-copy buffer is two copies instead
|
||||
(`HostBacking::DeviceCopy`): the GPU's in device-local memory of the runtime's own, which every
|
||||
command reads and writes, and the caller's pages, which `contents` answers. The pages are
|
||||
copied in as the buffer is made, by `did_modify_range` (its range) and by a rebind (whole),
|
||||
before any command of a command buffer committed after the call; a CPU write no
|
||||
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
|
||||
`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
|
||||
|
||||
@@ -4296,13 +4296,38 @@ 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<u64>) {
|
||||
let staged = match self.mirror() {
|
||||
Some(HostCopy::Caller(caller)) => caller.copy_in_now(self, range.clone()),
|
||||
Some(HostCopy::Own(_)) => match self.mapped_contents() {
|
||||
Some(contents) => {
|
||||
super::managed_copy::stage_in(self, contents, range.clone(), true)
|
||||
}
|
||||
None => Ok(()),
|
||||
},
|
||||
None => Ok(()),
|
||||
};
|
||||
// A failed submission past the bound fails the device, which the commit's own
|
||||
// submission reports.
|
||||
if staged.is_err() {
|
||||
let _ = self.upload(range);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// two-copy buffer's copy in from the caller's pages (`managed_copy.rs`), counted
|
||||
/// (`WorkCounts::managed_bytes_copied_in`). Where `bounded`, past the bound of what is
|
||||
/// staged, a submission waited for makes it now; else it waits for the device's next.
|
||||
/// (`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
|
||||
/// 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),
|
||||
@@ -4323,7 +4348,9 @@ impl Buffer {
|
||||
bytes,
|
||||
held,
|
||||
);
|
||||
device.shared().counts.managed_copied(bytes, true);
|
||||
if self.caller_copy().is_some() {
|
||||
device.shared().counts.managed_copied(bytes, true);
|
||||
}
|
||||
match past && bounded {
|
||||
true => device.submit_internal(|_, _| {}),
|
||||
false => Ok(()),
|
||||
|
||||
@@ -2305,7 +2305,8 @@ impl Texels {
|
||||
// reported, so the copies of the pages take it (`dirty_pages.rs`).
|
||||
let at = target as u64;
|
||||
self.buffer.wrote_host(at..at + (end - start));
|
||||
self.buffer.upload(start..end)
|
||||
self.buffer.modified(start..end);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The mapped bytes the CPU reaches the texels' bytes `start..start + len` of the buffer's
|
||||
@@ -2372,7 +2373,7 @@ impl Texels {
|
||||
.ok_or_else(|| invalid(call, Problem::Copy(CopyProblem::CpuAccessPrivate)))?;
|
||||
let at = contents.as_ptr() as u64 + start;
|
||||
self.buffer.wrote_host(at..at + (end - start));
|
||||
self.buffer.upload(start..end)?;
|
||||
self.buffer.modified(start..end);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -3339,6 +3339,10 @@ 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();
|
||||
// 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)? {
|
||||
|
||||
@@ -576,6 +576,9 @@ pub(super) struct DeviceShared {
|
||||
/// Its copied host buffers, compared at each commit and each batch's completion
|
||||
/// (`copied.rs`).
|
||||
pub(crate) copied: Arc<super::copied::CopiedBuffers>,
|
||||
/// The ranges of buffers with a host copy the CPU named since the last commit, which the next
|
||||
/// commit copies in (`managed.rs`).
|
||||
pub(crate) modified: super::managed::Modified,
|
||||
/// How many of the next imports of host pages by their pointer fail as the driver's out of
|
||||
/// device memory: for the tests of `copied.rs` (`Device::fail_next_host_imports`).
|
||||
#[cfg(test)]
|
||||
@@ -2933,6 +2936,7 @@ impl Device {
|
||||
aliases: Default::default(),
|
||||
host_ram: Default::default(),
|
||||
copied,
|
||||
modified: Default::default(),
|
||||
#[cfg(test)]
|
||||
failing_host_imports: Default::default(),
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -15,27 +15,44 @@
|
||||
//! - **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`
|
||||
//! copies the range from the host copy to the device's by the device's next submission, before
|
||||
//! any of its commands, and does not wait for it (it waited before); `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`).
|
||||
//! 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
|
||||
//! 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
|
||||
//! (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
|
||||
//! Graphics 630 (unified memory, one copy) it read the later write, as the GPU reads the memory
|
||||
//! the CPU wrote last. Read as the copy ran, a CPU write after the commit reached a command
|
||||
//! buffer the GPU had not reached yet. Taking the bytes costs a CPU copy of each range noted
|
||||
//! since the last commit into staging buffers of the runtime's own at that commit, freed once
|
||||
//! the submission that copies them in completed (`managed_copy.rs`, at most `STAGED_BYTES`
|
||||
//! staged ahead). A range the staging buffers find no memory for is copied in from the host
|
||||
//! copy as that submission runs, as before.
|
||||
|
||||
use super::buffer::Buffer;
|
||||
use super::buffer::{Buffer, WeakBuffer};
|
||||
use super::device::lock;
|
||||
use super::error::{invalid, Error, Problem};
|
||||
use super::values::StorageMode;
|
||||
use std::ops::Range;
|
||||
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 is copied to its own bytes by the device's next submission, before any of its
|
||||
/// commands; 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 the buffer.
|
||||
/// 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
|
||||
/// 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
|
||||
/// the buffer.
|
||||
pub fn did_modify_range(&self, range: Range<u64>) -> Result<(), Error> {
|
||||
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
|
||||
let _held = self.held_parts();
|
||||
@@ -55,11 +72,55 @@ impl Buffer {
|
||||
));
|
||||
}
|
||||
// A two-copy buffer over the caller's pages (`managed_copy.rs`): the range the program
|
||||
// asked, which the upload copies in.
|
||||
// asked, which the next commit 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);
|
||||
}
|
||||
self.upload(range)
|
||||
self.modified(range);
|
||||
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`]).
|
||||
pub(super) fn modified(&self, range: Range<u64>) {
|
||||
if self.mirror().is_none() || range.is_empty() {
|
||||
return;
|
||||
}
|
||||
self.device().shared().modified.note(self, range);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
#[derive(Default)]
|
||||
pub(super) struct Modified(Mutex<Vec<(WeakBuffer, Range<u64>)>>);
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
if let Some(buffer) = buffer.upgrade() {
|
||||
buffer.upload_now(range);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,12 @@
|
||||
//! (`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, `did_modify_range` copies its range in, 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
|
||||
//! 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,
|
||||
@@ -422,6 +423,21 @@ 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<u64>) -> Result<(), Error> {
|
||||
let range = range.start..range.end.min(self.reach);
|
||||
if range.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let Some(pointer) = self.mapped_contents() else {
|
||||
return Ok(());
|
||||
};
|
||||
stage_in(buffer, pointer, range, true)
|
||||
}
|
||||
|
||||
/// `detach_backing` of the buffer: the host copy's (the module docs). An import's detach
|
||||
/// answers as a no-copy buffer's; pages no import maps detach at once, their deallocator held
|
||||
/// until the buffer's next rebind or its release.
|
||||
@@ -479,7 +495,7 @@ 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.
|
||||
fn stage_in(
|
||||
pub(super) fn stage_in(
|
||||
buffer: &Buffer,
|
||||
pointer: NonNull<u8>,
|
||||
range: Range<u64>,
|
||||
|
||||
+7
-4
@@ -518,10 +518,13 @@ A texture of one level is `new_texture`'s, in place where that is.
|
||||
On a discrete GPU (no unified memory), a `Managed` no-copy buffer is two copies, as Metal keeps a
|
||||
`Managed` resource on a Mac with a discrete GPU (`HostBacking::DeviceCopy`): the GPU's own in
|
||||
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,
|
||||
at `did_modify_range` (its range) and at a rebind (whole), each before any command of a command
|
||||
buffer committed after the call; 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
|
||||
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
|
||||
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
@@ -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 before the next submission, the buffer whole at each
|
||||
`did_modify_range`'s range as the next commit 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)
|
||||
(`WorkCounts::managed_bytes_copied_in`, `managed_bytes_copied_out`, `managed_staging_bytes`). A
|
||||
|
||||
Reference in new issue
Block a user