blob: ba42a1e06434e2bef8171cdf69703ba2f1c40396 [file] [log] [blame]
Karl Schultz7b024b42018-08-30 16:18:18 -06001/* Copyright (c) 2015-2019 The Khronos Group Inc.
2 * Copyright (c) 2015-2019 Valve Corporation
3 * Copyright (c) 2015-2019 LunarG, Inc.
4 * Copyright (C) 2015-2019 Google Inc.
Chris Forbes47567b72017-06-09 12:09:45 -07005 *
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>
Dave Houlton51653902018-06-22 17:32:13 -060019 * Author: Dave Houlton <daveh@lunarg.com>
Chris Forbes47567b72017-06-09 12:09:45 -070020 */
21
22#include <cinttypes>
23#include <cassert>
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +020024#include <chrono>
Chris Forbes47567b72017-06-09 12:09:45 -070025#include <vector>
26#include <unordered_map>
27#include <string>
28#include <sstream>
29#include <SPIRV/spirv.hpp>
30#include "vk_loader_platform.h"
31#include "vk_enum_string_helper.h"
Chris Forbes47567b72017-06-09 12:09:45 -070032#include "vk_layer_data.h"
33#include "vk_layer_extension_utils.h"
34#include "vk_layer_utils.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070035#include "chassis.h"
Chris Forbes47567b72017-06-09 12:09:45 -070036#include "core_validation.h"
Chris Forbes47567b72017-06-09 12:09:45 -070037#include "shader_validation.h"
Chris Forbes4ae55b32017-06-09 14:42:56 -070038#include "spirv-tools/libspirv.h"
Chris Forbes9a61e082017-07-24 15:35:29 -070039#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070040
41enum FORMAT_TYPE {
42 FORMAT_TYPE_FLOAT = 1, // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader
43 FORMAT_TYPE_SINT = 2,
44 FORMAT_TYPE_UINT = 4,
45};
46
47typedef std::pair<unsigned, unsigned> location_t;
48
49struct interface_var {
50 uint32_t id;
51 uint32_t type_id;
52 uint32_t offset;
53 bool is_patch;
54 bool is_block_member;
55 bool is_relaxed_precision;
56 // TODO: collect the name, too? Isn't required to be present.
57};
58
59struct shader_stage_attributes {
60 char const *const name;
61 bool arrayed_input;
62 bool arrayed_output;
Ari Suonpaa696b3432019-03-11 14:02:57 +020063 VkShaderStageFlags stage;
Chris Forbes47567b72017-06-09 12:09:45 -070064};
65
66static shader_stage_attributes shader_stage_attribs[] = {
Ari Suonpaa696b3432019-03-11 14:02:57 +020067 {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT},
68 {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT},
69 {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT},
70 {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT},
71 {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT},
Chris Forbes47567b72017-06-09 12:09:45 -070072};
73
74// SPIRV utility functions
Mark Lobodzinski3c59d972019-04-25 11:28:14 -060075void SHADER_MODULE_STATE::BuildDefIndex() {
Chris Forbes47567b72017-06-09 12:09:45 -070076 for (auto insn : *this) {
77 switch (insn.opcode()) {
78 // Types
79 case spv::OpTypeVoid:
80 case spv::OpTypeBool:
81 case spv::OpTypeInt:
82 case spv::OpTypeFloat:
83 case spv::OpTypeVector:
84 case spv::OpTypeMatrix:
85 case spv::OpTypeImage:
86 case spv::OpTypeSampler:
87 case spv::OpTypeSampledImage:
88 case spv::OpTypeArray:
89 case spv::OpTypeRuntimeArray:
90 case spv::OpTypeStruct:
91 case spv::OpTypeOpaque:
92 case spv::OpTypePointer:
93 case spv::OpTypeFunction:
94 case spv::OpTypeEvent:
95 case spv::OpTypeDeviceEvent:
96 case spv::OpTypeReserveId:
97 case spv::OpTypeQueue:
98 case spv::OpTypePipe:
Shannon McPherson0fa28232018-11-01 11:59:02 -060099 case spv::OpTypeAccelerationStructureNV:
Jeff Bolze4356752019-03-07 11:23:46 -0600100 case spv::OpTypeCooperativeMatrixNV:
Chris Forbes47567b72017-06-09 12:09:45 -0700101 def_index[insn.word(1)] = insn.offset();
102 break;
103
104 // Fixed constants
105 case spv::OpConstantTrue:
106 case spv::OpConstantFalse:
107 case spv::OpConstant:
108 case spv::OpConstantComposite:
109 case spv::OpConstantSampler:
110 case spv::OpConstantNull:
111 def_index[insn.word(2)] = insn.offset();
112 break;
113
114 // Specialization constants
115 case spv::OpSpecConstantTrue:
116 case spv::OpSpecConstantFalse:
117 case spv::OpSpecConstant:
118 case spv::OpSpecConstantComposite:
119 case spv::OpSpecConstantOp:
120 def_index[insn.word(2)] = insn.offset();
121 break;
122
123 // Variables
124 case spv::OpVariable:
125 def_index[insn.word(2)] = insn.offset();
126 break;
127
128 // Functions
129 case spv::OpFunction:
130 def_index[insn.word(2)] = insn.offset();
131 break;
132
133 default:
134 // We don't care about any other defs for now.
135 break;
136 }
137 }
138}
139
Jeff Bolz105d6492018-09-29 15:46:44 -0500140unsigned ExecutionModelToShaderStageFlagBits(unsigned mode) {
141 switch (mode) {
142 case spv::ExecutionModelVertex:
143 return VK_SHADER_STAGE_VERTEX_BIT;
144 case spv::ExecutionModelTessellationControl:
145 return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
146 case spv::ExecutionModelTessellationEvaluation:
147 return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
148 case spv::ExecutionModelGeometry:
149 return VK_SHADER_STAGE_GEOMETRY_BIT;
150 case spv::ExecutionModelFragment:
151 return VK_SHADER_STAGE_FRAGMENT_BIT;
152 case spv::ExecutionModelGLCompute:
153 return VK_SHADER_STAGE_COMPUTE_BIT;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600154 case spv::ExecutionModelRayGenerationNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700155 return VK_SHADER_STAGE_RAYGEN_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600156 case spv::ExecutionModelAnyHitNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700157 return VK_SHADER_STAGE_ANY_HIT_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600158 case spv::ExecutionModelClosestHitNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700159 return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600160 case spv::ExecutionModelMissNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700161 return VK_SHADER_STAGE_MISS_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600162 case spv::ExecutionModelIntersectionNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700163 return VK_SHADER_STAGE_INTERSECTION_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600164 case spv::ExecutionModelCallableNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700165 return VK_SHADER_STAGE_CALLABLE_BIT_NV;
Jeff Bolz105d6492018-09-29 15:46:44 -0500166 case spv::ExecutionModelTaskNV:
167 return VK_SHADER_STAGE_TASK_BIT_NV;
168 case spv::ExecutionModelMeshNV:
169 return VK_SHADER_STAGE_MESH_BIT_NV;
170 default:
171 return 0;
172 }
173}
174
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600175static spirv_inst_iter FindEntrypoint(SHADER_MODULE_STATE const *src, char const *name, VkShaderStageFlagBits stageBits) {
Chris Forbes47567b72017-06-09 12:09:45 -0700176 for (auto insn : *src) {
177 if (insn.opcode() == spv::OpEntryPoint) {
178 auto entrypointName = (char const *)&insn.word(3);
Jeff Bolz105d6492018-09-29 15:46:44 -0500179 auto executionModel = insn.word(1);
180 auto entrypointStageBits = ExecutionModelToShaderStageFlagBits(executionModel);
Chris Forbes47567b72017-06-09 12:09:45 -0700181
182 if (!strcmp(entrypointName, name) && (entrypointStageBits & stageBits)) {
183 return insn;
184 }
185 }
186 }
187
188 return src->end();
189}
190
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600191static char const *StorageClassName(unsigned sc) {
Chris Forbes47567b72017-06-09 12:09:45 -0700192 switch (sc) {
193 case spv::StorageClassInput:
194 return "input";
195 case spv::StorageClassOutput:
196 return "output";
197 case spv::StorageClassUniformConstant:
198 return "const uniform";
199 case spv::StorageClassUniform:
200 return "uniform";
201 case spv::StorageClassWorkgroup:
202 return "workgroup local";
203 case spv::StorageClassCrossWorkgroup:
204 return "workgroup global";
205 case spv::StorageClassPrivate:
206 return "private global";
207 case spv::StorageClassFunction:
208 return "function";
209 case spv::StorageClassGeneric:
210 return "generic";
211 case spv::StorageClassAtomicCounter:
212 return "atomic counter";
213 case spv::StorageClassImage:
214 return "image";
215 case spv::StorageClassPushConstant:
216 return "push constant";
Chris Forbes9f89d752018-03-07 12:57:48 -0800217 case spv::StorageClassStorageBuffer:
218 return "storage buffer";
Chris Forbes47567b72017-06-09 12:09:45 -0700219 default:
220 return "unknown";
221 }
222}
223
224// Get the value of an integral constant
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600225unsigned GetConstantValue(SHADER_MODULE_STATE const *src, unsigned id) {
Chris Forbes47567b72017-06-09 12:09:45 -0700226 auto value = src->get_def(id);
227 assert(value != src->end());
228
229 if (value.opcode() != spv::OpConstant) {
230 // TODO: Either ensure that the specialization transform is already performed on a module we're
231 // considering here, OR -- specialize on the fly now.
232 return 1;
233 }
234
235 return value.word(3);
236}
237
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600238static void DescribeTypeInner(std::ostringstream &ss, SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700239 auto insn = src->get_def(type);
240 assert(insn != src->end());
241
242 switch (insn.opcode()) {
243 case spv::OpTypeBool:
244 ss << "bool";
245 break;
246 case spv::OpTypeInt:
247 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
248 break;
249 case spv::OpTypeFloat:
250 ss << "float" << insn.word(2);
251 break;
252 case spv::OpTypeVector:
253 ss << "vec" << insn.word(3) << " of ";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600254 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700255 break;
256 case spv::OpTypeMatrix:
257 ss << "mat" << insn.word(3) << " of ";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600258 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700259 break;
260 case spv::OpTypeArray:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600261 ss << "arr[" << GetConstantValue(src, insn.word(3)) << "] of ";
262 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700263 break;
Chris Forbes062f1222018-08-21 15:34:15 -0700264 case spv::OpTypeRuntimeArray:
265 ss << "runtime arr[] of ";
266 DescribeTypeInner(ss, src, insn.word(2));
267 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700268 case spv::OpTypePointer:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600269 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
270 DescribeTypeInner(ss, src, insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700271 break;
272 case spv::OpTypeStruct: {
273 ss << "struct of (";
274 for (unsigned i = 2; i < insn.len(); i++) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600275 DescribeTypeInner(ss, src, insn.word(i));
Chris Forbes47567b72017-06-09 12:09:45 -0700276 if (i == insn.len() - 1) {
277 ss << ")";
278 } else {
279 ss << ", ";
280 }
281 }
282 break;
283 }
284 case spv::OpTypeSampler:
285 ss << "sampler";
286 break;
287 case spv::OpTypeSampledImage:
288 ss << "sampler+";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600289 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700290 break;
291 case spv::OpTypeImage:
292 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
293 break;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600294 case spv::OpTypeAccelerationStructureNV:
Jeff Bolz105d6492018-09-29 15:46:44 -0500295 ss << "accelerationStruture";
296 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700297 default:
298 ss << "oddtype";
299 break;
300 }
301}
302
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600303static std::string DescribeType(SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700304 std::ostringstream ss;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600305 DescribeTypeInner(ss, src, type);
Chris Forbes47567b72017-06-09 12:09:45 -0700306 return ss.str();
307}
308
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600309static bool IsNarrowNumericType(spirv_inst_iter type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700310 if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false;
311 return type.word(2) < 64;
312}
313
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600314static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, unsigned a_type, unsigned b_type, bool a_arrayed,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600315 bool b_arrayed, bool relaxed) {
Chris Forbes47567b72017-06-09 12:09:45 -0700316 // Walk two type trees together, and complain about differences
317 auto a_insn = a->get_def(a_type);
318 auto b_insn = b->get_def(b_type);
319 assert(a_insn != a->end());
320 assert(b_insn != b->end());
321
Chris Forbes062f1222018-08-21 15:34:15 -0700322 // Ignore runtime-sized arrays-- they cannot appear in these interfaces.
323
Chris Forbes47567b72017-06-09 12:09:45 -0700324 if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600325 return TypesMatch(a, b, a_insn.word(2), b_type, false, b_arrayed, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700326 }
327
328 if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) {
329 // We probably just found the extra level of arrayness in b_type: compare the type inside it to a_type
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600330 return TypesMatch(a, b, a_type, b_insn.word(2), a_arrayed, false, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700331 }
332
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600333 if (a_insn.opcode() == spv::OpTypeVector && relaxed && IsNarrowNumericType(b_insn)) {
334 return TypesMatch(a, b, a_insn.word(2), b_type, a_arrayed, b_arrayed, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700335 }
336
337 if (a_insn.opcode() != b_insn.opcode()) {
338 return false;
339 }
340
341 if (a_insn.opcode() == spv::OpTypePointer) {
342 // Match on pointee type. storage class is expected to differ
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600343 return TypesMatch(a, b, a_insn.word(3), b_insn.word(3), a_arrayed, b_arrayed, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700344 }
345
346 if (a_arrayed || b_arrayed) {
347 // If we havent resolved array-of-verts by here, we're not going to.
348 return false;
349 }
350
351 switch (a_insn.opcode()) {
352 case spv::OpTypeBool:
353 return true;
354 case spv::OpTypeInt:
355 // Match on width, signedness
356 return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3);
357 case spv::OpTypeFloat:
358 // Match on width
359 return a_insn.word(2) == b_insn.word(2);
360 case spv::OpTypeVector:
361 // Match on element type, count.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600362 if (!TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false;
363 if (relaxed && IsNarrowNumericType(a->get_def(a_insn.word(2)))) {
Chris Forbes47567b72017-06-09 12:09:45 -0700364 return a_insn.word(3) >= b_insn.word(3);
365 } else {
366 return a_insn.word(3) == b_insn.word(3);
367 }
368 case spv::OpTypeMatrix:
369 // Match on element type, count.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600370 return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
Dave Houltona9df0ce2018-02-07 10:51:23 -0700371 a_insn.word(3) == b_insn.word(3);
Chris Forbes47567b72017-06-09 12:09:45 -0700372 case spv::OpTypeArray:
373 // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from
374 // vector & matrix types in that the array size is the id of a constant instruction, * not a literal within OpTypeArray
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600375 return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
376 GetConstantValue(a, a_insn.word(3)) == GetConstantValue(b, b_insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700377 case spv::OpTypeStruct:
378 // Match on all element types
Dave Houltona9df0ce2018-02-07 10:51:23 -0700379 {
380 if (a_insn.len() != b_insn.len()) {
381 return false; // Structs cannot match if member counts differ
Chris Forbes47567b72017-06-09 12:09:45 -0700382 }
Chris Forbes47567b72017-06-09 12:09:45 -0700383
Dave Houltona9df0ce2018-02-07 10:51:23 -0700384 for (unsigned i = 2; i < a_insn.len(); i++) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600385 if (!TypesMatch(a, b, a_insn.word(i), b_insn.word(i), a_arrayed, b_arrayed, false)) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700386 return false;
387 }
388 }
389
390 return true;
391 }
Chris Forbes47567b72017-06-09 12:09:45 -0700392 default:
393 // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match.
394 return false;
395 }
396}
397
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600398static unsigned ValueOrDefault(std::unordered_map<unsigned, unsigned> const &map, unsigned id, unsigned def) {
Chris Forbes47567b72017-06-09 12:09:45 -0700399 auto it = map.find(id);
400 if (it == map.end())
401 return def;
402 else
403 return it->second;
404}
405
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600406static unsigned GetLocationsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) {
Chris Forbes47567b72017-06-09 12:09:45 -0700407 auto insn = src->get_def(type);
408 assert(insn != src->end());
409
410 switch (insn.opcode()) {
411 case spv::OpTypePointer:
412 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
413 // pointers around.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600414 return GetLocationsConsumedByType(src, insn.word(3), strip_array_level);
Chris Forbes47567b72017-06-09 12:09:45 -0700415 case spv::OpTypeArray:
416 if (strip_array_level) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600417 return GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700418 } else {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600419 return GetConstantValue(src, insn.word(3)) * GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700420 }
421 case spv::OpTypeMatrix:
422 // Num locations is the dimension * element size
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600423 return insn.word(3) * GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700424 case spv::OpTypeVector: {
425 auto scalar_type = src->get_def(insn.word(2));
426 auto bit_width =
427 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
428
429 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
430 return (bit_width * insn.word(3) + 127) / 128;
431 }
432 default:
433 // Everything else is just 1.
434 return 1;
435
436 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
437 }
438}
439
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600440static unsigned GetComponentsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200441 auto insn = src->get_def(type);
442 assert(insn != src->end());
443
444 switch (insn.opcode()) {
445 case spv::OpTypePointer:
446 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
447 // pointers around.
448 return GetComponentsConsumedByType(src, insn.word(3), strip_array_level);
449 case spv::OpTypeStruct: {
450 uint32_t sum = 0;
451 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
452 sum += GetComponentsConsumedByType(src, insn.word(i), false);
453 }
454 return sum;
455 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500456 case spv::OpTypeArray:
457 if (strip_array_level) {
458 return GetComponentsConsumedByType(src, insn.word(2), false);
459 } else {
460 return GetConstantValue(src, insn.word(3)) * GetComponentsConsumedByType(src, insn.word(2), false);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200461 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200462 case spv::OpTypeMatrix:
463 // Num locations is the dimension * element size
464 return insn.word(3) * GetComponentsConsumedByType(src, insn.word(2), false);
465 case spv::OpTypeVector: {
466 auto scalar_type = src->get_def(insn.word(2));
467 auto bit_width =
468 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
469 // One component is 32-bit
470 return (bit_width * insn.word(3) + 31) / 32;
471 }
472 case spv::OpTypeFloat: {
473 auto bit_width = insn.word(2);
474 return (bit_width + 31) / 32;
475 }
476 case spv::OpTypeInt: {
477 auto bit_width = insn.word(2);
478 return (bit_width + 31) / 32;
479 }
480 case spv::OpConstant:
481 return GetComponentsConsumedByType(src, insn.word(1), false);
482 default:
483 return 0;
484 }
485}
486
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600487static unsigned GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -0700488 switch (format) {
489 case VK_FORMAT_R64G64B64A64_SFLOAT:
490 case VK_FORMAT_R64G64B64A64_SINT:
491 case VK_FORMAT_R64G64B64A64_UINT:
492 case VK_FORMAT_R64G64B64_SFLOAT:
493 case VK_FORMAT_R64G64B64_SINT:
494 case VK_FORMAT_R64G64B64_UINT:
495 return 2;
496 default:
497 return 1;
498 }
499}
500
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600501static unsigned GetFormatType(VkFormat fmt) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700502 if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT;
503 if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT;
504 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
505 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700506 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
507 return FORMAT_TYPE_FLOAT;
508}
509
510// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
Chris Forbes062f1222018-08-21 15:34:15 -0700511// also used for input attachments, as we statically know their format.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600512static unsigned GetFundamentalType(SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700513 auto insn = src->get_def(type);
514 assert(insn != src->end());
515
516 switch (insn.opcode()) {
517 case spv::OpTypeInt:
518 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
519 case spv::OpTypeFloat:
520 return FORMAT_TYPE_FLOAT;
521 case spv::OpTypeVector:
Chris Forbes47567b72017-06-09 12:09:45 -0700522 case spv::OpTypeMatrix:
Chris Forbes47567b72017-06-09 12:09:45 -0700523 case spv::OpTypeArray:
Chris Forbes062f1222018-08-21 15:34:15 -0700524 case spv::OpTypeRuntimeArray:
525 case spv::OpTypeImage:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600526 return GetFundamentalType(src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700527 case spv::OpTypePointer:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600528 return GetFundamentalType(src, insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700529
530 default:
531 return 0;
532 }
533}
534
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600535static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700536 uint32_t bit_pos = uint32_t(u_ffs(stage));
537 return bit_pos - 1;
538}
539
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600540static spirv_inst_iter GetStructType(SHADER_MODULE_STATE const *src, spirv_inst_iter def, bool is_array_of_verts) {
Chris Forbes47567b72017-06-09 12:09:45 -0700541 while (true) {
542 if (def.opcode() == spv::OpTypePointer) {
543 def = src->get_def(def.word(3));
544 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
545 def = src->get_def(def.word(2));
546 is_array_of_verts = false;
547 } else if (def.opcode() == spv::OpTypeStruct) {
548 return def;
549 } else {
550 return src->end();
551 }
552 }
553}
554
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600555static bool CollectInterfaceBlockMembers(SHADER_MODULE_STATE const *src, std::map<location_t, interface_var> *out,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600556 std::unordered_map<unsigned, unsigned> const &blocks, bool is_array_of_verts, uint32_t id,
557 uint32_t type_id, bool is_patch, int /*first_location*/) {
Chris Forbes47567b72017-06-09 12:09:45 -0700558 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600559 auto type = GetStructType(src, src->get_def(type_id), is_array_of_verts && !is_patch);
Chris Forbes47567b72017-06-09 12:09:45 -0700560 if (type == src->end() || blocks.find(type.word(1)) == blocks.end()) {
561 // This isn't an interface block.
Chris Forbesa313d772017-06-13 13:59:41 -0700562 return false;
Chris Forbes47567b72017-06-09 12:09:45 -0700563 }
564
565 std::unordered_map<unsigned, unsigned> member_components;
566 std::unordered_map<unsigned, unsigned> member_relaxed_precision;
Chris Forbesa313d772017-06-13 13:59:41 -0700567 std::unordered_map<unsigned, unsigned> member_patch;
Chris Forbes47567b72017-06-09 12:09:45 -0700568
569 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
570 for (auto insn : *src) {
571 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
572 unsigned member_index = insn.word(2);
573
574 if (insn.word(3) == spv::DecorationComponent) {
575 unsigned component = insn.word(4);
576 member_components[member_index] = component;
577 }
578
579 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
580 member_relaxed_precision[member_index] = 1;
581 }
Chris Forbesa313d772017-06-13 13:59:41 -0700582
583 if (insn.word(3) == spv::DecorationPatch) {
584 member_patch[member_index] = 1;
585 }
Chris Forbes47567b72017-06-09 12:09:45 -0700586 }
587 }
588
Chris Forbesa313d772017-06-13 13:59:41 -0700589 // TODO: correctly handle location assignment from outside
590
Chris Forbes47567b72017-06-09 12:09:45 -0700591 // Second pass -- produce the output, from Location decorations
592 for (auto insn : *src) {
593 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
594 unsigned member_index = insn.word(2);
595 unsigned member_type_id = type.word(2 + member_index);
596
597 if (insn.word(3) == spv::DecorationLocation) {
598 unsigned location = insn.word(4);
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600599 unsigned num_locations = GetLocationsConsumedByType(src, member_type_id, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700600 auto component_it = member_components.find(member_index);
601 unsigned component = component_it == member_components.end() ? 0 : component_it->second;
602 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
Dave Houltona9df0ce2018-02-07 10:51:23 -0700603 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700604
605 for (unsigned int offset = 0; offset < num_locations; offset++) {
606 interface_var v = {};
607 v.id = id;
608 // TODO: member index in interface_var too?
609 v.type_id = member_type_id;
610 v.offset = offset;
Chris Forbesa313d772017-06-13 13:59:41 -0700611 v.is_patch = member_is_patch;
Chris Forbes47567b72017-06-09 12:09:45 -0700612 v.is_block_member = true;
613 v.is_relaxed_precision = is_relaxed_precision;
614 (*out)[std::make_pair(location + offset, component)] = v;
615 }
616 }
617 }
618 }
Chris Forbesa313d772017-06-13 13:59:41 -0700619
620 return true;
Chris Forbes47567b72017-06-09 12:09:45 -0700621}
622
Ari Suonpaa696b3432019-03-11 14:02:57 +0200623static std::vector<uint32_t> FindEntrypointInterfaces(spirv_inst_iter entrypoint) {
624 std::vector<uint32_t> interfaces;
625 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
626 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
627 uint32_t word = 3;
628 while (entrypoint.word(word) & 0xff000000u) {
629 ++word;
630 }
631 ++word;
632
633 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
634
635 return interfaces;
636}
637
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600638static std::map<location_t, interface_var> CollectInterfaceByLocation(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600639 spv::StorageClass sinterface, bool is_array_of_verts) {
Chris Forbes47567b72017-06-09 12:09:45 -0700640 std::unordered_map<unsigned, unsigned> var_locations;
641 std::unordered_map<unsigned, unsigned> var_builtins;
642 std::unordered_map<unsigned, unsigned> var_components;
643 std::unordered_map<unsigned, unsigned> blocks;
644 std::unordered_map<unsigned, unsigned> var_patch;
645 std::unordered_map<unsigned, unsigned> var_relaxed_precision;
646
647 for (auto insn : *src) {
648 // We consider two interface models: SSO rendezvous-by-location, and builtins. Complain about anything that
649 // fits neither model.
650 if (insn.opcode() == spv::OpDecorate) {
651 if (insn.word(2) == spv::DecorationLocation) {
652 var_locations[insn.word(1)] = insn.word(3);
653 }
654
655 if (insn.word(2) == spv::DecorationBuiltIn) {
656 var_builtins[insn.word(1)] = insn.word(3);
657 }
658
659 if (insn.word(2) == spv::DecorationComponent) {
660 var_components[insn.word(1)] = insn.word(3);
661 }
662
663 if (insn.word(2) == spv::DecorationBlock) {
664 blocks[insn.word(1)] = 1;
665 }
666
667 if (insn.word(2) == spv::DecorationPatch) {
668 var_patch[insn.word(1)] = 1;
669 }
670
671 if (insn.word(2) == spv::DecorationRelaxedPrecision) {
672 var_relaxed_precision[insn.word(1)] = 1;
673 }
674 }
675 }
676
677 // TODO: handle grouped decorations
678 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
679
Chris Forbes47567b72017-06-09 12:09:45 -0700680 std::map<location_t, interface_var> out;
681
Ari Suonpaa696b3432019-03-11 14:02:57 +0200682 for (uint32_t word : FindEntrypointInterfaces(entrypoint)) {
683 auto insn = src->get_def(word);
Chris Forbes47567b72017-06-09 12:09:45 -0700684 assert(insn != src->end());
685 assert(insn.opcode() == spv::OpVariable);
686
687 if (insn.word(3) == static_cast<uint32_t>(sinterface)) {
688 unsigned id = insn.word(2);
689 unsigned type = insn.word(1);
690
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600691 int location = ValueOrDefault(var_locations, id, static_cast<unsigned>(-1));
692 int builtin = ValueOrDefault(var_builtins, id, static_cast<unsigned>(-1));
693 unsigned component = ValueOrDefault(var_components, id, 0); // Unspecified is OK, is 0
Chris Forbes47567b72017-06-09 12:09:45 -0700694 bool is_patch = var_patch.find(id) != var_patch.end();
695 bool is_relaxed_precision = var_relaxed_precision.find(id) != var_relaxed_precision.end();
696
Dave Houltona9df0ce2018-02-07 10:51:23 -0700697 if (builtin != -1)
698 continue;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600699 else if (!CollectInterfaceBlockMembers(src, &out, blocks, is_array_of_verts, id, type, is_patch, location)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700700 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
701 // one result for each.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600702 unsigned num_locations = GetLocationsConsumedByType(src, type, is_array_of_verts && !is_patch);
Chris Forbes47567b72017-06-09 12:09:45 -0700703 for (unsigned int offset = 0; offset < num_locations; offset++) {
704 interface_var v = {};
705 v.id = id;
706 v.type_id = type;
707 v.offset = offset;
708 v.is_patch = is_patch;
709 v.is_relaxed_precision = is_relaxed_precision;
710 out[std::make_pair(location + offset, component)] = v;
711 }
Chris Forbes47567b72017-06-09 12:09:45 -0700712 }
713 }
714 }
715
716 return out;
717}
718
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600719static std::vector<uint32_t> CollectBuiltinBlockMembers(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint,
Ari Suonpaa696b3432019-03-11 14:02:57 +0200720 uint32_t storageClass) {
721 std::vector<uint32_t> variables;
722 std::vector<uint32_t> builtinStructMembers;
723 std::vector<uint32_t> builtinDecorations;
724
725 for (auto insn : *src) {
726 switch (insn.opcode()) {
727 // Find all built-in member decorations
728 case spv::OpMemberDecorate:
729 if (insn.word(3) == spv::DecorationBuiltIn) {
730 builtinStructMembers.push_back(insn.word(1));
731 }
732 break;
733 // Find all built-in decorations
734 case spv::OpDecorate:
735 switch (insn.word(2)) {
736 case spv::DecorationBlock: {
737 uint32_t blockID = insn.word(1);
738 for (auto builtInBlockID : builtinStructMembers) {
739 // Check if one of the members of the block are built-in -> the block is built-in
740 if (blockID == builtInBlockID) {
741 builtinDecorations.push_back(blockID);
742 break;
743 }
744 }
745 break;
746 }
747 case spv::DecorationBuiltIn:
748 builtinDecorations.push_back(insn.word(1));
749 break;
750 default:
751 break;
752 }
753 break;
754 default:
755 break;
756 }
757 }
758
759 // Find all interface variables belonging to the entrypoint and matching the storage class
760 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
761 auto def = src->get_def(id);
762 assert(def != src->end());
763 assert(def.opcode() == spv::OpVariable);
764
765 if (def.word(3) == storageClass) variables.push_back(def.word(1));
766 }
767
768 // Find all members belonging to the builtin block selected
769 std::vector<uint32_t> builtinBlockMembers;
770 for (auto &var : variables) {
771 auto def = src->get_def(src->get_def(var).word(3));
772
773 // It could be an array of IO blocks. The element type should be the struct defining the block contents
774 if (def.opcode() == spv::OpTypeArray) def = src->get_def(def.word(2));
775
776 // Now find all members belonging to the struct defining the IO block
777 if (def.opcode() == spv::OpTypeStruct) {
778 for (auto builtInID : builtinDecorations) {
779 if (builtInID == def.word(1)) {
780 for (int i = 2; i < (int)def.len(); i++)
781 builtinBlockMembers.push_back(spv::BuiltInMax); // Start with undefined builtin for each struct member.
782 // These shouldn't be left after replacing.
783 for (auto insn : *src) {
784 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == builtInID &&
785 insn.word(3) == spv::DecorationBuiltIn) {
786 auto structIndex = insn.word(2);
787 assert(structIndex < builtinBlockMembers.size());
788 builtinBlockMembers[structIndex] = insn.word(4);
789 }
790 }
791 }
792 }
793 }
794 }
795
796 return builtinBlockMembers;
797}
798
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600799static std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex(
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600800 SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids) {
Chris Forbes47567b72017-06-09 12:09:45 -0700801 std::vector<std::pair<uint32_t, interface_var>> out;
802
803 for (auto insn : *src) {
804 if (insn.opcode() == spv::OpDecorate) {
805 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
806 auto attachment_index = insn.word(3);
807 auto id = insn.word(1);
808
809 if (accessible_ids.count(id)) {
810 auto def = src->get_def(id);
811 assert(def != src->end());
812
813 if (def.opcode() == spv::OpVariable && insn.word(3) == spv::StorageClassUniformConstant) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600814 auto num_locations = GetLocationsConsumedByType(src, def.word(1), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700815 for (unsigned int offset = 0; offset < num_locations; offset++) {
816 interface_var v = {};
817 v.id = id;
818 v.type_id = def.word(1);
819 v.offset = offset;
820 out.emplace_back(attachment_index + offset, v);
821 }
822 }
823 }
824 }
825 }
826 }
827
828 return out;
829}
830
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600831static bool IsWritableDescriptorType(SHADER_MODULE_STATE const *module, uint32_t type_id, bool is_storage_buffer) {
Chris Forbes8af24522018-03-07 11:37:45 -0800832 auto type = module->get_def(type_id);
833
834 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
Chris Forbes062f1222018-08-21 15:34:15 -0700835 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
836 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray) {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700837 type = module->get_def(type.word(2)); // Element type
Chris Forbes8af24522018-03-07 11:37:45 -0800838 } else {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700839 type = module->get_def(type.word(3)); // Pointee type
Chris Forbes8af24522018-03-07 11:37:45 -0800840 }
841 }
842
843 switch (type.opcode()) {
844 case spv::OpTypeImage: {
845 auto dim = type.word(3);
846 auto sampled = type.word(7);
847 return sampled == 2 && dim != spv::DimSubpassData;
848 }
849
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700850 case spv::OpTypeStruct: {
851 std::unordered_set<unsigned> nonwritable_members;
Chris Forbes8af24522018-03-07 11:37:45 -0800852 for (auto insn : *module) {
853 if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) {
854 if (insn.word(2) == spv::DecorationBufferBlock) {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700855 // Legacy storage block in the Uniform storage class
856 // has its struct type decorated with BufferBlock.
857 is_storage_buffer = true;
Chris Forbes8af24522018-03-07 11:37:45 -0800858 }
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700859 } else if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1) &&
860 insn.word(3) == spv::DecorationNonWritable) {
861 nonwritable_members.insert(insn.word(2));
Chris Forbes8af24522018-03-07 11:37:45 -0800862 }
863 }
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700864
865 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
866 // as nonwritable.
867 return is_storage_buffer && nonwritable_members.size() != type.len() - 2;
868 }
Chris Forbes8af24522018-03-07 11:37:45 -0800869 }
870
871 return false;
872}
873
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600874static std::vector<std::pair<descriptor_slot_t, interface_var>> CollectInterfaceByDescriptorSlot(
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600875 debug_report_data const *report_data, SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids,
Chris Forbes8af24522018-03-07 11:37:45 -0800876 bool *has_writable_descriptor) {
Chris Forbes47567b72017-06-09 12:09:45 -0700877 std::unordered_map<unsigned, unsigned> var_sets;
878 std::unordered_map<unsigned, unsigned> var_bindings;
Chris Forbes8af24522018-03-07 11:37:45 -0800879 std::unordered_map<unsigned, unsigned> var_nonwritable;
Chris Forbes47567b72017-06-09 12:09:45 -0700880
881 for (auto insn : *src) {
882 // All variables in the Uniform or UniformConstant storage classes are required to be decorated with both
883 // DecorationDescriptorSet and DecorationBinding.
884 if (insn.opcode() == spv::OpDecorate) {
885 if (insn.word(2) == spv::DecorationDescriptorSet) {
886 var_sets[insn.word(1)] = insn.word(3);
887 }
888
889 if (insn.word(2) == spv::DecorationBinding) {
890 var_bindings[insn.word(1)] = insn.word(3);
891 }
Chris Forbes8af24522018-03-07 11:37:45 -0800892
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700893 // Note: do toplevel DecorationNonWritable out here; it applies to
894 // the OpVariable rather than the type.
Chris Forbes8af24522018-03-07 11:37:45 -0800895 if (insn.word(2) == spv::DecorationNonWritable) {
896 var_nonwritable[insn.word(1)] = 1;
897 }
Chris Forbes47567b72017-06-09 12:09:45 -0700898 }
899 }
900
901 std::vector<std::pair<descriptor_slot_t, interface_var>> out;
902
903 for (auto id : accessible_ids) {
904 auto insn = src->get_def(id);
905 assert(insn != src->end());
906
907 if (insn.opcode() == spv::OpVariable &&
Chris Forbes9f89d752018-03-07 12:57:48 -0800908 (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant ||
909 insn.word(3) == spv::StorageClassStorageBuffer)) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600910 unsigned set = ValueOrDefault(var_sets, insn.word(2), 0);
911 unsigned binding = ValueOrDefault(var_bindings, insn.word(2), 0);
Chris Forbes47567b72017-06-09 12:09:45 -0700912
913 interface_var v = {};
914 v.id = insn.word(2);
915 v.type_id = insn.word(1);
916 out.emplace_back(std::make_pair(set, binding), v);
Chris Forbes8af24522018-03-07 11:37:45 -0800917
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700918 if (var_nonwritable.find(id) == var_nonwritable.end() &&
919 IsWritableDescriptorType(src, insn.word(1), insn.word(3) == spv::StorageClassStorageBuffer)) {
Chris Forbes8af24522018-03-07 11:37:45 -0800920 *has_writable_descriptor = true;
921 }
Chris Forbes47567b72017-06-09 12:09:45 -0700922 }
923 }
924
925 return out;
926}
927
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600928static bool ValidateViConsistency(debug_report_data const *report_data, VkPipelineVertexInputStateCreateInfo const *vi) {
Chris Forbes47567b72017-06-09 12:09:45 -0700929 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
930 // be specified only once.
931 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
932 bool skip = false;
933
934 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
935 auto desc = &vi->pVertexBindingDescriptions[i];
936 auto &binding = bindings[desc->binding];
937 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600938 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600939 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -0600940 kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
Chris Forbes47567b72017-06-09 12:09:45 -0700941 desc->binding);
942 } else {
943 binding = desc;
944 }
945 }
946
947 return skip;
948}
949
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600950static bool ValidateViAgainstVsInputs(debug_report_data const *report_data, VkPipelineVertexInputStateCreateInfo const *vi,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600951 SHADER_MODULE_STATE const *vs, spirv_inst_iter entrypoint) {
Chris Forbes47567b72017-06-09 12:09:45 -0700952 bool skip = false;
953
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600954 auto inputs = CollectInterfaceByLocation(vs, entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700955
956 // Build index by location
957 std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs;
958 if (vi) {
959 for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600960 auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
Chris Forbes47567b72017-06-09 12:09:45 -0700961 for (auto j = 0u; j < num_locations; j++) {
962 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
963 }
964 }
965 }
966
967 auto it_a = attribs.begin();
968 auto it_b = inputs.begin();
969 bool used = false;
970
971 while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) {
972 bool a_at_end = attribs.size() == 0 || it_a == attribs.end();
973 bool b_at_end = inputs.size() == 0 || it_b == inputs.end();
974 auto a_first = a_at_end ? 0 : it_a->first;
975 auto b_first = b_at_end ? 0 : it_b->first.first;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600976
Chris Forbes47567b72017-06-09 12:09:45 -0700977 if (!a_at_end && (b_at_end || a_first < b_first)) {
Mark Young4e919b22018-05-21 15:53:59 -0600978 if (!used &&
979 log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Dave Houlton51653902018-06-22 17:32:13 -0600980 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed,
Mark Young4e919b22018-05-21 15:53:59 -0600981 "Vertex attribute at location %d not consumed by vertex shader", a_first)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700982 skip = true;
983 }
984 used = false;
985 it_a++;
986 } else if (!b_at_end && (a_at_end || b_first < a_first)) {
Mark Young4e919b22018-05-21 15:53:59 -0600987 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Dave Houlton51653902018-06-22 17:32:13 -0600988 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
Mark Young4e919b22018-05-21 15:53:59 -0600989 "Vertex shader consumes input at location %d but not provided", b_first);
Chris Forbes47567b72017-06-09 12:09:45 -0700990 it_b++;
991 } else {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600992 unsigned attrib_type = GetFormatType(it_a->second->format);
993 unsigned input_type = GetFundamentalType(vs, it_b->second.type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700994
995 // Type checking
996 if (!(attrib_type & input_type)) {
Mark Young4e919b22018-05-21 15:53:59 -0600997 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Dave Houlton51653902018-06-22 17:32:13 -0600998 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Chris Forbes47567b72017-06-09 12:09:45 -0700999 "Attribute type of `%s` at location %d does not match vertex shader input type of `%s`",
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001000 string_VkFormat(it_a->second->format), a_first, DescribeType(vs, it_b->second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001001 }
1002
1003 // OK!
1004 used = true;
1005 it_b++;
1006 }
1007 }
1008
1009 return skip;
1010}
1011
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001012static bool ValidateFsOutputsAgainstRenderPass(debug_report_data const *report_data, SHADER_MODULE_STATE const *fs,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001013 spirv_inst_iter entrypoint, PIPELINE_STATE const *pipeline, uint32_t subpass_index) {
Petr Krause91f7a12017-12-14 20:57:36 +01001014 auto rpci = pipeline->rp_state->createInfo.ptr();
Chris Forbes8bca1652017-07-20 11:10:09 -07001015
Chris Forbes47567b72017-06-09 12:09:45 -07001016 std::map<uint32_t, VkFormat> color_attachments;
1017 auto subpass = rpci->pSubpasses[subpass_index];
1018 for (auto i = 0u; i < subpass.colorAttachmentCount; ++i) {
1019 uint32_t attachment = subpass.pColorAttachments[i].attachment;
1020 if (attachment == VK_ATTACHMENT_UNUSED) continue;
1021 if (rpci->pAttachments[attachment].format != VK_FORMAT_UNDEFINED) {
1022 color_attachments[i] = rpci->pAttachments[attachment].format;
1023 }
1024 }
1025
1026 bool skip = false;
1027
1028 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
1029
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001030 auto outputs = CollectInterfaceByLocation(fs, entrypoint, spv::StorageClassOutput, false);
Chris Forbes47567b72017-06-09 12:09:45 -07001031
1032 auto it_a = outputs.begin();
1033 auto it_b = color_attachments.begin();
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -06001034 bool used = false;
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001035 bool alphaToCoverageEnabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL &&
1036 pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
1037 bool locationZeroHasAlpha = false;
Chris Forbes47567b72017-06-09 12:09:45 -07001038
1039 // Walk attachment list and outputs together
1040
1041 while ((outputs.size() > 0 && it_a != outputs.end()) || (color_attachments.size() > 0 && it_b != color_attachments.end())) {
1042 bool a_at_end = outputs.size() == 0 || it_a == outputs.end();
1043 bool b_at_end = color_attachments.size() == 0 || it_b == color_attachments.end();
1044
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001045 if (!a_at_end && it_a->first.first == 0 && fs->get_def(it_a->second.type_id) != fs->end() &&
1046 GetComponentsConsumedByType(fs, it_a->second.type_id, false) == 4)
1047 locationZeroHasAlpha = true;
1048
Chris Forbes47567b72017-06-09 12:09:45 -07001049 if (!a_at_end && (b_at_end || it_a->first.first < it_b->first)) {
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001050 if (!alphaToCoverageEnabled || it_a->first.first != 0) {
1051 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1052 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed,
1053 "fragment shader writes to output location %d with no matching attachment", it_a->first.first);
1054 }
Chris Forbes47567b72017-06-09 12:09:45 -07001055 it_a++;
1056 } else if (!b_at_end && (a_at_end || it_a->first.first > it_b->first)) {
Chris Forbesefdd4082017-07-20 11:19:16 -07001057 // Only complain if there are unmasked channels for this attachment. If the writemask is 0, it's acceptable for the
1058 // shader to not produce a matching output.
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -06001059 if (!used) {
1060 if (pipeline->attachments[it_b->first].colorWriteMask != 0) {
Chris Forbescfe4dca2018-10-05 10:15:00 -07001061 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -06001062 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
Chris Forbescfe4dca2018-10-05 10:15:00 -07001063 "Attachment %d not written by fragment shader; undefined values will be written to attachment",
1064 it_b->first);
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -06001065 }
Chris Forbesefdd4082017-07-20 11:19:16 -07001066 }
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -06001067 used = false;
Chris Forbes47567b72017-06-09 12:09:45 -07001068 it_b++;
1069 } else {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001070 unsigned output_type = GetFundamentalType(fs, it_a->second.type_id);
1071 unsigned att_type = GetFormatType(it_b->second);
Chris Forbes47567b72017-06-09 12:09:45 -07001072
1073 // Type checking
1074 if (!(output_type & att_type)) {
Chris Forbescfe4dca2018-10-05 10:15:00 -07001075 skip |= log_msg(
1076 report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1077 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
1078 "Attachment %d of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
1079 it_b->first, string_VkFormat(it_b->second), DescribeType(fs, it_a->second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001080 }
1081
1082 // OK!
1083 it_a++;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -06001084 used = true;
Chris Forbes47567b72017-06-09 12:09:45 -07001085 }
1086 }
1087
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001088 if (alphaToCoverageEnabled && !locationZeroHasAlpha) {
1089 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1090 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
1091 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
1092 }
1093
Chris Forbes47567b72017-06-09 12:09:45 -07001094 return skip;
1095}
1096
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001097// For PointSize analysis we need to know if the variable decorated with the PointSize built-in was actually written to.
1098// This function examines instructions in the static call tree for a write to this variable.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001099static bool IsPointSizeWritten(SHADER_MODULE_STATE const *src, spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001100 auto type = builtin_instr.opcode();
1101 uint32_t target_id = builtin_instr.word(1);
1102 bool init_complete = false;
1103
1104 if (type == spv::OpMemberDecorate) {
1105 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1106 auto insn = entrypoint;
1107 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1108 switch (insn.opcode()) {
1109 case spv::OpTypePointer:
1110 if ((insn.word(3) == target_id) && (insn.word(2) == spv::StorageClassOutput)) {
1111 target_id = insn.word(1);
1112 }
1113 break;
1114 case spv::OpVariable:
1115 if (insn.word(1) == target_id) {
1116 target_id = insn.word(2);
1117 init_complete = true;
1118 }
1119 break;
1120 }
1121 insn++;
1122 }
1123 }
1124
Mark Lobodzinskif84b0b42018-09-11 14:54:32 -06001125 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1126
1127 bool found_write = false;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001128 std::unordered_set<uint32_t> worklist;
1129 worklist.insert(entrypoint.word(2));
1130
1131 // Follow instructions in call graph looking for writes to target
1132 while (!worklist.empty() && !found_write) {
1133 auto id_iter = worklist.begin();
1134 auto id = *id_iter;
1135 worklist.erase(id_iter);
1136
1137 auto insn = src->get_def(id);
1138 if (insn == src->end()) {
1139 continue;
1140 }
1141
1142 if (insn.opcode() == spv::OpFunction) {
1143 // Scan body of function looking for other function calls or items in our ID chain
1144 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
1145 switch (insn.opcode()) {
1146 case spv::OpAccessChain:
1147 if (insn.word(3) == target_id) {
1148 if (type == spv::OpMemberDecorate) {
1149 auto value = GetConstantValue(src, insn.word(4));
1150 if (value == builtin_instr.word(2)) {
1151 target_id = insn.word(2);
1152 }
1153 } else {
1154 target_id = insn.word(2);
1155 }
1156 }
1157 break;
1158 case spv::OpStore:
1159 if (insn.word(1) == target_id) {
1160 found_write = true;
1161 }
1162 break;
1163 case spv::OpFunctionCall:
1164 worklist.insert(insn.word(3));
1165 break;
1166 }
1167 }
1168 }
1169 }
1170 return found_write;
1171}
1172
Chris Forbes47567b72017-06-09 12:09:45 -07001173// For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is
1174// important for identifying the set of shader resources actually used by an entrypoint, for example.
1175// Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
1176// - NOT the shader input/output interfaces.
1177//
1178// TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
1179// converting parts of this to be generated from the machine-readable spec instead.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001180static std::unordered_set<uint32_t> MarkAccessibleIds(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) {
Chris Forbes47567b72017-06-09 12:09:45 -07001181 std::unordered_set<uint32_t> ids;
1182 std::unordered_set<uint32_t> worklist;
1183 worklist.insert(entrypoint.word(2));
1184
1185 while (!worklist.empty()) {
1186 auto id_iter = worklist.begin();
1187 auto id = *id_iter;
1188 worklist.erase(id_iter);
1189
1190 auto insn = src->get_def(id);
1191 if (insn == src->end()) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001192 // ID is something we didn't collect in BuildDefIndex. that's OK -- we'll stumble across all kinds of things here
Chris Forbes47567b72017-06-09 12:09:45 -07001193 // that we may not care about.
1194 continue;
1195 }
1196
1197 // Try to add to the output set
1198 if (!ids.insert(id).second) {
1199 continue; // If we already saw this id, we don't want to walk it again.
1200 }
1201
1202 switch (insn.opcode()) {
1203 case spv::OpFunction:
1204 // Scan whole body of the function, enlisting anything interesting
1205 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
1206 switch (insn.opcode()) {
1207 case spv::OpLoad:
1208 case spv::OpAtomicLoad:
1209 case spv::OpAtomicExchange:
1210 case spv::OpAtomicCompareExchange:
1211 case spv::OpAtomicCompareExchangeWeak:
1212 case spv::OpAtomicIIncrement:
1213 case spv::OpAtomicIDecrement:
1214 case spv::OpAtomicIAdd:
1215 case spv::OpAtomicISub:
1216 case spv::OpAtomicSMin:
1217 case spv::OpAtomicUMin:
1218 case spv::OpAtomicSMax:
1219 case spv::OpAtomicUMax:
1220 case spv::OpAtomicAnd:
1221 case spv::OpAtomicOr:
1222 case spv::OpAtomicXor:
1223 worklist.insert(insn.word(3)); // ptr
1224 break;
1225 case spv::OpStore:
1226 case spv::OpAtomicStore:
1227 worklist.insert(insn.word(1)); // ptr
1228 break;
1229 case spv::OpAccessChain:
1230 case spv::OpInBoundsAccessChain:
1231 worklist.insert(insn.word(3)); // base ptr
1232 break;
1233 case spv::OpSampledImage:
1234 case spv::OpImageSampleImplicitLod:
1235 case spv::OpImageSampleExplicitLod:
1236 case spv::OpImageSampleDrefImplicitLod:
1237 case spv::OpImageSampleDrefExplicitLod:
1238 case spv::OpImageSampleProjImplicitLod:
1239 case spv::OpImageSampleProjExplicitLod:
1240 case spv::OpImageSampleProjDrefImplicitLod:
1241 case spv::OpImageSampleProjDrefExplicitLod:
1242 case spv::OpImageFetch:
1243 case spv::OpImageGather:
1244 case spv::OpImageDrefGather:
1245 case spv::OpImageRead:
1246 case spv::OpImage:
1247 case spv::OpImageQueryFormat:
1248 case spv::OpImageQueryOrder:
1249 case spv::OpImageQuerySizeLod:
1250 case spv::OpImageQuerySize:
1251 case spv::OpImageQueryLod:
1252 case spv::OpImageQueryLevels:
1253 case spv::OpImageQuerySamples:
1254 case spv::OpImageSparseSampleImplicitLod:
1255 case spv::OpImageSparseSampleExplicitLod:
1256 case spv::OpImageSparseSampleDrefImplicitLod:
1257 case spv::OpImageSparseSampleDrefExplicitLod:
1258 case spv::OpImageSparseSampleProjImplicitLod:
1259 case spv::OpImageSparseSampleProjExplicitLod:
1260 case spv::OpImageSparseSampleProjDrefImplicitLod:
1261 case spv::OpImageSparseSampleProjDrefExplicitLod:
1262 case spv::OpImageSparseFetch:
1263 case spv::OpImageSparseGather:
1264 case spv::OpImageSparseDrefGather:
1265 case spv::OpImageTexelPointer:
1266 worklist.insert(insn.word(3)); // Image or sampled image
1267 break;
1268 case spv::OpImageWrite:
1269 worklist.insert(insn.word(1)); // Image -- different operand order to above
1270 break;
1271 case spv::OpFunctionCall:
1272 for (uint32_t i = 3; i < insn.len(); i++) {
1273 worklist.insert(insn.word(i)); // fn itself, and all args
1274 }
1275 break;
1276
1277 case spv::OpExtInst:
1278 for (uint32_t i = 5; i < insn.len(); i++) {
1279 worklist.insert(insn.word(i)); // Operands to ext inst
1280 }
1281 break;
1282 }
1283 }
1284 break;
1285 }
1286 }
1287
1288 return ids;
1289}
1290
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001291static bool ValidatePushConstantBlockAgainstPipeline(debug_report_data const *report_data,
1292 std::vector<VkPushConstantRange> const *push_constant_ranges,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001293 SHADER_MODULE_STATE const *src, spirv_inst_iter type,
1294 VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -07001295 bool skip = false;
1296
1297 // Strip off ptrs etc
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001298 type = GetStructType(src, type, false);
Chris Forbes47567b72017-06-09 12:09:45 -07001299 assert(type != src->end());
1300
1301 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
1302 // TODO: arrays, matrices, weird sizes
1303 for (auto insn : *src) {
1304 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
1305 if (insn.word(3) == spv::DecorationOffset) {
1306 unsigned offset = insn.word(4);
1307 auto size = 4; // Bytes; TODO: calculate this based on the type
1308
1309 bool found_range = false;
1310 for (auto const &range : *push_constant_ranges) {
1311 if (range.offset <= offset && range.offset + range.size >= offset + size) {
1312 found_range = true;
1313
1314 if ((range.stageFlags & stage) == 0) {
Dave Houltona9df0ce2018-02-07 10:51:23 -07001315 skip |=
1316 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06001317 kVUID_Core_Shader_PushConstantNotAccessibleFromStage,
Dave Houltona9df0ce2018-02-07 10:51:23 -07001318 "Push constant range covering variable starting at offset %u not accessible from stage %s",
1319 offset, string_VkShaderStageFlagBits(stage));
Chris Forbes47567b72017-06-09 12:09:45 -07001320 }
1321
1322 break;
1323 }
1324 }
1325
1326 if (!found_range) {
1327 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06001328 kVUID_Core_Shader_PushConstantOutOfRange,
Dave Houltona9df0ce2018-02-07 10:51:23 -07001329 "Push constant range covering variable starting at offset %u not declared in layout", offset);
Chris Forbes47567b72017-06-09 12:09:45 -07001330 }
1331 }
1332 }
1333 }
1334
1335 return skip;
1336}
1337
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001338static bool ValidatePushConstantUsage(debug_report_data const *report_data,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001339 std::vector<VkPushConstantRange> const *push_constant_ranges, SHADER_MODULE_STATE const *src,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001340 std::unordered_set<uint32_t> accessible_ids, VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -07001341 bool skip = false;
1342
1343 for (auto id : accessible_ids) {
1344 auto def_insn = src->get_def(id);
1345 if (def_insn.opcode() == spv::OpVariable && def_insn.word(3) == spv::StorageClassPushConstant) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001346 skip |= ValidatePushConstantBlockAgainstPipeline(report_data, push_constant_ranges, src, src->get_def(def_insn.word(1)),
1347 stage);
Chris Forbes47567b72017-06-09 12:09:45 -07001348 }
1349 }
1350
1351 return skip;
1352}
1353
1354// Validate that data for each specialization entry is fully contained within the buffer.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001355static bool ValidateSpecializationOffsets(debug_report_data const *report_data, VkPipelineShaderStageCreateInfo const *info) {
Chris Forbes47567b72017-06-09 12:09:45 -07001356 bool skip = false;
1357
1358 VkSpecializationInfo const *spec = info->pSpecializationInfo;
1359
1360 if (spec) {
1361 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Dave Houlton78d09922018-05-17 15:48:45 -06001362 // TODO: This is a good place for "VUID-VkSpecializationInfo-offset-00773".
Chris Forbes47567b72017-06-09 12:09:45 -07001363 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001364 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0,
Dave Houlton78d09922018-05-17 15:48:45 -06001365 "VUID-VkSpecializationInfo-pMapEntries-00774",
Dave Houltona9df0ce2018-02-07 10:51:23 -07001366 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001367 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..",
Dave Houltona9df0ce2018-02-07 10:51:23 -07001368 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001369 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -07001370 }
1371 }
1372 }
1373
1374 return skip;
1375}
1376
Jeff Bolz38b3ce72018-09-19 12:53:38 -05001377// TODO (jbolz): Can this return a const reference?
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001378static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count) {
Chris Forbes47567b72017-06-09 12:09:45 -07001379 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -08001380 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -07001381 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -05001382 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001383
1384 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
Jeff Bolzfdf96072018-04-10 14:32:18 -05001385 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
1386 if (type.opcode() == spv::OpTypeRuntimeArray) {
1387 descriptor_count = 0;
1388 type = module->get_def(type.word(2));
1389 } else if (type.opcode() == spv::OpTypeArray) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001390 descriptor_count *= GetConstantValue(module, type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -07001391 type = module->get_def(type.word(2));
1392 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -08001393 if (type.word(2) == spv::StorageClassStorageBuffer) {
1394 is_storage_buffer = true;
1395 }
Chris Forbes47567b72017-06-09 12:09:45 -07001396 type = module->get_def(type.word(3));
1397 }
1398 }
1399
1400 switch (type.opcode()) {
1401 case spv::OpTypeStruct: {
1402 for (auto insn : *module) {
1403 if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) {
1404 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -08001405 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001406 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1407 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
1408 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -08001409 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001410 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
1411 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
1412 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
1413 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -08001414 }
Chris Forbes47567b72017-06-09 12:09:45 -07001415 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001416 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1417 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
1418 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001419 }
1420 }
1421 }
1422
1423 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -05001424 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001425 }
1426
1427 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -05001428 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
1429 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1430 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001431
Chris Forbes73c00bf2018-06-22 16:28:06 -07001432 case spv::OpTypeSampledImage: {
1433 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
1434 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
1435 auto image_type = module->get_def(type.word(2));
1436 auto dim = image_type.word(3);
1437 auto sampled = image_type.word(7);
1438 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001439 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
1440 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001441 }
Chris Forbes73c00bf2018-06-22 16:28:06 -07001442 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001443 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1444 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001445
1446 case spv::OpTypeImage: {
1447 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
1448 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
1449 auto dim = type.word(3);
1450 auto sampled = type.word(7);
1451
1452 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001453 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
1454 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001455 } else if (dim == spv::DimBuffer) {
1456 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001457 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
1458 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001459 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001460 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
1461 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001462 }
1463 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001464 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
1465 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1466 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001467 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001468 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
1469 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001470 }
1471 }
Shannon McPherson0fa28232018-11-01 11:59:02 -06001472 case spv::OpTypeAccelerationStructureNV:
Eric Werness30127fd2018-10-31 21:01:03 -07001473 ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -05001474 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001475
1476 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
1477 default:
Jeff Bolze54ae892018-09-08 12:16:29 -05001478 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -07001479 }
1480}
1481
Jeff Bolze54ae892018-09-08 12:16:29 -05001482static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -07001483 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -05001484 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
1485 if (ss.tellp()) ss << ", ";
1486 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -07001487 }
1488 return ss.str();
1489}
1490
Jeff Bolzee743412019-06-20 22:24:32 -05001491static bool RequirePropertyFlag(debug_report_data const *report_data, VkBool32 check, char const *flag, char const *structure) {
1492 if (!check) {
1493 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
1494 kVUID_Core_Shader_ExceedDeviceLimit, "Shader requires flag %s set in %s but it is not set on the device", flag,
1495 structure)) {
1496 return true;
1497 }
1498 }
1499
1500 return false;
1501}
1502
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001503static bool RequireFeature(debug_report_data const *report_data, VkBool32 feature, char const *feature_name) {
Chris Forbes47567b72017-06-09 12:09:45 -07001504 if (!feature) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001505 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06001506 kVUID_Core_Shader_FeatureNotEnabled, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -07001507 return true;
1508 }
1509 }
1510
1511 return false;
1512}
1513
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001514static bool RequireExtension(debug_report_data const *report_data, bool extension, char const *extension_name) {
Chris Forbes47567b72017-06-09 12:09:45 -07001515 if (!extension) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001516 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06001517 kVUID_Core_Shader_FeatureNotEnabled, "Shader requires extension %s but is not enabled on the device",
Chris Forbes47567b72017-06-09 12:09:45 -07001518 extension_name)) {
1519 return true;
1520 }
1521 }
1522
1523 return false;
1524}
1525
Jeff Bolzee743412019-06-20 22:24:32 -05001526bool CoreChecks::ValidateShaderCapabilities(SHADER_MODULE_STATE const *src, VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -07001527 bool skip = false;
1528
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001529 struct FeaturePointer {
1530 // Callable object to test if this feature is enabled in the given aggregate feature struct
1531 const std::function<VkBool32(const DeviceFeatures &)> IsEnabled;
1532
1533 // Test if feature pointer is populated
1534 explicit operator bool() const { return static_cast<bool>(IsEnabled); }
1535
1536 // Default and nullptr constructor to create an empty FeaturePointer
1537 FeaturePointer() : IsEnabled(nullptr) {}
1538 FeaturePointer(std::nullptr_t ptr) : IsEnabled(nullptr) {}
1539
1540 // Constructors to populate FeaturePointer based on given pointer to member
1541 FeaturePointer(VkBool32 VkPhysicalDeviceFeatures::*ptr)
1542 : IsEnabled([=](const DeviceFeatures &features) { return features.core.*ptr; }) {}
1543 FeaturePointer(VkBool32 VkPhysicalDeviceDescriptorIndexingFeaturesEXT::*ptr)
1544 : IsEnabled([=](const DeviceFeatures &features) { return features.descriptor_indexing.*ptr; }) {}
1545 FeaturePointer(VkBool32 VkPhysicalDevice8BitStorageFeaturesKHR::*ptr)
1546 : IsEnabled([=](const DeviceFeatures &features) { return features.eight_bit_storage.*ptr; }) {}
Brett Lawsonbebfb6f2018-10-23 16:58:50 -07001547 FeaturePointer(VkBool32 VkPhysicalDeviceTransformFeedbackFeaturesEXT::*ptr)
1548 : IsEnabled([=](const DeviceFeatures &features) { return features.transform_feedback_features.*ptr; }) {}
Jose-Emilio Munoz-Lopez1109b452018-08-21 09:44:07 +01001549 FeaturePointer(VkBool32 VkPhysicalDeviceFloat16Int8FeaturesKHR::*ptr)
1550 : IsEnabled([=](const DeviceFeatures &features) { return features.float16_int8.*ptr; }) {}
Tobias Hector6a0ece72018-12-10 12:24:05 +00001551 FeaturePointer(VkBool32 VkPhysicalDeviceScalarBlockLayoutFeaturesEXT::*ptr)
1552 : IsEnabled([=](const DeviceFeatures &features) { return features.scalar_block_layout_features.*ptr; }) {}
Jeff Bolze4356752019-03-07 11:23:46 -06001553 FeaturePointer(VkBool32 VkPhysicalDeviceCooperativeMatrixFeaturesNV::*ptr)
1554 : IsEnabled([=](const DeviceFeatures &features) { return features.cooperative_matrix_features.*ptr; }) {}
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001555 FeaturePointer(VkBool32 VkPhysicalDeviceFloatControlsPropertiesKHR::*ptr)
1556 : IsEnabled([=](const DeviceFeatures &features) { return features.float_controls.*ptr; }) {}
Jason Macnakc5a621d2019-06-10 12:42:50 -07001557 FeaturePointer(VkBool32 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::*ptr)
1558 : IsEnabled([=](const DeviceFeatures &features) { return features.compute_shader_derivatives_features.*ptr; }) {}
Jason Macnak325e8b52019-06-10 13:33:10 -07001559 FeaturePointer(VkBool32 VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::*ptr)
1560 : IsEnabled([=](const DeviceFeatures &features) { return features.fragment_shader_barycentric_features.*ptr; }) {}
Jason Macnakd7fddf82019-06-13 09:52:49 -07001561 FeaturePointer(VkBool32 VkPhysicalDeviceShaderImageFootprintFeaturesNV::*ptr)
1562 : IsEnabled([=](const DeviceFeatures &features) { return features.shader_image_footprint_features.*ptr; }) {}
Jeff Bolz38f6cb52019-06-30 16:26:44 -05001563 FeaturePointer(VkBool32 VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::*ptr)
1564 : IsEnabled([=](const DeviceFeatures &features) { return features.fragment_shader_interlock_features.*ptr; }) {}
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001565 };
1566
Chris Forbes47567b72017-06-09 12:09:45 -07001567 struct CapabilityInfo {
1568 char const *name;
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001569 FeaturePointer feature;
1570 bool DeviceExtensions::*extension;
Chris Forbes47567b72017-06-09 12:09:45 -07001571 };
1572
Chris Forbes47567b72017-06-09 12:09:45 -07001573 // clang-format off
Dave Houltoneb10ea82017-12-22 12:21:50 -07001574 static const std::unordered_multimap<uint32_t, CapabilityInfo> capabilities = {
Chris Forbes47567b72017-06-09 12:09:45 -07001575 // Capabilities always supported by a Vulkan 1.0 implementation -- no
1576 // feature bits.
1577 {spv::CapabilityMatrix, {nullptr}},
1578 {spv::CapabilityShader, {nullptr}},
1579 {spv::CapabilityInputAttachment, {nullptr}},
1580 {spv::CapabilitySampled1D, {nullptr}},
1581 {spv::CapabilityImage1D, {nullptr}},
1582 {spv::CapabilitySampledBuffer, {nullptr}},
Toni Merilehtib13a4a22019-05-21 12:58:44 +03001583 {spv::CapabilityStorageImageExtendedFormats, {nullptr}},
Chris Forbes47567b72017-06-09 12:09:45 -07001584 {spv::CapabilityImageQuery, {nullptr}},
1585 {spv::CapabilityDerivativeControl, {nullptr}},
1586
1587 // Capabilities that are optionally supported, but require a feature to
1588 // be enabled on the device
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001589 {spv::CapabilityGeometry, {"VkPhysicalDeviceFeatures::geometryShader", &VkPhysicalDeviceFeatures::geometryShader}},
1590 {spv::CapabilityTessellation, {"VkPhysicalDeviceFeatures::tessellationShader", &VkPhysicalDeviceFeatures::tessellationShader}},
1591 {spv::CapabilityFloat64, {"VkPhysicalDeviceFeatures::shaderFloat64", &VkPhysicalDeviceFeatures::shaderFloat64}},
1592 {spv::CapabilityInt64, {"VkPhysicalDeviceFeatures::shaderInt64", &VkPhysicalDeviceFeatures::shaderInt64}},
1593 {spv::CapabilityTessellationPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}},
1594 {spv::CapabilityGeometryPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}},
1595 {spv::CapabilityImageGatherExtended, {"VkPhysicalDeviceFeatures::shaderImageGatherExtended", &VkPhysicalDeviceFeatures::shaderImageGatherExtended}},
1596 {spv::CapabilityStorageImageMultisample, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}},
1597 {spv::CapabilityUniformBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing}},
1598 {spv::CapabilitySampledImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing}},
1599 {spv::CapabilityStorageBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}},
1600 {spv::CapabilityStorageImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}},
1601 {spv::CapabilityClipDistance, {"VkPhysicalDeviceFeatures::shaderClipDistance", &VkPhysicalDeviceFeatures::shaderClipDistance}},
1602 {spv::CapabilityCullDistance, {"VkPhysicalDeviceFeatures::shaderCullDistance", &VkPhysicalDeviceFeatures::shaderCullDistance}},
1603 {spv::CapabilityImageCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}},
1604 {spv::CapabilitySampleRateShading, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}},
1605 {spv::CapabilitySparseResidency, {"VkPhysicalDeviceFeatures::shaderResourceResidency", &VkPhysicalDeviceFeatures::shaderResourceResidency}},
1606 {spv::CapabilityMinLod, {"VkPhysicalDeviceFeatures::shaderResourceMinLod", &VkPhysicalDeviceFeatures::shaderResourceMinLod}},
1607 {spv::CapabilitySampledCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}},
1608 {spv::CapabilityImageMSArray, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}},
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001609 {spv::CapabilityInterpolationFunction, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}},
1610 {spv::CapabilityStorageImageReadWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat}},
1611 {spv::CapabilityStorageImageWriteWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat}},
1612 {spv::CapabilityMultiViewport, {"VkPhysicalDeviceFeatures::multiViewport", &VkPhysicalDeviceFeatures::multiViewport}},
Jeff Bolzfdf96072018-04-10 14:32:18 -05001613
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001614 {spv::CapabilityShaderNonUniformEXT, {VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_descriptor_indexing}},
1615 {spv::CapabilityRuntimeDescriptorArrayEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::runtimeDescriptorArray", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::runtimeDescriptorArray}},
1616 {spv::CapabilityInputAttachmentArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderInputAttachmentArrayDynamicIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderInputAttachmentArrayDynamicIndexing}},
1617 {spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformTexelBufferArrayDynamicIndexing}},
1618 {spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageTexelBufferArrayDynamicIndexing}},
1619 {spv::CapabilityUniformBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformBufferArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformBufferArrayNonUniformIndexing}},
1620 {spv::CapabilitySampledImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderSampledImageArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderSampledImageArrayNonUniformIndexing}},
1621 {spv::CapabilityStorageBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageBufferArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageBufferArrayNonUniformIndexing}},
1622 {spv::CapabilityStorageImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageImageArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageImageArrayNonUniformIndexing}},
1623 {spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderInputAttachmentArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderInputAttachmentArrayNonUniformIndexing}},
1624 {spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformTexelBufferArrayNonUniformIndexing}},
Jason Macnakf7019582019-06-13 10:07:26 -07001625 {spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageTexelBufferArrayNonUniformIndexing}},
Chris Forbes47567b72017-06-09 12:09:45 -07001626
1627 // Capabilities that require an extension
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001628 {spv::CapabilityDrawParameters, {VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_draw_parameters}},
1629 {spv::CapabilityGeometryShaderPassthroughNV, {VK_NV_GEOMETRY_SHADER_PASSTHROUGH_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_geometry_shader_passthrough}},
1630 {spv::CapabilitySampleMaskOverrideCoverageNV, {VK_NV_SAMPLE_MASK_OVERRIDE_COVERAGE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_sample_mask_override_coverage}},
1631 {spv::CapabilityShaderViewportIndexLayerEXT, {VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_viewport_index_layer}},
1632 {spv::CapabilityShaderViewportIndexLayerNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}},
1633 {spv::CapabilityShaderViewportMaskNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}},
1634 {spv::CapabilitySubgroupBallotKHR, {VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_ballot }},
1635 {spv::CapabilitySubgroupVoteKHR, {VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_vote }},
Jason Macnakb7d091c2019-06-10 11:13:11 -07001636 {spv::CapabilityGroupNonUniformPartitionedNV, {VK_NV_SHADER_SUBGROUP_PARTITIONED_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_shader_subgroup_partitioned}},
aqnuep7033c702018-09-11 18:03:29 +02001637 {spv::CapabilityInt64Atomics, {VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_atomic_int64 }},
Alexander Galazin3bd8e342018-06-14 15:49:07 +02001638
Jason Macnakc5a621d2019-06-10 12:42:50 -07001639 {spv::CapabilityComputeDerivativeGroupQuadsNV, {"VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupQuads", &VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupQuads, &DeviceExtensions::vk_nv_compute_shader_derivatives}},
1640 {spv::CapabilityComputeDerivativeGroupLinearNV, {"VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupLinear", &VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupLinear, &DeviceExtensions::vk_nv_compute_shader_derivatives}},
Jason Macnakf7019582019-06-13 10:07:26 -07001641 {spv::CapabilityFragmentBarycentricNV, {"VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::fragmentShaderBarycentric", &VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::fragmentShaderBarycentric, &DeviceExtensions::vk_nv_fragment_shader_barycentric}},
Jason Macnakc5a621d2019-06-10 12:42:50 -07001642
Jason Macnakf7019582019-06-13 10:07:26 -07001643 {spv::CapabilityStorageBuffer8BitAccess, {"VkPhysicalDevice8BitStorageFeaturesKHR::storageBuffer8BitAccess", &VkPhysicalDevice8BitStorageFeaturesKHR::storageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}},
1644 {spv::CapabilityUniformAndStorageBuffer8BitAccess, {"VkPhysicalDevice8BitStorageFeaturesKHR::uniformAndStorageBuffer8BitAccess", &VkPhysicalDevice8BitStorageFeaturesKHR::uniformAndStorageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}},
1645 {spv::CapabilityStoragePushConstant8, {"VkPhysicalDevice8BitStorageFeaturesKHR::storagePushConstant8", &VkPhysicalDevice8BitStorageFeaturesKHR::storagePushConstant8, &DeviceExtensions::vk_khr_8bit_storage}},
Brett Lawsonbebfb6f2018-10-23 16:58:50 -07001646
Jason Macnakf7019582019-06-13 10:07:26 -07001647 {spv::CapabilityTransformFeedback, { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback, &DeviceExtensions::vk_ext_transform_feedback}},
1648 {spv::CapabilityGeometryStreams, { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams, &DeviceExtensions::vk_ext_transform_feedback}},
Jose-Emilio Munoz-Lopez1109b452018-08-21 09:44:07 +01001649
Jason Macnakf7019582019-06-13 10:07:26 -07001650 {spv::CapabilityFloat16, {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderFloat16", &VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderFloat16, &DeviceExtensions::vk_khr_shader_float16_int8}},
1651 {spv::CapabilityInt8, {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderInt8", &VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderInt8, &DeviceExtensions::vk_khr_shader_float16_int8}},
Jeff Bolze4356752019-03-07 11:23:46 -06001652
Jason Macnakd7fddf82019-06-13 09:52:49 -07001653 {spv::CapabilityImageFootprintNV, {"VkPhysicalDeviceShaderImageFootprintFeaturesNV::imageFootprint", &VkPhysicalDeviceShaderImageFootprintFeaturesNV::imageFootprint, &DeviceExtensions::vk_nv_shader_image_footprint}},
1654
Jeff Bolze4356752019-03-07 11:23:46 -06001655 {spv::CapabilityCooperativeMatrixNV, {"VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix", &VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix, &DeviceExtensions::vk_nv_cooperative_matrix}},
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001656
1657 {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat16, &DeviceExtensions::vk_khr_shader_float_controls}},
1658 {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat32, &DeviceExtensions::vk_khr_shader_float_controls}},
1659 {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat64, &DeviceExtensions::vk_khr_shader_float_controls}},
1660 {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat16, &DeviceExtensions::vk_khr_shader_float_controls}},
1661 {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat32, &DeviceExtensions::vk_khr_shader_float_controls}},
1662 {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat64, &DeviceExtensions::vk_khr_shader_float_controls}},
1663 {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat16, &DeviceExtensions::vk_khr_shader_float_controls}},
1664 {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat32, &DeviceExtensions::vk_khr_shader_float_controls}},
1665 {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat64, &DeviceExtensions::vk_khr_shader_float_controls}},
1666 {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat16, &DeviceExtensions::vk_khr_shader_float_controls}},
1667 {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat32, &DeviceExtensions::vk_khr_shader_float_controls}},
1668 {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat64, &DeviceExtensions::vk_khr_shader_float_controls}},
1669 {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat16, &DeviceExtensions::vk_khr_shader_float_controls}},
1670 {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat32, &DeviceExtensions::vk_khr_shader_float_controls}},
1671 {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat64, &DeviceExtensions::vk_khr_shader_float_controls}},
Jeff Bolz38f6cb52019-06-30 16:26:44 -05001672
1673 {spv::CapabilityFragmentShaderSampleInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderSampleInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderSampleInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
1674 {spv::CapabilityFragmentShaderPixelInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderPixelInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderPixelInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
1675 {spv::CapabilityFragmentShaderShadingRateInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderShadingRateInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderShadingRateInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
Chris Forbes47567b72017-06-09 12:09:45 -07001676 };
1677 // clang-format on
1678
1679 for (auto insn : *src) {
1680 if (insn.opcode() == spv::OpCapability) {
Dave Houltoneb10ea82017-12-22 12:21:50 -07001681 size_t n = capabilities.count(insn.word(1));
1682 if (1 == n) { // key occurs exactly once
1683 auto it = capabilities.find(insn.word(1));
1684 if (it != capabilities.end()) {
1685 if (it->second.feature) {
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001686 skip |= RequireFeature(report_data, it->second.feature.IsEnabled(enabled_features), it->second.name);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001687 }
1688 if (it->second.extension) {
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06001689 skip |= RequireExtension(report_data, device_extensions.*(it->second.extension), it->second.name);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001690 }
Chris Forbes47567b72017-06-09 12:09:45 -07001691 }
Dave Houltoneb10ea82017-12-22 12:21:50 -07001692 } else if (1 < n) { // key occurs multiple times, at least one must be enabled
1693 bool needs_feature = false, has_feature = false;
1694 bool needs_ext = false, has_ext = false;
1695 std::string feature_names = "(one of) [ ";
1696 std::string extension_names = feature_names;
1697 auto caps = capabilities.equal_range(insn.word(1));
1698 for (auto it = caps.first; it != caps.second; ++it) {
1699 if (it->second.feature) {
1700 needs_feature = true;
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001701 has_feature = has_feature || it->second.feature.IsEnabled(enabled_features);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001702 feature_names += it->second.name;
1703 feature_names += " ";
1704 }
1705 if (it->second.extension) {
1706 needs_ext = true;
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06001707 has_ext = has_ext || device_extensions.*(it->second.extension);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001708 extension_names += it->second.name;
1709 extension_names += " ";
1710 }
1711 }
1712 if (needs_feature) {
1713 feature_names += "]";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001714 skip |= RequireFeature(report_data, has_feature, feature_names.c_str());
Dave Houltoneb10ea82017-12-22 12:21:50 -07001715 }
1716 if (needs_ext) {
1717 extension_names += "]";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001718 skip |= RequireExtension(report_data, has_ext, extension_names.c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001719 }
Jeff Bolzee743412019-06-20 22:24:32 -05001720 } else { // Do group non-uniform checks
1721 const VkSubgroupFeatureFlags supportedOperations = phys_dev_ext_props.subgroup_props.supportedOperations;
1722 const VkSubgroupFeatureFlags supportedStages = phys_dev_ext_props.subgroup_props.supportedStages;
1723
1724 switch (insn.word(1)) {
1725 default:
1726 break;
1727 case spv::CapabilityGroupNonUniform:
1728 case spv::CapabilityGroupNonUniformVote:
1729 case spv::CapabilityGroupNonUniformArithmetic:
1730 case spv::CapabilityGroupNonUniformBallot:
1731 case spv::CapabilityGroupNonUniformShuffle:
1732 case spv::CapabilityGroupNonUniformShuffleRelative:
1733 case spv::CapabilityGroupNonUniformClustered:
1734 case spv::CapabilityGroupNonUniformQuad:
1735 case spv::CapabilityGroupNonUniformPartitionedNV:
1736 RequirePropertyFlag(report_data, supportedStages & stage, string_VkShaderStageFlagBits(stage),
1737 "VkPhysicalDeviceSubgroupProperties::supportedStages");
1738 break;
1739 }
1740
1741 switch (insn.word(1)) {
1742 default:
1743 break;
1744 case spv::CapabilityGroupNonUniform:
1745 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT,
1746 "VK_SUBGROUP_FEATURE_BASIC_BIT",
1747 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1748 break;
1749 case spv::CapabilityGroupNonUniformVote:
1750 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT,
1751 "VK_SUBGROUP_FEATURE_VOTE_BIT",
1752 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1753 break;
1754 case spv::CapabilityGroupNonUniformArithmetic:
1755 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT,
1756 "VK_SUBGROUP_FEATURE_ARITHMETIC_BIT",
1757 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1758 break;
1759 case spv::CapabilityGroupNonUniformBallot:
1760 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT,
1761 "VK_SUBGROUP_FEATURE_BALLOT_BIT",
1762 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1763 break;
1764 case spv::CapabilityGroupNonUniformShuffle:
1765 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_BIT,
1766 "VK_SUBGROUP_FEATURE_SHUFFLE_BIT",
1767 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1768 break;
1769 case spv::CapabilityGroupNonUniformShuffleRelative:
1770 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT,
1771 "VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT",
1772 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1773 break;
1774 case spv::CapabilityGroupNonUniformClustered:
1775 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_CLUSTERED_BIT,
1776 "VK_SUBGROUP_FEATURE_CLUSTERED_BIT",
1777 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1778 break;
1779 case spv::CapabilityGroupNonUniformQuad:
1780 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_QUAD_BIT,
1781 "VK_SUBGROUP_FEATURE_QUAD_BIT",
1782 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1783 break;
1784 case spv::CapabilityGroupNonUniformPartitionedNV:
1785 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV,
1786 "VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV",
1787 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1788 break;
1789 }
Chris Forbes47567b72017-06-09 12:09:45 -07001790 }
1791 }
1792 }
1793
Jeff Bolzee743412019-06-20 22:24:32 -05001794 return skip;
1795}
1796
1797bool CoreChecks::ValidateShaderStageWritableDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor) {
1798 bool skip = false;
1799
Chris Forbes349b3132018-03-07 11:38:08 -08001800 if (has_writable_descriptor) {
1801 switch (stage) {
1802 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -06001803 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1804 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1805 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1806 case VK_SHADER_STAGE_MISS_BIT_NV:
1807 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1808 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1809 case VK_SHADER_STAGE_TASK_BIT_NV:
1810 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -08001811 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -06001812 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -08001813 break;
1814 case VK_SHADER_STAGE_FRAGMENT_BIT:
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001815 skip |= RequireFeature(report_data, enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics");
Chris Forbes349b3132018-03-07 11:38:08 -08001816 break;
1817 default:
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001818 skip |= RequireFeature(report_data, enabled_features.core.vertexPipelineStoresAndAtomics,
1819 "vertexPipelineStoresAndAtomics");
Chris Forbes349b3132018-03-07 11:38:08 -08001820 break;
1821 }
1822 }
1823
Chris Forbes47567b72017-06-09 12:09:45 -07001824 return skip;
1825}
1826
Jeff Bolzee743412019-06-20 22:24:32 -05001827bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
1828 std::unordered_set<uint32_t> const &accessible_ids) {
1829 bool skip = false;
1830
1831 auto const subgroup_props = phys_dev_ext_props.subgroup_props;
1832
1833 for (uint32_t id : accessible_ids) {
1834 auto inst = module->get_def(id);
1835
1836 // Check the quad operations.
1837 switch (inst.opcode()) {
1838 default:
1839 break;
1840 case spv::OpGroupNonUniformQuadBroadcast:
1841 case spv::OpGroupNonUniformQuadSwap:
1842 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
1843 skip |= RequireFeature(report_data, subgroup_props.quadOperationsInAllStages,
1844 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages");
1845 }
1846 break;
1847 }
1848 }
1849
1850 return skip;
1851}
1852
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001853bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001854 PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001855 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
1856 pStage->stage == VK_SHADER_STAGE_ALL) {
1857 return false;
1858 }
1859
1860 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07001861 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001862
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001863 std::set<uint32_t> patchIDs;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001864 struct Variable {
1865 uint32_t baseTypePtrID;
1866 uint32_t ID;
1867 uint32_t storageClass;
1868 };
1869 std::vector<Variable> variables;
1870
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001871 uint32_t numVertices = 0;
1872
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001873 for (auto insn : *src) {
1874 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001875 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001876 case spv::OpDecorate:
1877 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001878 case spv::DecorationPatch: {
1879 patchIDs.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001880 break;
1881 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001882 default:
1883 break;
1884 }
1885 break;
1886 // Find all input and output variables
1887 case spv::OpVariable: {
1888 Variable var = {};
1889 var.storageClass = insn.word(3);
1890 if (var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) {
1891 var.baseTypePtrID = insn.word(1);
1892 var.ID = insn.word(2);
1893 variables.push_back(var);
1894 }
1895 break;
1896 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001897 case spv::OpExecutionMode:
1898 if (insn.word(1) == entrypoint.word(2)) {
1899 switch (insn.word(2)) {
1900 default:
1901 break;
1902 case spv::ExecutionModeOutputVertices:
1903 numVertices = insn.word(3);
1904 break;
1905 }
1906 }
1907 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001908 default:
1909 break;
1910 }
1911 }
1912
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001913 bool strip_output_array_level =
1914 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
1915 bool strip_input_array_level =
1916 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
1917 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
1918
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001919 uint32_t numCompIn = 0, numCompOut = 0;
1920 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001921 // Check if the variable is a patch. Patches can also be members of blocks,
1922 // but if they are then the top-level arrayness has already been stripped
1923 // by the time GetComponentsConsumedByType gets to it.
1924 bool isPatch = patchIDs.find(var.ID) != patchIDs.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001925
1926 if (var.storageClass == spv::StorageClassInput) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001927 numCompIn += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_input_array_level && !isPatch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001928 } else { // var.storageClass == spv::StorageClassOutput
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001929 numCompOut += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_output_array_level && !isPatch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001930 }
1931 }
1932
1933 switch (pStage->stage) {
1934 case VK_SHADER_STAGE_VERTEX_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001935 if (numCompOut > limits.maxVertexOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001936 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
1937 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
1938 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
1939 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
1940 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001941 limits.maxVertexOutputComponents, numCompOut - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001942 }
1943 break;
1944
1945 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001946 if (numCompIn > limits.maxTessellationControlPerVertexInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001947 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
1948 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
1949 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
1950 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
1951 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001952 limits.maxTessellationControlPerVertexInputComponents,
1953 numCompIn - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001954 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001955 if (numCompOut > limits.maxTessellationControlPerVertexOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001956 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
1957 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
1958 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
1959 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
1960 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001961 limits.maxTessellationControlPerVertexOutputComponents,
1962 numCompOut - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001963 }
1964 break;
1965
1966 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001967 if (numCompIn > limits.maxTessellationEvaluationInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001968 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
1969 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
1970 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
1971 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
1972 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001973 limits.maxTessellationEvaluationInputComponents,
1974 numCompIn - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001975 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001976 if (numCompOut > limits.maxTessellationEvaluationOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001977 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
1978 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
1979 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
1980 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
1981 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001982 limits.maxTessellationEvaluationOutputComponents,
1983 numCompOut - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001984 }
1985 break;
1986
1987 case VK_SHADER_STAGE_GEOMETRY_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001988 if (numCompIn > limits.maxGeometryInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001989 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
1990 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
1991 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1992 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
1993 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001994 limits.maxGeometryInputComponents, numCompIn - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001995 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07001996 if (numCompOut > limits.maxGeometryOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001997 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
1998 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
1999 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
2000 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
2001 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002002 limits.maxGeometryOutputComponents, numCompOut - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002003 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002004 if (numCompOut * numVertices > limits.maxGeometryTotalOutputComponents) {
2005 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2006 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2007 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
2008 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
2009 "components by %u components",
2010 limits.maxGeometryTotalOutputComponents,
2011 numCompOut * numVertices - limits.maxGeometryTotalOutputComponents);
2012 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002013 break;
2014
2015 case VK_SHADER_STAGE_FRAGMENT_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002016 if (numCompIn > limits.maxFragmentInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002017 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2018 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2019 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
2020 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
2021 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002022 limits.maxFragmentInputComponents, numCompIn - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002023 }
2024 break;
2025
Jeff Bolz148d94e2018-12-13 21:25:56 -06002026 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
2027 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
2028 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
2029 case VK_SHADER_STAGE_MISS_BIT_NV:
2030 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
2031 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
2032 case VK_SHADER_STAGE_TASK_BIT_NV:
2033 case VK_SHADER_STAGE_MESH_BIT_NV:
2034 break;
2035
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002036 default:
2037 assert(false); // This should never happen
2038 }
2039 return skip;
2040}
2041
Jeff Bolze4356752019-03-07 11:23:46 -06002042// copy the specialization constant value into buf, if it is present
2043void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
2044 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
2045
2046 if (spec && spec_id < spec->mapEntryCount) {
2047 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
2048 }
2049}
2050
2051// Fill in value with the constant or specialization constant value, if available.
2052// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002053static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Bolze4356752019-03-07 11:23:46 -06002054 const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
2055 auto type_id = src->get_def(insn.word(1));
2056 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
2057 return false;
2058 }
2059 switch (insn.opcode()) {
2060 case spv::OpSpecConstant:
2061 *value = insn.word(3);
2062 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
2063 return true;
2064 case spv::OpConstant:
2065 *value = insn.word(3);
2066 return true;
2067 default:
2068 return false;
2069 }
2070}
2071
2072// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002073VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06002074 switch (insn.opcode()) {
2075 case spv::OpTypeInt:
2076 switch (insn.word(2)) {
2077 case 8:
2078 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
2079 case 16:
2080 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
2081 case 32:
2082 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
2083 case 64:
2084 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
2085 default:
2086 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2087 }
2088 case spv::OpTypeFloat:
2089 switch (insn.word(2)) {
2090 case 16:
2091 return VK_COMPONENT_TYPE_FLOAT16_NV;
2092 case 32:
2093 return VK_COMPONENT_TYPE_FLOAT32_NV;
2094 case 64:
2095 return VK_COMPONENT_TYPE_FLOAT64_NV;
2096 default:
2097 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2098 }
2099 default:
2100 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2101 }
2102}
2103
2104// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
2105// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002106bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Bolze4356752019-03-07 11:23:46 -06002107 PIPELINE_STATE *pipeline) {
2108 bool skip = false;
2109
2110 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
2111 std::unordered_map<uint32_t, uint32_t> id_to_spec_id;
2112 // Map SPIR-V result ID to the ID of its type.
2113 std::unordered_map<uint32_t, uint32_t> id_to_type_id;
2114
2115 struct CoopMatType {
2116 uint32_t scope, rows, cols;
2117 VkComponentTypeNV component_type;
2118 bool all_constant;
2119
2120 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
2121
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002122 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Bolze4356752019-03-07 11:23:46 -06002123 const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
2124 spirv_inst_iter insn = src->get_def(id);
2125 uint32_t component_type_id = insn.word(2);
2126 uint32_t scope_id = insn.word(3);
2127 uint32_t rows_id = insn.word(4);
2128 uint32_t cols_id = insn.word(5);
2129 auto component_type_iter = src->get_def(component_type_id);
2130 auto scope_iter = src->get_def(scope_id);
2131 auto rows_iter = src->get_def(rows_id);
2132 auto cols_iter = src->get_def(cols_id);
2133
2134 all_constant = true;
2135 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
2136 all_constant = false;
2137 }
2138 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
2139 all_constant = false;
2140 }
2141 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
2142 all_constant = false;
2143 }
2144 component_type = GetComponentType(component_type_iter, src);
2145 }
2146 };
2147
2148 bool seen_coopmat_capability = false;
2149
2150 for (auto insn : *src) {
2151 // Whitelist instructions whose result can be a cooperative matrix type, and
2152 // keep track of their types. It would be nice if SPIRV-Headers generated code
2153 // to identify which instructions have a result type and result id. Lacking that,
2154 // this whitelist is based on the set of instructions that
2155 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
2156 switch (insn.opcode()) {
2157 case spv::OpLoad:
2158 case spv::OpCooperativeMatrixLoadNV:
2159 case spv::OpCooperativeMatrixMulAddNV:
2160 case spv::OpSNegate:
2161 case spv::OpFNegate:
2162 case spv::OpIAdd:
2163 case spv::OpFAdd:
2164 case spv::OpISub:
2165 case spv::OpFSub:
2166 case spv::OpFDiv:
2167 case spv::OpSDiv:
2168 case spv::OpUDiv:
2169 case spv::OpMatrixTimesScalar:
2170 case spv::OpConstantComposite:
2171 case spv::OpCompositeConstruct:
2172 case spv::OpConvertFToU:
2173 case spv::OpConvertFToS:
2174 case spv::OpConvertSToF:
2175 case spv::OpConvertUToF:
2176 case spv::OpUConvert:
2177 case spv::OpSConvert:
2178 case spv::OpFConvert:
2179 id_to_type_id[insn.word(2)] = insn.word(1);
2180 break;
2181 default:
2182 break;
2183 }
2184
2185 switch (insn.opcode()) {
2186 case spv::OpDecorate:
2187 if (insn.word(2) == spv::DecorationSpecId) {
2188 id_to_spec_id[insn.word(1)] = insn.word(3);
2189 }
2190 break;
2191 case spv::OpCapability:
2192 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
2193 seen_coopmat_capability = true;
2194
2195 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
2196 skip |=
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002197 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Jeff Bolze4356752019-03-07 11:23:46 -06002198 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixSupportedStages,
2199 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
2200 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
2201 }
2202 }
2203 break;
2204 case spv::OpMemoryModel:
2205 // If the capability isn't enabled, don't bother with the rest of this function.
2206 // OpMemoryModel is the first required instruction after all OpCapability instructions.
2207 if (!seen_coopmat_capability) {
2208 return skip;
2209 }
2210 break;
2211 case spv::OpTypeCooperativeMatrixNV: {
2212 CoopMatType M;
2213 M.Init(insn.word(1), src, pStage, id_to_spec_id);
2214
2215 if (M.all_constant) {
2216 // Validate that the type parameters are all supported for one of the
2217 // operands of a cooperative matrix property.
2218 bool valid = false;
2219 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
2220 if (cooperative_matrix_properties[i].AType == M.component_type &&
2221 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].KSize == M.cols &&
2222 cooperative_matrix_properties[i].scope == M.scope) {
2223 valid = true;
2224 break;
2225 }
2226 if (cooperative_matrix_properties[i].BType == M.component_type &&
2227 cooperative_matrix_properties[i].KSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2228 cooperative_matrix_properties[i].scope == M.scope) {
2229 valid = true;
2230 break;
2231 }
2232 if (cooperative_matrix_properties[i].CType == M.component_type &&
2233 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2234 cooperative_matrix_properties[i].scope == M.scope) {
2235 valid = true;
2236 break;
2237 }
2238 if (cooperative_matrix_properties[i].DType == M.component_type &&
2239 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2240 cooperative_matrix_properties[i].scope == M.scope) {
2241 valid = true;
2242 break;
2243 }
2244 }
2245 if (!valid) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002246 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Jeff Bolze4356752019-03-07 11:23:46 -06002247 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixType,
2248 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
2249 insn.word(1));
2250 }
2251 }
2252 break;
2253 }
2254 case spv::OpCooperativeMatrixMulAddNV: {
2255 CoopMatType A, B, C, D;
2256 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
2257 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
2258 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
2259 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07002260 // Couldn't find type of matrix
2261 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06002262 break;
2263 }
2264 D.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
2265 A.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
2266 B.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
2267 C.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
2268
2269 if (A.all_constant && B.all_constant && C.all_constant && D.all_constant) {
2270 // Validate that the type parameters are all supported for the same
2271 // cooperative matrix property.
2272 bool valid = false;
2273 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
2274 if (cooperative_matrix_properties[i].AType == A.component_type &&
2275 cooperative_matrix_properties[i].MSize == A.rows && cooperative_matrix_properties[i].KSize == A.cols &&
2276 cooperative_matrix_properties[i].scope == A.scope &&
2277
2278 cooperative_matrix_properties[i].BType == B.component_type &&
2279 cooperative_matrix_properties[i].KSize == B.rows && cooperative_matrix_properties[i].NSize == B.cols &&
2280 cooperative_matrix_properties[i].scope == B.scope &&
2281
2282 cooperative_matrix_properties[i].CType == C.component_type &&
2283 cooperative_matrix_properties[i].MSize == C.rows && cooperative_matrix_properties[i].NSize == C.cols &&
2284 cooperative_matrix_properties[i].scope == C.scope &&
2285
2286 cooperative_matrix_properties[i].DType == D.component_type &&
2287 cooperative_matrix_properties[i].MSize == D.rows && cooperative_matrix_properties[i].NSize == D.cols &&
2288 cooperative_matrix_properties[i].scope == D.scope) {
2289 valid = true;
2290 break;
2291 }
2292 }
2293 if (!valid) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002294 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Jeff Bolze4356752019-03-07 11:23:46 -06002295 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixMulAdd,
2296 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
2297 "VkCooperativeMatrixPropertiesNV",
2298 insn.word(2));
2299 }
2300 }
2301 break;
2302 }
2303 default:
2304 break;
2305 }
2306 }
2307
2308 return skip;
2309}
2310
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002311bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002312 auto entrypoint_id = entrypoint.word(2);
2313
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002314 // The first denorm execution mode encountered, along with its bit width.
2315 // Used to check if SeparateDenormSettings is respected.
2316 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002317
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002318 // The first rounding mode encountered, along with its bit width.
2319 // Used to check if SeparateRoundingModeSettings is respected.
2320 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002321
2322 bool skip = false;
2323
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002324 uint32_t verticesOut = 0;
2325 uint32_t invocations = 0;
2326
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002327 for (auto insn : *src) {
2328 if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) {
2329 auto mode = insn.word(2);
2330 switch (mode) {
2331 case spv::ExecutionModeSignedZeroInfNanPreserve: {
2332 auto bit_width = insn.word(3);
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002333 if ((bit_width == 16 && !enabled_features.float_controls.shaderSignedZeroInfNanPreserveFloat16) ||
2334 (bit_width == 32 && !enabled_features.float_controls.shaderSignedZeroInfNanPreserveFloat32) ||
2335 (bit_width == 64 && !enabled_features.float_controls.shaderSignedZeroInfNanPreserveFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002336 skip |=
2337 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2338 kVUID_Core_Shader_FeatureNotEnabled,
2339 "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device",
2340 bit_width);
2341 }
2342 break;
2343 }
2344
2345 case spv::ExecutionModeDenormPreserve: {
2346 auto bit_width = insn.word(3);
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002347 if ((bit_width == 16 && !enabled_features.float_controls.shaderDenormPreserveFloat16) ||
2348 (bit_width == 32 && !enabled_features.float_controls.shaderDenormPreserveFloat32) ||
2349 (bit_width == 64 && !enabled_features.float_controls.shaderDenormPreserveFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002350 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2351 kVUID_Core_Shader_FeatureNotEnabled,
2352 "Shader requires DenormPreserve for bit width %d but it is not enabled on the device",
2353 bit_width);
2354 }
2355
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002356 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
2357 // Register the first denorm execution mode found
2358 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
2359 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002360 !enabled_features.float_controls.separateDenormSettings) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002361 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2362 kVUID_Core_Shader_FeatureNotEnabled,
2363 "Shader uses separate denorm execution modes for different bit widths but "
2364 "SeparateDenormSettings is not enabled on the device");
2365 }
2366 break;
2367 }
2368
2369 case spv::ExecutionModeDenormFlushToZero: {
2370 auto bit_width = insn.word(3);
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002371 if ((bit_width == 16 && !enabled_features.float_controls.shaderDenormFlushToZeroFloat16) ||
2372 (bit_width == 32 && !enabled_features.float_controls.shaderDenormFlushToZeroFloat32) ||
2373 (bit_width == 64 && !enabled_features.float_controls.shaderDenormFlushToZeroFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002374 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2375 kVUID_Core_Shader_FeatureNotEnabled,
2376 "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device",
2377 bit_width);
2378 }
2379
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002380 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
2381 // Register the first denorm execution mode found
2382 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
2383 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002384 !enabled_features.float_controls.separateDenormSettings) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002385 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2386 kVUID_Core_Shader_FeatureNotEnabled,
2387 "Shader uses separate denorm execution modes for different bit widths but "
2388 "SeparateDenormSettings is not enabled on the device");
2389 }
2390 break;
2391 }
2392
2393 case spv::ExecutionModeRoundingModeRTE: {
2394 auto bit_width = insn.word(3);
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002395 if ((bit_width == 16 && !enabled_features.float_controls.shaderRoundingModeRTEFloat16) ||
2396 (bit_width == 32 && !enabled_features.float_controls.shaderRoundingModeRTEFloat32) ||
2397 (bit_width == 64 && !enabled_features.float_controls.shaderRoundingModeRTEFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002398 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2399 kVUID_Core_Shader_FeatureNotEnabled,
2400 "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device",
2401 bit_width);
2402 }
2403
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002404 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2405 // Register the first rounding mode found
2406 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
2407 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002408 !enabled_features.float_controls.separateRoundingModeSettings) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002409 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2410 kVUID_Core_Shader_FeatureNotEnabled,
2411 "Shader uses separate rounding modes for different bit widths but "
2412 "SeparateRoundingModeSettings is not enabled on the device");
2413 }
2414 break;
2415 }
2416
2417 case spv::ExecutionModeRoundingModeRTZ: {
2418 auto bit_width = insn.word(3);
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002419 if ((bit_width == 16 && !enabled_features.float_controls.shaderRoundingModeRTZFloat16) ||
2420 (bit_width == 32 && !enabled_features.float_controls.shaderRoundingModeRTZFloat32) ||
2421 (bit_width == 64 && !enabled_features.float_controls.shaderRoundingModeRTZFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002422 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2423 kVUID_Core_Shader_FeatureNotEnabled,
2424 "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device",
2425 bit_width);
2426 }
2427
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002428 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2429 // Register the first rounding mode found
2430 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
2431 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002432 !enabled_features.float_controls.separateRoundingModeSettings) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002433 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2434 kVUID_Core_Shader_FeatureNotEnabled,
2435 "Shader uses separate rounding modes for different bit widths but "
2436 "SeparateRoundingModeSettings is not enabled on the device");
2437 }
2438 break;
2439 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002440
2441 case spv::ExecutionModeOutputVertices: {
2442 verticesOut = insn.word(3);
2443 break;
2444 }
2445
2446 case spv::ExecutionModeInvocations: {
2447 invocations = insn.word(3);
2448 break;
2449 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002450 }
2451 }
2452 }
2453
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002454 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
2455 if (verticesOut == 0 || verticesOut > phys_dev_props.limits.maxGeometryOutputVertices) {
2456 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2457 "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2458 "Geometry shader entry point must have an OpExecutionMode instruction that "
2459 "specifies a maximum output vertex count that is greater than 0 and less "
2460 "than or equal to maxGeometryOutputVertices. "
2461 "OutputVertices=%d, maxGeometryOutputVertices=%d",
2462 verticesOut, phys_dev_props.limits.maxGeometryOutputVertices);
2463 }
2464
2465 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
2466 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2467 "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2468 "Geometry shader entry point must have an OpExecutionMode instruction that "
2469 "specifies an invocation count that is greater than 0 and less "
2470 "than or equal to maxGeometryShaderInvocations. "
2471 "Invocations=%d, maxGeometryShaderInvocations=%d",
2472 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
2473 }
2474 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002475 return skip;
2476}
2477
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002478static uint32_t DescriptorTypeToReqs(SHADER_MODULE_STATE const *module, uint32_t type_id) {
Chris Forbes47567b72017-06-09 12:09:45 -07002479 auto type = module->get_def(type_id);
2480
2481 while (true) {
2482 switch (type.opcode()) {
2483 case spv::OpTypeArray:
Chris Forbes062f1222018-08-21 15:34:15 -07002484 case spv::OpTypeRuntimeArray:
Chris Forbes47567b72017-06-09 12:09:45 -07002485 case spv::OpTypeSampledImage:
2486 type = module->get_def(type.word(2));
2487 break;
2488 case spv::OpTypePointer:
2489 type = module->get_def(type.word(3));
2490 break;
2491 case spv::OpTypeImage: {
2492 auto dim = type.word(3);
2493 auto arrayed = type.word(5);
2494 auto msaa = type.word(6);
2495
Chris Forbes74ba2232018-08-27 15:19:27 -07002496 uint32_t bits = 0;
2497 switch (GetFundamentalType(module, type.word(2))) {
2498 case FORMAT_TYPE_FLOAT:
2499 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
2500 break;
2501 case FORMAT_TYPE_UINT:
2502 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
2503 break;
2504 case FORMAT_TYPE_SINT:
2505 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
2506 break;
2507 default:
2508 break;
2509 }
2510
Chris Forbes47567b72017-06-09 12:09:45 -07002511 switch (dim) {
2512 case spv::Dim1D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002513 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
2514 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002515 case spv::Dim2D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002516 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
2517 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
2518 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002519 case spv::Dim3D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002520 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
2521 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002522 case spv::DimCube:
Chris Forbes74ba2232018-08-27 15:19:27 -07002523 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
2524 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002525 case spv::DimSubpassData:
Chris Forbes74ba2232018-08-27 15:19:27 -07002526 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
2527 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002528 default: // buffer, etc.
Chris Forbes74ba2232018-08-27 15:19:27 -07002529 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002530 }
2531 }
2532 default:
2533 return 0;
2534 }
2535 }
2536}
2537
2538// For given pipelineLayout verify that the set_layout_node at slot.first
2539// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002540static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002541 descriptor_slot_t slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002542 if (!pipelineLayout) return nullptr;
2543
2544 if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr;
2545
2546 return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second);
2547}
2548
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002549static bool FindLocalSize(SHADER_MODULE_STATE const *src, uint32_t &local_size_x, uint32_t &local_size_y, uint32_t &local_size_z) {
Locke1ec6d952019-04-02 11:57:21 -06002550 for (auto insn : *src) {
2551 if (insn.opcode() == spv::OpEntryPoint) {
2552 auto executionModel = insn.word(1);
2553 auto entrypointStageBits = ExecutionModelToShaderStageFlagBits(executionModel);
2554 if (entrypointStageBits == VK_SHADER_STAGE_COMPUTE_BIT) {
2555 auto entrypoint_id = insn.word(2);
2556 for (auto insn1 : *src) {
2557 if (insn1.opcode() == spv::OpExecutionMode && insn1.word(1) == entrypoint_id &&
2558 insn1.word(2) == spv::ExecutionModeLocalSize) {
2559 local_size_x = insn1.word(3);
2560 local_size_y = insn1.word(4);
2561 local_size_z = insn1.word(5);
2562 return true;
2563 }
2564 }
2565 }
2566 }
2567 }
2568 return false;
2569}
2570
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002571static void ProcessExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint, PIPELINE_STATE *pipeline) {
Jeff Bolz105d6492018-09-29 15:46:44 -05002572 auto entrypoint_id = entrypoint.word(2);
Chris Forbes0771b672018-03-22 21:13:46 -07002573 bool is_point_mode = false;
2574
2575 for (auto insn : *src) {
2576 if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) {
2577 switch (insn.word(2)) {
2578 case spv::ExecutionModePointMode:
2579 // In tessellation shaders, PointMode is separate and trumps the tessellation topology.
2580 is_point_mode = true;
2581 break;
2582
2583 case spv::ExecutionModeOutputPoints:
2584 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
2585 break;
2586
2587 case spv::ExecutionModeIsolines:
2588 case spv::ExecutionModeOutputLineStrip:
2589 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
2590 break;
2591
2592 case spv::ExecutionModeTriangles:
2593 case spv::ExecutionModeQuads:
2594 case spv::ExecutionModeOutputTriangleStrip:
2595 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
2596 break;
2597 }
2598 }
2599 }
2600
2601 if (is_point_mode) pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
2602}
2603
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002604// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2605// o If there is only a vertex shader : gl_PointSize must be written when using points
2606// o If there is a geometry or tessellation shader:
2607// - If shaderTessellationAndGeometryPointSize feature is enabled:
2608// * gl_PointSize must be written in the final geometry stage
2609// - If shaderTessellationAndGeometryPointSize feature is disabled:
2610// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002611bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
2612 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002613 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2614 return false;
2615 }
2616
2617 bool pointsize_written = false;
2618 bool skip = false;
2619
2620 // Search for PointSize built-in decorations
2621 std::vector<uint32_t> pointsize_builtin_offsets;
2622 spirv_inst_iter insn = entrypoint;
2623 while (!pointsize_written && (insn.opcode() != spv::OpFunction)) {
2624 if (insn.opcode() == spv::OpMemberDecorate) {
2625 if (insn.word(3) == spv::DecorationBuiltIn) {
2626 if (insn.word(4) == spv::BuiltInPointSize) {
2627 pointsize_written = IsPointSizeWritten(src, insn, entrypoint);
2628 }
2629 }
2630 } else if (insn.opcode() == spv::OpDecorate) {
2631 if (insn.word(2) == spv::DecorationBuiltIn) {
2632 if (insn.word(3) == spv::BuiltInPointSize) {
2633 pointsize_written = IsPointSizeWritten(src, insn, entrypoint);
2634 }
2635 }
2636 }
2637
2638 insn++;
2639 }
2640
2641 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002642 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002643 if (pointsize_written) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002644 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002645 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
2646 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2647 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
2648 }
2649 } else if (!pointsize_written) {
2650 skip |=
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002651 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002652 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_MissingPointSizeBuiltIn,
2653 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2654 string_VkShaderStageFlagBits(stage));
2655 }
2656 return skip;
2657}
2658
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002659bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, PIPELINE_STATE *pipeline,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002660 SHADER_MODULE_STATE const **out_module, spirv_inst_iter *out_entrypoint,
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002661 bool check_point_size) {
Chris Forbes47567b72017-06-09 12:09:45 -07002662 bool skip = false;
Mark Lobodzinski9e9da292019-03-06 16:19:55 -07002663 auto module = *out_module = GetShaderModuleState(pStage->module);
Chris Forbes47567b72017-06-09 12:09:45 -07002664
2665 if (!module->has_valid_spirv) return false;
2666
2667 // Find the entrypoint
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002668 auto entrypoint = *out_entrypoint = FindEntrypoint(module, pStage->pName, pStage->stage);
Chris Forbes47567b72017-06-09 12:09:45 -07002669 if (entrypoint == module->end()) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002670 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton78d09922018-05-17 15:48:45 -06002671 "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s..",
2672 pStage->pName, string_VkShaderStageFlagBits(pStage->stage))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002673 return true; // no point continuing beyond here, any analysis is just going to be garbage.
2674 }
2675 }
2676
Chris Forbes47567b72017-06-09 12:09:45 -07002677 // Mark accessible ids
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002678 auto accessible_ids = MarkAccessibleIds(module, entrypoint);
2679 ProcessExecutionModes(module, entrypoint, pipeline);
Chris Forbes47567b72017-06-09 12:09:45 -07002680
2681 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes8af24522018-03-07 11:37:45 -08002682 bool has_writable_descriptor = false;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002683 auto descriptor_uses = CollectInterfaceByDescriptorSlot(report_data, module, accessible_ids, &has_writable_descriptor);
Chris Forbes47567b72017-06-09 12:09:45 -07002684
Chris Forbes349b3132018-03-07 11:38:08 -08002685 // Validate shader capabilities against enabled device features
Jeff Bolzee743412019-06-20 22:24:32 -05002686 skip |= ValidateShaderCapabilities(module, pStage->stage);
2687 skip |= ValidateShaderStageWritableDescriptor(pStage->stage, has_writable_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002688 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Jeff Bolzee743412019-06-20 22:24:32 -05002689 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, accessible_ids);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002690 skip |= ValidateExecutionModes(module, entrypoint);
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002691 skip |= ValidateSpecializationOffsets(report_data, pStage);
2692 skip |= ValidatePushConstantUsage(report_data, pipeline->pipeline_layout.push_constant_ranges.get(), module, accessible_ids,
2693 pStage->stage);
Jeff Bolze54ae892018-09-08 12:16:29 -05002694 if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002695 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002696 }
Jeff Bolze4356752019-03-07 11:23:46 -06002697 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
Chris Forbes47567b72017-06-09 12:09:45 -07002698
2699 // Validate descriptor use
2700 for (auto use : descriptor_uses) {
2701 // While validating shaders capture which slots are used by the pipeline
2702 auto &reqs = pipeline->active_slots[use.first.first][use.first.second];
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002703 reqs = descriptor_req(reqs | DescriptorTypeToReqs(module, use.second.type_id));
Chris Forbes47567b72017-06-09 12:09:45 -07002704
2705 // Verify given pipelineLayout has requested setLayout with requested binding
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002706 const auto &binding = GetDescriptorBinding(&pipeline->pipeline_layout, use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002707 unsigned required_descriptor_count;
Jeff Bolze54ae892018-09-08 12:16:29 -05002708 std::set<uint32_t> descriptor_types = TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count);
Chris Forbes47567b72017-06-09 12:09:45 -07002709
2710 if (!binding) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002711 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06002712 kVUID_Core_Shader_MissingDescriptor,
Chris Forbes73c00bf2018-06-22 16:28:06 -07002713 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeff Bolze54ae892018-09-08 12:16:29 -05002714 use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002715 } else if (~binding->stageFlags & pStage->stage) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002716 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06002717 kVUID_Core_Shader_DescriptorNotAccessibleFromStage,
Chris Forbes73c00bf2018-06-22 16:28:06 -07002718 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first,
2719 use.first.second, string_VkShaderStageFlagBits(pStage->stage));
Jeff Bolze54ae892018-09-08 12:16:29 -05002720 } else if (descriptor_types.find(binding->descriptorType) == descriptor_types.end()) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002721 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06002722 kVUID_Core_Shader_DescriptorTypeMismatch,
Chris Forbes73c00bf2018-06-22 16:28:06 -07002723 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first,
Jeff Bolze54ae892018-09-08 12:16:29 -05002724 use.first.second, string_descriptorTypes(descriptor_types).c_str(),
Chris Forbes47567b72017-06-09 12:09:45 -07002725 string_VkDescriptorType(binding->descriptorType));
2726 } else if (binding->descriptorCount < required_descriptor_count) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002727 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06002728 kVUID_Core_Shader_DescriptorTypeMismatch,
Chris Forbes73c00bf2018-06-22 16:28:06 -07002729 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
2730 required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002731 }
2732 }
2733
2734 // Validate use of input attachments against subpass structure
2735 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002736 auto input_attachment_uses = CollectInterfaceByInputAttachmentIndex(module, accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002737
Petr Krause91f7a12017-12-14 20:57:36 +01002738 auto rpci = pipeline->rp_state->createInfo.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002739 auto subpass = pipeline->graphicsPipelineCI.subpass;
2740
2741 for (auto use : input_attachment_uses) {
2742 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2743 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07002744 ? input_attachments[use.first].attachment
2745 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002746
2747 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002748 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06002749 kVUID_Core_Shader_MissingInputAttachment,
Chris Forbes47567b72017-06-09 12:09:45 -07002750 "Shader consumes input attachment index %d but not provided in subpass", use.first);
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002751 } else if (!(GetFormatType(rpci->pAttachments[index].format) & GetFundamentalType(module, use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002752 skip |=
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002753 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton51653902018-06-22 17:32:13 -06002754 kVUID_Core_Shader_InputAttachmentTypeMismatch,
Chris Forbes47567b72017-06-09 12:09:45 -07002755 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002756 string_VkFormat(rpci->pAttachments[index].format), DescribeType(module, use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002757 }
2758 }
2759 }
Lockeaa8fdc02019-04-02 11:59:20 -06002760 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
2761 skip |= ValidateComputeWorkGroupSizes(module);
2762 }
Chris Forbes47567b72017-06-09 12:09:45 -07002763 return skip;
2764}
2765
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002766static bool ValidateInterfaceBetweenStages(debug_report_data const *report_data, SHADER_MODULE_STATE const *producer,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002767 spirv_inst_iter producer_entrypoint, shader_stage_attributes const *producer_stage,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002768 SHADER_MODULE_STATE const *consumer, spirv_inst_iter consumer_entrypoint,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002769 shader_stage_attributes const *consumer_stage) {
Chris Forbes47567b72017-06-09 12:09:45 -07002770 bool skip = false;
2771
2772 auto outputs =
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002773 CollectInterfaceByLocation(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2774 auto inputs = CollectInterfaceByLocation(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002775
2776 auto a_it = outputs.begin();
2777 auto b_it = inputs.begin();
2778
2779 // Maps sorted by key (location); walk them together to find mismatches
2780 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2781 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2782 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2783 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2784 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2785
2786 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Mark Young4e919b22018-05-21 15:53:59 -06002787 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Dave Houlton51653902018-06-22 17:32:13 -06002788 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed,
Mark Young4e919b22018-05-21 15:53:59 -06002789 "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, a_first.first,
2790 a_first.second, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002791 a_it++;
2792 } else if (a_at_end || a_first > b_first) {
Mark Young4e919b22018-05-21 15:53:59 -06002793 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Dave Houlton51653902018-06-22 17:32:13 -06002794 HandleToUint64(consumer->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
Mark Young4e919b22018-05-21 15:53:59 -06002795 "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first,
2796 b_first.second, producer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002797 b_it++;
2798 } else {
2799 // subtleties of arrayed interfaces:
2800 // - if is_patch, then the member is not arrayed, even though the interface may be.
2801 // - if is_block_member, then the extra array level of an arrayed interface is not
2802 // expressed in the member type -- it's expressed in the block type.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002803 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id,
2804 producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member,
2805 consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) {
Mark Young4e919b22018-05-21 15:53:59 -06002806 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Dave Houlton51653902018-06-22 17:32:13 -06002807 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Young4e919b22018-05-21 15:53:59 -06002808 "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002809 DescribeType(producer, a_it->second.type_id).c_str(),
2810 DescribeType(consumer, b_it->second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002811 }
2812 if (a_it->second.is_patch != b_it->second.is_patch) {
Mark Young4e919b22018-05-21 15:53:59 -06002813 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Dave Houlton51653902018-06-22 17:32:13 -06002814 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Dave Houltona9df0ce2018-02-07 10:51:23 -07002815 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
Chris Forbes47567b72017-06-09 12:09:45 -07002816 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2817 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
2818 }
2819 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Mark Young4e919b22018-05-21 15:53:59 -06002820 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
Dave Houlton51653902018-06-22 17:32:13 -06002821 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Chris Forbes47567b72017-06-09 12:09:45 -07002822 "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first,
2823 a_first.second, producer_stage->name, consumer_stage->name);
2824 }
2825 a_it++;
2826 b_it++;
2827 }
2828 }
2829
Ari Suonpaa696b3432019-03-11 14:02:57 +02002830 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
2831 auto builtins_producer = CollectBuiltinBlockMembers(producer, producer_entrypoint, spv::StorageClassOutput);
2832 auto builtins_consumer = CollectBuiltinBlockMembers(consumer, consumer_entrypoint, spv::StorageClassInput);
2833
2834 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2835 if (builtins_producer.size() != builtins_consumer.size()) {
2836 skip |=
2837 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
2838 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
2839 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).", producer_stage->name,
2840 (int)builtins_producer.size(), consumer_stage->name, (int)builtins_consumer.size());
2841 } else {
2842 auto it_producer = builtins_producer.begin();
2843 auto it_consumer = builtins_consumer.begin();
2844 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2845 if (*it_producer != *it_consumer) {
2846 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
2847 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
2848 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2849 consumer_stage->name);
2850 break;
2851 }
2852 it_producer++;
2853 it_consumer++;
2854 }
2855 }
2856 }
2857 }
2858
Chris Forbes47567b72017-06-09 12:09:45 -07002859 return skip;
2860}
2861
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002862static inline uint32_t DetermineFinalGeomStage(PIPELINE_STATE *pipeline, VkGraphicsPipelineCreateInfo *pCreateInfo) {
2863 uint32_t stage_mask = 0;
2864 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2865 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2866 stage_mask |= pCreateInfo->pStages[i].stage;
2867 }
2868 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002869 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2870 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2871 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002872 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2873 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2874 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2875 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2876 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002877 }
2878 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002879 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002880}
2881
Chris Forbes47567b72017-06-09 12:09:45 -07002882// Validate that the shaders used by the given pipeline and store the active_slots
2883// that are actually used by the pipeline into pPipeline->active_slots
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002884bool CoreChecks::ValidateAndCapturePipelineShaderState(PIPELINE_STATE *pipeline) {
Chris Forbesa400a8a2017-07-20 13:10:24 -07002885 auto pCreateInfo = pipeline->graphicsPipelineCI.ptr();
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002886 int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
2887 int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07002888
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002889 SHADER_MODULE_STATE const *shaders[32];
Chris Forbes47567b72017-06-09 12:09:45 -07002890 memset(shaders, 0, sizeof(shaders));
Jeff Bolz7e35c392018-09-04 15:30:41 -05002891 spirv_inst_iter entrypoints[32];
Chris Forbes47567b72017-06-09 12:09:45 -07002892 memset(entrypoints, 0, sizeof(entrypoints));
2893 bool skip = false;
2894
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002895 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, pCreateInfo);
2896
Chris Forbes47567b72017-06-09 12:09:45 -07002897 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2898 auto pStage = &pCreateInfo->pStages[i];
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002899 auto stage_id = GetShaderStageId(pStage->stage);
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002900 skip |= ValidatePipelineShaderStage(pStage, pipeline, &shaders[stage_id], &entrypoints[stage_id],
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002901 (pointlist_stage_mask == pStage->stage));
Chris Forbes47567b72017-06-09 12:09:45 -07002902 }
2903
2904 // if the shader stages are no good individually, cross-stage validation is pointless.
2905 if (skip) return true;
2906
2907 auto vi = pCreateInfo->pVertexInputState;
2908
2909 if (vi) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002910 skip |= ValidateViConsistency(report_data, vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002911 }
2912
2913 if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002914 skip |= ValidateViAgainstVsInputs(report_data, vi, shaders[vertex_stage], entrypoints[vertex_stage]);
Chris Forbes47567b72017-06-09 12:09:45 -07002915 }
2916
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002917 int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
2918 int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07002919
2920 while (!shaders[producer] && producer != fragment_stage) {
2921 producer++;
2922 consumer++;
2923 }
2924
2925 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
2926 assert(shaders[producer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002927 if (shaders[consumer]) {
2928 if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002929 skip |= ValidateInterfaceBetweenStages(report_data, shaders[producer], entrypoints[producer],
2930 &shader_stage_attribs[producer], shaders[consumer], entrypoints[consumer],
2931 &shader_stage_attribs[consumer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002932 }
Chris Forbes47567b72017-06-09 12:09:45 -07002933
2934 producer = consumer;
2935 }
2936 }
2937
2938 if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002939 skip |= ValidateFsOutputsAgainstRenderPass(report_data, shaders[fragment_stage], entrypoints[fragment_stage], pipeline,
2940 pCreateInfo->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07002941 }
2942
2943 return skip;
2944}
2945
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002946bool CoreChecks::ValidateComputePipeline(PIPELINE_STATE *pipeline) {
Chris Forbesa400a8a2017-07-20 13:10:24 -07002947 auto pCreateInfo = pipeline->computePipelineCI.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002948
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002949 SHADER_MODULE_STATE const *module;
Chris Forbes47567b72017-06-09 12:09:45 -07002950 spirv_inst_iter entrypoint;
2951
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002952 return ValidatePipelineShaderStage(&pCreateInfo->stage, pipeline, &module, &entrypoint, false);
Chris Forbes47567b72017-06-09 12:09:45 -07002953}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002954
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002955bool CoreChecks::ValidateRayTracingPipelineNV(PIPELINE_STATE *pipeline) {
Jeff Bolzfbe51582018-09-13 10:01:35 -05002956 auto pCreateInfo = pipeline->raytracingPipelineCI.ptr();
2957
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002958 SHADER_MODULE_STATE const *module;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002959 spirv_inst_iter entrypoint;
2960
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002961 return ValidatePipelineShaderStage(pCreateInfo->pStages, pipeline, &module, &entrypoint, false);
Jeff Bolzfbe51582018-09-13 10:01:35 -05002962}
2963
Dave Houltona9df0ce2018-02-07 10:51:23 -07002964uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07002965
Dave Houltona9df0ce2018-02-07 10:51:23 -07002966static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
John Zulauf25ea2432019-04-05 10:07:38 -06002967 const auto validation_cache_ci = lvl_find_in_chain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
2968 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06002969 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002970 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002971 return nullptr;
2972}
2973
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002974bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
2975 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002976 bool skip = false;
2977 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002978
Mark Lobodzinskib02a4852019-04-19 12:35:30 -06002979 if (disabled.shader_validation) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002980 return false;
2981 }
2982
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002983 auto have_glsl_shader = device_extensions.vk_nv_glsl_shader;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002984
2985 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski7767ad82019-03-09 13:35:25 -07002986 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
Dave Houlton78d09922018-05-17 15:48:45 -06002987 "VUID-VkShaderModuleCreateInfo-pCode-01376",
2988 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
2989 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002990 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07002991 auto cache = GetValidationCacheInfo(pCreateInfo);
2992 uint32_t hash = 0;
2993 if (cache) {
2994 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002995 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07002996 }
2997
Chris Forbes4ae55b32017-06-09 14:42:56 -07002998 // Use SPIRV-Tools validator to try and catch any issues with the module itself
Dave Houlton0ea2d012018-06-21 14:00:26 -06002999 spv_target_env spirv_environment = SPV_ENV_VULKAN_1_0;
Mark Lobodzinski544def72019-04-19 14:25:59 -06003000 if (api_version >= VK_API_VERSION_1_1) {
Dave Houlton0ea2d012018-06-21 14:00:26 -06003001 spirv_environment = SPV_ENV_VULKAN_1_1;
3002 }
3003 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003004 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003005 spv_diagnostic diag = nullptr;
Karl Schultzfda1b382018-08-08 18:56:11 -06003006 spv_validator_options options = spvValidatorOptionsCreate();
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06003007 if (device_extensions.vk_khr_relaxed_block_layout) {
Karl Schultzfda1b382018-08-08 18:56:11 -06003008 spvValidatorOptionsSetRelaxBlockLayout(options, true);
3009 }
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06003010 if (device_extensions.vk_ext_scalar_block_layout &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06003011 enabled_features.scalar_block_layout_features.scalarBlockLayout == VK_TRUE) {
Tobias Hector6a0ece72018-12-10 12:24:05 +00003012 spvValidatorOptionsSetScalarBlockLayout(options, true);
3013 }
Karl Schultzfda1b382018-08-08 18:56:11 -06003014 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003015 if (spv_valid != SPV_SUCCESS) {
3016 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski7767ad82019-03-09 13:35:25 -07003017 skip |=
3018 log_msg(report_data, spv_valid == SPV_WARNING ? VK_DEBUG_REPORT_WARNING_BIT_EXT : VK_DEBUG_REPORT_ERROR_BIT_EXT,
3019 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_InconsistentSpirv,
3020 "SPIR-V module not valid: %s", diag && diag->error ? diag->error : "(no error text)");
Chris Forbes4ae55b32017-06-09 14:42:56 -07003021 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003022 } else {
3023 if (cache) {
3024 cache->Insert(hash);
3025 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003026 }
3027
Karl Schultzfda1b382018-08-08 18:56:11 -06003028 spvValidatorOptionsDestroy(options);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003029 spvDiagnosticDestroy(diag);
3030 spvContextDestroy(ctx);
3031 }
3032
Chris Forbes4ae55b32017-06-09 14:42:56 -07003033 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003034}
3035
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003036void CoreChecks::PreCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
3037 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule,
3038 void *csm_state_data) {
Mark Lobodzinski1db77e82019-03-01 10:02:54 -07003039 create_shader_module_api_state *csm_state = reinterpret_cast<create_shader_module_api_state *>(csm_state_data);
Mark Lobodzinskib02a4852019-04-19 12:35:30 -06003040 if (enabled.gpu_validation) {
Mark Lobodzinski586d10e2019-03-08 18:19:48 -07003041 GpuPreCallCreateShaderModule(pCreateInfo, pAllocator, pShaderModule, &csm_state->unique_shader_id,
Mark Lobodzinski01734072019-02-13 17:39:15 -07003042 &csm_state->instrumented_create_info, &csm_state->instrumented_pgm);
3043 }
3044}
3045
John Zulauf7eeb6f72019-06-17 11:56:36 -06003046void ValidationStateTracker::PostCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
3047 const VkAllocationCallbacks *pAllocator,
3048 VkShaderModule *pShaderModule, VkResult result,
3049 void *csm_state_data) {
Mark Lobodzinski01734072019-02-13 17:39:15 -07003050 if (VK_SUCCESS != result) return;
Mark Lobodzinski1db77e82019-03-01 10:02:54 -07003051 create_shader_module_api_state *csm_state = reinterpret_cast<create_shader_module_api_state *>(csm_state_data);
Mark Lobodzinski01734072019-02-13 17:39:15 -07003052
Mark Lobodzinski544def72019-04-19 14:25:59 -06003053 spv_target_env spirv_environment = ((api_version >= VK_API_VERSION_1_1) ? SPV_ENV_VULKAN_1_1 : SPV_ENV_VULKAN_1_0);
Mark Lobodzinski01734072019-02-13 17:39:15 -07003054 bool is_spirv = (pCreateInfo->pCode[0] == spv::MagicNumber);
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06003055 std::unique_ptr<SHADER_MODULE_STATE> new_shader_module(
3056 is_spirv ? new SHADER_MODULE_STATE(pCreateInfo, *pShaderModule, spirv_environment, csm_state->unique_shader_id)
3057 : new SHADER_MODULE_STATE());
Mark Lobodzinski7767ad82019-03-09 13:35:25 -07003058 shaderModuleMap[*pShaderModule] = std::move(new_shader_module);
Mark Lobodzinski01734072019-02-13 17:39:15 -07003059}
Lockeaa8fdc02019-04-02 11:59:20 -06003060
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06003061bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader) {
Lockeaa8fdc02019-04-02 11:59:20 -06003062 bool skip = false;
3063 uint32_t local_size_x = 0;
3064 uint32_t local_size_y = 0;
3065 uint32_t local_size_z = 0;
3066 if (FindLocalSize(shader, local_size_x, local_size_y, local_size_z)) {
3067 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003068 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3069 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3070 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
3071 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3072 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06003073 }
3074 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003075 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3076 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3077 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
3078 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3079 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06003080 }
3081 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003082 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3083 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3084 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
3085 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3086 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06003087 }
3088
3089 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3090 uint64_t invocations = local_size_x * local_size_y;
3091 // Prevent overflow.
3092 bool fail = false;
3093 if (invocations > UINT32_MAX || invocations > limit) {
3094 fail = true;
3095 }
3096 if (!fail) {
3097 invocations *= local_size_z;
3098 if (invocations > UINT32_MAX || invocations > limit) {
3099 fail = true;
3100 }
3101 }
3102 if (fail) {
3103 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3104 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations",
locke-lunarg9edc2812019-06-17 23:18:52 -06003105 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
Lockeaa8fdc02019-04-02 11:59:20 -06003106 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
3107 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, local_size_y, local_size_z,
3108 limit);
3109 }
3110 }
3111 return skip;
3112}