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> |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 31 | #include <string> |
Tobin Ehlis | b80b425 | 2015-09-01 11:59:36 -0600 | [diff] [blame] | 32 | #include <iostream> |
Tobin Ehlis | b835d1b | 2015-07-03 10:34:49 -0600 | [diff] [blame] | 33 | #include "vk_loader_platform.h" |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 34 | #include "vk_dispatch_table_helper.h" |
Tobin Ehlis | 0c6f9ee | 2015-07-03 09:42:57 -0600 | [diff] [blame] | 35 | #include "vk_layer.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 36 | #include "vk_layer_config.h" |
Tobin Ehlis | a0cb02e | 2015-07-03 10:15:26 -0600 | [diff] [blame] | 37 | #include "vk_layer_table.h" |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 38 | #include "vk_layer_logging.h" |
Chris Forbes | 401784b | 2015-05-04 14:04:24 +1200 | [diff] [blame] | 39 | #include "vk_enum_string_helper.h" |
Chris Forbes | 6b2ead6 | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 40 | #include "shader_checker.h" |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 41 | // The following is #included again to catch certain OS-specific functions |
| 42 | // being used: |
Tobin Ehlis | b835d1b | 2015-07-03 10:34:49 -0600 | [diff] [blame] | 43 | #include "vk_loader_platform.h" |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 44 | #include "vk_layer_extension_utils.h" |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 45 | |
GregF | 7c45bed | 2015-07-21 17:22:50 -0600 | [diff] [blame] | 46 | #include "spirv/spirv.hpp" |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 47 | |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 48 | |
Cody Northrop | 55443ef | 2015-09-28 15:09:32 -0600 | [diff] [blame] | 49 | struct layer_data { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 50 | debug_report_data *report_data; |
| 51 | // TODO: put instance data here |
Courtney Goeltzenleuchter | ed12e71 | 2015-10-05 15:59:58 -0600 | [diff] [blame^] | 52 | std::vector<VkDbgMsgCallback> logging_callback; |
Cody Northrop | 55443ef | 2015-09-28 15:09:32 -0600 | [diff] [blame] | 53 | |
| 54 | layer_data() : |
Courtney Goeltzenleuchter | ed12e71 | 2015-10-05 15:59:58 -0600 | [diff] [blame^] | 55 | report_data(nullptr) |
Cody Northrop | 55443ef | 2015-09-28 15:09:32 -0600 | [diff] [blame] | 56 | {}; |
| 57 | }; |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 58 | |
| 59 | static std::unordered_map<void *, layer_data *> layer_data_map; |
| 60 | static device_table_map shader_checker_device_table_map; |
| 61 | static instance_table_map shader_checker_instance_table_map; |
| 62 | |
| 63 | |
| 64 | template layer_data *get_my_data_ptr<layer_data>( |
| 65 | void *data_key, |
| 66 | std::unordered_map<void *, layer_data *> &data_map); |
| 67 | |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 68 | debug_report_data *mdd(void *object) |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 69 | { |
| 70 | dispatch_key key = get_dispatch_key(object); |
| 71 | layer_data *my_data = get_my_data_ptr(key, layer_data_map); |
| 72 | #if DISPATCH_MAP_DEBUG |
| 73 | fprintf(stderr, "MDD: map: %p, object: %p, key: %p, data: %p\n", &layer_data_map, object, key, my_data); |
| 74 | #endif |
| 75 | return my_data->report_data; |
| 76 | } |
| 77 | |
| 78 | debug_report_data *mid(VkInstance object) |
| 79 | { |
| 80 | dispatch_key key = get_dispatch_key(object); |
Tobin Ehlis | bfbac25 | 2015-09-01 11:46:36 -0600 | [diff] [blame] | 81 | layer_data *my_data = get_my_data_ptr(key, layer_data_map); |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 82 | #if DISPATCH_MAP_DEBUG |
| 83 | fprintf(stderr, "MID: map: %p, object: %p, key: %p, data: %p\n", &layer_data_map, object, key, my_data); |
| 84 | #endif |
| 85 | return my_data->report_data; |
| 86 | } |
| 87 | |
Chris Forbes | b6b8c46 | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 88 | static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce); |
Chris Forbes | 7f96383 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 89 | // TODO : This can be much smarter, using separate locks for separate global data |
| 90 | static int globalLockInitialized = 0; |
| 91 | static loader_platform_thread_mutex globalLock; |
Chris Forbes | 3b1c421 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 92 | |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 93 | |
Chris Forbes | 2b8493a | 2015-08-12 15:03:22 +1200 | [diff] [blame] | 94 | std::unordered_map<uint32_t, std::vector<VkDescriptorSetLayoutBinding>*> descriptor_set_layout_map; |
| 95 | |
| 96 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDescriptorSetLayout( |
| 97 | VkDevice device, |
| 98 | const VkDescriptorSetLayoutCreateInfo* pCreateInfo, |
| 99 | VkDescriptorSetLayout* pSetLayout) |
| 100 | { |
| 101 | /* stash a copy of the layout bindings */ |
| 102 | VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, device); |
| 103 | VkResult result = pDeviceTable->CreateDescriptorSetLayout(device, pCreateInfo, pSetLayout); |
| 104 | |
| 105 | if (VK_SUCCESS == result) { |
| 106 | loader_platform_thread_lock_mutex(&globalLock); |
| 107 | auto& bindings = descriptor_set_layout_map[pSetLayout->handle]; |
| 108 | bindings = new std::vector<VkDescriptorSetLayoutBinding>( |
| 109 | pCreateInfo->pBinding, pCreateInfo->pBinding + pCreateInfo->count); |
| 110 | loader_platform_thread_unlock_mutex(&globalLock); |
| 111 | } |
| 112 | |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | std::unordered_map<uint32_t, std::vector<std::vector<VkDescriptorSetLayoutBinding>*>*> pipeline_layout_map; |
| 118 | |
| 119 | VK_LAYER_EXPORT VkResult VKAPI vkCreatePipelineLayout( |
| 120 | VkDevice device, |
| 121 | const VkPipelineLayoutCreateInfo* pCreateInfo, |
| 122 | VkPipelineLayout* pPipelineLayout) |
| 123 | { |
| 124 | VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, device); |
| 125 | VkResult result = pDeviceTable->CreatePipelineLayout(device, pCreateInfo, pPipelineLayout); |
| 126 | |
| 127 | if (VK_SUCCESS == result) { |
| 128 | loader_platform_thread_lock_mutex(&globalLock); |
| 129 | auto& layouts = pipeline_layout_map[pPipelineLayout->handle]; |
| 130 | layouts = new std::vector<std::vector<VkDescriptorSetLayoutBinding>*>(); |
| 131 | layouts->reserve(pCreateInfo->descriptorSetCount); |
| 132 | for (unsigned i = 0; i < pCreateInfo->descriptorSetCount; i++) { |
| 133 | layouts->push_back(descriptor_set_layout_map[pCreateInfo->pSetLayouts[i].handle]); |
| 134 | } |
| 135 | loader_platform_thread_unlock_mutex(&globalLock); |
| 136 | } |
| 137 | |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 142 | static void |
| 143 | build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index) |
| 144 | { |
| 145 | unsigned int const *code = (unsigned int const *)&words[0]; |
| 146 | size_t size = words.size(); |
| 147 | |
| 148 | unsigned word = 5; |
| 149 | while (word < size) { |
| 150 | unsigned opcode = code[word] & 0x0ffffu; |
| 151 | unsigned oplen = (code[word] & 0xffff0000u) >> 16; |
| 152 | |
| 153 | switch (opcode) { |
| 154 | case spv::OpTypeVoid: |
| 155 | case spv::OpTypeBool: |
| 156 | case spv::OpTypeInt: |
| 157 | case spv::OpTypeFloat: |
| 158 | case spv::OpTypeVector: |
| 159 | case spv::OpTypeMatrix: |
GregF | 7c45bed | 2015-07-21 17:22:50 -0600 | [diff] [blame] | 160 | case spv::OpTypeImage: |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 161 | case spv::OpTypeSampler: |
GregF | 7c45bed | 2015-07-21 17:22:50 -0600 | [diff] [blame] | 162 | case spv::OpTypeSampledImage: |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 163 | case spv::OpTypeArray: |
| 164 | case spv::OpTypeRuntimeArray: |
| 165 | case spv::OpTypeStruct: |
| 166 | case spv::OpTypeOpaque: |
| 167 | case spv::OpTypePointer: |
| 168 | case spv::OpTypeFunction: |
| 169 | case spv::OpTypeEvent: |
| 170 | case spv::OpTypeDeviceEvent: |
| 171 | case spv::OpTypeReserveId: |
| 172 | case spv::OpTypeQueue: |
| 173 | case spv::OpTypePipe: |
| 174 | type_def_index[code[word+1]] = word; |
| 175 | break; |
| 176 | |
| 177 | default: |
| 178 | /* We only care about type definitions */ |
| 179 | break; |
| 180 | } |
| 181 | |
| 182 | word += oplen; |
| 183 | } |
| 184 | } |
| 185 | |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 186 | |
| 187 | bool |
| 188 | shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo) |
| 189 | { |
| 190 | uint32_t *words = (uint32_t *)pCreateInfo->pCode; |
| 191 | uint32_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t); |
| 192 | |
| 193 | /* Just validate that the header makes sense. */ |
| 194 | return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version; |
| 195 | } |
| 196 | |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 197 | struct shader_module { |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 198 | /* the spirv image itself */ |
Chris Forbes | 3b1c421 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 199 | std::vector<uint32_t> words; |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 200 | /* a mapping of <id> to the first word of its def. this is useful because walking type |
| 201 | * trees requires jumping all over the instruction stream. |
| 202 | */ |
| 203 | std::unordered_map<unsigned, unsigned> type_def_index; |
Chris Forbes | 3b1c421 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 204 | |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 205 | shader_module(VkShaderModuleCreateInfo const *pCreateInfo) : |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 206 | words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)), |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 207 | type_def_index() { |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 208 | |
| 209 | build_type_def_index(words, type_def_index); |
Chris Forbes | 3b1c421 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 210 | } |
| 211 | }; |
| 212 | |
| 213 | |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 214 | static std::unordered_map<uint64_t, shader_module *> shader_module_map; |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 215 | |
| 216 | struct shader_object { |
| 217 | std::string name; |
| 218 | struct shader_module *module; |
| 219 | |
| 220 | shader_object(VkShaderCreateInfo const *pCreateInfo) |
| 221 | { |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 222 | module = shader_module_map[pCreateInfo->module.handle]; |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 223 | name = pCreateInfo->pName; |
| 224 | } |
| 225 | }; |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 226 | static std::unordered_map<uint64_t, shader_object *> shader_object_map; |
Chris Forbes | 3b1c421 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 227 | |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 228 | struct render_pass { |
| 229 | std::vector<std::vector<VkFormat>> subpass_color_formats; |
| 230 | |
| 231 | render_pass(VkRenderPassCreateInfo const *pCreateInfo) |
| 232 | { |
| 233 | uint32_t i; |
| 234 | |
| 235 | subpass_color_formats.reserve(pCreateInfo->subpassCount); |
| 236 | for (i = 0; i < pCreateInfo->subpassCount; i++) { |
| 237 | const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i]; |
| 238 | std::vector<VkFormat> color_formats; |
| 239 | uint32_t j; |
| 240 | |
| 241 | color_formats.reserve(subpass->colorCount); |
| 242 | for (j = 0; j < subpass->colorCount; j++) { |
Cody Northrop | a505dda | 2015-08-04 11:16:41 -0600 | [diff] [blame] | 243 | const uint32_t att = subpass->pColorAttachments[j].attachment; |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 244 | const VkFormat format = pCreateInfo->pAttachments[att].format; |
| 245 | |
| 246 | color_formats.push_back(pCreateInfo->pAttachments[att].format); |
| 247 | } |
| 248 | |
| 249 | subpass_color_formats.push_back(color_formats); |
| 250 | } |
| 251 | } |
| 252 | }; |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 253 | static std::unordered_map<uint64_t, render_pass *> render_pass_map; |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 254 | |
Chris Forbes | 3b1c421 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 255 | |
Chris Forbes | b6b8c46 | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 256 | static void |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 257 | init_shader_checker(layer_data *my_data) |
Chris Forbes | b6b8c46 | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 258 | { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 259 | uint32_t report_flags = 0; |
| 260 | uint32_t debug_action = 0; |
| 261 | FILE *log_output = NULL; |
| 262 | const char *option_str; |
Courtney Goeltzenleuchter | ed12e71 | 2015-10-05 15:59:58 -0600 | [diff] [blame^] | 263 | VkDbgMsgCallback callback; |
Chris Forbes | b6b8c46 | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 264 | // initialize ShaderChecker options |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 265 | report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0); |
| 266 | getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action); |
Chris Forbes | b6b8c46 | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 267 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 268 | if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG) |
Chris Forbes | b6b8c46 | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 269 | { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 270 | option_str = getLayerOption("ShaderCheckerLogFilename"); |
Tobin Ehlis | b1df55e | 2015-09-15 09:55:54 -0600 | [diff] [blame] | 271 | log_output = getLayerLogOutput(option_str, "ShaderChecker"); |
Courtney Goeltzenleuchter | ed12e71 | 2015-10-05 15:59:58 -0600 | [diff] [blame^] | 272 | layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &callback); |
| 273 | my_data->logging_callback.push_back(callback); |
| 274 | } |
| 275 | |
| 276 | if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) { |
| 277 | layer_create_msg_callback(my_data->report_data, report_flags, win32_debug_output_msg, NULL, &callback); |
| 278 | my_data->logging_callback.push_back(callback); |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (!globalLockInitialized) |
| 282 | { |
| 283 | // TODO/TBD: Need to delete this mutex sometime. How??? One |
| 284 | // suggestion is to call this during vkCreateInstance(), and then we |
| 285 | // can clean it up during vkDestroyInstance(). However, that requires |
| 286 | // that the layer have per-instance locks. We need to come back and |
| 287 | // address this soon. |
| 288 | loader_platform_thread_create_mutex(&globalLock); |
| 289 | globalLockInitialized = 1; |
Chris Forbes | b6b8c46 | 2015-04-15 06:59:41 +1200 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 293 | static const VkLayerProperties shader_checker_global_layers[] = { |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 294 | { |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 295 | "ShaderChecker", |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 296 | VK_API_VERSION, |
| 297 | VK_MAKE_VERSION(0, 1, 0), |
| 298 | "Validation layer: ShaderChecker", |
Courtney Goeltzenleuchter | f579fa6 | 2015-06-10 17:39:03 -0600 | [diff] [blame] | 299 | } |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 300 | }; |
| 301 | |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 302 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties( |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 303 | const char *pLayerName, |
| 304 | uint32_t *pCount, |
| 305 | VkExtensionProperties* pProperties) |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 306 | { |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 307 | /* shader checker does not have any global extensions */ |
| 308 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 309 | } |
| 310 | |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 311 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties( |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 312 | uint32_t *pCount, |
| 313 | VkLayerProperties* pProperties) |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 314 | { |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 315 | return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers), |
| 316 | shader_checker_global_layers, |
| 317 | pCount, pProperties); |
Tony Barbour | 59a4732 | 2015-06-24 16:06:58 -0600 | [diff] [blame] | 318 | } |
| 319 | |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 320 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties( |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 321 | VkPhysicalDevice physicalDevice, |
| 322 | const char* pLayerName, |
| 323 | uint32_t* pCount, |
| 324 | VkExtensionProperties* pProperties) |
Jon Ashburn | 207a3af | 2015-06-10 16:43:31 -0600 | [diff] [blame] | 325 | { |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 326 | /* Shader checker does not have any physical device extensions */ |
| 327 | return util_GetExtensionProperties(0, NULL, pCount, pProperties); |
| 328 | } |
Jon Ashburn | 207a3af | 2015-06-10 16:43:31 -0600 | [diff] [blame] | 329 | |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 330 | VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties( |
Courtney Goeltzenleuchter | da8adfe | 2015-07-07 10:05:05 -0600 | [diff] [blame] | 331 | VkPhysicalDevice physicalDevice, |
| 332 | uint32_t* pCount, |
| 333 | VkLayerProperties* pProperties) |
| 334 | { |
| 335 | /* Shader checker physical device layers are the same as global */ |
| 336 | return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers), |
| 337 | shader_checker_global_layers, |
| 338 | pCount, pProperties); |
Jon Ashburn | 207a3af | 2015-06-10 16:43:31 -0600 | [diff] [blame] | 339 | } |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 340 | |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 341 | static char const * |
| 342 | storage_class_name(unsigned sc) |
| 343 | { |
| 344 | switch (sc) { |
Cody Northrop | 97e52d8 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 345 | case spv::StorageClassInput: return "input"; |
| 346 | case spv::StorageClassOutput: return "output"; |
| 347 | case spv::StorageClassUniformConstant: return "const uniform"; |
| 348 | case spv::StorageClassUniform: return "uniform"; |
| 349 | case spv::StorageClassWorkgroupLocal: return "workgroup local"; |
| 350 | case spv::StorageClassWorkgroupGlobal: return "workgroup global"; |
| 351 | case spv::StorageClassPrivateGlobal: return "private global"; |
| 352 | case spv::StorageClassFunction: return "function"; |
| 353 | case spv::StorageClassGeneric: return "generic"; |
Cody Northrop | 97e52d8 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 354 | case spv::StorageClassAtomicCounter: return "atomic counter"; |
GregF | 7c45bed | 2015-07-21 17:22:50 -0600 | [diff] [blame] | 355 | case spv::StorageClassImage: return "image"; |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 356 | default: return "unknown"; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
| 361 | /* returns ptr to null terminator */ |
| 362 | static char * |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 363 | describe_type(char *dst, shader_module const *src, unsigned type) |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 364 | { |
| 365 | auto type_def_it = src->type_def_index.find(type); |
| 366 | |
| 367 | if (type_def_it == src->type_def_index.end()) { |
| 368 | return dst + sprintf(dst, "undef"); |
| 369 | } |
| 370 | |
| 371 | unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second]; |
| 372 | unsigned opcode = code[0] & 0x0ffffu; |
| 373 | switch (opcode) { |
| 374 | case spv::OpTypeBool: |
| 375 | return dst + sprintf(dst, "bool"); |
| 376 | case spv::OpTypeInt: |
| 377 | return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]); |
| 378 | case spv::OpTypeFloat: |
| 379 | return dst + sprintf(dst, "float%d", code[2]); |
| 380 | case spv::OpTypeVector: |
| 381 | dst += sprintf(dst, "vec%d of ", code[3]); |
| 382 | return describe_type(dst, src, code[2]); |
| 383 | case spv::OpTypeMatrix: |
| 384 | dst += sprintf(dst, "mat%d of ", code[3]); |
| 385 | return describe_type(dst, src, code[2]); |
| 386 | case spv::OpTypeArray: |
| 387 | dst += sprintf(dst, "arr[%d] of ", code[3]); |
| 388 | return describe_type(dst, src, code[2]); |
| 389 | case spv::OpTypePointer: |
| 390 | dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2])); |
| 391 | return describe_type(dst, src, code[3]); |
| 392 | case spv::OpTypeStruct: |
| 393 | { |
| 394 | unsigned oplen = code[0] >> 16; |
| 395 | dst += sprintf(dst, "struct of ("); |
Ian Elliott | 1cb6222 | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 396 | for (unsigned i = 2; i < oplen; i++) { |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 397 | dst = describe_type(dst, src, code[i]); |
| 398 | dst += sprintf(dst, i == oplen-1 ? ")" : ", "); |
| 399 | } |
| 400 | return dst; |
| 401 | } |
Chris Forbes | 0ae1580 | 2015-08-14 12:04:39 +1200 | [diff] [blame] | 402 | case spv::OpTypeSampler: |
| 403 | return dst + sprintf(dst, "sampler"); |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 404 | default: |
| 405 | return dst + sprintf(dst, "oddtype"); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | |
| 410 | static bool |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 411 | types_match(shader_module const *a, shader_module const *b, unsigned a_type, unsigned b_type, bool b_arrayed) |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 412 | { |
| 413 | auto a_type_def_it = a->type_def_index.find(a_type); |
| 414 | auto b_type_def_it = b->type_def_index.find(b_type); |
| 415 | |
| 416 | if (a_type_def_it == a->type_def_index.end()) { |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 417 | return false; |
| 418 | } |
| 419 | |
| 420 | if (b_type_def_it == b->type_def_index.end()) { |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 421 | return false; |
| 422 | } |
| 423 | |
| 424 | /* walk two type trees together, and complain about differences */ |
| 425 | unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second]; |
| 426 | unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second]; |
| 427 | |
| 428 | unsigned a_opcode = a_code[0] & 0x0ffffu; |
| 429 | unsigned b_opcode = b_code[0] & 0x0ffffu; |
| 430 | |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 431 | if (b_arrayed && b_opcode == spv::OpTypeArray) { |
| 432 | /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */ |
| 433 | return types_match(a, b, a_type, b_code[2], false); |
| 434 | } |
| 435 | |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 436 | if (a_opcode != b_opcode) { |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | |
| 440 | switch (a_opcode) { |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 441 | /* 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] | 442 | case spv::OpTypeBool: |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 443 | return true && !b_arrayed; |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 444 | case spv::OpTypeInt: |
| 445 | /* match on width, signedness */ |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 446 | 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] | 447 | case spv::OpTypeFloat: |
| 448 | /* match on width */ |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 449 | return a_code[2] == b_code[2] && !b_arrayed; |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 450 | case spv::OpTypeVector: |
| 451 | case spv::OpTypeMatrix: |
| 452 | case spv::OpTypeArray: |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 453 | /* match on element type, count. these all have the same layout. we don't get here if |
| 454 | * b_arrayed -- that is handled above. */ |
| 455 | 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] | 456 | case spv::OpTypeStruct: |
| 457 | /* match on all element types */ |
| 458 | { |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 459 | if (b_arrayed) { |
| 460 | /* for the purposes of matching different levels of arrayness, structs are leaves. */ |
| 461 | return false; |
| 462 | } |
| 463 | |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 464 | unsigned a_len = a_code[0] >> 16; |
| 465 | unsigned b_len = b_code[0] >> 16; |
| 466 | |
| 467 | if (a_len != b_len) { |
| 468 | return false; /* structs cannot match if member counts differ */ |
| 469 | } |
| 470 | |
Ian Elliott | 1cb6222 | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 471 | for (unsigned i = 2; i < a_len; i++) { |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 472 | 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] | 473 | return false; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | return true; |
| 478 | } |
| 479 | case spv::OpTypePointer: |
| 480 | /* match on pointee type. storage class is expected to differ */ |
Chris Forbes | f3fc033 | 2015-06-05 14:57:05 +1200 | [diff] [blame] | 481 | 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] | 482 | |
| 483 | default: |
| 484 | /* remaining types are CLisms, or may not appear in the interfaces we |
| 485 | * are interested in. Just claim no match. |
| 486 | */ |
| 487 | return false; |
| 488 | |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | |
Chris Forbes | 06e8fc3 | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 493 | static int |
| 494 | value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def) |
| 495 | { |
| 496 | auto it = map.find(id); |
| 497 | if (it == map.end()) |
| 498 | return def; |
| 499 | else |
| 500 | return it->second; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | struct interface_var { |
| 505 | uint32_t id; |
| 506 | uint32_t type_id; |
| 507 | /* TODO: collect the name, too? Isn't required to be present. */ |
| 508 | }; |
| 509 | |
| 510 | |
| 511 | static void |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 512 | collect_interface_by_location(VkDevice dev, |
| 513 | shader_module const *src, spv::StorageClass sinterface, |
Chris Forbes | 06e8fc3 | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 514 | std::map<uint32_t, interface_var> &out, |
| 515 | std::map<uint32_t, interface_var> &builtins_out) |
| 516 | { |
| 517 | unsigned int const *code = (unsigned int const *)&src->words[0]; |
| 518 | size_t size = src->words.size(); |
| 519 | |
Chris Forbes | 06e8fc3 | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 520 | std::unordered_map<unsigned, unsigned> var_locations; |
| 521 | std::unordered_map<unsigned, unsigned> var_builtins; |
| 522 | |
| 523 | unsigned word = 5; |
| 524 | while (word < size) { |
| 525 | |
| 526 | unsigned opcode = code[word] & 0x0ffffu; |
| 527 | unsigned oplen = (code[word] & 0xffff0000u) >> 16; |
| 528 | |
| 529 | /* We consider two interface models: SSO rendezvous-by-location, and |
| 530 | * builtins. Complain about anything that fits neither model. |
| 531 | */ |
| 532 | if (opcode == spv::OpDecorate) { |
Cody Northrop | 97e52d8 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 533 | if (code[word+2] == spv::DecorationLocation) { |
Chris Forbes | 06e8fc3 | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 534 | var_locations[code[word+1]] = code[word+3]; |
| 535 | } |
| 536 | |
Cody Northrop | 97e52d8 | 2015-04-20 14:09:40 -0600 | [diff] [blame] | 537 | if (code[word+2] == spv::DecorationBuiltIn) { |
Chris Forbes | 06e8fc3 | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 538 | var_builtins[code[word+1]] = code[word+3]; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /* TODO: handle grouped decorations */ |
| 543 | /* TODO: handle index=1 dual source outputs from FS -- two vars will |
| 544 | * have the same location, and we DONT want to clobber. */ |
| 545 | |
Ian Elliott | 1cb6222 | 2015-04-17 11:05:04 -0600 | [diff] [blame] | 546 | if (opcode == spv::OpVariable && code[word+3] == sinterface) { |
Chris Forbes | 06e8fc3 | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 547 | int location = value_or_default(var_locations, code[word+2], -1); |
| 548 | int builtin = value_or_default(var_builtins, code[word+2], -1); |
| 549 | |
| 550 | if (location == -1 && builtin == -1) { |
| 551 | /* No location defined, and not bound to an API builtin. |
| 552 | * The spec says nothing about how this case works (or doesn't) |
| 553 | * for interface matching. |
| 554 | */ |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 555 | log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 556 | "var %d (type %d) in %s interface has no Location or Builtin decoration", |
| 557 | code[word+2], code[word+1], storage_class_name(sinterface)); |
Chris Forbes | 06e8fc3 | 2015-04-13 12:14:52 +1200 | [diff] [blame] | 558 | } |
| 559 | else if (location != -1) { |
| 560 | /* A user-defined interface variable, with a location. */ |
| 561 | interface_var v; |
| 562 | v.id = code[word+2]; |
| 563 | v.type_id = code[word+1]; |
| 564 | out[location] = v; |
| 565 | } |
| 566 | else { |
| 567 | /* A builtin interface variable */ |
| 568 | interface_var v; |
| 569 | v.id = code[word+2]; |
| 570 | v.type_id = code[word+1]; |
| 571 | builtins_out[builtin] = v; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | word += oplen; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | |
Chris Forbes | 0ae1580 | 2015-08-14 12:04:39 +1200 | [diff] [blame] | 580 | static void |
| 581 | collect_interface_by_descriptor_slot(VkDevice dev, |
| 582 | shader_module const *src, spv::StorageClass sinterface, |
| 583 | std::map<std::pair<unsigned, unsigned>, interface_var> &out) |
| 584 | { |
| 585 | unsigned int const *code = (unsigned int const *)&src->words[0]; |
| 586 | size_t size = src->words.size(); |
| 587 | |
| 588 | std::unordered_map<unsigned, unsigned> var_sets; |
| 589 | std::unordered_map<unsigned, unsigned> var_bindings; |
| 590 | |
| 591 | unsigned word = 5; |
| 592 | while (word < size) { |
| 593 | |
| 594 | unsigned opcode = code[word] & 0x0ffffu; |
| 595 | unsigned oplen = (code[word] & 0xffff0000u) >> 16; |
| 596 | |
| 597 | /* We consider two interface models: SSO rendezvous-by-location, and |
| 598 | * builtins. Complain about anything that fits neither model. |
| 599 | */ |
| 600 | if (opcode == spv::OpDecorate) { |
| 601 | if (code[word+2] == spv::DecorationDescriptorSet) { |
| 602 | var_sets[code[word+1]] = code[word+3]; |
| 603 | } |
| 604 | |
| 605 | if (code[word+2] == spv::DecorationBinding) { |
| 606 | var_bindings[code[word+1]] = code[word+3]; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | if (opcode == spv::OpVariable && (code[word+3] == spv::StorageClassUniform || |
| 611 | code[word+3] == spv::StorageClassUniformConstant)) { |
| 612 | unsigned set = value_or_default(var_sets, code[word+2], 0); |
| 613 | unsigned binding = value_or_default(var_bindings, code[word+2], 0); |
| 614 | |
| 615 | auto existing_it = out.find(std::make_pair(set, binding)); |
| 616 | if (existing_it != out.end()) { |
| 617 | /* conflict within spv image */ |
| 618 | log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, |
| 619 | SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", |
| 620 | "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition", |
| 621 | code[word+2], code[word+1], storage_class_name(sinterface), |
| 622 | existing_it->first.first, existing_it->first.second); |
| 623 | } |
| 624 | |
| 625 | interface_var v; |
| 626 | v.id = code[word+2]; |
| 627 | v.type_id = code[word+1]; |
| 628 | out[std::make_pair(set, binding)] = v; |
| 629 | } |
| 630 | |
| 631 | word += oplen; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 636 | VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule( |
| 637 | VkDevice device, |
| 638 | const VkShaderModuleCreateInfo *pCreateInfo, |
| 639 | VkShaderModule *pShaderModule) |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 640 | { |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 641 | /* Protect the driver from non-SPIRV shaders */ |
| 642 | if (!shader_is_spirv(pCreateInfo)) { |
| 643 | log_msg(mdd(device), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, |
| 644 | /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC", |
| 645 | "Shader is not SPIR-V"); |
| 646 | return VK_ERROR_VALIDATION_FAILED; |
| 647 | } |
| 648 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 649 | VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShaderModule(device, pCreateInfo, pShaderModule); |
Chris Forbes | 3b1c421 | 2015-04-08 10:11:59 +1200 | [diff] [blame] | 650 | |
Courtney Goeltzenleuchter | b5afdbf | 2015-09-16 12:57:55 -0600 | [diff] [blame] | 651 | if (res == VK_SUCCESS) { |
| 652 | loader_platform_thread_lock_mutex(&globalLock); |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 653 | shader_module_map[pShaderModule->handle] = new shader_module(pCreateInfo); |
Courtney Goeltzenleuchter | b5afdbf | 2015-09-16 12:57:55 -0600 | [diff] [blame] | 654 | loader_platform_thread_unlock_mutex(&globalLock); |
| 655 | } |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 656 | return res; |
| 657 | } |
| 658 | |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 659 | VK_LAYER_EXPORT VkResult VKAPI vkCreateShader( |
| 660 | VkDevice device, |
| 661 | const VkShaderCreateInfo *pCreateInfo, |
| 662 | VkShader *pShader) |
| 663 | { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 664 | VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShader(device, pCreateInfo, pShader); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 665 | |
Courtney Goeltzenleuchter | b5afdbf | 2015-09-16 12:57:55 -0600 | [diff] [blame] | 666 | loader_platform_thread_lock_mutex(&globalLock); |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 667 | shader_object_map[pShader->handle] = new shader_object(pCreateInfo); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 668 | loader_platform_thread_unlock_mutex(&globalLock); |
| 669 | return res; |
| 670 | } |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 671 | |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 672 | VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass( |
| 673 | VkDevice device, |
| 674 | const VkRenderPassCreateInfo *pCreateInfo, |
| 675 | VkRenderPass *pRenderPass) |
| 676 | { |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 677 | VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateRenderPass(device, pCreateInfo, pRenderPass); |
| 678 | |
Courtney Goeltzenleuchter | b5afdbf | 2015-09-16 12:57:55 -0600 | [diff] [blame] | 679 | loader_platform_thread_lock_mutex(&globalLock); |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 680 | render_pass_map[pRenderPass->handle] = new render_pass(pCreateInfo); |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 681 | loader_platform_thread_unlock_mutex(&globalLock); |
| 682 | return res; |
| 683 | } |
| 684 | |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 685 | static bool |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 686 | validate_interface_between_stages(VkDevice dev, |
| 687 | shader_module const *producer, char const *producer_name, |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 688 | shader_module const *consumer, char const *consumer_name, |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 689 | bool consumer_arrayed_input) |
Chris Forbes | 4100245 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 690 | { |
| 691 | std::map<uint32_t, interface_var> outputs; |
| 692 | std::map<uint32_t, interface_var> inputs; |
| 693 | |
| 694 | std::map<uint32_t, interface_var> builtin_outputs; |
| 695 | std::map<uint32_t, interface_var> builtin_inputs; |
| 696 | |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 697 | bool pass = true; |
Chris Forbes | 4100245 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 698 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 699 | collect_interface_by_location(dev, producer, spv::StorageClassOutput, outputs, builtin_outputs); |
| 700 | collect_interface_by_location(dev, consumer, spv::StorageClassInput, inputs, builtin_inputs); |
Chris Forbes | 4100245 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 701 | |
| 702 | auto a_it = outputs.begin(); |
| 703 | auto b_it = inputs.begin(); |
| 704 | |
| 705 | /* maps sorted by key (location); walk them together to find mismatches */ |
David Pinedo | d8f83d8 | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 706 | while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) { |
| 707 | bool a_at_end = outputs.size() == 0 || a_it == outputs.end(); |
| 708 | bool b_at_end = inputs.size() == 0 || b_it == inputs.end(); |
Chris Forbes | 62cc3fc | 2015-06-10 08:37:27 +1200 | [diff] [blame] | 709 | auto a_first = a_at_end ? 0 : a_it->first; |
| 710 | auto b_first = b_at_end ? 0 : b_it->first; |
David Pinedo | d8f83d8 | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 711 | |
Mike Stroyan | 19a6de2 | 2015-09-10 14:10:25 -0600 | [diff] [blame] | 712 | if (b_at_end || ((!a_at_end) && (a_first < b_first))) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 713 | if (log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", |
| 714 | "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) { |
| 715 | pass = false; |
| 716 | } |
Chris Forbes | 4100245 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 717 | a_it++; |
| 718 | } |
David Pinedo | d8f83d8 | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 719 | else if (a_at_end || a_first > b_first) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 720 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", |
| 721 | "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) { |
| 722 | pass = false; |
| 723 | } |
Chris Forbes | 4100245 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 724 | b_it++; |
| 725 | } |
| 726 | else { |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 727 | 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] | 728 | /* OK! */ |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 729 | } |
| 730 | else { |
| 731 | char producer_type[1024]; |
| 732 | char consumer_type[1024]; |
| 733 | describe_type(producer_type, producer, a_it->second.type_id); |
| 734 | describe_type(consumer_type, consumer, b_it->second.type_id); |
| 735 | |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 736 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", |
| 737 | "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) { |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 738 | pass = false; |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 739 | } |
Chris Forbes | 3a5e99a | 2015-04-10 11:41:20 +1200 | [diff] [blame] | 740 | } |
Chris Forbes | 4100245 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 741 | a_it++; |
| 742 | b_it++; |
| 743 | } |
| 744 | } |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 745 | |
| 746 | return pass; |
Chris Forbes | 4100245 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 750 | enum FORMAT_TYPE { |
| 751 | FORMAT_TYPE_UNDEFINED, |
| 752 | FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */ |
| 753 | FORMAT_TYPE_SINT, |
| 754 | FORMAT_TYPE_UINT, |
| 755 | }; |
| 756 | |
| 757 | |
| 758 | static unsigned |
| 759 | get_format_type(VkFormat fmt) { |
| 760 | switch (fmt) { |
Chia-I Wu | a3b9a20 | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 761 | case VK_FORMAT_UNDEFINED: |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 762 | return FORMAT_TYPE_UNDEFINED; |
Chia-I Wu | a3b9a20 | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 763 | case VK_FORMAT_R8_SINT: |
| 764 | case VK_FORMAT_R8G8_SINT: |
| 765 | case VK_FORMAT_R8G8B8_SINT: |
| 766 | case VK_FORMAT_R8G8B8A8_SINT: |
| 767 | case VK_FORMAT_R16_SINT: |
| 768 | case VK_FORMAT_R16G16_SINT: |
| 769 | case VK_FORMAT_R16G16B16_SINT: |
| 770 | case VK_FORMAT_R16G16B16A16_SINT: |
| 771 | case VK_FORMAT_R32_SINT: |
| 772 | case VK_FORMAT_R32G32_SINT: |
| 773 | case VK_FORMAT_R32G32B32_SINT: |
| 774 | case VK_FORMAT_R32G32B32A32_SINT: |
| 775 | case VK_FORMAT_B8G8R8_SINT: |
| 776 | case VK_FORMAT_B8G8R8A8_SINT: |
| 777 | case VK_FORMAT_R10G10B10A2_SINT: |
| 778 | case VK_FORMAT_B10G10R10A2_SINT: |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 779 | return FORMAT_TYPE_SINT; |
Chia-I Wu | a3b9a20 | 2015-04-17 02:00:54 +0800 | [diff] [blame] | 780 | case VK_FORMAT_R8_UINT: |
| 781 | case VK_FORMAT_R8G8_UINT: |
| 782 | case VK_FORMAT_R8G8B8_UINT: |
| 783 | case VK_FORMAT_R8G8B8A8_UINT: |
| 784 | case VK_FORMAT_R16_UINT: |
| 785 | case VK_FORMAT_R16G16_UINT: |
| 786 | case VK_FORMAT_R16G16B16_UINT: |
| 787 | case VK_FORMAT_R16G16B16A16_UINT: |
| 788 | case VK_FORMAT_R32_UINT: |
| 789 | case VK_FORMAT_R32G32_UINT: |
| 790 | case VK_FORMAT_R32G32B32_UINT: |
| 791 | case VK_FORMAT_R32G32B32A32_UINT: |
| 792 | case VK_FORMAT_B8G8R8_UINT: |
| 793 | case VK_FORMAT_B8G8R8A8_UINT: |
| 794 | case VK_FORMAT_R10G10B10A2_UINT: |
| 795 | case VK_FORMAT_B10G10R10A2_UINT: |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 796 | return FORMAT_TYPE_UINT; |
| 797 | default: |
| 798 | return FORMAT_TYPE_FLOAT; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | |
Chris Forbes | 156a116 | 2015-05-04 14:04:06 +1200 | [diff] [blame] | 803 | /* characterizes a SPIR-V type appearing in an interface to a FF stage, |
| 804 | * for comparison to a VkFormat's characterization above. */ |
| 805 | static unsigned |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 806 | get_fundamental_type(shader_module const *src, unsigned type) |
Chris Forbes | 156a116 | 2015-05-04 14:04:06 +1200 | [diff] [blame] | 807 | { |
| 808 | auto type_def_it = src->type_def_index.find(type); |
| 809 | |
| 810 | if (type_def_it == src->type_def_index.end()) { |
| 811 | return FORMAT_TYPE_UNDEFINED; |
| 812 | } |
| 813 | |
| 814 | unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second]; |
| 815 | unsigned opcode = code[0] & 0x0ffffu; |
| 816 | switch (opcode) { |
| 817 | case spv::OpTypeInt: |
| 818 | return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT; |
| 819 | case spv::OpTypeFloat: |
| 820 | return FORMAT_TYPE_FLOAT; |
| 821 | case spv::OpTypeVector: |
| 822 | return get_fundamental_type(src, code[2]); |
| 823 | case spv::OpTypeMatrix: |
| 824 | return get_fundamental_type(src, code[2]); |
| 825 | case spv::OpTypeArray: |
| 826 | return get_fundamental_type(src, code[2]); |
| 827 | case spv::OpTypePointer: |
| 828 | return get_fundamental_type(src, code[3]); |
| 829 | default: |
| 830 | return FORMAT_TYPE_UNDEFINED; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 835 | static bool |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 836 | validate_vi_consistency(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi) |
Chris Forbes | 280ba2c | 2015-06-12 11:16:41 +1200 | [diff] [blame] | 837 | { |
| 838 | /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer. |
| 839 | * each binding should be specified only once. |
| 840 | */ |
| 841 | std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings; |
Chris Forbes | 280ba2c | 2015-06-12 11:16:41 +1200 | [diff] [blame] | 842 | bool pass = true; |
| 843 | |
| 844 | for (unsigned i = 0; i < vi->bindingCount; i++) { |
| 845 | auto desc = &vi->pVertexBindingDescriptions[i]; |
| 846 | auto & binding = bindings[desc->binding]; |
| 847 | if (binding) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 848 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INCONSISTENT_VI, "SC", |
| 849 | "Duplicate vertex input binding descriptions for binding %d", desc->binding)) { |
| 850 | pass = false; |
| 851 | } |
Chris Forbes | 280ba2c | 2015-06-12 11:16:41 +1200 | [diff] [blame] | 852 | } |
| 853 | else { |
| 854 | binding = desc; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | return pass; |
| 859 | } |
| 860 | |
| 861 | |
| 862 | static bool |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 863 | validate_vi_against_vs_inputs(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs) |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 864 | { |
| 865 | std::map<uint32_t, interface_var> inputs; |
| 866 | /* we collect builtin inputs, but they will never appear in the VI state -- |
| 867 | * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc) |
| 868 | */ |
| 869 | std::map<uint32_t, interface_var> builtin_inputs; |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 870 | bool pass = true; |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 871 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 872 | collect_interface_by_location(dev, vs, spv::StorageClassInput, inputs, builtin_inputs); |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 873 | |
| 874 | /* Build index by location */ |
| 875 | std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs; |
Chris Forbes | 7191cd5 | 2015-05-25 11:13:24 +1200 | [diff] [blame] | 876 | if (vi) { |
| 877 | for (unsigned i = 0; i < vi->attributeCount; i++) |
| 878 | attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i]; |
| 879 | } |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 880 | |
| 881 | auto it_a = attribs.begin(); |
| 882 | auto it_b = inputs.begin(); |
| 883 | |
David Pinedo | d8f83d8 | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 884 | while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) { |
| 885 | bool a_at_end = attribs.size() == 0 || it_a == attribs.end(); |
| 886 | bool b_at_end = inputs.size() == 0 || it_b == inputs.end(); |
Chris Forbes | 62cc3fc | 2015-06-10 08:37:27 +1200 | [diff] [blame] | 887 | auto a_first = a_at_end ? 0 : it_a->first; |
| 888 | auto b_first = b_at_end ? 0 : it_b->first; |
David Pinedo | d8f83d8 | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 889 | if (b_at_end || a_first < b_first) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 890 | if (log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", |
| 891 | "Vertex attribute at location %d not consumed by VS", a_first)) { |
| 892 | pass = false; |
| 893 | } |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 894 | it_a++; |
| 895 | } |
David Pinedo | d8f83d8 | 2015-04-27 16:36:17 -0600 | [diff] [blame] | 896 | else if (a_at_end || b_first < a_first) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 897 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", |
| 898 | "VS consumes input at location %d but not provided", b_first)) { |
| 899 | pass = false; |
| 900 | } |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 901 | it_b++; |
| 902 | } |
| 903 | else { |
Chris Forbes | 401784b | 2015-05-04 14:04:24 +1200 | [diff] [blame] | 904 | unsigned attrib_type = get_format_type(it_a->second->format); |
| 905 | unsigned input_type = get_fundamental_type(vs, it_b->second.type_id); |
| 906 | |
| 907 | /* type checking */ |
| 908 | if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) { |
| 909 | char vs_type[1024]; |
| 910 | describe_type(vs_type, vs, it_b->second.type_id); |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 911 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 912 | "Attribute type of `%s` at location %d does not match VS input type of `%s`", |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 913 | string_VkFormat(it_a->second->format), a_first, vs_type)) { |
| 914 | pass = false; |
| 915 | } |
Chris Forbes | 401784b | 2015-05-04 14:04:24 +1200 | [diff] [blame] | 916 | } |
| 917 | |
Chris Forbes | 6b2ead6 | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 918 | /* OK! */ |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 919 | it_a++; |
| 920 | it_b++; |
| 921 | } |
| 922 | } |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 923 | |
| 924 | return pass; |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 928 | static bool |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 929 | validate_fs_outputs_against_render_pass(VkDevice dev, shader_module const *fs, render_pass const *rp, uint32_t subpass) |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 930 | { |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 931 | const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass]; |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 932 | std::map<uint32_t, interface_var> outputs; |
| 933 | std::map<uint32_t, interface_var> builtin_outputs; |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 934 | bool pass = true; |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 935 | |
| 936 | /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */ |
| 937 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 938 | collect_interface_by_location(dev, fs, spv::StorageClassOutput, outputs, builtin_outputs); |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 939 | |
| 940 | /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs, |
| 941 | * and all color attachment should be UNORM/SNORM/FLOAT. |
| 942 | */ |
| 943 | if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) { |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 944 | if (outputs.size()) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 945 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC", |
| 946 | "Should not have user-defined FS outputs when using broadcast")) { |
| 947 | pass = false; |
| 948 | } |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 949 | } |
| 950 | |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 951 | for (unsigned i = 0; i < color_formats.size(); i++) { |
| 952 | unsigned attachmentType = get_format_type(color_formats[i]); |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 953 | if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 954 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", |
| 955 | "CB format should not be SINT or UINT when using broadcast")) { |
| 956 | pass = false; |
| 957 | } |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 961 | return pass; |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | auto it = outputs.begin(); |
| 965 | uint32_t attachment = 0; |
| 966 | |
| 967 | /* Walk attachment list and outputs together -- this is a little overpowered since attachments |
| 968 | * are currently dense, but the parallel with matching between shader stages is nice. |
| 969 | */ |
| 970 | |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 971 | /* TODO: Figure out compile error with cb->attachmentCount */ |
Chris Forbes | c2cbb0a | 2015-07-11 11:05:01 +1200 | [diff] [blame] | 972 | while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) { |
| 973 | if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 974 | if (log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", |
| 975 | "FS writes to output location %d with no matching attachment", it->first)) { |
| 976 | pass = false; |
| 977 | } |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 978 | it++; |
| 979 | } |
| 980 | else if (it == outputs.end() || it->first > attachment) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 981 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", |
| 982 | "Attachment %d not written by FS", attachment)) { |
| 983 | pass = false; |
| 984 | } |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 985 | attachment++; |
| 986 | } |
| 987 | else { |
Chris Forbes | 46d31e5 | 2015-05-04 14:20:10 +1200 | [diff] [blame] | 988 | unsigned output_type = get_fundamental_type(fs, it->second.type_id); |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 989 | unsigned att_type = get_format_type(color_formats[attachment]); |
Chris Forbes | 46d31e5 | 2015-05-04 14:20:10 +1200 | [diff] [blame] | 990 | |
| 991 | /* type checking */ |
| 992 | if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) { |
| 993 | char fs_type[1024]; |
| 994 | describe_type(fs_type, fs, it->second.type_id); |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 995 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 996 | "Attachment %d of type `%s` does not match FS output type of `%s`", |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 997 | attachment, string_VkFormat(color_formats[attachment]), fs_type)) { |
| 998 | pass = false; |
| 999 | } |
Chris Forbes | 46d31e5 | 2015-05-04 14:20:10 +1200 | [diff] [blame] | 1000 | } |
| 1001 | |
Chris Forbes | 6b2ead6 | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 1002 | /* OK! */ |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 1003 | it++; |
| 1004 | attachment++; |
| 1005 | } |
| 1006 | } |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 1007 | |
| 1008 | return pass; |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 1012 | struct shader_stage_attributes { |
| 1013 | char const * const name; |
| 1014 | bool arrayed_input; |
| 1015 | }; |
| 1016 | |
| 1017 | |
| 1018 | static shader_stage_attributes |
| 1019 | shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = { |
| 1020 | { "vertex shader", false }, |
| 1021 | { "tessellation control shader", true }, |
| 1022 | { "tessellation evaluation shader", false }, |
| 1023 | { "geometry shader", true }, |
| 1024 | { "fragment shader", false }, |
| 1025 | }; |
| 1026 | |
| 1027 | |
Chris Forbes | 556c76c | 2015-08-14 12:04:59 +1200 | [diff] [blame] | 1028 | static VkDescriptorSetLayoutBinding * |
| 1029 | find_descriptor_binding(std::vector<std::vector<VkDescriptorSetLayoutBinding>*>* layout, |
| 1030 | std::pair<unsigned, unsigned> slot) |
| 1031 | { |
| 1032 | if (!layout) |
| 1033 | return nullptr; |
| 1034 | |
| 1035 | if (slot.first >= layout->size()) |
| 1036 | return nullptr; |
| 1037 | |
| 1038 | auto set = (*layout)[slot.first]; |
| 1039 | |
| 1040 | if (slot.second >= set->size()) |
| 1041 | return nullptr; |
| 1042 | |
| 1043 | return &(*set)[slot.second]; |
| 1044 | } |
| 1045 | |
| 1046 | |
Chris Forbes | 81874ba | 2015-06-04 20:23:00 +1200 | [diff] [blame] | 1047 | static bool |
Chris Forbes | 36a372f | 2015-07-24 13:53:47 +1200 | [diff] [blame] | 1048 | validate_graphics_pipeline(VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo) |
Chris Forbes | 4175e6f | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 1049 | { |
Chris Forbes | f6800b5 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 1050 | /* We seem to allow pipeline stages to be specified out of order, so collect and identify them |
| 1051 | * before trying to do anything more: */ |
| 1052 | |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1053 | shader_module const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */ |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 1054 | memset(shaders, 0, sizeof(shaders)); |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1055 | render_pass const *rp = 0; |
Mark Lobodzinski | d5732f3 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1056 | VkPipelineVertexInputStateCreateInfo const *vi = 0; |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 1057 | bool pass = true; |
Chris Forbes | f6800b5 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 1058 | |
Chris Forbes | 7f96383 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 1059 | loader_platform_thread_lock_mutex(&globalLock); |
| 1060 | |
Tony Barbour | 92503c6 | 2015-07-13 15:28:47 -0600 | [diff] [blame] | 1061 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
Mark Lobodzinski | d5732f3 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1062 | VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i]; |
| 1063 | if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) { |
Chris Forbes | f6800b5 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 1064 | |
Mark Lobodzinski | d5732f3 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1065 | if (pStage->stage < VK_SHADER_STAGE_VERTEX || pStage->stage > VK_SHADER_STAGE_FRAGMENT) { |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 1066 | if (log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC", |
| 1067 | "Unknown shader stage %d", pStage->stage)) { |
| 1068 | pass = false; |
| 1069 | } |
Chris Forbes | 6b2ead6 | 2015-04-17 10:13:28 +1200 | [diff] [blame] | 1070 | } |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 1071 | else { |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 1072 | struct shader_object *shader = shader_object_map[pStage->shader.handle]; |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1073 | shaders[pStage->stage] = shader->module; |
Chris Forbes | 556c76c | 2015-08-14 12:04:59 +1200 | [diff] [blame] | 1074 | |
| 1075 | /* validate descriptor set layout against what the spirv module actually uses */ |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1076 | std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses; |
| 1077 | collect_interface_by_descriptor_slot(dev, shader->module, spv::StorageClassUniform, |
| 1078 | descriptor_uses); |
Chris Forbes | 556c76c | 2015-08-14 12:04:59 +1200 | [diff] [blame] | 1079 | |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1080 | auto layout = pCreateInfo->layout.handle ? |
| 1081 | pipeline_layout_map[pCreateInfo->layout.handle] : nullptr; |
Chris Forbes | 556c76c | 2015-08-14 12:04:59 +1200 | [diff] [blame] | 1082 | |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1083 | for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) { |
Chris Forbes | 556c76c | 2015-08-14 12:04:59 +1200 | [diff] [blame] | 1084 | |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1085 | /* find the matching binding */ |
| 1086 | auto binding = find_descriptor_binding(layout, it->first); |
Chris Forbes | 556c76c | 2015-08-14 12:04:59 +1200 | [diff] [blame] | 1087 | |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1088 | if (binding == nullptr) { |
| 1089 | char type_name[1024]; |
| 1090 | describe_type(type_name, shader->module, it->second.type_id); |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 1091 | if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1092 | SHADER_CHECKER_MISSING_DESCRIPTOR, "SC", |
| 1093 | "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout", |
Mike Stroyan | 830368a | 2015-09-10 14:12:01 -0600 | [diff] [blame] | 1094 | it->first.first, it->first.second, type_name)) { |
| 1095 | pass = false; |
| 1096 | } |
Chris Forbes | 556c76c | 2015-08-14 12:04:59 +1200 | [diff] [blame] | 1097 | } |
| 1098 | } |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 1099 | } |
Chris Forbes | f6800b5 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 1100 | } |
Chris Forbes | f6800b5 | 2015-04-08 10:16:45 +1200 | [diff] [blame] | 1101 | } |
| 1102 | |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1103 | if (pCreateInfo->renderPass != VK_NULL_HANDLE) |
Chris Forbes | 9715d4a | 2015-07-10 09:06:54 +1200 | [diff] [blame] | 1104 | rp = render_pass_map[pCreateInfo->renderPass.handle]; |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1105 | |
Mark Lobodzinski | d5732f3 | 2015-06-23 15:11:57 -0600 | [diff] [blame] | 1106 | vi = pCreateInfo->pVertexInputState; |
| 1107 | |
Chris Forbes | 280ba2c | 2015-06-12 11:16:41 +1200 | [diff] [blame] | 1108 | if (vi) { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1109 | pass = validate_vi_consistency(dev, vi) && pass; |
Chris Forbes | 280ba2c | 2015-06-12 11:16:41 +1200 | [diff] [blame] | 1110 | } |
| 1111 | |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1112 | if (shaders[VK_SHADER_STAGE_VERTEX]) { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1113 | pass = validate_vi_against_vs_inputs(dev, vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass; |
Chris Forbes | 772d03b | 2015-04-08 10:36:37 +1200 | [diff] [blame] | 1114 | } |
| 1115 | |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 1116 | /* TODO: enforce rules about present combinations of shaders */ |
| 1117 | int producer = VK_SHADER_STAGE_VERTEX; |
| 1118 | int consumer = VK_SHADER_STAGE_GEOMETRY; |
| 1119 | |
| 1120 | while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) { |
| 1121 | producer++; |
| 1122 | consumer++; |
Chris Forbes | 4100245 | 2015-04-08 10:19:16 +1200 | [diff] [blame] | 1123 | } |
| 1124 | |
Tony Barbour | 0102a90 | 2015-06-11 15:04:25 -0600 | [diff] [blame] | 1125 | for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) { |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 1126 | assert(shaders[producer]); |
| 1127 | if (shaders[consumer]) { |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1128 | pass = validate_interface_between_stages(dev, |
| 1129 | shaders[producer], shader_stage_attribs[producer].name, |
| 1130 | shaders[consumer], shader_stage_attribs[consumer].name, |
| 1131 | shader_stage_attribs[consumer].arrayed_input) && pass; |
Chris Forbes | f044ec9 | 2015-06-05 15:01:08 +1200 | [diff] [blame] | 1132 | |
| 1133 | producer = consumer; |
| 1134 | } |
| 1135 | } |
| 1136 | |
Chris Forbes | 46794b8 | 2015-09-18 11:40:23 +1200 | [diff] [blame] | 1137 | if (shaders[VK_SHADER_STAGE_FRAGMENT] && rp) { |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1138 | pass = validate_fs_outputs_against_render_pass(dev, shaders[VK_SHADER_STAGE_FRAGMENT], rp, pCreateInfo->subpass) && pass; |
Chris Forbes | 3616b46 | 2015-04-08 10:37:20 +1200 | [diff] [blame] | 1139 | } |
| 1140 | |
Chris Forbes | 7f96383 | 2015-05-29 14:55:18 +1200 | [diff] [blame] | 1141 | loader_platform_thread_unlock_mutex(&globalLock); |
Chris Forbes | 81874ba | 2015-06-04 20:23:00 +1200 | [diff] [blame] | 1142 | return pass; |
| 1143 | } |
| 1144 | |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1145 | //TODO handle pipelineCache entry points |
Chris Forbes | 39d8d75 | 2015-06-04 20:27:09 +1200 | [diff] [blame] | 1146 | VK_LAYER_EXPORT VkResult VKAPI |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1147 | vkCreateGraphicsPipelines(VkDevice device, |
| 1148 | VkPipelineCache pipelineCache, |
| 1149 | uint32_t count, |
| 1150 | const VkGraphicsPipelineCreateInfo *pCreateInfos, |
| 1151 | VkPipeline *pPipelines) |
Chris Forbes | 81874ba | 2015-06-04 20:23:00 +1200 | [diff] [blame] | 1152 | { |
Chris Forbes | 36a372f | 2015-07-24 13:53:47 +1200 | [diff] [blame] | 1153 | bool pass = true; |
| 1154 | for (uint32_t i = 0; i < count; i++) { |
| 1155 | pass = validate_graphics_pipeline(device, &pCreateInfos[i]) && pass; |
| 1156 | } |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 1157 | |
| 1158 | if (pass) { |
| 1159 | /* The driver is allowed to crash if passed junk. Only actually create the |
| 1160 | * pipeline if we didn't run into any showstoppers above. |
| 1161 | */ |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1162 | return get_dispatch_table(shader_checker_device_table_map, device)->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines); |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 1163 | } |
| 1164 | else { |
Courtney Goeltzenleuchter | 55659b7 | 2015-09-14 18:01:17 -0600 | [diff] [blame] | 1165 | return VK_ERROR_VALIDATION_FAILED; |
Chris Forbes | ee99b9b | 2015-05-25 11:13:22 +1200 | [diff] [blame] | 1166 | } |
Chris Forbes | 4175e6f | 2015-04-08 10:15:35 +1200 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1170 | VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice) |
| 1171 | { |
| 1172 | VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, *pDevice); |
| 1173 | VkResult result = pDeviceTable->CreateDevice(gpu, pCreateInfo, pDevice); |
| 1174 | if (result == VK_SUCCESS) { |
| 1175 | layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map); |
| 1176 | VkLayerDispatchTable *pTable = get_dispatch_table(shader_checker_device_table_map, *pDevice); |
| 1177 | layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map); |
| 1178 | my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice); |
| 1179 | } |
| 1180 | return result; |
| 1181 | } |
Chris Forbes | 39d8d75 | 2015-06-04 20:27:09 +1200 | [diff] [blame] | 1182 | |
Jon Ashburn | 9a8a2e2 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 1183 | /* hook DextroyDevice to remove tableMap entry */ |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1184 | VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device) |
Jon Ashburn | 9a8a2e2 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 1185 | { |
Courtney Goeltzenleuchter | 0daf228 | 2015-06-13 21:22:12 -0600 | [diff] [blame] | 1186 | dispatch_key key = get_dispatch_key(device); |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1187 | VkLayerDispatchTable *pDisp = get_dispatch_table(shader_checker_device_table_map, device); |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1188 | pDisp->DestroyDevice(device); |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1189 | shader_checker_device_table_map.erase(key); |
Jon Ashburn | 9a8a2e2 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 1190 | } |
| 1191 | |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1192 | VkResult VKAPI vkCreateInstance( |
| 1193 | const VkInstanceCreateInfo* pCreateInfo, |
| 1194 | VkInstance* pInstance) |
| 1195 | { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1196 | VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map,*pInstance); |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1197 | VkResult result = pTable->CreateInstance(pCreateInfo, pInstance); |
| 1198 | |
| 1199 | if (result == VK_SUCCESS) { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1200 | layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map); |
| 1201 | my_data->report_data = debug_report_create_instance( |
| 1202 | pTable, |
| 1203 | *pInstance, |
| 1204 | pCreateInfo->extensionCount, |
| 1205 | pCreateInfo->ppEnabledExtensionNames); |
Courtney Goeltzenleuchter | d02a964 | 2015-06-08 14:58:39 -0600 | [diff] [blame] | 1206 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1207 | init_shader_checker(my_data); |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1208 | } |
| 1209 | return result; |
| 1210 | } |
| 1211 | |
Jon Ashburn | 9a8a2e2 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 1212 | /* hook DestroyInstance to remove tableInstanceMap entry */ |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1213 | VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance) |
Jon Ashburn | 9a8a2e2 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 1214 | { |
Courtney Goeltzenleuchter | 0daf228 | 2015-06-13 21:22:12 -0600 | [diff] [blame] | 1215 | dispatch_key key = get_dispatch_key(instance); |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1216 | VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance); |
Mark Lobodzinski | 2141f65 | 2015-09-07 13:59:43 -0600 | [diff] [blame] | 1217 | pTable->DestroyInstance(instance); |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1218 | |
| 1219 | // Clean up logging callback, if any |
| 1220 | layer_data *my_data = get_my_data_ptr(key, layer_data_map); |
Courtney Goeltzenleuchter | ed12e71 | 2015-10-05 15:59:58 -0600 | [diff] [blame^] | 1221 | while (my_data->logging_callback.size() > 0) { |
| 1222 | VkDbgMsgCallback callback = my_data->logging_callback.back(); |
| 1223 | layer_destroy_msg_callback(my_data->report_data, callback); |
| 1224 | my_data->logging_callback.pop_back(); |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | layer_debug_report_destroy_instance(my_data->report_data); |
| 1228 | layer_data_map.erase(pTable); |
| 1229 | |
| 1230 | shader_checker_instance_table_map.erase(key); |
Jon Ashburn | 9a8a2e2 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 1231 | } |
Chris Forbes | db467bd | 2015-05-25 11:12:59 +1200 | [diff] [blame] | 1232 | |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1233 | VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback( |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1234 | VkInstance instance, |
| 1235 | VkFlags msgFlags, |
| 1236 | const PFN_vkDbgMsgCallback pfnMsgCallback, |
| 1237 | void* pUserData, |
| 1238 | VkDbgMsgCallback* pMsgCallback) |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1239 | { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1240 | VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance); |
| 1241 | VkResult res = pTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback); |
| 1242 | if (VK_SUCCESS == res) { |
| 1243 | layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map); |
| 1244 | res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback); |
| 1245 | } |
| 1246 | return res; |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback( |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1250 | VkInstance instance, |
| 1251 | VkDbgMsgCallback msgCallback) |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1252 | { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1253 | VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance); |
| 1254 | VkResult res = pTable->DbgDestroyMsgCallback(instance, msgCallback); |
| 1255 | layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map); |
| 1256 | layer_destroy_msg_callback(my_data->report_data, msgCallback); |
| 1257 | return res; |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1258 | } |
| 1259 | |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1260 | VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName) |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 1261 | { |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1262 | if (dev == NULL) |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 1263 | return NULL; |
| 1264 | |
Jon Ashburn | 8fd0825 | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 1265 | /* loader uses this to force layer initialization; device object is wrapped */ |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1266 | if (!strcmp("vkGetDeviceProcAddr", funcName)) { |
| 1267 | initDeviceTable(shader_checker_device_table_map, (const VkBaseLayerObject *) dev); |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1268 | return (PFN_vkVoidFunction) vkGetDeviceProcAddr; |
Jon Ashburn | 8fd0825 | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 1269 | } |
| 1270 | |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 1271 | #define ADD_HOOK(fn) \ |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1272 | if (!strncmp(#fn, funcName, sizeof(#fn))) \ |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1273 | return (PFN_vkVoidFunction) fn |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 1274 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1275 | ADD_HOOK(vkCreateDevice); |
Courtney Goeltzenleuchter | ee4027d | 2015-06-28 13:01:17 -0600 | [diff] [blame] | 1276 | ADD_HOOK(vkCreateShaderModule); |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 1277 | ADD_HOOK(vkCreateShader); |
Chia-I Wu | 08accc6 | 2015-07-07 11:50:03 +0800 | [diff] [blame] | 1278 | ADD_HOOK(vkCreateRenderPass); |
Jon Ashburn | 9a8a2e2 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 1279 | ADD_HOOK(vkDestroyDevice); |
Jon Ashburn | c669cc6 | 2015-07-09 15:02:25 -0600 | [diff] [blame] | 1280 | ADD_HOOK(vkCreateGraphicsPipelines); |
Chris Forbes | 2b8493a | 2015-08-12 15:03:22 +1200 | [diff] [blame] | 1281 | ADD_HOOK(vkCreateDescriptorSetLayout); |
| 1282 | ADD_HOOK(vkCreatePipelineLayout); |
Jon Ashburn | e59f84f | 2015-05-18 09:08:41 -0600 | [diff] [blame] | 1283 | #undef ADD_HOOK |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1284 | |
| 1285 | VkLayerDispatchTable* pTable = get_dispatch_table(shader_checker_device_table_map, dev); |
| 1286 | { |
| 1287 | if (pTable->GetDeviceProcAddr == NULL) |
| 1288 | return NULL; |
| 1289 | return pTable->GetDeviceProcAddr(dev, funcName); |
| 1290 | } |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 1291 | } |
| 1292 | |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1293 | VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName) |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 1294 | { |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1295 | PFN_vkVoidFunction fptr; |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1296 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1297 | if (instance == NULL) |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 1298 | return NULL; |
| 1299 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1300 | if (!strcmp("vkGetInstanceProcAddr", funcName)) { |
| 1301 | initInstanceTable(shader_checker_instance_table_map, (const VkBaseLayerObject *) instance); |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1302 | return (PFN_vkVoidFunction) vkGetInstanceProcAddr; |
Jon Ashburn | 8fd0825 | 2015-05-28 16:25:02 -0600 | [diff] [blame] | 1303 | } |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 1304 | #define ADD_HOOK(fn) \ |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1305 | if (!strncmp(#fn, funcName, sizeof(#fn))) \ |
Courtney Goeltzenleuchter | 2d3ba63 | 2015-07-12 14:35:22 -0600 | [diff] [blame] | 1306 | return (PFN_vkVoidFunction) fn |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 1307 | |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1308 | ADD_HOOK(vkCreateInstance); |
Jon Ashburn | 9a8a2e2 | 2015-05-19 16:34:53 -0600 | [diff] [blame] | 1309 | ADD_HOOK(vkDestroyInstance); |
Courtney Goeltzenleuchter | 35985f6 | 2015-09-14 17:22:16 -0600 | [diff] [blame] | 1310 | ADD_HOOK(vkEnumerateInstanceExtensionProperties); |
| 1311 | ADD_HOOK(vkEnumerateDeviceExtensionProperties); |
| 1312 | ADD_HOOK(vkEnumerateInstanceLayerProperties); |
| 1313 | ADD_HOOK(vkEnumerateDeviceLayerProperties); |
Jon Ashburn | e59f84f | 2015-05-18 09:08:41 -0600 | [diff] [blame] | 1314 | #undef ADD_HOOK |
Jon Ashburn | f6b33db | 2015-05-05 14:22:52 -0600 | [diff] [blame] | 1315 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1316 | |
| 1317 | layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map); |
| 1318 | fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName); |
Courtney Goeltzenleuchter | bfd2c66 | 2015-06-01 14:46:33 -0600 | [diff] [blame] | 1319 | if (fptr) |
| 1320 | return fptr; |
| 1321 | |
Chris Forbes | b1dee27 | 2015-07-03 13:50:24 +1200 | [diff] [blame] | 1322 | { |
| 1323 | VkLayerInstanceDispatchTable* pTable = get_dispatch_table(shader_checker_instance_table_map, instance); |
| 1324 | if (pTable->GetInstanceProcAddr == NULL) |
| 1325 | return NULL; |
| 1326 | return pTable->GetInstanceProcAddr(instance, funcName); |
| 1327 | } |
Chris Forbes | 2778f30 | 2015-04-02 13:22:31 +1300 | [diff] [blame] | 1328 | } |