blob: 38e49fdbd84e7b07d06f92b7f1e5847340bdb64d [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);
Michael Lentine8ea29962015-11-03 16:18:46 -0800610 bool skip_call = false;
Chris Forbes46794b82015-09-18 11:40:23 +1200611 if (!shader_is_spirv(pCreateInfo)) {
Michael Lentine8ea29962015-11-03 16:18:46 -0800612 skip_call |= 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");
Chris Forbes46794b82015-09-18 11:40:23 +1200615 }
616
Michael Lentine8ea29962015-11-03 16:18:46 -0800617 if (skip_call)
618 return VK_ERROR_VALIDATION_FAILED;
619
Chia-I Wuf7458c52015-10-26 21:10:41 +0800620 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200621
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600622 if (res == VK_SUCCESS) {
623 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600624 my_data->shader_module_map[*pShaderModule] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600625 loader_platform_thread_unlock_mutex(&globalLock);
626 }
Chris Forbes2778f302015-04-02 13:22:31 +1300627 return res;
628}
629
Chia-I Wu08accc62015-07-07 11:50:03 +0800630VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
631 VkDevice device,
632 const VkRenderPassCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800633 const VkAllocationCallbacks* pAllocator,
Chia-I Wu08accc62015-07-07 11:50:03 +0800634 VkRenderPass *pRenderPass)
635{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600636 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800637 VkResult res = my_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800638
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600639 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600640 my_data->render_pass_map[*pRenderPass] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800641 loader_platform_thread_unlock_mutex(&globalLock);
642 return res;
643}
644
Chris Forbesee99b9b2015-05-25 11:13:22 +1200645static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600646validate_interface_between_stages(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200647 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600648 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200649 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200650{
651 std::map<uint32_t, interface_var> outputs;
652 std::map<uint32_t, interface_var> inputs;
653
654 std::map<uint32_t, interface_var> builtin_outputs;
655 std::map<uint32_t, interface_var> builtin_inputs;
656
Chris Forbesee99b9b2015-05-25 11:13:22 +1200657 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200658
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600659 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
660 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200661
662 auto a_it = outputs.begin();
663 auto b_it = inputs.begin();
664
665 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600666 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
667 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
668 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200669 auto a_first = a_at_end ? 0 : a_it->first;
670 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600671
Mike Stroyan19a6de22015-09-10 14:10:25 -0600672 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600673 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 -0600674 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
675 pass = false;
676 }
Chris Forbes41002452015-04-08 10:19:16 +1200677 a_it++;
678 }
David Pinedod8f83d82015-04-27 16:36:17 -0600679 else if (a_at_end || a_first > b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600680 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 -0600681 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
682 pass = false;
683 }
Chris Forbes41002452015-04-08 10:19:16 +1200684 b_it++;
685 }
686 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200687 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 +1200688 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200689 }
690 else {
691 char producer_type[1024];
692 char consumer_type[1024];
693 describe_type(producer_type, producer, a_it->second.type_id);
694 describe_type(consumer_type, consumer, b_it->second.type_id);
695
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600696 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 -0600697 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
Chris Forbesee99b9b2015-05-25 11:13:22 +1200698 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600699 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200700 }
Chris Forbes41002452015-04-08 10:19:16 +1200701 a_it++;
702 b_it++;
703 }
704 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200705
706 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200707}
708
709
Chris Forbes3616b462015-04-08 10:37:20 +1200710enum FORMAT_TYPE {
711 FORMAT_TYPE_UNDEFINED,
712 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
713 FORMAT_TYPE_SINT,
714 FORMAT_TYPE_UINT,
715};
716
717
718static unsigned
719get_format_type(VkFormat fmt) {
720 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800721 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200722 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800723 case VK_FORMAT_R8_SINT:
724 case VK_FORMAT_R8G8_SINT:
725 case VK_FORMAT_R8G8B8_SINT:
726 case VK_FORMAT_R8G8B8A8_SINT:
727 case VK_FORMAT_R16_SINT:
728 case VK_FORMAT_R16G16_SINT:
729 case VK_FORMAT_R16G16B16_SINT:
730 case VK_FORMAT_R16G16B16A16_SINT:
731 case VK_FORMAT_R32_SINT:
732 case VK_FORMAT_R32G32_SINT:
733 case VK_FORMAT_R32G32B32_SINT:
734 case VK_FORMAT_R32G32B32A32_SINT:
735 case VK_FORMAT_B8G8R8_SINT:
736 case VK_FORMAT_B8G8R8A8_SINT:
737 case VK_FORMAT_R10G10B10A2_SINT:
738 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200739 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800740 case VK_FORMAT_R8_UINT:
741 case VK_FORMAT_R8G8_UINT:
742 case VK_FORMAT_R8G8B8_UINT:
743 case VK_FORMAT_R8G8B8A8_UINT:
744 case VK_FORMAT_R16_UINT:
745 case VK_FORMAT_R16G16_UINT:
746 case VK_FORMAT_R16G16B16_UINT:
747 case VK_FORMAT_R16G16B16A16_UINT:
748 case VK_FORMAT_R32_UINT:
749 case VK_FORMAT_R32G32_UINT:
750 case VK_FORMAT_R32G32B32_UINT:
751 case VK_FORMAT_R32G32B32A32_UINT:
752 case VK_FORMAT_B8G8R8_UINT:
753 case VK_FORMAT_B8G8R8A8_UINT:
754 case VK_FORMAT_R10G10B10A2_UINT:
755 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200756 return FORMAT_TYPE_UINT;
757 default:
758 return FORMAT_TYPE_FLOAT;
759 }
760}
761
762
Chris Forbes156a1162015-05-04 14:04:06 +1200763/* characterizes a SPIR-V type appearing in an interface to a FF stage,
764 * for comparison to a VkFormat's characterization above. */
765static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600766get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200767{
768 auto type_def_it = src->type_def_index.find(type);
769
770 if (type_def_it == src->type_def_index.end()) {
771 return FORMAT_TYPE_UNDEFINED;
772 }
773
774 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
775 unsigned opcode = code[0] & 0x0ffffu;
776 switch (opcode) {
777 case spv::OpTypeInt:
778 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
779 case spv::OpTypeFloat:
780 return FORMAT_TYPE_FLOAT;
781 case spv::OpTypeVector:
782 return get_fundamental_type(src, code[2]);
783 case spv::OpTypeMatrix:
784 return get_fundamental_type(src, code[2]);
785 case spv::OpTypeArray:
786 return get_fundamental_type(src, code[2]);
787 case spv::OpTypePointer:
788 return get_fundamental_type(src, code[3]);
789 default:
790 return FORMAT_TYPE_UNDEFINED;
791 }
792}
793
794
Chris Forbesee99b9b2015-05-25 11:13:22 +1200795static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600796validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200797{
798 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
799 * each binding should be specified only once.
800 */
801 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200802 bool pass = true;
803
Chia-I Wud50a7d72015-10-26 20:48:51 +0800804 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes280ba2c2015-06-12 11:16:41 +1200805 auto desc = &vi->pVertexBindingDescriptions[i];
806 auto & binding = bindings[desc->binding];
807 if (binding) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600808 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 -0600809 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
810 pass = false;
811 }
Chris Forbes280ba2c2015-06-12 11:16:41 +1200812 }
813 else {
814 binding = desc;
815 }
816 }
817
818 return pass;
819}
820
821
822static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600823validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200824{
825 std::map<uint32_t, interface_var> inputs;
826 /* we collect builtin inputs, but they will never appear in the VI state --
827 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
828 */
829 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200830 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200831
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600832 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200833
834 /* Build index by location */
835 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200836 if (vi) {
Chia-I Wud50a7d72015-10-26 20:48:51 +0800837 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
Chris Forbes7191cd52015-05-25 11:13:24 +1200838 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
839 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200840
841 auto it_a = attribs.begin();
842 auto it_b = inputs.begin();
843
David Pinedod8f83d82015-04-27 16:36:17 -0600844 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
845 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
846 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200847 auto a_first = a_at_end ? 0 : it_a->first;
848 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600849 if (b_at_end || a_first < b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600850 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 -0600851 "Vertex attribute at location %d not consumed by VS", a_first)) {
852 pass = false;
853 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200854 it_a++;
855 }
David Pinedod8f83d82015-04-27 16:36:17 -0600856 else if (a_at_end || b_first < a_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600857 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 -0600858 "VS consumes input at location %d but not provided", b_first)) {
859 pass = false;
860 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200861 it_b++;
862 }
863 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200864 unsigned attrib_type = get_format_type(it_a->second->format);
865 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
866
867 /* type checking */
868 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
869 char vs_type[1024];
870 describe_type(vs_type, vs, it_b->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600871 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200872 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600873 string_VkFormat(it_a->second->format), a_first, vs_type)) {
874 pass = false;
875 }
Chris Forbes401784b2015-05-04 14:04:24 +1200876 }
877
Chris Forbes6b2ead62015-04-17 10:13:28 +1200878 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200879 it_a++;
880 it_b++;
881 }
882 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200883
884 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200885}
886
887
Chris Forbesee99b9b2015-05-25 11:13:22 +1200888static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600889validate_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 +1200890{
Chia-I Wu08accc62015-07-07 11:50:03 +0800891 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200892 std::map<uint32_t, interface_var> outputs;
893 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200894 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200895
896 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
897
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600898 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200899
900 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
901 * and all color attachment should be UNORM/SNORM/FLOAT.
902 */
903 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200904 if (outputs.size()) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600905 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 -0600906 "Should not have user-defined FS outputs when using broadcast")) {
907 pass = false;
908 }
Chris Forbes3616b462015-04-08 10:37:20 +1200909 }
910
Chia-I Wu08accc62015-07-07 11:50:03 +0800911 for (unsigned i = 0; i < color_formats.size(); i++) {
912 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200913 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600914 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600915 "CB format should not be SINT or UINT when using broadcast")) {
916 pass = false;
917 }
Chris Forbes3616b462015-04-08 10:37:20 +1200918 }
919 }
920
Chris Forbesee99b9b2015-05-25 11:13:22 +1200921 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200922 }
923
924 auto it = outputs.begin();
925 uint32_t attachment = 0;
926
927 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
928 * are currently dense, but the parallel with matching between shader stages is nice.
929 */
930
Chris Forbes9715d4a2015-07-10 09:06:54 +1200931 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200932 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
933 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600934 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 -0600935 "FS writes to output location %d with no matching attachment", it->first)) {
936 pass = false;
937 }
Chris Forbes3616b462015-04-08 10:37:20 +1200938 it++;
939 }
940 else if (it == outputs.end() || it->first > attachment) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600941 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 -0600942 "Attachment %d not written by FS", attachment)) {
943 pass = false;
944 }
Chris Forbes3616b462015-04-08 10:37:20 +1200945 attachment++;
946 }
947 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200948 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800949 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200950
951 /* type checking */
952 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
953 char fs_type[1024];
954 describe_type(fs_type, fs, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600955 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbesb1dee272015-07-03 13:50:24 +1200956 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600957 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
958 pass = false;
959 }
Chris Forbes46d31e52015-05-04 14:20:10 +1200960 }
961
Chris Forbes6b2ead62015-04-17 10:13:28 +1200962 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200963 it++;
964 attachment++;
965 }
966 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200967
968 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200969}
970
971
Chris Forbesf044ec92015-06-05 15:01:08 +1200972struct shader_stage_attributes {
973 char const * const name;
974 bool arrayed_input;
975};
976
977
978static shader_stage_attributes
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600979shader_stage_attribs[] = {
Chris Forbesf044ec92015-06-05 15:01:08 +1200980 { "vertex shader", false },
981 { "tessellation control shader", true },
982 { "tessellation evaluation shader", false },
983 { "geometry shader", true },
984 { "fragment shader", false },
985};
986
987
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800988static bool
989has_descriptor_binding(std::vector<std::unordered_set<uint32_t>*>* layout,
990 std::pair<unsigned, unsigned> slot)
Chris Forbes556c76c2015-08-14 12:04:59 +1200991{
992 if (!layout)
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800993 return false;
Chris Forbes556c76c2015-08-14 12:04:59 +1200994
995 if (slot.first >= layout->size())
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800996 return false;
Chris Forbes556c76c2015-08-14 12:04:59 +1200997
998 auto set = (*layout)[slot.first];
999
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001000 return (set->find(slot.second) != set->end());
Chris Forbes556c76c2015-08-14 12:04:59 +12001001}
1002
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001003static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1004{
1005 uint32_t bit_pos = u_ffs(stage);
1006 return bit_pos-1;
1007}
Chris Forbes556c76c2015-08-14 12:04:59 +12001008
Chris Forbes81874ba2015-06-04 20:23:00 +12001009static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001010validate_graphics_pipeline(layer_data *my_data, VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001011{
Chris Forbesf6800b52015-04-08 10:16:45 +12001012 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1013 * before trying to do anything more: */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001014 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1015 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1016 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbesf6800b52015-04-08 10:16:45 +12001017
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001018 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1019 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Chia-I Wu08accc62015-07-07 11:50:03 +08001020 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001021 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001022 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001023
Chris Forbes7f963832015-05-29 14:55:18 +12001024 loader_platform_thread_lock_mutex(&globalLock);
1025
Tony Barbour92503c62015-07-13 15:28:47 -06001026 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001027 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1028 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001029
Chia-I Wu28e06912015-10-31 00:31:16 +08001030 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 -06001031 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001032 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 +08001033 "Unknown shader stage %d", pStage->stage)) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001034 pass = false;
1035 }
Chris Forbes6b2ead62015-04-17 10:13:28 +12001036 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001037 else {
Chia-I Wu28e06912015-10-31 00:31:16 +08001038 shader_module *module = my_data->shader_module_map[pStage->module];
1039 shaders[get_shader_stage_id(pStage->stage)] = module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001040
1041 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001042 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
Chia-I Wu28e06912015-10-31 00:31:16 +08001043 collect_interface_by_descriptor_slot(my_data, dev, module, spv::StorageClassUniform,
Chris Forbes46794b82015-09-18 11:40:23 +12001044 descriptor_uses);
Chris Forbes556c76c2015-08-14 12:04:59 +12001045
Chia-I Wue2fc5522015-10-26 20:04:44 +08001046 auto layout = pCreateInfo->layout != VK_NULL_HANDLE ?
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001047 my_data->pipeline_layout_map[pCreateInfo->layout] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001048
Chris Forbes46794b82015-09-18 11:40:23 +12001049 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001050
Chris Forbes46794b82015-09-18 11:40:23 +12001051 /* find the matching binding */
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001052 auto found = has_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001053
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001054 if (!found) {
Chris Forbes46794b82015-09-18 11:40:23 +12001055 char type_name[1024];
Chia-I Wu28e06912015-10-31 00:31:16 +08001056 describe_type(type_name, module, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001057 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 +12001058 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1059 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001060 it->first.first, it->first.second, type_name)) {
1061 pass = false;
1062 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001063 }
1064 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001065 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001066 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001067 }
1068
Chia-I Wu08accc62015-07-07 11:50:03 +08001069 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001070 rp = my_data->render_pass_map[pCreateInfo->renderPass];
Chia-I Wu08accc62015-07-07 11:50:03 +08001071
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001072 vi = pCreateInfo->pVertexInputState;
1073
Chris Forbes280ba2c2015-06-12 11:16:41 +12001074 if (vi) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001075 pass = validate_vi_consistency(my_data, dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001076 }
1077
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001078 if (shaders[vertex_stage]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001079 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001080 }
1081
Chris Forbesf044ec92015-06-05 15:01:08 +12001082 /* TODO: enforce rules about present combinations of shaders */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001083 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1084 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
Chris Forbesf044ec92015-06-05 15:01:08 +12001085
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001086 while (!shaders[producer] && producer != fragment_stage) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001087 producer++;
1088 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001089 }
1090
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001091 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001092 assert(shaders[producer]);
1093 if (shaders[consumer]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001094 pass = validate_interface_between_stages(my_data, dev,
Chris Forbes46794b82015-09-18 11:40:23 +12001095 shaders[producer], shader_stage_attribs[producer].name,
1096 shaders[consumer], shader_stage_attribs[consumer].name,
1097 shader_stage_attribs[consumer].arrayed_input) && pass;
Chris Forbesf044ec92015-06-05 15:01:08 +12001098
1099 producer = consumer;
1100 }
1101 }
1102
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001103 if (shaders[fragment_stage] && rp) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001104 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001105 }
1106
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001107 delete shaders;
1108
Chris Forbes7f963832015-05-29 14:55:18 +12001109 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001110 return pass;
1111}
1112
Jon Ashburnc669cc62015-07-09 15:02:25 -06001113//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001114VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001115vkCreateGraphicsPipelines(VkDevice device,
1116 VkPipelineCache pipelineCache,
1117 uint32_t count,
1118 const VkGraphicsPipelineCreateInfo *pCreateInfos,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001119 const VkAllocationCallbacks* pAllocator,
Jon Ashburnc669cc62015-07-09 15:02:25 -06001120 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001121{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001122 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes36a372f2015-07-24 13:53:47 +12001123 bool pass = true;
1124 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001125 pass = validate_graphics_pipeline(my_data, device, &pCreateInfos[i]) && pass;
Chris Forbes36a372f2015-07-24 13:53:47 +12001126 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001127
1128 if (pass) {
1129 /* The driver is allowed to crash if passed junk. Only actually create the
1130 * pipeline if we didn't run into any showstoppers above.
1131 */
Chia-I Wuf7458c52015-10-26 21:10:41 +08001132 return my_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001133 }
1134 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001135 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001136 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001137}
1138
1139
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001140VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Chris Forbesb1dee272015-07-03 13:50:24 +12001141{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001142 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001143 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Chris Forbesb1dee272015-07-03 13:50:24 +12001144 if (result == VK_SUCCESS) {
1145 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001146 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1147 }
1148 return result;
1149}
Chris Forbes39d8d752015-06-04 20:27:09 +12001150
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001151/* hook DextroyDevice to remove tableMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001152VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001153{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001154 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001155 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001156 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001157 delete my_device_data->device_dispatch_table;
1158 layer_data_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001159}
1160
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001161VkResult VKAPI vkCreateInstance(
1162 const VkInstanceCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001163 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001164 VkInstance* pInstance)
1165{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001166 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1167 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +08001168 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001169
1170 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001171 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1172 my_data->report_data = debug_report_create_instance(
1173 pTable,
1174 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +08001175 pCreateInfo->enabledExtensionNameCount,
Chris Forbesb1dee272015-07-03 13:50:24 +12001176 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001177
Chris Forbesb1dee272015-07-03 13:50:24 +12001178 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001179 }
1180 return result;
1181}
1182
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001183/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001184VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001185{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001186 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001187 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001188 my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
Chris Forbesb1dee272015-07-03 13:50:24 +12001189
1190 // Clean up logging callback, if any
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001191 while (my_data->logging_callback.size() > 0) {
1192 VkDbgMsgCallback callback = my_data->logging_callback.back();
1193 layer_destroy_msg_callback(my_data->report_data, callback);
1194 my_data->logging_callback.pop_back();
Chris Forbesb1dee272015-07-03 13:50:24 +12001195 }
1196
1197 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001198 delete my_data->instance_dispatch_table;
1199 layer_data_map.erase(key);
1200 if (layer_data_map.empty()) {
1201 // Release mutex when destroying last instance.
1202 loader_platform_thread_delete_mutex(&globalLock);
1203 globalLockInitialized = 0;
1204 }
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001205}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001206
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001207VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001208 VkInstance instance,
1209 VkFlags msgFlags,
1210 const PFN_vkDbgMsgCallback pfnMsgCallback,
1211 void* pUserData,
1212 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001213{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001214 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1215 VkResult res = my_data->instance_dispatch_table->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001216 if (VK_SUCCESS == res) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001217 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1218 }
1219 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001220}
1221
1222VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001223 VkInstance instance,
1224 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001225{
Chris Forbesb1dee272015-07-03 13:50:24 +12001226 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001227 VkResult res = my_data->instance_dispatch_table->DbgDestroyMsgCallback(instance, msgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001228 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1229 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001230}
1231
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001232VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001233{
Chris Forbesb1dee272015-07-03 13:50:24 +12001234 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001235 return NULL;
1236
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001237 layer_data *my_data;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001238 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001239 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001240 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
1241 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
1242 my_data->device_dispatch_table = new VkLayerDispatchTable;
1243 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001244 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001245 }
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001246 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Jon Ashburn8fd08252015-05-28 16:25:02 -06001247
Chris Forbes2778f302015-04-02 13:22:31 +13001248#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001249 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001250 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001251
Chris Forbesb1dee272015-07-03 13:50:24 +12001252 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001253 ADD_HOOK(vkCreateShaderModule);
Chia-I Wu08accc62015-07-07 11:50:03 +08001254 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001255 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001256 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001257 ADD_HOOK(vkCreateDescriptorSetLayout);
1258 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001259#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001260
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001261 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001262 {
1263 if (pTable->GetDeviceProcAddr == NULL)
1264 return NULL;
1265 return pTable->GetDeviceProcAddr(dev, funcName);
1266 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001267}
1268
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001269VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001270{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001271 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001272
Chris Forbesb1dee272015-07-03 13:50:24 +12001273 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001274 return NULL;
1275
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001276 layer_data *my_data;
Chris Forbesb1dee272015-07-03 13:50:24 +12001277 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001278 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
1279 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
1280 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
1281 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001282 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001283 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001284#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001285 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001286 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001287
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001288 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001289 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001290 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1291 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1292 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1293 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001294#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001295
Chris Forbesb1dee272015-07-03 13:50:24 +12001296
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001297 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001298 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001299 if (fptr)
1300 return fptr;
1301
Chris Forbesb1dee272015-07-03 13:50:24 +12001302 {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001303 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001304 if (pTable->GetInstanceProcAddr == NULL)
1305 return NULL;
1306 return pTable->GetInstanceProcAddr(instance, funcName);
1307 }
Chris Forbes2778f302015-04-02 13:22:31 +13001308}