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" |
Chris Forbes | 3317b38 | 2015-05-04 14:04:24 +1200 | [diff] [blame] | 36 | #include "vk_enum_string_helper.h" |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 37 | #include "shader_checker.h" |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 38 | // The following is #included again to catch certain OS-specific functions |
| 39 | // being used: |
| 40 | #include "loader_platform.h" |
| 41 | |
Chris Forbes | 32e3b46 | 2015-05-09 10:31:21 +1200 | [diff] [blame] | 42 | #include "spirv/spirv.h" |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 43 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 44 | |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 45 | static std::unordered_map<void *, VkLayerDispatchTable *> tableMap; |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 46 | static VkBaseLayerObject *pCurObj; |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 47 | static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce); |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 48 | // TODO : This can be much smarter, using separate locks for separate global data |
| 49 | static int globalLockInitialized = 0; |
| 50 | static loader_platform_thread_mutex globalLock; |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 51 | |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 52 | |
| 53 | static void |
| 54 | build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index) |
| 55 | { |
| 56 | unsigned int const *code = (unsigned int const *)&words[0]; |
| 57 | size_t size = words.size(); |
| 58 | |
| 59 | unsigned word = 5; |
| 60 | while (word < size) { |
| 61 | unsigned opcode = code[word] & 0x0ffffu; |
| 62 | unsigned oplen = (code[word] & 0xffff0000u) >> 16; |
| 63 | |
| 64 | switch (opcode) { |
| 65 | case spv::OpTypeVoid: |
| 66 | case spv::OpTypeBool: |
| 67 | case spv::OpTypeInt: |
| 68 | case spv::OpTypeFloat: |
| 69 | case spv::OpTypeVector: |
| 70 | case spv::OpTypeMatrix: |
| 71 | case spv::OpTypeSampler: |
| 72 | case spv::OpTypeFilter: |
| 73 | case spv::OpTypeArray: |
| 74 | case spv::OpTypeRuntimeArray: |
| 75 | case spv::OpTypeStruct: |
| 76 | case spv::OpTypeOpaque: |
| 77 | case spv::OpTypePointer: |
| 78 | case spv::OpTypeFunction: |
| 79 | case spv::OpTypeEvent: |
| 80 | case spv::OpTypeDeviceEvent: |
| 81 | case spv::OpTypeReserveId: |
| 82 | case spv::OpTypeQueue: |
| 83 | case spv::OpTypePipe: |
| 84 | type_def_index[code[word+1]] = word; |
| 85 | break; |
| 86 | |
| 87 | default: |
| 88 | /* We only care about type definitions */ |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | word += oplen; |
| 93 | } |
| 94 | } |
| 95 | |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 96 | struct shader_source { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 97 | /* the spirv image itself */ |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 98 | std::vector<uint32_t> words; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 99 | /* a mapping of <id> to the first word of its def. this is useful because walking type |
| 100 | * trees requires jumping all over the instruction stream. |
| 101 | */ |
| 102 | std::unordered_map<unsigned, unsigned> type_def_index; |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 103 | |
| 104 | shader_source(VkShaderCreateInfo const *pCreateInfo) : |
| 105 | words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)) { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 106 | |
| 107 | build_type_def_index(words, type_def_index); |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 108 | } |
| 109 | }; |
| 110 | |
| 111 | |
| 112 | static std::unordered_map<void *, shader_source *> shader_map; |
| 113 | |
| 114 | |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 115 | static void |
| 116 | initLayer() |
| 117 | { |
| 118 | const char *strOpt; |
| 119 | // initialize ShaderChecker options |
| 120 | getLayerOptionEnum("ShaderCheckerReportLevel", (uint32_t *) &g_reportingLevel); |
| 121 | g_actionIsDefault = getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &g_debugAction); |
| 122 | |
| 123 | if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG) |
| 124 | { |
| 125 | strOpt = getLayerOption("ShaderCheckerLogFilename"); |
| 126 | if (strOpt) |
| 127 | { |
| 128 | g_logFile = fopen(strOpt, "w"); |
| 129 | } |
| 130 | if (g_logFile == NULL) |
| 131 | g_logFile = stdout; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 136 | static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *gpuw) |
| 137 | { |
| 138 | VkLayerDispatchTable *pTable; |
| 139 | |
| 140 | assert(gpuw); |
| 141 | std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) gpuw->baseObject); |
| 142 | if (it == tableMap.end()) |
| 143 | { |
| 144 | pTable = new VkLayerDispatchTable; |
| 145 | tableMap[(void *) gpuw->baseObject] = pTable; |
| 146 | } else |
| 147 | { |
| 148 | return it->second; |
| 149 | } |
| 150 | |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 151 | layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalDevice) gpuw->nextObject); |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 152 | pCurObj = (VkBaseLayerObject *)gpuw->baseObject; |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 153 | |
| 154 | return pTable; |
| 155 | } |
| 156 | |
| 157 | |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 158 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 159 | { |
| 160 | VkLayerDispatchTable* pTable = tableMap[gpu]; |
| 161 | VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice); |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 162 | |
| 163 | loader_platform_thread_once(&g_initOnce, initLayer); |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 164 | // create a mapping for the device object into the dispatch table |
| 165 | tableMap.emplace(*pDevice, pTable); |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 166 | pCurObj = (VkBaseLayerObject *) *pDevice; |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 167 | return result; |
| 168 | } |
| 169 | |
| 170 | |
Courtney Goeltzenleuchter | bb1f360 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 171 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice physicalDevice, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 172 | { |
Courtney Goeltzenleuchter | bb1f360 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 173 | if (pLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pOutLayers[1] == NULL || pReserved == NULL) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 174 | return VK_ERROR_INVALID_POINTER; |
| 175 | |
Courtney Goeltzenleuchter | bb1f360 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 176 | if (*pLayerCount < 1) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 177 | return VK_ERROR_INITIALIZATION_FAILED; |
Courtney Goeltzenleuchter | bb1f360 | 2015-04-20 11:04:54 -0600 | [diff] [blame] | 178 | *pLayerCount = 1; |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 179 | strncpy((char *) pOutLayers[0], "ShaderChecker", maxStringSize); |
| 180 | return VK_SUCCESS; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | struct extProps { |
| 185 | uint32_t version; |
| 186 | const char * const name; |
| 187 | }; |
Tobin Ehlis | 432a9ba | 2015-04-17 08:55:13 -0600 | [diff] [blame] | 188 | #define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 2 |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 189 | static const struct extProps shaderCheckerExts[SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE] = { |
| 190 | // TODO what is the version? |
| 191 | 0x10, "ShaderChecker", |
Tobin Ehlis | 432a9ba | 2015-04-17 08:55:13 -0600 | [diff] [blame] | 192 | 0x10, "Validation", |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 193 | }; |
| 194 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 195 | VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo( |
| 196 | VkExtensionInfoType infoType, |
| 197 | uint32_t extensionIndex, |
| 198 | size_t* pDataSize, |
| 199 | void* pData) |
| 200 | { |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 201 | /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */ |
| 202 | VkExtensionProperties *ext_props; |
| 203 | uint32_t *count; |
| 204 | |
| 205 | if (pDataSize == NULL) |
| 206 | return VK_ERROR_INVALID_POINTER; |
| 207 | |
| 208 | switch (infoType) { |
| 209 | case VK_EXTENSION_INFO_TYPE_COUNT: |
| 210 | *pDataSize = sizeof(uint32_t); |
| 211 | if (pData == NULL) |
| 212 | return VK_SUCCESS; |
| 213 | count = (uint32_t *) pData; |
| 214 | *count = SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE; |
| 215 | break; |
| 216 | case VK_EXTENSION_INFO_TYPE_PROPERTIES: |
| 217 | *pDataSize = sizeof(VkExtensionProperties); |
| 218 | if (pData == NULL) |
| 219 | return VK_SUCCESS; |
| 220 | if (extensionIndex >= SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE) |
| 221 | return VK_ERROR_INVALID_VALUE; |
| 222 | ext_props = (VkExtensionProperties *) pData; |
| 223 | ext_props->version = shaderCheckerExts[extensionIndex].version; |
| 224 | strncpy(ext_props->extName, shaderCheckerExts[extensionIndex].name, |
| 225 | VK_MAX_EXTENSION_NAME); |
| 226 | ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0'; |
| 227 | break; |
| 228 | default: |
| 229 | return VK_ERROR_INVALID_VALUE; |
| 230 | }; |
| 231 | |
| 232 | return VK_SUCCESS; |
| 233 | } |
| 234 | |
| 235 | |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 236 | static char const * |
| 237 | storage_class_name(unsigned sc) |
| 238 | { |
| 239 | switch (sc) { |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 240 | case spv::StorageClassInput: return "input"; |
| 241 | case spv::StorageClassOutput: return "output"; |
| 242 | case spv::StorageClassUniformConstant: return "const uniform"; |
| 243 | case spv::StorageClassUniform: return "uniform"; |
| 244 | case spv::StorageClassWorkgroupLocal: return "workgroup local"; |
| 245 | case spv::StorageClassWorkgroupGlobal: return "workgroup global"; |
| 246 | case spv::StorageClassPrivateGlobal: return "private global"; |
| 247 | case spv::StorageClassFunction: return "function"; |
| 248 | case spv::StorageClassGeneric: return "generic"; |
| 249 | case spv::StorageClassPrivate: return "private"; |
| 250 | case spv::StorageClassAtomicCounter: return "atomic counter"; |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 251 | default: return "unknown"; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /* returns ptr to null terminator */ |
| 257 | static char * |
| 258 | describe_type(char *dst, shader_source const *src, unsigned type) |
| 259 | { |
| 260 | auto type_def_it = src->type_def_index.find(type); |
| 261 | |
| 262 | if (type_def_it == src->type_def_index.end()) { |
| 263 | return dst + sprintf(dst, "undef"); |
| 264 | } |
| 265 | |
| 266 | unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second]; |
| 267 | unsigned opcode = code[0] & 0x0ffffu; |
| 268 | switch (opcode) { |
| 269 | case spv::OpTypeBool: |
| 270 | return dst + sprintf(dst, "bool"); |
| 271 | case spv::OpTypeInt: |
| 272 | return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]); |
| 273 | case spv::OpTypeFloat: |
| 274 | return dst + sprintf(dst, "float%d", code[2]); |
| 275 | case spv::OpTypeVector: |
| 276 | dst += sprintf(dst, "vec%d of ", code[3]); |
| 277 | return describe_type(dst, src, code[2]); |
| 278 | case spv::OpTypeMatrix: |
| 279 | dst += sprintf(dst, "mat%d of ", code[3]); |
| 280 | return describe_type(dst, src, code[2]); |
| 281 | case spv::OpTypeArray: |
| 282 | dst += sprintf(dst, "arr[%d] of ", code[3]); |
| 283 | return describe_type(dst, src, code[2]); |
| 284 | case spv::OpTypePointer: |
| 285 | dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2])); |
| 286 | return describe_type(dst, src, code[3]); |
| 287 | case spv::OpTypeStruct: |
| 288 | { |
| 289 | unsigned oplen = code[0] >> 16; |
| 290 | dst += sprintf(dst, "struct of ("); |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 291 | for (unsigned i = 2; i < oplen; i++) { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 292 | dst = describe_type(dst, src, code[i]); |
| 293 | dst += sprintf(dst, i == oplen-1 ? ")" : ", "); |
| 294 | } |
| 295 | return dst; |
| 296 | } |
| 297 | default: |
| 298 | return dst + sprintf(dst, "oddtype"); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | |
| 303 | static bool |
| 304 | types_match(shader_source const *a, shader_source const *b, unsigned a_type, unsigned b_type) |
| 305 | { |
| 306 | auto a_type_def_it = a->type_def_index.find(a_type); |
| 307 | auto b_type_def_it = b->type_def_index.find(b_type); |
| 308 | |
| 309 | if (a_type_def_it == a->type_def_index.end()) { |
| 310 | printf("ERR: can't find def for type %d in producing shader %p; SPIRV probably invalid.\n", |
| 311 | a_type, a); |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | if (b_type_def_it == b->type_def_index.end()) { |
| 316 | printf("ERR: can't find def for type %d in consuming shader %p; SPIRV probably invalid.\n", |
| 317 | b_type, b); |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | /* walk two type trees together, and complain about differences */ |
| 322 | unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second]; |
| 323 | unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second]; |
| 324 | |
| 325 | unsigned a_opcode = a_code[0] & 0x0ffffu; |
| 326 | unsigned b_opcode = b_code[0] & 0x0ffffu; |
| 327 | |
| 328 | if (a_opcode != b_opcode) { |
| 329 | printf(" - FAIL: type def opcodes differ: %d vs %d\n", a_opcode, b_opcode); |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | switch (a_opcode) { |
| 334 | case spv::OpTypeBool: |
| 335 | return true; |
| 336 | case spv::OpTypeInt: |
| 337 | /* match on width, signedness */ |
| 338 | return a_code[2] == b_code[2] && a_code[3] == b_code[3]; |
| 339 | case spv::OpTypeFloat: |
| 340 | /* match on width */ |
| 341 | return a_code[2] == b_code[2]; |
| 342 | case spv::OpTypeVector: |
| 343 | case spv::OpTypeMatrix: |
| 344 | case spv::OpTypeArray: |
| 345 | /* match on element type, count. these all have the same layout */ |
| 346 | return types_match(a, b, a_code[2], b_code[2]) && a_code[3] == b_code[3]; |
| 347 | case spv::OpTypeStruct: |
| 348 | /* match on all element types */ |
| 349 | { |
| 350 | unsigned a_len = a_code[0] >> 16; |
| 351 | unsigned b_len = b_code[0] >> 16; |
| 352 | |
| 353 | if (a_len != b_len) { |
| 354 | return false; /* structs cannot match if member counts differ */ |
| 355 | } |
| 356 | |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 357 | for (unsigned i = 2; i < a_len; i++) { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 358 | if (!types_match(a, b, a_code[i], b_code[i])) { |
| 359 | return false; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return true; |
| 364 | } |
| 365 | case spv::OpTypePointer: |
| 366 | /* match on pointee type. storage class is expected to differ */ |
| 367 | return types_match(a, b, a_code[3], b_code[3]); |
| 368 | |
| 369 | default: |
| 370 | /* remaining types are CLisms, or may not appear in the interfaces we |
| 371 | * are interested in. Just claim no match. |
| 372 | */ |
| 373 | return false; |
| 374 | |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 379 | static int |
| 380 | value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def) |
| 381 | { |
| 382 | auto it = map.find(id); |
| 383 | if (it == map.end()) |
| 384 | return def; |
| 385 | else |
| 386 | return it->second; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | struct interface_var { |
| 391 | uint32_t id; |
| 392 | uint32_t type_id; |
| 393 | /* TODO: collect the name, too? Isn't required to be present. */ |
| 394 | }; |
| 395 | |
| 396 | |
| 397 | static void |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 398 | collect_interface_by_location(shader_source const *src, spv::StorageClass sinterface, |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 399 | std::map<uint32_t, interface_var> &out, |
| 400 | std::map<uint32_t, interface_var> &builtins_out) |
| 401 | { |
| 402 | unsigned int const *code = (unsigned int const *)&src->words[0]; |
| 403 | size_t size = src->words.size(); |
| 404 | |
| 405 | if (code[0] != spv::MagicNumber) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 406 | layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC", |
| 407 | "Shader is not SPIR-V, unable to extract interface"); |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | |
| 411 | std::unordered_map<unsigned, unsigned> var_locations; |
| 412 | std::unordered_map<unsigned, unsigned> var_builtins; |
| 413 | |
| 414 | unsigned word = 5; |
| 415 | while (word < size) { |
| 416 | |
| 417 | unsigned opcode = code[word] & 0x0ffffu; |
| 418 | unsigned oplen = (code[word] & 0xffff0000u) >> 16; |
| 419 | |
| 420 | /* We consider two interface models: SSO rendezvous-by-location, and |
| 421 | * builtins. Complain about anything that fits neither model. |
| 422 | */ |
| 423 | if (opcode == spv::OpDecorate) { |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 424 | if (code[word+2] == spv::DecorationLocation) { |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 425 | var_locations[code[word+1]] = code[word+3]; |
| 426 | } |
| 427 | |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 428 | if (code[word+2] == spv::DecorationBuiltIn) { |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 429 | var_builtins[code[word+1]] = code[word+3]; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* TODO: handle grouped decorations */ |
| 434 | /* TODO: handle index=1 dual source outputs from FS -- two vars will |
| 435 | * have the same location, and we DONT want to clobber. */ |
| 436 | |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 437 | if (opcode == spv::OpVariable && code[word+3] == sinterface) { |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 438 | int location = value_or_default(var_locations, code[word+2], -1); |
| 439 | int builtin = value_or_default(var_builtins, code[word+2], -1); |
| 440 | |
| 441 | if (location == -1 && builtin == -1) { |
| 442 | /* No location defined, and not bound to an API builtin. |
| 443 | * The spec says nothing about how this case works (or doesn't) |
| 444 | * for interface matching. |
| 445 | */ |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 446 | char str[1024]; |
| 447 | 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] | 448 | code[word+2], code[word+1], storage_class_name(sinterface)); |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 449 | layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", str); |
Chris Forbes | 67cc36f | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 450 | } |
| 451 | else if (location != -1) { |
| 452 | /* A user-defined interface variable, with a location. */ |
| 453 | interface_var v; |
| 454 | v.id = code[word+2]; |
| 455 | v.type_id = code[word+1]; |
| 456 | out[location] = v; |
| 457 | } |
| 458 | else { |
| 459 | /* A builtin interface variable */ |
| 460 | interface_var v; |
| 461 | v.id = code[word+2]; |
| 462 | v.type_id = code[word+1]; |
| 463 | builtins_out[builtin] = v; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | word += oplen; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 472 | VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo, |
| 473 | VkShader *pShader) |
| 474 | { |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 475 | loader_platform_thread_lock_mutex(&globalLock); |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 476 | VkLayerDispatchTable* pTable = tableMap[(VkBaseLayerObject *)device]; |
| 477 | VkResult res = pTable->CreateShader(device, pCreateInfo, pShader); |
Chris Forbes | 4396ff5 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 478 | |
| 479 | shader_map[(VkBaseLayerObject *) *pShader] = new shader_source(pCreateInfo); |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 480 | loader_platform_thread_unlock_mutex(&globalLock); |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 481 | return res; |
| 482 | } |
| 483 | |
| 484 | |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 485 | static void |
| 486 | validate_interface_between_stages(shader_source const *producer, char const *producer_name, |
| 487 | shader_source const *consumer, char const *consumer_name) |
| 488 | { |
| 489 | std::map<uint32_t, interface_var> outputs; |
| 490 | std::map<uint32_t, interface_var> inputs; |
| 491 | |
| 492 | std::map<uint32_t, interface_var> builtin_outputs; |
| 493 | std::map<uint32_t, interface_var> builtin_inputs; |
| 494 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 495 | char str[1024]; |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 496 | |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 497 | collect_interface_by_location(producer, spv::StorageClassOutput, outputs, builtin_outputs); |
| 498 | collect_interface_by_location(consumer, spv::StorageClassInput, inputs, builtin_inputs); |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 499 | |
| 500 | auto a_it = outputs.begin(); |
| 501 | auto b_it = inputs.begin(); |
| 502 | |
| 503 | /* maps sorted by key (location); walk them together to find mismatches */ |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 504 | while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) { |
| 505 | bool a_at_end = outputs.size() == 0 || a_it == outputs.end(); |
| 506 | bool b_at_end = inputs.size() == 0 || b_it == inputs.end(); |
| 507 | auto a_first = (outputs.size() > 0 ? a_it->first : 0); |
| 508 | auto b_first = (inputs.size() > 0 ? b_it->first : 0); |
| 509 | |
| 510 | if (b_at_end || a_first < b_first) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 511 | 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] | 512 | producer_name, a_first, consumer_name); |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 513 | layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str); |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 514 | a_it++; |
| 515 | } |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 516 | else if (a_at_end || a_first > b_first) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 517 | 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] | 518 | consumer_name, b_first, producer_name); |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 519 | layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str); |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 520 | b_it++; |
| 521 | } |
| 522 | else { |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 523 | if (types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id)) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 524 | /* OK! */ |
Chris Forbes | 1bb5a2e | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 525 | } |
| 526 | else { |
| 527 | char producer_type[1024]; |
| 528 | char consumer_type[1024]; |
| 529 | describe_type(producer_type, producer, a_it->second.type_id); |
| 530 | describe_type(consumer_type, consumer, b_it->second.type_id); |
| 531 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 532 | 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] | 533 | producer_type, consumer_type); |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 534 | layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str); |
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 | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 543 | enum FORMAT_TYPE { |
| 544 | FORMAT_TYPE_UNDEFINED, |
| 545 | FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */ |
| 546 | FORMAT_TYPE_SINT, |
| 547 | FORMAT_TYPE_UINT, |
| 548 | }; |
| 549 | |
| 550 | |
| 551 | static unsigned |
| 552 | get_format_type(VkFormat fmt) { |
| 553 | switch (fmt) { |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 554 | case VK_FORMAT_UNDEFINED: |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 555 | return FORMAT_TYPE_UNDEFINED; |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 556 | case VK_FORMAT_R8_SINT: |
| 557 | case VK_FORMAT_R8G8_SINT: |
| 558 | case VK_FORMAT_R8G8B8_SINT: |
| 559 | case VK_FORMAT_R8G8B8A8_SINT: |
| 560 | case VK_FORMAT_R16_SINT: |
| 561 | case VK_FORMAT_R16G16_SINT: |
| 562 | case VK_FORMAT_R16G16B16_SINT: |
| 563 | case VK_FORMAT_R16G16B16A16_SINT: |
| 564 | case VK_FORMAT_R32_SINT: |
| 565 | case VK_FORMAT_R32G32_SINT: |
| 566 | case VK_FORMAT_R32G32B32_SINT: |
| 567 | case VK_FORMAT_R32G32B32A32_SINT: |
| 568 | case VK_FORMAT_B8G8R8_SINT: |
| 569 | case VK_FORMAT_B8G8R8A8_SINT: |
| 570 | case VK_FORMAT_R10G10B10A2_SINT: |
| 571 | case VK_FORMAT_B10G10R10A2_SINT: |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 572 | return FORMAT_TYPE_SINT; |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 573 | case VK_FORMAT_R8_UINT: |
| 574 | case VK_FORMAT_R8G8_UINT: |
| 575 | case VK_FORMAT_R8G8B8_UINT: |
| 576 | case VK_FORMAT_R8G8B8A8_UINT: |
| 577 | case VK_FORMAT_R16_UINT: |
| 578 | case VK_FORMAT_R16G16_UINT: |
| 579 | case VK_FORMAT_R16G16B16_UINT: |
| 580 | case VK_FORMAT_R16G16B16A16_UINT: |
| 581 | case VK_FORMAT_R32_UINT: |
| 582 | case VK_FORMAT_R32G32_UINT: |
| 583 | case VK_FORMAT_R32G32B32_UINT: |
| 584 | case VK_FORMAT_R32G32B32A32_UINT: |
| 585 | case VK_FORMAT_B8G8R8_UINT: |
| 586 | case VK_FORMAT_B8G8R8A8_UINT: |
| 587 | case VK_FORMAT_R10G10B10A2_UINT: |
| 588 | case VK_FORMAT_B10G10R10A2_UINT: |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 589 | return FORMAT_TYPE_UINT; |
| 590 | default: |
| 591 | return FORMAT_TYPE_FLOAT; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
Chris Forbes | 28c5088 | 2015-05-04 14:04:06 +1200 | [diff] [blame] | 596 | /* characterizes a SPIR-V type appearing in an interface to a FF stage, |
| 597 | * for comparison to a VkFormat's characterization above. */ |
| 598 | static unsigned |
| 599 | get_fundamental_type(shader_source const *src, unsigned type) |
| 600 | { |
| 601 | auto type_def_it = src->type_def_index.find(type); |
| 602 | |
| 603 | if (type_def_it == src->type_def_index.end()) { |
| 604 | return FORMAT_TYPE_UNDEFINED; |
| 605 | } |
| 606 | |
| 607 | unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second]; |
| 608 | unsigned opcode = code[0] & 0x0ffffu; |
| 609 | switch (opcode) { |
| 610 | case spv::OpTypeInt: |
| 611 | return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT; |
| 612 | case spv::OpTypeFloat: |
| 613 | return FORMAT_TYPE_FLOAT; |
| 614 | case spv::OpTypeVector: |
| 615 | return get_fundamental_type(src, code[2]); |
| 616 | case spv::OpTypeMatrix: |
| 617 | return get_fundamental_type(src, code[2]); |
| 618 | case spv::OpTypeArray: |
| 619 | return get_fundamental_type(src, code[2]); |
| 620 | case spv::OpTypePointer: |
| 621 | return get_fundamental_type(src, code[3]); |
| 622 | default: |
| 623 | return FORMAT_TYPE_UNDEFINED; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 628 | static void |
| 629 | validate_vi_against_vs_inputs(VkPipelineVertexInputCreateInfo const *vi, shader_source const *vs) |
| 630 | { |
| 631 | std::map<uint32_t, interface_var> inputs; |
| 632 | /* we collect builtin inputs, but they will never appear in the VI state -- |
| 633 | * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc) |
| 634 | */ |
| 635 | std::map<uint32_t, interface_var> builtin_inputs; |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 636 | char str[1024]; |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 637 | |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 638 | collect_interface_by_location(vs, spv::StorageClassInput, inputs, builtin_inputs); |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 639 | |
| 640 | /* Build index by location */ |
| 641 | std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs; |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 642 | for (unsigned i = 0; i < vi->attributeCount; i++) |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 643 | attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i]; |
| 644 | |
| 645 | auto it_a = attribs.begin(); |
| 646 | auto it_b = inputs.begin(); |
| 647 | |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 648 | while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) { |
| 649 | bool a_at_end = attribs.size() == 0 || it_a == attribs.end(); |
| 650 | bool b_at_end = inputs.size() == 0 || it_b == inputs.end(); |
| 651 | auto a_first = (attribs.size() > 0 ? it_a->first : 0); |
| 652 | auto b_first = (inputs.size() > 0 ? it_b->first : 0); |
| 653 | if (b_at_end || a_first < b_first) { |
| 654 | sprintf(str, "Vertex attribute at location %d not consumed by VS", a_first); |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 655 | layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str); |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 656 | it_a++; |
| 657 | } |
David Pinedo | f5997ab | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 658 | else if (a_at_end || b_first < a_first) { |
| 659 | sprintf(str, "VS consumes input at location %d but not provided", b_first); |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 660 | layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str); |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 661 | it_b++; |
| 662 | } |
| 663 | else { |
Chris Forbes | 3317b38 | 2015-05-04 14:04:24 +1200 | [diff] [blame] | 664 | unsigned attrib_type = get_format_type(it_a->second->format); |
| 665 | unsigned input_type = get_fundamental_type(vs, it_b->second.type_id); |
| 666 | |
| 667 | /* type checking */ |
| 668 | if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) { |
| 669 | char vs_type[1024]; |
| 670 | describe_type(vs_type, vs, it_b->second.type_id); |
| 671 | sprintf(str, "Attribute type of `%s` at location %d does not match VS input type of `%s`", |
| 672 | string_VkFormat(it_a->second->format), a_first, vs_type); |
| 673 | layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str); |
| 674 | } |
| 675 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 676 | /* OK! */ |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 677 | it_a++; |
| 678 | it_b++; |
| 679 | } |
| 680 | } |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 684 | static void |
| 685 | validate_fs_outputs_against_cb(shader_source const *fs, VkPipelineCbStateCreateInfo const *cb) |
| 686 | { |
| 687 | std::map<uint32_t, interface_var> outputs; |
| 688 | std::map<uint32_t, interface_var> builtin_outputs; |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 689 | char str[1024]; |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 690 | |
| 691 | /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */ |
| 692 | |
Cody Northrop | 812b461 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 693 | collect_interface_by_location(fs, spv::StorageClassOutput, outputs, builtin_outputs); |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 694 | |
| 695 | /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs, |
| 696 | * and all color attachment should be UNORM/SNORM/FLOAT. |
| 697 | */ |
| 698 | if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) { |
| 699 | bool broadcast_err = false; |
| 700 | if (outputs.size()) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 701 | layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC", |
| 702 | "Should not have user-defined FS outputs when using broadcast"); |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 703 | broadcast_err = true; |
| 704 | } |
| 705 | |
Ian Elliott | f21f14b | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 706 | for (unsigned i = 0; i < cb->attachmentCount; i++) { |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 707 | unsigned attachmentType = get_format_type(cb->pAttachments[i].format); |
| 708 | if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 709 | layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", |
| 710 | "CB format should not be SINT or UINT when using broadcast"); |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 711 | broadcast_err = true; |
| 712 | } |
| 713 | } |
| 714 | |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 715 | return; |
| 716 | } |
| 717 | |
| 718 | auto it = outputs.begin(); |
| 719 | uint32_t attachment = 0; |
| 720 | |
| 721 | /* Walk attachment list and outputs together -- this is a little overpowered since attachments |
| 722 | * are currently dense, but the parallel with matching between shader stages is nice. |
| 723 | */ |
| 724 | |
Chris Forbes | 8802c99 | 2015-05-05 11:34:14 +1200 | [diff] [blame] | 725 | while ((outputs.size() > 0 && it != outputs.end()) || attachment < cb->attachmentCount) { |
scygan | 7a62cbe | 2015-06-01 19:48:11 +0200 | [diff] [blame] | 726 | if (attachment == cb->attachmentCount || ( it != outputs.end() && it->first < attachment)) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 727 | sprintf(str, "FS writes to output location %d with no matching attachment", it->first); |
| 728 | layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str); |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 729 | it++; |
| 730 | } |
| 731 | else if (it == outputs.end() || it->first > attachment) { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 732 | sprintf(str, "Attachment %d not written by FS", attachment); |
| 733 | layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str); |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 734 | attachment++; |
| 735 | } |
| 736 | else { |
Chris Forbes | 4b00900 | 2015-05-04 14:20:10 +1200 | [diff] [blame] | 737 | unsigned output_type = get_fundamental_type(fs, it->second.type_id); |
| 738 | unsigned att_type = get_format_type(cb->pAttachments[attachment].format); |
| 739 | |
| 740 | /* type checking */ |
| 741 | if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) { |
| 742 | char fs_type[1024]; |
| 743 | describe_type(fs_type, fs, it->second.type_id); |
| 744 | sprintf(str, "Attachment %d of type `%s` does not match FS output type of `%s`", |
| 745 | attachment, string_VkFormat(cb->pAttachments[attachment].format), fs_type); |
| 746 | layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str); |
| 747 | } |
| 748 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 749 | /* OK! */ |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 750 | it++; |
| 751 | attachment++; |
| 752 | } |
| 753 | } |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | |
Chris Forbes | 6054093 | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 757 | VK_LAYER_EXPORT VkResult VKAPI vkCreateGraphicsPipeline(VkDevice device, |
| 758 | const VkGraphicsPipelineCreateInfo *pCreateInfo, |
| 759 | VkPipeline *pPipeline) |
| 760 | { |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 761 | /* TODO: run cross-stage validation for GS, TCS, TES stages */ |
Chris Forbes | 6054093 | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 762 | |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 763 | /* We seem to allow pipeline stages to be specified out of order, so collect and identify them |
| 764 | * before trying to do anything more: */ |
| 765 | |
| 766 | shader_source const *vs_source = 0; |
| 767 | shader_source const *fs_source = 0; |
| 768 | VkPipelineCbStateCreateInfo const *cb = 0; |
| 769 | VkPipelineVertexInputCreateInfo const *vi = 0; |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 770 | char str[1024]; |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 771 | |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 772 | loader_platform_thread_lock_mutex(&globalLock); |
| 773 | |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 774 | for (auto stage = pCreateInfo; stage; stage = (decltype(stage))stage->pNext) { |
| 775 | if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) { |
| 776 | auto shader_stage = (VkPipelineShaderStageCreateInfo const *)stage; |
| 777 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 778 | if (shader_stage->shader.stage == VK_SHADER_STAGE_VERTEX) { |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 779 | vs_source = shader_map[(void *)(shader_stage->shader.shader)]; |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 780 | } |
| 781 | else if (shader_stage->shader.stage == VK_SHADER_STAGE_FRAGMENT) { |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 782 | fs_source = shader_map[(void *)(shader_stage->shader.shader)]; |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 783 | } |
| 784 | else { |
| 785 | sprintf(str, "Unknown shader stage %d\n", shader_stage->shader.stage); |
| 786 | layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC", str); |
| 787 | } |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 788 | } |
| 789 | else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO) { |
| 790 | cb = (VkPipelineCbStateCreateInfo const *)stage; |
| 791 | } |
| 792 | else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO) { |
| 793 | vi = (VkPipelineVertexInputCreateInfo const *)stage; |
| 794 | } |
| 795 | } |
| 796 | |
Chris Forbes | 5c75afe | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 797 | sprintf(str, "Pipeline: vi=%p vs=%p fs=%p cb=%p\n", vi, vs_source, fs_source, cb); |
| 798 | layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_NONE, "SC", str); |
Chris Forbes | 8f60093 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 799 | |
Chris Forbes | fcd05f1 | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 800 | if (vi && vs_source) { |
| 801 | validate_vi_against_vs_inputs(vi, vs_source); |
| 802 | } |
| 803 | |
Chris Forbes | bb164b6 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 804 | if (vs_source && fs_source) { |
| 805 | validate_interface_between_stages(vs_source, "vertex shader", |
| 806 | fs_source, "fragment shader"); |
| 807 | } |
| 808 | |
Chris Forbes | 9b9f5fe | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 809 | if (fs_source && cb) { |
| 810 | validate_fs_outputs_against_cb(fs_source, cb); |
| 811 | } |
| 812 | |
Chris Forbes | 6054093 | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 813 | VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device]; |
| 814 | VkResult res = pTable->CreateGraphicsPipeline(device, pCreateInfo, pPipeline); |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 815 | |
| 816 | loader_platform_thread_unlock_mutex(&globalLock); |
Chris Forbes | 6054093 | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 817 | return res; |
| 818 | } |
| 819 | |
| 820 | |
Chris Forbes | b65ba35 | 2015-05-25 11:12:59 +1200 | [diff] [blame] | 821 | VK_LAYER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback( |
| 822 | VkInstance instance, |
| 823 | VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, |
| 824 | void *pUserData) |
| 825 | { |
| 826 | // This layer intercepts callbacks |
| 827 | VK_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (VK_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(VK_LAYER_DBG_FUNCTION_NODE)); |
| 828 | if (!pNewDbgFuncNode) |
| 829 | return VK_ERROR_OUT_OF_HOST_MEMORY; |
| 830 | pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback; |
| 831 | pNewDbgFuncNode->pUserData = pUserData; |
| 832 | pNewDbgFuncNode->pNext = g_pDbgFunctionHead; |
| 833 | g_pDbgFunctionHead = pNewDbgFuncNode; |
| 834 | // force callbacks if DebugAction hasn't been set already other than initial value |
| 835 | if (g_actionIsDefault) { |
| 836 | g_debugAction = VK_DBG_LAYER_ACTION_CALLBACK; |
| 837 | } |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 838 | // NOT CORRECT WITH MULTIPLE DEVICES OR INSTANCES, BUT THIS IS ALL GOING AWAY SOON ANYWAY |
| 839 | VkLayerDispatchTable *pTable = tableMap[pCurObj]; |
| 840 | VkResult result = pTable->DbgRegisterMsgCallback(instance, pfnMsgCallback, pUserData); |
Chris Forbes | b65ba35 | 2015-05-25 11:12:59 +1200 | [diff] [blame] | 841 | return result; |
| 842 | } |
| 843 | |
| 844 | VK_LAYER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback( |
| 845 | VkInstance instance, |
| 846 | VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback) |
| 847 | { |
| 848 | VK_LAYER_DBG_FUNCTION_NODE *pInfo = g_pDbgFunctionHead; |
| 849 | VK_LAYER_DBG_FUNCTION_NODE *pPrev = pInfo; |
| 850 | while (pInfo) { |
| 851 | if (pInfo->pfnMsgCallback == pfnMsgCallback) { |
| 852 | pPrev->pNext = pInfo->pNext; |
| 853 | if (g_pDbgFunctionHead == pInfo) { |
| 854 | g_pDbgFunctionHead = pInfo->pNext; |
| 855 | } |
| 856 | free(pInfo); |
| 857 | break; |
| 858 | } |
| 859 | pPrev = pInfo; |
| 860 | pInfo = pInfo->pNext; |
| 861 | } |
| 862 | if (g_pDbgFunctionHead == NULL) { |
| 863 | if (g_actionIsDefault) { |
| 864 | g_debugAction = VK_DBG_LAYER_ACTION_LOG_MSG; |
| 865 | } else { |
| 866 | g_debugAction = (VK_LAYER_DBG_ACTION)(g_debugAction & ~((uint32_t)VK_DBG_LAYER_ACTION_CALLBACK)); |
| 867 | } |
| 868 | } |
Chris Forbes | 1ed0f98 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 869 | // NOT CORRECT WITH MULTIPLE DEVICES OR INSTANCES, BUT THIS IS ALL GOING AWAY SOON ANYWAY |
| 870 | VkLayerDispatchTable *pTable = tableMap[pCurObj]; |
| 871 | VkResult result = pTable->DbgUnregisterMsgCallback(instance, pfnMsgCallback); |
Chris Forbes | b65ba35 | 2015-05-25 11:12:59 +1200 | [diff] [blame] | 872 | return result; |
| 873 | } |
| 874 | |
| 875 | |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 876 | VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char* pName) |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 877 | { |
| 878 | if (gpu == NULL) |
| 879 | return NULL; |
| 880 | |
| 881 | initLayerTable((const VkBaseLayerObject *) gpu); |
| 882 | |
Chris Forbes | 1b466bd | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 883 | loader_platform_thread_once(&g_initOnce, initLayer); |
| 884 | |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 885 | #define ADD_HOOK(fn) \ |
| 886 | if (!strncmp(#fn, pName, sizeof(#fn))) \ |
| 887 | return (void *) fn |
| 888 | |
| 889 | ADD_HOOK(vkGetProcAddr); |
| 890 | ADD_HOOK(vkEnumerateLayers); |
| 891 | ADD_HOOK(vkCreateDevice); |
| 892 | ADD_HOOK(vkCreateShader); |
Chris Forbes | 6054093 | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 893 | ADD_HOOK(vkCreateGraphicsPipeline); |
Chris Forbes | b65ba35 | 2015-05-25 11:12:59 +1200 | [diff] [blame] | 894 | ADD_HOOK(vkDbgRegisterMsgCallback); |
| 895 | ADD_HOOK(vkDbgUnregisterMsgCallback); |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 896 | |
| 897 | VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu; |
| 898 | if (gpuw->pGPA == NULL) |
| 899 | return NULL; |
Chia-I Wu | 6097f3a | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 900 | return gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, pName); |
Chris Forbes | aab9d11 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 901 | } |