wgpu_core/
hal_api.rs

1use wgt::Backend;
2
3use crate::{
4    global::Global,
5    hub::Hub,
6    identity::GlobalIdentityHandlerFactory,
7    instance::{HalSurface, Instance, Surface},
8};
9
10pub trait HalApi: hal::Api {
11    const VARIANT: Backend;
12    fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance;
13    fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance>;
14    fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G>;
15    fn get_surface(surface: &Surface) -> Option<&HalSurface<Self>>;
16    fn get_surface_mut(surface: &mut Surface) -> Option<&mut HalSurface<Self>>;
17}
18
19impl HalApi for hal::api::Empty {
20    const VARIANT: Backend = Backend::Empty;
21    fn create_instance_from_hal(_: &str, _: Self::Instance) -> Instance {
22        unimplemented!("called empty api")
23    }
24    fn instance_as_hal(_: &Instance) -> Option<&Self::Instance> {
25        unimplemented!("called empty api")
26    }
27    fn hub<G: GlobalIdentityHandlerFactory>(_: &Global<G>) -> &Hub<Self, G> {
28        unimplemented!("called empty api")
29    }
30    fn get_surface(_: &Surface) -> Option<&HalSurface<Self>> {
31        unimplemented!("called empty api")
32    }
33    fn get_surface_mut(_: &mut Surface) -> Option<&mut HalSurface<Self>> {
34        unimplemented!("called empty api")
35    }
36}
37
38#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
39impl HalApi for hal::api::Vulkan {
40    const VARIANT: Backend = Backend::Vulkan;
41    fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
42        Instance {
43            name: name.to_owned(),
44            vulkan: Some(hal_instance),
45            ..Default::default()
46        }
47    }
48    fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
49        instance.vulkan.as_ref()
50    }
51    fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
52        &global.hubs.vulkan
53    }
54    fn get_surface(surface: &Surface) -> Option<&HalSurface<Self>> {
55        surface.vulkan.as_ref()
56    }
57    fn get_surface_mut(surface: &mut Surface) -> Option<&mut HalSurface<Self>> {
58        surface.vulkan.as_mut()
59    }
60}
61
62#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
63impl HalApi for hal::api::Metal {
64    const VARIANT: Backend = Backend::Metal;
65    fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
66        Instance {
67            name: name.to_owned(),
68            metal: Some(hal_instance),
69            ..Default::default()
70        }
71    }
72    fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
73        instance.metal.as_ref()
74    }
75    fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
76        &global.hubs.metal
77    }
78    fn get_surface(surface: &Surface) -> Option<&HalSurface<Self>> {
79        surface.metal.as_ref()
80    }
81    fn get_surface_mut(surface: &mut Surface) -> Option<&mut HalSurface<Self>> {
82        surface.metal.as_mut()
83    }
84}
85
86#[cfg(all(feature = "dx12", windows))]
87impl HalApi for hal::api::Dx12 {
88    const VARIANT: Backend = Backend::Dx12;
89    fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
90        Instance {
91            name: name.to_owned(),
92            dx12: Some(hal_instance),
93            ..Default::default()
94        }
95    }
96    fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
97        instance.dx12.as_ref()
98    }
99    fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
100        &global.hubs.dx12
101    }
102    fn get_surface(surface: &Surface) -> Option<&HalSurface<Self>> {
103        surface.dx12.as_ref()
104    }
105    fn get_surface_mut(surface: &mut Surface) -> Option<&mut HalSurface<Self>> {
106        surface.dx12.as_mut()
107    }
108}
109
110#[cfg(all(feature = "dx11", windows))]
111impl HalApi for hal::api::Dx11 {
112    const VARIANT: Backend = Backend::Dx11;
113    fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
114        Instance {
115            name: name.to_owned(),
116            dx11: Some(hal_instance),
117            ..Default::default()
118        }
119    }
120    fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
121        instance.dx11.as_ref()
122    }
123    fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
124        &global.hubs.dx11
125    }
126    fn get_surface(surface: &Surface) -> Option<&HalSurface<Self>> {
127        surface.dx11.as_ref()
128    }
129    fn get_surface_mut(surface: &mut Surface) -> Option<&mut HalSurface<Self>> {
130        surface.dx11.as_mut()
131    }
132}
133
134#[cfg(feature = "gles")]
135impl HalApi for hal::api::Gles {
136    const VARIANT: Backend = Backend::Gl;
137    fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
138        #[allow(clippy::needless_update)]
139        Instance {
140            name: name.to_owned(),
141            gl: Some(hal_instance),
142            ..Default::default()
143        }
144    }
145    fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
146        instance.gl.as_ref()
147    }
148    fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
149        &global.hubs.gl
150    }
151    fn get_surface(surface: &Surface) -> Option<&HalSurface<Self>> {
152        surface.gl.as_ref()
153    }
154    fn get_surface_mut(surface: &mut Surface) -> Option<&mut HalSurface<Self>> {
155        surface.gl.as_mut()
156    }
157}