From 71eb800ff8d5cdeeaf856cdc9ff049ec0f057cfd Mon Sep 17 00:00:00 2001 From: Garvit Surana <251430630+garvitsurana271@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:46:24 +0000 Subject: [PATCH] fix(gpu): cap each image array of the Metal descriptor heap at 4,194,304 descriptors, texel buffers included, where ANV on Gen9 to Gen12.0 Intel GPUs reported 201 million images, the heap's one pool asked for 203 million descriptors (9.1 GiB of host memory) and Metal could not open with OutOfMemory --- crates/stela-driver-gpu/src/metal/binding.rs | 50 ++++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/crates/stela-driver-gpu/src/metal/binding.rs b/crates/stela-driver-gpu/src/metal/binding.rs index b9bc69fed2..d4aa60fb7f 100644 --- a/crates/stela-driver-gpu/src/metal/binding.rs +++ b/crates/stela-driver-gpu/src/metal/binding.rs @@ -1193,21 +1193,30 @@ impl Writer<'_> { } /// Descriptor counts of each heap array, within the device's -/// update-after-bind limits. Texel buffers count against the image limits, -/// so they take a quarter of each. +/// update-after-bind limits and [`Self::MAX_IMAGES`]. Texel buffers count +/// against the image limits, so they take a quarter of each. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct HeapLayout { pub counts: [u32; 5], } impl HeapLayout { + /// The most sampled or storage descriptors the heap takes, texel buffers + /// included: eight times Apple's tier 2 count of 500 000. The heap is one + /// pool, and Intel's Vulkan driver on Gen9 to Gen12.0 GPUs reports 201 + /// million images, which its pool keeps 48 host bytes each for: 9.1 GiB + /// in one allocation, which no PC holds. + pub const MAX_IMAGES: u32 = 1 << 22; + pub fn new(limits: &Limits) -> Self { let sampled = limits .max_descriptor_set_update_after_bind_sampled_images - .min(limits.max_per_stage_update_after_bind_sampled_images); + .min(limits.max_per_stage_update_after_bind_sampled_images) + .min(Self::MAX_IMAGES); let storage = limits .max_descriptor_set_update_after_bind_storage_images - .min(limits.max_per_stage_update_after_bind_storage_images); + .min(limits.max_per_stage_update_after_bind_storage_images) + .min(Self::MAX_IMAGES); let samplers = limits .max_descriptor_set_update_after_bind_samplers .min(limits.max_per_stage_update_after_bind_samplers) @@ -2590,4 +2599,37 @@ mod tests { assert_eq!(heap.in_use(HeapArray::Samplers), 1); assert_eq!(heap.in_use(HeapArray::SampledImages), 0); } + + /// ANV on a Kaby Lake GPU (issue 8's ThinkPad T480s) reports 3 GiB of + /// 16-byte image descriptors and 64K samplers; uncapped, its one pool + /// asked for 203 489 280 descriptors, 9.1 GiB of 48-byte host records, + /// and Metal failed to open with `OutOfMemory`. + #[test] + fn anv_kaby_lake_heap_fits_in_host_memory() { + let images = 3 << 26; + let mut limits = nvk(&CHIPS[0], 8 << 30).limits; + limits.max_per_stage_update_after_bind_sampled_images = images; + limits.max_per_stage_update_after_bind_storage_images = images; + limits.max_descriptor_set_update_after_bind_sampled_images = images; + limits.max_descriptor_set_update_after_bind_storage_images = images; + limits.max_per_stage_update_after_bind_samplers = 3 << 27; + limits.max_descriptor_set_update_after_bind_samplers = 3 << 27; + limits.max_sampler_allocation_count = 64 * 1024; + let layout = HeapLayout::new(&limits); + assert_eq!( + layout.counts, + [3_145_728, 1_572_864, 65_536, 1_048_576, 524_288] + ); + assert!(layout.count(HeapArray::SampledImages) >= 500_000); + assert!(layout.count(HeapArray::StorageImages) >= 500_000); + // `sizeof(struct anv_descriptor)` host bytes per descriptor. + let host: u64 = layout + .counts + .iter() + .map(|&count| u64::from(count) * 48) + .sum(); + assert_eq!(host, 305_135_616); + let mut heap = DescriptorHeap::new(layout); + assert_eq!(heap.allocate(HeapArray::SampledImages), Ok(1)); + } }