1 /*
2 * Copyright © 2020 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23 #ifndef VK_OBJECT_H
24 #define VK_OBJECT_H
25
26 #include <vulkan/vulkan_core.h>
27 #include <vulkan/vk_icd.h>
28
29 #include "c11/threads.h"
30 #include "util/detect_os.h"
31 #include "util/macros.h"
32 #include "util/sparse_array.h"
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 struct hash_table;
39
40 struct vk_device;
41
42 /** Base struct for all Vulkan objects */
43 struct vk_object_base {
44 VK_LOADER_DATA _loader_data;
45
46 /** Type of this object
47 *
48 * This is used for runtime type checking when casting to and from Vulkan
49 * handle types since compile-time type checking doesn't always work.
50 */
51 VkObjectType type;
52
53 /* True if this object is fully constructed and visible to the client */
54 bool client_visible;
55
56 /** Pointer to the device in which this object exists, if any
57 *
58 * This is NULL for instances and physical devices but should point to a
59 * valid vk_device for almost everything else. (There are a few WSI
60 * objects that don't inherit from a device.)
61 */
62 struct vk_device *device;
63
64 /** Pointer to the instance in which this object exists
65 *
66 * This is NULL for device level objects as it's main purpose is to make
67 * the instance allocator reachable for freeing data owned by instance
68 * level objects.
69 */
70 struct vk_instance *instance;
71
72 /* For VK_EXT_private_data */
73 struct util_sparse_array private_data;
74
75 /* VK_EXT_debug_utils */
76 char *object_name;
77 };
78
79 /** Initialize a vk_base_object
80 *
81 * :param device: |in| The vk_device this object was created from or NULL
82 * :param base: |out| The vk_object_base to initialize
83 * :param obj_type: |in| The VkObjectType of the object being initialized
84 */
85 void vk_object_base_init(struct vk_device *device,
86 struct vk_object_base *base,
87 VkObjectType obj_type);
88
89 /** Initialize a vk_base_object for an instance level object
90 *
91 * :param instance: |in| The vk_instance this object was created from
92 * :param base: |out| The vk_object_base to initialize
93 * :param obj_type: |in| The VkObjectType of the object being initialized
94 */
95 void vk_object_base_instance_init(struct vk_instance *instance,
96 struct vk_object_base *base,
97 VkObjectType obj_type);
98
99 /** Tear down a vk_object_base
100 *
101 * :param base: |out| The vk_object_base being torn down
102 */
103 void vk_object_base_finish(struct vk_object_base *base);
104
105 /** Recycles a vk_object_base
106 *
107 * This should be called when an object is recycled and handed back to the
108 * client as if it were a new object. When it's called is not important as
109 * long as it's called between when the client thinks the object was destroyed
110 * and when the client sees it again as a supposedly new object.
111 *
112 * :param base: |inout| The vk_object_base being recycled
113 */
114 void vk_object_base_recycle(struct vk_object_base *base);
115
116 static inline void
vk_object_base_assert_valid(ASSERTED struct vk_object_base * base,ASSERTED VkObjectType obj_type)117 vk_object_base_assert_valid(ASSERTED struct vk_object_base *base,
118 ASSERTED VkObjectType obj_type)
119 {
120 assert(base == NULL || base->type == obj_type);
121 }
122
123 static inline struct vk_object_base *
vk_object_base_from_u64_handle(uint64_t handle,VkObjectType obj_type)124 vk_object_base_from_u64_handle(uint64_t handle, VkObjectType obj_type)
125 {
126 struct vk_object_base *base = (struct vk_object_base *)(uintptr_t)handle;
127 vk_object_base_assert_valid(base, obj_type);
128 return base;
129 }
130
131 /** Define handle cast macros for the given dispatchable handle type
132 *
133 * For a given `driver_struct`, this defines `driver_struct_to_handle()` and
134 * `driver_struct_from_handle()` helpers which provide type-safe (as much as
135 * possible with Vulkan handle types) casts to and from the `driver_struct`
136 * type. As an added layer of protection, these casts use the provided
137 * `VkObjectType` to assert that the object is of the correct type when
138 * running with a debug build.
139 *
140 * :param __driver_type: The name of the driver struct; it is assumed this is
141 * the name of a struct type and ``struct`` will be
142 * prepended automatically
143 *
144 * :param __base: The name of the vk_base_object member
145 *
146 * :param __VkType: The Vulkan object type such as VkImage
147 *
148 * :param __VK_TYPE: The VkObjectType corresponding to __VkType, such as
149 * VK_OBJECT_TYPE_IMAGE
150 */
151 #define VK_DEFINE_HANDLE_CASTS(__driver_type, __base, __VkType, __VK_TYPE) \
152 static inline struct __driver_type * \
153 __driver_type ## _from_handle(__VkType _handle) \
154 { \
155 struct vk_object_base *base = (struct vk_object_base *)_handle; \
156 vk_object_base_assert_valid(base, __VK_TYPE); \
157 STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \
158 return (struct __driver_type *) base; \
159 } \
160 \
161 static inline __VkType \
162 __driver_type ## _to_handle(struct __driver_type *_obj) \
163 { \
164 if (_obj != NULL) { \
165 vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \
166 _obj->__base.client_visible = true; \
167 } \
168 return (__VkType) _obj; \
169 }
170
171 /** Define handle cast macros for the given non-dispatchable handle type
172 *
173 * For a given `driver_struct`, this defines `driver_struct_to_handle()` and
174 * `driver_struct_from_handle()` helpers which provide type-safe (as much as
175 * possible with Vulkan handle types) casts to and from the `driver_struct`
176 * type. As an added layer of protection, these casts use the provided
177 * `VkObjectType` to assert that the object is of the correct type when
178 * running with a debug build.
179 *
180 * :param __driver_type: The name of the driver struct; it is assumed this is
181 * the name of a struct type and ``struct`` will be
182 * prepended automatically
183 *
184 * :param __base: The name of the vk_base_object member
185 *
186 * :param __VkType: The Vulkan object type such as VkImage
187 *
188 * :param __VK_TYPE: The VkObjectType corresponding to __VkType, such as
189 * VK_OBJECT_TYPE_IMAGE
190 */
191 #define VK_DEFINE_NONDISP_HANDLE_CASTS(__driver_type, __base, __VkType, __VK_TYPE) \
192 UNUSED static inline struct __driver_type * \
193 __driver_type ## _from_handle(__VkType _handle) \
194 { \
195 struct vk_object_base *base = \
196 (struct vk_object_base *)(uintptr_t)_handle; \
197 vk_object_base_assert_valid(base, __VK_TYPE); \
198 STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \
199 return (struct __driver_type *)base; \
200 } \
201 \
202 UNUSED static inline __VkType \
203 __driver_type ## _to_handle(struct __driver_type *_obj) \
204 { \
205 if (_obj != NULL) { \
206 vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \
207 _obj->__base.client_visible = true; \
208 } \
209 return (__VkType)(uintptr_t) _obj; \
210 }
211
212 /** Declares a __driver_type pointer which represents __handle
213 *
214 * :param __driver_type: The name of the driver struct; it is assumed this is
215 * the name of a struct type and ``struct`` will be
216 * prepended automatically
217 *
218 * :param __name: The name of the declared pointer
219 *
220 * :param __handle: The Vulkan object handle with which to initialize
221 * `__name`
222 */
223 #define VK_FROM_HANDLE(__driver_type, __name, __handle) \
224 struct __driver_type *__name = __driver_type ## _from_handle(__handle)
225
226 /* Helpers for vk object (de)allocation and (de)initialization */
227 void *
228 vk_object_alloc(struct vk_device *device,
229 const VkAllocationCallbacks *alloc,
230 size_t size,
231 VkObjectType vk_obj_type);
232
233 void *
234 vk_object_zalloc(struct vk_device *device,
235 const VkAllocationCallbacks *alloc,
236 size_t size,
237 VkObjectType vk_obj_type);
238
239 struct vk_multialloc;
240
241 void *
242 vk_object_multialloc(struct vk_device *device,
243 struct vk_multialloc *ma,
244 const VkAllocationCallbacks *alloc,
245 VkObjectType vk_obj_type);
246
247 void *
248 vk_object_multizalloc(struct vk_device *device,
249 struct vk_multialloc *ma,
250 const VkAllocationCallbacks *alloc,
251 VkObjectType vk_obj_type);
252
253 void
254 vk_object_free(struct vk_device *device,
255 const VkAllocationCallbacks *alloc,
256 void *data);
257
258
259 struct vk_private_data_slot {
260 struct vk_object_base base;
261 uint32_t index;
262 };
263 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_private_data_slot, base,
264 VkPrivateDataSlot,
265 VK_OBJECT_TYPE_PRIVATE_DATA_SLOT);
266
267 VkResult
268 vk_private_data_slot_create(struct vk_device *device,
269 const VkPrivateDataSlotCreateInfo* pCreateInfo,
270 const VkAllocationCallbacks* pAllocator,
271 VkPrivateDataSlot* pPrivateDataSlot);
272 void
273 vk_private_data_slot_destroy(struct vk_device *device,
274 VkPrivateDataSlot privateDataSlot,
275 const VkAllocationCallbacks *pAllocator);
276 VkResult
277 vk_object_base_set_private_data(struct vk_device *device,
278 VkObjectType objectType,
279 uint64_t objectHandle,
280 VkPrivateDataSlot privateDataSlot,
281 uint64_t data);
282 void
283 vk_object_base_get_private_data(struct vk_device *device,
284 VkObjectType objectType,
285 uint64_t objectHandle,
286 VkPrivateDataSlot privateDataSlot,
287 uint64_t *pData);
288
289 const char *
290 vk_object_base_name(struct vk_object_base *obj);
291
292 #ifdef __cplusplus
293 }
294 #endif
295
296 #endif /* VK_OBJECT_H */
297