a2e1cd8f667a1fe5877116b66e57f06acca3694e
[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 }
221
222 impl<T: Handle> Deref for SharedHandle<T> {
223 type Target = T::Value;
224 fn deref(&self) -> &T::Value {
225 unsafe { &*self.0.as_ptr() }
226 }
227 }
228
229 impl<T: Handle> fmt::Debug for SharedHandle<T>
230 where
231 T::Value: fmt::Debug,
232 {
233 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
234 f.debug_tuple("SharedHandle")
235 .field((*self).deref())
236 .finish()
237 }
238 }
239
240 pub type VkInstance = DispatchableHandle<Instance>;
241
242 impl HandleAllocFree for VkInstance {}
243
244 pub type VkPhysicalDevice = DispatchableHandle<PhysicalDevice>;
245
246 impl HandleAllocFree for VkPhysicalDevice {}
247
248 pub type VkDevice = DispatchableHandle<Device>;
249
250 impl HandleAllocFree for VkDevice {}
251
252 pub type VkQueue = DispatchableHandle<Queue>;
253
254 impl HandleAllocFree for VkQueue {}
255
256 pub struct CommandBuffer {}
257
258 pub type VkCommandBuffer = DispatchableHandle<CommandBuffer>;
259
260 impl HandleAllocFree for VkCommandBuffer {}
261
262 pub struct Semaphore {}
263
264 pub type VkSemaphore = NondispatchableHandle<Semaphore>;
265
266 impl HandleAllocFree for VkSemaphore {}
267
268 pub struct Fence {}
269
270 pub type VkFence = NondispatchableHandle<Fence>;
271
272 impl HandleAllocFree for VkFence {}
273
274 pub type VkDeviceMemory = NondispatchableHandle<DeviceMemory>;
275
276 impl HandleAllocFree for VkDeviceMemory {}
277
278 pub type VkBuffer = NondispatchableHandle<Buffer>;
279
280 impl HandleAllocFree for VkBuffer {}
281
282 pub struct Image {}
283
284 pub type VkImage = NondispatchableHandle<Image>;
285
286 impl HandleAllocFree for VkImage {}
287
288 pub struct Event {}
289
290 pub type VkEvent = NondispatchableHandle<Event>;
291
292 impl HandleAllocFree for VkEvent {}
293
294 pub struct QueryPool {}
295
296 pub type VkQueryPool = NondispatchableHandle<QueryPool>;
297
298 impl HandleAllocFree for VkQueryPool {}
299
300 pub struct BufferView {}
301
302 pub type VkBufferView = NondispatchableHandle<BufferView>;
303
304 impl HandleAllocFree for VkBufferView {}
305
306 pub struct ImageView {}
307
308 pub type VkImageView = NondispatchableHandle<ImageView>;
309
310 impl HandleAllocFree for VkImageView {}
311
312 pub type VkShaderModule = NondispatchableHandle<ShaderModule>;
313
314 impl HandleAllocFree for VkShaderModule {}
315
316 pub struct PipelineCache {}
317
318 pub type VkPipelineCache = NondispatchableHandle<PipelineCache>;
319
320 impl HandleAllocFree for VkPipelineCache {}
321
322 pub struct PipelineLayout {}
323
324 pub type VkPipelineLayout = NondispatchableHandle<PipelineLayout>;
325
326 impl HandleAllocFree for VkPipelineLayout {}
327
328 pub struct RenderPass {}
329
330 pub type VkRenderPass = NondispatchableHandle<RenderPass>;
331
332 impl HandleAllocFree for VkRenderPass {}
333
334 pub struct Pipeline {}
335
336 pub type VkPipeline = NondispatchableHandle<Pipeline>;
337
338 impl HandleAllocFree for VkPipeline {}
339
340 pub struct DescriptorSetLayout {}
341
342 pub type VkDescriptorSetLayout = NondispatchableHandle<DescriptorSetLayout>;
343
344 impl HandleAllocFree for VkDescriptorSetLayout {}
345
346 pub type VkSampler = NondispatchableHandle<Sampler>;
347
348 impl HandleAllocFree for VkSampler {}
349
350 pub struct DescriptorPool {}
351
352 pub type VkDescriptorPool = NondispatchableHandle<DescriptorPool>;
353
354 impl HandleAllocFree for VkDescriptorPool {}
355
356 pub struct DescriptorSet {}
357
358 pub type VkDescriptorSet = NondispatchableHandle<DescriptorSet>;
359
360 impl HandleAllocFree for VkDescriptorSet {}
361
362 pub struct Framebuffer {}
363
364 pub type VkFramebuffer = NondispatchableHandle<Framebuffer>;
365
366 impl HandleAllocFree for VkFramebuffer {}
367
368 pub struct CommandPool {}
369
370 pub type VkCommandPool = NondispatchableHandle<CommandPool>;
371
372 impl HandleAllocFree for VkCommandPool {}
373
374 pub type VkSamplerYcbcrConversion = NondispatchableHandle<SamplerYcbcrConversion>;
375
376 impl HandleAllocFree for VkSamplerYcbcrConversion {}
377
378 pub struct DescriptorUpdateTemplate {}
379
380 pub type VkDescriptorUpdateTemplate = NondispatchableHandle<DescriptorUpdateTemplate>;
381
382 impl HandleAllocFree for VkDescriptorUpdateTemplate {}
383
384 pub type VkSurfaceKHR = NondispatchableHandle<api::VkIcdSurfaceBase>;
385
386 // HandleAllocFree specifically not implemented for VkSurfaceKHR
387
388 pub type VkSwapchainKHR = NondispatchableHandle<Box<Swapchain>>;
389
390 impl HandleAllocFree for VkSwapchainKHR {}
391
392 pub struct DisplayKHR {}
393
394 pub type VkDisplayKHR = NondispatchableHandle<DisplayKHR>;
395
396 impl HandleAllocFree for VkDisplayKHR {}
397
398 pub struct DisplayModeKHR {}
399
400 pub type VkDisplayModeKHR = NondispatchableHandle<DisplayModeKHR>;
401
402 impl HandleAllocFree for VkDisplayModeKHR {}
403
404 pub struct DebugReportCallbackEXT {}
405
406 pub type VkDebugReportCallbackEXT = NondispatchableHandle<DebugReportCallbackEXT>;
407
408 impl HandleAllocFree for VkDebugReportCallbackEXT {}
409
410 pub struct DebugUtilsMessengerEXT {}
411
412 pub type VkDebugUtilsMessengerEXT = NondispatchableHandle<DebugUtilsMessengerEXT>;
413
414 impl HandleAllocFree for VkDebugUtilsMessengerEXT {}
415
416 pub struct ValidationCacheEXT {}
417
418 pub type VkValidationCacheEXT = NondispatchableHandle<ValidationCacheEXT>;
419
420 impl HandleAllocFree for VkValidationCacheEXT {}