b4a976e57e5d3407f42ba5e420700a580b198b7b
[kazan.git] / vulkan-driver / src / handle.rs
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // Copyright 2018 Jacob Lifshay
3 use api;
4 use api_impl::{Device, Instance, PhysicalDevice, Queue};
5 use buffer::Buffer;
6 use device_memory::DeviceMemory;
7 use sampler::Sampler;
8 use sampler::SamplerYcbcrConversion;
9 use shader_module::ShaderModule;
10 use std::fmt;
11 use std::marker::PhantomData;
12 use std::mem;
13 use std::ops::Deref;
14 use std::ops::DerefMut;
15 use std::ptr::null_mut;
16 use std::ptr::NonNull;
17 use swapchain::Swapchain;
18
19 #[repr(C)]
20 pub struct DispatchableType<T> {
21 loader_dispatch_ptr: usize,
22 value: T,
23 }
24
25 impl<T> From<T> for DispatchableType<T> {
26 fn from(v: T) -> Self {
27 Self {
28 loader_dispatch_ptr: api::ICD_LOADER_MAGIC as usize,
29 value: v,
30 }
31 }
32 }
33
34 impl<T> Deref for DispatchableType<T> {
35 type Target = T;
36 fn deref(&self) -> &T {
37 &self.value
38 }
39 }
40
41 impl<T> DerefMut for DispatchableType<T> {
42 fn deref_mut(&mut self) -> &mut T {
43 &mut self.value
44 }
45 }
46
47 pub trait HandleAllocFree: Handle {
48 unsafe fn allocate<T: Into<Self::Value>>(v: T) -> Self {
49 Self::new(Some(NonNull::new_unchecked(Box::into_raw(Box::new(
50 v.into(),
51 )))))
52 }
53 unsafe fn free(self) {
54 Box::from_raw(self.get().unwrap().as_ptr());
55 }
56 }
57
58 pub trait Handle: Copy + Eq + fmt::Debug {
59 type Value;
60 fn get(&self) -> Option<NonNull<Self::Value>>;
61 fn new(v: Option<NonNull<Self::Value>>) -> Self;
62 fn null() -> Self {
63 Self::new(None)
64 }
65 fn is_null(&self) -> bool {
66 self.get().is_none()
67 }
68 fn take(&mut self) -> Self {
69 let retval = self.clone();
70 *self = Self::null();
71 retval
72 }
73 }
74
75 #[repr(transparent)]
76 pub struct DispatchableHandle<T>(Option<NonNull<()>>, PhantomData<*mut DispatchableType<T>>);
77
78 impl<T> Clone for DispatchableHandle<T> {
79 fn clone(&self) -> Self {
80 DispatchableHandle(self.0, PhantomData)
81 }
82 }
83
84 impl<T> Copy for DispatchableHandle<T> {}
85
86 impl<T> Eq for DispatchableHandle<T> {}
87
88 impl<T> PartialEq for DispatchableHandle<T> {
89 fn eq(&self, rhs: &Self) -> bool {
90 self.0 == rhs.0
91 }
92 }
93
94 impl<T> fmt::Debug for DispatchableHandle<T> {
95 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96 f.debug_tuple("DispatchableHandle")
97 .field(
98 &self
99 .get()
100 .map(|v| v.as_ptr())
101 .unwrap_or(null_mut::<*mut ()>() as *mut _),
102 )
103 .finish()
104 }
105 }
106
107 impl<T> Handle for DispatchableHandle<T> {
108 type Value = DispatchableType<T>;
109 fn get(&self) -> Option<NonNull<DispatchableType<T>>> {
110 unsafe { mem::transmute(self.0) }
111 }
112 fn new(v: Option<NonNull<DispatchableType<T>>>) -> Self {
113 unsafe { DispatchableHandle(mem::transmute(v), PhantomData) }
114 }
115 }
116
117 #[repr(transparent)]
118 pub struct NondispatchableHandle<T>(u64, PhantomData<Option<NonNull<T>>>);
119
120 impl<T> Clone for NondispatchableHandle<T> {
121 fn clone(&self) -> Self {
122 NondispatchableHandle(self.0, PhantomData)
123 }
124 }
125
126 impl<T> Copy for NondispatchableHandle<T> {}
127
128 impl<T> Eq for NondispatchableHandle<T> {}
129
130 impl<T> PartialEq for NondispatchableHandle<T> {
131 fn eq(&self, rhs: &Self) -> bool {
132 self.0 == rhs.0
133 }
134 }
135
136 impl<T> fmt::Debug for NondispatchableHandle<T> {
137 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
138 f.debug_tuple("NondispatchableHandle")
139 .field(&self.get().map(|v| v.as_ptr()).unwrap_or(null_mut()))
140 .finish()
141 }
142 }
143
144 impl<T> Handle for NondispatchableHandle<T> {
145 type Value = T;
146 fn get(&self) -> Option<NonNull<T>> {
147 NonNull::new(self.0 as *mut T)
148 }
149 fn new(v: Option<NonNull<T>>) -> Self {
150 NondispatchableHandle(
151 v.map(|v| v.as_ptr()).unwrap_or(null_mut()) as u64,
152 PhantomData,
153 )
154 }
155 }
156
157 pub struct OwnedHandle<T: HandleAllocFree>(NonNull<T::Value>);
158
159 impl<T: HandleAllocFree> OwnedHandle<T> {
160 pub fn new<I: Into<T::Value>>(v: I) -> Self {
161 unsafe { OwnedHandle(T::allocate(v).get().unwrap()) }
162 }
163 pub unsafe fn from(v: T) -> Option<Self> {
164 v.get().map(OwnedHandle)
165 }
166 pub unsafe fn take(self) -> T {
167 let retval = self.0;
168 mem::forget(self);
169 T::new(Some(retval))
170 }
171 pub unsafe fn get_handle(&self) -> T {
172 T::new(Some(self.0))
173 }
174 }
175
176 impl<T: HandleAllocFree> Deref for OwnedHandle<T> {
177 type Target = T::Value;
178 fn deref(&self) -> &T::Value {
179 unsafe { &*self.0.as_ptr() }
180 }
181 }
182
183 impl<T: HandleAllocFree> DerefMut for OwnedHandle<T> {
184 fn deref_mut(&mut self) -> &mut T::Value {
185 unsafe { &mut *self.0.as_ptr() }
186 }
187 }
188
189 impl<T: HandleAllocFree> Drop for OwnedHandle<T> {
190 fn drop(&mut self) {
191 unsafe {
192 T::new(Some(self.0)).free();
193 }
194 }
195 }
196
197 impl<T: HandleAllocFree> fmt::Debug for OwnedHandle<T>
198 where
199 T::Value: fmt::Debug,
200 {
201 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
202 f.debug_tuple("OwnedHandle").field((*self).deref()).finish()
203 }
204 }
205
206 #[derive(Copy, Clone)]
207 #[repr(transparent)]
208 pub struct SharedHandle<T: Handle>(NonNull<T::Value>);
209
210 impl<T: Handle> SharedHandle<T> {
211 pub unsafe fn from(v: T) -> Option<Self> {
212 v.get().map(SharedHandle)
213 }
214 pub unsafe fn take(self) -> T {
215 T::new(Some(self.0))
216 }
217 pub unsafe fn get_handle(&self) -> T {
218 T::new(Some(self.0))
219 }
220 pub fn into_nonnull(self) -> NonNull<T::Value> {
221 self.0
222 }
223 }
224
225 impl<T: Handle> Deref for SharedHandle<T> {
226 type Target = T::Value;
227 fn deref(&self) -> &T::Value {
228 unsafe { &*self.0.as_ptr() }
229 }
230 }
231
232 impl<T: Handle> fmt::Debug for SharedHandle<T>
233 where
234 T::Value: fmt::Debug,
235 {
236 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
237 f.debug_tuple("SharedHandle")
238 .field((*self).deref())
239 .finish()
240 }
241 }
242
243 pub type VkInstance = DispatchableHandle<Instance>;
244
245 impl HandleAllocFree for VkInstance {}
246
247 pub type VkPhysicalDevice = DispatchableHandle<PhysicalDevice>;
248
249 impl HandleAllocFree for VkPhysicalDevice {}
250
251 pub type VkDevice = DispatchableHandle<Device>;
252
253 impl HandleAllocFree for VkDevice {}
254
255 pub type VkQueue = DispatchableHandle<Queue>;
256
257 impl HandleAllocFree for VkQueue {}
258
259 pub struct CommandBuffer {}
260
261 pub type VkCommandBuffer = DispatchableHandle<CommandBuffer>;
262
263 impl HandleAllocFree for VkCommandBuffer {}
264
265 pub struct Semaphore {}
266
267 pub type VkSemaphore = NondispatchableHandle<Semaphore>;
268
269 impl HandleAllocFree for VkSemaphore {}
270
271 pub struct Fence {}
272
273 pub type VkFence = NondispatchableHandle<Fence>;
274
275 impl HandleAllocFree for VkFence {}
276
277 pub type VkDeviceMemory = NondispatchableHandle<DeviceMemory>;
278
279 impl HandleAllocFree for VkDeviceMemory {}
280
281 pub type VkBuffer = NondispatchableHandle<Buffer>;
282
283 impl HandleAllocFree for VkBuffer {}
284
285 pub struct Image {}
286
287 pub type VkImage = NondispatchableHandle<Image>;
288
289 impl HandleAllocFree for VkImage {}
290
291 pub struct Event {}
292
293 pub type VkEvent = NondispatchableHandle<Event>;
294
295 impl HandleAllocFree for VkEvent {}
296
297 pub struct QueryPool {}
298
299 pub type VkQueryPool = NondispatchableHandle<QueryPool>;
300
301 impl HandleAllocFree for VkQueryPool {}
302
303 pub struct BufferView {}
304
305 pub type VkBufferView = NondispatchableHandle<BufferView>;
306
307 impl HandleAllocFree for VkBufferView {}
308
309 pub struct ImageView {}
310
311 pub type VkImageView = NondispatchableHandle<ImageView>;
312
313 impl HandleAllocFree for VkImageView {}
314
315 pub type VkShaderModule = NondispatchableHandle<ShaderModule>;
316
317 impl HandleAllocFree for VkShaderModule {}
318
319 pub struct PipelineCache {}
320
321 pub type VkPipelineCache = NondispatchableHandle<PipelineCache>;
322
323 impl HandleAllocFree for VkPipelineCache {}
324
325 pub struct PipelineLayout {}
326
327 pub type VkPipelineLayout = NondispatchableHandle<PipelineLayout>;
328
329 impl HandleAllocFree for VkPipelineLayout {}
330
331 pub struct RenderPass {}
332
333 pub type VkRenderPass = NondispatchableHandle<RenderPass>;
334
335 impl HandleAllocFree for VkRenderPass {}
336
337 pub struct Pipeline {}
338
339 pub type VkPipeline = NondispatchableHandle<Pipeline>;
340
341 impl HandleAllocFree for VkPipeline {}
342
343 pub struct DescriptorSetLayout {}
344
345 pub type VkDescriptorSetLayout = NondispatchableHandle<DescriptorSetLayout>;
346
347 impl HandleAllocFree for VkDescriptorSetLayout {}
348
349 pub type VkSampler = NondispatchableHandle<Sampler>;
350
351 impl HandleAllocFree for VkSampler {}
352
353 pub struct DescriptorPool {}
354
355 pub type VkDescriptorPool = NondispatchableHandle<DescriptorPool>;
356
357 impl HandleAllocFree for VkDescriptorPool {}
358
359 pub struct DescriptorSet {}
360
361 pub type VkDescriptorSet = NondispatchableHandle<DescriptorSet>;
362
363 impl HandleAllocFree for VkDescriptorSet {}
364
365 pub struct Framebuffer {}
366
367 pub type VkFramebuffer = NondispatchableHandle<Framebuffer>;
368
369 impl HandleAllocFree for VkFramebuffer {}
370
371 pub struct CommandPool {}
372
373 pub type VkCommandPool = NondispatchableHandle<CommandPool>;
374
375 impl HandleAllocFree for VkCommandPool {}
376
377 pub type VkSamplerYcbcrConversion = NondispatchableHandle<SamplerYcbcrConversion>;
378
379 impl HandleAllocFree for VkSamplerYcbcrConversion {}
380
381 pub struct DescriptorUpdateTemplate {}
382
383 pub type VkDescriptorUpdateTemplate = NondispatchableHandle<DescriptorUpdateTemplate>;
384
385 impl HandleAllocFree for VkDescriptorUpdateTemplate {}
386
387 pub type VkSurfaceKHR = NondispatchableHandle<api::VkIcdSurfaceBase>;
388
389 // HandleAllocFree specifically not implemented for VkSurfaceKHR
390
391 pub type VkSwapchainKHR = NondispatchableHandle<Box<Swapchain>>;
392
393 impl HandleAllocFree for VkSwapchainKHR {}
394
395 pub struct DisplayKHR {}
396
397 pub type VkDisplayKHR = NondispatchableHandle<DisplayKHR>;
398
399 impl HandleAllocFree for VkDisplayKHR {}
400
401 pub struct DisplayModeKHR {}
402
403 pub type VkDisplayModeKHR = NondispatchableHandle<DisplayModeKHR>;
404
405 impl HandleAllocFree for VkDisplayModeKHR {}
406
407 pub struct DebugReportCallbackEXT {}
408
409 pub type VkDebugReportCallbackEXT = NondispatchableHandle<DebugReportCallbackEXT>;
410
411 impl HandleAllocFree for VkDebugReportCallbackEXT {}
412
413 pub struct DebugUtilsMessengerEXT {}
414
415 pub type VkDebugUtilsMessengerEXT = NondispatchableHandle<DebugUtilsMessengerEXT>;
416
417 impl HandleAllocFree for VkDebugUtilsMessengerEXT {}
418
419 pub struct ValidationCacheEXT {}
420
421 pub type VkValidationCacheEXT = NondispatchableHandle<ValidationCacheEXT>;
422
423 impl HandleAllocFree for VkValidationCacheEXT {}