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 <stdlib.h>
29 #include <vulkan/vulkan.h>
30 
31 #include "vk_struct_id.h"
32 
33 namespace {  // anonymous
34 
35 struct vk_struct_common {
36     VkStructureType sType;
37     struct vk_struct_common* pNext;
38 };
39 
40 struct vk_struct_chain_iterator {
41     vk_struct_common* value;
42 };
43 
44 #define vk_foreach_struct(__iter, __start)                                              \
45     for (struct vk_struct_common* __iter = (struct vk_struct_common*)(__start); __iter; \
46          __iter = __iter->pNext)
47 
48 #define vk_foreach_struct_const(__iter, __start)                                            \
49     for (const struct vk_struct_common* __iter = (const struct vk_struct_common*)(__start); \
50          __iter; __iter = __iter->pNext)
51 
52 /**
53  * A wrapper for a Vulkan output array. A Vulkan output array is one that
54  * follows the convention of the parameters to
55  * vkGetPhysicalDeviceQueueFamilyProperties().
56  *
57  * Example Usage:
58  *
59  *    VkResult
60  *    vkGetPhysicalDeviceQueueFamilyProperties(
61  *       VkPhysicalDevice           physicalDevice,
62  *       uint32_t*                  pQueueFamilyPropertyCount,
63  *       VkQueueFamilyProperties*   pQueueFamilyProperties)
64  *    {
65  *       VK_OUTARRAY_MAKE(props, pQueueFamilyProperties,
66  *                         pQueueFamilyPropertyCount);
67  *
68  *       vk_outarray_append(&props, p) {
69  *          p->queueFlags = ...;
70  *          p->queueCount = ...;
71  *       }
72  *
73  *       vk_outarray_append(&props, p) {
74  *          p->queueFlags = ...;
75  *          p->queueCount = ...;
76  *       }
77  *
78  *       return vk_outarray_status(&props);
79  *    }
80  */
81 struct __vk_outarray {
82     /** May be null. */
83     void* data;
84 
85     /**
86      * Capacity, in number of elements. Capacity is unlimited (UINT32_MAX) if
87      * data is null.
88      */
89     uint32_t cap;
90 
91     /**
92      * Count of elements successfully written to the array. Every write is
93      * considered successful if data is null.
94      */
95     uint32_t* filled_len;
96 
97     /**
98      * Count of elements that would have been written to the array if its
99      * capacity were sufficient. Vulkan functions often return VK_INCOMPLETE
100      * when `*filled_len < wanted_len`.
101      */
102     uint32_t wanted_len;
103 };
104 
__vk_outarray_init(struct __vk_outarray * a,void * data,uint32_t * len)105 static inline void __vk_outarray_init(struct __vk_outarray* a, void* data, uint32_t* len) {
106     a->data = data;
107     a->cap = *len;
108     a->filled_len = len;
109     *a->filled_len = 0;
110     a->wanted_len = 0;
111 
112     if (a->data == NULL) a->cap = UINT32_MAX;
113 }
114 
__vk_outarray_status(const struct __vk_outarray * a)115 static inline VkResult __vk_outarray_status(const struct __vk_outarray* a) {
116     if (*a->filled_len < a->wanted_len)
117         return VK_INCOMPLETE;
118     else
119         return VK_SUCCESS;
120 }
121 
__vk_outarray_next(struct __vk_outarray * a,size_t elem_size)122 static inline void* __vk_outarray_next(struct __vk_outarray* a, size_t elem_size) {
123     void* p = NULL;
124 
125     a->wanted_len += 1;
126 
127     if (*a->filled_len >= a->cap) return NULL;
128 
129     if (a->data != NULL) 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) __vk_outarray_init(&(a)->base, (data), (len))
146 
147 #define VK_OUTARRAY_MAKE(name, data, len)    \
148     vk_outarray(__typeof__((data)[0])) name; \
149     vk_outarray_init(&name, (data), (len))
150 
151 #define VK_OUTARRAY_MAKE_TYPED(type, name, data, len) \
152     vk_outarray(type) name;                           \
153     vk_outarray_init(&name, (data), (len))
154 
155 #define vk_outarray_status(a) __vk_outarray_status(&(a)->base)
156 
157 #define vk_outarray_next(a) vk_outarray_next_typed(vk_outarray_typeof_elem(a), a)
158 #define vk_outarray_next_typed(type, a) \
159     ((type*)__vk_outarray_next(&(a)->base, vk_outarray_sizeof_elem(a)))
160 
161 /**
162  * Append to a Vulkan output array.
163  *
164  * This is a block-based macro. For example:
165  *
166  *    vk_outarray_append(&a, elem) {
167  *       elem->foo = ...;
168  *       elem->bar = ...;
169  *    }
170  *
171  * The array `a` has type `vk_outarray(elem_t) *`. It is usually declared with
172  * VK_OUTARRAY_MAKE(). The variable `elem` is block-scoped and has type
173  * `elem_t *`.
174  *
175  * The macro unconditionally increments the array's `wanted_len`. If the array
176  * is not full, then the macro also increment its `filled_len` and then
177  * executes the block. When the block is executed, `elem` is non-null and
178  * points to the newly appended element.
179  */
180 #define vk_outarray_append(a, elem) \
181     for (vk_outarray_typeof_elem(a)* elem = vk_outarray_next(a); elem != NULL; elem = NULL)
182 
183 #define vk_outarray_append_typed(type, a, elem) \
184     for (type* elem = vk_outarray_next_typed(type, a); elem != NULL; elem = NULL)
185 
__vk_find_struct(void * start,VkStructureType sType)186 static inline void* __vk_find_struct(void* start, VkStructureType sType) {
187     vk_foreach_struct(s, start) {
188         if (s->sType == sType) return s;
189     }
190 
191     return NULL;
192 }
193 
194 template <class T, class H>
vk_find_struct(H * head)195 T* vk_find_struct(H* head) {
196     (void)vk_get_vk_struct_id<H>::id;
197     return static_cast<T*>(__vk_find_struct(static_cast<void*>(head), vk_get_vk_struct_id<T>::id));
198 }
199 
200 template <class T, class H>
vk_find_struct(const H * head)201 const T* vk_find_struct(const H* head) {
202     (void)vk_get_vk_struct_id<H>::id;
203     return static_cast<const T*>(__vk_find_struct(const_cast<void*>(static_cast<const void*>(head)),
204                                                   vk_get_vk_struct_id<T>::id));
205 }
206 
207 uint32_t vk_get_driver_version(void);
208 
209 uint32_t vk_get_version_override(void);
210 
211 #define VK_EXT_OFFSET (1000000000UL)
212 #define VK_ENUM_EXTENSION(__enum) \
213     ((__enum) >= VK_EXT_OFFSET ? ((((__enum)-VK_EXT_OFFSET) / 1000UL) + 1) : 0)
214 #define VK_ENUM_OFFSET(__enum) ((__enum) >= VK_EXT_OFFSET ? ((__enum) % 1000) : (__enum))
215 
216 template <class T>
vk_make_orphan_copy(const T & vk_struct)217 T vk_make_orphan_copy(const T& vk_struct) {
218     T copy = vk_struct;
219     copy.pNext = NULL;
220     return copy;
221 }
222 
223 template <class T>
vk_make_chain_iterator(T * vk_struct)224 vk_struct_chain_iterator vk_make_chain_iterator(T* vk_struct) {
225     (void)vk_get_vk_struct_id<T>::id;
226     vk_struct_chain_iterator result = {reinterpret_cast<vk_struct_common*>(vk_struct)};
227     return result;
228 }
229 
230 template <class T>
vk_append_struct(vk_struct_chain_iterator * i,T * vk_struct)231 void vk_append_struct(vk_struct_chain_iterator* i, T* vk_struct) {
232     (void)vk_get_vk_struct_id<T>::id;
233 
234     vk_struct_common* p = i->value;
235     if (p->pNext) {
236         ::abort();
237     }
238 
239     p->pNext = reinterpret_cast<vk_struct_common*>(vk_struct);
240     vk_struct->pNext = NULL;
241 
242     *i = vk_make_chain_iterator(vk_struct);
243 }
244 
vk_descriptor_type_has_image_view(VkDescriptorType type)245 bool vk_descriptor_type_has_image_view(VkDescriptorType type) {
246     switch (type) {
247         case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
248         case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
249         case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
250         case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
251             return true;
252         default:
253             return false;
254     }
255 }
256 
vk_descriptor_type_has_descriptor_buffer(VkDescriptorType type)257 bool vk_descriptor_type_has_descriptor_buffer(VkDescriptorType type) {
258     switch (type) {
259         case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
260         case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
261         case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
262         case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
263             return true;
264         default:
265             return false;
266     }
267 }
268 
vk_descriptor_type_has_texel_buffer(VkDescriptorType type)269 bool vk_descriptor_type_has_texel_buffer(VkDescriptorType type) {
270     switch (type) {
271         case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
272         case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
273             return true;
274         default:
275             return false;
276     }
277 }
278 
279 }  // namespace
280 
281 #endif /* VK_UTIL_H */
282