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 #include "util/bitscan.h"
27 #include "util/macros.h"
28 
29 #ifndef VK_NO_NIR
30 #include "compiler/shader_enums.h"
31 #endif
32 
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "vk_struct_type_cast.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /* common inlines and macros for vulkan drivers */
43 
44 #include <vulkan/vulkan_core.h>
45 
46 struct vk_pnext_iterator {
47    VkBaseOutStructure *pos;
48 #ifndef NDEBUG
49    VkBaseOutStructure *half_pos;
50    unsigned idx;
51 #endif
52    bool done;
53 };
54 
55 static inline struct vk_pnext_iterator
vk_pnext_iterator_init(void * start)56 vk_pnext_iterator_init(void *start)
57 {
58    struct vk_pnext_iterator iter;
59 
60    iter.pos = (VkBaseOutStructure *)start;
61 #ifndef NDEBUG
62    iter.half_pos = (VkBaseOutStructure *)start;
63    iter.idx = 0;
64 #endif
65    iter.done = false;
66 
67    return iter;
68 }
69 
70 static inline struct vk_pnext_iterator
vk_pnext_iterator_init_const(const void * start)71 vk_pnext_iterator_init_const(const void *start)
72 {
73    return vk_pnext_iterator_init((void *)start);
74 }
75 
76 static inline VkBaseOutStructure *
vk_pnext_iterator_next(struct vk_pnext_iterator * iter)77 vk_pnext_iterator_next(struct vk_pnext_iterator *iter)
78 {
79    iter->pos = iter->pos->pNext;
80 
81 #ifndef NDEBUG
82    if (iter->idx++ & 1) {
83       /** This the "tortoise and the hare" algorithm.  We increment
84        * chaser->pNext every other time *iter gets incremented.  Because *iter
85        * is incrementing twice as fast as chaser->pNext, the distance between
86        * them in the list increases by one for each time we get here.  If we
87        * have a loop, eventually, both iterators will be inside the loop and
88        * this distance will be an integer multiple of the loop length, at
89        * which point the two pointers will be equal.
90        */
91       iter->half_pos = iter->half_pos->pNext;
92       if (iter->half_pos == iter->pos)
93          assert(!"Vulkan input pNext chain has a loop!");
94    }
95 #endif
96 
97    return iter->pos;
98 }
99 
100 /* Because the outer loop only executes once, independently of what happens in
101  * the inner loop, breaks and continues should work exactly the same as if
102  * there were only one for loop.
103  */
104 #define vk_foreach_struct(__e, __start) \
105    for (struct vk_pnext_iterator __iter = vk_pnext_iterator_init(__start); \
106         !__iter.done; __iter.done = true) \
107       for (VkBaseOutStructure *__e = __iter.pos; \
108            __e; __e = vk_pnext_iterator_next(&__iter))
109 
110 #define vk_foreach_struct_const(__e, __start) \
111    for (struct vk_pnext_iterator __iter = \
112             vk_pnext_iterator_init_const(__start); \
113         !__iter.done; __iter.done = true) \
114       for (const VkBaseInStructure *__e = (VkBaseInStructure *)__iter.pos; \
115            __e; __e = (VkBaseInStructure *)vk_pnext_iterator_next(&__iter))
116 
117 static inline void
vk_copy_struct_guts(VkBaseOutStructure * dst,VkBaseInStructure * src,size_t struct_size)118 vk_copy_struct_guts(VkBaseOutStructure *dst, VkBaseInStructure *src, size_t struct_size)
119 {
120    STATIC_ASSERT(sizeof(*dst) == sizeof(*src));
121    memcpy(dst + 1, src + 1, struct_size - sizeof(VkBaseOutStructure));
122 }
123 
124 /**
125  * A wrapper for a Vulkan output array. A Vulkan output array is one that
126  * follows the convention of the parameters to
127  * vkGetPhysicalDeviceQueueFamilyProperties().
128  *
129  * Example Usage:
130  *
131  *    VkResult
132  *    vkGetPhysicalDeviceQueueFamilyProperties(
133  *       VkPhysicalDevice           physicalDevice,
134  *       uint32_t*                  pQueueFamilyPropertyCount,
135  *       VkQueueFamilyProperties*   pQueueFamilyProperties)
136  *    {
137  *       VK_OUTARRAY_MAKE_TYPED(VkQueueFamilyProperties, props,
138  *                              pQueueFamilyProperties,
139  *                              pQueueFamilyPropertyCount);
140  *
141  *       vk_outarray_append_typed(VkQueueFamilyProperties, &props, p) {
142  *          p->queueFlags = ...;
143  *          p->queueCount = ...;
144  *       }
145  *
146  *       vk_outarray_append_typed(VkQueueFamilyProperties, &props, p) {
147  *          p->queueFlags = ...;
148  *          p->queueCount = ...;
149  *       }
150  *
151  *       return vk_outarray_status(&props);
152  *    }
153  */
154 struct __vk_outarray {
155    /** May be null. */
156    void *data;
157 
158    /**
159     * Capacity, in number of elements. Capacity is unlimited (UINT32_MAX) if
160     * data is null.
161     */
162    uint32_t cap;
163 
164    /**
165     * Count of elements successfully written to the array. Every write is
166     * considered successful if data is null.
167     */
168    uint32_t *filled_len;
169 
170    /**
171     * Count of elements that would have been written to the array if its
172     * capacity were sufficient. Vulkan functions often return VK_INCOMPLETE
173     * when `*filled_len < wanted_len`.
174     */
175    uint32_t wanted_len;
176 };
177 
178 static inline void
__vk_outarray_init(struct __vk_outarray * a,void * data,uint32_t * restrict len)179 __vk_outarray_init(struct __vk_outarray *a,
180                    void *data, uint32_t *restrict len)
181 {
182    a->data = data;
183    a->cap = *len;
184    a->filled_len = len;
185    *a->filled_len = 0;
186    a->wanted_len = 0;
187 
188    if (a->data == NULL)
189       a->cap = UINT32_MAX;
190 }
191 
192 static inline VkResult
__vk_outarray_status(const struct __vk_outarray * a)193 __vk_outarray_status(const struct __vk_outarray *a)
194 {
195    if (*a->filled_len < a->wanted_len)
196       return VK_INCOMPLETE;
197    else
198       return VK_SUCCESS;
199 }
200 
201 static inline void *
__vk_outarray_next(struct __vk_outarray * a,size_t elem_size)202 __vk_outarray_next(struct __vk_outarray *a, size_t elem_size)
203 {
204    void *p = NULL;
205 
206    a->wanted_len += 1;
207 
208    if (*a->filled_len >= a->cap)
209       return NULL;
210 
211    if (a->data != NULL)
212       p = (uint8_t *)a->data + (*a->filled_len) * elem_size;
213 
214    *a->filled_len += 1;
215 
216    return p;
217 }
218 
219 #define vk_outarray(elem_t) \
220    struct { \
221       struct __vk_outarray base; \
222       elem_t meta[]; \
223    }
224 
225 #define vk_outarray_typeof_elem(a) __typeof__((a)->meta[0])
226 #define vk_outarray_sizeof_elem(a) sizeof((a)->meta[0])
227 
228 #define vk_outarray_init(a, data, len) \
229    __vk_outarray_init(&(a)->base, (data), (len))
230 
231 #define VK_OUTARRAY_MAKE_TYPED(type, name, data, len) \
232    vk_outarray(type) name; \
233    vk_outarray_init(&name, (data), (len))
234 
235 #define vk_outarray_status(a) \
236    __vk_outarray_status(&(a)->base)
237 
238 #define vk_outarray_next(a) \
239    vk_outarray_next_typed(vk_outarray_typeof_elem(a), a)
240 #define vk_outarray_next_typed(type, a) \
241    ((type *) \
242       __vk_outarray_next(&(a)->base, vk_outarray_sizeof_elem(a)))
243 
244 /**
245  * Append to a Vulkan output array.
246  *
247  * This is a block-based macro. For example:
248  *
249  *    vk_outarray_append_typed(T, &a, elem) {
250  *       elem->foo = ...;
251  *       elem->bar = ...;
252  *    }
253  *
254  * The array `a` has type `vk_outarray(elem_t) *`. It is usually declared with
255  * VK_OUTARRAY_MAKE_TYPED(). The variable `elem` is block-scoped and has type
256  * `elem_t *`.
257  *
258  * The macro unconditionally increments the array's `wanted_len`. If the array
259  * is not full, then the macro also increment its `filled_len` and then
260  * executes the block. When the block is executed, `elem` is non-null and
261  * points to the newly appended element.
262  */
263 #define vk_outarray_append_typed(type, a, elem) \
264    for (type *elem = vk_outarray_next_typed(type, a); \
265         elem != NULL; elem = NULL)
266 
267 static inline void *
__vk_find_struct(void * start,VkStructureType sType)268 __vk_find_struct(void *start, VkStructureType sType)
269 {
270    vk_foreach_struct(s, start) {
271       if (s->sType == sType)
272          return s;
273    }
274 
275    return NULL;
276 }
277 
278 #define vk_find_struct(__start, __sType)                                       \
279   (VK_STRUCTURE_TYPE_##__sType##_cast *)__vk_find_struct(                      \
280       (__start), VK_STRUCTURE_TYPE_##__sType)
281 
282 #define vk_find_struct_const(__start, __sType)                                 \
283   (const VK_STRUCTURE_TYPE_##__sType##_cast *)__vk_find_struct(                \
284       (void *)(__start), VK_STRUCTURE_TYPE_##__sType)
285 
286 static inline void
__vk_append_struct(void * start,void * element)287 __vk_append_struct(void *start, void *element)
288 {
289    vk_foreach_struct(s, start) {
290       if (s->pNext)
291          continue;
292 
293       s->pNext = (struct VkBaseOutStructure *) element;
294       break;
295    }
296 }
297 
298 uint32_t vk_get_driver_version(void);
299 
300 uint32_t vk_get_version_override(void);
301 
302 void vk_warn_non_conformant_implementation(const char *driver_name);
303 
304 struct vk_pipeline_cache_header {
305    uint32_t header_size;
306    uint32_t header_version;
307    uint32_t vendor_id;
308    uint32_t device_id;
309    uint8_t  uuid[VK_UUID_SIZE];
310 };
311 
312 #define VK_EXT_OFFSET (1000000000UL)
313 #define VK_ENUM_EXTENSION(__enum) \
314    ((__enum) >= VK_EXT_OFFSET ? ((((__enum) - VK_EXT_OFFSET) / 1000UL) + 1) : 0)
315 #define VK_ENUM_OFFSET(__enum) \
316    ((__enum) >= VK_EXT_OFFSET ? ((__enum) % 1000) : (__enum))
317 
318 #define typed_memcpy(dest, src, count) do { \
319    STATIC_ASSERT(sizeof(*(src)) == sizeof(*(dest))); \
320    memcpy((dest), (src), (count) * sizeof(*(src))); \
321 } while (0)
322 
323 #ifndef VK_NO_NIR
324 static inline gl_shader_stage
vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)325 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
326 {
327    assert(util_bitcount((uint32_t) vk_stage) == 1);
328    return (gl_shader_stage) (ffs((uint32_t) vk_stage) - 1);
329 }
330 
331 static inline VkShaderStageFlagBits
mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)332 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
333 {
334    return (VkShaderStageFlagBits) (1 << ((uint32_t) mesa_stage));
335 }
336 #endif
337 
338 /* iterate over a sequence of indexed multidraws for VK_EXT_multi_draw extension */
339 /* 'i' must be explicitly declared */
340 #define vk_foreach_multi_draw_indexed(_draw, _i, _pDrawInfo, _num_draws, _stride) \
341    for (const VkMultiDrawIndexedInfoEXT *_draw = (const VkMultiDrawIndexedInfoEXT*)(_pDrawInfo); \
342         (_i) < (_num_draws); \
343         (_i)++, (_draw) = (const VkMultiDrawIndexedInfoEXT*)((const uint8_t*)(_draw) + (_stride)))
344 
345 /* iterate over a sequence of multidraws for VK_EXT_multi_draw extension */
346 /* 'i' must be explicitly declared */
347 #define vk_foreach_multi_draw(_draw, _i, _pDrawInfo, _num_draws, _stride) \
348    for (const VkMultiDrawInfoEXT *_draw = (const VkMultiDrawInfoEXT*)(_pDrawInfo); \
349         (_i) < (_num_draws); \
350         (_i)++, (_draw) = (const VkMultiDrawInfoEXT*)((const uint8_t*)(_draw) + (_stride)))
351 
352 #ifndef VK_NO_NIR
353 struct nir_spirv_specialization;
354 
355 struct nir_spirv_specialization*
356 vk_spec_info_to_nir_spirv(const VkSpecializationInfo *spec_info,
357                           uint32_t *out_num_spec_entries);
358 #endif
359 
360 #define STACK_ARRAY_SIZE 8
361 
362 #ifdef __cplusplus
363 #define STACK_ARRAY_ZERO_INIT {}
364 #else
365 #define STACK_ARRAY_ZERO_INIT {0}
366 #endif
367 
368 #define STACK_ARRAY(type, name, size) \
369    type _stack_##name[STACK_ARRAY_SIZE] = STACK_ARRAY_ZERO_INIT; \
370    type *const name = \
371      ((size) <= STACK_ARRAY_SIZE ? _stack_##name : (type *)malloc((size) * sizeof(type)))
372 
373 #define STACK_ARRAY_FINISH(name) \
374    if (name != _stack_##name) free(name)
375 
376 static inline uint8_t
vk_index_type_to_bytes(enum VkIndexType type)377 vk_index_type_to_bytes(enum VkIndexType type)
378 {
379    switch (type) {
380    case VK_INDEX_TYPE_NONE_KHR:  return 0;
381    case VK_INDEX_TYPE_UINT8_EXT: return 1;
382    case VK_INDEX_TYPE_UINT16:    return 2;
383    case VK_INDEX_TYPE_UINT32:    return 4;
384    default:                      unreachable("Invalid index type");
385    }
386 }
387 
388 #ifdef __cplusplus
389 }
390 #endif
391 
392 #endif /* VK_UTIL_H */
393