Runtime: a Direct3D 11 plane's first acquire names the undefined layout unless an access was reported

A plane's image is made undefined (VUID-VkImageCreateInfo-pNext-01443) and Vulkan takes it as
general only "when they are accessed in Vulkan after external access" (section 12.5.2). Every
acquire named the general layout as its old layout, so where rt used a plane before any
Direct3D 11 access (the consumer's transfer fill of a new target, or a guest command buffer that
writes the plane first), the acquire's old layout was neither the image's current layout nor
undefined, past section 12.5.1 ("the old layout value must either equal the current layout of the
image subresource (at the time the transition executes), or else be VK_IMAGE_LAYOUT_UNDEFINED").

Now a plane's first acquire recorded, in recording order on the submit thread, names the
undefined layout, unless the surface's external_writes_done was called before it; every later
acquire, and every release, names the general layout, as before. The plane's existing once-only
move-home flag (Image::homed) carries it: a Direct3D 11 plane, which no upload moves, is now made
with the flag clear and `first` undefined, its first acquire sets it as it moves the plane from
undefined, and external_writes_done sets it on the host for each plane of a Direct3D 11 surface,
with no GPU work. homing() never moves such a plane, so an upload that copies into one leaves its
move to the acquire ahead of it. VUID-VkImageMemoryBarrier-srcQueueFamilyIndex-12332 allows either
old layout, so the barrier is the same one with another old layout: no wait, copy, clear or
submission is added.

The consumer's precondition now includes external_writes_done after every Direct3D 11 access,
before the commit of the command buffers that use the plane: without it, the plane's first
acquire names the undefined layout, which need not preserve the contents (section 12.5.1), so the
device may discard what Direct3D 11 wrote. The Layout, Ownership and precondition paragraphs of
import_d3d11_surface, the d3d11 module's doc, external_writes_done's doc and the runtime README
say so.

The test commit's every_device_acquires_a_direct3d_11_plane_from_undefined_only_before_any_access
states this rule; its red run is of 74c3940054.
This commit is contained in:
Anees Iqbal committed 2026-11-26 23:02:17 -08:00
1 parent 74c3940054
commit 9737a44d98
4 files changed
+129 -65

No files matched your search

+27 -17
View File
@@ -1054,11 +1054,15 @@ The import submits nothing and waits for nothing. Vulkan 1.4's section 12.5.2 sa
created with external memory handle types listed in the external image implied layouts table are
accessed outside of Vulkan, they must be in the layout specified in the table, and can be assumed
to be in that layout when they are accessed in Vulkan after external access", and the table's row
for `VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT` is `VK_IMAGE_LAYOUT_GENERAL`. Each
`vkQueueSubmit2` whose command buffers or uploads use a plane acquires it from
`VK_QUEUE_FAMILY_EXTERNAL` in a command buffer of rt's own ahead of everything in it, and releases
it to that family in another after everything in it, both in the general layout: one barrier
each, counted in `Metrics::external_acquires` and `Metrics::external_releases`. The acquire
for `VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT` is `VK_IMAGE_LAYOUT_GENERAL`. A plane's
image is made in the undefined layout, and no upload moves it. Each `vkQueueSubmit2` whose command
buffers or uploads use a plane acquires it from `VK_QUEUE_FAMILY_EXTERNAL` in a command buffer of
rt's own ahead of everything in it, and releases it to that family in another after everything in
it, both to the general layout: one barrier each, counted in `Metrics::external_acquires` and
`Metrics::external_releases`. A plane's first acquire names the undefined layout as its old layout
where the consumer reported no Direct3D 11 access of it before (`Surface::external_writes_done`),
and the general layout where it did; every later acquire, and every release, names the general
layout. The acquire
follows a global barrier from and to every stage, so an earlier submission's release on rt's one
queue happens before it (Vulkan 1.4, section 7.7.6). The call counts in
`Metrics::d3d11_plane_submissions` and the two command buffers in
@@ -1068,21 +1072,27 @@ buffer that uses a plane is seen complete only once the plane's release has run.
command buffers rt sends member by member, one `vkQueueSubmit2` a member, each signalling its own
progress value, hands each member's planes over in that member's call, so a member ahead of the
last is seen complete after its own planes' release and not before it; each such call counts in
`Metrics::d3d11_plane_parts` too. Direct3D 11 thus owns the
planes' memory between rt's submissions, and `Surface::external_writes_done()` and
`BlitCommandEncoder::release_surface_to_external(&surface)` record nothing for such a surface. So
the consumer's precondition is that Direct3D 11's work that accesses a plane has completed before
the consumer commits the command buffers that use it, and that those command buffers have
completed before Direct3D 11 accesses it again. No keyed mutex is used, by rt or on the textures:
each is made with `D3D11_RESOURCE_MISC_SHARED | D3D11_RESOURCE_MISC_SHARED_NTHANDLE`, not
`Metrics::d3d11_plane_parts` too. Direct3D 11 thus owns the planes' memory between rt's
submissions. `Surface::external_writes_done()` records nothing and makes no GPU work for such a
surface: it marks each plane accessed by Direct3D 11, on the host, so each acquire from then on
names the general layout. `BlitCommandEncoder::release_surface_to_external(&surface)` records
nothing. So the consumer's precondition is that Direct3D 11's work that accesses a plane has
completed before the consumer commits the command buffers that use it, that the consumer calls
`Surface::external_writes_done()` after each such access and before that commit, and that rt's
command buffers that use a plane have completed before Direct3D 11 accesses it again. Without the
call after Direct3D 11's first access, rt's first acquire of the plane names the undefined layout,
which does not preserve the contents (Vulkan 1.4, section 12.5.1: "implying that the contents of
the image subresource need not be preserved"), so the device may discard what Direct3D 11 wrote.
No keyed mutex is used, by rt or on the textures: each is made with
`D3D11_RESOURCE_MISC_SHARED | D3D11_RESOURCE_MISC_SHARED_NTHANDLE`, not
`D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX`, whose mutex the device that makes the texture and each
device that opens it must acquire before rendering to it; the consumer orders the two APIs' work
by its waits. A plane rt uses before any Direct3D 11 access has undefined contents, as a Metal
texture nothing wrote does. The surface has no buffer: `Surface::buffer()` is `Gap::D3d11SurfaceBuffer`, as its
planes lie in memories of their own, and `Surface::handle()` is `None`. No move goes ahead of the
planes' first use, so `Surface::wait_until_ready()` returns at once. The planes' imports are freed
as an exported surface's memory is (above); rt keeps no handle of its own, and each import holds
the Direct3D 11 texture's payload until it is freed.
texture nothing wrote does. The surface has no buffer: `Surface::buffer()` is
`Gap::D3d11SurfaceBuffer`, as its planes lie in memories of their own, and `Surface::handle()` is
`None`. No upload's move goes ahead of the planes' first use, so `Surface::wait_until_ready()`
returns at once. The planes' imports are freed as an exported surface's memory is (above); rt keeps
no handle of its own, and each import holds the Direct3D 11 texture's payload until it is freed.
### Known gaps of memory blocks
+38 -21
View File
@@ -2,11 +2,12 @@
//! host's: the planes another API decodes or draws into, shared by their NT handles, each
//! imported as an optimal image of its own and read by rt's shaders in place.
//!
//! The import makes no submission and waits for nothing: Vulkan takes such an image as already
//! in the general layout once Direct3D 11 has accessed it (section 12.5.2, below), so no move out
//! of the undefined layout comes before its first use. Each `vkQueueSubmit2` that uses a plane
//! takes it from `VK_QUEUE_FAMILY_EXTERNAL` at its start and gives it back at its end, before its
//! signals (`crate::submit::parts`).
//! The import makes no submission and waits for nothing. A plane's image is made undefined, and
//! Vulkan takes it as general once Direct3D 11 has accessed it (section 12.5.2, below), which the
//! consumer reports by `Surface::external_writes_done`: no upload moves it, and its first acquire
//! names the undefined layout where no such access was reported before it. Each `vkQueueSubmit2`
//! that uses a plane takes it from `VK_QUEUE_FAMILY_EXTERNAL` at its start and gives it back at
//! its end, before its signals (`crate::submit::parts`).
use crate::counters::Gauge;
use crate::device::{Core, Device};
@@ -107,18 +108,26 @@ impl Device {
/// ([`Texture::new_texture_view`](crate::Texture::new_texture_view)) is [`Gap::TextureView`]
/// of [`TextureViewKind::SurfacePixelFormat`](crate::TextureViewKind::SurfacePixelFormat).
///
/// **Layout.** The call makes no submission and waits for nothing: no move out of the
/// undefined layout comes before a plane's first use. Vulkan 1.4, section 12.5.2 (Image
/// Layout Rules with External Memory): "When images created with external memory handle
/// types listed in the external image implied layouts table are accessed outside of Vulkan,
/// they must be in the layout specified in the table, and can be assumed to be in that layout
/// when they are accessed in Vulkan after external access", and its Table 17 gives
/// **Layout.** The call makes no submission and waits for nothing, and no upload moves a
/// plane. Each plane's image is made in the undefined layout
/// (`VUID-VkImageCreateInfo-pNext-01443`). Vulkan 1.4, section 12.5.2 (Image Layout Rules
/// with External Memory): "When images created with external memory handle types listed in
/// the external image implied layouts table are accessed outside of Vulkan, they must be in
/// the layout specified in the table, and can be assumed to be in that layout when they are
/// accessed in Vulkan after external access", and its Table 17 gives
/// `VK_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_TEXTURE_BIT` the layout `VK_IMAGE_LAYOUT_GENERAL`.
/// So a plane's first acquire names the undefined layout as its old layout where the
/// consumer reported no Direct3D 11 access of it before
/// ([`Surface::external_writes_done`]), and the general layout where it did; every later
/// acquire names the general layout, the plane being general after rt's own first acquire
/// and release. "First" is the first acquire rt records for the plane.
///
/// **Ownership.** Each `vkQueueSubmit2` whose command buffers or uploads use a plane acquires
/// it from `VK_QUEUE_FAMILY_EXTERNAL` in a command buffer of rt's own ahead of everything in
/// it, from and to the general layout (`VUID-VkImageMemoryBarrier-srcQueueFamilyIndex-12332`),
/// and releases it to that family in another after everything in it, in the general layout
/// it, from the general layout, or from the undefined layout at its first acquire as
/// **Layout** says (`VUID-VkImageMemoryBarrier-srcQueueFamilyIndex-12332` allows either), to
/// the general layout, and releases it to that family in another after everything in it, in
/// the general layout
/// (`...-dstQueueFamilyIndex-12331`), which takes that call's last signals, its progress value
/// among them, so it adds no submit info and a command buffer that uses a plane is seen
/// complete only once the plane's release has run: Direct3D 11 owns the planes' memory
@@ -126,20 +135,27 @@ impl Device {
/// [`Metrics::d3d11_plane_submissions`](crate::Metrics::d3d11_plane_submissions). A group of
/// command buffers rt sends member by member, one `vkQueueSubmit2` a member, hands each
/// member's planes over in that member's call
/// ([`Metrics::d3d11_plane_parts`](crate::Metrics::d3d11_plane_parts)). The acquire follows a global barrier from and to every stage, so an earlier submission's
/// release on rt's one queue happens before it (Vulkan 1.4, section 7.7.6).
/// ([`Metrics::d3d11_plane_parts`](crate::Metrics::d3d11_plane_parts)). The acquire follows
/// a global barrier from and to every stage, so an earlier submission's release on rt's one
/// queue happens before it (Vulkan 1.4, section 7.7.6).
/// [`Surface::external_writes_done`] and
/// [`release_surface_to_external`](crate::BlitCommandEncoder::release_surface_to_external)
/// record nothing for such a surface. No keyed mutex is used, by rt or on the textures: each
/// record nothing for such a surface; the first marks its planes accessed by Direct3D 11, on
/// the host (**Layout**). No keyed mutex is used, by rt or on the textures: each
/// is made with `D3D11_RESOURCE_MISC_SHARED | D3D11_RESOURCE_MISC_SHARED_NTHANDLE`, not
/// `D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX`, whose mutex the device that makes the texture and
/// each device that opens it must acquire before rendering to it; the consumer orders the two
/// APIs' work by its waits.
///
/// **The consumer's precondition.** Direct3D 11's work that accesses a plane has completed
/// before the consumer commits the command buffers that use it, and rt's command buffers that
/// use a plane have completed before Direct3D 11 accesses it again. A plane rt uses before
/// any Direct3D 11 access has undefined contents, as a Metal texture nothing wrote does.
/// before the consumer commits the command buffers that use it, the consumer calls
/// [`Surface::external_writes_done`] after each such access and before that commit, and rt's
/// command buffers that use a plane have completed before Direct3D 11 accesses it again.
/// Without the call after Direct3D 11's first access, rt's first acquire of the plane names
/// the undefined layout, and a transition from it does not preserve the contents (section
/// 12.5.1: "implying that the contents of the image subresource need not be preserved"), so
/// the device may discard what Direct3D 11 wrote. A plane rt uses before any Direct3D 11
/// access has undefined contents, as a Metal texture nothing wrote does.
///
/// The surface has no buffer: [`Surface::buffer`] is [`Gap::D3d11SurfaceBuffer`], as its
/// planes lie in memories of their own.
@@ -346,8 +362,9 @@ impl Device {
.tiling(vk::ImageTiling::OPTIMAL)
.usage(usage)
.sharing_mode(vk::SharingMode::EXCLUSIVE)
// An external image is made undefined (`VUID-VkImageCreateInfo-pNext-01443`); after
// Direct3D 11's access it is in the general layout (section 12.5.2).
// An external image is made undefined (`VUID-VkImageCreateInfo-pNext-01443`), which
// its first acquire names where no Direct3D 11 access was reported; after one, it is
// in the general layout (section 12.5.2, `Image::handed`).
.initial_layout(vk::ImageLayout::UNDEFINED)
.push_next(&mut external);
if !list.is_empty() {
+14 -3
View File
@@ -1649,8 +1649,15 @@ impl Surface {
/// [`Metrics::external_acquires`](crate::Metrics::external_acquires). In the copy form it is
/// the block's acquire, and the submissions copy the bytes into the planes' images. The
/// contract is the block's. For a Direct3D 11 import (`Device::import_d3d11_surface`, on
/// Windows) the call records nothing: each `vkQueueSubmit2` that uses its planes acquires
/// them itself ([`Metrics::d3d11_plane_submissions`](crate::Metrics::d3d11_plane_submissions)).
/// Windows) the call records nothing and makes no GPU work: each `vkQueueSubmit2` that uses
/// its planes acquires them itself
/// ([`Metrics::d3d11_plane_submissions`](crate::Metrics::d3d11_plane_submissions)), and the
/// call marks each plane accessed by Direct3D 11, on the host, so each acquire from then on
/// names the general layout, which Vulkan takes such a plane to be in after external access
/// (section 12.5.2). The consumer calls it after every Direct3D 11 access, before committing
/// the command buffers that use the planes: without it, a plane's first acquire names the
/// undefined layout it is made in, which may discard what Direct3D 11 wrote (section
/// 12.5.1).
///
/// Err: [`Refusal::InScheduledHandler`] in a scheduled handler; [`Error::Quiesced`]; and
/// [`Error::Vulkan`] where the upload's command buffer cannot be made.
@@ -1658,8 +1665,12 @@ impl Surface {
if let Kind::Copy(block, _) = &self.kind {
return block.external_writes_done();
}
// Each `vkQueueSubmit2` that uses a Direct3D 11 import's planes acquires them itself.
// Each `vkQueueSubmit2` that uses a Direct3D 11 import's planes acquires them itself; the
// call marks them accessed, so each acquire from now on names the general layout.
if self.handle.is_none() {
for texture in &self.planes {
texture.held_image().external_access();
}
return Ok(());
}
crate::command::not_in_scheduled_handler()?;
+50 -24
View File
@@ -501,7 +501,9 @@ pub(crate) struct Image {
linear_attachment: bool,
/// The layout its move home starts from: undefined; preinitialized for a texture over a buffer
/// in place, whose move home keeps the bytes (over a dma-buf's import, the layout of the
/// bytes the host wrote, which the move's acquire names, `foreign`).
/// bytes the host wrote, which the move's acquire names, `foreign`). A Direct3D 11 plane's is
/// undefined too, the layout it is made in, which its first acquire names
/// ([`handed`](Self::handed)).
first: vk::ImageLayout,
/// A heap texture's image placed over bytes another resource of its heap reaches
/// (`crate::alias`): its move home waits for every earlier command's writes. False for every
@@ -512,7 +514,11 @@ pub(crate) struct Image {
/// queue family (`VK_QUEUE_FAMILY_FOREIGN_EXT`) in one barrier with the image, so that both
/// keep the bytes. `None` for every other image.
foreign: Option<(vk::Buffer, u64, u64)>,
/// An upload recorded its move from `first` to `home`. Read and set under the work lock.
/// An upload recorded its move from `first` to `home`. Read and set under the work lock. A
/// Direct3D 11 plane, which no upload moves, is set by its first acquire, which moves it from
/// `first`, recorded on the submit thread ([`handed`](Self::handed)), or by the consumer's
/// report of Direct3D 11's access, after which Vulkan takes it as general
/// ([`external_access`](Self::external_access)).
homed: AtomicBool,
/// How the move home zeroes it, for an image the runtime places for a texture of its own
/// (`Source::Plain` and `Source::Managed`); `None` for every other image.
@@ -742,25 +748,36 @@ impl Image {
/// (`Device::import_d3d11_surface`, on Windows) not yet there: each `vkQueueSubmit2` that
/// reaches such an image hands it over at its start and its end ([`handed`](Self::handed)).
pub(crate) fn keep_plane(self: &Arc<Image>, planes: &mut Vec<Arc<Image>>) {
let plane = matches!(self._under, Some(Under::Surface { d3d11: true, .. }));
if plane && !planes.iter().any(|kept| Arc::ptr_eq(kept, self)) {
if self.d3d11_plane() && !planes.iter().any(|kept| Arc::ptr_eq(kept, self)) {
planes.push(Arc::clone(self));
}
}
/// Whether it is a Direct3D 11 texture's plane (`Device::import_d3d11_surface`, on Windows).
fn d3d11_plane(&self) -> bool {
matches!(self._under, Some(Under::Surface { d3d11: true, .. }))
}
/// A Direct3D 11 plane's hand-over between `VK_QUEUE_FAMILY_EXTERNAL` and the runtime's
/// queue family `family`, the general layout kept
/// queue family `family`, to the general layout
/// (`VUID-VkImageMemoryBarrier-srcQueueFamilyIndex-12332`, `...-dstQueueFamilyIndex-12331`):
/// its acquire (`acquire`), made visible to every later command, or its release, after every
/// earlier command's writes.
/// earlier command's writes. The plane's first acquire recorded, where the consumer reported
/// no Direct3D 11 access before it ([`external_access`](Self::external_access)), names the
/// undefined layout it is made in as its old layout (`VUID-VkImageCreateInfo-pNext-01443`),
/// and every later one, and every release, the general layout. Recorded on the submit thread.
pub(crate) fn handed(&self, family: u32, acquire: bool) -> vk::ImageMemoryBarrier2<'static> {
let (all, access) = (
vk::PipelineStageFlags2::ALL_COMMANDS,
vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE,
);
let old = match acquire && !self.homed.swap(true, Ordering::Relaxed) {
true => self.first,
false => self.home,
};
let barrier = vk::ImageMemoryBarrier2::default()
.old_layout(vk::ImageLayout::GENERAL)
.new_layout(vk::ImageLayout::GENERAL)
.old_layout(old)
.new_layout(self.home)
.image(self.image)
.subresource_range(whole(self.aspects));
match acquire {
@@ -780,7 +797,9 @@ impl Image {
/// for each acquire, [`Work::home`](crate::submit::Work::home)), with its zeroing between
/// two moves where it has one ([`Zero`]). Under the work lock.
pub(crate) fn homing(&self) -> Option<Homing> {
if self.homed.swap(true, Ordering::Relaxed) {
// A Direct3D 11 plane moves home in its first acquire, or is general after Direct3D 11's
// access ([`handed`](Self::handed)): no upload moves it.
if self.d3d11_plane() || self.homed.swap(true, Ordering::Relaxed) {
return None;
}
// A drawable's image moves home at each acquire: from the present layout after a present.
@@ -879,6 +898,14 @@ impl Image {
matches!(self.zero, Some(Zero::Copy { .. }))
}
/// A Direct3D 11 plane's access by Direct3D 11 has completed, as the consumer reports it
/// ([`Surface::external_writes_done`](crate::Surface::external_writes_done)): Vulkan takes it
/// as general from then on (section 12.5.2), so its next acquire names the general layout
/// ([`handed`](Self::handed)). On the host alone.
pub(crate) fn external_access(&self) {
self.homed.store(true, Ordering::Relaxed);
}
/// Its move home is owed, recorded by the next upload that holds it ([`homing`](Self::homing)):
/// a new image's, or a drawable's at each acquire. Under the work lock.
pub(crate) fn owe_home(&self) {
@@ -6381,9 +6408,9 @@ pub(crate) struct SurfaceImage {
pub(crate) format: vk::Format,
pub(crate) view_formats: Vec<vk::Format>,
pub(crate) block: Option<Arc<Block>>,
/// A Direct3D 11 texture's (`Device::import_d3d11_surface`, on Windows): its image is in the
/// general layout from its first use, with no move from undefined, which Vulkan takes as
/// general after Direct3D 11's access (section 12.5.2).
/// A Direct3D 11 texture's (`Device::import_d3d11_surface`, on Windows): no upload moves its
/// image, made undefined: its first acquire moves it to the general layout, or Vulkan takes
/// it as general after Direct3D 11's access (section 12.5.2, [`Image::handed`]).
pub(crate) d3d11: bool,
/// The image is `LINEAR` and its format renders only through
/// `VK_FORMAT_FEATURE_2_LINEAR_COLOR_ATTACHMENT_BIT_NV` (`crate::surface::Query::plane`):
@@ -6429,10 +6456,11 @@ impl Device {
/// The texture of a surface plane's image, at `index` as for [`Device::new_texture`]: its
/// home layout general, from undefined in the next upload, which goes ahead of every command
/// buffer committed after the make, or already general where the image is (a Direct3D 11
/// texture's). Its format is the descriptor's, a view of the image's own where they differ.
/// The texture holds the image, which its drop frees with its memory. It takes `unit`'s id in
/// the hazard sets, the surface's ([`HeapId::unit`]).
/// buffer committed after the make, or, a Direct3D 11 texture's, from undefined in its first
/// acquire or general after Direct3D 11's access ([`Image::handed`]). Its format is the
/// descriptor's, a view of the image's own where they differ. The texture holds the image,
/// which its drop frees with its memory. It takes `unit`'s id in the hazard sets, the
/// surface's ([`HeapId::unit`]).
///
/// Err: [`Refusal::TextureIndex`]; [`Gap::SurfaceUsage`] for a usage the texture's uses ask
/// that the image lacks; [`Error::Vulkan`] where a view cannot be made; what the texture's
@@ -6525,13 +6553,11 @@ impl Device {
view_formats: made.view_formats,
cube: false,
linear_attachment: made.linear_attachment,
first: match general {
true => home,
false => vk::ImageLayout::UNDEFINED,
},
first: vk::ImageLayout::UNDEFINED,
over: false,
// An image already general owes no move.
homed: AtomicBool::new(general),
// A Direct3D 11 plane's move from undefined is its first acquire's, or its access by
// Direct3D 11 makes it general: no upload moves it (`Image::handed`).
homed: AtomicBool::new(false),
foreign: None,
zero: None,
drawable: None,
@@ -6548,8 +6574,8 @@ impl Device {
let element = sampled.filter(|_| plan.read).map(|view| (view, home));
let looped = sampled.filter(|_| image.looped());
// SAFETY: the texture's views are live; its image is in its home layout whenever
// work uses it: the upload that moves it goes ahead of that work, or it is general
// after the other API's access.
// work uses it: the upload that moves it goes ahead of that work, or its acquire
// does, or it is general after the other API's access.
let owner = unsafe {
(core.arrays).place_texture(
&core.device,