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    /// Dynamic version of [Adapter::open]
26    unsafe fn open(
27        &self,
28        features: wgt::Features,
29        limits: &wgt::Limits,
30        memory_hints: &wgt::MemoryHints,
31    ) -> Result<DynOpenDevice, DeviceError>;
32
33    /// Dynamic version of [Adapter::texture_format_capabilities]
34    unsafe fn texture_format_capabilities(
35        &self,
36        format: wgt::TextureFormat,
37    ) -> TextureFormatCapabilities;
38
39    /// Dynamic version of [Adapter::surface_capabilities]
40    unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities>;
41
42    /// Dynamic version of [Adapter::surface_display_hdr_info]
43    unsafe fn surface_display_hdr_info(
44        &self,
45        surface: &dyn DynSurface,
46    ) -> Option<wgt::DisplayHdrInfo>;
47
48    /// Dynamic version of [Adapter::get_presentation_timestamp]
49    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;
50
51    /// Dynamic version of [Adapter::get_ordered_buffer_usages]
52    fn get_ordered_buffer_usages(&self) -> wgt::BufferUses;
53
54    /// Dynamic version of [Adapter::get_ordered_texture_usages]
55    fn get_ordered_texture_usages(&self) -> wgt::TextureUses;
56}
57
58impl<A: Adapter + DynResource> DynAdapter for A {
59    unsafe fn open(
60        &self,
61        features: wgt::Features,
62        limits: &wgt::Limits,
63        memory_hints: &wgt::MemoryHints,
64    ) -> Result<DynOpenDevice, DeviceError> {
65        unsafe { A::open(self, features, limits, memory_hints) }.map(|open_device| DynOpenDevice {
66            device: Box::new(open_device.device),
67            queue: Box::new(open_device.queue),
68        })
69    }
70
71    unsafe fn texture_format_capabilities(
72        &self,
73        format: wgt::TextureFormat,
74    ) -> TextureFormatCapabilities {
75        unsafe { A::texture_format_capabilities(self, format) }
76    }
77
78    unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities> {
79        let surface = surface.expect_downcast_ref();
80        unsafe { A::surface_capabilities(self, surface) }
81    }
82
83    unsafe fn surface_display_hdr_info(
84        &self,
85        surface: &dyn DynSurface,
86    ) -> Option<wgt::DisplayHdrInfo> {
87        let surface = surface.expect_downcast_ref();
88        unsafe { A::surface_display_hdr_info(self, surface) }
89    }
90
91    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
92        unsafe { A::get_presentation_timestamp(self) }
93    }
94
95    fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {
96        A::get_ordered_buffer_usages(self)
97    }
98
99    fn get_ordered_texture_usages(&self) -> wgt::TextureUses {
100        A::get_ordered_texture_usages(self)
101    }
102}