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
impl MetalDevice
Sourcepub fn system_default() -> Option<Self>
pub fn system_default() -> Option<Self>
Get the system default Metal device
Returns None if no Metal device is available.
Sourcepub unsafe fn from_ptr(ptr: *mut c_void) -> Option<Self>
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.
Sourcepub unsafe fn from_ptr_retained(ptr: *mut c_void) -> Option<Self>
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.
Sourcepub fn create_command_queue(&self) -> Option<MetalCommandQueue>
pub fn create_command_queue(&self) -> Option<MetalCommandQueue>
Create a command queue for this device
Sourcepub fn create_library_with_source(
&self,
source: &str,
) -> Result<MetalLibrary, String>
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.
Sourcepub fn create_buffer(
&self,
length: usize,
options: ResourceOptions,
) -> Option<MetalBuffer>
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.
Sourcepub fn create_buffer_with_bytes(&self, data: &[u8]) -> Option<MetalBuffer>
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);Sourcepub unsafe fn create_buffer_with_data<T: Copy>(
&self,
data: &T,
) -> Option<MetalBuffer>
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.
Sourcepub fn create_render_pipeline_state(
&self,
descriptor: &MetalRenderPipelineDescriptor,
) -> Option<MetalRenderPipelineState>
pub fn create_render_pipeline_state( &self, descriptor: &MetalRenderPipelineDescriptor, ) -> Option<MetalRenderPipelineState>
Create a render pipeline state from a descriptor
Sourcepub fn as_apple_metal(&self) -> BorrowedAppleMetalDevice<'_>
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.