blob: 94c5fd63326145cc595527f18fc30f6e639deb8b [file] [log] [blame]
Chris Forbes2778f302015-04-02 13:22:31 +13001/*
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 Forbes06e8fc32015-04-13 12:14:52 +120027#include <map>
Chris Forbes2778f302015-04-02 13:22:31 +130028#include <unordered_map>
Chris Forbes41002452015-04-08 10:19:16 +120029#include <map>
Chris Forbes3b1c4212015-04-08 10:11:59 +120030#include <vector>
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -060031#include <string>
Tobin Ehlisb80b4252015-09-01 11:59:36 -060032#include <iostream>
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060033#include "vk_loader_platform.h"
Chris Forbes2778f302015-04-02 13:22:31 +130034#include "vk_dispatch_table_helper.h"
Tobin Ehlis0c6f9ee2015-07-03 09:42:57 -060035#include "vk_layer.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060036#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060037#include "vk_layer_table.h"
Chris Forbesb1dee272015-07-03 13:50:24 +120038#include "vk_layer_logging.h"
Chris Forbes401784b2015-05-04 14:04:24 +120039#include "vk_enum_string_helper.h"
Chris Forbes6b2ead62015-04-17 10:13:28 +120040#include "shader_checker.h"
Chris Forbes2778f302015-04-02 13:22:31 +130041// The following is #included again to catch certain OS-specific functions
42// being used:
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060043#include "vk_loader_platform.h"
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -060044#include "vk_layer_extension_utils.h"
Chris Forbes2778f302015-04-02 13:22:31 +130045
GregF7c45bed2015-07-21 17:22:50 -060046#include "spirv/spirv.hpp"
Chris Forbes2778f302015-04-02 13:22:31 +130047
Chris Forbes2778f302015-04-02 13:22:31 +130048
Cody Northrop55443ef2015-09-28 15:09:32 -060049struct layer_data {
Chris Forbesb1dee272015-07-03 13:50:24 +120050 debug_report_data *report_data;
51 // TODO: put instance data here
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -060052 std::vector<VkDbgMsgCallback> logging_callback;
Cody Northrop55443ef2015-09-28 15:09:32 -060053
54 layer_data() :
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -060055 report_data(nullptr)
Cody Northrop55443ef2015-09-28 15:09:32 -060056 {};
57};
Chris Forbesb1dee272015-07-03 13:50:24 +120058
59static std::unordered_map<void *, layer_data *> layer_data_map;
60static device_table_map shader_checker_device_table_map;
61static instance_table_map shader_checker_instance_table_map;
62
63
64template layer_data *get_my_data_ptr<layer_data>(
65 void *data_key,
66 std::unordered_map<void *, layer_data *> &data_map);
67
Chris Forbes9715d4a2015-07-10 09:06:54 +120068debug_report_data *mdd(void *object)
Chris Forbesb1dee272015-07-03 13:50:24 +120069{
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
78debug_report_data *mid(VkInstance object)
79{
80 dispatch_key key = get_dispatch_key(object);
Tobin Ehlisbfbac252015-09-01 11:46:36 -060081 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +120082#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 Forbesb6b8c462015-04-15 06:59:41 +120088static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +120089// TODO : This can be much smarter, using separate locks for separate global data
90static int globalLockInitialized = 0;
91static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +120092
Chris Forbes3a5e99a2015-04-10 11:41:20 +120093
Chris Forbes2b8493a2015-08-12 15:03:22 +120094std::unordered_map<uint32_t, std::vector<VkDescriptorSetLayoutBinding>*> descriptor_set_layout_map;
95
96VK_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
117std::unordered_map<uint32_t, std::vector<std::vector<VkDescriptorSetLayoutBinding>*>*> pipeline_layout_map;
118
119VK_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 Forbes3a5e99a2015-04-10 11:41:20 +1200142static void
143build_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:
GregF7c45bed2015-07-21 17:22:50 -0600160 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200161 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600162 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200163 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 Forbes46794b82015-09-18 11:40:23 +1200186
187bool
188shader_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 Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600197struct shader_module {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200198 /* the spirv image itself */
Chris Forbes3b1c4212015-04-08 10:11:59 +1200199 std::vector<uint32_t> words;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200200 /* 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 Forbes3b1c4212015-04-08 10:11:59 +1200204
Chris Forbes46794b82015-09-18 11:40:23 +1200205 shader_module(VkShaderModuleCreateInfo const *pCreateInfo) :
Chris Forbesf044ec92015-06-05 15:01:08 +1200206 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
Chris Forbes46794b82015-09-18 11:40:23 +1200207 type_def_index() {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200208
209 build_type_def_index(words, type_def_index);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200210 }
211};
212
213
Chris Forbes9715d4a2015-07-10 09:06:54 +1200214static std::unordered_map<uint64_t, shader_module *> shader_module_map;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600215
216struct shader_object {
217 std::string name;
218 struct shader_module *module;
219
220 shader_object(VkShaderCreateInfo const *pCreateInfo)
221 {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200222 module = shader_module_map[pCreateInfo->module.handle];
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600223 name = pCreateInfo->pName;
224 }
225};
Chris Forbes9715d4a2015-07-10 09:06:54 +1200226static std::unordered_map<uint64_t, shader_object *> shader_object_map;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200227
Chia-I Wu08accc62015-07-07 11:50:03 +0800228struct 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 Northropa505dda2015-08-04 11:16:41 -0600243 const uint32_t att = subpass->pColorAttachments[j].attachment;
Chia-I Wu08accc62015-07-07 11:50:03 +0800244 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 Forbes9715d4a2015-07-10 09:06:54 +1200253static std::unordered_map<uint64_t, render_pass *> render_pass_map;
Chia-I Wu08accc62015-07-07 11:50:03 +0800254
Chris Forbes3b1c4212015-04-08 10:11:59 +1200255
Chris Forbesb6b8c462015-04-15 06:59:41 +1200256static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200257init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200258{
Chris Forbesb1dee272015-07-03 13:50:24 +1200259 uint32_t report_flags = 0;
260 uint32_t debug_action = 0;
261 FILE *log_output = NULL;
262 const char *option_str;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600263 VkDbgMsgCallback callback;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200264 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200265 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
266 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200267
Chris Forbesb1dee272015-07-03 13:50:24 +1200268 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200269 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200270 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600271 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600272 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 Forbesb1dee272015-07-03 13:50:24 +1200279 }
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 Forbesb6b8c462015-04-15 06:59:41 +1200290 }
291}
292
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600293static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600294 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600295 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600296 VK_API_VERSION,
297 VK_MAKE_VERSION(0, 1, 0),
298 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600299 }
Chris Forbes2778f302015-04-02 13:22:31 +1300300};
301
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600302VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600303 const char *pLayerName,
304 uint32_t *pCount,
305 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300306{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600307 /* shader checker does not have any global extensions */
308 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300309}
310
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600311VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600312 uint32_t *pCount,
313 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600314{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600315 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
316 shader_checker_global_layers,
317 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600318}
319
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600320VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600321 VkPhysicalDevice physicalDevice,
322 const char* pLayerName,
323 uint32_t* pCount,
324 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600325{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600326 /* Shader checker does not have any physical device extensions */
327 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
328}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600329
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600330VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600331 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 Ashburn207a3af2015-06-10 16:43:31 -0600339}
Chris Forbes2778f302015-04-02 13:22:31 +1300340
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200341static char const *
342storage_class_name(unsigned sc)
343{
344 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600345 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 Northrop97e52d82015-04-20 14:09:40 -0600354 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600355 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200356 default: return "unknown";
357 }
358}
359
360
361/* returns ptr to null terminator */
362static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600363describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200364{
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 Elliott1cb62222015-04-17 11:05:04 -0600396 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200397 dst = describe_type(dst, src, code[i]);
398 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
399 }
400 return dst;
401 }
Chris Forbes0ae15802015-08-14 12:04:39 +1200402 case spv::OpTypeSampler:
403 return dst + sprintf(dst, "sampler");
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200404 default:
405 return dst + sprintf(dst, "oddtype");
406 }
407}
408
409
410static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600411types_match(shader_module const *a, shader_module const *b, unsigned a_type, unsigned b_type, bool b_arrayed)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200412{
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 Forbes3a5e99a2015-04-10 11:41:20 +1200417 return false;
418 }
419
420 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200421 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 Forbesf3fc0332015-06-05 14:57:05 +1200431 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 Forbes3a5e99a2015-04-10 11:41:20 +1200436 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200437 return false;
438 }
439
440 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200441 /* if b_arrayed and we hit a leaf type, then we can't match -- there's nowhere for the extra OpTypeArray to be! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200442 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200443 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200444 case spv::OpTypeInt:
445 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200446 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200447 case spv::OpTypeFloat:
448 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200449 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200450 case spv::OpTypeVector:
451 case spv::OpTypeMatrix:
452 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200453 /* 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 Forbes3a5e99a2015-04-10 11:41:20 +1200456 case spv::OpTypeStruct:
457 /* match on all element types */
458 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200459 if (b_arrayed) {
460 /* for the purposes of matching different levels of arrayness, structs are leaves. */
461 return false;
462 }
463
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200464 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 Elliott1cb62222015-04-17 11:05:04 -0600471 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200472 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200473 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 Forbesf3fc0332015-06-05 14:57:05 +1200481 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200482
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 Forbes06e8fc32015-04-13 12:14:52 +1200493static int
494value_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
504struct 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
511static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200512collect_interface_by_location(VkDevice dev,
513 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200514 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 Forbes06e8fc32015-04-13 12:14:52 +1200520 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 Northrop97e52d82015-04-20 14:09:40 -0600533 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200534 var_locations[code[word+1]] = code[word+3];
535 }
536
Cody Northrop97e52d82015-04-20 14:09:40 -0600537 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200538 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 Elliott1cb62222015-04-17 11:05:04 -0600546 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200547 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 Forbes9715d4a2015-07-10 09:06:54 +1200555 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200556 "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 Forbes06e8fc32015-04-13 12:14:52 +1200558 }
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 Forbes0ae15802015-08-14 12:04:39 +1200580static void
581collect_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 Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600636VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
637 VkDevice device,
638 const VkShaderModuleCreateInfo *pCreateInfo,
639 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300640{
Chris Forbes46794b82015-09-18 11:40:23 +1200641 /* 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 Forbesb1dee272015-07-03 13:50:24 +1200649 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShaderModule(device, pCreateInfo, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200650
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600651 if (res == VK_SUCCESS) {
652 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes46794b82015-09-18 11:40:23 +1200653 shader_module_map[pShaderModule->handle] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600654 loader_platform_thread_unlock_mutex(&globalLock);
655 }
Chris Forbes2778f302015-04-02 13:22:31 +1300656 return res;
657}
658
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600659VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
660 VkDevice device,
661 const VkShaderCreateInfo *pCreateInfo,
662 VkShader *pShader)
663{
Chris Forbesb1dee272015-07-03 13:50:24 +1200664 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShader(device, pCreateInfo, pShader);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600665
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600666 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes9715d4a2015-07-10 09:06:54 +1200667 shader_object_map[pShader->handle] = new shader_object(pCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600668 loader_platform_thread_unlock_mutex(&globalLock);
669 return res;
670}
Chris Forbes2778f302015-04-02 13:22:31 +1300671
Chia-I Wu08accc62015-07-07 11:50:03 +0800672VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
673 VkDevice device,
674 const VkRenderPassCreateInfo *pCreateInfo,
675 VkRenderPass *pRenderPass)
676{
Chia-I Wu08accc62015-07-07 11:50:03 +0800677 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateRenderPass(device, pCreateInfo, pRenderPass);
678
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600679 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes9715d4a2015-07-10 09:06:54 +1200680 render_pass_map[pRenderPass->handle] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800681 loader_platform_thread_unlock_mutex(&globalLock);
682 return res;
683}
684
Chris Forbesee99b9b2015-05-25 11:13:22 +1200685static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200686validate_interface_between_stages(VkDevice dev,
687 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600688 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200689 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200690{
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 Forbesee99b9b2015-05-25 11:13:22 +1200697 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200698
Chris Forbesb1dee272015-07-03 13:50:24 +1200699 collect_interface_by_location(dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
700 collect_interface_by_location(dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200701
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 Pinedod8f83d82015-04-27 16:36:17 -0600706 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 Forbes62cc3fc2015-06-10 08:37:27 +1200709 auto a_first = a_at_end ? 0 : a_it->first;
710 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600711
Mike Stroyan19a6de22015-09-10 14:10:25 -0600712 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Mike Stroyan830368a2015-09-10 14:12:01 -0600713 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 Forbes41002452015-04-08 10:19:16 +1200717 a_it++;
718 }
David Pinedod8f83d82015-04-27 16:36:17 -0600719 else if (a_at_end || a_first > b_first) {
Mike Stroyan830368a2015-09-10 14:12:01 -0600720 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 Forbes41002452015-04-08 10:19:16 +1200724 b_it++;
725 }
726 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200727 if (types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id, consumer_arrayed_input)) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200728 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200729 }
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 Stroyan830368a2015-09-10 14:12:01 -0600736 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 Forbesee99b9b2015-05-25 11:13:22 +1200738 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600739 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200740 }
Chris Forbes41002452015-04-08 10:19:16 +1200741 a_it++;
742 b_it++;
743 }
744 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200745
746 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200747}
748
749
Chris Forbes3616b462015-04-08 10:37:20 +1200750enum 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
758static unsigned
759get_format_type(VkFormat fmt) {
760 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800761 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200762 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800763 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 Forbes3616b462015-04-08 10:37:20 +1200779 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800780 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 Forbes3616b462015-04-08 10:37:20 +1200796 return FORMAT_TYPE_UINT;
797 default:
798 return FORMAT_TYPE_FLOAT;
799 }
800}
801
802
Chris Forbes156a1162015-05-04 14:04:06 +1200803/* characterizes a SPIR-V type appearing in an interface to a FF stage,
804 * for comparison to a VkFormat's characterization above. */
805static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600806get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200807{
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 Forbesee99b9b2015-05-25 11:13:22 +1200835static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200836validate_vi_consistency(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200837{
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 Forbes280ba2c2015-06-12 11:16:41 +1200842 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 Stroyan830368a2015-09-10 14:12:01 -0600848 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 Forbes280ba2c2015-06-12 11:16:41 +1200852 }
853 else {
854 binding = desc;
855 }
856 }
857
858 return pass;
859}
860
861
862static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200863validate_vi_against_vs_inputs(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200864{
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 Forbesee99b9b2015-05-25 11:13:22 +1200870 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200871
Chris Forbesb1dee272015-07-03 13:50:24 +1200872 collect_interface_by_location(dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200873
874 /* Build index by location */
875 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200876 if (vi) {
877 for (unsigned i = 0; i < vi->attributeCount; i++)
878 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
879 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200880
881 auto it_a = attribs.begin();
882 auto it_b = inputs.begin();
883
David Pinedod8f83d82015-04-27 16:36:17 -0600884 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 Forbes62cc3fc2015-06-10 08:37:27 +1200887 auto a_first = a_at_end ? 0 : it_a->first;
888 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600889 if (b_at_end || a_first < b_first) {
Mike Stroyan830368a2015-09-10 14:12:01 -0600890 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 Forbes772d03b2015-04-08 10:36:37 +1200894 it_a++;
895 }
David Pinedod8f83d82015-04-27 16:36:17 -0600896 else if (a_at_end || b_first < a_first) {
Mike Stroyan830368a2015-09-10 14:12:01 -0600897 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 Forbes772d03b2015-04-08 10:36:37 +1200901 it_b++;
902 }
903 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200904 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 Stroyan830368a2015-09-10 14:12:01 -0600911 if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200912 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600913 string_VkFormat(it_a->second->format), a_first, vs_type)) {
914 pass = false;
915 }
Chris Forbes401784b2015-05-04 14:04:24 +1200916 }
917
Chris Forbes6b2ead62015-04-17 10:13:28 +1200918 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200919 it_a++;
920 it_b++;
921 }
922 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200923
924 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200925}
926
927
Chris Forbesee99b9b2015-05-25 11:13:22 +1200928static bool
Chia-I Wu08accc62015-07-07 11:50:03 +0800929validate_fs_outputs_against_render_pass(VkDevice dev, shader_module const *fs, render_pass const *rp, uint32_t subpass)
Chris Forbes3616b462015-04-08 10:37:20 +1200930{
Chia-I Wu08accc62015-07-07 11:50:03 +0800931 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200932 std::map<uint32_t, interface_var> outputs;
933 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200934 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200935
936 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
937
Chris Forbesb1dee272015-07-03 13:50:24 +1200938 collect_interface_by_location(dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200939
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 Forbes3616b462015-04-08 10:37:20 +1200944 if (outputs.size()) {
Mike Stroyan830368a2015-09-10 14:12:01 -0600945 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 Forbes3616b462015-04-08 10:37:20 +1200949 }
950
Chia-I Wu08accc62015-07-07 11:50:03 +0800951 for (unsigned i = 0; i < color_formats.size(); i++) {
952 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200953 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Mike Stroyan830368a2015-09-10 14:12:01 -0600954 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 Forbes3616b462015-04-08 10:37:20 +1200958 }
959 }
960
Chris Forbesee99b9b2015-05-25 11:13:22 +1200961 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200962 }
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 Forbes9715d4a2015-07-10 09:06:54 +1200971 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200972 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
973 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Mike Stroyan830368a2015-09-10 14:12:01 -0600974 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 Forbes3616b462015-04-08 10:37:20 +1200978 it++;
979 }
980 else if (it == outputs.end() || it->first > attachment) {
Mike Stroyan830368a2015-09-10 14:12:01 -0600981 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 Forbes3616b462015-04-08 10:37:20 +1200985 attachment++;
986 }
987 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200988 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800989 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200990
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 Stroyan830368a2015-09-10 14:12:01 -0600995 if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200996 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600997 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
998 pass = false;
999 }
Chris Forbes46d31e52015-05-04 14:20:10 +12001000 }
1001
Chris Forbes6b2ead62015-04-17 10:13:28 +12001002 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +12001003 it++;
1004 attachment++;
1005 }
1006 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001007
1008 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001009}
1010
1011
Chris Forbesf044ec92015-06-05 15:01:08 +12001012struct shader_stage_attributes {
1013 char const * const name;
1014 bool arrayed_input;
1015};
1016
1017
1018static shader_stage_attributes
1019shader_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 Forbes556c76c2015-08-14 12:04:59 +12001028static VkDescriptorSetLayoutBinding *
1029find_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 Forbes81874ba2015-06-04 20:23:00 +12001047static bool
Chris Forbes36a372f2015-07-24 13:53:47 +12001048validate_graphics_pipeline(VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001049{
Chris Forbesf6800b52015-04-08 10:16:45 +12001050 /* 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 Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001053 shader_module const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
Chris Forbesf044ec92015-06-05 15:01:08 +12001054 memset(shaders, 0, sizeof(shaders));
Chia-I Wu08accc62015-07-07 11:50:03 +08001055 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001056 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001057 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001058
Chris Forbes7f963832015-05-29 14:55:18 +12001059 loader_platform_thread_lock_mutex(&globalLock);
1060
Tony Barbour92503c62015-07-13 15:28:47 -06001061 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001062 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1063 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001064
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001065 if (pStage->stage < VK_SHADER_STAGE_VERTEX || pStage->stage > VK_SHADER_STAGE_FRAGMENT) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001066 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 Forbes6b2ead62015-04-17 10:13:28 +12001070 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001071 else {
Chris Forbes9715d4a2015-07-10 09:06:54 +12001072 struct shader_object *shader = shader_object_map[pStage->shader.handle];
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001073 shaders[pStage->stage] = shader->module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001074
1075 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001076 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 Forbes556c76c2015-08-14 12:04:59 +12001079
Chris Forbes46794b82015-09-18 11:40:23 +12001080 auto layout = pCreateInfo->layout.handle ?
1081 pipeline_layout_map[pCreateInfo->layout.handle] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001082
Chris Forbes46794b82015-09-18 11:40:23 +12001083 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001084
Chris Forbes46794b82015-09-18 11:40:23 +12001085 /* find the matching binding */
1086 auto binding = find_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001087
Chris Forbes46794b82015-09-18 11:40:23 +12001088 if (binding == nullptr) {
1089 char type_name[1024];
1090 describe_type(type_name, shader->module, it->second.type_id);
Mike Stroyan830368a2015-09-10 14:12:01 -06001091 if (log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0,
Chris Forbes46794b82015-09-18 11:40:23 +12001092 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1093 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001094 it->first.first, it->first.second, type_name)) {
1095 pass = false;
1096 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001097 }
1098 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001099 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001100 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001101 }
1102
Chia-I Wu08accc62015-07-07 11:50:03 +08001103 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Chris Forbes9715d4a2015-07-10 09:06:54 +12001104 rp = render_pass_map[pCreateInfo->renderPass.handle];
Chia-I Wu08accc62015-07-07 11:50:03 +08001105
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001106 vi = pCreateInfo->pVertexInputState;
1107
Chris Forbes280ba2c2015-06-12 11:16:41 +12001108 if (vi) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001109 pass = validate_vi_consistency(dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001110 }
1111
Chris Forbes46794b82015-09-18 11:40:23 +12001112 if (shaders[VK_SHADER_STAGE_VERTEX]) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001113 pass = validate_vi_against_vs_inputs(dev, vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001114 }
1115
Chris Forbesf044ec92015-06-05 15:01:08 +12001116 /* 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 Forbes41002452015-04-08 10:19:16 +12001123 }
1124
Tony Barbour0102a902015-06-11 15:04:25 -06001125 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001126 assert(shaders[producer]);
1127 if (shaders[consumer]) {
Chris Forbes46794b82015-09-18 11:40:23 +12001128 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 Forbesf044ec92015-06-05 15:01:08 +12001132
1133 producer = consumer;
1134 }
1135 }
1136
Chris Forbes46794b82015-09-18 11:40:23 +12001137 if (shaders[VK_SHADER_STAGE_FRAGMENT] && rp) {
Chia-I Wu08accc62015-07-07 11:50:03 +08001138 pass = validate_fs_outputs_against_render_pass(dev, shaders[VK_SHADER_STAGE_FRAGMENT], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001139 }
1140
Chris Forbes7f963832015-05-29 14:55:18 +12001141 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001142 return pass;
1143}
1144
Jon Ashburnc669cc62015-07-09 15:02:25 -06001145//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001146VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001147vkCreateGraphicsPipelines(VkDevice device,
1148 VkPipelineCache pipelineCache,
1149 uint32_t count,
1150 const VkGraphicsPipelineCreateInfo *pCreateInfos,
1151 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001152{
Chris Forbes36a372f2015-07-24 13:53:47 +12001153 bool pass = true;
1154 for (uint32_t i = 0; i < count; i++) {
1155 pass = validate_graphics_pipeline(device, &pCreateInfos[i]) && pass;
1156 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001157
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 Ashburnc669cc62015-07-09 15:02:25 -06001162 return get_dispatch_table(shader_checker_device_table_map, device)->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001163 }
1164 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001165 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001166 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001167}
1168
1169
Chris Forbesb1dee272015-07-03 13:50:24 +12001170VK_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 Forbes39d8d752015-06-04 20:27:09 +12001182
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001183/* hook DextroyDevice to remove tableMap entry */
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001184VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001185{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001186 dispatch_key key = get_dispatch_key(device);
Chris Forbesb1dee272015-07-03 13:50:24 +12001187 VkLayerDispatchTable *pDisp = get_dispatch_table(shader_checker_device_table_map, device);
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001188 pDisp->DestroyDevice(device);
Chris Forbesb1dee272015-07-03 13:50:24 +12001189 shader_checker_device_table_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001190}
1191
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001192VkResult VKAPI vkCreateInstance(
1193 const VkInstanceCreateInfo* pCreateInfo,
1194 VkInstance* pInstance)
1195{
Chris Forbesb1dee272015-07-03 13:50:24 +12001196 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map,*pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001197 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
1198
1199 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001200 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 Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001206
Chris Forbesb1dee272015-07-03 13:50:24 +12001207 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001208 }
1209 return result;
1210}
1211
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001212/* hook DestroyInstance to remove tableInstanceMap entry */
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001213VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001214{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001215 dispatch_key key = get_dispatch_key(instance);
Chris Forbesb1dee272015-07-03 13:50:24 +12001216 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001217 pTable->DestroyInstance(instance);
Chris Forbesb1dee272015-07-03 13:50:24 +12001218
1219 // Clean up logging callback, if any
1220 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001221 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 Forbesb1dee272015-07-03 13:50:24 +12001225 }
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 Ashburn9a8a2e22015-05-19 16:34:53 -06001231}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001232
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001233VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001234 VkInstance instance,
1235 VkFlags msgFlags,
1236 const PFN_vkDbgMsgCallback pfnMsgCallback,
1237 void* pUserData,
1238 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001239{
Chris Forbesb1dee272015-07-03 13:50:24 +12001240 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 Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001247}
1248
1249VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001250 VkInstance instance,
1251 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001252{
Chris Forbesb1dee272015-07-03 13:50:24 +12001253 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 Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001258}
1259
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001260VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001261{
Chris Forbesb1dee272015-07-03 13:50:24 +12001262 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001263 return NULL;
1264
Jon Ashburn8fd08252015-05-28 16:25:02 -06001265 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001266 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
1267 initDeviceTable(shader_checker_device_table_map, (const VkBaseLayerObject *) dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001268 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001269 }
1270
Chris Forbes2778f302015-04-02 13:22:31 +13001271#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001272 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001273 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001274
Chris Forbesb1dee272015-07-03 13:50:24 +12001275 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001276 ADD_HOOK(vkCreateShaderModule);
Chris Forbes2778f302015-04-02 13:22:31 +13001277 ADD_HOOK(vkCreateShader);
Chia-I Wu08accc62015-07-07 11:50:03 +08001278 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001279 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001280 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001281 ADD_HOOK(vkCreateDescriptorSetLayout);
1282 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001283#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001284
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 Ashburnf6b33db2015-05-05 14:22:52 -06001291}
1292
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001293VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001294{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001295 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001296
Chris Forbesb1dee272015-07-03 13:50:24 +12001297 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001298 return NULL;
1299
Chris Forbesb1dee272015-07-03 13:50:24 +12001300 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
1301 initInstanceTable(shader_checker_instance_table_map, (const VkBaseLayerObject *) instance);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001302 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001303 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001304#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001305 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001306 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001307
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001308 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001309 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001310 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1311 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1312 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1313 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001314#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001315
Chris Forbesb1dee272015-07-03 13:50:24 +12001316
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 Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001319 if (fptr)
1320 return fptr;
1321
Chris Forbesb1dee272015-07-03 13:50:24 +12001322 {
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 Forbes2778f302015-04-02 13:22:31 +13001328}