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 /** Pointer to the device in which this object exists, if any
54 *
55 * This is NULL for instances and physical devices but should point to a
56 * valid vk_device for almost everything else. (There are a few WSI
57 * objects that don't inherit from a device.)
58 */
59 struct vk_device *device;
60
61 /** Pointer to the instance in which this object exists
62 *
63 * This is NULL for device level objects as it's main purpose is to make
64 * the instance allocator reachable for freeing data owned by instance
65 * level objects.
66 */
67 struct vk_instance *instance;
68
69 /* True if this object is fully constructed and visible to the client */
70 bool client_visible;
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 vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \
165 if (_obj != NULL) \
166 _obj->__base.client_visible = true; \
167 return (__VkType) _obj; \
168 }
169
170 /** Define handle cast macros for the given non-dispatchable handle type
171 *
172 * For a given `driver_struct`, this defines `driver_struct_to_handle()` and
173 * `driver_struct_from_handle()` helpers which provide type-safe (as much as
174 * possible with Vulkan handle types) casts to and from the `driver_struct`
175 * type. As an added layer of protection, these casts use the provided
176 * `VkObjectType` to assert that the object is of the correct type when
177 * running with a debug build.
178 *
179 * :param __driver_type: The name of the driver struct; it is assumed this is
180 * the name of a struct type and ``struct`` will be
181 * prepended automatically
182 *
183 * :param __base: The name of the vk_base_object member
184 *
185 * :param __VkType: The Vulkan object type such as VkImage
186 *
187 * :param __VK_TYPE: The VkObjectType corresponding to __VkType, such as
188 * VK_OBJECT_TYPE_IMAGE
189 */
190 #define VK_DEFINE_NONDISP_HANDLE_CASTS(__driver_type, __base, __VkType, __VK_TYPE) \
191 UNUSED static inline struct __driver_type * \
192 __driver_type ## _from_handle(__VkType _handle) \
193 { \
194 struct vk_object_base *base = \
195 (struct vk_object_base *)(uintptr_t)_handle; \
196 vk_object_base_assert_valid(base, __VK_TYPE); \
197 STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \
198 return (struct __driver_type *)base; \
199 } \
200 \
201 UNUSED static inline __VkType \
202 __driver_type ## _to_handle(struct __driver_type *_obj) \
203 { \
204 vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \
205 if (_obj != NULL) \
206 _obj->__base.client_visible = true; \
207 return (__VkType)(uintptr_t) _obj; \
208 }
209
210 /** Declares a __driver_type pointer which represents __handle
211 *
212 * :param __driver_type: The name of the driver struct; it is assumed this is
213 * the name of a struct type and ``struct`` will be
214 * prepended automatically
215 *
216 * :param __name: The name of the declared pointer
217 *
218 * :param __handle: The Vulkan object handle with which to initialize
219 * `__name`
220 */
221 #define VK_FROM_HANDLE(__driver_type, __name, __handle) \
222 struct __driver_type *__name = __driver_type ## _from_handle(__handle)
223
224 /* Helpers for vk object (de)allocation and (de)initialization */
225 void *
226 vk_object_alloc(struct vk_device *device,
227 const VkAllocationCallbacks *alloc,
228 size_t size,
229 VkObjectType vk_obj_type);
230
231 void *
232 vk_object_zalloc(struct vk_device *device,
233 const VkAllocationCallbacks *alloc,
234 size_t size,
235 VkObjectType vk_obj_type);
236
237 struct vk_multialloc;
238
239 void *
240 vk_object_multialloc(struct vk_device *device,
241 struct vk_multialloc *ma,
242 const VkAllocationCallbacks *alloc,
243 VkObjectType vk_obj_type);
244
245 void *
246 vk_object_multizalloc(struct vk_device *device,
247 struct vk_multialloc *ma,
248 const VkAllocationCallbacks *alloc,
249 VkObjectType vk_obj_type);
250
251 void
252 vk_object_free(struct vk_device *device,
253 const VkAllocationCallbacks *alloc,
254 void *data);
255
256
257 struct vk_private_data_slot {
258 struct vk_object_base base;
259 uint32_t index;
260 };
261 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_private_data_slot, base,
262 VkPrivateDataSlot,
263 VK_OBJECT_TYPE_PRIVATE_DATA_SLOT);
264
265 VkResult
266 vk_private_data_slot_create(struct vk_device *device,
267 const VkPrivateDataSlotCreateInfo* pCreateInfo,
268 const VkAllocationCallbacks* pAllocator,
269 VkPrivateDataSlot* pPrivateDataSlot);
270 void
271 vk_private_data_slot_destroy(struct vk_device *device,
272 VkPrivateDataSlot privateDataSlot,
273 const VkAllocationCallbacks *pAllocator);
274 VkResult
275 vk_object_base_set_private_data(struct vk_device *device,
276 VkObjectType objectType,
277 uint64_t objectHandle,
278 VkPrivateDataSlot privateDataSlot,
279 uint64_t data);
280 void
281 vk_object_base_get_private_data(struct vk_device *device,
282 VkObjectType objectType,
283 uint64_t objectHandle,
284 VkPrivateDataSlot privateDataSlot,
285 uint64_t *pData);
286
287 const char *
288 vk_object_base_name(struct vk_object_base *obj);
289
290 #ifdef __cplusplus
291 }
292 #endif
293
294 #endif /* VK_OBJECT_H */
295