blob: 1d5b08a57b13585dccd9f5e46878fb18ff0f9143 [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"
37#include "vk_layer_msg.h"
38#include "vk_layer_table.h"
Chris Forbesb1dee272015-07-03 13:50:24 +120039#include "vk_layer_logging.h"
Chris Forbes401784b2015-05-04 14:04:24 +120040#include "vk_enum_string_helper.h"
Chris Forbes6b2ead62015-04-17 10:13:28 +120041#include "shader_checker.h"
Chris Forbes2778f302015-04-02 13:22:31 +130042// The following is #included again to catch certain OS-specific functions
43// being used:
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060044#include "vk_loader_platform.h"
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -060045#include "vk_layer_extension_utils.h"
Chris Forbes2778f302015-04-02 13:22:31 +130046
GregF7c45bed2015-07-21 17:22:50 -060047#include "spirv/spirv.hpp"
Chris Forbes2778f302015-04-02 13:22:31 +130048
Chris Forbes2778f302015-04-02 13:22:31 +130049
Chris Forbesb1dee272015-07-03 13:50:24 +120050typedef struct _layer_data {
51 debug_report_data *report_data;
52 // TODO: put instance data here
53 VkDbgMsgCallback logging_callback;
54} layer_data;
55
56static std::unordered_map<void *, layer_data *> layer_data_map;
57static device_table_map shader_checker_device_table_map;
58static instance_table_map shader_checker_instance_table_map;
59
60
61template layer_data *get_my_data_ptr<layer_data>(
62 void *data_key,
63 std::unordered_map<void *, layer_data *> &data_map);
64
Chris Forbes9715d4a2015-07-10 09:06:54 +120065debug_report_data *mdd(void *object)
Chris Forbesb1dee272015-07-03 13:50:24 +120066{
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
75debug_report_data *mid(VkInstance object)
76{
77 dispatch_key key = get_dispatch_key(object);
Tobin Ehlisbfbac252015-09-01 11:46:36 -060078 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +120079#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 Forbesb6b8c462015-04-15 06:59:41 +120085static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +120086// TODO : This can be much smarter, using separate locks for separate global data
87static int globalLockInitialized = 0;
88static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +120089
Chris Forbes3a5e99a2015-04-10 11:41:20 +120090
Chris Forbes2b8493a2015-08-12 15:03:22 +120091std::unordered_map<uint32_t, std::vector<VkDescriptorSetLayoutBinding>*> descriptor_set_layout_map;
92
93VK_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
114std::unordered_map<uint32_t, std::vector<std::vector<VkDescriptorSetLayoutBinding>*>*> pipeline_layout_map;
115
116VK_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 Forbes3a5e99a2015-04-10 11:41:20 +1200139static void
140build_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:
GregF7c45bed2015-07-21 17:22:50 -0600157 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200158 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600159 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200160 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
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600183struct shader_module {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200184 /* the spirv image itself */
Chris Forbes3b1c4212015-04-08 10:11:59 +1200185 std::vector<uint32_t> words;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200186 /* a mapping of <id> to the first word of its def. this is useful because walking type
187 * trees requires jumping all over the instruction stream.
188 */
189 std::unordered_map<unsigned, unsigned> type_def_index;
Chris Forbesf044ec92015-06-05 15:01:08 +1200190 bool is_spirv;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200191
Chris Forbesb1dee272015-07-03 13:50:24 +1200192 shader_module(VkDevice dev, VkShaderModuleCreateInfo const *pCreateInfo) :
Chris Forbesf044ec92015-06-05 15:01:08 +1200193 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
194 type_def_index(),
195 is_spirv(true) {
196
197 if (words.size() < 5 || words[0] != spv::MagicNumber || words[1] != spv::Version) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200198 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200199 "Shader is not SPIR-V, most checks will not be possible");
Chris Forbesf044ec92015-06-05 15:01:08 +1200200 is_spirv = false;
201 return;
202 }
203
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200204
205 build_type_def_index(words, type_def_index);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200206 }
207};
208
209
Chris Forbes9715d4a2015-07-10 09:06:54 +1200210static std::unordered_map<uint64_t, shader_module *> shader_module_map;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600211
212struct shader_object {
213 std::string name;
214 struct shader_module *module;
215
216 shader_object(VkShaderCreateInfo const *pCreateInfo)
217 {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200218 module = shader_module_map[pCreateInfo->module.handle];
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600219 name = pCreateInfo->pName;
220 }
221};
Chris Forbes9715d4a2015-07-10 09:06:54 +1200222static std::unordered_map<uint64_t, shader_object *> shader_object_map;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200223
Chia-I Wu08accc62015-07-07 11:50:03 +0800224struct render_pass {
225 std::vector<std::vector<VkFormat>> subpass_color_formats;
226
227 render_pass(VkRenderPassCreateInfo const *pCreateInfo)
228 {
229 uint32_t i;
230
231 subpass_color_formats.reserve(pCreateInfo->subpassCount);
232 for (i = 0; i < pCreateInfo->subpassCount; i++) {
233 const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i];
234 std::vector<VkFormat> color_formats;
235 uint32_t j;
236
237 color_formats.reserve(subpass->colorCount);
238 for (j = 0; j < subpass->colorCount; j++) {
Cody Northropa505dda2015-08-04 11:16:41 -0600239 const uint32_t att = subpass->pColorAttachments[j].attachment;
Chia-I Wu08accc62015-07-07 11:50:03 +0800240 const VkFormat format = pCreateInfo->pAttachments[att].format;
241
242 color_formats.push_back(pCreateInfo->pAttachments[att].format);
243 }
244
245 subpass_color_formats.push_back(color_formats);
246 }
247 }
248};
Chris Forbes9715d4a2015-07-10 09:06:54 +1200249static std::unordered_map<uint64_t, render_pass *> render_pass_map;
Chia-I Wu08accc62015-07-07 11:50:03 +0800250
Chris Forbes3b1c4212015-04-08 10:11:59 +1200251
Chris Forbesb6b8c462015-04-15 06:59:41 +1200252static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200253init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200254{
Chris Forbesb1dee272015-07-03 13:50:24 +1200255 uint32_t report_flags = 0;
256 uint32_t debug_action = 0;
257 FILE *log_output = NULL;
258 const char *option_str;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200259 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200260 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
261 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200262
Chris Forbesb1dee272015-07-03 13:50:24 +1200263 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200264 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200265 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600266 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Chris Forbesb1dee272015-07-03 13:50:24 +1200267 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &my_data->logging_callback);
268 }
269
270 if (!globalLockInitialized)
271 {
272 // TODO/TBD: Need to delete this mutex sometime. How??? One
273 // suggestion is to call this during vkCreateInstance(), and then we
274 // can clean it up during vkDestroyInstance(). However, that requires
275 // that the layer have per-instance locks. We need to come back and
276 // address this soon.
277 loader_platform_thread_create_mutex(&globalLock);
278 globalLockInitialized = 1;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200279 }
280}
281
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600282static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600283 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600284 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600285 VK_API_VERSION,
286 VK_MAKE_VERSION(0, 1, 0),
287 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600288 }
Chris Forbes2778f302015-04-02 13:22:31 +1300289};
290
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600291VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600292 const char *pLayerName,
293 uint32_t *pCount,
294 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300295{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600296 /* shader checker does not have any global extensions */
297 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300298}
299
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600300VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600301 uint32_t *pCount,
302 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600303{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600304 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
305 shader_checker_global_layers,
306 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600307}
308
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600309VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600310 VkPhysicalDevice physicalDevice,
311 const char* pLayerName,
312 uint32_t* pCount,
313 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600314{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600315 /* Shader checker does not have any physical device extensions */
316 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
317}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600318
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600319VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600320 VkPhysicalDevice physicalDevice,
321 uint32_t* pCount,
322 VkLayerProperties* pProperties)
323{
324 /* Shader checker physical device layers are the same as global */
325 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
326 shader_checker_global_layers,
327 pCount, pProperties);
Jon Ashburn207a3af2015-06-10 16:43:31 -0600328}
Chris Forbes2778f302015-04-02 13:22:31 +1300329
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200330static char const *
331storage_class_name(unsigned sc)
332{
333 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600334 case spv::StorageClassInput: return "input";
335 case spv::StorageClassOutput: return "output";
336 case spv::StorageClassUniformConstant: return "const uniform";
337 case spv::StorageClassUniform: return "uniform";
338 case spv::StorageClassWorkgroupLocal: return "workgroup local";
339 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
340 case spv::StorageClassPrivateGlobal: return "private global";
341 case spv::StorageClassFunction: return "function";
342 case spv::StorageClassGeneric: return "generic";
Cody Northrop97e52d82015-04-20 14:09:40 -0600343 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600344 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200345 default: return "unknown";
346 }
347}
348
349
350/* returns ptr to null terminator */
351static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600352describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200353{
354 auto type_def_it = src->type_def_index.find(type);
355
356 if (type_def_it == src->type_def_index.end()) {
357 return dst + sprintf(dst, "undef");
358 }
359
360 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
361 unsigned opcode = code[0] & 0x0ffffu;
362 switch (opcode) {
363 case spv::OpTypeBool:
364 return dst + sprintf(dst, "bool");
365 case spv::OpTypeInt:
366 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
367 case spv::OpTypeFloat:
368 return dst + sprintf(dst, "float%d", code[2]);
369 case spv::OpTypeVector:
370 dst += sprintf(dst, "vec%d of ", code[3]);
371 return describe_type(dst, src, code[2]);
372 case spv::OpTypeMatrix:
373 dst += sprintf(dst, "mat%d of ", code[3]);
374 return describe_type(dst, src, code[2]);
375 case spv::OpTypeArray:
376 dst += sprintf(dst, "arr[%d] of ", code[3]);
377 return describe_type(dst, src, code[2]);
378 case spv::OpTypePointer:
379 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
380 return describe_type(dst, src, code[3]);
381 case spv::OpTypeStruct:
382 {
383 unsigned oplen = code[0] >> 16;
384 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600385 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200386 dst = describe_type(dst, src, code[i]);
387 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
388 }
389 return dst;
390 }
Chris Forbes0ae15802015-08-14 12:04:39 +1200391 case spv::OpTypeSampler:
392 return dst + sprintf(dst, "sampler");
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200393 default:
394 return dst + sprintf(dst, "oddtype");
395 }
396}
397
398
399static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600400types_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 +1200401{
402 auto a_type_def_it = a->type_def_index.find(a_type);
403 auto b_type_def_it = b->type_def_index.find(b_type);
404
405 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200406 return false;
407 }
408
409 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200410 return false;
411 }
412
413 /* walk two type trees together, and complain about differences */
414 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
415 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
416
417 unsigned a_opcode = a_code[0] & 0x0ffffu;
418 unsigned b_opcode = b_code[0] & 0x0ffffu;
419
Chris Forbesf3fc0332015-06-05 14:57:05 +1200420 if (b_arrayed && b_opcode == spv::OpTypeArray) {
421 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
422 return types_match(a, b, a_type, b_code[2], false);
423 }
424
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200425 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200426 return false;
427 }
428
429 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200430 /* 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 +1200431 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200432 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200433 case spv::OpTypeInt:
434 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200435 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200436 case spv::OpTypeFloat:
437 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200438 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200439 case spv::OpTypeVector:
440 case spv::OpTypeMatrix:
441 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200442 /* match on element type, count. these all have the same layout. we don't get here if
443 * b_arrayed -- that is handled above. */
444 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 +1200445 case spv::OpTypeStruct:
446 /* match on all element types */
447 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200448 if (b_arrayed) {
449 /* for the purposes of matching different levels of arrayness, structs are leaves. */
450 return false;
451 }
452
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200453 unsigned a_len = a_code[0] >> 16;
454 unsigned b_len = b_code[0] >> 16;
455
456 if (a_len != b_len) {
457 return false; /* structs cannot match if member counts differ */
458 }
459
Ian Elliott1cb62222015-04-17 11:05:04 -0600460 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200461 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200462 return false;
463 }
464 }
465
466 return true;
467 }
468 case spv::OpTypePointer:
469 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200470 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200471
472 default:
473 /* remaining types are CLisms, or may not appear in the interfaces we
474 * are interested in. Just claim no match.
475 */
476 return false;
477
478 }
479}
480
481
Chris Forbes06e8fc32015-04-13 12:14:52 +1200482static int
483value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
484{
485 auto it = map.find(id);
486 if (it == map.end())
487 return def;
488 else
489 return it->second;
490}
491
492
493struct interface_var {
494 uint32_t id;
495 uint32_t type_id;
496 /* TODO: collect the name, too? Isn't required to be present. */
497};
498
499
500static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200501collect_interface_by_location(VkDevice dev,
502 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200503 std::map<uint32_t, interface_var> &out,
504 std::map<uint32_t, interface_var> &builtins_out)
505{
506 unsigned int const *code = (unsigned int const *)&src->words[0];
507 size_t size = src->words.size();
508
Chris Forbes06e8fc32015-04-13 12:14:52 +1200509 std::unordered_map<unsigned, unsigned> var_locations;
510 std::unordered_map<unsigned, unsigned> var_builtins;
511
512 unsigned word = 5;
513 while (word < size) {
514
515 unsigned opcode = code[word] & 0x0ffffu;
516 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
517
518 /* We consider two interface models: SSO rendezvous-by-location, and
519 * builtins. Complain about anything that fits neither model.
520 */
521 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600522 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200523 var_locations[code[word+1]] = code[word+3];
524 }
525
Cody Northrop97e52d82015-04-20 14:09:40 -0600526 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200527 var_builtins[code[word+1]] = code[word+3];
528 }
529 }
530
531 /* TODO: handle grouped decorations */
532 /* TODO: handle index=1 dual source outputs from FS -- two vars will
533 * have the same location, and we DONT want to clobber. */
534
Ian Elliott1cb62222015-04-17 11:05:04 -0600535 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200536 int location = value_or_default(var_locations, code[word+2], -1);
537 int builtin = value_or_default(var_builtins, code[word+2], -1);
538
539 if (location == -1 && builtin == -1) {
540 /* No location defined, and not bound to an API builtin.
541 * The spec says nothing about how this case works (or doesn't)
542 * for interface matching.
543 */
Chris Forbes9715d4a2015-07-10 09:06:54 +1200544 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 +1200545 "var %d (type %d) in %s interface has no Location or Builtin decoration",
546 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes06e8fc32015-04-13 12:14:52 +1200547 }
548 else if (location != -1) {
549 /* A user-defined interface variable, with a location. */
550 interface_var v;
551 v.id = code[word+2];
552 v.type_id = code[word+1];
553 out[location] = v;
554 }
555 else {
556 /* A builtin interface variable */
557 interface_var v;
558 v.id = code[word+2];
559 v.type_id = code[word+1];
560 builtins_out[builtin] = v;
561 }
562 }
563
564 word += oplen;
565 }
566}
567
568
Chris Forbes0ae15802015-08-14 12:04:39 +1200569static void
570collect_interface_by_descriptor_slot(VkDevice dev,
571 shader_module const *src, spv::StorageClass sinterface,
572 std::map<std::pair<unsigned, unsigned>, interface_var> &out)
573{
574 unsigned int const *code = (unsigned int const *)&src->words[0];
575 size_t size = src->words.size();
576
577 std::unordered_map<unsigned, unsigned> var_sets;
578 std::unordered_map<unsigned, unsigned> var_bindings;
579
580 unsigned word = 5;
581 while (word < size) {
582
583 unsigned opcode = code[word] & 0x0ffffu;
584 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
585
586 /* We consider two interface models: SSO rendezvous-by-location, and
587 * builtins. Complain about anything that fits neither model.
588 */
589 if (opcode == spv::OpDecorate) {
590 if (code[word+2] == spv::DecorationDescriptorSet) {
591 var_sets[code[word+1]] = code[word+3];
592 }
593
594 if (code[word+2] == spv::DecorationBinding) {
595 var_bindings[code[word+1]] = code[word+3];
596 }
597 }
598
599 if (opcode == spv::OpVariable && (code[word+3] == spv::StorageClassUniform ||
600 code[word+3] == spv::StorageClassUniformConstant)) {
601 unsigned set = value_or_default(var_sets, code[word+2], 0);
602 unsigned binding = value_or_default(var_bindings, code[word+2], 0);
603
604 auto existing_it = out.find(std::make_pair(set, binding));
605 if (existing_it != out.end()) {
606 /* conflict within spv image */
607 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0,
608 SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
609 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
610 code[word+2], code[word+1], storage_class_name(sinterface),
611 existing_it->first.first, existing_it->first.second);
612 }
613
614 interface_var v;
615 v.id = code[word+2];
616 v.type_id = code[word+1];
617 out[std::make_pair(set, binding)] = v;
618 }
619
620 word += oplen;
621 }
622}
623
624
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600625VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
626 VkDevice device,
627 const VkShaderModuleCreateInfo *pCreateInfo,
628 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300629{
Chris Forbesb1dee272015-07-03 13:50:24 +1200630 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShaderModule(device, pCreateInfo, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200631
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600632 if (res == VK_SUCCESS) {
633 loader_platform_thread_lock_mutex(&globalLock);
634 shader_module_map[pShaderModule->handle] = new shader_module(device, pCreateInfo);
635 loader_platform_thread_unlock_mutex(&globalLock);
636 }
Chris Forbes2778f302015-04-02 13:22:31 +1300637 return res;
638}
639
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600640VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
641 VkDevice device,
642 const VkShaderCreateInfo *pCreateInfo,
643 VkShader *pShader)
644{
Chris Forbesb1dee272015-07-03 13:50:24 +1200645 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShader(device, pCreateInfo, pShader);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600646
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600647 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes9715d4a2015-07-10 09:06:54 +1200648 shader_object_map[pShader->handle] = new shader_object(pCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600649 loader_platform_thread_unlock_mutex(&globalLock);
650 return res;
651}
Chris Forbes2778f302015-04-02 13:22:31 +1300652
Chia-I Wu08accc62015-07-07 11:50:03 +0800653VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
654 VkDevice device,
655 const VkRenderPassCreateInfo *pCreateInfo,
656 VkRenderPass *pRenderPass)
657{
Chia-I Wu08accc62015-07-07 11:50:03 +0800658 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateRenderPass(device, pCreateInfo, pRenderPass);
659
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600660 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes9715d4a2015-07-10 09:06:54 +1200661 render_pass_map[pRenderPass->handle] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800662 loader_platform_thread_unlock_mutex(&globalLock);
663 return res;
664}
665
Chris Forbesee99b9b2015-05-25 11:13:22 +1200666static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200667validate_interface_between_stages(VkDevice dev,
668 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600669 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200670 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200671{
672 std::map<uint32_t, interface_var> outputs;
673 std::map<uint32_t, interface_var> inputs;
674
675 std::map<uint32_t, interface_var> builtin_outputs;
676 std::map<uint32_t, interface_var> builtin_inputs;
677
Chris Forbesee99b9b2015-05-25 11:13:22 +1200678 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200679
Chris Forbesb1dee272015-07-03 13:50:24 +1200680 collect_interface_by_location(dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
681 collect_interface_by_location(dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200682
683 auto a_it = outputs.begin();
684 auto b_it = inputs.begin();
685
686 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600687 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
688 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
689 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200690 auto a_first = a_at_end ? 0 : a_it->first;
691 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600692
693 if (b_at_end || a_first < b_first) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200694 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200695 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name);
Chris Forbes41002452015-04-08 10:19:16 +1200696 a_it++;
697 }
David Pinedod8f83d82015-04-27 16:36:17 -0600698 else if (a_at_end || a_first > b_first) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200699 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200700 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200701 pass = false;
Chris Forbes41002452015-04-08 10:19:16 +1200702 b_it++;
703 }
704 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200705 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 +1200706 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200707 }
708 else {
709 char producer_type[1024];
710 char consumer_type[1024];
711 describe_type(producer_type, producer, a_it->second.type_id);
712 describe_type(consumer_type, consumer, b_it->second.type_id);
713
Chris Forbes9715d4a2015-07-10 09:06:54 +1200714 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 +1200715 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200716 pass = false;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200717 }
Chris Forbes41002452015-04-08 10:19:16 +1200718 a_it++;
719 b_it++;
720 }
721 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200722
723 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200724}
725
726
Chris Forbes3616b462015-04-08 10:37:20 +1200727enum FORMAT_TYPE {
728 FORMAT_TYPE_UNDEFINED,
729 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
730 FORMAT_TYPE_SINT,
731 FORMAT_TYPE_UINT,
732};
733
734
735static unsigned
736get_format_type(VkFormat fmt) {
737 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800738 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200739 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800740 case VK_FORMAT_R8_SINT:
741 case VK_FORMAT_R8G8_SINT:
742 case VK_FORMAT_R8G8B8_SINT:
743 case VK_FORMAT_R8G8B8A8_SINT:
744 case VK_FORMAT_R16_SINT:
745 case VK_FORMAT_R16G16_SINT:
746 case VK_FORMAT_R16G16B16_SINT:
747 case VK_FORMAT_R16G16B16A16_SINT:
748 case VK_FORMAT_R32_SINT:
749 case VK_FORMAT_R32G32_SINT:
750 case VK_FORMAT_R32G32B32_SINT:
751 case VK_FORMAT_R32G32B32A32_SINT:
752 case VK_FORMAT_B8G8R8_SINT:
753 case VK_FORMAT_B8G8R8A8_SINT:
754 case VK_FORMAT_R10G10B10A2_SINT:
755 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200756 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800757 case VK_FORMAT_R8_UINT:
758 case VK_FORMAT_R8G8_UINT:
759 case VK_FORMAT_R8G8B8_UINT:
760 case VK_FORMAT_R8G8B8A8_UINT:
761 case VK_FORMAT_R16_UINT:
762 case VK_FORMAT_R16G16_UINT:
763 case VK_FORMAT_R16G16B16_UINT:
764 case VK_FORMAT_R16G16B16A16_UINT:
765 case VK_FORMAT_R32_UINT:
766 case VK_FORMAT_R32G32_UINT:
767 case VK_FORMAT_R32G32B32_UINT:
768 case VK_FORMAT_R32G32B32A32_UINT:
769 case VK_FORMAT_B8G8R8_UINT:
770 case VK_FORMAT_B8G8R8A8_UINT:
771 case VK_FORMAT_R10G10B10A2_UINT:
772 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200773 return FORMAT_TYPE_UINT;
774 default:
775 return FORMAT_TYPE_FLOAT;
776 }
777}
778
779
Chris Forbes156a1162015-05-04 14:04:06 +1200780/* characterizes a SPIR-V type appearing in an interface to a FF stage,
781 * for comparison to a VkFormat's characterization above. */
782static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600783get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200784{
785 auto type_def_it = src->type_def_index.find(type);
786
787 if (type_def_it == src->type_def_index.end()) {
788 return FORMAT_TYPE_UNDEFINED;
789 }
790
791 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
792 unsigned opcode = code[0] & 0x0ffffu;
793 switch (opcode) {
794 case spv::OpTypeInt:
795 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
796 case spv::OpTypeFloat:
797 return FORMAT_TYPE_FLOAT;
798 case spv::OpTypeVector:
799 return get_fundamental_type(src, code[2]);
800 case spv::OpTypeMatrix:
801 return get_fundamental_type(src, code[2]);
802 case spv::OpTypeArray:
803 return get_fundamental_type(src, code[2]);
804 case spv::OpTypePointer:
805 return get_fundamental_type(src, code[3]);
806 default:
807 return FORMAT_TYPE_UNDEFINED;
808 }
809}
810
811
Chris Forbesee99b9b2015-05-25 11:13:22 +1200812static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200813validate_vi_consistency(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200814{
815 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
816 * each binding should be specified only once.
817 */
818 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200819 bool pass = true;
820
821 for (unsigned i = 0; i < vi->bindingCount; i++) {
822 auto desc = &vi->pVertexBindingDescriptions[i];
823 auto & binding = bindings[desc->binding];
824 if (binding) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200825 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INCONSISTENT_VI, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200826 "Duplicate vertex input binding descriptions for binding %d", desc->binding);
Chris Forbes280ba2c2015-06-12 11:16:41 +1200827 pass = false;
828 }
829 else {
830 binding = desc;
831 }
832 }
833
834 return pass;
835}
836
837
838static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200839validate_vi_against_vs_inputs(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200840{
841 std::map<uint32_t, interface_var> inputs;
842 /* we collect builtin inputs, but they will never appear in the VI state --
843 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
844 */
845 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200846 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200847
Chris Forbesb1dee272015-07-03 13:50:24 +1200848 collect_interface_by_location(dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200849
850 /* Build index by location */
851 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200852 if (vi) {
853 for (unsigned i = 0; i < vi->attributeCount; i++)
854 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
855 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200856
857 auto it_a = attribs.begin();
858 auto it_b = inputs.begin();
859
David Pinedod8f83d82015-04-27 16:36:17 -0600860 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
861 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
862 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200863 auto a_first = a_at_end ? 0 : it_a->first;
864 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600865 if (b_at_end || a_first < b_first) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200866 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200867 "Vertex attribute at location %d not consumed by VS", a_first);
Chris Forbes772d03b2015-04-08 10:36:37 +1200868 it_a++;
869 }
David Pinedod8f83d82015-04-27 16:36:17 -0600870 else if (a_at_end || b_first < a_first) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200871 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200872 "VS consumes input at location %d but not provided", b_first);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200873 pass = false;
Chris Forbes772d03b2015-04-08 10:36:37 +1200874 it_b++;
875 }
876 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200877 unsigned attrib_type = get_format_type(it_a->second->format);
878 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
879
880 /* type checking */
881 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
882 char vs_type[1024];
883 describe_type(vs_type, vs, it_b->second.type_id);
Chris Forbes9715d4a2015-07-10 09:06:54 +1200884 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 +1200885 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Chris Forbes401784b2015-05-04 14:04:24 +1200886 string_VkFormat(it_a->second->format), a_first, vs_type);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200887 pass = false;
Chris Forbes401784b2015-05-04 14:04:24 +1200888 }
889
Chris Forbes6b2ead62015-04-17 10:13:28 +1200890 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200891 it_a++;
892 it_b++;
893 }
894 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200895
896 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200897}
898
899
Chris Forbesee99b9b2015-05-25 11:13:22 +1200900static bool
Chia-I Wu08accc62015-07-07 11:50:03 +0800901validate_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 +1200902{
Chia-I Wu08accc62015-07-07 11:50:03 +0800903 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200904 std::map<uint32_t, interface_var> outputs;
905 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200906 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200907
908 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
909
Chris Forbesb1dee272015-07-03 13:50:24 +1200910 collect_interface_by_location(dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200911
912 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
913 * and all color attachment should be UNORM/SNORM/FLOAT.
914 */
915 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200916 if (outputs.size()) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200917 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200918 "Should not have user-defined FS outputs when using broadcast");
Chris Forbesee99b9b2015-05-25 11:13:22 +1200919 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200920 }
921
Chia-I Wu08accc62015-07-07 11:50:03 +0800922 for (unsigned i = 0; i < color_formats.size(); i++) {
923 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200924 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200925 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 +1200926 "CB format should not be SINT or UINT when using broadcast");
Chris Forbesee99b9b2015-05-25 11:13:22 +1200927 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200928 }
929 }
930
Chris Forbesee99b9b2015-05-25 11:13:22 +1200931 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200932 }
933
934 auto it = outputs.begin();
935 uint32_t attachment = 0;
936
937 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
938 * are currently dense, but the parallel with matching between shader stages is nice.
939 */
940
Chris Forbes9715d4a2015-07-10 09:06:54 +1200941 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200942 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
943 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200944 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200945 "FS writes to output location %d with no matching attachment", it->first);
Chris Forbes3616b462015-04-08 10:37:20 +1200946 it++;
947 }
948 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200949 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200950 "Attachment %d not written by FS", attachment);
Chris Forbes3616b462015-04-08 10:37:20 +1200951 attachment++;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200952 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200953 }
954 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200955 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800956 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200957
958 /* type checking */
959 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
960 char fs_type[1024];
961 describe_type(fs_type, fs, it->second.type_id);
Chris Forbes9715d4a2015-07-10 09:06:54 +1200962 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 +1200963 "Attachment %d of type `%s` does not match FS output type of `%s`",
Chia-I Wu08accc62015-07-07 11:50:03 +0800964 attachment, string_VkFormat(color_formats[attachment]), fs_type);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200965 pass = false;
Chris Forbes46d31e52015-05-04 14:20:10 +1200966 }
967
Chris Forbes6b2ead62015-04-17 10:13:28 +1200968 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200969 it++;
970 attachment++;
971 }
972 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200973
974 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200975}
976
977
Chris Forbesf044ec92015-06-05 15:01:08 +1200978struct shader_stage_attributes {
979 char const * const name;
980 bool arrayed_input;
981};
982
983
984static shader_stage_attributes
985shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
986 { "vertex shader", false },
987 { "tessellation control shader", true },
988 { "tessellation evaluation shader", false },
989 { "geometry shader", true },
990 { "fragment shader", false },
991};
992
993
Chris Forbes81874ba2015-06-04 20:23:00 +1200994static bool
Chris Forbes36a372f2015-07-24 13:53:47 +1200995validate_graphics_pipeline(VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +1200996{
Chris Forbesf6800b52015-04-08 10:16:45 +1200997 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
998 * before trying to do anything more: */
999
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001000 shader_module const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
Chris Forbesf044ec92015-06-05 15:01:08 +12001001 memset(shaders, 0, sizeof(shaders));
Chia-I Wu08accc62015-07-07 11:50:03 +08001002 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001003 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001004 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001005
Chris Forbes7f963832015-05-29 14:55:18 +12001006 loader_platform_thread_lock_mutex(&globalLock);
1007
Tony Barbour92503c62015-07-13 15:28:47 -06001008 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001009 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1010 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001011
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001012 if (pStage->stage < VK_SHADER_STAGE_VERTEX || pStage->stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes9715d4a2015-07-10 09:06:54 +12001013 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +12001014 "Unknown shader stage %d", pStage->stage);
Chris Forbes6b2ead62015-04-17 10:13:28 +12001015 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001016 else {
Chris Forbes9715d4a2015-07-10 09:06:54 +12001017 struct shader_object *shader = shader_object_map[pStage->shader.handle];
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001018 shaders[pStage->stage] = shader->module;
Chris Forbesf044ec92015-06-05 15:01:08 +12001019 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001020 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001021 }
1022
Chia-I Wu08accc62015-07-07 11:50:03 +08001023 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Chris Forbes9715d4a2015-07-10 09:06:54 +12001024 rp = render_pass_map[pCreateInfo->renderPass.handle];
Chia-I Wu08accc62015-07-07 11:50:03 +08001025
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001026 vi = pCreateInfo->pVertexInputState;
1027
Chris Forbes280ba2c2015-06-12 11:16:41 +12001028 if (vi) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001029 pass = validate_vi_consistency(dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001030 }
1031
Chris Forbesf044ec92015-06-05 15:01:08 +12001032 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001033 pass = validate_vi_against_vs_inputs(dev, vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001034 }
1035
Chris Forbesf044ec92015-06-05 15:01:08 +12001036 /* TODO: enforce rules about present combinations of shaders */
1037 int producer = VK_SHADER_STAGE_VERTEX;
1038 int consumer = VK_SHADER_STAGE_GEOMETRY;
1039
1040 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
1041 producer++;
1042 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001043 }
1044
Tony Barbour0102a902015-06-11 15:04:25 -06001045 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001046 assert(shaders[producer]);
1047 if (shaders[consumer]) {
1048 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001049 pass = validate_interface_between_stages(dev,
1050 shaders[producer], shader_stage_attribs[producer].name,
Chris Forbesf044ec92015-06-05 15:01:08 +12001051 shaders[consumer], shader_stage_attribs[consumer].name,
1052 shader_stage_attribs[consumer].arrayed_input) && pass;
1053 }
1054
1055 producer = consumer;
1056 }
1057 }
1058
Chia-I Wu08accc62015-07-07 11:50:03 +08001059 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && rp) {
1060 pass = validate_fs_outputs_against_render_pass(dev, shaders[VK_SHADER_STAGE_FRAGMENT], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001061 }
1062
Chris Forbes7f963832015-05-29 14:55:18 +12001063 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001064 return pass;
1065}
1066
Jon Ashburnc669cc62015-07-09 15:02:25 -06001067//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001068VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001069vkCreateGraphicsPipelines(VkDevice device,
1070 VkPipelineCache pipelineCache,
1071 uint32_t count,
1072 const VkGraphicsPipelineCreateInfo *pCreateInfos,
1073 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001074{
Chris Forbes36a372f2015-07-24 13:53:47 +12001075 bool pass = true;
1076 for (uint32_t i = 0; i < count; i++) {
1077 pass = validate_graphics_pipeline(device, &pCreateInfos[i]) && pass;
1078 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001079
1080 if (pass) {
1081 /* The driver is allowed to crash if passed junk. Only actually create the
1082 * pipeline if we didn't run into any showstoppers above.
1083 */
Jon Ashburnc669cc62015-07-09 15:02:25 -06001084 return get_dispatch_table(shader_checker_device_table_map, device)->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001085 }
1086 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001087 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001088 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001089}
1090
1091
Chris Forbesb1dee272015-07-03 13:50:24 +12001092VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
1093{
1094 VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
1095 VkResult result = pDeviceTable->CreateDevice(gpu, pCreateInfo, pDevice);
1096 if (result == VK_SUCCESS) {
1097 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
1098 VkLayerDispatchTable *pTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
1099 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
1100 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1101 }
1102 return result;
1103}
Chris Forbes39d8d752015-06-04 20:27:09 +12001104
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001105/* hook DextroyDevice to remove tableMap entry */
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001106VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001107{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001108 dispatch_key key = get_dispatch_key(device);
Chris Forbesb1dee272015-07-03 13:50:24 +12001109 VkLayerDispatchTable *pDisp = get_dispatch_table(shader_checker_device_table_map, device);
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001110 pDisp->DestroyDevice(device);
Chris Forbesb1dee272015-07-03 13:50:24 +12001111 shader_checker_device_table_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001112}
1113
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001114VkResult VKAPI vkCreateInstance(
1115 const VkInstanceCreateInfo* pCreateInfo,
1116 VkInstance* pInstance)
1117{
Chris Forbesb1dee272015-07-03 13:50:24 +12001118 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map,*pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001119 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
1120
1121 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001122 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1123 my_data->report_data = debug_report_create_instance(
1124 pTable,
1125 *pInstance,
1126 pCreateInfo->extensionCount,
1127 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001128
Chris Forbesb1dee272015-07-03 13:50:24 +12001129 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001130 }
1131 return result;
1132}
1133
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001134/* hook DestroyInstance to remove tableInstanceMap entry */
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001135VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001136{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001137 dispatch_key key = get_dispatch_key(instance);
Chris Forbesb1dee272015-07-03 13:50:24 +12001138 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001139 pTable->DestroyInstance(instance);
Chris Forbesb1dee272015-07-03 13:50:24 +12001140
1141 // Clean up logging callback, if any
1142 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
1143 if (my_data->logging_callback) {
1144 layer_destroy_msg_callback(my_data->report_data, my_data->logging_callback);
1145 }
1146
1147 layer_debug_report_destroy_instance(my_data->report_data);
1148 layer_data_map.erase(pTable);
1149
1150 shader_checker_instance_table_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001151}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001152
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001153VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001154 VkInstance instance,
1155 VkFlags msgFlags,
1156 const PFN_vkDbgMsgCallback pfnMsgCallback,
1157 void* pUserData,
1158 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001159{
Chris Forbesb1dee272015-07-03 13:50:24 +12001160 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1161 VkResult res = pTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1162 if (VK_SUCCESS == res) {
1163 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1164 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1165 }
1166 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001167}
1168
1169VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001170 VkInstance instance,
1171 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001172{
Chris Forbesb1dee272015-07-03 13:50:24 +12001173 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1174 VkResult res = pTable->DbgDestroyMsgCallback(instance, msgCallback);
1175 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1176 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1177 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001178}
1179
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001180VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001181{
Chris Forbesb1dee272015-07-03 13:50:24 +12001182 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001183 return NULL;
1184
Jon Ashburn8fd08252015-05-28 16:25:02 -06001185 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001186 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
1187 initDeviceTable(shader_checker_device_table_map, (const VkBaseLayerObject *) dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001188 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001189 }
1190
Chris Forbes2778f302015-04-02 13:22:31 +13001191#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001192 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001193 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001194
Chris Forbesb1dee272015-07-03 13:50:24 +12001195 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001196 ADD_HOOK(vkCreateShaderModule);
Chris Forbes2778f302015-04-02 13:22:31 +13001197 ADD_HOOK(vkCreateShader);
Chia-I Wu08accc62015-07-07 11:50:03 +08001198 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001199 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001200 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001201 ADD_HOOK(vkCreateDescriptorSetLayout);
1202 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001203#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001204
1205 VkLayerDispatchTable* pTable = get_dispatch_table(shader_checker_device_table_map, dev);
1206 {
1207 if (pTable->GetDeviceProcAddr == NULL)
1208 return NULL;
1209 return pTable->GetDeviceProcAddr(dev, funcName);
1210 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001211}
1212
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001213VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001214{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001215 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001216
Chris Forbesb1dee272015-07-03 13:50:24 +12001217 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001218 return NULL;
1219
Chris Forbesb1dee272015-07-03 13:50:24 +12001220 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
1221 initInstanceTable(shader_checker_instance_table_map, (const VkBaseLayerObject *) instance);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001222 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001223 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001224#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001225 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001226 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001227
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001228 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001229 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001230 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1231 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1232 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1233 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001234#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001235
Chris Forbesb1dee272015-07-03 13:50:24 +12001236
1237 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1238 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001239 if (fptr)
1240 return fptr;
1241
Chris Forbesb1dee272015-07-03 13:50:24 +12001242 {
1243 VkLayerInstanceDispatchTable* pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1244 if (pTable->GetInstanceProcAddr == NULL)
1245 return NULL;
1246 return pTable->GetInstanceProcAddr(instance, funcName);
1247 }
Chris Forbes2778f302015-04-02 13:22:31 +13001248}