blob: aa215fa4ded5904a5734ac61d1df7e453214d3d6 [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) {
Chris Forbesa400a8a2017-07-20 13:10:24 -0700774 auto rpci = pipeline->render_pass_ci.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
1165 static const std::unordered_map<uint32_t, CapabilityInfo> capabilities = {
1166 // 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}},
1209 {spv::CapabilityShaderViewportIndexLayerNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &E::vk_nv_viewport_array2}},
1210 {spv::CapabilityShaderViewportMaskNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &E::vk_nv_viewport_array2}},
1211 {spv::CapabilitySubgroupBallotKHR, {VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, nullptr, &E::vk_ext_shader_subgroup_ballot }},
1212 {spv::CapabilitySubgroupVoteKHR, {VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, nullptr, &E::vk_ext_shader_subgroup_vote }},
1213 };
1214 // clang-format on
1215
1216 for (auto insn : *src) {
1217 if (insn.opcode() == spv::OpCapability) {
1218 auto it = capabilities.find(insn.word(1));
1219 if (it != capabilities.end()) {
1220 if (it->second.feature) {
1221 skip |= require_feature(report_data, enabledFeatures->*(it->second.feature), it->second.name);
1222 }
1223 if (it->second.extension) {
1224 skip |= require_extension(report_data, extensions->*(it->second.extension), it->second.name);
1225 }
1226 }
1227 }
1228 }
1229
1230 return skip;
1231}
1232
1233static uint32_t descriptor_type_to_reqs(shader_module const *module, uint32_t type_id) {
1234 auto type = module->get_def(type_id);
1235
1236 while (true) {
1237 switch (type.opcode()) {
1238 case spv::OpTypeArray:
1239 case spv::OpTypeSampledImage:
1240 type = module->get_def(type.word(2));
1241 break;
1242 case spv::OpTypePointer:
1243 type = module->get_def(type.word(3));
1244 break;
1245 case spv::OpTypeImage: {
1246 auto dim = type.word(3);
1247 auto arrayed = type.word(5);
1248 auto msaa = type.word(6);
1249
1250 switch (dim) {
1251 case spv::Dim1D:
1252 return arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1253 case spv::Dim2D:
1254 return (msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE) |
1255 (arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D);
1256 case spv::Dim3D:
1257 return DESCRIPTOR_REQ_VIEW_TYPE_3D;
1258 case spv::DimCube:
1259 return arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1260 case spv::DimSubpassData:
1261 return msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1262 default: // buffer, etc.
1263 return 0;
1264 }
1265 }
1266 default:
1267 return 0;
1268 }
1269 }
1270}
1271
1272// For given pipelineLayout verify that the set_layout_node at slot.first
1273// has the requested binding at slot.second and return ptr to that binding
1274static VkDescriptorSetLayoutBinding const *get_descriptor_binding(PIPELINE_LAYOUT_NODE const *pipelineLayout,
1275 descriptor_slot_t slot) {
1276 if (!pipelineLayout) return nullptr;
1277
1278 if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr;
1279
1280 return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second);
1281}
1282
1283
1284static bool validate_pipeline_shader_stage(
1285 layer_data *dev_data, VkPipelineShaderStageCreateInfo const *pStage, PIPELINE_STATE *pipeline,
1286 shader_module const **out_module, spirv_inst_iter *out_entrypoint) {
1287 bool skip = false;
1288 auto module = *out_module = GetShaderModuleState(dev_data, pStage->module);
1289 auto report_data = GetReportData(dev_data);
1290
1291 if (!module->has_valid_spirv) return false;
1292
1293 // Find the entrypoint
1294 auto entrypoint = *out_entrypoint = find_entrypoint(module, pStage->pName, pStage->stage);
1295 if (entrypoint == module->end()) {
1296 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1297 VALIDATION_ERROR_10600586, "SC", "No entrypoint found named `%s` for stage %s. %s.", pStage->pName,
1298 string_VkShaderStageFlagBits(pStage->stage), validation_error_map[VALIDATION_ERROR_10600586])) {
1299 return true; // no point continuing beyond here, any analysis is just going to be garbage.
1300 }
1301 }
1302
1303 // Validate shader capabilities against enabled device features
1304 skip |= validate_shader_capabilities(dev_data, module);
1305
1306 // Mark accessible ids
1307 auto accessible_ids = mark_accessible_ids(module, entrypoint);
1308
1309 // Validate descriptor set layout against what the entrypoint actually uses
1310 auto descriptor_uses = collect_interface_by_descriptor_slot(report_data, module, accessible_ids);
1311
Chris Forbes47567b72017-06-09 12:09:45 -07001312 skip |= validate_specialization_offsets(report_data, pStage);
Chris Forbesc2f751a2017-06-21 11:34:16 -07001313 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 -07001314
1315 // Validate descriptor use
1316 for (auto use : descriptor_uses) {
1317 // While validating shaders capture which slots are used by the pipeline
1318 auto &reqs = pipeline->active_slots[use.first.first][use.first.second];
1319 reqs = descriptor_req(reqs | descriptor_type_to_reqs(module, use.second.type_id));
1320
1321 // Verify given pipelineLayout has requested setLayout with requested binding
Chris Forbesc2f751a2017-06-21 11:34:16 -07001322 const auto &binding = get_descriptor_binding(&pipeline->pipeline_layout, use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07001323 unsigned required_descriptor_count;
1324
1325 if (!binding) {
1326 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1327 SHADER_CHECKER_MISSING_DESCRIPTOR, "SC",
1328 "Shader uses descriptor slot %u.%u (used as type `%s`) but not declared in pipeline layout",
1329 use.first.first, use.first.second, describe_type(module, use.second.type_id).c_str());
1330 } else if (~binding->stageFlags & pStage->stage) {
1331 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, __LINE__,
1332 SHADER_CHECKER_DESCRIPTOR_NOT_ACCESSIBLE_FROM_STAGE, "SC",
1333 "Shader uses descriptor slot %u.%u (used "
1334 "as type `%s`) but descriptor not "
1335 "accessible from stage %s",
1336 use.first.first, use.first.second, describe_type(module, use.second.type_id).c_str(),
1337 string_VkShaderStageFlagBits(pStage->stage));
1338 } else if (!descriptor_type_match(module, use.second.type_id, binding->descriptorType, required_descriptor_count)) {
1339 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1340 SHADER_CHECKER_DESCRIPTOR_TYPE_MISMATCH, "SC",
1341 "Type mismatch on descriptor slot "
1342 "%u.%u (used as type `%s`) but "
1343 "descriptor of type %s",
1344 use.first.first, use.first.second, describe_type(module, use.second.type_id).c_str(),
1345 string_VkDescriptorType(binding->descriptorType));
1346 } else if (binding->descriptorCount < required_descriptor_count) {
1347 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1348 SHADER_CHECKER_DESCRIPTOR_TYPE_MISMATCH, "SC",
1349 "Shader expects at least %u descriptors for binding %u.%u (used as type `%s`) but only %u provided",
1350 required_descriptor_count, use.first.first, use.first.second,
1351 describe_type(module, use.second.type_id).c_str(), binding->descriptorCount);
1352 }
1353 }
1354
1355 // Validate use of input attachments against subpass structure
1356 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1357 auto input_attachment_uses = collect_interface_by_input_attachment_index(module, accessible_ids);
1358
1359 auto rpci = pipeline->render_pass_ci.ptr();
1360 auto subpass = pipeline->graphicsPipelineCI.subpass;
1361
1362 for (auto use : input_attachment_uses) {
1363 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
1364 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
1365 ? input_attachments[use.first].attachment
1366 : VK_ATTACHMENT_UNUSED;
1367
1368 if (index == VK_ATTACHMENT_UNUSED) {
1369 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1370 SHADER_CHECKER_MISSING_INPUT_ATTACHMENT, "SC",
1371 "Shader consumes input attachment index %d but not provided in subpass", use.first);
1372 } else if (!(get_format_type(rpci->pAttachments[index].format) & get_fundamental_type(module, use.second.type_id))) {
1373 skip |=
1374 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1375 SHADER_CHECKER_INPUT_ATTACHMENT_TYPE_MISMATCH, "SC",
1376 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
1377 string_VkFormat(rpci->pAttachments[index].format), describe_type(module, use.second.type_id).c_str());
1378 }
1379 }
1380 }
1381
1382 return skip;
1383}
1384
1385static bool validate_interface_between_stages(debug_report_data const *report_data, shader_module const *producer,
1386 spirv_inst_iter producer_entrypoint, shader_stage_attributes const *producer_stage,
1387 shader_module const *consumer, spirv_inst_iter consumer_entrypoint,
1388 shader_stage_attributes const *consumer_stage) {
1389 bool skip = false;
1390
1391 auto outputs =
1392 collect_interface_by_location(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
1393 auto inputs =
1394 collect_interface_by_location(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
1395
1396 auto a_it = outputs.begin();
1397 auto b_it = inputs.begin();
1398
1399 // Maps sorted by key (location); walk them together to find mismatches
1400 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
1401 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
1402 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
1403 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
1404 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
1405
1406 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
1407 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
1408 __LINE__, SHADER_CHECKER_OUTPUT_NOT_CONSUMED, "SC",
1409 "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, a_first.first,
1410 a_first.second, consumer_stage->name);
1411 a_it++;
1412 } else if (a_at_end || a_first > b_first) {
1413 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1414 SHADER_CHECKER_INPUT_NOT_PRODUCED, "SC", "%s consumes input location %u.%u which is not written by %s",
1415 consumer_stage->name, b_first.first, b_first.second, producer_stage->name);
1416 b_it++;
1417 } else {
1418 // subtleties of arrayed interfaces:
1419 // - if is_patch, then the member is not arrayed, even though the interface may be.
1420 // - if is_block_member, then the extra array level of an arrayed interface is not
1421 // expressed in the member type -- it's expressed in the block type.
1422 if (!types_match(producer, consumer, a_it->second.type_id, b_it->second.type_id,
1423 producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member,
1424 consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) {
1425 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
1426 SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC", "Type mismatch on location %u.%u: '%s' vs '%s'",
1427 a_first.first, a_first.second, describe_type(producer, a_it->second.type_id).c_str(),
1428 describe_type(consumer, b_it->second.type_id).c_str());
1429 }
1430 if (a_it->second.is_patch != b_it->second.is_patch) {
1431 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, __LINE__,
1432 SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
1433 "Decoration mismatch on location %u.%u: is per-%s in %s stage but "
1434 "per-%s in %s stage",
1435 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
1436 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
1437 }
1438 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
1439 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, __LINE__,
1440 SHADER_CHECKER_INTERFACE_TYPE_MISMATCH, "SC",
1441 "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first,
1442 a_first.second, producer_stage->name, consumer_stage->name);
1443 }
1444 a_it++;
1445 b_it++;
1446 }
1447 }
1448
1449 return skip;
1450}
1451
1452// Validate that the shaders used by the given pipeline and store the active_slots
1453// that are actually used by the pipeline into pPipeline->active_slots
Chris Forbesa400a8a2017-07-20 13:10:24 -07001454bool validate_and_capture_pipeline_shader_state(layer_data *dev_data, PIPELINE_STATE *pipeline) {
1455 auto pCreateInfo = pipeline->graphicsPipelineCI.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07001456 int vertex_stage = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1457 int fragment_stage = get_shader_stage_id(VK_SHADER_STAGE_FRAGMENT_BIT);
1458 auto report_data = GetReportData(dev_data);
1459
1460 shader_module const *shaders[5];
1461 memset(shaders, 0, sizeof(shaders));
1462 spirv_inst_iter entrypoints[5];
1463 memset(entrypoints, 0, sizeof(entrypoints));
1464 bool skip = false;
1465
1466 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
1467 auto pStage = &pCreateInfo->pStages[i];
1468 auto stage_id = get_shader_stage_id(pStage->stage);
Chris Forbesa400a8a2017-07-20 13:10:24 -07001469 skip |= validate_pipeline_shader_stage(dev_data, pStage, pipeline, &shaders[stage_id], &entrypoints[stage_id]);
Chris Forbes47567b72017-06-09 12:09:45 -07001470 }
1471
1472 // if the shader stages are no good individually, cross-stage validation is pointless.
1473 if (skip) return true;
1474
1475 auto vi = pCreateInfo->pVertexInputState;
1476
1477 if (vi) {
1478 skip |= validate_vi_consistency(report_data, vi);
1479 }
1480
1481 if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) {
1482 skip |= validate_vi_against_vs_inputs(report_data, vi, shaders[vertex_stage], entrypoints[vertex_stage]);
1483 }
1484
1485 int producer = get_shader_stage_id(VK_SHADER_STAGE_VERTEX_BIT);
1486 int consumer = get_shader_stage_id(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
1487
1488 while (!shaders[producer] && producer != fragment_stage) {
1489 producer++;
1490 consumer++;
1491 }
1492
1493 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
1494 assert(shaders[producer]);
1495 if (shaders[consumer] && shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) {
1496 skip |= validate_interface_between_stages(report_data, shaders[producer], entrypoints[producer],
1497 &shader_stage_attribs[producer], shaders[consumer], entrypoints[consumer],
1498 &shader_stage_attribs[consumer]);
1499
1500 producer = consumer;
1501 }
1502 }
1503
1504 if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) {
1505 skip |= validate_fs_outputs_against_render_pass(report_data, shaders[fragment_stage], entrypoints[fragment_stage],
Chris Forbesa400a8a2017-07-20 13:10:24 -07001506 pipeline, pCreateInfo->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07001507 }
1508
1509 return skip;
1510}
1511
Chris Forbesa400a8a2017-07-20 13:10:24 -07001512bool validate_compute_pipeline(layer_data *dev_data, PIPELINE_STATE *pipeline) {
1513 auto pCreateInfo = pipeline->computePipelineCI.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07001514
1515 shader_module const *module;
1516 spirv_inst_iter entrypoint;
1517
Chris Forbesa400a8a2017-07-20 13:10:24 -07001518 return validate_pipeline_shader_stage(dev_data, &pCreateInfo->stage, pipeline, &module, &entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07001519}
Chris Forbes4ae55b32017-06-09 14:42:56 -07001520
Chris Forbes9a61e082017-07-24 15:35:29 -07001521uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) {
1522 return XXH32(smci->pCode, smci->codeSize * sizeof(uint32_t), 0);
1523}
1524
1525static ValidationCache *GetValidationCacheInfo(
1526 VkShaderModuleCreateInfo const *pCreateInfo) {
1527 while ((pCreateInfo = (VkShaderModuleCreateInfo const *)pCreateInfo->pNext) != nullptr) {
1528 if (pCreateInfo->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_VALIDATION_CACHE_CREATE_INFO_EXT)
1529 return (ValidationCache *)((VkShaderModuleValidationCacheCreateInfoEXT const *)pCreateInfo)->validationCache;
1530 }
1531
1532 return nullptr;
1533}
1534
Chris Forbes4ae55b32017-06-09 14:42:56 -07001535bool PreCallValidateCreateShaderModule(layer_data *dev_data, VkShaderModuleCreateInfo const *pCreateInfo, bool *spirv_valid) {
1536 bool skip = false;
1537 spv_result_t spv_valid = SPV_SUCCESS;
1538 auto report_data = GetReportData(dev_data);
1539
1540 if (GetDisables(dev_data)->shader_validation) {
1541 return false;
1542 }
1543
1544 auto have_glsl_shader = GetEnabledExtensions(dev_data)->vk_nv_glsl_shader;
1545
1546 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
1547 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
1548 __LINE__, VALIDATION_ERROR_12a00ac0, "SC",
1549 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ". %s",
1550 pCreateInfo->codeSize, validation_error_map[VALIDATION_ERROR_12a00ac0]);
1551 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07001552 auto cache = GetValidationCacheInfo(pCreateInfo);
1553 uint32_t hash = 0;
1554 if (cache) {
1555 hash = ValidationCache::MakeShaderHash(pCreateInfo);
1556 if (cache->Contains(hash))
1557 return false;
1558 }
1559
Chris Forbes4ae55b32017-06-09 14:42:56 -07001560 // Use SPIRV-Tools validator to try and catch any issues with the module itself
1561 spv_context ctx = spvContextCreate(SPV_ENV_VULKAN_1_0);
1562 spv_const_binary_t binary{ pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t) };
1563 spv_diagnostic diag = nullptr;
1564
1565 spv_valid = spvValidate(ctx, &binary, &diag);
1566 if (spv_valid != SPV_SUCCESS) {
1567 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
1568 skip |= log_msg(report_data,
1569 spv_valid == SPV_WARNING ? VK_DEBUG_REPORT_WARNING_BIT_EXT : VK_DEBUG_REPORT_ERROR_BIT_EXT,
1570 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__, SHADER_CHECKER_INCONSISTENT_SPIRV, "SC",
1571 "SPIR-V module not valid: %s", diag && diag->error ? diag->error : "(no error text)");
1572 }
Chris Forbes9a61e082017-07-24 15:35:29 -07001573 } else {
1574 if (cache) {
1575 cache->Insert(hash);
1576 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07001577 }
1578
1579 spvDiagnosticDestroy(diag);
1580 spvContextDestroy(ctx);
1581 }
1582
1583 *spirv_valid = (spv_valid == SPV_SUCCESS);
1584 return skip;
1585}