1 /*
2  * Copyright © 2017 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_UTIL_H
24 #define VK_UTIL_H
25 
26 /* common inlines and macros for vulkan drivers */
27 
28 #include <vulkan/vulkan.h>
29 #include <stdlib.h>
30 #include "vk_struct_id.h"
31 
32 struct vk_struct_common {
33     VkStructureType sType;
34     struct vk_struct_common *pNext;
35 };
36 
37 struct vk_struct_chain_iterator {
38     vk_struct_common* value;
39 };
40 
41 #define vk_foreach_struct(__iter, __start) \
42    for (struct vk_struct_common *__iter = (struct vk_struct_common *)(__start); \
43         __iter; __iter = __iter->pNext)
44 
45 #define vk_foreach_struct_const(__iter, __start) \
46    for (const struct vk_struct_common *__iter = (const struct vk_struct_common *)(__start); \
47         __iter; __iter = __iter->pNext)
48 
49 /**
50  * A wrapper for a Vulkan output array. A Vulkan output array is one that
51  * follows the convention of the parameters to
52  * vkGetPhysicalDeviceQueueFamilyProperties().
53  *
54  * Example Usage:
55  *
56  *    VkResult
57  *    vkGetPhysicalDeviceQueueFamilyProperties(
58  *       VkPhysicalDevice           physicalDevice,
59  *       uint32_t*                  pQueueFamilyPropertyCount,
60  *       VkQueueFamilyProperties*   pQueueFamilyProperties)
61  *    {
62  *       VK_OUTARRAY_MAKE(props, pQueueFamilyProperties,
63  *                         pQueueFamilyPropertyCount);
64  *
65  *       vk_outarray_append(&props, p) {
66  *          p->queueFlags = ...;
67  *          p->queueCount = ...;
68  *       }
69  *
70  *       vk_outarray_append(&props, p) {
71  *          p->queueFlags = ...;
72  *          p->queueCount = ...;
73  *       }
74  *
75  *       return vk_outarray_status(&props);
76  *    }
77  */
78 struct __vk_outarray {
79    /** May be null. */
80    void *data;
81 
82    /**
83     * Capacity, in number of elements. Capacity is unlimited (UINT32_MAX) if
84     * data is null.
85     */
86    uint32_t cap;
87 
88    /**
89     * Count of elements successfully written to the array. Every write is
90     * considered successful if data is null.
91     */
92    uint32_t *filled_len;
93 
94    /**
95     * Count of elements that would have been written to the array if its
96     * capacity were sufficient. Vulkan functions often return VK_INCOMPLETE
97     * when `*filled_len < wanted_len`.
98     */
99    uint32_t wanted_len;
100 };
101 
102 static inline void
__vk_outarray_init(struct __vk_outarray * a,void * data,uint32_t * len)103 __vk_outarray_init(struct __vk_outarray *a,
104                    void *data, uint32_t * len)
105 {
106    a->data = data;
107    a->cap = *len;
108    a->filled_len = len;
109    *a->filled_len = 0;
110    a->wanted_len = 0;
111 
112    if (a->data == NULL)
113       a->cap = UINT32_MAX;
114 }
115 
116 static inline VkResult
__vk_outarray_status(const struct __vk_outarray * a)117 __vk_outarray_status(const struct __vk_outarray *a)
118 {
119    if (*a->filled_len < a->wanted_len)
120       return VK_INCOMPLETE;
121    else
122       return VK_SUCCESS;
123 }
124 
125 static inline void *
__vk_outarray_next(struct __vk_outarray * a,size_t elem_size)126 __vk_outarray_next(struct __vk_outarray *a, size_t elem_size)
127 {
128    void *p = NULL;
129 
130    a->wanted_len += 1;
131 
132    if (*a->filled_len >= a->cap)
133       return NULL;
134 
135    if (a->data != NULL)
136       p = ((uint8_t*)a->data) + (*a->filled_len) * elem_size;
137 
138    *a->filled_len += 1;
139 
140    return p;
141 }
142 
143 #define vk_outarray(elem_t) \
144    struct { \
145       struct __vk_outarray base; \
146       elem_t meta[]; \
147    }
148 
149 #define vk_outarray_typeof_elem(a) __typeof__((a)->meta[0])
150 #define vk_outarray_sizeof_elem(a) sizeof((a)->meta[0])
151 
152 #define vk_outarray_init(a, data, len) \
153    __vk_outarray_init(&(a)->base, (data), (len))
154 
155 #define VK_OUTARRAY_MAKE(name, data, len) \
156    vk_outarray(__typeof__((data)[0])) name; \
157    vk_outarray_init(&name, (data), (len))
158 
159 #define vk_outarray_status(a) \
160    __vk_outarray_status(&(a)->base)
161 
162 #define vk_outarray_next(a) \
163    ((vk_outarray_typeof_elem(a) *) \
164       __vk_outarray_next(&(a)->base, vk_outarray_sizeof_elem(a)))
165 
166 /**
167  * Append to a Vulkan output array.
168  *
169  * This is a block-based macro. For example:
170  *
171  *    vk_outarray_append(&a, elem) {
172  *       elem->foo = ...;
173  *       elem->bar = ...;
174  *    }
175  *
176  * The array `a` has type `vk_outarray(elem_t) *`. It is usually declared with
177  * VK_OUTARRAY_MAKE(). The variable `elem` is block-scoped and has type
178  * `elem_t *`.
179  *
180  * The macro unconditionally increments the array's `wanted_len`. If the array
181  * is not full, then the macro also increment its `filled_len` and then
182  * executes the block. When the block is executed, `elem` is non-null and
183  * points to the newly appended element.
184  */
185 #define vk_outarray_append(a, elem) \
186    for (vk_outarray_typeof_elem(a) *elem = vk_outarray_next(a); \
187         elem != NULL; elem = NULL)
188 
189 static inline void *
__vk_find_struct(void * start,VkStructureType sType)190 __vk_find_struct(void *start, VkStructureType sType)
191 {
192    vk_foreach_struct(s, start) {
193       if (s->sType == sType)
194          return s;
195    }
196 
197    return NULL;
198 }
199 
vk_find_struct(H * head)200 template <class T, class H> T* vk_find_struct(H* head)
201 {
202     vk_get_vk_struct_id<H>::id;
203     return static_cast<T*>(__vk_find_struct(static_cast<void*>(head), vk_get_vk_struct_id<T>::id));
204 }
205 
vk_find_struct(const H * head)206 template <class T, class H> const T* vk_find_struct(const H* head)
207 {
208     vk_get_vk_struct_id<H>::id;
209     return static_cast<const T*>(__vk_find_struct(const_cast<void*>(static_cast<const void*>(head)),
210                                  vk_get_vk_struct_id<T>::id));
211 }
212 
213 uint32_t vk_get_driver_version(void);
214 
215 uint32_t vk_get_version_override(void);
216 
217 #define VK_EXT_OFFSET (1000000000UL)
218 #define VK_ENUM_EXTENSION(__enum) \
219    ((__enum) >= VK_EXT_OFFSET ? ((((__enum) - VK_EXT_OFFSET) / 1000UL) + 1) : 0)
220 #define VK_ENUM_OFFSET(__enum) \
221    ((__enum) >= VK_EXT_OFFSET ? ((__enum) % 1000) : (__enum))
222 
vk_make_orphan_copy(const T & vk_struct)223 template <class T> T vk_make_orphan_copy(const T& vk_struct) {
224     T copy = vk_struct;
225     copy.pNext = NULL;
226     return copy;
227 }
228 
vk_make_chain_iterator(T * vk_struct)229 template <class T> vk_struct_chain_iterator vk_make_chain_iterator(T* vk_struct)
230 {
231     vk_get_vk_struct_id<T>::id;
232     vk_struct_chain_iterator result = { reinterpret_cast<vk_struct_common*>(vk_struct) };
233     return result;
234 }
235 
vk_append_struct(vk_struct_chain_iterator * i,T * vk_struct)236 template <class T> void vk_append_struct(vk_struct_chain_iterator* i, T* vk_struct)
237 {
238     vk_get_vk_struct_id<T>::id;
239 
240     vk_struct_common* p = i->value;
241     if (p->pNext) {
242         ::abort();
243     }
244 
245     p->pNext = reinterpret_cast<vk_struct_common *>(vk_struct);
246     vk_struct->pNext = NULL;
247 
248     *i = vk_make_chain_iterator(vk_struct);
249 }
250 
251 #endif /* VK_UTIL_H */
252