~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/vulkan/util/vk_util.h

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#ifdef __cplusplus
33
 
extern "C" {
34
 
#endif
35
 
 
36
 
/* common inlines and macros for vulkan drivers */
37
 
 
38
 
#include <vulkan/vulkan.h>
39
 
 
40
 
#define vk_foreach_struct(__iter, __start) \
41
 
   for (struct VkBaseOutStructure *__iter = (struct VkBaseOutStructure *)(__start); \
42
 
        __iter; __iter = __iter->pNext)
43
 
 
44
 
#define vk_foreach_struct_const(__iter, __start) \
45
 
   for (const struct VkBaseInStructure *__iter = (const struct VkBaseInStructure *)(__start); \
46
 
        __iter; __iter = __iter->pNext)
47
 
 
48
 
/**
49
 
 * A wrapper for a Vulkan output array. A Vulkan output array is one that
50
 
 * follows the convention of the parameters to
51
 
 * vkGetPhysicalDeviceQueueFamilyProperties().
52
 
 *
53
 
 * Example Usage:
54
 
 *
55
 
 *    VkResult
56
 
 *    vkGetPhysicalDeviceQueueFamilyProperties(
57
 
 *       VkPhysicalDevice           physicalDevice,
58
 
 *       uint32_t*                  pQueueFamilyPropertyCount,
59
 
 *       VkQueueFamilyProperties*   pQueueFamilyProperties)
60
 
 *    {
61
 
 *       VK_OUTARRAY_MAKE_TYPED(VkQueueFamilyProperties, props,
62
 
 *                              pQueueFamilyProperties,
63
 
 *                              pQueueFamilyPropertyCount);
64
 
 *
65
 
 *       vk_outarray_append_typed(VkQueueFamilyProperties, &props, p) {
66
 
 *          p->queueFlags = ...;
67
 
 *          p->queueCount = ...;
68
 
 *       }
69
 
 *
70
 
 *       vk_outarray_append_typed(VkQueueFamilyProperties, &props, p) {
71
 
 *          p->queueFlags = ...;
72
 
 *          p->queueCount = ...;
73
 
 *       }
74
 
 *
75
 
 *       return vk_outarray_status(&props);
76
 
 *    }
77
 
 */
78
 
struct __vk_outarray {
79
 
   /** May be null. */
80
 
   void *data;
81
 
 
82
 
   /**
83
 
    * Capacity, in number of elements. Capacity is unlimited (UINT32_MAX) if
84
 
    * data is null.
85
 
    */
86
 
   uint32_t cap;
87
 
 
88
 
   /**
89
 
    * Count of elements successfully written to the array. Every write is
90
 
    * considered successful if data is null.
91
 
    */
92
 
   uint32_t *filled_len;
93
 
 
94
 
   /**
95
 
    * Count of elements that would have been written to the array if its
96
 
    * capacity were sufficient. Vulkan functions often return VK_INCOMPLETE
97
 
    * when `*filled_len < wanted_len`.
98
 
    */
99
 
   uint32_t wanted_len;
100
 
};
101
 
 
102
 
static inline void
103
 
__vk_outarray_init(struct __vk_outarray *a,
104
 
                   void *data, uint32_t *restrict len)
105
 
{
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)
113
 
      a->cap = UINT32_MAX;
114
 
}
115
 
 
116
 
static inline VkResult
117
 
__vk_outarray_status(const struct __vk_outarray *a)
118
 
{
119
 
   if (*a->filled_len < a->wanted_len)
120
 
      return VK_INCOMPLETE;
121
 
   else
122
 
      return VK_SUCCESS;
123
 
}
124
 
 
125
 
static inline void *
126
 
__vk_outarray_next(struct __vk_outarray *a, size_t elem_size)
127
 
{
128
 
   void *p = NULL;
129
 
 
130
 
   a->wanted_len += 1;
131
 
 
132
 
   if (*a->filled_len >= a->cap)
133
 
      return NULL;
134
 
 
135
 
   if (a->data != NULL)
136
 
      p = (uint8_t *)a->data + (*a->filled_len) * elem_size;
137
 
 
138
 
   *a->filled_len += 1;
139
 
 
140
 
   return p;
141
 
}
142
 
 
143
 
#define vk_outarray(elem_t) \
144
 
   struct { \
145
 
      struct __vk_outarray base; \
146
 
      elem_t meta[]; \
147
 
   }
148
 
 
149
 
#define vk_outarray_typeof_elem(a) __typeof__((a)->meta[0])
150
 
#define vk_outarray_sizeof_elem(a) sizeof((a)->meta[0])
151
 
 
152
 
#define vk_outarray_init(a, data, len) \
153
 
   __vk_outarray_init(&(a)->base, (data), (len))
154
 
 
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_typed(T, &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_TYPED(). 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_typed(type, a, elem) \
188
 
   for (type *elem = vk_outarray_next_typed(type, a); \
189
 
        elem != NULL; elem = NULL)
190
 
 
191
 
static inline void *
192
 
__vk_find_struct(void *start, VkStructureType sType)
193
 
{
194
 
   vk_foreach_struct(s, start) {
195
 
      if (s->sType == sType)
196
 
         return s;
197
 
   }
198
 
 
199
 
   return NULL;
200
 
}
201
 
 
202
 
#define vk_find_struct(__start, __sType) \
203
 
   __vk_find_struct((__start), VK_STRUCTURE_TYPE_##__sType)
204
 
 
205
 
#define vk_find_struct_const(__start, __sType) \
206
 
   (const void *)__vk_find_struct((void *)(__start), VK_STRUCTURE_TYPE_##__sType)
207
 
 
208
 
static inline void
209
 
__vk_append_struct(void *start, void *element)
210
 
{
211
 
   vk_foreach_struct(s, start) {
212
 
      if (s->pNext)
213
 
         continue;
214
 
 
215
 
      s->pNext = (struct VkBaseOutStructure *) element;
216
 
      break;
217
 
   }
218
 
}
219
 
 
220
 
uint32_t vk_get_driver_version(void);
221
 
 
222
 
uint32_t vk_get_version_override(void);
223
 
 
224
 
void vk_warn_non_conformant_implementation(const char *driver_name);
225
 
 
226
 
struct vk_pipeline_cache_header {
227
 
   uint32_t header_size;
228
 
   uint32_t header_version;
229
 
   uint32_t vendor_id;
230
 
   uint32_t device_id;
231
 
   uint8_t  uuid[VK_UUID_SIZE];
232
 
};
233
 
 
234
 
#define VK_EXT_OFFSET (1000000000UL)
235
 
#define VK_ENUM_EXTENSION(__enum) \
236
 
   ((__enum) >= VK_EXT_OFFSET ? ((((__enum) - VK_EXT_OFFSET) / 1000UL) + 1) : 0)
237
 
#define VK_ENUM_OFFSET(__enum) \
238
 
   ((__enum) >= VK_EXT_OFFSET ? ((__enum) % 1000) : (__enum))
239
 
 
240
 
#define typed_memcpy(dest, src, count) do { \
241
 
   STATIC_ASSERT(sizeof(*(src)) == sizeof(*(dest))); \
242
 
   memcpy((dest), (src), (count) * sizeof(*(src))); \
243
 
} while (0)
244
 
 
245
 
static inline gl_shader_stage
246
 
vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
247
 
{
248
 
   assert(util_bitcount((uint32_t) vk_stage) == 1);
249
 
   return (gl_shader_stage) (ffs((uint32_t) vk_stage) - 1);
250
 
}
251
 
 
252
 
static inline VkShaderStageFlagBits
253
 
mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
254
 
{
255
 
   return (VkShaderStageFlagBits) (1 << ((uint32_t) mesa_stage));
256
 
}
257
 
 
258
 
/* iterate over a sequence of indexed multidraws for VK_EXT_multi_draw extension */
259
 
/* 'i' must be explicitly declared */
260
 
#define vk_foreach_multi_draw_indexed(_draw, _i, _pDrawInfo, _num_draws, _stride) \
261
 
   for (const VkMultiDrawIndexedInfoEXT *_draw = (const void*)(_pDrawInfo); \
262
 
        (_i) < (_num_draws); \
263
 
        (_i)++, (_draw) = (const VkMultiDrawIndexedInfoEXT*)((const uint8_t*)(_draw) + (_stride)))
264
 
 
265
 
/* iterate over a sequence of multidraws for VK_EXT_multi_draw extension */
266
 
/* 'i' must be explicitly declared */
267
 
#define vk_foreach_multi_draw(_draw, _i, _pDrawInfo, _num_draws, _stride) \
268
 
   for (const VkMultiDrawInfoEXT *_draw = (const void*)(_pDrawInfo); \
269
 
        (_i) < (_num_draws); \
270
 
        (_i)++, (_draw) = (const VkMultiDrawInfoEXT*)((const uint8_t*)(_draw) + (_stride)))
271
 
 
272
 
 
273
 
struct nir_spirv_specialization;
274
 
 
275
 
struct nir_spirv_specialization*
276
 
vk_spec_info_to_nir_spirv(const VkSpecializationInfo *spec_info,
277
 
                          uint32_t *out_num_spec_entries);
278
 
 
279
 
#define STACK_ARRAY_SIZE 8
280
 
 
281
 
#ifdef __cplusplus
282
 
#define STACK_ARRAY_ZERO_INIT {}
283
 
#else
284
 
#define STACK_ARRAY_ZERO_INIT {0}
285
 
#endif
286
 
 
287
 
#define STACK_ARRAY(type, name, size) \
288
 
   type _stack_##name[STACK_ARRAY_SIZE] = STACK_ARRAY_ZERO_INIT; \
289
 
   type *const name = \
290
 
     ((size) <= STACK_ARRAY_SIZE ? _stack_##name : (type *)malloc((size) * sizeof(type)))
291
 
 
292
 
#define STACK_ARRAY_FINISH(name) \
293
 
   if (name != _stack_##name) free(name)
294
 
 
295
 
#ifdef __cplusplus
296
 
}
297
 
#endif
298
 
 
299
 
#endif /* VK_UTIL_H */