metal: Move API availability checks out of PrivateCapabilities

We always want to call newer methods if available, so there is no need
to store this in a variable if we can use the `available!` macro
directly at the usage site.
This commit is contained in:
Mads Marquart authored and Andy Leiserson committed 2026-02-02 17:08:14 -08:00
1 parent 13a1da5df1
commit a5116520b0
4 files changed
+50 -62

No files matched your search

+10 -44
View File
@@ -9,7 +9,7 @@ use wgt::{AstcBlock, AstcChannel};
use alloc::{string::ToString as _, sync::Arc, vec::Vec};
use super::TimestampQuerySupport;
use super::{OsFeatures, TimestampQuerySupport};
/// Maximum number of command buffers for `MTLCommandQueue`s that we create.
///
@@ -375,19 +375,24 @@ impl crate::Adapter for super::Adapter {
formats.push(wgt::TextureFormat::Rgb10a2Unorm);
}
let pc = &self.shared.private_caps;
Some(crate::SurfaceCapabilities {
formats,
// We use this here to govern the maximum number of drawables + 1.
// See https://developer.apple.com/documentation/quartzcore/cametallayer/2938720-maximumdrawablecount
maximum_frame_latency: if pc.can_set_maximum_drawables_count {
// See https://developer.apple.com/documentation/quartzcore/cametallayer/maximumdrawablecount
maximum_frame_latency: if available!(
macos = 10.13.2,
ios = 11.2,
tvos = 11.2,
visionos = 1.0
) {
1..=2
} else {
// 3 is the default value for maximum drawables in `CAMetalLayer` documentation
// iOS 10.3 was tested to use 3 on iphone5s
2..=2
},
present_modes: if pc.can_set_display_sync {
// We enable Immediate mode using `-[CAMetalLayer setDisplaySyncEnabled: false]`.
present_modes: if OsFeatures::display_sync() {
vec![wgt::PresentMode::Fifo, wgt::PresentMode::Immediate]
} else {
vec![wgt::PresentMode::Fifo]
@@ -883,29 +888,6 @@ impl super::PrivateCapabilities {
&& (metal3
|| device.supportsFamily(MTLGPUFamily::Apple3)
|| device.supportsFamily(MTLGPUFamily::Mac2)),
// https://developer.apple.com/documentation/metal/mtlcapturemanager
supports_capture_manager: available!(
macos = 10.13,
ios = 11.0,
tvos = 11.0,
visionos = 1.0
),
// https://developer.apple.com/documentation/quartzcore/cametallayer/maximumdrawablecount
can_set_maximum_drawables_count: available!(
macos = 10.14,
ios = 11.2,
tvos = 11.2,
visionos = 1.0
),
// https://developer.apple.com/documentation/quartzcore/cametallayer/displaysyncenabled
can_set_display_sync: available!(macos = 10.13) || cfg!(target_env = "macabi"),
// https://developer.apple.com/documentation/quartzcore/cametallayer/allowsnextdrawabletimeout
can_set_next_drawable_timeout: available!(
macos = 10.13,
ios = 11.0,
tvos = 11.0,
visionos = 1.0
),
// This is just trusted blindly since docs referencing supports_any have been removed
// but we don't want to remove feature support.
supports_arrays_of_textures: Self::supports_any(
@@ -921,18 +903,9 @@ impl super::PrivateCapabilities {
&& (metal3
|| device.supportsFamily(MTLGPUFamily::Apple6)
|| device.supportsFamily(MTLGPUFamily::Mac2)),
// https://developer.apple.com/documentation/metal/mtlpipelinebufferdescriptor/mutability
supports_mutability: available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0),
// Depth clipping is supported on all macOS GPU families and iOS family 4 and later
supports_depth_clip_control: os_type == super::OsType::Macos
|| device.supportsFeatureSet(MTLFeatureSet::iOS_GPUFamily4_v1),
// https://developer.apple.com/documentation/metal/mtlcompileoptions/preserveinvariance
supports_preserve_invariance: available!(
macos = 11.0,
ios = 13.0,
tvos = 14.0,
visionos = 1.0
),
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4
supports_shader_primitive_index: family_check
&& (metal3
@@ -966,13 +939,6 @@ impl super::PrivateCapabilities {
&& (metal3
|| device.supportsFamily(MTLGPUFamily::Apple7)
|| device.supportsFamily(MTLGPUFamily::Mac2)),
// https://developer.apple.com/documentation/metal/mtlsharedevent
supports_shared_event: available!(
macos = 10.14,
ios = 12.0,
tvos = 12.0,
visionos = 1.0
),
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=5 (footnote)
// Supported on some Metal4, Apple7, Mac2, and some other platforms can be queried with device.supportsShaderBarycentricCoordinates().
shader_barycentrics: metal4
+18 -9
View File
@@ -8,7 +8,7 @@ use dispatch2::DispatchData;
use std::{thread, time};
use objc2::{
msg_send,
available, msg_send,
rc::{autoreleasepool, Retained},
runtime::ProtocolObject,
};
@@ -226,7 +226,8 @@ impl super::Device {
let options = MTLCompileOptions::new();
options.setLanguageVersion(self.shared.private_caps.msl_version);
if self.shared.private_caps.supports_preserve_invariance {
// https://developer.apple.com/documentation/metal/mtlcompileoptions/preserveinvariance
if available!(macos = 11.0, ios = 13.0, tvos = 14.0, visionos = 1.0) {
options.setPreserveInvariance(true);
}
@@ -1210,6 +1211,10 @@ impl crate::Device for super::Device {
}
}
// https://developer.apple.com/documentation/metal/mtlpipelinebufferdescriptor/mutability
let supports_mutability =
available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0);
let (primitive_class, raw_primitive_type) =
conv::map_primitive_topology(desc.primitive.topology);
@@ -1280,7 +1285,8 @@ impl crate::Device for super::Device {
)?;
descriptor.setVertexFunction(Some(&vs.function));
if self.shared.private_caps.supports_mutability {
if supports_mutability {
Self::set_buffers_mutability(
&descriptor.vertexBuffers(),
vs.immutable_buffer_mask,
@@ -1384,7 +1390,7 @@ impl crate::Device for super::Device {
naga::ShaderStage::Task,
)?;
unsafe { descriptor.setObjectFunction(Some(&ts.function)) };
if self.shared.private_caps.supports_mutability {
if supports_mutability {
Self::set_buffers_mutability(
&descriptor.meshBuffers(),
ts.immutable_buffer_mask,
@@ -1413,7 +1419,7 @@ impl crate::Device for super::Device {
naga::ShaderStage::Mesh,
)?;
unsafe { descriptor.setMeshFunction(Some(&ms.function)) };
if self.shared.private_caps.supports_mutability {
if supports_mutability {
Self::set_buffers_mutability(
&descriptor.meshBuffers(),
ms.immutable_buffer_mask,
@@ -1455,7 +1461,7 @@ impl crate::Device for super::Device {
)?;
unsafe { descriptor.setFragmentFunction(Some(&fs.function)) };
if self.shared.private_caps.supports_mutability {
if supports_mutability {
Self::set_buffers_mutability(
&descriptor.fragmentBuffers(),
fs.immutable_buffer_mask,
@@ -1658,7 +1664,8 @@ impl crate::Device for super::Device {
descriptor.setComputeFunction(Some(&cs.function));
if self.shared.private_caps.supports_mutability {
// https://developer.apple.com/documentation/metal/mtlpipelinebufferdescriptor/mutability
if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) {
Self::set_buffers_mutability(&descriptor.buffers(), cs.immutable_buffer_mask);
}
@@ -1791,7 +1798,8 @@ impl crate::Device for super::Device {
unsafe fn create_fence(&self) -> DeviceResult<super::Fence> {
self.counters.fences.add(1);
let shared_event = if self.shared.private_caps.supports_shared_event {
// https://developer.apple.com/documentation/metal/mtlsharedevent
let shared_event = if available!(macos = 10.14, ios = 12.0, tvos = 12.0, visionos = 1.0) {
Some(self.shared.device.newSharedEvent().unwrap())
} else {
None
@@ -1853,7 +1861,8 @@ impl crate::Device for super::Device {
}
unsafe fn start_graphics_debugger_capture(&self) -> bool {
if !self.shared.private_caps.supports_capture_manager {
// https://developer.apple.com/documentation/metal/mtlcapturemanager
if !available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) {
return false;
}
let device = &self.shared.device;
+16 -7
View File
@@ -37,6 +37,7 @@ use bitflags::bitflags;
use hashbrown::HashMap;
use naga::FastHashMap;
use objc2::{
available,
rc::{autoreleasepool, Retained},
runtime::ProtocolObject,
};
@@ -114,6 +115,21 @@ crate::impl_dyn_resource!(
TextureView
);
/// Provides availability information about Mac APIs.
///
/// This may include Metal features that depend only on software support.
/// Features with varying hardware support are in [`PrivateCapabilities`]
///
/// When feature detection is only needed once, it may also be done inline.
struct OsFeatures;
impl OsFeatures {
fn display_sync() -> bool {
// https://developer.apple.com/documentation/quartzcore/cametallayer/displaysyncenabled
available!(macos = 10.13) || cfg!(target_abi = "macabi")
}
}
pub struct Instance {}
impl Instance {
@@ -317,15 +333,9 @@ struct PrivateCapabilities {
sample_count_mask: crate::TextureFormatCapabilities,
supports_debug_markers: bool,
supports_binary_archives: bool,
supports_capture_manager: bool,
can_set_maximum_drawables_count: bool,
can_set_display_sync: bool,
can_set_next_drawable_timeout: bool,
supports_arrays_of_textures: bool,
supports_arrays_of_textures_write: bool,
supports_mutability: bool,
supports_depth_clip_control: bool,
supports_preserve_invariance: bool,
supports_shader_primitive_index: bool,
has_unified_memory: Option<bool>,
timestamp_query_support: TimestampQuerySupport,
@@ -335,7 +345,6 @@ struct PrivateCapabilities {
int64_atomics_min_max: bool,
int64_atomics: bool,
float_atomics: bool,
supports_shared_event: bool,
mesh_shaders: bool,
max_mesh_task_workgroup_count: u32,
max_task_payload_size: u32,
+6 -2
View File
@@ -1,6 +1,7 @@
use alloc::borrow::ToOwned as _;
use objc2::{
available,
rc::{autoreleasepool, Retained},
runtime::ProtocolObject,
ClassType, Message,
@@ -11,6 +12,8 @@ use objc2_metal::MTLTextureType;
use objc2_quartz_core::{CAMetalDrawable, CAMetalLayer};
use parking_lot::{Mutex, RwLock};
use super::OsFeatures;
impl super::Surface {
pub fn new(layer: Retained<CAMetalLayer>) -> Self {
Self {
@@ -95,10 +98,11 @@ impl crate::Surface for super::Surface {
// this gets ignored on iOS for certain OS/device combinations (iphone5s iOS 10.3)
render_layer.setMaximumDrawableCount(config.maximum_frame_latency as usize + 1);
render_layer.setDrawableSize(drawable_size);
if caps.can_set_next_drawable_timeout {
// https://developer.apple.com/documentation/quartzcore/cametallayer/allowsnextdrawabletimeout
if available!(macos = 10.13, ios = 11.0, tvos = 11.0, visionos = 1.0) {
render_layer.setAllowsNextDrawableTimeout(false);
}
if caps.can_set_display_sync {
if OsFeatures::display_sync() {
render_layer.setDisplaySyncEnabled(display_sync);
}