blob: c3c68e09413479c3be601575f57d5bc203113f6d [file] [log] [blame]
Chris Forbes2778f302015-04-02 13:22:31 +13001/*
Chris Forbes2778f302015-04-02 13:22:31 +13002 *
Courtney Goeltzenleuchterfcbe16f2015-10-29 13:50:34 -06003 * Copyright (C) 2015 Valve Corporation
Chris Forbes2778f302015-04-02 13:22:31 +13004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060022 *
23 * Author: Chia-I Wu <olv@lunarg.com>
24 * Author: Chris Forbes <chrisf@ijw.co.nz>
Chris Forbes2778f302015-04-02 13:22:31 +130025 */
26#include <string.h>
27#include <stdlib.h>
28#include <assert.h>
Chris Forbes06e8fc32015-04-13 12:14:52 +120029#include <map>
Chris Forbes2778f302015-04-02 13:22:31 +130030#include <unordered_map>
Chia-I Wud46e6ae2015-10-31 00:31:16 +080031#include <unordered_set>
Chris Forbes41002452015-04-08 10:19:16 +120032#include <map>
Chris Forbes3b1c4212015-04-08 10:11:59 +120033#include <vector>
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -060034#include <string>
Tobin Ehlisb80b4252015-09-01 11:59:36 -060035#include <iostream>
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060036#include "vk_loader_platform.h"
Chris Forbes2778f302015-04-02 13:22:31 +130037#include "vk_dispatch_table_helper.h"
David Pinedo9316d3b2015-11-06 12:54:48 -070038#include "vulkan/vk_layer.h"
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -060039#include "vk_layer_utils.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060040#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060041#include "vk_layer_table.h"
Chris Forbes401784b2015-05-04 14:04:24 +120042#include "vk_enum_string_helper.h"
Chris Forbes6b2ead62015-04-17 10:13:28 +120043#include "shader_checker.h"
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -060044#include "vk_layer_extension_utils.h"
Chris Forbes2778f302015-04-02 13:22:31 +130045
GregF7c45bed2015-07-21 17:22:50 -060046#include "spirv/spirv.hpp"
Chris Forbes2778f302015-04-02 13:22:31 +130047
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060048// fwd decls
49struct shader_module;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060050struct render_pass;
Chris Forbes2778f302015-04-02 13:22:31 +130051
Cody Northrop55443ef2015-09-28 15:09:32 -060052struct layer_data {
Chris Forbesb1dee272015-07-03 13:50:24 +120053 debug_report_data *report_data;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -060054 std::vector<VkDbgMsgCallback> logging_callback;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060055 VkLayerDispatchTable* device_dispatch_table;
56 VkLayerInstanceDispatchTable* instance_dispatch_table;
57
58 std::unordered_map<VkShaderModule, shader_module *> shader_module_map;
Chia-I Wud46e6ae2015-10-31 00:31:16 +080059 std::unordered_map<VkDescriptorSetLayout, std::unordered_set<uint32_t>*> descriptor_set_layout_map;
60 std::unordered_map<VkPipelineLayout, std::vector<std::unordered_set<uint32_t>*>*> pipeline_layout_map;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060061 std::unordered_map<VkRenderPass, render_pass *> render_pass_map;
Cody Northrop55443ef2015-09-28 15:09:32 -060062
63 layer_data() :
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060064 report_data(nullptr),
65 device_dispatch_table(nullptr),
66 instance_dispatch_table(nullptr)
Cody Northrop55443ef2015-09-28 15:09:32 -060067 {};
68};
Chris Forbesb1dee272015-07-03 13:50:24 +120069
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060070static void
71build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index);
Chris Forbesb1dee272015-07-03 13:50:24 +120072
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060073struct shader_module {
74 /* the spirv image itself */
75 std::vector<uint32_t> words;
76 /* a mapping of <id> to the first word of its def. this is useful because walking type
77 * trees requires jumping all over the instruction stream.
78 */
79 std::unordered_map<unsigned, unsigned> type_def_index;
80
81 shader_module(VkShaderModuleCreateInfo const *pCreateInfo) :
82 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
83 type_def_index() {
84
85 build_type_def_index(words, type_def_index);
86 }
87};
88
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060089struct render_pass {
90 std::vector<std::vector<VkFormat>> subpass_color_formats;
91
92 render_pass(VkRenderPassCreateInfo const *pCreateInfo)
93 {
94 uint32_t i;
95
96 subpass_color_formats.reserve(pCreateInfo->subpassCount);
97 for (i = 0; i < pCreateInfo->subpassCount; i++) {
98 const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i];
99 std::vector<VkFormat> color_formats;
100 uint32_t j;
101
Chia-I Wud50a7d72015-10-26 20:48:51 +0800102 color_formats.reserve(subpass->colorAttachmentCount);
103 for (j = 0; j < subpass->colorAttachmentCount; j++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600104 const uint32_t att = subpass->pColorAttachments[j].attachment;
105 const VkFormat format = pCreateInfo->pAttachments[att].format;
106
107 color_formats.push_back(pCreateInfo->pAttachments[att].format);
108 }
109
110 subpass_color_formats.push_back(color_formats);
111 }
112 }
113};
114
115static std::unordered_map<void *, layer_data *> layer_data_map;
Chris Forbesb1dee272015-07-03 13:50:24 +1200116
117template layer_data *get_my_data_ptr<layer_data>(
118 void *data_key,
119 std::unordered_map<void *, layer_data *> &data_map);
120
Chris Forbesb6b8c462015-04-15 06:59:41 +1200121static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +1200122// TODO : This can be much smarter, using separate locks for separate global data
123static int globalLockInitialized = 0;
124static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200125
Chia-I Wu9ab61502015-11-06 06:42:02 +0800126VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDescriptorSetLayout(
Chris Forbes2b8493a2015-08-12 15:03:22 +1200127 VkDevice device,
128 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800129 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200130 VkDescriptorSetLayout* pSetLayout)
131{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600132 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200133 /* stash a copy of the layout bindings */
Chia-I Wuf7458c52015-10-26 21:10:41 +0800134 VkResult result = my_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200135
136 if (VK_SUCCESS == result) {
137 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600138 auto& bindings = my_data->descriptor_set_layout_map[*pSetLayout];
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800139 bindings = new std::unordered_set<uint32_t>();
140 bindings->reserve(pCreateInfo->bindingCount);
141 for (uint32_t i = 0; i < pCreateInfo->bindingCount; i++)
142 bindings->insert(pCreateInfo->pBinding[i].binding);
143
Chris Forbes2b8493a2015-08-12 15:03:22 +1200144 loader_platform_thread_unlock_mutex(&globalLock);
145 }
146
147 return result;
148}
149
Chia-I Wu9ab61502015-11-06 06:42:02 +0800150VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreatePipelineLayout(
Chris Forbes2b8493a2015-08-12 15:03:22 +1200151 VkDevice device,
152 const VkPipelineLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800153 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200154 VkPipelineLayout* pPipelineLayout)
155{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600156 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800157 VkResult result = my_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200158
159 if (VK_SUCCESS == result) {
160 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600161 auto& layouts = my_data->pipeline_layout_map[*pPipelineLayout];
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800162 layouts = new std::vector<std::unordered_set<uint32_t>*>();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800163 layouts->reserve(pCreateInfo->setLayoutCount);
164 for (unsigned i = 0; i < pCreateInfo->setLayoutCount; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600165 layouts->push_back(my_data->descriptor_set_layout_map[pCreateInfo->pSetLayouts[i]]);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200166 }
167 loader_platform_thread_unlock_mutex(&globalLock);
168 }
169
170 return result;
171}
172
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200173static void
174build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
175{
176 unsigned int const *code = (unsigned int const *)&words[0];
177 size_t size = words.size();
178
179 unsigned word = 5;
180 while (word < size) {
181 unsigned opcode = code[word] & 0x0ffffu;
182 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
183
184 switch (opcode) {
185 case spv::OpTypeVoid:
186 case spv::OpTypeBool:
187 case spv::OpTypeInt:
188 case spv::OpTypeFloat:
189 case spv::OpTypeVector:
190 case spv::OpTypeMatrix:
GregF7c45bed2015-07-21 17:22:50 -0600191 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200192 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600193 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200194 case spv::OpTypeArray:
195 case spv::OpTypeRuntimeArray:
196 case spv::OpTypeStruct:
197 case spv::OpTypeOpaque:
198 case spv::OpTypePointer:
199 case spv::OpTypeFunction:
200 case spv::OpTypeEvent:
201 case spv::OpTypeDeviceEvent:
202 case spv::OpTypeReserveId:
203 case spv::OpTypeQueue:
204 case spv::OpTypePipe:
205 type_def_index[code[word+1]] = word;
206 break;
207
208 default:
209 /* We only care about type definitions */
210 break;
211 }
212
213 word += oplen;
214 }
215}
216
Chris Forbes46794b82015-09-18 11:40:23 +1200217bool
218shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo)
219{
220 uint32_t *words = (uint32_t *)pCreateInfo->pCode;
Courtney Goeltzenleuchter8f8367e2015-10-07 08:38:30 -0600221 size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
Chris Forbes46794b82015-09-18 11:40:23 +1200222
223 /* Just validate that the header makes sense. */
224 return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
225}
226
Chris Forbesb6b8c462015-04-15 06:59:41 +1200227static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200228init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200229{
Chris Forbesb1dee272015-07-03 13:50:24 +1200230 uint32_t report_flags = 0;
231 uint32_t debug_action = 0;
232 FILE *log_output = NULL;
233 const char *option_str;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600234 VkDbgMsgCallback callback;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200235 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200236 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
237 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200238
Chris Forbesb1dee272015-07-03 13:50:24 +1200239 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200240 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200241 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600242 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600243 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &callback);
244 my_data->logging_callback.push_back(callback);
245 }
246
247 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
248 layer_create_msg_callback(my_data->report_data, report_flags, win32_debug_output_msg, NULL, &callback);
249 my_data->logging_callback.push_back(callback);
Chris Forbesb1dee272015-07-03 13:50:24 +1200250 }
251
252 if (!globalLockInitialized)
253 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200254 loader_platform_thread_create_mutex(&globalLock);
255 globalLockInitialized = 1;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200256 }
257}
258
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600259static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600260 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600261 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600262 VK_API_VERSION,
263 VK_MAKE_VERSION(0, 1, 0),
264 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600265 }
Chris Forbes2778f302015-04-02 13:22:31 +1300266};
267
Chia-I Wu9ab61502015-11-06 06:42:02 +0800268VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600269 const char *pLayerName,
270 uint32_t *pCount,
271 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300272{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600273 /* shader checker does not have any global extensions */
274 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300275}
276
Chia-I Wu9ab61502015-11-06 06:42:02 +0800277VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600278 uint32_t *pCount,
279 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600280{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600281 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
282 shader_checker_global_layers,
283 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600284}
285
Chia-I Wu9ab61502015-11-06 06:42:02 +0800286VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600287 VkPhysicalDevice physicalDevice,
288 const char* pLayerName,
289 uint32_t* pCount,
290 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600291{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600292 /* Shader checker does not have any physical device extensions */
Jon Ashburn751c4842015-11-02 17:37:20 -0700293 if (pLayerName == NULL) {
294 dispatch_key key = get_dispatch_key(physicalDevice);
295 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Courtney Goeltzenleuchter6ed5dc22015-11-03 15:41:43 -0700296 return my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(
Jon Ashburn751c4842015-11-02 17:37:20 -0700297 physicalDevice,
298 NULL,
299 pCount,
300 pProperties);
301 } else {
302 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
303 }
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600304}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600305
Chia-I Wu9ab61502015-11-06 06:42:02 +0800306VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600307 VkPhysicalDevice physicalDevice,
308 uint32_t* pCount,
309 VkLayerProperties* pProperties)
310{
311 /* Shader checker physical device layers are the same as global */
312 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
313 shader_checker_global_layers,
314 pCount, pProperties);
Jon Ashburn207a3af2015-06-10 16:43:31 -0600315}
Chris Forbes2778f302015-04-02 13:22:31 +1300316
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200317static char const *
318storage_class_name(unsigned sc)
319{
320 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600321 case spv::StorageClassInput: return "input";
322 case spv::StorageClassOutput: return "output";
323 case spv::StorageClassUniformConstant: return "const uniform";
324 case spv::StorageClassUniform: return "uniform";
325 case spv::StorageClassWorkgroupLocal: return "workgroup local";
326 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
327 case spv::StorageClassPrivateGlobal: return "private global";
328 case spv::StorageClassFunction: return "function";
329 case spv::StorageClassGeneric: return "generic";
Cody Northrop97e52d82015-04-20 14:09:40 -0600330 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600331 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200332 default: return "unknown";
333 }
334}
335
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200336/* returns ptr to null terminator */
337static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600338describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200339{
340 auto type_def_it = src->type_def_index.find(type);
341
342 if (type_def_it == src->type_def_index.end()) {
343 return dst + sprintf(dst, "undef");
344 }
345
346 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
347 unsigned opcode = code[0] & 0x0ffffu;
348 switch (opcode) {
349 case spv::OpTypeBool:
350 return dst + sprintf(dst, "bool");
351 case spv::OpTypeInt:
352 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
353 case spv::OpTypeFloat:
354 return dst + sprintf(dst, "float%d", code[2]);
355 case spv::OpTypeVector:
356 dst += sprintf(dst, "vec%d of ", code[3]);
357 return describe_type(dst, src, code[2]);
358 case spv::OpTypeMatrix:
359 dst += sprintf(dst, "mat%d of ", code[3]);
360 return describe_type(dst, src, code[2]);
361 case spv::OpTypeArray:
362 dst += sprintf(dst, "arr[%d] of ", code[3]);
363 return describe_type(dst, src, code[2]);
364 case spv::OpTypePointer:
365 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
366 return describe_type(dst, src, code[3]);
367 case spv::OpTypeStruct:
368 {
369 unsigned oplen = code[0] >> 16;
370 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600371 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200372 dst = describe_type(dst, src, code[i]);
373 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
374 }
375 return dst;
376 }
Chris Forbes0ae15802015-08-14 12:04:39 +1200377 case spv::OpTypeSampler:
378 return dst + sprintf(dst, "sampler");
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200379 default:
380 return dst + sprintf(dst, "oddtype");
381 }
382}
383
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200384static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600385types_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 +1200386{
387 auto a_type_def_it = a->type_def_index.find(a_type);
388 auto b_type_def_it = b->type_def_index.find(b_type);
389
390 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200391 return false;
392 }
393
394 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200395 return false;
396 }
397
398 /* walk two type trees together, and complain about differences */
399 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
400 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
401
402 unsigned a_opcode = a_code[0] & 0x0ffffu;
403 unsigned b_opcode = b_code[0] & 0x0ffffu;
404
Chris Forbesf3fc0332015-06-05 14:57:05 +1200405 if (b_arrayed && b_opcode == spv::OpTypeArray) {
406 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
407 return types_match(a, b, a_type, b_code[2], false);
408 }
409
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200410 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200411 return false;
412 }
413
414 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200415 /* 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 +1200416 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200417 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200418 case spv::OpTypeInt:
419 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200420 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200421 case spv::OpTypeFloat:
422 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200423 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200424 case spv::OpTypeVector:
425 case spv::OpTypeMatrix:
426 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200427 /* match on element type, count. these all have the same layout. we don't get here if
428 * b_arrayed -- that is handled above. */
429 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 +1200430 case spv::OpTypeStruct:
431 /* match on all element types */
432 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200433 if (b_arrayed) {
434 /* for the purposes of matching different levels of arrayness, structs are leaves. */
435 return false;
436 }
437
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200438 unsigned a_len = a_code[0] >> 16;
439 unsigned b_len = b_code[0] >> 16;
440
441 if (a_len != b_len) {
442 return false; /* structs cannot match if member counts differ */
443 }
444
Ian Elliott1cb62222015-04-17 11:05:04 -0600445 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200446 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200447 return false;
448 }
449 }
450
451 return true;
452 }
453 case spv::OpTypePointer:
454 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200455 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200456
457 default:
458 /* remaining types are CLisms, or may not appear in the interfaces we
459 * are interested in. Just claim no match.
460 */
461 return false;
462
463 }
464}
465
Chris Forbes06e8fc32015-04-13 12:14:52 +1200466static int
467value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
468{
469 auto it = map.find(id);
470 if (it == map.end())
471 return def;
472 else
473 return it->second;
474}
475
Chris Forbes06e8fc32015-04-13 12:14:52 +1200476struct interface_var {
477 uint32_t id;
478 uint32_t type_id;
479 /* TODO: collect the name, too? Isn't required to be present. */
480};
481
Chris Forbes06e8fc32015-04-13 12:14:52 +1200482static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600483collect_interface_by_location(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200484 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200485 std::map<uint32_t, interface_var> &out,
486 std::map<uint32_t, interface_var> &builtins_out)
487{
488 unsigned int const *code = (unsigned int const *)&src->words[0];
489 size_t size = src->words.size();
490
Chris Forbes06e8fc32015-04-13 12:14:52 +1200491 std::unordered_map<unsigned, unsigned> var_locations;
492 std::unordered_map<unsigned, unsigned> var_builtins;
493
494 unsigned word = 5;
495 while (word < size) {
496
497 unsigned opcode = code[word] & 0x0ffffu;
498 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
499
500 /* We consider two interface models: SSO rendezvous-by-location, and
501 * builtins. Complain about anything that fits neither model.
502 */
503 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600504 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200505 var_locations[code[word+1]] = code[word+3];
506 }
507
Cody Northrop97e52d82015-04-20 14:09:40 -0600508 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200509 var_builtins[code[word+1]] = code[word+3];
510 }
511 }
512
513 /* TODO: handle grouped decorations */
514 /* TODO: handle index=1 dual source outputs from FS -- two vars will
515 * have the same location, and we DONT want to clobber. */
516
Ian Elliott1cb62222015-04-17 11:05:04 -0600517 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200518 int location = value_or_default(var_locations, code[word+2], -1);
519 int builtin = value_or_default(var_builtins, code[word+2], -1);
520
521 if (location == -1 && builtin == -1) {
522 /* No location defined, and not bound to an API builtin.
523 * The spec says nothing about how this case works (or doesn't)
524 * for interface matching.
525 */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600526 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 +1200527 "var %d (type %d) in %s interface has no Location or Builtin decoration",
528 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes06e8fc32015-04-13 12:14:52 +1200529 }
530 else if (location != -1) {
531 /* A user-defined interface variable, with a location. */
532 interface_var v;
533 v.id = code[word+2];
534 v.type_id = code[word+1];
535 out[location] = v;
536 }
537 else {
538 /* A builtin interface variable */
539 interface_var v;
540 v.id = code[word+2];
541 v.type_id = code[word+1];
542 builtins_out[builtin] = v;
543 }
544 }
545
546 word += oplen;
547 }
548}
549
Chris Forbes0ae15802015-08-14 12:04:39 +1200550static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600551collect_interface_by_descriptor_slot(layer_data *my_data, VkDevice dev,
Chris Forbes0ae15802015-08-14 12:04:39 +1200552 shader_module const *src, spv::StorageClass sinterface,
553 std::map<std::pair<unsigned, unsigned>, interface_var> &out)
554{
555 unsigned int const *code = (unsigned int const *)&src->words[0];
556 size_t size = src->words.size();
557
558 std::unordered_map<unsigned, unsigned> var_sets;
559 std::unordered_map<unsigned, unsigned> var_bindings;
560
561 unsigned word = 5;
562 while (word < size) {
563
564 unsigned opcode = code[word] & 0x0ffffu;
565 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
566
567 /* We consider two interface models: SSO rendezvous-by-location, and
568 * builtins. Complain about anything that fits neither model.
569 */
570 if (opcode == spv::OpDecorate) {
571 if (code[word+2] == spv::DecorationDescriptorSet) {
572 var_sets[code[word+1]] = code[word+3];
573 }
574
575 if (code[word+2] == spv::DecorationBinding) {
576 var_bindings[code[word+1]] = code[word+3];
577 }
578 }
579
580 if (opcode == spv::OpVariable && (code[word+3] == spv::StorageClassUniform ||
581 code[word+3] == spv::StorageClassUniformConstant)) {
582 unsigned set = value_or_default(var_sets, code[word+2], 0);
583 unsigned binding = value_or_default(var_bindings, code[word+2], 0);
584
585 auto existing_it = out.find(std::make_pair(set, binding));
586 if (existing_it != out.end()) {
587 /* conflict within spv image */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600588 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 +1200589 SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
590 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
591 code[word+2], code[word+1], storage_class_name(sinterface),
592 existing_it->first.first, existing_it->first.second);
593 }
594
595 interface_var v;
596 v.id = code[word+2];
597 v.type_id = code[word+1];
598 out[std::make_pair(set, binding)] = v;
599 }
600
601 word += oplen;
602 }
603}
604
Chia-I Wu9ab61502015-11-06 06:42:02 +0800605VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateShaderModule(
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600606 VkDevice device,
607 const VkShaderModuleCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800608 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600609 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300610{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600611 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Michael Lentine8ea29962015-11-03 16:18:46 -0800612 bool skip_call = false;
Chris Forbes46794b82015-09-18 11:40:23 +1200613 if (!shader_is_spirv(pCreateInfo)) {
Michael Lentine8ea29962015-11-03 16:18:46 -0800614 skip_call |= log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE,
Chris Forbes46794b82015-09-18 11:40:23 +1200615 /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
616 "Shader is not SPIR-V");
Chris Forbes46794b82015-09-18 11:40:23 +1200617 }
618
Michael Lentine8ea29962015-11-03 16:18:46 -0800619 if (skip_call)
620 return VK_ERROR_VALIDATION_FAILED;
621
Chia-I Wuf7458c52015-10-26 21:10:41 +0800622 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200623
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600624 if (res == VK_SUCCESS) {
625 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600626 my_data->shader_module_map[*pShaderModule] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600627 loader_platform_thread_unlock_mutex(&globalLock);
628 }
Chris Forbes2778f302015-04-02 13:22:31 +1300629 return res;
630}
631
Chia-I Wu9ab61502015-11-06 06:42:02 +0800632VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateRenderPass(
Chia-I Wu08accc62015-07-07 11:50:03 +0800633 VkDevice device,
634 const VkRenderPassCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800635 const VkAllocationCallbacks* pAllocator,
Chia-I Wu08accc62015-07-07 11:50:03 +0800636 VkRenderPass *pRenderPass)
637{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600638 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800639 VkResult res = my_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800640
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600641 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600642 my_data->render_pass_map[*pRenderPass] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800643 loader_platform_thread_unlock_mutex(&globalLock);
644 return res;
645}
646
Chris Forbesee99b9b2015-05-25 11:13:22 +1200647static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600648validate_interface_between_stages(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200649 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600650 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200651 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200652{
653 std::map<uint32_t, interface_var> outputs;
654 std::map<uint32_t, interface_var> inputs;
655
656 std::map<uint32_t, interface_var> builtin_outputs;
657 std::map<uint32_t, interface_var> builtin_inputs;
658
Chris Forbesee99b9b2015-05-25 11:13:22 +1200659 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200660
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600661 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
662 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200663
664 auto a_it = outputs.begin();
665 auto b_it = inputs.begin();
666
667 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600668 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
669 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
670 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200671 auto a_first = a_at_end ? 0 : a_it->first;
672 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600673
Mike Stroyan19a6de22015-09-10 14:10:25 -0600674 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Mark Lobodzinski8abd8882015-11-06 10:24:23 -0700675 if (log_msg(my_data->report_data, VK_DBG_REPORT_PERF_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600676 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
677 pass = false;
678 }
Chris Forbes41002452015-04-08 10:19:16 +1200679 a_it++;
680 }
David Pinedod8f83d82015-04-27 16:36:17 -0600681 else if (a_at_end || a_first > b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600682 if (log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600683 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
684 pass = false;
685 }
Chris Forbes41002452015-04-08 10:19:16 +1200686 b_it++;
687 }
688 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200689 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 +1200690 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200691 }
692 else {
693 char producer_type[1024];
694 char consumer_type[1024];
695 describe_type(producer_type, producer, a_it->second.type_id);
696 describe_type(consumer_type, consumer, b_it->second.type_id);
697
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600698 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 -0600699 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
Chris Forbesee99b9b2015-05-25 11:13:22 +1200700 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600701 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200702 }
Chris Forbes41002452015-04-08 10:19:16 +1200703 a_it++;
704 b_it++;
705 }
706 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200707
708 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200709}
710
711
Chris Forbes3616b462015-04-08 10:37:20 +1200712enum FORMAT_TYPE {
713 FORMAT_TYPE_UNDEFINED,
714 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
715 FORMAT_TYPE_SINT,
716 FORMAT_TYPE_UINT,
717};
718
719
720static unsigned
721get_format_type(VkFormat fmt) {
722 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800723 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200724 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800725 case VK_FORMAT_R8_SINT:
726 case VK_FORMAT_R8G8_SINT:
727 case VK_FORMAT_R8G8B8_SINT:
728 case VK_FORMAT_R8G8B8A8_SINT:
729 case VK_FORMAT_R16_SINT:
730 case VK_FORMAT_R16G16_SINT:
731 case VK_FORMAT_R16G16B16_SINT:
732 case VK_FORMAT_R16G16B16A16_SINT:
733 case VK_FORMAT_R32_SINT:
734 case VK_FORMAT_R32G32_SINT:
735 case VK_FORMAT_R32G32B32_SINT:
736 case VK_FORMAT_R32G32B32A32_SINT:
737 case VK_FORMAT_B8G8R8_SINT:
738 case VK_FORMAT_B8G8R8A8_SINT:
Chia-I Wu935f2ed2015-11-10 17:01:22 +0800739 case VK_FORMAT_A2B10G10R10_SINT_PACK32:
740 case VK_FORMAT_A2R10G10B10_SINT_PACK32:
Chris Forbes3616b462015-04-08 10:37:20 +1200741 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800742 case VK_FORMAT_R8_UINT:
743 case VK_FORMAT_R8G8_UINT:
744 case VK_FORMAT_R8G8B8_UINT:
745 case VK_FORMAT_R8G8B8A8_UINT:
746 case VK_FORMAT_R16_UINT:
747 case VK_FORMAT_R16G16_UINT:
748 case VK_FORMAT_R16G16B16_UINT:
749 case VK_FORMAT_R16G16B16A16_UINT:
750 case VK_FORMAT_R32_UINT:
751 case VK_FORMAT_R32G32_UINT:
752 case VK_FORMAT_R32G32B32_UINT:
753 case VK_FORMAT_R32G32B32A32_UINT:
754 case VK_FORMAT_B8G8R8_UINT:
755 case VK_FORMAT_B8G8R8A8_UINT:
Chia-I Wu935f2ed2015-11-10 17:01:22 +0800756 case VK_FORMAT_A2B10G10R10_UINT_PACK32:
757 case VK_FORMAT_A2R10G10B10_UINT_PACK32:
Chris Forbes3616b462015-04-08 10:37:20 +1200758 return FORMAT_TYPE_UINT;
759 default:
760 return FORMAT_TYPE_FLOAT;
761 }
762}
763
764
Chris Forbes156a1162015-05-04 14:04:06 +1200765/* characterizes a SPIR-V type appearing in an interface to a FF stage,
766 * for comparison to a VkFormat's characterization above. */
767static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600768get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200769{
770 auto type_def_it = src->type_def_index.find(type);
771
772 if (type_def_it == src->type_def_index.end()) {
773 return FORMAT_TYPE_UNDEFINED;
774 }
775
776 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
777 unsigned opcode = code[0] & 0x0ffffu;
778 switch (opcode) {
779 case spv::OpTypeInt:
780 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
781 case spv::OpTypeFloat:
782 return FORMAT_TYPE_FLOAT;
783 case spv::OpTypeVector:
784 return get_fundamental_type(src, code[2]);
785 case spv::OpTypeMatrix:
786 return get_fundamental_type(src, code[2]);
787 case spv::OpTypeArray:
788 return get_fundamental_type(src, code[2]);
789 case spv::OpTypePointer:
790 return get_fundamental_type(src, code[3]);
791 default:
792 return FORMAT_TYPE_UNDEFINED;
793 }
794}
795
796
Chris Forbesee99b9b2015-05-25 11:13:22 +1200797static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600798validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200799{
800 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
801 * each binding should be specified only once.
802 */
803 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200804 bool pass = true;
805
Chia-I Wud50a7d72015-10-26 20:48:51 +0800806 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes280ba2c2015-06-12 11:16:41 +1200807 auto desc = &vi->pVertexBindingDescriptions[i];
808 auto & binding = bindings[desc->binding];
809 if (binding) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600810 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 -0600811 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
812 pass = false;
813 }
Chris Forbes280ba2c2015-06-12 11:16:41 +1200814 }
815 else {
816 binding = desc;
817 }
818 }
819
820 return pass;
821}
822
823
824static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600825validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200826{
827 std::map<uint32_t, interface_var> inputs;
828 /* we collect builtin inputs, but they will never appear in the VI state --
829 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
830 */
831 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200832 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200833
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600834 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200835
836 /* Build index by location */
837 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200838 if (vi) {
Chia-I Wud50a7d72015-10-26 20:48:51 +0800839 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
Chris Forbes7191cd52015-05-25 11:13:24 +1200840 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
841 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200842
843 auto it_a = attribs.begin();
844 auto it_b = inputs.begin();
845
David Pinedod8f83d82015-04-27 16:36:17 -0600846 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
847 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
848 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200849 auto a_first = a_at_end ? 0 : it_a->first;
850 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600851 if (b_at_end || a_first < b_first) {
Mark Lobodzinski8abd8882015-11-06 10:24:23 -0700852 if (log_msg(my_data->report_data, VK_DBG_REPORT_PERF_WARN_BIT, VK_OBJECT_TYPE_DEVICE, /*dev*/0, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
Mike Stroyan830368a2015-09-10 14:12:01 -0600853 "Vertex attribute at location %d not consumed by VS", a_first)) {
854 pass = false;
855 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200856 it_a++;
857 }
David Pinedod8f83d82015-04-27 16:36:17 -0600858 else if (a_at_end || b_first < a_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600859 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 -0600860 "VS consumes input at location %d but not provided", b_first)) {
861 pass = false;
862 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200863 it_b++;
864 }
865 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200866 unsigned attrib_type = get_format_type(it_a->second->format);
867 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
868
869 /* type checking */
870 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
871 char vs_type[1024];
872 describe_type(vs_type, vs, it_b->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600873 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 +1200874 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600875 string_VkFormat(it_a->second->format), a_first, vs_type)) {
876 pass = false;
877 }
Chris Forbes401784b2015-05-04 14:04:24 +1200878 }
879
Chris Forbes6b2ead62015-04-17 10:13:28 +1200880 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200881 it_a++;
882 it_b++;
883 }
884 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200885
886 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200887}
888
889
Chris Forbesee99b9b2015-05-25 11:13:22 +1200890static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600891validate_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 +1200892{
Chia-I Wu08accc62015-07-07 11:50:03 +0800893 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200894 std::map<uint32_t, interface_var> outputs;
895 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200896 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200897
898 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
899
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600900 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200901
902 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
903 * and all color attachment should be UNORM/SNORM/FLOAT.
904 */
905 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200906 if (outputs.size()) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600907 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 -0600908 "Should not have user-defined FS outputs when using broadcast")) {
909 pass = false;
910 }
Chris Forbes3616b462015-04-08 10:37:20 +1200911 }
912
Chia-I Wu08accc62015-07-07 11:50:03 +0800913 for (unsigned i = 0; i < color_formats.size(); i++) {
914 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200915 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600916 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 -0600917 "CB format should not be SINT or UINT when using broadcast")) {
918 pass = false;
919 }
Chris Forbes3616b462015-04-08 10:37:20 +1200920 }
921 }
922
Chris Forbesee99b9b2015-05-25 11:13:22 +1200923 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200924 }
925
926 auto it = outputs.begin();
927 uint32_t attachment = 0;
928
929 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
930 * are currently dense, but the parallel with matching between shader stages is nice.
931 */
932
Chris Forbes9715d4a2015-07-10 09:06:54 +1200933 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200934 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
935 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600936 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 -0600937 "FS writes to output location %d with no matching attachment", it->first)) {
938 pass = false;
939 }
Chris Forbes3616b462015-04-08 10:37:20 +1200940 it++;
941 }
942 else if (it == outputs.end() || it->first > attachment) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600943 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 -0600944 "Attachment %d not written by FS", attachment)) {
945 pass = false;
946 }
Chris Forbes3616b462015-04-08 10:37:20 +1200947 attachment++;
948 }
949 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200950 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800951 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200952
953 /* type checking */
954 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
955 char fs_type[1024];
956 describe_type(fs_type, fs, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600957 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 +1200958 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600959 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
960 pass = false;
961 }
Chris Forbes46d31e52015-05-04 14:20:10 +1200962 }
963
Chris Forbes6b2ead62015-04-17 10:13:28 +1200964 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200965 it++;
966 attachment++;
967 }
968 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200969
970 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200971}
972
973
Chris Forbesf044ec92015-06-05 15:01:08 +1200974struct shader_stage_attributes {
975 char const * const name;
976 bool arrayed_input;
977};
978
979
980static shader_stage_attributes
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600981shader_stage_attribs[] = {
Chris Forbesf044ec92015-06-05 15:01:08 +1200982 { "vertex shader", false },
983 { "tessellation control shader", true },
984 { "tessellation evaluation shader", false },
985 { "geometry shader", true },
986 { "fragment shader", false },
987};
988
989
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800990static bool
991has_descriptor_binding(std::vector<std::unordered_set<uint32_t>*>* layout,
992 std::pair<unsigned, unsigned> slot)
Chris Forbes556c76c2015-08-14 12:04:59 +1200993{
994 if (!layout)
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800995 return false;
Chris Forbes556c76c2015-08-14 12:04:59 +1200996
997 if (slot.first >= layout->size())
Chia-I Wud46e6ae2015-10-31 00:31:16 +0800998 return false;
Chris Forbes556c76c2015-08-14 12:04:59 +1200999
1000 auto set = (*layout)[slot.first];
1001
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001002 return (set->find(slot.second) != set->end());
Chris Forbes556c76c2015-08-14 12:04:59 +12001003}
1004
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001005static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1006{
1007 uint32_t bit_pos = u_ffs(stage);
1008 return bit_pos-1;
1009}
Chris Forbes556c76c2015-08-14 12:04:59 +12001010
Chris Forbes81874ba2015-06-04 20:23:00 +12001011static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001012validate_graphics_pipeline(layer_data *my_data, VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001013{
Chris Forbesf6800b52015-04-08 10:16:45 +12001014 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1015 * before trying to do anything more: */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001016 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1017 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1018 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbesf6800b52015-04-08 10:16:45 +12001019
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001020 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1021 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Chia-I Wu08accc62015-07-07 11:50:03 +08001022 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001023 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001024 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001025
Chris Forbes7f963832015-05-29 14:55:18 +12001026 loader_platform_thread_lock_mutex(&globalLock);
1027
Tony Barbour92503c62015-07-13 15:28:47 -06001028 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001029 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1030 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001031
Chia-I Wu28e06912015-10-31 00:31:16 +08001032 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 -06001033 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001034 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 +08001035 "Unknown shader stage %d", pStage->stage)) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001036 pass = false;
1037 }
Chris Forbes6b2ead62015-04-17 10:13:28 +12001038 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001039 else {
Chia-I Wu28e06912015-10-31 00:31:16 +08001040 shader_module *module = my_data->shader_module_map[pStage->module];
1041 shaders[get_shader_stage_id(pStage->stage)] = module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001042
1043 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001044 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
Chia-I Wu28e06912015-10-31 00:31:16 +08001045 collect_interface_by_descriptor_slot(my_data, dev, module, spv::StorageClassUniform,
Chris Forbes46794b82015-09-18 11:40:23 +12001046 descriptor_uses);
Chris Forbes556c76c2015-08-14 12:04:59 +12001047
Chia-I Wue2fc5522015-10-26 20:04:44 +08001048 auto layout = pCreateInfo->layout != VK_NULL_HANDLE ?
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001049 my_data->pipeline_layout_map[pCreateInfo->layout] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001050
Chris Forbes46794b82015-09-18 11:40:23 +12001051 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001052
Chris Forbes46794b82015-09-18 11:40:23 +12001053 /* find the matching binding */
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001054 auto found = has_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001055
Chia-I Wud46e6ae2015-10-31 00:31:16 +08001056 if (!found) {
Chris Forbes46794b82015-09-18 11:40:23 +12001057 char type_name[1024];
Chia-I Wu28e06912015-10-31 00:31:16 +08001058 describe_type(type_name, module, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001059 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 +12001060 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1061 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001062 it->first.first, it->first.second, type_name)) {
1063 pass = false;
1064 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001065 }
1066 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001067 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001068 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001069 }
1070
Chia-I Wu08accc62015-07-07 11:50:03 +08001071 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001072 rp = my_data->render_pass_map[pCreateInfo->renderPass];
Chia-I Wu08accc62015-07-07 11:50:03 +08001073
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001074 vi = pCreateInfo->pVertexInputState;
1075
Chris Forbes280ba2c2015-06-12 11:16:41 +12001076 if (vi) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001077 pass = validate_vi_consistency(my_data, dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001078 }
1079
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001080 if (shaders[vertex_stage]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001081 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001082 }
1083
Chris Forbesf044ec92015-06-05 15:01:08 +12001084 /* TODO: enforce rules about present combinations of shaders */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001085 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1086 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
Chris Forbesf044ec92015-06-05 15:01:08 +12001087
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001088 while (!shaders[producer] && producer != fragment_stage) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001089 producer++;
1090 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001091 }
1092
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001093 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001094 assert(shaders[producer]);
1095 if (shaders[consumer]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001096 pass = validate_interface_between_stages(my_data, dev,
Chris Forbes46794b82015-09-18 11:40:23 +12001097 shaders[producer], shader_stage_attribs[producer].name,
1098 shaders[consumer], shader_stage_attribs[consumer].name,
1099 shader_stage_attribs[consumer].arrayed_input) && pass;
Chris Forbesf044ec92015-06-05 15:01:08 +12001100
1101 producer = consumer;
1102 }
1103 }
1104
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001105 if (shaders[fragment_stage] && rp) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001106 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001107 }
1108
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001109 delete shaders;
1110
Chris Forbes7f963832015-05-29 14:55:18 +12001111 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001112 return pass;
1113}
1114
Jon Ashburnc669cc62015-07-09 15:02:25 -06001115//TODO handle pipelineCache entry points
Chia-I Wu9ab61502015-11-06 06:42:02 +08001116VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL
Jon Ashburnc669cc62015-07-09 15:02:25 -06001117vkCreateGraphicsPipelines(VkDevice device,
1118 VkPipelineCache pipelineCache,
1119 uint32_t count,
1120 const VkGraphicsPipelineCreateInfo *pCreateInfos,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001121 const VkAllocationCallbacks* pAllocator,
Jon Ashburnc669cc62015-07-09 15:02:25 -06001122 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001123{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001124 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes36a372f2015-07-24 13:53:47 +12001125 bool pass = true;
1126 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001127 pass = validate_graphics_pipeline(my_data, device, &pCreateInfos[i]) && pass;
Chris Forbes36a372f2015-07-24 13:53:47 +12001128 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001129
1130 if (pass) {
1131 /* The driver is allowed to crash if passed junk. Only actually create the
1132 * pipeline if we didn't run into any showstoppers above.
1133 */
Chia-I Wuf7458c52015-10-26 21:10:41 +08001134 return my_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001135 }
1136 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001137 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001138 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001139}
1140
1141
Chia-I Wu9ab61502015-11-06 06:42:02 +08001142VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Chris Forbesb1dee272015-07-03 13:50:24 +12001143{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001144 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001145 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Chris Forbesb1dee272015-07-03 13:50:24 +12001146 if (result == VK_SUCCESS) {
1147 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001148 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1149 }
1150 return result;
1151}
Chris Forbes39d8d752015-06-04 20:27:09 +12001152
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001153/* hook DextroyDevice to remove tableMap entry */
Chia-I Wu9ab61502015-11-06 06:42:02 +08001154VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001155{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001156 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001157 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001158 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001159 delete my_device_data->device_dispatch_table;
1160 layer_data_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001161}
1162
Chia-I Wu9ab61502015-11-06 06:42:02 +08001163VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001164 const VkInstanceCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001165 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001166 VkInstance* pInstance)
1167{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001168 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1169 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +08001170 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001171
1172 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001173 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1174 my_data->report_data = debug_report_create_instance(
1175 pTable,
1176 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +08001177 pCreateInfo->enabledExtensionNameCount,
Chris Forbesb1dee272015-07-03 13:50:24 +12001178 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001179
Chris Forbesb1dee272015-07-03 13:50:24 +12001180 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001181 }
1182 return result;
1183}
1184
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001185/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu9ab61502015-11-06 06:42:02 +08001186VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001187{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001188 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001189 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001190 my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
Chris Forbesb1dee272015-07-03 13:50:24 +12001191
1192 // Clean up logging callback, if any
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001193 while (my_data->logging_callback.size() > 0) {
1194 VkDbgMsgCallback callback = my_data->logging_callback.back();
1195 layer_destroy_msg_callback(my_data->report_data, callback);
1196 my_data->logging_callback.pop_back();
Chris Forbesb1dee272015-07-03 13:50:24 +12001197 }
1198
1199 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001200 delete my_data->instance_dispatch_table;
1201 layer_data_map.erase(key);
1202 if (layer_data_map.empty()) {
1203 // Release mutex when destroying last instance.
1204 loader_platform_thread_delete_mutex(&globalLock);
1205 globalLockInitialized = 0;
1206 }
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001207}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001208
Chia-I Wu9ab61502015-11-06 06:42:02 +08001209VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001210 VkInstance instance,
1211 VkFlags msgFlags,
1212 const PFN_vkDbgMsgCallback pfnMsgCallback,
1213 void* pUserData,
1214 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001215{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001216 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1217 VkResult res = my_data->instance_dispatch_table->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001218 if (VK_SUCCESS == res) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001219 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1220 }
1221 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001222}
1223
Chia-I Wu9ab61502015-11-06 06:42:02 +08001224VK_LAYER_EXPORT VKAPI_ATTR VkResult VKAPI_CALL vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001225 VkInstance instance,
1226 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001227{
Chris Forbesb1dee272015-07-03 13:50:24 +12001228 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001229 VkResult res = my_data->instance_dispatch_table->DbgDestroyMsgCallback(instance, msgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001230 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1231 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001232}
1233
Chia-I Wu9ab61502015-11-06 06:42:02 +08001234VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001235{
Chris Forbesb1dee272015-07-03 13:50:24 +12001236 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001237 return NULL;
1238
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001239 layer_data *my_data;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001240 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001241 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001242 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
1243 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
1244 my_data->device_dispatch_table = new VkLayerDispatchTable;
1245 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001246 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001247 }
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001248 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Jon Ashburn8fd08252015-05-28 16:25:02 -06001249
Chris Forbes2778f302015-04-02 13:22:31 +13001250#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001251 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001252 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001253
Chris Forbesb1dee272015-07-03 13:50:24 +12001254 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001255 ADD_HOOK(vkCreateShaderModule);
Chia-I Wu08accc62015-07-07 11:50:03 +08001256 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001257 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001258 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001259 ADD_HOOK(vkCreateDescriptorSetLayout);
1260 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001261#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001262
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001263 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001264 {
1265 if (pTable->GetDeviceProcAddr == NULL)
1266 return NULL;
1267 return pTable->GetDeviceProcAddr(dev, funcName);
1268 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001269}
1270
Chia-I Wu9ab61502015-11-06 06:42:02 +08001271VK_LAYER_EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001272{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001273 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001274
Chris Forbesb1dee272015-07-03 13:50:24 +12001275 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001276 return NULL;
1277
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001278 layer_data *my_data;
Chris Forbesb1dee272015-07-03 13:50:24 +12001279 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001280 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
1281 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
1282 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
1283 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001284 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001285 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001286#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001287 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001288 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001289
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001290 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001291 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001292 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1293 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1294 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1295 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001296#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001297
Chris Forbesb1dee272015-07-03 13:50:24 +12001298
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001299 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001300 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001301 if (fptr)
1302 return fptr;
1303
Chris Forbesb1dee272015-07-03 13:50:24 +12001304 {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001305 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001306 if (pTable->GetInstanceProcAddr == NULL)
1307 return NULL;
1308 return pTable->GetInstanceProcAddr(instance, funcName);
1309 }
Chris Forbes2778f302015-04-02 13:22:31 +13001310}