1use wgt::{Backend, WasmNotSendSync};
2
3use crate::{
4 global::Global,
5 hub::Hub,
6 instance::{Instance, Surface},
7};
8
9pub trait HalApi: hal::Api + 'static + WasmNotSendSync {
10 const VARIANT: Backend;
11 fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance;
12 fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance>;
13 fn hub(global: &Global) -> &Hub<Self>;
14 fn surface_as_hal(surface: &Surface) -> Option<&Self::Surface>;
15}
16
17impl HalApi for hal::api::Empty {
18 const VARIANT: Backend = Backend::Empty;
19 fn create_instance_from_hal(_: &str, _: Self::Instance) -> Instance {
20 unimplemented!("called empty api")
21 }
22 fn instance_as_hal(_: &Instance) -> Option<&Self::Instance> {
23 unimplemented!("called empty api")
24 }
25 fn hub(_: &Global) -> &Hub<Self> {
26 unimplemented!("called empty api")
27 }
28 fn surface_as_hal(_: &Surface) -> Option<&Self::Surface> {
29 unimplemented!("called empty api")
30 }
31}
32
33#[cfg(vulkan)]
34impl HalApi for hal::api::Vulkan {
35 const VARIANT: Backend = Backend::Vulkan;
36 fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
37 Instance {
38 name: name.to_owned(),
39 vulkan: Some(hal_instance),
40 ..Default::default()
41 }
42 }
43 fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
44 instance.vulkan.as_ref()
45 }
46 fn hub(global: &Global) -> &Hub<Self> {
47 &global.hubs.vulkan
48 }
49 fn surface_as_hal(surface: &Surface) -> Option<&Self::Surface> {
50 surface.vulkan.as_ref()
51 }
52}
53
54#[cfg(metal)]
55impl HalApi for hal::api::Metal {
56 const VARIANT: Backend = Backend::Metal;
57 fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
58 Instance {
59 name: name.to_owned(),
60 metal: Some(hal_instance),
61 ..Default::default()
62 }
63 }
64 fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
65 instance.metal.as_ref()
66 }
67 fn hub(global: &Global) -> &Hub<Self> {
68 &global.hubs.metal
69 }
70 fn surface_as_hal(surface: &Surface) -> Option<&Self::Surface> {
71 surface.metal.as_ref()
72 }
73}
74
75#[cfg(dx12)]
76impl HalApi for hal::api::Dx12 {
77 const VARIANT: Backend = Backend::Dx12;
78 fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
79 Instance {
80 name: name.to_owned(),
81 dx12: Some(hal_instance),
82 ..Default::default()
83 }
84 }
85 fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
86 instance.dx12.as_ref()
87 }
88 fn hub(global: &Global) -> &Hub<Self> {
89 &global.hubs.dx12
90 }
91 fn surface_as_hal(surface: &Surface) -> Option<&Self::Surface> {
92 surface.dx12.as_ref()
93 }
94}
95
96#[cfg(gles)]
97impl HalApi for hal::api::Gles {
98 const VARIANT: Backend = Backend::Gl;
99 fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
100 #[allow(clippy::needless_update)]
101 Instance {
102 name: name.to_owned(),
103 gl: Some(hal_instance),
104 ..Default::default()
105 }
106 }
107 fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
108 instance.gl.as_ref()
109 }
110 fn hub(global: &Global) -> &Hub<Self> {
111 &global.hubs.gl
112 }
113 fn surface_as_hal(surface: &Surface) -> Option<&Self::Surface> {
114 surface.gl.as_ref()
115 }
116}