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