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