2 use api_impl::{Instance, PhysicalDevice};
3 use std::marker::PhantomData;
6 use std::ops::DerefMut;
7 use std::ptr::null_mut;
11 pub struct DispatchableType<T> {
12 loader_dispatch_ptr: usize,
16 impl<T> DispatchableType<T> {}
18 impl<T> From<T> for DispatchableType<T> {
19 fn from(v: T) -> Self {
21 loader_dispatch_ptr: api::ICD_LOADER_MAGIC as usize,
27 impl<T> Deref for DispatchableType<T> {
29 fn deref(&self) -> &T {
34 impl<T> DerefMut for DispatchableType<T> {
35 fn deref_mut(&mut self) -> &mut T {
40 pub trait Handle: Clone {
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(
49 unsafe fn free(self) {
50 Box::from_raw(self.get().unwrap().as_ptr());
55 fn is_null(&self) -> bool {
58 fn take(&mut self) -> Self {
59 let retval = self.clone();
66 pub struct DispatchableHandle<T>(Option<NonNull<()>>, PhantomData<*mut DispatchableType<T>>);
68 impl<T> Clone for DispatchableHandle<T> {
69 fn clone(&self) -> Self {
70 DispatchableHandle(self.0, PhantomData)
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) }
79 fn new(v: Option<NonNull<DispatchableType<T>>>) -> Self {
80 unsafe { DispatchableHandle(mem::transmute(v), PhantomData) }
85 pub struct NondispatchableHandle<T>(u64, PhantomData<Option<NonNull<T>>>);
87 impl<T> Clone for NondispatchableHandle<T> {
88 fn clone(&self) -> Self {
89 NondispatchableHandle(self.0, PhantomData)
93 impl<T> Handle for NondispatchableHandle<T> {
95 fn get(&self) -> Option<NonNull<T>> {
96 NonNull::new(self.0 as *mut T)
98 fn new(v: Option<NonNull<T>>) -> Self {
99 NondispatchableHandle(
100 v.map(|v| v.as_ptr()).unwrap_or(null_mut()) as u64,
108 pub struct OwnedHandle<T: Handle>(T);
110 impl<T: Handle> OwnedHandle<T> {
111 pub fn new<I: Into<T::Value>>(v: I) -> Self {
112 unsafe { OwnedHandle(T::allocate(v)) }
114 pub unsafe fn from(v: T) -> Self {
117 pub unsafe fn take(mut self) -> T {
120 pub unsafe fn get_handle(&self) -> &T {
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() }
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() }
138 impl<T: Handle> Drop for OwnedHandle<T> {
140 if !self.0.is_null() {
142 self.0.take().free();
150 pub struct SharedHandle<T: Handle>(T);
152 impl<T: Handle> SharedHandle<T> {
153 pub unsafe fn from(v: T) -> Self {
156 pub unsafe fn take(mut self) -> T {
159 pub unsafe fn get_handle(&self) -> &T {
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() }
171 pub type VkInstance = DispatchableHandle<Instance>;
173 pub type VkPhysicalDevice = DispatchableHandle<PhysicalDevice>;
177 pub type VkDevice = DispatchableHandle<Device>;
181 pub type VkQueue = DispatchableHandle<Queue>;
183 pub struct CommandBuffer {}
185 pub type VkCommandBuffer = DispatchableHandle<CommandBuffer>;
187 pub struct Semaphore {}
189 pub type VkSemaphore = NondispatchableHandle<Semaphore>;
193 pub type VkFence = NondispatchableHandle<Fence>;
195 pub struct DeviceMemory {}
197 pub type VkDeviceMemory = NondispatchableHandle<DeviceMemory>;
201 pub type VkBuffer = NondispatchableHandle<Buffer>;
205 pub type VkImage = NondispatchableHandle<Image>;
209 pub type VkEvent = NondispatchableHandle<Event>;
211 pub struct QueryPool {}
213 pub type VkQueryPool = NondispatchableHandle<QueryPool>;
215 pub struct BufferView {}
217 pub type VkBufferView = NondispatchableHandle<BufferView>;
219 pub struct ImageView {}
221 pub type VkImageView = NondispatchableHandle<ImageView>;
223 pub struct ShaderModule {}
225 pub type VkShaderModule = NondispatchableHandle<ShaderModule>;
227 pub struct PipelineCache {}
229 pub type VkPipelineCache = NondispatchableHandle<PipelineCache>;
231 pub struct PipelineLayout {}
233 pub type VkPipelineLayout = NondispatchableHandle<PipelineLayout>;
235 pub struct RenderPass {}
237 pub type VkRenderPass = NondispatchableHandle<RenderPass>;
239 pub struct Pipeline {}
241 pub type VkPipeline = NondispatchableHandle<Pipeline>;
243 pub struct DescriptorSetLayout {}
245 pub type VkDescriptorSetLayout = NondispatchableHandle<DescriptorSetLayout>;
247 pub struct Sampler {}
249 pub type VkSampler = NondispatchableHandle<Sampler>;
251 pub struct DescriptorPool {}
253 pub type VkDescriptorPool = NondispatchableHandle<DescriptorPool>;
255 pub struct DescriptorSet {}
257 pub type VkDescriptorSet = NondispatchableHandle<DescriptorSet>;
259 pub struct Framebuffer {}
261 pub type VkFramebuffer = NondispatchableHandle<Framebuffer>;
263 pub struct CommandPool {}
265 pub type VkCommandPool = NondispatchableHandle<CommandPool>;
267 pub struct SamplerYcbcrConversion {}
269 pub type VkSamplerYcbcrConversion = NondispatchableHandle<SamplerYcbcrConversion>;
271 pub struct DescriptorUpdateTemplate {}
273 pub type VkDescriptorUpdateTemplate = NondispatchableHandle<DescriptorUpdateTemplate>;
275 pub struct SurfaceKHR {}
277 pub type VkSurfaceKHR = NondispatchableHandle<SurfaceKHR>;
279 pub struct SwapchainKHR {}
281 pub type VkSwapchainKHR = NondispatchableHandle<SwapchainKHR>;
283 pub struct DisplayKHR {}
285 pub type VkDisplayKHR = NondispatchableHandle<DisplayKHR>;
287 pub struct DisplayModeKHR {}
289 pub type VkDisplayModeKHR = NondispatchableHandle<DisplayModeKHR>;
291 pub struct DebugReportCallbackEXT {}
293 pub type VkDebugReportCallbackEXT = NondispatchableHandle<DebugReportCallbackEXT>;
295 pub struct DebugUtilsMessengerEXT {}
297 pub type VkDebugUtilsMessengerEXT = NondispatchableHandle<DebugUtilsMessengerEXT>;
299 pub struct ValidationCacheEXT {}
301 pub type VkValidationCacheEXT = NondispatchableHandle<ValidationCacheEXT>;