Skip to main content

MetalDevice

Struct MetalDevice 

Source
pub struct MetalDevice { /* private fields */ }
Expand description

A Metal device (GPU)

This is a wrapper around MTLDevice that provides safe access to Metal functionality.

Implementations§

Source§

impl MetalDevice

Source

pub fn system_default() -> Option<Self>

Get the system default Metal device

Returns None if no Metal device is available.

Source

pub unsafe fn from_ptr(ptr: *mut c_void) -> Option<Self>

Create a MetalDevice from a raw MTLDevice pointer

This is useful when you already have a device from another source (e.g., the metal crate) and want to use it for texture creation.

§Safety

The pointer must be a valid MTLDevice pointer. The wrapper borrows the device: it will NOT be released when this wrapper is dropped. Use Self::from_ptr_retained if you want the wrapper to hold its own reference.

Source

pub unsafe fn from_ptr_retained(ptr: *mut c_void) -> Option<Self>

Create a MetalDevice from a raw MTLDevice pointer, retaining it

The wrapper takes its own +1 reference (via an Objective-C retain) and releases it on drop, so the caller keeps ownership of their reference.

§Safety

The pointer must be a valid MTLDevice pointer.

Source

pub fn name(&self) -> String

Get the name of this device

Source

pub fn create_command_queue(&self) -> Option<MetalCommandQueue>

Create a command queue for this device

Source

pub fn create_library_with_source( &self, source: &str, ) -> Result<MetalLibrary, String>

Create a shader library from source code

§Errors

Returns an error message if shader compilation fails.

Source

pub fn create_buffer( &self, length: usize, options: ResourceOptions, ) -> Option<MetalBuffer>

Create a buffer

Returns None if length exceeds isize::MAX: the Swift bridge takes a signed Int, so such a length would arrive as a negative size.

Source

pub fn create_buffer_with_bytes(&self, data: &[u8]) -> Option<MetalBuffer>

Create a buffer and populate it with initialized bytes.

use screencapturekit::metal::{MetalDevice, Uniforms};

let device = MetalDevice::system_default().expect("Metal device");
let bytes = Uniforms::new(1920.0, 1080.0, 1280.0, 720.0).to_bytes();
let buffer = device.create_buffer_with_bytes(&bytes).expect("buffer");
assert_eq!(buffer.length(), Uniforms::BYTE_LEN);
Source

pub unsafe fn create_buffer_with_data<T: Copy>( &self, data: &T, ) -> Option<MetalBuffer>

Create a buffer by copying the object representation of data.

Prefer Self::create_buffer_with_bytes and an explicit encoder such as Uniforms::to_bytes.

§Safety

Every byte in T, including padding, must be initialized. Its layout and native-endian representation must match the GPU consumer, and it must not contain references, pointers, or ownership-bearing handles that the GPU could interpret or outlive.

Source

pub fn create_render_pipeline_state( &self, descriptor: &MetalRenderPipelineDescriptor, ) -> Option<MetalRenderPipelineState>

Create a render pipeline state from a descriptor

Source

pub fn as_ptr(&self) -> *mut c_void

Get the raw pointer to the underlying MTLDevice

Source

pub fn as_apple_metal(&self) -> BorrowedAppleMetalDevice<'_>

Borrow this device as an apple_metal::MetalDevice for interop with the lightweight apple-metal crate.

The returned guard references the same MTLDevice instance and does not release it on drop, so it must not outlive this MetalDevice. That is enforced by the borrow: apple_metal::ManuallyDropDevice has no lifetime of its own, so returning it directly let callers keep a dangling MTLDevice handle after the owner was dropped.

Deref to reach the apple_metal::MetalDevice API.

Trait Implementations§

Source§

impl Debug for MetalDevice

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for MetalDevice

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for MetalDevice

Source§

impl Sync for MetalDevice

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.