-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MTLCopyAllDevices fallback for iOS, tvOS and visionOS
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
Showing
6 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters