diff --git a/crates/metal2vulkan-runtime/src/dma_buf.rs b/crates/metal2vulkan-runtime/src/dma_buf.rs index 219854f73..7030d8334 100644 --- a/crates/metal2vulkan-runtime/src/dma_buf.rs +++ b/crates/metal2vulkan-runtime/src/dma_buf.rs @@ -1528,11 +1528,18 @@ unsafe impl Send for ExportedPages {} impl ExportedPages { /// `length` bytes of host-visible, host-coherent memory, exported as a dma-buf and mapped, of - /// the first such type that is host-cached and not device-local where one is (system memory as - /// the CPU caches it, as a guest's pages are; the check reads every byte back, which took 3.1 s - /// for a 3840x2160 surface's 33 MB through RADV's uncached type on the Radeon 780M, against - /// 7.2 ms cached), else not device-local; `None` where the device imports no dma-bufs, makes no - /// images over them, exports no such memory but as a dedicated allocation, or refuses it. + /// the first such type that is host-cached, one that is not device-local before one that is, + /// else the first that is not device-local, else the first: memory as the CPU caches it, as a + /// guest's pages are, which the check reads every byte of back. That took 3.1 s for a + /// 3840x2160 surface's 33 MB through RADV's uncached type on the Radeon 780M, against 7.2 ms + /// cached; and on the Iris Xe (ANV), whose host-visible types are all device-local, the whole + /// check took 19.0 s over its first type, which is uncached. Allocated with the device-address + /// flag where the device enables `bufferDeviceAddress`, as each of the runtime's allocations + /// and imports is: ANV (Mesa 26.2.6) refuses an import of a dma-buf of its own memory whose + /// flag differs from the allocation's (`VK_ERROR_INVALID_EXTERNAL_HANDLE`, of every memory + /// type), which left the check's class unchecked there. `None` where the device imports no + /// dma-bufs, makes no images over them, exports no such memory but as a dedicated allocation, + /// or refuses it. pub(super) fn make(device: &Device, length: u64) -> Option { let shared = device.shared(); let importer = shared.host_import.as_ref().ok()?; @@ -1570,23 +1577,27 @@ impl ExportedPages { .map_while(|index| memory.type_flags(index).map(|flags| (index, flags))) .filter(|(_, flags)| flags.contains(wanted)) .collect(); - let system = |cached: bool| { - types.iter().find(|(_, flags)| { - !flags.contains(vk::MemoryPropertyFlags::DEVICE_LOCAL) - && (!cached || flags.contains(vk::MemoryPropertyFlags::HOST_CACHED)) + // Host-cached first, then system memory first; the first of the best. + (types.iter()) + .min_by_key(|(_, flags)| { + ( + !flags.contains(vk::MemoryPropertyFlags::HOST_CACHED), + flags.contains(vk::MemoryPropertyFlags::DEVICE_LOCAL), + ) }) - }; - system(true) - .or(system(false)) - .or(types.first()) .map(|&(index, _)| index)? }; let raw_device = shared.vk(); let mut export = vk::ExportMemoryAllocateInfo::default().handle_types(DMA_BUF); - let info = vk::MemoryAllocateInfo::default() + let mut flags = + vk::MemoryAllocateFlagsInfo::default().flags(vk::MemoryAllocateFlags::DEVICE_ADDRESS); + let mut info = vk::MemoryAllocateInfo::default() .allocation_size(length) .memory_type_index(type_index) .push_next(&mut export); + if shared.device_address { + info = info.push_next(&mut flags); + } // SAFETY: the device is live; the type exports dma-bufs of a buffer's memory (above). let memory = unsafe { crate::creation::allocate_memory(raw_device, &info) }.ok()?; let free = || { diff --git a/crates/metal2vulkan-runtime/src/in_place_check.rs b/crates/metal2vulkan-runtime/src/in_place_check.rs index b7028bfdf..75f8a0b03 100644 --- a/crates/metal2vulkan-runtime/src/in_place_check.rs +++ b/crates/metal2vulkan-runtime/src/in_place_check.rs @@ -127,10 +127,12 @@ //! waiter's `vkAllocateCommandBuffers` faulted on the magic (an M3 Max under MoltenVK 1.4.2, //! one run of three), where the same work with no check running did not in 30 000 rounds. //! - **Its dma-bufs.** The runtime runs no `udmabuf` code, so the dma-buf class's pages are an -//! allocation of the device's own, host-visible and exported as a dma-buf, mapped, and imported -//! only as that dma-buf (`HostMemory::dma_buf_only`), never by their pointer, which a driver may -//! take too (ANV's own mapped allocation); the check's source answers each request over them -//! with that dma-buf. The buffer's import, the image and its first move are the product's. +//! allocation of the device's own, host-visible and exported as a dma-buf, mapped +//! (`dma_buf::ExportedPages`: host-cached where a type is, and with the device-address flag its +//! imports carry), and imported only as that dma-buf (`HostMemory::dma_buf_only`), never by +//! their pointer, which a driver may take too (lavapipe takes its own mapped allocation's; ANV +//! refuses it); the check's source answers each request over them with that dma-buf. The +//! buffer's import, the image and its first move are the product's. use crate::check_draw::CheckDraw; use crate::device::holds::Holder; diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index 965bc0d42..fb0bd7763 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -2010,8 +2010,12 @@ impl Memory { /// Import the dma-buf `fd`, `size` bytes, as memory of the best type of `class` among /// `allowed` (the types both the dma-buf and what is bound in it take): the memory and its /// type, the caller's to free (`dma_buf.rs`'s `DmaBufPages`), in no block and never mapped. - /// For a buffer, allocated with the device-address flag where the device enables - /// `bufferDeviceAddress`; for `image`, its dedicated allocation (`size` its requirements'). + /// Allocated with the device-address flag where the device enables `bufferDeviceAddress`, as + /// every block of the runtime's is (`BlockMaker::make_as`), and for `image` as its dedicated + /// allocation (`size` its requirements'): every import of one dma-buf, a buffer's and its + /// textures' images', and the device's own allocation of it agree, as ANV (Mesa 26.2.6) + /// refuses an import of memory it holds whose flag differs (`VK_ERROR_INVALID_EXTERNAL_HANDLE`, + /// of every memory type: `dma_buf::ExportedPages`). /// Refused when no type of `class` is among `allowed`, else with the failure of the last type /// tried. /// @@ -2058,13 +2062,12 @@ impl Memory { .allocation_size(size) .memory_type_index(type_index) .push_next(&mut import); - match image { - Some(image) => { - dedicated = dedicated.image(image); - info = info.push_next(&mut dedicated); - } - None if self.device_address => info = info.push_next(&mut flags), - None => {} + if let Some(image) = image { + dedicated = dedicated.image(image); + info = info.push_next(&mut dedicated); + } + if self.device_address { + info = info.push_next(&mut flags); } // SAFETY: `info` and its chain live for the call; the type index is the device's and // allows the dma-buf (and the image, unbound and made for dma-bufs, whose size this diff --git a/crates/metal2vulkan-runtime/src/tests/in_place_check.rs b/crates/metal2vulkan-runtime/src/tests/in_place_check.rs index 6fed0c110..e3956f0e7 100644 --- a/crates/metal2vulkan-runtime/src/tests/in_place_check.rs +++ b/crates/metal2vulkan-runtime/src/tests/in_place_check.rs @@ -310,7 +310,8 @@ fn texture_ids_keep_metals_order_while_the_in_place_check_runs() { /// A no-copy buffer of `length` bytes over an allocation of `device`'s own exported as a dma-buf /// and mapped (`dma_buf::ExportedPages`, the check's dma-buf pages), with a source of that /// dma-buf, imported only as it where `only` (`HostMemory::dma_buf_only`), else as any no-copy -/// buffer's pages are, the pointer first. `None` where the device exports no such pages. +/// buffer's pages are, the pointer first. `None` where the device exports no such pages; pages it +/// exports but refuses to import fail the test, as the check's class would stay unchecked. #[cfg(target_os = "linux")] fn exported_buffer(device: &Device, length: u64, only: bool) -> Option { let exported = crate::dma_buf::ExportedPages::make(device, length)?; @@ -331,7 +332,14 @@ fn exported_buffer(device: &Device, length: u64, only: bool) -> Option { }; let descriptor = BufferDescriptor::new(length, ResourceOptions::shared(), contents) .with_placement(AddressPlacement::Outside); - device.new_buffer_with_descriptor(descriptor).ok() + match device.new_buffer_with_descriptor(descriptor) { + Ok(buffer) => Some(buffer), + Err(error) => panic!( + "{}: the device exports dma-buf pages it refuses to import (dma-buf only {only}): \ + {error:?}", + device.name() + ), + } } /// The device's check imports its dma-buf class's pages only as their dma-buf, never by their