#[cfg(target_os = "macos")]
use metal::foreign_types::{ForeignType, ForeignTypeRef};
#[cfg(target_os = "macos")]
pub fn extract_metal_device(device: &wgpu::Device) -> Option<metal::Device> {
extern "C" {
fn objc_retain(value: *mut std::ffi::c_void) -> *mut std::ffi::c_void;
}
unsafe {
let hal_guard = device.as_hal::<wgpu_hal::api::Metal>()?;
let proto_ref = &**hal_guard.raw_device();
let raw_ptr = proto_ref as *const _ as *mut metal::MTLDevice;
objc_retain(raw_ptr as *mut _);
Some(metal::Device::from_ptr(raw_ptr))
}
}
#[cfg(target_os = "macos")]
pub fn with_metal_texture<F>(texture: &wgpu::Texture, f: F)
where
F: FnOnce(&metal::TextureRef),
{
unsafe {
if let Some(hal_guard) = texture.as_hal::<wgpu_hal::api::Metal>() {
let proto_ref = hal_guard.raw_handle();
let raw_ptr = proto_ref as *const _ as *const metal::MTLTexture;
f(metal::TextureRef::from_ptr(raw_ptr as *mut _));
}
}
}