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
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    fn get_ordered_buffer_usages(&self) -> wgt::BufferUses;
41
42    fn get_ordered_texture_usages(&self) -> wgt::TextureUses;
43}
44
45impl<A: Adapter + DynResource> DynAdapter for A {
46    unsafe fn open(
47        &self,
48        features: wgt::Features,
49        limits: &wgt::Limits,
50        memory_hints: &wgt::MemoryHints,
51    ) -> Result<DynOpenDevice, DeviceError> {
52        unsafe { A::open(self, features, limits, memory_hints) }.map(|open_device| DynOpenDevice {
53            device: Box::new(open_device.device),
54            queue: Box::new(open_device.queue),
55        })
56    }
57
58    unsafe fn texture_format_capabilities(
59        &self,
60        format: wgt::TextureFormat,
61    ) -> TextureFormatCapabilities {
62        unsafe { A::texture_format_capabilities(self, format) }
63    }
64
65    unsafe fn surface_capabilities(&self, surface: &dyn DynSurface) -> Option<SurfaceCapabilities> {
66        let surface = surface.expect_downcast_ref();
67        unsafe { A::surface_capabilities(self, surface) }
68    }
69
70    unsafe fn get_presentation_timestamp(&self) -> wgt::PresentationTimestamp {
71        unsafe { A::get_presentation_timestamp(self) }
72    }
73
74    fn get_ordered_buffer_usages(&self) -> wgt::BufferUses {
75        A::get_ordered_buffer_usages(self)
76    }
77
78    fn get_ordered_texture_usages(&self) -> wgt::TextureUses {
79        A::get_ordered_texture_usages(self)
80    }
81}