Skip to content

Commit

Permalink
Add MTLCopyAllDevices fallback for iOS, tvOS and visionOS
Browse files Browse the repository at this point in the history
This function is only available on these platforms on very recent OS
versions, so let's add a fallback implementation so that users of
objc2-metal doesn't have to do it themselves.
  • Loading branch information
madsmtm committed Feb 3, 2025
1 parent a6fcabe commit 3543940
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/objc2/src/topics/about_generated/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- `QuickLookUI` / `objc2-quick-look-ui`.
- `SensorKit` / `objc2-sensor-kit`.
- `UserNotificationsUI` / `objc2-user-notifications-ui`.
* Added fallback for `MTLCopyAllDevices` on non-macOS platforms.


## 0.3.0 - 2025-01-22
Expand Down
47 changes: 47 additions & 0 deletions framework-crates/objc2-metal/src/device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::MTLDevice;
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2_foundation::NSArray;

/// Returns all Metal devices in the system.
///
/// On macOS and macCatalyst, this API will not cause the system to switch
/// devices and leaves the decision about which GPU to use up to the
/// application based on whatever criteria it deems appropriate.
///
/// On iOS, tvOS and visionOS, this API returns an array containing the same
/// device that MTLCreateSystemDefaultDevice would have returned, or an empty
/// array if it would have failed.
#[inline]
pub extern "C-unwind" fn MTLCopyAllDevices() -> Retained<NSArray<ProtocolObject<dyn MTLDevice>>> {
// MTLCopyAllDevices is always available on macOS and Mac Catalyst, but
// only available recently on iOS 18.0 / tvOS 18.0 / visionOS 2.0.
//
// Instead, we do the fallback to MTLCreateSystemDefaultDevice on those
// platforms that they do themselves on newer systems.
//
// TODO: Add `target_abi = "macabi" once in MSRV.
// TODO: Use something like <https://github.com/rust-lang/rfcs/pull/3750>
// to call the actual API when available.
#[cfg(target_os = "macos")]
{
extern "C-unwind" {
fn MTLCopyAllDevices() -> *mut NSArray<ProtocolObject<dyn MTLDevice>>;
}

let ret = unsafe { MTLCopyAllDevices() };
// SAFETY: Marked NS_RETURNS_RETAINED (and has `Copy` in the name).
unsafe { Retained::from_raw(ret) }
.expect("function was marked as returning non-null, but actually returned NULL")
}
#[cfg(not(target_os = "macos"))]
{
let device = crate::MTLCreateSystemDefaultDevice();
let slice = if let Some(device) = device.as_deref() {
&[device]
} else {
&[]
};
NSArray::from_slice(slice)
}
}
4 changes: 4 additions & 0 deletions framework-crates/objc2-metal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extern crate std;
mod capture;
#[cfg(feature = "MTLCounters")]
mod counters;
#[cfg(feature = "MTLDevice")]
mod device;
mod generated;
#[cfg(feature = "MTLAccelerationStructureTypes")]
mod packed;
Expand All @@ -72,6 +74,8 @@ mod types;

#[cfg(feature = "MTLCounters")]
pub use self::counters::*;
#[cfg(feature = "MTLDevice")]
pub use self::device::*;
#[allow(unused_imports, unreachable_pub)]
pub use self::generated::*;
#[cfg(feature = "MTLAccelerationStructureTypes")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ fn test_create_default() {
}

#[test]
#[cfg(target_os = "macos")]
fn get_all() {
let _ = objc2_metal::MTLCopyAllDevices();
}
3 changes: 3 additions & 0 deletions framework-crates/objc2-metal/translation-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ struct._MTLPackedFloat3.skipped = true
typedef.MTLPackedFloat3.skipped = true
fn.MTLPackedFloat3Make.skipped = true

# Manually defined to allow it to work on older OSes.
fn.MTLCopyAllDevices.skipped = true

###
### Safety
###
Expand Down
2 changes: 1 addition & 1 deletion generated

0 comments on commit 3543940

Please sign in to comment.