Skip to content

Commit

Permalink
metal: Reexpose static fns on MTLDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Jul 7, 2024
1 parent 5bab112 commit edb6ddf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub type Id<T> = Retained<T>;

impl<T: ?Sized> Retained<T> {
#[inline]
pub(crate) unsafe fn new_nonnull(ptr: NonNull<T>) -> Self {
pub unsafe fn new_nonnull(ptr: NonNull<T>) -> Self {
Self {
ptr,
item: PhantomData,
Expand Down
10 changes: 3 additions & 7 deletions framework-crates/objc2-metal/examples/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ use objc2_foundation::{
ns_string, NSDate, NSNotification, NSObject, NSObjectProtocol, NSPoint, NSRect, NSSize,
};
use objc2_metal::{
MTLCommandBuffer, MTLCommandEncoder, MTLCommandQueue, MTLCreateSystemDefaultDevice, MTLDevice,
MTLLibrary, MTLPackedFloat3, MTLPrimitiveType, MTLRenderCommandEncoder,
MTLRenderPipelineDescriptor, MTLRenderPipelineState,
MTLCommandBuffer, MTLCommandEncoder, MTLCommandQueue, MTLDevice, MTLLibrary, MTLPackedFloat3,
MTLPrimitiveType, MTLRenderCommandEncoder, MTLRenderPipelineDescriptor, MTLRenderPipelineState,
};
use objc2_metal_kit::{MTKView, MTKViewDelegate};

Expand Down Expand Up @@ -106,10 +105,7 @@ declare_class!(
};

// get the default device
let device = {
let ptr = unsafe { MTLCreateSystemDefaultDevice() };
unsafe { Retained::retain(ptr) }.expect("Failed to get default system device.")
};
let device = MTLDevice::system_default().expect("Failed to get default system device.");

// create the command queue
let command_queue = device
Expand Down
32 changes: 16 additions & 16 deletions framework-crates/objc2-metal/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// use crate::MTLDevice;
//
// pub trait MTLDeviceExtension {
// pub fn system_default() -> Option<Self> {
// MTLCreateSystemDefaultDevice()
// }
//
// pub fn all() -> Retained<NSArray<Self>> {
// #[cfg(target_os = "macos")]
// MTLCopyAllDevices()
// #[cfg(not(target_os = "macos"))]
// NSArray::from(MTLCreateSystemDefaultDevice())
// }
// }
//
// impl<P: MTLDevice> MTLDeviceExtension for P {}
use objc2::{rc::Id, runtime::ProtocolObject};
use objc2_foundation::NSArray;

use crate::MTLDevice;

pub trait MTLDeviceExt: MTLDevice {
fn system_default() -> Option<Id<ProtocolObject<dyn MTLDevice>>> {
unsafe { Id::from_raw(crate::MTLCreateSystemDefaultDevice()) }
}

fn all() -> Id<NSArray<ProtocolObject<dyn MTLDevice>>> {
unsafe { Id::new_nonnull(crate::MTLCopyAllDevices()) }
}
}

impl<P: MTLDevice> MTLDeviceExt for P {}
5 changes: 4 additions & 1 deletion framework-crates/objc2-metal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! [apple-doc]: https://developer.apple.com/documentation/xcode/validating-your-apps-metal-api-usage/.
//!
//! Note: To use `MTLCreateSystemDefaultDevice` you need to link to
//! Note: To use [`MTLDevice::system_default()`] you need to link to
//! `CoreGraphics`, this can be done by using `objc2-app-kit`, or by doing:
//! ```rust
//! #[link(name = "CoreGraphics", kind = "framework")]
Expand Down Expand Up @@ -64,6 +64,8 @@ mod texture;

#[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 All @@ -72,6 +74,7 @@ pub use self::packed::MTLPackedFloat3;
pub use self::private::MTLDevicePrivate;
#[cfg(feature = "MTLResource")]
pub use self::resource::*;
// TODO: CFG is already internal, use *?
#[cfg(all(feature = "MTLRenderCommandEncoder", feature = "MTLCommandEncoder"))]
pub use self::slice::MTLRenderCommandEncoderSliceExt;
#[cfg(feature = "MTLTexture")]
Expand Down
7 changes: 3 additions & 4 deletions framework-crates/objc2-metal/tests/runs_with_core_graphics.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#![cfg(feature = "MTLDevice")]
use objc2::rc::Retained;
use objc2_metal::MTLCreateSystemDefaultDevice;
use objc2_metal::MTLDevice;

#[link(name = "CoreGraphics", kind = "framework")]
extern "C" {}

#[test]
#[ignore = "doesn't work in CI"]
fn test_create_default() {
let _ = unsafe { Retained::from_raw(MTLCreateSystemDefaultDevice()).unwrap() };
let _ = MTLDevice::system_default();
}

#[test]
#[cfg(target_os = "macos")]
fn get_all() {
let _ = unsafe { Retained::from_raw(objc2_metal::MTLCopyAllDevices().as_ptr()).unwrap() };
let _ = MTLDevice::all();
}

0 comments on commit edb6ddf

Please sign in to comment.