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