From e483982c8d7b567fa80978d52496109be860311b Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Mon, 23 Nov 2026 12:03:35 -0800 Subject: [PATCH] rt: a stale heap texture takes part until its next write, whether or not a link is left A texture's staleness outlives its links. When U, written over T's bytes, was released, its release took T's last link and set T's part to false. `uses()` keeps only placements that take part, so T's next write added no check, T never moved from undefined, and its stale flag was never read again. Vulkan's aliasing rule does not end when the other resource is destroyed. Now a placement takes part while it has a link, or while it is a texture that owes a move (stale, as the submit thread has staged the segments). A release leaves a stale texture taking part. The submit thread, as it stages a write that clears a texture's staleness, ends its part where no link is left, and with it its count in the device's placements that take part, which keeps the heap-free fast path. Both changes are O(1): the release's own walk of its links, and one test under the written placement's links lock, which the stage already took. A texture placed over another's bytes is stale from its placement. It now keeps taking part through that other's release until its first write, which moves it. --- crates/metal2vulkan-runtime/src/alias.rs | 29 +++++++++++++++++------ crates/metal2vulkan-runtime/src/hazard.rs | 4 ++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/alias.rs b/crates/metal2vulkan-runtime/src/alias.rs index 70d452ea5..99580f449 100644 --- a/crates/metal2vulkan-runtime/src/alias.rs +++ b/crates/metal2vulkan-runtime/src/alias.rs @@ -15,7 +15,9 @@ //! A use that writes a placement makes its texture links stale. A stale texture's next use that //! writes it moves its whole image from undefined to its home layout first, in a barrier that //! orders every earlier command before it and every later one after it. Reads move nothing. A -//! buffer is never moved; its writes make its texture links stale. +//! buffer is never moved; its writes make its texture links stale. A stale texture takes part +//! until that write, whether or not a link is left: the release of what wrote over its bytes +//! leaves them written over. //! //! Inside a command buffer, encode order is the order its commands run in: [`Marks`] keeps what //! the command buffer did to the placements it touched, and a write of one that a link was @@ -151,8 +153,9 @@ pub(crate) struct Placement { /// The placements of its heap whose bytes meet its, a buffer's textures alone, changed under /// its heap's lock. links: Mutex, - /// It takes part ([`Links::take_part`]): the one test a placement that does not pays, read - /// without the lock. + /// It takes part: it has a link ([`Links::take_part`]), or it is a stale texture + /// ([`Placement::owes_move`]). The one test a placement that does not pays, read without the + /// lock, and changed under its links' lock. linked: AtomicBool, /// As the submit thread has staged the segments: a link was written since its own last write, /// or it was placed over another's bytes and is not yet written. A texture's alone is read. @@ -224,12 +227,19 @@ impl Placement { place } - /// Whether it takes part: a texture whose bytes another placement of its heap meets, or a - /// buffer whose bytes a texture's meet. + /// Whether it takes part: a texture whose bytes another placement of its heap meets, or + /// whose next write owes a move ([`owes_move`](Self::owes_move)), or a buffer whose bytes a + /// texture's meet. pub(crate) fn linked(&self) -> bool { self.linked.load(Relaxed) } + /// A texture that is stale, as the submit thread has staged the segments: its next write + /// moves it, whether or not a link is left. + fn owes_move(&self) -> bool { + self.moved.is_some() && self.stale.load(Relaxed) + } + /// Placed over another's bytes: its first move from the layout it is made in waits for every /// earlier command's writes. pub(crate) fn over(&self) -> bool { @@ -259,7 +269,7 @@ impl Drop for Placement { for other in &met { let mut links = lock(&other.links); links.drop_to(me, texture); - let linked = links.take_part(); + let linked = links.take_part() || other.owes_move(); if other.linked.swap(linked, Relaxed) && !linked { self.heap.linked(false); } @@ -422,9 +432,14 @@ pub(crate) fn stage( .collect(); for place in &part.writes { place.stale.store(false, Relaxed); - for link in lock(&place.links).textures.iter() { + let links = lock(&place.links); + for link in links.textures.iter() { link.stale.store(true, Relaxed); } + // A texture that took part only for the move it owed takes none once written. + if !links.take_part() && place.linked.swap(false, Relaxed) { + place.heap.linked(false); + } } if moves.is_empty() { return Ok(None); diff --git a/crates/metal2vulkan-runtime/src/hazard.rs b/crates/metal2vulkan-runtime/src/hazard.rs index b49f58b92..be567a35f 100644 --- a/crates/metal2vulkan-runtime/src/hazard.rs +++ b/crates/metal2vulkan-runtime/src/hazard.rs @@ -197,7 +197,7 @@ impl HeapId { &self.places } - /// A placement of the heap gained its first link (`gained`) or lost its last. + /// A placement of the heap began to take part (`gained`) or ceased to (`crate::alias`). pub(crate) fn linked(&self, gained: bool) { let linked = &self.core.heap_ids.linked; match gained { @@ -220,7 +220,7 @@ impl Drop for HeapId { #[derive(Default)] pub(crate) struct HeapIds { state: Mutex, - /// The device's heap placements with links (`crate::alias`): while none has, a command's + /// The device's heap placements that take part (`crate::alias`): while none does, a command's /// uses look at no placement. linked: AtomicUsize, }