blob: fef4512f2efba90b6cd0d9d7161ddb88015c5605 [file] [log] [blame]
Chris Forbesaab9d112015-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 Forbes67cc36f2015-04-13 12:14:52 +120027#include <map>
Chris Forbesaab9d112015-04-02 13:22:31 +130028#include <unordered_map>
Chris Forbesbb164b62015-04-08 10:19:16 +120029#include <map>
Chris Forbes4396ff52015-04-08 10:11:59 +120030#include <vector>
Chris Forbesaab9d112015-04-02 13:22:31 +130031#include "loader_platform.h"
32#include "vk_dispatch_table_helper.h"
33#include "vkLayer.h"
Chris Forbes1b466bd2015-04-15 06:59:41 +120034#include "layers_config.h"
35#include "layers_msg.h"
Chris Forbes3317b382015-05-04 14:04:24 +120036#include "vk_enum_string_helper.h"
Chris Forbes5c75afe2015-04-17 10:13:28 +120037#include "shader_checker.h"
Chris Forbesaab9d112015-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 Forbes32e3b462015-05-09 10:31:21 +120042#include "spirv/spirv.h"
Chris Forbesaab9d112015-04-02 13:22:31 +130043
Chris Forbesaab9d112015-04-02 13:22:31 +130044
Chris Forbes1b466bd2015-04-15 06:59:41 +120045static std::unordered_map<void *, VkLayerDispatchTable *> tableMap;
Chris Forbes1ed0f982015-05-29 14:55:18 +120046static VkBaseLayerObject *pCurObj;
Chris Forbes1b466bd2015-04-15 06:59:41 +120047static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes1ed0f982015-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 Forbes4396ff52015-04-08 10:11:59 +120051
Chris Forbes1bb5a2e2015-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 Forbes4396ff52015-04-08 10:11:59 +120096struct shader_source {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +120097 /* the spirv image itself */
Chris Forbes4396ff52015-04-08 10:11:59 +120098 std::vector<uint32_t> words;
Chris Forbes1bb5a2e2015-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 Forbes4453c772015-06-05 15:01:08 +1200103 bool is_spirv;
Chris Forbes4396ff52015-04-08 10:11:59 +1200104
105 shader_source(VkShaderCreateInfo const *pCreateInfo) :
Chris Forbes4453c772015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200117
118 build_type_def_index(words, type_def_index);
Chris Forbes4396ff52015-04-08 10:11:59 +1200119 }
120};
121
122
123static std::unordered_map<void *, shader_source *> shader_map;
124
125
Chris Forbes1b466bd2015-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 Forbesaab9d112015-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 Wu6097f3a2015-04-17 02:00:54 +0800162 layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalDevice) gpuw->nextObject);
Chris Forbes1ed0f982015-05-29 14:55:18 +1200163 pCurObj = (VkBaseLayerObject *)gpuw->baseObject;
Chris Forbesaab9d112015-04-02 13:22:31 +1300164
165 return pTable;
166}
167
168
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800169VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
Chris Forbesaab9d112015-04-02 13:22:31 +1300170{
171 VkLayerDispatchTable* pTable = tableMap[gpu];
172 VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice);
Chris Forbes1b466bd2015-04-15 06:59:41 +1200173
174 loader_platform_thread_once(&g_initOnce, initLayer);
Chris Forbesaab9d112015-04-02 13:22:31 +1300175 // create a mapping for the device object into the dispatch table
176 tableMap.emplace(*pDevice, pTable);
Chris Forbes1ed0f982015-05-29 14:55:18 +1200177 pCurObj = (VkBaseLayerObject *) *pDevice;
Chris Forbesaab9d112015-04-02 13:22:31 +1300178 return result;
179}
180
181
Courtney Goeltzenleuchterbb1f3602015-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 Forbesaab9d112015-04-02 13:22:31 +1300183{
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -0600184 if (pLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pOutLayers[1] == NULL || pReserved == NULL)
Chris Forbesaab9d112015-04-02 13:22:31 +1300185 return VK_ERROR_INVALID_POINTER;
186
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -0600187 if (*pLayerCount < 1)
Chris Forbesaab9d112015-04-02 13:22:31 +1300188 return VK_ERROR_INITIALIZATION_FAILED;
Courtney Goeltzenleuchterbb1f3602015-04-20 11:04:54 -0600189 *pLayerCount = 1;
Chris Forbesaab9d112015-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 Ehlis432a9ba2015-04-17 08:55:13 -0600199#define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 2
Chris Forbesaab9d112015-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 Ehlis432a9ba2015-04-17 08:55:13 -0600203 0x10, "Validation",
Chris Forbesaab9d112015-04-02 13:22:31 +1300204};
205
Chris Forbesaab9d112015-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 Forbesaab9d112015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200247static char const *
248storage_class_name(unsigned sc)
249{
250 switch (sc) {
Cody Northrop812b4612015-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 Forbes1bb5a2e2015-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 Elliottf21f14b2015-04-17 11:05:04 -0600302 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes1bb5a2e2015-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 Forbes0a94a372015-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 Forbes1bb5a2e2015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200321 return false;
322 }
323
324 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes1bb5a2e2015-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 Forbes0a94a372015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200340 if (a_opcode != b_opcode) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200341 return false;
342 }
343
344 switch (a_opcode) {
Chris Forbes0a94a372015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200346 case spv::OpTypeBool:
Chris Forbes0a94a372015-06-05 14:57:05 +1200347 return true && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200348 case spv::OpTypeInt:
349 /* match on width, signedness */
Chris Forbes0a94a372015-06-05 14:57:05 +1200350 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200351 case spv::OpTypeFloat:
352 /* match on width */
Chris Forbes0a94a372015-06-05 14:57:05 +1200353 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200354 case spv::OpTypeVector:
355 case spv::OpTypeMatrix:
356 case spv::OpTypeArray:
Chris Forbes0a94a372015-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 Forbes1bb5a2e2015-04-10 11:41:20 +1200360 case spv::OpTypeStruct:
361 /* match on all element types */
362 {
Chris Forbes0a94a372015-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 Forbes1bb5a2e2015-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 Elliottf21f14b2015-04-17 11:05:04 -0600375 for (unsigned i = 2; i < a_len; i++) {
Chris Forbes0a94a372015-06-05 14:57:05 +1200376 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes1bb5a2e2015-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 Forbes0a94a372015-06-05 14:57:05 +1200385 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes1bb5a2e2015-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 Forbes67cc36f2015-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 Elliottf21f14b2015-04-17 11:05:04 -0600416collect_interface_by_location(shader_source const *src, spv::StorageClass sinterface,
Chris Forbes67cc36f2015-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 Forbes67cc36f2015-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 Northrop812b4612015-04-20 14:09:40 -0600436 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200437 var_locations[code[word+1]] = code[word+3];
438 }
439
Cody Northrop812b4612015-04-20 14:09:40 -0600440 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes67cc36f2015-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 Elliottf21f14b2015-04-17 11:05:04 -0600449 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes67cc36f2015-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 Forbes5c75afe2015-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 Elliottf21f14b2015-04-17 11:05:04 -0600460 code[word+2], code[word+1], storage_class_name(sinterface));
Chris Forbes5c75afe2015-04-17 10:13:28 +1200461 layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", str);
Chris Forbes67cc36f2015-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 Forbesaab9d112015-04-02 13:22:31 +1300484VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo,
485 VkShader *pShader)
486{
Chris Forbes1ed0f982015-05-29 14:55:18 +1200487 loader_platform_thread_lock_mutex(&globalLock);
Chris Forbesaab9d112015-04-02 13:22:31 +1300488 VkLayerDispatchTable* pTable = tableMap[(VkBaseLayerObject *)device];
489 VkResult res = pTable->CreateShader(device, pCreateInfo, pShader);
Chris Forbes4396ff52015-04-08 10:11:59 +1200490
491 shader_map[(VkBaseLayerObject *) *pShader] = new shader_source(pCreateInfo);
Chris Forbes1ed0f982015-05-29 14:55:18 +1200492 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbesaab9d112015-04-02 13:22:31 +1300493 return res;
494}
495
496
Chris Forbes5f362d02015-05-25 11:13:22 +1200497static bool
Chris Forbesbb164b62015-04-08 10:19:16 +1200498validate_interface_between_stages(shader_source const *producer, char const *producer_name,
Chris Forbes4453c772015-06-05 15:01:08 +1200499 shader_source const *consumer, char const *consumer_name,
500 bool consumer_arrayed_input)
Chris Forbesbb164b62015-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 Forbes5c75afe2015-04-17 10:13:28 +1200508 char str[1024];
Chris Forbes5f362d02015-05-25 11:13:22 +1200509 bool pass = true;
Chris Forbesbb164b62015-04-08 10:19:16 +1200510
Cody Northrop812b4612015-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 Forbesbb164b62015-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 Pinedof5997ab2015-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 Forbes4cb97672015-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 Pinedof5997ab2015-04-27 16:36:17 -0600523
524 if (b_at_end || a_first < b_first) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200525 sprintf(str, "%s writes to output location %d which is not consumed by %s\n",
David Pinedof5997ab2015-04-27 16:36:17 -0600526 producer_name, a_first, consumer_name);
Chris Forbes5c75afe2015-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 Forbesbb164b62015-04-08 10:19:16 +1200528 a_it++;
529 }
David Pinedof5997ab2015-04-27 16:36:17 -0600530 else if (a_at_end || a_first > b_first) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200531 sprintf(str, "%s consumes input location %d which is not written by %s\n",
David Pinedof5997ab2015-04-27 16:36:17 -0600532 consumer_name, b_first, producer_name);
Chris Forbes5c75afe2015-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 Forbes5f362d02015-05-25 11:13:22 +1200534 pass = false;
Chris Forbesbb164b62015-04-08 10:19:16 +1200535 b_it++;
536 }
537 else {
Chris Forbes4453c772015-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 Forbes5c75afe2015-04-17 10:13:28 +1200539 /* OK! */
Chris Forbes1bb5a2e2015-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 Forbes5c75afe2015-04-17 10:13:28 +1200547 sprintf(str, "Type mismatch on location %d: '%s' vs '%s'\n", a_it->first,
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200548 producer_type, consumer_type);
Chris Forbes5c75afe2015-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 Forbes5f362d02015-05-25 11:13:22 +1200550 pass = false;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200551 }
Chris Forbesbb164b62015-04-08 10:19:16 +1200552 a_it++;
553 b_it++;
554 }
555 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200556
557 return pass;
Chris Forbesbb164b62015-04-08 10:19:16 +1200558}
559
560
Chris Forbes9b9f5fe2015-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 Wu6097f3a2015-04-17 02:00:54 +0800572 case VK_FORMAT_UNDEFINED:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200573 return FORMAT_TYPE_UNDEFINED;
Chia-I Wu6097f3a2015-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 Forbes9b9f5fe2015-04-08 10:37:20 +1200590 return FORMAT_TYPE_SINT;
Chia-I Wu6097f3a2015-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 Forbes9b9f5fe2015-04-08 10:37:20 +1200607 return FORMAT_TYPE_UINT;
608 default:
609 return FORMAT_TYPE_FLOAT;
610 }
611}
612
613
Chris Forbes28c50882015-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 Forbes5f362d02015-05-25 11:13:22 +1200646static bool
Chris Forbesfcd05f12015-04-08 10:36:37 +1200647validate_vi_against_vs_inputs(VkPipelineVertexInputCreateInfo const *vi, shader_source const *vs)
648{
649 std::map<uint32_t, interface_var> inputs;
650 /* we collect builtin inputs, but they will never appear in the VI state --
651 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
652 */
653 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbes5c75afe2015-04-17 10:13:28 +1200654 char str[1024];
Chris Forbes5f362d02015-05-25 11:13:22 +1200655 bool pass = true;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200656
Cody Northrop812b4612015-04-20 14:09:40 -0600657 collect_interface_by_location(vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200658
659 /* Build index by location */
660 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes6f2ab982015-05-25 11:13:24 +1200661 if (vi) {
662 for (unsigned i = 0; i < vi->attributeCount; i++)
663 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
664 }
Chris Forbesfcd05f12015-04-08 10:36:37 +1200665
666 auto it_a = attribs.begin();
667 auto it_b = inputs.begin();
668
David Pinedof5997ab2015-04-27 16:36:17 -0600669 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
670 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
671 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200672 auto a_first = a_at_end ? 0 : it_a->first;
673 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600674 if (b_at_end || a_first < b_first) {
675 sprintf(str, "Vertex attribute at location %d not consumed by VS", a_first);
Chris Forbes5c75afe2015-04-17 10:13:28 +1200676 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200677 it_a++;
678 }
David Pinedof5997ab2015-04-27 16:36:17 -0600679 else if (a_at_end || b_first < a_first) {
680 sprintf(str, "VS consumes input at location %d but not provided", b_first);
Chris Forbes5c75afe2015-04-17 10:13:28 +1200681 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbes5f362d02015-05-25 11:13:22 +1200682 pass = false;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200683 it_b++;
684 }
685 else {
Chris Forbes3317b382015-05-04 14:04:24 +1200686 unsigned attrib_type = get_format_type(it_a->second->format);
687 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
688
689 /* type checking */
690 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
691 char vs_type[1024];
692 describe_type(vs_type, vs, it_b->second.type_id);
693 sprintf(str, "Attribute type of `%s` at location %d does not match VS input type of `%s`",
694 string_VkFormat(it_a->second->format), a_first, vs_type);
695 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbes5f362d02015-05-25 11:13:22 +1200696 pass = false;
Chris Forbes3317b382015-05-04 14:04:24 +1200697 }
698
Chris Forbes5c75afe2015-04-17 10:13:28 +1200699 /* OK! */
Chris Forbesfcd05f12015-04-08 10:36:37 +1200700 it_a++;
701 it_b++;
702 }
703 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200704
705 return pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200706}
707
708
Chris Forbes5f362d02015-05-25 11:13:22 +1200709static bool
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200710validate_fs_outputs_against_cb(shader_source const *fs, VkPipelineCbStateCreateInfo const *cb)
711{
712 std::map<uint32_t, interface_var> outputs;
713 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbes5c75afe2015-04-17 10:13:28 +1200714 char str[1024];
Chris Forbes5f362d02015-05-25 11:13:22 +1200715 bool pass = true;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200716
717 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
718
Cody Northrop812b4612015-04-20 14:09:40 -0600719 collect_interface_by_location(fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200720
721 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
722 * and all color attachment should be UNORM/SNORM/FLOAT.
723 */
724 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200725 if (outputs.size()) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200726 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC",
727 "Should not have user-defined FS outputs when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200728 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200729 }
730
Ian Elliottf21f14b2015-04-17 11:05:04 -0600731 for (unsigned i = 0; i < cb->attachmentCount; i++) {
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200732 unsigned attachmentType = get_format_type(cb->pAttachments[i].format);
733 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200734 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
735 "CB format should not be SINT or UINT when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200736 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200737 }
738 }
739
Chris Forbes5f362d02015-05-25 11:13:22 +1200740 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200741 }
742
743 auto it = outputs.begin();
744 uint32_t attachment = 0;
745
746 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
747 * are currently dense, but the parallel with matching between shader stages is nice.
748 */
749
Chris Forbes8802c992015-05-05 11:34:14 +1200750 while ((outputs.size() > 0 && it != outputs.end()) || attachment < cb->attachmentCount) {
scygan7a62cbe2015-06-01 19:48:11 +0200751 if (attachment == cb->attachmentCount || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200752 sprintf(str, "FS writes to output location %d with no matching attachment", it->first);
753 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200754 it++;
755 }
756 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200757 sprintf(str, "Attachment %d not written by FS", attachment);
758 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200759 attachment++;
Chris Forbes5f362d02015-05-25 11:13:22 +1200760 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200761 }
762 else {
Chris Forbes4b009002015-05-04 14:20:10 +1200763 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
764 unsigned att_type = get_format_type(cb->pAttachments[attachment].format);
765
766 /* type checking */
767 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
768 char fs_type[1024];
769 describe_type(fs_type, fs, it->second.type_id);
770 sprintf(str, "Attachment %d of type `%s` does not match FS output type of `%s`",
771 attachment, string_VkFormat(cb->pAttachments[attachment].format), fs_type);
772 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbes5f362d02015-05-25 11:13:22 +1200773 pass = false;
Chris Forbes4b009002015-05-04 14:20:10 +1200774 }
775
Chris Forbes5c75afe2015-04-17 10:13:28 +1200776 /* OK! */
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200777 it++;
778 attachment++;
779 }
780 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200781
782 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200783}
784
785
Chris Forbes4453c772015-06-05 15:01:08 +1200786struct shader_stage_attributes {
787 char const * const name;
788 bool arrayed_input;
789};
790
791
792static shader_stage_attributes
793shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
794 { "vertex shader", false },
795 { "tessellation control shader", true },
796 { "tessellation evaluation shader", false },
797 { "geometry shader", true },
798 { "fragment shader", false },
799};
800
801
Chris Forbesf1060ca2015-06-04 20:23:00 +1200802static bool
803validate_graphics_pipeline(VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes60540932015-04-08 10:15:35 +1200804{
Chris Forbes8f600932015-04-08 10:16:45 +1200805 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
806 * before trying to do anything more: */
807
Chris Forbes4453c772015-06-05 15:01:08 +1200808 shader_source const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
809 memset(shaders, 0, sizeof(shaders));
Chris Forbes8f600932015-04-08 10:16:45 +1200810 VkPipelineCbStateCreateInfo const *cb = 0;
811 VkPipelineVertexInputCreateInfo const *vi = 0;
Chris Forbes5c75afe2015-04-17 10:13:28 +1200812 char str[1024];
Chris Forbes5f362d02015-05-25 11:13:22 +1200813 bool pass = true;
Chris Forbes8f600932015-04-08 10:16:45 +1200814
Chris Forbes1ed0f982015-05-29 14:55:18 +1200815 loader_platform_thread_lock_mutex(&globalLock);
816
Chris Forbes8f600932015-04-08 10:16:45 +1200817 for (auto stage = pCreateInfo; stage; stage = (decltype(stage))stage->pNext) {
818 if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
819 auto shader_stage = (VkPipelineShaderStageCreateInfo const *)stage;
820
Chris Forbes4453c772015-06-05 15:01:08 +1200821 if (shader_stage->shader.stage < VK_SHADER_STAGE_VERTEX || shader_stage->shader.stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200822 sprintf(str, "Unknown shader stage %d\n", shader_stage->shader.stage);
823 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC", str);
824 }
Chris Forbes4453c772015-06-05 15:01:08 +1200825 else {
826 shaders[shader_stage->shader.stage] = shader_map[(void *)(shader_stage->shader.shader)];
827 }
Chris Forbes8f600932015-04-08 10:16:45 +1200828 }
829 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO) {
830 cb = (VkPipelineCbStateCreateInfo const *)stage;
831 }
832 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO) {
833 vi = (VkPipelineVertexInputCreateInfo const *)stage;
834 }
835 }
836
Chris Forbes4453c772015-06-05 15:01:08 +1200837 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
838 pass = validate_vi_against_vs_inputs(vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200839 }
840
Chris Forbes4453c772015-06-05 15:01:08 +1200841 /* TODO: enforce rules about present combinations of shaders */
842 int producer = VK_SHADER_STAGE_VERTEX;
843 int consumer = VK_SHADER_STAGE_GEOMETRY;
844
845 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
846 producer++;
847 consumer++;
Chris Forbesbb164b62015-04-08 10:19:16 +1200848 }
849
Tony Barbour4eb3cd12015-06-11 15:04:25 -0600850 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbes4453c772015-06-05 15:01:08 +1200851 assert(shaders[producer]);
852 if (shaders[consumer]) {
853 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
854 pass = validate_interface_between_stages(shaders[producer], shader_stage_attribs[producer].name,
855 shaders[consumer], shader_stage_attribs[consumer].name,
856 shader_stage_attribs[consumer].arrayed_input) && pass;
857 }
858
859 producer = consumer;
860 }
861 }
862
863 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && cb) {
864 pass = validate_fs_outputs_against_cb(shaders[VK_SHADER_STAGE_FRAGMENT], cb) && pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200865 }
866
Chris Forbes1ed0f982015-05-29 14:55:18 +1200867 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbesf1060ca2015-06-04 20:23:00 +1200868 return pass;
869}
870
871
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200872VK_LAYER_EXPORT VkResult VKAPI
873vkCreateGraphicsPipeline(VkDevice device,
874 const VkGraphicsPipelineCreateInfo *pCreateInfo,
875 VkPipeline *pPipeline)
Chris Forbesf1060ca2015-06-04 20:23:00 +1200876{
877 bool pass = validate_graphics_pipeline(pCreateInfo);
Chris Forbes5f362d02015-05-25 11:13:22 +1200878
879 if (pass) {
880 /* The driver is allowed to crash if passed junk. Only actually create the
881 * pipeline if we didn't run into any showstoppers above.
882 */
Chris Forbesf1060ca2015-06-04 20:23:00 +1200883 VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device];
Chris Forbes5f362d02015-05-25 11:13:22 +1200884 return pTable->CreateGraphicsPipeline(device, pCreateInfo, pPipeline);
885 }
886 else {
887 return VK_ERROR_UNKNOWN;
888 }
Chris Forbes60540932015-04-08 10:15:35 +1200889}
890
891
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200892VK_LAYER_EXPORT VkResult VKAPI
893vkCreateGraphicsPipelineDerivative(VkDevice device,
894 const VkGraphicsPipelineCreateInfo *pCreateInfo,
895 VkPipeline basePipeline,
896 VkPipeline *pPipeline)
897{
898 bool pass = validate_graphics_pipeline(pCreateInfo);
899
900 if (pass) {
901 /* The driver is allowed to crash if passed junk. Only actually create the
902 * pipeline if we didn't run into any showstoppers above.
903 */
904 VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device];
905 return pTable->CreateGraphicsPipelineDerivative(device, pCreateInfo, basePipeline, pPipeline);
906 }
907 else {
908 return VK_ERROR_UNKNOWN;
909 }
910}
911
912
Chris Forbesb65ba352015-05-25 11:12:59 +1200913VK_LAYER_EXPORT VkResult VKAPI vkDbgRegisterMsgCallback(
914 VkInstance instance,
915 VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback,
916 void *pUserData)
917{
918 // This layer intercepts callbacks
919 VK_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (VK_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(VK_LAYER_DBG_FUNCTION_NODE));
920 if (!pNewDbgFuncNode)
921 return VK_ERROR_OUT_OF_HOST_MEMORY;
922 pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;
923 pNewDbgFuncNode->pUserData = pUserData;
924 pNewDbgFuncNode->pNext = g_pDbgFunctionHead;
925 g_pDbgFunctionHead = pNewDbgFuncNode;
926 // force callbacks if DebugAction hasn't been set already other than initial value
927 if (g_actionIsDefault) {
928 g_debugAction = VK_DBG_LAYER_ACTION_CALLBACK;
929 }
Chris Forbes1ed0f982015-05-29 14:55:18 +1200930 // NOT CORRECT WITH MULTIPLE DEVICES OR INSTANCES, BUT THIS IS ALL GOING AWAY SOON ANYWAY
931 VkLayerDispatchTable *pTable = tableMap[pCurObj];
932 VkResult result = pTable->DbgRegisterMsgCallback(instance, pfnMsgCallback, pUserData);
Chris Forbesb65ba352015-05-25 11:12:59 +1200933 return result;
934}
935
936VK_LAYER_EXPORT VkResult VKAPI vkDbgUnregisterMsgCallback(
937 VkInstance instance,
938 VK_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)
939{
940 VK_LAYER_DBG_FUNCTION_NODE *pInfo = g_pDbgFunctionHead;
941 VK_LAYER_DBG_FUNCTION_NODE *pPrev = pInfo;
942 while (pInfo) {
943 if (pInfo->pfnMsgCallback == pfnMsgCallback) {
944 pPrev->pNext = pInfo->pNext;
945 if (g_pDbgFunctionHead == pInfo) {
946 g_pDbgFunctionHead = pInfo->pNext;
947 }
948 free(pInfo);
949 break;
950 }
951 pPrev = pInfo;
952 pInfo = pInfo->pNext;
953 }
954 if (g_pDbgFunctionHead == NULL) {
955 if (g_actionIsDefault) {
956 g_debugAction = VK_DBG_LAYER_ACTION_LOG_MSG;
957 } else {
958 g_debugAction = (VK_LAYER_DBG_ACTION)(g_debugAction & ~((uint32_t)VK_DBG_LAYER_ACTION_CALLBACK));
959 }
960 }
Chris Forbes1ed0f982015-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->DbgUnregisterMsgCallback(instance, pfnMsgCallback);
Chris Forbesb65ba352015-05-25 11:12:59 +1200964 return result;
965}
966
967
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800968VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char* pName)
Chris Forbesaab9d112015-04-02 13:22:31 +1300969{
970 if (gpu == NULL)
971 return NULL;
972
973 initLayerTable((const VkBaseLayerObject *) gpu);
974
Chris Forbes1b466bd2015-04-15 06:59:41 +1200975 loader_platform_thread_once(&g_initOnce, initLayer);
976
Chris Forbesaab9d112015-04-02 13:22:31 +1300977#define ADD_HOOK(fn) \
978 if (!strncmp(#fn, pName, sizeof(#fn))) \
979 return (void *) fn
980
981 ADD_HOOK(vkGetProcAddr);
982 ADD_HOOK(vkEnumerateLayers);
983 ADD_HOOK(vkCreateDevice);
984 ADD_HOOK(vkCreateShader);
Chris Forbes60540932015-04-08 10:15:35 +1200985 ADD_HOOK(vkCreateGraphicsPipeline);
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200986 ADD_HOOK(vkCreateGraphicsPipelineDerivative);
Chris Forbesb65ba352015-05-25 11:12:59 +1200987 ADD_HOOK(vkDbgRegisterMsgCallback);
988 ADD_HOOK(vkDbgUnregisterMsgCallback);
Chris Forbesaab9d112015-04-02 13:22:31 +1300989
990 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
991 if (gpuw->pGPA == NULL)
992 return NULL;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800993 return gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, pName);
Chris Forbesaab9d112015-04-02 13:22:31 +1300994}