blob: 41885563fa33a90fc5f34bef1f1addb983bf118a [file] [log] [blame]
Chris Forbes47567b72017-06-09 12:09:45 -07001/* Copyright (c) 2015-2017 The Khronos Group Inc.
2 * Copyright (c) 2015-2017 Valve Corporation
3 * Copyright (c) 2015-2017 LunarG, Inc.
4 * Copyright (C) 2015-2017 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Chris Forbes <chrisf@ijw.co.nz>
19 */
20
21#include <cinttypes>
22#include <cassert>
23#include <vector>
24#include <unordered_map>
25#include <string>
26#include <sstream>
27#include <SPIRV/spirv.hpp>
28#include "vk_loader_platform.h"
29#include "vk_enum_string_helper.h"
30#include "vk_layer_table.h"
31#include "vk_layer_data.h"
32#include "vk_layer_extension_utils.h"
33#include "vk_layer_utils.h"
34#include "core_validation.h"
35#include "core_validation_types.h"
36#include "shader_validation.h"
Chris Forbes4ae55b32017-06-09 14:42:56 -070037#include "spirv-tools/libspirv.h"
Chris Forbes9a61e082017-07-24 15:35:29 -070038#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070039
40enum FORMAT_TYPE {
41 FORMAT_TYPE_FLOAT = 1, // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader
42 FORMAT_TYPE_SINT = 2,
43 FORMAT_TYPE_UINT = 4,
44};
45
46typedef std::pair<unsigned, unsigned> location_t;
47
48struct interface_var {
49 uint32_t id;
50 uint32_t type_id;
51 uint32_t offset;
52 bool is_patch;
53 bool is_block_member;
54 bool is_relaxed_precision;
55 // TODO: collect the name, too? Isn't required to be present.
56};
57
58struct shader_stage_attributes {
59 char const *const name;
60 bool arrayed_input;
61 bool arrayed_output;
62};
63
64static shader_stage_attributes shader_stage_attribs[] = {
65 {"vertex shader", false, false}, {"tessellation control shader", true, true}, {"tessellation evaluation shader", true, false},
66 {"geometry shader", true, false}, {"fragment shader", false, false},
67};
68
69// SPIRV utility functions
70void shader_module::build_def_index() {
71 for (auto insn : *this) {
72 switch (insn.opcode()) {
73 // Types
74 case spv::OpTypeVoid:
75 case spv::OpTypeBool:
76 case spv::OpTypeInt:
77 case spv::OpTypeFloat:
78 case spv::OpTypeVector:
79 case spv::OpTypeMatrix:
80 case spv::OpTypeImage:
81 case spv::OpTypeSampler:
82 case spv::OpTypeSampledImage:
83 case spv::OpTypeArray:
84 case spv::OpTypeRuntimeArray:
85 case spv::OpTypeStruct:
86 case spv::OpTypeOpaque:
87 case spv::OpTypePointer:
88 case spv::OpTypeFunction:
89 case spv::OpTypeEvent:
90 case spv::OpTypeDeviceEvent:
91 case spv::OpTypeReserveId:
92 case spv::OpTypeQueue:
93 case spv::OpTypePipe:
94 def_index[insn.word(1)] = insn.offset();
95 break;
96
97 // Fixed constants
98 case spv::OpConstantTrue:
99 case spv::OpConstantFalse:
100 case spv::OpConstant:
101 case spv::OpConstantComposite:
102 case spv::OpConstantSampler:
103 case spv::OpConstantNull:
104 def_index[insn.word(2)] = insn.offset();
105 break;
106
107 // Specialization constants
108 case spv::OpSpecConstantTrue:
109 case spv::OpSpecConstantFalse:
110 case spv::OpSpecConstant:
111 case spv::OpSpecConstantComposite:
112 case spv::OpSpecConstantOp:
113 def_index[insn.word(2)] = insn.offset();
114 break;
115
116 // Variables
117 case spv::OpVariable:
118 def_index[insn.word(2)] = insn.offset();
119 break;
120
121 // Functions
122 case spv::OpFunction:
123 def_index[insn.word(2)] = insn.offset();
124 break;
125
126 default:
127 // We don't care about any other defs for now.
128 break;
129 }
130 }
131}
132
133static spirv_inst_iter find_entrypoint(shader_module const *src, char const *name, VkShaderStageFlagBits stageBits) {
134 for (auto insn : *src) {
135 if (insn.opcode() == spv::OpEntryPoint) {
136 auto entrypointName = (char const *)&insn.word(3);
137 auto entrypointStageBits = 1u << insn.word(1);
138
139 if (!strcmp(entrypointName, name) && (entrypointStageBits & stageBits)) {
140 return insn;
141 }
142 }
143 }
144
145 return src->end();
146}
147
148static char const *storage_class_name(unsigned sc) {
149 switch (sc) {
150 case spv::StorageClassInput:
151 return "input";
152 case spv::StorageClassOutput:
153 return "output";
154 case spv::StorageClassUniformConstant:
155 return "const uniform";
156 case spv::StorageClassUniform:
157 return "uniform";
158 case spv::StorageClassWorkgroup:
159 return "workgroup local";
160 case spv::StorageClassCrossWorkgroup:
161 return "workgroup global";
162 case spv::StorageClassPrivate:
163 return "private global";
164 case spv::StorageClassFunction:
165 return "function";
166 case spv::StorageClassGeneric:
167 return "generic";
168 case spv::StorageClassAtomicCounter:
169 return "atomic counter";
170 case spv::StorageClassImage:
171 return "image";
172 case spv::StorageClassPushConstant:
173 return "push constant";
174 default:
175 return "unknown";
176 }
177}
178
179// Get the value of an integral constant
180unsigned get_constant_value(shader_module const *src, unsigned id) {
181 auto value = src->get_def(id);
182 assert(value != src->end());
183
184 if (value.opcode() != spv::OpConstant) {
185 // TODO: Either ensure that the specialization transform is already performed on a module we're
186 // considering here, OR -- specialize on the fly now.
187 return 1;
188 }
189
190 return value.word(3);
191}
192
193static void describe_type_inner(std::ostringstream &ss, shader_module const *src, unsigned type) {
194 auto insn = src->get_def(type);
195 assert(insn != src->end());
196
197 switch (insn.opcode()) {
198 case spv::OpTypeBool:
199 ss << "bool";
200 break;
201 case spv::OpTypeInt:
202 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
203 break;
204 case spv::OpTypeFloat:
205 ss << "float" << insn.word(2);
206 break;
207 case spv::OpTypeVector:
208 ss << "vec" << insn.word(3) << " of ";
209 describe_type_inner(ss, src, insn.word(2));
210 break;
211 case spv::OpTypeMatrix:
212 ss << "mat" << insn.word(3) << " of ";
213 describe_type_inner(ss, src, insn.word(2));
214 break;
215 case spv::OpTypeArray:
216 ss << "arr[" << get_constant_value(src, insn.word(3)) << "] of ";
217 describe_type_inner(ss, src, insn.word(2));
218 break;
219 case spv::OpTypePointer:
220 ss << "ptr to " << storage_class_name(insn.word(2)) << " ";
221 describe_type_inner(ss, src, insn.word(3));
222 break;
223 case spv::OpTypeStruct: {
224 ss << "struct of (";
225 for (unsigned i = 2; i < insn.len(); i++) {
226 describe_type_inner(ss, src, insn.word(i));
227 if (i == insn.len() - 1) {
228 ss << ")";
229 } else {
230 ss << ", ";
231 }
232 }
233 break;
234 }
235 case spv::OpTypeSampler:
236 ss << "sampler";
237 break;
238 case spv::OpTypeSampledImage:
239 ss << "sampler+";
240 describe_type_inner(ss, src, insn.word(2));
241 break;
242 case spv::OpTypeImage:
243 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
244 break;
245 default:
246 ss << "oddtype";
247 break;
248 }
249}
250
251static std::string describe_type(shader_module const *src, unsigned type) {
252 std::ostringstream ss;
253 describe_type_inner(ss, src, type);
254 return ss.str();
255}
256
257static bool is_narrow_numeric_type(spirv_inst_iter type) {
258 if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false;
259 return type.word(2) < 64;
260}
261
262static bool types_match(shader_module const *a, shader_module const *b, unsigned a_type, unsigned b_type, bool a_arrayed,
263 bool b_arrayed, bool relaxed) {
264 // Walk two type trees together, and complain about differences
265 auto a_insn = a->get_def(a_type);
266 auto b_insn = b->get_def(b_type);
267 assert(a_insn != a->end());
268 assert(b_insn != b->end());
269
270 if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) {
271 return types_match(a, b, a_insn.word(2), b_type, false, b_arrayed, relaxed);
272 }
273
274 if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) {
275 // We probably just found the extra level of arrayness in b_type: compare the type inside it to a_type
276 return types_match(a, b, a_type, b_insn.word(2), a_arrayed, false, relaxed);
277 }
278
279 if (a_insn.opcode() == spv::OpTypeVector && relaxed && is_narrow_numeric_type(b_insn)) {
280 return types_match(a, b, a_insn.word(2), b_type, a_arrayed, b_arrayed, false);
281 }
282
283 if (a_insn.opcode() != b_insn.opcode()) {
284 return false;
285 }
286
287 if (a_insn.opcode() == spv::OpTypePointer) {
288 // Match on pointee type. storage class is expected to differ
289 return types_match(a, b, a_insn.word(3), b_insn.word(3), a_arrayed, b_arrayed, relaxed);
290 }
291
292 if (a_arrayed || b_arrayed) {
293 // If we havent resolved array-of-verts by here, we're not going to.
294 return false;
295 }
296
297 switch (a_insn.opcode()) {
298 case spv::OpTypeBool:
299 return true;
300 case spv::OpTypeInt:
301 // Match on width, signedness
302 return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3);
303 case spv::OpTypeFloat:
304 // Match on width
305 return a_insn.word(2) == b_insn.word(2);
306 case spv::OpTypeVector:
307 // Match on element type, count.
308 if (!types_match(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false;
309 if (relaxed && is_narrow_numeric_type(a->get_def(a_insn.word(2)))) {
310 return a_insn.word(3) >= b_insn.word(3);
311 } else {
312 return a_insn.word(3) == b_insn.word(3);
313 }
314 case spv::OpTypeMatrix:
315 // Match on element type, count.
316 return types_match(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
317 a_insn.word(3) == b_insn.word(3);
318 case spv::OpTypeArray:
319 // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from
320 // vector & matrix types in that the array size is the id of a constant instruction, * not a literal within OpTypeArray
321 return types_match(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
322 get_constant_value(a, a_insn.word(3)) == get_constant_value(b, b_insn.word(3));
323 case spv::OpTypeStruct:
324 // Match on all element types
325 {
326 if (a_insn.len() != b_insn.len()) {
327 return false; // Structs cannot match if member counts differ
328 }
329
330 for (unsigned i = 2; i < a_insn.len(); i++) {
331 if (!types_match(a, b, a_insn.word(i), b_insn.word(i), a_arrayed, b_arrayed, false)) {
332 return false;
333 }
334 }
335
336 return true;
337 }
338 default:
339 // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match.
340 return false;
341 }
342}
343
344static unsigned value_or_default(std::unordered_map<unsigned, unsigned> const &map, unsigned id, unsigned def) {
345 auto it = map.find(id);
346 if (it == map.end())
347 return def;
348 else
349 return it->second;
350}
351
352static unsigned get_locations_consumed_by_type(shader_module const *src, unsigned type, bool strip_array_level) {
353 auto insn = src->get_def(type);
354 assert(insn != src->end());
355
356 switch (insn.opcode()) {
357 case spv::OpTypePointer:
358 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
359 // pointers around.
360 return get_locations_consumed_by_type(src, insn.word(3), strip_array_level);
361 case spv::OpTypeArray:
362 if (strip_array_level) {
363 return get_locations_consumed_by_type(src, insn.word(2), false);
364 } else {
365 return get_constant_value(src, insn.word(3)) * get_locations_consumed_by_type(src, insn.word(2), false);
366 }
367 case spv::OpTypeMatrix:
368 // Num locations is the dimension * element size
369 return insn.word(3) * get_locations_consumed_by_type(src, insn.word(2), false);
370 case spv::OpTypeVector: {
371 auto scalar_type = src->get_def(insn.word(2));
372 auto bit_width =
373 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
374
375 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
376 return (bit_width * insn.word(3) + 127) / 128;
377 }
378 default:
379 // Everything else is just 1.
380 return 1;
381
382 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
383 }
384}
385
386static unsigned get_locations_consumed_by_format(VkFormat format) {
387 switch (format) {
388 case VK_FORMAT_R64G64B64A64_SFLOAT:
389 case VK_FORMAT_R64G64B64A64_SINT:
390 case VK_FORMAT_R64G64B64A64_UINT:
391 case VK_FORMAT_R64G64B64_SFLOAT:
392 case VK_FORMAT_R64G64B64_SINT:
393 case VK_FORMAT_R64G64B64_UINT:
394 return 2;
395 default:
396 return 1;
397 }
398}
399
400static unsigned get_format_type(VkFormat fmt) {
401 if (FormatIsSInt(fmt))
402 return FORMAT_TYPE_SINT;
403 if (FormatIsUInt(fmt))
404 return FORMAT_TYPE_UINT;
405 if (FormatIsDepthAndStencil(fmt))
406 return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
407 if (fmt == VK_FORMAT_UNDEFINED)
408 return 0;
409 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
410 return FORMAT_TYPE_FLOAT;
411}
412
413// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
414static unsigned get_fundamental_type(shader_module const *src, unsigned type) {
415 auto insn = src->get_def(type);
416 assert(insn != src->end());
417
418 switch (insn.opcode()) {
419 case spv::OpTypeInt:
420 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
421 case spv::OpTypeFloat:
422 return FORMAT_TYPE_FLOAT;
423 case spv::OpTypeVector:
424 return get_fundamental_type(src, insn.word(2));
425 case spv::OpTypeMatrix:
426 return get_fundamental_type(src, insn.word(2));
427 case spv::OpTypeArray:
428 return get_fundamental_type(src, insn.word(2));
429 case spv::OpTypePointer:
430 return get_fundamental_type(src, insn.word(3));
431 case spv::OpTypeImage:
432 return get_fundamental_type(src, insn.word(2));
433
434 default:
435 return 0;
436 }
437}
438
439static uint32_t get_shader_stage_id(VkShaderStageFlagBits stage) {
440 uint32_t bit_pos = uint32_t(u_ffs(stage));
441 return bit_pos - 1;
442}
443
444static spirv_inst_iter get_struct_type(shader_module const *src, spirv_inst_iter def, bool is_array_of_verts) {
445 while (true) {
446 if (def.opcode() == spv::OpTypePointer) {
447 def = src->get_def(def.word(3));
448 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
449 def = src->get_def(def.word(2));
450 is_array_of_verts = false;
451 } else if (def.opcode() == spv::OpTypeStruct) {
452 return def;
453 } else {
454 return src->end();
455 }
456 }
457}
458
Chris Forbesa313d772017-06-13 13:59:41 -0700459static bool collect_interface_block_members(shader_module const *src, std::map<location_t, interface_var> *out,
Chris Forbes47567b72017-06-09 12:09:45 -0700460 std::unordered_map<unsigned, unsigned> const &blocks, bool is_array_of_verts,
Chris Forbesa313d772017-06-13 13:59:41 -0700461 uint32_t id, uint32_t type_id, bool is_patch, int /*first_location*/) {
Chris Forbes47567b72017-06-09 12:09:45 -0700462 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
463 auto type = get_struct_type(src, src->get_def(type_id), is_array_of_verts && !is_patch);
464 if (type == src->end() || blocks.find(type.word(1)) == blocks.end()) {
465 // This isn't an interface block.
Chris Forbesa313d772017-06-13 13:59:41 -0700466 return false;
Chris Forbes47567b72017-06-09 12:09:45 -0700467 }
468
469 std::unordered_map<unsigned, unsigned> member_components;
470 std::unordered_map<unsigned, unsigned> member_relaxed_precision;
Chris Forbesa313d772017-06-13 13:59:41 -0700471 std::unordered_map<unsigned, unsigned> member_patch;
Chris Forbes47567b72017-06-09 12:09:45 -0700472
473 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
474 for (auto insn : *src) {
475 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
476 unsigned member_index = insn.word(2);
477
478 if (insn.word(3) == spv::DecorationComponent) {
479 unsigned component = insn.word(4);
480 member_components[member_index] = component;
481 }
482
483 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
484 member_relaxed_precision[member_index] = 1;
485 }
Chris Forbesa313d772017-06-13 13:59:41 -0700486
487 if (insn.word(3) == spv::DecorationPatch) {
488 member_patch[member_index] = 1;
489 }
Chris Forbes47567b72017-06-09 12:09:45 -0700490 }
491 }
492
Chris Forbesa313d772017-06-13 13:59:41 -0700493 // TODO: correctly handle location assignment from outside
494
Chris Forbes47567b72017-06-09 12:09:45 -0700495 // Second pass -- produce the output, from Location decorations
496 for (auto insn : *src) {
497 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
498 unsigned member_index = insn.word(2);
499 unsigned member_type_id = type.word(2 + member_index);
500
501 if (insn.word(3) == spv::DecorationLocation) {
502 unsigned location = insn.word(4);
503 unsigned num_locations = get_locations_consumed_by_type(src, member_type_id, false);
504 auto component_it = member_components.find(member_index);
505 unsigned component = component_it == member_components.end() ? 0 : component_it->second;
506 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
Chris Forbesa313d772017-06-13 13:59:41 -0700507 bool member_is_patch = is_patch || member_patch.count(member_index)>0;
Chris Forbes47567b72017-06-09 12:09:45 -0700508
509 for (unsigned int offset = 0; offset < num_locations; offset++) {
510 interface_var v = {};
511 v.id = id;
512 // TODO: member index in interface_var too?
513 v.type_id = member_type_id;
514 v.offset = offset;
Chris Forbesa313d772017-06-13 13:59:41 -0700515 v.is_patch = member_is_patch;
Chris Forbes47567b72017-06-09 12:09:45 -0700516 v.is_block_member = true;
517 v.is_relaxed_precision = is_relaxed_precision;
518 (*out)[std::make_pair(location + offset, component)] = v;
519 }
520 }
521 }
522 }
Chris Forbesa313d772017-06-13 13:59:41 -0700523
524 return true;
Chris Forbes47567b72017-06-09 12:09:45 -0700525}
526
527static std::map<location_t, interface_var> collect_interface_by_location(shader_module const *src, spirv_inst_iter entrypoint,
528 spv::StorageClass sinterface, bool is_array_of_verts) {
529 std::unordered_map<unsigned, unsigned> var_locations;
530 std::unordered_map<unsigned, unsigned> var_builtins;
531 std::unordered_map<unsigned, unsigned> var_components;
532 std::unordered_map<unsigned, unsigned> blocks;
533 std::unordered_map<unsigned, unsigned> var_patch;
534 std::unordered_map<unsigned, unsigned> var_relaxed_precision;
535
536 for (auto insn : *src) {
537 // We consider two interface models: SSO rendezvous-by-location, and builtins. Complain about anything that
538 // fits neither model.
539 if (insn.opcode() == spv::OpDecorate) {
540 if (insn.word(2) == spv::DecorationLocation) {
541 var_locations[insn.word(1)] = insn.word(3);
542 }
543
544 if (insn.word(2) == spv::DecorationBuiltIn) {
545 var_builtins[insn.word(1)] = insn.word(3);
546 }
547
548 if (insn.word(2) == spv::DecorationComponent) {
549 var_components[insn.word(1)] = insn.word(3);
550 }
551
552 if (insn.word(2) == spv::DecorationBlock) {
553 blocks[insn.word(1)] = 1;
554 }
555
556 if (insn.word(2) == spv::DecorationPatch) {
557 var_patch[insn.word(1)] = 1;
558 }
559
560 if (insn.word(2) == spv::DecorationRelaxedPrecision) {
561 var_relaxed_precision[insn.word(1)] = 1;
562 }
563 }
564 }
565
566 // TODO: handle grouped decorations
567 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
568
569 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
570 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
571 uint32_t word = 3;
572 while (entrypoint.word(word) & 0xff000000u) {
573 ++word;
574 }
575 ++word;
576
577 std::map<location_t, interface_var> out;
578
579 for (; word < entrypoint.len(); word++) {
580 auto insn = src->get_def(entrypoint.word(word));
581 assert(insn != src->end());
582 assert(insn.opcode() == spv::OpVariable);
583
584 if (insn.word(3) == static_cast<uint32_t>(sinterface)) {
585 unsigned id = insn.word(2);
586 unsigned type = insn.word(1);
587
Jamie Madill061d1112017-11-08 16:25:22 -0500588 int location = value_or_default(var_locations, id, static_cast<unsigned>(-1));
589 int builtin = value_or_default(var_builtins, id, static_cast<unsigned>(-1));
Chris Forbes47567b72017-06-09 12:09:45 -0700590 unsigned component = value_or_default(var_components, id, 0); // Unspecified is OK, is 0
591 bool is_patch = var_patch.find(id) != var_patch.end();
592 bool is_relaxed_precision = var_relaxed_precision.find(id) != var_relaxed_precision.end();
593
Chris Forbesa313d772017-06-13 13:59:41 -0700594 if (builtin != -1) continue;
595 else if (!collect_interface_block_members(src, &out, blocks, is_array_of_verts, id, type, is_patch, location)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700596 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
597 // one result for each.
598 unsigned num_locations = get_locations_consumed_by_type(src, type, is_array_of_verts && !is_patch);
599 for (unsigned int offset = 0; offset < num_locations; offset++) {
600 interface_var v = {};
601 v.id = id;
602 v.type_id = type;
603 v.offset = offset;
604 v.is_patch = is_patch;
605 v.is_relaxed_precision = is_relaxed_precision;
606 out[std::make_pair(location + offset, component)] = v;
607 }
Chris Forbes47567b72017-06-09 12:09:45 -0700608 }
609 }
610 }
611
612 return out;
613}
614
615static std::vector<std::pair<uint32_t, interface_var>> collect_interface_by_input_attachment_index(
616 shader_module const *src, std::unordered_set<uint32_t> const &accessible_ids) {
617 std::vector<std::pair<uint32_t, interface_var>> out;
618
619 for (auto insn : *src) {
620 if (insn.opcode() == spv::OpDecorate) {
621 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
622 auto attachment_index = insn.word(3);
623 auto id = insn.word(1);
624
625 if (accessible_ids.count(id)) {
626 auto def = src->get_def(id);
627 assert(def != src->end());
628
629 if (def.opcode() == spv::OpVariable && insn.word(3) == spv::StorageClassUniformConstant) {
630 auto num_locations = get_locations_consumed_by_type(src, def.word(1), false);
631 for (unsigned int offset = 0; offset < num_locations; offset++) {
632 interface_var v = {};
633 v.id = id;
634 v.type_id = def.word(1);
635 v.offset = offset;
636 out.emplace_back(attachment_index + offset, v);
637 }
638 }
639 }
640 }
641 }
642 }
643
644 return out;
645}
646
647static std::vector<std::pair<descriptor_slot_t, interface_var>> collect_interface_by_descriptor_slot(
648 debug_report_data const *report_data, shader_module const *src, std::unordered_set<uint32_t> const &accessible_ids) {
649 std::unordered_map<unsigned, unsigned> var_sets;
650 std::unordered_map<unsigned, unsigned> var_bindings;
651
652 for (auto insn : *src) {
653 // All variables in the Uniform or UniformConstant storage classes are required to be decorated with both
654 // DecorationDescriptorSet and DecorationBinding.
655 if (insn.opcode() == spv::OpDecorate) {
656 if (insn.word(2) == spv::DecorationDescriptorSet) {
657 var_sets[insn.word(1)] = insn.word(3);
658 }
659
660 if (insn.word(2) == spv::DecorationBinding) {
661 var_bindings[insn.word(1)] = insn.word(3);
662 }
663 }
664 }
665
666 std::vector<std::pair<descriptor_slot_t, interface_var>> out;
667
668 for (auto id : accessible_ids) {
669 auto insn = src->get_def(id);
670 assert(insn != src->end());
671
672 if (insn.opcode() == spv::OpVariable &&
673 (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant)) {
674 unsigned set = value_or_default(var_sets, insn.word(2), 0);
675 unsigned binding = value_or_default(var_bindings, insn.word(2), 0);
676
677 interface_var v = {};
678 v.id = insn.word(2);
679 v.type_id = insn.word(1);
680 out.emplace_back(std::make_pair(set, binding), v);
681 }
682 }
683
684 return out;
685}
686
687
688
689static bool validate_vi_consistency(debug_report_data const *report_data, VkPipelineVertexInputStateCreateInfo const *vi) {
690 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
691 // be specified only once.
692 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
693 bool skip = false;
694
695 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
696 auto desc = &vi->pVertexBindingDescriptions[i];
697 auto &binding = bindings[desc->binding];
698 if (binding) {
699 // TODO: VALIDATION_ERROR_096005cc perhaps?
700 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
701 SHADER_CHECKER_INCONSISTENT_VI, "SC", "Duplicate vertex input binding descriptions for binding %d",
702 desc->binding);
703 } else {
704 binding = desc;
705 }
706 }
707
708 return skip;
709}
710
711static bool validate_vi_against_vs_inputs(debug_report_data const *report_data, VkPipelineVertexInputStateCreateInfo const *vi,
712 shader_module const *vs, spirv_inst_iter entrypoint) {
713 bool skip = false;
714
715 auto inputs = collect_interface_by_location(vs, entrypoint, spv::StorageClassInput, false);
716
717 // Build index by location
718 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
719 if (vi) {
720 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++) {
721 auto num_locations = get_locations_consumed_by_format(vi->pVertexAttributeDescriptions[i].format);
722 for (auto j = 0u; j < num_locations; j++) {
723 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
724 }
725 }
726 }
727
728 auto it_a = attribs.begin();
729 auto it_b = inputs.begin();
730 bool used = false;
731
732 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
733 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
734 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
735 auto a_first = a_at_end ? 0 : it_a->first;
736 auto b_first = b_at_end ? 0 : it_b->first.first;
737 if (!a_at_end && (b_at_end || a_first < b_first)) {
738 if (!used && log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
739 0, __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
740 "Vertex attribute at location %d not consumed by vertex shader", a_first)) {
741 skip = true;
742 }
743 used = false;
744 it_a++;
745 } else if (!b_at_end && (a_at_end || b_first < a_first)) {
746 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, __LINE__,
747 SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", "Vertex shader consumes input at location %d but not provided",
748 b_first);
749 it_b++;
750 } else {
751 unsigned attrib_type = get_format_type(it_a->second->format);
752 unsigned input_type = get_fundamental_type(vs, it_b->second.type_id);
753
754 // Type checking
755 if (!(attrib_type & input_type)) {
756 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
757 SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
758 "Attribute type of `%s` at location %d does not match vertex shader input type of `%s`",
759 string_VkFormat(it_a->second->format), a_first, describe_type(vs, it_b->second.type_id).c_str());
760 }
761
762 // OK!
763 used = true;
764 it_b++;
765 }
766 }
767
768 return skip;
769}
770
771static bool validate_fs_outputs_against_render_pass(debug_report_data const *report_data, shader_module const *fs,
Chris Forbesa400a8a2017-07-20 13:10:24 -0700772 spirv_inst_iter entrypoint, PIPELINE_STATE const *pipeline,
Chris Forbes47567b72017-06-09 12:09:45 -0700773 uint32_t subpass_index) {
Petr Krause91f7a12017-12-14 20:57:36 +0100774 auto rpci = pipeline->rp_state->createInfo.ptr();
Chris Forbes8bca1652017-07-20 11:10:09 -0700775
Chris Forbes47567b72017-06-09 12:09:45 -0700776 std::map<uint32_t, VkFormat> color_attachments;
777 auto subpass = rpci->pSubpasses[subpass_index];
778 for (auto i = 0u; i < subpass.colorAttachmentCount; ++i) {
779 uint32_t attachment = subpass.pColorAttachments[i].attachment;
780 if (attachment == VK_ATTACHMENT_UNUSED) continue;
781 if (rpci->pAttachments[attachment].format != VK_FORMAT_UNDEFINED) {
782 color_attachments[i] = rpci->pAttachments[attachment].format;
783 }
784 }
785
786 bool skip = false;
787
788 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
789
790 auto outputs = collect_interface_by_location(fs, entrypoint, spv::StorageClassOutput, false);
791
792 auto it_a = outputs.begin();
793 auto it_b = color_attachments.begin();
794
795 // Walk attachment list and outputs together
796
797 while ((outputs.size() > 0 && it_a != outputs.end()) || (color_attachments.size() > 0 && it_b != color_attachments.end())) {
798 bool a_at_end = outputs.size() == 0 || it_a == outputs.end();
799 bool b_at_end = color_attachments.size() == 0 || it_b == color_attachments.end();
800
801 if (!a_at_end && (b_at_end || it_a->first.first < it_b->first)) {
802 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
803 SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
804 "fragment shader writes to output location %d with no matching attachment", it_a->first.first);
805 it_a++;
806 } else if (!b_at_end && (a_at_end || it_a->first.first > it_b->first)) {
Chris Forbesefdd4082017-07-20 11:19:16 -0700807 // Only complain if there are unmasked channels for this attachment. If the writemask is 0, it's acceptable for the
808 // shader to not produce a matching output.
Chris Forbesa400a8a2017-07-20 13:10:24 -0700809 if (pipeline->attachments[it_b->first].colorWriteMask != 0) {
Chris Forbesefdd4082017-07-20 11:19:16 -0700810 skip |=
811 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
Chris Forbes47567b72017-06-09 12:09:45 -0700812 SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", "Attachment %d not written by fragment shader", it_b->first);
Chris Forbesefdd4082017-07-20 11:19:16 -0700813 }
Chris Forbes47567b72017-06-09 12:09:45 -0700814 it_b++;
815 } else {
816 unsigned output_type = get_fundamental_type(fs, it_a->second.type_id);
817 unsigned att_type = get_format_type(it_b->second);
818
819 // Type checking
820 if (!(output_type & att_type)) {
821 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
822 SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
823 "Attachment %d of type `%s` does not match fragment shader output type of `%s`", it_b->first,
824 string_VkFormat(it_b->second), describe_type(fs, it_a->second.type_id).c_str());
825 }
826
827 // OK!
828 it_a++;
829 it_b++;
830 }
831 }
832
833 return skip;
834}
835
836// For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is
837// important for identifying the set of shader resources actually used by an entrypoint, for example.
838// Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
839// - NOT the shader input/output interfaces.
840//
841// TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
842// converting parts of this to be generated from the machine-readable spec instead.
843static std::unordered_set<uint32_t> mark_accessible_ids(shader_module const *src, spirv_inst_iter entrypoint) {
844 std::unordered_set<uint32_t> ids;
845 std::unordered_set<uint32_t> worklist;
846 worklist.insert(entrypoint.word(2));
847
848 while (!worklist.empty()) {
849 auto id_iter = worklist.begin();
850 auto id = *id_iter;
851 worklist.erase(id_iter);
852
853 auto insn = src->get_def(id);
854 if (insn == src->end()) {
855 // ID is something we didn't collect in build_def_index. that's OK -- we'll stumble across all kinds of things here
856 // that we may not care about.
857 continue;
858 }
859
860 // Try to add to the output set
861 if (!ids.insert(id).second) {
862 continue; // If we already saw this id, we don't want to walk it again.
863 }
864
865 switch (insn.opcode()) {
866 case spv::OpFunction:
867 // Scan whole body of the function, enlisting anything interesting
868 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
869 switch (insn.opcode()) {
870 case spv::OpLoad:
871 case spv::OpAtomicLoad:
872 case spv::OpAtomicExchange:
873 case spv::OpAtomicCompareExchange:
874 case spv::OpAtomicCompareExchangeWeak:
875 case spv::OpAtomicIIncrement:
876 case spv::OpAtomicIDecrement:
877 case spv::OpAtomicIAdd:
878 case spv::OpAtomicISub:
879 case spv::OpAtomicSMin:
880 case spv::OpAtomicUMin:
881 case spv::OpAtomicSMax:
882 case spv::OpAtomicUMax:
883 case spv::OpAtomicAnd:
884 case spv::OpAtomicOr:
885 case spv::OpAtomicXor:
886 worklist.insert(insn.word(3)); // ptr
887 break;
888 case spv::OpStore:
889 case spv::OpAtomicStore:
890 worklist.insert(insn.word(1)); // ptr
891 break;
892 case spv::OpAccessChain:
893 case spv::OpInBoundsAccessChain:
894 worklist.insert(insn.word(3)); // base ptr
895 break;
896 case spv::OpSampledImage:
897 case spv::OpImageSampleImplicitLod:
898 case spv::OpImageSampleExplicitLod:
899 case spv::OpImageSampleDrefImplicitLod:
900 case spv::OpImageSampleDrefExplicitLod:
901 case spv::OpImageSampleProjImplicitLod:
902 case spv::OpImageSampleProjExplicitLod:
903 case spv::OpImageSampleProjDrefImplicitLod:
904 case spv::OpImageSampleProjDrefExplicitLod:
905 case spv::OpImageFetch:
906 case spv::OpImageGather:
907 case spv::OpImageDrefGather:
908 case spv::OpImageRead:
909 case spv::OpImage:
910 case spv::OpImageQueryFormat:
911 case spv::OpImageQueryOrder:
912 case spv::OpImageQuerySizeLod:
913 case spv::OpImageQuerySize:
914 case spv::OpImageQueryLod:
915 case spv::OpImageQueryLevels:
916 case spv::OpImageQuerySamples:
917 case spv::OpImageSparseSampleImplicitLod:
918 case spv::OpImageSparseSampleExplicitLod:
919 case spv::OpImageSparseSampleDrefImplicitLod:
920 case spv::OpImageSparseSampleDrefExplicitLod:
921 case spv::OpImageSparseSampleProjImplicitLod:
922 case spv::OpImageSparseSampleProjExplicitLod:
923 case spv::OpImageSparseSampleProjDrefImplicitLod:
924 case spv::OpImageSparseSampleProjDrefExplicitLod:
925 case spv::OpImageSparseFetch:
926 case spv::OpImageSparseGather:
927 case spv::OpImageSparseDrefGather:
928 case spv::OpImageTexelPointer:
929 worklist.insert(insn.word(3)); // Image or sampled image
930 break;
931 case spv::OpImageWrite:
932 worklist.insert(insn.word(1)); // Image -- different operand order to above
933 break;
934 case spv::OpFunctionCall:
935 for (uint32_t i = 3; i < insn.len(); i++) {
936 worklist.insert(insn.word(i)); // fn itself, and all args
937 }
938 break;
939
940 case spv::OpExtInst:
941 for (uint32_t i = 5; i < insn.len(); i++) {
942 worklist.insert(insn.word(i)); // Operands to ext inst
943 }
944 break;
945 }
946 }
947 break;
948 }
949 }
950
951 return ids;
952}
953
954static bool validate_push_constant_block_against_pipeline(debug_report_data const *report_data,
955 std::vector<VkPushConstantRange> const *push_constant_ranges,
956 shader_module const *src, spirv_inst_iter type,
957 VkShaderStageFlagBits stage) {
958 bool skip = false;
959
960 // Strip off ptrs etc
961 type = get_struct_type(src, type, false);
962 assert(type != src->end());
963
964 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
965 // TODO: arrays, matrices, weird sizes
966 for (auto insn : *src) {
967 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
968 if (insn.word(3) == spv::DecorationOffset) {
969 unsigned offset = insn.word(4);
970 auto size = 4; // Bytes; TODO: calculate this based on the type
971
972 bool found_range = false;
973 for (auto const &range : *push_constant_ranges) {
974 if (range.offset <= offset && range.offset + range.size >= offset + size) {
975 found_range = true;
976
977 if ((range.stageFlags & stage) == 0) {
978 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
979 __LINE__, SHADER_CHECKER_PUSH_CONSTANT_NOT_ACCESSIBLE_FROM_STAGE, "SC",
980 "Push constant range covering variable starting at "
981 "offset %u not accessible from stage %s",
982 offset, string_VkShaderStageFlagBits(stage));
983 }
984
985 break;
986 }
987 }
988
989 if (!found_range) {
990 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
991 __LINE__, SHADER_CHECKER_PUSH_CONSTANT_OUT_OF_RANGE, "SC",
992 "Push constant range covering variable starting at "
993 "offset %u not declared in layout",
994 offset);
995 }
996 }
997 }
998 }
999
1000 return skip;
1001}
1002
1003static bool validate_push_constant_usage(debug_report_data const *report_data,
1004 std::vector<VkPushConstantRange> const *push_constant_ranges, shader_module const *src,
1005 std::unordered_set<uint32_t> accessible_ids, VkShaderStageFlagBits stage) {
1006 bool skip = false;
1007
1008 for (auto id : accessible_ids) {
1009 auto def_insn = src->get_def(id);
1010 if (def_insn.opcode() == spv::OpVariable && def_insn.word(3) == spv::StorageClassPushConstant) {
1011 skip |= validate_push_constant_block_against_pipeline(report_data, push_constant_ranges, src,
1012 src->get_def(def_insn.word(1)), stage);
1013 }
1014 }
1015
1016 return skip;
1017}
1018
1019// Validate that data for each specialization entry is fully contained within the buffer.
1020static bool validate_specialization_offsets(debug_report_data const *report_data, VkPipelineShaderStageCreateInfo const *info) {
1021 bool skip = false;
1022
1023 VkSpecializationInfo const *spec = info->pSpecializationInfo;
1024
1025 if (spec) {
1026 for (auto i = 0u; i < spec->mapEntryCount; i++) {
1027 // TODO: This is a good place for VALIDATION_ERROR_1360060a.
1028 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
1029 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, __LINE__,
1030 VALIDATION_ERROR_1360060c, "SC",
1031 "Specialization entry %u (for constant id %u) references memory outside provided "
1032 "specialization data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER
1033 " bytes provided). %s.",
1034 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
1035 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize,
1036 validation_error_map[VALIDATION_ERROR_1360060c]);
1037 }
1038 }
1039 }
1040
1041 return skip;
1042}
1043
1044static bool descriptor_type_match(shader_module const *module, uint32_t type_id, VkDescriptorType descriptor_type,
1045 unsigned &descriptor_count) {
1046 auto type = module->get_def(type_id);
1047
1048 descriptor_count = 1;
1049
1050 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
1051 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer) {
1052 if (type.opcode() == spv::OpTypeArray) {
1053 descriptor_count *= get_constant_value(module, type.word(3));
1054 type = module->get_def(type.word(2));
1055 } else {
1056 type = module->get_def(type.word(3));
1057 }
1058 }
1059
1060 switch (type.opcode()) {
1061 case spv::OpTypeStruct: {
1062 for (auto insn : *module) {
1063 if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) {
1064 if (insn.word(2) == spv::DecorationBlock) {
1065 return descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
1066 descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
1067 } else if (insn.word(2) == spv::DecorationBufferBlock) {
1068 return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
1069 descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
1070 }
1071 }
1072 }
1073
1074 // Invalid
1075 return false;
1076 }
1077
1078 case spv::OpTypeSampler:
1079 return descriptor_type == VK_DESCRIPTOR_TYPE_SAMPLER || descriptor_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1080
1081 case spv::OpTypeSampledImage:
1082 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) {
1083 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
1084 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
1085 auto image_type = module->get_def(type.word(2));
1086 auto dim = image_type.word(3);
1087 auto sampled = image_type.word(7);
1088 return dim == spv::DimBuffer && sampled == 1;
1089 }
1090 return descriptor_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1091
1092 case spv::OpTypeImage: {
1093 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
1094 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
1095 auto dim = type.word(3);
1096 auto sampled = type.word(7);
1097
1098 if (dim == spv::DimSubpassData) {
1099 return descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
1100 } else if (dim == spv::DimBuffer) {
1101 if (sampled == 1) {
1102 return descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
1103 } else {
1104 return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
1105 }
1106 } else if (sampled == 1) {
1107 return descriptor_type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE ||
1108 descriptor_type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1109 } else {
1110 return descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
1111 }
1112 }
1113
1114 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
1115 default:
1116 return false; // Mismatch
1117 }
1118}
1119
1120static bool require_feature(debug_report_data const *report_data, VkBool32 feature, char const *feature_name) {
1121 if (!feature) {
1122 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1123 SHADER_CHECKER_FEATURE_NOT_ENABLED, "SC",
1124 "Shader requires VkPhysicalDeviceFeatures::%s but is not "
1125 "enabled on the device",
1126 feature_name)) {
1127 return true;
1128 }
1129 }
1130
1131 return false;
1132}
1133
1134static bool require_extension(debug_report_data const *report_data, bool extension, char const *extension_name) {
1135 if (!extension) {
1136 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1137 SHADER_CHECKER_FEATURE_NOT_ENABLED, "SC",
1138 "Shader requires extension %s but is not "
1139 "enabled on the device",
1140 extension_name)) {
1141 return true;
1142 }
1143 }
1144
1145 return false;
1146}
1147
1148static bool validate_shader_capabilities(layer_data *dev_data, shader_module const *src) {
1149 bool skip = false;
1150
1151 auto report_data = GetReportData(dev_data);
1152 auto const & enabledFeatures = GetEnabledFeatures(dev_data);
1153 auto const & extensions = GetEnabledExtensions(dev_data);
1154
1155 struct CapabilityInfo {
1156 char const *name;
1157 VkBool32 const VkPhysicalDeviceFeatures::*feature;
1158 bool const DeviceExtensions::*extension;
1159 };
1160
1161 using F = VkPhysicalDeviceFeatures;
1162 using E = DeviceExtensions;
1163
1164 // clang-format off
Dave Houltoneb10ea82017-12-22 12:21:50 -07001165 static const std::unordered_multimap<uint32_t, CapabilityInfo> capabilities = {
Chris Forbes47567b72017-06-09 12:09:45 -07001166 // Capabilities always supported by a Vulkan 1.0 implementation -- no
1167 // feature bits.
1168 {spv::CapabilityMatrix, {nullptr}},
1169 {spv::CapabilityShader, {nullptr}},
1170 {spv::CapabilityInputAttachment, {nullptr}},
1171 {spv::CapabilitySampled1D, {nullptr}},
1172 {spv::CapabilityImage1D, {nullptr}},
1173 {spv::CapabilitySampledBuffer, {nullptr}},
1174 {spv::CapabilityImageQuery, {nullptr}},
1175 {spv::CapabilityDerivativeControl, {nullptr}},
1176
1177 // Capabilities that are optionally supported, but require a feature to
1178 // be enabled on the device
1179 {spv::CapabilityGeometry, {"geometryShader", &F::geometryShader}},
1180 {spv::CapabilityTessellation, {"tessellationShader", &F::tessellationShader}},
1181 {spv::CapabilityFloat64, {"shaderFloat64", &F::shaderFloat64}},
1182 {spv::CapabilityInt64, {"shaderInt64", &F::shaderInt64}},
1183 {spv::CapabilityTessellationPointSize, {"shaderTessellationAndGeometryPointSize", &F::shaderTessellationAndGeometryPointSize}},
1184 {spv::CapabilityGeometryPointSize, {"shaderTessellationAndGeometryPointSize", &F::shaderTessellationAndGeometryPointSize}},
1185 {spv::CapabilityImageGatherExtended, {"shaderImageGatherExtended", &F::shaderImageGatherExtended}},
1186 {spv::CapabilityStorageImageMultisample, {"shaderStorageImageMultisample", &F::shaderStorageImageMultisample}},
1187 {spv::CapabilityUniformBufferArrayDynamicIndexing, {"shaderUniformBufferArrayDynamicIndexing", &F::shaderUniformBufferArrayDynamicIndexing}},
1188 {spv::CapabilitySampledImageArrayDynamicIndexing, {"shaderSampledImageArrayDynamicIndexing", &F::shaderSampledImageArrayDynamicIndexing}},
1189 {spv::CapabilityStorageBufferArrayDynamicIndexing, {"shaderStorageBufferArrayDynamicIndexing", &F::shaderStorageBufferArrayDynamicIndexing}},
1190 {spv::CapabilityStorageImageArrayDynamicIndexing, {"shaderStorageImageArrayDynamicIndexing", &F::shaderStorageBufferArrayDynamicIndexing}},
1191 {spv::CapabilityClipDistance, {"shaderClipDistance", &F::shaderClipDistance}},
1192 {spv::CapabilityCullDistance, {"shaderCullDistance", &F::shaderCullDistance}},
1193 {spv::CapabilityImageCubeArray, {"imageCubeArray", &F::imageCubeArray}},
1194 {spv::CapabilitySampleRateShading, {"sampleRateShading", &F::sampleRateShading}},
1195 {spv::CapabilitySparseResidency, {"shaderResourceResidency", &F::shaderResourceResidency}},
1196 {spv::CapabilityMinLod, {"shaderResourceMinLod", &F::shaderResourceMinLod}},
1197 {spv::CapabilitySampledCubeArray, {"imageCubeArray", &F::imageCubeArray}},
1198 {spv::CapabilityImageMSArray, {"shaderStorageImageMultisample", &F::shaderStorageImageMultisample}},
1199 {spv::CapabilityStorageImageExtendedFormats, {"shaderStorageImageExtendedFormats", &F::shaderStorageImageExtendedFormats}},
1200 {spv::CapabilityInterpolationFunction, {"sampleRateShading", &F::sampleRateShading}},
1201 {spv::CapabilityStorageImageReadWithoutFormat, {"shaderStorageImageReadWithoutFormat", &F::shaderStorageImageReadWithoutFormat}},
1202 {spv::CapabilityStorageImageWriteWithoutFormat, {"shaderStorageImageWriteWithoutFormat", &F::shaderStorageImageWriteWithoutFormat}},
1203 {spv::CapabilityMultiViewport, {"multiViewport", &F::multiViewport}},
1204
1205 // Capabilities that require an extension
1206 {spv::CapabilityDrawParameters, {VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, nullptr, &E::vk_khr_shader_draw_parameters}},
1207 {spv::CapabilityGeometryShaderPassthroughNV, {VK_NV_GEOMETRY_SHADER_PASSTHROUGH_EXTENSION_NAME, nullptr, &E::vk_nv_geometry_shader_passthrough}},
1208 {spv::CapabilitySampleMaskOverrideCoverageNV, {VK_NV_SAMPLE_MASK_OVERRIDE_COVERAGE_EXTENSION_NAME, nullptr, &E::vk_nv_sample_mask_override_coverage}},
Dave Houltoneb10ea82017-12-22 12:21:50 -07001209 {spv::CapabilityShaderViewportIndexLayerEXT, {VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME, nullptr, &E::vk_ext_shader_viewport_index_layer}},
Chris Forbes47567b72017-06-09 12:09:45 -07001210 {spv::CapabilityShaderViewportIndexLayerNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &E::vk_nv_viewport_array2}},
1211 {spv::CapabilityShaderViewportMaskNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &E::vk_nv_viewport_array2}},
1212 {spv::CapabilitySubgroupBallotKHR, {VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, nullptr, &E::vk_ext_shader_subgroup_ballot }},
1213 {spv::CapabilitySubgroupVoteKHR, {VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, nullptr, &E::vk_ext_shader_subgroup_vote }},
1214 };
1215 // clang-format on
1216
1217 for (auto insn : *src) {
1218 if (insn.opcode() == spv::OpCapability) {
Dave Houltoneb10ea82017-12-22 12:21:50 -07001219 size_t n = capabilities.count(insn.word(1));
1220 if (1 == n) { // key occurs exactly once
1221 auto it = capabilities.find(insn.word(1));
1222 if (it != capabilities.end()) {
1223 if (it->second.feature) {
1224 skip |= require_feature(report_data, enabledFeatures->*(it->second.feature), it->second.name);
1225 }
1226 if (it->second.extension) {
1227 skip |= require_extension(report_data, extensions->*(it->second.extension), it->second.name);
1228 }
Chris Forbes47567b72017-06-09 12:09:45 -07001229 }
Dave Houltoneb10ea82017-12-22 12:21:50 -07001230 } else if (1 < n) { // key occurs multiple times, at least one must be enabled
1231 bool needs_feature = false, has_feature = false;
1232 bool needs_ext = false, has_ext = false;
1233 std::string feature_names = "(one of) [ ";
1234 std::string extension_names = feature_names;
1235 auto caps = capabilities.equal_range(insn.word(1));
1236 for (auto it = caps.first; it != caps.second; ++it) {
1237 if (it->second.feature) {
1238 needs_feature = true;
1239 has_feature = has_feature || enabledFeatures->*(it->second.feature);
1240 feature_names += it->second.name;
1241 feature_names += " ";
1242 }
1243 if (it->second.extension) {
1244 needs_ext = true;
1245 has_ext = has_ext || extensions->*(it->second.extension);
1246 extension_names += it->second.name;
1247 extension_names += " ";
1248 }
1249 }
1250 if (needs_feature) {
1251 feature_names += "]";
1252 skip |= require_feature(report_data, has_feature, feature_names.c_str());
1253 }
1254 if (needs_ext) {
1255 extension_names += "]";
1256 skip |= require_extension(report_data, has_ext, extension_names.c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001257 }
1258 }
1259 }
1260 }
1261
1262 return skip;
1263}
1264
1265static uint32_t descriptor_type_to_reqs(shader_module const *module, uint32_t type_id) {
1266 auto type = module->get_def(type_id);
1267
1268 while (true) {
1269 switch (type.opcode()) {
1270 case spv::OpTypeArray:
1271 case spv::OpTypeSampledImage:
1272 type = module->get_def(type.word(2));
1273 break;
1274 case spv::OpTypePointer:
1275 type = module->get_def(type.word(3));
1276 break;
1277 case spv::OpTypeImage: {
1278 auto dim = type.word(3);
1279 auto arrayed = type.word(5);
1280 auto msaa = type.word(6);
1281
1282 switch (dim) {
1283 case spv::Dim1D:
1284 return arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1285 case spv::Dim2D:
1286 return (msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE) |
1287 (arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D);
1288 case spv::Dim3D:
1289 return DESCRIPTOR_REQ_VIEW_TYPE_3D;
1290 case spv::DimCube:
1291 return arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1292 case spv::DimSubpassData:
1293 return msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1294 default: // buffer, etc.
1295 return 0;
1296 }
1297 }
1298 default:
1299 return 0;
1300 }
1301 }
1302}
1303
1304// For given pipelineLayout verify that the set_layout_node at slot.first
1305// has the requested binding at slot.second and return ptr to that binding
1306static VkDescriptorSetLayoutBinding const *get_descriptor_binding(PIPELINE_LAYOUT_NODE const *pipelineLayout,
1307 descriptor_slot_t slot) {
1308 if (!pipelineLayout) return nullptr;
1309
1310 if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr;
1311
1312 return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second);
1313}
1314
1315
1316static bool validate_pipeline_shader_stage(
1317 layer_data *dev_data, VkPipelineShaderStageCreateInfo const *pStage, PIPELINE_STATE *pipeline,
1318 shader_module const **out_module, spirv_inst_iter *out_entrypoint) {
1319 bool skip = false;
1320 auto module = *out_module = GetShaderModuleState(dev_data, pStage->module);
1321 auto report_data = GetReportData(dev_data);
1322
1323 if (!module->has_valid_spirv) return false;
1324
1325 // Find the entrypoint
1326 auto entrypoint = *out_entrypoint = find_entrypoint(module, pStage->pName, pStage->stage);
1327 if (entrypoint == module->end()) {
1328 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1329 VALIDATION_ERROR_10600586, "SC", "No entrypoint found named `%s` for stage %s. %s.", pStage->pName,
1330 string_VkShaderStageFlagBits(pStage->stage), validation_error_map[VALIDATION_ERROR_10600586])) {
1331 return true; // no point continuing beyond here, any analysis is just going to be garbage.
1332 }
1333 }
1334
1335 // Validate shader capabilities against enabled device features
1336 skip |= validate_shader_capabilities(dev_data, module);
1337
1338 // Mark accessible ids
1339 auto accessible_ids = mark_accessible_ids(module, entrypoint);
1340
1341 // Validate descriptor set layout against what the entrypoint actually uses
1342 auto descriptor_uses = collect_interface_by_descriptor_slot(report_data, module, accessible_ids);
1343
Chris Forbes47567b72017-06-09 12:09:45 -07001344 skip |= validate_specialization_offsets(report_data, pStage);
Chris Forbesc2f751a2017-06-21 11:34:16 -07001345 skip |= validate_push_constant_usage(report_data, &pipeline->pipeline_layout.push_constant_ranges, module, accessible_ids, pStage->stage);
Chris Forbes47567b72017-06-09 12:09:45 -07001346
1347 // Validate descriptor use
1348 for (auto use : descriptor_uses) {
1349 // While validating shaders capture which slots are used by the pipeline
1350 auto &reqs = pipeline->active_slots[use.first.first][use.first.second];
1351 reqs = descriptor_req(reqs | descriptor_type_to_reqs(module, use.second.type_id));
1352
1353 // Verify given pipelineLayout has requested setLayout with requested binding
Chris Forbesc2f751a2017-06-21 11:34:16 -07001354 const auto &binding = get_descriptor_binding(&pipeline->pipeline_layout, use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07001355 unsigned required_descriptor_count;
1356
1357 if (!binding) {
1358 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1359 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1360 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
1361 use.first.first, use.first.second, describe_type(module, use.second.type_id).c_str());
1362 } else if (~binding->stageFlags & pStage->stage) {
1363 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, __LINE__,
1364 SHADER_CHECKER_DESCRIPTOR_NOT_ACCESSIBLE_FROM_STAGE, "SC",
1365 "Shader uses descriptor slot %u.%u (used "
1366 "as type `%s`) but descriptor not "
1367 "accessible from stage %s",
1368 use.first.first, use.first.second, describe_type(module, use.second.type_id).c_str(),
1369 string_VkShaderStageFlagBits(pStage->stage));
1370 } else if (!descriptor_type_match(module, use.second.type_id, binding->descriptorType, required_descriptor_count)) {
1371 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1372 SHADER_CHECKER_DESCRIPTOR_TYPE_MISMATCH, "SC",
1373 "Type mismatch on descriptor slot "
1374 "%u.%u (used as type `%s`) but "
1375 "descriptor of type %s",
1376 use.first.first, use.first.second, describe_type(module, use.second.type_id).c_str(),
1377 string_VkDescriptorType(binding->descriptorType));
1378 } else if (binding->descriptorCount < required_descriptor_count) {
1379 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1380 SHADER_CHECKER_DESCRIPTOR_TYPE_MISMATCH, "SC",
1381 "Shader expects at least %u descriptors for binding %u.%u (used as type `%s`) but only %u provided",
1382 required_descriptor_count, use.first.first, use.first.second,
1383 describe_type(module, use.second.type_id).c_str(), binding->descriptorCount);
1384 }
1385 }
1386
1387 // Validate use of input attachments against subpass structure
1388 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1389 auto input_attachment_uses = collect_interface_by_input_attachment_index(module, accessible_ids);
1390
Petr Krause91f7a12017-12-14 20:57:36 +01001391 auto rpci = pipeline->rp_state->createInfo.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07001392 auto subpass = pipeline->graphicsPipelineCI.subpass;
1393
1394 for (auto use : input_attachment_uses) {
1395 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
1396 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
1397 ? input_attachments[use.first].attachment
1398 : VK_ATTACHMENT_UNUSED;
1399
1400 if (index == VK_ATTACHMENT_UNUSED) {
1401 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1402 SHADER_CHECKER_MISSING_INPUT_ATTACHMENT, "SC",
1403 "Shader consumes input attachment index %d but not provided in subpass", use.first);
1404 } else if (!(get_format_type(rpci->pAttachments[index].format) & get_fundamental_type(module, use.second.type_id))) {
1405 skip |=
1406 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1407 SHADER_CHECKER_INPUT_ATTACHMENT_TYPE_MISMATCH, "SC",
1408 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
1409 string_VkFormat(rpci->pAttachments[index].format), describe_type(module, use.second.type_id).c_str());
1410 }
1411 }
1412 }
1413
1414 return skip;
1415}
1416
1417static bool validate_interface_between_stages(debug_report_data const *report_data, shader_module const *producer,
1418 spirv_inst_iter producer_entrypoint, shader_stage_attributes const *producer_stage,
1419 shader_module const *consumer, spirv_inst_iter consumer_entrypoint,
1420 shader_stage_attributes const *consumer_stage) {
1421 bool skip = false;
1422
1423 auto outputs =
1424 collect_interface_by_location(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
1425 auto inputs =
1426 collect_interface_by_location(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
1427
1428 auto a_it = outputs.begin();
1429 auto b_it = inputs.begin();
1430
1431 // Maps sorted by key (location); walk them together to find mismatches
1432 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
1433 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
1434 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
1435 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
1436 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
1437
1438 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
1439 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
1440 __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
1441 "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, a_first.first,
1442 a_first.second, consumer_stage->name);
1443 a_it++;
1444 } else if (a_at_end || a_first > b_first) {
1445 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1446 SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", "%s consumes input location %u.%u which is not written by %s",
1447 consumer_stage->name, b_first.first, b_first.second, producer_stage->name);
1448 b_it++;
1449 } else {
1450 // subtleties of arrayed interfaces:
1451 // - if is_patch, then the member is not arrayed, even though the interface may be.
1452 // - if is_block_member, then the extra array level of an arrayed interface is not
1453 // expressed in the member type -- it's expressed in the block type.
1454 if (!types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id,
1455 producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member,
1456 consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) {
1457 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1458 SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", "Type mismatch on location %u.%u: '%s' vs '%s'",
1459 a_first.first, a_first.second, describe_type(producer, a_it->second.type_id).c_str(),
1460 describe_type(consumer, b_it->second.type_id).c_str());
1461 }
1462 if (a_it->second.is_patch != b_it->second.is_patch) {
1463 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, __LINE__,
1464 SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
1465 "Decoration mismatch on location %u.%u: is per-%s in %s stage but "
1466 "per-%s in %s stage",
1467 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
1468 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
1469 }
1470 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
1471 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, __LINE__,
1472 SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
1473 "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first,
1474 a_first.second, producer_stage->name, consumer_stage->name);
1475 }
1476 a_it++;
1477 b_it++;
1478 }
1479 }
1480
1481 return skip;
1482}
1483
1484// Validate that the shaders used by the given pipeline and store the active_slots
1485// that are actually used by the pipeline into pPipeline->active_slots
Chris Forbesa400a8a2017-07-20 13:10:24 -07001486bool validate_and_capture_pipeline_shader_state(layer_data *dev_data, PIPELINE_STATE *pipeline) {
1487 auto pCreateInfo = pipeline->graphicsPipelineCI.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07001488 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1489 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
1490 auto report_data = GetReportData(dev_data);
1491
1492 shader_module const *shaders[5];
1493 memset(shaders, 0, sizeof(shaders));
1494 spirv_inst_iter entrypoints[5];
1495 memset(entrypoints, 0, sizeof(entrypoints));
1496 bool skip = false;
1497
1498 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
1499 auto pStage = &pCreateInfo->pStages[i];
1500 auto stage_id = get_shader_stage_id(pStage->stage);
Chris Forbesa400a8a2017-07-20 13:10:24 -07001501 skip |= validate_pipeline_shader_stage(dev_data, pStage, pipeline, &shaders[stage_id], &entrypoints[stage_id]);
Chris Forbes47567b72017-06-09 12:09:45 -07001502 }
1503
1504 // if the shader stages are no good individually, cross-stage validation is pointless.
1505 if (skip) return true;
1506
1507 auto vi = pCreateInfo->pVertexInputState;
1508
1509 if (vi) {
1510 skip |= validate_vi_consistency(report_data, vi);
1511 }
1512
1513 if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) {
1514 skip |= validate_vi_against_vs_inputs(report_data, vi, shaders[vertex_stage], entrypoints[vertex_stage]);
1515 }
1516
1517 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1518 int consumer = get_shader_stage_id(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
1519
1520 while (!shaders[producer] && producer != fragment_stage) {
1521 producer++;
1522 consumer++;
1523 }
1524
1525 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
1526 assert(shaders[producer]);
1527 if (shaders[consumer] && shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) {
1528 skip |= validate_interface_between_stages(report_data, shaders[producer], entrypoints[producer],
1529 &shader_stage_attribs[producer], shaders[consumer], entrypoints[consumer],
1530 &shader_stage_attribs[consumer]);
1531
1532 producer = consumer;
1533 }
1534 }
1535
1536 if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) {
1537 skip |= validate_fs_outputs_against_render_pass(report_data, shaders[fragment_stage], entrypoints[fragment_stage],
Chris Forbesa400a8a2017-07-20 13:10:24 -07001538 pipeline, pCreateInfo->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07001539 }
1540
1541 return skip;
1542}
1543
Chris Forbesa400a8a2017-07-20 13:10:24 -07001544bool validate_compute_pipeline(layer_data *dev_data, PIPELINE_STATE *pipeline) {
1545 auto pCreateInfo = pipeline->computePipelineCI.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07001546
1547 shader_module const *module;
1548 spirv_inst_iter entrypoint;
1549
Chris Forbesa400a8a2017-07-20 13:10:24 -07001550 return validate_pipeline_shader_stage(dev_data, &pCreateInfo->stage, pipeline, &module, &entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07001551}
Chris Forbes4ae55b32017-06-09 14:42:56 -07001552
Chris Forbes9a61e082017-07-24 15:35:29 -07001553uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) {
Cort Strattona3584fa2017-11-01 13:46:21 -07001554 return XXH32(smci->pCode, smci->codeSize, 0);
Chris Forbes9a61e082017-07-24 15:35:29 -07001555}
1556
1557static ValidationCache *GetValidationCacheInfo(
1558 VkShaderModuleCreateInfo const *pCreateInfo) {
1559 while ((pCreateInfo = (VkShaderModuleCreateInfo const *)pCreateInfo->pNext) != nullptr) {
1560 if (pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_VALIDATION_CACHE_CREATE_INFO_EXT)
1561 return (ValidationCache *)((VkShaderModuleValidationCacheCreateInfoEXT const *)pCreateInfo)->validationCache;
1562 }
1563
1564 return nullptr;
1565}
1566
Chris Forbes4ae55b32017-06-09 14:42:56 -07001567bool PreCallValidateCreateShaderModule(layer_data *dev_data, VkShaderModuleCreateInfo const *pCreateInfo, bool *spirv_valid) {
1568 bool skip = false;
1569 spv_result_t spv_valid = SPV_SUCCESS;
1570 auto report_data = GetReportData(dev_data);
1571
1572 if (GetDisables(dev_data)->shader_validation) {
1573 return false;
1574 }
1575
1576 auto have_glsl_shader = GetEnabledExtensions(dev_data)->vk_nv_glsl_shader;
1577
1578 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
1579 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
1580 __LINE__, VALIDATION_ERROR_12a00ac0, "SC",
1581 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ". %s",
1582 pCreateInfo->codeSize, validation_error_map[VALIDATION_ERROR_12a00ac0]);
1583 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07001584 auto cache = GetValidationCacheInfo(pCreateInfo);
1585 uint32_t hash = 0;
1586 if (cache) {
1587 hash = ValidationCache::MakeShaderHash(pCreateInfo);
1588 if (cache->Contains(hash))
1589 return false;
1590 }
1591
Chris Forbes4ae55b32017-06-09 14:42:56 -07001592 // Use SPIRV-Tools validator to try and catch any issues with the module itself
1593 spv_context ctx = spvContextCreate(SPV_ENV_VULKAN_1_0);
1594 spv_const_binary_t binary{ pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t) };
1595 spv_diagnostic diag = nullptr;
1596
1597 spv_valid = spvValidate(ctx, &binary, &diag);
1598 if (spv_valid != SPV_SUCCESS) {
1599 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
1600 skip |= log_msg(report_data,
1601 spv_valid == SPV_WARNING ? VK_DEBUG_REPORT_WARNING_BIT_EXT : VK_DEBUG_REPORT_ERROR_BIT_EXT,
1602 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
1603 "SPIR-V module not valid: %s", diag && diag->error ? diag->error : "(no error text)");
1604 }
Chris Forbes9a61e082017-07-24 15:35:29 -07001605 } else {
1606 if (cache) {
1607 cache->Insert(hash);
1608 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07001609 }
1610
1611 spvDiagnosticDestroy(diag);
1612 spvContextDestroy(ctx);
1613 }
1614
1615 *spirv_valid = (spv_valid == SPV_SUCCESS);
1616 return skip;
1617}