A device-gap texture over a buffer (its image refused for a reason that leaves its rows in place: format, pitch, placement, source, dma-buf) was a shadow made at new_texture: its VkImage, memory and views, its notice and shadow-log line, and its slot in the texture table and every heap set. A slot whose function only reads, writes or asks its size binds the rows in place, so a texture only ever bound that way paid for a shadow no command used (reims's gbs91 on the 780M: hundreds of such textures, 8 to 52 MB each). Now such a texture (rows in place, no pending in-place verdict, not over a heap's buffer) is made with its shadow left unmade (texture/lazy_shadow.rs): the storage accessor makes it at the first reach, once, under a lock, with the making's notice, log line and counts, then places its slot (its resource ID reads the neutral until then). Paths that must not make it read the recipe instead (rows binding, placement and heap-set writes, CPU copies, blits on the bytes, shape and plan questions, logging). The reaches that make it: a slot that binds the image (before the command's copies are planned from its uses), use_resource, a pass's attachment, an execution of commands inheriting the bindings, a view, its VkImage handed out. An out-of-memory making fails the reaching call, as new_texture did. WorkCounts::shadows_left_unmade and shadows_made_at_use count both halves; docs/API.md says when the shadowed-texture notice is given. Not in scope: two kinds still make their shadow at new_texture. A texture waiting on its pages' in-place verdict may move in place. A texture over a heap's buffer is made resident by use_heap through a path that checks the shadow. RUNTIME_VERSION moves because the run's announcements change. The shadowed-texture notice and the shadow-log line come at the making, and a texture only ever bound through its rows gives neither. A resource-ID read of an unmade texture gets the neutral. No new Degradation kind. No FAST-PATHS row: the copy work follows uses, not the shadow's existence (an unbound shadow was copied 0 times per commit before and after), so no accuracy is traded. The saving is the making: the VkImage, its memory, views and heap-set slots. Test: tests::texel_rows_device::a_shadow_is_made_at_the_first_reach_of_its_image, on a copied buffer, since over the pages lavapipe lays the rows in place. Three cases: - a written-only texture: shadows_left_unmade +1, none made, shadowed_textures 0, shadow_copies 0, texels inverted in the pages; - a rows write then a sample in one command buffer: made at the sample, copies > 0, pixels equal to a device-own texture's; - use_resource: made at the call. joined_residency's helper asserts shadows_bytes() where it asserted the shadow at new_texture. Verified, msi m3-max-30024 at 088a73d3b7: - clippy -D warnings, runtime crate, all targets and features: plain and --cfg m2v_cpu_icd exit 0; - lavapipe 206/206: texel_rows, shadow*, read_twin*, joined_residency, padding/kept_padding, exported_*, first_host_texture, surface_shapes, attached_read*, copied_device, fresh_pages, texture::*; - translator api_signatures, api_surface and fast_paths: 47/47; - the negative control in the same job (lazy = false): the test fails at "Copy: left unmade", (0,0,1) against (1,0,0).
604 lines
36 KiB
Rust
604 lines
36 KiB
Rust
//! Interop with the caller's own Vulkan work: the raw handles of the runtime's objects, and what
|
|
//! the device keeps for the caller's images and semaphores.
|
|
//!
|
|
//! - **Raw handles.** Unsafe accessors give the Vulkan objects behind a texture
|
|
//! ([`Texture::vk_image`]), a buffer ([`Buffer::vk_buffer`]), an event ([`Event::vk_semaphore`],
|
|
//! [`SharedEvent::vk_semaphore`]), the device ([`Device::raw`]) and a queue
|
|
//! ([`CommandQueue::with_vk_queue`]).
|
|
//! - **The caller's submissions and an event** ([`Event::vk_wait`],
|
|
//! [`Event::reserve_vk_signal`], and a shared event's same hooks): the event's semaphore counts
|
|
//! signals, not Metal's values, so the caller asks the counter value a Metal value is, or
|
|
//! reserves the next one for a value it signals (`event_timeline.rs`). Both kinds share one
|
|
//! implementation; they differ in what a command buffer's wait for a reserved signal becomes: a
|
|
//! plain event's (`MTLEvent`, whose value no host call sets) chains on its `VkQueue`, a shared
|
|
//! event's is held on the host until the signal landed, as its every wait the host may satisfy
|
|
//! is. The runtime keeps owning them: the caller never destroys one, and orders its own use of
|
|
//! one against the runtime's through semaphores or completion.
|
|
//! A queue's `VkQueue` is lent under the runtime's lock of it, which every submission takes,
|
|
//! since Vulkan requires a queue's submissions to be externally synchronized.
|
|
//! - **The caller's images** (`texture/wrapped.rs`): textures over images the caller made, which
|
|
//! the device's batches move between the caller's layout and `GENERAL`.
|
|
//! - **The caller's buffers** (`wrapped_buffer.rs`): buffers over `VkBuffer`s the caller made and
|
|
//! bound to its own memory (an imported dma-buf, for example), `Shared` at a host address the
|
|
//! caller gives, or `Private`.
|
|
//! - **Shared events whose semaphore another API imports** (`exported_semaphore.rs`,
|
|
//! [`Device::new_exportable_shared_event`](super::Device::new_exportable_shared_event)): the
|
|
//! event's timeline semaphore exported as an opaque file descriptor, its counter the signal
|
|
//! index the hooks above name.
|
|
//! - **Buffers whose memory another API imports** (`exported_memory.rs`,
|
|
//! [`Device::new_exportable_buffer`](super::Device::new_exportable_buffer)): an allocation of
|
|
//! its own, exported as an opaque file descriptor; the buffer outlives every import.
|
|
//! - **The caller's binary semaphores** (`CommandBuffer::encode_wait_for_vk_semaphore` and
|
|
//! `encode_signal_vk_semaphore`): Vulkan has no query for a semaphore's type, so the runtime
|
|
//! refuses what it can tell is no binary semaphore of the device: a null handle, and its own
|
|
//! timeline semaphores (events' and queues'). Every other contract is the caller's.
|
|
|
|
use super::buffer::ReadApart;
|
|
use super::device::relay::By;
|
|
use super::device::submit_stalls::Uses;
|
|
use super::error::Error;
|
|
use super::event::{CallerSignals, EventRef};
|
|
use super::event_timeline::{Signal, WaitFor};
|
|
use super::texture::wrapped::Wrapped;
|
|
use super::{Buffer, CommandQueue, Device, Event, SharedEvent, Texture};
|
|
use ash::vk;
|
|
use std::collections::HashSet;
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
/// No Metal counterpart: how a submission of the caller's waits for an event's value
|
|
/// ([`Event::vk_wait`], [`SharedEvent::vk_wait`]).
|
|
#[non_exhaustive]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum VkEventWait {
|
|
/// The value is reached: the submission needs no wait.
|
|
Reached,
|
|
/// The submission waits for the event's timeline semaphore ([`Event::vk_semaphore`],
|
|
/// [`SharedEvent::vk_semaphore`]) to reach this counter value: the index of the signal,
|
|
/// submitted by the runtime or reserved by the caller, that reaches the value.
|
|
Counter(u64),
|
|
/// No counter value names the value yet: no signal submitted or reserved, and no host set,
|
|
/// reaches it; or the one that does is seen only at its command buffer's completion, after
|
|
/// the copied host buffers it may write took its writes (`event_timeline.rs`). A submission
|
|
/// cannot wait for it on the GPU now: ask again once the signalling command buffer is
|
|
/// scheduled ([`CommandBuffer::wait_until_scheduled`](super::CommandBuffer::wait_until_scheduled)),
|
|
/// or, for the one seen at its completion, completed; or wait on the host, for a shared
|
|
/// event ([`SharedEvent::wait_until_signaled_value`]).
|
|
NotYet,
|
|
}
|
|
|
|
/// No Metal counterpart: what a submission of the caller's signals for an event's value
|
|
/// ([`Event::reserve_vk_signal`], [`SharedEvent::reserve_vk_signal`]).
|
|
#[non_exhaustive]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum VkEventSignal {
|
|
/// The value is not above what the event's signals, submitted or reserved, and a shared
|
|
/// event's host sets already reach: Metal's signal would change nothing (an event's value
|
|
/// only rises), so the submission signals nothing.
|
|
Dropped,
|
|
/// The submission signals the event's timeline semaphore ([`Event::vk_semaphore`],
|
|
/// [`SharedEvent::vk_semaphore`]) to `counter`, once the semaphore reached `after` where
|
|
/// given.
|
|
Counter {
|
|
/// The counter value the submission signals: the next index, reserved for the caller.
|
|
counter: u64,
|
|
/// The counter value the semaphore must reach before the signal: the signal reserved or
|
|
/// submitted before it, on whatever queue, so the semaphore's signals run in order.
|
|
after: Option<u64>,
|
|
},
|
|
}
|
|
|
|
/// Why a semaphore cannot be one of the caller's binary semaphores.
|
|
#[non_exhaustive]
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum SemaphoreProblem {
|
|
/// A null handle.
|
|
Null,
|
|
/// One of the runtime's own timeline semaphores: an event's ([`Event::vk_semaphore`],
|
|
/// [`SharedEvent::vk_semaphore`]) or a queue's.
|
|
Timeline,
|
|
}
|
|
|
|
/// What a device keeps for interop: the caller's images whose batches move them between layouts,
|
|
/// and the timeline semaphores its events hold.
|
|
#[derive(Default)]
|
|
pub(super) struct Interop {
|
|
pub(super) wrapped: Wrapped,
|
|
timelines: Mutex<HashSet<vk::Semaphore>>,
|
|
/// The caller's reserved signals of the device's events, which its watcher waits for
|
|
/// (`event.rs`).
|
|
pub(super) caller_signals: Arc<CallerSignals>,
|
|
}
|
|
|
|
impl Interop {
|
|
/// An event's timeline semaphore was made.
|
|
pub(super) fn timeline_made(&self, semaphore: vk::Semaphore) {
|
|
self.timelines().insert(semaphore);
|
|
}
|
|
|
|
/// An event's timeline semaphore is being destroyed.
|
|
pub(super) fn timeline_gone(&self, semaphore: vk::Semaphore) {
|
|
self.timelines().remove(&semaphore);
|
|
}
|
|
|
|
fn timelines(&self) -> std::sync::MutexGuard<'_, HashSet<vk::Semaphore>> {
|
|
self.timelines.lock().unwrap_or_else(|p| p.into_inner())
|
|
}
|
|
}
|
|
|
|
/// What the runtime can tell of `semaphore` as the caller's binary semaphore on a device whose
|
|
/// own timeline semaphores are `queues` (its queues') and `events` (its events').
|
|
pub(super) fn semaphore_problem(
|
|
semaphore: vk::Semaphore,
|
|
queues: impl IntoIterator<Item = vk::Semaphore>,
|
|
events: &HashSet<vk::Semaphore>,
|
|
) -> Option<SemaphoreProblem> {
|
|
if semaphore == vk::Semaphore::null() {
|
|
Some(SemaphoreProblem::Null)
|
|
} else if events.contains(&semaphore) || queues.into_iter().any(|queue| queue == semaphore) {
|
|
Some(SemaphoreProblem::Timeline)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
impl Device {
|
|
/// Why `semaphore` cannot be the caller's binary semaphore of this device, if the runtime can
|
|
/// tell.
|
|
pub(super) fn binary_semaphore_problem(
|
|
&self,
|
|
semaphore: vk::Semaphore,
|
|
) -> Option<SemaphoreProblem> {
|
|
let shared = self.shared();
|
|
semaphore_problem(
|
|
semaphore,
|
|
shared.queues.iter().map(|queue| queue.timeline),
|
|
&shared.interop.timelines(),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Texture {
|
|
/// No Metal counterpart (an interop hook): the texture's `VkImage`, the image of the texture
|
|
/// and of every view of it, made by the runtime or the caller's
|
|
/// ([`Device::new_texture_with_vk_image`](super::Device::new_texture_with_vk_image)). `None`
|
|
/// where the texels are a buffer's bytes the runtime copies into and out of an image of its
|
|
/// own (a texture buffer, a texture over a buffer whose image does not alias it, or a shadow
|
|
/// over a no-copy buffer's host pages, `shadowed-texture`), or another image's texels it so
|
|
/// copies (a view of another component layout on a device whose views reinterpret none).
|
|
///
|
|
/// A texture over a buffer's bytes whose image aliases them gives a linear image of
|
|
/// `VK_IMAGE_TILING_LINEAR`, bound to the buffer's memory, the runtime's own: such a texture's
|
|
/// rows lie one after another and its image's subresource holds no byte but its texels. A
|
|
/// texture over a no-copy buffer's host pages in place (where the device's in-place check
|
|
/// passed for the pages and the texture's accesses, under the default
|
|
/// `HostPageTexturePolicy::InPlace`) gives its image in place, bound where the texels lie: over
|
|
/// pages imported by their pointer, the driver's linear image where it lays the rows out at the
|
|
/// texture's pitch, else an image of `VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT` with
|
|
/// `DRM_FORMAT_MOD_LINEAR` at those pitches; over pages imported as dma-bufs, the latter. The
|
|
/// bytes between rows that lie apart are in its subresource. Over memory the runtime exports
|
|
/// (`MemoryExport`), either image names the memory's handle type
|
|
/// (`VkExternalMemoryImageCreateInfo`). A shadow over host pages (or over the device's pooled
|
|
/// memory at Metal's pitch, or over its exported memory) made before the device's check
|
|
/// answered gives none until it moves in place, at a commit once the check passed for it
|
|
/// (`texture/storage_form.rs`). A
|
|
/// texture placed in a heap over host pages imported as dma-bufs (`HostMemory::with_dma_buf`)
|
|
/// gives an image of `VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT` with `DRM_FORMAT_MOD_LINEAR`:
|
|
/// ask its layout of its memory plane (`VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT`, not the color
|
|
/// aspect), and its modifier with `vkGetImageDrmFormatModifierPropertiesEXT`.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// The image stays its owner's: the caller does not destroy one the runtime made or bind its
|
|
/// memory, and uses it only while the texture lives, ordered against the runtime's work on
|
|
/// it. An image the runtime made rests in `VK_IMAGE_LAYOUT_GENERAL`, owned by the device's
|
|
/// queue family, whenever no batch of the runtime uses it, and the caller leaves it so; a
|
|
/// wrapped image follows its layout contract
|
|
/// ([`VkImageLayouts`](super::VkImageLayouts)).
|
|
///
|
|
/// The texture keeps the image for good: a fetched render target whose image takes
|
|
/// `STORAGE` later no longer does once its image was handed out. Once its device's recovery
|
|
/// carried the texture to a new `VkDevice` (`Degradation::DeviceRecovered`), an image answered
|
|
/// before stays the lost device's, kept with that device until the texture goes, and a later
|
|
/// call answers the new one's.
|
|
pub unsafe fn vk_image(&self) -> Option<vk::Image> {
|
|
let _held = self.hold();
|
|
if self.shadows_bytes() {
|
|
return None;
|
|
}
|
|
self.exported_image().ok()
|
|
}
|
|
}
|
|
|
|
impl Buffer {
|
|
/// The buffer's `VkBuffer`, and where the buffer's byte 0 lies in it: 0, but for a no-copy
|
|
/// buffer bound below an unaligned pointer. No Metal counterpart: an interop hook. A buffer the
|
|
/// runtime placed in its own memory has no `SHADER_DEVICE_ADDRESS` usage of its own (it takes
|
|
/// its address from its memory block's buffer), so the caller asks it for no device address.
|
|
/// Once it was handed out, a shadowed texture over a `Private` buffer's bytes copies them in
|
|
/// at each command buffer's first use, as one over the CPU's bytes does: the caller's own
|
|
/// work may write them. Handing it out first makes the work that still waits on its bytes
|
|
/// (a new `Private` buffer's zeros or bytes, a `Managed` buffer's upload, which the device's
|
|
/// next submission would make before its commands), that work alone, by a submission waited
|
|
/// for, as [`Texture::vk_image`](crate::Texture::vk_image) makes its texture's: so the
|
|
/// caller's work on the handle comes after it. Where the device reserved a queue for its
|
|
/// staging copies (a copy engine, or a queue of compute work beside its own), the
|
|
/// submission goes there and waits for no work of the program's;
|
|
/// elsewhere it goes to the device's first `VkQueue`, behind what was submitted there before.
|
|
/// Nothing is submitted where no such work waits. Where that submission fails (the device
|
|
/// lost, or out of memory), the work waits for the next. Once its device's recovery carried
|
|
/// the buffer to a new `VkDevice` (`Degradation::DeviceRecovered`), a handle answered before
|
|
/// stays the lost device's, kept with that device until the buffer goes, and a later call
|
|
/// answers the new one's.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// The `VkBuffer` stays the runtime's: the caller does not destroy it, and uses it only while
|
|
/// the buffer lives, ordered against the runtime's work on it.
|
|
pub unsafe fn vk_buffer(&self) -> (vk::Buffer, u64) {
|
|
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
|
|
let _held = self.held_parts();
|
|
self.hand_out();
|
|
let handle = self.at_for(0, ReadApart::VkBuffer);
|
|
// A failed submission gives the work back, to the device's next submission; the caller
|
|
// sees the failure at its next call (a lost device refuses it).
|
|
let _ = self.settle_first_work(handle.0);
|
|
handle
|
|
}
|
|
|
|
/// No Metal counterpart (an interop hook): where the buffer's byte 0 lies in Vulkan memory,
|
|
/// where its bytes are the caller's host pages in place, in one `VkDeviceMemory` the device
|
|
/// imported by their pointer (`VK_EXT_external_memory_host`): a no-copy buffer's
|
|
/// (`newBufferWithBytesNoCopy:length:options:deallocator:`), or a buffer's in a heap over the
|
|
/// caller's host memory, whose pages the device imported by their pointer
|
|
/// ([`HostBacking::ImportedByPointer`](crate::HostBacking::ImportedByPointer)) or bound in its
|
|
/// import of a guest RAM chunk ([`HostBacking::InHostRam`](crate::HostBacking::InHostRam)). A
|
|
/// `VkBuffer` of the caller's own bound there uses the bytes the runtime's commands use, with
|
|
/// a usage the runtime's buffers lack (a video decode's bitstream source,
|
|
/// `VK_BUFFER_USAGE_VIDEO_DECODE_SRC_BIT_KHR`); Vulkan asks it to name the answer's
|
|
/// [`handle_type`](VkHostMemory::handle_type) in its `VkExternalMemoryBufferCreateInfo`. The
|
|
/// import is never a dedicated allocation, so any number of buffers bind to it.
|
|
///
|
|
/// `None` for every other buffer: one in the device's own memory or the caller's `VkBuffer`;
|
|
/// a copied host buffer ([`HostBacking::Copied`](crate::HostBacking::Copied)), whose memory is
|
|
/// a copy the runtime brings in step with the pages only at its own commits and completions;
|
|
/// pages imported as dma-bufs (several allocations, sparsely bound); pages of Metal's private
|
|
/// option bit 24, whose import is of a read-only view that drops the GPU's writes (on macOS;
|
|
/// elsewhere they are a copied host buffer); a detached
|
|
/// buffer; and an import the device released while idle and could not make again (the device
|
|
/// is then lost). As [`vk_buffer`](Self::vk_buffer) does, the call keeps an import the device
|
|
/// would release while idle imported for good.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// The memory stays the runtime's: the caller never frees or maps it, and the buffers it
|
|
/// binds to it are destroyed, their submissions completed, before the answer ends. The
|
|
/// answer holds while the buffer lives and lies in the same pages: until the buffer's (for a
|
|
/// heap's buffer, its heap's) [`rebind_bytes_no_copy`](Self::rebind_bytes_no_copy) or
|
|
/// [`detach_backing`](Self::detach_backing). The caller orders its work on the bytes against
|
|
/// the runtime's (an event's semaphore: [`Event::vk_wait`], [`Event::reserve_vk_signal`],
|
|
/// [`SharedEvent::vk_wait`], [`SharedEvent::reserve_vk_signal`]) and against the host's
|
|
/// writes to the pages, as for any use of the buffer.
|
|
pub unsafe fn vk_host_memory(&self) -> Option<VkHostMemory> {
|
|
// Its parts held while the call reaches them, across a recovery (`Buffer::held_parts`).
|
|
let _held = self.held_parts();
|
|
self.host_memory_at(0)
|
|
}
|
|
}
|
|
|
|
/// No Metal counterpart: where a buffer's bytes lie in the device's import of the caller's host
|
|
/// pages ([`Buffer::vk_host_memory`]).
|
|
#[non_exhaustive]
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
pub struct VkHostMemory {
|
|
/// The import: memory made with `VkImportMemoryHostPointerInfoEXT` over the pages, or over a
|
|
/// guest RAM chunk the pages lie in.
|
|
pub memory: vk::DeviceMemory,
|
|
/// Where the buffer's byte 0 lies in `memory`.
|
|
pub offset: u64,
|
|
/// The bytes of `memory` from `offset` that are the imported pages': the buffer's length, and
|
|
/// the rest of the import past it.
|
|
pub size: u64,
|
|
/// The memory's type (`VkMemoryAllocateInfo::memoryTypeIndex`), which a buffer bound to it
|
|
/// takes (`VkMemoryRequirements::memoryTypeBits`).
|
|
pub type_index: u32,
|
|
/// The handle type the memory was imported as, which a `VkBuffer` bound to it names in its
|
|
/// `VkExternalMemoryBufferCreateInfo::handleTypes`:
|
|
/// `VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT`.
|
|
pub handle_type: vk::ExternalMemoryHandleTypeFlags,
|
|
}
|
|
|
|
impl std::fmt::Debug for VkHostMemory {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
// `ash`'s flags print only with its `debug` feature, which the runtime leaves off.
|
|
f.debug_struct("VkHostMemory")
|
|
.field("memory", &self.memory)
|
|
.field("offset", &self.offset)
|
|
.field("size", &self.size)
|
|
.field("type_index", &self.type_index)
|
|
.field(
|
|
"handle_type",
|
|
&format_args!("{:#x}", self.handle_type.as_raw()),
|
|
)
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
/// Either kind's timeline semaphore ([`Event::vk_semaphore`], [`SharedEvent::vk_semaphore`]).
|
|
fn semaphore(event: EventRef<'_>) -> vk::Semaphore {
|
|
loop {
|
|
// An event carried since this read it answers where it was carried (`EventRef::shared`).
|
|
if let Some(semaphore) = event.shared().hand_out_semaphore() {
|
|
return semaphore;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Either kind's answer of how the caller's submission waits for `value` ([`Event::vk_wait`],
|
|
/// [`SharedEvent::vk_wait`]): the host's answer (`EventTimeline::landing`), the index of the
|
|
/// signal that reaches the value whether or not the runtime's own waits for it are held on the
|
|
/// host, which for a plain event is its command buffers' own wait.
|
|
fn vk_wait(event: EventRef<'_>, value: u64) -> VkEventWait {
|
|
match event.shared().landing(value) {
|
|
Some(WaitFor::Reached) => VkEventWait::Reached,
|
|
Some(WaitFor::Index(index)) => VkEventWait::Counter(index),
|
|
None => VkEventWait::NotYet,
|
|
}
|
|
}
|
|
|
|
/// Either kind's reservation of a signal of `value` for the caller's submission
|
|
/// ([`Event::reserve_vk_signal`], [`SharedEvent::reserve_vk_signal`]): the timeline keeps it as
|
|
/// the event's next signal whatever its kind (`EventTimeline::signal_apart`); what a command
|
|
/// buffer's wait for it becomes is the timeline's answer for the kind (`EventTimeline::wait`).
|
|
fn reserve_vk_signal(event: EventRef<'_>, value: u64) -> Result<VkEventSignal, Error> {
|
|
Ok(match event.shared().reserve_signal(value)? {
|
|
Signal::Dropped => VkEventSignal::Dropped,
|
|
Signal::Kept { index, after } => VkEventSignal::Counter {
|
|
counter: index,
|
|
after,
|
|
},
|
|
})
|
|
}
|
|
|
|
impl Event {
|
|
/// No Metal counterpart (an interop hook; Metal's `MTLEvent` is backed by no Vulkan object):
|
|
/// the event's timeline semaphore, which the caller's submissions wait for and signal to
|
|
/// order their work against the runtime's command buffers' `encodeWaitForEvent:` and
|
|
/// `encodeSignalEvent:` of the event. Refuses nothing.
|
|
///
|
|
/// **Its counter is a signal index, not the event's value.** Metal's value only rises: a
|
|
/// signal not above it changes nothing, and two signals land as their maximum. A Vulkan
|
|
/// timeline semaphore takes only rising signals, so each signal the runtime keeps, its
|
|
/// batches' and the caller's, raises the counter by one, and the runtime maps each index to
|
|
/// its Metal value (`event_timeline.rs`). A submission waits for the counter value
|
|
/// [`vk_wait`](Self::vk_wait) answers for a Metal value, and signals only the one
|
|
/// [`reserve_vk_signal`](Self::reserve_vk_signal) reserved.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// The semaphore stays the runtime's (once its device's recovery carried the event to a new
|
|
/// `VkDevice`, one answered before stays the lost device's until the event goes, and a later
|
|
/// call answers the new one's): the caller never destroys it, never uses it as a
|
|
/// binary semaphore, signals it only as a reservation names (never from the host but as
|
|
/// that call says), and uses it only while the event lives: it keeps the event (a clone of
|
|
/// it) until every submission of its own that waits for or signals the semaphore completed.
|
|
pub unsafe fn vk_semaphore(&self) -> vk::Semaphore {
|
|
semaphore(self.into())
|
|
}
|
|
|
|
/// No Metal counterpart (an interop hook): how a submission of the caller's waits for the
|
|
/// event's value to reach `value`, as a command buffer's `encodeWaitForEvent:value:` waits:
|
|
/// no wait where the value is reached, a wait for the event's timeline semaphore
|
|
/// ([`vk_semaphore`](Self::vk_semaphore)) to reach the counter value the answer names, or
|
|
/// none yet ([`VkEventWait::NotYet`]). Refuses nothing.
|
|
///
|
|
/// No host call sets a plain event's value, so the GPU wait for the counter value is Metal's
|
|
/// wait exactly: the signal it names is the one that reaches `value`, as for the runtime's
|
|
/// own command buffers, whose waits for the event chain on their `VkQueue`s the same way. A
|
|
/// counter value the semaphore already reached is a wait that ends at once.
|
|
pub fn vk_wait(&self, value: u64) -> VkEventWait {
|
|
vk_wait(self.into(), value)
|
|
}
|
|
|
|
/// No Metal counterpart (an interop hook): reserve a signal of `value` for a submission of
|
|
/// the caller's, as a command buffer's `encodeSignalEvent:value:` signals. Where `value` is
|
|
/// above everything the event's signals reach, the answer names the next counter value of the
|
|
/// event's timeline semaphore ([`vk_semaphore`](Self::vk_semaphore)), which the caller's
|
|
/// submission signals, after a wait for the signal before it (`after`, on whatever queue that
|
|
/// one runs); else the signal is dropped, as Metal's maximum makes it a no-op.
|
|
///
|
|
/// From the reservation, the runtime counts that signal among the event's: a command
|
|
/// buffer's wait for `value` (or less) goes to its `VkQueue` as a wait for `counter`, whether
|
|
/// or not the caller's submission was made yet (a wait before its signal, which Vulkan's
|
|
/// timeline semaphores take), as a wait for a command buffer's signal of the event does; a
|
|
/// later command buffer's signal of the event waits for it to land; and the device's watcher
|
|
/// waits for it on the host and, once it landed, observes it, as for a command buffer's
|
|
/// signal. No host call sets a plain event's value, so none of these waits is held on the
|
|
/// host, unlike a shared event's ([`SharedEvent::reserve_vk_signal`]). The signal is ordered
|
|
/// after the work the caller's submission orders it after, as Vulkan orders a semaphore
|
|
/// signal; work the runtime submitted is no part of it.
|
|
///
|
|
/// Refused where the device is lost (`DeviceLost`), and where the watcher's thread cannot
|
|
/// start (`OutOfMemory(Host)`): nothing is reserved then.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// For a [`VkEventSignal::Counter`] answer, the caller submits exactly one signal operation of
|
|
/// the semaphore to `counter`, in a submission that first waits for the semaphore to reach
|
|
/// `after` where given, to a queue of the event's device, soon: every later signal of the
|
|
/// event and every wait for a value it reaches waits for it, the runtime's command buffers'
|
|
/// on their queues included, and a reservation never signalled stalls them for good. So
|
|
/// does a texture's `get_bytes` or `replace_region` behind such a wait on a device that
|
|
/// copies textures through a submission of its own on its first `VkQueue` (one with no
|
|
/// `VK_EXT_host_image_copy` and no staging queue, a depth texture's on one with no host
|
|
/// copies): a thread submits the signal before it makes such a call. Those
|
|
/// command buffers may be submitted before the signal is, so the caller's submission depends
|
|
/// on none of them: it waits for no such command buffer's completion, and goes to a queue no
|
|
/// command queue submits to (an extra or reserved queue,
|
|
/// [`DeviceHandles::extra_queues`](crate::DeviceHandles::extra_queues),
|
|
/// [`DeviceHandles::reserved_queues`](crate::DeviceHandles::reserved_queues)), or to a
|
|
/// command queue's `VkQueue` ([`CommandQueue::with_vk_queue`]) behind none of them. Where the
|
|
/// submission fails, the caller signals the semaphore to `counter` from the host
|
|
/// (`vkSignalSemaphore`) once the semaphore reached `after`, so the rest goes on: a signal of
|
|
/// the semaphore itself, which releases the waits on the `VkQueue`s as the submission's
|
|
/// would, not a Metal host set, which a plain event has none of. The semaphore's other
|
|
/// contract is [`vk_semaphore`](Self::vk_semaphore)'s; the runtime holds the event until the
|
|
/// signal landed.
|
|
///
|
|
/// A command buffer's completion sync file
|
|
/// ([`CommandBuffer::export_completion_sync_file`](crate::CommandBuffer::export_completion_sync_file))
|
|
/// is exported only after the caller submitted every such signal its work may wait for: the
|
|
/// signal of a value it, or a command buffer committed before it on its `VkQueue`, waits for.
|
|
/// Vulkan exports a sync file only once every signal operation its signal depends on was
|
|
/// submitted (`VUID-VkSemaphoreGetFdInfoKHR-handleType-03254`), and these waits go to the
|
|
/// `VkQueue` before their signal.
|
|
pub unsafe fn reserve_vk_signal(&self, value: u64) -> Result<VkEventSignal, Error> {
|
|
reserve_vk_signal(self.into(), value)
|
|
}
|
|
}
|
|
|
|
impl SharedEvent {
|
|
/// No Metal counterpart (an interop hook; Metal's `MTLSharedEvent` is backed by no Vulkan
|
|
/// object): the event's timeline semaphore, which the caller's submissions wait for and
|
|
/// signal to order their work against the runtime's command buffers' `encodeWaitForEvent:`
|
|
/// and `encodeSignalEvent:` of the event.
|
|
///
|
|
/// **Its counter is a signal index, not the event's value.** Metal's value only rises: a
|
|
/// signal not above it changes nothing, two signals land as their maximum, and a host set
|
|
/// (`setSignaledValue:`) takes effect at once. A Vulkan timeline semaphore takes only rising
|
|
/// signals, and no host signal while a GPU signal is pending
|
|
/// (`VUID-VkSemaphoreSignalInfo-value-03258`). So each signal the runtime keeps, its
|
|
/// batches' and the caller's, raises the counter by one, and the runtime maps each index to
|
|
/// its Metal value; a host set never touches the semaphore (`event_timeline.rs`). A
|
|
/// submission waits for the counter value [`vk_wait`](Self::vk_wait) answers for a Metal
|
|
/// value, and signals only the one [`reserve_vk_signal`](Self::reserve_vk_signal) reserved.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// The semaphore stays the runtime's (once its device's recovery carried the event to a new
|
|
/// `VkDevice`, one answered before stays the lost device's until the event goes, and a later
|
|
/// call answers the new one's): the caller never destroys it, never uses it as a
|
|
/// binary semaphore, signals it only as a reservation names (never from the host but as
|
|
/// that call says), and uses it only while the event lives: it keeps the event (a clone of
|
|
/// it) until every submission of its own that waits for or signals the semaphore completed.
|
|
pub unsafe fn vk_semaphore(&self) -> vk::Semaphore {
|
|
semaphore(self.into())
|
|
}
|
|
|
|
/// No Metal counterpart (an interop hook): how a submission of the caller's waits for the
|
|
/// event's value to reach `value`, as a command buffer's `encodeWaitForEvent:value:` waits:
|
|
/// no wait where the value is reached, a wait for the event's timeline semaphore
|
|
/// ([`vk_semaphore`](Self::vk_semaphore)) to reach the counter value the answer names, or
|
|
/// none yet ([`VkEventWait::NotYet`]).
|
|
///
|
|
/// Unlike Metal's wait, a GPU wait for a counter value is released only by the signal it
|
|
/// names, not by a host set of the value meanwhile (`setSignaledValue:`), which Metal's wait
|
|
/// would take at once: the signal comes, so the wait ends, later at most. A counter value the
|
|
/// semaphore already reached is a wait that ends at once.
|
|
pub fn vk_wait(&self, value: u64) -> VkEventWait {
|
|
vk_wait(self.into(), value)
|
|
}
|
|
|
|
/// No Metal counterpart (an interop hook): reserve a signal of `value` for a submission of
|
|
/// the caller's, as a command buffer's `encodeSignalEvent:value:` signals. Where `value` is
|
|
/// above everything the event's signals and host sets reach, the answer names the next
|
|
/// counter value of the event's timeline semaphore ([`vk_semaphore`](Self::vk_semaphore)),
|
|
/// which the caller's submission signals, after a wait for the signal before it (`after`,
|
|
/// on whatever queue that one runs); else the signal is dropped, as Metal's maximum makes it
|
|
/// a no-op.
|
|
///
|
|
/// From the reservation, the runtime counts that signal among the event's: a command
|
|
/// buffer's wait for `value` (or less) waits for it, a later command buffer's signal of the
|
|
/// event waits for it to land, and the device's watcher waits for it on the host and, once
|
|
/// it landed, raises the event's value, wakes its host waits and posts its listeners'
|
|
/// notifications, as for a command buffer's signal. The signal is ordered after the work the
|
|
/// caller's submission orders it after, as Vulkan orders a semaphore signal; work the runtime
|
|
/// submitted is no part of it.
|
|
///
|
|
/// Refused where the device is lost (`DeviceLost`), and where the watcher's thread cannot
|
|
/// start (`OutOfMemory(Host)`): nothing is reserved then.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// For a [`VkEventSignal::Counter`] answer, the caller submits exactly one signal operation of
|
|
/// the semaphore to `counter`, in a submission that first waits for the semaphore to reach
|
|
/// `after` where given, to a queue of the event's device (or of an API that imported the
|
|
/// semaphore of an event made exportable, [`export_semaphore_fd`](Self::export_semaphore_fd)),
|
|
/// soon: every later signal of the
|
|
/// event and every wait for a value it reaches waits for it, the runtime's command buffers'
|
|
/// on their queues included, and a reservation never signalled stalls them for good. A
|
|
/// command buffer's wait for such a value is held on the host, never on a `VkQueue`, but a
|
|
/// command buffer that signals the event after the reservation waits for the reserved signal
|
|
/// on its `VkQueue` before its own signal. So does a texture's `get_bytes` or
|
|
/// `replace_region` behind such a command buffer on a device that copies textures through a
|
|
/// submission of its own on its first `VkQueue` (one with no `VK_EXT_host_image_copy` and no
|
|
/// staging queue, a depth texture's on one with no host copies): a thread submits the signal
|
|
/// before it makes such a call. Those command buffers may be submitted before the signal is,
|
|
/// so the caller's submission depends on none of them: it waits for no such command buffer's
|
|
/// completion, and goes to a queue no command queue submits to (an extra or reserved queue,
|
|
/// [`DeviceHandles::extra_queues`](crate::DeviceHandles::extra_queues),
|
|
/// [`DeviceHandles::reserved_queues`](crate::DeviceHandles::reserved_queues)), or to a
|
|
/// command queue's `VkQueue` ([`CommandQueue::with_vk_queue`]) behind none of them. Where
|
|
/// the submission fails, the caller signals the semaphore to `counter` from the host
|
|
/// (`vkSignalSemaphore`) once the semaphore reached `after`, so the rest goes on. The
|
|
/// semaphore's other contract is [`vk_semaphore`](Self::vk_semaphore)'s; the runtime holds
|
|
/// the event until the signal landed.
|
|
pub unsafe fn reserve_vk_signal(&self, value: u64) -> Result<VkEventSignal, Error> {
|
|
reserve_vk_signal(self.into(), value)
|
|
}
|
|
}
|
|
|
|
impl CommandQueue {
|
|
/// `f` with the `VkDevice` this queue's `VkQueue` belongs to (with its device-level commands),
|
|
/// that `VkQueue` and its family's index, under the runtime's lock of that `VkQueue`, which
|
|
/// every submission to it takes: the caller's own submissions and presents
|
|
/// (`vkQueuePresentKHR`) go there, externally synchronized as Vulkan requires.
|
|
/// From the first call on the queue keeps that `VkQueue` for its life: where the device
|
|
/// shares `VkQueue`s among its command queues (`QueueSharingPolicy::Adaptive`), it moves no
|
|
/// more. After a recovery (`Degradation::DeviceRecovered`) the queue lends the new
|
|
/// `VkDevice`'s `VkQueue`, and `f` gets the new `VkDevice` with it, read under the same lock:
|
|
/// a caller whose objects (a command pool, a fence) were made on another `VkDevice` than the
|
|
/// one `f` gets makes them again on it before it submits there. The handles
|
|
/// [`Device::raw`](crate::Device::raw) answered before the recovery name the lost one until a
|
|
/// later `raw` call answers the new one's.
|
|
/// On MoltenVK, where the runtime relays the driver's command-buffer failures, it submits no
|
|
/// work with a fence of its own after `f` under the same lock, so a failure of the caller's
|
|
/// submissions stays the caller's. There the runtime's frees of its memory wait for the work
|
|
/// in flight (`RuntimeDriverDefect::FreedAddressMemoryFailsPendingWork`), the caller's
|
|
/// submissions in `f` among it: that submission of no work also signals a value the use took
|
|
/// before `f` ran, and memory that goes while it is pending is freed once it completed.
|
|
///
|
|
/// # Safety
|
|
///
|
|
/// The caller does not keep the handles past `f`, and `f` makes no call into the runtime and
|
|
/// waits for nothing that needs a later submission of the runtime's (the scheduler submits
|
|
/// only under this lock).
|
|
pub unsafe fn with_vk_queue<R>(&self, f: impl FnOnce(&ash::Device, vk::Queue, u32) -> R) -> R {
|
|
// The queue's place on the generation that serves, which keeps its `VkQueue`
|
|
// (`queue.rs`); a recovery that retires it has the queue take a new place, of its own.
|
|
let place = self.serving();
|
|
let device = place.device();
|
|
let shared = device.shared();
|
|
// The caller's own submissions are GPU work of the program's: textures' host shadows are
|
|
// read against the device's count of it (`texture/host_shadow.rs`).
|
|
shared.program_gave_gpu_work();
|
|
// The copies queued for the staging queue are made first, waited for: the caller's
|
|
// submissions may read what they wrote (`texture/staging_copy.rs`). A failure there is a
|
|
// lost device, which the caller's own submissions then meet.
|
|
let _ = device.flush_staging_waited();
|
|
// The queue keeps this `VkQueue` for good from here on, so the caller's submissions there
|
|
// stay in its order (`queue_share.rs`).
|
|
let vk_queue = place.pinned_vk_queue();
|
|
let family = shared.queue_family_of(vk_queue);
|
|
// Where the queue's failures are relayed, a mark after `f`'s submissions keeps a failure
|
|
// of theirs the caller's (`device/relay.rs`); where freed memory waits for the work in
|
|
// flight, it waits for that mark (`freed_memory.rs`).
|
|
let used = shared.use_queue(vk_queue, By::Program, Uses::Unknown, |queue| {
|
|
f(shared.vk(), queue, family)
|
|
});
|
|
if let Some(parking) = shared.parking.get() {
|
|
parking.watch(device);
|
|
}
|
|
used
|
|
}
|
|
}
|