wgpu_hal/dynamic/
adapter.rs

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