#![allow(
unreachable_patterns,
irrefutable_let_patterns,
clippy::match_wildcard_for_single_variants,
reason = "the backends' variants depend on the build's features and target"
)]
use std::sync::Arc;
use mln_gfx::Window;
pub use mln_gfx::backend::Type as BackendType;
pub use mln_gfx::{
ConsumerSync, DepthStencilFormat, DepthStencilFormats, DmaBufFormat, DmaBufPlane, ExportKind, ExportKinds,
ExportedImage, ExportedRing, FrameDepth, GpuSync, NativeTexture, SyncKind, SyncKinds,
};
use mln_style::error::Error;
use mln_style::size::Size;
#[cfg(any(has_metal, has_vulkan, has_d3d12, feature = "webgpu", feature = "gl"))]
use super::backends;
pub type Wake = Arc<dyn Fn() + Send + Sync>;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Driver {
#[default]
CoreWorker,
CallerThread,
}
#[derive(Clone, Default)]
pub struct AttachOptions {
pub driver: Driver,
pub frame_wake: Option<Wake>,
pub redraw_wake: Option<Wake>,
pub driver_work_wake: Option<Wake>,
pub producer_sync_kinds: SyncKinds,
}
impl std::fmt::Debug for AttachOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AttachOptions")
.field("driver", &self.driver)
.field("frame_wake", &self.frame_wake.is_some())
.field("redraw_wake", &self.redraw_wake.is_some())
.field("driver_work_wake", &self.driver_work_wake.is_some())
.field("producer_sync_kinds", &self.producer_sync_kinds)
.finish()
}
}
#[derive(Debug)]
pub enum RenderTarget {
Offscreen { size: Size, device: Option<Device> },
Surface { size: Size, surface: Surface, device: Option<Device> },
Borrowed { size: Size, ring: BorrowedRing, device: Device },
Exported { size: Size, ring: ExportedRing, device: Option<Device> },
}
#[derive(Clone, Debug, PartialEq)]
pub struct BorrowedRing {
pub(crate) kind: RingKind,
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum RingKind {
#[cfg(has_vulkan)]
Vulkan(mln_vulkan::BorrowedImages),
#[cfg(has_metal)]
Metal(mln_mtl::BorrowedTextures),
#[cfg(feature = "gl")]
Gl(mln_gl::BorrowedTextures),
#[cfg(feature = "webgpu")]
WebGpu(mln_webgpu::BorrowedTextures),
#[cfg(has_d3d12)]
D3d12(mln_d3d12::BorrowedTextures),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FrameTexture {
pub size: mln_gfx::Size,
pub kind: FrameTextureKind,
pub native: Option<FrameTextureKind>,
pub depth: Option<FrameDepth>,
}
impl FrameTexture {
#[must_use]
pub fn native_kind(&self) -> Option<&FrameTextureKind> {
match &self.kind {
FrameTextureKind::Exported(_) => self.native.as_ref(),
kind => Some(kind),
}
}
pub(crate) fn exported(frame: mln_gfx::ExportedFrame, device: &Device) -> FrameTexture {
FrameTexture {
size: frame.size,
kind: FrameTextureKind::Exported(frame.image),
native: native_kind(frame.texture, device),
depth: frame.depth,
}
}
}
fn native_kind(texture: NativeTexture, device: &Device) -> Option<FrameTextureKind> {
match (texture, device) {
#[cfg(has_vulkan)]
(NativeTexture::Vulkan { image, view, format, layout }, Device::Vulkan(device)) => {
Some(backends::vulkan::native_texture(image, view, format, layout, device))
}
#[cfg(has_metal)]
(NativeTexture::Metal { texture, pixel_format }, Device::Metal(device)) => {
Some(backends::metal::native_texture(texture, pixel_format, device))
}
(NativeTexture::OpenGl { texture, target, .. }, _) => Some(FrameTextureKind::OpenGl { texture, target }),
#[cfg(has_d3d12)]
(NativeTexture::D3d12 { resource, format, state }, Device::D3d12(device)) => {
Some(backends::d3d12::native_texture(resource, format, state, device))
}
_ => None,
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FrameTextureKind {
Vulkan { image: u64, view: u64, device: u64, format: i32, layout: i32 },
Metal { texture: u64, device: u64, pixel_format: u64 },
OpenGl { texture: u32, target: u32 },
#[cfg(has_webgl)]
WebGl { texture: mln_gl::WebTexture, target: u32, depth: Option<mln_gl::WebTexture> },
WebGpu,
D3d12 { resource: u64, device: u64, format: u32, state: u32 },
Exported(ExportedImage),
}
impl BorrowedRing {
#[must_use]
pub fn backend(&self) -> BackendType {
match self.kind {
#[cfg(has_vulkan)]
RingKind::Vulkan(_) => BackendType::Vulkan,
#[cfg(has_metal)]
RingKind::Metal(_) => BackendType::Metal,
#[cfg(feature = "gl")]
RingKind::Gl(_) => BackendType::OpenGL,
#[cfg(feature = "webgpu")]
RingKind::WebGpu(_) => BackendType::WebGPU,
#[cfg(has_d3d12)]
RingKind::D3d12(_) => BackendType::D3D12,
_ => BackendType::WebGPU,
}
}
#[must_use]
pub fn depth(&self) -> usize {
match &self.kind {
#[cfg(has_vulkan)]
RingKind::Vulkan(images) => images.images.len(),
#[cfg(has_metal)]
RingKind::Metal(textures) => textures.textures.len(),
#[cfg(feature = "gl")]
RingKind::Gl(textures) => textures.textures.len(),
#[cfg(feature = "webgpu")]
RingKind::WebGpu(textures) => textures.textures.len(),
#[cfg(has_d3d12)]
RingKind::D3d12(textures) => textures.textures.len(),
_ => 0,
}
}
#[must_use]
pub fn physical_size(&self) -> mln_gfx::Size {
match &self.kind {
#[cfg(has_vulkan)]
RingKind::Vulkan(images) => images.size,
#[cfg(has_metal)]
RingKind::Metal(textures) => textures.size,
#[cfg(feature = "gl")]
RingKind::Gl(textures) => textures.size,
#[cfg(feature = "webgpu")]
RingKind::WebGpu(textures) => textures.size,
#[cfg(has_d3d12)]
RingKind::D3d12(textures) => textures.size,
_ => mln_gfx::Size::default(),
}
}
#[must_use]
pub fn takes_depth_on(&self, device: &Device) -> bool {
#[cfg(feature = "gl")]
if let RingKind::Gl(_) = self.kind {
return true;
}
!self.has_depth()
|| self.depth_stencil_format().is_some_and(|format| device.depth_stencil_formats().contains(format))
}
#[must_use]
pub fn depth_stencil_format(&self) -> Option<DepthStencilFormat> {
match &self.kind {
#[cfg(has_vulkan)]
RingKind::Vulkan(images) => backends::vulkan::ring_depth_stencil_format(images),
#[cfg(has_metal)]
RingKind::Metal(textures) => backends::metal::ring_depth_stencil_format(textures),
#[cfg(feature = "webgpu")]
RingKind::WebGpu(textures) => backends::webgpu::ring_depth_stencil_format(textures),
#[cfg(has_d3d12)]
RingKind::D3d12(textures) => backends::d3d12::ring_depth_stencil_format(textures),
_ => None,
}
}
#[must_use]
pub fn has_depth(&self) -> bool {
match &self.kind {
#[cfg(has_vulkan)]
RingKind::Vulkan(images) => images.depth.is_some(),
#[cfg(has_metal)]
RingKind::Metal(textures) => !textures.depth_textures.is_empty(),
#[cfg(feature = "gl")]
RingKind::Gl(textures) => !textures.depth_textures.is_empty(),
#[cfg(feature = "webgpu")]
RingKind::WebGpu(textures) => !textures.depth_textures.is_empty(),
#[cfg(has_d3d12)]
RingKind::D3d12(textures) => !textures.depth_textures.is_empty(),
_ => false,
}
}
pub(crate) fn same_texture(&self, slot: usize, other: &BorrowedRing, other_slot: usize) -> bool {
match (&self.kind, &other.kind) {
#[cfg(has_vulkan)]
(RingKind::Vulkan(images), RingKind::Vulkan(others)) => {
backends::vulkan::ring_same_texture(images, slot, others, other_slot)
}
#[cfg(has_metal)]
(RingKind::Metal(textures), RingKind::Metal(others)) => {
backends::metal::ring_same_texture(textures, slot, others, other_slot)
}
#[cfg(feature = "gl")]
(RingKind::Gl(textures), RingKind::Gl(others)) => {
backends::gl::ring_same_texture(textures, slot, others, other_slot)
}
#[cfg(feature = "webgpu")]
(RingKind::WebGpu(textures), RingKind::WebGpu(others)) => {
backends::webgpu::ring_same_texture(textures, slot, others, other_slot)
}
#[cfg(has_d3d12)]
(RingKind::D3d12(textures), RingKind::D3d12(others)) => {
backends::d3d12::ring_same_texture(textures, slot, others, other_slot)
}
_ => false,
}
}
#[must_use]
pub fn texture(&self, slot: usize, device: &Device) -> Option<FrameTexture> {
match (&self.kind, device) {
#[cfg(has_vulkan)]
(RingKind::Vulkan(images), Device::Vulkan(device)) => backends::vulkan::ring_texture(images, slot, device),
#[cfg(has_metal)]
(RingKind::Metal(textures), Device::Metal(device)) => backends::metal::ring_texture(textures, slot, device),
#[cfg(has_native_gl)]
(RingKind::Gl(textures), Device::Gl(_)) => backends::gl::ring_texture_native(textures, slot),
#[cfg(has_webgl)]
(RingKind::Gl(textures), Device::WebGl(Some(_))) => backends::gl::ring_texture_webgl(textures, slot),
#[cfg(feature = "webgpu")]
(RingKind::WebGpu(textures), Device::WebGpu(_)) => backends::webgpu::ring_texture(textures, slot),
#[cfg(has_d3d12)]
(RingKind::D3d12(textures), Device::D3d12(device)) => backends::d3d12::ring_texture(textures, slot, device),
_ => None,
}
}
}
pub struct Surface {
pub(crate) kind: SurfaceKind,
pub(crate) cap_frame_rate: bool,
}
pub enum SurfaceOverlay {
#[cfg(feature = "webgpu")]
WebGpu(mln_webgpu::Overlay),
#[cfg(has_metal)]
Metal(mln_mtl::surface_target::Overlay),
#[cfg(has_vulkan)]
Vulkan(Box<dyn mln_vulkan::Overlay>),
#[cfg(has_d3d12)]
D3d12(Box<dyn mln_d3d12::Overlay>),
#[cfg(has_gl_window)]
Gl(mln_gl::Overlay),
}
pub(crate) enum SurfaceKind {
Window(Arc<dyn Window>),
#[cfg(has_metal)]
MetalLayer(mln_mtl::Layer),
#[cfg(has_native_webgpu)]
Wgpu(wgpu::Surface<'static>),
#[cfg(has_vulkan)]
Vulkan(mln_vulkan::HostSurface),
#[cfg(has_native_gl)]
Gl(mln_gl::HostSurface),
#[cfg(has_d3d12)]
D3d12Composition {
composition: mln_d3d12::Composition,
thread: std::thread::ThreadId,
},
#[cfg(target_family = "wasm")]
WebCanvas {
canvas: wasm_bindgen::JsValue,
format: Option<CanvasFormat>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CanvasFormat {
Bgra8Unorm,
Rgba8Unorm,
Rgba16Float,
}
impl CanvasFormat {
#[must_use]
pub fn from_name(name: &str) -> Option<CanvasFormat> {
match name {
"bgra8unorm" => Some(CanvasFormat::Bgra8Unorm),
"rgba8unorm" => Some(CanvasFormat::Rgba8Unorm),
"rgba16float" => Some(CanvasFormat::Rgba16Float),
_ => None,
}
}
}
impl Surface {
pub(super) fn of(kind: SurfaceKind) -> Surface {
Surface { kind, cap_frame_rate: true }
}
#[must_use]
pub fn without_frame_rate_cap(self) -> Surface {
Surface { cap_frame_rate: false, ..self }
}
pub fn window(window: impl Window + 'static) -> Surface {
Surface::of(SurfaceKind::Window(Arc::new(window)))
}
#[cfg(target_family = "wasm")]
pub fn web_canvas(canvas: wasm_bindgen::JsValue, format: Option<CanvasFormat>) -> Result<Surface, Error> {
use wasm_bindgen::JsCast as _;
if !canvas.is_instance_of::<web_sys::OffscreenCanvas>()
&& !canvas.is_instance_of::<web_sys::HtmlCanvasElement>()
{
return Err(Error::invalid_argument("a web canvas is an OffscreenCanvas or an HTMLCanvasElement"));
}
Ok(Surface::of(SurfaceKind::WebCanvas { canvas, format }))
}
#[must_use]
pub fn host_backend(&self) -> Option<BackendType> {
match self.kind {
#[cfg(has_vulkan)]
SurfaceKind::Vulkan(_) => Some(BackendType::Vulkan),
#[cfg(has_native_gl)]
SurfaceKind::Gl(_) => Some(BackendType::OpenGL),
_ => None,
}
}
pub(crate) fn clone_window(&self) -> Option<Surface> {
let kind = match &self.kind {
SurfaceKind::Window(window) => SurfaceKind::Window(Arc::clone(window)),
#[cfg(has_native_gl)]
SurfaceKind::Gl(surface) => SurfaceKind::Gl(*surface),
_ => return None,
};
Some(Surface { kind, cap_frame_rate: self.cap_frame_rate })
}
pub(crate) fn check_area(&self) -> Result<(), Error> {
if let SurfaceKind::Window(window) = &self.kind
&& !mln_gfx::window::has_area(&**window)
{
return Err(Error::invalid_state("the window is minimized or has no client area"));
}
Ok(())
}
#[cfg_attr(
not(all(feature = "gl", windows)),
expect(clippy::unnecessary_wraps, reason = "only OpenGL on Windows checks the window's pixel format")
)]
pub(crate) fn check_pixel_format(&self, device: Option<&Device>) -> Result<(), Error> {
#[cfg(all(feature = "gl", windows))]
if let SurfaceKind::Window(window) = &self.kind
&& device.map_or(cfg!(not(any(has_vulkan, has_d3d12))), |device| device.backend() == BackendType::OpenGL)
{
backends::gl::check_window_pixel_format(&**window)?;
}
#[cfg(not(all(feature = "gl", windows)))]
let _ = (self, device);
Ok(())
}
pub(crate) fn host_window(&self) -> Option<Arc<dyn Window>> {
match &self.kind {
SurfaceKind::Window(window) => Some(Arc::clone(window)),
_ => None,
}
}
pub(crate) fn needs_no_device(&self) -> bool {
match self.kind {
SurfaceKind::Window(_) => true,
#[cfg(has_metal)]
SurfaceKind::MetalLayer(_) => true,
#[cfg(has_native_webgpu)]
SurfaceKind::Wgpu(_) => false,
#[cfg(has_vulkan)]
SurfaceKind::Vulkan(_) => false,
#[cfg(has_native_gl)]
SurfaceKind::Gl(_) => false,
#[cfg(has_d3d12)]
SurfaceKind::D3d12Composition { .. } => true,
#[cfg(target_family = "wasm")]
SurfaceKind::WebCanvas { .. } => true,
}
}
}
#[cfg(not(target_family = "wasm"))]
pub(crate) struct SentSurface(Surface);
#[cfg(target_family = "wasm")]
pub(crate) enum SentSurface {
Window(Arc<dyn Window>),
WebCanvas { canvas: crate::web::HandedCanvas, format: Option<CanvasFormat> },
}
impl SentSurface {
pub(crate) fn new(surface: Surface, session: super::SessionId) -> SentSurface {
#[cfg(not(target_family = "wasm"))]
{
let _ = session;
SentSurface(surface)
}
#[cfg(target_family = "wasm")]
match surface.kind {
SurfaceKind::Window(window) => SentSurface::Window(window),
SurfaceKind::WebCanvas { canvas, format } => {
SentSurface::WebCanvas { canvas: crate::web::hand_canvas(session, canvas), format }
}
}
}
#[cfg_attr(
not(target_family = "wasm"),
expect(clippy::unnecessary_wraps, reason = "only the browser's canvases wait for their session's thread")
)]
pub(crate) fn receive(self) -> Result<Surface, Error> {
#[cfg(not(target_family = "wasm"))]
{
Ok(self.0)
}
#[cfg(target_family = "wasm")]
match self {
SentSurface::Window(window) => Ok(Surface::of(SurfaceKind::Window(window))),
SentSurface::WebCanvas { canvas, format } => {
let canvas = canvas.take().ok_or_else(|| Error::invalid_state("the surface's canvas is gone"))?;
Ok(Surface::of(SurfaceKind::WebCanvas { canvas, format }))
}
}
}
#[cfg(target_family = "wasm")]
pub(crate) fn presents_with_web_gpu(&self) -> bool {
matches!(self, SentSurface::WebCanvas { .. }) && cfg!(feature = "webgpu")
}
}
impl std::fmt::Debug for Surface {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Surface").finish_non_exhaustive()
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Device {
#[cfg(feature = "webgpu")]
WebGpu(mln_webgpu::Device),
#[cfg(has_metal)]
Metal(mln_mtl::Device),
#[cfg(has_vulkan)]
Vulkan(mln_vulkan::Device),
#[cfg(has_d3d12)]
D3d12(mln_d3d12::Device),
#[cfg(has_native_gl)]
Gl(mln_gl::Device),
#[cfg(has_webgl)]
WebGl(Option<mln_gl::WebGlDevice>),
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct DeviceOptions {
pub adapter: Option<Adapter>,
pub gl_api: GlApi,
pub gl_single_context: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GlApi {
Gles,
Gl,
}
impl Default for GlApi {
fn default() -> GlApi {
if cfg!(windows) { GlApi::Gl } else { GlApi::Gles }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Adapter {
Index(usize),
Uuid([u8; 16]),
Luid([u8; 8]),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SurfaceType {
MetalLayer,
Win32,
Xlib,
Xcb,
Wayland,
AndroidNdk,
OhosNdk,
}
impl SurfaceType {
#[must_use]
pub fn presents_with(self, backend: BackendType) -> bool {
let windowing = match self {
SurfaceType::MetalLayer => false,
SurfaceType::Win32 => cfg!(windows),
SurfaceType::Xlib | SurfaceType::Xcb | SurfaceType::Wayland => {
cfg!(all(unix, not(target_vendor = "apple"), not(target_os = "android"), not(target_env = "ohos")))
}
SurfaceType::AndroidNdk => cfg!(target_os = "android"),
SurfaceType::OhosNdk => cfg!(target_env = "ohos"),
};
match backend {
BackendType::Metal => cfg!(has_metal) && self == SurfaceType::MetalLayer,
BackendType::Vulkan => cfg!(has_vulkan) && windowing,
BackendType::OpenGL => {
cfg!(has_gl_window)
&& windowing
&& matches!(
self,
SurfaceType::Win32
| SurfaceType::Xlib
| SurfaceType::Wayland
| SurfaceType::AndroidNdk
| SurfaceType::OhosNdk
)
}
BackendType::WebGPU => cfg!(has_native_webgpu) && windowing && self != SurfaceType::OhosNdk,
BackendType::D3D12 => cfg!(has_d3d12) && self == SurfaceType::Win32,
}
}
#[must_use]
pub fn presents_with_any(self) -> bool {
[BackendType::Metal, BackendType::Vulkan, BackendType::D3D12, BackendType::OpenGL, BackendType::WebGPU]
.into_iter()
.any(|backend| self.presents_with(backend))
}
#[must_use]
pub fn presents_without_device(self) -> bool {
match self {
SurfaceType::MetalLayer => self.presents_with(BackendType::Metal),
_ => [BackendType::Vulkan, BackendType::D3D12, BackendType::OpenGL, BackendType::WebGPU]
.into_iter()
.any(|backend| self.presents_with(backend)),
}
}
}
#[cfg(not(target_family = "wasm"))]
fn missing(error: &Error) -> bool {
error.kind == mln_style::error::ErrorKind::Unsupported
}
#[derive(Debug, Default)]
pub(crate) struct DeviceLease {
#[cfg(has_vulkan)]
queue: Option<mln_vulkan::QueueLease>,
}
impl DeviceLease {
#[cfg_attr(
not(has_vulkan),
expect(clippy::unused_self, reason = "only a Vulkan device holds the host's queue lock")
)]
pub(crate) fn revoke(&self) {
#[cfg(has_vulkan)]
if let Some(queue) = &self.queue {
queue.revoke();
}
}
}
impl Device {
pub(crate) fn lease(self) -> (Device, DeviceLease) {
match self {
#[cfg(has_vulkan)]
Device::Vulkan(device) => {
let (device, queue) = device.lease();
(Device::Vulkan(device), DeviceLease { queue: Some(queue) })
}
device => (device, DeviceLease::default()),
}
}
pub(crate) fn lost(&self) -> Option<Error> {
match *self {
#[cfg(feature = "webgpu")]
Device::WebGpu(ref device) => device.lost().map(|message| Error::target_lost(message.to_owned())),
_ => None,
}
}
pub(crate) fn abandon_keeps_graphics(&self) -> bool {
match *self {
#[cfg(feature = "webgpu")]
Device::WebGpu(_) => true,
#[cfg(has_metal)]
Device::Metal(_) => false,
#[cfg(has_vulkan)]
Device::Vulkan(_) => false,
#[cfg(has_d3d12)]
Device::D3d12(_) => false,
#[cfg(has_native_gl)]
Device::Gl(_) => true,
#[cfg(has_webgl)]
Device::WebGl(_) => true,
}
}
pub fn new(options: DeviceOptions) -> Result<Device, Error> {
#[cfg(target_family = "wasm")]
{
let _ = options;
Err(Error::unsupported("the browser has no library devices"))
}
#[cfg(not(target_family = "wasm"))]
{
let mut error = Error::unsupported("this build has no backend");
for backend in Self::native_order() {
match Self::with_backend(*backend, options) {
Ok(device) => return Ok(device),
Err(e) if missing(&e) => error = e,
Err(e) => return Err(e),
}
}
Err(error)
}
}
pub fn with_backend(backend: BackendType, options: DeviceOptions) -> Result<Device, Error> {
match backend {
#[cfg(has_native_webgpu)]
BackendType::WebGPU => backends::webgpu::create_device(options.adapter),
#[cfg(has_metal)]
BackendType::Metal => backends::metal::create_device(options.adapter),
#[cfg(has_vulkan)]
BackendType::Vulkan => backends::vulkan::create_device(options.adapter),
#[cfg(has_d3d12)]
BackendType::D3D12 => backends::d3d12::create_device(options.adapter),
#[cfg(has_gl_device)]
BackendType::OpenGL => backends::gl::create_device(options),
#[cfg(has_webgl)]
BackendType::OpenGL if options.adapter.is_none() => Ok(Device::WebGl(None)),
_ => {
let _ = options;
Err(Error::unsupported(format!("this build has no {backend:?} backend")))
}
}
}
pub fn borrowed_sync_kinds(&self, ring: &BorrowedRing) -> Result<(SyncKinds, SyncKinds), Error> {
match (self, &ring.kind) {
#[cfg(has_vulkan)]
(Device::Vulkan(device), RingKind::Vulkan(images)) => backends::vulkan::borrowed_sync_kinds(device, images),
#[cfg(has_metal)]
(Device::Metal(device), RingKind::Metal(textures)) => {
backends::metal::borrowed_sync_kinds(device, textures)
}
#[cfg(has_native_gl)]
(Device::Gl(device), RingKind::Gl(_)) => backends::gl::borrowed_sync_kinds_native(device),
#[cfg(has_webgl)]
(Device::WebGl(device), RingKind::Gl(_)) => backends::gl::borrowed_sync_kinds_webgl(device.as_ref()),
#[cfg(feature = "webgpu")]
(Device::WebGpu(_), RingKind::WebGpu(textures)) => backends::webgpu::borrowed_sync_kinds(textures),
#[cfg(has_d3d12)]
(Device::D3d12(device), RingKind::D3d12(textures)) => {
backends::d3d12::borrowed_sync_kinds(device, textures)
}
_ => Err(Error::unsupported(format!(
"a {:?} device renders into no ring of {:?} textures",
self.backend(),
ring.backend()
))),
}
}
#[must_use]
pub fn depth_stencil_formats(&self) -> DepthStencilFormats {
match *self {
#[cfg(has_vulkan)]
Device::Vulkan(ref device) => device.depth_stencil_formats(),
#[cfg(has_d3d12)]
Device::D3d12(_) => mln_d3d12::BorrowedTextures::depth_stencil_formats(),
#[cfg(has_metal)]
Device::Metal(_) => mln_mtl::BorrowedTextures::depth_stencil_formats(),
#[cfg(has_native_gl)]
Device::Gl(_) => mln_gl::BorrowedTextures::depth_stencil_formats(),
#[cfg(has_webgl)]
Device::WebGl(Some(_)) => mln_gl::BorrowedTextures::depth_stencil_formats(),
#[cfg(feature = "webgpu")]
Device::WebGpu(ref device) => mln_webgpu::BorrowedTextures::depth_stencil_formats(device.device()),
#[cfg(target_family = "wasm")]
_ => DepthStencilFormats::NONE,
}
}
#[must_use]
pub fn depth_export_kinds(&self) -> ExportKinds {
match self {
#[cfg(has_vulkan)]
Device::Vulkan(device) => device.depth_export_kinds(),
_ => ExportKinds::NONE,
}
}
#[must_use]
pub fn export_kinds(&self) -> ExportKinds {
match self {
#[cfg(has_vulkan)]
Device::Vulkan(device) => device.export_kinds(),
#[cfg(has_metal)]
Device::Metal(device) => mln_mtl::exported_target::export_kinds(device),
#[cfg(has_gl_exported)]
Device::Gl(device) => mln_gl::exported_target::export_kinds(device),
#[cfg(has_d3d12)]
Device::D3d12(device) => device.export_kinds(),
_ => ExportKinds::NONE,
}
}
pub fn export_plan(&self, ring: &ExportedRing) -> Result<(ExportKind, SyncKinds, SyncKinds), Error> {
match self {
#[cfg(has_vulkan)]
Device::Vulkan(device) => backends::vulkan::export_plan(device, ring),
#[cfg(has_metal)]
Device::Metal(device) => backends::metal::export_plan(device, ring),
#[cfg(has_gl_exported)]
Device::Gl(device) => backends::gl::export_plan(device, ring),
#[cfg(has_d3d12)]
Device::D3d12(device) => backends::d3d12::export_plan(device, ring),
_ => {
let _ = ring;
Err(Error::unsupported(format!("a {:?} device exports no ring", self.backend())))
}
}
}
#[must_use]
pub fn export_sync_kinds(&self) -> (SyncKinds, SyncKinds) {
if self.export_kinds().is_empty() {
return (SyncKinds::NONE, SyncKinds::NONE);
}
match self {
#[cfg(has_vulkan)]
Device::Vulkan(device) => mln_vulkan::exported_target::sync_kinds(device),
#[cfg(has_metal)]
Device::Metal(device) => mln_mtl::exported_target::sync_kinds(device),
#[cfg(has_gl_exported)]
Device::Gl(device) => mln_gl::exported_target::sync_kinds(device),
#[cfg(has_d3d12)]
Device::D3d12(_) => mln_d3d12::exported_target::sync_kinds(),
_ => (SyncKinds::NONE, SyncKinds::NONE),
}
}
pub fn for_exported_ring(ring: &ExportedRing) -> Result<Device, Error> {
#[cfg(target_family = "wasm")]
{
let _ = ring;
Err(Error::unsupported("the browser has no library devices"))
}
#[cfg(not(target_family = "wasm"))]
{
let mut error = Error::unsupported("no backend of this build exports the ring's kinds of image");
for backend in Self::native_order() {
match Self::with_backend(*backend, DeviceOptions::default()) {
Ok(device) => match device.export_plan(ring) {
Ok(_) => return Ok(device),
Err(e) => error = e,
},
Err(e) if missing(&e) => {}
Err(e) => return Err(e),
}
}
Err(error)
}
}
#[must_use]
pub fn presents_to_host_surfaces(&self) -> bool {
match self {
#[cfg(has_vulkan)]
Device::Vulkan(device) => device.presents(),
#[cfg(has_native_gl)]
Device::Gl(device) => device.is_host(),
_ => false,
}
}
#[must_use]
pub fn presents_to(&self, surface: SurfaceType) -> bool {
if !surface.presents_with(self.backend()) {
return false;
}
match self {
#[cfg(has_vulkan)]
Device::Vulkan(device) => {
backends::vulkan::surface_extension(surface).is_some_and(|name| device.presents_through(name))
}
_ => true,
}
}
#[cfg(not(target_family = "wasm"))]
fn native_order() -> &'static [BackendType] {
if cfg!(target_os = "macos") {
&[BackendType::Metal, BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
} else if cfg!(target_vendor = "apple") {
&[BackendType::Metal, BackendType::WebGPU]
} else if cfg!(windows) {
&[BackendType::Vulkan, BackendType::D3D12, BackendType::OpenGL, BackendType::WebGPU]
} else {
&[BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
}
}
#[must_use]
pub fn backend(&self) -> BackendType {
match *self {
#[cfg(feature = "webgpu")]
Device::WebGpu(_) => BackendType::WebGPU,
#[cfg(has_metal)]
Device::Metal(_) => BackendType::Metal,
#[cfg(has_vulkan)]
Device::Vulkan(_) => BackendType::Vulkan,
#[cfg(has_d3d12)]
Device::D3d12(_) => BackendType::D3D12,
#[cfg(has_native_gl)]
Device::Gl(_) => BackendType::OpenGL,
#[cfg(has_webgl)]
Device::WebGl(_) => BackendType::OpenGL,
}
}
}