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