working on implementing vulkan functions
[kazan.git] / vulkan-driver / src / handle.rs
1 use api;
2 use api_impl::{Instance, PhysicalDevice};
3 use std::marker::PhantomData;
4 use std::mem;
5 use std::ops::Deref;
6 use std::ops::DerefMut;
7 use std::ptr::null_mut;
8 use std::ptr::NonNull;
9
10 #[repr(C)]
11 pub struct DispatchableType<T> {
12 loader_dispatch_ptr: usize,
13 value: T,
14 }
15
16 impl<T> DispatchableType<T> {}
17
18 impl<T> From<T> for DispatchableType<T> {
19 fn from(v: T) -> Self {
20 Self {
21 loader_dispatch_ptr: api::ICD_LOADER_MAGIC as usize,
22 value: v,
23 }
24 }
25 }
26
27 impl<T> Deref for DispatchableType<T> {
28 type Target = T;
29 fn deref(&self) -> &T {
30 &self.value
31 }
32 }
33
34 impl<T> DerefMut for DispatchableType<T> {
35 fn deref_mut(&mut self) -> &mut T {
36 &mut self.value
37 }
38 }
39
40 pub trait Handle: Clone {
41 type Value;
42 fn get(&self) -> Option<NonNull<Self::Value>>;
43 fn new(v: Option<NonNull<Self::Value>>) -> Self;
44 unsafe fn allocate<T: Into<Self::Value>>(v: T) -> Self {
45 Self::new(Some(NonNull::new_unchecked(Box::into_raw(Box::new(
46 v.into(),
47 )))))
48 }
49 unsafe fn free(self) {
50 Box::from_raw(self.get().unwrap().as_ptr());
51 }
52 fn null() -> Self {
53 Self::new(None)
54 }
55 fn is_null(&self) -> bool {
56 self.get().is_none()
57 }
58 fn take(&mut self) -> Self {
59 let retval = self.clone();
60 *self = Self::null();
61 retval
62 }
63 }
64
65 #[repr(transparent)]
66 pub struct DispatchableHandle<T>(Option<NonNull<()>>, PhantomData<*mut DispatchableType<T>>);
67
68 impl<T> Clone for DispatchableHandle<T> {
69 fn clone(&self) -> Self {
70 DispatchableHandle(self.0, PhantomData)
71 }
72 }
73
74 impl<T> Handle for DispatchableHandle<T> {
75 type Value = DispatchableType<T>;
76 fn get(&self) -> Option<NonNull<DispatchableType<T>>> {
77 unsafe { mem::transmute(self.0) }
78 }
79 fn new(v: Option<NonNull<DispatchableType<T>>>) -> Self {
80 unsafe { DispatchableHandle(mem::transmute(v), PhantomData) }
81 }
82 }
83
84 #[repr(transparent)]
85 pub struct NondispatchableHandle<T>(u64, PhantomData<Option<NonNull<T>>>);
86
87 impl<T> Clone for NondispatchableHandle<T> {
88 fn clone(&self) -> Self {
89 NondispatchableHandle(self.0, PhantomData)
90 }
91 }
92
93 impl<T> Handle for NondispatchableHandle<T> {
94 type Value = T;
95 fn get(&self) -> Option<NonNull<T>> {
96 NonNull::new(self.0 as *mut T)
97 }
98 fn new(v: Option<NonNull<T>>) -> Self {
99 NondispatchableHandle(
100 v.map(|v| v.as_ptr()).unwrap_or(null_mut()) as u64,
101 PhantomData,
102 )
103 }
104 }
105
106 #[derive(Debug)]
107 #[repr(transparent)]
108 pub struct OwnedHandle<T: Handle>(T);
109
110 impl<T: Handle> OwnedHandle<T> {
111 pub fn new<I: Into<T::Value>>(v: I) -> Self {
112 unsafe { OwnedHandle(T::allocate(v)) }
113 }
114 pub unsafe fn from(v: T) -> Self {
115 OwnedHandle(v)
116 }
117 pub unsafe fn take(mut self) -> T {
118 self.0.take()
119 }
120 pub unsafe fn get_handle(&self) -> &T {
121 &self.0
122 }
123 }
124
125 impl<T: Handle> Deref for OwnedHandle<T> {
126 type Target = T::Value;
127 fn deref(&self) -> &T::Value {
128 unsafe { &*self.0.get().unwrap().as_ptr() }
129 }
130 }
131
132 impl<T: Handle> DerefMut for OwnedHandle<T> {
133 fn deref_mut(&mut self) -> &mut T::Value {
134 unsafe { &mut *self.0.get().unwrap().as_ptr() }
135 }
136 }
137
138 impl<T: Handle> Drop for OwnedHandle<T> {
139 fn drop(&mut self) {
140 if !self.0.is_null() {
141 unsafe {
142 self.0.take().free();
143 }
144 }
145 }
146 }
147
148 #[derive(Debug)]
149 #[repr(transparent)]
150 pub struct SharedHandle<T: Handle>(T);
151
152 impl<T: Handle> SharedHandle<T> {
153 pub unsafe fn from(v: T) -> Self {
154 SharedHandle(v)
155 }
156 pub unsafe fn take(mut self) -> T {
157 self.0.take()
158 }
159 pub unsafe fn get_handle(&self) -> &T {
160 &self.0
161 }
162 }
163
164 impl<T: Handle> Deref for SharedHandle<T> {
165 type Target = T::Value;
166 fn deref(&self) -> &T::Value {
167 unsafe { &*self.0.get().unwrap().as_ptr() }
168 }
169 }
170
171 pub type VkInstance = DispatchableHandle<Instance>;
172
173 pub type VkPhysicalDevice = DispatchableHandle<PhysicalDevice>;
174
175 pub struct Device {}
176
177 pub type VkDevice = DispatchableHandle<Device>;
178
179 pub struct Queue {}
180
181 pub type VkQueue = DispatchableHandle<Queue>;
182
183 pub struct CommandBuffer {}
184
185 pub type VkCommandBuffer = DispatchableHandle<CommandBuffer>;
186
187 pub struct Semaphore {}
188
189 pub type VkSemaphore = NondispatchableHandle<Semaphore>;
190
191 pub struct Fence {}
192
193 pub type VkFence = NondispatchableHandle<Fence>;
194
195 pub struct DeviceMemory {}
196
197 pub type VkDeviceMemory = NondispatchableHandle<DeviceMemory>;
198
199 pub struct Buffer {}
200
201 pub type VkBuffer = NondispatchableHandle<Buffer>;
202
203 pub struct Image {}
204
205 pub type VkImage = NondispatchableHandle<Image>;
206
207 pub struct Event {}
208
209 pub type VkEvent = NondispatchableHandle<Event>;
210
211 pub struct QueryPool {}
212
213 pub type VkQueryPool = NondispatchableHandle<QueryPool>;
214
215 pub struct BufferView {}
216
217 pub type VkBufferView = NondispatchableHandle<BufferView>;
218
219 pub struct ImageView {}
220
221 pub type VkImageView = NondispatchableHandle<ImageView>;
222
223 pub struct ShaderModule {}
224
225 pub type VkShaderModule = NondispatchableHandle<ShaderModule>;
226
227 pub struct PipelineCache {}
228
229 pub type VkPipelineCache = NondispatchableHandle<PipelineCache>;
230
231 pub struct PipelineLayout {}
232
233 pub type VkPipelineLayout = NondispatchableHandle<PipelineLayout>;
234
235 pub struct RenderPass {}
236
237 pub type VkRenderPass = NondispatchableHandle<RenderPass>;
238
239 pub struct Pipeline {}
240
241 pub type VkPipeline = NondispatchableHandle<Pipeline>;
242
243 pub struct DescriptorSetLayout {}
244
245 pub type VkDescriptorSetLayout = NondispatchableHandle<DescriptorSetLayout>;
246
247 pub struct Sampler {}
248
249 pub type VkSampler = NondispatchableHandle<Sampler>;
250
251 pub struct DescriptorPool {}
252
253 pub type VkDescriptorPool = NondispatchableHandle<DescriptorPool>;
254
255 pub struct DescriptorSet {}
256
257 pub type VkDescriptorSet = NondispatchableHandle<DescriptorSet>;
258
259 pub struct Framebuffer {}
260
261 pub type VkFramebuffer = NondispatchableHandle<Framebuffer>;
262
263 pub struct CommandPool {}
264
265 pub type VkCommandPool = NondispatchableHandle<CommandPool>;
266
267 pub struct SamplerYcbcrConversion {}
268
269 pub type VkSamplerYcbcrConversion = NondispatchableHandle<SamplerYcbcrConversion>;
270
271 pub struct DescriptorUpdateTemplate {}
272
273 pub type VkDescriptorUpdateTemplate = NondispatchableHandle<DescriptorUpdateTemplate>;
274
275 pub struct SurfaceKHR {}
276
277 pub type VkSurfaceKHR = NondispatchableHandle<SurfaceKHR>;
278
279 pub struct SwapchainKHR {}
280
281 pub type VkSwapchainKHR = NondispatchableHandle<SwapchainKHR>;
282
283 pub struct DisplayKHR {}
284
285 pub type VkDisplayKHR = NondispatchableHandle<DisplayKHR>;
286
287 pub struct DisplayModeKHR {}
288
289 pub type VkDisplayModeKHR = NondispatchableHandle<DisplayModeKHR>;
290
291 pub struct DebugReportCallbackEXT {}
292
293 pub type VkDebugReportCallbackEXT = NondispatchableHandle<DebugReportCallbackEXT>;
294
295 pub struct DebugUtilsMessengerEXT {}
296
297 pub type VkDebugUtilsMessengerEXT = NondispatchableHandle<DebugUtilsMessengerEXT>;
298
299 pub struct ValidationCacheEXT {}
300
301 pub type VkValidationCacheEXT = NondispatchableHandle<ValidationCacheEXT>;