blob: 53efcad7df8b7a9170af28b8fad11f1175b91028 [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,
141 VkDescriptorSetLayout* pSetLayout)
142{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600143 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200144 /* stash a copy of the layout bindings */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600145 VkResult result = my_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pSetLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200146
147 if (VK_SUCCESS == result) {
148 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600149 auto& bindings = my_data->descriptor_set_layout_map[*pSetLayout];
Chris Forbes2b8493a2015-08-12 15:03:22 +1200150 bindings = new std::vector<VkDescriptorSetLayoutBinding>(
Chia-I Wud50a7d72015-10-26 20:48:51 +0800151 pCreateInfo->pBindings, pCreateInfo->pBindings + pCreateInfo->bindingCount);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200152 loader_platform_thread_unlock_mutex(&globalLock);
153 }
154
155 return result;
156}
157
Chris Forbes2b8493a2015-08-12 15:03:22 +1200158VK_LAYER_EXPORT VkResult VKAPI vkCreatePipelineLayout(
159 VkDevice device,
160 const VkPipelineLayoutCreateInfo* pCreateInfo,
161 VkPipelineLayout* pPipelineLayout)
162{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600163 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
164 VkResult result = my_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pPipelineLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200165
166 if (VK_SUCCESS == result) {
167 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600168 auto& layouts = my_data->pipeline_layout_map[*pPipelineLayout];
Chris Forbes2b8493a2015-08-12 15:03:22 +1200169 layouts = new std::vector<std::vector<VkDescriptorSetLayoutBinding>*>();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800170 layouts->reserve(pCreateInfo->setLayoutCount);
171 for (unsigned i = 0; i < pCreateInfo->setLayoutCount; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600172 layouts->push_back(my_data->descriptor_set_layout_map[pCreateInfo->pSetLayouts[i]]);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200173 }
174 loader_platform_thread_unlock_mutex(&globalLock);
175 }
176
177 return result;
178}
179
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200180static void
181build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
182{
183 unsigned int const *code = (unsigned int const *)&words[0];
184 size_t size = words.size();
185
186 unsigned word = 5;
187 while (word < size) {
188 unsigned opcode = code[word] & 0x0ffffu;
189 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
190
191 switch (opcode) {
192 case spv::OpTypeVoid:
193 case spv::OpTypeBool:
194 case spv::OpTypeInt:
195 case spv::OpTypeFloat:
196 case spv::OpTypeVector:
197 case spv::OpTypeMatrix:
GregF7c45bed2015-07-21 17:22:50 -0600198 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200199 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600200 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200201 case spv::OpTypeArray:
202 case spv::OpTypeRuntimeArray:
203 case spv::OpTypeStruct:
204 case spv::OpTypeOpaque:
205 case spv::OpTypePointer:
206 case spv::OpTypeFunction:
207 case spv::OpTypeEvent:
208 case spv::OpTypeDeviceEvent:
209 case spv::OpTypeReserveId:
210 case spv::OpTypeQueue:
211 case spv::OpTypePipe:
212 type_def_index[code[word+1]] = word;
213 break;
214
215 default:
216 /* We only care about type definitions */
217 break;
218 }
219
220 word += oplen;
221 }
222}
223
Chris Forbes46794b82015-09-18 11:40:23 +1200224bool
225shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo)
226{
227 uint32_t *words = (uint32_t *)pCreateInfo->pCode;
Courtney Goeltzenleuchter8f8367e2015-10-07 08:38:30 -0600228 size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
Chris Forbes46794b82015-09-18 11:40:23 +1200229
230 /* Just validate that the header makes sense. */
231 return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
232}
233
Chris Forbesb6b8c462015-04-15 06:59:41 +1200234static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200235init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200236{
Chris Forbesb1dee272015-07-03 13:50:24 +1200237 uint32_t report_flags = 0;
238 uint32_t debug_action = 0;
239 FILE *log_output = NULL;
240 const char *option_str;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600241 VkDbgMsgCallback callback;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200242 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200243 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
244 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200245
Chris Forbesb1dee272015-07-03 13:50:24 +1200246 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200247 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200248 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600249 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600250 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &callback);
251 my_data->logging_callback.push_back(callback);
252 }
253
254 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
255 layer_create_msg_callback(my_data->report_data, report_flags, win32_debug_output_msg, NULL, &callback);
256 my_data->logging_callback.push_back(callback);
Chris Forbesb1dee272015-07-03 13:50:24 +1200257 }
258
259 if (!globalLockInitialized)
260 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200261 loader_platform_thread_create_mutex(&globalLock);
262 globalLockInitialized = 1;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200263 }
264}
265
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600266static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600267 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600268 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600269 VK_API_VERSION,
270 VK_MAKE_VERSION(0, 1, 0),
271 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600272 }
Chris Forbes2778f302015-04-02 13:22:31 +1300273};
274
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600275VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600276 const char *pLayerName,
277 uint32_t *pCount,
278 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300279{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600280 /* shader checker does not have any global extensions */
281 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300282}
283
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600284VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600285 uint32_t *pCount,
286 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600287{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600288 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
289 shader_checker_global_layers,
290 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600291}
292
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600293VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600294 VkPhysicalDevice physicalDevice,
295 const char* pLayerName,
296 uint32_t* pCount,
297 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600298{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600299 /* Shader checker does not have any physical device extensions */
300 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
301}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600302
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600303VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600304 VkPhysicalDevice physicalDevice,
305 uint32_t* pCount,
306 VkLayerProperties* pProperties)
307{
308 /* Shader checker physical device layers are the same as global */
309 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
310 shader_checker_global_layers,
311 pCount, pProperties);
Jon Ashburn207a3af2015-06-10 16:43:31 -0600312}
Chris Forbes2778f302015-04-02 13:22:31 +1300313
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200314static char const *
315storage_class_name(unsigned sc)
316{
317 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600318 case spv::StorageClassInput: return "input";
319 case spv::StorageClassOutput: return "output";
320 case spv::StorageClassUniformConstant: return "const uniform";
321 case spv::StorageClassUniform: return "uniform";
322 case spv::StorageClassWorkgroupLocal: return "workgroup local";
323 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
324 case spv::StorageClassPrivateGlobal: return "private global";
325 case spv::StorageClassFunction: return "function";
326 case spv::StorageClassGeneric: return "generic";
Cody Northrop97e52d82015-04-20 14:09:40 -0600327 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600328 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200329 default: return "unknown";
330 }
331}
332
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200333/* returns ptr to null terminator */
334static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600335describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200336{
337 auto type_def_it = src->type_def_index.find(type);
338
339 if (type_def_it == src->type_def_index.end()) {
340 return dst + sprintf(dst, "undef");
341 }
342
343 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
344 unsigned opcode = code[0] & 0x0ffffu;
345 switch (opcode) {
346 case spv::OpTypeBool:
347 return dst + sprintf(dst, "bool");
348 case spv::OpTypeInt:
349 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
350 case spv::OpTypeFloat:
351 return dst + sprintf(dst, "float%d", code[2]);
352 case spv::OpTypeVector:
353 dst += sprintf(dst, "vec%d of ", code[3]);
354 return describe_type(dst, src, code[2]);
355 case spv::OpTypeMatrix:
356 dst += sprintf(dst, "mat%d of ", code[3]);
357 return describe_type(dst, src, code[2]);
358 case spv::OpTypeArray:
359 dst += sprintf(dst, "arr[%d] of ", code[3]);
360 return describe_type(dst, src, code[2]);
361 case spv::OpTypePointer:
362 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
363 return describe_type(dst, src, code[3]);
364 case spv::OpTypeStruct:
365 {
366 unsigned oplen = code[0] >> 16;
367 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600368 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200369 dst = describe_type(dst, src, code[i]);
370 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
371 }
372 return dst;
373 }
Chris Forbes0ae15802015-08-14 12:04:39 +1200374 case spv::OpTypeSampler:
375 return dst + sprintf(dst, "sampler");
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200376 default:
377 return dst + sprintf(dst, "oddtype");
378 }
379}
380
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200381static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600382types_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 +1200383{
384 auto a_type_def_it = a->type_def_index.find(a_type);
385 auto b_type_def_it = b->type_def_index.find(b_type);
386
387 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200388 return false;
389 }
390
391 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200392 return false;
393 }
394
395 /* walk two type trees together, and complain about differences */
396 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
397 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
398
399 unsigned a_opcode = a_code[0] & 0x0ffffu;
400 unsigned b_opcode = b_code[0] & 0x0ffffu;
401
Chris Forbesf3fc0332015-06-05 14:57:05 +1200402 if (b_arrayed && b_opcode == spv::OpTypeArray) {
403 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
404 return types_match(a, b, a_type, b_code[2], false);
405 }
406
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200407 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200408 return false;
409 }
410
411 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200412 /* 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 +1200413 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200414 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200415 case spv::OpTypeInt:
416 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200417 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200418 case spv::OpTypeFloat:
419 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200420 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200421 case spv::OpTypeVector:
422 case spv::OpTypeMatrix:
423 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200424 /* match on element type, count. these all have the same layout. we don't get here if
425 * b_arrayed -- that is handled above. */
426 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 +1200427 case spv::OpTypeStruct:
428 /* match on all element types */
429 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200430 if (b_arrayed) {
431 /* for the purposes of matching different levels of arrayness, structs are leaves. */
432 return false;
433 }
434
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200435 unsigned a_len = a_code[0] >> 16;
436 unsigned b_len = b_code[0] >> 16;
437
438 if (a_len != b_len) {
439 return false; /* structs cannot match if member counts differ */
440 }
441
Ian Elliott1cb62222015-04-17 11:05:04 -0600442 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200443 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200444 return false;
445 }
446 }
447
448 return true;
449 }
450 case spv::OpTypePointer:
451 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200452 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200453
454 default:
455 /* remaining types are CLisms, or may not appear in the interfaces we
456 * are interested in. Just claim no match.
457 */
458 return false;
459
460 }
461}
462
Chris Forbes06e8fc32015-04-13 12:14:52 +1200463static int
464value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
465{
466 auto it = map.find(id);
467 if (it == map.end())
468 return def;
469 else
470 return it->second;
471}
472
Chris Forbes06e8fc32015-04-13 12:14:52 +1200473struct interface_var {
474 uint32_t id;
475 uint32_t type_id;
476 /* TODO: collect the name, too? Isn't required to be present. */
477};
478
Chris Forbes06e8fc32015-04-13 12:14:52 +1200479static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600480collect_interface_by_location(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200481 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200482 std::map<uint32_t, interface_var> &out,
483 std::map<uint32_t, interface_var> &builtins_out)
484{
485 unsigned int const *code = (unsigned int const *)&src->words[0];
486 size_t size = src->words.size();
487
Chris Forbes06e8fc32015-04-13 12:14:52 +1200488 std::unordered_map<unsigned, unsigned> var_locations;
489 std::unordered_map<unsigned, unsigned> var_builtins;
490
491 unsigned word = 5;
492 while (word < size) {
493
494 unsigned opcode = code[word] & 0x0ffffu;
495 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
496
497 /* We consider two interface models: SSO rendezvous-by-location, and
498 * builtins. Complain about anything that fits neither model.
499 */
500 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600501 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200502 var_locations[code[word+1]] = code[word+3];
503 }
504
Cody Northrop97e52d82015-04-20 14:09:40 -0600505 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200506 var_builtins[code[word+1]] = code[word+3];
507 }
508 }
509
510 /* TODO: handle grouped decorations */
511 /* TODO: handle index=1 dual source outputs from FS -- two vars will
512 * have the same location, and we DONT want to clobber. */
513
Ian Elliott1cb62222015-04-17 11:05:04 -0600514 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200515 int location = value_or_default(var_locations, code[word+2], -1);
516 int builtin = value_or_default(var_builtins, code[word+2], -1);
517
518 if (location == -1 && builtin == -1) {
519 /* No location defined, and not bound to an API builtin.
520 * The spec says nothing about how this case works (or doesn't)
521 * for interface matching.
522 */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600523 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 +1200524 "var %d (type %d) in %s interface has no Location or Builtin decoration",
525 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes06e8fc32015-04-13 12:14:52 +1200526 }
527 else if (location != -1) {
528 /* A user-defined interface variable, with a location. */
529 interface_var v;
530 v.id = code[word+2];
531 v.type_id = code[word+1];
532 out[location] = v;
533 }
534 else {
535 /* A builtin interface variable */
536 interface_var v;
537 v.id = code[word+2];
538 v.type_id = code[word+1];
539 builtins_out[builtin] = v;
540 }
541 }
542
543 word += oplen;
544 }
545}
546
Chris Forbes0ae15802015-08-14 12:04:39 +1200547static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600548collect_interface_by_descriptor_slot(layer_data *my_data, VkDevice dev,
Chris Forbes0ae15802015-08-14 12:04:39 +1200549 shader_module const *src, spv::StorageClass sinterface,
550 std::map<std::pair<unsigned, unsigned>, interface_var> &out)
551{
552 unsigned int const *code = (unsigned int const *)&src->words[0];
553 size_t size = src->words.size();
554
555 std::unordered_map<unsigned, unsigned> var_sets;
556 std::unordered_map<unsigned, unsigned> var_bindings;
557
558 unsigned word = 5;
559 while (word < size) {
560
561 unsigned opcode = code[word] & 0x0ffffu;
562 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
563
564 /* We consider two interface models: SSO rendezvous-by-location, and
565 * builtins. Complain about anything that fits neither model.
566 */
567 if (opcode == spv::OpDecorate) {
568 if (code[word+2] == spv::DecorationDescriptorSet) {
569 var_sets[code[word+1]] = code[word+3];
570 }
571
572 if (code[word+2] == spv::DecorationBinding) {
573 var_bindings[code[word+1]] = code[word+3];
574 }
575 }
576
577 if (opcode == spv::OpVariable && (code[word+3] == spv::StorageClassUniform ||
578 code[word+3] == spv::StorageClassUniformConstant)) {
579 unsigned set = value_or_default(var_sets, code[word+2], 0);
580 unsigned binding = value_or_default(var_bindings, code[word+2], 0);
581
582 auto existing_it = out.find(std::make_pair(set, binding));
583 if (existing_it != out.end()) {
584 /* conflict within spv image */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600585 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 +1200586 SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
587 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
588 code[word+2], code[word+1], storage_class_name(sinterface),
589 existing_it->first.first, existing_it->first.second);
590 }
591
592 interface_var v;
593 v.id = code[word+2];
594 v.type_id = code[word+1];
595 out[std::make_pair(set, binding)] = v;
596 }
597
598 word += oplen;
599 }
600}
601
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600602VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
603 VkDevice device,
604 const VkShaderModuleCreateInfo *pCreateInfo,
605 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300606{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600607 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes46794b82015-09-18 11:40:23 +1200608 /* Protect the driver from non-SPIRV shaders */
609 if (!shader_is_spirv(pCreateInfo)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600610 log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE,
Chris Forbes46794b82015-09-18 11:40:23 +1200611 /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
612 "Shader is not SPIR-V");
613 return VK_ERROR_VALIDATION_FAILED;
614 }
615
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600616 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200617
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600618 if (res == VK_SUCCESS) {
619 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600620 my_data->shader_module_map[*pShaderModule] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600621 loader_platform_thread_unlock_mutex(&globalLock);
622 }
Chris Forbes2778f302015-04-02 13:22:31 +1300623 return res;
624}
625
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600626VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(
627 VkDevice device,
628 const VkShaderCreateInfo *pCreateInfo,
629 VkShader *pShader)
630{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600631 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
632 VkResult res = my_data->device_dispatch_table->CreateShader(device, pCreateInfo, pShader);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600633
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600634 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600635 my_data->shader_object_map[*pShader] = new shader_object(my_data, pCreateInfo);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600636 loader_platform_thread_unlock_mutex(&globalLock);
637 return res;
638}
Chris Forbes2778f302015-04-02 13:22:31 +1300639
Chia-I Wu08accc62015-07-07 11:50:03 +0800640VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
641 VkDevice device,
642 const VkRenderPassCreateInfo *pCreateInfo,
643 VkRenderPass *pRenderPass)
644{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600645 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
646 VkResult res = my_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pRenderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800647
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600648 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600649 my_data->render_pass_map[*pRenderPass] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800650 loader_platform_thread_unlock_mutex(&globalLock);
651 return res;
652}
653
Chris Forbesee99b9b2015-05-25 11:13:22 +1200654static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600655validate_interface_between_stages(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200656 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600657 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200658 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200659{
660 std::map<uint32_t, interface_var> outputs;
661 std::map<uint32_t, interface_var> inputs;
662
663 std::map<uint32_t, interface_var> builtin_outputs;
664 std::map<uint32_t, interface_var> builtin_inputs;
665
Chris Forbesee99b9b2015-05-25 11:13:22 +1200666 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200667
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600668 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
669 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200670
671 auto a_it = outputs.begin();
672 auto b_it = inputs.begin();
673
674 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600675 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
676 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
677 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200678 auto a_first = a_at_end ? 0 : a_it->first;
679 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600680
Mike Stroyan19a6de22015-09-10 14:10:25 -0600681 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600682 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 -0600683 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
684 pass = false;
685 }
Chris Forbes41002452015-04-08 10:19:16 +1200686 a_it++;
687 }
David Pinedod8f83d82015-04-27 16:36:17 -0600688 else if (a_at_end || a_first > b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600689 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 -0600690 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
691 pass = false;
692 }
Chris Forbes41002452015-04-08 10:19:16 +1200693 b_it++;
694 }
695 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200696 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 +1200697 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200698 }
699 else {
700 char producer_type[1024];
701 char consumer_type[1024];
702 describe_type(producer_type, producer, a_it->second.type_id);
703 describe_type(consumer_type, consumer, b_it->second.type_id);
704
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600705 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 -0600706 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
Chris Forbesee99b9b2015-05-25 11:13:22 +1200707 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600708 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200709 }
Chris Forbes41002452015-04-08 10:19:16 +1200710 a_it++;
711 b_it++;
712 }
713 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200714
715 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200716}
717
718
Chris Forbes3616b462015-04-08 10:37:20 +1200719enum FORMAT_TYPE {
720 FORMAT_TYPE_UNDEFINED,
721 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
722 FORMAT_TYPE_SINT,
723 FORMAT_TYPE_UINT,
724};
725
726
727static unsigned
728get_format_type(VkFormat fmt) {
729 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800730 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200731 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800732 case VK_FORMAT_R8_SINT:
733 case VK_FORMAT_R8G8_SINT:
734 case VK_FORMAT_R8G8B8_SINT:
735 case VK_FORMAT_R8G8B8A8_SINT:
736 case VK_FORMAT_R16_SINT:
737 case VK_FORMAT_R16G16_SINT:
738 case VK_FORMAT_R16G16B16_SINT:
739 case VK_FORMAT_R16G16B16A16_SINT:
740 case VK_FORMAT_R32_SINT:
741 case VK_FORMAT_R32G32_SINT:
742 case VK_FORMAT_R32G32B32_SINT:
743 case VK_FORMAT_R32G32B32A32_SINT:
744 case VK_FORMAT_B8G8R8_SINT:
745 case VK_FORMAT_B8G8R8A8_SINT:
746 case VK_FORMAT_R10G10B10A2_SINT:
747 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200748 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800749 case VK_FORMAT_R8_UINT:
750 case VK_FORMAT_R8G8_UINT:
751 case VK_FORMAT_R8G8B8_UINT:
752 case VK_FORMAT_R8G8B8A8_UINT:
753 case VK_FORMAT_R16_UINT:
754 case VK_FORMAT_R16G16_UINT:
755 case VK_FORMAT_R16G16B16_UINT:
756 case VK_FORMAT_R16G16B16A16_UINT:
757 case VK_FORMAT_R32_UINT:
758 case VK_FORMAT_R32G32_UINT:
759 case VK_FORMAT_R32G32B32_UINT:
760 case VK_FORMAT_R32G32B32A32_UINT:
761 case VK_FORMAT_B8G8R8_UINT:
762 case VK_FORMAT_B8G8R8A8_UINT:
763 case VK_FORMAT_R10G10B10A2_UINT:
764 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200765 return FORMAT_TYPE_UINT;
766 default:
767 return FORMAT_TYPE_FLOAT;
768 }
769}
770
771
Chris Forbes156a1162015-05-04 14:04:06 +1200772/* characterizes a SPIR-V type appearing in an interface to a FF stage,
773 * for comparison to a VkFormat's characterization above. */
774static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600775get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200776{
777 auto type_def_it = src->type_def_index.find(type);
778
779 if (type_def_it == src->type_def_index.end()) {
780 return FORMAT_TYPE_UNDEFINED;
781 }
782
783 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
784 unsigned opcode = code[0] & 0x0ffffu;
785 switch (opcode) {
786 case spv::OpTypeInt:
787 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
788 case spv::OpTypeFloat:
789 return FORMAT_TYPE_FLOAT;
790 case spv::OpTypeVector:
791 return get_fundamental_type(src, code[2]);
792 case spv::OpTypeMatrix:
793 return get_fundamental_type(src, code[2]);
794 case spv::OpTypeArray:
795 return get_fundamental_type(src, code[2]);
796 case spv::OpTypePointer:
797 return get_fundamental_type(src, code[3]);
798 default:
799 return FORMAT_TYPE_UNDEFINED;
800 }
801}
802
803
Chris Forbesee99b9b2015-05-25 11:13:22 +1200804static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600805validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200806{
807 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
808 * each binding should be specified only once.
809 */
810 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200811 bool pass = true;
812
Chia-I Wud50a7d72015-10-26 20:48:51 +0800813 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes280ba2c2015-06-12 11:16:41 +1200814 auto desc = &vi->pVertexBindingDescriptions[i];
815 auto & binding = bindings[desc->binding];
816 if (binding) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600817 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 -0600818 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
819 pass = false;
820 }
Chris Forbes280ba2c2015-06-12 11:16:41 +1200821 }
822 else {
823 binding = desc;
824 }
825 }
826
827 return pass;
828}
829
830
831static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600832validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200833{
834 std::map<uint32_t, interface_var> inputs;
835 /* we collect builtin inputs, but they will never appear in the VI state --
836 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
837 */
838 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200839 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200840
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600841 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200842
843 /* Build index by location */
844 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200845 if (vi) {
Chia-I Wud50a7d72015-10-26 20:48:51 +0800846 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
Chris Forbes7191cd52015-05-25 11:13:24 +1200847 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
848 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200849
850 auto it_a = attribs.begin();
851 auto it_b = inputs.begin();
852
David Pinedod8f83d82015-04-27 16:36:17 -0600853 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
854 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
855 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200856 auto a_first = a_at_end ? 0 : it_a->first;
857 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600858 if (b_at_end || a_first < b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600859 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 -0600860 "Vertex attribute at location %d not consumed by VS", a_first)) {
861 pass = false;
862 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200863 it_a++;
864 }
David Pinedod8f83d82015-04-27 16:36:17 -0600865 else if (a_at_end || b_first < a_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600866 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 -0600867 "VS consumes input at location %d but not provided", b_first)) {
868 pass = false;
869 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200870 it_b++;
871 }
872 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200873 unsigned attrib_type = get_format_type(it_a->second->format);
874 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
875
876 /* type checking */
877 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
878 char vs_type[1024];
879 describe_type(vs_type, vs, it_b->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600880 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 +1200881 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600882 string_VkFormat(it_a->second->format), a_first, vs_type)) {
883 pass = false;
884 }
Chris Forbes401784b2015-05-04 14:04:24 +1200885 }
886
Chris Forbes6b2ead62015-04-17 10:13:28 +1200887 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200888 it_a++;
889 it_b++;
890 }
891 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200892
893 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200894}
895
896
Chris Forbesee99b9b2015-05-25 11:13:22 +1200897static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600898validate_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 +1200899{
Chia-I Wu08accc62015-07-07 11:50:03 +0800900 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200901 std::map<uint32_t, interface_var> outputs;
902 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200903 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200904
905 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
906
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600907 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200908
909 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
910 * and all color attachment should be UNORM/SNORM/FLOAT.
911 */
912 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200913 if (outputs.size()) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600914 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 -0600915 "Should not have user-defined FS outputs when using broadcast")) {
916 pass = false;
917 }
Chris Forbes3616b462015-04-08 10:37:20 +1200918 }
919
Chia-I Wu08accc62015-07-07 11:50:03 +0800920 for (unsigned i = 0; i < color_formats.size(); i++) {
921 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200922 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600923 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 -0600924 "CB format should not be SINT or UINT when using broadcast")) {
925 pass = false;
926 }
Chris Forbes3616b462015-04-08 10:37:20 +1200927 }
928 }
929
Chris Forbesee99b9b2015-05-25 11:13:22 +1200930 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200931 }
932
933 auto it = outputs.begin();
934 uint32_t attachment = 0;
935
936 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
937 * are currently dense, but the parallel with matching between shader stages is nice.
938 */
939
Chris Forbes9715d4a2015-07-10 09:06:54 +1200940 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200941 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
942 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600943 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 -0600944 "FS writes to output location %d with no matching attachment", it->first)) {
945 pass = false;
946 }
Chris Forbes3616b462015-04-08 10:37:20 +1200947 it++;
948 }
949 else if (it == outputs.end() || it->first > attachment) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600950 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 -0600951 "Attachment %d not written by FS", attachment)) {
952 pass = false;
953 }
Chris Forbes3616b462015-04-08 10:37:20 +1200954 attachment++;
955 }
956 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200957 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800958 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200959
960 /* type checking */
961 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
962 char fs_type[1024];
963 describe_type(fs_type, fs, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600964 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 +1200965 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600966 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
967 pass = false;
968 }
Chris Forbes46d31e52015-05-04 14:20:10 +1200969 }
970
Chris Forbes6b2ead62015-04-17 10:13:28 +1200971 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200972 it++;
973 attachment++;
974 }
975 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200976
977 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200978}
979
980
Chris Forbesf044ec92015-06-05 15:01:08 +1200981struct shader_stage_attributes {
982 char const * const name;
983 bool arrayed_input;
984};
985
986
987static shader_stage_attributes
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600988shader_stage_attribs[] = {
Chris Forbesf044ec92015-06-05 15:01:08 +1200989 { "vertex shader", false },
990 { "tessellation control shader", true },
991 { "tessellation evaluation shader", false },
992 { "geometry shader", true },
993 { "fragment shader", false },
994};
995
996
Chris Forbes556c76c2015-08-14 12:04:59 +1200997static VkDescriptorSetLayoutBinding *
998find_descriptor_binding(std::vector<std::vector<VkDescriptorSetLayoutBinding>*>* layout,
999 std::pair<unsigned, unsigned> slot)
1000{
1001 if (!layout)
1002 return nullptr;
1003
1004 if (slot.first >= layout->size())
1005 return nullptr;
1006
1007 auto set = (*layout)[slot.first];
1008
1009 if (slot.second >= set->size())
1010 return nullptr;
1011
1012 return &(*set)[slot.second];
1013}
1014
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001015static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1016{
1017 uint32_t bit_pos = u_ffs(stage);
1018 return bit_pos-1;
1019}
Chris Forbes556c76c2015-08-14 12:04:59 +12001020
Chris Forbes81874ba2015-06-04 20:23:00 +12001021static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001022validate_graphics_pipeline(layer_data *my_data, VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001023{
Chris Forbesf6800b52015-04-08 10:16:45 +12001024 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1025 * before trying to do anything more: */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001026 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1027 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1028 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbesf6800b52015-04-08 10:16:45 +12001029
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001030 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1031 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Chia-I Wu08accc62015-07-07 11:50:03 +08001032 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001033 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001034 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001035
Chris Forbes7f963832015-05-29 14:55:18 +12001036 loader_platform_thread_lock_mutex(&globalLock);
1037
Tony Barbour92503c62015-07-13 15:28:47 -06001038 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001039 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1040 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001041
Chia-I Wuff3780f2015-10-26 19:52:46 +08001042 // always true; pStage->stage may be revived in a later revision and
1043 // this will make sense again
1044 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 -06001045 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001046 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 +08001047 "Unknown shader stage %d", VK_SHADER_STAGE_VERTEX_BIT)) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001048 pass = false;
1049 }
Chris Forbes6b2ead62015-04-17 10:13:28 +12001050 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001051 else {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001052 struct shader_object *shader = my_data->shader_object_map[pStage->shader];
Chia-I Wuff3780f2015-10-26 19:52:46 +08001053 shaders[get_shader_stage_id(shader->stage)] = shader->module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001054
1055 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001056 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001057 collect_interface_by_descriptor_slot(my_data, dev, shader->module, spv::StorageClassUniform,
Chris Forbes46794b82015-09-18 11:40:23 +12001058 descriptor_uses);
Chris Forbes556c76c2015-08-14 12:04:59 +12001059
Chia-I Wue2fc5522015-10-26 20:04:44 +08001060 auto layout = pCreateInfo->layout != VK_NULL_HANDLE ?
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001061 my_data->pipeline_layout_map[pCreateInfo->layout] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001062
Chris Forbes46794b82015-09-18 11:40:23 +12001063 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001064
Chris Forbes46794b82015-09-18 11:40:23 +12001065 /* find the matching binding */
1066 auto binding = find_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001067
Chris Forbes46794b82015-09-18 11:40:23 +12001068 if (binding == nullptr) {
1069 char type_name[1024];
1070 describe_type(type_name, shader->module, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001071 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 +12001072 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1073 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001074 it->first.first, it->first.second, type_name)) {
1075 pass = false;
1076 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001077 }
1078 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001079 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001080 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001081 }
1082
Chia-I Wu08accc62015-07-07 11:50:03 +08001083 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001084 rp = my_data->render_pass_map[pCreateInfo->renderPass];
Chia-I Wu08accc62015-07-07 11:50:03 +08001085
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001086 vi = pCreateInfo->pVertexInputState;
1087
Chris Forbes280ba2c2015-06-12 11:16:41 +12001088 if (vi) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001089 pass = validate_vi_consistency(my_data, dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001090 }
1091
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001092 if (shaders[vertex_stage]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001093 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001094 }
1095
Chris Forbesf044ec92015-06-05 15:01:08 +12001096 /* TODO: enforce rules about present combinations of shaders */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001097 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1098 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
Chris Forbesf044ec92015-06-05 15:01:08 +12001099
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001100 while (!shaders[producer] && producer != fragment_stage) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001101 producer++;
1102 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001103 }
1104
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001105 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001106 assert(shaders[producer]);
1107 if (shaders[consumer]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001108 pass = validate_interface_between_stages(my_data, dev,
Chris Forbes46794b82015-09-18 11:40:23 +12001109 shaders[producer], shader_stage_attribs[producer].name,
1110 shaders[consumer], shader_stage_attribs[consumer].name,
1111 shader_stage_attribs[consumer].arrayed_input) && pass;
Chris Forbesf044ec92015-06-05 15:01:08 +12001112
1113 producer = consumer;
1114 }
1115 }
1116
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001117 if (shaders[fragment_stage] && rp) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001118 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001119 }
1120
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001121 delete shaders;
1122
Chris Forbes7f963832015-05-29 14:55:18 +12001123 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001124 return pass;
1125}
1126
Jon Ashburnc669cc62015-07-09 15:02:25 -06001127//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001128VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001129vkCreateGraphicsPipelines(VkDevice device,
1130 VkPipelineCache pipelineCache,
1131 uint32_t count,
1132 const VkGraphicsPipelineCreateInfo *pCreateInfos,
1133 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001134{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001135 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes36a372f2015-07-24 13:53:47 +12001136 bool pass = true;
1137 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001138 pass = validate_graphics_pipeline(my_data, device, &pCreateInfos[i]) && pass;
Chris Forbes36a372f2015-07-24 13:53:47 +12001139 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001140
1141 if (pass) {
1142 /* The driver is allowed to crash if passed junk. Only actually create the
1143 * pipeline if we didn't run into any showstoppers above.
1144 */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001145 return my_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001146 }
1147 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001148 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001149 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001150}
1151
1152
Chris Forbesb1dee272015-07-03 13:50:24 +12001153VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
1154{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001155 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
1156 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pDevice);
Chris Forbesb1dee272015-07-03 13:50:24 +12001157 if (result == VK_SUCCESS) {
1158 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001159 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1160 }
1161 return result;
1162}
Chris Forbes39d8d752015-06-04 20:27:09 +12001163
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001164/* hook DextroyDevice to remove tableMap entry */
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001165VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001166{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001167 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001168 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
1169 my_device_data->device_dispatch_table->DestroyDevice(device);
1170 delete my_device_data->device_dispatch_table;
1171 layer_data_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001172}
1173
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001174VkResult VKAPI vkCreateInstance(
1175 const VkInstanceCreateInfo* pCreateInfo,
1176 VkInstance* pInstance)
1177{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001178 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1179 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001180 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
1181
1182 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001183 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1184 my_data->report_data = debug_report_create_instance(
1185 pTable,
1186 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +08001187 pCreateInfo->enabledExtensionNameCount,
Chris Forbesb1dee272015-07-03 13:50:24 +12001188 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001189
Chris Forbesb1dee272015-07-03 13:50:24 +12001190 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001191 }
1192 return result;
1193}
1194
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001195/* hook DestroyInstance to remove tableInstanceMap entry */
Mark Lobodzinski2141f652015-09-07 13:59:43 -06001196VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001197{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001198 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001199 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
1200 my_data->instance_dispatch_table->DestroyInstance(instance);
Chris Forbesb1dee272015-07-03 13:50:24 +12001201
1202 // Clean up logging callback, if any
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001203 while (my_data->logging_callback.size() > 0) {
1204 VkDbgMsgCallback callback = my_data->logging_callback.back();
1205 layer_destroy_msg_callback(my_data->report_data, callback);
1206 my_data->logging_callback.pop_back();
Chris Forbesb1dee272015-07-03 13:50:24 +12001207 }
1208
1209 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001210 delete my_data->instance_dispatch_table;
1211 layer_data_map.erase(key);
1212 if (layer_data_map.empty()) {
1213 // Release mutex when destroying last instance.
1214 loader_platform_thread_delete_mutex(&globalLock);
1215 globalLockInitialized = 0;
1216 }
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001217}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001218
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001219VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001220 VkInstance instance,
1221 VkFlags msgFlags,
1222 const PFN_vkDbgMsgCallback pfnMsgCallback,
1223 void* pUserData,
1224 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001225{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001226 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1227 VkResult res = my_data->instance_dispatch_table->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001228 if (VK_SUCCESS == res) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001229 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1230 }
1231 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001232}
1233
1234VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001235 VkInstance instance,
1236 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001237{
Chris Forbesb1dee272015-07-03 13:50:24 +12001238 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001239 VkResult res = my_data->instance_dispatch_table->DbgDestroyMsgCallback(instance, msgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001240 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1241 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001242}
1243
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001244VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001245{
Chris Forbesb1dee272015-07-03 13:50:24 +12001246 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001247 return NULL;
1248
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001249 layer_data *my_data;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001250 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001251 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001252 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
1253 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
1254 my_data->device_dispatch_table = new VkLayerDispatchTable;
1255 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001256 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001257 }
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001258 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Jon Ashburn8fd08252015-05-28 16:25:02 -06001259
Chris Forbes2778f302015-04-02 13:22:31 +13001260#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001261 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001262 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001263
Chris Forbesb1dee272015-07-03 13:50:24 +12001264 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001265 ADD_HOOK(vkCreateShaderModule);
Chris Forbes2778f302015-04-02 13:22:31 +13001266 ADD_HOOK(vkCreateShader);
Chia-I Wu08accc62015-07-07 11:50:03 +08001267 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001268 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001269 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001270 ADD_HOOK(vkCreateDescriptorSetLayout);
1271 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001272#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001273
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001274 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001275 {
1276 if (pTable->GetDeviceProcAddr == NULL)
1277 return NULL;
1278 return pTable->GetDeviceProcAddr(dev, funcName);
1279 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001280}
1281
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001282VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001283{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001284 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001285
Chris Forbesb1dee272015-07-03 13:50:24 +12001286 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001287 return NULL;
1288
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001289 layer_data *my_data;
Chris Forbesb1dee272015-07-03 13:50:24 +12001290 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001291 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
1292 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
1293 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
1294 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001295 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001296 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001297#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001298 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001299 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001300
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001301 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001302 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001303 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1304 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1305 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1306 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001307#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001308
Chris Forbesb1dee272015-07-03 13:50:24 +12001309
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001310 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001311 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001312 if (fptr)
1313 return fptr;
1314
Chris Forbesb1dee272015-07-03 13:50:24 +12001315 {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001316 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001317 if (pTable->GetInstanceProcAddr == NULL)
1318 return NULL;
1319 return pTable->GetInstanceProcAddr(instance, funcName);
1320 }
Chris Forbes2778f302015-04-02 13:22:31 +13001321}