1use std::sync::Arc;
4
5use mln_gfx::Window;
6pub use mln_gfx::backend::Type as BackendType;
7pub use mln_gfx::{
8 ConsumerSync, DmaBufFormat, DmaBufPlane, ExportKind, ExportKinds, ExportedImage, ExportedRing, GpuSync, SyncKind,
9 SyncKinds,
10};
11use mln_util::error::Error;
12use mln_util::size::Size;
13
14pub type Wake = Arc<dyn Fn() + Send + Sync>;
17
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
20pub enum Driver {
21 #[default]
23 CoreWorker,
24 CallerThread,
27}
28
29#[derive(Clone, Default)]
31pub struct AttachOptions {
32 pub driver: Driver,
33 pub frame_wake: Option<Wake>,
37 pub redraw_wake: Option<Wake>,
40 pub driver_work_wake: Option<Wake>,
44 pub producer_sync_kinds: SyncKinds,
48}
49
50impl std::fmt::Debug for AttachOptions {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.debug_struct("AttachOptions")
53 .field("driver", &self.driver)
54 .field("frame_wake", &self.frame_wake.is_some())
55 .field("redraw_wake", &self.redraw_wake.is_some())
56 .field("driver_work_wake", &self.driver_work_wake.is_some())
57 .field("producer_sync_kinds", &self.producer_sync_kinds)
58 .finish()
59 }
60}
61
62#[derive(Debug)]
67pub enum RenderTarget {
68 Offscreen { size: Size, device: Option<Device> },
72 Surface { size: Size, surface: Surface, device: Option<Device> },
87 Borrowed { size: Size, ring: BorrowedRing, device: Device },
94 Exported { size: Size, ring: ExportedRing, device: Option<Device> },
105}
106
107#[derive(Clone, Debug, PartialEq)]
109pub struct BorrowedRing {
110 pub(crate) kind: RingKind,
111}
112
113#[derive(Clone, Debug, PartialEq)]
114pub(crate) enum RingKind {
115 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
116 Vulkan(mln_vulkan::BorrowedImages),
117 #[cfg(all(feature = "metal", target_vendor = "apple"))]
118 Metal(mln_mtl::BorrowedTextures),
119 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
120 Gl(mln_gl::BorrowedTextures),
121 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
122 WebGpu(mln_webgpu::BorrowedTextures),
123}
124
125#[derive(Clone, Debug, PartialEq, Eq)]
128pub struct FrameTexture {
129 pub size: Size,
130 pub kind: FrameTextureKind,
131}
132
133#[derive(Clone, Debug, PartialEq, Eq)]
135#[non_exhaustive]
136pub enum FrameTextureKind {
137 Vulkan { image: u64, view: u64, device: u64, format: i32, layout: i32 },
140 Metal { texture: u64, device: u64, pixel_format: u64 },
142 OpenGl { texture: u32, target: u32 },
144 WebGpu,
146 Exported(ExportedImage),
148}
149
150impl BorrowedRing {
151 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
153 #[must_use]
154 pub fn vulkan(images: mln_vulkan::BorrowedImages) -> BorrowedRing {
155 BorrowedRing { kind: RingKind::Vulkan(images) }
156 }
157
158 #[cfg(all(feature = "metal", target_vendor = "apple"))]
160 pub fn metal(textures: mln_mtl::BorrowedTextures) -> BorrowedRing {
161 BorrowedRing { kind: RingKind::Metal(textures) }
162 }
163
164 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
167 #[must_use]
168 pub fn gl(textures: mln_gl::BorrowedTextures) -> BorrowedRing {
169 BorrowedRing { kind: RingKind::Gl(textures) }
170 }
171
172 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
175 #[must_use]
176 pub fn webgpu(textures: mln_webgpu::BorrowedTextures) -> BorrowedRing {
177 BorrowedRing { kind: RingKind::WebGpu(textures) }
178 }
179
180 #[must_use]
182 pub fn backend(&self) -> BackendType {
183 match self.kind {
184 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
185 RingKind::Vulkan(_) => BackendType::Vulkan,
186 #[cfg(all(feature = "metal", target_vendor = "apple"))]
187 RingKind::Metal(_) => BackendType::Metal,
188 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
189 RingKind::Gl(_) => BackendType::OpenGL,
190 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
191 RingKind::WebGpu(_) => BackendType::WebGPU,
192 #[allow(unreachable_patterns)]
194 _ => BackendType::WebGPU,
195 }
196 }
197
198 #[must_use]
200 pub fn depth(&self) -> usize {
201 match &self.kind {
202 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
203 RingKind::Vulkan(images) => images.images.len(),
204 #[cfg(all(feature = "metal", target_vendor = "apple"))]
205 RingKind::Metal(textures) => textures.textures.len(),
206 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
207 RingKind::Gl(textures) => textures.textures.len(),
208 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
209 RingKind::WebGpu(textures) => textures.textures.len(),
210 #[allow(unreachable_patterns)]
211 _ => 0,
212 }
213 }
214
215 #[must_use]
217 pub fn physical_size(&self) -> Size {
218 match &self.kind {
219 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
220 RingKind::Vulkan(images) => images.size,
221 #[cfg(all(feature = "metal", target_vendor = "apple"))]
222 RingKind::Metal(textures) => textures.size,
223 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
224 RingKind::Gl(textures) => textures.size,
225 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
226 RingKind::WebGpu(textures) => textures.size,
227 #[allow(unreachable_patterns)]
228 _ => Size::default(),
229 }
230 }
231
232 #[cfg_attr(target_family = "wasm", allow(unused_variables))]
234 pub(crate) fn same_texture(&self, slot: usize, other: &BorrowedRing, other_slot: usize) -> bool {
235 #[allow(unreachable_patterns)]
236 match (&self.kind, &other.kind) {
237 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
238 (RingKind::Vulkan(images), RingKind::Vulkan(others)) => images
239 .images
240 .get(slot)
241 .zip(others.images.get(other_slot))
242 .is_some_and(|(image, other)| image.image == other.image),
243 #[cfg(all(feature = "metal", target_vendor = "apple"))]
244 (RingKind::Metal(textures), RingKind::Metal(others)) => {
245 textures.textures.get(slot).zip(others.textures.get(other_slot)).is_some_and(|(texture, other)| {
246 std::ptr::eq(
247 mln_mtl::objc2::rc::Retained::as_ptr(texture),
248 mln_mtl::objc2::rc::Retained::as_ptr(other),
249 )
250 })
251 }
252 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
253 (RingKind::Gl(textures), RingKind::Gl(others)) => {
254 textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
255 }
256 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
257 (RingKind::WebGpu(textures), RingKind::WebGpu(others)) => {
258 textures.textures.get(slot).is_some_and(|texture| others.textures.get(other_slot) == Some(texture))
259 }
260 _ => false,
261 }
262 }
263
264 #[cfg_attr(target_family = "wasm", allow(unused_variables))]
266 #[must_use]
267 pub fn texture(&self, slot: usize, device: &Device) -> Option<FrameTexture> {
268 #[allow(unreachable_patterns)]
269 match (&self.kind, device) {
270 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
271 (RingKind::Vulkan(images), Device::Vulkan(device)) => {
272 use mln_vulkan::ash::vk::Handle as _;
273 images.images.get(slot).map(|image| FrameTexture {
274 size: images.size,
275 kind: FrameTextureKind::Vulkan {
276 image: image.image.as_raw(),
277 view: image.view.as_raw(),
278 device: device.raw().device.as_raw(),
279 format: images.format.as_raw(),
280 layout: images.final_layout.as_raw(),
281 },
282 })
283 }
284 #[cfg(all(feature = "metal", target_vendor = "apple"))]
285 (RingKind::Metal(textures), Device::Metal(device)) => {
286 use mln_mtl::objc2::rc::Retained;
287 textures.textures.get(slot).map(|texture| FrameTexture {
288 size: textures.size,
289 kind: FrameTextureKind::Metal {
290 texture: Retained::as_ptr(texture) as usize as u64,
291 device: Retained::as_ptr(device.device()) as usize as u64,
292 pixel_format: textures.pixel_format().0 as u64,
293 },
294 })
295 }
296 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
297 (RingKind::Gl(textures), Device::Gl(_)) => textures.textures.get(slot).map(|&texture| FrameTexture {
298 size: textures.size,
299 kind: FrameTextureKind::OpenGl { texture, target: 0x0DE1 },
301 }),
302 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
303 (RingKind::WebGpu(textures), Device::WebGpu(_)) => textures
304 .textures
305 .get(slot)
306 .map(|_| FrameTexture { size: textures.size, kind: FrameTextureKind::WebGpu }),
307 _ => None,
308 }
309 }
310}
311
312pub struct Surface {
314 pub(crate) kind: SurfaceKind,
315}
316
317pub(crate) enum SurfaceKind {
318 Window(Arc<dyn Window>),
319 #[cfg(all(feature = "metal", target_vendor = "apple"))]
320 MetalLayer(mln_mtl::Layer),
321 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
322 Wgpu(wgpu::Surface<'static>),
323 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
325 Vulkan(mln_vulkan::HostSurface),
326 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
328 Gl(mln_gl::HostSurface),
329}
330
331impl Surface {
332 pub fn window(window: impl Window + 'static) -> Surface {
338 Surface { kind: SurfaceKind::Window(Arc::new(window)) }
339 }
340
341 #[cfg(all(feature = "metal", target_vendor = "apple"))]
344 #[must_use]
345 pub fn metal_layer(layer: mln_mtl::Layer) -> Surface {
346 Surface { kind: SurfaceKind::MetalLayer(layer) }
347 }
348
349 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
352 pub fn wgpu(surface: wgpu::Surface<'static>) -> Surface {
353 Surface { kind: SurfaceKind::Wgpu(surface) }
354 }
355
356 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
359 #[must_use]
360 pub fn vulkan(surface: mln_vulkan::HostSurface) -> Surface {
361 Surface { kind: SurfaceKind::Vulkan(surface) }
362 }
363
364 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
367 #[must_use]
368 pub fn gl(surface: mln_gl::HostSurface) -> Surface {
369 Surface { kind: SurfaceKind::Gl(surface) }
370 }
371
372 #[must_use]
375 pub fn host_backend(&self) -> Option<BackendType> {
376 match self.kind {
377 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
378 SurfaceKind::Vulkan(_) => Some(BackendType::Vulkan),
379 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
380 SurfaceKind::Gl(_) => Some(BackendType::OpenGL),
381 #[allow(unreachable_patterns)]
382 _ => None,
383 }
384 }
385
386 pub(crate) fn clone_window(&self) -> Option<Surface> {
389 match &self.kind {
390 SurfaceKind::Window(window) => Some(Surface { kind: SurfaceKind::Window(window.clone()) }),
391 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
392 SurfaceKind::Gl(surface) => Some(Surface { kind: SurfaceKind::Gl(*surface) }),
393 #[allow(unreachable_patterns)]
394 _ => None,
395 }
396 }
397
398 pub(crate) fn check_area(&self) -> Result<(), Error> {
402 if let SurfaceKind::Window(window) = &self.kind
403 && !mln_gfx::window::has_area(&**window)
404 {
405 return Err(Error::invalid_state("the window is minimized or has no client area"));
406 }
407 Ok(())
408 }
409
410 pub(crate) fn host_window(&self) -> Option<Arc<dyn Window>> {
412 match &self.kind {
413 SurfaceKind::Window(window) => Some(window.clone()),
414 #[allow(unreachable_patterns)]
415 _ => None,
416 }
417 }
418
419 pub(crate) fn needs_no_device(&self) -> bool {
421 match self.kind {
422 SurfaceKind::Window(_) => true,
423 #[cfg(all(feature = "metal", target_vendor = "apple"))]
424 SurfaceKind::MetalLayer(_) => true,
425 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
426 SurfaceKind::Wgpu(_) => false,
427 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
428 SurfaceKind::Vulkan(_) => false,
429 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
430 SurfaceKind::Gl(_) => false,
431 }
432 }
433}
434
435impl std::fmt::Debug for Surface {
436 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
437 f.debug_struct("Surface").finish_non_exhaustive()
438 }
439}
440
441#[derive(Clone, Debug)]
445#[non_exhaustive]
446pub enum Device {
447 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
450 WebGpu(mln_webgpu::Device),
451 #[cfg(all(feature = "metal", target_vendor = "apple"))]
453 Metal(mln_mtl::Device),
454 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
456 Vulkan(mln_vulkan::Device),
457 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
460 Gl(mln_gl::Device),
461}
462
463#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
465pub struct DeviceOptions {
466 pub adapter: Option<Adapter>,
468 pub gl_api: GlApi,
471 pub gl_single_context: bool,
474}
475
476#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
478pub enum GlApi {
479 #[default]
481 Gles,
482 Gl,
484}
485
486#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
488pub enum Adapter {
489 Index(usize),
491 Uuid([u8; 16]),
494 Luid([u8; 8]),
498}
499
500#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
502#[non_exhaustive]
503pub enum SurfaceType {
504 MetalLayer,
506 Win32,
508 Xlib,
510 Xcb,
512 Wayland,
514 AndroidNdk,
516 OhosNdk,
518}
519
520impl SurfaceType {
521 #[must_use]
524 pub fn presents_with(self, backend: BackendType) -> bool {
525 let windowing = match self {
526 SurfaceType::MetalLayer => false,
527 SurfaceType::Win32 => cfg!(windows),
528 SurfaceType::Xlib | SurfaceType::Xcb | SurfaceType::Wayland => {
529 cfg!(all(unix, not(target_vendor = "apple"), not(target_os = "android"), not(target_env = "ohos")))
530 }
531 SurfaceType::AndroidNdk => cfg!(target_os = "android"),
532 SurfaceType::OhosNdk => cfg!(target_env = "ohos"),
533 };
534 match backend {
535 BackendType::Metal => {
536 cfg!(all(feature = "metal", target_vendor = "apple")) && self == SurfaceType::MetalLayer
537 }
538 BackendType::Vulkan => {
539 cfg!(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm")))) && windowing
540 }
541 BackendType::OpenGL => {
542 cfg!(all(feature = "gl", unix, not(target_vendor = "apple")))
543 && windowing
544 && matches!(
545 self,
546 SurfaceType::Xlib | SurfaceType::Wayland | SurfaceType::AndroidNdk | SurfaceType::OhosNdk
547 )
548 }
549 BackendType::WebGPU => {
552 cfg!(all(feature = "webgpu", not(target_family = "wasm"))) && windowing && self != SurfaceType::OhosNdk
553 }
554 #[allow(unreachable_patterns)]
555 _ => false,
556 }
557 }
558
559 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
561 fn vulkan_extension(self) -> Option<&'static std::ffi::CStr> {
562 use mln_vulkan::ash::khr;
563 match self {
564 SurfaceType::MetalLayer => None,
565 SurfaceType::Win32 => Some(khr::win32_surface::NAME),
566 SurfaceType::Xlib => Some(khr::xlib_surface::NAME),
567 SurfaceType::Xcb => Some(khr::xcb_surface::NAME),
568 SurfaceType::Wayland => Some(khr::wayland_surface::NAME),
569 SurfaceType::AndroidNdk => Some(khr::android_surface::NAME),
570 SurfaceType::OhosNdk => Some(mln_vulkan::device::OHOS_SURFACE),
571 }
572 }
573
574 #[must_use]
577 pub fn presents_with_any(self) -> bool {
578 [BackendType::Metal, BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
579 .into_iter()
580 .any(|backend| self.presents_with(backend))
581 }
582
583 #[must_use]
587 pub fn presents_without_device(self) -> bool {
588 match self {
589 SurfaceType::MetalLayer => self.presents_with(BackendType::Metal),
590 _ if cfg!(target_vendor = "apple") => self.presents_with(BackendType::WebGPU),
591 _ => [BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
592 .into_iter()
593 .any(|backend| self.presents_with(backend)),
594 }
595 }
596}
597
598#[cfg(not(target_family = "wasm"))]
601fn missing(error: &Error) -> bool {
602 error.kind == mln_util::ErrorKind::Unsupported
603}
604
605impl Device {
606 pub fn new(options: DeviceOptions) -> Result<Device, Error> {
620 #[cfg(target_family = "wasm")]
621 {
622 let _ = options;
623 Err(Error::unsupported("the browser has no library devices"))
624 }
625 #[cfg(not(target_family = "wasm"))]
626 {
627 let mut error = Error::unsupported("this build has no backend");
628 for backend in Self::native_order() {
629 match Self::with_backend(*backend, options) {
630 Ok(device) => return Ok(device),
631 Err(e) if missing(&e) => error = e,
632 Err(e) => return Err(e),
633 }
634 }
635 Err(error)
636 }
637 }
638
639 pub fn with_backend(backend: BackendType, options: DeviceOptions) -> Result<Device, Error> {
645 let DeviceOptions { adapter, gl_api, gl_single_context } = options;
646 let index = |adapter: Option<Adapter>| match adapter {
648 None => Ok(None),
649 Some(Adapter::Index(index)) => Ok(Some(index)),
650 Some(_) => Err(Error::unsupported(format!("{backend:?} names no GPU by UUID or LUID"))),
651 };
652 match backend {
653 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
654 BackendType::WebGPU => mln_webgpu::Device::new(index(adapter)?).map(Device::WebGpu),
655 #[cfg(all(feature = "metal", target_vendor = "apple"))]
656 BackendType::Metal => mln_mtl::Device::new(index(adapter)?).map(Device::Metal),
657 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
658 BackendType::Vulkan => match adapter {
659 Some(Adapter::Uuid(uuid)) => mln_vulkan::Device::with_adapter_id(mln_vulkan::AdapterId::Uuid(uuid)),
660 Some(Adapter::Luid(luid)) => mln_vulkan::Device::with_adapter_id(mln_vulkan::AdapterId::Luid(luid)),
661 _ => mln_vulkan::Device::new(index(adapter)?),
662 }
663 .map(Device::Vulkan),
664 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
665 BackendType::OpenGL => {
666 if adapter.is_some() {
667 return Err(Error::unsupported("OpenGL picks no GPU"));
668 }
669 let api = match gl_api {
670 GlApi::Gles => mln_gl::Api::Gles,
671 GlApi::Gl => mln_gl::Api::Gl,
672 };
673 let options = mln_gl::DeviceOptions { api, single_context: gl_single_context };
674 mln_gl::Device::with_options(options).map(Device::Gl)
675 }
676 #[allow(unreachable_patterns)]
677 _ => {
678 let _ = (index, adapter, gl_api, gl_single_context);
679 Err(Error::unsupported(format!("this build has no {backend:?} backend")))
680 }
681 }
682 }
683
684 pub fn borrowed_sync_kinds(&self, ring: &BorrowedRing) -> Result<(SyncKinds, SyncKinds), Error> {
692 #[allow(unreachable_patterns)]
693 match (self, &ring.kind) {
694 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
695 (Device::Vulkan(device), RingKind::Vulkan(images)) => {
696 images.check(device)?;
697 Ok(device.sync_kinds())
698 }
699 #[cfg(all(feature = "metal", target_vendor = "apple"))]
700 (Device::Metal(device), RingKind::Metal(textures)) => {
701 textures.check(device)?;
702 Ok(mln_mtl::BorrowedTextures::sync_kinds(device))
703 }
704 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
705 (Device::Gl(device), RingKind::Gl(_)) => {
706 if !device.is_host() {
707 return Err(Error::unsupported(
708 "an OpenGL ring renders on a device in the share group of the host's context",
709 ));
710 }
711 Ok(mln_gl::BorrowedTextures::sync_kinds())
712 }
713 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
714 (Device::WebGpu(_), RingKind::WebGpu(textures)) => {
715 textures.check()?;
716 Ok(mln_webgpu::BorrowedTextures::sync_kinds())
717 }
718 _ => Err(Error::unsupported(format!(
719 "a {:?} device renders into no ring of {:?} textures",
720 self.backend(),
721 ring.backend()
722 ))),
723 }
724 }
725
726 #[must_use]
729 pub fn export_kinds(&self) -> ExportKinds {
730 match self {
731 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
732 Device::Vulkan(device) => device.export_kinds(),
733 #[cfg(all(feature = "metal", target_vendor = "apple"))]
734 Device::Metal(device) => mln_mtl::exported_target::export_kinds(device),
735 #[cfg(all(feature = "gl", unix, not(target_vendor = "apple"), not(target_os = "android")))]
736 Device::Gl(device) => mln_gl::exported_target::export_kinds(device),
737 #[allow(unreachable_patterns)]
738 _ => ExportKinds::NONE,
739 }
740 }
741
742 pub fn export_plan(&self, ring: &ExportedRing) -> Result<(ExportKind, SyncKinds, SyncKinds), Error> {
749 #[allow(unreachable_patterns)]
750 match self {
751 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
752 Device::Vulkan(device) => {
753 let kind = mln_vulkan::exported_target::plan(device, ring)?;
754 let (producer, consumer) = mln_vulkan::exported_target::sync_kinds(device);
755 Ok((kind, producer, consumer))
756 }
757 #[cfg(all(feature = "metal", target_vendor = "apple"))]
758 Device::Metal(device) => {
759 let kind = mln_mtl::exported_target::plan(device, ring)?;
760 let (producer, consumer) = mln_mtl::exported_target::sync_kinds(device);
761 Ok((kind, producer, consumer))
762 }
763 #[cfg(all(feature = "gl", unix, not(target_vendor = "apple"), not(target_os = "android")))]
764 Device::Gl(device) => {
765 let kind = mln_gl::exported_target::plan(device, ring)?;
766 let (producer, consumer) = mln_gl::exported_target::sync_kinds();
767 Ok((kind, producer, consumer))
768 }
769 _ => {
770 let _ = ring;
771 Err(Error::unsupported(format!("a {:?} device exports no ring", self.backend())))
772 }
773 }
774 }
775
776 #[must_use]
780 pub fn export_sync_kinds(&self) -> (SyncKinds, SyncKinds) {
781 if self.export_kinds().is_empty() {
782 return (SyncKinds::NONE, SyncKinds::NONE);
783 }
784 #[allow(unreachable_patterns)]
785 match self {
786 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
787 Device::Vulkan(device) => mln_vulkan::exported_target::sync_kinds(device),
788 #[cfg(all(feature = "metal", target_vendor = "apple"))]
789 Device::Metal(device) => mln_mtl::exported_target::sync_kinds(device),
790 #[cfg(all(feature = "gl", unix, not(target_vendor = "apple"), not(target_os = "android")))]
791 Device::Gl(_) => mln_gl::exported_target::sync_kinds(),
792 _ => (SyncKinds::NONE, SyncKinds::NONE),
793 }
794 }
795
796 pub fn for_exported_ring(ring: &ExportedRing) -> Result<Device, Error> {
802 #[cfg(target_family = "wasm")]
803 {
804 let _ = ring;
805 Err(Error::unsupported("the browser has no library devices"))
806 }
807 #[cfg(not(target_family = "wasm"))]
808 {
809 let mut error = Error::unsupported("no backend of this build exports the ring's kinds of image");
810 for backend in Self::native_order() {
811 match Self::with_backend(*backend, DeviceOptions::default()) {
812 Ok(device) => match device.export_plan(ring) {
813 Ok(_) => return Ok(device),
814 Err(e) => error = e,
815 },
816 Err(e) if missing(&e) => {}
817 Err(e) => return Err(e),
818 }
819 }
820 Err(error)
821 }
822 }
823
824 #[must_use]
828 pub fn presents_to_host_surfaces(&self) -> bool {
829 match self {
830 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
831 Device::Vulkan(device) => device.presents(),
832 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
833 Device::Gl(device) => device.is_host(),
834 #[allow(unreachable_patterns)]
835 _ => false,
836 }
837 }
838
839 #[must_use]
843 pub fn presents_to(&self, surface: SurfaceType) -> bool {
844 if !surface.presents_with(self.backend()) {
845 return false;
846 }
847 match self {
848 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
849 Device::Vulkan(device) => surface.vulkan_extension().is_some_and(|name| device.presents_through(name)),
850 #[allow(unreachable_patterns)]
851 _ => true,
852 }
853 }
854
855 #[cfg(not(target_family = "wasm"))]
857 fn native_order() -> &'static [BackendType] {
858 if cfg!(target_vendor = "apple") {
859 &[BackendType::Metal, BackendType::WebGPU]
860 } else {
861 &[BackendType::Vulkan, BackendType::OpenGL, BackendType::WebGPU]
862 }
863 }
864
865 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
869 #[must_use]
870 pub fn webgpu(
871 instance: wgpu::Instance,
872 device: wgpu::Device,
873 queue: wgpu::Queue,
874 format: wgpu::TextureFormat,
875 ) -> Device {
876 Device::WebGpu(mln_webgpu::Device::from_wgpu(Some(instance), device, queue, Some(format)))
877 }
878
879 #[must_use]
881 pub fn backend(&self) -> BackendType {
882 match *self {
883 #[cfg(all(feature = "webgpu", not(target_family = "wasm")))]
884 Device::WebGpu(_) => BackendType::WebGPU,
885 #[cfg(all(feature = "metal", target_vendor = "apple"))]
886 Device::Metal(_) => BackendType::Metal,
887 #[cfg(all(feature = "vulkan", not(any(target_vendor = "apple", target_family = "wasm"))))]
888 Device::Vulkan(_) => BackendType::Vulkan,
889 #[cfg(all(feature = "gl", not(target_family = "wasm")))]
890 Device::Gl(_) => BackendType::OpenGL,
891 }
892 }
893}