blob: 57aab4f05ed1988fd62c52d5ac07d67d76eacb8e [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"
Jon Ashburn5a10d212015-06-01 10:02:09 -060036#include "layers_table.h"
Chris Forbes3317b382015-05-04 14:04:24 +120037#include "vk_enum_string_helper.h"
Chris Forbes5c75afe2015-04-17 10:13:28 +120038#include "shader_checker.h"
Chris Forbesaab9d112015-04-02 13:22:31 +130039// The following is #included again to catch certain OS-specific functions
40// being used:
41#include "loader_platform.h"
42
Chris Forbes32e3b462015-05-09 10:31:21 +120043#include "spirv/spirv.h"
Chris Forbesaab9d112015-04-02 13:22:31 +130044
Chris Forbesaab9d112015-04-02 13:22:31 +130045
Chris Forbes1b466bd2015-04-15 06:59:41 +120046static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(g_initOnce);
Chris Forbes1ed0f982015-05-29 14:55:18 +120047// TODO : This can be much smarter, using separate locks for separate global data
48static int globalLockInitialized = 0;
49static loader_platform_thread_mutex globalLock;
Chris Forbes4396ff52015-04-08 10:11:59 +120050
Chris Forbes1bb5a2e2015-04-10 11:41:20 +120051
52static void
53build_type_def_index(std::vector<unsigned> const &words, std::unordered_map<unsigned, unsigned> &type_def_index)
54{
55 unsigned int const *code = (unsigned int const *)&words[0];
56 size_t size = words.size();
57
58 unsigned word = 5;
59 while (word < size) {
60 unsigned opcode = code[word] & 0x0ffffu;
61 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
62
63 switch (opcode) {
64 case spv::OpTypeVoid:
65 case spv::OpTypeBool:
66 case spv::OpTypeInt:
67 case spv::OpTypeFloat:
68 case spv::OpTypeVector:
69 case spv::OpTypeMatrix:
70 case spv::OpTypeSampler:
71 case spv::OpTypeFilter:
72 case spv::OpTypeArray:
73 case spv::OpTypeRuntimeArray:
74 case spv::OpTypeStruct:
75 case spv::OpTypeOpaque:
76 case spv::OpTypePointer:
77 case spv::OpTypeFunction:
78 case spv::OpTypeEvent:
79 case spv::OpTypeDeviceEvent:
80 case spv::OpTypeReserveId:
81 case spv::OpTypeQueue:
82 case spv::OpTypePipe:
83 type_def_index[code[word+1]] = word;
84 break;
85
86 default:
87 /* We only care about type definitions */
88 break;
89 }
90
91 word += oplen;
92 }
93}
94
Chris Forbes4396ff52015-04-08 10:11:59 +120095struct shader_source {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +120096 /* the spirv image itself */
Chris Forbes4396ff52015-04-08 10:11:59 +120097 std::vector<uint32_t> words;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +120098 /* a mapping of <id> to the first word of its def. this is useful because walking type
99 * trees requires jumping all over the instruction stream.
100 */
101 std::unordered_map<unsigned, unsigned> type_def_index;
Chris Forbes4453c772015-06-05 15:01:08 +1200102 bool is_spirv;
Chris Forbes4396ff52015-04-08 10:11:59 +1200103
104 shader_source(VkShaderCreateInfo const *pCreateInfo) :
Chris Forbes4453c772015-06-05 15:01:08 +1200105 words((uint32_t *)pCreateInfo->pCode, (uint32_t *)pCreateInfo->pCode + pCreateInfo->codeSize / sizeof(uint32_t)),
106 type_def_index(),
107 is_spirv(true) {
108
109 if (words.size() < 5 || words[0] != spv::MagicNumber || words[1] != spv::Version) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600110 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_NON_SPIRV_SHADER, "SC",
Chris Forbes4453c772015-06-05 15:01:08 +1200111 "Shader is not SPIR-V, most checks will not be possible");
112 is_spirv = false;
113 return;
114 }
115
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200116
117 build_type_def_index(words, type_def_index);
Chris Forbes4396ff52015-04-08 10:11:59 +1200118 }
119};
120
121
122static std::unordered_map<void *, shader_source *> shader_map;
123
124
Chris Forbes1b466bd2015-04-15 06:59:41 +1200125static void
126initLayer()
127{
128 const char *strOpt;
129 // initialize ShaderChecker options
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600130 getLayerOptionEnum("ShaderCheckerReportLevel", (uint32_t *) &g_reportFlags);
Chris Forbes1b466bd2015-04-15 06:59:41 +1200131 g_actionIsDefault = getLayerOptionEnum("ShaderCheckerDebugAction", (uint32_t *) &g_debugAction);
132
133 if (g_debugAction & VK_DBG_LAYER_ACTION_LOG_MSG)
134 {
135 strOpt = getLayerOption("ShaderCheckerLogFilename");
136 if (strOpt)
137 {
138 g_logFile = fopen(strOpt, "w");
139 }
140 if (g_logFile == NULL)
141 g_logFile = stdout;
142 }
143}
144
Tobin Ehlis432a9ba2015-04-17 08:55:13 -0600145#define SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE 2
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600146static const VkExtensionProperties shaderCheckerExts[SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE] = {
147 {
148 VK_STRUCTURE_TYPE_EXTENSION_PROPERTIES,
149 "ShaderChecker",
150 0x10,
151 "Sample layer: ShaderChecker",
152// 0,
153// NULL,
154 }
Chris Forbesaab9d112015-04-02 13:22:31 +1300155};
156
Chris Forbesaab9d112015-04-02 13:22:31 +1300157VK_LAYER_EXPORT VkResult VKAPI vkGetGlobalExtensionInfo(
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600158 VkExtensionInfoType infoType,
159 uint32_t extensionIndex,
160 size_t* pDataSize,
161 void* pData)
Chris Forbesaab9d112015-04-02 13:22:31 +1300162{
Chris Forbesaab9d112015-04-02 13:22:31 +1300163 /* This entrypoint is NOT going to init it's own dispatch table since loader calls here early */
Chris Forbesaab9d112015-04-02 13:22:31 +1300164 uint32_t *count;
165
166 if (pDataSize == NULL)
167 return VK_ERROR_INVALID_POINTER;
168
169 switch (infoType) {
170 case VK_EXTENSION_INFO_TYPE_COUNT:
171 *pDataSize = sizeof(uint32_t);
172 if (pData == NULL)
173 return VK_SUCCESS;
174 count = (uint32_t *) pData;
175 *count = SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE;
176 break;
177 case VK_EXTENSION_INFO_TYPE_PROPERTIES:
178 *pDataSize = sizeof(VkExtensionProperties);
179 if (pData == NULL)
180 return VK_SUCCESS;
181 if (extensionIndex >= SHADER_CHECKER_LAYER_EXT_ARRAY_SIZE)
182 return VK_ERROR_INVALID_VALUE;
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600183 memcpy((VkExtensionProperties *) pData, &shaderCheckerExts[extensionIndex], sizeof(VkExtensionProperties));
Chris Forbesaab9d112015-04-02 13:22:31 +1300184 break;
185 default:
186 return VK_ERROR_INVALID_VALUE;
187 };
188
189 return VK_SUCCESS;
190}
191
192
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200193static char const *
194storage_class_name(unsigned sc)
195{
196 switch (sc) {
Cody Northrop812b4612015-04-20 14:09:40 -0600197 case spv::StorageClassInput: return "input";
198 case spv::StorageClassOutput: return "output";
199 case spv::StorageClassUniformConstant: return "const uniform";
200 case spv::StorageClassUniform: return "uniform";
201 case spv::StorageClassWorkgroupLocal: return "workgroup local";
202 case spv::StorageClassWorkgroupGlobal: return "workgroup global";
203 case spv::StorageClassPrivateGlobal: return "private global";
204 case spv::StorageClassFunction: return "function";
205 case spv::StorageClassGeneric: return "generic";
206 case spv::StorageClassPrivate: return "private";
207 case spv::StorageClassAtomicCounter: return "atomic counter";
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200208 default: return "unknown";
209 }
210}
211
212
213/* returns ptr to null terminator */
214static char *
215describe_type(char *dst, shader_source const *src, unsigned type)
216{
217 auto type_def_it = src->type_def_index.find(type);
218
219 if (type_def_it == src->type_def_index.end()) {
220 return dst + sprintf(dst, "undef");
221 }
222
223 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
224 unsigned opcode = code[0] & 0x0ffffu;
225 switch (opcode) {
226 case spv::OpTypeBool:
227 return dst + sprintf(dst, "bool");
228 case spv::OpTypeInt:
229 return dst + sprintf(dst, "%cint%d", code[3] ? 's' : 'u', code[2]);
230 case spv::OpTypeFloat:
231 return dst + sprintf(dst, "float%d", code[2]);
232 case spv::OpTypeVector:
233 dst += sprintf(dst, "vec%d of ", code[3]);
234 return describe_type(dst, src, code[2]);
235 case spv::OpTypeMatrix:
236 dst += sprintf(dst, "mat%d of ", code[3]);
237 return describe_type(dst, src, code[2]);
238 case spv::OpTypeArray:
239 dst += sprintf(dst, "arr[%d] of ", code[3]);
240 return describe_type(dst, src, code[2]);
241 case spv::OpTypePointer:
242 dst += sprintf(dst, "ptr to %s ", storage_class_name(code[2]));
243 return describe_type(dst, src, code[3]);
244 case spv::OpTypeStruct:
245 {
246 unsigned oplen = code[0] >> 16;
247 dst += sprintf(dst, "struct of (");
Ian Elliottf21f14b2015-04-17 11:05:04 -0600248 for (unsigned i = 2; i < oplen; i++) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200249 dst = describe_type(dst, src, code[i]);
250 dst += sprintf(dst, i == oplen-1 ? ")" : ", ");
251 }
252 return dst;
253 }
254 default:
255 return dst + sprintf(dst, "oddtype");
256 }
257}
258
259
260static bool
Chris Forbes0a94a372015-06-05 14:57:05 +1200261types_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 +1200262{
263 auto a_type_def_it = a->type_def_index.find(a_type);
264 auto b_type_def_it = b->type_def_index.find(b_type);
265
266 if (a_type_def_it == a->type_def_index.end()) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200267 return false;
268 }
269
270 if (b_type_def_it == b->type_def_index.end()) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200271 return false;
272 }
273
274 /* walk two type trees together, and complain about differences */
275 unsigned int const *a_code = (unsigned int const *)&a->words[a_type_def_it->second];
276 unsigned int const *b_code = (unsigned int const *)&b->words[b_type_def_it->second];
277
278 unsigned a_opcode = a_code[0] & 0x0ffffu;
279 unsigned b_opcode = b_code[0] & 0x0ffffu;
280
Chris Forbes0a94a372015-06-05 14:57:05 +1200281 if (b_arrayed && b_opcode == spv::OpTypeArray) {
282 /* we probably just found the extra level of arrayness in b_type: compare the type inside it to a_type */
283 return types_match(a, b, a_type, b_code[2], false);
284 }
285
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200286 if (a_opcode != b_opcode) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200287 return false;
288 }
289
290 switch (a_opcode) {
Chris Forbes0a94a372015-06-05 14:57:05 +1200291 /* 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 +1200292 case spv::OpTypeBool:
Chris Forbes0a94a372015-06-05 14:57:05 +1200293 return true && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200294 case spv::OpTypeInt:
295 /* match on width, signedness */
Chris Forbes0a94a372015-06-05 14:57:05 +1200296 return a_code[2] == b_code[2] && a_code[3] == b_code[3] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200297 case spv::OpTypeFloat:
298 /* match on width */
Chris Forbes0a94a372015-06-05 14:57:05 +1200299 return a_code[2] == b_code[2] && !b_arrayed;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200300 case spv::OpTypeVector:
301 case spv::OpTypeMatrix:
302 case spv::OpTypeArray:
Chris Forbes0a94a372015-06-05 14:57:05 +1200303 /* match on element type, count. these all have the same layout. we don't get here if
304 * b_arrayed -- that is handled above. */
305 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 +1200306 case spv::OpTypeStruct:
307 /* match on all element types */
308 {
Chris Forbes0a94a372015-06-05 14:57:05 +1200309 if (b_arrayed) {
310 /* for the purposes of matching different levels of arrayness, structs are leaves. */
311 return false;
312 }
313
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200314 unsigned a_len = a_code[0] >> 16;
315 unsigned b_len = b_code[0] >> 16;
316
317 if (a_len != b_len) {
318 return false; /* structs cannot match if member counts differ */
319 }
320
Ian Elliottf21f14b2015-04-17 11:05:04 -0600321 for (unsigned i = 2; i < a_len; i++) {
Chris Forbes0a94a372015-06-05 14:57:05 +1200322 if (!types_match(a, b, a_code[i], b_code[i], b_arrayed)) {
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200323 return false;
324 }
325 }
326
327 return true;
328 }
329 case spv::OpTypePointer:
330 /* match on pointee type. storage class is expected to differ */
Chris Forbes0a94a372015-06-05 14:57:05 +1200331 return types_match(a, b, a_code[3], b_code[3], b_arrayed);
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200332
333 default:
334 /* remaining types are CLisms, or may not appear in the interfaces we
335 * are interested in. Just claim no match.
336 */
337 return false;
338
339 }
340}
341
342
Chris Forbes67cc36f2015-04-13 12:14:52 +1200343static int
344value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, int def)
345{
346 auto it = map.find(id);
347 if (it == map.end())
348 return def;
349 else
350 return it->second;
351}
352
353
354struct interface_var {
355 uint32_t id;
356 uint32_t type_id;
357 /* TODO: collect the name, too? Isn't required to be present. */
358};
359
360
361static void
Ian Elliottf21f14b2015-04-17 11:05:04 -0600362collect_interface_by_location(shader_source const *src, spv::StorageClass sinterface,
Chris Forbes67cc36f2015-04-13 12:14:52 +1200363 std::map<uint32_t, interface_var> &out,
364 std::map<uint32_t, interface_var> &builtins_out)
365{
366 unsigned int const *code = (unsigned int const *)&src->words[0];
367 size_t size = src->words.size();
368
Chris Forbes67cc36f2015-04-13 12:14:52 +1200369 std::unordered_map<unsigned, unsigned> var_locations;
370 std::unordered_map<unsigned, unsigned> var_builtins;
371
372 unsigned word = 5;
373 while (word < size) {
374
375 unsigned opcode = code[word] & 0x0ffffu;
376 unsigned oplen = (code[word] & 0xffff0000u) >> 16;
377
378 /* We consider two interface models: SSO rendezvous-by-location, and
379 * builtins. Complain about anything that fits neither model.
380 */
381 if (opcode == spv::OpDecorate) {
Cody Northrop812b4612015-04-20 14:09:40 -0600382 if (code[word+2] == spv::DecorationLocation) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200383 var_locations[code[word+1]] = code[word+3];
384 }
385
Cody Northrop812b4612015-04-20 14:09:40 -0600386 if (code[word+2] == spv::DecorationBuiltIn) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200387 var_builtins[code[word+1]] = code[word+3];
388 }
389 }
390
391 /* TODO: handle grouped decorations */
392 /* TODO: handle index=1 dual source outputs from FS -- two vars will
393 * have the same location, and we DONT want to clobber. */
394
Ian Elliottf21f14b2015-04-17 11:05:04 -0600395 if (opcode == spv::OpVariable && code[word+3] == sinterface) {
Chris Forbes67cc36f2015-04-13 12:14:52 +1200396 int location = value_or_default(var_locations, code[word+2], -1);
397 int builtin = value_or_default(var_builtins, code[word+2], -1);
398
399 if (location == -1 && builtin == -1) {
400 /* No location defined, and not bound to an API builtin.
401 * The spec says nothing about how this case works (or doesn't)
402 * for interface matching.
403 */
Chris Forbes5c75afe2015-04-17 10:13:28 +1200404 char str[1024];
405 sprintf(str, "var %d (type %d) in %s interface has no Location or Builtin decoration\n",
Ian Elliottf21f14b2015-04-17 11:05:04 -0600406 code[word+2], code[word+1], storage_class_name(sinterface));
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600407 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC", str);
Chris Forbes67cc36f2015-04-13 12:14:52 +1200408 }
409 else if (location != -1) {
410 /* A user-defined interface variable, with a location. */
411 interface_var v;
412 v.id = code[word+2];
413 v.type_id = code[word+1];
414 out[location] = v;
415 }
416 else {
417 /* A builtin interface variable */
418 interface_var v;
419 v.id = code[word+2];
420 v.type_id = code[word+1];
421 builtins_out[builtin] = v;
422 }
423 }
424
425 word += oplen;
426 }
427}
428
429
Chris Forbesaab9d112015-04-02 13:22:31 +1300430VK_LAYER_EXPORT VkResult VKAPI vkCreateShader(VkDevice device, const VkShaderCreateInfo *pCreateInfo,
431 VkShader *pShader)
432{
Chris Forbes1ed0f982015-05-29 14:55:18 +1200433 loader_platform_thread_lock_mutex(&globalLock);
Jon Ashburn5a10d212015-06-01 10:02:09 -0600434 VkResult res = device_dispatch_table(device)->CreateShader(device, pCreateInfo, pShader);
Chris Forbes4396ff52015-04-08 10:11:59 +1200435
436 shader_map[(VkBaseLayerObject *) *pShader] = new shader_source(pCreateInfo);
Chris Forbes1ed0f982015-05-29 14:55:18 +1200437 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbesaab9d112015-04-02 13:22:31 +1300438 return res;
439}
440
441
Chris Forbes5f362d02015-05-25 11:13:22 +1200442static bool
Chris Forbesbb164b62015-04-08 10:19:16 +1200443validate_interface_between_stages(shader_source const *producer, char const *producer_name,
Chris Forbes4453c772015-06-05 15:01:08 +1200444 shader_source const *consumer, char const *consumer_name,
445 bool consumer_arrayed_input)
Chris Forbesbb164b62015-04-08 10:19:16 +1200446{
447 std::map<uint32_t, interface_var> outputs;
448 std::map<uint32_t, interface_var> inputs;
449
450 std::map<uint32_t, interface_var> builtin_outputs;
451 std::map<uint32_t, interface_var> builtin_inputs;
452
Chris Forbes5c75afe2015-04-17 10:13:28 +1200453 char str[1024];
Chris Forbes5f362d02015-05-25 11:13:22 +1200454 bool pass = true;
Chris Forbesbb164b62015-04-08 10:19:16 +1200455
Cody Northrop812b4612015-04-20 14:09:40 -0600456 collect_interface_by_location(producer, spv::StorageClassOutput, outputs, builtin_outputs);
457 collect_interface_by_location(consumer, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesbb164b62015-04-08 10:19:16 +1200458
459 auto a_it = outputs.begin();
460 auto b_it = inputs.begin();
461
462 /* maps sorted by key (location); walk them together to find mismatches */
David Pinedof5997ab2015-04-27 16:36:17 -0600463 while ((outputs.size() > 0 && a_it != outputs.end()) || ( inputs.size() && b_it != inputs.end())) {
464 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
465 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200466 auto a_first = a_at_end ? 0 : a_it->first;
467 auto b_first = b_at_end ? 0 : b_it->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600468
469 if (b_at_end || a_first < b_first) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200470 sprintf(str, "%s writes to output location %d which is not consumed by %s\n",
David Pinedof5997ab2015-04-27 16:36:17 -0600471 producer_name, a_first, consumer_name);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600472 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbesbb164b62015-04-08 10:19:16 +1200473 a_it++;
474 }
David Pinedof5997ab2015-04-27 16:36:17 -0600475 else if (a_at_end || a_first > b_first) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200476 sprintf(str, "%s consumes input location %d which is not written by %s\n",
David Pinedof5997ab2015-04-27 16:36:17 -0600477 consumer_name, b_first, producer_name);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600478 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes5f362d02015-05-25 11:13:22 +1200479 pass = false;
Chris Forbesbb164b62015-04-08 10:19:16 +1200480 b_it++;
481 }
482 else {
Chris Forbes4453c772015-06-05 15:01:08 +1200483 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 +1200484 /* OK! */
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200485 }
486 else {
487 char producer_type[1024];
488 char consumer_type[1024];
489 describe_type(producer_type, producer, a_it->second.type_id);
490 describe_type(consumer_type, consumer, b_it->second.type_id);
491
Chris Forbes5c75afe2015-04-17 10:13:28 +1200492 sprintf(str, "Type mismatch on location %d: '%s' vs '%s'\n", a_it->first,
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200493 producer_type, consumer_type);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600494 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbes5f362d02015-05-25 11:13:22 +1200495 pass = false;
Chris Forbes1bb5a2e2015-04-10 11:41:20 +1200496 }
Chris Forbesbb164b62015-04-08 10:19:16 +1200497 a_it++;
498 b_it++;
499 }
500 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200501
502 return pass;
Chris Forbesbb164b62015-04-08 10:19:16 +1200503}
504
505
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200506enum FORMAT_TYPE {
507 FORMAT_TYPE_UNDEFINED,
508 FORMAT_TYPE_FLOAT, /* UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader */
509 FORMAT_TYPE_SINT,
510 FORMAT_TYPE_UINT,
511};
512
513
514static unsigned
515get_format_type(VkFormat fmt) {
516 switch (fmt) {
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800517 case VK_FORMAT_UNDEFINED:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200518 return FORMAT_TYPE_UNDEFINED;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800519 case VK_FORMAT_R8_SINT:
520 case VK_FORMAT_R8G8_SINT:
521 case VK_FORMAT_R8G8B8_SINT:
522 case VK_FORMAT_R8G8B8A8_SINT:
523 case VK_FORMAT_R16_SINT:
524 case VK_FORMAT_R16G16_SINT:
525 case VK_FORMAT_R16G16B16_SINT:
526 case VK_FORMAT_R16G16B16A16_SINT:
527 case VK_FORMAT_R32_SINT:
528 case VK_FORMAT_R32G32_SINT:
529 case VK_FORMAT_R32G32B32_SINT:
530 case VK_FORMAT_R32G32B32A32_SINT:
531 case VK_FORMAT_B8G8R8_SINT:
532 case VK_FORMAT_B8G8R8A8_SINT:
533 case VK_FORMAT_R10G10B10A2_SINT:
534 case VK_FORMAT_B10G10R10A2_SINT:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200535 return FORMAT_TYPE_SINT;
Chia-I Wu6097f3a2015-04-17 02:00:54 +0800536 case VK_FORMAT_R8_UINT:
537 case VK_FORMAT_R8G8_UINT:
538 case VK_FORMAT_R8G8B8_UINT:
539 case VK_FORMAT_R8G8B8A8_UINT:
540 case VK_FORMAT_R16_UINT:
541 case VK_FORMAT_R16G16_UINT:
542 case VK_FORMAT_R16G16B16_UINT:
543 case VK_FORMAT_R16G16B16A16_UINT:
544 case VK_FORMAT_R32_UINT:
545 case VK_FORMAT_R32G32_UINT:
546 case VK_FORMAT_R32G32B32_UINT:
547 case VK_FORMAT_R32G32B32A32_UINT:
548 case VK_FORMAT_B8G8R8_UINT:
549 case VK_FORMAT_B8G8R8A8_UINT:
550 case VK_FORMAT_R10G10B10A2_UINT:
551 case VK_FORMAT_B10G10R10A2_UINT:
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200552 return FORMAT_TYPE_UINT;
553 default:
554 return FORMAT_TYPE_FLOAT;
555 }
556}
557
558
Chris Forbes28c50882015-05-04 14:04:06 +1200559/* characterizes a SPIR-V type appearing in an interface to a FF stage,
560 * for comparison to a VkFormat's characterization above. */
561static unsigned
562get_fundamental_type(shader_source const *src, unsigned type)
563{
564 auto type_def_it = src->type_def_index.find(type);
565
566 if (type_def_it == src->type_def_index.end()) {
567 return FORMAT_TYPE_UNDEFINED;
568 }
569
570 unsigned int const *code = (unsigned int const *)&src->words[type_def_it->second];
571 unsigned opcode = code[0] & 0x0ffffu;
572 switch (opcode) {
573 case spv::OpTypeInt:
574 return code[3] ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
575 case spv::OpTypeFloat:
576 return FORMAT_TYPE_FLOAT;
577 case spv::OpTypeVector:
578 return get_fundamental_type(src, code[2]);
579 case spv::OpTypeMatrix:
580 return get_fundamental_type(src, code[2]);
581 case spv::OpTypeArray:
582 return get_fundamental_type(src, code[2]);
583 case spv::OpTypePointer:
584 return get_fundamental_type(src, code[3]);
585 default:
586 return FORMAT_TYPE_UNDEFINED;
587 }
588}
589
590
Chris Forbes5f362d02015-05-25 11:13:22 +1200591static bool
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200592validate_vi_consistency(VkPipelineVertexInputCreateInfo const *vi)
593{
594 /* walk the binding descriptions, which describe the step rate and stride of each vertex buffer.
595 * each binding should be specified only once.
596 */
597 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
598 char str[1024];
599 bool pass = true;
600
601 for (unsigned i = 0; i < vi->bindingCount; i++) {
602 auto desc = &vi->pVertexBindingDescriptions[i];
603 auto & binding = bindings[desc->binding];
604 if (binding) {
605 sprintf(str, "Duplicate vertex input binding descriptions for binding %d", desc->binding);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600606 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INCONSISTENT_VI, "SC", str);
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200607 pass = false;
608 }
609 else {
610 binding = desc;
611 }
612 }
613
614 return pass;
615}
616
617
618static bool
Chris Forbesfcd05f12015-04-08 10:36:37 +1200619validate_vi_against_vs_inputs(VkPipelineVertexInputCreateInfo const *vi, shader_source const *vs)
620{
621 std::map<uint32_t, interface_var> inputs;
622 /* we collect builtin inputs, but they will never appear in the VI state --
623 * the vs builtin inputs are generated in the pipeline, not sourced from buffers (VertexID, etc)
624 */
625 std::map<uint32_t, interface_var> builtin_inputs;
Chris Forbes5c75afe2015-04-17 10:13:28 +1200626 char str[1024];
Chris Forbes5f362d02015-05-25 11:13:22 +1200627 bool pass = true;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200628
Cody Northrop812b4612015-04-20 14:09:40 -0600629 collect_interface_by_location(vs, spv::StorageClassInput, inputs, builtin_inputs);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200630
631 /* Build index by location */
632 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
Chris Forbes6f2ab982015-05-25 11:13:24 +1200633 if (vi) {
634 for (unsigned i = 0; i < vi->attributeCount; i++)
635 attribs[vi->pVertexAttributeDescriptions[i].location] = &vi->pVertexAttributeDescriptions[i];
636 }
Chris Forbesfcd05f12015-04-08 10:36:37 +1200637
638 auto it_a = attribs.begin();
639 auto it_b = inputs.begin();
640
David Pinedof5997ab2015-04-27 16:36:17 -0600641 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
642 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
643 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
Chris Forbes4cb97672015-06-10 08:37:27 +1200644 auto a_first = a_at_end ? 0 : it_a->first;
645 auto b_first = b_at_end ? 0 : it_b->first;
David Pinedof5997ab2015-04-27 16:36:17 -0600646 if (b_at_end || a_first < b_first) {
647 sprintf(str, "Vertex attribute at location %d not consumed by VS", a_first);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600648 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbesfcd05f12015-04-08 10:36:37 +1200649 it_a++;
650 }
David Pinedof5997ab2015-04-27 16:36:17 -0600651 else if (a_at_end || b_first < a_first) {
652 sprintf(str, "VS consumes input at location %d but not provided", b_first);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600653 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbes5f362d02015-05-25 11:13:22 +1200654 pass = false;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200655 it_b++;
656 }
657 else {
Chris Forbes3317b382015-05-04 14:04:24 +1200658 unsigned attrib_type = get_format_type(it_a->second->format);
659 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
660
661 /* type checking */
662 if (attrib_type != FORMAT_TYPE_UNDEFINED && input_type != FORMAT_TYPE_UNDEFINED && attrib_type != input_type) {
663 char vs_type[1024];
664 describe_type(vs_type, vs, it_b->second.type_id);
665 sprintf(str, "Attribute type of `%s` at location %d does not match VS input type of `%s`",
666 string_VkFormat(it_a->second->format), a_first, vs_type);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600667 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbes5f362d02015-05-25 11:13:22 +1200668 pass = false;
Chris Forbes3317b382015-05-04 14:04:24 +1200669 }
670
Chris Forbes5c75afe2015-04-17 10:13:28 +1200671 /* OK! */
Chris Forbesfcd05f12015-04-08 10:36:37 +1200672 it_a++;
673 it_b++;
674 }
675 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200676
677 return pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200678}
679
680
Chris Forbes5f362d02015-05-25 11:13:22 +1200681static bool
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200682validate_fs_outputs_against_cb(shader_source const *fs, VkPipelineCbStateCreateInfo const *cb)
683{
684 std::map<uint32_t, interface_var> outputs;
685 std::map<uint32_t, interface_var> builtin_outputs;
Chris Forbes5c75afe2015-04-17 10:13:28 +1200686 char str[1024];
Chris Forbes5f362d02015-05-25 11:13:22 +1200687 bool pass = true;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200688
689 /* TODO: dual source blend index (spv::DecIndex, zero if not provided) */
690
Cody Northrop812b4612015-04-20 14:09:40 -0600691 collect_interface_by_location(fs, spv::StorageClassOutput, outputs, builtin_outputs);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200692
693 /* Check for legacy gl_FragColor broadcast: In this case, we should have no user-defined outputs,
694 * and all color attachment should be UNORM/SNORM/FLOAT.
695 */
696 if (builtin_outputs.find(spv::BuiltInFragColor) != builtin_outputs.end()) {
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200697 if (outputs.size()) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600698 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_FS_MIXED_BROADCAST, "SC",
Chris Forbes5c75afe2015-04-17 10:13:28 +1200699 "Should not have user-defined FS outputs when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200700 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200701 }
702
Ian Elliottf21f14b2015-04-17 11:05:04 -0600703 for (unsigned i = 0; i < cb->attachmentCount; i++) {
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200704 unsigned attachmentType = get_format_type(cb->pAttachments[i].format);
705 if (attachmentType == FORMAT_TYPE_SINT || attachmentType == FORMAT_TYPE_UINT) {
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600706 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
Chris Forbes5c75afe2015-04-17 10:13:28 +1200707 "CB format should not be SINT or UINT when using broadcast");
Chris Forbes5f362d02015-05-25 11:13:22 +1200708 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200709 }
710 }
711
Chris Forbes5f362d02015-05-25 11:13:22 +1200712 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200713 }
714
715 auto it = outputs.begin();
716 uint32_t attachment = 0;
717
718 /* Walk attachment list and outputs together -- this is a little overpowered since attachments
719 * are currently dense, but the parallel with matching between shader stages is nice.
720 */
721
Chris Forbes8802c992015-05-05 11:34:14 +1200722 while ((outputs.size() > 0 && it != outputs.end()) || attachment < cb->attachmentCount) {
scygan7a62cbe2015-06-01 19:48:11 +0200723 if (attachment == cb->attachmentCount || ( it != outputs.end() && it->first < attachment)) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200724 sprintf(str, "FS writes to output location %d with no matching attachment", it->first);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600725 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC", str);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200726 it++;
727 }
728 else if (it == outputs.end() || it->first > attachment) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200729 sprintf(str, "Attachment %d not written by FS", attachment);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600730 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", str);
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200731 attachment++;
Chris Forbes5f362d02015-05-25 11:13:22 +1200732 pass = false;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200733 }
734 else {
Chris Forbes4b009002015-05-04 14:20:10 +1200735 unsigned output_type = get_fundamental_type(fs, it->second.type_id);
736 unsigned att_type = get_format_type(cb->pAttachments[attachment].format);
737
738 /* type checking */
739 if (att_type != FORMAT_TYPE_UNDEFINED && output_type != FORMAT_TYPE_UNDEFINED && att_type != output_type) {
740 char fs_type[1024];
741 describe_type(fs_type, fs, it->second.type_id);
742 sprintf(str, "Attachment %d of type `%s` does not match FS output type of `%s`",
743 attachment, string_VkFormat(cb->pAttachments[attachment].format), fs_type);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600744 layerCbMsg(VK_DBG_REPORT_ERROR_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", str);
Chris Forbes5f362d02015-05-25 11:13:22 +1200745 pass = false;
Chris Forbes4b009002015-05-04 14:20:10 +1200746 }
747
Chris Forbes5c75afe2015-04-17 10:13:28 +1200748 /* OK! */
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200749 it++;
750 attachment++;
751 }
752 }
Chris Forbes5f362d02015-05-25 11:13:22 +1200753
754 return pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200755}
756
757
Chris Forbes4453c772015-06-05 15:01:08 +1200758struct shader_stage_attributes {
759 char const * const name;
760 bool arrayed_input;
761};
762
763
764static shader_stage_attributes
765shader_stage_attribs[VK_SHADER_STAGE_FRAGMENT + 1] = {
766 { "vertex shader", false },
767 { "tessellation control shader", true },
768 { "tessellation evaluation shader", false },
769 { "geometry shader", true },
770 { "fragment shader", false },
771};
772
773
Chris Forbesf1060ca2015-06-04 20:23:00 +1200774static bool
775validate_graphics_pipeline(VkGraphicsPipelineCreateInfo const *pCreateInfo)
Chris Forbes60540932015-04-08 10:15:35 +1200776{
Chris Forbes8f600932015-04-08 10:16:45 +1200777 /* We seem to allow pipeline stages to be specified out of order, so collect and identify them
778 * before trying to do anything more: */
779
Chris Forbes4453c772015-06-05 15:01:08 +1200780 shader_source const *shaders[VK_SHADER_STAGE_FRAGMENT + 1]; /* exclude CS */
781 memset(shaders, 0, sizeof(shaders));
Chris Forbes8f600932015-04-08 10:16:45 +1200782 VkPipelineCbStateCreateInfo const *cb = 0;
783 VkPipelineVertexInputCreateInfo const *vi = 0;
Chris Forbes5c75afe2015-04-17 10:13:28 +1200784 char str[1024];
Chris Forbes5f362d02015-05-25 11:13:22 +1200785 bool pass = true;
Chris Forbes8f600932015-04-08 10:16:45 +1200786
Chris Forbes1ed0f982015-05-29 14:55:18 +1200787 loader_platform_thread_lock_mutex(&globalLock);
788
Chris Forbes8f600932015-04-08 10:16:45 +1200789 for (auto stage = pCreateInfo; stage; stage = (decltype(stage))stage->pNext) {
790 if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO) {
791 auto shader_stage = (VkPipelineShaderStageCreateInfo const *)stage;
792
Chris Forbes4453c772015-06-05 15:01:08 +1200793 if (shader_stage->shader.stage < VK_SHADER_STAGE_VERTEX || shader_stage->shader.stage > VK_SHADER_STAGE_FRAGMENT) {
Chris Forbes5c75afe2015-04-17 10:13:28 +1200794 sprintf(str, "Unknown shader stage %d\n", shader_stage->shader.stage);
Courtney Goeltzenleuchter1c7c65d2015-06-10 17:39:03 -0600795 layerCbMsg(VK_DBG_REPORT_WARN_BIT, (VkObjectType) 0, NULL, 0, SHADER_CHECKER_UNKNOWN_STAGE, "SC", str);
Chris Forbes5c75afe2015-04-17 10:13:28 +1200796 }
Chris Forbes4453c772015-06-05 15:01:08 +1200797 else {
798 shaders[shader_stage->shader.stage] = shader_map[(void *)(shader_stage->shader.shader)];
799 }
Chris Forbes8f600932015-04-08 10:16:45 +1200800 }
801 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_CB_STATE_CREATE_INFO) {
802 cb = (VkPipelineCbStateCreateInfo const *)stage;
803 }
804 else if (stage->sType == VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_CREATE_INFO) {
805 vi = (VkPipelineVertexInputCreateInfo const *)stage;
806 }
807 }
808
Chris Forbes0bf8fe12015-06-12 11:16:41 +1200809 if (vi) {
810 pass = validate_vi_consistency(vi) && pass;
811 }
812
Chris Forbes4453c772015-06-05 15:01:08 +1200813 if (shaders[VK_SHADER_STAGE_VERTEX] && shaders[VK_SHADER_STAGE_VERTEX]->is_spirv) {
814 pass = validate_vi_against_vs_inputs(vi, shaders[VK_SHADER_STAGE_VERTEX]) && pass;
Chris Forbesfcd05f12015-04-08 10:36:37 +1200815 }
816
Chris Forbes4453c772015-06-05 15:01:08 +1200817 /* TODO: enforce rules about present combinations of shaders */
818 int producer = VK_SHADER_STAGE_VERTEX;
819 int consumer = VK_SHADER_STAGE_GEOMETRY;
820
821 while (!shaders[producer] && producer != VK_SHADER_STAGE_FRAGMENT) {
822 producer++;
823 consumer++;
Chris Forbesbb164b62015-04-08 10:19:16 +1200824 }
825
Tony Barbour4eb3cd12015-06-11 15:04:25 -0600826 for (; producer != VK_SHADER_STAGE_FRAGMENT && consumer <= VK_SHADER_STAGE_FRAGMENT; consumer++) {
Chris Forbes4453c772015-06-05 15:01:08 +1200827 assert(shaders[producer]);
828 if (shaders[consumer]) {
829 if (shaders[producer]->is_spirv && shaders[consumer]->is_spirv) {
830 pass = validate_interface_between_stages(shaders[producer], shader_stage_attribs[producer].name,
831 shaders[consumer], shader_stage_attribs[consumer].name,
832 shader_stage_attribs[consumer].arrayed_input) && pass;
833 }
834
835 producer = consumer;
836 }
837 }
838
839 if (shaders[VK_SHADER_STAGE_FRAGMENT] && shaders[VK_SHADER_STAGE_FRAGMENT]->is_spirv && cb) {
840 pass = validate_fs_outputs_against_cb(shaders[VK_SHADER_STAGE_FRAGMENT], cb) && pass;
Chris Forbes9b9f5fe2015-04-08 10:37:20 +1200841 }
842
Chris Forbes1ed0f982015-05-29 14:55:18 +1200843 loader_platform_thread_unlock_mutex(&globalLock);
Chris Forbesf1060ca2015-06-04 20:23:00 +1200844 return pass;
845}
846
847
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200848VK_LAYER_EXPORT VkResult VKAPI
849vkCreateGraphicsPipeline(VkDevice device,
850 const VkGraphicsPipelineCreateInfo *pCreateInfo,
851 VkPipeline *pPipeline)
Chris Forbesf1060ca2015-06-04 20:23:00 +1200852{
853 bool pass = validate_graphics_pipeline(pCreateInfo);
Chris Forbes5f362d02015-05-25 11:13:22 +1200854
855 if (pass) {
856 /* The driver is allowed to crash if passed junk. Only actually create the
857 * pipeline if we didn't run into any showstoppers above.
858 */
Jon Ashburn5a10d212015-06-01 10:02:09 -0600859 return device_dispatch_table(device)->CreateGraphicsPipeline(device, pCreateInfo, pPipeline);
Chris Forbes5f362d02015-05-25 11:13:22 +1200860 }
861 else {
862 return VK_ERROR_UNKNOWN;
863 }
Chris Forbes60540932015-04-08 10:15:35 +1200864}
865
866
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200867VK_LAYER_EXPORT VkResult VKAPI
868vkCreateGraphicsPipelineDerivative(VkDevice device,
869 const VkGraphicsPipelineCreateInfo *pCreateInfo,
870 VkPipeline basePipeline,
871 VkPipeline *pPipeline)
872{
873 bool pass = validate_graphics_pipeline(pCreateInfo);
874
875 if (pass) {
876 /* The driver is allowed to crash if passed junk. Only actually create the
877 * pipeline if we didn't run into any showstoppers above.
878 */
Jon Ashburn5a10d212015-06-01 10:02:09 -0600879 return device_dispatch_table(device)->CreateGraphicsPipelineDerivative(device, pCreateInfo, basePipeline, pPipeline);
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200880 }
881 else {
882 return VK_ERROR_UNKNOWN;
883 }
884}
885
886
Jon Ashburn17f37372015-05-19 16:34:53 -0600887/* hook DextroyDevice to remove tableMap entry */
888VK_LAYER_EXPORT VkResult VKAPI vkDestroyDevice(VkDevice device)
889{
Jon Ashburn5a10d212015-06-01 10:02:09 -0600890 VkLayerDispatchTable *pDisp = *(VkLayerDispatchTable **) device;
891 VkResult res = device_dispatch_table(device)->DestroyDevice(device);
892 tableMap.erase(pDisp);
Jon Ashburn17f37372015-05-19 16:34:53 -0600893 return res;
894}
895
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -0600896VkResult VKAPI vkCreateInstance(
897 const VkInstanceCreateInfo* pCreateInfo,
898 VkInstance* pInstance)
899{
900
901 loader_platform_thread_once(&g_initOnce, initLayer);
902 /*
903 * For layers, the pInstance has already been filled out
904 * by the loader so that dispatch table is available.
905 */
Jon Ashburn5a10d212015-06-01 10:02:09 -0600906 VkLayerInstanceDispatchTable *pTable = initInstanceTable((const VkBaseLayerObject *) (*pInstance));
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -0600907
908 VkResult result = pTable->CreateInstance(pCreateInfo, pInstance);
909
910 if (result == VK_SUCCESS) {
911 enable_debug_report(pCreateInfo->extensionCount, pCreateInfo->pEnabledExtensions);
Courtney Goeltzenleuchterf4a2eba2015-06-08 14:58:39 -0600912
913 debug_report_init_instance_extension_dispatch_table(
914 pTable,
915 pTable->GetInstanceProcAddr,
916 *pInstance);
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -0600917 }
918 return result;
919}
920
Jon Ashburn17f37372015-05-19 16:34:53 -0600921/* hook DestroyInstance to remove tableInstanceMap entry */
922VK_LAYER_EXPORT VkResult VKAPI vkDestroyInstance(VkInstance instance)
923{
Jon Ashburn5a10d212015-06-01 10:02:09 -0600924 VkLayerInstanceDispatchTable *pDisp = *(VkLayerInstanceDispatchTable **) instance;
925 VkResult res = instance_dispatch_table(instance)->DestroyInstance(instance);
926 tableInstanceMap.erase(pDisp);
Jon Ashburn17f37372015-05-19 16:34:53 -0600927 return res;
928}
Chris Forbesb65ba352015-05-25 11:12:59 +1200929
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -0600930VK_LAYER_EXPORT VkResult VKAPI vkDbgCreateMsgCallback(
931 VkInstance instance,
932 VkFlags msgFlags,
933 const PFN_vkDbgMsgCallback pfnMsgCallback,
934 void* pUserData,
935 VkDbgMsgCallback* pMsgCallback)
936{
937 VkLayerInstanceDispatchTable *pTable = tableInstanceMap[(VkBaseLayerObject *)instance];
938 return layer_create_msg_callback(instance, pTable, msgFlags, pfnMsgCallback, pUserData, pMsgCallback);
939}
940
941VK_LAYER_EXPORT VkResult VKAPI vkDbgDestroyMsgCallback(
942 VkInstance instance,
943 VkDbgMsgCallback msgCallback)
944{
945 VkLayerInstanceDispatchTable *pTable = tableInstanceMap[(VkBaseLayerObject *)instance];
946 return layer_destroy_msg_callback(instance, pTable, msgCallback);
947}
948
Jon Ashburn1245cec2015-05-18 13:20:15 -0600949VK_LAYER_EXPORT void * VKAPI vkGetDeviceProcAddr(VkDevice device, const char* pName)
Chris Forbesaab9d112015-04-02 13:22:31 +1300950{
Jon Ashburn1245cec2015-05-18 13:20:15 -0600951 if (device == NULL)
Chris Forbesaab9d112015-04-02 13:22:31 +1300952 return NULL;
953
Chris Forbes1b466bd2015-04-15 06:59:41 +1200954 loader_platform_thread_once(&g_initOnce, initLayer);
955
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600956 /* loader uses this to force layer initialization; device object is wrapped */
957 if (!strcmp("vkGetDeviceProcAddr", pName)) {
Jon Ashburn5a10d212015-06-01 10:02:09 -0600958 initDeviceTable((const VkBaseLayerObject *) device);
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600959 return (void *) vkGetDeviceProcAddr;
960 }
961
Chris Forbesaab9d112015-04-02 13:22:31 +1300962#define ADD_HOOK(fn) \
963 if (!strncmp(#fn, pName, sizeof(#fn))) \
964 return (void *) fn
965
Chris Forbesaab9d112015-04-02 13:22:31 +1300966 ADD_HOOK(vkCreateShader);
Jon Ashburn17f37372015-05-19 16:34:53 -0600967 ADD_HOOK(vkDestroyDevice);
Chris Forbes60540932015-04-08 10:15:35 +1200968 ADD_HOOK(vkCreateGraphicsPipeline);
Chris Forbesd0f7f7c2015-06-04 20:27:09 +1200969 ADD_HOOK(vkCreateGraphicsPipelineDerivative);
Jon Ashburn8198fd02015-05-18 09:08:41 -0600970#undef ADD_HOOK
Jon Ashburn5a10d212015-06-01 10:02:09 -0600971 VkLayerDispatchTable* pTable = device_dispatch_table(device);
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600972 if (pTable->GetDeviceProcAddr == NULL)
Chris Forbesaab9d112015-04-02 13:22:31 +1300973 return NULL;
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600974 return pTable->GetDeviceProcAddr(device, pName);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600975}
976
977VK_LAYER_EXPORT void * VKAPI vkGetInstanceProcAddr(VkInstance inst, const char* pName)
978{
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -0600979 void *fptr;
980
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600981 if (inst == NULL)
982 return NULL;
983
Jon Ashburnd9564002015-05-07 10:27:37 -0600984 loader_platform_thread_once(&g_initOnce, initLayer);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600985
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600986 if (!strcmp("vkGetInstanceProcAddr", pName)) {
Jon Ashburn5a10d212015-06-01 10:02:09 -0600987 initInstanceTable((const VkBaseLayerObject *) inst);
Jon Ashburn4f2575f2015-05-28 16:25:02 -0600988 return (void *) vkGetInstanceProcAddr;
989 }
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600990#define ADD_HOOK(fn) \
991 if (!strncmp(#fn, pName, sizeof(#fn))) \
992 return (void *) fn
993
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -0600994 ADD_HOOK(vkCreateInstance);
Jon Ashburn17f37372015-05-19 16:34:53 -0600995 ADD_HOOK(vkDestroyInstance);
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600996 ADD_HOOK(vkGetGlobalExtensionInfo);
Jon Ashburn8198fd02015-05-18 09:08:41 -0600997#undef ADD_HOOK
Jon Ashburn79b78ac2015-05-05 14:22:52 -0600998
Courtney Goeltzenleuchter6c813dc2015-06-01 14:46:33 -0600999 fptr = msg_callback_get_proc_addr(pName);
1000 if (fptr)
1001 return fptr;
1002
Jon Ashburn5a10d212015-06-01 10:02:09 -06001003 VkLayerInstanceDispatchTable* pTable = instance_dispatch_table(inst);
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001004 if (pTable->GetInstanceProcAddr == NULL)
Jon Ashburn79b78ac2015-05-05 14:22:52 -06001005 return NULL;
Jon Ashburn4f2575f2015-05-28 16:25:02 -06001006 return pTable->GetInstanceProcAddr(inst, pName);
Chris Forbesaab9d112015-04-02 13:22:31 +13001007}