blob: 9b2b54bf37a9f05bb611a376cf5e6d7f75cee5f2 [file] [log] [blame]
Chris Forbes2778f302015-04-02 13:22:31 +13001/*
Chris Forbes2778f302015-04-02 13:22:31 +13002 *
3 * Copyright (C) 2015 LunarG, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23#include <string.h>
24#include <stdlib.h>
25#include <assert.h>
Chris Forbes06e8fc32015-04-13 12:14:52 +120026#include <map>
Chris Forbes2778f302015-04-02 13:22:31 +130027#include <unordered_map>
Chia-I Wud46e6ae2015-10-31 00:31:16 +080028#include <unordered_set>
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;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060047struct render_pass;
Chris Forbes2778f302015-04-02 13:22:31 +130048
Cody Northrop55443ef2015-09-28 15:09:32 -060049struct layer_data {
Chris Forbesb1dee272015-07-03 13:50:24 +120050 debug_report_data *report_data;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -060051 std::vector<VkDbgMsgCallback> logging_callback;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060052 VkLayerDispatchTable* device_dispatch_table;
53 VkLayerInstanceDispatchTable* instance_dispatch_table;
54
55 std::unordered_map<VkShaderModule, shader_module *> shader_module_map;
Chia-I Wud46e6ae2015-10-31 00:31:16 +080056 std::unordered_map<VkDescriptorSetLayout, std::unordered_set<uint32_t>*> descriptor_set_layout_map;
57 std::unordered_map<VkPipelineLayout, std::vector<std::unordered_set<uint32_t>*>*> pipeline_layout_map;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060058 std::unordered_map<VkRenderPass, render_pass *> render_pass_map;
Cody Northrop55443ef2015-09-28 15:09:32 -060059
60 layer_data() :
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060061 report_data(nullptr),
62 device_dispatch_table(nullptr),
63 instance_dispatch_table(nullptr)
Cody Northrop55443ef2015-09-28 15:09:32 -060064 {};
65};
Chris Forbesb1dee272015-07-03 13:50:24 +120066
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060067static void
68build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index);
Chris Forbesb1dee272015-07-03 13:50:24 +120069
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060070struct shader_module {
71 /* the spirv image itself */
72 std::vector<uint32_t> words;
73 /* a mapping of <id> to the first word of its def. this is useful because walking type
74 * trees requires jumping all over the instruction stream.
75 */
76 std::unordered_map<unsigned, unsigned> type_def_index;
77
78 shader_module(VkShaderModuleCreateInfo const *pCreateInfo) :
79 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
80 type_def_index() {
81
82 build_type_def_index(words, type_def_index);
83 }
84};
85
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060086struct render_pass {
87 std::vector<std::vector<VkFormat>> subpass_color_formats;
88
89 render_pass(VkRenderPassCreateInfo const *pCreateInfo)
90 {
91 uint32_t i;
92
93 subpass_color_formats.reserve(pCreateInfo->subpassCount);
94 for (i = 0; i < pCreateInfo->subpassCount; i++) {
95 const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i];
96 std::vector<VkFormat> color_formats;
97 uint32_t j;
98
Chia-I Wud50a7d72015-10-26 20:48:51 +080099 color_formats.reserve(subpass->colorAttachmentCount);
100 for (j = 0; j < subpass->colorAttachmentCount; j++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600101 const uint32_t att = subpass->pColorAttachments[j].attachment;
102 const VkFormat format = pCreateInfo->pAttachments[att].format;
103
104 color_formats.push_back(pCreateInfo->pAttachments[att].format);
105 }
106
107 subpass_color_formats.push_back(color_formats);
108 }
109 }
110};
111
112static std::unordered_map<void *, layer_data *> layer_data_map;
Chris Forbesb1dee272015-07-03 13:50:24 +1200113
114template layer_data *get_my_data_ptr<layer_data>(
115 void *data_key,
116 std::unordered_map<void *, layer_data *> &data_map);
117
Chris Forbesb6b8c462015-04-15 06:59:41 +1200118static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +1200119// TODO : This can be much smarter, using separate locks for separate global data
120static int globalLockInitialized = 0;
121static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200122
Chris Forbes2b8493a2015-08-12 15:03:22 +1200123VK_LAYER_EXPORT VkResult VKAPI vkCreateDescriptorSetLayout(
124 VkDevice device,
125 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800126 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200127 VkDescriptorSetLayout* pSetLayout)
128{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600129 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200130 /* stash a copy of the layout bindings */
Chia-I Wuf7458c52015-10-26 21:10:41 +0800131 VkResult result = my_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200132
133 if (VK_SUCCESS == result) {
134 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600135 auto& bindings = my_data->descriptor_set_layout_map[*pSetLayout];
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800136 bindings = new std::unordered_set<uint32_t>();
137 bindings->reserve(pCreateInfo->bindingCount);
138 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++)
139 bindings->insert(pCreateInfo->pBinding[i].binding);
140
Chris Forbes2b8493a2015-08-12 15:03:22 +1200141 loader_platform_thread_unlock_mutex(&globalLock);
142 }
143
144 return result;
145}
146
Chris Forbes2b8493a2015-08-12 15:03:22 +1200147VK_LAYER_EXPORT VkResult VKAPI vkCreatePipelineLayout(
148 VkDevice device,
149 const VkPipelineLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800150 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200151 VkPipelineLayout* pPipelineLayout)
152{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600153 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800154 VkResult result = my_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200155
156 if (VK_SUCCESS == result) {
157 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600158 auto& layouts = my_data->pipeline_layout_map[*pPipelineLayout];
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800159 layouts = new std::vector<std::unordered_set<uint32_t>*>();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800160 layouts->reserve(pCreateInfo->setLayoutCount);
161 for (unsigned i = 0; i < pCreateInfo->setLayoutCount; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600162 layouts->push_back(my_data->descriptor_set_layout_map[pCreateInfo->pSetLayouts[i]]);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200163 }
164 loader_platform_thread_unlock_mutex(&globalLock);
165 }
166
167 return result;
168}
169
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200170static void
171build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
172{
173 unsigned int const *code = (unsigned int const *)&words[0];
174 size_t size = words.size();
175
176 unsigned word = 5;
177 while (word < size) {
178 unsigned opcode = code[word] & 0x0ffffu;
179 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
180
181 switch (opcode) {
182 case spv::OpTypeVoid:
183 case spv::OpTypeBool:
184 case spv::OpTypeInt:
185 case spv::OpTypeFloat:
186 case spv::OpTypeVector:
187 case spv::OpTypeMatrix:
GregF7c45bed2015-07-21 17:22:50 -0600188 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200189 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600190 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200191 case spv::OpTypeArray:
192 case spv::OpTypeRuntimeArray:
193 case spv::OpTypeStruct:
194 case spv::OpTypeOpaque:
195 case spv::OpTypePointer:
196 case spv::OpTypeFunction:
197 case spv::OpTypeEvent:
198 case spv::OpTypeDeviceEvent:
199 case spv::OpTypeReserveId:
200 case spv::OpTypeQueue:
201 case spv::OpTypePipe:
202 type_def_index[code[word+1]] = word;
203 break;
204
205 default:
206 /* We only care about type definitions */
207 break;
208 }
209
210 word += oplen;
211 }
212}
213
Chris Forbes46794b82015-09-18 11:40:23 +1200214bool
215shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo)
216{
217 uint32_t *words = (uint32_t *)pCreateInfo->pCode;
Courtney Goeltzenleuchter8f8367e2015-10-07 08:38:30 -0600218 size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
Chris Forbes46794b82015-09-18 11:40:23 +1200219
220 /* Just validate that the header makes sense. */
221 return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
222}
223
Chris Forbesb6b8c462015-04-15 06:59:41 +1200224static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200225init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200226{
Chris Forbesb1dee272015-07-03 13:50:24 +1200227 uint32_t report_flags = 0;
228 uint32_t debug_action = 0;
229 FILE *log_output = NULL;
230 const char *option_str;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600231 VkDbgMsgCallback callback;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200232 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200233 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
234 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200235
Chris Forbesb1dee272015-07-03 13:50:24 +1200236 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200237 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200238 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600239 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600240 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &callback);
241 my_data->logging_callback.push_back(callback);
242 }
243
244 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
245 layer_create_msg_callback(my_data->report_data, report_flags, win32_debug_output_msg, NULL, &callback);
246 my_data->logging_callback.push_back(callback);
Chris Forbesb1dee272015-07-03 13:50:24 +1200247 }
248
249 if (!globalLockInitialized)
250 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200251 loader_platform_thread_create_mutex(&globalLock);
252 globalLockInitialized = 1;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200253 }
254}
255
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600256static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600257 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600258 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600259 VK_API_VERSION,
260 VK_MAKE_VERSION(0, 1, 0),
261 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600262 }
Chris Forbes2778f302015-04-02 13:22:31 +1300263};
264
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600265VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600266 const char *pLayerName,
267 uint32_t *pCount,
268 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300269{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600270 /* shader checker does not have any global extensions */
271 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300272}
273
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600274VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600275 uint32_t *pCount,
276 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600277{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600278 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
279 shader_checker_global_layers,
280 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600281}
282
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600283VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600284 VkPhysicalDevice physicalDevice,
285 const char* pLayerName,
286 uint32_t* pCount,
287 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600288{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600289 /* Shader checker does not have any physical device extensions */
Jon Ashburn751c4842015-11-02 17:37:20 -0700290 if (pLayerName == NULL) {
291 dispatch_key key = get_dispatch_key(physicalDevice);
292 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -0700293 return my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(
Jon Ashburn751c4842015-11-02 17:37:20 -0700294 physicalDevice,
295 NULL,
296 pCount,
297 pProperties);
298 } else {
299 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
300 }
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600301}
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,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800605 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600606 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300607{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600608 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Michael Lentine8ea29962015-11-03 16:18:46 -0800609 bool skip_call = false;
Chris Forbes46794b82015-09-18 11:40:23 +1200610 if (!shader_is_spirv(pCreateInfo)) {
Michael Lentine8ea29962015-11-03 16:18:46 -0800611 skip_call |= log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE,
Chris Forbes46794b82015-09-18 11:40:23 +1200612 /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
613 "Shader is not SPIR-V");
Chris Forbes46794b82015-09-18 11:40:23 +1200614 }
615
Michael Lentine8ea29962015-11-03 16:18:46 -0800616 if (skip_call)
617 return VK_ERROR_VALIDATION_FAILED;
618
Chia-I Wuf7458c52015-10-26 21:10:41 +0800619 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200620
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600621 if (res == VK_SUCCESS) {
622 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600623 my_data->shader_module_map[*pShaderModule] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600624 loader_platform_thread_unlock_mutex(&globalLock);
625 }
Chris Forbes2778f302015-04-02 13:22:31 +1300626 return res;
627}
628
Chia-I Wu08accc62015-07-07 11:50:03 +0800629VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
630 VkDevice device,
631 const VkRenderPassCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800632 const VkAllocationCallbacks* pAllocator,
Chia-I Wu08accc62015-07-07 11:50:03 +0800633 VkRenderPass *pRenderPass)
634{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600635 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800636 VkResult res = my_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800637
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600638 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600639 my_data->render_pass_map[*pRenderPass] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800640 loader_platform_thread_unlock_mutex(&globalLock);
641 return res;
642}
643
Chris Forbesee99b9b2015-05-25 11:13:22 +1200644static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600645validate_interface_between_stages(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200646 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600647 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200648 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200649{
650 std::map<uint32_t, interface_var> outputs;
651 std::map<uint32_t, interface_var> inputs;
652
653 std::map<uint32_t, interface_var> builtin_outputs;
654 std::map<uint32_t, interface_var> builtin_inputs;
655
Chris Forbesee99b9b2015-05-25 11:13:22 +1200656 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200657
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600658 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
659 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200660
661 auto a_it = outputs.begin();
662 auto b_it = inputs.begin();
663
664 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600665 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
666 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
667 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200668 auto a_first = a_at_end ? 0 : a_it->first;
669 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600670
Mike Stroyan19a6de22015-09-10 14:10:25 -0600671 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600672 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 -0600673 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
674 pass = false;
675 }
Chris Forbes41002452015-04-08 10:19:16 +1200676 a_it++;
677 }
David Pinedod8f83d82015-04-27 16:36:17 -0600678 else if (a_at_end || a_first > b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600679 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 -0600680 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
681 pass = false;
682 }
Chris Forbes41002452015-04-08 10:19:16 +1200683 b_it++;
684 }
685 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200686 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 +1200687 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200688 }
689 else {
690 char producer_type[1024];
691 char consumer_type[1024];
692 describe_type(producer_type, producer, a_it->second.type_id);
693 describe_type(consumer_type, consumer, b_it->second.type_id);
694
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600695 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 -0600696 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
Chris Forbesee99b9b2015-05-25 11:13:22 +1200697 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600698 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200699 }
Chris Forbes41002452015-04-08 10:19:16 +1200700 a_it++;
701 b_it++;
702 }
703 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200704
705 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200706}
707
708
Chris Forbes3616b462015-04-08 10:37:20 +1200709enum FORMAT_TYPE {
710 FORMAT_TYPE_UNDEFINED,
711 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
712 FORMAT_TYPE_SINT,
713 FORMAT_TYPE_UINT,
714};
715
716
717static unsigned
718get_format_type(VkFormat fmt) {
719 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800720 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200721 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800722 case VK_FORMAT_R8_SINT:
723 case VK_FORMAT_R8G8_SINT:
724 case VK_FORMAT_R8G8B8_SINT:
725 case VK_FORMAT_R8G8B8A8_SINT:
726 case VK_FORMAT_R16_SINT:
727 case VK_FORMAT_R16G16_SINT:
728 case VK_FORMAT_R16G16B16_SINT:
729 case VK_FORMAT_R16G16B16A16_SINT:
730 case VK_FORMAT_R32_SINT:
731 case VK_FORMAT_R32G32_SINT:
732 case VK_FORMAT_R32G32B32_SINT:
733 case VK_FORMAT_R32G32B32A32_SINT:
734 case VK_FORMAT_B8G8R8_SINT:
735 case VK_FORMAT_B8G8R8A8_SINT:
736 case VK_FORMAT_R10G10B10A2_SINT:
737 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200738 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800739 case VK_FORMAT_R8_UINT:
740 case VK_FORMAT_R8G8_UINT:
741 case VK_FORMAT_R8G8B8_UINT:
742 case VK_FORMAT_R8G8B8A8_UINT:
743 case VK_FORMAT_R16_UINT:
744 case VK_FORMAT_R16G16_UINT:
745 case VK_FORMAT_R16G16B16_UINT:
746 case VK_FORMAT_R16G16B16A16_UINT:
747 case VK_FORMAT_R32_UINT:
748 case VK_FORMAT_R32G32_UINT:
749 case VK_FORMAT_R32G32B32_UINT:
750 case VK_FORMAT_R32G32B32A32_UINT:
751 case VK_FORMAT_B8G8R8_UINT:
752 case VK_FORMAT_B8G8R8A8_UINT:
753 case VK_FORMAT_R10G10B10A2_UINT:
754 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200755 return FORMAT_TYPE_UINT;
756 default:
757 return FORMAT_TYPE_FLOAT;
758 }
759}
760
761
Chris Forbes156a1162015-05-04 14:04:06 +1200762/* characterizes a SPIR-V type appearing in an interface to a FF stage,
763 * for comparison to a VkFormat's characterization above. */
764static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600765get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200766{
767 auto type_def_it = src->type_def_index.find(type);
768
769 if (type_def_it == src->type_def_index.end()) {
770 return FORMAT_TYPE_UNDEFINED;
771 }
772
773 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
774 unsigned opcode = code[0] & 0x0ffffu;
775 switch (opcode) {
776 case spv::OpTypeInt:
777 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
778 case spv::OpTypeFloat:
779 return FORMAT_TYPE_FLOAT;
780 case spv::OpTypeVector:
781 return get_fundamental_type(src, code[2]);
782 case spv::OpTypeMatrix:
783 return get_fundamental_type(src, code[2]);
784 case spv::OpTypeArray:
785 return get_fundamental_type(src, code[2]);
786 case spv::OpTypePointer:
787 return get_fundamental_type(src, code[3]);
788 default:
789 return FORMAT_TYPE_UNDEFINED;
790 }
791}
792
793
Chris Forbesee99b9b2015-05-25 11:13:22 +1200794static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600795validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200796{
797 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
798 * each binding should be specified only once.
799 */
800 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200801 bool pass = true;
802
Chia-I Wud50a7d72015-10-26 20:48:51 +0800803 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes280ba2c2015-06-12 11:16:41 +1200804 auto desc = &vi->pVertexBindingDescriptions[i];
805 auto & binding = bindings[desc->binding];
806 if (binding) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600807 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 -0600808 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
809 pass = false;
810 }
Chris Forbes280ba2c2015-06-12 11:16:41 +1200811 }
812 else {
813 binding = desc;
814 }
815 }
816
817 return pass;
818}
819
820
821static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600822validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200823{
824 std::map<uint32_t, interface_var> inputs;
825 /* we collect builtin inputs, but they will never appear in the VI state --
826 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
827 */
828 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200829 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200830
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600831 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200832
833 /* Build index by location */
834 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200835 if (vi) {
Chia-I Wud50a7d72015-10-26 20:48:51 +0800836 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
Chris Forbes7191cd52015-05-25 11:13:24 +1200837 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
838 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200839
840 auto it_a = attribs.begin();
841 auto it_b = inputs.begin();
842
David Pinedod8f83d82015-04-27 16:36:17 -0600843 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
844 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
845 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200846 auto a_first = a_at_end ? 0 : it_a->first;
847 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600848 if (b_at_end || a_first < b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600849 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 -0600850 "Vertex attribute at location %d not consumed by VS", a_first)) {
851 pass = false;
852 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200853 it_a++;
854 }
David Pinedod8f83d82015-04-27 16:36:17 -0600855 else if (a_at_end || b_first < a_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600856 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 -0600857 "VS consumes input at location %d but not provided", b_first)) {
858 pass = false;
859 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200860 it_b++;
861 }
862 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200863 unsigned attrib_type = get_format_type(it_a->second->format);
864 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
865
866 /* type checking */
867 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
868 char vs_type[1024];
869 describe_type(vs_type, vs, it_b->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600870 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 +1200871 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600872 string_VkFormat(it_a->second->format), a_first, vs_type)) {
873 pass = false;
874 }
Chris Forbes401784b2015-05-04 14:04:24 +1200875 }
876
Chris Forbes6b2ead62015-04-17 10:13:28 +1200877 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200878 it_a++;
879 it_b++;
880 }
881 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200882
883 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200884}
885
886
Chris Forbesee99b9b2015-05-25 11:13:22 +1200887static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600888validate_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 +1200889{
Chia-I Wu08accc62015-07-07 11:50:03 +0800890 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200891 std::map<uint32_t, interface_var> outputs;
892 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200893 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200894
895 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
896
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600897 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200898
899 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
900 * and all color attachment should be UNORM/SNORM/FLOAT.
901 */
902 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200903 if (outputs.size()) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600904 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 -0600905 "Should not have user-defined FS outputs when using broadcast")) {
906 pass = false;
907 }
Chris Forbes3616b462015-04-08 10:37:20 +1200908 }
909
Chia-I Wu08accc62015-07-07 11:50:03 +0800910 for (unsigned i = 0; i < color_formats.size(); i++) {
911 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200912 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600913 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 -0600914 "CB format should not be SINT or UINT when using broadcast")) {
915 pass = false;
916 }
Chris Forbes3616b462015-04-08 10:37:20 +1200917 }
918 }
919
Chris Forbesee99b9b2015-05-25 11:13:22 +1200920 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200921 }
922
923 auto it = outputs.begin();
924 uint32_t attachment = 0;
925
926 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
927 * are currently dense, but the parallel with matching between shader stages is nice.
928 */
929
Chris Forbes9715d4a2015-07-10 09:06:54 +1200930 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200931 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
932 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600933 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 -0600934 "FS writes to output location %d with no matching attachment", it->first)) {
935 pass = false;
936 }
Chris Forbes3616b462015-04-08 10:37:20 +1200937 it++;
938 }
939 else if (it == outputs.end() || it->first > attachment) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600940 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 -0600941 "Attachment %d not written by FS", attachment)) {
942 pass = false;
943 }
Chris Forbes3616b462015-04-08 10:37:20 +1200944 attachment++;
945 }
946 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200947 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800948 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200949
950 /* type checking */
951 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
952 char fs_type[1024];
953 describe_type(fs_type, fs, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600954 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 +1200955 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600956 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
957 pass = false;
958 }
Chris Forbes46d31e52015-05-04 14:20:10 +1200959 }
960
Chris Forbes6b2ead62015-04-17 10:13:28 +1200961 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200962 it++;
963 attachment++;
964 }
965 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200966
967 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200968}
969
970
Chris Forbesf044ec92015-06-05 15:01:08 +1200971struct shader_stage_attributes {
972 char const * const name;
973 bool arrayed_input;
974};
975
976
977static shader_stage_attributes
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600978shader_stage_attribs[] = {
Chris Forbesf044ec92015-06-05 15:01:08 +1200979 { "vertex shader", false },
980 { "tessellation control shader", true },
981 { "tessellation evaluation shader", false },
982 { "geometry shader", true },
983 { "fragment shader", false },
984};
985
986
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800987static bool
988has_descriptor_binding(std::vector<std::unordered_set<uint32_t>*>* layout,
989 std::pair<unsigned, unsigned> slot)
Chris Forbes556c76c2015-08-14 12:04:59 +1200990{
991 if (!layout)
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800992 return false;
Chris Forbes556c76c2015-08-14 12:04:59 +1200993
994 if (slot.first >= layout->size())
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800995 return false;
Chris Forbes556c76c2015-08-14 12:04:59 +1200996
997 auto set = (*layout)[slot.first];
998
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800999 return (set->find(slot.second) != set->end());
Chris Forbes556c76c2015-08-14 12:04:59 +12001000}
1001
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001002static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1003{
1004 uint32_t bit_pos = u_ffs(stage);
1005 return bit_pos-1;
1006}
Chris Forbes556c76c2015-08-14 12:04:59 +12001007
Chris Forbes81874ba2015-06-04 20:23:00 +12001008static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001009validate_graphics_pipeline(layer_data *my_data, VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001010{
Chris Forbesf6800b52015-04-08 10:16:45 +12001011 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1012 * before trying to do anything more: */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001013 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1014 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1015 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbesf6800b52015-04-08 10:16:45 +12001016
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001017 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1018 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Chia-I Wu08accc62015-07-07 11:50:03 +08001019 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001020 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001021 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001022
Chris Forbes7f963832015-05-29 14:55:18 +12001023 loader_platform_thread_lock_mutex(&globalLock);
1024
Tony Barbour92503c62015-07-13 15:28:47 -06001025 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001026 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1027 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001028
Chia-I Wu28e06912015-10-31 00:31:16 +08001029 if ((pStage->stage & (VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_GEOMETRY_BIT | VK_SHADER_STAGE_FRAGMENT_BIT
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001030 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001031 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 Wu28e06912015-10-31 00:31:16 +08001032 "Unknown shader stage %d", pStage->stage)) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001033 pass = false;
1034 }
Chris Forbes6b2ead62015-04-17 10:13:28 +12001035 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001036 else {
Chia-I Wu28e06912015-10-31 00:31:16 +08001037 shader_module *module = my_data->shader_module_map[pStage->module];
1038 shaders[get_shader_stage_id(pStage->stage)] = module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001039
1040 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001041 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
Chia-I Wu28e06912015-10-31 00:31:16 +08001042 collect_interface_by_descriptor_slot(my_data, dev, module, spv::StorageClassUniform,
Chris Forbes46794b82015-09-18 11:40:23 +12001043 descriptor_uses);
Chris Forbes556c76c2015-08-14 12:04:59 +12001044
Chia-I Wue2fc5522015-10-26 20:04:44 +08001045 auto layout = pCreateInfo->layout != VK_NULL_HANDLE ?
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001046 my_data->pipeline_layout_map[pCreateInfo->layout] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001047
Chris Forbes46794b82015-09-18 11:40:23 +12001048 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001049
Chris Forbes46794b82015-09-18 11:40:23 +12001050 /* find the matching binding */
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001051 auto found = has_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001052
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001053 if (!found) {
Chris Forbes46794b82015-09-18 11:40:23 +12001054 char type_name[1024];
Chia-I Wu28e06912015-10-31 00:31:16 +08001055 describe_type(type_name, module, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001056 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 +12001057 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1058 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001059 it->first.first, it->first.second, type_name)) {
1060 pass = false;
1061 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001062 }
1063 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001064 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001065 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001066 }
1067
Chia-I Wu08accc62015-07-07 11:50:03 +08001068 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001069 rp = my_data->render_pass_map[pCreateInfo->renderPass];
Chia-I Wu08accc62015-07-07 11:50:03 +08001070
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001071 vi = pCreateInfo->pVertexInputState;
1072
Chris Forbes280ba2c2015-06-12 11:16:41 +12001073 if (vi) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001074 pass = validate_vi_consistency(my_data, dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001075 }
1076
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001077 if (shaders[vertex_stage]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001078 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001079 }
1080
Chris Forbesf044ec92015-06-05 15:01:08 +12001081 /* TODO: enforce rules about present combinations of shaders */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001082 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1083 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
Chris Forbesf044ec92015-06-05 15:01:08 +12001084
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001085 while (!shaders[producer] && producer != fragment_stage) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001086 producer++;
1087 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001088 }
1089
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001090 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001091 assert(shaders[producer]);
1092 if (shaders[consumer]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001093 pass = validate_interface_between_stages(my_data, dev,
Chris Forbes46794b82015-09-18 11:40:23 +12001094 shaders[producer], shader_stage_attribs[producer].name,
1095 shaders[consumer], shader_stage_attribs[consumer].name,
1096 shader_stage_attribs[consumer].arrayed_input) && pass;
Chris Forbesf044ec92015-06-05 15:01:08 +12001097
1098 producer = consumer;
1099 }
1100 }
1101
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001102 if (shaders[fragment_stage] && rp) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001103 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001104 }
1105
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001106 delete shaders;
1107
Chris Forbes7f963832015-05-29 14:55:18 +12001108 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001109 return pass;
1110}
1111
Jon Ashburnc669cc62015-07-09 15:02:25 -06001112//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001113VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001114vkCreateGraphicsPipelines(VkDevice device,
1115 VkPipelineCache pipelineCache,
1116 uint32_t count,
1117 const VkGraphicsPipelineCreateInfo *pCreateInfos,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001118 const VkAllocationCallbacks* pAllocator,
Jon Ashburnc669cc62015-07-09 15:02:25 -06001119 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001120{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001121 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes36a372f2015-07-24 13:53:47 +12001122 bool pass = true;
1123 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001124 pass = validate_graphics_pipeline(my_data, device, &pCreateInfos[i]) && pass;
Chris Forbes36a372f2015-07-24 13:53:47 +12001125 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001126
1127 if (pass) {
1128 /* The driver is allowed to crash if passed junk. Only actually create the
1129 * pipeline if we didn't run into any showstoppers above.
1130 */
Chia-I Wuf7458c52015-10-26 21:10:41 +08001131 return my_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001132 }
1133 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001134 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001135 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001136}
1137
1138
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001139VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Chris Forbesb1dee272015-07-03 13:50:24 +12001140{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001141 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001142 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Chris Forbesb1dee272015-07-03 13:50:24 +12001143 if (result == VK_SUCCESS) {
1144 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001145 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1146 }
1147 return result;
1148}
Chris Forbes39d8d752015-06-04 20:27:09 +12001149
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001150/* hook DextroyDevice to remove tableMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001151VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001152{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001153 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001154 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001155 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001156 delete my_device_data->device_dispatch_table;
1157 layer_data_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001158}
1159
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001160VkResult VKAPI vkCreateInstance(
1161 const VkInstanceCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001162 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001163 VkInstance* pInstance)
1164{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001165 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1166 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +08001167 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001168
1169 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001170 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1171 my_data->report_data = debug_report_create_instance(
1172 pTable,
1173 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +08001174 pCreateInfo->enabledExtensionNameCount,
Chris Forbesb1dee272015-07-03 13:50:24 +12001175 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001176
Chris Forbesb1dee272015-07-03 13:50:24 +12001177 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001178 }
1179 return result;
1180}
1181
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001182/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001183VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001184{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001185 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001186 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001187 my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
Chris Forbesb1dee272015-07-03 13:50:24 +12001188
1189 // Clean up logging callback, if any
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001190 while (my_data->logging_callback.size() > 0) {
1191 VkDbgMsgCallback callback = my_data->logging_callback.back();
1192 layer_destroy_msg_callback(my_data->report_data, callback);
1193 my_data->logging_callback.pop_back();
Chris Forbesb1dee272015-07-03 13:50:24 +12001194 }
1195
1196 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001197 delete my_data->instance_dispatch_table;
1198 layer_data_map.erase(key);
1199 if (layer_data_map.empty()) {
1200 // Release mutex when destroying last instance.
1201 loader_platform_thread_delete_mutex(&globalLock);
1202 globalLockInitialized = 0;
1203 }
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001204}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001205
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001206VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001207 VkInstance instance,
1208 VkFlags msgFlags,
1209 const PFN_vkDbgMsgCallback pfnMsgCallback,
1210 void* pUserData,
1211 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001212{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001213 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1214 VkResult res = my_data->instance_dispatch_table->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001215 if (VK_SUCCESS == res) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001216 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1217 }
1218 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001219}
1220
1221VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001222 VkInstance instance,
1223 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001224{
Chris Forbesb1dee272015-07-03 13:50:24 +12001225 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001226 VkResult res = my_data->instance_dispatch_table->DbgDestroyMsgCallback(instance, msgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001227 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1228 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001229}
1230
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001231VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001232{
Chris Forbesb1dee272015-07-03 13:50:24 +12001233 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001234 return NULL;
1235
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001236 layer_data *my_data;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001237 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001238 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001239 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
1240 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
1241 my_data->device_dispatch_table = new VkLayerDispatchTable;
1242 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001243 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001244 }
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001245 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Jon Ashburn8fd08252015-05-28 16:25:02 -06001246
Chris Forbes2778f302015-04-02 13:22:31 +13001247#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001248 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001249 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001250
Chris Forbesb1dee272015-07-03 13:50:24 +12001251 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001252 ADD_HOOK(vkCreateShaderModule);
Chia-I Wu08accc62015-07-07 11:50:03 +08001253 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001254 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001255 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001256 ADD_HOOK(vkCreateDescriptorSetLayout);
1257 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001258#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001259
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001260 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001261 {
1262 if (pTable->GetDeviceProcAddr == NULL)
1263 return NULL;
1264 return pTable->GetDeviceProcAddr(dev, funcName);
1265 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001266}
1267
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001268VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001269{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001270 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001271
Chris Forbesb1dee272015-07-03 13:50:24 +12001272 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001273 return NULL;
1274
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001275 layer_data *my_data;
Chris Forbesb1dee272015-07-03 13:50:24 +12001276 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001277 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
1278 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
1279 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
1280 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001281 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001282 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001283#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001284 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001285 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001286
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001287 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001288 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001289 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1290 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1291 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1292 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001293#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001294
Chris Forbesb1dee272015-07-03 13:50:24 +12001295
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001296 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001297 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001298 if (fptr)
1299 return fptr;
1300
Chris Forbesb1dee272015-07-03 13:50:24 +12001301 {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001302 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001303 if (pTable->GetInstanceProcAddr == NULL)
1304 return NULL;
1305 return pTable->GetInstanceProcAddr(instance, funcName);
1306 }
Chris Forbes2778f302015-04-02 13:22:31 +13001307}