A recovery now makes a no-copy buffer again on the new generation, over the caller's pages it lies in, from the memory they were given as (HostPages::made_of). It goes through the same making as before: an import of the pointer or of the source's dma-bufs, a run of a guest RAM the new device uses (carried just before), a copy, or a read-only view. The caller's gate and resolver are asked again there. The made buffer keeps the buffer's ObjectId, identity, label and purgeable state, its Metal address (a share of its range) and its range token. Its contents pointer and bytes are as they were. Release order: the deallocator and keep-alive move to a HeldBack (Release::carry_on). It now counts the imports of the pages still mapped (the lost generation's, then each later one's) rather than a single flag. The pages made on the new generation carry a release that tells the HeldBack the buffer's release came and that their own import went (Release::carried, HeldBack::released). So the deallocator runs at the buffer's release, or at its next rebind, once no generation's import maps the pages, or at once where a keep-alive keeps them mapped. A detach of the carried buffer holds back the same wait, as a detach holds back a deallocator. At the lost generation's rest, the buffer's parts there go (LostHost). It leaves that generation's live buffers and its registries of no-copy buffers. Its cell's pages go (Lying::Gone, which nothing reads from then on). The cell and the buffer let go of their handles of the generation (HostCell::device is now a MadeOn). A call under way that holds the parts defers this, as for any carried buffer, and a handed-out VkBuffer keeps them until the buffer goes. Left the lost generation's: a detached no-copy buffer, one whose last detach's deallocator still waits for its next rebind, a no-copy heap's buffers, and any buffer whose making on the new device fails. Verification: none yet. Tests follow. (cherry picked from commit 3a7ea4834d47fac623ef36a325a265b6d4a11e5b)
82 lines
4.7 KiB
Rust
82 lines
4.7 KiB
Rust
//! What the GPU uses for a buffer over the caller's host memory ([`Buffer::host_backing`], a
|
|
//! runtime extension): the caller's pages themselves, through one of the device's imports of
|
|
//! them, or a copy of them (`copied.rs`). Metal has no counterpart: a no-copy buffer's bytes are
|
|
//! always the caller's pages there.
|
|
//!
|
|
//! The answer is read from where the buffer lies now (`buffer::Lying`): its import's shape
|
|
//! (`buffer::Shape`: dma-bufs, a guest RAM's chunk, else a pointer) and whether that import is a
|
|
//! copy. It reads no page and takes no import's objects, so it keeps no import imported
|
|
//! (`host_residency.rs`).
|
|
|
|
use super::buffer::{Buffer, Lying};
|
|
|
|
/// What the GPU reads and writes for a buffer over the caller's host memory
|
|
/// ([`Buffer::host_backing`], a runtime extension): the caller's pages, through one of the
|
|
/// device's imports of them, or a copy of them. Metal has no counterpart: a no-copy buffer's bytes
|
|
/// are always the caller's pages there (`newBufferWithBytesNoCopy:length:options:deallocator:`),
|
|
/// so [`HostBacking::Copied`] is the one form that differs from Metal's.
|
|
#[non_exhaustive]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub enum HostBacking {
|
|
/// In place: the device imported the pages by their pointer (`VK_EXT_external_memory_host`),
|
|
/// or for memory whose GPU writes Metal's private option bit 24 drops, by the pointer of a
|
|
/// read-only view of them. An import the device may release while idle and import again at
|
|
/// its next use (a runtime-internal residency, which nothing of Metal's observes) answers so
|
|
/// throughout.
|
|
ImportedByPointer,
|
|
/// In place: the device imported dma-bufs of the pages that the caller's source made
|
|
/// ([`HostMemory::with_dma_buf`](crate::HostMemory::with_dma_buf)), where it refused their
|
|
/// pointer. Released while idle and imported again as an import by pointer may be.
|
|
ImportedAsDmaBuf,
|
|
/// In place: bound in the device's import of a chunk of a guest RAM it uses
|
|
/// ([`Device::use_host_ram`](crate::Device::use_host_ram),
|
|
/// [`HostMemory::with_runs`](crate::HostMemory::with_runs)), with no import of its own.
|
|
InHostRam,
|
|
/// A copy of the pages in memory of the device's own, which the GPU reads and writes in their
|
|
/// place: the caller's gate refused their import
|
|
/// ([`HostMemory::with_import_gate`](crate::HostMemory::with_import_gate)), the import the
|
|
/// device and the gate admitted failed, a rebind's new bytes lie at another offset in their
|
|
/// page than the old under a texture over the buffer, on Linux the process maps some of the
|
|
/// pages read-only, or off macOS the memory is of Metal's private option bit 24; the copy of
|
|
/// read-only pages or bit-24 memory drops the GPU's writes. Announced as the
|
|
/// [`copied-host-pages`](crate::Degradation::CopiedHostPages) degradation: each commit writes
|
|
/// into the copy the bytes the CPU changed in the pages, and each batch's completion writes
|
|
/// back the bytes the GPU changed, where Metal's GPU uses the pages themselves.
|
|
Copied,
|
|
/// In no pages: detached ([`Buffer::detach_backing`]) until a rebind gives it some.
|
|
Detached,
|
|
}
|
|
|
|
impl Buffer {
|
|
/// What the GPU uses for the buffer's bytes now, where they are the caller's host memory: a
|
|
/// no-copy buffer's, or for a buffer placed in a heap over the caller's host memory
|
|
/// (`Device::new_heap_with_bytes_no_copy`) its heap's ([`HostBacking`]). `None` for any other
|
|
/// buffer. A runtime extension; Metal has no counterpart, as a no-copy buffer's bytes are
|
|
/// always the caller's pages there: [`HostBacking::Copied`] answers where they are a copy
|
|
/// instead, announced as the
|
|
/// [`copied-host-pages`](crate::Degradation::CopiedHostPages) degradation.
|
|
///
|
|
/// The answer changes only where the buffer's pages do: at its
|
|
/// [`rebind_bytes_no_copy`](Buffer::rebind_bytes_no_copy), which may move it between an import
|
|
/// and a copy, and its [`detach_backing`](Buffer::detach_backing) (for a heap's buffer, its
|
|
/// heap's); between them it stays. A query alone: it reads no page and keeps no import
|
|
/// imported, unlike [`gpu_address`](Buffer::gpu_address).
|
|
pub fn host_backing(&self) -> Option<HostBacking> {
|
|
let pages = match self.pages_cell()?.lying() {
|
|
Lying::In(pages) => pages,
|
|
Lying::Detached(_) => return Some(HostBacking::Detached),
|
|
Lying::Gone(_) => super::buffer::read_gone(),
|
|
};
|
|
let backing = if pages.copied() {
|
|
HostBacking::Copied
|
|
} else if pages.in_ram_chunk() {
|
|
HostBacking::InHostRam
|
|
} else if pages.over_dma_buf() {
|
|
HostBacking::ImportedAsDmaBuf
|
|
} else {
|
|
HostBacking::ImportedByPointer
|
|
};
|
|
Some(backing)
|
|
}
|
|
}
|