diff --git a/crates/metal2vulkan-runtime/src/capabilities.rs b/crates/metal2vulkan-runtime/src/capabilities.rs index 41928fddf..6327374d0 100644 --- a/crates/metal2vulkan-runtime/src/capabilities.rs +++ b/crates/metal2vulkan-runtime/src/capabilities.rs @@ -107,10 +107,16 @@ pub struct DeviceProperties { pub drm_render: Option<(i64, i64)>, /// The Vulkan version the device runs at: the lower of the instance's and the device's. pub api_version: ApiVersion, - /// `maxBufferLength`: the longest buffer a shader can bind whole, the compile layer's answer: - /// `maxStorageBufferRange` where buffers are reached through descriptors only, else the lower - /// of `maxBufferSize` and `maxMemoryAllocationSize` (the device's allocation sizes, which - /// follow its machine's memory on integrated GPUs, as Metal's answer does). + /// `maxBufferLength`: the longest buffer a shader can bind whole, the compile layer's answer + /// (`maxStorageBufferRange` where buffers are reached through descriptors only, else the lower + /// of `maxBufferSize` and `maxMemoryAllocationSize`), at most three quarters of the device's + /// largest device-local heap, rounded down to 16 KiB ([`working_set_buffer_bound`]). Metal + /// answers three quarters of its `recommendedMaxWorkingSetSize`: 3,208,642,560 of 4,278,190,080 + /// on an AMD Radeon Pro 5300M (4 GiB, macOS 26.7.1), and 41,747,087,360 of 55,662,788,608, to + /// 16 KiB, on an Apple M3 Max (64 GiB, macOS 27.0.1); the runtime holds no working set size and + /// takes the heap for it, so a discrete GPU's answer follows its memory, as Metal's does, where + /// its Vulkan limits alone allow far more (`maxBufferSize` 1 TiB on NVIDIA's 615.71.09). On + /// MoltenVK the bound is above the compile layer's answer, Metal's own. pub max_buffer_length: u64, /// `maxThreadsPerThreadgroup`: per axis, `maxComputeWorkGroupSize` capped by /// `maxComputeWorkGroupInvocations` and by an Apple GPU's 1024. @@ -197,6 +203,16 @@ pub(crate) struct DeviceIdentity { /// The sample counts every color and depth image can be sampled at: /// `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`]. + pub device_local_heap: Option, +} + +/// The longest buffer Metal's rule allows a device whose working set is `working_set` bytes: +/// three quarters of it, rounded down to 16 KiB ([`DeviceProperties::max_buffer_length`]). +pub(crate) fn working_set_buffer_bound(working_set: u64) -> u64 { + const PAGE: u64 = 16 << 10; + working_set / 4 * 3 / PAGE * PAGE } /// The class of Mac a device presents, whose GPU decides answers Metal gives without a property @@ -264,6 +280,11 @@ impl DeviceProperties { ) -> DeviceProperties { let lossless_compression = presents_lossless_compression(&identity, mac_class); let metal = compile.metal_properties(); + let max_buffer_length = identity + .device_local_heap + .map_or(metal.max_buffer_length, |heap| { + metal.max_buffer_length.min(working_set_buffer_bound(heap)) + }); let [width, height, depth] = metal.max_threads_per_threadgroup.map(u64::from); let texture_sample_counts = metal.raster_sample_counts & identity.sampled_sample_counts; let max_vertex_amplification_count = if description.has(VkFeature::Multiview) { @@ -282,7 +303,7 @@ impl DeviceProperties { pci_bus: identity.pci_bus, drm_render: identity.drm_render, api_version: identity.api_version, - max_buffer_length: metal.max_buffer_length, + max_buffer_length, max_threads_per_threadgroup: Size::new(width, height, depth), max_threadgroup_memory_length: u64::from(metal.max_threadgroup_memory_length), argument_buffers_support: metal.argument_buffers_tier, diff --git a/crates/metal2vulkan-runtime/src/device/properties.rs b/crates/metal2vulkan-runtime/src/device/properties.rs index e97e8b334..f9bb84e84 100644 --- a/crates/metal2vulkan-runtime/src/device/properties.rs +++ b/crates/metal2vulkan-runtime/src/device/properties.rs @@ -152,6 +152,15 @@ pub(super) unsafe fn query( let core = properties.properties; // SAFETY: the caller's contract. let (pci_bus, drm_render) = unsafe { bus_addresses(instance, physical) }; + // SAFETY: the caller's contract. + let memory = unsafe { instance.get_physical_device_memory_properties(physical) }; + let heaps = + &memory.memory_heaps[..(memory.memory_heap_count as usize).min(vk::MAX_MEMORY_HEAPS)]; + let device_local_heap = heaps + .iter() + .filter(|heap| heap.flags.contains(vk::MemoryHeapFlags::DEVICE_LOCAL)) + .map(|heap| heap.size) + .max(); let identity = DeviceIdentity { name: core .device_name_as_c_str() @@ -173,6 +182,7 @@ pub(super) unsafe fn query( & core.limits.sampled_image_depth_sample_counts) .as_raw(), ), + device_local_heap, }; (identity, v11.max_memory_allocation_size) } diff --git a/crates/metal2vulkan-runtime/src/tests/capabilities.rs b/crates/metal2vulkan-runtime/src/tests/capabilities.rs index 8b540fc73..4f1af2e11 100644 --- a/crates/metal2vulkan-runtime/src/tests/capabilities.rs +++ b/crates/metal2vulkan-runtime/src/tests/capabilities.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; use crate::capabilities::{ amplification_count_supported, rate_map_layers_supported, sample_count_supported, - DeviceIdentity, RATE_MAP_LAYERS, + working_set_buffer_bound, DeviceIdentity, RATE_MAP_LAYERS, }; use crate::{ BufferTextureProblem, DeviceKind, DeviceProperties, Error, Invalid, MacClass, Problem, Query, @@ -81,6 +81,7 @@ fn identity(stem: &str) -> DeviceIdentity { gpu_core_count: None, sampled_sample_counts: profile_counts(stem, "sampledImageColorSampleCounts") & profile_counts(stem, "sampledImageDepthSampleCounts"), + device_local_heap: None, } } @@ -176,6 +177,44 @@ fn lossless_compression_is_the_presented_mac_class() { } } +/// Metal's rule for `maxBufferLength`, three quarters of the working set to 16 KiB, gives what +/// Metal answered: an AMD Radeon Pro 5300M (4 GiB, macOS 26.7.1) of `recommendedMaxWorkingSetSize` +/// 4,278,190,080 and an Apple M3 Max (64 GiB, macOS 27.0.1) of 55,662,788,608. A device's answer +/// is the compile layer's bound by its largest device-local heap's (a 16 GiB heap's 12 GiB, a heap +/// of the limit's size three quarters of it), and the compile layer's where the bound lies above +/// it or no heap was read. +#[test] +fn max_buffer_length_is_three_quarters_of_the_device_local_heap_at_most() { + assert_eq!(working_set_buffer_bound(4_278_190_080), 3_208_642_560); + assert_eq!(working_set_buffer_bound(55_662_788_608), 41_747_087_360); + let (stem, desc) = &committed_descriptions()[0]; + let policy = DevicePolicy::UNTRUSTED; + let enabled = desc + .clone() + .with_enabled(&desc.recommended_enables(&policy)) + .unwrap_or_else(|e| panic!("{stem}: {e}")); + let compile = Device::new(enabled.clone(), policy).unwrap_or_else(|e| panic!("{stem}: {e}")); + let limit = compile.metal_properties().max_buffer_length; + for heap in [ + None, + Some(16 << 30), + Some(64 << 30), + Some(limit), + Some(u64::MAX), + ] { + let identity = DeviceIdentity { + device_local_heap: heap, + ..identity(stem) + }; + let p = DeviceProperties::new(identity, enabled.clone(), &compile, None); + let want = heap.map_or(limit, |heap| { + limit.min(heap / 4 * 3 / (16 << 10) * (16 << 10)) + }); + assert_eq!(p.max_buffer_length, want, "{stem}: heap {heap:?}"); + assert!(p.max_buffer_length <= limit, "{stem}: heap {heap:?}"); + } +} + /// Every answer equals its source on every committed profile: the Vulkan limit, feature or /// format the device was created with, or the compile layer's answer for it, an Apple GPU's at /// most. diff --git a/crates/metal2vulkan-runtime/src/tests/device.rs b/crates/metal2vulkan-runtime/src/tests/device.rs index 13b0e5953..de79adc03 100644 --- a/crates/metal2vulkan-runtime/src/tests/device.rs +++ b/crates/metal2vulkan-runtime/src/tests/device.rs @@ -39,6 +39,7 @@ fn identity( max_viewports: 16, gpu_core_count: None, sampled_sample_counts: SampleCountFlags::empty(), + device_local_heap: None, } } diff --git a/crates/metal2vulkan-validation/src/runtime_lane/capabilities.rs b/crates/metal2vulkan-validation/src/runtime_lane/capabilities.rs index ad62929d6..997fa1690 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/capabilities.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/capabilities.rs @@ -125,7 +125,20 @@ fn a_runtime_device_answers_its_queries_and_the_apple_only_calls() { assert_eq!(p.vendor_id, d.vendor_id); assert_eq!(p.device_id, d.device_id); assert_eq!(p.api_version, d.api_version); - assert_eq!(p.max_buffer_length, metal.max_buffer_length); + // The compile layer's answer, at most three quarters of the device's largest device-local + // heap, to 16 KiB (`DeviceProperties::max_buffer_length`). + println!( + "CAPABILITIES {}: max_buffer_length {} of the compile layer's {}", + p.name, p.max_buffer_length, metal.max_buffer_length + ); + assert!( + p.max_buffer_length == metal.max_buffer_length + || (p.max_buffer_length < metal.max_buffer_length + && p.max_buffer_length.is_multiple_of(16 << 10)), + "max_buffer_length {} against the compile layer's {}", + p.max_buffer_length, + metal.max_buffer_length + ); let [width, height, depth] = metal.max_threads_per_threadgroup.map(u64::from); assert_eq!( p.max_threads_per_threadgroup,