blob: 9484cd0b7fe3bcaec707942ea1c6cf44a3e381cf [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>
Chris Forbes2778f302015-04-02 13:22:31 +130031#include "loader_platform.h"
32#include "vk_dispatch_table_helper.h"
33#include "vkLayer.h"
Chris Forbesb6b8c462015-04-15 06:59:41 +120034#include "layers_config.h"
35#include "layers_msg.h"
Chris Forbes401784b2015-05-04 14:04:24 +120036#include "vk_enum_string_helper.h"
Chris Forbes6b2ead62015-04-17 10:13:28 +120037#include "shader_checker.h"
Chris Forbes2778f302015-04-02 13:22:31 +130038// The following is #included again to catch certain OS-specific functions
39// being used:
40#include "loader_platform.h"
41
Chris Forbes7f720542015-05-09 10:31:21 +120042#include "spirv/spirv.h"
Chris Forbes2778f302015-04-02 13:22:31 +130043
Chris Forbes2778f302015-04-02 13:22:31 +130044
Chris Forbesb6b8c462015-04-15 06:59:41 +120045static std::unordered_map<void *, VkLayerDispatchTable *> tableMap;
Chris Forbes7f963832015-05-29 14:55:18 +120046static VkBaseLayerObject *pCurObj;
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -060047static std::unordered_map<void *, VkLayerInstanceDispatchTable *> tableInstanceMap;
Chris Forbesb6b8c462015-04-15 06:59:41 +120048static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +120049// TODO : This can be much smarter, using separate locks for separate global data
50static int globalLockInitialized = 0;
51static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +120052
Chris Forbes3a5e99a2015-04-10 11:41:20 +120053
54static void
55build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
56{
57 unsigned int const *code = (unsigned int const *)&words[0];
58 size_t size = words.size();
59
60 unsigned word = 5;
61 while (word < size) {
62 unsigned opcode = code[word] & 0x0ffffu;
63 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
64
65 switch (opcode) {
66 case spv::OpTypeVoid:
67 case spv::OpTypeBool:
68 case spv::OpTypeInt:
69 case spv::OpTypeFloat:
70 case spv::OpTypeVector:
71 case spv::OpTypeMatrix:
72 case spv::OpTypeSampler:
73 case spv::OpTypeFilter:
74 case spv::OpTypeArray:
75 case spv::OpTypeRuntimeArray:
76 case spv::OpTypeStruct:
77 case spv::OpTypeOpaque:
78 case spv::OpTypePointer:
79 case spv::OpTypeFunction:
80 case spv::OpTypeEvent:
81 case spv::OpTypeDeviceEvent:
82 case spv::OpTypeReserveId:
83 case spv::OpTypeQueue:
84 case spv::OpTypePipe:
85 type_def_index[code[word+1]] = word;
86 break;
87
88 default:
89 /* We only care about type definitions */
90 break;
91 }
92
93 word += oplen;
94 }
95}
96
Chris Forbes3b1c4212015-04-08 10:11:59 +120097struct shader_source {
Chris Forbes3a5e99a2015-04-10 11:41:20 +120098 /* the spirv image itself */
Chris Forbes3b1c4212015-04-08 10:11:59 +120099 std::vector<uint32_t> words;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200100 /* a mapping of <id> to the first word of its def. this is useful because walking type
101 * trees requires jumping all over the instruction stream.
102 */
103 std::unordered_map<unsigned, unsigned> type_def_index;
Chris Forbesf044ec92015-06-05 15:01:08 +1200104 bool is_spirv;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200105
106 shader_source(VkShaderCreateInfo const *pCreateInfo) :
Chris Forbesf044ec92015-06-05 15:01:08 +1200107 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
108 type_def_index(),
109 is_spirv(true) {
110
111 if (words.size() < 5 || words[0] != spv::MagicNumber || words[1] != spv::Version) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600112 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
Chris Forbesf044ec92015-06-05 15:01:08 +1200113 "Shader is not SPIR-V, most checks will not be possible");
114 is_spirv = false;
115 return;
116 }
117
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200118
119 build_type_def_index(words, type_def_index);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200120 }
121};
122
123
124static std::unordered_map<void *, shader_source *> shader_map;
125
126
Chris Forbesb6b8c462015-04-15 06:59:41 +1200127static void
128initLayer()
129{
130 const char *strOpt;
131 // initialize ShaderChecker options
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600132 getLayerOptionEnum("ShaderCheckerReportLevel", (uint32_t *) &g_reportFlags);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200133 g_actionIsDefault = getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &g_debugAction);
134
135 if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)
136 {
137 strOpt = getLayerOption("ShaderCheckerLogFilename");
138 if (strOpt)
139 {
140 g_logFile = fopen(strOpt, "w");
141 }
142 if (g_logFile == NULL)
143 g_logFile = stdout;
144 }
145}
146
147
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600148static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *devw)
Chris Forbes2778f302015-04-02 13:22:31 +1300149{
150 VkLayerDispatchTable *pTable;
151
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600152 assert(devw);
153 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) devw->baseObject);
Chris Forbes2778f302015-04-02 13:22:31 +1300154 if (it == tableMap.end())
155 {
156 pTable = new VkLayerDispatchTable;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600157 tableMap[(void *) devw->baseObject] = pTable;
Chris Forbes2778f302015-04-02 13:22:31 +1300158 } else
159 {
160 return it->second;
161 }
162
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600163 layer_initialize_dispatch_table(pTable, (PFN_vkGetDeviceProcAddr) devw->pGPA, (VkDevice) devw->nextObject);
Chris Forbes2778f302015-04-02 13:22:31 +1300164
165 return pTable;
166}
167
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600168static VkLayerInstanceDispatchTable * initLayerInstanceTable(const VkBaseLayerObject *instw)
169{
170 VkLayerInstanceDispatchTable *pTable;
171
172 assert(instw);
173 std::unordered_map<void *, VkLayerInstanceDispatchTable *>::const_iterator it = tableInstanceMap.find((void *) instw->baseObject);
174 if (it == tableInstanceMap.end())
175 {
176 pTable = new VkLayerInstanceDispatchTable;
177 tableInstanceMap[(void *) instw->baseObject] = pTable;
178 } else
179 {
180 return it->second;
181 }
182
183 layer_init_instance_dispatch_table(pTable, (PFN_vkGetInstanceProcAddr) instw->pGPA, (VkInstance) instw->nextObject);
184
185 return pTable;
186}
Chris Forbes2778f302015-04-02 13:22:31 +1300187
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -0600188VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice physicalDevice, size_t maxStringSize, size_t* pLayerCount, char* const* pOutLayers, void* pReserved)
Chris Forbes2778f302015-04-02 13:22:31 +1300189{
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -0600190 if (pLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pOutLayers[1] == NULL || pReserved == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +1300191 return VK_ERROR_INVALID_POINTER;
192
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -0600193 if (*pLayerCount < 1)
Chris Forbes2778f302015-04-02 13:22:31 +1300194 return VK_ERROR_INITIALIZATION_FAILED;
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -0600195 *pLayerCount = 1;
Chris Forbes2778f302015-04-02 13:22:31 +1300196 strncpy((char *) pOutLayers[0], "ShaderChecker", maxStringSize);
197 return VK_SUCCESS;
198}
199
200
Tobin Ehlis5d9c2242015-04-17 08:55:13 -0600201#define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 2
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600202static const VkExtensionProperties shaderCheckerExts[SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE] = {
203 {
204 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
205 "ShaderChecker",
206 0x10,
207 "Sample layer: ShaderChecker",
208// 0,
209// NULL,
210 }
Chris Forbes2778f302015-04-02 13:22:31 +1300211};
212
Chris Forbes2778f302015-04-02 13:22:31 +1300213VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600214 VkExtensionInfoType infoType,
215 uint32_t extensionIndex,
216 size_t* pDataSize,
217 void* pData)
Chris Forbes2778f302015-04-02 13:22:31 +1300218{
Chris Forbes2778f302015-04-02 13:22:31 +1300219 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
Chris Forbes2778f302015-04-02 13:22:31 +1300220 uint32_t *count;
221
222 if (pDataSize == NULL)
223 return VK_ERROR_INVALID_POINTER;
224
225 switch (infoType) {
226 case VK_EXTENSION_INFO_TYPE_COUNT:
227 *pDataSize = sizeof(uint32_t);
228 if (pData == NULL)
229 return VK_SUCCESS;
230 count = (uint32_t *) pData;
231 *count = SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE;
232 break;
233 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
234 *pDataSize = sizeof(VkExtensionProperties);
235 if (pData == NULL)
236 return VK_SUCCESS;
237 if (extensionIndex >= SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE)
238 return VK_ERROR_INVALID_VALUE;
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600239 memcpy((VkExtensionProperties *) pData, &shaderCheckerExts[extensionIndex], sizeof(VkExtensionProperties));
Chris Forbes2778f302015-04-02 13:22:31 +1300240 break;
241 default:
242 return VK_ERROR_INVALID_VALUE;
243 };
244
245 return VK_SUCCESS;
246}
247
248
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200249static char const *
250storage_class_name(unsigned sc)
251{
252 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600253 case spv::StorageClassInput: return "input";
254 case spv::StorageClassOutput: return "output";
255 case spv::StorageClassUniformConstant: return "const uniform";
256 case spv::StorageClassUniform: return "uniform";
257 case spv::StorageClassWorkgroupLocal: return "workgroup local";
258 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
259 case spv::StorageClassPrivateGlobal: return "private global";
260 case spv::StorageClassFunction: return "function";
261 case spv::StorageClassGeneric: return "generic";
262 case spv::StorageClassPrivate: return "private";
263 case spv::StorageClassAtomicCounter: return "atomic counter";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200264 default: return "unknown";
265 }
266}
267
268
269/* returns ptr to null terminator */
270static char *
271describe_type(char *dst, shader_source const *src, unsigned type)
272{
273 auto type_def_it = src->type_def_index.find(type);
274
275 if (type_def_it == src->type_def_index.end()) {
276 return dst + sprintf(dst, "undef");
277 }
278
279 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
280 unsigned opcode = code[0] & 0x0ffffu;
281 switch (opcode) {
282 case spv::OpTypeBool:
283 return dst + sprintf(dst, "bool");
284 case spv::OpTypeInt:
285 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
286 case spv::OpTypeFloat:
287 return dst + sprintf(dst, "float%d", code[2]);
288 case spv::OpTypeVector:
289 dst += sprintf(dst, "vec%d of ", code[3]);
290 return describe_type(dst, src, code[2]);
291 case spv::OpTypeMatrix:
292 dst += sprintf(dst, "mat%d of ", code[3]);
293 return describe_type(dst, src, code[2]);
294 case spv::OpTypeArray:
295 dst += sprintf(dst, "arr[%d] of ", code[3]);
296 return describe_type(dst, src, code[2]);
297 case spv::OpTypePointer:
298 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
299 return describe_type(dst, src, code[3]);
300 case spv::OpTypeStruct:
301 {
302 unsigned oplen = code[0] >> 16;
303 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600304 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200305 dst = describe_type(dst, src, code[i]);
306 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
307 }
308 return dst;
309 }
310 default:
311 return dst + sprintf(dst, "oddtype");
312 }
313}
314
315
316static bool
Chris Forbesf3fc0332015-06-05 14:57:05 +1200317types_match(shader_source const *a, shader_source const *b, unsigned a_type, unsigned b_type, bool b_arrayed)
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200318{
319 auto a_type_def_it = a->type_def_index.find(a_type);
320 auto b_type_def_it = b->type_def_index.find(b_type);
321
322 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200323 return false;
324 }
325
326 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200327 return false;
328 }
329
330 /* walk two type trees together, and complain about differences */
331 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
332 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
333
334 unsigned a_opcode = a_code[0] & 0x0ffffu;
335 unsigned b_opcode = b_code[0] & 0x0ffffu;
336
Chris Forbesf3fc0332015-06-05 14:57:05 +1200337 if (b_arrayed && b_opcode == spv::OpTypeArray) {
338 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
339 return types_match(a, b, a_type, b_code[2], false);
340 }
341
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200342 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200343 return false;
344 }
345
346 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200347 /* 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 +1200348 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200349 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200350 case spv::OpTypeInt:
351 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200352 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200353 case spv::OpTypeFloat:
354 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200355 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200356 case spv::OpTypeVector:
357 case spv::OpTypeMatrix:
358 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200359 /* match on element type, count. these all have the same layout. we don't get here if
360 * b_arrayed -- that is handled above. */
361 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 +1200362 case spv::OpTypeStruct:
363 /* match on all element types */
364 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200365 if (b_arrayed) {
366 /* for the purposes of matching different levels of arrayness, structs are leaves. */
367 return false;
368 }
369
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200370 unsigned a_len = a_code[0] >> 16;
371 unsigned b_len = b_code[0] >> 16;
372
373 if (a_len != b_len) {
374 return false; /* structs cannot match if member counts differ */
375 }
376
Ian Elliott1cb62222015-04-17 11:05:04 -0600377 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200378 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200379 return false;
380 }
381 }
382
383 return true;
384 }
385 case spv::OpTypePointer:
386 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200387 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200388
389 default:
390 /* remaining types are CLisms, or may not appear in the interfaces we
391 * are interested in. Just claim no match.
392 */
393 return false;
394
395 }
396}
397
398
Chris Forbes06e8fc32015-04-13 12:14:52 +1200399static int
400value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
401{
402 auto it = map.find(id);
403 if (it == map.end())
404 return def;
405 else
406 return it->second;
407}
408
409
410struct interface_var {
411 uint32_t id;
412 uint32_t type_id;
413 /* TODO: collect the name, too? Isn't required to be present. */
414};
415
416
417static void
Ian Elliott1cb62222015-04-17 11:05:04 -0600418collect_interface_by_location(shader_source const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200419 std::map<uint32_t, interface_var> &out,
420 std::map<uint32_t, interface_var> &builtins_out)
421{
422 unsigned int const *code = (unsigned int const *)&src->words[0];
423 size_t size = src->words.size();
424
Chris Forbes06e8fc32015-04-13 12:14:52 +1200425 std::unordered_map<unsigned, unsigned> var_locations;
426 std::unordered_map<unsigned, unsigned> var_builtins;
427
428 unsigned word = 5;
429 while (word < size) {
430
431 unsigned opcode = code[word] & 0x0ffffu;
432 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
433
434 /* We consider two interface models: SSO rendezvous-by-location, and
435 * builtins. Complain about anything that fits neither model.
436 */
437 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600438 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200439 var_locations[code[word+1]] = code[word+3];
440 }
441
Cody Northrop97e52d82015-04-20 14:09:40 -0600442 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200443 var_builtins[code[word+1]] = code[word+3];
444 }
445 }
446
447 /* TODO: handle grouped decorations */
448 /* TODO: handle index=1 dual source outputs from FS -- two vars will
449 * have the same location, and we DONT want to clobber. */
450
Ian Elliott1cb62222015-04-17 11:05:04 -0600451 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200452 int location = value_or_default(var_locations, code[word+2], -1);
453 int builtin = value_or_default(var_builtins, code[word+2], -1);
454
455 if (location == -1 && builtin == -1) {
456 /* No location defined, and not bound to an API builtin.
457 * The spec says nothing about how this case works (or doesn't)
458 * for interface matching.
459 */
Chris Forbes6b2ead62015-04-17 10:13:28 +1200460 char str[1024];
461 sprintf(str, "var %d (type %d) in %s interface has no Location or Builtin decoration\n",
Ian Elliott1cb62222015-04-17 11:05:04 -0600462 code[word+2], code[word+1], storage_class_name(sinterface));
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600463 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", str);
Chris Forbes06e8fc32015-04-13 12:14:52 +1200464 }
465 else if (location != -1) {
466 /* A user-defined interface variable, with a location. */
467 interface_var v;
468 v.id = code[word+2];
469 v.type_id = code[word+1];
470 out[location] = v;
471 }
472 else {
473 /* A builtin interface variable */
474 interface_var v;
475 v.id = code[word+2];
476 v.type_id = code[word+1];
477 builtins_out[builtin] = v;
478 }
479 }
480
481 word += oplen;
482 }
483}
484
485
Chris Forbes2778f302015-04-02 13:22:31 +1300486VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo,
487 VkShader *pShader)
488{
Chris Forbes7f963832015-05-29 14:55:18 +1200489 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes2778f302015-04-02 13:22:31 +1300490 VkLayerDispatchTable* pTable = tableMap[(VkBaseLayerObject *)device];
491 VkResult res = pTable->CreateShader(device, pCreateInfo, pShader);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200492
493 shader_map[(VkBaseLayerObject *) *pShader] = new shader_source(pCreateInfo);
Chris Forbes7f963832015-05-29 14:55:18 +1200494 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes2778f302015-04-02 13:22:31 +1300495 return res;
496}
497
498
Chris Forbesee99b9b2015-05-25 11:13:22 +1200499static bool
Chris Forbes41002452015-04-08 10:19:16 +1200500validate_interface_between_stages(shader_source const *producer, char const *producer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200501 shader_source const *consumer, char const *consumer_name,
502 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200503{
504 std::map<uint32_t, interface_var> outputs;
505 std::map<uint32_t, interface_var> inputs;
506
507 std::map<uint32_t, interface_var> builtin_outputs;
508 std::map<uint32_t, interface_var> builtin_inputs;
509
Chris Forbes6b2ead62015-04-17 10:13:28 +1200510 char str[1024];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200511 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200512
Cody Northrop97e52d82015-04-20 14:09:40 -0600513 collect_interface_by_location(producer, spv::StorageClassOutput, outputs, builtin_outputs);
514 collect_interface_by_location(consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200515
516 auto a_it = outputs.begin();
517 auto b_it = inputs.begin();
518
519 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600520 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
521 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
522 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200523 auto a_first = a_at_end ? 0 : a_it->first;
524 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600525
526 if (b_at_end || a_first < b_first) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200527 sprintf(str, "%s writes to output location %d which is not consumed by %s\n",
David Pinedod8f83d82015-04-27 16:36:17 -0600528 producer_name, a_first, consumer_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600529 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes41002452015-04-08 10:19:16 +1200530 a_it++;
531 }
David Pinedod8f83d82015-04-27 16:36:17 -0600532 else if (a_at_end || a_first > b_first) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200533 sprintf(str, "%s consumes input location %d which is not written by %s\n",
David Pinedod8f83d82015-04-27 16:36:17 -0600534 consumer_name, b_first, producer_name);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600535 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200536 pass = false;
Chris Forbes41002452015-04-08 10:19:16 +1200537 b_it++;
538 }
539 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200540 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 +1200541 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200542 }
543 else {
544 char producer_type[1024];
545 char consumer_type[1024];
546 describe_type(producer_type, producer, a_it->second.type_id);
547 describe_type(consumer_type, consumer, b_it->second.type_id);
548
Chris Forbes6b2ead62015-04-17 10:13:28 +1200549 sprintf(str, "Type mismatch on location %d: '%s' vs '%s'\n", a_it->first,
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200550 producer_type, consumer_type);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600551 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200552 pass = false;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200553 }
Chris Forbes41002452015-04-08 10:19:16 +1200554 a_it++;
555 b_it++;
556 }
557 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200558
559 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200560}
561
562
Chris Forbes3616b462015-04-08 10:37:20 +1200563enum FORMAT_TYPE {
564 FORMAT_TYPE_UNDEFINED,
565 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
566 FORMAT_TYPE_SINT,
567 FORMAT_TYPE_UINT,
568};
569
570
571static unsigned
572get_format_type(VkFormat fmt) {
573 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800574 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200575 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800576 case VK_FORMAT_R8_SINT:
577 case VK_FORMAT_R8G8_SINT:
578 case VK_FORMAT_R8G8B8_SINT:
579 case VK_FORMAT_R8G8B8A8_SINT:
580 case VK_FORMAT_R16_SINT:
581 case VK_FORMAT_R16G16_SINT:
582 case VK_FORMAT_R16G16B16_SINT:
583 case VK_FORMAT_R16G16B16A16_SINT:
584 case VK_FORMAT_R32_SINT:
585 case VK_FORMAT_R32G32_SINT:
586 case VK_FORMAT_R32G32B32_SINT:
587 case VK_FORMAT_R32G32B32A32_SINT:
588 case VK_FORMAT_B8G8R8_SINT:
589 case VK_FORMAT_B8G8R8A8_SINT:
590 case VK_FORMAT_R10G10B10A2_SINT:
591 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200592 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800593 case VK_FORMAT_R8_UINT:
594 case VK_FORMAT_R8G8_UINT:
595 case VK_FORMAT_R8G8B8_UINT:
596 case VK_FORMAT_R8G8B8A8_UINT:
597 case VK_FORMAT_R16_UINT:
598 case VK_FORMAT_R16G16_UINT:
599 case VK_FORMAT_R16G16B16_UINT:
600 case VK_FORMAT_R16G16B16A16_UINT:
601 case VK_FORMAT_R32_UINT:
602 case VK_FORMAT_R32G32_UINT:
603 case VK_FORMAT_R32G32B32_UINT:
604 case VK_FORMAT_R32G32B32A32_UINT:
605 case VK_FORMAT_B8G8R8_UINT:
606 case VK_FORMAT_B8G8R8A8_UINT:
607 case VK_FORMAT_R10G10B10A2_UINT:
608 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200609 return FORMAT_TYPE_UINT;
610 default:
611 return FORMAT_TYPE_FLOAT;
612 }
613}
614
615
Chris Forbes156a1162015-05-04 14:04:06 +1200616/* characterizes a SPIR-V type appearing in an interface to a FF stage,
617 * for comparison to a VkFormat's characterization above. */
618static unsigned
619get_fundamental_type(shader_source const *src, unsigned type)
620{
621 auto type_def_it = src->type_def_index.find(type);
622
623 if (type_def_it == src->type_def_index.end()) {
624 return FORMAT_TYPE_UNDEFINED;
625 }
626
627 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
628 unsigned opcode = code[0] & 0x0ffffu;
629 switch (opcode) {
630 case spv::OpTypeInt:
631 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
632 case spv::OpTypeFloat:
633 return FORMAT_TYPE_FLOAT;
634 case spv::OpTypeVector:
635 return get_fundamental_type(src, code[2]);
636 case spv::OpTypeMatrix:
637 return get_fundamental_type(src, code[2]);
638 case spv::OpTypeArray:
639 return get_fundamental_type(src, code[2]);
640 case spv::OpTypePointer:
641 return get_fundamental_type(src, code[3]);
642 default:
643 return FORMAT_TYPE_UNDEFINED;
644 }
645}
646
647
Chris Forbesee99b9b2015-05-25 11:13:22 +1200648static bool
Chris Forbes280ba2c2015-06-12 11:16:41 +1200649validate_vi_consistency(VkPipelineVertexInputCreateInfo const *vi)
650{
651 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
652 * each binding should be specified only once.
653 */
654 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
655 char str[1024];
656 bool pass = true;
657
658 for (unsigned i = 0; i < vi->bindingCount; i++) {
659 auto desc = &vi->pVertexBindingDescriptions[i];
660 auto & binding = bindings[desc->binding];
661 if (binding) {
662 sprintf(str, "Duplicate vertex input binding descriptions for binding %d", desc->binding);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600663 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INCONSISTENT_VI, "SC", str);
Chris Forbes280ba2c2015-06-12 11:16:41 +1200664 pass = false;
665 }
666 else {
667 binding = desc;
668 }
669 }
670
671 return pass;
672}
673
674
675static bool
Chris Forbes772d03b2015-04-08 10:36:37 +1200676validate_vi_against_vs_inputs(VkPipelineVertexInputCreateInfo const *vi, shader_source const *vs)
677{
678 std::map<uint32_t, interface_var> inputs;
679 /* we collect builtin inputs, but they will never appear in the VI state --
680 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
681 */
682 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200683 char str[1024];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200684 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200685
Cody Northrop97e52d82015-04-20 14:09:40 -0600686 collect_interface_by_location(vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200687
688 /* Build index by location */
689 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200690 if (vi) {
691 for (unsigned i = 0; i < vi->attributeCount; i++)
692 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
693 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200694
695 auto it_a = attribs.begin();
696 auto it_b = inputs.begin();
697
David Pinedod8f83d82015-04-27 16:36:17 -0600698 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
699 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
700 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200701 auto a_first = a_at_end ? 0 : it_a->first;
702 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600703 if (b_at_end || a_first < b_first) {
704 sprintf(str, "Vertex attribute at location %d not consumed by VS", a_first);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600705 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes772d03b2015-04-08 10:36:37 +1200706 it_a++;
707 }
David Pinedod8f83d82015-04-27 16:36:17 -0600708 else if (a_at_end || b_first < a_first) {
709 sprintf(str, "VS consumes input at location %d but not provided", b_first);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600710 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200711 pass = false;
Chris Forbes772d03b2015-04-08 10:36:37 +1200712 it_b++;
713 }
714 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200715 unsigned attrib_type = get_format_type(it_a->second->format);
716 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
717
718 /* type checking */
719 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
720 char vs_type[1024];
721 describe_type(vs_type, vs, it_b->second.type_id);
722 sprintf(str, "Attribute type of `%s` at location %d does not match VS input type of `%s`",
723 string_VkFormat(it_a->second->format), a_first, vs_type);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600724 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200725 pass = false;
Chris Forbes401784b2015-05-04 14:04:24 +1200726 }
727
Chris Forbes6b2ead62015-04-17 10:13:28 +1200728 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200729 it_a++;
730 it_b++;
731 }
732 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200733
734 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200735}
736
737
Chris Forbesee99b9b2015-05-25 11:13:22 +1200738static bool
Chris Forbes3616b462015-04-08 10:37:20 +1200739validate_fs_outputs_against_cb(shader_source const *fs, VkPipelineCbStateCreateInfo const *cb)
740{
741 std::map<uint32_t, interface_var> outputs;
742 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200743 char str[1024];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200744 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200745
746 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
747
Cody Northrop97e52d82015-04-20 14:09:40 -0600748 collect_interface_by_location(fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200749
750 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
751 * and all color attachment should be UNORM/SNORM/FLOAT.
752 */
753 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200754 if (outputs.size()) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600755 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC",
Chris Forbes6b2ead62015-04-17 10:13:28 +1200756 "Should not have user-defined FS outputs when using broadcast");
Chris Forbesee99b9b2015-05-25 11:13:22 +1200757 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200758 }
759
Ian Elliott1cb62222015-04-17 11:05:04 -0600760 for (unsigned i = 0; i < cb->attachmentCount; i++) {
Chris Forbes3616b462015-04-08 10:37:20 +1200761 unsigned attachmentType = get_format_type(cb->pAttachments[i].format);
762 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600763 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbes6b2ead62015-04-17 10:13:28 +1200764 "CB format should not be SINT or UINT when using broadcast");
Chris Forbesee99b9b2015-05-25 11:13:22 +1200765 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200766 }
767 }
768
Chris Forbesee99b9b2015-05-25 11:13:22 +1200769 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200770 }
771
772 auto it = outputs.begin();
773 uint32_t attachment = 0;
774
775 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
776 * are currently dense, but the parallel with matching between shader stages is nice.
777 */
778
Chris Forbesbf2b1d22015-05-05 11:34:14 +1200779 while ((outputs.size() > 0 && it != outputs.end()) || attachment < cb->attachmentCount) {
scygan3a22ce92015-06-01 19:48:11 +0200780 if (attachment == cb->attachmentCount || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200781 sprintf(str, "FS writes to output location %d with no matching attachment", it->first);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600782 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes3616b462015-04-08 10:37:20 +1200783 it++;
784 }
785 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200786 sprintf(str, "Attachment %d not written by FS", attachment);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600787 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbes3616b462015-04-08 10:37:20 +1200788 attachment++;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200789 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200790 }
791 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200792 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
793 unsigned att_type = get_format_type(cb->pAttachments[attachment].format);
794
795 /* type checking */
796 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
797 char fs_type[1024];
798 describe_type(fs_type, fs, it->second.type_id);
799 sprintf(str, "Attachment %d of type `%s` does not match FS output type of `%s`",
800 attachment, string_VkFormat(cb->pAttachments[attachment].format), fs_type);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600801 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200802 pass = false;
Chris Forbes46d31e52015-05-04 14:20:10 +1200803 }
804
Chris Forbes6b2ead62015-04-17 10:13:28 +1200805 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200806 it++;
807 attachment++;
808 }
809 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200810
811 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200812}
813
814
Chris Forbesf044ec92015-06-05 15:01:08 +1200815struct shader_stage_attributes {
816 char const * const name;
817 bool arrayed_input;
818};
819
820
821static shader_stage_attributes
822shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
823 { "vertex shader", false },
824 { "tessellation control shader", true },
825 { "tessellation evaluation shader", false },
826 { "geometry shader", true },
827 { "fragment shader", false },
828};
829
830
Chris Forbes81874ba2015-06-04 20:23:00 +1200831static bool
832validate_graphics_pipeline(VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +1200833{
Chris Forbesf6800b52015-04-08 10:16:45 +1200834 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
835 * before trying to do anything more: */
836
Chris Forbesf044ec92015-06-05 15:01:08 +1200837 shader_source const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
838 memset(shaders, 0, sizeof(shaders));
Chris Forbesf6800b52015-04-08 10:16:45 +1200839 VkPipelineCbStateCreateInfo const *cb = 0;
840 VkPipelineVertexInputCreateInfo const *vi = 0;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200841 char str[1024];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200842 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +1200843
Chris Forbes7f963832015-05-29 14:55:18 +1200844 loader_platform_thread_lock_mutex(&globalLock);
845
Chris Forbesf6800b52015-04-08 10:16:45 +1200846 for (auto stage = pCreateInfo; stage; stage = (decltype(stage))stage->pNext) {
847 if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
848 auto shader_stage = (VkPipelineShaderStageCreateInfo const *)stage;
849
Chris Forbesf044ec92015-06-05 15:01:08 +1200850 if (shader_stage->shader.stage < VK_SHADER_STAGE_VERTEX || shader_stage->shader.stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200851 sprintf(str, "Unknown shader stage %d\n", shader_stage->shader.stage);
Courtney Goeltzenleuchterf579fa62015-06-10 17:39:03 -0600852 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC", str);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200853 }
Chris Forbesf044ec92015-06-05 15:01:08 +1200854 else {
855 shaders[shader_stage->shader.stage] = shader_map[(void *)(shader_stage->shader.shader)];
856 }
Chris Forbesf6800b52015-04-08 10:16:45 +1200857 }
858 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO) {
859 cb = (VkPipelineCbStateCreateInfo const *)stage;
860 }
861 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO) {
862 vi = (VkPipelineVertexInputCreateInfo const *)stage;
863 }
864 }
865
Chris Forbes280ba2c2015-06-12 11:16:41 +1200866 if (vi) {
867 pass = validate_vi_consistency(vi) && pass;
868 }
869
Chris Forbesf044ec92015-06-05 15:01:08 +1200870 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
871 pass = validate_vi_against_vs_inputs(vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200872 }
873
Chris Forbesf044ec92015-06-05 15:01:08 +1200874 /* TODO: enforce rules about present combinations of shaders */
875 int producer = VK_SHADER_STAGE_VERTEX;
876 int consumer = VK_SHADER_STAGE_GEOMETRY;
877
878 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
879 producer++;
880 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +1200881 }
882
Tony Barbour0102a902015-06-11 15:04:25 -0600883 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +1200884 assert(shaders[producer]);
885 if (shaders[consumer]) {
886 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
887 pass = validate_interface_between_stages(shaders[producer], shader_stage_attribs[producer].name,
888 shaders[consumer], shader_stage_attribs[consumer].name,
889 shader_stage_attribs[consumer].arrayed_input) && pass;
890 }
891
892 producer = consumer;
893 }
894 }
895
896 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && cb) {
897 pass = validate_fs_outputs_against_cb(shaders[VK_SHADER_STAGE_FRAGMENT], cb) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200898 }
899
Chris Forbes7f963832015-05-29 14:55:18 +1200900 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +1200901 return pass;
902}
903
904
Chris Forbes39d8d752015-06-04 20:27:09 +1200905VK_LAYER_EXPORT VkResult VKAPI
906vkCreateGraphicsPipeline(VkDevice device,
907 const VkGraphicsPipelineCreateInfo *pCreateInfo,
908 VkPipeline *pPipeline)
Chris Forbes81874ba2015-06-04 20:23:00 +1200909{
910 bool pass = validate_graphics_pipeline(pCreateInfo);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200911
912 if (pass) {
913 /* The driver is allowed to crash if passed junk. Only actually create the
914 * pipeline if we didn't run into any showstoppers above.
915 */
Chris Forbes81874ba2015-06-04 20:23:00 +1200916 VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200917 return pTable->CreateGraphicsPipeline(device, pCreateInfo, pPipeline);
918 }
919 else {
920 return VK_ERROR_UNKNOWN;
921 }
Chris Forbes4175e6f2015-04-08 10:15:35 +1200922}
923
924
Chris Forbes39d8d752015-06-04 20:27:09 +1200925VK_LAYER_EXPORT VkResult VKAPI
926vkCreateGraphicsPipelineDerivative(VkDevice device,
927 const VkGraphicsPipelineCreateInfo *pCreateInfo,
928 VkPipeline basePipeline,
929 VkPipeline *pPipeline)
930{
931 bool pass = validate_graphics_pipeline(pCreateInfo);
932
933 if (pass) {
934 /* The driver is allowed to crash if passed junk. Only actually create the
935 * pipeline if we didn't run into any showstoppers above.
936 */
937 VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device];
938 return pTable->CreateGraphicsPipelineDerivative(device, pCreateInfo, basePipeline, pPipeline);
939 }
940 else {
941 return VK_ERROR_UNKNOWN;
942 }
943}
944
945
Jon Ashburn9a8a2e22015-05-19 16:34:53 -0600946/* hook DextroyDevice to remove tableMap entry */
947VK_LAYER_EXPORT VkResult VKAPI vkDestroyDevice(VkDevice device)
948{
949 VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device];
950 VkResult res = pTable->DestroyDevice(device);
951 tableMap.erase(device);
952 return res;
953}
954
955/* hook DestroyInstance to remove tableInstanceMap entry */
956VK_LAYER_EXPORT VkResult VKAPI vkDestroyInstance(VkInstance instance)
957{
958 VkLayerInstanceDispatchTable *pTable = tableInstanceMap[(VkBaseLayerObject *)instance];
959 VkResult res = pTable->DestroyInstance(instance);
960 tableInstanceMap.erase(instance);
961 return res;
962}
Chris Forbesdb467bd2015-05-25 11:12:59 +1200963
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600964VK_LAYER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char* pName)
Chris Forbes2778f302015-04-02 13:22:31 +1300965{
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600966 if (device == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +1300967 return NULL;
968
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600969 initLayerTable((const VkBaseLayerObject *) device);
Chris Forbes2778f302015-04-02 13:22:31 +1300970
Chris Forbesb6b8c462015-04-15 06:59:41 +1200971 loader_platform_thread_once(&g_initOnce, initLayer);
972
Chris Forbes2778f302015-04-02 13:22:31 +1300973#define ADD_HOOK(fn) \
974 if (!strncmp(#fn, pName, sizeof(#fn))) \
975 return (void *) fn
976
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600977 ADD_HOOK(vkGetDeviceProcAddr);
Chris Forbes2778f302015-04-02 13:22:31 +1300978 ADD_HOOK(vkCreateShader);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -0600979 ADD_HOOK(vkDestroyDevice);
Chris Forbes4175e6f2015-04-08 10:15:35 +1200980 ADD_HOOK(vkCreateGraphicsPipeline);
Chris Forbes39d8d752015-06-04 20:27:09 +1200981 ADD_HOOK(vkCreateGraphicsPipelineDerivative);
Jon Ashburne59f84f2015-05-18 09:08:41 -0600982#undef ADD_HOOK
Chris Forbes2778f302015-04-02 13:22:31 +1300983
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600984 VkBaseLayerObject* devw = (VkBaseLayerObject *) device;
985 if (devw->pGPA == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +1300986 return NULL;
Jon Ashburn8d1b0b52015-05-18 13:20:15 -0600987 return devw->pGPA((VkObject) devw->nextObject, pName);
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600988}
989
990VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance inst, const char* pName)
991{
992 if (inst == NULL)
993 return NULL;
994
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600995 initLayerInstanceTable((const VkBaseLayerObject *) inst);
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600996
Jon Ashburn8c5cbcf2015-05-07 10:27:37 -0600997 loader_platform_thread_once(&g_initOnce, initLayer);
Jon Ashburnf6b33db2015-05-05 14:22:52 -0600998
999#define ADD_HOOK(fn) \
1000 if (!strncmp(#fn, pName, sizeof(#fn))) \
1001 return (void *) fn
1002
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001003 ADD_HOOK(vkGetInstanceProcAddr);
Jon Ashburn9a8a2e22015-05-19 16:34:53 -06001004 ADD_HOOK(vkDestroyInstance);
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001005 ADD_HOOK(vkEnumerateLayers);
1006 ADD_HOOK(vkGetGlobalExtensionInfo);
Jon Ashburne59f84f2015-05-18 09:08:41 -06001007#undef ADD_HOOK
Jon Ashburnf6b33db2015-05-05 14:22:52 -06001008
1009 VkBaseLayerObject* instw = (VkBaseLayerObject *) inst;
1010 if (instw->pGPA == NULL)
1011 return NULL;
1012 return instw->pGPA((VkObject) instw->nextObject, pName);
Chris Forbes2778f302015-04-02 13:22:31 +13001013}