blob: a576314a80481e6d82d42fd3f7c0c685c116319e [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;
Chris Forbesb6b8c462015-04-15 06:59:41 +120047static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes7f963832015-05-29 14:55:18 +120048// TODO : This can be much smarter, using separate locks for separate global data
49static int globalLockInitialized = 0;
50static loader_platform_thread_mutex globalLock;
Chris Forbes3b1c4212015-04-08 10:11:59 +120051
Chris Forbes3a5e99a2015-04-10 11:41:20 +120052
53static void
54build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
55{
56 unsigned int const *code = (unsigned int const *)&words[0];
57 size_t size = words.size();
58
59 unsigned word = 5;
60 while (word < size) {
61 unsigned opcode = code[word] & 0x0ffffu;
62 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
63
64 switch (opcode) {
65 case spv::OpTypeVoid:
66 case spv::OpTypeBool:
67 case spv::OpTypeInt:
68 case spv::OpTypeFloat:
69 case spv::OpTypeVector:
70 case spv::OpTypeMatrix:
71 case spv::OpTypeSampler:
72 case spv::OpTypeFilter:
73 case spv::OpTypeArray:
74 case spv::OpTypeRuntimeArray:
75 case spv::OpTypeStruct:
76 case spv::OpTypeOpaque:
77 case spv::OpTypePointer:
78 case spv::OpTypeFunction:
79 case spv::OpTypeEvent:
80 case spv::OpTypeDeviceEvent:
81 case spv::OpTypeReserveId:
82 case spv::OpTypeQueue:
83 case spv::OpTypePipe:
84 type_def_index[code[word+1]] = word;
85 break;
86
87 default:
88 /* We only care about type definitions */
89 break;
90 }
91
92 word += oplen;
93 }
94}
95
Chris Forbes3b1c4212015-04-08 10:11:59 +120096struct shader_source {
Chris Forbes3a5e99a2015-04-10 11:41:20 +120097 /* the spirv image itself */
Chris Forbes3b1c4212015-04-08 10:11:59 +120098 std::vector<uint32_t> words;
Chris Forbes3a5e99a2015-04-10 11:41:20 +120099 /* a mapping of <id> to the first word of its def. this is useful because walking type
100 * trees requires jumping all over the instruction stream.
101 */
102 std::unordered_map<unsigned, unsigned> type_def_index;
Chris Forbesf044ec92015-06-05 15:01:08 +1200103 bool is_spirv;
Chris Forbes3b1c4212015-04-08 10:11:59 +1200104
105 shader_source(VkShaderCreateInfo const *pCreateInfo) :
Chris Forbesf044ec92015-06-05 15:01:08 +1200106 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
107 type_def_index(),
108 is_spirv(true) {
109
110 if (words.size() < 5 || words[0] != spv::MagicNumber || words[1] != spv::Version) {
111 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
112 "Shader is not SPIR-V, most checks will not be possible");
113 is_spirv = false;
114 return;
115 }
116
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200117
118 build_type_def_index(words, type_def_index);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200119 }
120};
121
122
123static std::unordered_map<void *, shader_source *> shader_map;
124
125
Chris Forbesb6b8c462015-04-15 06:59:41 +1200126static void
127initLayer()
128{
129 const char *strOpt;
130 // initialize ShaderChecker options
131 getLayerOptionEnum("ShaderCheckerReportLevel", (uint32_t *) &g_reportingLevel);
132 g_actionIsDefault = getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &g_debugAction);
133
134 if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)
135 {
136 strOpt = getLayerOption("ShaderCheckerLogFilename");
137 if (strOpt)
138 {
139 g_logFile = fopen(strOpt, "w");
140 }
141 if (g_logFile == NULL)
142 g_logFile = stdout;
143 }
144}
145
146
Chris Forbes2778f302015-04-02 13:22:31 +1300147static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *gpuw)
148{
149 VkLayerDispatchTable *pTable;
150
151 assert(gpuw);
152 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) gpuw->baseObject);
153 if (it == tableMap.end())
154 {
155 pTable = new VkLayerDispatchTable;
156 tableMap[(void *) gpuw->baseObject] = pTable;
157 } else
158 {
159 return it->second;
160 }
161
Chia-I Wua3b9a202015-04-17 02:00:54 +0800162 layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalDevice) gpuw->nextObject);
Chris Forbes7f963832015-05-29 14:55:18 +1200163 pCurObj = (VkBaseLayerObject *)gpuw->baseObject;
Chris Forbes2778f302015-04-02 13:22:31 +1300164
165 return pTable;
166}
167
168
Chia-I Wua3b9a202015-04-17 02:00:54 +0800169VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
Chris Forbes2778f302015-04-02 13:22:31 +1300170{
171 VkLayerDispatchTable* pTable = tableMap[gpu];
172 VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200173
174 loader_platform_thread_once(&g_initOnce, initLayer);
Chris Forbes2778f302015-04-02 13:22:31 +1300175 // create a mapping for the device object into the dispatch table
176 tableMap.emplace(*pDevice, pTable);
Chris Forbes7f963832015-05-29 14:55:18 +1200177 pCurObj = (VkBaseLayerObject *) *pDevice;
Chris Forbes2778f302015-04-02 13:22:31 +1300178 return result;
179}
180
181
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -0600182VK_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 +1300183{
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -0600184 if (pLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pOutLayers[1] == NULL || pReserved == NULL)
Chris Forbes2778f302015-04-02 13:22:31 +1300185 return VK_ERROR_INVALID_POINTER;
186
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -0600187 if (*pLayerCount < 1)
Chris Forbes2778f302015-04-02 13:22:31 +1300188 return VK_ERROR_INITIALIZATION_FAILED;
Courtney Goeltzenleuchterd9dc0c72015-04-20 11:04:54 -0600189 *pLayerCount = 1;
Chris Forbes2778f302015-04-02 13:22:31 +1300190 strncpy((char *) pOutLayers[0], "ShaderChecker", maxStringSize);
191 return VK_SUCCESS;
192}
193
194
195struct extProps {
196 uint32_t version;
197 const char * const name;
198};
Tobin Ehlis5d9c2242015-04-17 08:55:13 -0600199#define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 2
Chris Forbes2778f302015-04-02 13:22:31 +1300200static const struct extProps shaderCheckerExts[SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE] = {
201 // TODO what is the version?
202 0x10, "ShaderChecker",
Tobin Ehlis5d9c2242015-04-17 08:55:13 -0600203 0x10, "Validation",
Chris Forbes2778f302015-04-02 13:22:31 +1300204};
205
Chris Forbes2778f302015-04-02 13:22:31 +1300206VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
207 VkExtensionInfoType infoType,
208 uint32_t extensionIndex,
209 size_t* pDataSize,
210 void* pData)
211{
Chris Forbes2778f302015-04-02 13:22:31 +1300212 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
213 VkExtensionProperties *ext_props;
214 uint32_t *count;
215
216 if (pDataSize == NULL)
217 return VK_ERROR_INVALID_POINTER;
218
219 switch (infoType) {
220 case VK_EXTENSION_INFO_TYPE_COUNT:
221 *pDataSize = sizeof(uint32_t);
222 if (pData == NULL)
223 return VK_SUCCESS;
224 count = (uint32_t *) pData;
225 *count = SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE;
226 break;
227 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
228 *pDataSize = sizeof(VkExtensionProperties);
229 if (pData == NULL)
230 return VK_SUCCESS;
231 if (extensionIndex >= SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE)
232 return VK_ERROR_INVALID_VALUE;
233 ext_props = (VkExtensionProperties *) pData;
234 ext_props->version = shaderCheckerExts[extensionIndex].version;
235 strncpy(ext_props->extName, shaderCheckerExts[extensionIndex].name,
236 VK_MAX_EXTENSION_NAME);
237 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
238 break;
239 default:
240 return VK_ERROR_INVALID_VALUE;
241 };
242
243 return VK_SUCCESS;
244}
245
246
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200247static char const *
248storage_class_name(unsigned sc)
249{
250 switch (sc) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600251 case spv::StorageClassInput: return "input";
252 case spv::StorageClassOutput: return "output";
253 case spv::StorageClassUniformConstant: return "const uniform";
254 case spv::StorageClassUniform: return "uniform";
255 case spv::StorageClassWorkgroupLocal: return "workgroup local";
256 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
257 case spv::StorageClassPrivateGlobal: return "private global";
258 case spv::StorageClassFunction: return "function";
259 case spv::StorageClassGeneric: return "generic";
260 case spv::StorageClassPrivate: return "private";
261 case spv::StorageClassAtomicCounter: return "atomic counter";
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200262 default: return "unknown";
263 }
264}
265
266
267/* returns ptr to null terminator */
268static char *
269describe_type(char *dst, shader_source const *src, unsigned type)
270{
271 auto type_def_it = src->type_def_index.find(type);
272
273 if (type_def_it == src->type_def_index.end()) {
274 return dst + sprintf(dst, "undef");
275 }
276
277 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
278 unsigned opcode = code[0] & 0x0ffffu;
279 switch (opcode) {
280 case spv::OpTypeBool:
281 return dst + sprintf(dst, "bool");
282 case spv::OpTypeInt:
283 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
284 case spv::OpTypeFloat:
285 return dst + sprintf(dst, "float%d", code[2]);
286 case spv::OpTypeVector:
287 dst += sprintf(dst, "vec%d of ", code[3]);
288 return describe_type(dst, src, code[2]);
289 case spv::OpTypeMatrix:
290 dst += sprintf(dst, "mat%d of ", code[3]);
291 return describe_type(dst, src, code[2]);
292 case spv::OpTypeArray:
293 dst += sprintf(dst, "arr[%d] of ", code[3]);
294 return describe_type(dst, src, code[2]);
295 case spv::OpTypePointer:
296 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
297 return describe_type(dst, src, code[3]);
298 case spv::OpTypeStruct:
299 {
300 unsigned oplen = code[0] >> 16;
301 dst += sprintf(dst, "struct of (");
Ian Elliott1cb62222015-04-17 11:05:04 -0600302 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200303 dst = describe_type(dst, src, code[i]);
304 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
305 }
306 return dst;
307 }
308 default:
309 return dst + sprintf(dst, "oddtype");
310 }
311}
312
313
314static bool
Chris Forbesf3fc0332015-06-05 14:57:05 +1200315types_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 +1200316{
317 auto a_type_def_it = a->type_def_index.find(a_type);
318 auto b_type_def_it = b->type_def_index.find(b_type);
319
320 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200321 return false;
322 }
323
324 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200325 return false;
326 }
327
328 /* walk two type trees together, and complain about differences */
329 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
330 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
331
332 unsigned a_opcode = a_code[0] & 0x0ffffu;
333 unsigned b_opcode = b_code[0] & 0x0ffffu;
334
Chris Forbesf3fc0332015-06-05 14:57:05 +1200335 if (b_arrayed && b_opcode == spv::OpTypeArray) {
336 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
337 return types_match(a, b, a_type, b_code[2], false);
338 }
339
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200340 if (a_opcode != b_opcode) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200341 return false;
342 }
343
344 switch (a_opcode) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200345 /* 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 +1200346 case spv::OpTypeBool:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200347 return true && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200348 case spv::OpTypeInt:
349 /* match on width, signedness */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200350 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200351 case spv::OpTypeFloat:
352 /* match on width */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200353 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200354 case spv::OpTypeVector:
355 case spv::OpTypeMatrix:
356 case spv::OpTypeArray:
Chris Forbesf3fc0332015-06-05 14:57:05 +1200357 /* match on element type, count. these all have the same layout. we don't get here if
358 * b_arrayed -- that is handled above. */
359 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 +1200360 case spv::OpTypeStruct:
361 /* match on all element types */
362 {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200363 if (b_arrayed) {
364 /* for the purposes of matching different levels of arrayness, structs are leaves. */
365 return false;
366 }
367
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200368 unsigned a_len = a_code[0] >> 16;
369 unsigned b_len = b_code[0] >> 16;
370
371 if (a_len != b_len) {
372 return false; /* structs cannot match if member counts differ */
373 }
374
Ian Elliott1cb62222015-04-17 11:05:04 -0600375 for (unsigned i = 2; i < a_len; i++) {
Chris Forbesf3fc0332015-06-05 14:57:05 +1200376 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200377 return false;
378 }
379 }
380
381 return true;
382 }
383 case spv::OpTypePointer:
384 /* match on pointee type. storage class is expected to differ */
Chris Forbesf3fc0332015-06-05 14:57:05 +1200385 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200386
387 default:
388 /* remaining types are CLisms, or may not appear in the interfaces we
389 * are interested in. Just claim no match.
390 */
391 return false;
392
393 }
394}
395
396
Chris Forbes06e8fc32015-04-13 12:14:52 +1200397static int
398value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
399{
400 auto it = map.find(id);
401 if (it == map.end())
402 return def;
403 else
404 return it->second;
405}
406
407
408struct interface_var {
409 uint32_t id;
410 uint32_t type_id;
411 /* TODO: collect the name, too? Isn't required to be present. */
412};
413
414
415static void
Ian Elliott1cb62222015-04-17 11:05:04 -0600416collect_interface_by_location(shader_source const *src, spv::StorageClass sinterface,
Chris Forbes06e8fc32015-04-13 12:14:52 +1200417 std::map<uint32_t, interface_var> &out,
418 std::map<uint32_t, interface_var> &builtins_out)
419{
420 unsigned int const *code = (unsigned int const *)&src->words[0];
421 size_t size = src->words.size();
422
Chris Forbes06e8fc32015-04-13 12:14:52 +1200423 std::unordered_map<unsigned, unsigned> var_locations;
424 std::unordered_map<unsigned, unsigned> var_builtins;
425
426 unsigned word = 5;
427 while (word < size) {
428
429 unsigned opcode = code[word] & 0x0ffffu;
430 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
431
432 /* We consider two interface models: SSO rendezvous-by-location, and
433 * builtins. Complain about anything that fits neither model.
434 */
435 if (opcode == spv::OpDecorate) {
Cody Northrop97e52d82015-04-20 14:09:40 -0600436 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200437 var_locations[code[word+1]] = code[word+3];
438 }
439
Cody Northrop97e52d82015-04-20 14:09:40 -0600440 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200441 var_builtins[code[word+1]] = code[word+3];
442 }
443 }
444
445 /* TODO: handle grouped decorations */
446 /* TODO: handle index=1 dual source outputs from FS -- two vars will
447 * have the same location, and we DONT want to clobber. */
448
Ian Elliott1cb62222015-04-17 11:05:04 -0600449 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes06e8fc32015-04-13 12:14:52 +1200450 int location = value_or_default(var_locations, code[word+2], -1);
451 int builtin = value_or_default(var_builtins, code[word+2], -1);
452
453 if (location == -1 && builtin == -1) {
454 /* No location defined, and not bound to an API builtin.
455 * The spec says nothing about how this case works (or doesn't)
456 * for interface matching.
457 */
Chris Forbes6b2ead62015-04-17 10:13:28 +1200458 char str[1024];
459 sprintf(str, "var %d (type %d) in %s interface has no Location or Builtin decoration\n",
Ian Elliott1cb62222015-04-17 11:05:04 -0600460 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes6b2ead62015-04-17 10:13:28 +1200461 layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", str);
Chris Forbes06e8fc32015-04-13 12:14:52 +1200462 }
463 else if (location != -1) {
464 /* A user-defined interface variable, with a location. */
465 interface_var v;
466 v.id = code[word+2];
467 v.type_id = code[word+1];
468 out[location] = v;
469 }
470 else {
471 /* A builtin interface variable */
472 interface_var v;
473 v.id = code[word+2];
474 v.type_id = code[word+1];
475 builtins_out[builtin] = v;
476 }
477 }
478
479 word += oplen;
480 }
481}
482
483
Chris Forbes2778f302015-04-02 13:22:31 +1300484VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo,
485 VkShader *pShader)
486{
Chris Forbes7f963832015-05-29 14:55:18 +1200487 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbes2778f302015-04-02 13:22:31 +1300488 VkLayerDispatchTable* pTable = tableMap[(VkBaseLayerObject *)device];
489 VkResult res = pTable->CreateShader(device, pCreateInfo, pShader);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200490
491 shader_map[(VkBaseLayerObject *) *pShader] = new shader_source(pCreateInfo);
Chris Forbes7f963832015-05-29 14:55:18 +1200492 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes2778f302015-04-02 13:22:31 +1300493 return res;
494}
495
496
Chris Forbesee99b9b2015-05-25 11:13:22 +1200497static bool
Chris Forbes41002452015-04-08 10:19:16 +1200498validate_interface_between_stages(shader_source const *producer, char const *producer_name,
Chris Forbesf044ec92015-06-05 15:01:08 +1200499 shader_source const *consumer, char const *consumer_name,
500 bool consumer_arrayed_input)
Chris Forbes41002452015-04-08 10:19:16 +1200501{
502 std::map<uint32_t, interface_var> outputs;
503 std::map<uint32_t, interface_var> inputs;
504
505 std::map<uint32_t, interface_var> builtin_outputs;
506 std::map<uint32_t, interface_var> builtin_inputs;
507
Chris Forbes6b2ead62015-04-17 10:13:28 +1200508 char str[1024];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200509 bool pass = true;
Chris Forbes41002452015-04-08 10:19:16 +1200510
Cody Northrop97e52d82015-04-20 14:09:40 -0600511 collect_interface_by_location(producer, spv::StorageClassOutput, outputs, builtin_outputs);
512 collect_interface_by_location(consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes41002452015-04-08 10:19:16 +1200513
514 auto a_it = outputs.begin();
515 auto b_it = inputs.begin();
516
517 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedod8f83d82015-04-27 16:36:17 -0600518 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
519 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
520 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200521 auto a_first = a_at_end ? 0 : a_it->first;
522 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600523
524 if (b_at_end || a_first < b_first) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200525 sprintf(str, "%s writes to output location %d which is not consumed by %s\n",
David Pinedod8f83d82015-04-27 16:36:17 -0600526 producer_name, a_first, consumer_name);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200527 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes41002452015-04-08 10:19:16 +1200528 a_it++;
529 }
David Pinedod8f83d82015-04-27 16:36:17 -0600530 else if (a_at_end || a_first > b_first) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200531 sprintf(str, "%s consumes input location %d which is not written by %s\n",
David Pinedod8f83d82015-04-27 16:36:17 -0600532 consumer_name, b_first, producer_name);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200533 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200534 pass = false;
Chris Forbes41002452015-04-08 10:19:16 +1200535 b_it++;
536 }
537 else {
Chris Forbesf044ec92015-06-05 15:01:08 +1200538 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 +1200539 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200540 }
541 else {
542 char producer_type[1024];
543 char consumer_type[1024];
544 describe_type(producer_type, producer, a_it->second.type_id);
545 describe_type(consumer_type, consumer, b_it->second.type_id);
546
Chris Forbes6b2ead62015-04-17 10:13:28 +1200547 sprintf(str, "Type mismatch on location %d: '%s' vs '%s'\n", a_it->first,
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200548 producer_type, consumer_type);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200549 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200550 pass = false;
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200551 }
Chris Forbes41002452015-04-08 10:19:16 +1200552 a_it++;
553 b_it++;
554 }
555 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200556
557 return pass;
Chris Forbes41002452015-04-08 10:19:16 +1200558}
559
560
Chris Forbes3616b462015-04-08 10:37:20 +1200561enum FORMAT_TYPE {
562 FORMAT_TYPE_UNDEFINED,
563 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
564 FORMAT_TYPE_SINT,
565 FORMAT_TYPE_UINT,
566};
567
568
569static unsigned
570get_format_type(VkFormat fmt) {
571 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800572 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200573 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800574 case VK_FORMAT_R8_SINT:
575 case VK_FORMAT_R8G8_SINT:
576 case VK_FORMAT_R8G8B8_SINT:
577 case VK_FORMAT_R8G8B8A8_SINT:
578 case VK_FORMAT_R16_SINT:
579 case VK_FORMAT_R16G16_SINT:
580 case VK_FORMAT_R16G16B16_SINT:
581 case VK_FORMAT_R16G16B16A16_SINT:
582 case VK_FORMAT_R32_SINT:
583 case VK_FORMAT_R32G32_SINT:
584 case VK_FORMAT_R32G32B32_SINT:
585 case VK_FORMAT_R32G32B32A32_SINT:
586 case VK_FORMAT_B8G8R8_SINT:
587 case VK_FORMAT_B8G8R8A8_SINT:
588 case VK_FORMAT_R10G10B10A2_SINT:
589 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200590 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800591 case VK_FORMAT_R8_UINT:
592 case VK_FORMAT_R8G8_UINT:
593 case VK_FORMAT_R8G8B8_UINT:
594 case VK_FORMAT_R8G8B8A8_UINT:
595 case VK_FORMAT_R16_UINT:
596 case VK_FORMAT_R16G16_UINT:
597 case VK_FORMAT_R16G16B16_UINT:
598 case VK_FORMAT_R16G16B16A16_UINT:
599 case VK_FORMAT_R32_UINT:
600 case VK_FORMAT_R32G32_UINT:
601 case VK_FORMAT_R32G32B32_UINT:
602 case VK_FORMAT_R32G32B32A32_UINT:
603 case VK_FORMAT_B8G8R8_UINT:
604 case VK_FORMAT_B8G8R8A8_UINT:
605 case VK_FORMAT_R10G10B10A2_UINT:
606 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200607 return FORMAT_TYPE_UINT;
608 default:
609 return FORMAT_TYPE_FLOAT;
610 }
611}
612
613
Chris Forbes156a1162015-05-04 14:04:06 +1200614/* characterizes a SPIR-V type appearing in an interface to a FF stage,
615 * for comparison to a VkFormat's characterization above. */
616static unsigned
617get_fundamental_type(shader_source const *src, unsigned type)
618{
619 auto type_def_it = src->type_def_index.find(type);
620
621 if (type_def_it == src->type_def_index.end()) {
622 return FORMAT_TYPE_UNDEFINED;
623 }
624
625 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
626 unsigned opcode = code[0] & 0x0ffffu;
627 switch (opcode) {
628 case spv::OpTypeInt:
629 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
630 case spv::OpTypeFloat:
631 return FORMAT_TYPE_FLOAT;
632 case spv::OpTypeVector:
633 return get_fundamental_type(src, code[2]);
634 case spv::OpTypeMatrix:
635 return get_fundamental_type(src, code[2]);
636 case spv::OpTypeArray:
637 return get_fundamental_type(src, code[2]);
638 case spv::OpTypePointer:
639 return get_fundamental_type(src, code[3]);
640 default:
641 return FORMAT_TYPE_UNDEFINED;
642 }
643}
644
645
Chris Forbesee99b9b2015-05-25 11:13:22 +1200646static bool
Chris Forbes280ba2c2015-06-12 11:16:41 +1200647validate_vi_consistency(VkPipelineVertexInputCreateInfo const *vi)
648{
649 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
650 * each binding should be specified only once.
651 */
652 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
653 char str[1024];
654 bool pass = true;
655
656 for (unsigned i = 0; i < vi->bindingCount; i++) {
657 auto desc = &vi->pVertexBindingDescriptions[i];
658 auto & binding = bindings[desc->binding];
659 if (binding) {
660 sprintf(str, "Duplicate vertex input binding descriptions for binding %d", desc->binding);
661 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INCONSISTENT_VI, "SC", str);
662 pass = false;
663 }
664 else {
665 binding = desc;
666 }
667 }
668
669 return pass;
670}
671
672
673static bool
Chris Forbes772d03b2015-04-08 10:36:37 +1200674validate_vi_against_vs_inputs(VkPipelineVertexInputCreateInfo const *vi, shader_source const *vs)
675{
676 std::map<uint32_t, interface_var> inputs;
677 /* we collect builtin inputs, but they will never appear in the VI state --
678 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
679 */
680 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200681 char str[1024];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200682 bool pass = true;
Chris Forbes772d03b2015-04-08 10:36:37 +1200683
Cody Northrop97e52d82015-04-20 14:09:40 -0600684 collect_interface_by_location(vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbes772d03b2015-04-08 10:36:37 +1200685
686 /* Build index by location */
687 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes7191cd52015-05-25 11:13:24 +1200688 if (vi) {
689 for (unsigned i = 0; i < vi->attributeCount; i++)
690 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
691 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200692
693 auto it_a = attribs.begin();
694 auto it_b = inputs.begin();
695
David Pinedod8f83d82015-04-27 16:36:17 -0600696 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
697 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
698 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes62cc3fc2015-06-10 08:37:27 +1200699 auto a_first = a_at_end ? 0 : it_a->first;
700 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedod8f83d82015-04-27 16:36:17 -0600701 if (b_at_end || a_first < b_first) {
702 sprintf(str, "Vertex attribute at location %d not consumed by VS", a_first);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200703 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes772d03b2015-04-08 10:36:37 +1200704 it_a++;
705 }
David Pinedod8f83d82015-04-27 16:36:17 -0600706 else if (a_at_end || b_first < a_first) {
707 sprintf(str, "VS consumes input at location %d but not provided", b_first);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200708 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200709 pass = false;
Chris Forbes772d03b2015-04-08 10:36:37 +1200710 it_b++;
711 }
712 else {
Chris Forbes401784b2015-05-04 14:04:24 +1200713 unsigned attrib_type = get_format_type(it_a->second->format);
714 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
715
716 /* type checking */
717 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
718 char vs_type[1024];
719 describe_type(vs_type, vs, it_b->second.type_id);
720 sprintf(str, "Attribute type of `%s` at location %d does not match VS input type of `%s`",
721 string_VkFormat(it_a->second->format), a_first, vs_type);
722 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200723 pass = false;
Chris Forbes401784b2015-05-04 14:04:24 +1200724 }
725
Chris Forbes6b2ead62015-04-17 10:13:28 +1200726 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200727 it_a++;
728 it_b++;
729 }
730 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200731
732 return pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200733}
734
735
Chris Forbesee99b9b2015-05-25 11:13:22 +1200736static bool
Chris Forbes3616b462015-04-08 10:37:20 +1200737validate_fs_outputs_against_cb(shader_source const *fs, VkPipelineCbStateCreateInfo const *cb)
738{
739 std::map<uint32_t, interface_var> outputs;
740 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200741 char str[1024];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200742 bool pass = true;
Chris Forbes3616b462015-04-08 10:37:20 +1200743
744 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
745
Cody Northrop97e52d82015-04-20 14:09:40 -0600746 collect_interface_by_location(fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes3616b462015-04-08 10:37:20 +1200747
748 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
749 * and all color attachment should be UNORM/SNORM/FLOAT.
750 */
751 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes3616b462015-04-08 10:37:20 +1200752 if (outputs.size()) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200753 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC",
754 "Should not have user-defined FS outputs when using broadcast");
Chris Forbesee99b9b2015-05-25 11:13:22 +1200755 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200756 }
757
Ian Elliott1cb62222015-04-17 11:05:04 -0600758 for (unsigned i = 0; i < cb->attachmentCount; i++) {
Chris Forbes3616b462015-04-08 10:37:20 +1200759 unsigned attachmentType = get_format_type(cb->pAttachments[i].format);
760 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200761 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
762 "CB format should not be SINT or UINT when using broadcast");
Chris Forbesee99b9b2015-05-25 11:13:22 +1200763 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200764 }
765 }
766
Chris Forbesee99b9b2015-05-25 11:13:22 +1200767 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200768 }
769
770 auto it = outputs.begin();
771 uint32_t attachment = 0;
772
773 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
774 * are currently dense, but the parallel with matching between shader stages is nice.
775 */
776
Chris Forbesbf2b1d22015-05-05 11:34:14 +1200777 while ((outputs.size() > 0 && it != outputs.end()) || attachment < cb->attachmentCount) {
scygan3a22ce92015-06-01 19:48:11 +0200778 if (attachment == cb->attachmentCount || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200779 sprintf(str, "FS writes to output location %d with no matching attachment", it->first);
780 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes3616b462015-04-08 10:37:20 +1200781 it++;
782 }
783 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200784 sprintf(str, "Attachment %d not written by FS", attachment);
785 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbes3616b462015-04-08 10:37:20 +1200786 attachment++;
Chris Forbesee99b9b2015-05-25 11:13:22 +1200787 pass = false;
Chris Forbes3616b462015-04-08 10:37:20 +1200788 }
789 else {
Chris Forbes46d31e52015-05-04 14:20:10 +1200790 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
791 unsigned att_type = get_format_type(cb->pAttachments[attachment].format);
792
793 /* type checking */
794 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
795 char fs_type[1024];
796 describe_type(fs_type, fs, it->second.type_id);
797 sprintf(str, "Attachment %d of type `%s` does not match FS output type of `%s`",
798 attachment, string_VkFormat(cb->pAttachments[attachment].format), fs_type);
799 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200800 pass = false;
Chris Forbes46d31e52015-05-04 14:20:10 +1200801 }
802
Chris Forbes6b2ead62015-04-17 10:13:28 +1200803 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200804 it++;
805 attachment++;
806 }
807 }
Chris Forbesee99b9b2015-05-25 11:13:22 +1200808
809 return pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200810}
811
812
Chris Forbesf044ec92015-06-05 15:01:08 +1200813struct shader_stage_attributes {
814 char const * const name;
815 bool arrayed_input;
816};
817
818
819static shader_stage_attributes
820shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
821 { "vertex shader", false },
822 { "tessellation control shader", true },
823 { "tessellation evaluation shader", false },
824 { "geometry shader", true },
825 { "fragment shader", false },
826};
827
828
Chris Forbes81874ba2015-06-04 20:23:00 +1200829static bool
830validate_graphics_pipeline(VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes4175e6f2015-04-08 10:15:35 +1200831{
Chris Forbesf6800b52015-04-08 10:16:45 +1200832 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
833 * before trying to do anything more: */
834
Chris Forbesf044ec92015-06-05 15:01:08 +1200835 shader_source const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
836 memset(shaders, 0, sizeof(shaders));
Chris Forbesf6800b52015-04-08 10:16:45 +1200837 VkPipelineCbStateCreateInfo const *cb = 0;
838 VkPipelineVertexInputCreateInfo const *vi = 0;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200839 char str[1024];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200840 bool pass = true;
Chris Forbesf6800b52015-04-08 10:16:45 +1200841
Chris Forbes7f963832015-05-29 14:55:18 +1200842 loader_platform_thread_lock_mutex(&globalLock);
843
Chris Forbesf6800b52015-04-08 10:16:45 +1200844 for (auto stage = pCreateInfo; stage; stage = (decltype(stage))stage->pNext) {
845 if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
846 auto shader_stage = (VkPipelineShaderStageCreateInfo const *)stage;
847
Chris Forbesf044ec92015-06-05 15:01:08 +1200848 if (shader_stage->shader.stage < VK_SHADER_STAGE_VERTEX || shader_stage->shader.stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200849 sprintf(str, "Unknown shader stage %d\n", shader_stage->shader.stage);
850 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC", str);
851 }
Chris Forbesf044ec92015-06-05 15:01:08 +1200852 else {
853 shaders[shader_stage->shader.stage] = shader_map[(void *)(shader_stage->shader.shader)];
854 }
Chris Forbesf6800b52015-04-08 10:16:45 +1200855 }
856 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO) {
857 cb = (VkPipelineCbStateCreateInfo const *)stage;
858 }
859 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO) {
860 vi = (VkPipelineVertexInputCreateInfo const *)stage;
861 }
862 }
863
Chris Forbes280ba2c2015-06-12 11:16:41 +1200864 if (vi) {
865 pass = validate_vi_consistency(vi) && pass;
866 }
867
Chris Forbesf044ec92015-06-05 15:01:08 +1200868 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
869 pass = validate_vi_against_vs_inputs(vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbes772d03b2015-04-08 10:36:37 +1200870 }
871
Chris Forbesf044ec92015-06-05 15:01:08 +1200872 /* TODO: enforce rules about present combinations of shaders */
873 int producer = VK_SHADER_STAGE_VERTEX;
874 int consumer = VK_SHADER_STAGE_GEOMETRY;
875
876 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
877 producer++;
878 consumer++;
Chris Forbes41002452015-04-08 10:19:16 +1200879 }
880
Tony Barbour0102a902015-06-11 15:04:25 -0600881 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbesf044ec92015-06-05 15:01:08 +1200882 assert(shaders[producer]);
883 if (shaders[consumer]) {
884 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
885 pass = validate_interface_between_stages(shaders[producer], shader_stage_attribs[producer].name,
886 shaders[consumer], shader_stage_attribs[consumer].name,
887 shader_stage_attribs[consumer].arrayed_input) && pass;
888 }
889
890 producer = consumer;
891 }
892 }
893
894 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && cb) {
895 pass = validate_fs_outputs_against_cb(shaders[VK_SHADER_STAGE_FRAGMENT], cb) && pass;
Chris Forbes3616b462015-04-08 10:37:20 +1200896 }
897
Chris Forbes7f963832015-05-29 14:55:18 +1200898 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbes81874ba2015-06-04 20:23:00 +1200899 return pass;
900}
901
902
Chris Forbes39d8d752015-06-04 20:27:09 +1200903VK_LAYER_EXPORT VkResult VKAPI
904vkCreateGraphicsPipeline(VkDevice device,
905 const VkGraphicsPipelineCreateInfo *pCreateInfo,
906 VkPipeline *pPipeline)
Chris Forbes81874ba2015-06-04 20:23:00 +1200907{
908 bool pass = validate_graphics_pipeline(pCreateInfo);
Chris Forbesee99b9b2015-05-25 11:13:22 +1200909
910 if (pass) {
911 /* The driver is allowed to crash if passed junk. Only actually create the
912 * pipeline if we didn't run into any showstoppers above.
913 */
Chris Forbes81874ba2015-06-04 20:23:00 +1200914 VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device];
Chris Forbesee99b9b2015-05-25 11:13:22 +1200915 return pTable->CreateGraphicsPipeline(device, pCreateInfo, pPipeline);
916 }
917 else {
918 return VK_ERROR_UNKNOWN;
919 }
Chris Forbes4175e6f2015-04-08 10:15:35 +1200920}
921
922
Chris Forbes39d8d752015-06-04 20:27:09 +1200923VK_LAYER_EXPORT VkResult VKAPI
924vkCreateGraphicsPipelineDerivative(VkDevice device,
925 const VkGraphicsPipelineCreateInfo *pCreateInfo,
926 VkPipeline basePipeline,
927 VkPipeline *pPipeline)
928{
929 bool pass = validate_graphics_pipeline(pCreateInfo);
930
931 if (pass) {
932 /* The driver is allowed to crash if passed junk. Only actually create the
933 * pipeline if we didn't run into any showstoppers above.
934 */
935 VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device];
936 return pTable->CreateGraphicsPipelineDerivative(device, pCreateInfo, basePipeline, pPipeline);
937 }
938 else {
939 return VK_ERROR_UNKNOWN;
940 }
941}
942
943
Chris Forbesdb467bd2015-05-25 11:12:59 +1200944VK_LAYER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(
945 VkInstance instance,
946 VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback,
947 void *pUserData)
948{
949 // This layer intercepts callbacks
950 VK_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (VK_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(VK_LAYER_DBG_FUNCTION_NODE));
951 if (!pNewDbgFuncNode)
952 return VK_ERROR_OUT_OF_HOST_MEMORY;
953 pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;
954 pNewDbgFuncNode->pUserData = pUserData;
955 pNewDbgFuncNode->pNext = g_pDbgFunctionHead;
956 g_pDbgFunctionHead = pNewDbgFuncNode;
957 // force callbacks if DebugAction hasn't been set already other than initial value
958 if (g_actionIsDefault) {
959 g_debugAction = VK_DBG_LAYER_ACTION_CALLBACK;
960 }
Chris Forbes7f963832015-05-29 14:55:18 +1200961 // NOT CORRECT WITH MULTIPLE DEVICES OR INSTANCES, BUT THIS IS ALL GOING AWAY SOON ANYWAY
962 VkLayerDispatchTable *pTable = tableMap[pCurObj];
963 VkResult result = pTable->DbgRegisterMsgCallback(instance, pfnMsgCallback, pUserData);
Chris Forbesdb467bd2015-05-25 11:12:59 +1200964 return result;
965}
966
967VK_LAYER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(
968 VkInstance instance,
969 VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
970{
971 VK_LAYER_DBG_FUNCTION_NODE *pInfo = g_pDbgFunctionHead;
972 VK_LAYER_DBG_FUNCTION_NODE *pPrev = pInfo;
973 while (pInfo) {
974 if (pInfo->pfnMsgCallback == pfnMsgCallback) {
975 pPrev->pNext = pInfo->pNext;
976 if (g_pDbgFunctionHead == pInfo) {
977 g_pDbgFunctionHead = pInfo->pNext;
978 }
979 free(pInfo);
980 break;
981 }
982 pPrev = pInfo;
983 pInfo = pInfo->pNext;
984 }
985 if (g_pDbgFunctionHead == NULL) {
986 if (g_actionIsDefault) {
987 g_debugAction = VK_DBG_LAYER_ACTION_LOG_MSG;
988 } else {
989 g_debugAction = (VK_LAYER_DBG_ACTION)(g_debugAction & ~((uint32_t)VK_DBG_LAYER_ACTION_CALLBACK));
990 }
991 }
Chris Forbes7f963832015-05-29 14:55:18 +1200992 // NOT CORRECT WITH MULTIPLE DEVICES OR INSTANCES, BUT THIS IS ALL GOING AWAY SOON ANYWAY
993 VkLayerDispatchTable *pTable = tableMap[pCurObj];
994 VkResult result = pTable->DbgUnregisterMsgCallback(instance, pfnMsgCallback);
Chris Forbesdb467bd2015-05-25 11:12:59 +1200995 return result;
996}
997
998
Chia-I Wua3b9a202015-04-17 02:00:54 +0800999VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char* pName)
Chris Forbes2778f302015-04-02 13:22:31 +13001000{
1001 if (gpu == NULL)
1002 return NULL;
1003
1004 initLayerTable((const VkBaseLayerObject *) gpu);
1005
Chris Forbesb6b8c462015-04-15 06:59:41 +12001006 loader_platform_thread_once(&g_initOnce, initLayer);
1007
Chris Forbes2778f302015-04-02 13:22:31 +13001008#define ADD_HOOK(fn) \
1009 if (!strncmp(#fn, pName, sizeof(#fn))) \
1010 return (void *) fn
1011
1012 ADD_HOOK(vkGetProcAddr);
1013 ADD_HOOK(vkEnumerateLayers);
1014 ADD_HOOK(vkCreateDevice);
1015 ADD_HOOK(vkCreateShader);
Chris Forbes4175e6f2015-04-08 10:15:35 +12001016 ADD_HOOK(vkCreateGraphicsPipeline);
Chris Forbes39d8d752015-06-04 20:27:09 +12001017 ADD_HOOK(vkCreateGraphicsPipelineDerivative);
Chris Forbesdb467bd2015-05-25 11:12:59 +12001018 ADD_HOOK(vkDbgRegisterMsgCallback);
1019 ADD_HOOK(vkDbgUnregisterMsgCallback);
Chris Forbes2778f302015-04-02 13:22:31 +13001020
1021 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
1022 if (gpuw->pGPA == NULL)
1023 return NULL;
Chia-I Wua3b9a202015-04-17 02:00:54 +08001024 return gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, pName);
Chris Forbes2778f302015-04-02 13:22:31 +13001025}