bedrock/
handle.rs

1use crate::*;
2
3/// Wrapping a Vulkan Dispatchable/Nondispatchable Handler
4pub trait VkHandle {
5    type Handle;
6
7    /// Retrieve an underlying handle
8    fn native_ptr(&self) -> Self::Handle;
9
10    #[inline(always)]
11    fn as_transparent_ref(&self) -> VkHandleRef<Self::Handle> {
12        VkHandleRef::new(self)
13    }
14}
15/// Wrapping a Vulkan Dispatchable/Nondispatchable Mutable Handler
16pub trait VkHandleMut: VkHandle {
17    /// Retrieve an underlying mutable handle
18    fn native_ptr_mut(&mut self) -> Self::Handle;
19
20    #[inline(always)]
21    fn as_transparent_ref_mut(&mut self) -> VkHandleRefMut<Self::Handle> {
22        VkHandleRefMut::new(self)
23    }
24}
25
26DerefContainerBracketImpl!(for VkHandle {
27    type Handle = T::Handle;
28
29    #[inline(always)]
30    fn native_ptr(&self) -> Self::Handle { T::native_ptr(self) }
31});
32DerefContainerBracketImpl!(for mut VkHandleMut {
33    #[inline(always)]
34    fn native_ptr_mut(&mut self) -> Self::Handle { T::native_ptr_mut(self) }
35});
36
37impl<T: VkHandle + ?Sized> VkHandle for std::cell::Ref<'_, T> {
38    type Handle = T::Handle;
39
40    #[inline(always)]
41    fn native_ptr(&self) -> Self::Handle {
42        T::native_ptr(&**self)
43    }
44}
45impl<T: VkHandle + ?Sized> VkHandle for std::cell::RefMut<'_, T> {
46    type Handle = T::Handle;
47
48    #[inline(always)]
49    fn native_ptr(&self) -> Self::Handle {
50        T::native_ptr(&**self)
51    }
52}
53impl<T: VkHandleMut + ?Sized> VkHandleMut for std::cell::RefMut<'_, T> {
54    #[inline(always)]
55    fn native_ptr_mut(&mut self) -> Self::Handle {
56        T::native_ptr_mut(&mut **self)
57    }
58}
59
60impl<T: VkHandle + ?Sized> VkHandle for std::sync::MutexGuard<'_, T> {
61    type Handle = T::Handle;
62
63    #[inline(always)]
64    fn native_ptr(&self) -> Self::Handle {
65        T::native_ptr(&**self)
66    }
67}
68impl<T: VkHandleMut + ?Sized> VkHandleMut for std::sync::MutexGuard<'_, T> {
69    #[inline(always)]
70    fn native_ptr_mut(&mut self) -> Self::Handle {
71        T::native_ptr_mut(&mut **self)
72    }
73}
74
75impl<T: VkHandle + ?Sized> VkHandle for std::sync::RwLockReadGuard<'_, T> {
76    type Handle = T::Handle;
77
78    #[inline(always)]
79    fn native_ptr(&self) -> Self::Handle {
80        T::native_ptr(&**self)
81    }
82}
83
84impl<T: VkHandle + ?Sized> VkHandle for std::sync::RwLockWriteGuard<'_, T> {
85    type Handle = T::Handle;
86
87    #[inline(always)]
88    fn native_ptr(&self) -> Self::Handle {
89        T::native_ptr(&**self)
90    }
91}
92impl<T: VkHandleMut + ?Sized> VkHandleMut for std::sync::RwLockWriteGuard<'_, T> {
93    #[inline(always)]
94    fn native_ptr_mut(&mut self) -> Self::Handle {
95        T::native_ptr_mut(&mut **self)
96    }
97}
98
99impl<T: VkHandle + ?Sized> VkHandle for parking_lot::RwLockReadGuard<'_, T> {
100    type Handle = T::Handle;
101
102    #[inline(always)]
103    fn native_ptr(&self) -> Self::Handle {
104        T::native_ptr(&**self)
105    }
106}
107impl<T: VkHandle + ?Sized> VkHandle for parking_lot::MappedRwLockReadGuard<'_, T> {
108    type Handle = T::Handle;
109
110    #[inline(always)]
111    fn native_ptr(&self) -> Self::Handle {
112        T::native_ptr(&**self)
113    }
114}
115impl<T: VkHandle + ?Sized> VkHandle for parking_lot::RwLockWriteGuard<'_, T> {
116    type Handle = T::Handle;
117
118    #[inline(always)]
119    fn native_ptr(&self) -> Self::Handle {
120        T::native_ptr(&**self)
121    }
122}
123impl<T: VkHandle + ?Sized> VkHandle for parking_lot::MappedRwLockWriteGuard<'_, T> {
124    type Handle = T::Handle;
125
126    #[inline(always)]
127    fn native_ptr(&self) -> Self::Handle {
128        T::native_ptr(&**self)
129    }
130}
131impl<T: VkHandleMut + ?Sized> VkHandleMut for parking_lot::RwLockWriteGuard<'_, T> {
132    #[inline(always)]
133    fn native_ptr_mut(&mut self) -> Self::Handle {
134        T::native_ptr_mut(&mut **self)
135    }
136}
137impl<T: VkHandleMut + ?Sized> VkHandleMut for parking_lot::MappedRwLockWriteGuard<'_, T> {
138    #[inline(always)]
139    fn native_ptr_mut(&mut self) -> Self::Handle {
140        T::native_ptr_mut(&mut **self)
141    }
142}
143
144impl<T: VkHandle + ?Sized> VkHandle for parking_lot::MutexGuard<'_, T> {
145    type Handle = T::Handle;
146
147    #[inline(always)]
148    fn native_ptr(&self) -> Self::Handle {
149        T::native_ptr(&**self)
150    }
151}
152impl<T: VkHandle + ?Sized> VkHandle for parking_lot::MappedMutexGuard<'_, T> {
153    type Handle = T::Handle;
154
155    #[inline(always)]
156    fn native_ptr(&self) -> Self::Handle {
157        T::native_ptr(&**self)
158    }
159}
160impl<T: VkHandleMut + ?Sized> VkHandleMut for parking_lot::MutexGuard<'_, T> {
161    #[inline(always)]
162    fn native_ptr_mut(&mut self) -> Self::Handle {
163        T::native_ptr_mut(&mut **self)
164    }
165}
166impl<T: VkHandleMut + ?Sized> VkHandleMut for parking_lot::MappedMutexGuard<'_, T> {
167    #[inline(always)]
168    fn native_ptr_mut(&mut self) -> Self::Handle {
169        T::native_ptr_mut(&mut **self)
170    }
171}
172
173pub trait VkRawHandle {
174    const OBJECT_TYPE: VkObjectType;
175    const NULL: Self;
176
177    fn raw_handle_value(&self) -> u64;
178}
179
180/// Extension methods(not dyn compatible) for `VkHandle`
181pub trait VkHandleExt: VkHandle {
182    /// Checks the equality between vulkan objects by their handle value.
183    #[inline(always)]
184    fn eq_handle(&self, other: &Self) -> bool
185    where
186        Self::Handle: VkRawHandle,
187    {
188        self.native_ptr().raw_handle_value() == other.native_ptr().raw_handle_value()
189    }
190}
191impl<T: VkHandle> VkHandleExt for T {}
192
193pub trait VkDeviceChildNonExtDestroyable {
194    unsafe fn destroy(self, device: crate::vk::VkDevice, allocator: *const crate::vk::VkAllocationCallbacks);
195}
196
197/// A smart handle to a Vulkan object that holds a source lifetime
198/// (bitpattern as same as native handle type)
199#[repr(transparent)]
200#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
201pub struct VkHandleRef<'r, H>(pub(crate) H, core::marker::PhantomData<&'r dyn VkHandle<Handle = H>>);
202impl<'r, H> VkHandleRef<'r, H> {
203    pub fn new(r: &'r (impl VkHandle<Handle = H> + ?Sized)) -> Self {
204        Self(r.native_ptr(), core::marker::PhantomData)
205    }
206
207    pub const fn from_raw_ref(h: &'r H) -> &'r Self {
208        unsafe { core::mem::transmute(h) }
209    }
210
211    /// simple raw handle wrapper without any lifetime constraints.
212    /// # Safety
213    /// owner of the handle must be alive while the handle will be used.
214    pub const unsafe fn dangling(h: H) -> Self {
215        Self(h, core::marker::PhantomData)
216    }
217}
218impl<H: Copy> VkHandle for VkHandleRef<'_, H> {
219    type Handle = H;
220
221    #[inline(always)]
222    fn native_ptr(&self) -> H {
223        self.0
224    }
225}
226
227/// A smart handle to a Vulkan object that holds a source lifetime and mutable-borrowing
228/// (bitpattern as same as native handle type)
229#[repr(transparent)]
230#[derive(Clone, Hash, PartialEq, Eq, Debug)]
231pub struct VkHandleRefMut<'r, H>(
232    pub(crate) H,
233    core::marker::PhantomData<&'r mut dyn VkHandleMut<Handle = H>>,
234);
235impl<'r, H> VkHandleRefMut<'r, H> {
236    pub fn new(r: &'r mut (impl VkHandleMut<Handle = H> + ?Sized)) -> Self {
237        Self(r.native_ptr(), core::marker::PhantomData)
238    }
239
240    pub const fn from_raw_ref(h: &'r H) -> &'r Self {
241        unsafe { core::mem::transmute(h) }
242    }
243
244    /// simple raw handle wrapper without any lifetime constraints.
245    /// # Safety
246    /// owner of the handle must be alive while the handle will be used.
247    pub const unsafe fn dangling(h: H) -> Self {
248        Self(h, core::marker::PhantomData)
249    }
250}
251impl<H: Copy> VkHandle for VkHandleRefMut<'_, H> {
252    type Handle = H;
253
254    #[inline(always)]
255    fn native_ptr(&self) -> H {
256        self.0
257    }
258}
259impl<H: Copy> VkHandleMut for VkHandleRefMut<'_, H> {
260    #[inline(always)]
261    fn native_ptr_mut(&mut self) -> H {
262        self.0
263    }
264}