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
This commit is contained in:
Andy Leiserson authored and GitHub committed 2026-03-23 10:38:33 -07:00
1 parent 4e4708423c
commit f76801d8c1
2 files changed
+27 -3

No files matched your search

+4
View File
@@ -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
+23 -3
View File
@@ -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<dyn MTLDevice>, 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
},