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