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