forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk_layer_utils.h
579 lines (511 loc) · 19.8 KB
/
vk_layer_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/* Copyright (c) 2015-2017, 2019-2022 The Khronos Group Inc.
* Copyright (c) 2015-2017, 2019-2022 Valve Corporation
* Copyright (c) 2015-2017, 2019-2022 LunarG, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Mark Lobodzinski <[email protected]>
* Author: Courtney Goeltzenleuchter <[email protected]>
* Author: Dave Houlton <[email protected]>
*/
#pragma once
#include <cassert>
#include <cstddef>
#include <functional>
#include <stdbool.h>
#include <string>
#include <vector>
#include <iomanip>
#include "cast_utils.h"
#include "vk_format_utils.h"
#include "vk_layer_logging.h"
#ifndef WIN32
#include <strings.h> // For ffs()
#else
#include <intrin.h> // For __lzcnt()
#endif
#define STRINGIFY(s) STRINGIFY_HELPER(s)
#define STRINGIFY_HELPER(s) #s
#ifdef __cplusplus
static inline VkExtent3D CastTo3D(const VkExtent2D &d2) {
VkExtent3D d3 = {d2.width, d2.height, 1};
return d3;
}
static inline VkOffset3D CastTo3D(const VkOffset2D &d2) {
VkOffset3D d3 = {d2.x, d2.y, 0};
return d3;
}
// Convert integer API version to a string
static inline std::string StringAPIVersion(uint32_t version) {
std::stringstream version_name;
uint32_t major = VK_VERSION_MAJOR(version);
uint32_t minor = VK_VERSION_MINOR(version);
uint32_t patch = VK_VERSION_PATCH(version);
version_name << major << "." << minor << "." << patch << " (0x" << std::setfill('0') << std::setw(8) << std::hex << version
<< ")";
return version_name.str();
}
// Traits objects to allow string_join to operate on collections of const char *
template <typename String>
struct StringJoinSizeTrait {
static size_t size(const String &str) { return str.size(); }
};
template <>
struct StringJoinSizeTrait<const char *> {
static size_t size(const char *str) {
if (!str) return 0;
return strlen(str);
}
};
// Similar to perl/python join
// * String must support size, reserve, append, and be default constructable
// * StringCollection must support size, const forward iteration, and store
// strings compatible with String::append
// * Accessor trait can be set if default accessors (compatible with string
// and const char *) don't support size(StringCollection::value_type &)
//
// Return type based on sep type
template <typename String = std::string, typename StringCollection = std::vector<String>,
typename Accessor = StringJoinSizeTrait<typename StringCollection::value_type>>
static inline String string_join(const String &sep, const StringCollection &strings) {
String joined;
const size_t count = strings.size();
if (!count) return joined;
// Prereserved storage, s.t. we will execute in linear time (avoids reallocation copies)
size_t reserve = (count - 1) * sep.size();
for (const auto &str : strings) {
reserve += Accessor::size(str); // abstracted to allow const char * type in StringCollection
}
joined.reserve(reserve + 1);
// Seps only occur *between* strings entries, so first is special
auto current = strings.cbegin();
joined.append(*current);
++current;
for (; current != strings.cend(); ++current) {
joined.append(sep);
joined.append(*current);
}
return joined;
}
// Requires StringCollection::value_type has a const char * constructor and is compatible the string_join::String above
template <typename StringCollection = std::vector<std::string>, typename SepString = std::string>
static inline SepString string_join(const char *sep, const StringCollection &strings) {
return string_join<SepString, StringCollection>(SepString(sep), strings);
}
static inline std::string string_trim(const std::string &s) {
const char *whitespace = " \t\f\v\n\r";
const auto trimmed_beg = s.find_first_not_of(whitespace);
if (trimmed_beg == std::string::npos) return "";
const auto trimmed_end = s.find_last_not_of(whitespace);
assert(trimmed_end != std::string::npos && trimmed_beg <= trimmed_end);
return s.substr(trimmed_beg, trimmed_end - trimmed_beg + 1);
}
// Perl/Python style join operation for general types using stream semantics
// Note: won't be as fast as string_join above, but simpler to use (and code)
// Note: Modifiable reference doesn't match the google style but does match std style for stream handling and algorithms
template <typename Stream, typename String, typename ForwardIt>
Stream &stream_join(Stream &stream, const String &sep, ForwardIt first, ForwardIt last) {
if (first != last) {
stream << *first;
++first;
while (first != last) {
stream << sep << *first;
++first;
}
}
return stream;
}
// stream_join For whole collections with forward iterators
template <typename Stream, typename String, typename Collection>
Stream &stream_join(Stream &stream, const String &sep, const Collection &values) {
return stream_join(stream, sep, values.cbegin(), values.cend());
}
typedef void *dispatch_key;
static inline dispatch_key get_dispatch_key(const void *object) { return (dispatch_key) * (VkLayerDispatchTable **)object; }
VK_LAYER_EXPORT VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func);
VK_LAYER_EXPORT VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func);
static inline bool IsPowerOfTwo(unsigned x) { return x && !(x & (x - 1)); }
// Returns the 0-based index of the MSB, like the x86 bit scan reverse (bsr) instruction
// Note: an input mask of 0 yields -1
static inline int MostSignificantBit(uint32_t mask) {
#if defined __GNUC__
return mask ? __builtin_clz(mask) ^ 31 : -1;
#elif defined _MSC_VER
unsigned long bit_pos;
return _BitScanReverse(&bit_pos, mask) ? int(bit_pos) : -1;
#else
for (int k = 31; k >= 0; --k) {
if (((mask >> k) & 1) != 0) {
return k;
}
}
return -1;
#endif
}
static inline uint32_t SampleCountSize(VkSampleCountFlagBits sample_count) {
uint32_t size = 0;
switch (sample_count) {
case VK_SAMPLE_COUNT_1_BIT:
size = 1;
break;
case VK_SAMPLE_COUNT_2_BIT:
size = 2;
break;
case VK_SAMPLE_COUNT_4_BIT:
size = 4;
break;
case VK_SAMPLE_COUNT_8_BIT:
size = 8;
break;
case VK_SAMPLE_COUNT_16_BIT:
size = 16;
break;
case VK_SAMPLE_COUNT_32_BIT:
size = 32;
break;
case VK_SAMPLE_COUNT_64_BIT:
size = 64;
break;
default:
size = 0;
}
return size;
}
static inline bool IsImageLayoutReadOnly(VkImageLayout layout) {
constexpr std::array<VkImageLayout, 7> read_only_layouts = {
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
};
return std::any_of(read_only_layouts.begin(), read_only_layouts.end(),
[layout](const VkImageLayout read_only_layout) { return layout == read_only_layout; });
}
static inline bool IsImageLayoutDepthReadOnly(VkImageLayout layout) {
constexpr std::array<VkImageLayout, 7> read_only_layouts = {
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
};
return std::any_of(read_only_layouts.begin(), read_only_layouts.end(),
[layout](const VkImageLayout read_only_layout) { return layout == read_only_layout; });
}
static inline bool IsImageLayoutStencilReadOnly(VkImageLayout layout) {
constexpr std::array<VkImageLayout, 7> read_only_layouts = {
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL,
};
return std::any_of(read_only_layouts.begin(), read_only_layouts.end(),
[layout](const VkImageLayout read_only_layout) { return layout == read_only_layout; });
}
static inline bool IsIdentitySwizzle(VkComponentMapping components) {
// clang-format off
return (
((components.r == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.r == VK_COMPONENT_SWIZZLE_R)) &&
((components.g == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.g == VK_COMPONENT_SWIZZLE_G)) &&
((components.b == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.b == VK_COMPONENT_SWIZZLE_B)) &&
((components.a == VK_COMPONENT_SWIZZLE_IDENTITY) || (components.a == VK_COMPONENT_SWIZZLE_A))
);
// clang-format on
}
static inline VkDeviceSize GetIndexAlignment(VkIndexType indexType) {
switch (indexType) {
case VK_INDEX_TYPE_UINT16:
return 2;
case VK_INDEX_TYPE_UINT32:
return 4;
case VK_INDEX_TYPE_UINT8_EXT:
return 1;
default:
// Not a real index type. Express no alignment requirement here; we expect upper layer
// to have already picked up on the enum being nonsense.
return 1;
}
}
static inline uint32_t GetPlaneIndex(VkImageAspectFlags aspect) {
// Returns an out of bounds index on error
switch (aspect) {
case VK_IMAGE_ASPECT_PLANE_0_BIT:
return 0;
break;
case VK_IMAGE_ASPECT_PLANE_1_BIT:
return 1;
break;
case VK_IMAGE_ASPECT_PLANE_2_BIT:
return 2;
break;
default:
// If more than one plane bit is set, return error condition
return FORMAT_MAX_PLANES;
break;
}
}
// Perform a zero-tolerant modulo operation
static inline VkDeviceSize SafeModulo(VkDeviceSize dividend, VkDeviceSize divisor) {
VkDeviceSize result = 0;
if (divisor != 0) {
result = dividend % divisor;
}
return result;
}
static inline VkDeviceSize SafeDivision(VkDeviceSize dividend, VkDeviceSize divisor) {
VkDeviceSize result = 0;
if (divisor != 0) {
result = dividend / divisor;
}
return result;
}
extern "C" {
#endif
#define VK_LAYER_API_VERSION VK_HEADER_VERSION_COMPLETE
typedef enum VkStringErrorFlagBits {
VK_STRING_ERROR_NONE = 0x00000000,
VK_STRING_ERROR_LENGTH = 0x00000001,
VK_STRING_ERROR_BAD_DATA = 0x00000002,
} VkStringErrorFlagBits;
typedef VkFlags VkStringErrorFlags;
VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
const char *layer_identifier);
VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data, const VkAllocationCallbacks *pAllocator,
const char *layer_identifier);
VK_LAYER_EXPORT VkStringErrorFlags vk_string_validate(const int max_length, const char *char_array);
VK_LAYER_EXPORT bool white_list(const char *item, const std::set<std::string> &whitelist);
static inline int u_ffs(int val) {
#ifdef WIN32
unsigned long bit_pos = 0;
if (_BitScanForward(&bit_pos, val) != 0) {
bit_pos += 1;
}
return bit_pos;
#else
return ffs(val);
#endif
}
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
// clang sets _MSC_VER to 1800 and _MSC_FULL_VER to 180000000, but we only want to clean up after MSVC.
#if defined(_MSC_FULL_VER) && !defined(__clang__)
// Minimum Visual Studio 2015 Update 2, or libc++ with C++17
// But, before Visual Studio 2017 version 15.7, __cplusplus is not set
// correctly. See:
// https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-160
// Also, according to commit e2a6c442cb1e4, SDKs older than NTDDI_WIN10_RS2 do not
// support shared_mutex.
#if _MSC_FULL_VER >= 190023918 && NTDDI_VERSION > NTDDI_WIN10_RS2 && (!defined(_LIBCPP_VERSION) || __cplusplus >= 201703)
#define VVL_USE_SHARED_MUTEX 1
#endif
#elif __cplusplus >= 201703
#define VVL_USE_SHARED_MUTEX 1
#elif __cplusplus >= 201402
#define VVL_USE_SHARED_TIMED_MUTEX 1
#endif
#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
#include <shared_mutex>
#endif
class ReadWriteLock {
private:
#if defined(VVL_USE_SHARED_MUTEX)
typedef std::shared_mutex Lock;
#elif defined(VVL_USE_SHARED_TIMED_MUTEX)
typedef std::shared_timed_mutex Lock;
#else
typedef std::mutex Lock;
#endif
public:
void lock() { m_lock.lock(); }
bool try_lock() { return m_lock.try_lock(); }
void unlock() { m_lock.unlock(); }
#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
void lock_shared() { m_lock.lock_shared(); }
bool try_lock_shared() { return m_lock.try_lock_shared(); }
void unlock_shared() { m_lock.unlock_shared(); }
#else
void lock_shared() { lock(); }
bool try_lock_shared() { return try_lock(); }
void unlock_shared() { unlock(); }
#endif
private:
Lock m_lock;
};
#if defined(VVL_USE_SHARED_MUTEX) || defined(VVL_USE_SHARED_TIMED_MUTEX)
typedef std::shared_lock<ReadWriteLock> ReadLockGuard;
#else
typedef std::unique_lock<ReadWriteLock> ReadLockGuard;
#endif
typedef std::unique_lock<ReadWriteLock> WriteLockGuard;
// helper class for the very common case of getting and then locking a command buffer (or other state object)
template <typename T, typename Guard>
class LockedSharedPtr : public std::shared_ptr<T> {
public:
LockedSharedPtr(std::shared_ptr<T> &&ptr, Guard &&guard) : std::shared_ptr<T>(std::move(ptr)), guard_(std::move(guard)) {}
LockedSharedPtr() : std::shared_ptr<T>(), guard_() {}
private:
Guard guard_;
};
// Limited concurrent_unordered_map that supports internally-synchronized
// insert/erase/access. Splits locking across N buckets and uses shared_mutex
// for read/write locking. Iterators are not supported. The following
// operations are supported:
//
// insert_or_assign: Insert a new element or update an existing element.
// insert: Insert a new element and return whether it was inserted.
// erase: Remove an element.
// contains: Returns true if the key is in the map.
// find: Returns != end() if found, value is in ret->second.
// pop: Erases and returns the erased value if found.
//
// find/end: find returns a vaguely iterator-like type that can be compared to
// end and can use iter->second to retrieve the reference. This is to ease porting
// for existing code that combines the existence check and lookup in a single
// operation (and thus a single lock). i.e.:
//
// auto iter = map.find(key);
// if (iter != map.end()) {
// T t = iter->second;
// ...
//
// snapshot: Return an array of elements (key, value pairs) that satisfy an optional
// predicate. This can be used as a substitute for iterators in exceptional cases.
template <typename Key, typename T, int BUCKETSLOG2 = 2, typename Hash = layer_data::hash<Key>>
class vl_concurrent_unordered_map {
public:
template <typename... Args>
void insert_or_assign(const Key &key, Args &&...args) {
uint32_t h = ConcurrentMapHashObject(key);
WriteLockGuard lock(locks[h].lock);
maps[h][key] = {std::forward<Args>(args)...};
}
template <typename... Args>
bool insert(const Key &key, Args &&...args) {
uint32_t h = ConcurrentMapHashObject(key);
WriteLockGuard lock(locks[h].lock);
auto ret = maps[h].emplace(key, std::forward<Args>(args)...);
return ret.second;
}
// returns size_type
size_t erase(const Key &key) {
uint32_t h = ConcurrentMapHashObject(key);
WriteLockGuard lock(locks[h].lock);
return maps[h].erase(key);
}
bool contains(const Key &key) const {
uint32_t h = ConcurrentMapHashObject(key);
ReadLockGuard lock(locks[h].lock);
return maps[h].count(key) != 0;
}
// type returned by find() and end().
class FindResult {
public:
FindResult(bool a, T b) : result(a, std::move(b)) {}
// == and != only support comparing against end()
bool operator==(const FindResult &other) const {
if (result.first == false && other.result.first == false) {
return true;
}
return false;
}
bool operator!=(const FindResult &other) const { return !(*this == other); }
// Make -> act kind of like an iterator.
std::pair<bool, T> *operator->() { return &result; }
const std::pair<bool, T> *operator->() const { return &result; }
private:
// (found, reference to element)
std::pair<bool, T> result;
};
// find()/end() return a FindResult containing a copy of the value. For end(),
// return a default value.
FindResult end() const { return FindResult(false, T()); }
FindResult cend() const { return end(); }
FindResult find(const Key &key) const {
uint32_t h = ConcurrentMapHashObject(key);
ReadLockGuard lock(locks[h].lock);
auto itr = maps[h].find(key);
bool found = itr != maps[h].end();
if (found) {
return FindResult(true, itr->second);
} else {
return end();
}
}
FindResult pop(const Key &key) {
uint32_t h = ConcurrentMapHashObject(key);
WriteLockGuard lock(locks[h].lock);
auto itr = maps[h].find(key);
bool found = itr != maps[h].end();
if (found) {
auto ret = std::move(FindResult(true, itr->second));
maps[h].erase(itr);
return ret;
} else {
return end();
}
}
std::vector<std::pair<const Key, T>> snapshot(std::function<bool(T)> f = nullptr) const {
std::vector<std::pair<const Key, T>> ret;
for (int h = 0; h < BUCKETS; ++h) {
ReadLockGuard lock(locks[h].lock);
for (const auto &j : maps[h]) {
if (!f || f(j.second)) {
ret.emplace_back(j.first, j.second);
}
}
}
return ret;
}
void clear() {
for (int h = 0; h < BUCKETS; ++h) {
WriteLockGuard lock(locks[h].lock);
maps[h].clear();
}
}
size_t size() const {
size_t result = 0;
for (int h = 0; h < BUCKETS; ++h) {
ReadLockGuard lock(locks[h].lock);
result += maps[h].size();
}
return result;
}
bool empty() const {
bool result = 0;
for (int h = 0; h < BUCKETS; ++h) {
ReadLockGuard lock(locks[h].lock);
result |= maps[h].empty();
}
return result;
}
private:
static const int BUCKETS = (1 << BUCKETSLOG2);
layer_data::unordered_map<Key, T, Hash> maps[BUCKETS];
struct {
mutable ReadWriteLock lock;
// Put each lock on its own cache line to avoid false cache line sharing.
char padding[(-int(sizeof(ReadWriteLock))) & 63];
} locks[BUCKETS];
uint32_t ConcurrentMapHashObject(const Key &object) const {
uint64_t u64 = (uint64_t)(uintptr_t)object;
uint32_t hash = (uint32_t)(u64 >> 32) + (uint32_t)u64;
hash ^= (hash >> BUCKETSLOG2) ^ (hash >> (2 * BUCKETSLOG2));
hash &= (BUCKETS - 1);
return hash;
}
};
#endif