| John Zulauf | f05b377 | 2019-04-03 18:04:23 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2015-2017, 2019 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2017, 2019 Valve Corporation |
| 3 | * Copyright (c) 2015-2017, 2019 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 | a055501 | 2018-08-15 16:43:49 -0600 | [diff] [blame] | 30 | #include <set> |
| Dave Houlton | 3c9fca7 | 2017-03-27 17:25:54 -0600 | [diff] [blame] | 31 | #include "vk_format_utils.h" |
| Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 32 | #include "vk_layer_logging.h" |
| 33 | |
| Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 34 | #ifndef WIN32 |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 35 | #include <strings.h> // For ffs() |
| Courtney Goeltzenleuchter | 3698c62 | 2015-10-27 11:23:21 -0600 | [diff] [blame] | 36 | #else |
| Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 37 | #include <intrin.h> // For __lzcnt() |
| Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 38 | #endif |
| Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 39 | |
| 40 | #ifdef __cplusplus |
| John Zulauf | 965d88d | 2018-04-12 15:47:26 -0600 | [diff] [blame] | 41 | // Traits objects to allow string_join to operate on collections of const char * |
| 42 | template <typename String> |
| 43 | struct StringJoinSizeTrait { |
| 44 | static size_t size(const String &str) { return str.size(); } |
| 45 | }; |
| 46 | |
| 47 | template <> |
| 48 | struct StringJoinSizeTrait<const char *> { |
| 49 | static size_t size(const char *str) { |
| 50 | if (!str) return 0; |
| 51 | return strlen(str); |
| 52 | } |
| 53 | }; |
| 54 | // Similar to perl/python join |
| 55 | // * String must support size, reserve, append, and be default constructable |
| 56 | // * StringCollection must support size, const forward iteration, and store |
| 57 | // strings compatible with String::append |
| 58 | // * Accessor trait can be set if default accessors (compatible with string |
| 59 | // and const char *) don't support size(StringCollection::value_type &) |
| 60 | // |
| 61 | // Return type based on sep type |
| 62 | template <typename String = std::string, typename StringCollection = std::vector<String>, |
| 63 | typename Accessor = StringJoinSizeTrait<typename StringCollection::value_type>> |
| 64 | static inline String string_join(const String &sep, const StringCollection &strings) { |
| 65 | String joined; |
| 66 | const size_t count = strings.size(); |
| 67 | if (!count) return joined; |
| 68 | |
| 69 | // Prereserved storage, s.t. we will execute in linear time (avoids reallocation copies) |
| 70 | size_t reserve = (count - 1) * sep.size(); |
| 71 | for (const auto &str : strings) { |
| 72 | reserve += Accessor::size(str); // abstracted to allow const char * type in StringCollection |
| 73 | } |
| 74 | joined.reserve(reserve + 1); |
| 75 | |
| 76 | // Seps only occur *between* strings entries, so first is special |
| 77 | auto current = strings.cbegin(); |
| 78 | joined.append(*current); |
| 79 | ++current; |
| 80 | for (; current != strings.cend(); ++current) { |
| 81 | joined.append(sep); |
| 82 | joined.append(*current); |
| 83 | } |
| 84 | return joined; |
| 85 | } |
| 86 | |
| 87 | // Requires StringCollection::value_type has a const char * constructor and is compatible the string_join::String above |
| 88 | template <typename StringCollection = std::vector<std::string>, typename SepString = std::string> |
| 89 | static inline SepString string_join(const char *sep, const StringCollection &strings) { |
| 90 | return string_join<SepString, StringCollection>(SepString(sep), strings); |
| 91 | } |
| 92 | |
| John Zulauf | df851b1 | 2018-06-12 14:49:04 -0600 | [diff] [blame] | 93 | // Perl/Python style join operation for general types using stream semantics |
| 94 | // Note: won't be as fast as string_join above, but simpler to use (and code) |
| 95 | // Note: Modifiable reference doesn't match the google style but does match std style for stream handling and algorithms |
| 96 | template <typename Stream, typename String, typename ForwardIt> |
| 97 | Stream &stream_join(Stream &stream, const String &sep, ForwardIt first, ForwardIt last) { |
| 98 | if (first != last) { |
| 99 | stream << *first; |
| 100 | ++first; |
| 101 | while (first != last) { |
| 102 | stream << sep << *first; |
| 103 | ++first; |
| 104 | } |
| 105 | } |
| 106 | return stream; |
| 107 | } |
| 108 | |
| 109 | // stream_join For whole collections with forward iterators |
| 110 | template <typename Stream, typename String, typename Collection> |
| 111 | Stream &stream_join(Stream &stream, const String &sep, const Collection &values) { |
| 112 | return stream_join(stream, sep, values.cbegin(), values.cend()); |
| 113 | } |
| 114 | |
| Mark Lobodzinski | c1b5b88 | 2018-06-25 14:54:04 -0600 | [diff] [blame] | 115 | typedef void *dispatch_key; |
| 116 | static inline dispatch_key get_dispatch_key(const void *object) { return (dispatch_key) * (VkLayerDispatchTable **)object; } |
| 117 | |
| 118 | VK_LAYER_EXPORT VkLayerInstanceCreateInfo *get_chain_info(const VkInstanceCreateInfo *pCreateInfo, VkLayerFunction func); |
| 119 | VK_LAYER_EXPORT VkLayerDeviceCreateInfo *get_chain_info(const VkDeviceCreateInfo *pCreateInfo, VkLayerFunction func); |
| 120 | |
| Chris Mayer | 334e72f | 2018-11-29 14:25:41 +0100 | [diff] [blame] | 121 | static inline bool IsPowerOfTwo(unsigned x) { return x && !(x & (x - 1)); } |
| Chris Mayer | 9bc9d09 | 2018-11-12 12:29:10 +0100 | [diff] [blame] | 122 | |
| John Zulauf | f05b377 | 2019-04-03 18:04:23 -0600 | [diff] [blame] | 123 | // Casts to allow various types of less than 64 bits to be cast to and from uint64_t safely and portably |
| 124 | template <typename HandleType, typename Uint> |
| 125 | static inline HandleType CastFromUint(Uint untyped_handle) { |
| 126 | static_assert(sizeof(HandleType) == sizeof(Uint), "HandleType must be the same size as untyped handle"); |
| 127 | return *reinterpret_cast<HandleType *>(&untyped_handle); |
| 128 | } |
| 129 | template <typename HandleType, typename Uint> |
| 130 | static inline Uint CastToUint(HandleType handle) { |
| 131 | static_assert(sizeof(HandleType) == sizeof(Uint), "HandleType must be the same size as untyped handle"); |
| 132 | return *reinterpret_cast<Uint *>(&handle); |
| 133 | } |
| 134 | |
| 135 | // Ensure that the size changing casts are *static* to ensure portability |
| 136 | template <typename HandleType> |
| 137 | static inline HandleType CastFromUint64(uint64_t untyped_handle) { |
| 138 | static_assert(sizeof(HandleType) <= sizeof(uint64_t), "HandleType must be not larger than the untyped handle size"); |
| 139 | // Since size mismatched reinterpret casts are strongly non-portable we use std::condtional to find the appropriate |
| 140 | // unsigned integer type for the reinterpret cast we are using. C++11 doesn't have anything like a switch, for the type |
| 141 | // conditionals, so the various cases are nested in the "false" type of std::conditional. |
| 142 | // The formatting makes it clear, but each indent is else if. |
| 143 | typedef |
| 144 | typename std::conditional<sizeof(HandleType) == sizeof(uint8_t), uint8_t, |
| 145 | typename std::conditional<sizeof(HandleType) == sizeof(uint16_t), uint16_t, |
| 146 | typename std::conditional<sizeof(HandleType) == sizeof(uint32_t), |
| 147 | uint32_t, uint64_t>::type>::type>::type Uint; |
| 148 | return CastFromUint<HandleType, Uint>(static_cast<Uint>(untyped_handle)); |
| 149 | } |
| 150 | |
| 151 | template <typename HandleType> |
| 152 | static uint64_t CastToUint64(HandleType handle) { |
| 153 | static_assert(sizeof(HandleType) <= sizeof(uint64_t), "HandleType must be not larger than the untyped handle size"); |
| 154 | typedef |
| 155 | typename std::conditional<sizeof(HandleType) == sizeof(uint8_t), uint8_t, |
| 156 | typename std::conditional<sizeof(HandleType) == sizeof(uint16_t), uint16_t, |
| 157 | typename std::conditional<sizeof(HandleType) == sizeof(uint32_t), |
| 158 | uint32_t, uint64_t>::type>::type>::type Uint; |
| 159 | return static_cast<uint64_t>(CastToUint<HandleType, Uint>(handle)); |
| 160 | } |
| 161 | |
| John Zulauf | 25ea243 | 2019-04-05 10:07:38 -0600 | [diff] [blame^] | 162 | // Convenience functions to case between handles and the types the handles abstract, reflecting the Vulkan handle scheme, where |
| 163 | // Handles are either pointers (dispatchable) or sizeof(uint64_t) (non-dispatchable), s.t. full size-safe casts are used and |
| 164 | // we ensure that handles are large enough to contain the underlying type. |
| 165 | template <typename HandleType, typename ValueType> |
| 166 | void CastToHandle(ValueType value, HandleType *handle) { |
| 167 | static_assert(sizeof(HandleType) >= sizeof(ValueType), "HandleType must large enough to hold internal value"); |
| 168 | *handle = CastFromUint64<HandleType>(CastToUint64<ValueType>(value)); |
| 169 | } |
| 170 | // This form is conveniently "inline" but inconveniently requires both template arguments. |
| 171 | template <typename HandleType, typename ValueType> |
| 172 | HandleType CastToHandle(ValueType value) { |
| 173 | HandleType handle; |
| 174 | CastToHandle(value, &handle); |
| 175 | return handle; |
| 176 | } |
| 177 | |
| 178 | template <typename HandleType, typename ValueType> |
| 179 | void CastFromHandle(HandleType handle, ValueType *value) { |
| 180 | static_assert(sizeof(HandleType) >= sizeof(ValueType), "HandleType must large enough to hold internal value"); |
| 181 | *value = CastFromUint64<ValueType>(CastToUint64<HandleType>(handle)); |
| 182 | } |
| 183 | template <typename HandleType, typename ValueType> |
| 184 | ValueType CastFromHandle(HandleType handle) { |
| 185 | ValueType value; |
| 186 | CastFromHandle(handle, &value); |
| 187 | return value; |
| 188 | } |
| 189 | |
| Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 190 | extern "C" { |
| 191 | #endif |
| 192 | |
| Jon Ashburn | d883d81 | 2016-03-24 08:32:09 -0600 | [diff] [blame] | 193 | #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] | 194 | |
| Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 195 | typedef enum VkStringErrorFlagBits { |
| Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 196 | VK_STRING_ERROR_NONE = 0x00000000, |
| 197 | VK_STRING_ERROR_LENGTH = 0x00000001, |
| 198 | VK_STRING_ERROR_BAD_DATA = 0x00000002, |
| Mark Lobodzinski | a9f3349 | 2016-01-11 14:17:05 -0700 | [diff] [blame] | 199 | } VkStringErrorFlagBits; |
| 200 | typedef VkFlags VkStringErrorFlags; |
| Mark Lobodzinski | 1ed594e | 2016-02-03 09:57:14 -0700 | [diff] [blame] | 201 | |
| Mark Young | 6ba8abe | 2017-11-09 10:37:04 -0700 | [diff] [blame] | 202 | VK_LAYER_EXPORT void layer_debug_report_actions(debug_report_data *report_data, |
| 203 | std::vector<VkDebugReportCallbackEXT> &logging_callback, |
| 204 | const VkAllocationCallbacks *pAllocator, const char *layer_identifier); |
| 205 | |
| 206 | VK_LAYER_EXPORT void layer_debug_messenger_actions(debug_report_data *report_data, |
| 207 | std::vector<VkDebugUtilsMessengerEXT> &logging_messenger, |
| 208 | const VkAllocationCallbacks *pAllocator, const char *layer_identifier); |
| Mark Lobodzinski | 1079e1b | 2016-03-15 14:21:59 -0600 | [diff] [blame] | 209 | |
| Mike Stroyan | a551bc0 | 2016-09-28 09:42:28 -0600 | [diff] [blame] | 210 | 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] | 211 | 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] | 212 | |
| Jon Ashburn | 5484e0c | 2016-03-08 17:48:44 -0700 | [diff] [blame] | 213 | static inline int u_ffs(int val) { |
| Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 214 | #ifdef WIN32 |
| Mark Lobodzinski | 5ddf6c3 | 2015-12-16 17:47:28 -0700 | [diff] [blame] | 215 | unsigned long bit_pos = 0; |
| Mike Stroyan | debb984 | 2016-01-07 10:05:21 -0700 | [diff] [blame] | 216 | if (_BitScanForward(&bit_pos, val) != 0) { |
| Mark Lobodzinski | 5ddf6c3 | 2015-12-16 17:47:28 -0700 | [diff] [blame] | 217 | bit_pos += 1; |
| 218 | } |
| 219 | return bit_pos; |
| Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 220 | #else |
| Mark Lobodzinski | 5ddf6c3 | 2015-12-16 17:47:28 -0700 | [diff] [blame] | 221 | return ffs(val); |
| Courtney Goeltzenleuchter | d263550 | 2015-10-21 17:08:06 -0600 | [diff] [blame] | 222 | #endif |
| 223 | } |
| Mark Lobodzinski | 6f2274e | 2015-09-22 09:33:21 -0600 | [diff] [blame] | 224 | |
| 225 | #ifdef __cplusplus |
| 226 | } |
| 227 | #endif |