blob: 715fb115271ac3bca18429b06f99d954ce35e341 [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>
Chia-I Wud46e6ae2015-10-31 00:31:16 +080029#include <unordered_set>
Chris Forbes41002452015-04-08 10:19:16 +120030#include <map>
Chris Forbes3b1c4212015-04-08 10:11:59 +120031#include <vector>
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -060032#include <string>
Tobin Ehlisb80b4252015-09-01 11:59:36 -060033#include <iostream>
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060034#include "vk_loader_platform.h"
Chris Forbes2778f302015-04-02 13:22:31 +130035#include "vk_dispatch_table_helper.h"
Tobin Ehlis0c6f9ee2015-07-03 09:42:57 -060036#include "vk_layer.h"
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -060037#include "vk_layer_utils.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060038#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060039#include "vk_layer_table.h"
Chris Forbes401784b2015-05-04 14:04:24 +120040#include "vk_enum_string_helper.h"
Chris Forbes6b2ead62015-04-17 10:13:28 +120041#include "shader_checker.h"
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -060042#include "vk_layer_extension_utils.h"
Chris Forbes2778f302015-04-02 13:22:31 +130043
GregF7c45bed2015-07-21 17:22:50 -060044#include "spirv/spirv.hpp"
Chris Forbes2778f302015-04-02 13:22:31 +130045
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060046// fwd decls
47struct shader_module;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060048struct 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;
Chia-I Wud46e6ae2015-10-31 00:31:16 +080057 std::unordered_map<VkDescriptorSetLayout, std::unordered_set<uint32_t>*> descriptor_set_layout_map;
58 std::unordered_map<VkPipelineLayout, std::vector<std::unordered_set<uint32_t>*>*> pipeline_layout_map;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060059 std::unordered_map<VkRenderPass, render_pass *> render_pass_map;
Cody Northrop55443ef2015-09-28 15:09:32 -060060
61 layer_data() :
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060062 report_data(nullptr),
63 device_dispatch_table(nullptr),
64 instance_dispatch_table(nullptr)
Cody Northrop55443ef2015-09-28 15:09:32 -060065 {};
66};
Chris Forbesb1dee272015-07-03 13:50:24 +120067
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060068static void
69build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index);
Chris Forbesb1dee272015-07-03 13:50:24 +120070
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060071struct shader_module {
72 /* the spirv image itself */
73 std::vector<uint32_t> words;
74 /* a mapping of <id> to the first word of its def. this is useful because walking type
75 * trees requires jumping all over the instruction stream.
76 */
77 std::unordered_map<unsigned, unsigned> type_def_index;
78
79 shader_module(VkShaderModuleCreateInfo const *pCreateInfo) :
80 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
81 type_def_index() {
82
83 build_type_def_index(words, type_def_index);
84 }
85};
86
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060087struct render_pass {
88 std::vector<std::vector<VkFormat>> subpass_color_formats;
89
90 render_pass(VkRenderPassCreateInfo const *pCreateInfo)
91 {
92 uint32_t i;
93
94 subpass_color_formats.reserve(pCreateInfo->subpassCount);
95 for (i = 0; i < pCreateInfo->subpassCount; i++) {
96 const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i];
97 std::vector<VkFormat> color_formats;
98 uint32_t j;
99
Chia-I Wud50a7d72015-10-26 20:48:51 +0800100 color_formats.reserve(subpass->colorAttachmentCount);
101 for (j = 0; j < subpass->colorAttachmentCount; j++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600102 const uint32_t att = subpass->pColorAttachments[j].attachment;
103 const VkFormat format = pCreateInfo->pAttachments[att].format;
104
105 color_formats.push_back(pCreateInfo->pAttachments[att].format);
106 }
107
108 subpass_color_formats.push_back(color_formats);
109 }
110 }
111};
112
113static std::unordered_map<void *, layer_data *> layer_data_map;
Chris Forbesb1dee272015-07-03 13:50:24 +1200114
115template layer_data *get_my_data_ptr<layer_data>(
116 void *data_key,
117 std::unordered_map<void *, layer_data *> &data_map);
118
Chris Forbesb6b8c462015-04-15 06:59:41 +1200119static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +1200120// TODO : This can be much smarter, using separate locks for separate global data
121static int globalLockInitialized = 0;
122static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200123
Chris Forbes2b8493a2015-08-12 15:03:22 +1200124VK_LAYER_EXPORT VkResult VKAPI vkCreateDescriptorSetLayout(
125 VkDevice device,
126 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800127 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200128 VkDescriptorSetLayout* pSetLayout)
129{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600130 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200131 /* stash a copy of the layout bindings */
Chia-I Wuf7458c52015-10-26 21:10:41 +0800132 VkResult result = my_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200133
134 if (VK_SUCCESS == result) {
135 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600136 auto& bindings = my_data->descriptor_set_layout_map[*pSetLayout];
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800137 bindings = new std::unordered_set<uint32_t>();
138 bindings->reserve(pCreateInfo->bindingCount);
139 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++)
140 bindings->insert(pCreateInfo->pBinding[i].binding);
141
Chris Forbes2b8493a2015-08-12 15:03:22 +1200142 loader_platform_thread_unlock_mutex(&globalLock);
143 }
144
145 return result;
146}
147
Chris Forbes2b8493a2015-08-12 15:03:22 +1200148VK_LAYER_EXPORT VkResult VKAPI vkCreatePipelineLayout(
149 VkDevice device,
150 const VkPipelineLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800151 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200152 VkPipelineLayout* pPipelineLayout)
153{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600154 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800155 VkResult result = my_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200156
157 if (VK_SUCCESS == result) {
158 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600159 auto& layouts = my_data->pipeline_layout_map[*pPipelineLayout];
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800160 layouts = new std::vector<std::unordered_set<uint32_t>*>();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800161 layouts->reserve(pCreateInfo->setLayoutCount);
162 for (unsigned i = 0; i < pCreateInfo->setLayoutCount; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600163 layouts->push_back(my_data->descriptor_set_layout_map[pCreateInfo->pSetLayouts[i]]);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200164 }
165 loader_platform_thread_unlock_mutex(&globalLock);
166 }
167
168 return result;
169}
170
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200171static void
172build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
173{
174 unsigned int const *code = (unsigned int const *)&words[0];
175 size_t size = words.size();
176
177 unsigned word = 5;
178 while (word < size) {
179 unsigned opcode = code[word] & 0x0ffffu;
180 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
181
182 switch (opcode) {
183 case spv::OpTypeVoid:
184 case spv::OpTypeBool:
185 case spv::OpTypeInt:
186 case spv::OpTypeFloat:
187 case spv::OpTypeVector:
188 case spv::OpTypeMatrix:
GregF7c45bed2015-07-21 17:22:50 -0600189 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200190 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600191 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200192 case spv::OpTypeArray:
193 case spv::OpTypeRuntimeArray:
194 case spv::OpTypeStruct:
195 case spv::OpTypeOpaque:
196 case spv::OpTypePointer:
197 case spv::OpTypeFunction:
198 case spv::OpTypeEvent:
199 case spv::OpTypeDeviceEvent:
200 case spv::OpTypeReserveId:
201 case spv::OpTypeQueue:
202 case spv::OpTypePipe:
203 type_def_index[code[word+1]] = word;
204 break;
205
206 default:
207 /* We only care about type definitions */
208 break;
209 }
210
211 word += oplen;
212 }
213}
214
Chris Forbes46794b82015-09-18 11:40:23 +1200215bool
216shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo)
217{
218 uint32_t *words = (uint32_t *)pCreateInfo->pCode;
Courtney Goeltzenleuchter8f8367e2015-10-07 08:38:30 -0600219 size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
Chris Forbes46794b82015-09-18 11:40:23 +1200220
221 /* Just validate that the header makes sense. */
222 return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
223}
224
Chris Forbesb6b8c462015-04-15 06:59:41 +1200225static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200226init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200227{
Chris Forbesb1dee272015-07-03 13:50:24 +1200228 uint32_t report_flags = 0;
229 uint32_t debug_action = 0;
230 FILE *log_output = NULL;
231 const char *option_str;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600232 VkDbgMsgCallback callback;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200233 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200234 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
235 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200236
Chris Forbesb1dee272015-07-03 13:50:24 +1200237 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200238 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200239 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600240 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600241 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &callback);
242 my_data->logging_callback.push_back(callback);
243 }
244
245 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
246 layer_create_msg_callback(my_data->report_data, report_flags, win32_debug_output_msg, NULL, &callback);
247 my_data->logging_callback.push_back(callback);
Chris Forbesb1dee272015-07-03 13:50:24 +1200248 }
249
250 if (!globalLockInitialized)
251 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200252 loader_platform_thread_create_mutex(&globalLock);
253 globalLockInitialized = 1;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200254 }
255}
256
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600257static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600258 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600259 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600260 VK_API_VERSION,
261 VK_MAKE_VERSION(0, 1, 0),
262 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600263 }
Chris Forbes2778f302015-04-02 13:22:31 +1300264};
265
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600266VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600267 const char *pLayerName,
268 uint32_t *pCount,
269 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300270{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600271 /* shader checker does not have any global extensions */
272 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300273}
274
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600275VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600276 uint32_t *pCount,
277 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600278{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600279 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
280 shader_checker_global_layers,
281 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600282}
283
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600284VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600285 VkPhysicalDevice physicalDevice,
286 const char* pLayerName,
287 uint32_t* pCount,
288 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600289{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600290 /* Shader checker does not have any physical device extensions */
Jon Ashburn751c4842015-11-02 17:37:20 -0700291 if (pLayerName == NULL) {
292 dispatch_key key = get_dispatch_key(physicalDevice);
293 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -0700294 return my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(
Jon Ashburn751c4842015-11-02 17:37:20 -0700295 physicalDevice,
296 NULL,
297 pCount,
298 pProperties);
299 } else {
300 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
301 }
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600302}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600303
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600304VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600305 VkPhysicalDevice physicalDevice,
306 uint32_t* pCount,
307 VkLayerProperties* pProperties)
308{
309 /* Shader checker physical device layers are the same as global */
310 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
311 shader_checker_global_layers,
312 pCount, pProperties);
Jon Ashburn207a3af2015-06-10 16:43:31 -0600313}
Chris Forbes2778f302015-04-02 13:22:31 +1300314
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200315static char const *
316storage_class_name(unsigned sc)
317{
318 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600319 case spv::StorageClassInput: return "input";
320 case spv::StorageClassOutput: return "output";
321 case spv::StorageClassUniformConstant: return "const uniform";
322 case spv::StorageClassUniform: return "uniform";
323 case spv::StorageClassWorkgroupLocal: return "workgroup local";
324 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
325 case spv::StorageClassPrivateGlobal: return "private global";
326 case spv::StorageClassFunction: return "function";
327 case spv::StorageClassGeneric: return "generic";
Cody Northrop97e52d82015-04-20 14:09:40 -0600328 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600329 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200330 default: return "unknown";
331 }
332}
333
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200334/* returns ptr to null terminator */
335static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600336describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200337{
338 auto type_def_it = src->type_def_index.find(type);
339
340 if (type_def_it == src->type_def_index.end()) {
341 return dst + sprintf(dst, "undef");
342 }
343
344 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
345 unsigned opcode = code[0] & 0x0ffffu;
346 switch (opcode) {
347 case spv::OpTypeBool:
348 return dst + sprintf(dst, "bool");
349 case spv::OpTypeInt:
350 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
351 case spv::OpTypeFloat:
352 return dst + sprintf(dst, "float%d", code[2]);
353 case spv::OpTypeVector:
354 dst += sprintf(dst, "vec%d of ", code[3]);
355 return describe_type(dst, src, code[2]);
356 case spv::OpTypeMatrix:
357 dst += sprintf(dst, "mat%d of ", code[3]);
358 return describe_type(dst, src, code[2]);
359 case spv::OpTypeArray:
360 dst += sprintf(dst, "arr[%d] of ", code[3]);
361 return describe_type(dst, src, code[2]);
362 case spv::OpTypePointer:
363 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
364 return describe_type(dst, src, code[3]);
365 case spv::OpTypeStruct:
366 {
367 unsigned oplen = code[0] >> 16;
368 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600369 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200370 dst = describe_type(dst, src, code[i]);
371 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
372 }
373 return dst;
374 }
Chris Forbes0ae15802015-08-14 12:04:39 +1200375 case spv::OpTypeSampler:
376 return dst + sprintf(dst, "sampler");
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200377 default:
378 return dst + sprintf(dst, "oddtype");
379 }
380}
381
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200382static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600383types_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 +1200384{
385 auto a_type_def_it = a->type_def_index.find(a_type);
386 auto b_type_def_it = b->type_def_index.find(b_type);
387
388 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200389 return false;
390 }
391
392 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200393 return false;
394 }
395
396 /* walk two type trees together, and complain about differences */
397 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
398 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
399
400 unsigned a_opcode = a_code[0] & 0x0ffffu;
401 unsigned b_opcode = b_code[0] & 0x0ffffu;
402
Chris Forbesf3fc0332015-06-05 14:57:05 +1200403 if (b_arrayed && b_opcode == spv::OpTypeArray) {
404 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
405 return types_match(a, b, a_type, b_code[2], false);
406 }
407
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200408 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200409 return false;
410 }
411
412 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200413 /* 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 +1200414 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200415 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200416 case spv::OpTypeInt:
417 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200418 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200419 case spv::OpTypeFloat:
420 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200421 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200422 case spv::OpTypeVector:
423 case spv::OpTypeMatrix:
424 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200425 /* match on element type, count. these all have the same layout. we don't get here if
426 * b_arrayed -- that is handled above. */
427 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 +1200428 case spv::OpTypeStruct:
429 /* match on all element types */
430 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200431 if (b_arrayed) {
432 /* for the purposes of matching different levels of arrayness, structs are leaves. */
433 return false;
434 }
435
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200436 unsigned a_len = a_code[0] >> 16;
437 unsigned b_len = b_code[0] >> 16;
438
439 if (a_len != b_len) {
440 return false; /* structs cannot match if member counts differ */
441 }
442
Ian Elliott1cb62222015-04-17 11:05:04 -0600443 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200444 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200445 return false;
446 }
447 }
448
449 return true;
450 }
451 case spv::OpTypePointer:
452 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200453 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200454
455 default:
456 /* remaining types are CLisms, or may not appear in the interfaces we
457 * are interested in. Just claim no match.
458 */
459 return false;
460
461 }
462}
463
Chris Forbes06e8fc32015-04-13 12:14:52 +1200464static int
465value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
466{
467 auto it = map.find(id);
468 if (it == map.end())
469 return def;
470 else
471 return it->second;
472}
473
Chris Forbes06e8fc32015-04-13 12:14:52 +1200474struct interface_var {
475 uint32_t id;
476 uint32_t type_id;
477 /* TODO: collect the name, too? Isn't required to be present. */
478};
479
Chris Forbes06e8fc32015-04-13 12:14:52 +1200480static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600481collect_interface_by_location(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200482 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200483 std::map<uint32_t, interface_var> &out,
484 std::map<uint32_t, interface_var> &builtins_out)
485{
486 unsigned int const *code = (unsigned int const *)&src->words[0];
487 size_t size = src->words.size();
488
Chris Forbes06e8fc32015-04-13 12:14:52 +1200489 std::unordered_map<unsigned, unsigned> var_locations;
490 std::unordered_map<unsigned, unsigned> var_builtins;
491
492 unsigned word = 5;
493 while (word < size) {
494
495 unsigned opcode = code[word] & 0x0ffffu;
496 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
497
498 /* We consider two interface models: SSO rendezvous-by-location, and
499 * builtins. Complain about anything that fits neither model.
500 */
501 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600502 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200503 var_locations[code[word+1]] = code[word+3];
504 }
505
Cody Northrop97e52d82015-04-20 14:09:40 -0600506 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200507 var_builtins[code[word+1]] = code[word+3];
508 }
509 }
510
511 /* TODO: handle grouped decorations */
512 /* TODO: handle index=1 dual source outputs from FS -- two vars will
513 * have the same location, and we DONT want to clobber. */
514
Ian Elliott1cb62222015-04-17 11:05:04 -0600515 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200516 int location = value_or_default(var_locations, code[word+2], -1);
517 int builtin = value_or_default(var_builtins, code[word+2], -1);
518
519 if (location == -1 && builtin == -1) {
520 /* No location defined, and not bound to an API builtin.
521 * The spec says nothing about how this case works (or doesn't)
522 * for interface matching.
523 */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600524 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 +1200525 "var %d (type %d) in %s interface has no Location or Builtin decoration",
526 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes06e8fc32015-04-13 12:14:52 +1200527 }
528 else if (location != -1) {
529 /* A user-defined interface variable, with a location. */
530 interface_var v;
531 v.id = code[word+2];
532 v.type_id = code[word+1];
533 out[location] = v;
534 }
535 else {
536 /* A builtin interface variable */
537 interface_var v;
538 v.id = code[word+2];
539 v.type_id = code[word+1];
540 builtins_out[builtin] = v;
541 }
542 }
543
544 word += oplen;
545 }
546}
547
Chris Forbes0ae15802015-08-14 12:04:39 +1200548static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600549collect_interface_by_descriptor_slot(layer_data *my_data, VkDevice dev,
Chris Forbes0ae15802015-08-14 12:04:39 +1200550 shader_module const *src, spv::StorageClass sinterface,
551 std::map<std::pair<unsigned, unsigned>, interface_var> &out)
552{
553 unsigned int const *code = (unsigned int const *)&src->words[0];
554 size_t size = src->words.size();
555
556 std::unordered_map<unsigned, unsigned> var_sets;
557 std::unordered_map<unsigned, unsigned> var_bindings;
558
559 unsigned word = 5;
560 while (word < size) {
561
562 unsigned opcode = code[word] & 0x0ffffu;
563 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
564
565 /* We consider two interface models: SSO rendezvous-by-location, and
566 * builtins. Complain about anything that fits neither model.
567 */
568 if (opcode == spv::OpDecorate) {
569 if (code[word+2] == spv::DecorationDescriptorSet) {
570 var_sets[code[word+1]] = code[word+3];
571 }
572
573 if (code[word+2] == spv::DecorationBinding) {
574 var_bindings[code[word+1]] = code[word+3];
575 }
576 }
577
578 if (opcode == spv::OpVariable && (code[word+3] == spv::StorageClassUniform ||
579 code[word+3] == spv::StorageClassUniformConstant)) {
580 unsigned set = value_or_default(var_sets, code[word+2], 0);
581 unsigned binding = value_or_default(var_bindings, code[word+2], 0);
582
583 auto existing_it = out.find(std::make_pair(set, binding));
584 if (existing_it != out.end()) {
585 /* conflict within spv image */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600586 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 +1200587 SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
588 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
589 code[word+2], code[word+1], storage_class_name(sinterface),
590 existing_it->first.first, existing_it->first.second);
591 }
592
593 interface_var v;
594 v.id = code[word+2];
595 v.type_id = code[word+1];
596 out[std::make_pair(set, binding)] = v;
597 }
598
599 word += oplen;
600 }
601}
602
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600603VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
604 VkDevice device,
605 const VkShaderModuleCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800606 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600607 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300608{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600609 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes46794b82015-09-18 11:40:23 +1200610 /* Protect the driver from non-SPIRV shaders */
611 if (!shader_is_spirv(pCreateInfo)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600612 log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE,
Chris Forbes46794b82015-09-18 11:40:23 +1200613 /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
614 "Shader is not SPIR-V");
615 return VK_ERROR_VALIDATION_FAILED;
616 }
617
Chia-I Wuf7458c52015-10-26 21:10:41 +0800618 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200619
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600620 if (res == VK_SUCCESS) {
621 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600622 my_data->shader_module_map[*pShaderModule] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600623 loader_platform_thread_unlock_mutex(&globalLock);
624 }
Chris Forbes2778f302015-04-02 13:22:31 +1300625 return res;
626}
627
Chia-I Wu08accc62015-07-07 11:50:03 +0800628VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
629 VkDevice device,
630 const VkRenderPassCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800631 const VkAllocationCallbacks* pAllocator,
Chia-I Wu08accc62015-07-07 11:50:03 +0800632 VkRenderPass *pRenderPass)
633{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600634 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800635 VkResult res = my_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800636
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600637 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600638 my_data->render_pass_map[*pRenderPass] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800639 loader_platform_thread_unlock_mutex(&globalLock);
640 return res;
641}
642
Chris Forbesee99b9b2015-05-25 11:13:22 +1200643static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600644validate_interface_between_stages(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200645 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600646 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200647 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200648{
649 std::map<uint32_t, interface_var> outputs;
650 std::map<uint32_t, interface_var> inputs;
651
652 std::map<uint32_t, interface_var> builtin_outputs;
653 std::map<uint32_t, interface_var> builtin_inputs;
654
Chris Forbesee99b9b2015-05-25 11:13:22 +1200655 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200656
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600657 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
658 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200659
660 auto a_it = outputs.begin();
661 auto b_it = inputs.begin();
662
663 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600664 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
665 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
666 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200667 auto a_first = a_at_end ? 0 : a_it->first;
668 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600669
Mike Stroyan19a6de22015-09-10 14:10:25 -0600670 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600671 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 -0600672 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
673 pass = false;
674 }
Chris Forbes41002452015-04-08 10:19:16 +1200675 a_it++;
676 }
David Pinedod8f83d82015-04-27 16:36:17 -0600677 else if (a_at_end || a_first > b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600678 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 -0600679 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
680 pass = false;
681 }
Chris Forbes41002452015-04-08 10:19:16 +1200682 b_it++;
683 }
684 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200685 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 +1200686 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200687 }
688 else {
689 char producer_type[1024];
690 char consumer_type[1024];
691 describe_type(producer_type, producer, a_it->second.type_id);
692 describe_type(consumer_type, consumer, b_it->second.type_id);
693
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600694 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600695 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
Chris Forbesee99b9b2015-05-25 11:13:22 +1200696 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600697 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200698 }
Chris Forbes41002452015-04-08 10:19:16 +1200699 a_it++;
700 b_it++;
701 }
702 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200703
704 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200705}
706
707
Chris Forbes3616b462015-04-08 10:37:20 +1200708enum FORMAT_TYPE {
709 FORMAT_TYPE_UNDEFINED,
710 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
711 FORMAT_TYPE_SINT,
712 FORMAT_TYPE_UINT,
713};
714
715
716static unsigned
717get_format_type(VkFormat fmt) {
718 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800719 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200720 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800721 case VK_FORMAT_R8_SINT:
722 case VK_FORMAT_R8G8_SINT:
723 case VK_FORMAT_R8G8B8_SINT:
724 case VK_FORMAT_R8G8B8A8_SINT:
725 case VK_FORMAT_R16_SINT:
726 case VK_FORMAT_R16G16_SINT:
727 case VK_FORMAT_R16G16B16_SINT:
728 case VK_FORMAT_R16G16B16A16_SINT:
729 case VK_FORMAT_R32_SINT:
730 case VK_FORMAT_R32G32_SINT:
731 case VK_FORMAT_R32G32B32_SINT:
732 case VK_FORMAT_R32G32B32A32_SINT:
733 case VK_FORMAT_B8G8R8_SINT:
734 case VK_FORMAT_B8G8R8A8_SINT:
735 case VK_FORMAT_R10G10B10A2_SINT:
736 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200737 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800738 case VK_FORMAT_R8_UINT:
739 case VK_FORMAT_R8G8_UINT:
740 case VK_FORMAT_R8G8B8_UINT:
741 case VK_FORMAT_R8G8B8A8_UINT:
742 case VK_FORMAT_R16_UINT:
743 case VK_FORMAT_R16G16_UINT:
744 case VK_FORMAT_R16G16B16_UINT:
745 case VK_FORMAT_R16G16B16A16_UINT:
746 case VK_FORMAT_R32_UINT:
747 case VK_FORMAT_R32G32_UINT:
748 case VK_FORMAT_R32G32B32_UINT:
749 case VK_FORMAT_R32G32B32A32_UINT:
750 case VK_FORMAT_B8G8R8_UINT:
751 case VK_FORMAT_B8G8R8A8_UINT:
752 case VK_FORMAT_R10G10B10A2_UINT:
753 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200754 return FORMAT_TYPE_UINT;
755 default:
756 return FORMAT_TYPE_FLOAT;
757 }
758}
759
760
Chris Forbes156a1162015-05-04 14:04:06 +1200761/* characterizes a SPIR-V type appearing in an interface to a FF stage,
762 * for comparison to a VkFormat's characterization above. */
763static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600764get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200765{
766 auto type_def_it = src->type_def_index.find(type);
767
768 if (type_def_it == src->type_def_index.end()) {
769 return FORMAT_TYPE_UNDEFINED;
770 }
771
772 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
773 unsigned opcode = code[0] & 0x0ffffu;
774 switch (opcode) {
775 case spv::OpTypeInt:
776 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
777 case spv::OpTypeFloat:
778 return FORMAT_TYPE_FLOAT;
779 case spv::OpTypeVector:
780 return get_fundamental_type(src, code[2]);
781 case spv::OpTypeMatrix:
782 return get_fundamental_type(src, code[2]);
783 case spv::OpTypeArray:
784 return get_fundamental_type(src, code[2]);
785 case spv::OpTypePointer:
786 return get_fundamental_type(src, code[3]);
787 default:
788 return FORMAT_TYPE_UNDEFINED;
789 }
790}
791
792
Chris Forbesee99b9b2015-05-25 11:13:22 +1200793static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600794validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200795{
796 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
797 * each binding should be specified only once.
798 */
799 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200800 bool pass = true;
801
Chia-I Wud50a7d72015-10-26 20:48:51 +0800802 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes280ba2c2015-06-12 11:16:41 +1200803 auto desc = &vi->pVertexBindingDescriptions[i];
804 auto & binding = bindings[desc->binding];
805 if (binding) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600806 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 -0600807 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
808 pass = false;
809 }
Chris Forbes280ba2c2015-06-12 11:16:41 +1200810 }
811 else {
812 binding = desc;
813 }
814 }
815
816 return pass;
817}
818
819
820static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600821validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200822{
823 std::map<uint32_t, interface_var> inputs;
824 /* we collect builtin inputs, but they will never appear in the VI state --
825 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
826 */
827 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200828 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200829
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600830 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200831
832 /* Build index by location */
833 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200834 if (vi) {
Chia-I Wud50a7d72015-10-26 20:48:51 +0800835 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
Chris Forbes7191cd52015-05-25 11:13:24 +1200836 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
837 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200838
839 auto it_a = attribs.begin();
840 auto it_b = inputs.begin();
841
David Pinedod8f83d82015-04-27 16:36:17 -0600842 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
843 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
844 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200845 auto a_first = a_at_end ? 0 : it_a->first;
846 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600847 if (b_at_end || a_first < b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600848 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 -0600849 "Vertex attribute at location %d not consumed by VS", a_first)) {
850 pass = false;
851 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200852 it_a++;
853 }
David Pinedod8f83d82015-04-27 16:36:17 -0600854 else if (a_at_end || b_first < a_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600855 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 -0600856 "VS consumes input at location %d but not provided", b_first)) {
857 pass = false;
858 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200859 it_b++;
860 }
861 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200862 unsigned attrib_type = get_format_type(it_a->second->format);
863 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
864
865 /* type checking */
866 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
867 char vs_type[1024];
868 describe_type(vs_type, vs, it_b->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600869 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 +1200870 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600871 string_VkFormat(it_a->second->format), a_first, vs_type)) {
872 pass = false;
873 }
Chris Forbes401784b2015-05-04 14:04:24 +1200874 }
875
Chris Forbes6b2ead62015-04-17 10:13:28 +1200876 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200877 it_a++;
878 it_b++;
879 }
880 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200881
882 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200883}
884
885
Chris Forbesee99b9b2015-05-25 11:13:22 +1200886static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600887validate_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 +1200888{
Chia-I Wu08accc62015-07-07 11:50:03 +0800889 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200890 std::map<uint32_t, interface_var> outputs;
891 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200892 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200893
894 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
895
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600896 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200897
898 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
899 * and all color attachment should be UNORM/SNORM/FLOAT.
900 */
901 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200902 if (outputs.size()) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600903 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 -0600904 "Should not have user-defined FS outputs when using broadcast")) {
905 pass = false;
906 }
Chris Forbes3616b462015-04-08 10:37:20 +1200907 }
908
Chia-I Wu08accc62015-07-07 11:50:03 +0800909 for (unsigned i = 0; i < color_formats.size(); i++) {
910 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200911 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600912 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 -0600913 "CB format should not be SINT or UINT when using broadcast")) {
914 pass = false;
915 }
Chris Forbes3616b462015-04-08 10:37:20 +1200916 }
917 }
918
Chris Forbesee99b9b2015-05-25 11:13:22 +1200919 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200920 }
921
922 auto it = outputs.begin();
923 uint32_t attachment = 0;
924
925 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
926 * are currently dense, but the parallel with matching between shader stages is nice.
927 */
928
Chris Forbes9715d4a2015-07-10 09:06:54 +1200929 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200930 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
931 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600932 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 -0600933 "FS writes to output location %d with no matching attachment", it->first)) {
934 pass = false;
935 }
Chris Forbes3616b462015-04-08 10:37:20 +1200936 it++;
937 }
938 else if (it == outputs.end() || it->first > attachment) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600939 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 -0600940 "Attachment %d not written by FS", attachment)) {
941 pass = false;
942 }
Chris Forbes3616b462015-04-08 10:37:20 +1200943 attachment++;
944 }
945 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200946 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800947 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200948
949 /* type checking */
950 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
951 char fs_type[1024];
952 describe_type(fs_type, fs, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600953 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 +1200954 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600955 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
956 pass = false;
957 }
Chris Forbes46d31e52015-05-04 14:20:10 +1200958 }
959
Chris Forbes6b2ead62015-04-17 10:13:28 +1200960 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200961 it++;
962 attachment++;
963 }
964 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200965
966 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200967}
968
969
Chris Forbesf044ec92015-06-05 15:01:08 +1200970struct shader_stage_attributes {
971 char const * const name;
972 bool arrayed_input;
973};
974
975
976static shader_stage_attributes
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600977shader_stage_attribs[] = {
Chris Forbesf044ec92015-06-05 15:01:08 +1200978 { "vertex shader", false },
979 { "tessellation control shader", true },
980 { "tessellation evaluation shader", false },
981 { "geometry shader", true },
982 { "fragment shader", false },
983};
984
985
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800986static bool
987has_descriptor_binding(std::vector<std::unordered_set<uint32_t>*>* layout,
988 std::pair<unsigned, unsigned> slot)
Chris Forbes556c76c2015-08-14 12:04:59 +1200989{
990 if (!layout)
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800991 return false;
Chris Forbes556c76c2015-08-14 12:04:59 +1200992
993 if (slot.first >= layout->size())
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800994 return false;
Chris Forbes556c76c2015-08-14 12:04:59 +1200995
996 auto set = (*layout)[slot.first];
997
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800998 return (set->find(slot.second) != set->end());
Chris Forbes556c76c2015-08-14 12:04:59 +1200999}
1000
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001001static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1002{
1003 uint32_t bit_pos = u_ffs(stage);
1004 return bit_pos-1;
1005}
Chris Forbes556c76c2015-08-14 12:04:59 +12001006
Chris Forbes81874ba2015-06-04 20:23:00 +12001007static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001008validate_graphics_pipeline(layer_data *my_data, VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001009{
Chris Forbesf6800b52015-04-08 10:16:45 +12001010 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1011 * before trying to do anything more: */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001012 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1013 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1014 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbesf6800b52015-04-08 10:16:45 +12001015
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001016 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1017 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Chia-I Wu08accc62015-07-07 11:50:03 +08001018 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001019 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001020 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001021
Chris Forbes7f963832015-05-29 14:55:18 +12001022 loader_platform_thread_lock_mutex(&globalLock);
1023
Tony Barbour92503c62015-07-13 15:28:47 -06001024 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001025 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1026 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001027
Chia-I Wu28e06912015-10-31 00:31:16 +08001028 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 -06001029 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001030 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 +08001031 "Unknown shader stage %d", pStage->stage)) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001032 pass = false;
1033 }
Chris Forbes6b2ead62015-04-17 10:13:28 +12001034 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001035 else {
Chia-I Wu28e06912015-10-31 00:31:16 +08001036 shader_module *module = my_data->shader_module_map[pStage->module];
1037 shaders[get_shader_stage_id(pStage->stage)] = module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001038
1039 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001040 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
Chia-I Wu28e06912015-10-31 00:31:16 +08001041 collect_interface_by_descriptor_slot(my_data, dev, module, spv::StorageClassUniform,
Chris Forbes46794b82015-09-18 11:40:23 +12001042 descriptor_uses);
Chris Forbes556c76c2015-08-14 12:04:59 +12001043
Chia-I Wue2fc5522015-10-26 20:04:44 +08001044 auto layout = pCreateInfo->layout != VK_NULL_HANDLE ?
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001045 my_data->pipeline_layout_map[pCreateInfo->layout] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001046
Chris Forbes46794b82015-09-18 11:40:23 +12001047 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001048
Chris Forbes46794b82015-09-18 11:40:23 +12001049 /* find the matching binding */
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001050 auto found = has_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001051
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001052 if (!found) {
Chris Forbes46794b82015-09-18 11:40:23 +12001053 char type_name[1024];
Chia-I Wu28e06912015-10-31 00:31:16 +08001054 describe_type(type_name, module, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001055 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 +12001056 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1057 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001058 it->first.first, it->first.second, type_name)) {
1059 pass = false;
1060 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001061 }
1062 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001063 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001064 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001065 }
1066
Chia-I Wu08accc62015-07-07 11:50:03 +08001067 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001068 rp = my_data->render_pass_map[pCreateInfo->renderPass];
Chia-I Wu08accc62015-07-07 11:50:03 +08001069
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001070 vi = pCreateInfo->pVertexInputState;
1071
Chris Forbes280ba2c2015-06-12 11:16:41 +12001072 if (vi) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001073 pass = validate_vi_consistency(my_data, dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001074 }
1075
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001076 if (shaders[vertex_stage]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001077 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001078 }
1079
Chris Forbesf044ec92015-06-05 15:01:08 +12001080 /* TODO: enforce rules about present combinations of shaders */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001081 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1082 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
Chris Forbesf044ec92015-06-05 15:01:08 +12001083
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001084 while (!shaders[producer] && producer != fragment_stage) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001085 producer++;
1086 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001087 }
1088
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001089 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001090 assert(shaders[producer]);
1091 if (shaders[consumer]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001092 pass = validate_interface_between_stages(my_data, dev,
Chris Forbes46794b82015-09-18 11:40:23 +12001093 shaders[producer], shader_stage_attribs[producer].name,
1094 shaders[consumer], shader_stage_attribs[consumer].name,
1095 shader_stage_attribs[consumer].arrayed_input) && pass;
Chris Forbesf044ec92015-06-05 15:01:08 +12001096
1097 producer = consumer;
1098 }
1099 }
1100
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001101 if (shaders[fragment_stage] && rp) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001102 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001103 }
1104
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001105 delete shaders;
1106
Chris Forbes7f963832015-05-29 14:55:18 +12001107 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001108 return pass;
1109}
1110
Jon Ashburnc669cc62015-07-09 15:02:25 -06001111//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001112VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001113vkCreateGraphicsPipelines(VkDevice device,
1114 VkPipelineCache pipelineCache,
1115 uint32_t count,
1116 const VkGraphicsPipelineCreateInfo *pCreateInfos,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001117 const VkAllocationCallbacks* pAllocator,
Jon Ashburnc669cc62015-07-09 15:02:25 -06001118 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001119{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001120 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes36a372f2015-07-24 13:53:47 +12001121 bool pass = true;
1122 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001123 pass = validate_graphics_pipeline(my_data, device, &pCreateInfos[i]) && pass;
Chris Forbes36a372f2015-07-24 13:53:47 +12001124 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001125
1126 if (pass) {
1127 /* The driver is allowed to crash if passed junk. Only actually create the
1128 * pipeline if we didn't run into any showstoppers above.
1129 */
Chia-I Wuf7458c52015-10-26 21:10:41 +08001130 return my_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001131 }
1132 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001133 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001134 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001135}
1136
1137
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001138VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Chris Forbesb1dee272015-07-03 13:50:24 +12001139{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001140 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001141 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Chris Forbesb1dee272015-07-03 13:50:24 +12001142 if (result == VK_SUCCESS) {
1143 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001144 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1145 }
1146 return result;
1147}
Chris Forbes39d8d752015-06-04 20:27:09 +12001148
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001149/* hook DextroyDevice to remove tableMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001150VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001151{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001152 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001153 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001154 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001155 delete my_device_data->device_dispatch_table;
1156 layer_data_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001157}
1158
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001159VkResult VKAPI vkCreateInstance(
1160 const VkInstanceCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001161 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001162 VkInstance* pInstance)
1163{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001164 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1165 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +08001166 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001167
1168 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001169 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1170 my_data->report_data = debug_report_create_instance(
1171 pTable,
1172 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +08001173 pCreateInfo->enabledExtensionNameCount,
Chris Forbesb1dee272015-07-03 13:50:24 +12001174 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001175
Chris Forbesb1dee272015-07-03 13:50:24 +12001176 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001177 }
1178 return result;
1179}
1180
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001181/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001182VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001183{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001184 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001185 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001186 my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
Chris Forbesb1dee272015-07-03 13:50:24 +12001187
1188 // Clean up logging callback, if any
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001189 while (my_data->logging_callback.size() > 0) {
1190 VkDbgMsgCallback callback = my_data->logging_callback.back();
1191 layer_destroy_msg_callback(my_data->report_data, callback);
1192 my_data->logging_callback.pop_back();
Chris Forbesb1dee272015-07-03 13:50:24 +12001193 }
1194
1195 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001196 delete my_data->instance_dispatch_table;
1197 layer_data_map.erase(key);
1198 if (layer_data_map.empty()) {
1199 // Release mutex when destroying last instance.
1200 loader_platform_thread_delete_mutex(&globalLock);
1201 globalLockInitialized = 0;
1202 }
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001203}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001204
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001205VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001206 VkInstance instance,
1207 VkFlags msgFlags,
1208 const PFN_vkDbgMsgCallback pfnMsgCallback,
1209 void* pUserData,
1210 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001211{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001212 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1213 VkResult res = my_data->instance_dispatch_table->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001214 if (VK_SUCCESS == res) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001215 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1216 }
1217 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001218}
1219
1220VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001221 VkInstance instance,
1222 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001223{
Chris Forbesb1dee272015-07-03 13:50:24 +12001224 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001225 VkResult res = my_data->instance_dispatch_table->DbgDestroyMsgCallback(instance, msgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001226 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1227 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001228}
1229
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001230VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001231{
Chris Forbesb1dee272015-07-03 13:50:24 +12001232 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001233 return NULL;
1234
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001235 layer_data *my_data;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001236 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001237 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001238 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
1239 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
1240 my_data->device_dispatch_table = new VkLayerDispatchTable;
1241 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001242 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001243 }
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001244 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Jon Ashburn8fd08252015-05-28 16:25:02 -06001245
Chris Forbes2778f302015-04-02 13:22:31 +13001246#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001247 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001248 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001249
Chris Forbesb1dee272015-07-03 13:50:24 +12001250 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001251 ADD_HOOK(vkCreateShaderModule);
Chia-I Wu08accc62015-07-07 11:50:03 +08001252 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001253 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001254 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001255 ADD_HOOK(vkCreateDescriptorSetLayout);
1256 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001257#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001258
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001259 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001260 {
1261 if (pTable->GetDeviceProcAddr == NULL)
1262 return NULL;
1263 return pTable->GetDeviceProcAddr(dev, funcName);
1264 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001265}
1266
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001267VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001268{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001269 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001270
Chris Forbesb1dee272015-07-03 13:50:24 +12001271 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001272 return NULL;
1273
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001274 layer_data *my_data;
Chris Forbesb1dee272015-07-03 13:50:24 +12001275 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001276 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
1277 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
1278 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
1279 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001280 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001281 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001282#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001283 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001284 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001285
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001286 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001287 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001288 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1289 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1290 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1291 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001292#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001293
Chris Forbesb1dee272015-07-03 13:50:24 +12001294
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001295 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001296 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001297 if (fptr)
1298 return fptr;
1299
Chris Forbesb1dee272015-07-03 13:50:24 +12001300 {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001301 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001302 if (pTable->GetInstanceProcAddr == NULL)
1303 return NULL;
1304 return pTable->GetInstanceProcAddr(instance, funcName);
1305 }
Chris Forbes2778f302015-04-02 13:22:31 +13001306}