Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 1 | /* |
| 2 | * Vulkan |
| 3 | * |
| 4 | * Copyright (C) 2015 LunarG, Inc. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included |
| 14 | * in all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 | * DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <assert.h> |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 27 | #include <map> |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 28 | #include <unordered_map> |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 29 | #include <map> |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 30 | #include <vector> |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 31 | #include "loader_platform.h" |
| 32 | #include "vk_dispatch_table_helper.h" |
| 33 | #include "vkLayer.h" |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 34 | #include "layers_config.h" |
| 35 | #include "layers_msg.h" |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 36 | #include "layers_table.h" |
Chris Forbes | 3317b38 | 2015-05-04 14:04:24 +1200 | [diff] [blame] | 37 | #include "vk_enum_string_helper.h" |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 38 | #include "shader_checker.h" |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 39 | // The following is #included again to catch certain OS-specific functions |
| 40 | // being used: |
| 41 | #include "loader_platform.h" |
| 42 | |
Chris Forbes | 32e3b46 | 2015-05-09 10:31:21 +1200 | [diff] [blame] | 43 | #include "spirv/spirv.h" |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 44 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 45 | |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 46 | static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce); |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 47 | // TODO : This can be much smarter, using separate locks for separate global data |
| 48 | static int globalLockInitialized = 0; |
| 49 | static loader_platform_thread_mutex globalLock; |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 50 | |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 51 | |
| 52 | static void |
| 53 | build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index) |
| 54 | { |
| 55 | unsigned int const *code = (unsigned int const *)&words[0]; |
| 56 | size_t size = words.size(); |
| 57 | |
| 58 | unsigned word = 5; |
| 59 | while (word < size) { |
| 60 | unsigned opcode = code[word] & 0x0ffffu; |
| 61 | unsigned oplen = (code[word] & 0xffff0000u) >> 16; |
| 62 | |
| 63 | switch (opcode) { |
| 64 | case spv::OpTypeVoid: |
| 65 | case spv::OpTypeBool: |
| 66 | case spv::OpTypeInt: |
| 67 | case spv::OpTypeFloat: |
| 68 | case spv::OpTypeVector: |
| 69 | case spv::OpTypeMatrix: |
| 70 | case spv::OpTypeSampler: |
| 71 | case spv::OpTypeFilter: |
| 72 | case spv::OpTypeArray: |
| 73 | case spv::OpTypeRuntimeArray: |
| 74 | case spv::OpTypeStruct: |
| 75 | case spv::OpTypeOpaque: |
| 76 | case spv::OpTypePointer: |
| 77 | case spv::OpTypeFunction: |
| 78 | case spv::OpTypeEvent: |
| 79 | case spv::OpTypeDeviceEvent: |
| 80 | case spv::OpTypeReserveId: |
| 81 | case spv::OpTypeQueue: |
| 82 | case spv::OpTypePipe: |
| 83 | type_def_index[code[word+1]] = word; |
| 84 | break; |
| 85 | |
| 86 | default: |
| 87 | /* We only care about type definitions */ |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | word += oplen; |
| 92 | } |
| 93 | } |
| 94 | |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 95 | struct shader_source { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 96 | /* the spirv image itself */ |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 97 | std::vector<uint32_t> words; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 98 | /* a mapping of <id> to the first word of its def. this is useful because walking type |
| 99 | * trees requires jumping all over the instruction stream. |
| 100 | */ |
| 101 | std::unordered_map<unsigned, unsigned> type_def_index; |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 102 | bool is_spirv; |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 103 | |
| 104 | shader_source(VkShaderCreateInfo const *pCreateInfo) : |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 105 | words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)), |
| 106 | type_def_index(), |
| 107 | is_spirv(true) { |
| 108 | |
| 109 | if (words.size() < 5 || words[0] != spv::MagicNumber || words[1] != spv::Version) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 110 | layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC", |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 111 | "Shader is not SPIR-V, most checks will not be possible"); |
| 112 | is_spirv = false; |
| 113 | return; |
| 114 | } |
| 115 | |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 116 | |
| 117 | build_type_def_index(words, type_def_index); |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 118 | } |
| 119 | }; |
| 120 | |
| 121 | |
| 122 | static std::unordered_map<void *, shader_source *> shader_map; |
| 123 | |
| 124 | |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 125 | static void |
| 126 | initLayer() |
| 127 | { |
| 128 | const char *strOpt; |
| 129 | // initialize ShaderChecker options |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 130 | getLayerOptionEnum("ShaderCheckerReportLevel", (uint32_t *) &g_reportFlags); |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 131 | g_actionIsDefault = getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &g_debugAction); |
| 132 | |
| 133 | if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG) |
| 134 | { |
| 135 | strOpt = getLayerOption("ShaderCheckerLogFilename"); |
| 136 | if (strOpt) |
| 137 | { |
| 138 | g_logFile = fopen(strOpt, "w"); |
| 139 | } |
| 140 | if (g_logFile == NULL) |
| 141 | g_logFile = stdout; |
| 142 | } |
| 143 | } |
| 144 | |
Tobin Ehlis | 432a9ba | 2015-04-17 08:55:13 -0600 | [diff] [blame] | 145 | #define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 2 |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 146 | static const VkExtensionProperties shaderCheckerExts[SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE] = { |
| 147 | { |
| 148 | VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES, |
| 149 | "ShaderChecker", |
| 150 | 0x10, |
| 151 | "Sample layer: ShaderChecker", |
| 152 | // 0, |
| 153 | // NULL, |
| 154 | } |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 155 | }; |
| 156 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 157 | VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo( |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 158 | VkExtensionInfoType infoType, |
| 159 | uint32_t extensionIndex, |
| 160 | size_t* pDataSize, |
| 161 | void* pData) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 162 | { |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 163 | /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */ |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 164 | uint32_t *count; |
| 165 | |
| 166 | if (pDataSize == NULL) |
| 167 | return VK_ERROR_INVALID_POINTER; |
| 168 | |
| 169 | switch (infoType) { |
| 170 | case VK_EXTENSION_INFO_TYPE_COUNT: |
| 171 | *pDataSize = sizeof(uint32_t); |
| 172 | if (pData == NULL) |
| 173 | return VK_SUCCESS; |
| 174 | count = (uint32_t *) pData; |
| 175 | *count = SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE; |
| 176 | break; |
| 177 | case VK_EXTENSION_INFO_TYPE_PROPERTIES: |
| 178 | *pDataSize = sizeof(VkExtensionProperties); |
| 179 | if (pData == NULL) |
| 180 | return VK_SUCCESS; |
| 181 | if (extensionIndex >= SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE) |
| 182 | return VK_ERROR_INVALID_VALUE; |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 183 | memcpy((VkExtensionProperties *) pData, &shaderCheckerExts[extensionIndex], sizeof(VkExtensionProperties)); |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 184 | break; |
| 185 | default: |
| 186 | return VK_ERROR_INVALID_VALUE; |
| 187 | }; |
| 188 | |
| 189 | return VK_SUCCESS; |
| 190 | } |
| 191 | |
| 192 | |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 193 | static char const * |
| 194 | storage_class_name(unsigned sc) |
| 195 | { |
| 196 | switch (sc) { |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 197 | case spv::StorageClassInput: return "input"; |
| 198 | case spv::StorageClassOutput: return "output"; |
| 199 | case spv::StorageClassUniformConstant: return "const uniform"; |
| 200 | case spv::StorageClassUniform: return "uniform"; |
| 201 | case spv::StorageClassWorkgroupLocal: return "workgroup local"; |
| 202 | case spv::StorageClassWorkgroupGlobal: return "workgroup global"; |
| 203 | case spv::StorageClassPrivateGlobal: return "private global"; |
| 204 | case spv::StorageClassFunction: return "function"; |
| 205 | case spv::StorageClassGeneric: return "generic"; |
| 206 | case spv::StorageClassPrivate: return "private"; |
| 207 | case spv::StorageClassAtomicCounter: return "atomic counter"; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 208 | default: return "unknown"; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /* returns ptr to null terminator */ |
| 214 | static char * |
| 215 | describe_type(char *dst, shader_source const *src, unsigned type) |
| 216 | { |
| 217 | auto type_def_it = src->type_def_index.find(type); |
| 218 | |
| 219 | if (type_def_it == src->type_def_index.end()) { |
| 220 | return dst + sprintf(dst, "undef"); |
| 221 | } |
| 222 | |
| 223 | unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second]; |
| 224 | unsigned opcode = code[0] & 0x0ffffu; |
| 225 | switch (opcode) { |
| 226 | case spv::OpTypeBool: |
| 227 | return dst + sprintf(dst, "bool"); |
| 228 | case spv::OpTypeInt: |
| 229 | return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]); |
| 230 | case spv::OpTypeFloat: |
| 231 | return dst + sprintf(dst, "float%d", code[2]); |
| 232 | case spv::OpTypeVector: |
| 233 | dst += sprintf(dst, "vec%d of ", code[3]); |
| 234 | return describe_type(dst, src, code[2]); |
| 235 | case spv::OpTypeMatrix: |
| 236 | dst += sprintf(dst, "mat%d of ", code[3]); |
| 237 | return describe_type(dst, src, code[2]); |
| 238 | case spv::OpTypeArray: |
| 239 | dst += sprintf(dst, "arr[%d] of ", code[3]); |
| 240 | return describe_type(dst, src, code[2]); |
| 241 | case spv::OpTypePointer: |
| 242 | dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2])); |
| 243 | return describe_type(dst, src, code[3]); |
| 244 | case spv::OpTypeStruct: |
| 245 | { |
| 246 | unsigned oplen = code[0] >> 16; |
| 247 | dst += sprintf(dst, "struct of ("); |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 248 | for (unsigned i = 2; i < oplen; i++) { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 249 | dst = describe_type(dst, src, code[i]); |
| 250 | dst += sprintf(dst, i == oplen-1 ? ")" : ", "); |
| 251 | } |
| 252 | return dst; |
| 253 | } |
| 254 | default: |
| 255 | return dst + sprintf(dst, "oddtype"); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
| 260 | static bool |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 261 | types_match(shader_source const *a, shader_source const *b, unsigned a_type, unsigned b_type, bool b_arrayed) |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 262 | { |
| 263 | auto a_type_def_it = a->type_def_index.find(a_type); |
| 264 | auto b_type_def_it = b->type_def_index.find(b_type); |
| 265 | |
| 266 | if (a_type_def_it == a->type_def_index.end()) { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | |
| 270 | if (b_type_def_it == b->type_def_index.end()) { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | |
| 274 | /* walk two type trees together, and complain about differences */ |
| 275 | unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second]; |
| 276 | unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second]; |
| 277 | |
| 278 | unsigned a_opcode = a_code[0] & 0x0ffffu; |
| 279 | unsigned b_opcode = b_code[0] & 0x0ffffu; |
| 280 | |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 281 | if (b_arrayed && b_opcode == spv::OpTypeArray) { |
| 282 | /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */ |
| 283 | return types_match(a, b, a_type, b_code[2], false); |
| 284 | } |
| 285 | |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 286 | if (a_opcode != b_opcode) { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 287 | return false; |
| 288 | } |
| 289 | |
| 290 | switch (a_opcode) { |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 291 | /* if b_arrayed and we hit a leaf type, then we can't match -- there's nowhere for the extra OpTypeArray to be! */ |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 292 | case spv::OpTypeBool: |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 293 | return true && !b_arrayed; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 294 | case spv::OpTypeInt: |
| 295 | /* match on width, signedness */ |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 296 | return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 297 | case spv::OpTypeFloat: |
| 298 | /* match on width */ |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 299 | return a_code[2] == b_code[2] && !b_arrayed; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 300 | case spv::OpTypeVector: |
| 301 | case spv::OpTypeMatrix: |
| 302 | case spv::OpTypeArray: |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 303 | /* match on element type, count. these all have the same layout. we don't get here if |
| 304 | * b_arrayed -- that is handled above. */ |
| 305 | return !b_arrayed && types_match(a, b, a_code[2], b_code[2], b_arrayed) && a_code[3] == b_code[3]; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 306 | case spv::OpTypeStruct: |
| 307 | /* match on all element types */ |
| 308 | { |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 309 | if (b_arrayed) { |
| 310 | /* for the purposes of matching different levels of arrayness, structs are leaves. */ |
| 311 | return false; |
| 312 | } |
| 313 | |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 314 | unsigned a_len = a_code[0] >> 16; |
| 315 | unsigned b_len = b_code[0] >> 16; |
| 316 | |
| 317 | if (a_len != b_len) { |
| 318 | return false; /* structs cannot match if member counts differ */ |
| 319 | } |
| 320 | |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 321 | for (unsigned i = 2; i < a_len; i++) { |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 322 | if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return true; |
| 328 | } |
| 329 | case spv::OpTypePointer: |
| 330 | /* match on pointee type. storage class is expected to differ */ |
Chris Forbes | 0a94a37 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 331 | return types_match(a, b, a_code[3], b_code[3], b_arrayed); |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 332 | |
| 333 | default: |
| 334 | /* remaining types are CLisms, or may not appear in the interfaces we |
| 335 | * are interested in. Just claim no match. |
| 336 | */ |
| 337 | return false; |
| 338 | |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 343 | static int |
| 344 | value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def) |
| 345 | { |
| 346 | auto it = map.find(id); |
| 347 | if (it == map.end()) |
| 348 | return def; |
| 349 | else |
| 350 | return it->second; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | struct interface_var { |
| 355 | uint32_t id; |
| 356 | uint32_t type_id; |
| 357 | /* TODO: collect the name, too? Isn't required to be present. */ |
| 358 | }; |
| 359 | |
| 360 | |
| 361 | static void |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 362 | collect_interface_by_location(shader_source const *src, spv::StorageClass sinterface, |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 363 | std::map<uint32_t, interface_var> &out, |
| 364 | std::map<uint32_t, interface_var> &builtins_out) |
| 365 | { |
| 366 | unsigned int const *code = (unsigned int const *)&src->words[0]; |
| 367 | size_t size = src->words.size(); |
| 368 | |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 369 | std::unordered_map<unsigned, unsigned> var_locations; |
| 370 | std::unordered_map<unsigned, unsigned> var_builtins; |
| 371 | |
| 372 | unsigned word = 5; |
| 373 | while (word < size) { |
| 374 | |
| 375 | unsigned opcode = code[word] & 0x0ffffu; |
| 376 | unsigned oplen = (code[word] & 0xffff0000u) >> 16; |
| 377 | |
| 378 | /* We consider two interface models: SSO rendezvous-by-location, and |
| 379 | * builtins. Complain about anything that fits neither model. |
| 380 | */ |
| 381 | if (opcode == spv::OpDecorate) { |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 382 | if (code[word+2] == spv::DecorationLocation) { |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 383 | var_locations[code[word+1]] = code[word+3]; |
| 384 | } |
| 385 | |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 386 | if (code[word+2] == spv::DecorationBuiltIn) { |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 387 | var_builtins[code[word+1]] = code[word+3]; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* TODO: handle grouped decorations */ |
| 392 | /* TODO: handle index=1 dual source outputs from FS -- two vars will |
| 393 | * have the same location, and we DONT want to clobber. */ |
| 394 | |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 395 | if (opcode == spv::OpVariable && code[word+3] == sinterface) { |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 396 | int location = value_or_default(var_locations, code[word+2], -1); |
| 397 | int builtin = value_or_default(var_builtins, code[word+2], -1); |
| 398 | |
| 399 | if (location == -1 && builtin == -1) { |
| 400 | /* No location defined, and not bound to an API builtin. |
| 401 | * The spec says nothing about how this case works (or doesn't) |
| 402 | * for interface matching. |
| 403 | */ |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 404 | char str[1024]; |
| 405 | sprintf(str, "var %d (type %d) in %s interface has no Location or Builtin decoration\n", |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 406 | code[word+2], code[word+1], storage_class_name(sinterface)); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 407 | layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", str); |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 408 | } |
| 409 | else if (location != -1) { |
| 410 | /* A user-defined interface variable, with a location. */ |
| 411 | interface_var v; |
| 412 | v.id = code[word+2]; |
| 413 | v.type_id = code[word+1]; |
| 414 | out[location] = v; |
| 415 | } |
| 416 | else { |
| 417 | /* A builtin interface variable */ |
| 418 | interface_var v; |
| 419 | v.id = code[word+2]; |
| 420 | v.type_id = code[word+1]; |
| 421 | builtins_out[builtin] = v; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | word += oplen; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 430 | VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo, |
| 431 | VkShader *pShader) |
| 432 | { |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 433 | loader_platform_thread_lock_mutex(&globalLock); |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 434 | VkResult res = device_dispatch_table(device)->CreateShader(device, pCreateInfo, pShader); |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 435 | |
| 436 | shader_map[(VkBaseLayerObject *) *pShader] = new shader_source(pCreateInfo); |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 437 | loader_platform_thread_unlock_mutex(&globalLock); |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 438 | return res; |
| 439 | } |
| 440 | |
| 441 | |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 442 | static bool |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 443 | validate_interface_between_stages(shader_source const *producer, char const *producer_name, |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 444 | shader_source const *consumer, char const *consumer_name, |
| 445 | bool consumer_arrayed_input) |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 446 | { |
| 447 | std::map<uint32_t, interface_var> outputs; |
| 448 | std::map<uint32_t, interface_var> inputs; |
| 449 | |
| 450 | std::map<uint32_t, interface_var> builtin_outputs; |
| 451 | std::map<uint32_t, interface_var> builtin_inputs; |
| 452 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 453 | char str[1024]; |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 454 | bool pass = true; |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 455 | |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 456 | collect_interface_by_location(producer, spv::StorageClassOutput, outputs, builtin_outputs); |
| 457 | collect_interface_by_location(consumer, spv::StorageClassInput, inputs, builtin_inputs); |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 458 | |
| 459 | auto a_it = outputs.begin(); |
| 460 | auto b_it = inputs.begin(); |
| 461 | |
| 462 | /* maps sorted by key (location); walk them together to find mismatches */ |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 463 | while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) { |
| 464 | bool a_at_end = outputs.size() == 0 || a_it == outputs.end(); |
| 465 | bool b_at_end = inputs.size() == 0 || b_it == inputs.end(); |
Chris Forbes | 4cb9767 | 2015-06-10 08:37:27 +1200 | [diff] [blame] | 466 | auto a_first = a_at_end ? 0 : a_it->first; |
| 467 | auto b_first = b_at_end ? 0 : b_it->first; |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 468 | |
| 469 | if (b_at_end || a_first < b_first) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 470 | sprintf(str, "%s writes to output location %d which is not consumed by %s\n", |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 471 | producer_name, a_first, consumer_name); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 472 | layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str); |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 473 | a_it++; |
| 474 | } |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 475 | else if (a_at_end || a_first > b_first) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 476 | sprintf(str, "%s consumes input location %d which is not written by %s\n", |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 477 | consumer_name, b_first, producer_name); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 478 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 479 | pass = false; |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 480 | b_it++; |
| 481 | } |
| 482 | else { |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 483 | if (types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id, consumer_arrayed_input)) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 484 | /* OK! */ |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 485 | } |
| 486 | else { |
| 487 | char producer_type[1024]; |
| 488 | char consumer_type[1024]; |
| 489 | describe_type(producer_type, producer, a_it->second.type_id); |
| 490 | describe_type(consumer_type, consumer, b_it->second.type_id); |
| 491 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 492 | sprintf(str, "Type mismatch on location %d: '%s' vs '%s'\n", a_it->first, |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 493 | producer_type, consumer_type); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 494 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 495 | pass = false; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 496 | } |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 497 | a_it++; |
| 498 | b_it++; |
| 499 | } |
| 500 | } |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 501 | |
| 502 | return pass; |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 506 | enum FORMAT_TYPE { |
| 507 | FORMAT_TYPE_UNDEFINED, |
| 508 | FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */ |
| 509 | FORMAT_TYPE_SINT, |
| 510 | FORMAT_TYPE_UINT, |
| 511 | }; |
| 512 | |
| 513 | |
| 514 | static unsigned |
| 515 | get_format_type(VkFormat fmt) { |
| 516 | switch (fmt) { |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 517 | case VK_FORMAT_UNDEFINED: |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 518 | return FORMAT_TYPE_UNDEFINED; |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 519 | case VK_FORMAT_R8_SINT: |
| 520 | case VK_FORMAT_R8G8_SINT: |
| 521 | case VK_FORMAT_R8G8B8_SINT: |
| 522 | case VK_FORMAT_R8G8B8A8_SINT: |
| 523 | case VK_FORMAT_R16_SINT: |
| 524 | case VK_FORMAT_R16G16_SINT: |
| 525 | case VK_FORMAT_R16G16B16_SINT: |
| 526 | case VK_FORMAT_R16G16B16A16_SINT: |
| 527 | case VK_FORMAT_R32_SINT: |
| 528 | case VK_FORMAT_R32G32_SINT: |
| 529 | case VK_FORMAT_R32G32B32_SINT: |
| 530 | case VK_FORMAT_R32G32B32A32_SINT: |
| 531 | case VK_FORMAT_B8G8R8_SINT: |
| 532 | case VK_FORMAT_B8G8R8A8_SINT: |
| 533 | case VK_FORMAT_R10G10B10A2_SINT: |
| 534 | case VK_FORMAT_B10G10R10A2_SINT: |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 535 | return FORMAT_TYPE_SINT; |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 536 | case VK_FORMAT_R8_UINT: |
| 537 | case VK_FORMAT_R8G8_UINT: |
| 538 | case VK_FORMAT_R8G8B8_UINT: |
| 539 | case VK_FORMAT_R8G8B8A8_UINT: |
| 540 | case VK_FORMAT_R16_UINT: |
| 541 | case VK_FORMAT_R16G16_UINT: |
| 542 | case VK_FORMAT_R16G16B16_UINT: |
| 543 | case VK_FORMAT_R16G16B16A16_UINT: |
| 544 | case VK_FORMAT_R32_UINT: |
| 545 | case VK_FORMAT_R32G32_UINT: |
| 546 | case VK_FORMAT_R32G32B32_UINT: |
| 547 | case VK_FORMAT_R32G32B32A32_UINT: |
| 548 | case VK_FORMAT_B8G8R8_UINT: |
| 549 | case VK_FORMAT_B8G8R8A8_UINT: |
| 550 | case VK_FORMAT_R10G10B10A2_UINT: |
| 551 | case VK_FORMAT_B10G10R10A2_UINT: |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 552 | return FORMAT_TYPE_UINT; |
| 553 | default: |
| 554 | return FORMAT_TYPE_FLOAT; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | |
Chris Forbes | 28c5088 | 2015-05-04 14:04:06 +1200 | [diff] [blame] | 559 | /* characterizes a SPIR-V type appearing in an interface to a FF stage, |
| 560 | * for comparison to a VkFormat's characterization above. */ |
| 561 | static unsigned |
| 562 | get_fundamental_type(shader_source const *src, unsigned type) |
| 563 | { |
| 564 | auto type_def_it = src->type_def_index.find(type); |
| 565 | |
| 566 | if (type_def_it == src->type_def_index.end()) { |
| 567 | return FORMAT_TYPE_UNDEFINED; |
| 568 | } |
| 569 | |
| 570 | unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second]; |
| 571 | unsigned opcode = code[0] & 0x0ffffu; |
| 572 | switch (opcode) { |
| 573 | case spv::OpTypeInt: |
| 574 | return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT; |
| 575 | case spv::OpTypeFloat: |
| 576 | return FORMAT_TYPE_FLOAT; |
| 577 | case spv::OpTypeVector: |
| 578 | return get_fundamental_type(src, code[2]); |
| 579 | case spv::OpTypeMatrix: |
| 580 | return get_fundamental_type(src, code[2]); |
| 581 | case spv::OpTypeArray: |
| 582 | return get_fundamental_type(src, code[2]); |
| 583 | case spv::OpTypePointer: |
| 584 | return get_fundamental_type(src, code[3]); |
| 585 | default: |
| 586 | return FORMAT_TYPE_UNDEFINED; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 591 | static bool |
Chris Forbes | 0bf8fe1 | 2015-06-12 11:16:41 +1200 | [diff] [blame] | 592 | validate_vi_consistency(VkPipelineVertexInputCreateInfo const *vi) |
| 593 | { |
| 594 | /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer. |
| 595 | * each binding should be specified only once. |
| 596 | */ |
| 597 | std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings; |
| 598 | char str[1024]; |
| 599 | bool pass = true; |
| 600 | |
| 601 | for (unsigned i = 0; i < vi->bindingCount; i++) { |
| 602 | auto desc = &vi->pVertexBindingDescriptions[i]; |
| 603 | auto & binding = bindings[desc->binding]; |
| 604 | if (binding) { |
| 605 | sprintf(str, "Duplicate vertex input binding descriptions for binding %d", desc->binding); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 606 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INCONSISTENT_VI, "SC", str); |
Chris Forbes | 0bf8fe1 | 2015-06-12 11:16:41 +1200 | [diff] [blame] | 607 | pass = false; |
| 608 | } |
| 609 | else { |
| 610 | binding = desc; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return pass; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | static bool |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 619 | validate_vi_against_vs_inputs(VkPipelineVertexInputCreateInfo const *vi, shader_source const *vs) |
| 620 | { |
| 621 | std::map<uint32_t, interface_var> inputs; |
| 622 | /* we collect builtin inputs, but they will never appear in the VI state -- |
| 623 | * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc) |
| 624 | */ |
| 625 | std::map<uint32_t, interface_var> builtin_inputs; |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 626 | char str[1024]; |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 627 | bool pass = true; |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 628 | |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 629 | collect_interface_by_location(vs, spv::StorageClassInput, inputs, builtin_inputs); |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 630 | |
| 631 | /* Build index by location */ |
| 632 | std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs; |
Chris Forbes | 6f2ab98 | 2015-05-25 11:13:24 +1200 | [diff] [blame] | 633 | if (vi) { |
| 634 | for (unsigned i = 0; i < vi->attributeCount; i++) |
| 635 | attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i]; |
| 636 | } |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 637 | |
| 638 | auto it_a = attribs.begin(); |
| 639 | auto it_b = inputs.begin(); |
| 640 | |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 641 | while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) { |
| 642 | bool a_at_end = attribs.size() == 0 || it_a == attribs.end(); |
| 643 | bool b_at_end = inputs.size() == 0 || it_b == inputs.end(); |
Chris Forbes | 4cb9767 | 2015-06-10 08:37:27 +1200 | [diff] [blame] | 644 | auto a_first = a_at_end ? 0 : it_a->first; |
| 645 | auto b_first = b_at_end ? 0 : it_b->first; |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 646 | if (b_at_end || a_first < b_first) { |
| 647 | sprintf(str, "Vertex attribute at location %d not consumed by VS", a_first); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 648 | layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str); |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 649 | it_a++; |
| 650 | } |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 651 | else if (a_at_end || b_first < a_first) { |
| 652 | sprintf(str, "VS consumes input at location %d but not provided", b_first); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 653 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 654 | pass = false; |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 655 | it_b++; |
| 656 | } |
| 657 | else { |
Chris Forbes | 3317b38 | 2015-05-04 14:04:24 +1200 | [diff] [blame] | 658 | unsigned attrib_type = get_format_type(it_a->second->format); |
| 659 | unsigned input_type = get_fundamental_type(vs, it_b->second.type_id); |
| 660 | |
| 661 | /* type checking */ |
| 662 | if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) { |
| 663 | char vs_type[1024]; |
| 664 | describe_type(vs_type, vs, it_b->second.type_id); |
| 665 | sprintf(str, "Attribute type of `%s` at location %d does not match VS input type of `%s`", |
| 666 | string_VkFormat(it_a->second->format), a_first, vs_type); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 667 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 668 | pass = false; |
Chris Forbes | 3317b38 | 2015-05-04 14:04:24 +1200 | [diff] [blame] | 669 | } |
| 670 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 671 | /* OK! */ |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 672 | it_a++; |
| 673 | it_b++; |
| 674 | } |
| 675 | } |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 676 | |
| 677 | return pass; |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 681 | static bool |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 682 | validate_fs_outputs_against_cb(shader_source const *fs, VkPipelineCbStateCreateInfo const *cb) |
| 683 | { |
| 684 | std::map<uint32_t, interface_var> outputs; |
| 685 | std::map<uint32_t, interface_var> builtin_outputs; |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 686 | char str[1024]; |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 687 | bool pass = true; |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 688 | |
| 689 | /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */ |
| 690 | |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 691 | collect_interface_by_location(fs, spv::StorageClassOutput, outputs, builtin_outputs); |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 692 | |
| 693 | /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs, |
| 694 | * and all color attachment should be UNORM/SNORM/FLOAT. |
| 695 | */ |
| 696 | if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) { |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 697 | if (outputs.size()) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 698 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC", |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 699 | "Should not have user-defined FS outputs when using broadcast"); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 700 | pass = false; |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 701 | } |
| 702 | |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 703 | for (unsigned i = 0; i < cb->attachmentCount; i++) { |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 704 | unsigned attachmentType = get_format_type(cb->pAttachments[i].format); |
| 705 | if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) { |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 706 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 707 | "CB format should not be SINT or UINT when using broadcast"); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 708 | pass = false; |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 712 | return pass; |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | auto it = outputs.begin(); |
| 716 | uint32_t attachment = 0; |
| 717 | |
| 718 | /* Walk attachment list and outputs together -- this is a little overpowered since attachments |
| 719 | * are currently dense, but the parallel with matching between shader stages is nice. |
| 720 | */ |
| 721 | |
Chris Forbes | 8802c99 | 2015-05-05 11:34:14 +1200 | [diff] [blame] | 722 | while ((outputs.size() > 0 && it != outputs.end()) || attachment < cb->attachmentCount) { |
scygan | 7a62cbe | 2015-06-01 19:48:11 +0200 | [diff] [blame] | 723 | if (attachment == cb->attachmentCount || ( it != outputs.end() && it->first < attachment)) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 724 | sprintf(str, "FS writes to output location %d with no matching attachment", it->first); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 725 | layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str); |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 726 | it++; |
| 727 | } |
| 728 | else if (it == outputs.end() || it->first > attachment) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 729 | sprintf(str, "Attachment %d not written by FS", attachment); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 730 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str); |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 731 | attachment++; |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 732 | pass = false; |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 733 | } |
| 734 | else { |
Chris Forbes | 4b00900 | 2015-05-04 14:20:10 +1200 | [diff] [blame] | 735 | unsigned output_type = get_fundamental_type(fs, it->second.type_id); |
| 736 | unsigned att_type = get_format_type(cb->pAttachments[attachment].format); |
| 737 | |
| 738 | /* type checking */ |
| 739 | if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) { |
| 740 | char fs_type[1024]; |
| 741 | describe_type(fs_type, fs, it->second.type_id); |
| 742 | sprintf(str, "Attachment %d of type `%s` does not match FS output type of `%s`", |
| 743 | attachment, string_VkFormat(cb->pAttachments[attachment].format), fs_type); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 744 | layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 745 | pass = false; |
Chris Forbes | 4b00900 | 2015-05-04 14:20:10 +1200 | [diff] [blame] | 746 | } |
| 747 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 748 | /* OK! */ |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 749 | it++; |
| 750 | attachment++; |
| 751 | } |
| 752 | } |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 753 | |
| 754 | return pass; |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 758 | struct shader_stage_attributes { |
| 759 | char const * const name; |
| 760 | bool arrayed_input; |
| 761 | }; |
| 762 | |
| 763 | |
| 764 | static shader_stage_attributes |
| 765 | shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = { |
| 766 | { "vertex shader", false }, |
| 767 | { "tessellation control shader", true }, |
| 768 | { "tessellation evaluation shader", false }, |
| 769 | { "geometry shader", true }, |
| 770 | { "fragment shader", false }, |
| 771 | }; |
| 772 | |
| 773 | |
Chris Forbes | f1060ca | 2015-06-04 20:23:00 +1200 | [diff] [blame] | 774 | static bool |
| 775 | validate_graphics_pipeline(VkGraphicsPipelineCreateInfo const *pCreateInfo) |
Chris Forbes | 6054093 | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 776 | { |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 777 | /* We seem to allow pipeline stages to be specified out of order, so collect and identify them |
| 778 | * before trying to do anything more: */ |
| 779 | |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 780 | shader_source const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */ |
| 781 | memset(shaders, 0, sizeof(shaders)); |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 782 | VkPipelineCbStateCreateInfo const *cb = 0; |
| 783 | VkPipelineVertexInputCreateInfo const *vi = 0; |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 784 | char str[1024]; |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 785 | bool pass = true; |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 786 | |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 787 | loader_platform_thread_lock_mutex(&globalLock); |
| 788 | |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 789 | for (auto stage = pCreateInfo; stage; stage = (decltype(stage))stage->pNext) { |
| 790 | if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) { |
| 791 | auto shader_stage = (VkPipelineShaderStageCreateInfo const *)stage; |
| 792 | |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 793 | if (shader_stage->shader.stage < VK_SHADER_STAGE_VERTEX || shader_stage->shader.stage > VK_SHADER_STAGE_FRAGMENT) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 794 | sprintf(str, "Unknown shader stage %d\n", shader_stage->shader.stage); |
Courtney Goeltzenleuchter | 1c7c65d | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 795 | layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC", str); |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 796 | } |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 797 | else { |
| 798 | shaders[shader_stage->shader.stage] = shader_map[(void *)(shader_stage->shader.shader)]; |
| 799 | } |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 800 | } |
| 801 | else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO) { |
| 802 | cb = (VkPipelineCbStateCreateInfo const *)stage; |
| 803 | } |
| 804 | else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO) { |
| 805 | vi = (VkPipelineVertexInputCreateInfo const *)stage; |
| 806 | } |
| 807 | } |
| 808 | |
Chris Forbes | 0bf8fe1 | 2015-06-12 11:16:41 +1200 | [diff] [blame] | 809 | if (vi) { |
| 810 | pass = validate_vi_consistency(vi) && pass; |
| 811 | } |
| 812 | |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 813 | if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) { |
| 814 | pass = validate_vi_against_vs_inputs(vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass; |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 815 | } |
| 816 | |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 817 | /* TODO: enforce rules about present combinations of shaders */ |
| 818 | int producer = VK_SHADER_STAGE_VERTEX; |
| 819 | int consumer = VK_SHADER_STAGE_GEOMETRY; |
| 820 | |
| 821 | while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) { |
| 822 | producer++; |
| 823 | consumer++; |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 824 | } |
| 825 | |
Tony Barbour | 4eb3cd1 | 2015-06-11 15:04:25 -0600 | [diff] [blame] | 826 | for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) { |
Chris Forbes | 4453c77 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 827 | assert(shaders[producer]); |
| 828 | if (shaders[consumer]) { |
| 829 | if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) { |
| 830 | pass = validate_interface_between_stages(shaders[producer], shader_stage_attribs[producer].name, |
| 831 | shaders[consumer], shader_stage_attribs[consumer].name, |
| 832 | shader_stage_attribs[consumer].arrayed_input) && pass; |
| 833 | } |
| 834 | |
| 835 | producer = consumer; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && cb) { |
| 840 | pass = validate_fs_outputs_against_cb(shaders[VK_SHADER_STAGE_FRAGMENT], cb) && pass; |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 841 | } |
| 842 | |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 843 | loader_platform_thread_unlock_mutex(&globalLock); |
Chris Forbes | f1060ca | 2015-06-04 20:23:00 +1200 | [diff] [blame] | 844 | return pass; |
| 845 | } |
| 846 | |
| 847 | |
Chris Forbes | d0f7f7c | 2015-06-04 20:27:09 +1200 | [diff] [blame] | 848 | VK_LAYER_EXPORT VkResult VKAPI |
| 849 | vkCreateGraphicsPipeline(VkDevice device, |
| 850 | const VkGraphicsPipelineCreateInfo *pCreateInfo, |
| 851 | VkPipeline *pPipeline) |
Chris Forbes | f1060ca | 2015-06-04 20:23:00 +1200 | [diff] [blame] | 852 | { |
| 853 | bool pass = validate_graphics_pipeline(pCreateInfo); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 854 | |
| 855 | if (pass) { |
| 856 | /* The driver is allowed to crash if passed junk. Only actually create the |
| 857 | * pipeline if we didn't run into any showstoppers above. |
| 858 | */ |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 859 | return device_dispatch_table(device)->CreateGraphicsPipeline(device, pCreateInfo, pPipeline); |
Chris Forbes | 5f362d0 | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 860 | } |
| 861 | else { |
| 862 | return VK_ERROR_UNKNOWN; |
| 863 | } |
Chris Forbes | 6054093 | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | |
Chris Forbes | d0f7f7c | 2015-06-04 20:27:09 +1200 | [diff] [blame] | 867 | VK_LAYER_EXPORT VkResult VKAPI |
| 868 | vkCreateGraphicsPipelineDerivative(VkDevice device, |
| 869 | const VkGraphicsPipelineCreateInfo *pCreateInfo, |
| 870 | VkPipeline basePipeline, |
| 871 | VkPipeline *pPipeline) |
| 872 | { |
| 873 | bool pass = validate_graphics_pipeline(pCreateInfo); |
| 874 | |
| 875 | if (pass) { |
| 876 | /* The driver is allowed to crash if passed junk. Only actually create the |
| 877 | * pipeline if we didn't run into any showstoppers above. |
| 878 | */ |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 879 | return device_dispatch_table(device)->CreateGraphicsPipelineDerivative(device, pCreateInfo, basePipeline, pPipeline); |
Chris Forbes | d0f7f7c | 2015-06-04 20:27:09 +1200 | [diff] [blame] | 880 | } |
| 881 | else { |
| 882 | return VK_ERROR_UNKNOWN; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | |
Jon Ashburn | 17f3737 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 887 | /* hook DextroyDevice to remove tableMap entry */ |
| 888 | VK_LAYER_EXPORT VkResult VKAPI vkDestroyDevice(VkDevice device) |
| 889 | { |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 890 | VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) device; |
| 891 | VkResult res = device_dispatch_table(device)->DestroyDevice(device); |
| 892 | tableMap.erase(pDisp); |
Jon Ashburn | 17f3737 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 893 | return res; |
| 894 | } |
| 895 | |
Courtney Goeltzenleuchter | 6c813dc | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 896 | VkResult VKAPI vkCreateInstance( |
| 897 | const VkInstanceCreateInfo* pCreateInfo, |
| 898 | VkInstance* pInstance) |
| 899 | { |
| 900 | |
| 901 | loader_platform_thread_once(&g_initOnce, initLayer); |
| 902 | /* |
| 903 | * For layers, the pInstance has already been filled out |
| 904 | * by the loader so that dispatch table is available. |
| 905 | */ |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 906 | VkLayerInstanceDispatchTable *pTable = initInstanceTable((const VkBaseLayerObject *) (*pInstance)); |
Courtney Goeltzenleuchter | 6c813dc | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 907 | |
| 908 | VkResult result = pTable->CreateInstance(pCreateInfo, pInstance); |
| 909 | |
| 910 | if (result == VK_SUCCESS) { |
| 911 | enable_debug_report(pCreateInfo->extensionCount, pCreateInfo->pEnabledExtensions); |
Courtney Goeltzenleuchter | f4a2eba | 2015-06-08 14:58:39 -0600 | [diff] [blame] | 912 | |
| 913 | debug_report_init_instance_extension_dispatch_table( |
| 914 | pTable, |
| 915 | pTable->GetInstanceProcAddr, |
| 916 | *pInstance); |
Courtney Goeltzenleuchter | 6c813dc | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 917 | } |
| 918 | return result; |
| 919 | } |
| 920 | |
Jon Ashburn | 17f3737 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 921 | /* hook DestroyInstance to remove tableInstanceMap entry */ |
| 922 | VK_LAYER_EXPORT VkResult VKAPI vkDestroyInstance(VkInstance instance) |
| 923 | { |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 924 | VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) instance; |
| 925 | VkResult res = instance_dispatch_table(instance)->DestroyInstance(instance); |
| 926 | tableInstanceMap.erase(pDisp); |
Jon Ashburn | 17f3737 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 927 | return res; |
| 928 | } |
Chris Forbes | b65ba35 | 2015-05-25 11:12:59 +1200 | [diff] [blame] | 929 | |
Courtney Goeltzenleuchter | 6c813dc | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 930 | VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback( |
| 931 | VkInstance instance, |
| 932 | VkFlags msgFlags, |
| 933 | const PFN_vkDbgMsgCallback pfnMsgCallback, |
| 934 | void* pUserData, |
| 935 | VkDbgMsgCallback* pMsgCallback) |
| 936 | { |
| 937 | VkLayerInstanceDispatchTable *pTable = tableInstanceMap[(VkBaseLayerObject *)instance]; |
| 938 | return layer_create_msg_callback(instance, pTable, msgFlags, pfnMsgCallback, pUserData, pMsgCallback); |
| 939 | } |
| 940 | |
| 941 | VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback( |
| 942 | VkInstance instance, |
| 943 | VkDbgMsgCallback msgCallback) |
| 944 | { |
| 945 | VkLayerInstanceDispatchTable *pTable = tableInstanceMap[(VkBaseLayerObject *)instance]; |
| 946 | return layer_destroy_msg_callback(instance, pTable, msgCallback); |
| 947 | } |
| 948 | |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 949 | VK_LAYER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char* pName) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 950 | { |
Jon Ashburn | 1245cec | 2015-05-18 13:20:15 -0600 | [diff] [blame] | 951 | if (device == NULL) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 952 | return NULL; |
| 953 | |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 954 | loader_platform_thread_once(&g_initOnce, initLayer); |
| 955 | |
Jon Ashburn | 4f2575f | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 956 | /* loader uses this to force layer initialization; device object is wrapped */ |
| 957 | if (!strcmp("vkGetDeviceProcAddr", pName)) { |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 958 | initDeviceTable((const VkBaseLayerObject *) device); |
Jon Ashburn | 4f2575f | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 959 | return (void *) vkGetDeviceProcAddr; |
| 960 | } |
| 961 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 962 | #define ADD_HOOK(fn) \ |
| 963 | if (!strncmp(#fn, pName, sizeof(#fn))) \ |
| 964 | return (void *) fn |
| 965 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 966 | ADD_HOOK(vkCreateShader); |
Jon Ashburn | 17f3737 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 967 | ADD_HOOK(vkDestroyDevice); |
Chris Forbes | 6054093 | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 968 | ADD_HOOK(vkCreateGraphicsPipeline); |
Chris Forbes | d0f7f7c | 2015-06-04 20:27:09 +1200 | [diff] [blame] | 969 | ADD_HOOK(vkCreateGraphicsPipelineDerivative); |
Jon Ashburn | 8198fd0 | 2015-05-18 09:08:41 -0600 | [diff] [blame] | 970 | #undef ADD_HOOK |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 971 | VkLayerDispatchTable* pTable = device_dispatch_table(device); |
Jon Ashburn | 4f2575f | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 972 | if (pTable->GetDeviceProcAddr == NULL) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 973 | return NULL; |
Jon Ashburn | 4f2575f | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 974 | return pTable->GetDeviceProcAddr(device, pName); |
Jon Ashburn | 79b78ac | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance inst, const char* pName) |
| 978 | { |
Courtney Goeltzenleuchter | 6c813dc | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 979 | void *fptr; |
| 980 | |
Jon Ashburn | 79b78ac | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 981 | if (inst == NULL) |
| 982 | return NULL; |
| 983 | |
Jon Ashburn | d956400 | 2015-05-07 10:27:37 -0600 | [diff] [blame] | 984 | loader_platform_thread_once(&g_initOnce, initLayer); |
Jon Ashburn | 79b78ac | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 985 | |
Jon Ashburn | 4f2575f | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 986 | if (!strcmp("vkGetInstanceProcAddr", pName)) { |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 987 | initInstanceTable((const VkBaseLayerObject *) inst); |
Jon Ashburn | 4f2575f | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 988 | return (void *) vkGetInstanceProcAddr; |
| 989 | } |
Jon Ashburn | 79b78ac | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 990 | #define ADD_HOOK(fn) \ |
| 991 | if (!strncmp(#fn, pName, sizeof(#fn))) \ |
| 992 | return (void *) fn |
| 993 | |
Courtney Goeltzenleuchter | 6c813dc | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 994 | ADD_HOOK(vkCreateInstance); |
Jon Ashburn | 17f3737 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 995 | ADD_HOOK(vkDestroyInstance); |
Jon Ashburn | 79b78ac | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 996 | ADD_HOOK(vkGetGlobalExtensionInfo); |
Jon Ashburn | 8198fd0 | 2015-05-18 09:08:41 -0600 | [diff] [blame] | 997 | #undef ADD_HOOK |
Jon Ashburn | 79b78ac | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 998 | |
Courtney Goeltzenleuchter | 6c813dc | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 999 | fptr = msg_callback_get_proc_addr(pName); |
| 1000 | if (fptr) |
| 1001 | return fptr; |
| 1002 | |
Jon Ashburn | 5a10d21 | 2015-06-01 10:02:09 -0600 | [diff] [blame^] | 1003 | VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(inst); |
Jon Ashburn | 4f2575f | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 1004 | if (pTable->GetInstanceProcAddr == NULL) |
Jon Ashburn | 79b78ac | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 1005 | return NULL; |
Jon Ashburn | 4f2575f | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 1006 | return pTable->GetInstanceProcAddr(inst, pName); |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 1007 | } |