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
9pub struct DynOpenDevice {
10    pub device: Box<dyn DynDevice>,
11    pub queue: Box<dyn DynQueue>,
12}
13
14impl<A: Api> From<OpenDevice<A>> for DynOpenDevice {
15    fn from(open_device: OpenDevice<A>) -> Self {
16        Self {
17            device: Box::new(open_device.device),
18            queue: Box::new(open_device.queue),
19        }
20    }
21}
22
23pub trait DynAdapter: DynResource {
24    unsafe fn open(
25        &self,
26        features: wgt::Features,
27        limits: &wgt::Limits,
28        memory_hints: &wgt::MemoryHints,
29    ) -> Result<DynOpenDevice, DeviceError>;
30
31    unsafe fn texture_format_capabilities(
32        &self,
33        format: wgt::TextureFormat,
34    ) -> TextureFormatCapabilities;
35
36    unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities>;
37
38    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp;
39}
40
41impl<A: Adapter + DynResource> DynAdapter for A {
42    unsafe fn open(
43        &self,
44        features: wgt::Features,
45        limits: &wgt::Limits,
46        memory_hints: &wgt::MemoryHints,
47    ) -> Result<DynOpenDevice, DeviceError> {
48        unsafe { A::open(self, features, limits, memory_hints) }.map(|open_device| DynOpenDevice {
49            device: Box::new(open_device.device),
50            queue: Box::new(open_device.queue),
51        })
52    }
53
54    unsafe fn texture_format_capabilities(
55        &self,
56        format: wgt::TextureFormat,
57    ) -> TextureFormatCapabilities {
58        unsafe { A::texture_format_capabilities(self, format) }
59    }
60
61    unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities> {
62        let surface = surface.expect_downcast_ref();
63        unsafe { A::surface_capabilities(self, surface) }
64    }
65
66    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
67        unsafe { A::get_presentation_timestamp(self) }
68    }
69}