blob: 4127d02ce1e2d2d5aa0d8e321e188798070251d0 [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
Chris Forbese0e94992015-08-12 15:03:22 +120091std::unordered_map<uint32_t, std::vector<VkDescriptorSetLayoutBinding>*> descriptor_set_layout_map;
92
93VK_LAYER_EXPORT VkResult VKAPI vkCreateDescriptorSetLayout(
94 VkDevice device,
95 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
96 VkDescriptorSetLayout* pSetLayout)
97{
98 /* stash a copy of the layout bindings */
99 VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, device);
100 VkResult result = pDeviceTable->CreateDescriptorSetLayout(device, pCreateInfo, pSetLayout);
101
102 if (VK_SUCCESS == result) {
103 loader_platform_thread_lock_mutex(&globalLock);
104 auto& bindings = descriptor_set_layout_map[pSetLayout->handle];
105 bindings = new std::vector<VkDescriptorSetLayoutBinding>(
106 pCreateInfo->pBinding, pCreateInfo->pBinding + pCreateInfo->count);
107 loader_platform_thread_unlock_mutex(&globalLock);
108 }
109
110 return result;
111}
112
113
114std::unordered_map<uint32_t, std::vector<std::vector<VkDescriptorSetLayoutBinding>*>*> pipeline_layout_map;
115
116VK_LAYER_EXPORT VkResult VKAPI vkCreatePipelineLayout(
117 VkDevice device,
118 const VkPipelineLayoutCreateInfo* pCreateInfo,
119 VkPipelineLayout* pPipelineLayout)
120{
121 VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, device);
122 VkResult result = pDeviceTable->CreatePipelineLayout(device, pCreateInfo, pPipelineLayout);
123
124 if (VK_SUCCESS == result) {
125 loader_platform_thread_lock_mutex(&globalLock);
126 auto& layouts = pipeline_layout_map[pPipelineLayout->handle];
127 layouts = new std::vector<std::vector<VkDescriptorSetLayoutBinding>*>();
128 layouts->reserve(pCreateInfo->descriptorSetCount);
129 for (unsigned i = 0; i < pCreateInfo->descriptorSetCount; i++) {
130 layouts->push_back(descriptor_set_layout_map[pCreateInfo->pSetLayouts[i].handle]);
131 }
132 loader_platform_thread_unlock_mutex(&globalLock);
133 }
134
135 return result;
136}
137
138
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200139static void
140build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
141{
142 unsigned int const *code = (unsigned int const *)&words[0];
143 size_t size = words.size();
144
145 unsigned word = 5;
146 while (word < size) {
147 unsigned opcode = code[word] & 0x0ffffu;
148 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
149
150 switch (opcode) {
151 case spv::OpTypeVoid:
152 case spv::OpTypeBool:
153 case spv::OpTypeInt:
154 case spv::OpTypeFloat:
155 case spv::OpTypeVector:
156 case spv::OpTypeMatrix:
GregF3aaa0882015-07-21 17:22:50 -0600157 case spv::OpTypeImage:
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200158 case spv::OpTypeSampler:
GregF3aaa0882015-07-21 17:22:50 -0600159 case spv::OpTypeSampledImage:
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200160 case spv::OpTypeArray:
161 case spv::OpTypeRuntimeArray:
162 case spv::OpTypeStruct:
163 case spv::OpTypeOpaque:
164 case spv::OpTypePointer:
165 case spv::OpTypeFunction:
166 case spv::OpTypeEvent:
167 case spv::OpTypeDeviceEvent:
168 case spv::OpTypeReserveId:
169 case spv::OpTypeQueue:
170 case spv::OpTypePipe:
171 type_def_index[code[word+1]] = word;
172 break;
173
174 default:
175 /* We only care about type definitions */
176 break;
177 }
178
179 word += oplen;
180 }
181}
182
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600183struct shader_module {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200184 /* the spirv image itself */
Chris Forbes4396ff52015-04-08 10:11:59 +1200185 std::vector<uint32_t> words;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200186 /* a mapping of <id> to the first word of its def. this is useful because walking type
187 * trees requires jumping all over the instruction stream.
188 */
189 std::unordered_map<unsigned, unsigned> type_def_index;
Chris Forbes4453c772015-06-05 15:01:08 +1200190 bool is_spirv;
Chris Forbes4396ff52015-04-08 10:11:59 +1200191
Chris Forbese20111c2015-07-03 13:50:24 +1200192 shader_module(VkDevice dev, VkShaderModuleCreateInfo const *pCreateInfo) :
Chris Forbes4453c772015-06-05 15:01:08 +1200193 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
194 type_def_index(),
195 is_spirv(true) {
196
197 if (words.size() < 5 || words[0] != spv::MagicNumber || words[1] != spv::Version) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200198 log_msg(mdd(dev), VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
Chris Forbese20111c2015-07-03 13:50:24 +1200199 "Shader is not SPIR-V, most checks will not be possible");
Chris Forbes4453c772015-06-05 15:01:08 +1200200 is_spirv = false;
201 return;
202 }
203
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200204
205 build_type_def_index(words, type_def_index);
Chris Forbes4396ff52015-04-08 10:11:59 +1200206 }
207};
208
209
Chris Forbes2b7c0002015-07-10 09:06:54 +1200210static std::unordered_map<uint64_t, shader_module *> shader_module_map;
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600211
212struct shader_object {
213 std::string name;
214 struct shader_module *module;
215
216 shader_object(VkShaderCreateInfo const *pCreateInfo)
217 {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200218 module = shader_module_map[pCreateInfo->module.handle];
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600219 name = pCreateInfo->pName;
220 }
221};
Chris Forbes2b7c0002015-07-10 09:06:54 +1200222static std::unordered_map<uint64_t, shader_object *> shader_object_map;
Chris Forbes4396ff52015-04-08 10:11:59 +1200223
Chia-I Wuc278df82015-07-07 11:50:03 +0800224struct render_pass {
225 std::vector<std::vector<VkFormat>> subpass_color_formats;
226
227 render_pass(VkRenderPassCreateInfo const *pCreateInfo)
228 {
229 uint32_t i;
230
231 subpass_color_formats.reserve(pCreateInfo->subpassCount);
232 for (i = 0; i < pCreateInfo->subpassCount; i++) {
233 const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i];
234 std::vector<VkFormat> color_formats;
235 uint32_t j;
236
237 color_formats.reserve(subpass->colorCount);
238 for (j = 0; j < subpass->colorCount; j++) {
Cody Northrop6de6b0b2015-08-04 11:16:41 -0600239 const uint32_t att = subpass->pColorAttachments[j].attachment;
Chia-I Wuc278df82015-07-07 11:50:03 +0800240 const VkFormat format = pCreateInfo->pAttachments[att].format;
241
242 color_formats.push_back(pCreateInfo->pAttachments[att].format);
243 }
244
245 subpass_color_formats.push_back(color_formats);
246 }
247 }
248};
Chris Forbes2b7c0002015-07-10 09:06:54 +1200249static std::unordered_map<uint64_t, render_pass *> render_pass_map;
Chia-I Wuc278df82015-07-07 11:50:03 +0800250
Chris Forbes4396ff52015-04-08 10:11:59 +1200251
Chris Forbes1b466bd2015-04-15 06:59:41 +1200252static void
Chris Forbese20111c2015-07-03 13:50:24 +1200253init_shader_checker(layer_data *my_data)
Chris Forbes1b466bd2015-04-15 06:59:41 +1200254{
Chris Forbese20111c2015-07-03 13:50:24 +1200255 uint32_t report_flags = 0;
256 uint32_t debug_action = 0;
257 FILE *log_output = NULL;
258 const char *option_str;
Chris Forbes1b466bd2015-04-15 06:59:41 +1200259 // initialize ShaderChecker options
Chris Forbese20111c2015-07-03 13:50:24 +1200260 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
261 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbes1b466bd2015-04-15 06:59:41 +1200262
Chris Forbese20111c2015-07-03 13:50:24 +1200263 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbes1b466bd2015-04-15 06:59:41 +1200264 {
Chris Forbese20111c2015-07-03 13:50:24 +1200265 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb4b6e7c2015-09-15 09:55:54 -0600266 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Chris Forbese20111c2015-07-03 13:50:24 +1200267 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &my_data->logging_callback);
268 }
269
270 if (!globalLockInitialized)
271 {
272 // TODO/TBD: Need to delete this mutex sometime. How??? One
273 // suggestion is to call this during vkCreateInstance(), and then we
274 // can clean it up during vkDestroyInstance(). However, that requires
275 // that the layer have per-instance locks. We need to come back and
276 // address this soon.
277 loader_platform_thread_create_mutex(&globalLock);
278 globalLockInitialized = 1;
Chris Forbes1b466bd2015-04-15 06:59:41 +1200279 }
280}
281
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600282static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600283 {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600284 "ShaderChecker",
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600285 VK_API_VERSION,
286 VK_MAKE_VERSION(0, 1, 0),
287 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600288 }
Chris Forbesaab9d112015-04-02 13:22:31 +1300289};
290
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600291VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600292 const char *pLayerName,
293 uint32_t *pCount,
294 VkExtensionProperties* pProperties)
Chris Forbesaab9d112015-04-02 13:22:31 +1300295{
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600296 /* shader checker does not have any global extensions */
297 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbesaab9d112015-04-02 13:22:31 +1300298}
299
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600300VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600301 uint32_t *pCount,
302 VkLayerProperties* pProperties)
Tony Barbour426b9052015-06-24 16:06:58 -0600303{
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600304 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
305 shader_checker_global_layers,
306 pCount, pProperties);
Tony Barbour426b9052015-06-24 16:06:58 -0600307}
308
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600309VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600310 VkPhysicalDevice physicalDevice,
311 const char* pLayerName,
312 uint32_t* pCount,
313 VkExtensionProperties* pProperties)
Jon Ashburnade3bee2015-06-10 16:43:31 -0600314{
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600315 /* Shader checker does not have any physical device extensions */
316 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
317}
Jon Ashburnade3bee2015-06-10 16:43:31 -0600318
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -0600319VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchter7abf8e52015-07-07 10:05:05 -0600320 VkPhysicalDevice physicalDevice,
321 uint32_t* pCount,
322 VkLayerProperties* pProperties)
323{
324 /* Shader checker physical device layers are the same as global */
325 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
326 shader_checker_global_layers,
327 pCount, pProperties);
Jon Ashburnade3bee2015-06-10 16:43:31 -0600328}
Chris Forbesaab9d112015-04-02 13:22:31 +1300329
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200330static char const *
331storage_class_name(unsigned sc)
332{
333 switch (sc) {
Cody Northrop812b4612015-04-20 14:09:40 -0600334 case spv::StorageClassInput: return "input";
335 case spv::StorageClassOutput: return "output";
336 case spv::StorageClassUniformConstant: return "const uniform";
337 case spv::StorageClassUniform: return "uniform";
338 case spv::StorageClassWorkgroupLocal: return "workgroup local";
339 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
340 case spv::StorageClassPrivateGlobal: return "private global";
341 case spv::StorageClassFunction: return "function";
342 case spv::StorageClassGeneric: return "generic";
Cody Northrop812b4612015-04-20 14:09:40 -0600343 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF3aaa0882015-07-21 17:22:50 -0600344 case spv::StorageClassImage: return "image";
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200345 default: return "unknown";
346 }
347}
348
349
350/* returns ptr to null terminator */
351static char *
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600352describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200353{
354 auto type_def_it = src->type_def_index.find(type);
355
356 if (type_def_it == src->type_def_index.end()) {
357 return dst + sprintf(dst, "undef");
358 }
359
360 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
361 unsigned opcode = code[0] & 0x0ffffu;
362 switch (opcode) {
363 case spv::OpTypeBool:
364 return dst + sprintf(dst, "bool");
365 case spv::OpTypeInt:
366 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
367 case spv::OpTypeFloat:
368 return dst + sprintf(dst, "float%d", code[2]);
369 case spv::OpTypeVector:
370 dst += sprintf(dst, "vec%d of ", code[3]);
371 return describe_type(dst, src, code[2]);
372 case spv::OpTypeMatrix:
373 dst += sprintf(dst, "mat%d of ", code[3]);
374 return describe_type(dst, src, code[2]);
375 case spv::OpTypeArray:
376 dst += sprintf(dst, "arr[%d] of ", code[3]);
377 return describe_type(dst, src, code[2]);
378 case spv::OpTypePointer:
379 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
380 return describe_type(dst, src, code[3]);
381 case spv::OpTypeStruct:
382 {
383 unsigned oplen = code[0] >> 16;
384 dst += sprintf(dst, "struct of (");
Ian Elliottf21f14b2015-04-17 11:05:04 -0600385 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200386 dst = describe_type(dst, src, code[i]);
387 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
388 }
389 return dst;
390 }
391 default:
392 return dst + sprintf(dst, "oddtype");
393 }
394}
395
396
397static bool
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600398types_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 +1200399{
400 auto a_type_def_it = a->type_def_index.find(a_type);
401 auto b_type_def_it = b->type_def_index.find(b_type);
402
403 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200404 return false;
405 }
406
407 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200408 return false;
409 }
410
411 /* walk two type trees together, and complain about differences */
412 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
413 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
414
415 unsigned a_opcode = a_code[0] & 0x0ffffu;
416 unsigned b_opcode = b_code[0] & 0x0ffffu;
417
Chris Forbes0a94a372015-06-05 14:57:05 +1200418 if (b_arrayed && b_opcode == spv::OpTypeArray) {
419 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
420 return types_match(a, b, a_type, b_code[2], false);
421 }
422
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200423 if (a_opcode != b_opcode) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200424 return false;
425 }
426
427 switch (a_opcode) {
Chris Forbes0a94a372015-06-05 14:57:05 +1200428 /* 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 +1200429 case spv::OpTypeBool:
Chris Forbes0a94a372015-06-05 14:57:05 +1200430 return true && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200431 case spv::OpTypeInt:
432 /* match on width, signedness */
Chris Forbes0a94a372015-06-05 14:57:05 +1200433 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200434 case spv::OpTypeFloat:
435 /* match on width */
Chris Forbes0a94a372015-06-05 14:57:05 +1200436 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200437 case spv::OpTypeVector:
438 case spv::OpTypeMatrix:
439 case spv::OpTypeArray:
Chris Forbes0a94a372015-06-05 14:57:05 +1200440 /* match on element type, count. these all have the same layout. we don't get here if
441 * b_arrayed -- that is handled above. */
442 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 +1200443 case spv::OpTypeStruct:
444 /* match on all element types */
445 {
Chris Forbes0a94a372015-06-05 14:57:05 +1200446 if (b_arrayed) {
447 /* for the purposes of matching different levels of arrayness, structs are leaves. */
448 return false;
449 }
450
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200451 unsigned a_len = a_code[0] >> 16;
452 unsigned b_len = b_code[0] >> 16;
453
454 if (a_len != b_len) {
455 return false; /* structs cannot match if member counts differ */
456 }
457
Ian Elliottf21f14b2015-04-17 11:05:04 -0600458 for (unsigned i = 2; i < a_len; i++) {
Chris Forbes0a94a372015-06-05 14:57:05 +1200459 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200460 return false;
461 }
462 }
463
464 return true;
465 }
466 case spv::OpTypePointer:
467 /* match on pointee type. storage class is expected to differ */
Chris Forbes0a94a372015-06-05 14:57:05 +1200468 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200469
470 default:
471 /* remaining types are CLisms, or may not appear in the interfaces we
472 * are interested in. Just claim no match.
473 */
474 return false;
475
476 }
477}
478
479
Chris Forbes67cc36f2015-04-13 12:14:52 +1200480static int
481value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
482{
483 auto it = map.find(id);
484 if (it == map.end())
485 return def;
486 else
487 return it->second;
488}
489
490
491struct interface_var {
492 uint32_t id;
493 uint32_t type_id;
494 /* TODO: collect the name, too? Isn't required to be present. */
495};
496
497
498static void
Chris Forbese20111c2015-07-03 13:50:24 +1200499collect_interface_by_location(VkDevice dev,
500 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes67cc36f2015-04-13 12:14:52 +1200501 std::map<uint32_t, interface_var> &out,
502 std::map<uint32_t, interface_var> &builtins_out)
503{
504 unsigned int const *code = (unsigned int const *)&src->words[0];
505 size_t size = src->words.size();
506
Chris Forbes67cc36f2015-04-13 12:14:52 +1200507 std::unordered_map<unsigned, unsigned> var_locations;
508 std::unordered_map<unsigned, unsigned> var_builtins;
509
510 unsigned word = 5;
511 while (word < size) {
512
513 unsigned opcode = code[word] & 0x0ffffu;
514 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
515
516 /* We consider two interface models: SSO rendezvous-by-location, and
517 * builtins. Complain about anything that fits neither model.
518 */
519 if (opcode == spv::OpDecorate) {
Cody Northrop812b4612015-04-20 14:09:40 -0600520 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200521 var_locations[code[word+1]] = code[word+3];
522 }
523
Cody Northrop812b4612015-04-20 14:09:40 -0600524 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200525 var_builtins[code[word+1]] = code[word+3];
526 }
527 }
528
529 /* TODO: handle grouped decorations */
530 /* TODO: handle index=1 dual source outputs from FS -- two vars will
531 * have the same location, and we DONT want to clobber. */
532
Ian Elliottf21f14b2015-04-17 11:05:04 -0600533 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200534 int location = value_or_default(var_locations, code[word+2], -1);
535 int builtin = value_or_default(var_builtins, code[word+2], -1);
536
537 if (location == -1 && builtin == -1) {
538 /* No location defined, and not bound to an API builtin.
539 * The spec says nothing about how this case works (or doesn't)
540 * for interface matching.
541 */
Chris Forbes2b7c0002015-07-10 09:06:54 +1200542 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 +1200543 "var %d (type %d) in %s interface has no Location or Builtin decoration",
544 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes67cc36f2015-04-13 12:14:52 +1200545 }
546 else if (location != -1) {
547 /* A user-defined interface variable, with a location. */
548 interface_var v;
549 v.id = code[word+2];
550 v.type_id = code[word+1];
551 out[location] = v;
552 }
553 else {
554 /* A builtin interface variable */
555 interface_var v;
556 v.id = code[word+2];
557 v.type_id = code[word+1];
558 builtins_out[builtin] = v;
559 }
560 }
561
562 word += oplen;
563 }
564}
565
566
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600567VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
568 VkDevice device,
569 const VkShaderModuleCreateInfo *pCreateInfo,
570 VkShaderModule *pShaderModule)
Chris Forbesaab9d112015-04-02 13:22:31 +1300571{
Chris Forbese20111c2015-07-03 13:50:24 +1200572 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShaderModule(device, pCreateInfo, pShaderModule);
Chris Forbes4396ff52015-04-08 10:11:59 +1200573
Courtney Goeltzenleuchter0b590602015-09-16 12:57:55 -0600574 if (res == VK_SUCCESS) {
575 loader_platform_thread_lock_mutex(&globalLock);
576 shader_module_map[pShaderModule->handle] = new shader_module(device, pCreateInfo);
577 loader_platform_thread_unlock_mutex(&globalLock);
578 }
Chris Forbesaab9d112015-04-02 13:22:31 +1300579 return res;
580}
581
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600582VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
583 VkDevice device,
584 const VkShaderCreateInfo *pCreateInfo,
585 VkShader *pShader)
586{
Chris Forbese20111c2015-07-03 13:50:24 +1200587 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateShader(device, pCreateInfo, pShader);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600588
Courtney Goeltzenleuchter0b590602015-09-16 12:57:55 -0600589 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200590 shader_object_map[pShader->handle] = new shader_object(pCreateInfo);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600591 loader_platform_thread_unlock_mutex(&globalLock);
592 return res;
593}
Chris Forbesaab9d112015-04-02 13:22:31 +1300594
Chia-I Wuc278df82015-07-07 11:50:03 +0800595VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
596 VkDevice device,
597 const VkRenderPassCreateInfo *pCreateInfo,
598 VkRenderPass *pRenderPass)
599{
Chia-I Wuc278df82015-07-07 11:50:03 +0800600 VkResult res = get_dispatch_table(shader_checker_device_table_map, device)->CreateRenderPass(device, pCreateInfo, pRenderPass);
601
Courtney Goeltzenleuchter0b590602015-09-16 12:57:55 -0600602 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200603 render_pass_map[pRenderPass->handle] = new render_pass(pCreateInfo);
Chia-I Wuc278df82015-07-07 11:50:03 +0800604 loader_platform_thread_unlock_mutex(&globalLock);
605 return res;
606}
607
Chris Forbes5f362d02015-05-25 11:13:22 +1200608static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200609validate_interface_between_stages(VkDevice dev,
610 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600611 shader_module const *consumer, char const *consumer_name,
Chris Forbes4453c772015-06-05 15:01:08 +1200612 bool consumer_arrayed_input)
Chris Forbesbb164b62015-04-08 10:19:16 +1200613{
614 std::map<uint32_t, interface_var> outputs;
615 std::map<uint32_t, interface_var> inputs;
616
617 std::map<uint32_t, interface_var> builtin_outputs;
618 std::map<uint32_t, interface_var> builtin_inputs;
619
Chris Forbes5f362d02015-05-25 11:13:22 +1200620 bool pass = true;
Chris Forbesbb164b62015-04-08 10:19:16 +1200621
Chris Forbese20111c2015-07-03 13:50:24 +1200622 collect_interface_by_location(dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
623 collect_interface_by_location(dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesbb164b62015-04-08 10:19:16 +1200624
625 auto a_it = outputs.begin();
626 auto b_it = inputs.begin();
627
628 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedof5997ab2015-04-27 16:36:17 -0600629 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
630 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
631 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200632 auto a_first = a_at_end ? 0 : a_it->first;
633 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600634
635 if (b_at_end || a_first < b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200636 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 +1200637 "%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 +1200638 a_it++;
639 }
David Pinedof5997ab2015-04-27 16:36:17 -0600640 else if (a_at_end || a_first > b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200641 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 +1200642 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name);
Chris Forbes5f362d02015-05-25 11:13:22 +1200643 pass = false;
Chris Forbesbb164b62015-04-08 10:19:16 +1200644 b_it++;
645 }
646 else {
Chris Forbes4453c772015-06-05 15:01:08 +1200647 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 +1200648 /* OK! */
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200649 }
650 else {
651 char producer_type[1024];
652 char consumer_type[1024];
653 describe_type(producer_type, producer, a_it->second.type_id);
654 describe_type(consumer_type, consumer, b_it->second.type_id);
655
Chris Forbes2b7c0002015-07-10 09:06:54 +1200656 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 +1200657 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200658 pass = false;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200659 }
Chris Forbesbb164b62015-04-08 10:19:16 +1200660 a_it++;
661 b_it++;
662 }
663 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200664
665 return pass;
Chris Forbesbb164b62015-04-08 10:19:16 +1200666}
667
668
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200669enum FORMAT_TYPE {
670 FORMAT_TYPE_UNDEFINED,
671 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
672 FORMAT_TYPE_SINT,
673 FORMAT_TYPE_UINT,
674};
675
676
677static unsigned
678get_format_type(VkFormat fmt) {
679 switch (fmt) {
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800680 case VK_FORMAT_UNDEFINED:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200681 return FORMAT_TYPE_UNDEFINED;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800682 case VK_FORMAT_R8_SINT:
683 case VK_FORMAT_R8G8_SINT:
684 case VK_FORMAT_R8G8B8_SINT:
685 case VK_FORMAT_R8G8B8A8_SINT:
686 case VK_FORMAT_R16_SINT:
687 case VK_FORMAT_R16G16_SINT:
688 case VK_FORMAT_R16G16B16_SINT:
689 case VK_FORMAT_R16G16B16A16_SINT:
690 case VK_FORMAT_R32_SINT:
691 case VK_FORMAT_R32G32_SINT:
692 case VK_FORMAT_R32G32B32_SINT:
693 case VK_FORMAT_R32G32B32A32_SINT:
694 case VK_FORMAT_B8G8R8_SINT:
695 case VK_FORMAT_B8G8R8A8_SINT:
696 case VK_FORMAT_R10G10B10A2_SINT:
697 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200698 return FORMAT_TYPE_SINT;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800699 case VK_FORMAT_R8_UINT:
700 case VK_FORMAT_R8G8_UINT:
701 case VK_FORMAT_R8G8B8_UINT:
702 case VK_FORMAT_R8G8B8A8_UINT:
703 case VK_FORMAT_R16_UINT:
704 case VK_FORMAT_R16G16_UINT:
705 case VK_FORMAT_R16G16B16_UINT:
706 case VK_FORMAT_R16G16B16A16_UINT:
707 case VK_FORMAT_R32_UINT:
708 case VK_FORMAT_R32G32_UINT:
709 case VK_FORMAT_R32G32B32_UINT:
710 case VK_FORMAT_R32G32B32A32_UINT:
711 case VK_FORMAT_B8G8R8_UINT:
712 case VK_FORMAT_B8G8R8A8_UINT:
713 case VK_FORMAT_R10G10B10A2_UINT:
714 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200715 return FORMAT_TYPE_UINT;
716 default:
717 return FORMAT_TYPE_FLOAT;
718 }
719}
720
721
Chris Forbes28c50882015-05-04 14:04:06 +1200722/* characterizes a SPIR-V type appearing in an interface to a FF stage,
723 * for comparison to a VkFormat's characterization above. */
724static unsigned
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600725get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes28c50882015-05-04 14:04:06 +1200726{
727 auto type_def_it = src->type_def_index.find(type);
728
729 if (type_def_it == src->type_def_index.end()) {
730 return FORMAT_TYPE_UNDEFINED;
731 }
732
733 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
734 unsigned opcode = code[0] & 0x0ffffu;
735 switch (opcode) {
736 case spv::OpTypeInt:
737 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
738 case spv::OpTypeFloat:
739 return FORMAT_TYPE_FLOAT;
740 case spv::OpTypeVector:
741 return get_fundamental_type(src, code[2]);
742 case spv::OpTypeMatrix:
743 return get_fundamental_type(src, code[2]);
744 case spv::OpTypeArray:
745 return get_fundamental_type(src, code[2]);
746 case spv::OpTypePointer:
747 return get_fundamental_type(src, code[3]);
748 default:
749 return FORMAT_TYPE_UNDEFINED;
750 }
751}
752
753
Chris Forbes5f362d02015-05-25 11:13:22 +1200754static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200755validate_vi_consistency(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200756{
757 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
758 * each binding should be specified only once.
759 */
760 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200761 bool pass = true;
762
763 for (unsigned i = 0; i < vi->bindingCount; i++) {
764 auto desc = &vi->pVertexBindingDescriptions[i];
765 auto & binding = bindings[desc->binding];
766 if (binding) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200767 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 +1200768 "Duplicate vertex input binding descriptions for binding %d", desc->binding);
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200769 pass = false;
770 }
771 else {
772 binding = desc;
773 }
774 }
775
776 return pass;
777}
778
779
780static bool
Chris Forbese20111c2015-07-03 13:50:24 +1200781validate_vi_against_vs_inputs(VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbesfcd05f12015-04-08 10:36:37 +1200782{
783 std::map<uint32_t, interface_var> inputs;
784 /* we collect builtin inputs, but they will never appear in the VI state --
785 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
786 */
787 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbes5f362d02015-05-25 11:13:22 +1200788 bool pass = true;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200789
Chris Forbese20111c2015-07-03 13:50:24 +1200790 collect_interface_by_location(dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200791
792 /* Build index by location */
793 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes6f2ab982015-05-25 11:13:24 +1200794 if (vi) {
795 for (unsigned i = 0; i < vi->attributeCount; i++)
796 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
797 }
Chris Forbesfcd05f12015-04-08 10:36:37 +1200798
799 auto it_a = attribs.begin();
800 auto it_b = inputs.begin();
801
David Pinedof5997ab2015-04-27 16:36:17 -0600802 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
803 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
804 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200805 auto a_first = a_at_end ? 0 : it_a->first;
806 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600807 if (b_at_end || a_first < b_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200808 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 +1200809 "Vertex attribute at location %d not consumed by VS", a_first);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200810 it_a++;
811 }
David Pinedof5997ab2015-04-27 16:36:17 -0600812 else if (a_at_end || b_first < a_first) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200813 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 +1200814 "VS consumes input at location %d but not provided", b_first);
Chris Forbes5f362d02015-05-25 11:13:22 +1200815 pass = false;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200816 it_b++;
817 }
818 else {
Chris Forbes3317b382015-05-04 14:04:24 +1200819 unsigned attrib_type = get_format_type(it_a->second->format);
820 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
821
822 /* type checking */
823 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
824 char vs_type[1024];
825 describe_type(vs_type, vs, it_b->second.type_id);
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 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Chris Forbes3317b382015-05-04 14:04:24 +1200828 string_VkFormat(it_a->second->format), a_first, vs_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200829 pass = false;
Chris Forbes3317b382015-05-04 14:04:24 +1200830 }
831
Chris Forbes5c75afe2015-04-17 10:13:28 +1200832 /* OK! */
Chris Forbesfcd05f12015-04-08 10:36:37 +1200833 it_a++;
834 it_b++;
835 }
836 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200837
838 return pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200839}
840
841
Chris Forbes5f362d02015-05-25 11:13:22 +1200842static bool
Chia-I Wuc278df82015-07-07 11:50:03 +0800843validate_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 +1200844{
Chia-I Wuc278df82015-07-07 11:50:03 +0800845 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200846 std::map<uint32_t, interface_var> outputs;
847 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbes5f362d02015-05-25 11:13:22 +1200848 bool pass = true;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200849
850 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
851
Chris Forbese20111c2015-07-03 13:50:24 +1200852 collect_interface_by_location(dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200853
854 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
855 * and all color attachment should be UNORM/SNORM/FLOAT.
856 */
857 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200858 if (outputs.size()) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200859 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 +1200860 "Should not have user-defined FS outputs when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200861 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200862 }
863
Chia-I Wuc278df82015-07-07 11:50:03 +0800864 for (unsigned i = 0; i < color_formats.size(); i++) {
865 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200866 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200867 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 +1200868 "CB format should not be SINT or UINT when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200869 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200870 }
871 }
872
Chris Forbes5f362d02015-05-25 11:13:22 +1200873 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200874 }
875
876 auto it = outputs.begin();
877 uint32_t attachment = 0;
878
879 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
880 * are currently dense, but the parallel with matching between shader stages is nice.
881 */
882
Chris Forbes2b7c0002015-07-10 09:06:54 +1200883 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbes768eead2015-07-11 11:05:01 +1200884 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
885 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200886 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 +1200887 "FS writes to output location %d with no matching attachment", it->first);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200888 it++;
889 }
890 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200891 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 +1200892 "Attachment %d not written by FS", attachment);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200893 attachment++;
Chris Forbes5f362d02015-05-25 11:13:22 +1200894 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200895 }
896 else {
Chris Forbes4b009002015-05-04 14:20:10 +1200897 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wuc278df82015-07-07 11:50:03 +0800898 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes4b009002015-05-04 14:20:10 +1200899
900 /* type checking */
901 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
902 char fs_type[1024];
903 describe_type(fs_type, fs, it->second.type_id);
Chris Forbes2b7c0002015-07-10 09:06:54 +1200904 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 +1200905 "Attachment %d of type `%s` does not match FS output type of `%s`",
Chia-I Wuc278df82015-07-07 11:50:03 +0800906 attachment, string_VkFormat(color_formats[attachment]), fs_type);
Chris Forbes5f362d02015-05-25 11:13:22 +1200907 pass = false;
Chris Forbes4b009002015-05-04 14:20:10 +1200908 }
909
Chris Forbes5c75afe2015-04-17 10:13:28 +1200910 /* OK! */
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200911 it++;
912 attachment++;
913 }
914 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200915
916 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200917}
918
919
Chris Forbes4453c772015-06-05 15:01:08 +1200920struct shader_stage_attributes {
921 char const * const name;
922 bool arrayed_input;
923};
924
925
926static shader_stage_attributes
927shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
928 { "vertex shader", false },
929 { "tessellation control shader", true },
930 { "tessellation evaluation shader", false },
931 { "geometry shader", true },
932 { "fragment shader", false },
933};
934
935
Chris Forbesf1060ca2015-06-04 20:23:00 +1200936static bool
Chris Forbesd8bde292015-07-24 13:53:47 +1200937validate_graphics_pipeline(VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes60540932015-04-08 10:15:35 +1200938{
Chris Forbes8f600932015-04-08 10:16:45 +1200939 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
940 * before trying to do anything more: */
941
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600942 shader_module const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
Chris Forbes4453c772015-06-05 15:01:08 +1200943 memset(shaders, 0, sizeof(shaders));
Chia-I Wuc278df82015-07-07 11:50:03 +0800944 render_pass const *rp = 0;
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600945 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbes5f362d02015-05-25 11:13:22 +1200946 bool pass = true;
Chris Forbes8f600932015-04-08 10:16:45 +1200947
Chris Forbes1ed0f982015-05-29 14:55:18 +1200948 loader_platform_thread_lock_mutex(&globalLock);
949
Tony Barbourb2cc40a2015-07-13 15:28:47 -0600950 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600951 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
952 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbes8f600932015-04-08 10:16:45 +1200953
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600954 if (pStage->stage < VK_SHADER_STAGE_VERTEX || pStage->stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200955 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 +1200956 "Unknown shader stage %d", pStage->stage);
Chris Forbes5c75afe2015-04-17 10:13:28 +1200957 }
Chris Forbes4453c772015-06-05 15:01:08 +1200958 else {
Chris Forbes2b7c0002015-07-10 09:06:54 +1200959 struct shader_object *shader = shader_object_map[pStage->shader.handle];
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -0600960 shaders[pStage->stage] = shader->module;
Chris Forbes4453c772015-06-05 15:01:08 +1200961 }
Chris Forbes8f600932015-04-08 10:16:45 +1200962 }
Chris Forbes8f600932015-04-08 10:16:45 +1200963 }
964
Chia-I Wuc278df82015-07-07 11:50:03 +0800965 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Chris Forbes2b7c0002015-07-10 09:06:54 +1200966 rp = render_pass_map[pCreateInfo->renderPass.handle];
Chia-I Wuc278df82015-07-07 11:50:03 +0800967
Mark Lobodzinski0e0fb5c2015-06-23 15:11:57 -0600968 vi = pCreateInfo->pVertexInputState;
969
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200970 if (vi) {
Chris Forbese20111c2015-07-03 13:50:24 +1200971 pass = validate_vi_consistency(dev, vi) && pass;
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200972 }
973
Chris Forbes4453c772015-06-05 15:01:08 +1200974 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
Chris Forbese20111c2015-07-03 13:50:24 +1200975 pass = validate_vi_against_vs_inputs(dev, vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200976 }
977
Chris Forbes4453c772015-06-05 15:01:08 +1200978 /* TODO: enforce rules about present combinations of shaders */
979 int producer = VK_SHADER_STAGE_VERTEX;
980 int consumer = VK_SHADER_STAGE_GEOMETRY;
981
982 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
983 producer++;
984 consumer++;
Chris Forbesbb164b62015-04-08 10:19:16 +1200985 }
986
Tony Barbour4eb3cd12015-06-11 15:04:25 -0600987 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbes4453c772015-06-05 15:01:08 +1200988 assert(shaders[producer]);
989 if (shaders[consumer]) {
990 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
Chris Forbese20111c2015-07-03 13:50:24 +1200991 pass = validate_interface_between_stages(dev,
992 shaders[producer], shader_stage_attribs[producer].name,
Chris Forbes4453c772015-06-05 15:01:08 +1200993 shaders[consumer], shader_stage_attribs[consumer].name,
994 shader_stage_attribs[consumer].arrayed_input) && pass;
995 }
996
997 producer = consumer;
998 }
999 }
1000
Chia-I Wuc278df82015-07-07 11:50:03 +08001001 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && rp) {
1002 pass = validate_fs_outputs_against_render_pass(dev, shaders[VK_SHADER_STAGE_FRAGMENT], rp, pCreateInfo->subpass) && pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +12001003 }
1004
Chris Forbes1ed0f982015-05-29 14:55:18 +12001005 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbesf1060ca2015-06-04 20:23:00 +12001006 return pass;
1007}
1008
Jon Ashburn0d60d272015-07-09 15:02:25 -06001009//TODO handle pipelineCache entry points
Chris Forbesd0f7f7c2015-06-04 20:27:09 +12001010VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburn0d60d272015-07-09 15:02:25 -06001011vkCreateGraphicsPipelines(VkDevice device,
1012 VkPipelineCache pipelineCache,
1013 uint32_t count,
1014 const VkGraphicsPipelineCreateInfo *pCreateInfos,
1015 VkPipeline *pPipelines)
Chris Forbesf1060ca2015-06-04 20:23:00 +12001016{
Chris Forbesd8bde292015-07-24 13:53:47 +12001017 bool pass = true;
1018 for (uint32_t i = 0; i < count; i++) {
1019 pass = validate_graphics_pipeline(device, &pCreateInfos[i]) && pass;
1020 }
Chris Forbes5f362d02015-05-25 11:13:22 +12001021
1022 if (pass) {
1023 /* The driver is allowed to crash if passed junk. Only actually create the
1024 * pipeline if we didn't run into any showstoppers above.
1025 */
Jon Ashburn0d60d272015-07-09 15:02:25 -06001026 return get_dispatch_table(shader_checker_device_table_map, device)->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines);
Chris Forbes5f362d02015-05-25 11:13:22 +12001027 }
1028 else {
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -06001029 return VK_ERROR_VALIDATION_FAILED;
Chris Forbes5f362d02015-05-25 11:13:22 +12001030 }
Chris Forbes60540932015-04-08 10:15:35 +12001031}
1032
1033
Chris Forbese20111c2015-07-03 13:50:24 +12001034VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
1035{
1036 VkLayerDispatchTable *pDeviceTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
1037 VkResult result = pDeviceTable->CreateDevice(gpu, pCreateInfo, pDevice);
1038 if (result == VK_SUCCESS) {
1039 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
1040 VkLayerDispatchTable *pTable = get_dispatch_table(shader_checker_device_table_map, *pDevice);
1041 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
1042 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1043 }
1044 return result;
1045}
Chris Forbesd0f7f7c2015-06-04 20:27:09 +12001046
Jon Ashburn17f37372015-05-19 16:34:53 -06001047/* hook DextroyDevice to remove tableMap entry */
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001048VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device)
Jon Ashburn17f37372015-05-19 16:34:53 -06001049{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001050 dispatch_key key = get_dispatch_key(device);
Chris Forbese20111c2015-07-03 13:50:24 +12001051 VkLayerDispatchTable *pDisp = get_dispatch_table(shader_checker_device_table_map, device);
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001052 pDisp->DestroyDevice(device);
Chris Forbese20111c2015-07-03 13:50:24 +12001053 shader_checker_device_table_map.erase(key);
Jon Ashburn17f37372015-05-19 16:34:53 -06001054}
1055
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001056VkResult VKAPI vkCreateInstance(
1057 const VkInstanceCreateInfo* pCreateInfo,
1058 VkInstance* pInstance)
1059{
Chris Forbese20111c2015-07-03 13:50:24 +12001060 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map,*pInstance);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001061 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
1062
1063 if (result == VK_SUCCESS) {
Chris Forbese20111c2015-07-03 13:50:24 +12001064 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1065 my_data->report_data = debug_report_create_instance(
1066 pTable,
1067 *pInstance,
1068 pCreateInfo->extensionCount,
1069 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterf4a2eba2015-06-08 14:58:39 -06001070
Chris Forbese20111c2015-07-03 13:50:24 +12001071 init_shader_checker(my_data);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001072 }
1073 return result;
1074}
1075
Jon Ashburn17f37372015-05-19 16:34:53 -06001076/* hook DestroyInstance to remove tableInstanceMap entry */
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001077VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance)
Jon Ashburn17f37372015-05-19 16:34:53 -06001078{
Courtney Goeltzenleuchter9f171942015-06-13 21:22:12 -06001079 dispatch_key key = get_dispatch_key(instance);
Chris Forbese20111c2015-07-03 13:50:24 +12001080 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
Mark Lobodzinski67b42b72015-09-07 13:59:43 -06001081 pTable->DestroyInstance(instance);
Chris Forbese20111c2015-07-03 13:50:24 +12001082
1083 // Clean up logging callback, if any
1084 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
1085 if (my_data->logging_callback) {
1086 layer_destroy_msg_callback(my_data->report_data, my_data->logging_callback);
1087 }
1088
1089 layer_debug_report_destroy_instance(my_data->report_data);
1090 layer_data_map.erase(pTable);
1091
1092 shader_checker_instance_table_map.erase(key);
Jon Ashburn17f37372015-05-19 16:34:53 -06001093}
Chris Forbesb65ba352015-05-25 11:12:59 +12001094
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001095VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbese20111c2015-07-03 13:50:24 +12001096 VkInstance instance,
1097 VkFlags msgFlags,
1098 const PFN_vkDbgMsgCallback pfnMsgCallback,
1099 void* pUserData,
1100 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001101{
Chris Forbese20111c2015-07-03 13:50:24 +12001102 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1103 VkResult res = pTable->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1104 if (VK_SUCCESS == res) {
1105 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1106 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1107 }
1108 return res;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001109}
1110
1111VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbese20111c2015-07-03 13:50:24 +12001112 VkInstance instance,
1113 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001114{
Chris Forbese20111c2015-07-03 13:50:24 +12001115 VkLayerInstanceDispatchTable *pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1116 VkResult res = pTable->DbgDestroyMsgCallback(instance, msgCallback);
1117 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1118 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1119 return res;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001120}
1121
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001122VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbesaab9d112015-04-02 13:22:31 +13001123{
Chris Forbese20111c2015-07-03 13:50:24 +12001124 if (dev == NULL)
Chris Forbesaab9d112015-04-02 13:22:31 +13001125 return NULL;
1126
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001127 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbese20111c2015-07-03 13:50:24 +12001128 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
1129 initDeviceTable(shader_checker_device_table_map, (const VkBaseLayerObject *) dev);
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001130 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001131 }
1132
Chris Forbesaab9d112015-04-02 13:22:31 +13001133#define ADD_HOOK(fn) \
Chris Forbese20111c2015-07-03 13:50:24 +12001134 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001135 return (PFN_vkVoidFunction) fn
Chris Forbesaab9d112015-04-02 13:22:31 +13001136
Chris Forbese20111c2015-07-03 13:50:24 +12001137 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchter2d034fd2015-06-28 13:01:17 -06001138 ADD_HOOK(vkCreateShaderModule);
Chris Forbesaab9d112015-04-02 13:22:31 +13001139 ADD_HOOK(vkCreateShader);
Chia-I Wuc278df82015-07-07 11:50:03 +08001140 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn17f37372015-05-19 16:34:53 -06001141 ADD_HOOK(vkDestroyDevice);
Jon Ashburn0d60d272015-07-09 15:02:25 -06001142 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbese0e94992015-08-12 15:03:22 +12001143 ADD_HOOK(vkCreateDescriptorSetLayout);
1144 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburn8198fd02015-05-18 09:08:41 -06001145#undef ADD_HOOK
Chris Forbese20111c2015-07-03 13:50:24 +12001146
1147 VkLayerDispatchTable* pTable = get_dispatch_table(shader_checker_device_table_map, dev);
1148 {
1149 if (pTable->GetDeviceProcAddr == NULL)
1150 return NULL;
1151 return pTable->GetDeviceProcAddr(dev, funcName);
1152 }
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001153}
1154
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001155VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001156{
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001157 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001158
Chris Forbese20111c2015-07-03 13:50:24 +12001159 if (instance == NULL)
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001160 return NULL;
1161
Chris Forbese20111c2015-07-03 13:50:24 +12001162 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
1163 initInstanceTable(shader_checker_instance_table_map, (const VkBaseLayerObject *) instance);
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001164 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001165 }
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001166#define ADD_HOOK(fn) \
Chris Forbese20111c2015-07-03 13:50:24 +12001167 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchtera4c8c712015-07-12 14:35:22 -06001168 return (PFN_vkVoidFunction) fn
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001169
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001170 ADD_HOOK(vkCreateInstance);
Jon Ashburn17f37372015-05-19 16:34:53 -06001171 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter74c4ce92015-09-14 17:22:16 -06001172 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1173 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1174 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1175 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburn8198fd02015-05-18 09:08:41 -06001176#undef ADD_HOOK
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001177
Chris Forbese20111c2015-07-03 13:50:24 +12001178
1179 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1180 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -06001181 if (fptr)
1182 return fptr;
1183
Chris Forbese20111c2015-07-03 13:50:24 +12001184 {
1185 VkLayerInstanceDispatchTable* pTable = get_dispatch_table(shader_checker_instance_table_map, instance);
1186 if (pTable->GetInstanceProcAddr == NULL)
1187 return NULL;
1188 return pTable->GetInstanceProcAddr(instance, funcName);
1189 }
Chris Forbesaab9d112015-04-02 13:22:31 +13001190}