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