From 0b1dd0fc361da50aa1e4e284d80c3d6141b35a17 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 8 Nov 2026 17:51:48 -0800 Subject: [PATCH] An image over a dma-buf block names a type the block is exported as, with no image-side compatibility test On the 780M (m3-max-10304 at 07d9fcac79) the image naming an opaque file descriptor over a block exported as DMA_BUF | OPAQUE_FD was refused only by the image-side compatibility test this branch added: RADV's format query lists compatibleHandleTypes 0x1 (the opaque type alone) for its opaque linear images. That test was stricter than Vulkan: - VUID-VkExportMemoryAllocateInfo-handleTypes-09860: the export set's bits must be compatible "as reported by ... VkExternalImageFormatProperties, or VkExternalBufferProperties"; the buffer query answers them so (FdExport::new). - VUID-VkImageCreateInfo-pNext-00990: an image's handleTypes lie in its own compatibleHandleTypes; the image names one type. - VUID-vkBindImageMemory-memory-02728: the memory's export set includes one of the image's types. So the test goes (LinearRefusal::Incompatible and ModifierMemory::shared_with with it). What stays, per handle type the image names: the format query reports it EXPORTABLE, not DEDICATED_ONLY; the image made requires no dedicated allocation (VkMemoryDedicatedRequirements); its memoryTypeBits take the block's type and its alignment is met at the texels' offset (alias_unbound_in's aliases check); and the in-place check measures that exact image, under the layer, before a texture takes it. The VUIDs are cited where the export set (FdExport::new) and the image's handle type (alias_unbound) are decided. Docs follow: API.md's shadow rule, HOWTO section 7, the in_place.rs module doc. Verification: the 780M, the RX 550, lavapipe and the Iris Xe next, then the 5080 through an entry. (cherry picked from commit 239d8ff9bbd4477d784f4ccc815ebf8af53dcad0) --- crates/metal2vulkan-runtime/src/dma_buf.rs | 25 --------- .../src/exported_memory.rs | 14 +++-- crates/metal2vulkan-runtime/src/in_place.rs | 8 +-- .../src/tests/linear_plan.rs | 14 ----- crates/metal2vulkan-runtime/src/texture.rs | 53 +++++++++---------- docs/API.md | 16 +++--- docs/HOWTO.md | 14 ++--- 7 files changed, 53 insertions(+), 91 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/dma_buf.rs b/crates/metal2vulkan-runtime/src/dma_buf.rs index 85207832d..4c4febdd7 100644 --- a/crates/metal2vulkan-runtime/src/dma_buf.rs +++ b/crates/metal2vulkan-runtime/src/dma_buf.rs @@ -1590,17 +1590,6 @@ impl Device { features.as_raw() )); } - let compatible = imported.external_memory_properties.compatible_handle_types; - if !compatible.contains(memory.shared_with()) { - return Err(format!( - "its image format query lists handle types {:#x} as compatible with a \ - DRM_FORMAT_MOD_LINEAR image of {:?} for {}, whose memory is exported as {:#x}", - compatible.as_raw(), - plan.format, - memory.words(), - memory.shared_with().as_raw() - )); - } Ok(limits) } } @@ -1677,20 +1666,6 @@ impl ModifierMemory { } } - /// The handle types an image's format query must list as compatible with the one it names - /// (`VkExternalMemoryProperties::compatibleHandleTypes`): for the runtime's exported memory, - /// every type its blocks are exported as, which an image bound there shares memory with - /// (`VUID-VkExportMemoryAllocateInfo-handleTypes-09860`); none for other memory, whose - /// allocation names the image's type alone. - pub(super) fn shared_with(self) -> vk::ExternalMemoryHandleTypeFlags { - match self { - ModifierMemory::Exported { exports, .. } => exports, - ModifierMemory::DmaBuf | ModifierMemory::Host | ModifierMemory::Own => { - vk::ExternalMemoryHandleTypeFlags::empty() - } - } - } - /// The memory, in words. pub(super) fn words(self) -> &'static str { match self { diff --git a/crates/metal2vulkan-runtime/src/exported_memory.rs b/crates/metal2vulkan-runtime/src/exported_memory.rs index 4ef0ed28d..012c1e17b 100644 --- a/crates/metal2vulkan-runtime/src/exported_memory.rs +++ b/crates/metal2vulkan-runtime/src/exported_memory.rs @@ -167,11 +167,15 @@ impl FdExport { /// /// A dma-buf's shared blocks are exportable as an opaque file descriptor too where that query /// answers each handle type compatible with the other and an opaque file descriptor - /// exportable from a buffer's memory not only as a dedicated allocation: an image bound in such - /// a block may name either (`VUID-vkBindImageMemory-memory-02728`), and a device whose images - /// of a dma-buf's handle type take only a dedicated allocation (RADV's) binds one naming an - /// opaque file descriptor there (`texture.rs`'s `alias_unbound`). The dma-buf a buffer's export - /// answers is the same memory's either way (`vkGetMemoryFdKHR` of the dma-buf handle type). + /// exportable from a buffer's memory not only as a dedicated allocation: the export set's + /// compatibility is this query's (`VUID-VkExportMemoryAllocateInfo-handleTypes-09860`: "as + /// reported by VkExternalImageFormatProperties, or VkExternalBufferProperties"), an image + /// bound in such a block may name either (`VUID-vkBindImageMemory-memory-02728`: one type in + /// common), its own compatible types bounding only those it names + /// (`VUID-VkImageCreateInfo-pNext-00990`), and a device whose images of a dma-buf's handle + /// type take only a dedicated allocation (RADV's) binds one naming an opaque file descriptor + /// there (`texture.rs`'s `alias_unbound`). The dma-buf a buffer's export answers is the same + /// memory's either way (`vkGetMemoryFdKHR` of the dma-buf handle type). /// /// # Safety /// diff --git a/crates/metal2vulkan-runtime/src/in_place.rs b/crates/metal2vulkan-runtime/src/in_place.rs index f29db0167..8bf157274 100644 --- a/crates/metal2vulkan-runtime/src/in_place.rs +++ b/crates/metal2vulkan-runtime/src/in_place.rs @@ -96,10 +96,10 @@ //! the opaque one; the check's own textures over that memory take the same image, so its //! verdicts are of the image a texture takes. Where the device's image format query //! (`VkPhysicalDeviceExternalImageFormatInfo`) takes none of those handle types for such an -//! image of the texture's format, usage and tiling but with a dedicated allocation, or lists -//! not every type the block is exported as among those compatible with it, or where the memory -//! is the buffer's dedicated allocation (`VUID-vkBindImageMemory-memory-10926`), the texture is -//! a shadow, its notice naming why. Where such an image lies is decided as for any texture over a +//! image of the texture's format, usage and tiling but with a dedicated allocation, or the +//! image made requires one (`VkMemoryDedicatedRequirements`), or where the memory is the +//! buffer's dedicated allocation (`VUID-vkBindImageMemory-memory-10926`), the texture is a +//! shadow, its notice naming why. Where such an image lies is decided as for any texture over a //! buffer (`linear_plan::aliases`), of the image made for the handle type, for each texture (each //! plane of a picture) apart: the texels' offset in the memory (the buffer's first byte lies at //! a multiple of 256 bytes there, or its `aligned_byte` does, `BufferDescriptor::aligned_byte`; diff --git a/crates/metal2vulkan-runtime/src/tests/linear_plan.rs b/crates/metal2vulkan-runtime/src/tests/linear_plan.rs index a3bdb5b63..881f25214 100644 --- a/crates/metal2vulkan-runtime/src/tests/linear_plan.rs +++ b/crates/metal2vulkan-runtime/src/tests/linear_plan.rs @@ -1440,7 +1440,6 @@ fn a_refused_linear_image_names_the_need_that_failed() { "{import}" ); let opaque = vk::ExternalMemoryHandleTypeFlags::OPAQUE_FD; - let dma_buf = vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT; let export = describe(LinearRefusal::External( ModifierMemory::Exported { named: opaque, @@ -1453,19 +1452,6 @@ fn a_refused_linear_image_names_the_need_that_failed() { && export.contains("exportable true, dedicated only true"), "{export}" ); - let both = ModifierMemory::Exported { - named: opaque, - exports: opaque | dma_buf, - }; - let incompatible = describe(LinearRefusal::Incompatible(both, opaque)); - assert!( - incompatible.contains("lists handle types 0x1 as compatible") - && incompatible.contains( - "for the runtime's memory exported as dma-bufs, named as opaque file \ - descriptors, whose memory is exported as 0x201" - ), - "{incompatible}" - ); } /// The committed profiles of the devices linear images were measured on. diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index 739349fec..f2537b770 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -810,10 +810,6 @@ pub(super) enum LinearRefusal { /// type): the device's image format query takes none of that memory for such an image, or /// only a dedicated allocation (its external memory features). External(ModifierMemory, vk::ExternalMemoryFeatureFlags), - /// For the runtime's memory exported as more than one handle type: the device's image format - /// query, of the handle type the image names, lists these handle types as compatible with - /// it, not every one the memory is exported as. - Incompatible(ModifierMemory, vk::ExternalMemoryHandleTypeFlags), } impl LinearRefusal { @@ -864,13 +860,6 @@ impl LinearRefusal { features.contains(memory.needs()), features.contains(vk::ExternalMemoryFeatureFlags::DEDICATED_ONLY) ), - LinearRefusal::Incompatible(memory, compatible) => format!( - "its image format query lists handle types {:#x} as compatible with a linear \ - image of {format:?}, usage {usage:?}, for {}, whose memory is exported as {:#x}", - compatible.as_raw(), - memory.words(), - memory.shared_with().as_raw() - ), } } } @@ -2836,10 +2825,8 @@ impl Device { /// handle type (host allocations, `VK_EXT_external_memory_host`, or the runtime's memory /// exported as an opaque file descriptor or a dma-buf), where its image format query /// (`VkPhysicalDeviceExternalImageFormatInfo`) takes that memory for such an image (importable, - /// or exportable for memory the runtime exports) without a dedicated allocation, and lists - /// every handle type the memory is exported as among those compatible with it - /// (`ModifierMemory::shared_with`); why not where it does not, or makes no such image (the - /// refusal named). + /// or exportable for memory the runtime exports) without a dedicated allocation; why not where + /// it does not, or makes no such image (the refusal named). pub(super) fn external_image_limits( &self, info: &vk::ImageCreateInfo<'_>, @@ -2881,13 +2868,9 @@ impl Device { Ok(()) => { let takes = features.contains(memory.needs()) && !features.contains(vk::ExternalMemoryFeatureFlags::DEDICATED_ONLY); - let compatible = external.external_memory_properties.compatible_handle_types; - if !takes { - Ok(Err(LinearRefusal::External(memory, features))) - } else if !compatible.contains(memory.shared_with()) { - Ok(Err(LinearRefusal::Incompatible(memory, compatible))) - } else { - Ok(Ok(limits)) + match takes { + true => Ok(Ok(limits)), + false => Ok(Err(LinearRefusal::External(memory, features))), } } Err(vk::Result::ERROR_FORMAT_NOT_SUPPORTED) => { @@ -2915,13 +2898,14 @@ impl Device { /// ([`PagesClass::ExportedFd`], [`PagesClass::ExportedDmaBuf`]) take each access the usage /// takes for texels that lie in its lines as these do (a pending verdict starts the check, /// and the texture is a shadow meanwhile), and where the device's image format query takes - /// the handle type for the image without a dedicated allocation, listing every type the - /// block is exported as among those compatible with it. A block of dma-bufs exportable as an - /// opaque file descriptor too (`exported_memory.rs`'s `FdExport::new`) has its image name the - /// dma-buf type where the query takes it, else the opaque one: RADV takes the dma-buf type - /// for no linear image and for a `DRM_FORMAT_MOD_LINEAR` one only in a dedicated allocation, - /// and takes the opaque one in a shared block. The check's own textures take them whatever - /// the verdicts, so its verdicts are of the image a texture names. + /// the handle type for the image without a dedicated allocation and the image made requires + /// none. A block of dma-bufs exportable as an opaque file descriptor too + /// (`exported_memory.rs`'s `FdExport::new`) has its image name the dma-buf type where the + /// device takes it so, else the opaque one: RADV's format query takes the dma-buf type for no + /// linear image, and its `DRM_FORMAT_MOD_LINEAR` image of it requires a dedicated + /// allocation, where it takes the opaque one for a linear image in a shared block. The check's + /// own textures take them whatever the verdicts, so its verdicts are of the image a texture + /// names. /// - In a buffer's dedicated allocation (an exportable buffer's where the driver exports /// such memory only so, or prefers or requires it), no image /// (`VUID-vkBindImageMemory-memory-10926`). @@ -2972,6 +2956,17 @@ impl Device { ))); } }; + // Each handle type the block is exported as, the dma-buf's first. An image naming one of + // them binds there: the bind needs one type in common with the memory's export set + // (`VUID-vkBindImageMemory-memory-02728`); the image's own `compatibleHandleTypes` bounds + // only the types it names itself (`VUID-VkImageCreateInfo-pNext-00990`), which is one; + // and the export set's compatibility is the buffer query's + // (`VUID-VkExportMemoryAllocateInfo-handleTypes-09860`: "as reported by + // VkExternalImageFormatProperties, or VkExternalBufferProperties", `FdExport::new`). + // So the image's format query must take the type it names (exportable, not only as a + // dedicated allocation), the image made must require no dedicated allocation, and it + // must take the block's memory type at the texels' offset (`alias_unbound_in`); RADV's + // lists the opaque type alone as compatible with its opaque linear images. let dma_buf = vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT; let opaque = vk::ExternalMemoryHandleTypeFlags::OPAQUE_FD; let mut refused: Option<(String, ShadowCause)> = None; diff --git a/docs/API.md b/docs/API.md index 6a409022e..f6e5d5952 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1659,14 +1659,14 @@ bound. over memory the runtime exports (`MemoryExport`, a runtime extension), where either image names a handle type the memory is exportable as, as Vulkan requires, and so starts `UNDEFINED`: its check of that exported memory has not passed for the texture's accesses (a pending one moving - the same way), the device's image format query takes none of those handle types for such an - image but in a dedicated allocation, or lists not every one the memory is exportable as among - those compatible with it, or the memory is the buffer's dedicated allocation, to which no image - may be bound (a block of dma-bufs is exportable as an opaque file descriptor too where the - device's buffer queries answer both together, each compatible with the other, and its image then - names the opaque handle type where the query takes no dma-buf for it, as RADV's does for linear - images, `WorkCounts::exported_dma_buf_images_named_opaque_fd`; the buffer's export stays a dma-buf - of the same memory); + the same way), the device takes none of those handle types for such an image but in a dedicated + allocation (its image format query says so, or the image made requires one), or the memory is + the buffer's dedicated allocation, to which no image may be bound (a block of dma-bufs is + exportable as an opaque file descriptor too where the device's buffer queries answer both + together, each compatible with the other, and its image then names the opaque handle type where + the device takes no dma-buf for it in a shared block, as RADV takes none for a linear image, + `WorkCounts::exported_dma_buf_images_named_opaque_fd`; the buffer's export stays a dma-buf of the + same memory); for one that renders, whose rows each to its pitch share no line with bytes past them, the check of renders that lets the rows' padding change, as the declaration does, and no byte past the last row's pitch); past a bound in bytes of its rows to their diff --git a/docs/HOWTO.md b/docs/HOWTO.md index c47d6ed6f..f29258212 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -820,8 +820,8 @@ format, extent and usage, that handle type; the plain linear image first, else a - the image format query (`vkGetPhysicalDeviceImageFormatProperties2` with `VkPhysicalDeviceExternalImageFormatInfo`) takes the handle type for it, and not in a dedicated - allocation alone, and lists every handle type the memory is exportable as among those - compatible with it; + allocation alone, and the image made requires no dedicated allocation + (`VkMemoryDedicatedRequirements`); - the plane's byte in the memory, `exported.offset` plus the plane's offset in the frame, is a multiple of the `alignment` `vkGetImageMemoryRequirements` answers for the image; - `vkGetImageSubresourceLayout` answers `offset` 0 and a `rowPitch` equal to the plane's row @@ -833,10 +833,12 @@ format, extent and usage, that handle type; the plain linear image first, else a Dma-buf-exportable buffers that share a block share one allocated exportable as a dma-buf, and as an opaque file descriptor too where `vkGetPhysicalDeviceExternalBufferProperties` answers both exportable for the runtime's buffers, each compatible with the other, the opaque one not only in a -dedicated allocation. A plane's image there names the dma-buf handle type where the format query -takes it as above, else the opaque one where the query takes that. RADV takes the dma-buf handle -type for no plain linear image and for a `DRM_FORMAT_MOD_LINEAR` image only in a dedicated -allocation, so its planes over such a block name the opaque one +dedicated allocation. A plane's image there names the dma-buf handle type where the device takes +it as above, else the opaque one where it takes that: Vulkan binds an image in memory exported as +several handle types where the image names one of them, and the export set's compatibility is the +buffer query's. RADV's format query takes the dma-buf handle type for no plain linear image, and +its `DRM_FORMAT_MOD_LINEAR` image of it requires a dedicated allocation, so its planes over such a +block name the opaque one (`WorkCounts::exported_dma_buf_images_named_opaque_fd`). `export_memory_dma_buf` answers a dma-buf of the same memory whatever else the block is exportable as, and the device's check of that memory makes the image a texture there makes.