• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/macros.h"
31 #include "util/sparse_array.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 struct hash_table;
38 
39 struct vk_device;
40 
41 /** Base struct for all Vulkan objects */
42 struct vk_object_base {
43    VK_LOADER_DATA _loader_data;
44 
45    /** Type of this object
46     *
47     * This is used for runtime type checking when casting to and from Vulkan
48     * handle types since compile-time type checking doesn't always work.
49     */
50    VkObjectType type;
51 
52    /** Pointer to the device in which this object exists, if any
53     *
54     * This is NULL for instances and physical devices but should point to a
55     * valid vk_device for almost everything else.  (There are a few WSI
56     * objects that don't inherit from a device.)
57     */
58    struct vk_device *device;
59 
60    /* True if this object is fully constructed and visible to the client */
61    bool client_visible;
62 
63    /* For VK_EXT_private_data */
64    struct util_sparse_array private_data;
65 
66    /* VK_EXT_debug_utils */
67    char *object_name;
68 };
69 
70 /** Initialize a vk_base_object
71  *
72  * @param[in]  device   The vk_device this object was created from or NULL
73  * @param[out] base     The vk_object_base to initialize
74  * @param[in]  obj_type The VkObjectType of the object being initialized
75  */
76 void vk_object_base_init(struct vk_device *device,
77                          struct vk_object_base *base,
78                          VkObjectType obj_type);
79 
80 /** Tear down a vk_object_base
81  *
82  * @param[out] base     The vk_object_base being torn down
83  */
84 void vk_object_base_finish(struct vk_object_base *base);
85 
86 /** Recycles a vk_object_base
87  *
88  * This should be called when an object is recycled and handed back to the
89  * client as if it were a new object.  When it's called is not important as
90  * long as it's called between when the client thinks the object was destroyed
91  * and when the client sees it again as a supposedly new object.
92  *
93  * @param[inout] base   The vk_object_base being recycled
94  */
95 void vk_object_base_recycle(struct vk_object_base *base);
96 
97 static inline void
vk_object_base_assert_valid(ASSERTED struct vk_object_base * base,ASSERTED VkObjectType obj_type)98 vk_object_base_assert_valid(ASSERTED struct vk_object_base *base,
99                             ASSERTED VkObjectType obj_type)
100 {
101    assert(base == NULL || base->type == obj_type);
102 }
103 
104 static inline struct vk_object_base *
vk_object_base_from_u64_handle(uint64_t handle,VkObjectType obj_type)105 vk_object_base_from_u64_handle(uint64_t handle, VkObjectType obj_type)
106 {
107    struct vk_object_base *base = (struct vk_object_base *)(uintptr_t)handle;
108    vk_object_base_assert_valid(base, obj_type);
109    return base;
110 }
111 
112 /** Define handle cast macros for the given dispatchable handle type
113  *
114  * For a given `driver_struct`, this defines `driver_struct_to_handle()` and
115  * `driver_struct_from_handle()` helpers which provide type-safe (as much as
116  * possible with Vulkan handle types) casts to and from the `driver_struct`
117  * type.  As an added layer of protection, these casts use the provided
118  * `VkObjectType` to assert that the object is of the correct type when
119  * running with a debug build.
120  *
121  * @param __driver_type The name of the driver struct; it is assumed this is
122  *                      the name of a struct type and `struct` will be
123  *                      prepended automatically
124  *
125  * @param __base        The name of the vk_base_object member
126  *
127  * @param __VkType      The Vulkan object type such as VkImage
128  *
129  * @param __VK_TYPE     The VkObjectType corresponding to __VkType, such as
130  *                      VK_OBJECT_TYPE_IMAGE
131  */
132 #define VK_DEFINE_HANDLE_CASTS(__driver_type, __base, __VkType, __VK_TYPE) \
133    static inline struct __driver_type *                                    \
134    __driver_type ## _from_handle(__VkType _handle)                         \
135    {                                                                       \
136       struct vk_object_base *base = (struct vk_object_base *)_handle;      \
137       vk_object_base_assert_valid(base, __VK_TYPE);                        \
138       STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0);          \
139       return (struct __driver_type *) base;                                \
140    }                                                                       \
141                                                                            \
142    static inline __VkType                                                  \
143    __driver_type ## _to_handle(struct __driver_type *_obj)                 \
144    {                                                                       \
145       vk_object_base_assert_valid(&_obj->__base, __VK_TYPE);               \
146       if (_obj != NULL)                                                    \
147          _obj->__base.client_visible = true;                               \
148       return (__VkType) _obj;                                              \
149    }
150 
151 /** Define handle cast macros for the given non-dispatchable handle type
152  *
153  * For a given `driver_struct`, this defines `driver_struct_to_handle()` and
154  * `driver_struct_from_handle()` helpers which provide type-safe (as much as
155  * possible with Vulkan handle types) casts to and from the `driver_struct`
156  * type.  As an added layer of protection, these casts use the provided
157  * `VkObjectType` to assert that the object is of the correct type when
158  * running with a debug build.
159  *
160  * @param __driver_type The name of the driver struct; it is assumed this is
161  *                      the name of a struct type and `struct` will be
162  *                      prepended automatically
163  *
164  * @param __base        The name of the vk_base_object member
165  *
166  * @param __VkType      The Vulkan object type such as VkImage
167  *
168  * @param __VK_TYPE     The VkObjectType corresponding to __VkType, such as
169  *                      VK_OBJECT_TYPE_IMAGE
170  */
171 #define VK_DEFINE_NONDISP_HANDLE_CASTS(__driver_type, __base, __VkType, __VK_TYPE) \
172    UNUSED static inline struct __driver_type *                             \
173    __driver_type ## _from_handle(__VkType _handle)                         \
174    {                                                                       \
175       struct vk_object_base *base =                                        \
176          (struct vk_object_base *)(uintptr_t)_handle;                      \
177       vk_object_base_assert_valid(base, __VK_TYPE);                        \
178       STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0);          \
179       return (struct __driver_type *)base;                                 \
180    }                                                                       \
181                                                                            \
182    UNUSED static inline __VkType                                           \
183    __driver_type ## _to_handle(struct __driver_type *_obj)                 \
184    {                                                                       \
185       vk_object_base_assert_valid(&_obj->__base, __VK_TYPE);               \
186       if (_obj != NULL)                                                    \
187          _obj->__base.client_visible = true;                               \
188       return (__VkType)(uintptr_t) _obj;                                   \
189    }
190 
191 /** Declares a __driver_type pointer which represents __handle
192  *
193  * @param __driver_type The name of the driver struct; it is assumed this is
194  *                      the name of a struct type and `struct` will be
195  *                      prepended automatically
196  *
197  * @param __name        The name of the declared pointer
198  *
199  * @param __handle      The Vulkan object handle with which to initialize
200  *                      `__name`
201  */
202 #define VK_FROM_HANDLE(__driver_type, __name, __handle) \
203    struct __driver_type *__name = __driver_type ## _from_handle(__handle)
204 
205 /* Helpers for vk object (de)allocation and (de)initialization */
206 void *
207 vk_object_alloc(struct vk_device *device,
208                 const VkAllocationCallbacks *alloc,
209                 size_t size,
210                 VkObjectType vk_obj_type);
211 
212 void *
213 vk_object_zalloc(struct vk_device *device,
214                 const VkAllocationCallbacks *alloc,
215                 size_t size,
216                 VkObjectType vk_obj_type);
217 
218 struct vk_multialloc;
219 
220 void *
221 vk_object_multialloc(struct vk_device *device,
222                      struct vk_multialloc *ma,
223                      const VkAllocationCallbacks *alloc,
224                      VkObjectType vk_obj_type);
225 
226 void *
227 vk_object_multizalloc(struct vk_device *device,
228                       struct vk_multialloc *ma,
229                       const VkAllocationCallbacks *alloc,
230                       VkObjectType vk_obj_type);
231 
232 void
233 vk_object_free(struct vk_device *device,
234                const VkAllocationCallbacks *alloc,
235                void *data);
236 
237 
238 struct vk_private_data_slot {
239    struct vk_object_base base;
240    uint32_t index;
241 };
242 VK_DEFINE_NONDISP_HANDLE_CASTS(vk_private_data_slot, base,
243                                VkPrivateDataSlot,
244                                VK_OBJECT_TYPE_PRIVATE_DATA_SLOT);
245 
246 VkResult
247 vk_private_data_slot_create(struct vk_device *device,
248                             const VkPrivateDataSlotCreateInfo* pCreateInfo,
249                             const VkAllocationCallbacks* pAllocator,
250                             VkPrivateDataSlot* pPrivateDataSlot);
251 void
252 vk_private_data_slot_destroy(struct vk_device *device,
253                              VkPrivateDataSlot privateDataSlot,
254                              const VkAllocationCallbacks *pAllocator);
255 VkResult
256 vk_object_base_set_private_data(struct vk_device *device,
257                                 VkObjectType objectType,
258                                 uint64_t objectHandle,
259                                 VkPrivateDataSlot privateDataSlot,
260                                 uint64_t data);
261 void
262 vk_object_base_get_private_data(struct vk_device *device,
263                                 VkObjectType objectType,
264                                 uint64_t objectHandle,
265                                 VkPrivateDataSlot privateDataSlot,
266                                 uint64_t *pData);
267 
268 const char *
269 vk_object_base_name(struct vk_object_base *obj);
270 
271 #ifdef __cplusplus
272 }
273 #endif
274 
275 #endif /* VK_OBJECT_H */
276