diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 97e9eb900..7069b52c5 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -5378,7 +5378,10 @@ types, each the first with its flags that a buffer of `floor::BUFFER_USAGE` can usage Vulkan 1.3 defines, and device addresses. Vulkan guarantees such a buffer a device-local type, so of the three only Shared and the ring can be missing on a conforming device. Vulkan answers which memory types a buffer can use only for a created device, so `PhysicalDevice::check` finds a type with each set of flags, and -`Device::new` chooses on the device (`Device::memory_types` answers `floor::MemoryTypes`). +`Device::new` chooses on the device (`Device::memory_types` answers `floor::MemoryTypes`). A device +without one is refused with `floor::Missing::MemoryType`, which names the flags it lacks and every +memory type the device offers buffers, by index and flags: from `PhysicalDevice::check`, every type +the device lists. | Memory type | Flags | Used for | |---|---|---| diff --git a/crates/metal2vulkan-runtime/src/floor.rs b/crates/metal2vulkan-runtime/src/floor.rs index a8e040a42..456143681 100644 --- a/crates/metal2vulkan-runtime/src/floor.rs +++ b/crates/metal2vulkan-runtime/src/floor.rs @@ -310,8 +310,9 @@ pub(crate) const RING_MEMORY: vk::MemoryPropertyFlags = vk::MemoryPropertyFlags: | vk::MemoryPropertyFlags::HOST_COHERENT.as_raw(), ); -/// The Vulkan names of the memory property flags the floor asks for. -pub(crate) const MEMORY_FLAG_NAMES: [(vk::MemoryPropertyFlags, &str); 4] = [ +/// The Vulkan names of the memory property flags: those the floor asks for, then the rest Vulkan +/// 1.3 and its extensions define, which a device's own types may carry. +pub(crate) const MEMORY_FLAG_NAMES: [(vk::MemoryPropertyFlags, &str); 9] = [ ( vk::MemoryPropertyFlags::DEVICE_LOCAL, "VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT", @@ -328,6 +329,26 @@ pub(crate) const MEMORY_FLAG_NAMES: [(vk::MemoryPropertyFlags, &str); 4] = [ vk::MemoryPropertyFlags::HOST_CACHED, "VK_MEMORY_PROPERTY_HOST_CACHED_BIT", ), + ( + vk::MemoryPropertyFlags::LAZILY_ALLOCATED, + "VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT", + ), + ( + vk::MemoryPropertyFlags::PROTECTED, + "VK_MEMORY_PROPERTY_PROTECTED_BIT", + ), + ( + vk::MemoryPropertyFlags::DEVICE_COHERENT_AMD, + "VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD", + ), + ( + vk::MemoryPropertyFlags::DEVICE_UNCACHED_AMD, + "VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD", + ), + ( + vk::MemoryPropertyFlags::RDMA_CAPABLE_NV, + "VK_MEMORY_PROPERTY_RDMA_CAPABLE_BIT_NV", + ), ]; /// The feature structures of Vulkan 1.3 that the floor reads and device creation enables, each @@ -913,10 +934,16 @@ pub enum Missing { /// A queue family with both `VK_QUEUE_GRAPHICS_BIT` and `VK_QUEUE_COMPUTE_BIT`. QueueFamily, /// A memory type with these property flags, as Vulkan names them, for buffers of every usage - /// the runtime gives them. [`BUFFER_USAGE`] is that usage. + /// the runtime gives them. [`BUFFER_USAGE`] is that usage. It names the types the device + /// offers such buffers too. MemoryType { /// The flags, joined by ` | `. flags: String, + /// The memory types the device offers such a buffer, each as `type with ` + /// (a bit Vulkan does not name as its hexadecimal value, a type of none as `no flags`), + /// joined by `, `, or `no type`. From `PhysicalDevice::check`, every type the device + /// lists: Vulkan answers which types a buffer can use only for a created device. + offered: String, }, /// A time domain of [`TIME_DOMAINS`] the device does not calibrate /// (`vkGetPhysicalDeviceCalibrateableTimeDomainsEXT`), as Vulkan names it. @@ -949,7 +976,10 @@ impl fmt::Display for Missing { needed, } => write!(f, "{name} {device}, below {needed}"), Missing::QueueFamily => f.write_str("a queue family with graphics and compute"), - Missing::MemoryType { flags } => write!(f, "a memory type with {flags} for buffers"), + Missing::MemoryType { flags, offered } => write!( + f, + "a memory type with {flags} for buffers (the device offers buffers: {offered})" + ), Missing::TimeDomain(name) => write!(f, "the calibrateable time domain {name}"), Missing::SetLayout { set } => write!( f, @@ -1547,17 +1577,39 @@ pub(crate) fn choose( .filter(|(index, _)| index.is_none()) .map(|(_, flags)| Missing::MemoryType { flags: flag_names(flags), + offered: offered(memory, usable), }) .collect()), } } -/// The Vulkan names of `flags`, joined by ` | `. +/// The memory types of `memory` whose bit is set in `usable`, as [`Missing::MemoryType`]'s +/// `offered` gives them. +fn offered(memory: &vk::PhysicalDeviceMemoryProperties, usable: u32) -> String { + let types: Vec = (0u32..) + .zip(memory.memory_types_as_slice()) + .filter(|(index, _)| usable & (1 << index) != 0) + .map(|(index, kind)| match kind.property_flags.is_empty() { + true => format!("type {index} with no flags"), + false => format!("type {index} with {}", flag_names(kind.property_flags)), + }) + .collect(); + match types.is_empty() { + true => "no type".to_owned(), + false => types.join(", "), + } +} + +/// The Vulkan names of `flags`, joined by ` | `, and the bits Vulkan does not name after them as +/// one hexadecimal value. pub(crate) fn flag_names(flags: vk::MemoryPropertyFlags) -> String { + let named = (MEMORY_FLAG_NAMES.iter()).fold(0, |named, (flag, _)| named | flag.as_raw()); + let unnamed = flags.as_raw() & !named; MEMORY_FLAG_NAMES .iter() .filter(|(flag, _)| flags.contains(*flag)) - .map(|(_, name)| *name) + .map(|(_, name)| (*name).to_owned()) + .chain((unnamed != 0).then(|| format!("{unnamed:#x}"))) .collect::>() .join(" | ") } diff --git a/crates/metal2vulkan-runtime/src/tests/floor.rs b/crates/metal2vulkan-runtime/src/tests/floor.rs index 4e2c4a688..1f443cb15 100644 --- a/crates/metal2vulkan-runtime/src/tests/floor.rs +++ b/crates/metal2vulkan-runtime/src/tests/floor.rs @@ -957,62 +957,138 @@ fn the_first_graphics_and_compute_family_is_the_queue_family() { ); } +/// The Vulkan names of the four flags the floor asks for: device-local, host-visible, +/// host-coherent and host-cached. +const NAMES: [&str; 4] = [ + "VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT", + "VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT", + "VK_MEMORY_PROPERTY_HOST_COHERENT_BIT", + "VK_MEMORY_PROPERTY_HOST_CACHED_BIT", +]; + +fn memory_type(flags: &str, offered: String) -> Missing { + Missing::MemoryType { + flags: flags.to_string(), + offered, + } +} + #[test] fn each_missing_memory_type_is_named() { - let shared = Missing::MemoryType { - flags: "VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | \ - VK_MEMORY_PROPERTY_HOST_CACHED_BIT" - .to_string(), - }; - let ring = Missing::MemoryType { - flags: "VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | \ - VK_MEMORY_PROPERTY_HOST_COHERENT_BIT" - .to_string(), - }; + let [local, visible, coherent, cached] = NAMES; + let shared = format!("{visible} | {coherent} | {cached}"); + let ring = format!("{local} | {visible} | {coherent}"); let host_coherent = vk::MemoryPropertyFlags::HOST_VISIBLE | vk::MemoryPropertyFlags::HOST_COHERENT; - let cases = [ - // Host memory that is not cached is not Shared memory. - (vec![DEVICE_LOCAL, host_coherent, RING_MEMORY], &shared), - // Device-local host-visible memory that is not coherent is not the ring's. - ( - vec![ - DEVICE_LOCAL, - SHARED_MEMORY, - DEVICE_LOCAL | vk::MemoryPropertyFlags::HOST_VISIBLE, - ], - &ring, - ), - ]; - for (types, expected) in cases { - let mut device = at_the_floor(); - device.memory = memory(&types); - assert_eq!(&only_missing(&device), expected); - } let mut device = at_the_floor(); + // Host memory that is not cached is not Shared memory. + device.memory = memory(&[DEVICE_LOCAL, host_coherent, RING_MEMORY]); + assert_eq!( + only_missing(&device), + memory_type( + &shared, + format!("type 0 with {local}, type 1 with {visible} | {coherent}, type 2 with {ring}") + ) + ); + // Device-local host-visible memory that is not coherent is not the ring's. + device.memory = memory(&[ + DEVICE_LOCAL, + SHARED_MEMORY, + DEVICE_LOCAL | vk::MemoryPropertyFlags::HOST_VISIBLE, + ]); + assert_eq!( + only_missing(&device), + memory_type( + &ring, + format!("type 0 with {local}, type 1 with {shared}, type 2 with {local} | {visible}") + ) + ); device.memory = memory(&[DEVICE_LOCAL]); let Err(below) = decide(&device) else { panic!("the device is above the floor"); }; - assert_eq!(below.missing, [shared.clone(), ring.clone()]); + let offered = format!("type 0 with {local}"); + assert_eq!( + below.missing, + [ + memory_type(&shared, offered.clone()), + memory_type(&ring, offered) + ] + ); // Vulkan guarantees a device-local type; a device without one is named for it too. device.memory = memory(&[SHARED_MEMORY]); let Err(below) = decide(&device) else { panic!("the device is above the floor"); }; + let offered = format!("type 0 with {shared}"); assert_eq!( below.missing, [ - Missing::MemoryType { - flags: "VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT".to_string(), - }, - ring, + memory_type(local, offered.clone()), + memory_type(&ring, offered) ] ); +} + +/// A refusal for a missing memory type names every type the device offers buffers, so a report +/// of it says what the device has: here a device whose host-visible memory is all uncached, a +/// type with a flag Vulkan does not name and a type with no flags. +#[test] +fn a_missing_memory_type_names_the_types_the_device_offers() { + let host_coherent = DEVICE_LOCAL + | vk::MemoryPropertyFlags::HOST_VISIBLE + | vk::MemoryPropertyFlags::HOST_COHERENT; + let unnamed = vk::MemoryPropertyFlags::from_raw(1 << 12); + let mut device = at_the_floor(); + device.memory = memory(&[ + DEVICE_LOCAL, + host_coherent, + vk::MemoryPropertyFlags::empty(), + DEVICE_LOCAL | unnamed, + ]); + let missing = only_missing(&device); assert_eq!( - shared.to_string(), + missing.to_string(), "a memory type with VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | \ - VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT for buffers" + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT for buffers \ + (the device offers buffers: type 0 with VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, type 1 with \ + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | \ + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, type 2 with no flags, type 3 with \ + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | 0x1000)" + ); + // On the created device, only the types a buffer can use are named. + let [local, visible, coherent, _] = NAMES; + let Err(lacking) = choose(&device.memory, 0b0011) else { + panic!("the device has every memory type"); + }; + assert_eq!( + lacking, + [memory_type( + &flag_names(SHARED_MEMORY), + format!("type 0 with {local}, type 1 with {local} | {visible} | {coherent}") + )] + ); + let Err(lacking) = choose(&device.memory, 0b0100) else { + panic!("the device has every memory type"); + }; + assert_eq!( + (lacking.len(), lacking[0].to_string()), + ( + 3, + "a memory type with VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT for buffers (the device \ + offers buffers: type 2 with no flags)" + .to_string() + ) + ); + let Err(lacking) = choose(&device.memory, 1 << 4) else { + panic!("the device has every memory type"); + }; + assert!( + (lacking.iter()).all(|missing| matches!( + missing, + Missing::MemoryType { offered, .. } if offered == "no type" + )), + "{lacking:?}" ); } @@ -1088,6 +1164,12 @@ fn memory_flags_are_named_as_vulkan_names_them() { "VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | \ VK_MEMORY_PROPERTY_HOST_COHERENT_BIT" ); + // Every flag Vulkan defines has its name; bits past them are one hexadecimal value. + assert_eq!(named.as_raw(), 0x1ff); + assert_eq!( + flag_names(vk::MemoryPropertyFlags::from_raw(0x2 | 0x40 | 0x600)), + "VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD | 0x600" + ); } #[test] @@ -1132,6 +1214,11 @@ fn every_missing_item_is_named_at_once() { Missing::QueueFamily, Missing::MemoryType { flags: flag_names(RING_MEMORY), + offered: format!( + "type 0 with {}, type 1 with {}", + flag_names(DEVICE_LOCAL), + flag_names(SHARED_MEMORY) + ), }, ] ); @@ -1141,7 +1228,10 @@ fn every_missing_item_is_named_at_once() { VK_EXT_robustness2; VK_EXT_calibrated_timestamps; VK_EXT_line_rasterization; VkPhysicalDeviceFeatures.shaderInt64; VkPhysicalDeviceVulkan13Features.dynamicRendering; \ maxColorAttachments 4, below 8; a queue family with graphics and compute; a memory \ type with VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | \ - VK_MEMORY_PROPERTY_HOST_COHERENT_BIT for buffers" + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT for buffers (the device offers buffers: type 0 \ + with VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, type 1 with \ + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | \ + VK_MEMORY_PROPERTY_HOST_CACHED_BIT)" ); } diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 6f50c9297..47d59a381 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -8780,7 +8780,7 @@ variant metal2vulkan-runtime::VisibilityResultType::Reset Reset variant metal2vulkan-runtime::floor::Missing::Extension Extension(&'static CStr) variant metal2vulkan-runtime::floor::Missing::Feature Feature { structure: &'static str, member: String } variant metal2vulkan-runtime::floor::Missing::Limit Limit { name: &'static str, device: u32, needed: u32 } -variant metal2vulkan-runtime::floor::Missing::MemoryType MemoryType { flags: String } +variant metal2vulkan-runtime::floor::Missing::MemoryType MemoryType { flags: String, offered: String } variant metal2vulkan-runtime::floor::Missing::QueueFamily QueueFamily variant metal2vulkan-runtime::floor::Missing::SetLayout SetLayout { set: u32 } variant metal2vulkan-runtime::floor::Missing::TimeDomain TimeDomain(&'static str)