From f76801d8c19426223bbbbebd11052d3af673496b Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Mon, 23 Mar 2026 10:38:33 -0700 Subject: [PATCH] fix(metal): Check respondsToSelector before feature detection calls (#9284) These methods are not implemented by `CaptureMTLDevice`. The actual calls succeed, because the exception is caught and handled by a fallback mechanism, but the valid-method checks by objc2 in debug builds are not aware of the fallback and panic. Fixes #9282 --- CHANGELOG.md | 4 ++++ wgpu-hal/src/metal/adapter.rs | 26 +++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 516500663..839f719b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,10 @@ Bottom level categories: - Fixed overflow detection and argument domain validation for `acosh`, `length`, `normalize`, and `pow` in constant evaluation. By @ecoricemon in [#9249](https://github.com/gfx-rs/wgpu/pull/9249). +#### Metal + +- Added guards to avoid calling some feature detection methods that are not implemented on `CaptureMTLDevice`. By @andyleiserson in [#9284](https://github.com/gfx-rs/wgpu/pull/9284). + ## v29.0.0 (2026-03-18) ### Major Changes diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index df2904d8a..c8b165e82 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -1,4 +1,5 @@ -use objc2::{available, runtime::ProtocolObject}; +use objc2::runtime::{AnyObject, ProtocolObject, Sel}; +use objc2::{available, sel}; use objc2_foundation::{NSOperatingSystemVersion, NSProcessInfo}; use objc2_metal::{ MTLArgumentBuffersTier, MTLCounterSamplingPoint, MTLDevice, MTLFeatureSet, MTLGPUFamily, @@ -14,6 +15,16 @@ use crate::metal::QueueShared; use super::{OsFeatures, TimestampQuerySupport}; +/// Check if a device's class has a given method in its method table. +/// +/// This mirrors the check that `objc2` performs internally (in debug builds) +/// before sending a message. We use it to skip method calls that would panic +/// on proxy objects like Apple's `CaptureMTLDevice`, which forwards messages +/// at runtime but doesn't declare the methods in its class. +fn device_class_responds_to(device: &ProtocolObject, sel: Sel) -> bool { + AnyObject::class(device.as_ref()).responds_to(sel) +} + /// Maximum number of command buffers for `MTLCommandQueue`s that we create. /// /// If a [new command buffer] is requested when Metal has run out of command @@ -711,6 +722,7 @@ impl super::CapabilitiesQuery { texture_cube_array: Self::supports_any(device, TEXTURE_CUBE_ARRAY_SUPPORT), supports_float_filtering: os_type == super::OsType::Macos || (available!(macos = 11.0, ios = 14.0, tvos = 16.0, visionos = 1.0) + && device_class_responds_to(device, sel!(supports32BitFloatFiltering)) && device.supports32BitFloatFiltering()), format_depth24_stencil8: os_type == super::OsType::Macos && device.isDepth24Stencil8PixelFormatSupported(), @@ -983,7 +995,12 @@ impl super::CapabilitiesQuery { || device.supportsFamily(MTLGPUFamily::Apple7) || device.supportsFamily(MTLGPUFamily::Mac2)), // https://developer.apple.com/documentation/metal/mtldevice/hasunifiedmemory - has_unified_memory: if available!(macos = 15.0, ios = 13.0, tvos = 13.0, visionos = 1.0) + has_unified_memory: if available!( + macos = 10.15, + ios = 13.0, + tvos = 13.0, + visionos = 1.0 + ) && device_class_responds_to(device, sel!(hasUnifiedMemory)) { Some(device.hasUnifiedMemory()) } else { @@ -1057,7 +1074,10 @@ impl super::CapabilitiesQuery { tvos = 18.0, visionos = 2.0, ) { - device.supportsRaytracing() && device.supportsRaytracingFromRender() + device_class_responds_to(device, sel!(supportsRaytracing)) + && device.supportsRaytracing() + && device_class_responds_to(device, sel!(supportsRaytracingFromRender)) + && device.supportsRaytracingFromRender() } else { false },