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