blob: 63c17a0600f6b93f5fc95ea397d9907369991851 [file] [log] [blame]
Chris Forbes2778f302015-04-02 13:22:31 +13001/*
2 * Vulkan
3 *
4 * Copyright (C) 2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
Chris Forbes06e8fc32015-04-13 12:14:52 +120027#include <map>
Chris Forbes2778f302015-04-02 13:22:31 +130028#include <unordered_map>
Chris Forbes41002452015-04-08 10:19:16 +120029#include <map>
Chris Forbes3b1c4212015-04-08 10:11:59 +120030#include <vector>
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -060031#include <string>
Tobin Ehlisb80b4252015-09-01 11:59:36 -060032#include <iostream>
Tobin Ehlisb835d1b2015-07-03 10:34:49 -060033#include "vk_loader_platform.h"
Chris Forbes2778f302015-04-02 13:22:31 +130034#include "vk_dispatch_table_helper.h"
Tobin Ehlis0c6f9ee2015-07-03 09:42:57 -060035#include "vk_layer.h"
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -060036#include "vk_layer_utils.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060037#include "vk_layer_config.h"
Tobin Ehlisa0cb02e2015-07-03 10:15:26 -060038#include "vk_layer_table.h"
Chris Forbes401784b2015-05-04 14:04:24 +120039#include "vk_enum_string_helper.h"
Chris Forbes6b2ead62015-04-17 10:13:28 +120040#include "shader_checker.h"
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -060041#include "vk_layer_extension_utils.h"
Chris Forbes2778f302015-04-02 13:22:31 +130042
GregF7c45bed2015-07-21 17:22:50 -060043#include "spirv/spirv.hpp"
Chris Forbes2778f302015-04-02 13:22:31 +130044
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060045// fwd decls
46struct shader_module;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060047struct render_pass;
Chris Forbes2778f302015-04-02 13:22:31 +130048
Cody Northrop55443ef2015-09-28 15:09:32 -060049struct layer_data {
Chris Forbesb1dee272015-07-03 13:50:24 +120050 debug_report_data *report_data;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -060051 std::vector<VkDbgMsgCallback> logging_callback;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060052 VkLayerDispatchTable* device_dispatch_table;
53 VkLayerInstanceDispatchTable* instance_dispatch_table;
54
55 std::unordered_map<VkShaderModule, shader_module *> shader_module_map;
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060056 std::unordered_map<VkDescriptorSetLayout, std::vector<VkDescriptorSetLayoutBinding>*> descriptor_set_layout_map;
57 std::unordered_map<VkPipelineLayout, std::vector<std::vector<VkDescriptorSetLayoutBinding>*>*> pipeline_layout_map;
58 std::unordered_map<VkRenderPass, render_pass *> render_pass_map;
Cody Northrop55443ef2015-09-28 15:09:32 -060059
60 layer_data() :
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060061 report_data(nullptr),
62 device_dispatch_table(nullptr),
63 instance_dispatch_table(nullptr)
Cody Northrop55443ef2015-09-28 15:09:32 -060064 {};
65};
Chris Forbesb1dee272015-07-03 13:50:24 +120066
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060067static void
68build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index);
Chris Forbesb1dee272015-07-03 13:50:24 +120069
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060070struct shader_module {
71 /* the spirv image itself */
72 std::vector<uint32_t> words;
73 /* a mapping of <id> to the first word of its def. this is useful because walking type
74 * trees requires jumping all over the instruction stream.
75 */
76 std::unordered_map<unsigned, unsigned> type_def_index;
77
78 shader_module(VkShaderModuleCreateInfo const *pCreateInfo) :
79 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
80 type_def_index() {
81
82 build_type_def_index(words, type_def_index);
83 }
84};
85
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -060086struct render_pass {
87 std::vector<std::vector<VkFormat>> subpass_color_formats;
88
89 render_pass(VkRenderPassCreateInfo const *pCreateInfo)
90 {
91 uint32_t i;
92
93 subpass_color_formats.reserve(pCreateInfo->subpassCount);
94 for (i = 0; i < pCreateInfo->subpassCount; i++) {
95 const VkSubpassDescription *subpass = &pCreateInfo->pSubpasses[i];
96 std::vector<VkFormat> color_formats;
97 uint32_t j;
98
Chia-I Wud50a7d72015-10-26 20:48:51 +080099 color_formats.reserve(subpass->colorAttachmentCount);
100 for (j = 0; j < subpass->colorAttachmentCount; j++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600101 const uint32_t att = subpass->pColorAttachments[j].attachment;
102 const VkFormat format = pCreateInfo->pAttachments[att].format;
103
104 color_formats.push_back(pCreateInfo->pAttachments[att].format);
105 }
106
107 subpass_color_formats.push_back(color_formats);
108 }
109 }
110};
111
112static std::unordered_map<void *, layer_data *> layer_data_map;
Chris Forbesb1dee272015-07-03 13:50:24 +1200113
114template layer_data *get_my_data_ptr<layer_data>(
115 void *data_key,
116 std::unordered_map<void *, layer_data *> &data_map);
117
Chris Forbesb6b8c462015-04-15 06:59:41 +1200118static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +1200119// TODO : This can be much smarter, using separate locks for separate global data
120static int globalLockInitialized = 0;
121static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200122
Chris Forbes2b8493a2015-08-12 15:03:22 +1200123VK_LAYER_EXPORT VkResult VKAPI vkCreateDescriptorSetLayout(
124 VkDevice device,
125 const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800126 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200127 VkDescriptorSetLayout* pSetLayout)
128{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600129 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200130 /* stash a copy of the layout bindings */
Chia-I Wuf7458c52015-10-26 21:10:41 +0800131 VkResult result = my_data->device_dispatch_table->CreateDescriptorSetLayout(device, pCreateInfo, pAllocator, pSetLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200132
133 if (VK_SUCCESS == result) {
134 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600135 auto& bindings = my_data->descriptor_set_layout_map[*pSetLayout];
Chris Forbes2b8493a2015-08-12 15:03:22 +1200136 bindings = new std::vector<VkDescriptorSetLayoutBinding>(
Chia-I Wua745e512015-10-31 00:31:16 +0800137 pCreateInfo->pBinding, pCreateInfo->pBinding + pCreateInfo->bindingCount);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200138 loader_platform_thread_unlock_mutex(&globalLock);
139 }
140
141 return result;
142}
143
Chris Forbes2b8493a2015-08-12 15:03:22 +1200144VK_LAYER_EXPORT VkResult VKAPI vkCreatePipelineLayout(
145 VkDevice device,
146 const VkPipelineLayoutCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800147 const VkAllocationCallbacks* pAllocator,
Chris Forbes2b8493a2015-08-12 15:03:22 +1200148 VkPipelineLayout* pPipelineLayout)
149{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600150 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800151 VkResult result = my_data->device_dispatch_table->CreatePipelineLayout(device, pCreateInfo, pAllocator, pPipelineLayout);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200152
153 if (VK_SUCCESS == result) {
154 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600155 auto& layouts = my_data->pipeline_layout_map[*pPipelineLayout];
Chris Forbes2b8493a2015-08-12 15:03:22 +1200156 layouts = new std::vector<std::vector<VkDescriptorSetLayoutBinding>*>();
Chia-I Wud50a7d72015-10-26 20:48:51 +0800157 layouts->reserve(pCreateInfo->setLayoutCount);
158 for (unsigned i = 0; i < pCreateInfo->setLayoutCount; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600159 layouts->push_back(my_data->descriptor_set_layout_map[pCreateInfo->pSetLayouts[i]]);
Chris Forbes2b8493a2015-08-12 15:03:22 +1200160 }
161 loader_platform_thread_unlock_mutex(&globalLock);
162 }
163
164 return result;
165}
166
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200167static void
168build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
169{
170 unsigned int const *code = (unsigned int const *)&words[0];
171 size_t size = words.size();
172
173 unsigned word = 5;
174 while (word < size) {
175 unsigned opcode = code[word] & 0x0ffffu;
176 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
177
178 switch (opcode) {
179 case spv::OpTypeVoid:
180 case spv::OpTypeBool:
181 case spv::OpTypeInt:
182 case spv::OpTypeFloat:
183 case spv::OpTypeVector:
184 case spv::OpTypeMatrix:
GregF7c45bed2015-07-21 17:22:50 -0600185 case spv::OpTypeImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200186 case spv::OpTypeSampler:
GregF7c45bed2015-07-21 17:22:50 -0600187 case spv::OpTypeSampledImage:
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200188 case spv::OpTypeArray:
189 case spv::OpTypeRuntimeArray:
190 case spv::OpTypeStruct:
191 case spv::OpTypeOpaque:
192 case spv::OpTypePointer:
193 case spv::OpTypeFunction:
194 case spv::OpTypeEvent:
195 case spv::OpTypeDeviceEvent:
196 case spv::OpTypeReserveId:
197 case spv::OpTypeQueue:
198 case spv::OpTypePipe:
199 type_def_index[code[word+1]] = word;
200 break;
201
202 default:
203 /* We only care about type definitions */
204 break;
205 }
206
207 word += oplen;
208 }
209}
210
Chris Forbes46794b82015-09-18 11:40:23 +1200211bool
212shader_is_spirv(VkShaderModuleCreateInfo const *pCreateInfo)
213{
214 uint32_t *words = (uint32_t *)pCreateInfo->pCode;
Courtney Goeltzenleuchter8f8367e2015-10-07 08:38:30 -0600215 size_t sizeInWords = pCreateInfo->codeSize / sizeof(uint32_t);
Chris Forbes46794b82015-09-18 11:40:23 +1200216
217 /* Just validate that the header makes sense. */
218 return sizeInWords >= 5 && words[0] == spv::MagicNumber && words[1] == spv::Version;
219}
220
Chris Forbesb6b8c462015-04-15 06:59:41 +1200221static void
Chris Forbesb1dee272015-07-03 13:50:24 +1200222init_shader_checker(layer_data *my_data)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200223{
Chris Forbesb1dee272015-07-03 13:50:24 +1200224 uint32_t report_flags = 0;
225 uint32_t debug_action = 0;
226 FILE *log_output = NULL;
227 const char *option_str;
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600228 VkDbgMsgCallback callback;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200229 // initialize ShaderChecker options
Chris Forbesb1dee272015-07-03 13:50:24 +1200230 report_flags = getLayerOptionFlags("ShaderCheckerReportFlags", 0);
231 getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &debug_action);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200232
Chris Forbesb1dee272015-07-03 13:50:24 +1200233 if (debug_action & VK_DBG_LAYER_ACTION_LOG_MSG)
Chris Forbesb6b8c462015-04-15 06:59:41 +1200234 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200235 option_str = getLayerOption("ShaderCheckerLogFilename");
Tobin Ehlisb1df55e2015-09-15 09:55:54 -0600236 log_output = getLayerLogOutput(option_str, "ShaderChecker");
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -0600237 layer_create_msg_callback(my_data->report_data, report_flags, log_callback, (void *) log_output, &callback);
238 my_data->logging_callback.push_back(callback);
239 }
240
241 if (debug_action & VK_DBG_LAYER_ACTION_DEBUG_OUTPUT) {
242 layer_create_msg_callback(my_data->report_data, report_flags, win32_debug_output_msg, NULL, &callback);
243 my_data->logging_callback.push_back(callback);
Chris Forbesb1dee272015-07-03 13:50:24 +1200244 }
245
246 if (!globalLockInitialized)
247 {
Chris Forbesb1dee272015-07-03 13:50:24 +1200248 loader_platform_thread_create_mutex(&globalLock);
249 globalLockInitialized = 1;
Chris Forbesb6b8c462015-04-15 06:59:41 +1200250 }
251}
252
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600253static const VkLayerProperties shader_checker_global_layers[] = {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600254 {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600255 "ShaderChecker",
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600256 VK_API_VERSION,
257 VK_MAKE_VERSION(0, 1, 0),
258 "Validation layer: ShaderChecker",
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600259 }
Chris Forbes2778f302015-04-02 13:22:31 +1300260};
261
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600262VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600263 const char *pLayerName,
264 uint32_t *pCount,
265 VkExtensionProperties* pProperties)
Chris Forbes2778f302015-04-02 13:22:31 +1300266{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600267 /* shader checker does not have any global extensions */
268 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
Chris Forbes2778f302015-04-02 13:22:31 +1300269}
270
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600271VK_LAYER_EXPORT VkResult VKAPI vkEnumerateInstanceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600272 uint32_t *pCount,
273 VkLayerProperties* pProperties)
Tony Barbour59a47322015-06-24 16:06:58 -0600274{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600275 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
276 shader_checker_global_layers,
277 pCount, pProperties);
Tony Barbour59a47322015-06-24 16:06:58 -0600278}
279
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600280VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceExtensionProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600281 VkPhysicalDevice physicalDevice,
282 const char* pLayerName,
283 uint32_t* pCount,
284 VkExtensionProperties* pProperties)
Jon Ashburn207a3af2015-06-10 16:43:31 -0600285{
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600286 /* Shader checker does not have any physical device extensions */
Jon Ashburn751c4842015-11-02 17:37:20 -0700287 if (pLayerName == NULL) {
288 dispatch_key key = get_dispatch_key(physicalDevice);
289 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
290 my_data->instance_dispatch_table->EnumerateDeviceExtensionProperties(
291 physicalDevice,
292 NULL,
293 pCount,
294 pProperties);
295 } else {
296 return util_GetExtensionProperties(0, NULL, pCount, pProperties);
297 }
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600298}
Jon Ashburn207a3af2015-06-10 16:43:31 -0600299
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -0600300VK_LAYER_EXPORT VkResult VKAPI vkEnumerateDeviceLayerProperties(
Courtney Goeltzenleuchterda8adfe2015-07-07 10:05:05 -0600301 VkPhysicalDevice physicalDevice,
302 uint32_t* pCount,
303 VkLayerProperties* pProperties)
304{
305 /* Shader checker physical device layers are the same as global */
306 return util_GetLayerProperties(ARRAY_SIZE(shader_checker_global_layers),
307 shader_checker_global_layers,
308 pCount, pProperties);
Jon Ashburn207a3af2015-06-10 16:43:31 -0600309}
Chris Forbes2778f302015-04-02 13:22:31 +1300310
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200311static char const *
312storage_class_name(unsigned sc)
313{
314 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600315 case spv::StorageClassInput: return "input";
316 case spv::StorageClassOutput: return "output";
317 case spv::StorageClassUniformConstant: return "const uniform";
318 case spv::StorageClassUniform: return "uniform";
319 case spv::StorageClassWorkgroupLocal: return "workgroup local";
320 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
321 case spv::StorageClassPrivateGlobal: return "private global";
322 case spv::StorageClassFunction: return "function";
323 case spv::StorageClassGeneric: return "generic";
Cody Northrop97e52d82015-04-20 14:09:40 -0600324 case spv::StorageClassAtomicCounter: return "atomic counter";
GregF7c45bed2015-07-21 17:22:50 -0600325 case spv::StorageClassImage: return "image";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200326 default: return "unknown";
327 }
328}
329
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200330/* returns ptr to null terminator */
331static char *
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600332describe_type(char *dst, shader_module const *src, unsigned type)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200333{
334 auto type_def_it = src->type_def_index.find(type);
335
336 if (type_def_it == src->type_def_index.end()) {
337 return dst + sprintf(dst, "undef");
338 }
339
340 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
341 unsigned opcode = code[0] & 0x0ffffu;
342 switch (opcode) {
343 case spv::OpTypeBool:
344 return dst + sprintf(dst, "bool");
345 case spv::OpTypeInt:
346 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
347 case spv::OpTypeFloat:
348 return dst + sprintf(dst, "float%d", code[2]);
349 case spv::OpTypeVector:
350 dst += sprintf(dst, "vec%d of ", code[3]);
351 return describe_type(dst, src, code[2]);
352 case spv::OpTypeMatrix:
353 dst += sprintf(dst, "mat%d of ", code[3]);
354 return describe_type(dst, src, code[2]);
355 case spv::OpTypeArray:
356 dst += sprintf(dst, "arr[%d] of ", code[3]);
357 return describe_type(dst, src, code[2]);
358 case spv::OpTypePointer:
359 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
360 return describe_type(dst, src, code[3]);
361 case spv::OpTypeStruct:
362 {
363 unsigned oplen = code[0] >> 16;
364 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600365 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200366 dst = describe_type(dst, src, code[i]);
367 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
368 }
369 return dst;
370 }
Chris Forbes0ae15802015-08-14 12:04:39 +1200371 case spv::OpTypeSampler:
372 return dst + sprintf(dst, "sampler");
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200373 default:
374 return dst + sprintf(dst, "oddtype");
375 }
376}
377
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200378static bool
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600379types_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 +1200380{
381 auto a_type_def_it = a->type_def_index.find(a_type);
382 auto b_type_def_it = b->type_def_index.find(b_type);
383
384 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200385 return false;
386 }
387
388 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200389 return false;
390 }
391
392 /* walk two type trees together, and complain about differences */
393 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
394 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
395
396 unsigned a_opcode = a_code[0] & 0x0ffffu;
397 unsigned b_opcode = b_code[0] & 0x0ffffu;
398
Chris Forbesf3fc0332015-06-05 14:57:05 +1200399 if (b_arrayed && b_opcode == spv::OpTypeArray) {
400 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
401 return types_match(a, b, a_type, b_code[2], false);
402 }
403
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200404 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200405 return false;
406 }
407
408 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200409 /* 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 +1200410 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200411 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200412 case spv::OpTypeInt:
413 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200414 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200415 case spv::OpTypeFloat:
416 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200417 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200418 case spv::OpTypeVector:
419 case spv::OpTypeMatrix:
420 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200421 /* match on element type, count. these all have the same layout. we don't get here if
422 * b_arrayed -- that is handled above. */
423 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 +1200424 case spv::OpTypeStruct:
425 /* match on all element types */
426 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200427 if (b_arrayed) {
428 /* for the purposes of matching different levels of arrayness, structs are leaves. */
429 return false;
430 }
431
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200432 unsigned a_len = a_code[0] >> 16;
433 unsigned b_len = b_code[0] >> 16;
434
435 if (a_len != b_len) {
436 return false; /* structs cannot match if member counts differ */
437 }
438
Ian Elliott1cb62222015-04-17 11:05:04 -0600439 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200440 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200441 return false;
442 }
443 }
444
445 return true;
446 }
447 case spv::OpTypePointer:
448 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200449 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200450
451 default:
452 /* remaining types are CLisms, or may not appear in the interfaces we
453 * are interested in. Just claim no match.
454 */
455 return false;
456
457 }
458}
459
Chris Forbes06e8fc32015-04-13 12:14:52 +1200460static int
461value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
462{
463 auto it = map.find(id);
464 if (it == map.end())
465 return def;
466 else
467 return it->second;
468}
469
Chris Forbes06e8fc32015-04-13 12:14:52 +1200470struct interface_var {
471 uint32_t id;
472 uint32_t type_id;
473 /* TODO: collect the name, too? Isn't required to be present. */
474};
475
Chris Forbes06e8fc32015-04-13 12:14:52 +1200476static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600477collect_interface_by_location(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200478 shader_module const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200479 std::map<uint32_t, interface_var> &out,
480 std::map<uint32_t, interface_var> &builtins_out)
481{
482 unsigned int const *code = (unsigned int const *)&src->words[0];
483 size_t size = src->words.size();
484
Chris Forbes06e8fc32015-04-13 12:14:52 +1200485 std::unordered_map<unsigned, unsigned> var_locations;
486 std::unordered_map<unsigned, unsigned> var_builtins;
487
488 unsigned word = 5;
489 while (word < size) {
490
491 unsigned opcode = code[word] & 0x0ffffu;
492 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
493
494 /* We consider two interface models: SSO rendezvous-by-location, and
495 * builtins. Complain about anything that fits neither model.
496 */
497 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600498 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200499 var_locations[code[word+1]] = code[word+3];
500 }
501
Cody Northrop97e52d82015-04-20 14:09:40 -0600502 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200503 var_builtins[code[word+1]] = code[word+3];
504 }
505 }
506
507 /* TODO: handle grouped decorations */
508 /* TODO: handle index=1 dual source outputs from FS -- two vars will
509 * have the same location, and we DONT want to clobber. */
510
Ian Elliott1cb62222015-04-17 11:05:04 -0600511 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200512 int location = value_or_default(var_locations, code[word+2], -1);
513 int builtin = value_or_default(var_builtins, code[word+2], -1);
514
515 if (location == -1 && builtin == -1) {
516 /* No location defined, and not bound to an API builtin.
517 * The spec says nothing about how this case works (or doesn't)
518 * for interface matching.
519 */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600520 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 +1200521 "var %d (type %d) in %s interface has no Location or Builtin decoration",
522 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes06e8fc32015-04-13 12:14:52 +1200523 }
524 else if (location != -1) {
525 /* A user-defined interface variable, with a location. */
526 interface_var v;
527 v.id = code[word+2];
528 v.type_id = code[word+1];
529 out[location] = v;
530 }
531 else {
532 /* A builtin interface variable */
533 interface_var v;
534 v.id = code[word+2];
535 v.type_id = code[word+1];
536 builtins_out[builtin] = v;
537 }
538 }
539
540 word += oplen;
541 }
542}
543
Chris Forbes0ae15802015-08-14 12:04:39 +1200544static void
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600545collect_interface_by_descriptor_slot(layer_data *my_data, VkDevice dev,
Chris Forbes0ae15802015-08-14 12:04:39 +1200546 shader_module const *src, spv::StorageClass sinterface,
547 std::map<std::pair<unsigned, unsigned>, interface_var> &out)
548{
549 unsigned int const *code = (unsigned int const *)&src->words[0];
550 size_t size = src->words.size();
551
552 std::unordered_map<unsigned, unsigned> var_sets;
553 std::unordered_map<unsigned, unsigned> var_bindings;
554
555 unsigned word = 5;
556 while (word < size) {
557
558 unsigned opcode = code[word] & 0x0ffffu;
559 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
560
561 /* We consider two interface models: SSO rendezvous-by-location, and
562 * builtins. Complain about anything that fits neither model.
563 */
564 if (opcode == spv::OpDecorate) {
565 if (code[word+2] == spv::DecorationDescriptorSet) {
566 var_sets[code[word+1]] = code[word+3];
567 }
568
569 if (code[word+2] == spv::DecorationBinding) {
570 var_bindings[code[word+1]] = code[word+3];
571 }
572 }
573
574 if (opcode == spv::OpVariable && (code[word+3] == spv::StorageClassUniform ||
575 code[word+3] == spv::StorageClassUniformConstant)) {
576 unsigned set = value_or_default(var_sets, code[word+2], 0);
577 unsigned binding = value_or_default(var_bindings, code[word+2], 0);
578
579 auto existing_it = out.find(std::make_pair(set, binding));
580 if (existing_it != out.end()) {
581 /* conflict within spv image */
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600582 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 +1200583 SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
584 "var %d (type %d) in %s interface in descriptor slot (%u,%u) conflicts with existing definition",
585 code[word+2], code[word+1], storage_class_name(sinterface),
586 existing_it->first.first, existing_it->first.second);
587 }
588
589 interface_var v;
590 v.id = code[word+2];
591 v.type_id = code[word+1];
592 out[std::make_pair(set, binding)] = v;
593 }
594
595 word += oplen;
596 }
597}
598
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600599VK_LAYER_EXPORT VkResult VKAPI vkCreateShaderModule(
600 VkDevice device,
601 const VkShaderModuleCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800602 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600603 VkShaderModule *pShaderModule)
Chris Forbes2778f302015-04-02 13:22:31 +1300604{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600605 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes46794b82015-09-18 11:40:23 +1200606 /* Protect the driver from non-SPIRV shaders */
607 if (!shader_is_spirv(pCreateInfo)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600608 log_msg(my_data->report_data, VK_DBG_REPORT_ERROR_BIT, VK_OBJECT_TYPE_DEVICE,
Chris Forbes46794b82015-09-18 11:40:23 +1200609 /* dev */ 0, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
610 "Shader is not SPIR-V");
611 return VK_ERROR_VALIDATION_FAILED;
612 }
613
Chia-I Wuf7458c52015-10-26 21:10:41 +0800614 VkResult res = my_data->device_dispatch_table->CreateShaderModule(device, pCreateInfo, pAllocator, pShaderModule);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200615
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600616 if (res == VK_SUCCESS) {
617 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600618 my_data->shader_module_map[*pShaderModule] = new shader_module(pCreateInfo);
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600619 loader_platform_thread_unlock_mutex(&globalLock);
620 }
Chris Forbes2778f302015-04-02 13:22:31 +1300621 return res;
622}
623
Chia-I Wu08accc62015-07-07 11:50:03 +0800624VK_LAYER_EXPORT VkResult VKAPI vkCreateRenderPass(
625 VkDevice device,
626 const VkRenderPassCreateInfo *pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800627 const VkAllocationCallbacks* pAllocator,
Chia-I Wu08accc62015-07-07 11:50:03 +0800628 VkRenderPass *pRenderPass)
629{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600630 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +0800631 VkResult res = my_data->device_dispatch_table->CreateRenderPass(device, pCreateInfo, pAllocator, pRenderPass);
Chia-I Wu08accc62015-07-07 11:50:03 +0800632
Courtney Goeltzenleuchterb5afdbf2015-09-16 12:57:55 -0600633 loader_platform_thread_lock_mutex(&globalLock);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600634 my_data->render_pass_map[*pRenderPass] = new render_pass(pCreateInfo);
Chia-I Wu08accc62015-07-07 11:50:03 +0800635 loader_platform_thread_unlock_mutex(&globalLock);
636 return res;
637}
638
Chris Forbesee99b9b2015-05-25 11:13:22 +1200639static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600640validate_interface_between_stages(layer_data *my_data, VkDevice dev,
Chris Forbesb1dee272015-07-03 13:50:24 +1200641 shader_module const *producer, char const *producer_name,
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600642 shader_module const *consumer, char const *consumer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200643 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200644{
645 std::map<uint32_t, interface_var> outputs;
646 std::map<uint32_t, interface_var> inputs;
647
648 std::map<uint32_t, interface_var> builtin_outputs;
649 std::map<uint32_t, interface_var> builtin_inputs;
650
Chris Forbesee99b9b2015-05-25 11:13:22 +1200651 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200652
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600653 collect_interface_by_location(my_data, dev, producer, spv::StorageClassOutput, outputs, builtin_outputs);
654 collect_interface_by_location(my_data, dev, consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200655
656 auto a_it = outputs.begin();
657 auto b_it = inputs.begin();
658
659 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600660 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
661 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
662 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200663 auto a_first = a_at_end ? 0 : a_it->first;
664 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600665
Mike Stroyan19a6de22015-09-10 14:10:25 -0600666 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600667 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 -0600668 "%s writes to output location %d which is not consumed by %s", producer_name, a_first, consumer_name)) {
669 pass = false;
670 }
Chris Forbes41002452015-04-08 10:19:16 +1200671 a_it++;
672 }
David Pinedod8f83d82015-04-27 16:36:17 -0600673 else if (a_at_end || a_first > b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600674 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 -0600675 "%s consumes input location %d which is not written by %s", consumer_name, b_first, producer_name)) {
676 pass = false;
677 }
Chris Forbes41002452015-04-08 10:19:16 +1200678 b_it++;
679 }
680 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200681 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 +1200682 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200683 }
684 else {
685 char producer_type[1024];
686 char consumer_type[1024];
687 describe_type(producer_type, producer, a_it->second.type_id);
688 describe_type(consumer_type, consumer, b_it->second.type_id);
689
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600690 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 -0600691 "Type mismatch on location %d: '%s' vs '%s'", a_it->first, producer_type, consumer_type)) {
Chris Forbesee99b9b2015-05-25 11:13:22 +1200692 pass = false;
Mike Stroyan830368a2015-09-10 14:12:01 -0600693 }
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200694 }
Chris Forbes41002452015-04-08 10:19:16 +1200695 a_it++;
696 b_it++;
697 }
698 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200699
700 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200701}
702
703
Chris Forbes3616b462015-04-08 10:37:20 +1200704enum FORMAT_TYPE {
705 FORMAT_TYPE_UNDEFINED,
706 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
707 FORMAT_TYPE_SINT,
708 FORMAT_TYPE_UINT,
709};
710
711
712static unsigned
713get_format_type(VkFormat fmt) {
714 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800715 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200716 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800717 case VK_FORMAT_R8_SINT:
718 case VK_FORMAT_R8G8_SINT:
719 case VK_FORMAT_R8G8B8_SINT:
720 case VK_FORMAT_R8G8B8A8_SINT:
721 case VK_FORMAT_R16_SINT:
722 case VK_FORMAT_R16G16_SINT:
723 case VK_FORMAT_R16G16B16_SINT:
724 case VK_FORMAT_R16G16B16A16_SINT:
725 case VK_FORMAT_R32_SINT:
726 case VK_FORMAT_R32G32_SINT:
727 case VK_FORMAT_R32G32B32_SINT:
728 case VK_FORMAT_R32G32B32A32_SINT:
729 case VK_FORMAT_B8G8R8_SINT:
730 case VK_FORMAT_B8G8R8A8_SINT:
731 case VK_FORMAT_R10G10B10A2_SINT:
732 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200733 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800734 case VK_FORMAT_R8_UINT:
735 case VK_FORMAT_R8G8_UINT:
736 case VK_FORMAT_R8G8B8_UINT:
737 case VK_FORMAT_R8G8B8A8_UINT:
738 case VK_FORMAT_R16_UINT:
739 case VK_FORMAT_R16G16_UINT:
740 case VK_FORMAT_R16G16B16_UINT:
741 case VK_FORMAT_R16G16B16A16_UINT:
742 case VK_FORMAT_R32_UINT:
743 case VK_FORMAT_R32G32_UINT:
744 case VK_FORMAT_R32G32B32_UINT:
745 case VK_FORMAT_R32G32B32A32_UINT:
746 case VK_FORMAT_B8G8R8_UINT:
747 case VK_FORMAT_B8G8R8A8_UINT:
748 case VK_FORMAT_R10G10B10A2_UINT:
749 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200750 return FORMAT_TYPE_UINT;
751 default:
752 return FORMAT_TYPE_FLOAT;
753 }
754}
755
756
Chris Forbes156a1162015-05-04 14:04:06 +1200757/* characterizes a SPIR-V type appearing in an interface to a FF stage,
758 * for comparison to a VkFormat's characterization above. */
759static unsigned
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -0600760get_fundamental_type(shader_module const *src, unsigned type)
Chris Forbes156a1162015-05-04 14:04:06 +1200761{
762 auto type_def_it = src->type_def_index.find(type);
763
764 if (type_def_it == src->type_def_index.end()) {
765 return FORMAT_TYPE_UNDEFINED;
766 }
767
768 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
769 unsigned opcode = code[0] & 0x0ffffu;
770 switch (opcode) {
771 case spv::OpTypeInt:
772 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
773 case spv::OpTypeFloat:
774 return FORMAT_TYPE_FLOAT;
775 case spv::OpTypeVector:
776 return get_fundamental_type(src, code[2]);
777 case spv::OpTypeMatrix:
778 return get_fundamental_type(src, code[2]);
779 case spv::OpTypeArray:
780 return get_fundamental_type(src, code[2]);
781 case spv::OpTypePointer:
782 return get_fundamental_type(src, code[3]);
783 default:
784 return FORMAT_TYPE_UNDEFINED;
785 }
786}
787
788
Chris Forbesee99b9b2015-05-25 11:13:22 +1200789static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600790validate_vi_consistency(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi)
Chris Forbes280ba2c2015-06-12 11:16:41 +1200791{
792 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
793 * each binding should be specified only once.
794 */
795 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes280ba2c2015-06-12 11:16:41 +1200796 bool pass = true;
797
Chia-I Wud50a7d72015-10-26 20:48:51 +0800798 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes280ba2c2015-06-12 11:16:41 +1200799 auto desc = &vi->pVertexBindingDescriptions[i];
800 auto & binding = bindings[desc->binding];
801 if (binding) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600802 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 -0600803 "Duplicate vertex input binding descriptions for binding %d", desc->binding)) {
804 pass = false;
805 }
Chris Forbes280ba2c2015-06-12 11:16:41 +1200806 }
807 else {
808 binding = desc;
809 }
810 }
811
812 return pass;
813}
814
815
816static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600817validate_vi_against_vs_inputs(layer_data *my_data, VkDevice dev, VkPipelineVertexInputStateCreateInfo const *vi, shader_module const *vs)
Chris Forbes772d03b2015-04-08 10:36:37 +1200818{
819 std::map<uint32_t, interface_var> inputs;
820 /* we collect builtin inputs, but they will never appear in the VI state --
821 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
822 */
823 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200824 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200825
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600826 collect_interface_by_location(my_data, dev, vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200827
828 /* Build index by location */
829 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200830 if (vi) {
Chia-I Wud50a7d72015-10-26 20:48:51 +0800831 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++)
Chris Forbes7191cd52015-05-25 11:13:24 +1200832 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
833 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200834
835 auto it_a = attribs.begin();
836 auto it_b = inputs.begin();
837
David Pinedod8f83d82015-04-27 16:36:17 -0600838 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
839 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
840 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200841 auto a_first = a_at_end ? 0 : it_a->first;
842 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600843 if (b_at_end || a_first < b_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600844 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 -0600845 "Vertex attribute at location %d not consumed by VS", a_first)) {
846 pass = false;
847 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200848 it_a++;
849 }
David Pinedod8f83d82015-04-27 16:36:17 -0600850 else if (a_at_end || b_first < a_first) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600851 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 -0600852 "VS consumes input at location %d but not provided", b_first)) {
853 pass = false;
854 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200855 it_b++;
856 }
857 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200858 unsigned attrib_type = get_format_type(it_a->second->format);
859 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
860
861 /* type checking */
862 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
863 char vs_type[1024];
864 describe_type(vs_type, vs, it_b->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600865 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 +1200866 "Attribute type of `%s` at location %d does not match VS input type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600867 string_VkFormat(it_a->second->format), a_first, vs_type)) {
868 pass = false;
869 }
Chris Forbes401784b2015-05-04 14:04:24 +1200870 }
871
Chris Forbes6b2ead62015-04-17 10:13:28 +1200872 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200873 it_a++;
874 it_b++;
875 }
876 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200877
878 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200879}
880
881
Chris Forbesee99b9b2015-05-25 11:13:22 +1200882static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600883validate_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 +1200884{
Chia-I Wu08accc62015-07-07 11:50:03 +0800885 const std::vector<VkFormat> &color_formats = rp->subpass_color_formats[subpass];
Chris Forbes3616b462015-04-08 10:37:20 +1200886 std::map<uint32_t, interface_var> outputs;
887 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200888 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200889
890 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
891
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600892 collect_interface_by_location(my_data, dev, fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200893
894 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
895 * and all color attachment should be UNORM/SNORM/FLOAT.
896 */
897 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200898 if (outputs.size()) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600899 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 -0600900 "Should not have user-defined FS outputs when using broadcast")) {
901 pass = false;
902 }
Chris Forbes3616b462015-04-08 10:37:20 +1200903 }
904
Chia-I Wu08accc62015-07-07 11:50:03 +0800905 for (unsigned i = 0; i < color_formats.size(); i++) {
906 unsigned attachmentType = get_format_type(color_formats[i]);
Chris Forbes3616b462015-04-08 10:37:20 +1200907 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600908 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 -0600909 "CB format should not be SINT or UINT when using broadcast")) {
910 pass = false;
911 }
Chris Forbes3616b462015-04-08 10:37:20 +1200912 }
913 }
914
Chris Forbesee99b9b2015-05-25 11:13:22 +1200915 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200916 }
917
918 auto it = outputs.begin();
919 uint32_t attachment = 0;
920
921 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
922 * are currently dense, but the parallel with matching between shader stages is nice.
923 */
924
Chris Forbes9715d4a2015-07-10 09:06:54 +1200925 /* TODO: Figure out compile error with cb->attachmentCount */
Chris Forbesc2cbb0a2015-07-11 11:05:01 +1200926 while ((outputs.size() > 0 && it != outputs.end()) || attachment < color_formats.size()) {
927 if (attachment == color_formats.size() || ( it != outputs.end() && it->first < attachment)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600928 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 -0600929 "FS writes to output location %d with no matching attachment", it->first)) {
930 pass = false;
931 }
Chris Forbes3616b462015-04-08 10:37:20 +1200932 it++;
933 }
934 else if (it == outputs.end() || it->first > attachment) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600935 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 -0600936 "Attachment %d not written by FS", attachment)) {
937 pass = false;
938 }
Chris Forbes3616b462015-04-08 10:37:20 +1200939 attachment++;
940 }
941 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200942 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
Chia-I Wu08accc62015-07-07 11:50:03 +0800943 unsigned att_type = get_format_type(color_formats[attachment]);
Chris Forbes46d31e52015-05-04 14:20:10 +1200944
945 /* type checking */
946 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
947 char fs_type[1024];
948 describe_type(fs_type, fs, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -0600949 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 +1200950 "Attachment %d of type `%s` does not match FS output type of `%s`",
Mike Stroyan830368a2015-09-10 14:12:01 -0600951 attachment, string_VkFormat(color_formats[attachment]), fs_type)) {
952 pass = false;
953 }
Chris Forbes46d31e52015-05-04 14:20:10 +1200954 }
955
Chris Forbes6b2ead62015-04-17 10:13:28 +1200956 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200957 it++;
958 attachment++;
959 }
960 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200961
962 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200963}
964
965
Chris Forbesf044ec92015-06-05 15:01:08 +1200966struct shader_stage_attributes {
967 char const * const name;
968 bool arrayed_input;
969};
970
971
972static shader_stage_attributes
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -0600973shader_stage_attribs[] = {
Chris Forbesf044ec92015-06-05 15:01:08 +1200974 { "vertex shader", false },
975 { "tessellation control shader", true },
976 { "tessellation evaluation shader", false },
977 { "geometry shader", true },
978 { "fragment shader", false },
979};
980
981
Chris Forbes556c76c2015-08-14 12:04:59 +1200982static VkDescriptorSetLayoutBinding *
983find_descriptor_binding(std::vector<std::vector<VkDescriptorSetLayoutBinding>*>* layout,
984 std::pair<unsigned, unsigned> slot)
985{
986 if (!layout)
987 return nullptr;
988
989 if (slot.first >= layout->size())
990 return nullptr;
991
992 auto set = (*layout)[slot.first];
993
994 if (slot.second >= set->size())
995 return nullptr;
996
997 return &(*set)[slot.second];
998}
999
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001000static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage)
1001{
1002 uint32_t bit_pos = u_ffs(stage);
1003 return bit_pos-1;
1004}
Chris Forbes556c76c2015-08-14 12:04:59 +12001005
Chris Forbes81874ba2015-06-04 20:23:00 +12001006static bool
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001007validate_graphics_pipeline(layer_data *my_data, VkDevice dev, VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +12001008{
Chris Forbesf6800b52015-04-08 10:16:45 +12001009 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
1010 * before trying to do anything more: */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001011 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1012 int geometry_stage = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
1013 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbesf6800b52015-04-08 10:16:45 +12001014
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001015 shader_module **shaders = new shader_module*[fragment_stage + 1]; /* exclude CS */
1016 memset(shaders, 0, sizeof(shader_module *) * (fragment_stage +1));
Chia-I Wu08accc62015-07-07 11:50:03 +08001017 render_pass const *rp = 0;
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001018 VkPipelineVertexInputStateCreateInfo const *vi = 0;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001019 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +12001020
Chris Forbes7f963832015-05-29 14:55:18 +12001021 loader_platform_thread_lock_mutex(&globalLock);
1022
Tony Barbour92503c62015-07-13 15:28:47 -06001023 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001024 VkPipelineShaderStageCreateInfo const *pStage = &pCreateInfo->pStages[i];
1025 if (pStage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
Chris Forbesf6800b52015-04-08 10:16:45 +12001026
Chia-I Wu28e06912015-10-31 00:31:16 +08001027 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 -06001028 | VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT | VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT)) == 0) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001029 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 +08001030 "Unknown shader stage %d", pStage->stage)) {
Mike Stroyan830368a2015-09-10 14:12:01 -06001031 pass = false;
1032 }
Chris Forbes6b2ead62015-04-17 10:13:28 +12001033 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001034 else {
Chia-I Wu28e06912015-10-31 00:31:16 +08001035 shader_module *module = my_data->shader_module_map[pStage->module];
1036 shaders[get_shader_stage_id(pStage->stage)] = module;
Chris Forbes556c76c2015-08-14 12:04:59 +12001037
1038 /* validate descriptor set layout against what the spirv module actually uses */
Chris Forbes46794b82015-09-18 11:40:23 +12001039 std::map<std::pair<unsigned, unsigned>, interface_var> descriptor_uses;
Chia-I Wu28e06912015-10-31 00:31:16 +08001040 collect_interface_by_descriptor_slot(my_data, dev, module, spv::StorageClassUniform,
Chris Forbes46794b82015-09-18 11:40:23 +12001041 descriptor_uses);
Chris Forbes556c76c2015-08-14 12:04:59 +12001042
Chia-I Wue2fc5522015-10-26 20:04:44 +08001043 auto layout = pCreateInfo->layout != VK_NULL_HANDLE ?
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001044 my_data->pipeline_layout_map[pCreateInfo->layout] : nullptr;
Chris Forbes556c76c2015-08-14 12:04:59 +12001045
Chris Forbes46794b82015-09-18 11:40:23 +12001046 for (auto it = descriptor_uses.begin(); it != descriptor_uses.end(); it++) {
Chris Forbes556c76c2015-08-14 12:04:59 +12001047
Chris Forbes46794b82015-09-18 11:40:23 +12001048 /* find the matching binding */
1049 auto binding = find_descriptor_binding(layout, it->first);
Chris Forbes556c76c2015-08-14 12:04:59 +12001050
Chris Forbes46794b82015-09-18 11:40:23 +12001051 if (binding == nullptr) {
1052 char type_name[1024];
Chia-I Wu28e06912015-10-31 00:31:16 +08001053 describe_type(type_name, module, it->second.type_id);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001054 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 +12001055 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1056 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
Mike Stroyan830368a2015-09-10 14:12:01 -06001057 it->first.first, it->first.second, type_name)) {
1058 pass = false;
1059 }
Chris Forbes556c76c2015-08-14 12:04:59 +12001060 }
1061 }
Chris Forbesf044ec92015-06-05 15:01:08 +12001062 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001063 }
Chris Forbesf6800b52015-04-08 10:16:45 +12001064 }
1065
Chia-I Wu08accc62015-07-07 11:50:03 +08001066 if (pCreateInfo->renderPass != VK_NULL_HANDLE)
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001067 rp = my_data->render_pass_map[pCreateInfo->renderPass];
Chia-I Wu08accc62015-07-07 11:50:03 +08001068
Mark Lobodzinskid5732f32015-06-23 15:11:57 -06001069 vi = pCreateInfo->pVertexInputState;
1070
Chris Forbes280ba2c2015-06-12 11:16:41 +12001071 if (vi) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001072 pass = validate_vi_consistency(my_data, dev, vi) && pass;
Chris Forbes280ba2c2015-06-12 11:16:41 +12001073 }
1074
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001075 if (shaders[vertex_stage]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001076 pass = validate_vi_against_vs_inputs(my_data, dev, vi, shaders[vertex_stage]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +12001077 }
1078
Chris Forbesf044ec92015-06-05 15:01:08 +12001079 /* TODO: enforce rules about present combinations of shaders */
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001080 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1081 int consumer = get_shader_stage_id(VK_SHADER_STAGE_GEOMETRY_BIT);
Chris Forbesf044ec92015-06-05 15:01:08 +12001082
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001083 while (!shaders[producer] && producer != fragment_stage) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001084 producer++;
1085 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +12001086 }
1087
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001088 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +12001089 assert(shaders[producer]);
1090 if (shaders[consumer]) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001091 pass = validate_interface_between_stages(my_data, dev,
Chris Forbes46794b82015-09-18 11:40:23 +12001092 shaders[producer], shader_stage_attribs[producer].name,
1093 shaders[consumer], shader_stage_attribs[consumer].name,
1094 shader_stage_attribs[consumer].arrayed_input) && pass;
Chris Forbesf044ec92015-06-05 15:01:08 +12001095
1096 producer = consumer;
1097 }
1098 }
1099
Courtney Goeltzenleuchterd2635502015-10-21 17:08:06 -06001100 if (shaders[fragment_stage] && rp) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001101 pass = validate_fs_outputs_against_render_pass(my_data, dev, shaders[fragment_stage], rp, pCreateInfo->subpass) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +12001102 }
1103
Courtney Goeltzenleuchterbbf66082015-10-27 11:32:31 -06001104 delete shaders;
1105
Chris Forbes7f963832015-05-29 14:55:18 +12001106 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +12001107 return pass;
1108}
1109
Jon Ashburnc669cc62015-07-09 15:02:25 -06001110//TODO handle pipelineCache entry points
Chris Forbes39d8d752015-06-04 20:27:09 +12001111VK_LAYER_EXPORT VkResult VKAPI
Jon Ashburnc669cc62015-07-09 15:02:25 -06001112vkCreateGraphicsPipelines(VkDevice device,
1113 VkPipelineCache pipelineCache,
1114 uint32_t count,
1115 const VkGraphicsPipelineCreateInfo *pCreateInfos,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001116 const VkAllocationCallbacks* pAllocator,
Jon Ashburnc669cc62015-07-09 15:02:25 -06001117 VkPipeline *pPipelines)
Chris Forbes81874ba2015-06-04 20:23:00 +12001118{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001119 layer_data *my_data = get_my_data_ptr(get_dispatch_key(device), layer_data_map);
Chris Forbes36a372f2015-07-24 13:53:47 +12001120 bool pass = true;
1121 for (uint32_t i = 0; i < count; i++) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001122 pass = validate_graphics_pipeline(my_data, device, &pCreateInfos[i]) && pass;
Chris Forbes36a372f2015-07-24 13:53:47 +12001123 }
Chris Forbesee99b9b2015-05-25 11:13:22 +12001124
1125 if (pass) {
1126 /* The driver is allowed to crash if passed junk. Only actually create the
1127 * pipeline if we didn't run into any showstoppers above.
1128 */
Chia-I Wuf7458c52015-10-26 21:10:41 +08001129 return my_data->device_dispatch_table->CreateGraphicsPipelines(device, pipelineCache, count, pCreateInfos, pAllocator, pPipelines);
Chris Forbesee99b9b2015-05-25 11:13:22 +12001130 }
1131 else {
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -06001132 return VK_ERROR_VALIDATION_FAILED;
Chris Forbesee99b9b2015-05-25 11:13:22 +12001133 }
Chris Forbes4175e6f2015-04-08 10:15:35 +12001134}
1135
1136
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001137VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
Chris Forbesb1dee272015-07-03 13:50:24 +12001138{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001139 layer_data *my_device_data = get_my_data_ptr(get_dispatch_key(*pDevice), layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001140 VkResult result = my_device_data->device_dispatch_table->CreateDevice(gpu, pCreateInfo, pAllocator, pDevice);
Chris Forbesb1dee272015-07-03 13:50:24 +12001141 if (result == VK_SUCCESS) {
1142 layer_data *my_instance_data = get_my_data_ptr(get_dispatch_key(gpu), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001143 my_device_data->report_data = layer_debug_report_create_device(my_instance_data->report_data, *pDevice);
1144 }
1145 return result;
1146}
Chris Forbes39d8d752015-06-04 20:27:09 +12001147
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001148/* hook DextroyDevice to remove tableMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001149VK_LAYER_EXPORT void VKAPI vkDestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001150{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001151 dispatch_key key = get_dispatch_key(device);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001152 layer_data *my_device_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001153 my_device_data->device_dispatch_table->DestroyDevice(device, pAllocator);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001154 delete my_device_data->device_dispatch_table;
1155 layer_data_map.erase(key);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001156}
1157
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001158VkResult VKAPI vkCreateInstance(
1159 const VkInstanceCreateInfo* pCreateInfo,
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001160 const VkAllocationCallbacks* pAllocator,
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001161 VkInstance* pInstance)
1162{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001163 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1164 VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table;
Chia-I Wuf7458c52015-10-26 21:10:41 +08001165 VkResult result = pTable->CreateInstance(pCreateInfo, pAllocator, pInstance);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001166
1167 if (result == VK_SUCCESS) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001168 layer_data *my_data = get_my_data_ptr(get_dispatch_key(*pInstance), layer_data_map);
1169 my_data->report_data = debug_report_create_instance(
1170 pTable,
1171 *pInstance,
Chia-I Wud50a7d72015-10-26 20:48:51 +08001172 pCreateInfo->enabledExtensionNameCount,
Chris Forbesb1dee272015-07-03 13:50:24 +12001173 pCreateInfo->ppEnabledExtensionNames);
Courtney Goeltzenleuchterd02a9642015-06-08 14:58:39 -06001174
Chris Forbesb1dee272015-07-03 13:50:24 +12001175 init_shader_checker(my_data);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001176 }
1177 return result;
1178}
1179
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001180/* hook DestroyInstance to remove tableInstanceMap entry */
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001181VK_LAYER_EXPORT void VKAPI vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator)
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001182{
Courtney Goeltzenleuchter0daf2282015-06-13 21:22:12 -06001183 dispatch_key key = get_dispatch_key(instance);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001184 layer_data *my_data = get_my_data_ptr(key, layer_data_map);
Chia-I Wuf7458c52015-10-26 21:10:41 +08001185 my_data->instance_dispatch_table->DestroyInstance(instance, pAllocator);
Chris Forbesb1dee272015-07-03 13:50:24 +12001186
1187 // Clean up logging callback, if any
Courtney Goeltzenleuchtered12e712015-10-05 15:59:58 -06001188 while (my_data->logging_callback.size() > 0) {
1189 VkDbgMsgCallback callback = my_data->logging_callback.back();
1190 layer_destroy_msg_callback(my_data->report_data, callback);
1191 my_data->logging_callback.pop_back();
Chris Forbesb1dee272015-07-03 13:50:24 +12001192 }
1193
1194 layer_debug_report_destroy_instance(my_data->report_data);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001195 delete my_data->instance_dispatch_table;
1196 layer_data_map.erase(key);
1197 if (layer_data_map.empty()) {
1198 // Release mutex when destroying last instance.
1199 loader_platform_thread_delete_mutex(&globalLock);
1200 globalLockInitialized = 0;
1201 }
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001202}
Chris Forbesdb467bd2015-05-25 11:12:59 +12001203
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001204VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001205 VkInstance instance,
1206 VkFlags msgFlags,
1207 const PFN_vkDbgMsgCallback pfnMsgCallback,
1208 void* pUserData,
1209 VkDbgMsgCallback* pMsgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001210{
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001211 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
1212 VkResult res = my_data->instance_dispatch_table->DbgCreateMsgCallback(instance, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001213 if (VK_SUCCESS == res) {
Chris Forbesb1dee272015-07-03 13:50:24 +12001214 res = layer_create_msg_callback(my_data->report_data, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
1215 }
1216 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001217}
1218
1219VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
Chris Forbesb1dee272015-07-03 13:50:24 +12001220 VkInstance instance,
1221 VkDbgMsgCallback msgCallback)
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001222{
Chris Forbesb1dee272015-07-03 13:50:24 +12001223 layer_data *my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001224 VkResult res = my_data->instance_dispatch_table->DbgDestroyMsgCallback(instance, msgCallback);
Chris Forbesb1dee272015-07-03 13:50:24 +12001225 layer_destroy_msg_callback(my_data->report_data, msgCallback);
1226 return res;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001227}
1228
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001229VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetDeviceProcAddr(VkDevice dev, const char* funcName)
Chris Forbes2778f302015-04-02 13:22:31 +13001230{
Chris Forbesb1dee272015-07-03 13:50:24 +12001231 if (dev == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +13001232 return NULL;
1233
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001234 layer_data *my_data;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001235 /* loader uses this to force layer initialization; device object is wrapped */
Chris Forbesb1dee272015-07-03 13:50:24 +12001236 if (!strcmp("vkGetDeviceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001237 VkBaseLayerObject* wrapped_dev = (VkBaseLayerObject*) dev;
1238 my_data = get_my_data_ptr(get_dispatch_key(wrapped_dev->baseObject), layer_data_map);
1239 my_data->device_dispatch_table = new VkLayerDispatchTable;
1240 layer_initialize_dispatch_table(my_data->device_dispatch_table, wrapped_dev);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001241 return (PFN_vkVoidFunction) vkGetDeviceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001242 }
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001243 my_data = get_my_data_ptr(get_dispatch_key(dev), layer_data_map);
Jon Ashburn8fd08252015-05-28 16:25:02 -06001244
Chris Forbes2778f302015-04-02 13:22:31 +13001245#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001246 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001247 return (PFN_vkVoidFunction) fn
Chris Forbes2778f302015-04-02 13:22:31 +13001248
Chris Forbesb1dee272015-07-03 13:50:24 +12001249 ADD_HOOK(vkCreateDevice);
Courtney Goeltzenleuchteree4027d2015-06-28 13:01:17 -06001250 ADD_HOOK(vkCreateShaderModule);
Chia-I Wu08accc62015-07-07 11:50:03 +08001251 ADD_HOOK(vkCreateRenderPass);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001252 ADD_HOOK(vkDestroyDevice);
Jon Ashburnc669cc62015-07-09 15:02:25 -06001253 ADD_HOOK(vkCreateGraphicsPipelines);
Chris Forbes2b8493a2015-08-12 15:03:22 +12001254 ADD_HOOK(vkCreateDescriptorSetLayout);
1255 ADD_HOOK(vkCreatePipelineLayout);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001256#undef ADD_HOOK
Chris Forbesb1dee272015-07-03 13:50:24 +12001257
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001258 VkLayerDispatchTable* pTable = my_data->device_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001259 {
1260 if (pTable->GetDeviceProcAddr == NULL)
1261 return NULL;
1262 return pTable->GetDeviceProcAddr(dev, funcName);
1263 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001264}
1265
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001266VK_LAYER_EXPORT PFN_vkVoidFunction VKAPI vkGetInstanceProcAddr(VkInstance instance, const char* funcName)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001267{
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001268 PFN_vkVoidFunction fptr;
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001269
Chris Forbesb1dee272015-07-03 13:50:24 +12001270 if (instance == NULL)
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001271 return NULL;
1272
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001273 layer_data *my_data;
Chris Forbesb1dee272015-07-03 13:50:24 +12001274 if (!strcmp("vkGetInstanceProcAddr", funcName)) {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001275 VkBaseLayerObject* wrapped_inst = (VkBaseLayerObject*) instance;
1276 my_data = get_my_data_ptr(get_dispatch_key(wrapped_inst->baseObject), layer_data_map);
1277 my_data->instance_dispatch_table = new VkLayerInstanceDispatchTable;
1278 layer_init_instance_dispatch_table(my_data->instance_dispatch_table, wrapped_inst);
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001279 return (PFN_vkVoidFunction) vkGetInstanceProcAddr;
Jon Ashburn8fd08252015-05-28 16:25:02 -06001280 }
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001281#define ADD_HOOK(fn) \
Chris Forbesb1dee272015-07-03 13:50:24 +12001282 if (!strncmp(#fn, funcName, sizeof(#fn))) \
Courtney Goeltzenleuchter2d3ba632015-07-12 14:35:22 -06001283 return (PFN_vkVoidFunction) fn
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001284
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001285 ADD_HOOK(vkCreateInstance);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001286 ADD_HOOK(vkDestroyInstance);
Courtney Goeltzenleuchter35985f62015-09-14 17:22:16 -06001287 ADD_HOOK(vkEnumerateInstanceExtensionProperties);
1288 ADD_HOOK(vkEnumerateDeviceExtensionProperties);
1289 ADD_HOOK(vkEnumerateInstanceLayerProperties);
1290 ADD_HOOK(vkEnumerateDeviceLayerProperties);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001291#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001292
Chris Forbesb1dee272015-07-03 13:50:24 +12001293
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001294 my_data = get_my_data_ptr(get_dispatch_key(instance), layer_data_map);
Chris Forbesb1dee272015-07-03 13:50:24 +12001295 fptr = debug_report_get_instance_proc_addr(my_data->report_data, funcName);
Courtney Goeltzenleuchterbfd2c662015-06-01 14:46:33 -06001296 if (fptr)
1297 return fptr;
1298
Chris Forbesb1dee272015-07-03 13:50:24 +12001299 {
Tobin Ehlis3c6f2e52015-10-29 11:11:53 -06001300 VkLayerInstanceDispatchTable* pTable = my_data->instance_dispatch_table;
Chris Forbesb1dee272015-07-03 13:50:24 +12001301 if (pTable->GetInstanceProcAddr == NULL)
1302 return NULL;
1303 return pTable->GetInstanceProcAddr(instance, funcName);
1304 }
Chris Forbes2778f302015-04-02 13:22:31 +13001305}