blob: 7e3602c63d27b6e25d53a1bcf8a317eaca8a5b64 [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");
218 if (option_str)
Chris Forbes1b466bd2015-04-15 06:59:41 +1200219 {
Chris Forbese20111c2015-07-03 13:50:24 +1200220 log_output = fopen(option_str, "w");
Chris Forbes1b466bd2015-04-15 06:59:41 +1200221 }
Tobin Ehlisaf14e9f2015-09-01 11:59:36 -0600222 if (log_output == NULL) {
223 if (option_str)
224 std::cout << std::endl << "ShaderChecker ERROR: Bad output filename specified: " << option_str << ". Writing to STDOUT instead" << std::endl << std::endl;
Chris Forbese20111c2015-07-03 13:50:24 +1200225 log_output = stdout;
Tobin Ehlisaf14e9f2015-09-01 11:59:36 -0600226 }
Chris Forbese20111c2015-07-03 13:50:24 +1200227
228 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &my_data->logging_callback);
229 }
230
231 if (!globalLockInitialized)
232 {
233 // TODO/TBD: Need to delete this mutex sometime. How??? One
234 // suggestion is to call this during vkCreateInstance(), and then we
235 // can clean it up during vkDestroyInstance(). However, that requires
236 // that the layer have per-instance locks. We need to come back and
237 // address this soon.
238 loader_platform_thread_create_mutex(&globalLock);
239 globalLockInitialized = 1;
Chris Forbes1b466bd2015-04-15 06:59:41 +1200240 }
241}
242
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600243static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600244 {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600245 "ShaderChecker",
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600246 VK_API_VERSION,
247 VK_MAKE_VERSION(0, 1, 0),
248 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600249 }
Chris Forbesaab9d112015-04-02 13:22:31 +1300250};
251
Tony Barbour426b9052015-06-24 16:06:58 -0600252VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600253 const char *pLayerName,
254 uint32_t *pCount,
255 VkExtensionProperties* pProperties)
Chris Forbesaab9d112015-04-02 13:22:31 +1300256{
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600257 /* shader checker does not have any global extensions */
258 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbesaab9d112015-04-02 13:22:31 +1300259}
260
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600261VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalLayerProperties(
262 uint32_t *pCount,
263 VkLayerProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600264{
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600265 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
266 shader_checker_global_layers,
267 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600268}
269
270VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceExtensionProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600271 VkPhysicalDevice physicalDevice,
272 const char* pLayerName,
273 uint32_t* pCount,
274 VkExtensionProperties* pProperties)
Jon Ashburnade3bee2015-06-10 16:43:31 -0600275{
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600276 /* Shader checker does not have any physical device extensions */
277 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
278}
Jon Ashburnade3bee2015-06-10 16:43:31 -0600279
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600280VK_LAYER_EXPORT VkResult VKAPI vkGetPhysicalDeviceLayerProperties(
281 VkPhysicalDevice physicalDevice,
282 uint32_t* pCount,
283 VkLayerProperties* pProperties)
284{
285 /* Shader checker physical device layers are the same as global */
286 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
287 shader_checker_global_layers,
288 pCount, pProperties);
Jon Ashburnade3bee2015-06-10 16:43:31 -0600289}
Chris Forbesaab9d112015-04-02 13:22:31 +1300290
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200291static char const *
292storage_class_name(unsigned sc)
293{
294 switch (sc) {
Cody Northrop812b4612015-04-20 14:09:40 -0600295 case spv::StorageClassInput: return "input";
296 case spv::StorageClassOutput: return "output";
297 case spv::StorageClassUniformConstant: return "const uniform";
298 case spv::StorageClassUniform: return "uniform";
299 case spv::StorageClassWorkgroupLocal: return "workgroup local";
300 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
301 case spv::StorageClassPrivateGlobal: return "private global";
302 case spv::StorageClassFunction: return "function";
303 case spv::StorageClassGeneric: return "generic";
Cody Northrop812b4612015-04-20 14:09:40 -0600304 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF3aaa0882015-07-21 17:22:50 -0600305 case spv::StorageClassImage: return "image";
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200306 default: return "unknown";
307 }
308}
309
310
311/* returns ptr to null terminator */
312static char *
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600313describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200314{
315 auto type_def_it = src->type_def_index.find(type);
316
317 if (type_def_it == src->type_def_index.end()) {
318 return dst + sprintf(dst, "undef");
319 }
320
321 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
322 unsigned opcode = code[0] & 0x0ffffu;
323 switch (opcode) {
324 case spv::OpTypeBool:
325 return dst + sprintf(dst, "bool");
326 case spv::OpTypeInt:
327 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
328 case spv::OpTypeFloat:
329 return dst + sprintf(dst, "float%d", code[2]);
330 case spv::OpTypeVector:
331 dst += sprintf(dst, "vec%d of ", code[3]);
332 return describe_type(dst, src, code[2]);
333 case spv::OpTypeMatrix:
334 dst += sprintf(dst, "mat%d of ", code[3]);
335 return describe_type(dst, src, code[2]);
336 case spv::OpTypeArray:
337 dst += sprintf(dst, "arr[%d] of ", code[3]);
338 return describe_type(dst, src, code[2]);
339 case spv::OpTypePointer:
340 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
341 return describe_type(dst, src, code[3]);
342 case spv::OpTypeStruct:
343 {
344 unsigned oplen = code[0] >> 16;
345 dst += sprintf(dst, "struct of (");
Ian Elliottf21f14b2015-04-17 11:05:04 -0600346 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200347 dst = describe_type(dst, src, code[i]);
348 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
349 }
350 return dst;
351 }
352 default:
353 return dst + sprintf(dst, "oddtype");
354 }
355}
356
357
358static bool
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600359types_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 +1200360{
361 auto a_type_def_it = a->type_def_index.find(a_type);
362 auto b_type_def_it = b->type_def_index.find(b_type);
363
364 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200365 return false;
366 }
367
368 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200369 return false;
370 }
371
372 /* walk two type trees together, and complain about differences */
373 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
374 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
375
376 unsigned a_opcode = a_code[0] & 0x0ffffu;
377 unsigned b_opcode = b_code[0] & 0x0ffffu;
378
Chris Forbes0a94a372015-06-05 14:57:05 +1200379 if (b_arrayed && b_opcode == spv::OpTypeArray) {
380 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
381 return types_match(a, b, a_type, b_code[2], false);
382 }
383
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200384 if (a_opcode != b_opcode) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200385 return false;
386 }
387
388 switch (a_opcode) {
Chris Forbes0a94a372015-06-05 14:57:05 +1200389 /* 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 +1200390 case spv::OpTypeBool:
Chris Forbes0a94a372015-06-05 14:57:05 +1200391 return true && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200392 case spv::OpTypeInt:
393 /* match on width, signedness */
Chris Forbes0a94a372015-06-05 14:57:05 +1200394 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200395 case spv::OpTypeFloat:
396 /* match on width */
Chris Forbes0a94a372015-06-05 14:57:05 +1200397 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200398 case spv::OpTypeVector:
399 case spv::OpTypeMatrix:
400 case spv::OpTypeArray:
Chris Forbes0a94a372015-06-05 14:57:05 +1200401 /* match on element type, count. these all have the same layout. we don't get here if
402 * b_arrayed -- that is handled above. */
403 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 +1200404 case spv::OpTypeStruct:
405 /* match on all element types */
406 {
Chris Forbes0a94a372015-06-05 14:57:05 +1200407 if (b_arrayed) {
408 /* for the purposes of matching different levels of arrayness, structs are leaves. */
409 return false;
410 }
411
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200412 unsigned a_len = a_code[0] >> 16;
413 unsigned b_len = b_code[0] >> 16;
414
415 if (a_len != b_len) {
416 return false; /* structs cannot match if member counts differ */
417 }
418
Ian Elliottf21f14b2015-04-17 11:05:04 -0600419 for (unsigned i = 2; i < a_len; i++) {
Chris Forbes0a94a372015-06-05 14:57:05 +1200420 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200421 return false;
422 }
423 }
424
425 return true;
426 }
427 case spv::OpTypePointer:
428 /* match on pointee type. storage class is expected to differ */
Chris Forbes0a94a372015-06-05 14:57:05 +1200429 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200430
431 default:
432 /* remaining types are CLisms, or may not appear in the interfaces we
433 * are interested in. Just claim no match.
434 */
435 return false;
436
437 }
438}
439
440
Chris Forbes67cc36f2015-04-13 12:14:52 +1200441static int
442value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
443{
444 auto it = map.find(id);
445 if (it == map.end())
446 return def;
447 else
448 return it->second;
449}
450
451
452struct interface_var {
453 uint32_t id;
454 uint32_t type_id;
455 /* TODO: collect the name, too? Isn't required to be present. */
456};
457
458
459static void
Chris Forbese20111c2015-07-03 13:50:24 +1200460collect_interface_by_location(VkDevice dev,
461 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes67cc36f2015-04-13 12:14:52 +1200462 std::map<uint32_t, interface_var> &out,
463 std::map<uint32_t, interface_var> &builtins_out)
464{
465 unsigned int const *code = (unsigned int const *)&src->words[0];
466 size_t size = src->words.size();
467
Chris Forbes67cc36f2015-04-13 12:14:52 +1200468 std::unordered_map<unsigned, unsigned> var_locations;
469 std::unordered_map<unsigned, unsigned> var_builtins;
470
471 unsigned word = 5;
472 while (word < size) {
473
474 unsigned opcode = code[word] & 0x0ffffu;
475 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
476
477 /* We consider two interface models: SSO rendezvous-by-location, and
478 * builtins. Complain about anything that fits neither model.
479 */
480 if (opcode == spv::OpDecorate) {
Cody Northrop812b4612015-04-20 14:09:40 -0600481 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200482 var_locations[code[word+1]] = code[word+3];
483 }
484
Cody Northrop812b4612015-04-20 14:09:40 -0600485 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200486 var_builtins[code[word+1]] = code[word+3];
487 }
488 }
489
490 /* TODO: handle grouped decorations */
491 /* TODO: handle index=1 dual source outputs from FS -- two vars will
492 * have the same location, and we DONT want to clobber. */
493
Ian Elliottf21f14b2015-04-17 11:05:04 -0600494 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200495 int location = value_or_default(var_locations, code[word+2], -1);
496 int builtin = value_or_default(var_builtins, code[word+2], -1);
497
498 if (location == -1 && builtin == -1) {
499 /* No location defined, and not bound to an API builtin.
500 * The spec says nothing about how this case works (or doesn't)
501 * for interface matching.
502 */
Chris Forbes2b7c0002015-07-10 09:06:54 +1200503 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 +1200504 "var %d (type %d) in %s interface has no Location or Builtin decoration",
505 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes67cc36f2015-04-13 12:14:52 +1200506 }
507 else if (location != -1) {
508 /* A user-defined interface variable, with a location. */
509 interface_var v;
510 v.id = code[word+2];
511 v.type_id = code[word+1];
512 out[location] = v;
513 }
514 else {
515 /* A builtin interface variable */
516 interface_var v;
517 v.id = code[word+2];
518 v.type_id = code[word+1];
519 builtins_out[builtin] = v;
520 }
521 }
522
523 word += oplen;
524 }
525}
526
527
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600528VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
529 VkDevice device,
530 const VkShaderModuleCreateInfo *pCreateInfo,
531 VkShaderModule *pShaderModule)
Chris Forbesaab9d112015-04-02 13:22:31 +1300532{
Chris Forbes1ed0f982015-05-29 14:55:18 +1200533 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbese20111c2015-07-03 13:50:24 +1200534 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShaderModule(device, pCreateInfo, pShaderModule);
Chris Forbes4396ff52015-04-08 10:11:59 +1200535
Chris Forbes2b7c0002015-07-10 09:06:54 +1200536 shader_module_map[pShaderModule->handle] = new shader_module(device, pCreateInfo);
Chris Forbes1ed0f982015-05-29 14:55:18 +1200537 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbesaab9d112015-04-02 13:22:31 +1300538 return res;
539}
540
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600541VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
542 VkDevice device,
543 const VkShaderCreateInfo *pCreateInfo,
544 VkShader *pShader)
545{
546 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbese20111c2015-07-03 13:50:24 +1200547 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShader(device, pCreateInfo, pShader);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600548
Chris Forbes2b7c0002015-07-10 09:06:54 +1200549 shader_object_map[pShader->handle] = new shader_object(pCreateInfo);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600550 loader_platform_thread_unlock_mutex(&globalLock);
551 return res;
552}
Chris Forbesaab9d112015-04-02 13:22:31 +1300553
Chia-I Wuc278df82015-07-07 11:50:03 +0800554VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
555 VkDevice device,
556 const VkRenderPassCreateInfo *pCreateInfo,
557 VkRenderPass *pRenderPass)
558{
559 loader_platform_thread_lock_mutex(&globalLock);
560 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateRenderPass(device, pCreateInfo, pRenderPass);
561
Chris Forbes2b7c0002015-07-10 09:06:54 +1200562 render_pass_map[pRenderPass->handle] = new render_pass(pCreateInfo);
Chia-I Wuc278df82015-07-07 11:50:03 +0800563 loader_platform_thread_unlock_mutex(&globalLock);
564 return res;
565}
566
Chris Forbes5f362d02015-05-25 11:13:22 +1200567static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200568validate_interface_between_stages(VkDevice dev,
569 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600570 shader_module const *consumer, char const *consumer_name,
Chris Forbes4453c772015-06-05 15:01:08 +1200571 bool consumer_arrayed_input)
Chris Forbesbb164b62015-04-08 10:19:16 +1200572{
573 std::map<uint32_t, interface_var> outputs;
574 std::map<uint32_t, interface_var> inputs;
575
576 std::map<uint32_t, interface_var> builtin_outputs;
577 std::map<uint32_t, interface_var> builtin_inputs;
578
Chris Forbes5f362d02015-05-25 11:13:22 +1200579 bool pass = true;
Chris Forbesbb164b62015-04-08 10:19:16 +1200580
Chris Forbese20111c2015-07-03 13:50:24 +1200581 collect_interface_by_location(dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
582 collect_interface_by_location(dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesbb164b62015-04-08 10:19:16 +1200583
584 auto a_it = outputs.begin();
585 auto b_it = inputs.begin();
586
587 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedof5997ab2015-04-27 16:36:17 -0600588 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
589 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
590 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200591 auto a_first = a_at_end ? 0 : a_it->first;
592 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600593
594 if (b_at_end || a_first < b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200595 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 +1200596 "%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 +1200597 a_it++;
598 }
David Pinedof5997ab2015-04-27 16:36:17 -0600599 else if (a_at_end || a_first > b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200600 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 +1200601 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name);
Chris Forbes5f362d02015-05-25 11:13:22 +1200602 pass = false;
Chris Forbesbb164b62015-04-08 10:19:16 +1200603 b_it++;
604 }
605 else {
Chris Forbes4453c772015-06-05 15:01:08 +1200606 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 +1200607 /* OK! */
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200608 }
609 else {
610 char producer_type[1024];
611 char consumer_type[1024];
612 describe_type(producer_type, producer, a_it->second.type_id);
613 describe_type(consumer_type, consumer, b_it->second.type_id);
614
Chris Forbes2b7c0002015-07-10 09:06:54 +1200615 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 +1200616 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200617 pass = false;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200618 }
Chris Forbesbb164b62015-04-08 10:19:16 +1200619 a_it++;
620 b_it++;
621 }
622 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200623
624 return pass;
Chris Forbesbb164b62015-04-08 10:19:16 +1200625}
626
627
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200628enum FORMAT_TYPE {
629 FORMAT_TYPE_UNDEFINED,
630 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
631 FORMAT_TYPE_SINT,
632 FORMAT_TYPE_UINT,
633};
634
635
636static unsigned
637get_format_type(VkFormat fmt) {
638 switch (fmt) {
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800639 case VK_FORMAT_UNDEFINED:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200640 return FORMAT_TYPE_UNDEFINED;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800641 case VK_FORMAT_R8_SINT:
642 case VK_FORMAT_R8G8_SINT:
643 case VK_FORMAT_R8G8B8_SINT:
644 case VK_FORMAT_R8G8B8A8_SINT:
645 case VK_FORMAT_R16_SINT:
646 case VK_FORMAT_R16G16_SINT:
647 case VK_FORMAT_R16G16B16_SINT:
648 case VK_FORMAT_R16G16B16A16_SINT:
649 case VK_FORMAT_R32_SINT:
650 case VK_FORMAT_R32G32_SINT:
651 case VK_FORMAT_R32G32B32_SINT:
652 case VK_FORMAT_R32G32B32A32_SINT:
653 case VK_FORMAT_B8G8R8_SINT:
654 case VK_FORMAT_B8G8R8A8_SINT:
655 case VK_FORMAT_R10G10B10A2_SINT:
656 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200657 return FORMAT_TYPE_SINT;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800658 case VK_FORMAT_R8_UINT:
659 case VK_FORMAT_R8G8_UINT:
660 case VK_FORMAT_R8G8B8_UINT:
661 case VK_FORMAT_R8G8B8A8_UINT:
662 case VK_FORMAT_R16_UINT:
663 case VK_FORMAT_R16G16_UINT:
664 case VK_FORMAT_R16G16B16_UINT:
665 case VK_FORMAT_R16G16B16A16_UINT:
666 case VK_FORMAT_R32_UINT:
667 case VK_FORMAT_R32G32_UINT:
668 case VK_FORMAT_R32G32B32_UINT:
669 case VK_FORMAT_R32G32B32A32_UINT:
670 case VK_FORMAT_B8G8R8_UINT:
671 case VK_FORMAT_B8G8R8A8_UINT:
672 case VK_FORMAT_R10G10B10A2_UINT:
673 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200674 return FORMAT_TYPE_UINT;
675 default:
676 return FORMAT_TYPE_FLOAT;
677 }
678}
679
680
Chris Forbes28c50882015-05-04 14:04:06 +1200681/* characterizes a SPIR-V type appearing in an interface to a FF stage,
682 * for comparison to a VkFormat's characterization above. */
683static unsigned
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600684get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes28c50882015-05-04 14:04:06 +1200685{
686 auto type_def_it = src->type_def_index.find(type);
687
688 if (type_def_it == src->type_def_index.end()) {
689 return FORMAT_TYPE_UNDEFINED;
690 }
691
692 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
693 unsigned opcode = code[0] & 0x0ffffu;
694 switch (opcode) {
695 case spv::OpTypeInt:
696 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
697 case spv::OpTypeFloat:
698 return FORMAT_TYPE_FLOAT;
699 case spv::OpTypeVector:
700 return get_fundamental_type(src, code[2]);
701 case spv::OpTypeMatrix:
702 return get_fundamental_type(src, code[2]);
703 case spv::OpTypeArray:
704 return get_fundamental_type(src, code[2]);
705 case spv::OpTypePointer:
706 return get_fundamental_type(src, code[3]);
707 default:
708 return FORMAT_TYPE_UNDEFINED;
709 }
710}
711
712
Chris Forbes5f362d02015-05-25 11:13:22 +1200713static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200714validate_vi_consistency(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200715{
716 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
717 * each binding should be specified only once.
718 */
719 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200720 bool pass = true;
721
722 for (unsigned i = 0; i < vi->bindingCount; i++) {
723 auto desc = &vi->pVertexBindingDescriptions[i];
724 auto & binding = bindings[desc->binding];
725 if (binding) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200726 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 +1200727 "Duplicate vertex input binding descriptions for binding %d", desc->binding);
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200728 pass = false;
729 }
730 else {
731 binding = desc;
732 }
733 }
734
735 return pass;
736}
737
738
739static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200740validate_vi_against_vs_inputs(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbesfcd05f12015-04-08 10:36:37 +1200741{
742 std::map<uint32_t, interface_var> inputs;
743 /* we collect builtin inputs, but they will never appear in the VI state --
744 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
745 */
746 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbes5f362d02015-05-25 11:13:22 +1200747 bool pass = true;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200748
Chris Forbese20111c2015-07-03 13:50:24 +1200749 collect_interface_by_location(dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200750
751 /* Build index by location */
752 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes6f2ab982015-05-25 11:13:24 +1200753 if (vi) {
754 for (unsigned i = 0; i < vi->attributeCount; i++)
755 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
756 }
Chris Forbesfcd05f12015-04-08 10:36:37 +1200757
758 auto it_a = attribs.begin();
759 auto it_b = inputs.begin();
760
David Pinedof5997ab2015-04-27 16:36:17 -0600761 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
762 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
763 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200764 auto a_first = a_at_end ? 0 : it_a->first;
765 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600766 if (b_at_end || a_first < b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200767 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 +1200768 "Vertex attribute at location %d not consumed by VS", a_first);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200769 it_a++;
770 }
David Pinedof5997ab2015-04-27 16:36:17 -0600771 else if (a_at_end || b_first < a_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200772 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 +1200773 "VS consumes input at location %d but not provided", b_first);
Chris Forbes5f362d02015-05-25 11:13:22 +1200774 pass = false;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200775 it_b++;
776 }
777 else {
Chris Forbes3317b382015-05-04 14:04:24 +1200778 unsigned attrib_type = get_format_type(it_a->second->format);
779 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
780
781 /* type checking */
782 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
783 char vs_type[1024];
784 describe_type(vs_type, vs, it_b->second.type_id);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200785 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 +1200786 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Chris Forbes3317b382015-05-04 14:04:24 +1200787 string_VkFormat(it_a->second->format), a_first, vs_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200788 pass = false;
Chris Forbes3317b382015-05-04 14:04:24 +1200789 }
790
Chris Forbes5c75afe2015-04-17 10:13:28 +1200791 /* OK! */
Chris Forbesfcd05f12015-04-08 10:36:37 +1200792 it_a++;
793 it_b++;
794 }
795 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200796
797 return pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200798}
799
800
Chris Forbes5f362d02015-05-25 11:13:22 +1200801static bool
Chia-I Wuc278df82015-07-07 11:50:03 +0800802validate_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 +1200803{
Chia-I Wuc278df82015-07-07 11:50:03 +0800804 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200805 std::map<uint32_t, interface_var> outputs;
806 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbes5f362d02015-05-25 11:13:22 +1200807 bool pass = true;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200808
809 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
810
Chris Forbese20111c2015-07-03 13:50:24 +1200811 collect_interface_by_location(dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200812
813 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
814 * and all color attachment should be UNORM/SNORM/FLOAT.
815 */
816 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200817 if (outputs.size()) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200818 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 +1200819 "Should not have user-defined FS outputs when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200820 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200821 }
822
Chia-I Wuc278df82015-07-07 11:50:03 +0800823 for (unsigned i = 0; i < color_formats.size(); i++) {
824 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200825 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200826 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 +1200827 "CB format should not be SINT or UINT when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200828 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200829 }
830 }
831
Chris Forbes5f362d02015-05-25 11:13:22 +1200832 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200833 }
834
835 auto it = outputs.begin();
836 uint32_t attachment = 0;
837
838 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
839 * are currently dense, but the parallel with matching between shader stages is nice.
840 */
841
Chris Forbes2b7c0002015-07-10 09:06:54 +1200842 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbes768eead2015-07-11 11:05:01 +1200843 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
844 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200845 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 +1200846 "FS writes to output location %d with no matching attachment", it->first);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200847 it++;
848 }
849 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200850 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 +1200851 "Attachment %d not written by FS", attachment);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200852 attachment++;
Chris Forbes5f362d02015-05-25 11:13:22 +1200853 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200854 }
855 else {
Chris Forbes4b009002015-05-04 14:20:10 +1200856 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wuc278df82015-07-07 11:50:03 +0800857 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes4b009002015-05-04 14:20:10 +1200858
859 /* type checking */
860 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
861 char fs_type[1024];
862 describe_type(fs_type, fs, it->second.type_id);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200863 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 +1200864 "Attachment %d of type `%s` does not match FS output type of `%s`",
Chia-I Wuc278df82015-07-07 11:50:03 +0800865 attachment, string_VkFormat(color_formats[attachment]), fs_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200866 pass = false;
Chris Forbes4b009002015-05-04 14:20:10 +1200867 }
868
Chris Forbes5c75afe2015-04-17 10:13:28 +1200869 /* OK! */
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200870 it++;
871 attachment++;
872 }
873 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200874
875 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200876}
877
878
Chris Forbes4453c772015-06-05 15:01:08 +1200879struct shader_stage_attributes {
880 char const * const name;
881 bool arrayed_input;
882};
883
884
885static shader_stage_attributes
886shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
887 { "vertex shader", false },
888 { "tessellation control shader", true },
889 { "tessellation evaluation shader", false },
890 { "geometry shader", true },
891 { "fragment shader", false },
892};
893
894
Chris Forbesf1060ca2015-06-04 20:23:00 +1200895static bool
Chris Forbesd8bde292015-07-24 13:53:47 +1200896validate_graphics_pipeline(VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes60540932015-04-08 10:15:35 +1200897{
Chris Forbes8f600932015-04-08 10:16:45 +1200898 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
899 * before trying to do anything more: */
900
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600901 shader_module const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
Chris Forbes4453c772015-06-05 15:01:08 +1200902 memset(shaders, 0, sizeof(shaders));
Chia-I Wuc278df82015-07-07 11:50:03 +0800903 render_pass const *rp = 0;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600904 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbes5f362d02015-05-25 11:13:22 +1200905 bool pass = true;
Chris Forbes8f600932015-04-08 10:16:45 +1200906
Chris Forbes1ed0f982015-05-29 14:55:18 +1200907 loader_platform_thread_lock_mutex(&globalLock);
908
Tony Barbourb2cc40a2015-07-13 15:28:47 -0600909 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600910 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
911 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbes8f600932015-04-08 10:16:45 +1200912
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600913 if (pStage->stage < VK_SHADER_STAGE_VERTEX || pStage->stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200914 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 +1200915 "Unknown shader stage %d", pStage->stage);
Chris Forbes5c75afe2015-04-17 10:13:28 +1200916 }
Chris Forbes4453c772015-06-05 15:01:08 +1200917 else {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200918 struct shader_object *shader = shader_object_map[pStage->shader.handle];
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600919 shaders[pStage->stage] = shader->module;
Chris Forbes4453c772015-06-05 15:01:08 +1200920 }
Chris Forbes8f600932015-04-08 10:16:45 +1200921 }
Chris Forbes8f600932015-04-08 10:16:45 +1200922 }
923
Chia-I Wuc278df82015-07-07 11:50:03 +0800924 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Chris Forbes2b7c0002015-07-10 09:06:54 +1200925 rp = render_pass_map[pCreateInfo->renderPass.handle];
Chia-I Wuc278df82015-07-07 11:50:03 +0800926
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600927 vi = pCreateInfo->pVertexInputState;
928
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200929 if (vi) {
Chris Forbese20111c2015-07-03 13:50:24 +1200930 pass = validate_vi_consistency(dev, vi) && pass;
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200931 }
932
Chris Forbes4453c772015-06-05 15:01:08 +1200933 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
Chris Forbese20111c2015-07-03 13:50:24 +1200934 pass = validate_vi_against_vs_inputs(dev, vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200935 }
936
Chris Forbes4453c772015-06-05 15:01:08 +1200937 /* TODO: enforce rules about present combinations of shaders */
938 int producer = VK_SHADER_STAGE_VERTEX;
939 int consumer = VK_SHADER_STAGE_GEOMETRY;
940
941 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
942 producer++;
943 consumer++;
Chris Forbesbb164b62015-04-08 10:19:16 +1200944 }
945
Tony Barbour4eb3cd12015-06-11 15:04:25 -0600946 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbes4453c772015-06-05 15:01:08 +1200947 assert(shaders[producer]);
948 if (shaders[consumer]) {
949 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
Chris Forbese20111c2015-07-03 13:50:24 +1200950 pass = validate_interface_between_stages(dev,
951 shaders[producer], shader_stage_attribs[producer].name,
Chris Forbes4453c772015-06-05 15:01:08 +1200952 shaders[consumer], shader_stage_attribs[consumer].name,
953 shader_stage_attribs[consumer].arrayed_input) && pass;
954 }
955
956 producer = consumer;
957 }
958 }
959
Chia-I Wuc278df82015-07-07 11:50:03 +0800960 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && rp) {
961 pass = validate_fs_outputs_against_render_pass(dev, shaders[VK_SHADER_STAGE_FRAGMENT], rp, pCreateInfo->subpass) && pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200962 }
963
Chris Forbes1ed0f982015-05-29 14:55:18 +1200964 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbesf1060ca2015-06-04 20:23:00 +1200965 return pass;
966}
967
Jon Ashburn0d60d272015-07-09 15:02:25 -0600968//TODO handle pipelineCache entry points
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200969VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburn0d60d272015-07-09 15:02:25 -0600970vkCreateGraphicsPipelines(VkDevice device,
971 VkPipelineCache pipelineCache,
972 uint32_t count,
973 const VkGraphicsPipelineCreateInfo *pCreateInfos,
974 VkPipeline *pPipelines)
Chris Forbesf1060ca2015-06-04 20:23:00 +1200975{
Chris Forbesd8bde292015-07-24 13:53:47 +1200976 bool pass = true;
977 for (uint32_t i = 0; i < count; i++) {
978 pass = validate_graphics_pipeline(device, &pCreateInfos[i]) && pass;
979 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200980
981 if (pass) {
982 /* The driver is allowed to crash if passed junk. Only actually create the
983 * pipeline if we didn't run into any showstoppers above.
984 */
Jon Ashburn0d60d272015-07-09 15:02:25 -0600985 return get_dispatch_table(shader_checker_device_table_map, device)->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines);
Chris Forbes5f362d02015-05-25 11:13:22 +1200986 }
987 else {
988 return VK_ERROR_UNKNOWN;
989 }
Chris Forbes60540932015-04-08 10:15:35 +1200990}
991
992
Chris Forbese20111c2015-07-03 13:50:24 +1200993VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
994{
995 VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
996 VkResult result = pDeviceTable->CreateDevice(gpu, pCreateInfo, pDevice);
997 if (result == VK_SUCCESS) {
998 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
999 VkLayerDispatchTable *pTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
1000 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
1001 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1002 }
1003 return result;
1004}
Chris Forbesd0f7f7c2015-06-04 20:27:09 +12001005
Jon Ashburn17f37372015-05-19 16:34:53 -06001006/* hook DextroyDevice to remove tableMap entry */
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001007VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device)
Jon Ashburn17f37372015-05-19 16:34:53 -06001008{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001009 dispatch_key key = get_dispatch_key(device);
Chris Forbese20111c2015-07-03 13:50:24 +12001010 VkLayerDispatchTable *pDisp = get_dispatch_table(shader_checker_device_table_map, device);
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001011 pDisp->DestroyDevice(device);
Chris Forbese20111c2015-07-03 13:50:24 +12001012 shader_checker_device_table_map.erase(key);
Jon Ashburn17f37372015-05-19 16:34:53 -06001013}
1014
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001015VkResult VKAPI vkCreateInstance(
1016 const VkInstanceCreateInfo* pCreateInfo,
1017 VkInstance* pInstance)
1018{
Chris Forbese20111c2015-07-03 13:50:24 +12001019 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map,*pInstance);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001020 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
1021
1022 if (result == VK_SUCCESS) {
Chris Forbese20111c2015-07-03 13:50:24 +12001023 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1024 my_data->report_data = debug_report_create_instance(
1025 pTable,
1026 *pInstance,
1027 pCreateInfo->extensionCount,
1028 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterf4a2eba2015-06-08 14:58:39 -06001029
Chris Forbese20111c2015-07-03 13:50:24 +12001030 init_shader_checker(my_data);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001031 }
1032 return result;
1033}
1034
Jon Ashburn17f37372015-05-19 16:34:53 -06001035/* hook DestroyInstance to remove tableInstanceMap entry */
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001036VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance)
Jon Ashburn17f37372015-05-19 16:34:53 -06001037{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001038 dispatch_key key = get_dispatch_key(instance);
Chris Forbese20111c2015-07-03 13:50:24 +12001039 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001040 pTable->DestroyInstance(instance);
Chris Forbese20111c2015-07-03 13:50:24 +12001041
1042 // Clean up logging callback, if any
1043 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
1044 if (my_data->logging_callback) {
1045 layer_destroy_msg_callback(my_data->report_data, my_data->logging_callback);
1046 }
1047
1048 layer_debug_report_destroy_instance(my_data->report_data);
1049 layer_data_map.erase(pTable);
1050
1051 shader_checker_instance_table_map.erase(key);
Jon Ashburn17f37372015-05-19 16:34:53 -06001052}
Chris Forbesb65ba352015-05-25 11:12:59 +12001053
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001054VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbese20111c2015-07-03 13:50:24 +12001055 VkInstance instance,
1056 VkFlags msgFlags,
1057 const PFN_vkDbgMsgCallback pfnMsgCallback,
1058 void* pUserData,
1059 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001060{
Chris Forbese20111c2015-07-03 13:50:24 +12001061 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1062 VkResult res = pTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1063 if (VK_SUCCESS == res) {
1064 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1065 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1066 }
1067 return res;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001068}
1069
1070VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbese20111c2015-07-03 13:50:24 +12001071 VkInstance instance,
1072 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001073{
Chris Forbese20111c2015-07-03 13:50:24 +12001074 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1075 VkResult res = pTable->DbgDestroyMsgCallback(instance, msgCallback);
1076 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1077 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1078 return res;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001079}
1080
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001081VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbesaab9d112015-04-02 13:22:31 +13001082{
Chris Forbese20111c2015-07-03 13:50:24 +12001083 if (dev == NULL)
Chris Forbesaab9d112015-04-02 13:22:31 +13001084 return NULL;
1085
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001086 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbese20111c2015-07-03 13:50:24 +12001087 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
1088 initDeviceTable(shader_checker_device_table_map, (const VkBaseLayerObject *) dev);
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001089 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001090 }
1091
Chris Forbesaab9d112015-04-02 13:22:31 +13001092#define ADD_HOOK(fn) \
Chris Forbese20111c2015-07-03 13:50:24 +12001093 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001094 return (PFN_vkVoidFunction) fn
Chris Forbesaab9d112015-04-02 13:22:31 +13001095
Chris Forbese20111c2015-07-03 13:50:24 +12001096 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001097 ADD_HOOK(vkCreateShaderModule);
Chris Forbesaab9d112015-04-02 13:22:31 +13001098 ADD_HOOK(vkCreateShader);
Chia-I Wuc278df82015-07-07 11:50:03 +08001099 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn17f37372015-05-19 16:34:53 -06001100 ADD_HOOK(vkDestroyDevice);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001101 ADD_HOOK(vkCreateGraphicsPipelines);
Jon Ashburn8198fd02015-05-18 09:08:41 -06001102#undef ADD_HOOK
Chris Forbese20111c2015-07-03 13:50:24 +12001103
1104 VkLayerDispatchTable* pTable = get_dispatch_table(shader_checker_device_table_map, dev);
1105 {
1106 if (pTable->GetDeviceProcAddr == NULL)
1107 return NULL;
1108 return pTable->GetDeviceProcAddr(dev, funcName);
1109 }
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001110}
1111
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001112VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001113{
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001114 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001115
Chris Forbese20111c2015-07-03 13:50:24 +12001116 if (instance == NULL)
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001117 return NULL;
1118
Chris Forbese20111c2015-07-03 13:50:24 +12001119 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
1120 initInstanceTable(shader_checker_instance_table_map, (const VkBaseLayerObject *) instance);
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001121 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001122 }
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001123#define ADD_HOOK(fn) \
Chris Forbese20111c2015-07-03 13:50:24 +12001124 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001125 return (PFN_vkVoidFunction) fn
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001126
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001127 ADD_HOOK(vkCreateInstance);
Jon Ashburn17f37372015-05-19 16:34:53 -06001128 ADD_HOOK(vkDestroyInstance);
Tony Barbour426b9052015-06-24 16:06:58 -06001129 ADD_HOOK(vkGetGlobalExtensionProperties);
Tony Barbour426b9052015-06-24 16:06:58 -06001130 ADD_HOOK(vkGetPhysicalDeviceExtensionProperties);
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -06001131 ADD_HOOK(vkGetGlobalLayerProperties);
1132 ADD_HOOK(vkGetPhysicalDeviceLayerProperties);
Jon Ashburn8198fd02015-05-18 09:08:41 -06001133#undef ADD_HOOK
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001134
Chris Forbese20111c2015-07-03 13:50:24 +12001135
1136 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1137 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001138 if (fptr)
1139 return fptr;
1140
Chris Forbese20111c2015-07-03 13:50:24 +12001141 {
1142 VkLayerInstanceDispatchTable* pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1143 if (pTable->GetInstanceProcAddr == NULL)
1144 return NULL;
1145 return pTable->GetInstanceProcAddr(instance, funcName);
1146 }
Chris Forbesaab9d112015-04-02 13:22:31 +13001147}