use mln_gfx::{ExportKind, ExportedRing, SyncKind, SyncKinds};
use mln_style::error::Error;
use objc2::rc::Retained;
use super::adapter_index;
use crate::render::gfx_error;
use crate::render::local::{
BackendRef, Borrowed, Exported, FramesInit, Gfx, LocalBackend, Target, gfx, no_surface, overlay_of,
};
use crate::render::target::{
Adapter, BackendType, BorrowedRing, DepthStencilFormat, Device, FrameDepth, FrameTexture, FrameTextureKind,
NativeTexture, RingKind, Surface, SurfaceKind, SurfaceOverlay,
};
impl BorrowedRing {
#[must_use]
pub fn metal(textures: mln_mtl::BorrowedTextures) -> BorrowedRing {
BorrowedRing { kind: RingKind::Metal(textures) }
}
}
impl Surface {
#[must_use]
pub fn metal_layer(layer: mln_mtl::Layer) -> Surface {
Surface::of(SurfaceKind::MetalLayer(layer))
}
}
pub(in super::super) fn native_texture(texture: u64, pixel_format: u64, device: &mln_mtl::Device) -> FrameTextureKind {
FrameTextureKind::Metal { texture, device: Retained::as_ptr(device.device()) as usize as u64, pixel_format }
}
pub(in super::super) fn ring_depth_stencil_format(textures: &mln_mtl::BorrowedTextures) -> Option<DepthStencilFormat> {
(textures.depth_pixel_format() == Some(mln_mtl::objc2_metal::MTLPixelFormat::Depth32Float_Stencil8))
.then_some(DepthStencilFormat::Depth32FloatStencil8)
}
pub(in super::super) fn ring_same_texture(
textures: &mln_mtl::BorrowedTextures,
slot: usize,
others: &mln_mtl::BorrowedTextures,
other_slot: usize,
) -> bool {
textures
.textures
.get(slot)
.zip(others.textures.get(other_slot))
.is_some_and(|(texture, other)| std::ptr::eq(Retained::as_ptr(texture), Retained::as_ptr(other)))
}
pub(in super::super) fn ring_texture(
textures: &mln_mtl::BorrowedTextures,
slot: usize,
device: &mln_mtl::Device,
) -> Option<FrameTexture> {
textures.textures.get(slot).map(|texture| FrameTexture {
size: textures.size,
kind: FrameTextureKind::Metal {
texture: Retained::as_ptr(texture) as usize as u64,
device: Retained::as_ptr(device.device()) as usize as u64,
pixel_format: textures.pixel_format().0 as u64,
},
native: None,
depth: textures.depth_textures.get(slot).zip(textures.depth_pixel_format()).map(|(depth, pixel_format)| {
FrameDepth {
texture: NativeTexture::Metal {
texture: Retained::as_ptr(depth) as usize as u64,
pixel_format: pixel_format.0 as u64,
},
export: None,
}
}),
})
}
pub(in super::super) fn create_device(adapter: Option<Adapter>) -> Result<Device, Error> {
mln_mtl::Device::new(adapter_index(BackendType::Metal, adapter)?).map(Device::Metal).map_err(gfx_error)
}
pub(in super::super) fn borrowed_sync_kinds(
device: &mln_mtl::Device,
textures: &mln_mtl::BorrowedTextures,
) -> Result<(SyncKinds, SyncKinds), Error> {
textures.check(device).map_err(gfx_error)?;
Ok(mln_mtl::BorrowedTextures::sync_kinds(device))
}
pub(in super::super) fn export_plan(
device: &mln_mtl::Device,
ring: &ExportedRing,
) -> Result<(ExportKind, SyncKinds, SyncKinds), Error> {
let kind = mln_mtl::exported_target::plan(device, ring).map_err(gfx_error)?;
let (producer, consumer) = mln_mtl::exported_target::sync_kinds(device);
Ok((kind, producer, consumer))
}
impl LocalBackend for mln_mtl::RendererBackend {
type RingTextures = mln_mtl::BorrowedTextures;
fn ring_textures(ring: RingKind) -> Option<Self::RingTextures> {
if let RingKind::Metal(textures) = ring { Some(textures) } else { None }
}
fn by_type(&self) -> BackendRef<'_> {
BackendRef::Metal(self)
}
fn borrowed(&mut self, textures: Self::RingTextures, producer: SyncKind) -> Result<Box<dyn Borrowed<Self>>, Error> {
Ok(Box::new(mln_mtl::BorrowedTarget::new(self, textures, producer).map_err(gfx_error)?))
}
fn exported(
&mut self,
_device: &Device,
ring: &ExportedRing,
kind: ExportKind,
size: mln_gfx::Size,
producer: SyncKind,
) -> Result<Box<dyn Exported<Self>>, Error> {
Ok(Box::new(mln_mtl::ExportedTarget::new(self, ring, kind, size, producer).map_err(gfx_error)?))
}
fn surface(
&mut self,
surface: Surface,
overlay: Option<SurfaceOverlay>,
size: mln_gfx::Size,
) -> Result<Target<Self>, Error> {
let layer = match surface.kind {
SurfaceKind::MetalLayer(layer) => layer,
SurfaceKind::Window(window) => mln_mtl::Layer::of_window(&*window).map_err(gfx_error)?,
_ => return Err(no_surface()),
};
let overlay =
overlay_of(
overlay,
|overlay| {
if let SurfaceOverlay::Metal(overlay) = overlay { Ok(overlay) } else { Err(overlay) }
},
)?;
let mut target = mln_mtl::SurfaceTarget::new(self, layer, size, surface.cap_frame_rate);
target.set_overlay(overlay);
Ok(Box::new(target))
}
}
pub(in super::super) fn new_gfx(device: &mln_mtl::Device, init: FramesInit<'_>) -> Result<Box<dyn Gfx>, Error> {
gfx(mln_mtl::RendererBackend::new(device), init)
}