Heap textures reached through their retains: a heap use never lets go of a texture handle it took
Heap::textures_from upgraded every texture of the heap's list under its lock, then let go, with the lock free, of each one that no program handle held (Texture::for_heap_use, RV 2128). If the command buffer that held such a texture completed in between, that let-go was the texture's last handle, and the texture's release ran inside the heap use: on the encoding thread, or on the scheduler's steps thread in a host step's sync_heaps, outside any passing hold (resource_ids.rs). The heap's list now holds each texture's Retain weakly. Retain(Weak<TextureShared>) is made with the texture (Arc::new_cyclic). textures_from upgrades the retains, which only the program's handles hold, and reaches each texture through its retain (Texture::heap_use). A texture no program handle holds is never taken. A retain that outlives its texture's last program handle drops holding the texture weakly, so it never releases the texture. Behaviour is otherwise unchanged: the same textures are made resident. Checked by the existing heap-use tests (tests::heap_use_released, the residency and heap suites) on lavapipe. No deterministic test exists for the race itself: it is a let-go between two threads that the structure now rules out.
This commit is contained in:
1 parent
b3b930353d
commit
6feb4604c6
2 files changed
+66
-48
No files matched your search
@@ -63,7 +63,7 @@ use super::resource::{ObjectId, ResourceKey};
|
||||
use super::sparse::heap::SparseHeap;
|
||||
use super::sparse::texture::sparse_refusal;
|
||||
use super::sparse::SparsePageSize;
|
||||
use super::texture::{Texture, TextureShared};
|
||||
use super::texture::{Retain, Texture};
|
||||
use super::values::{CpuCacheMode, HazardTrackingMode, ResourceOptions, StorageMode};
|
||||
use super::Device;
|
||||
use metal2vulkan::device::AllocationLimits;
|
||||
@@ -305,7 +305,7 @@ struct HeapShared {
|
||||
/// The textures made in the heap, while they live: those a handle other than a heap use's
|
||||
/// holds are what `use_heap` makes resident, and an encoder that used the heap makes resident
|
||||
/// as it encodes each later command (`residency.rs`, [`Heap::textures_from`]).
|
||||
textures: Mutex<Numbered<TextureShared>>,
|
||||
textures: Mutex<Numbered<Retain>>,
|
||||
/// How many textures `textures` numbered, stored under its lock as each is held: what an
|
||||
/// encoder that used the heap reads at each command to see whether it made one since, at one
|
||||
/// load.
|
||||
@@ -1215,7 +1215,7 @@ impl Heap {
|
||||
/// every texture the heap made before it.
|
||||
pub(super) fn hold_texture(&self, texture: &Texture) {
|
||||
let mut textures = lock(&self.0.textures);
|
||||
textures.hold(texture.downgrade());
|
||||
textures.hold(texture.retained());
|
||||
// Stored while the texture is held: a load that reads it, then takes the lock, finds the
|
||||
// texture there.
|
||||
(self.0.textures_made).store(textures.made(), Ordering::Release);
|
||||
@@ -1228,20 +1228,25 @@ impl Heap {
|
||||
}
|
||||
|
||||
/// The heap's textures numbered `from` or later that a handle other than a heap use's holds,
|
||||
/// in the order they were made, each as a heap use holds it (`Texture::for_heap_use`), and
|
||||
/// the number the next texture it makes takes. Metal's `useHeap:` holds the heap, not its
|
||||
/// in the order they were made, each as a heap use holds it (`Texture::heap_use`), and the
|
||||
/// number the next texture it makes takes. Metal's `useHeap:` holds the heap, not its
|
||||
/// textures: one the program released, which only command buffers that used the heap hold,
|
||||
/// is not among them, so a later use does not hold it again and it goes as they complete.
|
||||
/// Each is reached through its retain (`texture::Retain`), which the program's handles hold:
|
||||
/// a texture none of them holds is never taken, so none is let go here.
|
||||
pub(super) fn textures_from(&self, from: u64) -> (Vec<Texture>, u64) {
|
||||
let (found, made) = {
|
||||
let (retains, made) = {
|
||||
let textures = lock(&self.0.textures);
|
||||
let found: Vec<Texture> = textures.since(from).filter_map(Texture::upgrade).collect();
|
||||
(found, textures.made())
|
||||
let retains: Vec<Arc<Retain>> =
|
||||
textures.since(from).filter_map(Weak::upgrade).collect();
|
||||
(retains, textures.made())
|
||||
};
|
||||
// With the lock free: a texture only heap uses hold may go here.
|
||||
let live = found
|
||||
.into_iter()
|
||||
.filter_map(Texture::for_heap_use)
|
||||
// With the lock free. A retain whose texture's last handle of the program's went
|
||||
// meanwhile goes here, holding the texture weakly: the texture goes with that handle,
|
||||
// or with the use, never here.
|
||||
let live = retains
|
||||
.iter()
|
||||
.filter_map(|retain| Texture::heap_use(retain))
|
||||
.collect();
|
||||
(live, made)
|
||||
}
|
||||
|
||||
@@ -426,13 +426,16 @@ impl ImagePlacement {
|
||||
#[derive(Clone)]
|
||||
pub struct Texture(Arc<TextureShared>, Option<Arc<Retain>>);
|
||||
|
||||
/// What a texture's handles hold, but those a heap use holds ([`Texture::for_heap_use`]) and
|
||||
/// those [`Texture::upgrade`] makes once no handle holds it: while it lives, the texture is among
|
||||
/// the textures of its heap that `use_heap` makes resident (`Heap::textures_from`). Metal's
|
||||
/// `useHeap:` holds the heap, not its textures: a texture the program released, which only command
|
||||
/// buffers that used its heap hold, is not made resident again by a later use of the heap, so it
|
||||
/// goes as those command buffers complete.
|
||||
pub(super) struct Retain;
|
||||
/// What a texture's handles hold, but those a heap use holds ([`Texture::heap_use`]) and those
|
||||
/// [`Texture::upgrade`] makes once no handle holds it: while it lives, the texture is among the
|
||||
/// textures of its heap that `use_heap` makes resident (`Heap::textures_from`), which holds the
|
||||
/// retain weakly and reaches the texture through it. Metal's `useHeap:` holds the heap, not its
|
||||
/// textures: a texture the program released, which only command buffers that used its heap hold,
|
||||
/// is not made resident again by a later use of the heap, so it goes as those command buffers
|
||||
/// complete. Reached through its retain, a texture no handle of the program's holds is never
|
||||
/// taken, so no heap use lets go of a handle it took, the texture's last among them, on whatever
|
||||
/// thread it runs.
|
||||
pub(super) struct Retain(Weak<TextureShared>);
|
||||
|
||||
pub(super) struct TextureShared {
|
||||
storage: Storage,
|
||||
@@ -5220,30 +5223,35 @@ impl Texture {
|
||||
},
|
||||
Storage::Texels(_) | Storage::Alias { .. } | Storage::Shadow { .. } => None,
|
||||
};
|
||||
let retain = Arc::new(Retain);
|
||||
let shared = Arc::new(TextureShared {
|
||||
storage,
|
||||
retain: Arc::downgrade(&retain),
|
||||
moved: OnceLock::new(),
|
||||
pending_move: Mutex::new(None),
|
||||
record,
|
||||
parent,
|
||||
id,
|
||||
label: Mutex::new(None),
|
||||
object_id: ObjectId::new(),
|
||||
held_by: HeldBy::default(),
|
||||
usage_shadow: OnceLock::new(),
|
||||
usage_shadow_making: Mutex::new(()),
|
||||
faces: OnceLock::new(),
|
||||
snapshot: Mutex::new(Vec::new()),
|
||||
coverage: Mutex::default(),
|
||||
kept_backings: Mutex::new(Vec::new()),
|
||||
render_shadow: Mutex::new(None),
|
||||
heap,
|
||||
facts: OnceLock::new(),
|
||||
planes_in_place,
|
||||
let mut retain = None;
|
||||
let shared = Arc::new_cyclic(|shared| {
|
||||
let made = Arc::new(Retain(Weak::clone(shared)));
|
||||
let weak = Arc::downgrade(&made);
|
||||
retain = Some(made);
|
||||
TextureShared {
|
||||
storage,
|
||||
retain: weak,
|
||||
moved: OnceLock::new(),
|
||||
pending_move: Mutex::new(None),
|
||||
record,
|
||||
parent,
|
||||
id,
|
||||
label: Mutex::new(None),
|
||||
object_id: ObjectId::new(),
|
||||
held_by: HeldBy::default(),
|
||||
usage_shadow: OnceLock::new(),
|
||||
usage_shadow_making: Mutex::new(()),
|
||||
faces: OnceLock::new(),
|
||||
snapshot: Mutex::new(Vec::new()),
|
||||
coverage: Mutex::default(),
|
||||
kept_backings: Mutex::new(Vec::new()),
|
||||
render_shadow: Mutex::new(None),
|
||||
heap,
|
||||
facts: OnceLock::new(),
|
||||
planes_in_place,
|
||||
}
|
||||
});
|
||||
let texture = Texture(shared, Some(retain));
|
||||
let texture = Texture(shared, retain);
|
||||
if let Some(objects) = texture.storage().objects() {
|
||||
objects.object_made();
|
||||
}
|
||||
@@ -5312,12 +5320,17 @@ impl Texture {
|
||||
Some(Texture(shared, retain))
|
||||
}
|
||||
|
||||
/// The texture as a heap use holds it (`Heap::textures_from`), while it holds its [`Retain`]:
|
||||
/// the texture object without the retain, so the use keeps the texture among its heap's
|
||||
/// textures no longer than the other handles do. `None` for one that holds no retain.
|
||||
pub(super) fn for_heap_use(self) -> Option<Texture> {
|
||||
let Texture(shared, retain) = self;
|
||||
retain.map(|_| Texture(shared, None))
|
||||
/// The texture `retain` is of, as a heap use holds it (`Heap::textures_from`): the texture
|
||||
/// object without the retain, so the use keeps the texture among its heap's textures no
|
||||
/// longer than the program's handles do. `None` once the texture is gone.
|
||||
pub(super) fn heap_use(retain: &Retain) -> Option<Texture> {
|
||||
retain.0.upgrade().map(|shared| Texture(shared, None))
|
||||
}
|
||||
|
||||
/// Its [`Retain`], weakly, for its heap's list (`Heap::hold_texture`): none for a handle
|
||||
/// that holds none.
|
||||
pub(super) fn retained(&self) -> Weak<Retain> {
|
||||
self.1.as_ref().map_or_else(Weak::new, Arc::downgrade)
|
||||
}
|
||||
|
||||
/// The texture object, held while something else keeps it (a heap's, `heap.rs`).
|
||||
|
||||
Reference in new issue
Block a user