diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index e65d7a5d9..32b4e134c 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -290,7 +290,8 @@ the runtime made. its bytes in memory of the device's own, announced (`copied-host-pages`, [HOWTO](../../docs/HOWTO.md#copied-host-buffers)), wherever the process maps the pages readable (a dma-buf's mapping among them); over pages not mapped readable it is refused. A - rebind's textures follow it onto the copy. + rebind's textures follow it onto the copy. A buffer over exactly the bytes a live copy covers + lies in that copy, as one over a live import's bytes lies in that import. `CommandBuffer::used_resources` lists what a command buffer uses until it completes, so a program knows when it may take memory away. - **Hazards** are tracked as Metal tracks them. A heap's resources are untracked by default, so diff --git a/crates/metal2vulkan-runtime/src/buffer.rs b/crates/metal2vulkan-runtime/src/buffer.rs index fab1bb6cc..ad3bae2e5 100644 --- a/crates/metal2vulkan-runtime/src/buffer.rs +++ b/crates/metal2vulkan-runtime/src/buffer.rs @@ -1169,6 +1169,12 @@ impl HostImport { self.copied.get().is_some() } + /// Where the bytes start in its first page: their offset in their host page for an import of + /// them, 0 for a copied host buffer's copy, which lays them from its first byte. + pub(super) fn offset(&self) -> u64 { + self.shape.offset + } + /// Keep the logs of the writes to its pages (set once, as its first buffer is made), which /// its copy, where it is one, reports its merges back to (`copied.rs`). pub(super) fn keep_log(&self, log: Arc) { diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs index ee01cc5aa..ec85aa69f 100644 --- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs @@ -22,6 +22,7 @@ use super::dirty_pages::{DirtySource, PagesLog}; use super::error::MemoryKind; use super::error::{internal, invalid, nil, vulkan, Error, Problem, RuntimeGap}; use super::heap::{Heap, HeapProblem}; +use super::host_aliases::Kind; use super::host_bytes::HostBytes; use super::host_memory::{ based_size, binds_in, buffer_base, import_refused, import_window, not_importable, @@ -502,8 +503,11 @@ impl Device { return Err(invalid(call, Problem::Heap(problem))); } // A buffer over the bytes a live no-copy buffer was made over, of its memory class, lies - // in its import (`host_aliases.rs`), its dma-buf source dropped uncalled; a heap's own - // buffer imports its own, as does one whose GPU writes are dropped. + // in its import, and a buffer of either class in its copy where it is a copied host + // buffer and no import of the class lives (`host_aliases.rs`, `copied.rs`): one + // `VkBuffer`, one snapshot and one key, its dma-buf source dropped uncalled and the gate + // unasked, as no page is mapped for it. A heap's own buffer imports or copies its own, as + // does one whose GPU writes are dropped (its import is a read-only view of the pages). let made_over = (pointer.as_ptr() as u64, length); let release = release.of(pointer, length); let shares = (!of_heap && !drops_writes) @@ -514,19 +518,39 @@ impl Device { } if let Some(import) = shares { let address = self.place_window(placement, &window, length)?; + let kind = match import.copied() { + true => Kind::Copy, + false => Kind::Import(class), + }; let buffer = self.buffer_in_import(Arc::clone(&import), length, modes, address, release); + let key = buffer.identity().key; // The GPU's writes through it are reported to the import's logs, as through the - // buffer it was made for, over the same bytes (`dirty_pages.rs`). + // buffer it was made for, over the same bytes (`dirty_pages.rs`), and merged back + // from the copy where it lies in one. if let Some(pages) = buffer.host_pages() { - pages.logged_as(buffer.identity().key); + pages.logged_as(key); + pages.copied_as(key); } - shared - .aliases - .join(buffer.identity().key, made_over, class, &import); - shared.aliases.place(buffer.identity().key, made_over); + if kind == Kind::Copy { + shared.counts.copied_host_buffer_shared(); + } + shared.aliases.join(key, made_over, kind, &import); + shared.aliases.place(key, made_over); return Ok(buffer); } + // A copied host buffer made below joins the memory as an import does, so that a later + // buffer over the same bytes lies in its copy; a heap's own copy is placed, joining none. + let noted = |buffer: Buffer| { + let key = buffer.identity().key; + if let (false, Some(pages)) = (of_heap, buffer.host_pages()) { + shared + .aliases + .join(key, made_over, Kind::Copy, pages.import()); + } + shared.aliases.place(key, made_over); + buffer + }; // Pages the caller resolves to runs of a guest RAM the device uses (`host_ram.rs`): asked // once, here, holding no lock of the runtime's, before the gate. One run inside one chunk // is bound in the chunk's import, with no import of its own: no pin, no kernel mapping, @@ -573,7 +597,7 @@ impl Device { shared.counts.host_import_refused_by_host(); // SAFETY: the caller's contract. let copied = unsafe { self.copied_buffer(copy, release, dirty, None) }; - return copied.map(|buffer| kept(buffer, log, kept_runs)); + return copied.map(|buffer| noted(kept(buffer, log, kept_runs))); } // Only a buffer that imports its pages pins them, and so has their writability // asked: one that lies in a live import pins none, that import's making asked for @@ -619,7 +643,7 @@ impl Device { // SAFETY: the caller's contract. let copied = unsafe { self.copied_buffer(copy, release, dirty, Some(error)) }; - return copied.map(|buffer| kept(buffer, log, kept_runs)); + return copied.map(|buffer| noted(kept(buffer, log, kept_runs))); } Err(error) => return Err(error), }; @@ -666,9 +690,12 @@ impl Device { // A buffer over the bytes another no-copy buffer covers joins it (`host_aliases.rs`), and // a later one lies in its import; one whose GPU writes are dropped joins none. if let (false, false, Some(current)) = (of_heap, drops_writes, current) { - shared - .aliases - .join(buffer.identity().key, made_over, class, current.import()); + (shared.aliases).join( + buffer.identity().key, + made_over, + Kind::Import(class), + current.import(), + ); } // Its uses order against every other key's over bytes they meet, a heap's own buffer's // too (`host_aliases.rs`). @@ -772,17 +799,33 @@ impl Device { false => None, }; // Bytes a live no-copy buffer was made over, of its memory class, are given the pages of - // its import, as a buffer made over them lies in it (`host_aliases.rs`), the dma-buf source - // dropped uncalled: they pin no page, that import's making asked about the same window, - // and the pages stay writable while imported (`HostMemory::new`). A heap's own buffer - // imports its own, whose images bound to its bytes are made again over the new import's - // memory or its dma-bufs, as does a buffer whose GPU writes are dropped. + // its import, or of its copy where it is a copied host buffer and no import of the class + // lives, as a buffer made over them lies in it (`host_aliases.rs`), the dma-buf source + // dropped uncalled and the gate unasked: they pin no page, that import's making asked + // about the same window, and the pages stay writable while imported (`HostMemory::new`). + // An import lays the bytes at their offset in their page, so it serves textures planned + // at the old offset only where the bytes did not move in it; a copy lays them from its + // first byte, so it serves textures planned there, and no image in place, whose own copy + // lies in fresh pages it rides on. A heap's own buffer imports its own, whose images + // bound to its bytes are made again over the new import's memory or its dma-bufs, as does + // a buffer whose GPU writes are dropped. let made_over = (pointer.as_ptr() as u64, length); - let shares = (heap.is_none() && !drops_writes && !moved) + let shares = (heap.is_none() && !drops_writes) .then(|| shared.aliases.import(made_over, class)) - .flatten(); + .flatten() + .filter(|import| match import.copied() { + false => !moved, + true => { + in_place.is_empty() && (texels.is_empty() || import.offset() == cell.offset()) + } + }); let (pages, remade) = match shares { - Some(import) => (self.pages_in_import(import), Remade::default()), + Some(import) => { + if import.copied() { + shared.counts.copied_host_buffer_shared(); + } + (self.pages_in_import(import), Remade::default()) + } None => { // Pages the caller resolves to runs of a guest RAM the device uses // (`host_ram.rs`), as at the buffer's making: asked once, here, holding no lock of diff --git a/crates/metal2vulkan-runtime/src/copied.rs b/crates/metal2vulkan-runtime/src/copied.rs index e44173e72..0f5d3e407 100644 --- a/crates/metal2vulkan-runtime/src/copied.rs +++ b/crates/metal2vulkan-runtime/src/copied.rs @@ -14,6 +14,15 @@ //! read permission ([`mapped_readable`]): on Linux the kernel reads no page it cannot pin, such //! as a mapping of a dma-buf (`VM_PFNMAP`). Pages not mapped, or mapped without read permission //! (memory Metal makes no buffer over), keep the import's own refusal. +//! - **One memory, one copy.** A no-copy buffer made over exactly the bytes (the same first byte +//! and length) a live copied buffer was made over lies in its copy, of either storage mode, as +//! one made over a live import's bytes lies in that import (`host_aliases.rs`): one `VkBuffer`, +//! one snapshot, one hazard key, the gate not asked and no page read. So two textures over one +//! surface's pages are ordered as one resource's accesses are, with no trip through the pages. +//! A rebind to such bytes lies in the copy too where its textures were planned at the copy's +//! offset (its first byte) and no image in place rides on it. Counted: +//! `WorkCounts::copied_host_buffers_shared`. A heap's own buffer and a buffer whose GPU writes +//! are dropped keep their own (`host_aliases.rs`, "Not joined"). //! - **Coherence.** The runtime keeps, for each copy, the bytes the pages and the copy last agreed //! on (its snapshot). At each commit, before any of the command buffer's work is submitted, //! every byte the CPU changed in the pages since then is written into the copy @@ -52,6 +61,17 @@ //! same batch read the write back, and a later command buffer the pages' bytes. The CPU's writes //! to the pages through another mapping are copied in at each commit, as above. Counted: //! `WorkCounts::copied_dropped_bytes`. +//! - **Between copies, GPU to GPU** (`docs/FAST-PATHS.md`, known gaps). A copy and another copy, or +//! an import, of pages that overlap without being the same bytes (a rebind's bytes inside a +//! surface's, a guest RAM chunk's import over a copy's pages, a buffer whose texture moved in +//! its page, the bytes again after a reported remap) are two memories, which agree only through +//! the pages: at the writer's completion (its merge back, or its writes in place) and at the +//! reader's commit (its copy in). A read committed before the write completed reads the bytes +//! as of its commit, wherever the tracker orders it after the write: in one command buffer, in +//! one queue's command buffers in flight together, and across queues an event orders, as the +//! held reader is not copied in again when the event releases it. Of two such copies written in +//! flight, the second's merge back takes a byte the first merged for the CPU's write, which a +//! merge keeps, so the first write stays where Metal keeps the later. //! - **Event signals.** A batch's event signals, submitted while a copy is live, are seen at its //! completion turn alone, after the merge: no read of the value observes them earlier, and a //! wait for them is held on the host until that turn (`event_timeline.rs`), so the GPU's writes diff --git a/crates/metal2vulkan-runtime/src/host_aliases.rs b/crates/metal2vulkan-runtime/src/host_aliases.rs index 9990acbf0..8b6d12682 100644 --- a/crates/metal2vulkan-runtime/src/host_aliases.rs +++ b/crates/metal2vulkan-runtime/src/host_aliases.rs @@ -19,6 +19,13 @@ //! - **One memory, one import.** A buffer that joins a memory, of the memory class of a live //! import of it, lies in that import's `VkBuffer` (`HostImport`, `Device::buffer_in_import`): //! one resource to the driver, where the tracker's barriers are not all the ordering there is. +//! A copied host buffer (`copied.rs`) joins the memory as an import does, and a later buffer of +//! either class lies in its copy while no import of its class lives ([`Kind::Copy`]): the copy +//! is the runtime's own memory of one class whatever the storage mode, and two copies of one +//! memory agree only through the pages, at a completion's merge back and a commit's copy in, so +//! a read committed before the write's completion read the bytes from before it (the +//! consumer's two textures over one surface past its import gate, every texel stale in one +//! command buffer and across queues an event ordered). //! MoltenVK records no barrier between blits and leaves their order to Metal's hazard //! tracking, which is by `MTLBuffer`: two imports were two, and a blit's write through one //! raced a read through the other in one encoder and in one command buffer (an M4: mixed texels); @@ -32,7 +39,8 @@ //! moved as within one buffer (`blit.rs`, `blit_texture.rs`). A buffer rebound onto the bytes a //! live buffer was made over, of the class of its import, lies in that import too, joining no key //! (`Device::rebind_host_pages`; a heap's own buffer, and bytes with a texture's aliasing image -//! over them, import their own). +//! over them, import their own), and in a live copy of them where its textures were planned at +//! the offset the copy lays the bytes at (its first byte) and no image in place rides on it. //! - **Ownership stays the buffer's.** The added uses are hazard tracking's alone: what a command //! holds and lists (`EncoderCore::touch`, rebind holds, `used_resources`) is named by the //! command's own uses, each buffer by its own key. @@ -40,7 +48,9 @@ //! (`ShadowCopies::meets`), so a write through the other buffer, or a texture over it, makes //! the shadow copy in again, as a write through its own buffer does. //! - **Not joined.** Buffers over memory that overlaps without being the same range, a heap's -//! bytes, and the memory a rebound buffer takes, keep their own keys: Metal orders no two +//! bytes (a heap's own copy too), and the memory a rebound buffer takes, keep their own keys, +//! and a buffer whose GPU writes are dropped imports a read-only view of its own (a copy shared +//! with it would take its writes): Metal orders no two //! no-copy buffers over one allocation (the same probe's `host` cases read stale or mixed //! texels), so the runtime's joining of the same range is the IOSurface's ordering, which a //! race over plain no-copy buffers may also give. A rebound buffer leaves the memory it was @@ -98,6 +108,18 @@ pub(super) struct Alias { pub(super) key: ResourceKey, } +/// What a memory's import is to the buffers made over the memory later ([`HostAliases::import`]). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum Kind { + /// An import of the caller's pages, in memory of this class: a later buffer of the class lies + /// in it. + Import(MemoryClass), + /// A copy of the caller's pages in memory of the runtime's own (`copied.rs`): a later buffer + /// of either class lies in it while no import of its class lives, as the copy's memory is of + /// one class whatever the buffer's storage mode, the memory a copy of its own would take. + Copy, +} + /// The device's no-copy buffers by the host memory they were made over (the module docs), and /// the imports of it they lie in (`I`: a `HostImport`, but in unit tests). pub(super) struct HostAliases { @@ -166,24 +188,26 @@ struct Memory { key: ResourceKey, /// How many live buffers were made over it. buffers: usize, - /// Its imports a buffer made, by memory class: a later buffer of the class lies in it while - /// it lives. - imports: Vec<(MemoryClass, Weak)>, + /// Its imports a buffer made, by kind: a later buffer lies in one while it lives + /// ([`HostAliases::import`]). + imports: Vec<(Kind, Weak)>, /// The serial it was noted with ([`Over`]). serial: u64, } impl HostAliases { /// The live import of `class` of `memory`, the bytes a live buffer was made over (its first - /// byte's host address and its length): a buffer made over them lies in it. + /// byte's host address and its length), else its live copy ([`Kind::Copy`]): a buffer of + /// `class` made over them lies in it. pub(super) fn import(&self, memory: (u64, u64), class: MemoryClass) -> Option> { let inner = lock(&self.inner); let memory = inner.memories.get(memory)?; - memory - .imports - .iter() - .filter(|(made, _)| *made == class) - .find_map(|(_, import)| import.upgrade()) + let live = |kind: Kind| { + (memory.imports.iter()) + .filter(|(made, _)| *made == kind) + .find_map(|(_, import)| import.upgrade()) + }; + live(Kind::Import(class)).or_else(|| live(Kind::Copy)) } /// Whether a live import of `class` was made over memory that holds all of `memory` and more @@ -202,22 +226,23 @@ impl HostAliases { }) .any(|at| { inner.memories.get(at).is_some_and(|noted| { - (noted.imports.iter()) - .any(|(made, import)| *made == class && import.strong_count() > 0) + (noted.imports.iter()).any(|(made, import)| { + *made == Kind::Import(class) && import.strong_count() > 0 + }) }) }); found } /// Note the buffer of `key`, made over `memory` (its first byte's host address and its - /// length) and lying in `import`, of memory class `class`: it joins the memory a live buffer - /// was made over, or its key becomes the memory's; later buffers of `class` over the memory - /// lie in `import` unless a live import of it is noted already. + /// length) and lying in `import`, of `kind`: it joins the memory a live buffer was made over, + /// or its key becomes the memory's; later buffers over the memory lie in `import`, where + /// [`import`](Self::import) finds it, unless a live import of its kind is noted already. pub(super) fn join( &self, key: ResourceKey, memory: (u64, u64), - class: MemoryClass, + kind: Kind, import: &Arc, ) -> Option { let mut inner = lock(&self.inner); @@ -241,8 +266,8 @@ impl HostAliases { noted .imports .retain(|(_, import)| import.strong_count() > 0); - if !noted.imports.iter().any(|(made, _)| *made == class) { - noted.imports.push((class, Arc::downgrade(import))); + if !noted.imports.iter().any(|(made, _)| *made == kind) { + noted.imports.push((kind, Arc::downgrade(import))); } let owner = noted.key; let over = Over { diff --git a/crates/metal2vulkan-runtime/src/host_memory.rs b/crates/metal2vulkan-runtime/src/host_memory.rs index 1efe71c6c..058b9443f 100644 --- a/crates/metal2vulkan-runtime/src/host_memory.rs +++ b/crates/metal2vulkan-runtime/src/host_memory.rs @@ -169,13 +169,13 @@ pub type RunResolver = Arc Option>> + Send + /// copy whatever their permission, and a copy of pages the process maps read-only drops the GPU's /// writes, as Metal drops them. Called on the /// thread that makes the buffer, heap or rebind, holding no lock of the runtime's. Not asked for a -/// buffer or rebind that lies in a live import (one made over the same bytes), nor where the device -/// refused the import before it (no host-pointer import, an import alignment the range cannot -/// meet), nor for memory whose GPU writes Metal's private option bit 24 drops, nor for a buffer's -/// rebind to bytes at another offset in their page under a texture planned at the old one, which -/// is a copy without asking. A heap's rebind asks it as a heap's making does. An import it lets go -/// on that then fails (out of memory, another Vulkan error), of the pointer or of the dma-bufs of -/// the caller's source, makes the copy all the same. Every copy is announced +/// buffer or rebind that lies in a live import or copy (one made over the same bytes), nor where +/// the device refused the import before it (no host-pointer import, an import alignment the range +/// cannot meet), nor for memory whose GPU writes Metal's private option bit 24 drops, nor for a +/// buffer's rebind to bytes at another offset in their page under a texture planned at the old +/// one, which is a copy without asking. A heap's rebind asks it as a heap's making does. An +/// import it lets go on that then fails (out of memory, another Vulkan error), of the pointer or +/// of the dma-bufs of the caller's source, makes the copy all the same. Every copy is announced /// (`copied-host-pages`). No Metal counterpart. pub type ImportGate = Arc bool + Send + Sync + 'static>; diff --git a/crates/metal2vulkan-runtime/src/tests/host_aliases.rs b/crates/metal2vulkan-runtime/src/tests/host_aliases.rs index 94b3b73e2..3b7ae0de2 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_aliases.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_aliases.rs @@ -1,6 +1,6 @@ //! No-copy buffers over the same host memory (`host_aliases.rs`). -use super::super::host_aliases::{Alias, HostAliases, Memories}; +use super::super::host_aliases::{Alias, HostAliases, Kind, Memories}; use super::super::memory::MemoryClass; use super::super::resource::ResourceKey; use super::super::tracker::{Access, Use, UseRange}; @@ -13,7 +13,7 @@ type Aliases = HostAliases; /// Note `key` over `memory`, lying in a new import of `Shared` memory, which is dropped at once. fn join(aliases: &Aliases, key: ResourceKey, memory: (u64, u64)) -> Option { - aliases.join(key, memory, MemoryClass::Shared, &Arc::new(0)) + aliases.join(key, memory, Kind::Import(MemoryClass::Shared), &Arc::new(0)) } fn copy() -> Access { @@ -104,7 +104,7 @@ fn a_buffer_over_the_bytes_another_covers_lies_in_its_import() { let (first, second, managed) = (ResourceKey::new(), ResourceKey::new(), ResourceKey::new()); assert!(aliases.import(memory, MemoryClass::Shared).is_none()); let import = Arc::new(1); - aliases.join(first, memory, MemoryClass::Shared, &import); + aliases.join(first, memory, Kind::Import(MemoryClass::Shared), &import); let shared = aliases.import(memory, MemoryClass::Shared).unwrap(); assert!(Arc::ptr_eq(&shared, &import)); // Other bytes, or another class, share nothing. @@ -114,14 +114,14 @@ fn a_buffer_over_the_bytes_another_covers_lies_in_its_import() { assert!(aliases.import(memory, MemoryClass::Managed).is_none()); // A second over the memory lies in it, and the first import stays the memory's. let again = Arc::new(2); - aliases.join(second, memory, MemoryClass::Shared, &again); + aliases.join(second, memory, Kind::Import(MemoryClass::Shared), &again); assert!(Arc::ptr_eq( &aliases.import(memory, MemoryClass::Shared).unwrap(), &import )); // A managed buffer's import is the memory's for its class. let own = Arc::new(3); - aliases.join(managed, memory, MemoryClass::Managed, &own); + aliases.join(managed, memory, Kind::Import(MemoryClass::Managed), &own); assert!(Arc::ptr_eq( &aliases.import(memory, MemoryClass::Managed).unwrap(), &own @@ -130,7 +130,7 @@ fn a_buffer_over_the_bytes_another_covers_lies_in_its_import() { drop((import, shared)); assert!(aliases.import(memory, MemoryClass::Shared).is_none()); let third = ResourceKey::new(); - aliases.join(third, memory, MemoryClass::Shared, &again); + aliases.join(third, memory, Kind::Import(MemoryClass::Shared), &again); assert!(Arc::ptr_eq( &aliases.import(memory, MemoryClass::Shared).unwrap(), &again @@ -144,6 +144,47 @@ fn a_buffer_over_the_bytes_another_covers_lies_in_its_import() { drop((again, own)); } +/// A buffer over the bytes a live copy was made over (`copied.rs`) lies in the copy, of either +/// memory class, while no import of its class lives; a copy is not an import a buffer over part +/// of the bytes counts as inside, and once every buffer over the memory is gone it is not shared. +#[test] +fn a_buffer_over_the_bytes_a_copy_covers_lies_in_the_copy() { + let aliases = Aliases::default(); + let memory = (0x40000, 0x4000); + let (first, second, third) = (ResourceKey::new(), ResourceKey::new(), ResourceKey::new()); + let copy = Arc::new(1); + assert_eq!(aliases.join(first, memory, Kind::Copy, ©), None); + for class in [MemoryClass::Shared, MemoryClass::Managed] { + assert!(Arc::ptr_eq(&aliases.import(memory, class).unwrap(), ©)); + } + // Other bytes share nothing, and the copy is no live import around part of them. + assert!(aliases + .import((0x40000, 0x2000), MemoryClass::Shared) + .is_none()); + assert!(!aliases.inside_live((0x40000, 0x2000), MemoryClass::Shared)); + // A buffer that lies in it joins the memory's key. + assert_eq!( + aliases.join(second, memory, Kind::Copy, ©), + Some(Alias { key: first }) + ); + // An import of a class noted over the same bytes (two buffers made at once) is its class's. + let import = Arc::new(2); + aliases.join(third, memory, Kind::Import(MemoryClass::Managed), &import); + assert!(Arc::ptr_eq( + &aliases.import(memory, MemoryClass::Managed).unwrap(), + &import + )); + assert!(Arc::ptr_eq( + &aliases.import(memory, MemoryClass::Shared).unwrap(), + © + )); + for key in [first, second, third] { + aliases.forget(key); + } + assert!(aliases.import(memory, MemoryClass::Shared).is_none()); + drop((copy, import)); +} + /// The host page of the tests that name none of their own: an import's unit. const PAGE: u64 = 0x1000; @@ -203,11 +244,11 @@ fn a_reported_remap_ends_the_sharing_of_imports_made_before() { let (first, second, after) = (ResourceKey::new(), ResourceKey::new(), ResourceKey::new()); let import = Arc::new(1); assert_eq!( - aliases.join(first, memory, MemoryClass::Shared, &import), + aliases.join(first, memory, Kind::Import(MemoryClass::Shared), &import), None ); assert_eq!( - aliases.join(second, memory, MemoryClass::Shared, &import), + aliases.join(second, memory, Kind::Import(MemoryClass::Shared), &import), Some(Alias { key: first }) ); // Bytes next to the memory, on either side: nothing changes. @@ -220,7 +261,7 @@ fn a_reported_remap_ends_the_sharing_of_imports_made_before() { // A buffer made after imports anew and joins nothing; the second keeps the key it joined. let fresh = Arc::new(2); assert_eq!( - aliases.join(after, memory, MemoryClass::Shared, &fresh), + aliases.join(after, memory, Kind::Import(MemoryClass::Shared), &fresh), None ); assert_eq!(aliases.of(second), Some(Alias { key: first })); @@ -370,11 +411,11 @@ fn remaps_from_any_thread_keep_the_notes_whole() { let memory = memory(round + worker); let import = Arc::new(worker as u32); let (first, second) = (ResourceKey::new(), ResourceKey::new()); - aliases.join(first, memory, MemoryClass::Shared, &import); + aliases.join(first, memory, Kind::Import(MemoryClass::Shared), &import); let shared = aliases .import(memory, MemoryClass::Shared) .unwrap_or_else(|| Arc::clone(&import)); - aliases.join(second, memory, MemoryClass::Shared, &shared); + aliases.join(second, memory, Kind::Import(MemoryClass::Shared), &shared); aliases.forget(first); aliases.forget(second); assert_eq!(aliases.of(second), None); diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index cdccb9509..14d8c6f9c 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -342,6 +342,7 @@ pub(crate) struct Counters { copied_rebind_pages_made: AtomicU64, copied_rebind_images_made: AtomicU64, copied_heaps_made: AtomicU64, + copied_host_buffers_shared: AtomicU64, copied_copy_in_bytes: AtomicU64, copied_copy_in_bytes_skipped: AtomicU64, copied_merge_back_bytes: AtomicU64, @@ -2478,6 +2479,12 @@ impl Counters { (self.copied_rebind_pages_made).fetch_add(1, Ordering::Relaxed); } + /// A no-copy buffer made over, or a rebind to, the bytes a live copied host buffer was made + /// over lay in its copy, making none of its own (`copied.rs`, `host_aliases.rs`). + pub(crate) fn copied_host_buffer_shared(&self) { + (self.copied_host_buffers_shared).fetch_add(1, Ordering::Relaxed); + } + /// The device imported a chunk of `bytes` of a guest RAM it uses (`host_ram.rs`). pub(crate) fn host_ram_chunk_imported(&self, bytes: u64) { (self.host_ram_chunks_imported).fetch_add(1, Ordering::Relaxed); @@ -5172,6 +5179,12 @@ pub struct WorkCounts { /// in `copied_host_buffers_made` and its kin too: the heap's buffers lie in the copy, and its /// textures are shadows of its bytes. pub copied_heaps_made: u64, + /// The no-copy buffers made over, and the rebinds to, the bytes a live copied host buffer was + /// made over (the same first byte and length) that lay in its copy rather than making one of + /// their own (`host_aliases.rs`): one `VkBuffer`, one snapshot and one hazard key for the + /// memory, as an import of it is one, the gate not asked. In none of the counts of copies + /// made. + pub copied_host_buffers_shared: u64, /// The bytes the CPU changed in copied host buffers' pages that commits copied into the /// copies. pub copied_copy_in_bytes: u64, @@ -6395,6 +6408,8 @@ impl Device { copied_rebind_pages_made: counters.copied_rebind_pages_made.load(Ordering::Relaxed), copied_rebind_images_made: counters.copied_rebind_images_made.load(Ordering::Relaxed), copied_heaps_made: counters.copied_heaps_made.load(Ordering::Relaxed), + copied_host_buffers_shared: (counters.copied_host_buffers_shared) + .load(Ordering::Relaxed), copied_copy_in_bytes: counters.copied_copy_in_bytes.load(Ordering::Relaxed), copied_copy_in_bytes_skipped: (counters.copied_copy_in_bytes_skipped) .load(Ordering::Relaxed), diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index c7d488bab..94af3d3aa 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2548,6 +2548,7 @@ field metal2vulkan-runtime::WorkCounts.copied_heaps_made pub copied_heaps_made: field metal2vulkan-runtime::WorkCounts.copied_host_buffers_degraded pub copied_host_buffers_degraded: u64 field metal2vulkan-runtime::WorkCounts.copied_host_buffers_made pub copied_host_buffers_made: u64 field metal2vulkan-runtime::WorkCounts.copied_host_buffers_refused pub copied_host_buffers_refused: u64 +field metal2vulkan-runtime::WorkCounts.copied_host_buffers_shared pub copied_host_buffers_shared: u64 field metal2vulkan-runtime::WorkCounts.copied_merge_back_bytes pub copied_merge_back_bytes: u64 field metal2vulkan-runtime::WorkCounts.copied_merges_compared pub copied_merges_compared: u64 field metal2vulkan-runtime::WorkCounts.copied_merges_skipped pub copied_merges_skipped: u64 diff --git a/docs/API.md b/docs/API.md index 071e55c49..18c223c6f 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2727,7 +2727,13 @@ bound. private option bit 24 drops is never copied. A buffer's or heap's rebind is copied the same way, its heap's bound textures and its images in place made again over the copy, and a buffer's rebind to bytes at another offset in their page under a texture planned at the old one is a copy - laid out there (the detail says which). Counted: `WorkCounts::copied_host_buffers_made`, of them + laid out there (the detail says which). A buffer made over exactly the bytes (the same first + byte and length) a live copied buffer was made over, of either storage mode, lies in its copy, + as one over a live import's bytes lies in that import, and makes no copy of its own + (`copied_host_buffers_shared`, the gate not asked): one memory, ordered as one resource's + accesses; a rebind to them does too where its textures were planned at the copy's offset and no + image in place rides on it. A heap's own buffer and memory whose GPU writes are dropped keep + their own. Counted: `WorkCounts::copied_host_buffers_made`, of them `copied_host_buffers_refused`, `copied_host_buffers_degraded` and `copied_rebind_pages_moved` (and `copied_heaps_made`, `copied_rebind_pages_made`, `copied_rebind_images_made`); each such buffer, and each buffer placed in such a heap, answers `Buffer::host_backing` with @@ -2739,7 +2745,9 @@ bound. reads before the batch completes, is not seen, and each commit and completion compares every byte of every copy. An event signal submitted while a copy is live is seen once its batch's writes are back in the pages, as Metal's: its value at the completion turn, and a wait for it - held on the host until then (`event_signals_behind_merge`). + held on the host until then (`event_signals_behind_merge`). A copy and another memory over + overlapping but not the same bytes agree GPU to GPU only through the pages, so a read committed + before the write's completion reads the bytes from before it (docs/FAST-PATHS.md, known gaps). - `metal2vulkan-degrade-grouped-completion` (precision). `GroupedCompletion`: Batches of command buffers committed on one queue that the scheduler found ready together, or held while two or more earlier submissions of their queue ran, went to the driver in one `vkQueueSubmit`, only the last diff --git a/docs/FAST-PATHS.md b/docs/FAST-PATHS.md index 38cbb1793..9efa335c7 100644 --- a/docs/FAST-PATHS.md +++ b/docs/FAST-PATHS.md @@ -347,6 +347,17 @@ driver's. Each is open work. (0.3 and 3.5 ms on the 275HX). A completion whose command buffers' uses name the copies they write compares those alone. A copy is made only where the caller's gate refuses an import or an admitted import fails, never by default. +- **Two memories over overlapping host bytes agree GPU to GPU only through the pages when one is + a copied host buffer** (`copied.rs`, `copied-host-pages`): a copy and another copy or an import + of pages that overlap without being the same first byte and length (a rebind's bytes inside a + surface's, a guest RAM chunk's import over a copy's pages, a rebind whose texture moved in its + page, a buffer made after the range was reported remapped). The writer's bytes reach the pages + at its completion, and the reader's copy at its commit, so a read committed before the write + completed reads the bytes from before it: in one command buffer, in one queue's command buffers + in flight together, and across queues an event orders (the held reader is not copied in again + when the event releases it). Of two such copies written in flight, the first's write stays where + Metal keeps the later. Buffers over exactly the bytes a live copy covers lie in that copy and are + one memory (`copied_host_buffers_shared`). - **A copied host buffer's merge keeps the CPU's byte where the CPU and the GPU both wrote it** between a commit and the completion: bytes are merged one at a time, so a GPU write never rewrites a byte beside it, but a byte both wrote keeps the CPU's, where on Metal the later write diff --git a/docs/HOWTO.md b/docs/HOWTO.md index 68a28be81..0e9c7932b 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -360,8 +360,8 @@ happens where: byte's through the last's). `false` makes the copy (`WorkCounts::host_imports_refused_by_host`, `copied_host_buffers_refused`). A consumer with a budget of mappings (Linux's `vm.max_map_count`) refuses there, paying in copies instead of a failed call. It is not asked - for a buffer or rebind that lies in a live import of the same bytes, memory whose GPU writes - Metal's private option bit 24 drops, a rebind whose bytes moved in their page under a texture + for a buffer or rebind that lies in a live import or copy of the same bytes, memory whose GPU + writes Metal's private option bit 24 drops, a rebind whose bytes moved in their page under a texture (below), or where the device refused the pointer before it; - an import the device and the gate admitted fails, of the pointer or of the dma-bufs of the caller's source (out of memory, another Vulkan error: `copied_host_buffers_degraded`); @@ -370,6 +370,19 @@ happens where: `WorkCounts::host_imports_unpinnable`), and no dma-buf of the caller's source served them (`copied_host_buffers_degraded`). +A buffer made over exactly the bytes (the same first byte and length) a live copied buffer was +made over lies in that copy, of either storage mode, as a buffer over a live import's bytes lies in +that import: no second copy, no read of the pages, the gate not asked +(`copied_host_buffers_shared`). So textures of one surface over a new buffer each frame are one +memory with the surface's other buffers while one of them lives, ordered as one resource's +accesses in one command buffer and across queues, as Metal orders them; a buffer made after the +last one over the bytes went makes a new copy. A rebind to such bytes lies in the copy where its +textures were planned at the copy's offset (its first byte) and no image in place rides on it. A +heap's own buffer, and memory whose GPU writes Metal's private option bit 24 drops, keep their own. +A copy and another copy or import of overlapping but different bytes agree GPU to GPU only +through the pages: a read committed before the write completed reads the bytes from before it +([FAST-PATHS](FAST-PATHS.md), known gaps). + `Buffer::host_backing` answers, per buffer, whether it got the copy: `HostBacking::Copied`, else how its pages are imported (`ImportedByPointer`, `ImportedAsDmaBuf`, `InHostRam`), or `Detached`; `None` for a buffer over no host memory of the caller's. A buffer placed in a no-copy