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