diff --git a/crates/metal2vulkan-runtime/src/device.rs b/crates/metal2vulkan-runtime/src/device.rs index 11cdfaff3..8cccdb641 100644 --- a/crates/metal2vulkan-runtime/src/device.rs +++ b/crates/metal2vulkan-runtime/src/device.rs @@ -477,6 +477,12 @@ pub struct DeviceOptions { /// same. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] pub(crate) described: Option DeviceDescription>, + /// For the device tests: the device's memory types and heaps as the runtime is to read them, + /// in place of the device's own answers, from the floor's memory types on; the floor's check + /// of the physical device reads the device's own. The Vulkan device is the same, so a view + /// only takes flags away from a type or leaves it. + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + pub(crate) memory: Option, } /// Takes a lock. A panic while another thread held it leaves what it guards usable (a queue, the @@ -521,6 +527,15 @@ impl Device { let vk_instance = instance.vk(); let raw = physical.raw(); let capabilities = physical.check().map_err(Error::BelowFloor)?; + #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] + let capabilities = match options.memory { + Some(view) => { + let mut capabilities = capabilities; + view(&mut capabilities.memory); + capabilities + } + None => capabilities, + }; // SAFETY: `raw` is the instance's, which was made at the floor's version. let description = unsafe { description(vk_instance, raw, &capabilities) }?; #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] diff --git a/crates/metal2vulkan-runtime/src/floor.rs b/crates/metal2vulkan-runtime/src/floor.rs index 456143681..90ecef2f6 100644 --- a/crates/metal2vulkan-runtime/src/floor.rs +++ b/crates/metal2vulkan-runtime/src/floor.rs @@ -1528,6 +1528,17 @@ pub(crate) unsafe fn memory_types( device: &ash::Device, capabilities: &Capabilities, ) -> Result { + // SAFETY: the caller's contract. + let usable = unsafe { buffer_memory_bits(device) }; + choose(&capabilities.memory, usable).map_err(|missing| BelowFloor { missing }) +} + +/// The memory types a buffer of [`BUFFER_USAGE`] can use on `device`, as its `memoryTypeBits`. +/// +/// # Safety +/// +/// `device` was created with the floor's features. +pub(crate) unsafe fn buffer_memory_bits(device: &ash::Device) -> u32 { let buffer = vk::BufferCreateInfo::default() .size(65_536) .usage(BUFFER_USAGE) @@ -1535,13 +1546,9 @@ pub(crate) unsafe fn memory_types( let question = vk::DeviceBufferMemoryRequirements::default().create_info(&buffer); let mut answer = vk::MemoryRequirements2::default(); // SAFETY: the caller's contract; `BUFFER_USAGE`'s device addresses need - // `bufferDeviceAddress`, which the floor requires and `capabilities.features` enables. + // `bufferDeviceAddress`, which the floor requires. unsafe { device.get_device_buffer_memory_requirements(&question, &mut answer) }; - choose( - &capabilities.memory, - answer.memory_requirements.memory_type_bits, - ) - .map_err(|missing| BelowFloor { missing }) + answer.memory_requirements.memory_type_bits } /// For each of [`MemoryTypes`], the first memory type with its flags whose bit is set in `usable`, diff --git a/crates/metal2vulkan-runtime/src/tests/floor.rs b/crates/metal2vulkan-runtime/src/tests/floor.rs index 1f443cb15..0982ac229 100644 --- a/crates/metal2vulkan-runtime/src/tests/floor.rs +++ b/crates/metal2vulkan-runtime/src/tests/floor.rs @@ -1,6 +1,7 @@ //! The floor's check: each missing item is named, the optional features decide //! `Capabilities`, memory types are chosen by flags and by what buffers can use, the README states -//! what the code checks, and, under a device cfg, every device the loader lists is above the floor. +//! what the code checks, and, under a device cfg, every device the loader lists is above the floor +//! and is refused through a memory view that leaves it no host-cached, host-coherent type. use crate::floor::{ choose, decide, flag_names, limits, vulkan_name, BelowFloor, Features, IndirectCommandLimits, @@ -2372,6 +2373,65 @@ fn every_device_the_loader_lists_is_above_the_floor() { assert!(below.is_empty(), "below the floor: {below:#?}"); } +/// Every device of the cfg's kind, read through a memory view that takes `HOST_COHERENT` from +/// each type that is both host-cached and host-coherent (so the device lists its types as a +/// Meteor Lake GPU does under Mesa's Intel driver and the i915 kernel driver), is refused at its +/// make. The refusal is the floor's choice over the view: it names the flags each missing type +/// needs and the types the device offers a buffer, with the view's flags. The device made with +/// no view first answers which types such a buffer can use. +#[cfg(any(m2v_gpu, m2v_cpu_icd))] +#[test] +fn a_device_with_no_cached_coherent_type_is_refused_naming_its_types() { + use crate::{Device, DeviceOptions, Error}; + + /// The view: no type is both host-cached and host-coherent. + fn cached_types_incoherent(memory: &mut vk::PhysicalDeviceMemoryProperties) { + let both = vk::MemoryPropertyFlags::HOST_CACHED | vk::MemoryPropertyFlags::HOST_COHERENT; + let count = memory.memory_type_count as usize; + for kind in &mut memory.memory_types[..count] { + if kind.property_flags.contains(both) { + kind.property_flags &= !vk::MemoryPropertyFlags::HOST_COHERENT; + } + } + } + + let runtime = crate::tests::validation::instance(&[]); + let (_, vk_instance) = runtime.raw(); + for (name, physical) in crate::tests::devices_of_the_cfgs_kind(&runtime) { + let device = Device::new(&physical, DeviceOptions::default()) + .unwrap_or_else(|error| panic!("{name}: {error}")); + // SAFETY: the device is live, made with the floor's features. + let usable = unsafe { crate::floor::buffer_memory_bits(device.raw().device) }; + drop(device); + // SAFETY: `physical` is one of the instance's. + let mut memory = + unsafe { vk_instance.get_physical_device_memory_properties(physical.raw()) }; + cached_types_incoherent(&mut memory); + let Err(missing) = choose(&memory, usable) else { + panic!("{name}: the view leaves a type with every flag the floor asks"); + }; + assert!( + (missing.iter()).any(|missing| matches!( + missing, + Missing::MemoryType { flags, .. } if *flags == flag_names(SHARED_MEMORY) + )), + "{name}: {missing:?}" + ); + let options = DeviceOptions { + memory: Some(cached_types_incoherent), + ..DeviceOptions::default() + }; + match Device::new(&physical, options) { + Err(Error::BelowFloor(below)) => { + assert_eq!(below, BelowFloor { missing }, "{name}"); + println!("floor: {name} under the view: {}", Error::BelowFloor(below)); + } + Err(error) => panic!("{name}: not the floor's refusal: {error}"), + Ok(_) => panic!("{name}: made under a view with no cached coherent type"), + } + } +} + /// A device that does not calibrate the host clock's time domain with its own is below the floor, /// named for the domain; one that calibrates neither is named for both, its own first. #[test]