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