blob: 659251a477d78ee957be928ddd520510d4d81bd6 [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 Forbes6b2ead62015-04-17 10:13:28 +120036#include "shader_checker.h"
Chris Forbes2778f302015-04-02 13:22:31 +130037// The following is #included again to catch certain OS-specific functions
38// being used:
39#include "loader_platform.h"
40
41#include "SPIRV/spirv.h"
42
Chris Forbes2778f302015-04-02 13:22:31 +130043
Chris Forbesb6b8c462015-04-15 06:59:41 +120044static std::unordered_map<void *, VkLayerDispatchTable *> tableMap;
45static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes3b1c4212015-04-08 10:11:59 +120046
Chris Forbes3a5e99a2015-04-10 11:41:20 +120047
48static void
49build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
50{
51 unsigned int const *code = (unsigned int const *)&words[0];
52 size_t size = words.size();
53
54 unsigned word = 5;
55 while (word < size) {
56 unsigned opcode = code[word] & 0x0ffffu;
57 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
58
59 switch (opcode) {
60 case spv::OpTypeVoid:
61 case spv::OpTypeBool:
62 case spv::OpTypeInt:
63 case spv::OpTypeFloat:
64 case spv::OpTypeVector:
65 case spv::OpTypeMatrix:
66 case spv::OpTypeSampler:
67 case spv::OpTypeFilter:
68 case spv::OpTypeArray:
69 case spv::OpTypeRuntimeArray:
70 case spv::OpTypeStruct:
71 case spv::OpTypeOpaque:
72 case spv::OpTypePointer:
73 case spv::OpTypeFunction:
74 case spv::OpTypeEvent:
75 case spv::OpTypeDeviceEvent:
76 case spv::OpTypeReserveId:
77 case spv::OpTypeQueue:
78 case spv::OpTypePipe:
79 type_def_index[code[word+1]] = word;
80 break;
81
82 default:
83 /* We only care about type definitions */
84 break;
85 }
86
87 word += oplen;
88 }
89}
90
Chris Forbes3b1c4212015-04-08 10:11:59 +120091struct shader_source {
Chris Forbes3a5e99a2015-04-10 11:41:20 +120092 /* the spirv image itself */
Chris Forbes3b1c4212015-04-08 10:11:59 +120093 std::vector<uint32_t> words;
Chris Forbes3a5e99a2015-04-10 11:41:20 +120094 /* a mapping of <id> to the first word of its def. this is useful because walking type
95 * trees requires jumping all over the instruction stream.
96 */
97 std::unordered_map<unsigned, unsigned> type_def_index;
Chris Forbes3b1c4212015-04-08 10:11:59 +120098
99 shader_source(VkShaderCreateInfo const *pCreateInfo) :
100 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)) {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200101
102 build_type_def_index(words, type_def_index);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200103 }
104};
105
106
107static std::unordered_map<void *, shader_source *> shader_map;
108
109
Chris Forbesb6b8c462015-04-15 06:59:41 +1200110static void
111initLayer()
112{
113 const char *strOpt;
114 // initialize ShaderChecker options
115 getLayerOptionEnum("ShaderCheckerReportLevel", (uint32_t *) &g_reportingLevel);
116 g_actionIsDefault = getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &g_debugAction);
117
118 if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)
119 {
120 strOpt = getLayerOption("ShaderCheckerLogFilename");
121 if (strOpt)
122 {
123 g_logFile = fopen(strOpt, "w");
124 }
125 if (g_logFile == NULL)
126 g_logFile = stdout;
127 }
128}
129
130
Chris Forbes2778f302015-04-02 13:22:31 +1300131static VkLayerDispatchTable * initLayerTable(const VkBaseLayerObject *gpuw)
132{
133 VkLayerDispatchTable *pTable;
134
135 assert(gpuw);
136 std::unordered_map<void *, VkLayerDispatchTable *>::const_iterator it = tableMap.find((void *) gpuw->baseObject);
137 if (it == tableMap.end())
138 {
139 pTable = new VkLayerDispatchTable;
140 tableMap[(void *) gpuw->baseObject] = pTable;
141 } else
142 {
143 return it->second;
144 }
145
Chia-I Wua3b9a202015-04-17 02:00:54 +0800146 layer_initialize_dispatch_table(pTable, gpuw->pGPA, (VkPhysicalDevice) gpuw->nextObject);
Chris Forbes2778f302015-04-02 13:22:31 +1300147
148 return pTable;
149}
150
151
Chia-I Wua3b9a202015-04-17 02:00:54 +0800152VK_LAYER_EXPORT VkResult VKAPI vkCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo* pCreateInfo, VkDevice* pDevice)
Chris Forbes2778f302015-04-02 13:22:31 +1300153{
154 VkLayerDispatchTable* pTable = tableMap[gpu];
155 VkResult result = pTable->CreateDevice(gpu, pCreateInfo, pDevice);
Chris Forbesb6b8c462015-04-15 06:59:41 +1200156
157 loader_platform_thread_once(&g_initOnce, initLayer);
Chris Forbes2778f302015-04-02 13:22:31 +1300158 // create a mapping for the device object into the dispatch table
159 tableMap.emplace(*pDevice, pTable);
160 return result;
161}
162
163
Chia-I Wua3b9a202015-04-17 02:00:54 +0800164VK_LAYER_EXPORT VkResult VKAPI vkEnumerateLayers(VkPhysicalDevice gpu, size_t maxLayerCount, size_t maxStringSize, size_t* pOutLayerCount, char* const* pOutLayers, void* pReserved)
Chris Forbes2778f302015-04-02 13:22:31 +1300165{
166 if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL || pOutLayers[1] == NULL || pReserved == NULL)
167 return VK_ERROR_INVALID_POINTER;
168
169 if (maxLayerCount < 1)
170 return VK_ERROR_INITIALIZATION_FAILED;
171 *pOutLayerCount = 1;
172 strncpy((char *) pOutLayers[0], "ShaderChecker", maxStringSize);
173 return VK_SUCCESS;
174}
175
176
177struct extProps {
178 uint32_t version;
179 const char * const name;
180};
Tobin Ehlis5d9c2242015-04-17 08:55:13 -0600181#define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 2
Chris Forbes2778f302015-04-02 13:22:31 +1300182static const struct extProps shaderCheckerExts[SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE] = {
183 // TODO what is the version?
184 0x10, "ShaderChecker",
Tobin Ehlis5d9c2242015-04-17 08:55:13 -0600185 0x10, "Validation",
Chris Forbes2778f302015-04-02 13:22:31 +1300186};
187
Chris Forbes2778f302015-04-02 13:22:31 +1300188VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
189 VkExtensionInfoType infoType,
190 uint32_t extensionIndex,
191 size_t* pDataSize,
192 void* pData)
193{
194 VkResult result;
195
196 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
197 VkExtensionProperties *ext_props;
198 uint32_t *count;
199
200 if (pDataSize == NULL)
201 return VK_ERROR_INVALID_POINTER;
202
203 switch (infoType) {
204 case VK_EXTENSION_INFO_TYPE_COUNT:
205 *pDataSize = sizeof(uint32_t);
206 if (pData == NULL)
207 return VK_SUCCESS;
208 count = (uint32_t *) pData;
209 *count = SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE;
210 break;
211 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
212 *pDataSize = sizeof(VkExtensionProperties);
213 if (pData == NULL)
214 return VK_SUCCESS;
215 if (extensionIndex >= SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE)
216 return VK_ERROR_INVALID_VALUE;
217 ext_props = (VkExtensionProperties *) pData;
218 ext_props->version = shaderCheckerExts[extensionIndex].version;
219 strncpy(ext_props->extName, shaderCheckerExts[extensionIndex].name,
220 VK_MAX_EXTENSION_NAME);
221 ext_props->extName[VK_MAX_EXTENSION_NAME - 1] = '\0';
222 break;
223 default:
224 return VK_ERROR_INVALID_VALUE;
225 };
226
227 return VK_SUCCESS;
228}
229
230
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200231static char const *
232storage_class_name(unsigned sc)
233{
234 switch (sc) {
235 case spv::StorageInput: return "input";
236 case spv::StorageOutput: return "output";
237 case spv::StorageConstantUniform: return "const uniform";
238 case spv::StorageUniform: return "uniform";
239 case spv::StorageWorkgroupLocal: return "workgroup local";
240 case spv::StorageWorkgroupGlobal: return "workgroup global";
241 case spv::StoragePrivateGlobal: return "private global";
242 case spv::StorageFunction: return "function";
243 case spv::StorageGeneric: return "generic";
244 case spv::StoragePrivate: return "private";
245 case spv::StorageAtomicCounter: return "atomic counter";
246 default: return "unknown";
247 }
248}
249
250
251/* returns ptr to null terminator */
252static char *
253describe_type(char *dst, shader_source const *src, unsigned type)
254{
255 auto type_def_it = src->type_def_index.find(type);
256
257 if (type_def_it == src->type_def_index.end()) {
258 return dst + sprintf(dst, "undef");
259 }
260
261 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
262 unsigned opcode = code[0] & 0x0ffffu;
263 switch (opcode) {
264 case spv::OpTypeBool:
265 return dst + sprintf(dst, "bool");
266 case spv::OpTypeInt:
267 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
268 case spv::OpTypeFloat:
269 return dst + sprintf(dst, "float%d", code[2]);
270 case spv::OpTypeVector:
271 dst += sprintf(dst, "vec%d of ", code[3]);
272 return describe_type(dst, src, code[2]);
273 case spv::OpTypeMatrix:
274 dst += sprintf(dst, "mat%d of ", code[3]);
275 return describe_type(dst, src, code[2]);
276 case spv::OpTypeArray:
277 dst += sprintf(dst, "arr[%d] of ", code[3]);
278 return describe_type(dst, src, code[2]);
279 case spv::OpTypePointer:
280 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
281 return describe_type(dst, src, code[3]);
282 case spv::OpTypeStruct:
283 {
284 unsigned oplen = code[0] >> 16;
285 dst += sprintf(dst, "struct of (");
286 for (int i = 2; i < oplen; i++) {
287 dst = describe_type(dst, src, code[i]);
288 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
289 }
290 return dst;
291 }
292 default:
293 return dst + sprintf(dst, "oddtype");
294 }
295}
296
297
298static bool
299types_match(shader_source const *a, shader_source const *b, unsigned a_type, unsigned b_type)
300{
301 auto a_type_def_it = a->type_def_index.find(a_type);
302 auto b_type_def_it = b->type_def_index.find(b_type);
303
304 if (a_type_def_it == a->type_def_index.end()) {
305 printf("ERR: can't find def for type %d in producing shader %p; SPIRV probably invalid.\n",
306 a_type, a);
307 return false;
308 }
309
310 if (b_type_def_it == b->type_def_index.end()) {
311 printf("ERR: can't find def for type %d in consuming shader %p; SPIRV probably invalid.\n",
312 b_type, b);
313 return false;
314 }
315
316 /* walk two type trees together, and complain about differences */
317 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
318 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
319
320 unsigned a_opcode = a_code[0] & 0x0ffffu;
321 unsigned b_opcode = b_code[0] & 0x0ffffu;
322
323 if (a_opcode != b_opcode) {
324 printf(" - FAIL: type def opcodes differ: %d vs %d\n", a_opcode, b_opcode);
325 return false;
326 }
327
328 switch (a_opcode) {
329 case spv::OpTypeBool:
330 return true;
331 case spv::OpTypeInt:
332 /* match on width, signedness */
333 return a_code[2] == b_code[2] && a_code[3] == b_code[3];
334 case spv::OpTypeFloat:
335 /* match on width */
336 return a_code[2] == b_code[2];
337 case spv::OpTypeVector:
338 case spv::OpTypeMatrix:
339 case spv::OpTypeArray:
340 /* match on element type, count. these all have the same layout */
341 return types_match(a, b, a_code[2], b_code[2]) && a_code[3] == b_code[3];
342 case spv::OpTypeStruct:
343 /* match on all element types */
344 {
345 unsigned a_len = a_code[0] >> 16;
346 unsigned b_len = b_code[0] >> 16;
347
348 if (a_len != b_len) {
349 return false; /* structs cannot match if member counts differ */
350 }
351
352 for (int i = 2; i < a_len; i++) {
353 if (!types_match(a, b, a_code[i], b_code[i])) {
354 return false;
355 }
356 }
357
358 return true;
359 }
360 case spv::OpTypePointer:
361 /* match on pointee type. storage class is expected to differ */
362 return types_match(a, b, a_code[3], b_code[3]);
363
364 default:
365 /* remaining types are CLisms, or may not appear in the interfaces we
366 * are interested in. Just claim no match.
367 */
368 return false;
369
370 }
371}
372
373
Chris Forbes06e8fc32015-04-13 12:14:52 +1200374static int
375value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
376{
377 auto it = map.find(id);
378 if (it == map.end())
379 return def;
380 else
381 return it->second;
382}
383
384
385struct interface_var {
386 uint32_t id;
387 uint32_t type_id;
388 /* TODO: collect the name, too? Isn't required to be present. */
389};
390
391
392static void
393collect_interface_by_location(shader_source const *src, spv::StorageClass interface,
394 std::map<uint32_t, interface_var> &out,
395 std::map<uint32_t, interface_var> &builtins_out)
396{
397 unsigned int const *code = (unsigned int const *)&src->words[0];
398 size_t size = src->words.size();
399
400 if (code[0] != spv::MagicNumber) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200401 layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
402 "Shader is not SPIR-V, unable to extract interface");
Chris Forbes06e8fc32015-04-13 12:14:52 +1200403 return;
404 }
405
406 std::unordered_map<unsigned, unsigned> var_locations;
407 std::unordered_map<unsigned, unsigned> var_builtins;
408
409 unsigned word = 5;
410 while (word < size) {
411
412 unsigned opcode = code[word] & 0x0ffffu;
413 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
414
415 /* We consider two interface models: SSO rendezvous-by-location, and
416 * builtins. Complain about anything that fits neither model.
417 */
418 if (opcode == spv::OpDecorate) {
419 if (code[word+2] == spv::DecLocation) {
420 var_locations[code[word+1]] = code[word+3];
421 }
422
423 if (code[word+2] == spv::DecBuiltIn) {
424 var_builtins[code[word+1]] = code[word+3];
425 }
426 }
427
428 /* TODO: handle grouped decorations */
429 /* TODO: handle index=1 dual source outputs from FS -- two vars will
430 * have the same location, and we DONT want to clobber. */
431
432 if (opcode == spv::OpVariable && code[word+3] == interface) {
433 int location = value_or_default(var_locations, code[word+2], -1);
434 int builtin = value_or_default(var_builtins, code[word+2], -1);
435
436 if (location == -1 && builtin == -1) {
437 /* No location defined, and not bound to an API builtin.
438 * The spec says nothing about how this case works (or doesn't)
439 * for interface matching.
440 */
Chris Forbes6b2ead62015-04-17 10:13:28 +1200441 char str[1024];
442 sprintf(str, "var %d (type %d) in %s interface has no Location or Builtin decoration\n",
443 code[word+2], code[word+1], storage_class_name(interface));
444 layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", str);
Chris Forbes06e8fc32015-04-13 12:14:52 +1200445 }
446 else if (location != -1) {
447 /* A user-defined interface variable, with a location. */
448 interface_var v;
449 v.id = code[word+2];
450 v.type_id = code[word+1];
451 out[location] = v;
452 }
453 else {
454 /* A builtin interface variable */
455 interface_var v;
456 v.id = code[word+2];
457 v.type_id = code[word+1];
458 builtins_out[builtin] = v;
459 }
460 }
461
462 word += oplen;
463 }
464}
465
466
Chris Forbes2778f302015-04-02 13:22:31 +1300467VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo,
468 VkShader *pShader)
469{
470 VkLayerDispatchTable* pTable = tableMap[(VkBaseLayerObject *)device];
471 VkResult res = pTable->CreateShader(device, pCreateInfo, pShader);
Chris Forbes3b1c4212015-04-08 10:11:59 +1200472
473 shader_map[(VkBaseLayerObject *) *pShader] = new shader_source(pCreateInfo);
Chris Forbes2778f302015-04-02 13:22:31 +1300474 return res;
475}
476
477
Chris Forbes41002452015-04-08 10:19:16 +1200478static void
479validate_interface_between_stages(shader_source const *producer, char const *producer_name,
480 shader_source const *consumer, char const *consumer_name)
481{
482 std::map<uint32_t, interface_var> outputs;
483 std::map<uint32_t, interface_var> inputs;
484
485 std::map<uint32_t, interface_var> builtin_outputs;
486 std::map<uint32_t, interface_var> builtin_inputs;
487
Chris Forbes6b2ead62015-04-17 10:13:28 +1200488 char str[1024];
Chris Forbes41002452015-04-08 10:19:16 +1200489
490 collect_interface_by_location(producer, spv::StorageOutput, outputs, builtin_outputs);
491 collect_interface_by_location(consumer, spv::StorageInput, inputs, builtin_inputs);
492
493 auto a_it = outputs.begin();
494 auto b_it = inputs.begin();
495
496 /* maps sorted by key (location); walk them together to find mismatches */
497 while (a_it != outputs.end() || b_it != inputs.end()) {
498 if (b_it == inputs.end() || a_it->first < b_it->first) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200499 sprintf(str, "%s writes to output location %d which is not consumed by %s\n",
Chris Forbes41002452015-04-08 10:19:16 +1200500 producer_name, a_it->first, consumer_name);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200501 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 +1200502 a_it++;
503 }
504 else if (a_it == outputs.end() || a_it->first > b_it->first) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200505 sprintf(str, "%s consumes input location %d which is not written by %s\n",
Chris Forbes41002452015-04-08 10:19:16 +1200506 consumer_name, b_it->first, producer_name);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200507 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes41002452015-04-08 10:19:16 +1200508 b_it++;
509 }
510 else {
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200511 if (types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200512 /* OK! */
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200513 }
514 else {
515 char producer_type[1024];
516 char consumer_type[1024];
517 describe_type(producer_type, producer, a_it->second.type_id);
518 describe_type(consumer_type, consumer, b_it->second.type_id);
519
Chris Forbes6b2ead62015-04-17 10:13:28 +1200520 sprintf(str, "Type mismatch on location %d: '%s' vs '%s'\n", a_it->first,
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200521 producer_type, consumer_type);
Chris Forbes6b2ead62015-04-17 10:13:28 +1200522 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbes3a5e99a2015-04-10 11:41:20 +1200523 }
Chris Forbes41002452015-04-08 10:19:16 +1200524 a_it++;
525 b_it++;
526 }
527 }
Chris Forbes41002452015-04-08 10:19:16 +1200528}
529
530
Chris Forbes3616b462015-04-08 10:37:20 +1200531enum FORMAT_TYPE {
532 FORMAT_TYPE_UNDEFINED,
533 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
534 FORMAT_TYPE_SINT,
535 FORMAT_TYPE_UINT,
536};
537
538
539static unsigned
540get_format_type(VkFormat fmt) {
541 switch (fmt) {
Chia-I Wua3b9a202015-04-17 02:00:54 +0800542 case VK_FORMAT_UNDEFINED:
Chris Forbes3616b462015-04-08 10:37:20 +1200543 return FORMAT_TYPE_UNDEFINED;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800544 case VK_FORMAT_R8_SINT:
545 case VK_FORMAT_R8G8_SINT:
546 case VK_FORMAT_R8G8B8_SINT:
547 case VK_FORMAT_R8G8B8A8_SINT:
548 case VK_FORMAT_R16_SINT:
549 case VK_FORMAT_R16G16_SINT:
550 case VK_FORMAT_R16G16B16_SINT:
551 case VK_FORMAT_R16G16B16A16_SINT:
552 case VK_FORMAT_R32_SINT:
553 case VK_FORMAT_R32G32_SINT:
554 case VK_FORMAT_R32G32B32_SINT:
555 case VK_FORMAT_R32G32B32A32_SINT:
556 case VK_FORMAT_B8G8R8_SINT:
557 case VK_FORMAT_B8G8R8A8_SINT:
558 case VK_FORMAT_R10G10B10A2_SINT:
559 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200560 return FORMAT_TYPE_SINT;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800561 case VK_FORMAT_R8_UINT:
562 case VK_FORMAT_R8G8_UINT:
563 case VK_FORMAT_R8G8B8_UINT:
564 case VK_FORMAT_R8G8B8A8_UINT:
565 case VK_FORMAT_R16_UINT:
566 case VK_FORMAT_R16G16_UINT:
567 case VK_FORMAT_R16G16B16_UINT:
568 case VK_FORMAT_R16G16B16A16_UINT:
569 case VK_FORMAT_R32_UINT:
570 case VK_FORMAT_R32G32_UINT:
571 case VK_FORMAT_R32G32B32_UINT:
572 case VK_FORMAT_R32G32B32A32_UINT:
573 case VK_FORMAT_B8G8R8_UINT:
574 case VK_FORMAT_B8G8R8A8_UINT:
575 case VK_FORMAT_R10G10B10A2_UINT:
576 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes3616b462015-04-08 10:37:20 +1200577 return FORMAT_TYPE_UINT;
578 default:
579 return FORMAT_TYPE_FLOAT;
580 }
581}
582
583
Chris Forbes772d03b2015-04-08 10:36:37 +1200584static void
585validate_vi_against_vs_inputs(VkPipelineVertexInputCreateInfo const *vi, shader_source const *vs)
586{
587 std::map<uint32_t, interface_var> inputs;
588 /* we collect builtin inputs, but they will never appear in the VI state --
589 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
590 */
591 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200592 char str[1024];
Chris Forbes772d03b2015-04-08 10:36:37 +1200593
594 collect_interface_by_location(vs, spv::StorageInput, inputs, builtin_inputs);
595
596 /* Build index by location */
597 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
598 for (int i = 0; i < vi->attributeCount; i++)
599 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
600
601 auto it_a = attribs.begin();
602 auto it_b = inputs.begin();
603
604 while (it_a != attribs.end() || it_b != inputs.end()) {
605 if (it_b == inputs.end() || it_a->first < it_b->first) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200606 sprintf(str, "Vertex attribute at location %d not consumed by VS", it_a->first);
607 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 +1200608 it_a++;
609 }
610 else if (it_a == attribs.end() || it_b->first < it_a->first) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200611 sprintf(str, "VS consumes input at location %d but not provided", it_b->first);
612 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbes772d03b2015-04-08 10:36:37 +1200613 it_b++;
614 }
615 else {
616 /* TODO: type check */
Chris Forbes6b2ead62015-04-17 10:13:28 +1200617 /* OK! */
Chris Forbes772d03b2015-04-08 10:36:37 +1200618 it_a++;
619 it_b++;
620 }
621 }
Chris Forbes772d03b2015-04-08 10:36:37 +1200622}
623
624
Chris Forbes3616b462015-04-08 10:37:20 +1200625static void
626validate_fs_outputs_against_cb(shader_source const *fs, VkPipelineCbStateCreateInfo const *cb)
627{
628 std::map<uint32_t, interface_var> outputs;
629 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200630 char str[1024];
Chris Forbes3616b462015-04-08 10:37:20 +1200631
632 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
633
634 collect_interface_by_location(fs, spv::StorageOutput, outputs, builtin_outputs);
635
636 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
637 * and all color attachment should be UNORM/SNORM/FLOAT.
638 */
639 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
640 bool broadcast_err = false;
641 if (outputs.size()) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200642 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC",
643 "Should not have user-defined FS outputs when using broadcast");
Chris Forbes3616b462015-04-08 10:37:20 +1200644 broadcast_err = true;
645 }
646
647 for (int i = 0; i < cb->attachmentCount; i++) {
648 unsigned attachmentType = get_format_type(cb->pAttachments[i].format);
649 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200650 layerCbMsg(VK_DBG_MSG_ERROR, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
651 "CB format should not be SINT or UINT when using broadcast");
Chris Forbes3616b462015-04-08 10:37:20 +1200652 broadcast_err = true;
653 }
654 }
655
Chris Forbes3616b462015-04-08 10:37:20 +1200656 return;
657 }
658
659 auto it = outputs.begin();
660 uint32_t attachment = 0;
661
662 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
663 * are currently dense, but the parallel with matching between shader stages is nice.
664 */
665
666 while (it != outputs.end() || attachment < cb->attachmentCount) {
667 if (attachment == cb->attachmentCount || it->first < attachment) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200668 sprintf(str, "FS writes to output location %d with no matching attachment", it->first);
669 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 +1200670 it++;
671 }
672 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200673 sprintf(str, "Attachment %d not written by FS", attachment);
674 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 +1200675 attachment++;
676 }
677 else {
Chris Forbes6b2ead62015-04-17 10:13:28 +1200678 /* OK! */
Chris Forbes3616b462015-04-08 10:37:20 +1200679 /* TODO: typecheck */
680 it++;
681 attachment++;
682 }
683 }
Chris Forbes3616b462015-04-08 10:37:20 +1200684}
685
686
Chris Forbes4175e6f2015-04-08 10:15:35 +1200687VK_LAYER_EXPORT VkResult VKAPI vkCreateGraphicsPipeline(VkDevice device,
688 const VkGraphicsPipelineCreateInfo *pCreateInfo,
689 VkPipeline *pPipeline)
690{
Chris Forbes6b2ead62015-04-17 10:13:28 +1200691 /* TODO: run cross-stage validation for GS, TCS, TES stages */
Chris Forbes4175e6f2015-04-08 10:15:35 +1200692
Chris Forbesf6800b52015-04-08 10:16:45 +1200693 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
694 * before trying to do anything more: */
695
696 shader_source const *vs_source = 0;
697 shader_source const *fs_source = 0;
698 VkPipelineCbStateCreateInfo const *cb = 0;
699 VkPipelineVertexInputCreateInfo const *vi = 0;
Chris Forbes6b2ead62015-04-17 10:13:28 +1200700 char str[1024];
Chris Forbesf6800b52015-04-08 10:16:45 +1200701
702 for (auto stage = pCreateInfo; stage; stage = (decltype(stage))stage->pNext) {
703 if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
704 auto shader_stage = (VkPipelineShaderStageCreateInfo const *)stage;
705
Chris Forbes6b2ead62015-04-17 10:13:28 +1200706 if (shader_stage->shader.stage == VK_SHADER_STAGE_VERTEX) {
Chris Forbesf6800b52015-04-08 10:16:45 +1200707 vs_source = shader_map[(void *)(shader_stage->shader.shader)];
Chris Forbes6b2ead62015-04-17 10:13:28 +1200708 }
709 else if (shader_stage->shader.stage == VK_SHADER_STAGE_FRAGMENT) {
Chris Forbesf6800b52015-04-08 10:16:45 +1200710 fs_source = shader_map[(void *)(shader_stage->shader.shader)];
Chris Forbes6b2ead62015-04-17 10:13:28 +1200711 }
712 else {
713 sprintf(str, "Unknown shader stage %d\n", shader_stage->shader.stage);
714 layerCbMsg(VK_DBG_MSG_WARNING, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC", str);
715 }
Chris Forbesf6800b52015-04-08 10:16:45 +1200716 }
717 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO) {
718 cb = (VkPipelineCbStateCreateInfo const *)stage;
719 }
720 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO) {
721 vi = (VkPipelineVertexInputCreateInfo const *)stage;
722 }
723 }
724
Chris Forbes6b2ead62015-04-17 10:13:28 +1200725 sprintf(str, "Pipeline: vi=%p vs=%p fs=%p cb=%p\n", vi, vs_source, fs_source, cb);
726 layerCbMsg(VK_DBG_MSG_UNKNOWN, VK_VALIDATION_LEVEL_0, NULL, 0, SHADER_CHECKER_NONE, "SC", str);
Chris Forbesf6800b52015-04-08 10:16:45 +1200727
Chris Forbes772d03b2015-04-08 10:36:37 +1200728 if (vi && vs_source) {
729 validate_vi_against_vs_inputs(vi, vs_source);
730 }
731
Chris Forbes41002452015-04-08 10:19:16 +1200732 if (vs_source && fs_source) {
733 validate_interface_between_stages(vs_source, "vertex shader",
734 fs_source, "fragment shader");
735 }
736
Chris Forbes3616b462015-04-08 10:37:20 +1200737 if (fs_source && cb) {
738 validate_fs_outputs_against_cb(fs_source, cb);
739 }
740
Chris Forbes4175e6f2015-04-08 10:15:35 +1200741 VkLayerDispatchTable *pTable = tableMap[(VkBaseLayerObject *)device];
742 VkResult res = pTable->CreateGraphicsPipeline(device, pCreateInfo, pPipeline);
743 return res;
744}
745
746
Chia-I Wua3b9a202015-04-17 02:00:54 +0800747VK_LAYER_EXPORT void * VKAPI vkGetProcAddr(VkPhysicalDevice gpu, const char* pName)
Chris Forbes2778f302015-04-02 13:22:31 +1300748{
749 if (gpu == NULL)
750 return NULL;
751
752 initLayerTable((const VkBaseLayerObject *) gpu);
753
Chris Forbesb6b8c462015-04-15 06:59:41 +1200754 loader_platform_thread_once(&g_initOnce, initLayer);
755
Chris Forbes2778f302015-04-02 13:22:31 +1300756#define ADD_HOOK(fn) \
757 if (!strncmp(#fn, pName, sizeof(#fn))) \
758 return (void *) fn
759
760 ADD_HOOK(vkGetProcAddr);
761 ADD_HOOK(vkEnumerateLayers);
762 ADD_HOOK(vkCreateDevice);
763 ADD_HOOK(vkCreateShader);
Chris Forbes4175e6f2015-04-08 10:15:35 +1200764 ADD_HOOK(vkCreateGraphicsPipeline);
Chris Forbes2778f302015-04-02 13:22:31 +1300765
766 VkBaseLayerObject* gpuw = (VkBaseLayerObject *) gpu;
767 if (gpuw->pGPA == NULL)
768 return NULL;
Chia-I Wua3b9a202015-04-17 02:00:54 +0800769 return gpuw->pGPA((VkPhysicalDevice) gpuw->nextObject, pName);
Chris Forbes2778f302015-04-02 13:22:31 +1300770}