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