blob: d7a9aa9144b4f9b75d097deba6dee5ca11c14528 [file] [log] [blame]
Chris Forbes2778f302015-04-02 13:22:31 +13001/*
2 * Vulkan
3 *
4 * Copyright (C) 2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
Chris Forbes06e8fc32015-04-13 12:14:52 +120027#include <map>
Chris Forbes2778f302015-04-02 13:22:31 +130028#include <unordered_map>
Chris Forbes41002452015-04-08 10:19:16 +120029#include <map>
Chris Forbes3b1c4212015-04-08 10:11:59 +120030#include <vector>
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -060031#include <string>
Tobin Ehlisb80b4252015-09-01 11:59:36 -060032#include <iostream>
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060033#include "vk_loader_platform.h"
Chris Forbes2778f302015-04-02 13:22:31 +130034#include "vk_dispatch_table_helper.h"
Tobin Ehlis0c6f9ee2015-07-03 09:42:57 -060035#include "vk_layer.h"
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -060036#include "vk_layer_utils.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060037#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060038#include "vk_layer_table.h"
Chris Forbes401784b2015-05-04 14:04:24 +120039#include "vk_enum_string_helper.h"
Chris Forbes6b2ead62015-04-17 10:13:28 +120040#include "shader_checker.h"
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -060041#include "vk_layer_extension_utils.h"
Chris Forbes2778f302015-04-02 13:22:31 +130042
GregF7c45bed2015-07-21 17:22:50 -060043#include "spirv/spirv.hpp"
Chris Forbes2778f302015-04-02 13:22:31 +130044
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060045// fwd decls
46struct shader_module;
47struct shader_object;
48struct render_pass;
Chris Forbes2778f302015-04-02 13:22:31 +130049
Cody Northrop55443ef2015-09-28 15:09:32 -060050struct layer_data {
Chris Forbesb1dee272015-07-03 13:50:24 +120051 debug_report_data *report_data;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -060052 std::vector<VkDbgMsgCallback> logging_callback;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060053 VkLayerDispatchTable* device_dispatch_table;
54 VkLayerInstanceDispatchTable* instance_dispatch_table;
55
56 std::unordered_map<VkShaderModule, shader_module *> shader_module_map;
57 std::unordered_map<VkShader, shader_object *> shader_object_map;
58 std::unordered_map<VkDescriptorSetLayout, std::vector<VkDescriptorSetLayoutBinding>*> descriptor_set_layout_map;
59 std::unordered_map<VkPipelineLayout, std::vector<std::vector<VkDescriptorSetLayoutBinding>*>*> pipeline_layout_map;
60 std::unordered_map<VkRenderPass, render_pass *> render_pass_map;
Cody Northrop55443ef2015-09-28 15:09:32 -060061
62 layer_data() :
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060063 report_data(nullptr),
64 device_dispatch_table(nullptr),
65 instance_dispatch_table(nullptr)
Cody Northrop55443ef2015-09-28 15:09:32 -060066 {};
67};
Chris Forbesb1dee272015-07-03 13:50:24 +120068
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060069static void
70build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index);
Chris Forbesb1dee272015-07-03 13:50:24 +120071
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060072struct shader_module {
73 /* the spirv image itself */
74 std::vector<uint32_t> words;
75 /* a mapping of <id> to the first word of its def. this is useful because walking type
76 * trees requires jumping all over the instruction stream.
77 */
78 std::unordered_map<unsigned, unsigned> type_def_index;
79
80 shader_module(VkShaderModuleCreateInfo const *pCreateInfo) :
81 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
82 type_def_index() {
83
84 build_type_def_index(words, type_def_index);
85 }
86};
87
88struct shader_object {
89 std::string name;
90 struct shader_module *module;
91 VkShaderStageFlagBits stage;
92
93 shader_object(layer_data *my_data, VkShaderCreateInfo const *pCreateInfo)
94 {
95 module = my_data->shader_module_map[pCreateInfo->module];
96 stage = pCreateInfo->stage;
97 name = pCreateInfo->pName;
98 }
99};
100
101struct render_pass {
102 std::vector<std::vector<VkFormat>> subpass_color_formats;
103
104 render_pass(VkRenderPassCreateInfo const *pCreateInfo)
105 {
106 uint32_t i;
107
108 subpass_color_formats.reserve(pCreateInfo->subpassCount);
109 for (i = 0; i < pCreateInfo->subpassCount; i++) {
110 const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i];
111 std::vector<VkFormat> color_formats;
112 uint32_t j;
113
Chia-I Wud50a7d72015-10-26 20:48:51 +0800114 color_formats.reserve(subpass->colorAttachmentCount);
115 for (j = 0; j < subpass->colorAttachmentCount; j++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600116 const uint32_t att = subpass->pColorAttachments[j].attachment;
117 const VkFormat format = pCreateInfo->pAttachments[att].format;
118
119 color_formats.push_back(pCreateInfo->pAttachments[att].format);
120 }
121
122 subpass_color_formats.push_back(color_formats);
123 }
124 }
125};
126
127static std::unordered_map<void *, layer_data *> layer_data_map;
Chris Forbesb1dee272015-07-03 13:50:24 +1200128
129template layer_data *get_my_data_ptr<layer_data>(
130 void *data_key,
131 std::unordered_map<void *, layer_data *> &data_map);
132
Chris Forbesb6b8c462015-04-15 06:59:41 +1200133static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +1200134// TODO : This can be much smarter, using separate locks for separate global data
135static int globalLockInitialized = 0;
136static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200137
Chris Forbes2b8493a2015-08-12 15:03:22 +1200138VK_LAYER_EXPORT VkResult VKAPI vkCreateDescriptorSetLayout(
139 VkDevice device,
140 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800141 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200142 VkDescriptorSetLayout* pSetLayout)
143{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600144 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200145 /* stash a copy of the layout bindings */
Chia-I Wuf7458c52015-10-26 21:10:41 +0800146 VkResult result = my_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200147
148 if (VK_SUCCESS == result) {
149 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600150 auto& bindings = my_data->descriptor_set_layout_map[*pSetLayout];
Chris Forbes2b8493a2015-08-12 15:03:22 +1200151 bindings = new std::vector<VkDescriptorSetLayoutBinding>(
Chia-I Wud50a7d72015-10-26 20:48:51 +0800152 pCreateInfo->pBindings, pCreateInfo->pBindings + pCreateInfo->bindingCount);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200153 loader_platform_thread_unlock_mutex(&globalLock);
154 }
155
156 return result;
157}
158
Chris Forbes2b8493a2015-08-12 15:03:22 +1200159VK_LAYER_EXPORT VkResult VKAPI vkCreatePipelineLayout(
160 VkDevice device,
161 const VkPipelineLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800162 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200163 VkPipelineLayout* pPipelineLayout)
164{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600165 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800166 VkResult result = my_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200167
168 if (VK_SUCCESS == result) {
169 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600170 auto& layouts = my_data->pipeline_layout_map[*pPipelineLayout];
Chris Forbes2b8493a2015-08-12 15:03:22 +1200171 layouts = new std::vector<std::vector<VkDescriptorSetLayoutBinding>*>();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800172 layouts->reserve(pCreateInfo->setLayoutCount);
173 for (unsigned i = 0; i < pCreateInfo->setLayoutCount; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600174 layouts->push_back(my_data->descriptor_set_layout_map[pCreateInfo->pSetLayouts[i]]);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200175 }
176 loader_platform_thread_unlock_mutex(&globalLock);
177 }
178
179 return result;
180}
181
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200182static void
183build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
184{
185 unsigned int const *code = (unsigned int const *)&words[0];
186 size_t size = words.size();
187
188 unsigned word = 5;
189 while (word < size) {
190 unsigned opcode = code[word] & 0x0ffffu;
191 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
192
193 switch (opcode) {
194 case spv::OpTypeVoid:
195 case spv::OpTypeBool:
196 case spv::OpTypeInt:
197 case spv::OpTypeFloat:
198 case spv::OpTypeVector:
199 case spv::OpTypeMatrix:
GregF7c45bed2015-07-21 17:22:50 -0600200 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200201 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600202 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200203 case spv::OpTypeArray:
204 case spv::OpTypeRuntimeArray:
205 case spv::OpTypeStruct:
206 case spv::OpTypeOpaque:
207 case spv::OpTypePointer:
208 case spv::OpTypeFunction:
209 case spv::OpTypeEvent:
210 case spv::OpTypeDeviceEvent:
211 case spv::OpTypeReserveId:
212 case spv::OpTypeQueue:
213 case spv::OpTypePipe:
214 type_def_index[code[word+1]] = word;
215 break;
216
217 default:
218 /* We only care about type definitions */
219 break;
220 }
221
222 word += oplen;
223 }
224}
225
Chris Forbes46794b82015-09-18 11:40:23 +1200226bool
227shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo)
228{
229 uint32_t *words = (uint32_t *)pCreateInfo->pCode;
Courtney Goeltzenleuchter8f8367e2015-10-07 08:38:30 -0600230 size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
Chris Forbes46794b82015-09-18 11:40:23 +1200231
232 /* Just validate that the header makes sense. */
233 return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
234}
235
Chris Forbesb6b8c462015-04-15 06:59:41 +1200236static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200237init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200238{
Chris Forbesb1dee272015-07-03 13:50:24 +1200239 uint32_t report_flags = 0;
240 uint32_t debug_action = 0;
241 FILE *log_output = NULL;
242 const char *option_str;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600243 VkDbgMsgCallback callback;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200244 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200245 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
246 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200247
Chris Forbesb1dee272015-07-03 13:50:24 +1200248 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200249 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200250 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600251 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600252 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &callback);
253 my_data->logging_callback.push_back(callback);
254 }
255
256 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
257 layer_create_msg_callback(my_data->report_data, report_flags, win32_debug_output_msg, NULL, &callback);
258 my_data->logging_callback.push_back(callback);
Chris Forbesb1dee272015-07-03 13:50:24 +1200259 }
260
261 if (!globalLockInitialized)
262 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200263 loader_platform_thread_create_mutex(&globalLock);
264 globalLockInitialized = 1;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200265 }
266}
267
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600268static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600269 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600270 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600271 VK_API_VERSION,
272 VK_MAKE_VERSION(0, 1, 0),
273 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600274 }
Chris Forbes2778f302015-04-02 13:22:31 +1300275};
276
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600277VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600278 const char *pLayerName,
279 uint32_t *pCount,
280 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300281{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600282 /* shader checker does not have any global extensions */
283 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300284}
285
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600286VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600287 uint32_t *pCount,
288 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600289{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600290 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
291 shader_checker_global_layers,
292 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600293}
294
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600295VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600296 VkPhysicalDevice physicalDevice,
297 const char* pLayerName,
298 uint32_t* pCount,
299 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600300{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600301 /* Shader checker does not have any physical device extensions */
302 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
303}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600304
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600305VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600306 VkPhysicalDevice physicalDevice,
307 uint32_t* pCount,
308 VkLayerProperties* pProperties)
309{
310 /* Shader checker physical device layers are the same as global */
311 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
312 shader_checker_global_layers,
313 pCount, pProperties);
Jon Ashburn207a3af2015-06-10 16:43:31 -0600314}
Chris Forbes2778f302015-04-02 13:22:31 +1300315
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200316static char const *
317storage_class_name(unsigned sc)
318{
319 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600320 case spv::StorageClassInput: return "input";
321 case spv::StorageClassOutput: return "output";
322 case spv::StorageClassUniformConstant: return "const uniform";
323 case spv::StorageClassUniform: return "uniform";
324 case spv::StorageClassWorkgroupLocal: return "workgroup local";
325 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
326 case spv::StorageClassPrivateGlobal: return "private global";
327 case spv::StorageClassFunction: return "function";
328 case spv::StorageClassGeneric: return "generic";
Cody Northrop97e52d82015-04-20 14:09:40 -0600329 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600330 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200331 default: return "unknown";
332 }
333}
334
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200335/* returns ptr to null terminator */
336static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600337describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200338{
339 auto type_def_it = src->type_def_index.find(type);
340
341 if (type_def_it == src->type_def_index.end()) {
342 return dst + sprintf(dst, "undef");
343 }
344
345 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
346 unsigned opcode = code[0] & 0x0ffffu;
347 switch (opcode) {
348 case spv::OpTypeBool:
349 return dst + sprintf(dst, "bool");
350 case spv::OpTypeInt:
351 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
352 case spv::OpTypeFloat:
353 return dst + sprintf(dst, "float%d", code[2]);
354 case spv::OpTypeVector:
355 dst += sprintf(dst, "vec%d of ", code[3]);
356 return describe_type(dst, src, code[2]);
357 case spv::OpTypeMatrix:
358 dst += sprintf(dst, "mat%d of ", code[3]);
359 return describe_type(dst, src, code[2]);
360 case spv::OpTypeArray:
361 dst += sprintf(dst, "arr[%d] of ", code[3]);
362 return describe_type(dst, src, code[2]);
363 case spv::OpTypePointer:
364 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
365 return describe_type(dst, src, code[3]);
366 case spv::OpTypeStruct:
367 {
368 unsigned oplen = code[0] >> 16;
369 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600370 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200371 dst = describe_type(dst, src, code[i]);
372 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
373 }
374 return dst;
375 }
Chris Forbes0ae15802015-08-14 12:04:39 +1200376 case spv::OpTypeSampler:
377 return dst + sprintf(dst, "sampler");
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200378 default:
379 return dst + sprintf(dst, "oddtype");
380 }
381}
382
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200383static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600384types_match(shader_module const *a, shader_module const *b, unsigned a_type, unsigned b_type, bool b_arrayed)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200385{
386 auto a_type_def_it = a->type_def_index.find(a_type);
387 auto b_type_def_it = b->type_def_index.find(b_type);
388
389 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200390 return false;
391 }
392
393 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200394 return false;
395 }
396
397 /* walk two type trees together, and complain about differences */
398 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
399 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
400
401 unsigned a_opcode = a_code[0] & 0x0ffffu;
402 unsigned b_opcode = b_code[0] & 0x0ffffu;
403
Chris Forbesf3fc0332015-06-05 14:57:05 +1200404 if (b_arrayed && b_opcode == spv::OpTypeArray) {
405 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
406 return types_match(a, b, a_type, b_code[2], false);
407 }
408
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200409 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200410 return false;
411 }
412
413 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200414 /* if b_arrayed and we hit a leaf type, then we can't match -- there's nowhere for the extra OpTypeArray to be! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200415 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200416 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200417 case spv::OpTypeInt:
418 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200419 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200420 case spv::OpTypeFloat:
421 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200422 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200423 case spv::OpTypeVector:
424 case spv::OpTypeMatrix:
425 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200426 /* match on element type, count. these all have the same layout. we don't get here if
427 * b_arrayed -- that is handled above. */
428 return !b_arrayed && types_match(a, b, a_code[2], b_code[2], b_arrayed) && a_code[3] == b_code[3];
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200429 case spv::OpTypeStruct:
430 /* match on all element types */
431 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200432 if (b_arrayed) {
433 /* for the purposes of matching different levels of arrayness, structs are leaves. */
434 return false;
435 }
436
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200437 unsigned a_len = a_code[0] >> 16;
438 unsigned b_len = b_code[0] >> 16;
439
440 if (a_len != b_len) {
441 return false; /* structs cannot match if member counts differ */
442 }
443
Ian Elliott1cb62222015-04-17 11:05:04 -0600444 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200445 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200446 return false;
447 }
448 }
449
450 return true;
451 }
452 case spv::OpTypePointer:
453 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200454 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200455
456 default:
457 /* remaining types are CLisms, or may not appear in the interfaces we
458 * are interested in. Just claim no match.
459 */
460 return false;
461
462 }
463}
464
Chris Forbes06e8fc32015-04-13 12:14:52 +1200465static int
466value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
467{
468 auto it = map.find(id);
469 if (it == map.end())
470 return def;
471 else
472 return it->second;
473}
474
Chris Forbes06e8fc32015-04-13 12:14:52 +1200475struct interface_var {
476 uint32_t id;
477 uint32_t type_id;
478 /* TODO: collect the name, too? Isn't required to be present. */
479};
480
Chris Forbes06e8fc32015-04-13 12:14:52 +1200481static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600482collect_interface_by_location(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200483 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200484 std::map<uint32_t, interface_var> &out,
485 std::map<uint32_t, interface_var> &builtins_out)
486{
487 unsigned int const *code = (unsigned int const *)&src->words[0];
488 size_t size = src->words.size();
489
Chris Forbes06e8fc32015-04-13 12:14:52 +1200490 std::unordered_map<unsigned, unsigned> var_locations;
491 std::unordered_map<unsigned, unsigned> var_builtins;
492
493 unsigned word = 5;
494 while (word < size) {
495
496 unsigned opcode = code[word] & 0x0ffffu;
497 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
498
499 /* We consider two interface models: SSO rendezvous-by-location, and
500 * builtins. Complain about anything that fits neither model.
501 */
502 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600503 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200504 var_locations[code[word+1]] = code[word+3];
505 }
506
Cody Northrop97e52d82015-04-20 14:09:40 -0600507 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200508 var_builtins[code[word+1]] = code[word+3];
509 }
510 }
511
512 /* TODO: handle grouped decorations */
513 /* TODO: handle index=1 dual source outputs from FS -- two vars will
514 * have the same location, and we DONT want to clobber. */
515
Ian Elliott1cb62222015-04-17 11:05:04 -0600516 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200517 int location = value_or_default(var_locations, code[word+2], -1);
518 int builtin = value_or_default(var_builtins, code[word+2], -1);
519
520 if (location == -1 && builtin == -1) {
521 /* No location defined, and not bound to an API builtin.
522 * The spec says nothing about how this case works (or doesn't)
523 * for interface matching.
524 */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600525 log_msg(my_data->report_data, VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200526 "var %d (type %d) in %s interface has no Location or Builtin decoration",
527 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes06e8fc32015-04-13 12:14:52 +1200528 }
529 else if (location != -1) {
530 /* A user-defined interface variable, with a location. */
531 interface_var v;
532 v.id = code[word+2];
533 v.type_id = code[word+1];
534 out[location] = v;
535 }
536 else {
537 /* A builtin interface variable */
538 interface_var v;
539 v.id = code[word+2];
540 v.type_id = code[word+1];
541 builtins_out[builtin] = v;
542 }
543 }
544
545 word += oplen;
546 }
547}
548
Chris Forbes0ae15802015-08-14 12:04:39 +1200549static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600550collect_interface_by_descriptor_slot(layer_data *my_data, VkDevice dev,
Chris Forbes0ae15802015-08-14 12:04:39 +1200551 shader_module const *src, spv::StorageClass sinterface,
552 std::map<std::pair<unsigned, unsigned>, interface_var> &out)
553{
554 unsigned int const *code = (unsigned int const *)&src->words[0];
555 size_t size = src->words.size();
556
557 std::unordered_map<unsigned, unsigned> var_sets;
558 std::unordered_map<unsigned, unsigned> var_bindings;
559
560 unsigned word = 5;
561 while (word < size) {
562
563 unsigned opcode = code[word] & 0x0ffffu;
564 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
565
566 /* We consider two interface models: SSO rendezvous-by-location, and
567 * builtins. Complain about anything that fits neither model.
568 */
569 if (opcode == spv::OpDecorate) {
570 if (code[word+2] == spv::DecorationDescriptorSet) {
571 var_sets[code[word+1]] = code[word+3];
572 }
573
574 if (code[word+2] == spv::DecorationBinding) {
575 var_bindings[code[word+1]] = code[word+3];
576 }
577 }
578
579 if (opcode == spv::OpVariable && (code[word+3] == spv::StorageClassUniform ||
580 code[word+3] == spv::StorageClassUniformConstant)) {
581 unsigned set = value_or_default(var_sets, code[word+2], 0);
582 unsigned binding = value_or_default(var_bindings, code[word+2], 0);
583
584 auto existing_it = out.find(std::make_pair(set, binding));
585 if (existing_it != out.end()) {
586 /* conflict within spv image */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600587 log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0,
Chris Forbes0ae15802015-08-14 12:04:39 +1200588 SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
589 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
590 code[word+2], code[word+1], storage_class_name(sinterface),
591 existing_it->first.first, existing_it->first.second);
592 }
593
594 interface_var v;
595 v.id = code[word+2];
596 v.type_id = code[word+1];
597 out[std::make_pair(set, binding)] = v;
598 }
599
600 word += oplen;
601 }
602}
603
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600604VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
605 VkDevice device,
606 const VkShaderModuleCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800607 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600608 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300609{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600610 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes46794b82015-09-18 11:40:23 +1200611 /* Protect the driver from non-SPIRV shaders */
612 if (!shader_is_spirv(pCreateInfo)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600613 log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE,
Chris Forbes46794b82015-09-18 11:40:23 +1200614 /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
615 "Shader is not SPIR-V");
616 return VK_ERROR_VALIDATION_FAILED;
617 }
618
Chia-I Wuf7458c52015-10-26 21:10:41 +0800619 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200620
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600621 if (res == VK_SUCCESS) {
622 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600623 my_data->shader_module_map[*pShaderModule] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600624 loader_platform_thread_unlock_mutex(&globalLock);
625 }
Chris Forbes2778f302015-04-02 13:22:31 +1300626 return res;
627}
628
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600629VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
630 VkDevice device,
631 const VkShaderCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800632 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600633 VkShader *pShader)
634{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600635 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800636 VkResult res = my_data->device_dispatch_table->CreateShader(device, pCreateInfo, pAllocator, pShader);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600637
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600638 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600639 my_data->shader_object_map[*pShader] = new shader_object(my_data, pCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600640 loader_platform_thread_unlock_mutex(&globalLock);
641 return res;
642}
Chris Forbes2778f302015-04-02 13:22:31 +1300643
Chia-I Wu08accc62015-07-07 11:50:03 +0800644VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
645 VkDevice device,
646 const VkRenderPassCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800647 const VkAllocationCallbacks* pAllocator,
Chia-I Wu08accc62015-07-07 11:50:03 +0800648 VkRenderPass *pRenderPass)
649{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600650 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800651 VkResult res = my_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800652
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600653 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600654 my_data->render_pass_map[*pRenderPass] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800655 loader_platform_thread_unlock_mutex(&globalLock);
656 return res;
657}
658
Chris Forbesee99b9b2015-05-25 11:13:22 +1200659static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600660validate_interface_between_stages(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200661 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600662 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200663 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200664{
665 std::map<uint32_t, interface_var> outputs;
666 std::map<uint32_t, interface_var> inputs;
667
668 std::map<uint32_t, interface_var> builtin_outputs;
669 std::map<uint32_t, interface_var> builtin_inputs;
670
Chris Forbesee99b9b2015-05-25 11:13:22 +1200671 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200672
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600673 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
674 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200675
676 auto a_it = outputs.begin();
677 auto b_it = inputs.begin();
678
679 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600680 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
681 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
682 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200683 auto a_first = a_at_end ? 0 : a_it->first;
684 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600685
Mike Stroyan19a6de22015-09-10 14:10:25 -0600686 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600687 if (log_msg(my_data->report_data, VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600688 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
689 pass = false;
690 }
Chris Forbes41002452015-04-08 10:19:16 +1200691 a_it++;
692 }
David Pinedod8f83d82015-04-27 16:36:17 -0600693 else if (a_at_end || a_first > b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600694 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600695 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
696 pass = false;
697 }
Chris Forbes41002452015-04-08 10:19:16 +1200698 b_it++;
699 }
700 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200701 if (types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id, consumer_arrayed_input)) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200702 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200703 }
704 else {
705 char producer_type[1024];
706 char consumer_type[1024];
707 describe_type(producer_type, producer, a_it->second.type_id);
708 describe_type(consumer_type, consumer, b_it->second.type_id);
709
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600710 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600711 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
Chris Forbesee99b9b2015-05-25 11:13:22 +1200712 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600713 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200714 }
Chris Forbes41002452015-04-08 10:19:16 +1200715 a_it++;
716 b_it++;
717 }
718 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200719
720 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200721}
722
723
Chris Forbes3616b462015-04-08 10:37:20 +1200724enum FORMAT_TYPE {
725 FORMAT_TYPE_UNDEFINED,
726 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
727 FORMAT_TYPE_SINT,
728 FORMAT_TYPE_UINT,
729};
730
731
732static unsigned
733get_format_type(VkFormat fmt) {
734 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800735 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200736 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800737 case VK_FORMAT_R8_SINT:
738 case VK_FORMAT_R8G8_SINT:
739 case VK_FORMAT_R8G8B8_SINT:
740 case VK_FORMAT_R8G8B8A8_SINT:
741 case VK_FORMAT_R16_SINT:
742 case VK_FORMAT_R16G16_SINT:
743 case VK_FORMAT_R16G16B16_SINT:
744 case VK_FORMAT_R16G16B16A16_SINT:
745 case VK_FORMAT_R32_SINT:
746 case VK_FORMAT_R32G32_SINT:
747 case VK_FORMAT_R32G32B32_SINT:
748 case VK_FORMAT_R32G32B32A32_SINT:
749 case VK_FORMAT_B8G8R8_SINT:
750 case VK_FORMAT_B8G8R8A8_SINT:
751 case VK_FORMAT_R10G10B10A2_SINT:
752 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200753 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800754 case VK_FORMAT_R8_UINT:
755 case VK_FORMAT_R8G8_UINT:
756 case VK_FORMAT_R8G8B8_UINT:
757 case VK_FORMAT_R8G8B8A8_UINT:
758 case VK_FORMAT_R16_UINT:
759 case VK_FORMAT_R16G16_UINT:
760 case VK_FORMAT_R16G16B16_UINT:
761 case VK_FORMAT_R16G16B16A16_UINT:
762 case VK_FORMAT_R32_UINT:
763 case VK_FORMAT_R32G32_UINT:
764 case VK_FORMAT_R32G32B32_UINT:
765 case VK_FORMAT_R32G32B32A32_UINT:
766 case VK_FORMAT_B8G8R8_UINT:
767 case VK_FORMAT_B8G8R8A8_UINT:
768 case VK_FORMAT_R10G10B10A2_UINT:
769 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200770 return FORMAT_TYPE_UINT;
771 default:
772 return FORMAT_TYPE_FLOAT;
773 }
774}
775
776
Chris Forbes156a1162015-05-04 14:04:06 +1200777/* characterizes a SPIR-V type appearing in an interface to a FF stage,
778 * for comparison to a VkFormat's characterization above. */
779static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600780get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200781{
782 auto type_def_it = src->type_def_index.find(type);
783
784 if (type_def_it == src->type_def_index.end()) {
785 return FORMAT_TYPE_UNDEFINED;
786 }
787
788 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
789 unsigned opcode = code[0] & 0x0ffffu;
790 switch (opcode) {
791 case spv::OpTypeInt:
792 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
793 case spv::OpTypeFloat:
794 return FORMAT_TYPE_FLOAT;
795 case spv::OpTypeVector:
796 return get_fundamental_type(src, code[2]);
797 case spv::OpTypeMatrix:
798 return get_fundamental_type(src, code[2]);
799 case spv::OpTypeArray:
800 return get_fundamental_type(src, code[2]);
801 case spv::OpTypePointer:
802 return get_fundamental_type(src, code[3]);
803 default:
804 return FORMAT_TYPE_UNDEFINED;
805 }
806}
807
808
Chris Forbesee99b9b2015-05-25 11:13:22 +1200809static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600810validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200811{
812 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
813 * each binding should be specified only once.
814 */
815 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200816 bool pass = true;
817
Chia-I Wud50a7d72015-10-26 20:48:51 +0800818 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes280ba2c2015-06-12 11:16:41 +1200819 auto desc = &vi->pVertexBindingDescriptions[i];
820 auto & binding = bindings[desc->binding];
821 if (binding) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600822 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INCONSISTENT_VI, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600823 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
824 pass = false;
825 }
Chris Forbes280ba2c2015-06-12 11:16:41 +1200826 }
827 else {
828 binding = desc;
829 }
830 }
831
832 return pass;
833}
834
835
836static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600837validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200838{
839 std::map<uint32_t, interface_var> inputs;
840 /* we collect builtin inputs, but they will never appear in the VI state --
841 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
842 */
843 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200844 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200845
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600846 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200847
848 /* Build index by location */
849 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200850 if (vi) {
Chia-I Wud50a7d72015-10-26 20:48:51 +0800851 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
Chris Forbes7191cd52015-05-25 11:13:24 +1200852 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
853 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200854
855 auto it_a = attribs.begin();
856 auto it_b = inputs.begin();
857
David Pinedod8f83d82015-04-27 16:36:17 -0600858 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
859 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
860 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200861 auto a_first = a_at_end ? 0 : it_a->first;
862 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600863 if (b_at_end || a_first < b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600864 if (log_msg(my_data->report_data, VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600865 "Vertex attribute at location %d not consumed by VS", a_first)) {
866 pass = false;
867 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200868 it_a++;
869 }
David Pinedod8f83d82015-04-27 16:36:17 -0600870 else if (a_at_end || b_first < a_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600871 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600872 "VS consumes input at location %d but not provided", b_first)) {
873 pass = false;
874 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200875 it_b++;
876 }
877 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200878 unsigned attrib_type = get_format_type(it_a->second->format);
879 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
880
881 /* type checking */
882 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
883 char vs_type[1024];
884 describe_type(vs_type, vs, it_b->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600885 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200886 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600887 string_VkFormat(it_a->second->format), a_first, vs_type)) {
888 pass = false;
889 }
Chris Forbes401784b2015-05-04 14:04:24 +1200890 }
891
Chris Forbes6b2ead62015-04-17 10:13:28 +1200892 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200893 it_a++;
894 it_b++;
895 }
896 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200897
898 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200899}
900
901
Chris Forbesee99b9b2015-05-25 11:13:22 +1200902static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600903validate_fs_outputs_against_render_pass(layer_data *my_data, VkDevice dev, shader_module const *fs, render_pass const *rp, uint32_t subpass)
Chris Forbes3616b462015-04-08 10:37:20 +1200904{
Chia-I Wu08accc62015-07-07 11:50:03 +0800905 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200906 std::map<uint32_t, interface_var> outputs;
907 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200908 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200909
910 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
911
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600912 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200913
914 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
915 * and all color attachment should be UNORM/SNORM/FLOAT.
916 */
917 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200918 if (outputs.size()) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600919 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600920 "Should not have user-defined FS outputs when using broadcast")) {
921 pass = false;
922 }
Chris Forbes3616b462015-04-08 10:37:20 +1200923 }
924
Chia-I Wu08accc62015-07-07 11:50:03 +0800925 for (unsigned i = 0; i < color_formats.size(); i++) {
926 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200927 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600928 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600929 "CB format should not be SINT or UINT when using broadcast")) {
930 pass = false;
931 }
Chris Forbes3616b462015-04-08 10:37:20 +1200932 }
933 }
934
Chris Forbesee99b9b2015-05-25 11:13:22 +1200935 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200936 }
937
938 auto it = outputs.begin();
939 uint32_t attachment = 0;
940
941 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
942 * are currently dense, but the parallel with matching between shader stages is nice.
943 */
944
Chris Forbes9715d4a2015-07-10 09:06:54 +1200945 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200946 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
947 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600948 if (log_msg(my_data->report_data, VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600949 "FS writes to output location %d with no matching attachment", it->first)) {
950 pass = false;
951 }
Chris Forbes3616b462015-04-08 10:37:20 +1200952 it++;
953 }
954 else if (it == outputs.end() || it->first > attachment) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600955 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600956 "Attachment %d not written by FS", attachment)) {
957 pass = false;
958 }
Chris Forbes3616b462015-04-08 10:37:20 +1200959 attachment++;
960 }
961 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200962 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800963 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200964
965 /* type checking */
966 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
967 char fs_type[1024];
968 describe_type(fs_type, fs, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600969 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200970 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600971 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
972 pass = false;
973 }
Chris Forbes46d31e52015-05-04 14:20:10 +1200974 }
975
Chris Forbes6b2ead62015-04-17 10:13:28 +1200976 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200977 it++;
978 attachment++;
979 }
980 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200981
982 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200983}
984
985
Chris Forbesf044ec92015-06-05 15:01:08 +1200986struct shader_stage_attributes {
987 char const * const name;
988 bool arrayed_input;
989};
990
991
992static shader_stage_attributes
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600993shader_stage_attribs[] = {
Chris Forbesf044ec92015-06-05 15:01:08 +1200994 { "vertex shader", false },
995 { "tessellation control shader", true },
996 { "tessellation evaluation shader", false },
997 { "geometry shader", true },
998 { "fragment shader", false },
999};
1000
1001
Chris Forbes556c76c2015-08-14 12:04:59 +12001002static VkDescriptorSetLayoutBinding *
1003find_descriptor_binding(std::vector<std::vector<VkDescriptorSetLayoutBinding>*>* layout,
1004 std::pair<unsigned, unsigned> slot)
1005{
1006 if (!layout)
1007 return nullptr;
1008
1009 if (slot.first >= layout->size())
1010 return nullptr;
1011
1012 auto set = (*layout)[slot.first];
1013
1014 if (slot.second >= set->size())
1015 return nullptr;
1016
1017 return &(*set)[slot.second];
1018}
1019
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001020static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1021{
1022 uint32_t bit_pos = u_ffs(stage);
1023 return bit_pos-1;
1024}
Chris Forbes556c76c2015-08-14 12:04:59 +12001025
Chris Forbes81874ba2015-06-04 20:23:00 +12001026static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001027validate_graphics_pipeline(layer_data *my_data, VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001028{
Chris Forbesf6800b52015-04-08 10:16:45 +12001029 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1030 * before trying to do anything more: */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001031 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1032 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1033 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbesf6800b52015-04-08 10:16:45 +12001034
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001035 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1036 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Chia-I Wu08accc62015-07-07 11:50:03 +08001037 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001038 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001039 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001040
Chris Forbes7f963832015-05-29 14:55:18 +12001041 loader_platform_thread_lock_mutex(&globalLock);
1042
Tony Barbour92503c62015-07-13 15:28:47 -06001043 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001044 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1045 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001046
Chia-I Wuff3780f2015-10-26 19:52:46 +08001047 // always true; pStage->stage may be revived in a later revision and
1048 // this will make sense again
1049 if ((VK_SHADER_STAGE_VERTEX_BIT & (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_FRAGMENT_BIT
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001050 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001051 if (log_msg(my_data->report_data, VK_DBG_REPORT_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC",
Chia-I Wuff3780f2015-10-26 19:52:46 +08001052 "Unknown shader stage %d", VK_SHADER_STAGE_VERTEX_BIT)) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001053 pass = false;
1054 }
Chris Forbes6b2ead62015-04-17 10:13:28 +12001055 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001056 else {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001057 struct shader_object *shader = my_data->shader_object_map[pStage->shader];
Chia-I Wuff3780f2015-10-26 19:52:46 +08001058 shaders[get_shader_stage_id(shader->stage)] = shader->module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001059
1060 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001061 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001062 collect_interface_by_descriptor_slot(my_data, dev, shader->module, spv::StorageClassUniform,
Chris Forbes46794b82015-09-18 11:40:23 +12001063 descriptor_uses);
Chris Forbes556c76c2015-08-14 12:04:59 +12001064
Chia-I Wue2fc5522015-10-26 20:04:44 +08001065 auto layout = pCreateInfo->layout != VK_NULL_HANDLE ?
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001066 my_data->pipeline_layout_map[pCreateInfo->layout] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001067
Chris Forbes46794b82015-09-18 11:40:23 +12001068 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001069
Chris Forbes46794b82015-09-18 11:40:23 +12001070 /* find the matching binding */
1071 auto binding = find_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001072
Chris Forbes46794b82015-09-18 11:40:23 +12001073 if (binding == nullptr) {
1074 char type_name[1024];
1075 describe_type(type_name, shader->module, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001076 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0,
Chris Forbes46794b82015-09-18 11:40:23 +12001077 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1078 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001079 it->first.first, it->first.second, type_name)) {
1080 pass = false;
1081 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001082 }
1083 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001084 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001085 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001086 }
1087
Chia-I Wu08accc62015-07-07 11:50:03 +08001088 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001089 rp = my_data->render_pass_map[pCreateInfo->renderPass];
Chia-I Wu08accc62015-07-07 11:50:03 +08001090
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001091 vi = pCreateInfo->pVertexInputState;
1092
Chris Forbes280ba2c2015-06-12 11:16:41 +12001093 if (vi) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001094 pass = validate_vi_consistency(my_data, dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001095 }
1096
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001097 if (shaders[vertex_stage]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001098 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001099 }
1100
Chris Forbesf044ec92015-06-05 15:01:08 +12001101 /* TODO: enforce rules about present combinations of shaders */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001102 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1103 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
Chris Forbesf044ec92015-06-05 15:01:08 +12001104
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001105 while (!shaders[producer] && producer != fragment_stage) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001106 producer++;
1107 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001108 }
1109
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001110 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001111 assert(shaders[producer]);
1112 if (shaders[consumer]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001113 pass = validate_interface_between_stages(my_data, dev,
Chris Forbes46794b82015-09-18 11:40:23 +12001114 shaders[producer], shader_stage_attribs[producer].name,
1115 shaders[consumer], shader_stage_attribs[consumer].name,
1116 shader_stage_attribs[consumer].arrayed_input) && pass;
Chris Forbesf044ec92015-06-05 15:01:08 +12001117
1118 producer = consumer;
1119 }
1120 }
1121
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001122 if (shaders[fragment_stage] && rp) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001123 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001124 }
1125
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001126 delete shaders;
1127
Chris Forbes7f963832015-05-29 14:55:18 +12001128 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001129 return pass;
1130}
1131
Jon Ashburnc669cc62015-07-09 15:02:25 -06001132//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001133VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001134vkCreateGraphicsPipelines(VkDevice device,
1135 VkPipelineCache pipelineCache,
1136 uint32_t count,
1137 const VkGraphicsPipelineCreateInfo *pCreateInfos,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001138 const VkAllocationCallbacks* pAllocator,
Jon Ashburnc669cc62015-07-09 15:02:25 -06001139 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001140{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001141 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes36a372f2015-07-24 13:53:47 +12001142 bool pass = true;
1143 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001144 pass = validate_graphics_pipeline(my_data, device, &pCreateInfos[i]) && pass;
Chris Forbes36a372f2015-07-24 13:53:47 +12001145 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001146
1147 if (pass) {
1148 /* The driver is allowed to crash if passed junk. Only actually create the
1149 * pipeline if we didn't run into any showstoppers above.
1150 */
Chia-I Wuf7458c52015-10-26 21:10:41 +08001151 return my_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001152 }
1153 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001154 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001155 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001156}
1157
1158
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001159VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Chris Forbesb1dee272015-07-03 13:50:24 +12001160{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001161 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001162 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Chris Forbesb1dee272015-07-03 13:50:24 +12001163 if (result == VK_SUCCESS) {
1164 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001165 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1166 }
1167 return result;
1168}
Chris Forbes39d8d752015-06-04 20:27:09 +12001169
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001170/* hook DextroyDevice to remove tableMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001171VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001172{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001173 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001174 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001175 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001176 delete my_device_data->device_dispatch_table;
1177 layer_data_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001178}
1179
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001180VkResult VKAPI vkCreateInstance(
1181 const VkInstanceCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001182 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001183 VkInstance* pInstance)
1184{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001185 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1186 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +08001187 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001188
1189 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001190 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1191 my_data->report_data = debug_report_create_instance(
1192 pTable,
1193 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +08001194 pCreateInfo->enabledExtensionNameCount,
Chris Forbesb1dee272015-07-03 13:50:24 +12001195 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001196
Chris Forbesb1dee272015-07-03 13:50:24 +12001197 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001198 }
1199 return result;
1200}
1201
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001202/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001203VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001204{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001205 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001206 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001207 my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
Chris Forbesb1dee272015-07-03 13:50:24 +12001208
1209 // Clean up logging callback, if any
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001210 while (my_data->logging_callback.size() > 0) {
1211 VkDbgMsgCallback callback = my_data->logging_callback.back();
1212 layer_destroy_msg_callback(my_data->report_data, callback);
1213 my_data->logging_callback.pop_back();
Chris Forbesb1dee272015-07-03 13:50:24 +12001214 }
1215
1216 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001217 delete my_data->instance_dispatch_table;
1218 layer_data_map.erase(key);
1219 if (layer_data_map.empty()) {
1220 // Release mutex when destroying last instance.
1221 loader_platform_thread_delete_mutex(&globalLock);
1222 globalLockInitialized = 0;
1223 }
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001224}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001225
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001226VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001227 VkInstance instance,
1228 VkFlags msgFlags,
1229 const PFN_vkDbgMsgCallback pfnMsgCallback,
1230 void* pUserData,
1231 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001232{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001233 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1234 VkResult res = my_data->instance_dispatch_table->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001235 if (VK_SUCCESS == res) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001236 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1237 }
1238 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001239}
1240
1241VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001242 VkInstance instance,
1243 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001244{
Chris Forbesb1dee272015-07-03 13:50:24 +12001245 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001246 VkResult res = my_data->instance_dispatch_table->DbgDestroyMsgCallback(instance, msgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001247 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1248 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001249}
1250
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001251VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001252{
Chris Forbesb1dee272015-07-03 13:50:24 +12001253 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001254 return NULL;
1255
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001256 layer_data *my_data;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001257 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001258 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001259 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
1260 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
1261 my_data->device_dispatch_table = new VkLayerDispatchTable;
1262 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001263 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001264 }
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001265 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Jon Ashburn8fd08252015-05-28 16:25:02 -06001266
Chris Forbes2778f302015-04-02 13:22:31 +13001267#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001268 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001269 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001270
Chris Forbesb1dee272015-07-03 13:50:24 +12001271 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001272 ADD_HOOK(vkCreateShaderModule);
Chris Forbes2778f302015-04-02 13:22:31 +13001273 ADD_HOOK(vkCreateShader);
Chia-I Wu08accc62015-07-07 11:50:03 +08001274 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001275 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001276 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001277 ADD_HOOK(vkCreateDescriptorSetLayout);
1278 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001279#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001280
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001281 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001282 {
1283 if (pTable->GetDeviceProcAddr == NULL)
1284 return NULL;
1285 return pTable->GetDeviceProcAddr(dev, funcName);
1286 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001287}
1288
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001289VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001290{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001291 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001292
Chris Forbesb1dee272015-07-03 13:50:24 +12001293 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001294 return NULL;
1295
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001296 layer_data *my_data;
Chris Forbesb1dee272015-07-03 13:50:24 +12001297 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001298 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
1299 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
1300 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
1301 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001302 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001303 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001304#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001305 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001306 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001307
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001308 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001309 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001310 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1311 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1312 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1313 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001314#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001315
Chris Forbesb1dee272015-07-03 13:50:24 +12001316
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001317 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001318 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001319 if (fptr)
1320 return fptr;
1321
Chris Forbesb1dee272015-07-03 13:50:24 +12001322 {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001323 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001324 if (pTable->GetInstanceProcAddr == NULL)
1325 return NULL;
1326 return pTable->GetInstanceProcAddr(instance, funcName);
1327 }
Chris Forbes2778f302015-04-02 13:22:31 +13001328}