1#![allow(
8 unreachable_patterns,
9 irrefutable_let_patterns,
10 clippy::match_wildcard_for_single_variants,
11 reason = "the backends' variants depend on the build's features and target"
12)]
13
14use std::sync::Arc;
15
16use mln_gfx::Window;
17pub use mln_gfx::backend::Type as BackendType;
18pub use mln_gfx::{
19 ConsumerSync, DepthStencilFormat, DepthStencilFormats, DmaBufFormat, DmaBufPlane, ExportKind, ExportKinds,
20 ExportedImage, ExportedRing, FrameDepth, GpuSync, NativeTexture, SyncKind, SyncKinds,
21};
22use mln_util::error::Error;
23use mln_util::size::Size;
24
25pub type Wake = Arc<dyn Fn() + Send + Sync>;
28
29#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
31pub enum Driver {
32 #[default]
34 CoreWorker,
35 CallerThread,
38}
39
40#[derive(Clone, Default)]
42pub struct AttachOptions {
43 pub driver: Driver,
44 pub frame_wake: Option<Wake>,
48 pub redraw_wake: Option<Wake>,
53 pub driver_work_wake: Option<Wake>,
57 pub producer_sync_kinds: SyncKinds,
61}
62
63impl std::fmt::Debug for AttachOptions {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 f.debug_struct("AttachOptions")
66 .field("driver", &self.driver)
67 .field("frame_wake", &self.frame_wake.is_some())
68 .field("redraw_wake", &self.redraw_wake.is_some())
69 .field("driver_work_wake", &self.driver_work_wake.is_some())
70 .field("producer_sync_kinds", &self.producer_sync_kinds)
71 .finish()
72 }
73}
74
75#[derive(Debug)]
80pub enum RenderTarget {
81 Offscreen { size: Size, device: Option<Device> },
85 Surface { size: Size, surface: Surface, device: Option<Device> },
100 Borrowed { size: Size, ring: BorrowedRing, device: Device },
107 Exported { size: Size, ring: ExportedRing, device: Option<Device> },
118}
119
120#[derive(Clone, Debug, PartialEq)]
122pub struct BorrowedRing {
123 pub(crate) kind: RingKind,
124}
125
126#[derive(Clone, Debug, PartialEq)]
127pub(crate) enum RingKind {
128 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
129 Vulkan(mln_vulkan::BorrowedImages),
130 #[cfg(all(feature = "metal", target_vendor = "apple"))]
131 Metal(mln_mtl::BorrowedTextures),
132 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
133 Gl(mln_gl::BorrowedTextures),
134 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
135 WebGpu(mln_webgpu::BorrowedTextures),
136 #[cfg(all(feature = "d3d12", windows))]
137 D3d12(mln_d3d12::BorrowedTextures),
138}
139
140#[derive(Clone, Debug, PartialEq, Eq)]
143pub struct FrameTexture {
144 pub size: Size,
145 pub kind: FrameTextureKind,
146 pub native: Option<FrameTextureKind>,
149 pub depth: Option<FrameDepth>,
152}
153
154impl FrameTexture {
155 #[must_use]
158 pub fn native_kind(&self) -> Option<&FrameTextureKind> {
159 match &self.kind {
160 FrameTextureKind::Exported(_) => self.native.as_ref(),
161 kind => Some(kind),
162 }
163 }
164
165 pub(crate) fn exported(frame: mln_gfx::ExportedFrame, device: &Device) -> FrameTexture {
167 FrameTexture {
168 size: frame.size,
169 kind: FrameTextureKind::Exported(frame.image),
170 native: native_kind(frame.texture, device),
171 depth: frame.depth,
172 }
173 }
174}
175
176fn native_kind(texture: NativeTexture, device: &Device) -> Option<FrameTextureKind> {
178 match (texture, device) {
179 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
180 (NativeTexture::Vulkan { image, view, format, layout }, Device::Vulkan(device)) => {
181 use mln_vulkan::ash::vk::Handle as _;
182 Some(FrameTextureKind::Vulkan { image, view, device: device.raw().device.as_raw(), format, layout })
183 }
184 #[cfg(all(feature = "metal", target_vendor = "apple"))]
185 (NativeTexture::Metal { texture, pixel_format }, Device::Metal(device)) => Some(FrameTextureKind::Metal {
186 texture,
187 device: mln_mtl::objc2::rc::Retained::as_ptr(device.device()) as usize as u64,
188 pixel_format,
189 }),
190 (NativeTexture::OpenGl { texture, target, .. }, _) => Some(FrameTextureKind::OpenGl { texture, target }),
191 #[cfg(all(feature = "d3d12", windows))]
192 (NativeTexture::D3d12 { resource, format, state }, Device::D3d12(device)) => {
193 use mln_d3d12::windows::core::Interface as _;
194 Some(FrameTextureKind::D3d12 { resource, device: device.device().as_raw() as usize as u64, format, state })
195 }
196 _ => None,
197 }
198}
199
200#[derive(Clone, Debug, PartialEq, Eq)]
202#[non_exhaustive]
203pub enum FrameTextureKind {
204 Vulkan { image: u64, view: u64, device: u64, format: i32, layout: i32 },
207 Metal { texture: u64, device: u64, pixel_format: u64 },
209 OpenGl { texture: u32, target: u32 },
211 WebGpu,
213 D3d12 { resource: u64, device: u64, format: u32, state: u32 },
216 Exported(ExportedImage),
218}
219
220impl BorrowedRing {
221 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
223 #[must_use]
224 pub fn vulkan(images: mln_vulkan::BorrowedImages) -> BorrowedRing {
225 BorrowedRing { kind: RingKind::Vulkan(images) }
226 }
227
228 #[cfg(all(feature = "metal", target_vendor = "apple"))]
230 #[must_use]
231 pub fn metal(textures: mln_mtl::BorrowedTextures) -> BorrowedRing {
232 BorrowedRing { kind: RingKind::Metal(textures) }
233 }
234
235 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
238 #[must_use]
239 pub fn gl(textures: mln_gl::BorrowedTextures) -> BorrowedRing {
240 BorrowedRing { kind: RingKind::Gl(textures) }
241 }
242
243 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
246 #[must_use]
247 pub fn webgpu(textures: mln_webgpu::BorrowedTextures) -> BorrowedRing {
248 BorrowedRing { kind: RingKind::WebGpu(textures) }
249 }
250
251 #[cfg(all(feature = "d3d12", windows))]
254 #[must_use]
255 pub fn d3d12(textures: mln_d3d12::BorrowedTextures) -> BorrowedRing {
256 BorrowedRing { kind: RingKind::D3d12(textures) }
257 }
258
259 #[must_use]
261 pub fn backend(&self) -> BackendType {
262 match self.kind {
263 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
264 RingKind::Vulkan(_) => BackendType::Vulkan,
265 #[cfg(all(feature = "metal", target_vendor = "apple"))]
266 RingKind::Metal(_) => BackendType::Metal,
267 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
268 RingKind::Gl(_) => BackendType::OpenGL,
269 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
270 RingKind::WebGpu(_) => BackendType::WebGPU,
271 #[cfg(all(feature = "d3d12", windows))]
272 RingKind::D3d12(_) => BackendType::D3D12,
273 _ => BackendType::WebGPU,
275 }
276 }
277
278 #[must_use]
280 pub fn depth(&self) -> usize {
281 match &self.kind {
282 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
283 RingKind::Vulkan(images) => images.images.len(),
284 #[cfg(all(feature = "metal", target_vendor = "apple"))]
285 RingKind::Metal(textures) => textures.textures.len(),
286 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
287 RingKind::Gl(textures) => textures.textures.len(),
288 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
289 RingKind::WebGpu(textures) => textures.textures.len(),
290 #[cfg(all(feature = "d3d12", windows))]
291 RingKind::D3d12(textures) => textures.textures.len(),
292 _ => 0,
293 }
294 }
295
296 #[must_use]
298 pub fn physical_size(&self) -> Size {
299 match &self.kind {
300 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
301 RingKind::Vulkan(images) => images.size,
302 #[cfg(all(feature = "metal", target_vendor = "apple"))]
303 RingKind::Metal(textures) => textures.size,
304 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
305 RingKind::Gl(textures) => textures.size,
306 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
307 RingKind::WebGpu(textures) => textures.size,
308 #[cfg(all(feature = "d3d12", windows))]
309 RingKind::D3d12(textures) => textures.size,
310 _ => Size::default(),
311 }
312 }
313
314 #[must_use]
319 pub fn takes_depth_on(&self, device: &Device) -> bool {
320 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
323 if let RingKind::Gl(_) = self.kind {
324 return true;
325 }
326 !self.has_depth()
327 || self.depth_stencil_format().is_some_and(|format| device.depth_stencil_formats().contains(format))
328 }
329
330 #[must_use]
333 pub fn depth_stencil_format(&self) -> Option<DepthStencilFormat> {
334 match &self.kind {
335 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
336 RingKind::Vulkan(images) => {
337 images.depth.as_ref().and_then(|depth| mln_vulkan::device::depth_stencil_format(depth.format))
338 }
339 #[cfg(all(feature = "metal", target_vendor = "apple"))]
340 RingKind::Metal(textures) => (textures.depth_pixel_format()
341 == Some(mln_mtl::objc2_metal::MTLPixelFormat::Depth32Float_Stencil8))
342 .then_some(DepthStencilFormat::Depth32FloatStencil8),
343 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
344 RingKind::WebGpu(textures) => {
345 textures.depth_textures.first().and_then(|texture| mln_webgpu::depth_stencil_format(texture.format()))
346 }
347 #[cfg(all(feature = "d3d12", windows))]
348 RingKind::D3d12(textures) => textures.depth_format().and_then(mln_d3d12::depth_stencil_format),
349 _ => None,
350 }
351 }
352
353 #[must_use]
355 pub fn has_depth(&self) -> bool {
356 match &self.kind {
357 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
358 RingKind::Vulkan(images) => images.depth.is_some(),
359 #[cfg(all(feature = "metal", target_vendor = "apple"))]
360 RingKind::Metal(textures) => !textures.depth_textures.is_empty(),
361 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
362 RingKind::Gl(textures) => !textures.depth_textures.is_empty(),
363 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
364 RingKind::WebGpu(textures) => !textures.depth_textures.is_empty(),
365 #[cfg(all(feature = "d3d12", windows))]
366 RingKind::D3d12(textures) => !textures.depth_textures.is_empty(),
367 _ => false,
368 }
369 }
370
371 #[cfg_attr(target_family = "wasm", expect(unused_variables, reason = "the browser build borrows no ring"))]
373 pub(crate) fn same_texture(&self, slot: usize, other: &BorrowedRing, other_slot: usize) -> bool {
374 match (&self.kind, &other.kind) {
375 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
376 (RingKind::Vulkan(images), RingKind::Vulkan(others)) => images
377 .images
378 .get(slot)
379 .zip(others.images.get(other_slot))
380 .is_some_and(|(image, other)| image.image == other.image),
381 #[cfg(all(feature = "metal", target_vendor = "apple"))]
382 (RingKind::Metal(textures), RingKind::Metal(others)) => {
383 textures.textures.get(slot).zip(others.textures.get(other_slot)).is_some_and(|(texture, other)| {
384 std::ptr::eq(
385 mln_mtl::objc2::rc::Retained::as_ptr(texture),
386 mln_mtl::objc2::rc::Retained::as_ptr(other),
387 )
388 })
389 }
390 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
391 (RingKind::Gl(textures), RingKind::Gl(others)) => {
392 textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
393 }
394 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
395 (RingKind::WebGpu(textures), RingKind::WebGpu(others)) => {
396 textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
397 }
398 #[cfg(all(feature = "d3d12", windows))]
399 (RingKind::D3d12(textures), RingKind::D3d12(others)) => {
400 textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
401 }
402 _ => false,
403 }
404 }
405
406 #[cfg_attr(target_family = "wasm", expect(unused_variables, reason = "the browser build borrows no ring"))]
408 #[must_use]
409 pub fn texture(&self, slot: usize, device: &Device) -> Option<FrameTexture> {
410 match (&self.kind, device) {
411 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
412 (RingKind::Vulkan(images), Device::Vulkan(device)) => {
413 use mln_vulkan::ash::vk::Handle as _;
414 let depth = images.depth.as_ref().and_then(|depth| {
415 depth.images.get(slot).map(|image| FrameDepth {
416 texture: NativeTexture::Vulkan {
417 image: image.image.as_raw(),
418 view: image.view.as_raw(),
419 format: depth.format.as_raw(),
420 layout: depth.final_layout.as_raw(),
421 },
422 export: None,
423 })
424 });
425 images.images.get(slot).map(|image| FrameTexture {
426 size: images.size,
427 kind: FrameTextureKind::Vulkan {
428 image: image.image.as_raw(),
429 view: image.view.as_raw(),
430 device: device.raw().device.as_raw(),
431 format: images.format.as_raw(),
432 layout: images.final_layout.as_raw(),
433 },
434 native: None,
435 depth,
436 })
437 }
438 #[cfg(all(feature = "metal", target_vendor = "apple"))]
439 (RingKind::Metal(textures), Device::Metal(device)) => {
440 use mln_mtl::objc2::rc::Retained;
441 textures.textures.get(slot).map(|texture| FrameTexture {
442 size: textures.size,
443 kind: FrameTextureKind::Metal {
444 texture: Retained::as_ptr(texture) as usize as u64,
445 device: Retained::as_ptr(device.device()) as usize as u64,
446 pixel_format: textures.pixel_format().0 as u64,
447 },
448 native: None,
449 depth: textures.depth_textures.get(slot).zip(textures.depth_pixel_format()).map(
450 |(depth, pixel_format)| FrameDepth {
451 texture: NativeTexture::Metal {
452 texture: Retained::as_ptr(depth) as usize as u64,
453 pixel_format: pixel_format.0 as u64,
454 },
455 export: None,
456 },
457 ),
458 })
459 }
460 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
461 (RingKind::Gl(textures), Device::Gl(_)) => textures.textures.get(slot).map(|&texture| FrameTexture {
463 size: textures.size,
464 kind: FrameTextureKind::OpenGl { texture, target: 0x0DE1 },
465 native: None,
466 depth: textures.depth_textures.get(slot).map(|&depth| FrameDepth {
467 texture: NativeTexture::OpenGl { texture: depth, target: 0x0DE1, internal_format: 0 },
468 export: None,
469 }),
470 }),
471 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
472 (RingKind::WebGpu(textures), Device::WebGpu(_)) => textures.textures.get(slot).map(|_| FrameTexture {
473 size: textures.size,
474 kind: FrameTextureKind::WebGpu,
475 native: None,
476 depth: (!textures.depth_textures.is_empty())
477 .then_some(FrameDepth { texture: NativeTexture::WebGpu, export: None }),
478 }),
479 #[cfg(all(feature = "d3d12", windows))]
480 (RingKind::D3d12(textures), Device::D3d12(device)) => {
481 use mln_d3d12::windows::core::Interface as _;
482 let depth =
483 textures.depth_textures.get(slot).zip(textures.depth_format()).map(|(depth, format)| FrameDepth {
484 texture: NativeTexture::D3d12 {
485 resource: depth.as_raw() as usize as u64,
486 format: format.0 as u32,
487 state: textures.depth_state.0 as u32,
488 },
489 export: None,
490 });
491 textures.textures.get(slot).map(|texture| FrameTexture {
492 size: textures.size,
493 kind: FrameTextureKind::D3d12 {
494 resource: texture.as_raw() as usize as u64,
495 device: device.device().as_raw() as usize as u64,
496 format: textures.format().0 as u32,
497 state: textures.state.0 as u32,
498 },
499 native: None,
500 depth,
501 })
502 }
503 _ => None,
504 }
505 }
506}
507
508pub struct Surface {
513 pub(crate) kind: SurfaceKind,
514 pub(crate) cap_frame_rate: bool,
517}
518
519pub enum SurfaceOverlay {
524 #[cfg(feature = "webgpu")]
525 WebGpu(mln_webgpu::Overlay),
526 #[cfg(all(feature = "metal", target_vendor = "apple"))]
527 Metal(mln_mtl::surface_target::Overlay),
528 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
529 Vulkan(Box<dyn mln_vulkan::Overlay>),
530 #[cfg(all(feature = "d3d12", windows))]
531 D3d12(Box<dyn mln_d3d12::Overlay>),
532 #[cfg(all(feature = "gl", any(windows, all(unix, not(target_vendor = "apple")))))]
533 Gl(mln_gl::Overlay),
534}
535
536pub(crate) enum SurfaceKind {
537 Window(Arc<dyn Window>),
538 #[cfg(all(feature = "metal", target_vendor = "apple"))]
539 MetalLayer(mln_mtl::Layer),
540 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
541 Wgpu(wgpu::Surface<'static>),
542 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
544 Vulkan(mln_vulkan::HostSurface),
545 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
547 Gl(mln_gl::HostSurface),
548 #[cfg(all(feature = "d3d12", windows))]
552 D3d12Composition {
553 composition: mln_d3d12::Composition,
554 thread: std::thread::ThreadId,
555 },
556 #[cfg(target_family = "wasm")]
559 WebCanvas {
560 canvas: wasm_bindgen::JsValue,
561 format: Option<CanvasFormat>,
562 },
563}
564
565#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
567pub enum CanvasFormat {
568 Bgra8Unorm,
570 Rgba8Unorm,
572 Rgba16Float,
574}
575
576impl CanvasFormat {
577 #[must_use]
579 pub fn from_name(name: &str) -> Option<CanvasFormat> {
580 match name {
581 "bgra8unorm" => Some(CanvasFormat::Bgra8Unorm),
582 "rgba8unorm" => Some(CanvasFormat::Rgba8Unorm),
583 "rgba16float" => Some(CanvasFormat::Rgba16Float),
584 _ => None,
585 }
586 }
587}
588
589impl Surface {
590 fn of(kind: SurfaceKind) -> Surface {
591 Surface { kind, cap_frame_rate: true }
592 }
593
594 #[must_use]
600 pub fn without_frame_rate_cap(self) -> Surface {
601 Surface { cap_frame_rate: false, ..self }
602 }
603
604 pub fn window(window: impl Window + 'static) -> Surface {
610 Surface::of(SurfaceKind::Window(Arc::new(window)))
611 }
612
613 #[cfg(target_family = "wasm")]
628 pub fn web_canvas(canvas: wasm_bindgen::JsValue, format: Option<CanvasFormat>) -> Result<Surface, Error> {
629 use wasm_bindgen::JsCast as _;
630 if !canvas.is_instance_of::<web_sys::OffscreenCanvas>()
631 && !canvas.is_instance_of::<web_sys::HtmlCanvasElement>()
632 {
633 return Err(Error::invalid_argument("a web canvas is an OffscreenCanvas or an HTMLCanvasElement"));
634 }
635 Ok(Surface::of(SurfaceKind::WebCanvas { canvas, format }))
636 }
637
638 #[cfg(all(feature = "metal", target_vendor = "apple"))]
641 #[must_use]
642 pub fn metal_layer(layer: mln_mtl::Layer) -> Surface {
643 Surface::of(SurfaceKind::MetalLayer(layer))
644 }
645
646 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
649 pub fn wgpu(surface: wgpu::Surface<'static>) -> Surface {
650 Surface::of(SurfaceKind::Wgpu(surface))
651 }
652
653 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
656 #[must_use]
657 pub fn vulkan(surface: mln_vulkan::HostSurface) -> Surface {
658 Surface::of(SurfaceKind::Vulkan(surface))
659 }
660
661 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
664 #[must_use]
665 pub fn gl(surface: mln_gl::HostSurface) -> Surface {
666 Surface::of(SurfaceKind::Gl(surface))
667 }
668
669 #[cfg(all(feature = "d3d12", windows))]
683 #[must_use]
684 pub fn d3d12_composition(composition: mln_d3d12::Composition) -> Surface {
685 Surface::of(SurfaceKind::D3d12Composition { composition, thread: std::thread::current().id() })
686 }
687
688 #[must_use]
691 pub fn host_backend(&self) -> Option<BackendType> {
692 match self.kind {
693 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
694 SurfaceKind::Vulkan(_) => Some(BackendType::Vulkan),
695 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
696 SurfaceKind::Gl(_) => Some(BackendType::OpenGL),
697 _ => None,
698 }
699 }
700
701 pub(crate) fn clone_window(&self) -> Option<Surface> {
704 let kind = match &self.kind {
705 SurfaceKind::Window(window) => SurfaceKind::Window(Arc::clone(window)),
706 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
707 SurfaceKind::Gl(surface) => SurfaceKind::Gl(*surface),
708 _ => return None,
709 };
710 Some(Surface { kind, cap_frame_rate: self.cap_frame_rate })
711 }
712
713 pub(crate) fn check_area(&self) -> Result<(), Error> {
717 if let SurfaceKind::Window(window) = &self.kind
718 && !mln_gfx::window::has_area(&**window)
719 {
720 return Err(Error::invalid_state("the window is minimized or has no client area"));
721 }
722 Ok(())
723 }
724
725 #[cfg_attr(
732 not(all(feature = "gl", windows)),
733 expect(clippy::unnecessary_wraps, reason = "only OpenGL on Windows checks the window's pixel format")
734 )]
735 pub(crate) fn check_pixel_format(&self, device: Option<&Device>) -> Result<(), Error> {
736 #[cfg(all(feature = "gl", windows))]
737 if let SurfaceKind::Window(window) = &self.kind
738 && device.map_or(cfg!(not(any(feature = "vulkan", feature = "d3d12"))), |device| {
739 device.backend() == BackendType::OpenGL
740 })
741 {
742 mln_gl::SurfaceTarget::check_window(&**window)?;
743 }
744 #[cfg(not(all(feature = "gl", windows)))]
745 let _ = (self, device);
746 Ok(())
747 }
748
749 pub(crate) fn host_window(&self) -> Option<Arc<dyn Window>> {
751 match &self.kind {
752 SurfaceKind::Window(window) => Some(Arc::clone(window)),
753 _ => None,
754 }
755 }
756
757 pub(crate) fn needs_no_device(&self) -> bool {
759 match self.kind {
760 SurfaceKind::Window(_) => true,
761 #[cfg(all(feature = "metal", target_vendor = "apple"))]
762 SurfaceKind::MetalLayer(_) => true,
763 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
764 SurfaceKind::Wgpu(_) => false,
765 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
766 SurfaceKind::Vulkan(_) => false,
767 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
768 SurfaceKind::Gl(_) => false,
769 #[cfg(all(feature = "d3d12", windows))]
770 SurfaceKind::D3d12Composition { .. } => true,
771 #[cfg(target_family = "wasm")]
772 SurfaceKind::WebCanvas { .. } => true,
773 }
774 }
775}
776
777#[cfg(not(target_family = "wasm"))]
783pub(crate) struct SentSurface(Surface);
784
785#[cfg(target_family = "wasm")]
791pub(crate) enum SentSurface {
792 Window(Arc<dyn Window>),
793 WebCanvas { canvas: crate::web::HandedCanvas, format: Option<CanvasFormat> },
794}
795
796impl SentSurface {
797 pub(crate) fn new(surface: Surface, session: super::SessionId) -> SentSurface {
799 #[cfg(not(target_family = "wasm"))]
800 {
801 let _ = session;
802 SentSurface(surface)
803 }
804 #[cfg(target_family = "wasm")]
805 match surface.kind {
806 SurfaceKind::Window(window) => SentSurface::Window(window),
807 SurfaceKind::WebCanvas { canvas, format } => {
808 SentSurface::WebCanvas { canvas: crate::web::hand_canvas(session, canvas), format }
809 }
810 }
811 }
812
813 #[cfg_attr(
817 not(target_family = "wasm"),
818 expect(clippy::unnecessary_wraps, reason = "only the browser's canvases wait for their session's thread")
819 )]
820 pub(crate) fn receive(self) -> Result<Surface, Error> {
821 #[cfg(not(target_family = "wasm"))]
822 {
823 Ok(self.0)
824 }
825 #[cfg(target_family = "wasm")]
826 match self {
827 SentSurface::Window(window) => Ok(Surface::of(SurfaceKind::Window(window))),
828 SentSurface::WebCanvas { canvas, format } => {
829 let canvas = canvas.take().ok_or_else(|| Error::invalid_state("the surface's canvas is gone"))?;
830 Ok(Surface::of(SurfaceKind::WebCanvas { canvas, format }))
831 }
832 }
833 }
834
835 #[cfg(target_family = "wasm")]
838 pub(crate) fn presents_with_web_gpu(&self) -> bool {
839 matches!(self, SentSurface::WebCanvas { .. }) && cfg!(feature = "webgpu")
840 }
841}
842
843impl std::fmt::Debug for Surface {
844 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
845 f.debug_struct("Surface").finish_non_exhaustive()
846 }
847}
848
849#[derive(Clone, Debug)]
853#[non_exhaustive]
854pub enum Device {
855 #[cfg(feature = "webgpu")]
859 WebGpu(mln_webgpu::Device),
860 #[cfg(all(feature = "metal", target_vendor = "apple"))]
862 Metal(mln_mtl::Device),
863 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
865 Vulkan(mln_vulkan::Device),
866 #[cfg(all(feature = "d3d12", windows))]
868 D3d12(mln_d3d12::Device),
869 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
872 Gl(mln_gl::Device),
873 #[cfg(all(feature = "gl", target_family = "wasm"))]
876 WebGl,
877}
878
879#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
881pub struct DeviceOptions {
882 pub adapter: Option<Adapter>,
884 pub gl_api: GlApi,
888 pub gl_single_context: bool,
891}
892
893#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
896pub enum GlApi {
897 Gles,
899 Gl,
901}
902
903impl Default for GlApi {
904 fn default() -> GlApi {
905 if cfg!(windows) { GlApi::Gl } else { GlApi::Gles }
906 }
907}
908
909#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
911pub enum Adapter {
912 Index(usize),
914 Uuid([u8; 16]),
917 Luid([u8; 8]),
921}
922
923#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
925#[non_exhaustive]
926pub enum SurfaceType {
927 MetalLayer,
929 Win32,
931 Xlib,
933 Xcb,
935 Wayland,
937 AndroidNdk,
939 OhosNdk,
941}
942
943impl SurfaceType {
944 #[must_use]
947 pub fn presents_with(self, backend: BackendType) -> bool {
948 let windowing = match self {
949 SurfaceType::MetalLayer => false,
950 SurfaceType::Win32 => cfg!(windows),
951 SurfaceType::Xlib | SurfaceType::Xcb | SurfaceType::Wayland => {
952 cfg!(all(unix, not(target_vendor = "apple"), not(target_os = "android"), not(target_env = "ohos")))
953 }
954 SurfaceType::AndroidNdk => cfg!(target_os = "android"),
955 SurfaceType::OhosNdk => cfg!(target_env = "ohos"),
956 };
957 match backend {
958 BackendType::Metal => {
959 cfg!(all(feature = "metal", target_vendor = "apple")) && self == SurfaceType::MetalLayer
960 }
961 BackendType::Vulkan => {
962 cfg!(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm")))) && windowing
963 }
964 BackendType::OpenGL => {
967 cfg!(all(feature = "gl", any(windows, all(unix, not(target_vendor = "apple")))))
968 && windowing
969 && matches!(
970 self,
971 SurfaceType::Win32
972 | SurfaceType::Xlib
973 | SurfaceType::Wayland
974 | SurfaceType::AndroidNdk
975 | SurfaceType::OhosNdk
976 )
977 }
978 BackendType::WebGPU => {
981 cfg!(all(feature = "webgpu", not(target_family = "wasm"))) && windowing && self != SurfaceType::OhosNdk
982 }
983 BackendType::D3D12 => cfg!(all(feature = "d3d12", windows)) && self == SurfaceType::Win32,
984 }
985 }
986
987 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
989 fn vulkan_extension(self) -> Option<&'static std::ffi::CStr> {
990 use mln_vulkan::ash::khr;
991 match self {
992 SurfaceType::MetalLayer => None,
993 SurfaceType::Win32 => Some(khr::win32_surface::NAME),
994 SurfaceType::Xlib => Some(khr::xlib_surface::NAME),
995 SurfaceType::Xcb => Some(khr::xcb_surface::NAME),
996 SurfaceType::Wayland => Some(khr::wayland_surface::NAME),
997 SurfaceType::AndroidNdk => Some(khr::android_surface::NAME),
998 SurfaceType::OhosNdk => Some(mln_vulkan::device::OHOS_SURFACE),
999 }
1000 }
1001
1002 #[must_use]
1005 pub fn presents_with_any(self) -> bool {
1006 [BackendType::Metal, BackendType::Vulkan, BackendType::D3D12, BackendType::OpenGL, BackendType::WebGPU]
1007 .into_iter()
1008 .any(|backend| self.presents_with(backend))
1009 }
1010
1011 #[must_use]
1015 pub fn presents_without_device(self) -> bool {
1016 match self {
1017 SurfaceType::MetalLayer => self.presents_with(BackendType::Metal),
1018 _ if cfg!(target_vendor = "apple") => self.presents_with(BackendType::WebGPU),
1019 _ => [BackendType::Vulkan, BackendType::D3D12, BackendType::OpenGL, BackendType::WebGPU]
1020 .into_iter()
1021 .any(|backend| self.presents_with(backend)),
1022 }
1023 }
1024}
1025
1026#[cfg(not(target_family = "wasm"))]
1029fn missing(error: &Error) -> bool {
1030 error.kind == mln_util::ErrorKind::Unsupported
1031}
1032
1033#[derive(Debug, Default)]
1036pub(crate) struct DeviceLease {
1037 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1038 queue: Option<mln_vulkan::QueueLease>,
1039}
1040
1041impl DeviceLease {
1042 #[cfg_attr(
1045 not(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm")))),
1046 expect(clippy::unused_self, reason = "only a Vulkan device holds the host's queue lock")
1047 )]
1048 pub(crate) fn revoke(&self) {
1049 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1050 if let Some(queue) = &self.queue {
1051 queue.revoke();
1052 }
1053 }
1054}
1055
1056impl Device {
1057 pub(crate) fn lease(self) -> (Device, DeviceLease) {
1062 match self {
1063 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1064 Device::Vulkan(device) => {
1065 let (device, queue) = device.lease();
1066 (Device::Vulkan(device), DeviceLease { queue: Some(queue) })
1067 }
1068 device => (device, DeviceLease::default()),
1069 }
1070 }
1071
1072 pub(crate) fn lost(&self) -> Option<Error> {
1075 match *self {
1076 #[cfg(feature = "webgpu")]
1077 Device::WebGpu(ref device) => device.lost().map(|message| Error::target_lost(message.to_owned())),
1078 _ => None,
1079 }
1080 }
1081
1082 pub(crate) fn abandon_keeps_graphics(&self) -> bool {
1088 match *self {
1089 #[cfg(feature = "webgpu")]
1090 Device::WebGpu(_) => true,
1091 #[cfg(all(feature = "metal", target_vendor = "apple"))]
1092 Device::Metal(_) => false,
1093 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1094 Device::Vulkan(_) => false,
1095 #[cfg(all(feature = "d3d12", windows))]
1096 Device::D3d12(_) => false,
1097 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
1098 Device::Gl(_) => true,
1099 #[cfg(all(feature = "gl", target_family = "wasm"))]
1100 Device::WebGl => true,
1101 }
1102 }
1103
1104 pub fn new(options: DeviceOptions) -> Result<Device, Error> {
1120 #[cfg(target_family = "wasm")]
1121 {
1122 let _ = options;
1123 Err(Error::unsupported("the browser has no library devices"))
1124 }
1125 #[cfg(not(target_family = "wasm"))]
1126 {
1127 let mut error = Error::unsupported("this build has no backend");
1128 for backend in Self::native_order() {
1129 match Self::with_backend(*backend, options) {
1130 Ok(device) => return Ok(device),
1131 Err(e) if missing(&e) => error = e,
1132 Err(e) => return Err(e),
1133 }
1134 }
1135 Err(error)
1136 }
1137 }
1138
1139 pub fn with_backend(backend: BackendType, options: DeviceOptions) -> Result<Device, Error> {
1148 let DeviceOptions { adapter, gl_api, gl_single_context } = options;
1149 let index = |adapter: Option<Adapter>| match adapter {
1151 None => Ok(None),
1152 Some(Adapter::Index(index)) => Ok(Some(index)),
1153 Some(_) => Err(Error::unsupported(format!("{backend:?} names no GPU by UUID or LUID"))),
1154 };
1155 match backend {
1156 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
1157 BackendType::WebGPU => mln_webgpu::Device::new(index(adapter)?).map(Device::WebGpu),
1158 #[cfg(all(feature = "metal", target_vendor = "apple"))]
1159 BackendType::Metal => mln_mtl::Device::new(index(adapter)?).map(Device::Metal),
1160 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1161 BackendType::Vulkan => match adapter {
1162 Some(Adapter::Uuid(uuid)) => mln_vulkan::Device::with_adapter_id(mln_vulkan::AdapterId::Uuid(uuid)),
1163 Some(Adapter::Luid(luid)) => mln_vulkan::Device::with_adapter_id(mln_vulkan::AdapterId::Luid(luid)),
1164 _ => mln_vulkan::Device::new(index(adapter)?),
1165 }
1166 .map(Device::Vulkan),
1167 #[cfg(all(feature = "d3d12", windows))]
1168 BackendType::D3D12 => match adapter {
1169 Some(Adapter::Luid(luid)) => mln_d3d12::Device::with_luid(luid),
1170 Some(Adapter::Uuid(_)) => Err(Error::unsupported("D3D12 names no GPU by UUID")),
1171 _ => mln_d3d12::Device::new(index(adapter)?),
1172 }
1173 .map(Device::D3d12),
1174 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
1175 BackendType::OpenGL => {
1176 match index(adapter)? {
1179 None | Some(0) => {}
1180 Some(index) => {
1181 return Err(Error::not_found(format!("adapter_index {index} is past OpenGL's one GPU")));
1182 }
1183 }
1184 let api = match gl_api {
1185 GlApi::Gles => mln_gl::Api::Gles,
1186 GlApi::Gl => mln_gl::Api::Gl,
1187 };
1188 let options = mln_gl::DeviceOptions { api, single_context: gl_single_context };
1189 mln_gl::Device::with_options(options).map(Device::Gl)
1190 }
1191 #[cfg(all(feature = "gl", target_family = "wasm"))]
1192 BackendType::OpenGL if adapter.is_none() => Ok(Device::WebGl),
1193 _ => {
1194 let _ = (index, adapter, gl_api, gl_single_context);
1195 Err(Error::unsupported(format!("this build has no {backend:?} backend")))
1196 }
1197 }
1198 }
1199
1200 pub fn borrowed_sync_kinds(&self, ring: &BorrowedRing) -> Result<(SyncKinds, SyncKinds), Error> {
1208 match (self, &ring.kind) {
1209 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1210 (Device::Vulkan(device), RingKind::Vulkan(images)) => {
1211 images.check(device)?;
1212 Ok(device.sync_kinds())
1213 }
1214 #[cfg(all(feature = "metal", target_vendor = "apple"))]
1215 (Device::Metal(device), RingKind::Metal(textures)) => {
1216 textures.check(device)?;
1217 Ok(mln_mtl::BorrowedTextures::sync_kinds(device))
1218 }
1219 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
1220 (Device::Gl(device), RingKind::Gl(_)) => {
1221 if !device.is_host() {
1222 return Err(Error::unsupported(
1223 "an OpenGL ring renders on a device in the share group of the host's context",
1224 ));
1225 }
1226 Ok(mln_gl::BorrowedTextures::sync_kinds())
1227 }
1228 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
1229 (Device::WebGpu(_), RingKind::WebGpu(textures)) => {
1230 textures.check()?;
1231 Ok(mln_webgpu::BorrowedTextures::sync_kinds())
1232 }
1233 #[cfg(all(feature = "d3d12", windows))]
1234 (Device::D3d12(device), RingKind::D3d12(textures)) => {
1235 textures.check(device)?;
1236 Ok(mln_d3d12::BorrowedTextures::sync_kinds())
1237 }
1238 _ => Err(Error::unsupported(format!(
1239 "a {:?} device renders into no ring of {:?} textures",
1240 self.backend(),
1241 ring.backend()
1242 ))),
1243 }
1244 }
1245
1246 #[must_use]
1249 pub fn depth_stencil_formats(&self) -> DepthStencilFormats {
1250 match *self {
1251 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1252 Device::Vulkan(ref device) => device.depth_stencil_formats(),
1253 #[cfg(all(feature = "d3d12", windows))]
1254 Device::D3d12(_) => mln_d3d12::BorrowedTextures::depth_stencil_formats(),
1255 #[cfg(all(feature = "metal", target_vendor = "apple"))]
1256 Device::Metal(_) => mln_mtl::BorrowedTextures::depth_stencil_formats(),
1257 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
1258 Device::Gl(_) => mln_gl::BorrowedTextures::depth_stencil_formats(),
1259 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
1260 Device::WebGpu(ref device) => mln_webgpu::BorrowedTextures::depth_stencil_formats(device.device()),
1261 #[cfg(target_family = "wasm")]
1262 _ => DepthStencilFormats::NONE,
1263 }
1264 }
1265
1266 #[must_use]
1272 pub fn depth_export_kinds(&self) -> ExportKinds {
1273 match self {
1274 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1275 Device::Vulkan(device) => device.depth_export_kinds(),
1276 _ => ExportKinds::NONE,
1277 }
1278 }
1279
1280 #[must_use]
1283 pub fn export_kinds(&self) -> ExportKinds {
1284 match self {
1285 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1286 Device::Vulkan(device) => device.export_kinds(),
1287 #[cfg(all(feature = "metal", target_vendor = "apple"))]
1288 Device::Metal(device) => mln_mtl::exported_target::export_kinds(device),
1289 #[cfg(all(feature = "gl", unix, not(target_vendor = "apple")))]
1290 Device::Gl(device) => mln_gl::exported_target::export_kinds(device),
1291 #[cfg(all(feature = "d3d12", windows))]
1292 Device::D3d12(device) => device.export_kinds(),
1293 _ => ExportKinds::NONE,
1294 }
1295 }
1296
1297 pub fn export_plan(&self, ring: &ExportedRing) -> Result<(ExportKind, SyncKinds, SyncKinds), Error> {
1304 match self {
1305 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1306 Device::Vulkan(device) => {
1307 let kind = mln_vulkan::exported_target::plan(device, ring)?;
1308 let (producer, consumer) = mln_vulkan::exported_target::sync_kinds(device);
1309 Ok((kind, producer, consumer))
1310 }
1311 #[cfg(all(feature = "metal", target_vendor = "apple"))]
1312 Device::Metal(device) => {
1313 let kind = mln_mtl::exported_target::plan(device, ring)?;
1314 let (producer, consumer) = mln_mtl::exported_target::sync_kinds(device);
1315 Ok((kind, producer, consumer))
1316 }
1317 #[cfg(all(feature = "gl", unix, not(target_vendor = "apple")))]
1318 Device::Gl(device) => {
1319 let kind = mln_gl::exported_target::plan(device, ring)?;
1320 let (producer, consumer) = mln_gl::exported_target::sync_kinds(device);
1321 Ok((kind, producer, consumer))
1322 }
1323 #[cfg(all(feature = "d3d12", windows))]
1324 Device::D3d12(device) => {
1325 let kind = mln_d3d12::exported_target::plan(device, ring)?;
1326 let (producer, consumer) = mln_d3d12::exported_target::sync_kinds();
1327 Ok((kind, producer, consumer))
1328 }
1329 _ => {
1330 let _ = ring;
1331 Err(Error::unsupported(format!("a {:?} device exports no ring", self.backend())))
1332 }
1333 }
1334 }
1335
1336 #[must_use]
1340 pub fn export_sync_kinds(&self) -> (SyncKinds, SyncKinds) {
1341 if self.export_kinds().is_empty() {
1342 return (SyncKinds::NONE, SyncKinds::NONE);
1343 }
1344 match self {
1345 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1346 Device::Vulkan(device) => mln_vulkan::exported_target::sync_kinds(device),
1347 #[cfg(all(feature = "metal", target_vendor = "apple"))]
1348 Device::Metal(device) => mln_mtl::exported_target::sync_kinds(device),
1349 #[cfg(all(feature = "gl", unix, not(target_vendor = "apple")))]
1350 Device::Gl(device) => mln_gl::exported_target::sync_kinds(device),
1351 #[cfg(all(feature = "d3d12", windows))]
1352 Device::D3d12(_) => mln_d3d12::exported_target::sync_kinds(),
1353 _ => (SyncKinds::NONE, SyncKinds::NONE),
1354 }
1355 }
1356
1357 pub fn for_exported_ring(ring: &ExportedRing) -> Result<Device, Error> {
1363 #[cfg(target_family = "wasm")]
1364 {
1365 let _ = ring;
1366 Err(Error::unsupported("the browser has no library devices"))
1367 }
1368 #[cfg(not(target_family = "wasm"))]
1369 {
1370 let mut error = Error::unsupported("no backend of this build exports the ring's kinds of image");
1371 for backend in Self::native_order() {
1372 match Self::with_backend(*backend, DeviceOptions::default()) {
1373 Ok(device) => match device.export_plan(ring) {
1374 Ok(_) => return Ok(device),
1375 Err(e) => error = e,
1376 },
1377 Err(e) if missing(&e) => {}
1378 Err(e) => return Err(e),
1379 }
1380 }
1381 Err(error)
1382 }
1383 }
1384
1385 #[must_use]
1389 pub fn presents_to_host_surfaces(&self) -> bool {
1390 match self {
1391 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1392 Device::Vulkan(device) => device.presents(),
1393 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
1394 Device::Gl(device) => device.is_host(),
1395 _ => false,
1396 }
1397 }
1398
1399 #[must_use]
1403 pub fn presents_to(&self, surface: SurfaceType) -> bool {
1404 if !surface.presents_with(self.backend()) {
1405 return false;
1406 }
1407 match self {
1408 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1409 Device::Vulkan(device) => surface.vulkan_extension().is_some_and(|name| device.presents_through(name)),
1410 _ => true,
1411 }
1412 }
1413
1414 #[cfg(not(target_family = "wasm"))]
1417 fn native_order() -> &'static [BackendType] {
1418 if cfg!(target_vendor = "apple") {
1419 &[BackendType::Metal, BackendType::WebGPU]
1420 } else if cfg!(windows) {
1421 &[BackendType::Vulkan, BackendType::D3D12, BackendType::OpenGL, BackendType::WebGPU]
1422 } else {
1423 &[BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
1424 }
1425 }
1426
1427 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
1431 #[must_use]
1432 pub fn webgpu(
1433 instance: wgpu::Instance,
1434 device: wgpu::Device,
1435 queue: wgpu::Queue,
1436 format: wgpu::TextureFormat,
1437 ) -> Device {
1438 Device::WebGpu(mln_webgpu::Device::from_wgpu(Some(instance), device, queue, Some(format)))
1439 }
1440
1441 #[must_use]
1443 pub fn backend(&self) -> BackendType {
1444 match *self {
1445 #[cfg(feature = "webgpu")]
1446 Device::WebGpu(_) => BackendType::WebGPU,
1447 #[cfg(all(feature = "metal", target_vendor = "apple"))]
1448 Device::Metal(_) => BackendType::Metal,
1449 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
1450 Device::Vulkan(_) => BackendType::Vulkan,
1451 #[cfg(all(feature = "d3d12", windows))]
1452 Device::D3d12(_) => BackendType::D3D12,
1453 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
1454 Device::Gl(_) => BackendType::OpenGL,
1455 #[cfg(all(feature = "gl", target_family = "wasm"))]
1456 Device::WebGl => BackendType::OpenGL,
1457 }
1458 }
1459
1460 #[cfg(all(feature = "gl", target_family = "wasm"))]
1470 pub fn web_gl(context: &wasm_bindgen::JsValue) -> Result<Device, Error> {
1471 if !wasm_bindgen::JsCast::is_instance_of::<web_sys::WebGl2RenderingContext>(context) {
1472 return Err(Error::invalid_argument("a WebGL device wraps a WebGL2RenderingContext"));
1473 }
1474 Ok(Device::WebGl)
1475 }
1476
1477 #[cfg(all(feature = "webgpu", target_family = "wasm"))]
1483 pub async fn request_web_gpu() -> Result<Device, Error> {
1484 mln_webgpu::Device::request().await.map(Device::WebGpu)
1485 }
1486}