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