Skip to main content

wgpu_hal/dynamic/
adapter.rs

1use alloc::boxed::Box;
2
3use crate::{
4    Adapter, Api, DeviceError, OpenDevice, SurfaceCapabilities, TextureFormatCapabilities,
5};
6
7use super::{DynDevice, DynQueue, DynResource, DynResourceExt, DynSurface};
8
9#[expect(missing_debug_implementations, reason = "dyn")]
10pub struct DynOpenDevice {
11    pub device: Box<dyn DynDevice>,
12    pub queue: Box<dyn DynQueue>,
13}
14
15impl<A: Api> From<OpenDevice<A>> for DynOpenDevice {
16    fn from(open_device: OpenDevice<A>) -> Self {
17        Self {
18            device: Box::new(open_device.device),
19            queue: Box::new(open_device.queue),
20        }
21    }
22}
23
24pub trait DynAdapter: DynResource {
25    unsafe fn open(
26        &self,
27        features: wgt::Features,
28        limits: &wgt::Limits,
29        memory_hints: &wgt::MemoryHints,
30    ) -> Result<DynOpenDevice, DeviceError>;
31
32    unsafe fn texture_format_capabilities(
33        &self,
34        format: wgt::TextureFormat,
35    ) -> TextureFormatCapabilities;
36
37    unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities>;
38
39    unsafe fn surface_display_hdr_info(
40        &self,
41        surface: &dyn DynSurface,
42    ) -> Option<wgt::DisplayHdrInfo>;
43
44    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;
45
46    fn get_ordered_buffer_usages(&self) -> wgt::BufferUses;
47
48    fn get_ordered_texture_usages(&self) -> wgt::TextureUses;
49}
50
51impl<A: Adapter + DynResource> DynAdapter for A {
52    unsafe fn open(
53        &self,
54        features: wgt::Features,
55        limits: &wgt::Limits,
56        memory_hints: &wgt::MemoryHints,
57    ) -> Result<DynOpenDevice, DeviceError> {
58        unsafe { A::open(self, features, limits, memory_hints) }.map(|open_device| DynOpenDevice {
59            device: Box::new(open_device.device),
60            queue: Box::new(open_device.queue),
61        })
62    }
63
64    unsafe fn texture_format_capabilities(
65        &self,
66        format: wgt::TextureFormat,
67    ) -> TextureFormatCapabilities {
68        unsafe { A::texture_format_capabilities(self, format) }
69    }
70
71    unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities> {
72        let surface = surface.expect_downcast_ref();
73        unsafe { A::surface_capabilities(self, surface) }
74    }
75
76    unsafe fn surface_display_hdr_info(
77        &self,
78        surface: &dyn DynSurface,
79    ) -> Option<wgt::DisplayHdrInfo> {
80        let surface = surface.expect_downcast_ref();
81        unsafe { A::surface_display_hdr_info(self, surface) }
82    }
83
84    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
85        unsafe { A::get_presentation_timestamp(self) }
86    }
87
88    fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {
89        A::get_ordered_buffer_usages(self)
90    }
91
92    fn get_ordered_texture_usages(&self) -> wgt::TextureUses {
93        A::get_ordered_texture_usages(self)
94    }
95}