Files
metal2vulkan/crates/metal2vulkan-runtime/src/sharing.rs
T
Anees Iqbal ddd339e2a3 Count released objects by kind and shadow keeps by planner, for the residency churn
The steps this packet landed as, in order:
- Count no-copy imports a live import already holds
- Recycle a texture's image and memory for the next texture of the same making
- Add a control build that turns the image recycler off (--cfg m2v_no_recycle)
- Count image, buffer and memory releases by the place that released them
- Recycle the runtime's buffers and their memory for the next buffer of the same size

Content: the tree of `development` at db877e7a50, unchanged.
2026-10-26 13:19:03 +02:00

107 lines
4.3 KiB
Rust

//! How a device's buffers and images are shared among the queue families it submits to.
//!
//! A device that takes queues of several families (every family equivalent to the first, as
//! MoltenVK's four of one queue each: `device/create.rs`, `queue_plan`) submits each logical
//! queue's command buffers to its own queue, so any buffer or image may be used by queues of
//! every family. Vulkan keeps an exclusive resource's contents only in the family that owns it,
//! and the runtime transfers no ownership: every buffer and image it makes is concurrent among
//! all the device's families. A device of one family keeps them exclusive. MoltenVK reads no
//! sharing mode, so the concurrent form costs nothing there.
use ash::vk;
/// The sharing of a device's buffers and images: its queue families, the primary first.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Sharing<'a> {
families: &'a [u32],
}
impl<'a> Sharing<'a> {
/// The sharing of a device that submits to `families` (at least one).
pub(crate) fn new(families: &'a [u32]) -> Sharing<'a> {
Sharing { families }
}
/// The queue families it shares among.
pub(crate) fn families(self) -> &'a [u32] {
self.families
}
/// Whether resources are concurrent: the device submits to more than one family.
fn concurrent(self) -> bool {
self.families.len() > 1
}
/// `info` with the device's sharing.
pub(crate) fn buffer<'b>(self, info: vk::BufferCreateInfo<'b>) -> vk::BufferCreateInfo<'b>
where
'a: 'b,
{
match self.concurrent() {
true => info
.sharing_mode(vk::SharingMode::CONCURRENT)
.queue_family_indices(self.families),
false => info.sharing_mode(vk::SharingMode::EXCLUSIVE),
}
}
/// `info` with the device's sharing.
pub(crate) fn image<'b>(self, info: vk::ImageCreateInfo<'b>) -> vk::ImageCreateInfo<'b>
where
'a: 'b,
{
match self.concurrent() {
true => info
.sharing_mode(vk::SharingMode::CONCURRENT)
.queue_family_indices(self.families),
false => info.sharing_mode(vk::SharingMode::EXCLUSIVE),
}
}
/// Whether a resource of the caller's, made `shared` among `families` (those of a concurrent
/// one; ignored for an exclusive one), may be used on every queue of the device: on a device
/// of one family, an exclusive resource (the caller's family is the device's) or a concurrent
/// one among the device's family; on a device of several, a concurrent one among all of
/// them, as the runtime transfers no ownership.
pub(crate) fn admits(self, shared: vk::SharingMode, families: &[u32]) -> bool {
match shared == vk::SharingMode::CONCURRENT {
true => self.families.iter().all(|family| families.contains(family)),
false => !self.concurrent(),
}
}
}
#[cfg(test)]
mod tests {
use super::Sharing;
use ash::vk;
#[test]
fn one_family_keeps_resources_exclusive() {
let sharing = Sharing::new(&[0]);
let info = sharing.buffer(vk::BufferCreateInfo::default());
assert!(info.sharing_mode == vk::SharingMode::EXCLUSIVE);
assert_eq!(info.queue_family_index_count, 0);
let info = sharing.image(vk::ImageCreateInfo::default());
assert!(info.sharing_mode == vk::SharingMode::EXCLUSIVE);
assert!(sharing.admits(vk::SharingMode::EXCLUSIVE, &[]));
assert!(sharing.admits(vk::SharingMode::CONCURRENT, &[0, 2]));
assert!(!sharing.admits(vk::SharingMode::CONCURRENT, &[1, 2]));
}
#[test]
fn several_families_make_resources_concurrent_among_all() {
let families = [0, 1, 2, 3];
let sharing = Sharing::new(&families);
let info = sharing.buffer(vk::BufferCreateInfo::default());
assert!(info.sharing_mode == vk::SharingMode::CONCURRENT);
assert_eq!(info.queue_family_index_count, 4);
let info = sharing.image(vk::ImageCreateInfo::default());
assert!(info.sharing_mode == vk::SharingMode::CONCURRENT);
assert_eq!(info.queue_family_index_count, 4);
assert!(!sharing.admits(vk::SharingMode::EXCLUSIVE, &[]));
assert!(!sharing.admits(vk::SharingMode::CONCURRENT, &[0, 1, 2]));
assert!(sharing.admits(vk::SharingMode::CONCURRENT, &[3, 2, 1, 0]));
}
}