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