blob: 678b5671f2943bc7150833563a7c143c1d37f474 [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
91static void
92build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
93{
94 unsigned int const *code = (unsigned int const *)&words[0];
95 size_t size = words.size();
96
97 unsigned word = 5;
98 while (word < size) {
99 unsigned opcode = code[word] & 0x0ffffu;
100 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
101
102 switch (opcode) {
103 case spv::OpTypeVoid:
104 case spv::OpTypeBool:
105 case spv::OpTypeInt:
106 case spv::OpTypeFloat:
107 case spv::OpTypeVector:
108 case spv::OpTypeMatrix:
GregF7c45bed2015-07-21 17:22:50 -0600109 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200110 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600111 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200112 case spv::OpTypeArray:
113 case spv::OpTypeRuntimeArray:
114 case spv::OpTypeStruct:
115 case spv::OpTypeOpaque:
116 case spv::OpTypePointer:
117 case spv::OpTypeFunction:
118 case spv::OpTypeEvent:
119 case spv::OpTypeDeviceEvent:
120 case spv::OpTypeReserveId:
121 case spv::OpTypeQueue:
122 case spv::OpTypePipe:
123 type_def_index[code[word+1]] = word;
124 break;
125
126 default:
127 /* We only care about type definitions */
128 break;
129 }
130
131 word += oplen;
132 }
133}
134
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600135struct shader_module {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200136 /* the spirv image itself */
Chris Forbes3b1c4212015-04-08 10:11:59 +1200137 std::vector<uint32_t> words;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200138 /* a mapping of <id> to the first word of its def. this is useful because walking type
139 * trees requires jumping all over the instruction stream.
140 */
141 std::unordered_map<unsigned, unsigned> type_def_index;
Chris Forbesf044ec92015-06-05 15:01:08 +1200142 bool is_spirv;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200143
Chris Forbesb1dee272015-07-03 13:50:24 +1200144 shader_module(VkDevice dev, VkShaderModuleCreateInfo const *pCreateInfo) :
Chris Forbesf044ec92015-06-05 15:01:08 +1200145 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
146 type_def_index(),
147 is_spirv(true) {
148
149 if (words.size() < 5 || words[0] != spv::MagicNumber || words[1] != spv::Version) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200150 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 +1200151 "Shader is not SPIR-V, most checks will not be possible");
Chris Forbesf044ec92015-06-05 15:01:08 +1200152 is_spirv = false;
153 return;
154 }
155
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200156
157 build_type_def_index(words, type_def_index);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200158 }
159};
160
161
Chris Forbes9715d4a2015-07-10 09:06:54 +1200162static std::unordered_map<uint64_t, shader_module *> shader_module_map;
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600163
164struct shader_object {
165 std::string name;
166 struct shader_module *module;
167
168 shader_object(VkShaderCreateInfo const *pCreateInfo)
169 {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200170 module = shader_module_map[pCreateInfo->module.handle];
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600171 name = pCreateInfo->pName;
172 }
173};
Chris Forbes9715d4a2015-07-10 09:06:54 +1200174static std::unordered_map<uint64_t, shader_object *> shader_object_map;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200175
Chia-I Wu08accc62015-07-07 11:50:03 +0800176struct render_pass {
177 std::vector<std::vector<VkFormat>> subpass_color_formats;
178
179 render_pass(VkRenderPassCreateInfo const *pCreateInfo)
180 {
181 uint32_t i;
182
183 subpass_color_formats.reserve(pCreateInfo->subpassCount);
184 for (i = 0; i < pCreateInfo->subpassCount; i++) {
185 const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i];
186 std::vector<VkFormat> color_formats;
187 uint32_t j;
188
189 color_formats.reserve(subpass->colorCount);
190 for (j = 0; j < subpass->colorCount; j++) {
Cody Northropa505dda2015-08-04 11:16:41 -0600191 const uint32_t att = subpass->pColorAttachments[j].attachment;
Chia-I Wu08accc62015-07-07 11:50:03 +0800192 const VkFormat format = pCreateInfo->pAttachments[att].format;
193
194 color_formats.push_back(pCreateInfo->pAttachments[att].format);
195 }
196
197 subpass_color_formats.push_back(color_formats);
198 }
199 }
200};
Chris Forbes9715d4a2015-07-10 09:06:54 +1200201static std::unordered_map<uint64_t, render_pass *> render_pass_map;
Chia-I Wu08accc62015-07-07 11:50:03 +0800202
Chris Forbes3b1c4212015-04-08 10:11:59 +1200203
Chris Forbesb6b8c462015-04-15 06:59:41 +1200204static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200205init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200206{
Chris Forbesb1dee272015-07-03 13:50:24 +1200207 uint32_t report_flags = 0;
208 uint32_t debug_action = 0;
209 FILE *log_output = NULL;
210 const char *option_str;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200211 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200212 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
213 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200214
Chris Forbesb1dee272015-07-03 13:50:24 +1200215 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200216 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200217 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600218 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Chris Forbesb1dee272015-07-03 13:50:24 +1200219 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &my_data->logging_callback);
220 }
221
222 if (!globalLockInitialized)
223 {
224 // TODO/TBD: Need to delete this mutex sometime. How??? One
225 // suggestion is to call this during vkCreateInstance(), and then we
226 // can clean it up during vkDestroyInstance(). However, that requires
227 // that the layer have per-instance locks. We need to come back and
228 // address this soon.
229 loader_platform_thread_create_mutex(&globalLock);
230 globalLockInitialized = 1;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200231 }
232}
233
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600234static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600235 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600236 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600237 VK_API_VERSION,
238 VK_MAKE_VERSION(0, 1, 0),
239 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600240 }
Chris Forbes2778f302015-04-02 13:22:31 +1300241};
242
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600243VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600244 const char *pLayerName,
245 uint32_t *pCount,
246 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300247{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600248 /* shader checker does not have any global extensions */
249 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300250}
251
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600252VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600253 uint32_t *pCount,
254 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600255{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600256 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
257 shader_checker_global_layers,
258 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600259}
260
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600261VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600262 VkPhysicalDevice physicalDevice,
263 const char* pLayerName,
264 uint32_t* pCount,
265 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600266{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600267 /* Shader checker does not have any physical device extensions */
268 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
269}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600270
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600271VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600272 VkPhysicalDevice physicalDevice,
273 uint32_t* pCount,
274 VkLayerProperties* pProperties)
275{
276 /* Shader checker physical device layers are the same as global */
277 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
278 shader_checker_global_layers,
279 pCount, pProperties);
Jon Ashburn207a3af2015-06-10 16:43:31 -0600280}
Chris Forbes2778f302015-04-02 13:22:31 +1300281
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200282static char const *
283storage_class_name(unsigned sc)
284{
285 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600286 case spv::StorageClassInput: return "input";
287 case spv::StorageClassOutput: return "output";
288 case spv::StorageClassUniformConstant: return "const uniform";
289 case spv::StorageClassUniform: return "uniform";
290 case spv::StorageClassWorkgroupLocal: return "workgroup local";
291 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
292 case spv::StorageClassPrivateGlobal: return "private global";
293 case spv::StorageClassFunction: return "function";
294 case spv::StorageClassGeneric: return "generic";
Cody Northrop97e52d82015-04-20 14:09:40 -0600295 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600296 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200297 default: return "unknown";
298 }
299}
300
301
302/* returns ptr to null terminator */
303static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600304describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200305{
306 auto type_def_it = src->type_def_index.find(type);
307
308 if (type_def_it == src->type_def_index.end()) {
309 return dst + sprintf(dst, "undef");
310 }
311
312 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
313 unsigned opcode = code[0] & 0x0ffffu;
314 switch (opcode) {
315 case spv::OpTypeBool:
316 return dst + sprintf(dst, "bool");
317 case spv::OpTypeInt:
318 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
319 case spv::OpTypeFloat:
320 return dst + sprintf(dst, "float%d", code[2]);
321 case spv::OpTypeVector:
322 dst += sprintf(dst, "vec%d of ", code[3]);
323 return describe_type(dst, src, code[2]);
324 case spv::OpTypeMatrix:
325 dst += sprintf(dst, "mat%d of ", code[3]);
326 return describe_type(dst, src, code[2]);
327 case spv::OpTypeArray:
328 dst += sprintf(dst, "arr[%d] of ", code[3]);
329 return describe_type(dst, src, code[2]);
330 case spv::OpTypePointer:
331 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
332 return describe_type(dst, src, code[3]);
333 case spv::OpTypeStruct:
334 {
335 unsigned oplen = code[0] >> 16;
336 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600337 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200338 dst = describe_type(dst, src, code[i]);
339 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
340 }
341 return dst;
342 }
343 default:
344 return dst + sprintf(dst, "oddtype");
345 }
346}
347
348
349static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600350types_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 +1200351{
352 auto a_type_def_it = a->type_def_index.find(a_type);
353 auto b_type_def_it = b->type_def_index.find(b_type);
354
355 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200356 return false;
357 }
358
359 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200360 return false;
361 }
362
363 /* walk two type trees together, and complain about differences */
364 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
365 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
366
367 unsigned a_opcode = a_code[0] & 0x0ffffu;
368 unsigned b_opcode = b_code[0] & 0x0ffffu;
369
Chris Forbesf3fc0332015-06-05 14:57:05 +1200370 if (b_arrayed && b_opcode == spv::OpTypeArray) {
371 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
372 return types_match(a, b, a_type, b_code[2], false);
373 }
374
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200375 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200376 return false;
377 }
378
379 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200380 /* 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 +1200381 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200382 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200383 case spv::OpTypeInt:
384 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200385 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200386 case spv::OpTypeFloat:
387 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200388 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200389 case spv::OpTypeVector:
390 case spv::OpTypeMatrix:
391 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200392 /* match on element type, count. these all have the same layout. we don't get here if
393 * b_arrayed -- that is handled above. */
394 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 +1200395 case spv::OpTypeStruct:
396 /* match on all element types */
397 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200398 if (b_arrayed) {
399 /* for the purposes of matching different levels of arrayness, structs are leaves. */
400 return false;
401 }
402
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200403 unsigned a_len = a_code[0] >> 16;
404 unsigned b_len = b_code[0] >> 16;
405
406 if (a_len != b_len) {
407 return false; /* structs cannot match if member counts differ */
408 }
409
Ian Elliott1cb62222015-04-17 11:05:04 -0600410 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200411 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200412 return false;
413 }
414 }
415
416 return true;
417 }
418 case spv::OpTypePointer:
419 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200420 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200421
422 default:
423 /* remaining types are CLisms, or may not appear in the interfaces we
424 * are interested in. Just claim no match.
425 */
426 return false;
427
428 }
429}
430
431
Chris Forbes06e8fc32015-04-13 12:14:52 +1200432static int
433value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
434{
435 auto it = map.find(id);
436 if (it == map.end())
437 return def;
438 else
439 return it->second;
440}
441
442
443struct interface_var {
444 uint32_t id;
445 uint32_t type_id;
446 /* TODO: collect the name, too? Isn't required to be present. */
447};
448
449
450static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200451collect_interface_by_location(VkDevice dev,
452 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200453 std::map<uint32_t, interface_var> &out,
454 std::map<uint32_t, interface_var> &builtins_out)
455{
456 unsigned int const *code = (unsigned int const *)&src->words[0];
457 size_t size = src->words.size();
458
Chris Forbes06e8fc32015-04-13 12:14:52 +1200459 std::unordered_map<unsigned, unsigned> var_locations;
460 std::unordered_map<unsigned, unsigned> var_builtins;
461
462 unsigned word = 5;
463 while (word < size) {
464
465 unsigned opcode = code[word] & 0x0ffffu;
466 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
467
468 /* We consider two interface models: SSO rendezvous-by-location, and
469 * builtins. Complain about anything that fits neither model.
470 */
471 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600472 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200473 var_locations[code[word+1]] = code[word+3];
474 }
475
Cody Northrop97e52d82015-04-20 14:09:40 -0600476 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200477 var_builtins[code[word+1]] = code[word+3];
478 }
479 }
480
481 /* TODO: handle grouped decorations */
482 /* TODO: handle index=1 dual source outputs from FS -- two vars will
483 * have the same location, and we DONT want to clobber. */
484
Ian Elliott1cb62222015-04-17 11:05:04 -0600485 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200486 int location = value_or_default(var_locations, code[word+2], -1);
487 int builtin = value_or_default(var_builtins, code[word+2], -1);
488
489 if (location == -1 && builtin == -1) {
490 /* No location defined, and not bound to an API builtin.
491 * The spec says nothing about how this case works (or doesn't)
492 * for interface matching.
493 */
Chris Forbes9715d4a2015-07-10 09:06:54 +1200494 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 +1200495 "var %d (type %d) in %s interface has no Location or Builtin decoration",
496 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes06e8fc32015-04-13 12:14:52 +1200497 }
498 else if (location != -1) {
499 /* A user-defined interface variable, with a location. */
500 interface_var v;
501 v.id = code[word+2];
502 v.type_id = code[word+1];
503 out[location] = v;
504 }
505 else {
506 /* A builtin interface variable */
507 interface_var v;
508 v.id = code[word+2];
509 v.type_id = code[word+1];
510 builtins_out[builtin] = v;
511 }
512 }
513
514 word += oplen;
515 }
516}
517
518
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600519VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
520 VkDevice device,
521 const VkShaderModuleCreateInfo *pCreateInfo,
522 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300523{
Chris Forbes7f963832015-05-29 14:55:18 +1200524 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbesb1dee272015-07-03 13:50:24 +1200525 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShaderModule(device, pCreateInfo, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200526
Chris Forbes9715d4a2015-07-10 09:06:54 +1200527 shader_module_map[pShaderModule->handle] = new shader_module(device, pCreateInfo);
Chris Forbes7f963832015-05-29 14:55:18 +1200528 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes2778f302015-04-02 13:22:31 +1300529 return res;
530}
531
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600532VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
533 VkDevice device,
534 const VkShaderCreateInfo *pCreateInfo,
535 VkShader *pShader)
536{
537 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbesb1dee272015-07-03 13:50:24 +1200538 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShader(device, pCreateInfo, pShader);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600539
Chris Forbes9715d4a2015-07-10 09:06:54 +1200540 shader_object_map[pShader->handle] = new shader_object(pCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600541 loader_platform_thread_unlock_mutex(&globalLock);
542 return res;
543}
Chris Forbes2778f302015-04-02 13:22:31 +1300544
Chia-I Wu08accc62015-07-07 11:50:03 +0800545VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
546 VkDevice device,
547 const VkRenderPassCreateInfo *pCreateInfo,
548 VkRenderPass *pRenderPass)
549{
550 loader_platform_thread_lock_mutex(&globalLock);
551 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateRenderPass(device, pCreateInfo, pRenderPass);
552
Chris Forbes9715d4a2015-07-10 09:06:54 +1200553 render_pass_map[pRenderPass->handle] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800554 loader_platform_thread_unlock_mutex(&globalLock);
555 return res;
556}
557
Chris Forbesee99b9b2015-05-25 11:13:22 +1200558static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200559validate_interface_between_stages(VkDevice dev,
560 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600561 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200562 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200563{
564 std::map<uint32_t, interface_var> outputs;
565 std::map<uint32_t, interface_var> inputs;
566
567 std::map<uint32_t, interface_var> builtin_outputs;
568 std::map<uint32_t, interface_var> builtin_inputs;
569
Chris Forbesee99b9b2015-05-25 11:13:22 +1200570 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200571
Chris Forbesb1dee272015-07-03 13:50:24 +1200572 collect_interface_by_location(dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
573 collect_interface_by_location(dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200574
575 auto a_it = outputs.begin();
576 auto b_it = inputs.begin();
577
578 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600579 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
580 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
581 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200582 auto a_first = a_at_end ? 0 : a_it->first;
583 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600584
585 if (b_at_end || a_first < b_first) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200586 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 +1200587 "%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 +1200588 a_it++;
589 }
David Pinedod8f83d82015-04-27 16:36:17 -0600590 else if (a_at_end || a_first > b_first) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200591 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 +1200592 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200593 pass = false;
Chris Forbes41002452015-04-08 10:19:16 +1200594 b_it++;
595 }
596 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200597 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 +1200598 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200599 }
600 else {
601 char producer_type[1024];
602 char consumer_type[1024];
603 describe_type(producer_type, producer, a_it->second.type_id);
604 describe_type(consumer_type, consumer, b_it->second.type_id);
605
Chris Forbes9715d4a2015-07-10 09:06:54 +1200606 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 +1200607 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200608 pass = false;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200609 }
Chris Forbes41002452015-04-08 10:19:16 +1200610 a_it++;
611 b_it++;
612 }
613 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200614
615 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200616}
617
618
Chris Forbes3616b462015-04-08 10:37:20 +1200619enum FORMAT_TYPE {
620 FORMAT_TYPE_UNDEFINED,
621 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
622 FORMAT_TYPE_SINT,
623 FORMAT_TYPE_UINT,
624};
625
626
627static unsigned
628get_format_type(VkFormat fmt) {
629 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800630 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200631 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800632 case VK_FORMAT_R8_SINT:
633 case VK_FORMAT_R8G8_SINT:
634 case VK_FORMAT_R8G8B8_SINT:
635 case VK_FORMAT_R8G8B8A8_SINT:
636 case VK_FORMAT_R16_SINT:
637 case VK_FORMAT_R16G16_SINT:
638 case VK_FORMAT_R16G16B16_SINT:
639 case VK_FORMAT_R16G16B16A16_SINT:
640 case VK_FORMAT_R32_SINT:
641 case VK_FORMAT_R32G32_SINT:
642 case VK_FORMAT_R32G32B32_SINT:
643 case VK_FORMAT_R32G32B32A32_SINT:
644 case VK_FORMAT_B8G8R8_SINT:
645 case VK_FORMAT_B8G8R8A8_SINT:
646 case VK_FORMAT_R10G10B10A2_SINT:
647 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200648 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800649 case VK_FORMAT_R8_UINT:
650 case VK_FORMAT_R8G8_UINT:
651 case VK_FORMAT_R8G8B8_UINT:
652 case VK_FORMAT_R8G8B8A8_UINT:
653 case VK_FORMAT_R16_UINT:
654 case VK_FORMAT_R16G16_UINT:
655 case VK_FORMAT_R16G16B16_UINT:
656 case VK_FORMAT_R16G16B16A16_UINT:
657 case VK_FORMAT_R32_UINT:
658 case VK_FORMAT_R32G32_UINT:
659 case VK_FORMAT_R32G32B32_UINT:
660 case VK_FORMAT_R32G32B32A32_UINT:
661 case VK_FORMAT_B8G8R8_UINT:
662 case VK_FORMAT_B8G8R8A8_UINT:
663 case VK_FORMAT_R10G10B10A2_UINT:
664 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200665 return FORMAT_TYPE_UINT;
666 default:
667 return FORMAT_TYPE_FLOAT;
668 }
669}
670
671
Chris Forbes156a1162015-05-04 14:04:06 +1200672/* characterizes a SPIR-V type appearing in an interface to a FF stage,
673 * for comparison to a VkFormat's characterization above. */
674static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600675get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200676{
677 auto type_def_it = src->type_def_index.find(type);
678
679 if (type_def_it == src->type_def_index.end()) {
680 return FORMAT_TYPE_UNDEFINED;
681 }
682
683 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
684 unsigned opcode = code[0] & 0x0ffffu;
685 switch (opcode) {
686 case spv::OpTypeInt:
687 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
688 case spv::OpTypeFloat:
689 return FORMAT_TYPE_FLOAT;
690 case spv::OpTypeVector:
691 return get_fundamental_type(src, code[2]);
692 case spv::OpTypeMatrix:
693 return get_fundamental_type(src, code[2]);
694 case spv::OpTypeArray:
695 return get_fundamental_type(src, code[2]);
696 case spv::OpTypePointer:
697 return get_fundamental_type(src, code[3]);
698 default:
699 return FORMAT_TYPE_UNDEFINED;
700 }
701}
702
703
Chris Forbesee99b9b2015-05-25 11:13:22 +1200704static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200705validate_vi_consistency(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200706{
707 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
708 * each binding should be specified only once.
709 */
710 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200711 bool pass = true;
712
713 for (unsigned i = 0; i < vi->bindingCount; i++) {
714 auto desc = &vi->pVertexBindingDescriptions[i];
715 auto & binding = bindings[desc->binding];
716 if (binding) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200717 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 +1200718 "Duplicate vertex input binding descriptions for binding %d", desc->binding);
Chris Forbes280ba2c2015-06-12 11:16:41 +1200719 pass = false;
720 }
721 else {
722 binding = desc;
723 }
724 }
725
726 return pass;
727}
728
729
730static bool
Chris Forbesb1dee272015-07-03 13:50:24 +1200731validate_vi_against_vs_inputs(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200732{
733 std::map<uint32_t, interface_var> inputs;
734 /* we collect builtin inputs, but they will never appear in the VI state --
735 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
736 */
737 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200738 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200739
Chris Forbesb1dee272015-07-03 13:50:24 +1200740 collect_interface_by_location(dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200741
742 /* Build index by location */
743 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200744 if (vi) {
745 for (unsigned i = 0; i < vi->attributeCount; i++)
746 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
747 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200748
749 auto it_a = attribs.begin();
750 auto it_b = inputs.begin();
751
David Pinedod8f83d82015-04-27 16:36:17 -0600752 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
753 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
754 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200755 auto a_first = a_at_end ? 0 : it_a->first;
756 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600757 if (b_at_end || a_first < b_first) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200758 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 +1200759 "Vertex attribute at location %d not consumed by VS", a_first);
Chris Forbes772d03b2015-04-08 10:36:37 +1200760 it_a++;
761 }
David Pinedod8f83d82015-04-27 16:36:17 -0600762 else if (a_at_end || b_first < a_first) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200763 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 +1200764 "VS consumes input at location %d but not provided", b_first);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200765 pass = false;
Chris Forbes772d03b2015-04-08 10:36:37 +1200766 it_b++;
767 }
768 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200769 unsigned attrib_type = get_format_type(it_a->second->format);
770 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
771
772 /* type checking */
773 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
774 char vs_type[1024];
775 describe_type(vs_type, vs, it_b->second.type_id);
Chris Forbes9715d4a2015-07-10 09:06:54 +1200776 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 +1200777 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Chris Forbes401784b2015-05-04 14:04:24 +1200778 string_VkFormat(it_a->second->format), a_first, vs_type);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200779 pass = false;
Chris Forbes401784b2015-05-04 14:04:24 +1200780 }
781
Chris Forbes6b2ead62015-04-17 10:13:28 +1200782 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200783 it_a++;
784 it_b++;
785 }
786 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200787
788 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200789}
790
791
Chris Forbesee99b9b2015-05-25 11:13:22 +1200792static bool
Chia-I Wu08accc62015-07-07 11:50:03 +0800793validate_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 +1200794{
Chia-I Wu08accc62015-07-07 11:50:03 +0800795 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200796 std::map<uint32_t, interface_var> outputs;
797 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200798 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200799
800 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
801
Chris Forbesb1dee272015-07-03 13:50:24 +1200802 collect_interface_by_location(dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200803
804 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
805 * and all color attachment should be UNORM/SNORM/FLOAT.
806 */
807 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200808 if (outputs.size()) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200809 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 +1200810 "Should not have user-defined FS outputs when using broadcast");
Chris Forbesee99b9b2015-05-25 11:13:22 +1200811 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200812 }
813
Chia-I Wu08accc62015-07-07 11:50:03 +0800814 for (unsigned i = 0; i < color_formats.size(); i++) {
815 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200816 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200817 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 +1200818 "CB format should not be SINT or UINT when using broadcast");
Chris Forbesee99b9b2015-05-25 11:13:22 +1200819 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200820 }
821 }
822
Chris Forbesee99b9b2015-05-25 11:13:22 +1200823 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200824 }
825
826 auto it = outputs.begin();
827 uint32_t attachment = 0;
828
829 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
830 * are currently dense, but the parallel with matching between shader stages is nice.
831 */
832
Chris Forbes9715d4a2015-07-10 09:06:54 +1200833 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200834 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
835 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200836 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 +1200837 "FS writes to output location %d with no matching attachment", it->first);
Chris Forbes3616b462015-04-08 10:37:20 +1200838 it++;
839 }
840 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200841 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 +1200842 "Attachment %d not written by FS", attachment);
Chris Forbes3616b462015-04-08 10:37:20 +1200843 attachment++;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200844 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200845 }
846 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200847 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800848 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200849
850 /* type checking */
851 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
852 char fs_type[1024];
853 describe_type(fs_type, fs, it->second.type_id);
Chris Forbes9715d4a2015-07-10 09:06:54 +1200854 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 +1200855 "Attachment %d of type `%s` does not match FS output type of `%s`",
Chia-I Wu08accc62015-07-07 11:50:03 +0800856 attachment, string_VkFormat(color_formats[attachment]), fs_type);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200857 pass = false;
Chris Forbes46d31e52015-05-04 14:20:10 +1200858 }
859
Chris Forbes6b2ead62015-04-17 10:13:28 +1200860 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200861 it++;
862 attachment++;
863 }
864 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200865
866 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200867}
868
869
Chris Forbesf044ec92015-06-05 15:01:08 +1200870struct shader_stage_attributes {
871 char const * const name;
872 bool arrayed_input;
873};
874
875
876static shader_stage_attributes
877shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
878 { "vertex shader", false },
879 { "tessellation control shader", true },
880 { "tessellation evaluation shader", false },
881 { "geometry shader", true },
882 { "fragment shader", false },
883};
884
885
Chris Forbes81874ba2015-06-04 20:23:00 +1200886static bool
Chris Forbes36a372f2015-07-24 13:53:47 +1200887validate_graphics_pipeline(VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +1200888{
Chris Forbesf6800b52015-04-08 10:16:45 +1200889 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
890 * before trying to do anything more: */
891
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600892 shader_module const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
Chris Forbesf044ec92015-06-05 15:01:08 +1200893 memset(shaders, 0, sizeof(shaders));
Chia-I Wu08accc62015-07-07 11:50:03 +0800894 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600895 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200896 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +1200897
Chris Forbes7f963832015-05-29 14:55:18 +1200898 loader_platform_thread_lock_mutex(&globalLock);
899
Tony Barbour92503c62015-07-13 15:28:47 -0600900 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600901 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
902 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +1200903
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600904 if (pStage->stage < VK_SHADER_STAGE_VERTEX || pStage->stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200905 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 +1200906 "Unknown shader stage %d", pStage->stage);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200907 }
Chris Forbesf044ec92015-06-05 15:01:08 +1200908 else {
Chris Forbes9715d4a2015-07-10 09:06:54 +1200909 struct shader_object *shader = shader_object_map[pStage->shader.handle];
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600910 shaders[pStage->stage] = shader->module;
Chris Forbesf044ec92015-06-05 15:01:08 +1200911 }
Chris Forbesf6800b52015-04-08 10:16:45 +1200912 }
Chris Forbesf6800b52015-04-08 10:16:45 +1200913 }
914
Chia-I Wu08accc62015-07-07 11:50:03 +0800915 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Chris Forbes9715d4a2015-07-10 09:06:54 +1200916 rp = render_pass_map[pCreateInfo->renderPass.handle];
Chia-I Wu08accc62015-07-07 11:50:03 +0800917
Mark Lobodzinskid5732f32015-06-23 15:11:57 -0600918 vi = pCreateInfo->pVertexInputState;
919
Chris Forbes280ba2c2015-06-12 11:16:41 +1200920 if (vi) {
Chris Forbesb1dee272015-07-03 13:50:24 +1200921 pass = validate_vi_consistency(dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200922 }
923
Chris Forbesf044ec92015-06-05 15:01:08 +1200924 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
Chris Forbesb1dee272015-07-03 13:50:24 +1200925 pass = validate_vi_against_vs_inputs(dev, vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200926 }
927
Chris Forbesf044ec92015-06-05 15:01:08 +1200928 /* TODO: enforce rules about present combinations of shaders */
929 int producer = VK_SHADER_STAGE_VERTEX;
930 int consumer = VK_SHADER_STAGE_GEOMETRY;
931
932 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
933 producer++;
934 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +1200935 }
936
Tony Barbour0102a902015-06-11 15:04:25 -0600937 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +1200938 assert(shaders[producer]);
939 if (shaders[consumer]) {
940 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
Chris Forbesb1dee272015-07-03 13:50:24 +1200941 pass = validate_interface_between_stages(dev,
942 shaders[producer], shader_stage_attribs[producer].name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200943 shaders[consumer], shader_stage_attribs[consumer].name,
944 shader_stage_attribs[consumer].arrayed_input) && pass;
945 }
946
947 producer = consumer;
948 }
949 }
950
Chia-I Wu08accc62015-07-07 11:50:03 +0800951 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && rp) {
952 pass = validate_fs_outputs_against_render_pass(dev, shaders[VK_SHADER_STAGE_FRAGMENT], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200953 }
954
Chris Forbes7f963832015-05-29 14:55:18 +1200955 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +1200956 return pass;
957}
958
Jon Ashburnc669cc62015-07-09 15:02:25 -0600959//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +1200960VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -0600961vkCreateGraphicsPipelines(VkDevice device,
962 VkPipelineCache pipelineCache,
963 uint32_t count,
964 const VkGraphicsPipelineCreateInfo *pCreateInfos,
965 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +1200966{
Chris Forbes36a372f2015-07-24 13:53:47 +1200967 bool pass = true;
968 for (uint32_t i = 0; i < count; i++) {
969 pass = validate_graphics_pipeline(device, &pCreateInfos[i]) && pass;
970 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200971
972 if (pass) {
973 /* The driver is allowed to crash if passed junk. Only actually create the
974 * pipeline if we didn't run into any showstoppers above.
975 */
Jon Ashburnc669cc62015-07-09 15:02:25 -0600976 return get_dispatch_table(shader_checker_device_table_map, device)->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200977 }
978 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -0600979 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200980 }
Chris Forbes4175e6f2015-04-08 10:15:35 +1200981}
982
983
Chris Forbesb1dee272015-07-03 13:50:24 +1200984VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
985{
986 VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
987 VkResult result = pDeviceTable->CreateDevice(gpu, pCreateInfo, pDevice);
988 if (result == VK_SUCCESS) {
989 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
990 VkLayerDispatchTable *pTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
991 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
992 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
993 }
994 return result;
995}
Chris Forbes39d8d752015-06-04 20:27:09 +1200996
Jon Ashburn9a8a2e22015-05-19 16:34:53 -0600997/* hook DextroyDevice to remove tableMap entry */
Mark Lobodzinski2141f652015-09-07 13:59:43 -0600998VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -0600999{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001000 dispatch_key key = get_dispatch_key(device);
Chris Forbesb1dee272015-07-03 13:50:24 +12001001 VkLayerDispatchTable *pDisp = get_dispatch_table(shader_checker_device_table_map, device);
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001002 pDisp->DestroyDevice(device);
Chris Forbesb1dee272015-07-03 13:50:24 +12001003 shader_checker_device_table_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001004}
1005
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001006VkResult VKAPI vkCreateInstance(
1007 const VkInstanceCreateInfo* pCreateInfo,
1008 VkInstance* pInstance)
1009{
Chris Forbesb1dee272015-07-03 13:50:24 +12001010 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map,*pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001011 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
1012
1013 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001014 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1015 my_data->report_data = debug_report_create_instance(
1016 pTable,
1017 *pInstance,
1018 pCreateInfo->extensionCount,
1019 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001020
Chris Forbesb1dee272015-07-03 13:50:24 +12001021 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001022 }
1023 return result;
1024}
1025
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001026/* hook DestroyInstance to remove tableInstanceMap entry */
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001027VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001028{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001029 dispatch_key key = get_dispatch_key(instance);
Chris Forbesb1dee272015-07-03 13:50:24 +12001030 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001031 pTable->DestroyInstance(instance);
Chris Forbesb1dee272015-07-03 13:50:24 +12001032
1033 // Clean up logging callback, if any
1034 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
1035 if (my_data->logging_callback) {
1036 layer_destroy_msg_callback(my_data->report_data, my_data->logging_callback);
1037 }
1038
1039 layer_debug_report_destroy_instance(my_data->report_data);
1040 layer_data_map.erase(pTable);
1041
1042 shader_checker_instance_table_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001043}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001044
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001045VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001046 VkInstance instance,
1047 VkFlags msgFlags,
1048 const PFN_vkDbgMsgCallback pfnMsgCallback,
1049 void* pUserData,
1050 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001051{
Chris Forbesb1dee272015-07-03 13:50:24 +12001052 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1053 VkResult res = pTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1054 if (VK_SUCCESS == res) {
1055 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1056 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1057 }
1058 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001059}
1060
1061VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001062 VkInstance instance,
1063 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001064{
Chris Forbesb1dee272015-07-03 13:50:24 +12001065 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1066 VkResult res = pTable->DbgDestroyMsgCallback(instance, msgCallback);
1067 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1068 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1069 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001070}
1071
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001072VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001073{
Chris Forbesb1dee272015-07-03 13:50:24 +12001074 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001075 return NULL;
1076
Jon Ashburn8fd08252015-05-28 16:25:02 -06001077 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001078 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
1079 initDeviceTable(shader_checker_device_table_map, (const VkBaseLayerObject *) dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001080 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001081 }
1082
Chris Forbes2778f302015-04-02 13:22:31 +13001083#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001084 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001085 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001086
Chris Forbesb1dee272015-07-03 13:50:24 +12001087 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001088 ADD_HOOK(vkCreateShaderModule);
Chris Forbes2778f302015-04-02 13:22:31 +13001089 ADD_HOOK(vkCreateShader);
Chia-I Wu08accc62015-07-07 11:50:03 +08001090 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001091 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001092 ADD_HOOK(vkCreateGraphicsPipelines);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001093#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001094
1095 VkLayerDispatchTable* pTable = get_dispatch_table(shader_checker_device_table_map, dev);
1096 {
1097 if (pTable->GetDeviceProcAddr == NULL)
1098 return NULL;
1099 return pTable->GetDeviceProcAddr(dev, funcName);
1100 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001101}
1102
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001103VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001104{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001105 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001106
Chris Forbesb1dee272015-07-03 13:50:24 +12001107 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001108 return NULL;
1109
Chris Forbesb1dee272015-07-03 13:50:24 +12001110 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
1111 initInstanceTable(shader_checker_instance_table_map, (const VkBaseLayerObject *) instance);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001112 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001113 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001114#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001115 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001116 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001117
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001118 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001119 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001120 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1121 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1122 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1123 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001124#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001125
Chris Forbesb1dee272015-07-03 13:50:24 +12001126
1127 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1128 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001129 if (fptr)
1130 return fptr;
1131
Chris Forbesb1dee272015-07-03 13:50:24 +12001132 {
1133 VkLayerInstanceDispatchTable* pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1134 if (pTable->GetInstanceProcAddr == NULL)
1135 return NULL;
1136 return pTable->GetInstanceProcAddr(instance, funcName);
1137 }
Chris Forbes2778f302015-04-02 13:22:31 +13001138}