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