diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index c84fe0ed3..76a0a90e1 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -190,11 +190,14 @@ While a handle lives, equal options give the same device for a physical device, `MTLDevice` per GPU. Nothing is read from the environment but the platform's default cache directory. `Device::registry_ids_with` lists each qualifying physical device's `registryID`, preferred first, without making any device, and `Device::with_registry_id` makes only the device -of one ID (runtime extensions). `Device::system_default_identity_with` reads the identity of the -physical device `Device::system_default_with` makes its device of, with no device made: its name, -kind, IDs, `deviceUUID`, `registryID`, Vulkan version, and its PCI bus address and DRM render node -where the physical device lists `VK_EXT_pci_bus_info` and `VK_EXT_physical_device_drm`, which a -consumer addresses the same GPU through another API by. The same two are +of one ID (runtime extensions). `Device::identities_with` reads the identity of each of those +physical devices, in the same order, with no device made, and `Device::system_default_identity_with` +the first of them, the one `Device::system_default_with` makes its device of: its name, kind, IDs, +`deviceUUID`, `registryID`, Vulkan version, the size of its largest `DEVICE_LOCAL` memory heap +(`recommended_max_working_set_size`, named after Metal's nearest property, whose estimate it is +not), and its PCI bus address and DRM render node where the physical device lists +`VK_EXT_pci_bus_info` and `VK_EXT_physical_device_drm`, which a consumer addresses the same GPU +through another API by. The same two are `DeviceProperties::pci_bus` and `DeviceProperties::drm_render` on a device that exists; both are `None` on macOS, where MoltenVK lists neither extension. diff --git a/crates/metal2vulkan-runtime/src/capabilities.rs b/crates/metal2vulkan-runtime/src/capabilities.rs index 95ac9da5b..23ee9f22a 100644 --- a/crates/metal2vulkan-runtime/src/capabilities.rs +++ b/crates/metal2vulkan-runtime/src/capabilities.rs @@ -235,7 +235,8 @@ pub(crate) struct DeviceIdentity { /// `sampledImageColorSampleCounts & sampledImageDepthSampleCounts`. pub sampled_sample_counts: SampleCountFlags, /// The size of the device's largest `DEVICE_LOCAL` memory heap, where it was read - /// (`VkPhysicalDeviceMemoryProperties`): what bounds [`DeviceProperties::max_buffer_length`]. + /// (`VkPhysicalDeviceMemoryProperties`): what bounds [`DeviceProperties::max_buffer_length`], + /// and what `PhysicalDeviceIdentity::recommended_max_working_set_size` answers. pub device_local_heap: Option, } diff --git a/crates/metal2vulkan-runtime/src/device/create.rs b/crates/metal2vulkan-runtime/src/device/create.rs index 811b2b35d..68ceb8307 100644 --- a/crates/metal2vulkan-runtime/src/device/create.rs +++ b/crates/metal2vulkan-runtime/src/device/create.rs @@ -2164,17 +2164,11 @@ impl Device { } /// The identity of the physical device [`Device::system_default_with`] makes its device of - /// with `options`, read without making any device: the same selection, in the same order (the - /// preferred qualifying kind, then enumeration order), over the same physical devices, by the - /// same calls (`ranked`, `identified`, `properties::query`), so the two cannot name - /// different physical devices. A runtime extension, no Metal call (Metal's program holds an - /// `MTLDevice` to ask it anything). - /// - /// What is read is the physical device's own identity, which the device made of it answers: - /// `properties::query`'s property reads of the physical device, and no `vkCreateDevice`, no - /// compile device and no queue. The instance is the one of the devices of equal options while - /// one lives (as [`Device::system_default_with`] takes it), else one made for the call, which - /// goes with it. + /// with `options`, read without making any device: the first of + /// [`Device::identities_with`]'s, whose order (the preferred qualifying kind, then + /// enumeration order) and physical devices are the ones that call tries, so the two cannot + /// name different physical devices. A runtime extension, no Metal call (Metal's program holds + /// an `MTLDevice` to ask it anything). /// /// The physical device named is the one [`Device::system_default_with`] tries first. Where /// that call then refuses it as it makes its device (unsupported, or without a graphics and @@ -2189,41 +2183,58 @@ impl Device { pub fn system_default_identity_with( options: &DeviceOptions, ) -> Result { - let mut made = lock(&MADE); - let (_, instance) = group(&mut made, options)?; - let ranked = ranked(&instance, options)?; - let identity = (identified(&instance, ranked).into_iter().next()).map(|(_, physical)| { - // SAFETY: `physical` was enumerated from the instance, created at Vulkan 1.2 or - // later, and `identified` kept only physical devices at Vulkan 1.2 or later. - let (identity, _) = - unsafe { super::properties::query(&instance.raw, physical, instance.api_version) }; - PhysicalDeviceIdentity::of(&identity) - }); - drop(made); - // The instance made for the call goes here, outside the lock. - drop(instance); - identity.ok_or(Error::NoDevice) + let first = Device::identities_with(options)?.into_iter().next(); + first.ok_or(Error::NoDevice) } /// The registry IDs ([`Device::registry_id`], `registryID`) of the physical devices /// [`Device::all_with`] makes its devices of, in its order, read without making any device: a - /// runtime extension, no Metal call (Metal lists its devices, not their IDs). The physical - /// devices of `options.kinds` at Vulkan 1.2 or later are listed, each by its `deviceUUID`; one - /// the runtime refuses as it makes its device (unsupported, or without a graphics and compute + /// runtime extension, no Metal call (Metal lists its devices, not their IDs). They are the + /// [`PhysicalDeviceIdentity::registry_id`]s of [`Device::identities_with`], one per physical + /// device it lists: one the runtime refuses as it makes its device (unsupported, or without a + /// graphics and compute queue), which [`Device::all_with`] leaves out, is listed all the + /// same, and [`Device::with_registry_id`] answers its refusal. + pub fn registry_ids_with(options: &DeviceOptions) -> Result, Error> { + let identities = Device::identities_with(options)?.into_iter(); + Ok(identities.map(|identity| identity.registry_id).collect()) + } + + /// The identity of each physical device `options` admit, in [`Device::system_default_with`]'s + /// order (the preferred qualifying kind, then enumeration order), read without making any + /// device. No Metal call: a runtime extension, + /// whose nearest Metal counterpart is `MTLCopyAllDevices`, which answers devices that exist + /// where this answers what a device of each physical device would answer, before any is + /// made, so a consumer can choose a GPU and make only its device + /// ([`Device::with_registry_id`]). + /// + /// The physical devices of `options.kinds` at Vulkan 1.2 or later are listed. Each identity + /// is read by the property reads the device made of it takes its own from + /// (`properties::query`), with no `vkCreateDevice`, no compile device and no queue. One the + /// runtime refuses as it makes its device (unsupported, or without a graphics and compute /// queue), which [`Device::all_with`] leaves out, is listed all the same, and /// [`Device::with_registry_id`] answers its refusal. The instance is the one of the devices of /// equal options while one lives (as [`Device::system_default_with`] takes it), else one made - /// for the call, which goes with it. - pub fn registry_ids_with(options: &DeviceOptions) -> Result, Error> { + /// for the call, which goes with it. [`Device::registry_ids_with`] and + /// [`Device::system_default_identity_with`] answer from this list. + pub fn identities_with(options: &DeviceOptions) -> Result, Error> { let mut made = lock(&MADE); let (_, instance) = group(&mut made, options)?; let ranked = ranked(&instance, options)?; - let ids = identified(&instance, ranked).into_iter().map(|(id, _)| id); - let ids = ids.collect(); + let identities = identified(&instance, ranked) + .into_iter() + .map(|(_, physical)| { + // SAFETY: `physical` was enumerated from the instance, created at Vulkan 1.2 or + // later, and `identified` kept only physical devices at Vulkan 1.2 or later. + let (identity, _) = unsafe { + super::properties::query(&instance.raw, physical, instance.api_version) + }; + PhysicalDeviceIdentity::of(&identity) + }); + let identities = identities.collect(); drop(made); // The instance made for the call goes here, outside the lock. drop(instance); - Ok(ids) + Ok(identities) } /// The device of the physical device whose registry ID ([`Device::registry_id`]) is diff --git a/crates/metal2vulkan-runtime/src/device/properties.rs b/crates/metal2vulkan-runtime/src/device/properties.rs index 1fa531d8b..c4f4b49bd 100644 --- a/crates/metal2vulkan-runtime/src/device/properties.rs +++ b/crates/metal2vulkan-runtime/src/device/properties.rs @@ -3,8 +3,8 @@ //! queries answer from (`capabilities.rs`). //! //! The same read answers [`PhysicalDeviceIdentity`] of a physical device no device was made of -//! ([`Device::system_default_identity_with`](crate::Device::system_default_identity_with)), so the -//! identity a consumer reads before making a device is the one the device then answers. +//! ([`Device::identities_with`](crate::Device::identities_with)), so the identity a consumer reads +//! before making a device is the one the device then answers. use crate::capabilities::DeviceIdentity; use ash::vk; @@ -72,12 +72,14 @@ pub(crate) fn registry_id(uuid: &[u8; 16]) -> u64 { } /// A physical device's identity, read without making a device -/// ([`Device::system_default_identity_with`](crate::Device::system_default_identity_with)). +/// ([`Device::identities_with`](crate::Device::identities_with), +/// [`Device::system_default_identity_with`](crate::Device::system_default_identity_with)). /// /// No Metal counterpart: Metal's program holds an `MTLDevice` and asks it, so every one of these /// is a property of a device that exists. The runtime answers them of the physical device alone, -/// so a consumer can tell which GPU it is about to get before paying for it. Every field is the -/// one the device made of that physical device answers. +/// so a consumer can tell which GPU it is about to get before paying for it. Every field but +/// [`recommended_max_working_set_size`](Self::recommended_max_working_set_size), which no device +/// property answers, is the one the device made of that physical device answers. #[non_exhaustive] #[derive(Clone, Debug, PartialEq, Eq)] pub struct PhysicalDeviceIdentity { @@ -112,6 +114,18 @@ pub struct PhysicalDeviceIdentity { /// The physical device's DRM render node /// ([`DeviceProperties::drm_render`](crate::DeviceProperties::drm_render)). pub drm_render: Option<(i64, i64)>, + /// The size in bytes of the physical device's largest `DEVICE_LOCAL` memory heap + /// (`VkPhysicalDeviceMemoryProperties::memoryHeaps`), as its Vulkan driver reports it; the + /// device's [`DeviceProperties::max_buffer_length`](crate::DeviceProperties::max_buffer_length) + /// is at most three quarters of it, to 16 KiB. Named after Metal's nearest counterpart, + /// `recommendedMaxWorkingSetSize`, which it is not: Metal's is its estimate of the memory the + /// GPU can use without hurting its performance, and this is the heap's whole size, fixed for + /// the physical device, with nothing taken off for the system or for other processes' use + /// (what `VK_EXT_memory_budget` would report) and no device made to read it. On a discrete + /// GPU the heap is its own memory; on one of unified memory, the share of the host's memory + /// its driver reports. No device property answers it. `None` only where the driver reports + /// no `DEVICE_LOCAL` heap, which Vulkan requires one of. + pub recommended_max_working_set_size: Option, } impl PhysicalDeviceIdentity { @@ -128,6 +142,7 @@ impl PhysicalDeviceIdentity { api_version: identity.api_version, pci_bus: identity.pci_bus, drm_render: identity.drm_render, + recommended_max_working_set_size: identity.device_local_heap, } } } diff --git a/crates/metal2vulkan-runtime/src/tests/device_registry.rs b/crates/metal2vulkan-runtime/src/tests/device_registry.rs index 87ec6c867..241947551 100644 --- a/crates/metal2vulkan-runtime/src/tests/device_registry.rs +++ b/crates/metal2vulkan-runtime/src/tests/device_registry.rs @@ -1,13 +1,14 @@ //! Devices by registry ID (`Device::registry_ids_with`, `Device::with_registry_id`): the IDs are //! listed with no device made, the device of an ID is the one the other creators make of its //! physical device (the same device while it lives), and an ID no physical device has is -//! `NoDevice`. The identity of the physical device the default device is made of -//! (`Device::system_default_identity_with`) is read with no device made too, and is the one that -//! device answers. +//! `NoDevice`. The identities of the physical devices (`Device::identities_with`), and of the one +//! the default device is made of (`Device::system_default_identity_with`), are read with no device +//! made too, and each is the one its device answers. //! //! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, //! `m2v_cpu_icd`), on the kind of device the cfg selects. Nothing runs on the GPU. +use crate::capabilities::working_set_buffer_bound; use crate::device::create::live_devices_with; use crate::{Device, DeviceKind, DeviceOptions, Error, MessageSink}; @@ -101,3 +102,55 @@ fn the_default_device_s_identity_is_read_with_no_device_made_and_is_the_device_s ); } } + +#[test] +fn every_listed_identity_is_read_with_no_device_made_and_is_its_device_s_own() { + let options = options(); + let identities = Device::identities_with(&options).unwrap(); + assert!(!identities.is_empty(), "no physical device listed"); + let ids: Vec = identities + .iter() + .map(|identity| identity.registry_id) + .collect(); + assert_eq!(ids, Device::registry_ids_with(&options).unwrap()); + let first = Device::system_default_identity_with(&options).unwrap(); + assert_eq!(first, identities[0]); + assert_eq!( + live_devices_with(&options), + 0, + "reading the identities made a device" + ); + let mut opened = 0; + for identity in &identities { + eprintln!("listed: {identity:?}"); + // Vulkan requires a `DEVICE_LOCAL` heap of every physical device. + let heap = identity.recommended_max_working_set_size.unwrap(); + let device = match Device::with_registry_id(identity.registry_id, &options) { + Ok(device) => device, + // Listed all the same where the runtime refuses its device, which this answers. + Err(error @ (Error::Unsupported(_) | Error::NoDevice)) => { + eprintln!("refused: {error}"); + continue; + } + Err(error) => panic!("{}: {error}", identity.name), + }; + let properties = device.properties(); + assert_eq!(device.registry_id(), identity.registry_id); + assert_eq!(properties.device_uuid, identity.device_uuid); + assert_eq!(properties.name, identity.name); + assert_eq!(properties.kind, identity.kind); + assert_eq!(properties.vendor_id, identity.vendor_id); + assert_eq!(properties.device_id, identity.device_id); + assert_eq!(properties.api_version, identity.api_version); + assert_eq!(properties.pci_bus, identity.pci_bus); + assert_eq!(properties.drm_render, identity.drm_render); + // The heap no device property answers bounds the one that does. + assert!( + properties.max_buffer_length <= working_set_buffer_bound(heap), + "maxBufferLength {} past three quarters of the heap {heap}", + properties.max_buffer_length + ); + opened += 1; + } + assert!(opened > 0, "the runtime made no device of {identities:?}"); +} diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 10d041f5d..67a742826 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2285,6 +2285,7 @@ field metal2vulkan-runtime::PhysicalDeviceIdentity.drm_render pub drm_render: Op field metal2vulkan-runtime::PhysicalDeviceIdentity.kind pub kind: DeviceKind field metal2vulkan-runtime::PhysicalDeviceIdentity.name pub name: String field metal2vulkan-runtime::PhysicalDeviceIdentity.pci_bus pub pci_bus: Option<(u32, u32, u32, u32)> +field metal2vulkan-runtime::PhysicalDeviceIdentity.recommended_max_working_set_size pub recommended_max_working_set_size: Option field metal2vulkan-runtime::PhysicalDeviceIdentity.registry_id pub registry_id: u64 field metal2vulkan-runtime::PhysicalDeviceIdentity.vendor_id pub vendor_id: u32 field metal2vulkan-runtime::PipelineOptions.binary_archives pub binary_archives: Vec @@ -7971,6 +7972,7 @@ method metal2vulkan-runtime::Device::from_raw pub unsafe fn from_raw(raw: RawDev method metal2vulkan-runtime::Device::heap_buffer_size_and_align pub fn heap_buffer_size_and_align(&self, length: u64, _options: ResourceOptions) -> SizeAndAlign method metal2vulkan-runtime::Device::heap_texture_size_and_align pub fn heap_texture_size_and_align(&self, descriptor: &TextureDescriptor) -> Result method metal2vulkan-runtime::Device::host_range_remapped pub fn host_range_remapped(&self, bytes: NonNull, length: u64) +method metal2vulkan-runtime::Device::identities_with pub fn identities_with(options: &DeviceOptions) -> Result, Error> method metal2vulkan-runtime::Device::imports_shared_host_pointers pub fn imports_shared_host_pointers(&self) -> Result method metal2vulkan-runtime::Device::in_place_check_line pub fn in_place_check_line(&self) -> u64 method metal2vulkan-runtime::Device::in_place_check_report pub fn in_place_check_report(&self) -> Vec