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