blob: e0f3693804f2f9e5930496ec3af204c6d51ae4c9 [file] [log] [blame]
Chris Forbesaab9d112015-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 Forbes67cc36f2015-04-13 12:14:52 +120027#include <map>
Chris Forbesaab9d112015-04-02 13:22:31 +130028#include <unordered_map>
Chris Forbesbb164b62015-04-08 10:19:16 +120029#include <map>
Chris Forbes4396ff52015-04-08 10:11:59 +120030#include <vector>
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -060031#include <string>
Tobin Ehlisaf14e9f2015-09-01 11:59:36 -060032#include <iostream>
Tobin Ehlis7a51d902015-07-03 10:34:49 -060033#include "vk_loader_platform.h"
Chris Forbesaab9d112015-04-02 13:22:31 +130034#include "vk_dispatch_table_helper.h"
Tobin Ehlis2d1d9702015-07-03 09:42:57 -060035#include "vk_layer.h"
Tobin Ehlis56d204a2015-07-03 10:15:26 -060036#include "vk_layer_config.h"
37#include "vk_layer_msg.h"
38#include "vk_layer_table.h"
Chris Forbese20111c2015-07-03 13:50:24 +120039#include "vk_layer_logging.h"
Chris Forbes3317b382015-05-04 14:04:24 +120040#include "vk_enum_string_helper.h"
Chris Forbes5c75afe2015-04-17 10:13:28 +120041#include "shader_checker.h"
Chris Forbesaab9d112015-04-02 13:22:31 +130042// The following is #included again to catch certain OS-specific functions
43// being used:
Tobin Ehlis7a51d902015-07-03 10:34:49 -060044#include "vk_loader_platform.h"
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -060045#include "vk_layer_extension_utils.h"
Chris Forbesaab9d112015-04-02 13:22:31 +130046
GregF3aaa0882015-07-21 17:22:50 -060047#include "spirv/spirv.hpp"
Chris Forbesaab9d112015-04-02 13:22:31 +130048
Chris Forbesaab9d112015-04-02 13:22:31 +130049
Chris Forbese20111c2015-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 Forbes2b7c0002015-07-10 09:06:54 +120065debug_report_data *mdd(void *object)
Chris Forbese20111c2015-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 Ehlis8354e022015-09-01 11:46:36 -060078 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chris Forbese20111c2015-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 Forbes1b466bd2015-04-15 06:59:41 +120085static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes1ed0f982015-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 Forbes4396ff52015-04-08 10:11:59 +120089
Chris Forbes1bb5a2e2015-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:
GregF3aaa0882015-07-21 17:22:50 -0600109 case spv::OpTypeImage:
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200110 case spv::OpTypeSampler:
GregF3aaa0882015-07-21 17:22:50 -0600111 case spv::OpTypeSampledImage:
Chris Forbes1bb5a2e2015-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 Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600135struct shader_module {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200136 /* the spirv image itself */
Chris Forbes4396ff52015-04-08 10:11:59 +1200137 std::vector<uint32_t> words;
Chris Forbes1bb5a2e2015-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 Forbes4453c772015-06-05 15:01:08 +1200142 bool is_spirv;
Chris Forbes4396ff52015-04-08 10:11:59 +1200143
Chris Forbese20111c2015-07-03 13:50:24 +1200144 shader_module(VkDevice dev, VkShaderModuleCreateInfo const *pCreateInfo) :
Chris Forbes4453c772015-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 Forbes2b7c0002015-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 Forbese20111c2015-07-03 13:50:24 +1200151 "Shader is not SPIR-V, most checks will not be possible");
Chris Forbes4453c772015-06-05 15:01:08 +1200152 is_spirv = false;
153 return;
154 }
155
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200156
157 build_type_def_index(words, type_def_index);
Chris Forbes4396ff52015-04-08 10:11:59 +1200158 }
159};
160
161
Chris Forbes2b7c0002015-07-10 09:06:54 +1200162static std::unordered_map<uint64_t, shader_module *> shader_module_map;
Courtney Goeltzenleuchter2d034fd2015-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 Forbes2b7c0002015-07-10 09:06:54 +1200170 module = shader_module_map[pCreateInfo->module.handle];
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600171 name = pCreateInfo->pName;
172 }
173};
Chris Forbes2b7c0002015-07-10 09:06:54 +1200174static std::unordered_map<uint64_t, shader_object *> shader_object_map;
Chris Forbes4396ff52015-04-08 10:11:59 +1200175
Chia-I Wuc278df82015-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 Northrop6de6b0b2015-08-04 11:16:41 -0600191 const uint32_t att = subpass->pColorAttachments[j].attachment;
Chia-I Wuc278df82015-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 Forbes2b7c0002015-07-10 09:06:54 +1200201static std::unordered_map<uint64_t, render_pass *> render_pass_map;
Chia-I Wuc278df82015-07-07 11:50:03 +0800202
Chris Forbes4396ff52015-04-08 10:11:59 +1200203
Chris Forbes1b466bd2015-04-15 06:59:41 +1200204static void
Chris Forbese20111c2015-07-03 13:50:24 +1200205init_shader_checker(layer_data *my_data)
Chris Forbes1b466bd2015-04-15 06:59:41 +1200206{
Chris Forbese20111c2015-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 Forbes1b466bd2015-04-15 06:59:41 +1200211 // initialize ShaderChecker options
Chris Forbese20111c2015-07-03 13:50:24 +1200212 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
213 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbes1b466bd2015-04-15 06:59:41 +1200214
Chris Forbese20111c2015-07-03 13:50:24 +1200215 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbes1b466bd2015-04-15 06:59:41 +1200216 {
Chris Forbese20111c2015-07-03 13:50:24 +1200217 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb4b6e7c2015-09-15 09:55:54 -0600218 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Chris Forbese20111c2015-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 Forbes1b466bd2015-04-15 06:59:41 +1200231 }
232}
233
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600234static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600235 {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600236 "ShaderChecker",
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600237 VK_API_VERSION,
238 VK_MAKE_VERSION(0, 1, 0),
239 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600240 }
Chris Forbesaab9d112015-04-02 13:22:31 +1300241};
242
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600243VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600244 const char *pLayerName,
245 uint32_t *pCount,
246 VkExtensionProperties* pProperties)
Chris Forbesaab9d112015-04-02 13:22:31 +1300247{
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600248 /* shader checker does not have any global extensions */
249 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbesaab9d112015-04-02 13:22:31 +1300250}
251
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600252VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600253 uint32_t *pCount,
254 VkLayerProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600255{
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600256 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
257 shader_checker_global_layers,
258 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600259}
260
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600261VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600262 VkPhysicalDevice physicalDevice,
263 const char* pLayerName,
264 uint32_t* pCount,
265 VkExtensionProperties* pProperties)
Jon Ashburnade3bee2015-06-10 16:43:31 -0600266{
Courtney Goeltzenleuchter7abf8e52015-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 Ashburnade3bee2015-06-10 16:43:31 -0600270
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600271VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchter7abf8e52015-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 Ashburnade3bee2015-06-10 16:43:31 -0600280}
Chris Forbesaab9d112015-04-02 13:22:31 +1300281
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200282static char const *
283storage_class_name(unsigned sc)
284{
285 switch (sc) {
Cody Northrop812b4612015-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 Northrop812b4612015-04-20 14:09:40 -0600295 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF3aaa0882015-07-21 17:22:50 -0600296 case spv::StorageClassImage: return "image";
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200297 default: return "unknown";
298 }
299}
300
301
302/* returns ptr to null terminator */
303static char *
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600304describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes1bb5a2e2015-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 Elliottf21f14b2015-04-17 11:05:04 -0600337 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes1bb5a2e2015-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 Goeltzenleuchter2d034fd2015-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 Forbes1bb5a2e2015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200356 return false;
357 }
358
359 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes1bb5a2e2015-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 Forbes0a94a372015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200375 if (a_opcode != b_opcode) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200376 return false;
377 }
378
379 switch (a_opcode) {
Chris Forbes0a94a372015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200381 case spv::OpTypeBool:
Chris Forbes0a94a372015-06-05 14:57:05 +1200382 return true && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200383 case spv::OpTypeInt:
384 /* match on width, signedness */
Chris Forbes0a94a372015-06-05 14:57:05 +1200385 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200386 case spv::OpTypeFloat:
387 /* match on width */
Chris Forbes0a94a372015-06-05 14:57:05 +1200388 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200389 case spv::OpTypeVector:
390 case spv::OpTypeMatrix:
391 case spv::OpTypeArray:
Chris Forbes0a94a372015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200395 case spv::OpTypeStruct:
396 /* match on all element types */
397 {
Chris Forbes0a94a372015-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 Forbes1bb5a2e2015-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 Elliottf21f14b2015-04-17 11:05:04 -0600410 for (unsigned i = 2; i < a_len; i++) {
Chris Forbes0a94a372015-06-05 14:57:05 +1200411 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes1bb5a2e2015-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 Forbes0a94a372015-06-05 14:57:05 +1200420 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes1bb5a2e2015-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 Forbes67cc36f2015-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 Forbese20111c2015-07-03 13:50:24 +1200451collect_interface_by_location(VkDevice dev,
452 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes67cc36f2015-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 Forbes67cc36f2015-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 Northrop812b4612015-04-20 14:09:40 -0600472 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200473 var_locations[code[word+1]] = code[word+3];
474 }
475
Cody Northrop812b4612015-04-20 14:09:40 -0600476 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes67cc36f2015-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 Elliottf21f14b2015-04-17 11:05:04 -0600485 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes67cc36f2015-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 Forbes2b7c0002015-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 Forbese20111c2015-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 Forbes67cc36f2015-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 Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600519VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
520 VkDevice device,
521 const VkShaderModuleCreateInfo *pCreateInfo,
522 VkShaderModule *pShaderModule)
Chris Forbesaab9d112015-04-02 13:22:31 +1300523{
Chris Forbese20111c2015-07-03 13:50:24 +1200524 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShaderModule(device, pCreateInfo, pShaderModule);
Chris Forbes4396ff52015-04-08 10:11:59 +1200525
Courtney Goeltzenleuchter0b590602015-09-16 12:57:55 -0600526 if (res == VK_SUCCESS) {
527 loader_platform_thread_lock_mutex(&globalLock);
528 shader_module_map[pShaderModule->handle] = new shader_module(device, pCreateInfo);
529 loader_platform_thread_unlock_mutex(&globalLock);
530 }
Chris Forbesaab9d112015-04-02 13:22:31 +1300531 return res;
532}
533
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600534VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
535 VkDevice device,
536 const VkShaderCreateInfo *pCreateInfo,
537 VkShader *pShader)
538{
Chris Forbese20111c2015-07-03 13:50:24 +1200539 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShader(device, pCreateInfo, pShader);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600540
Courtney Goeltzenleuchter0b590602015-09-16 12:57:55 -0600541 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200542 shader_object_map[pShader->handle] = new shader_object(pCreateInfo);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600543 loader_platform_thread_unlock_mutex(&globalLock);
544 return res;
545}
Chris Forbesaab9d112015-04-02 13:22:31 +1300546
Chia-I Wuc278df82015-07-07 11:50:03 +0800547VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
548 VkDevice device,
549 const VkRenderPassCreateInfo *pCreateInfo,
550 VkRenderPass *pRenderPass)
551{
Chia-I Wuc278df82015-07-07 11:50:03 +0800552 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateRenderPass(device, pCreateInfo, pRenderPass);
553
Courtney Goeltzenleuchter0b590602015-09-16 12:57:55 -0600554 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200555 render_pass_map[pRenderPass->handle] = new render_pass(pCreateInfo);
Chia-I Wuc278df82015-07-07 11:50:03 +0800556 loader_platform_thread_unlock_mutex(&globalLock);
557 return res;
558}
559
Chris Forbes5f362d02015-05-25 11:13:22 +1200560static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200561validate_interface_between_stages(VkDevice dev,
562 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600563 shader_module const *consumer, char const *consumer_name,
Chris Forbes4453c772015-06-05 15:01:08 +1200564 bool consumer_arrayed_input)
Chris Forbesbb164b62015-04-08 10:19:16 +1200565{
566 std::map<uint32_t, interface_var> outputs;
567 std::map<uint32_t, interface_var> inputs;
568
569 std::map<uint32_t, interface_var> builtin_outputs;
570 std::map<uint32_t, interface_var> builtin_inputs;
571
Chris Forbes5f362d02015-05-25 11:13:22 +1200572 bool pass = true;
Chris Forbesbb164b62015-04-08 10:19:16 +1200573
Chris Forbese20111c2015-07-03 13:50:24 +1200574 collect_interface_by_location(dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
575 collect_interface_by_location(dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesbb164b62015-04-08 10:19:16 +1200576
577 auto a_it = outputs.begin();
578 auto b_it = inputs.begin();
579
580 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedof5997ab2015-04-27 16:36:17 -0600581 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
582 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
583 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200584 auto a_first = a_at_end ? 0 : a_it->first;
585 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600586
587 if (b_at_end || a_first < b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200588 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200589 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name);
Chris Forbesbb164b62015-04-08 10:19:16 +1200590 a_it++;
591 }
David Pinedof5997ab2015-04-27 16:36:17 -0600592 else if (a_at_end || a_first > b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200593 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200594 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name);
Chris Forbes5f362d02015-05-25 11:13:22 +1200595 pass = false;
Chris Forbesbb164b62015-04-08 10:19:16 +1200596 b_it++;
597 }
598 else {
Chris Forbes4453c772015-06-05 15:01:08 +1200599 if (types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id, consumer_arrayed_input)) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200600 /* OK! */
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200601 }
602 else {
603 char producer_type[1024];
604 char consumer_type[1024];
605 describe_type(producer_type, producer, a_it->second.type_id);
606 describe_type(consumer_type, consumer, b_it->second.type_id);
607
Chris Forbes2b7c0002015-07-10 09:06:54 +1200608 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200609 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200610 pass = false;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200611 }
Chris Forbesbb164b62015-04-08 10:19:16 +1200612 a_it++;
613 b_it++;
614 }
615 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200616
617 return pass;
Chris Forbesbb164b62015-04-08 10:19:16 +1200618}
619
620
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200621enum FORMAT_TYPE {
622 FORMAT_TYPE_UNDEFINED,
623 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
624 FORMAT_TYPE_SINT,
625 FORMAT_TYPE_UINT,
626};
627
628
629static unsigned
630get_format_type(VkFormat fmt) {
631 switch (fmt) {
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800632 case VK_FORMAT_UNDEFINED:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200633 return FORMAT_TYPE_UNDEFINED;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800634 case VK_FORMAT_R8_SINT:
635 case VK_FORMAT_R8G8_SINT:
636 case VK_FORMAT_R8G8B8_SINT:
637 case VK_FORMAT_R8G8B8A8_SINT:
638 case VK_FORMAT_R16_SINT:
639 case VK_FORMAT_R16G16_SINT:
640 case VK_FORMAT_R16G16B16_SINT:
641 case VK_FORMAT_R16G16B16A16_SINT:
642 case VK_FORMAT_R32_SINT:
643 case VK_FORMAT_R32G32_SINT:
644 case VK_FORMAT_R32G32B32_SINT:
645 case VK_FORMAT_R32G32B32A32_SINT:
646 case VK_FORMAT_B8G8R8_SINT:
647 case VK_FORMAT_B8G8R8A8_SINT:
648 case VK_FORMAT_R10G10B10A2_SINT:
649 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200650 return FORMAT_TYPE_SINT;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800651 case VK_FORMAT_R8_UINT:
652 case VK_FORMAT_R8G8_UINT:
653 case VK_FORMAT_R8G8B8_UINT:
654 case VK_FORMAT_R8G8B8A8_UINT:
655 case VK_FORMAT_R16_UINT:
656 case VK_FORMAT_R16G16_UINT:
657 case VK_FORMAT_R16G16B16_UINT:
658 case VK_FORMAT_R16G16B16A16_UINT:
659 case VK_FORMAT_R32_UINT:
660 case VK_FORMAT_R32G32_UINT:
661 case VK_FORMAT_R32G32B32_UINT:
662 case VK_FORMAT_R32G32B32A32_UINT:
663 case VK_FORMAT_B8G8R8_UINT:
664 case VK_FORMAT_B8G8R8A8_UINT:
665 case VK_FORMAT_R10G10B10A2_UINT:
666 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200667 return FORMAT_TYPE_UINT;
668 default:
669 return FORMAT_TYPE_FLOAT;
670 }
671}
672
673
Chris Forbes28c50882015-05-04 14:04:06 +1200674/* characterizes a SPIR-V type appearing in an interface to a FF stage,
675 * for comparison to a VkFormat's characterization above. */
676static unsigned
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600677get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes28c50882015-05-04 14:04:06 +1200678{
679 auto type_def_it = src->type_def_index.find(type);
680
681 if (type_def_it == src->type_def_index.end()) {
682 return FORMAT_TYPE_UNDEFINED;
683 }
684
685 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
686 unsigned opcode = code[0] & 0x0ffffu;
687 switch (opcode) {
688 case spv::OpTypeInt:
689 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
690 case spv::OpTypeFloat:
691 return FORMAT_TYPE_FLOAT;
692 case spv::OpTypeVector:
693 return get_fundamental_type(src, code[2]);
694 case spv::OpTypeMatrix:
695 return get_fundamental_type(src, code[2]);
696 case spv::OpTypeArray:
697 return get_fundamental_type(src, code[2]);
698 case spv::OpTypePointer:
699 return get_fundamental_type(src, code[3]);
700 default:
701 return FORMAT_TYPE_UNDEFINED;
702 }
703}
704
705
Chris Forbes5f362d02015-05-25 11:13:22 +1200706static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200707validate_vi_consistency(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200708{
709 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
710 * each binding should be specified only once.
711 */
712 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200713 bool pass = true;
714
715 for (unsigned i = 0; i < vi->bindingCount; i++) {
716 auto desc = &vi->pVertexBindingDescriptions[i];
717 auto & binding = bindings[desc->binding];
718 if (binding) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200719 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INCONSISTENT_VI, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200720 "Duplicate vertex input binding descriptions for binding %d", desc->binding);
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200721 pass = false;
722 }
723 else {
724 binding = desc;
725 }
726 }
727
728 return pass;
729}
730
731
732static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200733validate_vi_against_vs_inputs(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbesfcd05f12015-04-08 10:36:37 +1200734{
735 std::map<uint32_t, interface_var> inputs;
736 /* we collect builtin inputs, but they will never appear in the VI state --
737 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
738 */
739 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbes5f362d02015-05-25 11:13:22 +1200740 bool pass = true;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200741
Chris Forbese20111c2015-07-03 13:50:24 +1200742 collect_interface_by_location(dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200743
744 /* Build index by location */
745 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes6f2ab982015-05-25 11:13:24 +1200746 if (vi) {
747 for (unsigned i = 0; i < vi->attributeCount; i++)
748 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
749 }
Chris Forbesfcd05f12015-04-08 10:36:37 +1200750
751 auto it_a = attribs.begin();
752 auto it_b = inputs.begin();
753
David Pinedof5997ab2015-04-27 16:36:17 -0600754 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
755 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
756 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200757 auto a_first = a_at_end ? 0 : it_a->first;
758 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600759 if (b_at_end || a_first < b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200760 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200761 "Vertex attribute at location %d not consumed by VS", a_first);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200762 it_a++;
763 }
David Pinedof5997ab2015-04-27 16:36:17 -0600764 else if (a_at_end || b_first < a_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200765 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200766 "VS consumes input at location %d but not provided", b_first);
Chris Forbes5f362d02015-05-25 11:13:22 +1200767 pass = false;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200768 it_b++;
769 }
770 else {
Chris Forbes3317b382015-05-04 14:04:24 +1200771 unsigned attrib_type = get_format_type(it_a->second->format);
772 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
773
774 /* type checking */
775 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
776 char vs_type[1024];
777 describe_type(vs_type, vs, it_b->second.type_id);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200778 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200779 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Chris Forbes3317b382015-05-04 14:04:24 +1200780 string_VkFormat(it_a->second->format), a_first, vs_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200781 pass = false;
Chris Forbes3317b382015-05-04 14:04:24 +1200782 }
783
Chris Forbes5c75afe2015-04-17 10:13:28 +1200784 /* OK! */
Chris Forbesfcd05f12015-04-08 10:36:37 +1200785 it_a++;
786 it_b++;
787 }
788 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200789
790 return pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200791}
792
793
Chris Forbes5f362d02015-05-25 11:13:22 +1200794static bool
Chia-I Wuc278df82015-07-07 11:50:03 +0800795validate_fs_outputs_against_render_pass(VkDevice dev, shader_module const *fs, render_pass const *rp, uint32_t subpass)
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200796{
Chia-I Wuc278df82015-07-07 11:50:03 +0800797 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200798 std::map<uint32_t, interface_var> outputs;
799 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbes5f362d02015-05-25 11:13:22 +1200800 bool pass = true;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200801
802 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
803
Chris Forbese20111c2015-07-03 13:50:24 +1200804 collect_interface_by_location(dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200805
806 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
807 * and all color attachment should be UNORM/SNORM/FLOAT.
808 */
809 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200810 if (outputs.size()) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200811 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200812 "Should not have user-defined FS outputs when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200813 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200814 }
815
Chia-I Wuc278df82015-07-07 11:50:03 +0800816 for (unsigned i = 0; i < color_formats.size(); i++) {
817 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200818 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200819 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200820 "CB format should not be SINT or UINT when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200821 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200822 }
823 }
824
Chris Forbes5f362d02015-05-25 11:13:22 +1200825 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200826 }
827
828 auto it = outputs.begin();
829 uint32_t attachment = 0;
830
831 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
832 * are currently dense, but the parallel with matching between shader stages is nice.
833 */
834
Chris Forbes2b7c0002015-07-10 09:06:54 +1200835 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbes768eead2015-07-11 11:05:01 +1200836 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
837 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200838 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200839 "FS writes to output location %d with no matching attachment", it->first);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200840 it++;
841 }
842 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200843 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200844 "Attachment %d not written by FS", attachment);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200845 attachment++;
Chris Forbes5f362d02015-05-25 11:13:22 +1200846 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200847 }
848 else {
Chris Forbes4b009002015-05-04 14:20:10 +1200849 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wuc278df82015-07-07 11:50:03 +0800850 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes4b009002015-05-04 14:20:10 +1200851
852 /* type checking */
853 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
854 char fs_type[1024];
855 describe_type(fs_type, fs, it->second.type_id);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200856 log_msg(mdd(dev), VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200857 "Attachment %d of type `%s` does not match FS output type of `%s`",
Chia-I Wuc278df82015-07-07 11:50:03 +0800858 attachment, string_VkFormat(color_formats[attachment]), fs_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200859 pass = false;
Chris Forbes4b009002015-05-04 14:20:10 +1200860 }
861
Chris Forbes5c75afe2015-04-17 10:13:28 +1200862 /* OK! */
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200863 it++;
864 attachment++;
865 }
866 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200867
868 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200869}
870
871
Chris Forbes4453c772015-06-05 15:01:08 +1200872struct shader_stage_attributes {
873 char const * const name;
874 bool arrayed_input;
875};
876
877
878static shader_stage_attributes
879shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
880 { "vertex shader", false },
881 { "tessellation control shader", true },
882 { "tessellation evaluation shader", false },
883 { "geometry shader", true },
884 { "fragment shader", false },
885};
886
887
Chris Forbesf1060ca2015-06-04 20:23:00 +1200888static bool
Chris Forbesd8bde292015-07-24 13:53:47 +1200889validate_graphics_pipeline(VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes60540932015-04-08 10:15:35 +1200890{
Chris Forbes8f600932015-04-08 10:16:45 +1200891 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
892 * before trying to do anything more: */
893
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600894 shader_module const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
Chris Forbes4453c772015-06-05 15:01:08 +1200895 memset(shaders, 0, sizeof(shaders));
Chia-I Wuc278df82015-07-07 11:50:03 +0800896 render_pass const *rp = 0;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600897 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbes5f362d02015-05-25 11:13:22 +1200898 bool pass = true;
Chris Forbes8f600932015-04-08 10:16:45 +1200899
Chris Forbes1ed0f982015-05-29 14:55:18 +1200900 loader_platform_thread_lock_mutex(&globalLock);
901
Tony Barbourb2cc40a2015-07-13 15:28:47 -0600902 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600903 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
904 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbes8f600932015-04-08 10:16:45 +1200905
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600906 if (pStage->stage < VK_SHADER_STAGE_VERTEX || pStage->stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200907 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200908 "Unknown shader stage %d", pStage->stage);
Chris Forbes5c75afe2015-04-17 10:13:28 +1200909 }
Chris Forbes4453c772015-06-05 15:01:08 +1200910 else {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200911 struct shader_object *shader = shader_object_map[pStage->shader.handle];
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600912 shaders[pStage->stage] = shader->module;
Chris Forbes4453c772015-06-05 15:01:08 +1200913 }
Chris Forbes8f600932015-04-08 10:16:45 +1200914 }
Chris Forbes8f600932015-04-08 10:16:45 +1200915 }
916
Chia-I Wuc278df82015-07-07 11:50:03 +0800917 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Chris Forbes2b7c0002015-07-10 09:06:54 +1200918 rp = render_pass_map[pCreateInfo->renderPass.handle];
Chia-I Wuc278df82015-07-07 11:50:03 +0800919
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600920 vi = pCreateInfo->pVertexInputState;
921
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200922 if (vi) {
Chris Forbese20111c2015-07-03 13:50:24 +1200923 pass = validate_vi_consistency(dev, vi) && pass;
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200924 }
925
Chris Forbes4453c772015-06-05 15:01:08 +1200926 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
Chris Forbese20111c2015-07-03 13:50:24 +1200927 pass = validate_vi_against_vs_inputs(dev, vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200928 }
929
Chris Forbes4453c772015-06-05 15:01:08 +1200930 /* TODO: enforce rules about present combinations of shaders */
931 int producer = VK_SHADER_STAGE_VERTEX;
932 int consumer = VK_SHADER_STAGE_GEOMETRY;
933
934 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
935 producer++;
936 consumer++;
Chris Forbesbb164b62015-04-08 10:19:16 +1200937 }
938
Tony Barbour4eb3cd12015-06-11 15:04:25 -0600939 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbes4453c772015-06-05 15:01:08 +1200940 assert(shaders[producer]);
941 if (shaders[consumer]) {
942 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
Chris Forbese20111c2015-07-03 13:50:24 +1200943 pass = validate_interface_between_stages(dev,
944 shaders[producer], shader_stage_attribs[producer].name,
Chris Forbes4453c772015-06-05 15:01:08 +1200945 shaders[consumer], shader_stage_attribs[consumer].name,
946 shader_stage_attribs[consumer].arrayed_input) && pass;
947 }
948
949 producer = consumer;
950 }
951 }
952
Chia-I Wuc278df82015-07-07 11:50:03 +0800953 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && rp) {
954 pass = validate_fs_outputs_against_render_pass(dev, shaders[VK_SHADER_STAGE_FRAGMENT], rp, pCreateInfo->subpass) && pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200955 }
956
Chris Forbes1ed0f982015-05-29 14:55:18 +1200957 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbesf1060ca2015-06-04 20:23:00 +1200958 return pass;
959}
960
Jon Ashburn0d60d272015-07-09 15:02:25 -0600961//TODO handle pipelineCache entry points
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200962VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburn0d60d272015-07-09 15:02:25 -0600963vkCreateGraphicsPipelines(VkDevice device,
964 VkPipelineCache pipelineCache,
965 uint32_t count,
966 const VkGraphicsPipelineCreateInfo *pCreateInfos,
967 VkPipeline *pPipelines)
Chris Forbesf1060ca2015-06-04 20:23:00 +1200968{
Chris Forbesd8bde292015-07-24 13:53:47 +1200969 bool pass = true;
970 for (uint32_t i = 0; i < count; i++) {
971 pass = validate_graphics_pipeline(device, &pCreateInfos[i]) && pass;
972 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200973
974 if (pass) {
975 /* The driver is allowed to crash if passed junk. Only actually create the
976 * pipeline if we didn't run into any showstoppers above.
977 */
Jon Ashburn0d60d272015-07-09 15:02:25 -0600978 return get_dispatch_table(shader_checker_device_table_map, device)->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines);
Chris Forbes5f362d02015-05-25 11:13:22 +1200979 }
980 else {
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -0600981 return VK_ERROR_VALIDATION_FAILED;
Chris Forbes5f362d02015-05-25 11:13:22 +1200982 }
Chris Forbes60540932015-04-08 10:15:35 +1200983}
984
985
Chris Forbese20111c2015-07-03 13:50:24 +1200986VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
987{
988 VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
989 VkResult result = pDeviceTable->CreateDevice(gpu, pCreateInfo, pDevice);
990 if (result == VK_SUCCESS) {
991 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
992 VkLayerDispatchTable *pTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
993 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
994 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
995 }
996 return result;
997}
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200998
Jon Ashburn17f37372015-05-19 16:34:53 -0600999/* hook DextroyDevice to remove tableMap entry */
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001000VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device)
Jon Ashburn17f37372015-05-19 16:34:53 -06001001{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001002 dispatch_key key = get_dispatch_key(device);
Chris Forbese20111c2015-07-03 13:50:24 +12001003 VkLayerDispatchTable *pDisp = get_dispatch_table(shader_checker_device_table_map, device);
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001004 pDisp->DestroyDevice(device);
Chris Forbese20111c2015-07-03 13:50:24 +12001005 shader_checker_device_table_map.erase(key);
Jon Ashburn17f37372015-05-19 16:34:53 -06001006}
1007
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001008VkResult VKAPI vkCreateInstance(
1009 const VkInstanceCreateInfo* pCreateInfo,
1010 VkInstance* pInstance)
1011{
Chris Forbese20111c2015-07-03 13:50:24 +12001012 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map,*pInstance);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001013 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
1014
1015 if (result == VK_SUCCESS) {
Chris Forbese20111c2015-07-03 13:50:24 +12001016 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1017 my_data->report_data = debug_report_create_instance(
1018 pTable,
1019 *pInstance,
1020 pCreateInfo->extensionCount,
1021 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterf4a2eba2015-06-08 14:58:39 -06001022
Chris Forbese20111c2015-07-03 13:50:24 +12001023 init_shader_checker(my_data);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001024 }
1025 return result;
1026}
1027
Jon Ashburn17f37372015-05-19 16:34:53 -06001028/* hook DestroyInstance to remove tableInstanceMap entry */
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001029VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance)
Jon Ashburn17f37372015-05-19 16:34:53 -06001030{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001031 dispatch_key key = get_dispatch_key(instance);
Chris Forbese20111c2015-07-03 13:50:24 +12001032 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001033 pTable->DestroyInstance(instance);
Chris Forbese20111c2015-07-03 13:50:24 +12001034
1035 // Clean up logging callback, if any
1036 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
1037 if (my_data->logging_callback) {
1038 layer_destroy_msg_callback(my_data->report_data, my_data->logging_callback);
1039 }
1040
1041 layer_debug_report_destroy_instance(my_data->report_data);
1042 layer_data_map.erase(pTable);
1043
1044 shader_checker_instance_table_map.erase(key);
Jon Ashburn17f37372015-05-19 16:34:53 -06001045}
Chris Forbesb65ba352015-05-25 11:12:59 +12001046
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001047VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbese20111c2015-07-03 13:50:24 +12001048 VkInstance instance,
1049 VkFlags msgFlags,
1050 const PFN_vkDbgMsgCallback pfnMsgCallback,
1051 void* pUserData,
1052 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001053{
Chris Forbese20111c2015-07-03 13:50:24 +12001054 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1055 VkResult res = pTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1056 if (VK_SUCCESS == res) {
1057 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1058 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1059 }
1060 return res;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001061}
1062
1063VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbese20111c2015-07-03 13:50:24 +12001064 VkInstance instance,
1065 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001066{
Chris Forbese20111c2015-07-03 13:50:24 +12001067 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1068 VkResult res = pTable->DbgDestroyMsgCallback(instance, msgCallback);
1069 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1070 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1071 return res;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001072}
1073
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001074VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbesaab9d112015-04-02 13:22:31 +13001075{
Chris Forbese20111c2015-07-03 13:50:24 +12001076 if (dev == NULL)
Chris Forbesaab9d112015-04-02 13:22:31 +13001077 return NULL;
1078
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001079 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbese20111c2015-07-03 13:50:24 +12001080 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
1081 initDeviceTable(shader_checker_device_table_map, (const VkBaseLayerObject *) dev);
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001082 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001083 }
1084
Chris Forbesaab9d112015-04-02 13:22:31 +13001085#define ADD_HOOK(fn) \
Chris Forbese20111c2015-07-03 13:50:24 +12001086 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001087 return (PFN_vkVoidFunction) fn
Chris Forbesaab9d112015-04-02 13:22:31 +13001088
Chris Forbese20111c2015-07-03 13:50:24 +12001089 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001090 ADD_HOOK(vkCreateShaderModule);
Chris Forbesaab9d112015-04-02 13:22:31 +13001091 ADD_HOOK(vkCreateShader);
Chia-I Wuc278df82015-07-07 11:50:03 +08001092 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn17f37372015-05-19 16:34:53 -06001093 ADD_HOOK(vkDestroyDevice);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001094 ADD_HOOK(vkCreateGraphicsPipelines);
Jon Ashburn8198fd02015-05-18 09:08:41 -06001095#undef ADD_HOOK
Chris Forbese20111c2015-07-03 13:50:24 +12001096
1097 VkLayerDispatchTable* pTable = get_dispatch_table(shader_checker_device_table_map, dev);
1098 {
1099 if (pTable->GetDeviceProcAddr == NULL)
1100 return NULL;
1101 return pTable->GetDeviceProcAddr(dev, funcName);
1102 }
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001103}
1104
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001105VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001106{
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001107 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001108
Chris Forbese20111c2015-07-03 13:50:24 +12001109 if (instance == NULL)
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001110 return NULL;
1111
Chris Forbese20111c2015-07-03 13:50:24 +12001112 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
1113 initInstanceTable(shader_checker_instance_table_map, (const VkBaseLayerObject *) instance);
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001114 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001115 }
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001116#define ADD_HOOK(fn) \
Chris Forbese20111c2015-07-03 13:50:24 +12001117 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001118 return (PFN_vkVoidFunction) fn
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001119
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001120 ADD_HOOK(vkCreateInstance);
Jon Ashburn17f37372015-05-19 16:34:53 -06001121 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001122 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1123 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1124 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1125 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburn8198fd02015-05-18 09:08:41 -06001126#undef ADD_HOOK
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001127
Chris Forbese20111c2015-07-03 13:50:24 +12001128
1129 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1130 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001131 if (fptr)
1132 return fptr;
1133
Chris Forbese20111c2015-07-03 13:50:24 +12001134 {
1135 VkLayerInstanceDispatchTable* pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1136 if (pTable->GetInstanceProcAddr == NULL)
1137 return NULL;
1138 return pTable->GetInstanceProcAddr(instance, funcName);
1139 }
Chris Forbesaab9d112015-04-02 13:22:31 +13001140}