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