1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#[cfg(feature = "rafx-metal")]
use crate::metal::RafxDeviceContextMetal;
#[cfg(feature = "rafx-vulkan")]
use crate::vulkan::RafxDeviceContextVulkan;
use crate::*;
use raw_window_handle::HasRawWindowHandle;
#[derive(Clone)]
pub enum RafxDeviceContext {
#[cfg(feature = "rafx-vulkan")]
Vk(RafxDeviceContextVulkan),
#[cfg(feature = "rafx-metal")]
Metal(RafxDeviceContextMetal),
}
impl RafxDeviceContext {
pub fn device_info(&self) -> &RafxDeviceInfo {
match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => inner.device_info(),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
}
}
pub fn find_supported_format(
&self,
candidates: &[RafxFormat],
resource_type: RafxResourceType,
) -> Option<RafxFormat> {
match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => inner.find_supported_format(candidates, resource_type),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
}
}
pub fn create_queue(
&self,
queue_type: RafxQueueType,
) -> RafxResult<RafxQueue> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => RafxQueue::Vk(inner.create_queue(queue_type)?),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_fence(&self) -> RafxResult<RafxFence> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => RafxFence::Vk(inner.create_fence()?),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_semaphore(&self) -> RafxResult<RafxSemaphore> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => RafxSemaphore::Vk(inner.create_semaphore()?),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_swapchain(
&self,
raw_window_handle: &dyn HasRawWindowHandle,
swapchain_def: &RafxSwapchainDef,
) -> RafxResult<RafxSwapchain> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => {
RafxSwapchain::Vk(inner.create_swapchain(raw_window_handle, swapchain_def)?)
}
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn wait_for_fences(
&self,
fences: &[&RafxFence],
) -> RafxResult<()> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => {
let fences: Vec<_> = fences.iter().map(|x| x.vk_fence().unwrap()).collect();
inner.wait_for_fences(&fences)?
}
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn wait_for_device_idle(&self) -> RafxResult<()> {
match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => inner.wait_for_device_idle(),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
}
}
pub fn create_sampler(
&self,
sampler_def: &RafxSamplerDef,
) -> RafxResult<RafxSampler> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => RafxSampler::Vk(inner.create_sampler(sampler_def)?),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_texture(
&self,
texture_def: &RafxTextureDef,
) -> RafxResult<RafxTexture> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => RafxTexture::Vk(inner.create_texture(texture_def)?),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_render_target(
&self,
render_target_def: &RafxRenderTargetDef,
) -> RafxResult<RafxRenderTarget> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => {
RafxRenderTarget::Vk(inner.create_render_target(render_target_def)?)
}
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_buffer(
&self,
buffer_def: &RafxBufferDef,
) -> RafxResult<RafxBuffer> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => RafxBuffer::Vk(inner.create_buffer(buffer_def)?),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_shader(
&self,
stages: Vec<RafxShaderStageDef>,
) -> RafxResult<RafxShader> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => RafxShader::Vk(inner.create_shader(stages)?),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_root_signature(
&self,
root_signature_def: &RafxRootSignatureDef,
) -> RafxResult<RafxRootSignature> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => {
RafxRootSignature::Vk(inner.create_root_signature(root_signature_def)?)
}
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_graphics_pipeline(
&self,
pipeline_def: &RafxGraphicsPipelineDef,
) -> RafxResult<RafxPipeline> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => {
RafxPipeline::Vk(inner.create_graphics_pipeline(pipeline_def)?)
}
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_compute_pipeline(
&self,
pipeline_def: &RafxComputePipelineDef,
) -> RafxResult<RafxPipeline> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => {
RafxPipeline::Vk(inner.create_compute_pipeline(pipeline_def)?)
}
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
pub fn create_descriptor_set_array(
&self,
descriptor_set_array_def: &RafxDescriptorSetArrayDef,
) -> RafxResult<RafxDescriptorSetArray> {
Ok(match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => RafxDescriptorSetArray::Vk(
inner.create_descriptor_set_array(descriptor_set_array_def)?,
),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_inner) => unimplemented!(),
})
}
#[cfg(feature = "rafx-vulkan")]
pub fn vk_device_context(&self) -> Option<&RafxDeviceContextVulkan> {
match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(inner) => Some(inner),
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(_) => None,
}
}
#[cfg(feature = "rafx-metal")]
pub fn metal_device_context(&self) -> Option<&RafxDeviceContextMetal> {
match self {
#[cfg(feature = "rafx-vulkan")]
RafxDeviceContext::Vk(_) => None,
#[cfg(feature = "rafx-metal")]
RafxDeviceContext::Metal(inner) => Some(inner),
}
}
}