blob: 1999aef27f5522aefe82f564438b158b21f205d1 [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 */
Jon Ashburn751c4842015-11-02 17:37:20 -0700302 if (pLayerName == NULL) {
303 dispatch_key key = get_dispatch_key(physicalDevice);
304 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
305 my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(
306 physicalDevice,
307 NULL,
308 pCount,
309 pProperties);
310 } else {
311 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
312 }
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600313}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600314
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600315VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600316 VkPhysicalDevice physicalDevice,
317 uint32_t* pCount,
318 VkLayerProperties* pProperties)
319{
320 /* Shader checker physical device layers are the same as global */
321 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
322 shader_checker_global_layers,
323 pCount, pProperties);
Jon Ashburn207a3af2015-06-10 16:43:31 -0600324}
Chris Forbes2778f302015-04-02 13:22:31 +1300325
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200326static char const *
327storage_class_name(unsigned sc)
328{
329 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600330 case spv::StorageClassInput: return "input";
331 case spv::StorageClassOutput: return "output";
332 case spv::StorageClassUniformConstant: return "const uniform";
333 case spv::StorageClassUniform: return "uniform";
334 case spv::StorageClassWorkgroupLocal: return "workgroup local";
335 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
336 case spv::StorageClassPrivateGlobal: return "private global";
337 case spv::StorageClassFunction: return "function";
338 case spv::StorageClassGeneric: return "generic";
Cody Northrop97e52d82015-04-20 14:09:40 -0600339 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600340 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200341 default: return "unknown";
342 }
343}
344
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200345/* returns ptr to null terminator */
346static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600347describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200348{
349 auto type_def_it = src->type_def_index.find(type);
350
351 if (type_def_it == src->type_def_index.end()) {
352 return dst + sprintf(dst, "undef");
353 }
354
355 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
356 unsigned opcode = code[0] & 0x0ffffu;
357 switch (opcode) {
358 case spv::OpTypeBool:
359 return dst + sprintf(dst, "bool");
360 case spv::OpTypeInt:
361 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
362 case spv::OpTypeFloat:
363 return dst + sprintf(dst, "float%d", code[2]);
364 case spv::OpTypeVector:
365 dst += sprintf(dst, "vec%d of ", code[3]);
366 return describe_type(dst, src, code[2]);
367 case spv::OpTypeMatrix:
368 dst += sprintf(dst, "mat%d of ", code[3]);
369 return describe_type(dst, src, code[2]);
370 case spv::OpTypeArray:
371 dst += sprintf(dst, "arr[%d] of ", code[3]);
372 return describe_type(dst, src, code[2]);
373 case spv::OpTypePointer:
374 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
375 return describe_type(dst, src, code[3]);
376 case spv::OpTypeStruct:
377 {
378 unsigned oplen = code[0] >> 16;
379 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600380 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200381 dst = describe_type(dst, src, code[i]);
382 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
383 }
384 return dst;
385 }
Chris Forbes0ae15802015-08-14 12:04:39 +1200386 case spv::OpTypeSampler:
387 return dst + sprintf(dst, "sampler");
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200388 default:
389 return dst + sprintf(dst, "oddtype");
390 }
391}
392
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200393static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600394types_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 +1200395{
396 auto a_type_def_it = a->type_def_index.find(a_type);
397 auto b_type_def_it = b->type_def_index.find(b_type);
398
399 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200400 return false;
401 }
402
403 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200404 return false;
405 }
406
407 /* walk two type trees together, and complain about differences */
408 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
409 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
410
411 unsigned a_opcode = a_code[0] & 0x0ffffu;
412 unsigned b_opcode = b_code[0] & 0x0ffffu;
413
Chris Forbesf3fc0332015-06-05 14:57:05 +1200414 if (b_arrayed && b_opcode == spv::OpTypeArray) {
415 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
416 return types_match(a, b, a_type, b_code[2], false);
417 }
418
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200419 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200420 return false;
421 }
422
423 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200424 /* 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 +1200425 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200426 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200427 case spv::OpTypeInt:
428 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200429 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200430 case spv::OpTypeFloat:
431 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200432 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200433 case spv::OpTypeVector:
434 case spv::OpTypeMatrix:
435 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200436 /* match on element type, count. these all have the same layout. we don't get here if
437 * b_arrayed -- that is handled above. */
438 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 +1200439 case spv::OpTypeStruct:
440 /* match on all element types */
441 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200442 if (b_arrayed) {
443 /* for the purposes of matching different levels of arrayness, structs are leaves. */
444 return false;
445 }
446
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200447 unsigned a_len = a_code[0] >> 16;
448 unsigned b_len = b_code[0] >> 16;
449
450 if (a_len != b_len) {
451 return false; /* structs cannot match if member counts differ */
452 }
453
Ian Elliott1cb62222015-04-17 11:05:04 -0600454 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200455 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200456 return false;
457 }
458 }
459
460 return true;
461 }
462 case spv::OpTypePointer:
463 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200464 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200465
466 default:
467 /* remaining types are CLisms, or may not appear in the interfaces we
468 * are interested in. Just claim no match.
469 */
470 return false;
471
472 }
473}
474
Chris Forbes06e8fc32015-04-13 12:14:52 +1200475static int
476value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
477{
478 auto it = map.find(id);
479 if (it == map.end())
480 return def;
481 else
482 return it->second;
483}
484
Chris Forbes06e8fc32015-04-13 12:14:52 +1200485struct interface_var {
486 uint32_t id;
487 uint32_t type_id;
488 /* TODO: collect the name, too? Isn't required to be present. */
489};
490
Chris Forbes06e8fc32015-04-13 12:14:52 +1200491static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600492collect_interface_by_location(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200493 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200494 std::map<uint32_t, interface_var> &out,
495 std::map<uint32_t, interface_var> &builtins_out)
496{
497 unsigned int const *code = (unsigned int const *)&src->words[0];
498 size_t size = src->words.size();
499
Chris Forbes06e8fc32015-04-13 12:14:52 +1200500 std::unordered_map<unsigned, unsigned> var_locations;
501 std::unordered_map<unsigned, unsigned> var_builtins;
502
503 unsigned word = 5;
504 while (word < size) {
505
506 unsigned opcode = code[word] & 0x0ffffu;
507 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
508
509 /* We consider two interface models: SSO rendezvous-by-location, and
510 * builtins. Complain about anything that fits neither model.
511 */
512 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600513 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200514 var_locations[code[word+1]] = code[word+3];
515 }
516
Cody Northrop97e52d82015-04-20 14:09:40 -0600517 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200518 var_builtins[code[word+1]] = code[word+3];
519 }
520 }
521
522 /* TODO: handle grouped decorations */
523 /* TODO: handle index=1 dual source outputs from FS -- two vars will
524 * have the same location, and we DONT want to clobber. */
525
Ian Elliott1cb62222015-04-17 11:05:04 -0600526 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200527 int location = value_or_default(var_locations, code[word+2], -1);
528 int builtin = value_or_default(var_builtins, code[word+2], -1);
529
530 if (location == -1 && builtin == -1) {
531 /* No location defined, and not bound to an API builtin.
532 * The spec says nothing about how this case works (or doesn't)
533 * for interface matching.
534 */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600535 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 +1200536 "var %d (type %d) in %s interface has no Location or Builtin decoration",
537 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes06e8fc32015-04-13 12:14:52 +1200538 }
539 else if (location != -1) {
540 /* A user-defined interface variable, with a location. */
541 interface_var v;
542 v.id = code[word+2];
543 v.type_id = code[word+1];
544 out[location] = v;
545 }
546 else {
547 /* A builtin interface variable */
548 interface_var v;
549 v.id = code[word+2];
550 v.type_id = code[word+1];
551 builtins_out[builtin] = v;
552 }
553 }
554
555 word += oplen;
556 }
557}
558
Chris Forbes0ae15802015-08-14 12:04:39 +1200559static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600560collect_interface_by_descriptor_slot(layer_data *my_data, VkDevice dev,
Chris Forbes0ae15802015-08-14 12:04:39 +1200561 shader_module const *src, spv::StorageClass sinterface,
562 std::map<std::pair<unsigned, unsigned>, interface_var> &out)
563{
564 unsigned int const *code = (unsigned int const *)&src->words[0];
565 size_t size = src->words.size();
566
567 std::unordered_map<unsigned, unsigned> var_sets;
568 std::unordered_map<unsigned, unsigned> var_bindings;
569
570 unsigned word = 5;
571 while (word < size) {
572
573 unsigned opcode = code[word] & 0x0ffffu;
574 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
575
576 /* We consider two interface models: SSO rendezvous-by-location, and
577 * builtins. Complain about anything that fits neither model.
578 */
579 if (opcode == spv::OpDecorate) {
580 if (code[word+2] == spv::DecorationDescriptorSet) {
581 var_sets[code[word+1]] = code[word+3];
582 }
583
584 if (code[word+2] == spv::DecorationBinding) {
585 var_bindings[code[word+1]] = code[word+3];
586 }
587 }
588
589 if (opcode == spv::OpVariable && (code[word+3] == spv::StorageClassUniform ||
590 code[word+3] == spv::StorageClassUniformConstant)) {
591 unsigned set = value_or_default(var_sets, code[word+2], 0);
592 unsigned binding = value_or_default(var_bindings, code[word+2], 0);
593
594 auto existing_it = out.find(std::make_pair(set, binding));
595 if (existing_it != out.end()) {
596 /* conflict within spv image */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600597 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 +1200598 SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
599 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
600 code[word+2], code[word+1], storage_class_name(sinterface),
601 existing_it->first.first, existing_it->first.second);
602 }
603
604 interface_var v;
605 v.id = code[word+2];
606 v.type_id = code[word+1];
607 out[std::make_pair(set, binding)] = v;
608 }
609
610 word += oplen;
611 }
612}
613
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600614VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
615 VkDevice device,
616 const VkShaderModuleCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800617 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600618 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300619{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600620 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes46794b82015-09-18 11:40:23 +1200621 /* Protect the driver from non-SPIRV shaders */
622 if (!shader_is_spirv(pCreateInfo)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600623 log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE,
Chris Forbes46794b82015-09-18 11:40:23 +1200624 /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
625 "Shader is not SPIR-V");
626 return VK_ERROR_VALIDATION_FAILED;
627 }
628
Chia-I Wuf7458c52015-10-26 21:10:41 +0800629 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200630
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600631 if (res == VK_SUCCESS) {
632 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600633 my_data->shader_module_map[*pShaderModule] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600634 loader_platform_thread_unlock_mutex(&globalLock);
635 }
Chris Forbes2778f302015-04-02 13:22:31 +1300636 return res;
637}
638
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600639VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
640 VkDevice device,
641 const VkShaderCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800642 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600643 VkShader *pShader)
644{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600645 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800646 VkResult res = my_data->device_dispatch_table->CreateShader(device, pCreateInfo, pAllocator, pShader);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600647
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600648 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600649 my_data->shader_object_map[*pShader] = new shader_object(my_data, pCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600650 loader_platform_thread_unlock_mutex(&globalLock);
651 return res;
652}
Chris Forbes2778f302015-04-02 13:22:31 +1300653
Chia-I Wu08accc62015-07-07 11:50:03 +0800654VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
655 VkDevice device,
656 const VkRenderPassCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800657 const VkAllocationCallbacks* pAllocator,
Chia-I Wu08accc62015-07-07 11:50:03 +0800658 VkRenderPass *pRenderPass)
659{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600660 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800661 VkResult res = my_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800662
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600663 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600664 my_data->render_pass_map[*pRenderPass] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800665 loader_platform_thread_unlock_mutex(&globalLock);
666 return res;
667}
668
Chris Forbesee99b9b2015-05-25 11:13:22 +1200669static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600670validate_interface_between_stages(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200671 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600672 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200673 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200674{
675 std::map<uint32_t, interface_var> outputs;
676 std::map<uint32_t, interface_var> inputs;
677
678 std::map<uint32_t, interface_var> builtin_outputs;
679 std::map<uint32_t, interface_var> builtin_inputs;
680
Chris Forbesee99b9b2015-05-25 11:13:22 +1200681 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200682
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600683 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
684 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200685
686 auto a_it = outputs.begin();
687 auto b_it = inputs.begin();
688
689 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600690 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
691 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
692 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200693 auto a_first = a_at_end ? 0 : a_it->first;
694 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600695
Mike Stroyan19a6de22015-09-10 14:10:25 -0600696 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600697 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 -0600698 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
699 pass = false;
700 }
Chris Forbes41002452015-04-08 10:19:16 +1200701 a_it++;
702 }
David Pinedod8f83d82015-04-27 16:36:17 -0600703 else if (a_at_end || a_first > b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600704 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 -0600705 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
706 pass = false;
707 }
Chris Forbes41002452015-04-08 10:19:16 +1200708 b_it++;
709 }
710 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200711 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 +1200712 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200713 }
714 else {
715 char producer_type[1024];
716 char consumer_type[1024];
717 describe_type(producer_type, producer, a_it->second.type_id);
718 describe_type(consumer_type, consumer, b_it->second.type_id);
719
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600720 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 -0600721 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
Chris Forbesee99b9b2015-05-25 11:13:22 +1200722 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600723 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200724 }
Chris Forbes41002452015-04-08 10:19:16 +1200725 a_it++;
726 b_it++;
727 }
728 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200729
730 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200731}
732
733
Chris Forbes3616b462015-04-08 10:37:20 +1200734enum FORMAT_TYPE {
735 FORMAT_TYPE_UNDEFINED,
736 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
737 FORMAT_TYPE_SINT,
738 FORMAT_TYPE_UINT,
739};
740
741
742static unsigned
743get_format_type(VkFormat fmt) {
744 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800745 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200746 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800747 case VK_FORMAT_R8_SINT:
748 case VK_FORMAT_R8G8_SINT:
749 case VK_FORMAT_R8G8B8_SINT:
750 case VK_FORMAT_R8G8B8A8_SINT:
751 case VK_FORMAT_R16_SINT:
752 case VK_FORMAT_R16G16_SINT:
753 case VK_FORMAT_R16G16B16_SINT:
754 case VK_FORMAT_R16G16B16A16_SINT:
755 case VK_FORMAT_R32_SINT:
756 case VK_FORMAT_R32G32_SINT:
757 case VK_FORMAT_R32G32B32_SINT:
758 case VK_FORMAT_R32G32B32A32_SINT:
759 case VK_FORMAT_B8G8R8_SINT:
760 case VK_FORMAT_B8G8R8A8_SINT:
761 case VK_FORMAT_R10G10B10A2_SINT:
762 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200763 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800764 case VK_FORMAT_R8_UINT:
765 case VK_FORMAT_R8G8_UINT:
766 case VK_FORMAT_R8G8B8_UINT:
767 case VK_FORMAT_R8G8B8A8_UINT:
768 case VK_FORMAT_R16_UINT:
769 case VK_FORMAT_R16G16_UINT:
770 case VK_FORMAT_R16G16B16_UINT:
771 case VK_FORMAT_R16G16B16A16_UINT:
772 case VK_FORMAT_R32_UINT:
773 case VK_FORMAT_R32G32_UINT:
774 case VK_FORMAT_R32G32B32_UINT:
775 case VK_FORMAT_R32G32B32A32_UINT:
776 case VK_FORMAT_B8G8R8_UINT:
777 case VK_FORMAT_B8G8R8A8_UINT:
778 case VK_FORMAT_R10G10B10A2_UINT:
779 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200780 return FORMAT_TYPE_UINT;
781 default:
782 return FORMAT_TYPE_FLOAT;
783 }
784}
785
786
Chris Forbes156a1162015-05-04 14:04:06 +1200787/* characterizes a SPIR-V type appearing in an interface to a FF stage,
788 * for comparison to a VkFormat's characterization above. */
789static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600790get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200791{
792 auto type_def_it = src->type_def_index.find(type);
793
794 if (type_def_it == src->type_def_index.end()) {
795 return FORMAT_TYPE_UNDEFINED;
796 }
797
798 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
799 unsigned opcode = code[0] & 0x0ffffu;
800 switch (opcode) {
801 case spv::OpTypeInt:
802 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
803 case spv::OpTypeFloat:
804 return FORMAT_TYPE_FLOAT;
805 case spv::OpTypeVector:
806 return get_fundamental_type(src, code[2]);
807 case spv::OpTypeMatrix:
808 return get_fundamental_type(src, code[2]);
809 case spv::OpTypeArray:
810 return get_fundamental_type(src, code[2]);
811 case spv::OpTypePointer:
812 return get_fundamental_type(src, code[3]);
813 default:
814 return FORMAT_TYPE_UNDEFINED;
815 }
816}
817
818
Chris Forbesee99b9b2015-05-25 11:13:22 +1200819static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600820validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200821{
822 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
823 * each binding should be specified only once.
824 */
825 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200826 bool pass = true;
827
Chia-I Wud50a7d72015-10-26 20:48:51 +0800828 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes280ba2c2015-06-12 11:16:41 +1200829 auto desc = &vi->pVertexBindingDescriptions[i];
830 auto & binding = bindings[desc->binding];
831 if (binding) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600832 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 -0600833 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
834 pass = false;
835 }
Chris Forbes280ba2c2015-06-12 11:16:41 +1200836 }
837 else {
838 binding = desc;
839 }
840 }
841
842 return pass;
843}
844
845
846static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600847validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200848{
849 std::map<uint32_t, interface_var> inputs;
850 /* we collect builtin inputs, but they will never appear in the VI state --
851 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
852 */
853 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200854 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200855
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600856 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200857
858 /* Build index by location */
859 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200860 if (vi) {
Chia-I Wud50a7d72015-10-26 20:48:51 +0800861 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
Chris Forbes7191cd52015-05-25 11:13:24 +1200862 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
863 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200864
865 auto it_a = attribs.begin();
866 auto it_b = inputs.begin();
867
David Pinedod8f83d82015-04-27 16:36:17 -0600868 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
869 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
870 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200871 auto a_first = a_at_end ? 0 : it_a->first;
872 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600873 if (b_at_end || a_first < b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600874 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 -0600875 "Vertex attribute at location %d not consumed by VS", a_first)) {
876 pass = false;
877 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200878 it_a++;
879 }
David Pinedod8f83d82015-04-27 16:36:17 -0600880 else if (a_at_end || b_first < a_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600881 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 -0600882 "VS consumes input at location %d but not provided", b_first)) {
883 pass = false;
884 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200885 it_b++;
886 }
887 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200888 unsigned attrib_type = get_format_type(it_a->second->format);
889 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
890
891 /* type checking */
892 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
893 char vs_type[1024];
894 describe_type(vs_type, vs, it_b->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600895 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 +1200896 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600897 string_VkFormat(it_a->second->format), a_first, vs_type)) {
898 pass = false;
899 }
Chris Forbes401784b2015-05-04 14:04:24 +1200900 }
901
Chris Forbes6b2ead62015-04-17 10:13:28 +1200902 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200903 it_a++;
904 it_b++;
905 }
906 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200907
908 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200909}
910
911
Chris Forbesee99b9b2015-05-25 11:13:22 +1200912static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600913validate_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 +1200914{
Chia-I Wu08accc62015-07-07 11:50:03 +0800915 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200916 std::map<uint32_t, interface_var> outputs;
917 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200918 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200919
920 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
921
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600922 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200923
924 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
925 * and all color attachment should be UNORM/SNORM/FLOAT.
926 */
927 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200928 if (outputs.size()) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600929 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 -0600930 "Should not have user-defined FS outputs when using broadcast")) {
931 pass = false;
932 }
Chris Forbes3616b462015-04-08 10:37:20 +1200933 }
934
Chia-I Wu08accc62015-07-07 11:50:03 +0800935 for (unsigned i = 0; i < color_formats.size(); i++) {
936 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200937 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600938 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 -0600939 "CB format should not be SINT or UINT when using broadcast")) {
940 pass = false;
941 }
Chris Forbes3616b462015-04-08 10:37:20 +1200942 }
943 }
944
Chris Forbesee99b9b2015-05-25 11:13:22 +1200945 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200946 }
947
948 auto it = outputs.begin();
949 uint32_t attachment = 0;
950
951 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
952 * are currently dense, but the parallel with matching between shader stages is nice.
953 */
954
Chris Forbes9715d4a2015-07-10 09:06:54 +1200955 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200956 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
957 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600958 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 -0600959 "FS writes to output location %d with no matching attachment", it->first)) {
960 pass = false;
961 }
Chris Forbes3616b462015-04-08 10:37:20 +1200962 it++;
963 }
964 else if (it == outputs.end() || it->first > attachment) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600965 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 -0600966 "Attachment %d not written by FS", attachment)) {
967 pass = false;
968 }
Chris Forbes3616b462015-04-08 10:37:20 +1200969 attachment++;
970 }
971 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200972 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800973 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200974
975 /* type checking */
976 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
977 char fs_type[1024];
978 describe_type(fs_type, fs, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600979 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 +1200980 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600981 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
982 pass = false;
983 }
Chris Forbes46d31e52015-05-04 14:20:10 +1200984 }
985
Chris Forbes6b2ead62015-04-17 10:13:28 +1200986 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200987 it++;
988 attachment++;
989 }
990 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200991
992 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200993}
994
995
Chris Forbesf044ec92015-06-05 15:01:08 +1200996struct shader_stage_attributes {
997 char const * const name;
998 bool arrayed_input;
999};
1000
1001
1002static shader_stage_attributes
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001003shader_stage_attribs[] = {
Chris Forbesf044ec92015-06-05 15:01:08 +12001004 { "vertex shader", false },
1005 { "tessellation control shader", true },
1006 { "tessellation evaluation shader", false },
1007 { "geometry shader", true },
1008 { "fragment shader", false },
1009};
1010
1011
Chris Forbes556c76c2015-08-14 12:04:59 +12001012static VkDescriptorSetLayoutBinding *
1013find_descriptor_binding(std::vector<std::vector<VkDescriptorSetLayoutBinding>*>* layout,
1014 std::pair<unsigned, unsigned> slot)
1015{
1016 if (!layout)
1017 return nullptr;
1018
1019 if (slot.first >= layout->size())
1020 return nullptr;
1021
1022 auto set = (*layout)[slot.first];
1023
1024 if (slot.second >= set->size())
1025 return nullptr;
1026
1027 return &(*set)[slot.second];
1028}
1029
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001030static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1031{
1032 uint32_t bit_pos = u_ffs(stage);
1033 return bit_pos-1;
1034}
Chris Forbes556c76c2015-08-14 12:04:59 +12001035
Chris Forbes81874ba2015-06-04 20:23:00 +12001036static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001037validate_graphics_pipeline(layer_data *my_data, VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001038{
Chris Forbesf6800b52015-04-08 10:16:45 +12001039 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1040 * before trying to do anything more: */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001041 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1042 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1043 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbesf6800b52015-04-08 10:16:45 +12001044
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001045 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1046 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Chia-I Wu08accc62015-07-07 11:50:03 +08001047 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001048 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001049 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001050
Chris Forbes7f963832015-05-29 14:55:18 +12001051 loader_platform_thread_lock_mutex(&globalLock);
1052
Tony Barbour92503c62015-07-13 15:28:47 -06001053 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001054 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1055 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001056
Chia-I Wuff3780f2015-10-26 19:52:46 +08001057 // always true; pStage->stage may be revived in a later revision and
1058 // this will make sense again
1059 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 -06001060 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001061 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 +08001062 "Unknown shader stage %d", VK_SHADER_STAGE_VERTEX_BIT)) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001063 pass = false;
1064 }
Chris Forbes6b2ead62015-04-17 10:13:28 +12001065 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001066 else {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001067 struct shader_object *shader = my_data->shader_object_map[pStage->shader];
Chia-I Wuff3780f2015-10-26 19:52:46 +08001068 shaders[get_shader_stage_id(shader->stage)] = shader->module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001069
1070 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001071 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001072 collect_interface_by_descriptor_slot(my_data, dev, shader->module, spv::StorageClassUniform,
Chris Forbes46794b82015-09-18 11:40:23 +12001073 descriptor_uses);
Chris Forbes556c76c2015-08-14 12:04:59 +12001074
Chia-I Wue2fc5522015-10-26 20:04:44 +08001075 auto layout = pCreateInfo->layout != VK_NULL_HANDLE ?
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001076 my_data->pipeline_layout_map[pCreateInfo->layout] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001077
Chris Forbes46794b82015-09-18 11:40:23 +12001078 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001079
Chris Forbes46794b82015-09-18 11:40:23 +12001080 /* find the matching binding */
1081 auto binding = find_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001082
Chris Forbes46794b82015-09-18 11:40:23 +12001083 if (binding == nullptr) {
1084 char type_name[1024];
1085 describe_type(type_name, shader->module, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001086 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 +12001087 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1088 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001089 it->first.first, it->first.second, type_name)) {
1090 pass = false;
1091 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001092 }
1093 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001094 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001095 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001096 }
1097
Chia-I Wu08accc62015-07-07 11:50:03 +08001098 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001099 rp = my_data->render_pass_map[pCreateInfo->renderPass];
Chia-I Wu08accc62015-07-07 11:50:03 +08001100
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001101 vi = pCreateInfo->pVertexInputState;
1102
Chris Forbes280ba2c2015-06-12 11:16:41 +12001103 if (vi) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001104 pass = validate_vi_consistency(my_data, dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001105 }
1106
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001107 if (shaders[vertex_stage]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001108 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001109 }
1110
Chris Forbesf044ec92015-06-05 15:01:08 +12001111 /* TODO: enforce rules about present combinations of shaders */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001112 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1113 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
Chris Forbesf044ec92015-06-05 15:01:08 +12001114
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001115 while (!shaders[producer] && producer != fragment_stage) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001116 producer++;
1117 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001118 }
1119
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001120 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001121 assert(shaders[producer]);
1122 if (shaders[consumer]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001123 pass = validate_interface_between_stages(my_data, dev,
Chris Forbes46794b82015-09-18 11:40:23 +12001124 shaders[producer], shader_stage_attribs[producer].name,
1125 shaders[consumer], shader_stage_attribs[consumer].name,
1126 shader_stage_attribs[consumer].arrayed_input) && pass;
Chris Forbesf044ec92015-06-05 15:01:08 +12001127
1128 producer = consumer;
1129 }
1130 }
1131
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001132 if (shaders[fragment_stage] && rp) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001133 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001134 }
1135
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001136 delete shaders;
1137
Chris Forbes7f963832015-05-29 14:55:18 +12001138 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001139 return pass;
1140}
1141
Jon Ashburnc669cc62015-07-09 15:02:25 -06001142//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001143VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001144vkCreateGraphicsPipelines(VkDevice device,
1145 VkPipelineCache pipelineCache,
1146 uint32_t count,
1147 const VkGraphicsPipelineCreateInfo *pCreateInfos,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001148 const VkAllocationCallbacks* pAllocator,
Jon Ashburnc669cc62015-07-09 15:02:25 -06001149 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001150{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001151 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes36a372f2015-07-24 13:53:47 +12001152 bool pass = true;
1153 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001154 pass = validate_graphics_pipeline(my_data, device, &pCreateInfos[i]) && pass;
Chris Forbes36a372f2015-07-24 13:53:47 +12001155 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001156
1157 if (pass) {
1158 /* The driver is allowed to crash if passed junk. Only actually create the
1159 * pipeline if we didn't run into any showstoppers above.
1160 */
Chia-I Wuf7458c52015-10-26 21:10:41 +08001161 return my_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001162 }
1163 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001164 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001165 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001166}
1167
1168
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001169VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Chris Forbesb1dee272015-07-03 13:50:24 +12001170{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001171 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001172 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Chris Forbesb1dee272015-07-03 13:50:24 +12001173 if (result == VK_SUCCESS) {
1174 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001175 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1176 }
1177 return result;
1178}
Chris Forbes39d8d752015-06-04 20:27:09 +12001179
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001180/* hook DextroyDevice to remove tableMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001181VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001182{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001183 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001184 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001185 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001186 delete my_device_data->device_dispatch_table;
1187 layer_data_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001188}
1189
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001190VkResult VKAPI vkCreateInstance(
1191 const VkInstanceCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001192 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001193 VkInstance* pInstance)
1194{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001195 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1196 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +08001197 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001198
1199 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001200 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1201 my_data->report_data = debug_report_create_instance(
1202 pTable,
1203 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +08001204 pCreateInfo->enabledExtensionNameCount,
Chris Forbesb1dee272015-07-03 13:50:24 +12001205 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001206
Chris Forbesb1dee272015-07-03 13:50:24 +12001207 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001208 }
1209 return result;
1210}
1211
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001212/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001213VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001214{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001215 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001216 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001217 my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
Chris Forbesb1dee272015-07-03 13:50:24 +12001218
1219 // Clean up logging callback, if any
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001220 while (my_data->logging_callback.size() > 0) {
1221 VkDbgMsgCallback callback = my_data->logging_callback.back();
1222 layer_destroy_msg_callback(my_data->report_data, callback);
1223 my_data->logging_callback.pop_back();
Chris Forbesb1dee272015-07-03 13:50:24 +12001224 }
1225
1226 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001227 delete my_data->instance_dispatch_table;
1228 layer_data_map.erase(key);
1229 if (layer_data_map.empty()) {
1230 // Release mutex when destroying last instance.
1231 loader_platform_thread_delete_mutex(&globalLock);
1232 globalLockInitialized = 0;
1233 }
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001234}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001235
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001236VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001237 VkInstance instance,
1238 VkFlags msgFlags,
1239 const PFN_vkDbgMsgCallback pfnMsgCallback,
1240 void* pUserData,
1241 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001242{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001243 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1244 VkResult res = my_data->instance_dispatch_table->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001245 if (VK_SUCCESS == res) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001246 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1247 }
1248 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001249}
1250
1251VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001252 VkInstance instance,
1253 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001254{
Chris Forbesb1dee272015-07-03 13:50:24 +12001255 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001256 VkResult res = my_data->instance_dispatch_table->DbgDestroyMsgCallback(instance, msgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001257 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1258 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001259}
1260
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001261VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001262{
Chris Forbesb1dee272015-07-03 13:50:24 +12001263 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001264 return NULL;
1265
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001266 layer_data *my_data;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001267 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001268 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001269 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
1270 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
1271 my_data->device_dispatch_table = new VkLayerDispatchTable;
1272 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001273 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001274 }
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001275 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Jon Ashburn8fd08252015-05-28 16:25:02 -06001276
Chris Forbes2778f302015-04-02 13:22:31 +13001277#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001278 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001279 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001280
Chris Forbesb1dee272015-07-03 13:50:24 +12001281 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001282 ADD_HOOK(vkCreateShaderModule);
Chris Forbes2778f302015-04-02 13:22:31 +13001283 ADD_HOOK(vkCreateShader);
Chia-I Wu08accc62015-07-07 11:50:03 +08001284 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001285 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001286 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001287 ADD_HOOK(vkCreateDescriptorSetLayout);
1288 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001289#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001290
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001291 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001292 {
1293 if (pTable->GetDeviceProcAddr == NULL)
1294 return NULL;
1295 return pTable->GetDeviceProcAddr(dev, funcName);
1296 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001297}
1298
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001299VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001300{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001301 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001302
Chris Forbesb1dee272015-07-03 13:50:24 +12001303 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001304 return NULL;
1305
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001306 layer_data *my_data;
Chris Forbesb1dee272015-07-03 13:50:24 +12001307 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001308 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
1309 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
1310 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
1311 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001312 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001313 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001314#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001315 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001316 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001317
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001318 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001319 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001320 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1321 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1322 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1323 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001324#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001325
Chris Forbesb1dee272015-07-03 13:50:24 +12001326
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001327 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001328 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001329 if (fptr)
1330 return fptr;
1331
Chris Forbesb1dee272015-07-03 13:50:24 +12001332 {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001333 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001334 if (pTable->GetInstanceProcAddr == NULL)
1335 return NULL;
1336 return pTable->GetInstanceProcAddr(instance, funcName);
1337 }
Chris Forbes2778f302015-04-02 13:22:31 +13001338}