blob: 4690bc1a8be59bb140f9571596488047808d698f [file] [log] [blame]
Tony-LunarG73719992020-01-15 10:20:28 -07001/* Copyright (c) 2015-2020 The Khronos Group Inc.
2 * Copyright (c) 2015-2020 Valve Corporation
3 * Copyright (c) 2015-2020 LunarG, Inc.
4 * Copyright (C) 2015-2020 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
Petr Kraus25810d02019-08-27 17:41:15 +020022#include "shader_validation.h"
23
Chris Forbes47567b72017-06-09 12:09:45 -070024#include <cassert>
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +020025#include <chrono>
Petr Kraus25810d02019-08-27 17:41:15 +020026#include <cinttypes>
Jeff Bolzf234bf82019-11-04 14:07:15 -060027#include <cmath>
Petr Kraus25810d02019-08-27 17:41:15 +020028#include <map>
Chris Forbes47567b72017-06-09 12:09:45 -070029#include <sstream>
Petr Kraus25810d02019-08-27 17:41:15 +020030#include <string>
31#include <unordered_map>
32#include <vector>
33
Chris Forbes47567b72017-06-09 12:09:45 -070034#include <SPIRV/spirv.hpp>
35#include "vk_loader_platform.h"
36#include "vk_enum_string_helper.h"
Chris Forbes47567b72017-06-09 12:09:45 -070037#include "vk_layer_data.h"
38#include "vk_layer_extension_utils.h"
39#include "vk_layer_utils.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070040#include "chassis.h"
Chris Forbes47567b72017-06-09 12:09:45 -070041#include "core_validation.h"
Petr Kraus25810d02019-08-27 17:41:15 +020042
Chris Forbes4ae55b32017-06-09 14:42:56 -070043#include "spirv-tools/libspirv.h"
Chris Forbes9a61e082017-07-24 15:35:29 -070044#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070045
Chris Forbes8a6d8cb2019-02-14 14:33:08 -080046void decoration_set::add(uint32_t decoration, uint32_t value) {
47 switch (decoration) {
48 case spv::DecorationLocation:
49 flags |= location_bit;
50 location = value;
51 break;
52 case spv::DecorationPatch:
53 flags |= patch_bit;
54 break;
55 case spv::DecorationRelaxedPrecision:
56 flags |= relaxed_precision_bit;
57 break;
58 case spv::DecorationBlock:
59 flags |= block_bit;
60 break;
61 case spv::DecorationBufferBlock:
62 flags |= buffer_block_bit;
63 break;
64 case spv::DecorationComponent:
65 flags |= component_bit;
66 component = value;
67 break;
68 case spv::DecorationInputAttachmentIndex:
69 flags |= input_attachment_index_bit;
70 input_attachment_index = value;
71 break;
72 case spv::DecorationDescriptorSet:
73 flags |= descriptor_set_bit;
74 descriptor_set = value;
75 break;
76 case spv::DecorationBinding:
77 flags |= binding_bit;
78 binding = value;
79 break;
80 case spv::DecorationNonWritable:
81 flags |= nonwritable_bit;
82 break;
83 case spv::DecorationBuiltIn:
84 flags |= builtin_bit;
85 builtin = value;
86 break;
87 }
88}
89
Chris Forbes47567b72017-06-09 12:09:45 -070090enum FORMAT_TYPE {
91 FORMAT_TYPE_FLOAT = 1, // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader
92 FORMAT_TYPE_SINT = 2,
93 FORMAT_TYPE_UINT = 4,
94};
95
96typedef std::pair<unsigned, unsigned> location_t;
97
Chris Forbes47567b72017-06-09 12:09:45 -070098static shader_stage_attributes shader_stage_attribs[] = {
Ari Suonpaa696b3432019-03-11 14:02:57 +020099 {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT},
100 {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT},
101 {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT},
102 {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT},
103 {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT},
Chris Forbes47567b72017-06-09 12:09:45 -0700104};
105
John Zulauf14c355b2019-06-27 16:09:37 -0600106unsigned ExecutionModelToShaderStageFlagBits(unsigned mode);
107
Chris Forbes47567b72017-06-09 12:09:45 -0700108// SPIRV utility functions
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600109void SHADER_MODULE_STATE::BuildDefIndex() {
Chris Forbes47567b72017-06-09 12:09:45 -0700110 for (auto insn : *this) {
111 switch (insn.opcode()) {
112 // Types
113 case spv::OpTypeVoid:
114 case spv::OpTypeBool:
115 case spv::OpTypeInt:
116 case spv::OpTypeFloat:
117 case spv::OpTypeVector:
118 case spv::OpTypeMatrix:
119 case spv::OpTypeImage:
120 case spv::OpTypeSampler:
121 case spv::OpTypeSampledImage:
122 case spv::OpTypeArray:
123 case spv::OpTypeRuntimeArray:
124 case spv::OpTypeStruct:
125 case spv::OpTypeOpaque:
126 case spv::OpTypePointer:
127 case spv::OpTypeFunction:
128 case spv::OpTypeEvent:
129 case spv::OpTypeDeviceEvent:
130 case spv::OpTypeReserveId:
131 case spv::OpTypeQueue:
132 case spv::OpTypePipe:
Shannon McPherson0fa28232018-11-01 11:59:02 -0600133 case spv::OpTypeAccelerationStructureNV:
Jeff Bolze4356752019-03-07 11:23:46 -0600134 case spv::OpTypeCooperativeMatrixNV:
Chris Forbes47567b72017-06-09 12:09:45 -0700135 def_index[insn.word(1)] = insn.offset();
136 break;
137
138 // Fixed constants
139 case spv::OpConstantTrue:
140 case spv::OpConstantFalse:
141 case spv::OpConstant:
142 case spv::OpConstantComposite:
143 case spv::OpConstantSampler:
144 case spv::OpConstantNull:
145 def_index[insn.word(2)] = insn.offset();
146 break;
147
148 // Specialization constants
149 case spv::OpSpecConstantTrue:
150 case spv::OpSpecConstantFalse:
151 case spv::OpSpecConstant:
152 case spv::OpSpecConstantComposite:
153 case spv::OpSpecConstantOp:
154 def_index[insn.word(2)] = insn.offset();
155 break;
156
157 // Variables
158 case spv::OpVariable:
159 def_index[insn.word(2)] = insn.offset();
160 break;
161
162 // Functions
163 case spv::OpFunction:
164 def_index[insn.word(2)] = insn.offset();
165 break;
166
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800167 // Decorations
168 case spv::OpDecorate: {
169 auto targetId = insn.word(1);
170 decorations[targetId].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u);
171 } break;
172 case spv::OpGroupDecorate: {
173 auto const &src = decorations[insn.word(1)];
174 for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src);
175 } break;
176
John Zulauf14c355b2019-06-27 16:09:37 -0600177 // Entry points ... add to the entrypoint table
178 case spv::OpEntryPoint: {
179 // Entry points do not have an id (the id is the function id) and thus need their own table
180 auto entrypoint_name = (char const *)&insn.word(3);
181 auto execution_model = insn.word(1);
182 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
183 entry_points.emplace(entrypoint_name, EntryPoint{insn.offset(), entrypoint_stage});
184 break;
185 }
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800186
Chris Forbes47567b72017-06-09 12:09:45 -0700187 default:
188 // We don't care about any other defs for now.
189 break;
190 }
191 }
192}
193
Jeff Bolz105d6492018-09-29 15:46:44 -0500194unsigned ExecutionModelToShaderStageFlagBits(unsigned mode) {
195 switch (mode) {
196 case spv::ExecutionModelVertex:
197 return VK_SHADER_STAGE_VERTEX_BIT;
198 case spv::ExecutionModelTessellationControl:
199 return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
200 case spv::ExecutionModelTessellationEvaluation:
201 return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
202 case spv::ExecutionModelGeometry:
203 return VK_SHADER_STAGE_GEOMETRY_BIT;
204 case spv::ExecutionModelFragment:
205 return VK_SHADER_STAGE_FRAGMENT_BIT;
206 case spv::ExecutionModelGLCompute:
207 return VK_SHADER_STAGE_COMPUTE_BIT;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600208 case spv::ExecutionModelRayGenerationNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700209 return VK_SHADER_STAGE_RAYGEN_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600210 case spv::ExecutionModelAnyHitNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700211 return VK_SHADER_STAGE_ANY_HIT_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600212 case spv::ExecutionModelClosestHitNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700213 return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600214 case spv::ExecutionModelMissNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700215 return VK_SHADER_STAGE_MISS_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600216 case spv::ExecutionModelIntersectionNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700217 return VK_SHADER_STAGE_INTERSECTION_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600218 case spv::ExecutionModelCallableNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700219 return VK_SHADER_STAGE_CALLABLE_BIT_NV;
Jeff Bolz105d6492018-09-29 15:46:44 -0500220 case spv::ExecutionModelTaskNV:
221 return VK_SHADER_STAGE_TASK_BIT_NV;
222 case spv::ExecutionModelMeshNV:
223 return VK_SHADER_STAGE_MESH_BIT_NV;
224 default:
225 return 0;
226 }
227}
228
locke-lunargd9a069d2019-09-17 01:50:19 -0600229spirv_inst_iter FindEntrypoint(SHADER_MODULE_STATE const *src, char const *name, VkShaderStageFlagBits stageBits) {
John Zulauf14c355b2019-06-27 16:09:37 -0600230 auto range = src->entry_points.equal_range(name);
231 for (auto it = range.first; it != range.second; ++it) {
232 if (it->second.stage == stageBits) {
233 return src->at(it->second.offset);
Chris Forbes47567b72017-06-09 12:09:45 -0700234 }
235 }
Chris Forbes47567b72017-06-09 12:09:45 -0700236 return src->end();
237}
238
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600239static char const *StorageClassName(unsigned sc) {
Chris Forbes47567b72017-06-09 12:09:45 -0700240 switch (sc) {
241 case spv::StorageClassInput:
242 return "input";
243 case spv::StorageClassOutput:
244 return "output";
245 case spv::StorageClassUniformConstant:
246 return "const uniform";
247 case spv::StorageClassUniform:
248 return "uniform";
249 case spv::StorageClassWorkgroup:
250 return "workgroup local";
251 case spv::StorageClassCrossWorkgroup:
252 return "workgroup global";
253 case spv::StorageClassPrivate:
254 return "private global";
255 case spv::StorageClassFunction:
256 return "function";
257 case spv::StorageClassGeneric:
258 return "generic";
259 case spv::StorageClassAtomicCounter:
260 return "atomic counter";
261 case spv::StorageClassImage:
262 return "image";
263 case spv::StorageClassPushConstant:
264 return "push constant";
Chris Forbes9f89d752018-03-07 12:57:48 -0800265 case spv::StorageClassStorageBuffer:
266 return "storage buffer";
Chris Forbes47567b72017-06-09 12:09:45 -0700267 default:
268 return "unknown";
269 }
270}
271
272// Get the value of an integral constant
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600273unsigned GetConstantValue(SHADER_MODULE_STATE const *src, unsigned id) {
Chris Forbes47567b72017-06-09 12:09:45 -0700274 auto value = src->get_def(id);
275 assert(value != src->end());
276
277 if (value.opcode() != spv::OpConstant) {
278 // TODO: Either ensure that the specialization transform is already performed on a module we're
279 // considering here, OR -- specialize on the fly now.
280 return 1;
281 }
282
283 return value.word(3);
284}
285
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600286static void DescribeTypeInner(std::ostringstream &ss, SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700287 auto insn = src->get_def(type);
288 assert(insn != src->end());
289
290 switch (insn.opcode()) {
291 case spv::OpTypeBool:
292 ss << "bool";
293 break;
294 case spv::OpTypeInt:
295 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
296 break;
297 case spv::OpTypeFloat:
298 ss << "float" << insn.word(2);
299 break;
300 case spv::OpTypeVector:
301 ss << "vec" << insn.word(3) << " of ";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600302 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700303 break;
304 case spv::OpTypeMatrix:
305 ss << "mat" << insn.word(3) << " of ";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600306 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700307 break;
308 case spv::OpTypeArray:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600309 ss << "arr[" << GetConstantValue(src, insn.word(3)) << "] of ";
310 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700311 break;
Chris Forbes062f1222018-08-21 15:34:15 -0700312 case spv::OpTypeRuntimeArray:
313 ss << "runtime arr[] of ";
314 DescribeTypeInner(ss, src, insn.word(2));
315 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700316 case spv::OpTypePointer:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600317 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
318 DescribeTypeInner(ss, src, insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700319 break;
320 case spv::OpTypeStruct: {
321 ss << "struct of (";
322 for (unsigned i = 2; i < insn.len(); i++) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600323 DescribeTypeInner(ss, src, insn.word(i));
Chris Forbes47567b72017-06-09 12:09:45 -0700324 if (i == insn.len() - 1) {
325 ss << ")";
326 } else {
327 ss << ", ";
328 }
329 }
330 break;
331 }
332 case spv::OpTypeSampler:
333 ss << "sampler";
334 break;
335 case spv::OpTypeSampledImage:
336 ss << "sampler+";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600337 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700338 break;
339 case spv::OpTypeImage:
340 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
341 break;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600342 case spv::OpTypeAccelerationStructureNV:
Jeff Bolz105d6492018-09-29 15:46:44 -0500343 ss << "accelerationStruture";
344 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700345 default:
346 ss << "oddtype";
347 break;
348 }
349}
350
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600351static std::string DescribeType(SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700352 std::ostringstream ss;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600353 DescribeTypeInner(ss, src, type);
Chris Forbes47567b72017-06-09 12:09:45 -0700354 return ss.str();
355}
356
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600357static bool IsNarrowNumericType(spirv_inst_iter type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700358 if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false;
359 return type.word(2) < 64;
360}
361
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600362static 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 -0600363 bool b_arrayed, bool relaxed) {
Chris Forbes47567b72017-06-09 12:09:45 -0700364 // Walk two type trees together, and complain about differences
365 auto a_insn = a->get_def(a_type);
366 auto b_insn = b->get_def(b_type);
367 assert(a_insn != a->end());
368 assert(b_insn != b->end());
369
Chris Forbes062f1222018-08-21 15:34:15 -0700370 // Ignore runtime-sized arrays-- they cannot appear in these interfaces.
371
Chris Forbes47567b72017-06-09 12:09:45 -0700372 if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600373 return TypesMatch(a, b, a_insn.word(2), b_type, false, b_arrayed, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700374 }
375
376 if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) {
377 // 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 -0600378 return TypesMatch(a, b, a_type, b_insn.word(2), a_arrayed, false, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700379 }
380
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600381 if (a_insn.opcode() == spv::OpTypeVector && relaxed && IsNarrowNumericType(b_insn)) {
382 return TypesMatch(a, b, a_insn.word(2), b_type, a_arrayed, b_arrayed, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700383 }
384
385 if (a_insn.opcode() != b_insn.opcode()) {
386 return false;
387 }
388
389 if (a_insn.opcode() == spv::OpTypePointer) {
390 // Match on pointee type. storage class is expected to differ
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600391 return TypesMatch(a, b, a_insn.word(3), b_insn.word(3), a_arrayed, b_arrayed, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700392 }
393
394 if (a_arrayed || b_arrayed) {
395 // If we havent resolved array-of-verts by here, we're not going to.
396 return false;
397 }
398
399 switch (a_insn.opcode()) {
400 case spv::OpTypeBool:
401 return true;
402 case spv::OpTypeInt:
403 // Match on width, signedness
404 return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3);
405 case spv::OpTypeFloat:
406 // Match on width
407 return a_insn.word(2) == b_insn.word(2);
408 case spv::OpTypeVector:
409 // Match on element type, count.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600410 if (!TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false;
411 if (relaxed && IsNarrowNumericType(a->get_def(a_insn.word(2)))) {
Chris Forbes47567b72017-06-09 12:09:45 -0700412 return a_insn.word(3) >= b_insn.word(3);
413 } else {
414 return a_insn.word(3) == b_insn.word(3);
415 }
416 case spv::OpTypeMatrix:
417 // Match on element type, count.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600418 return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
Dave Houltona9df0ce2018-02-07 10:51:23 -0700419 a_insn.word(3) == b_insn.word(3);
Chris Forbes47567b72017-06-09 12:09:45 -0700420 case spv::OpTypeArray:
421 // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from
422 // 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 -0600423 return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
424 GetConstantValue(a, a_insn.word(3)) == GetConstantValue(b, b_insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700425 case spv::OpTypeStruct:
426 // Match on all element types
Dave Houltona9df0ce2018-02-07 10:51:23 -0700427 {
428 if (a_insn.len() != b_insn.len()) {
429 return false; // Structs cannot match if member counts differ
Chris Forbes47567b72017-06-09 12:09:45 -0700430 }
Chris Forbes47567b72017-06-09 12:09:45 -0700431
Dave Houltona9df0ce2018-02-07 10:51:23 -0700432 for (unsigned i = 2; i < a_insn.len(); i++) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600433 if (!TypesMatch(a, b, a_insn.word(i), b_insn.word(i), a_arrayed, b_arrayed, false)) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700434 return false;
435 }
436 }
437
438 return true;
439 }
Chris Forbes47567b72017-06-09 12:09:45 -0700440 default:
441 // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match.
442 return false;
443 }
444}
445
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600446static unsigned ValueOrDefault(std::unordered_map<unsigned, unsigned> const &map, unsigned id, unsigned def) {
Chris Forbes47567b72017-06-09 12:09:45 -0700447 auto it = map.find(id);
448 if (it == map.end())
449 return def;
450 else
451 return it->second;
452}
453
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600454static unsigned GetLocationsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) {
Chris Forbes47567b72017-06-09 12:09:45 -0700455 auto insn = src->get_def(type);
456 assert(insn != src->end());
457
458 switch (insn.opcode()) {
459 case spv::OpTypePointer:
460 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
461 // pointers around.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600462 return GetLocationsConsumedByType(src, insn.word(3), strip_array_level);
Chris Forbes47567b72017-06-09 12:09:45 -0700463 case spv::OpTypeArray:
464 if (strip_array_level) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600465 return GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700466 } else {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600467 return GetConstantValue(src, insn.word(3)) * GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700468 }
469 case spv::OpTypeMatrix:
470 // Num locations is the dimension * element size
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600471 return insn.word(3) * GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700472 case spv::OpTypeVector: {
473 auto scalar_type = src->get_def(insn.word(2));
474 auto bit_width =
475 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
476
477 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
478 return (bit_width * insn.word(3) + 127) / 128;
479 }
480 default:
481 // Everything else is just 1.
482 return 1;
483
484 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
485 }
486}
487
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600488static unsigned GetComponentsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200489 auto insn = src->get_def(type);
490 assert(insn != src->end());
491
492 switch (insn.opcode()) {
493 case spv::OpTypePointer:
494 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
495 // pointers around.
496 return GetComponentsConsumedByType(src, insn.word(3), strip_array_level);
497 case spv::OpTypeStruct: {
498 uint32_t sum = 0;
499 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
500 sum += GetComponentsConsumedByType(src, insn.word(i), false);
501 }
502 return sum;
503 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500504 case spv::OpTypeArray:
505 if (strip_array_level) {
506 return GetComponentsConsumedByType(src, insn.word(2), false);
507 } else {
508 return GetConstantValue(src, insn.word(3)) * GetComponentsConsumedByType(src, insn.word(2), false);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200509 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200510 case spv::OpTypeMatrix:
511 // Num locations is the dimension * element size
512 return insn.word(3) * GetComponentsConsumedByType(src, insn.word(2), false);
513 case spv::OpTypeVector: {
514 auto scalar_type = src->get_def(insn.word(2));
515 auto bit_width =
516 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
517 // One component is 32-bit
518 return (bit_width * insn.word(3) + 31) / 32;
519 }
520 case spv::OpTypeFloat: {
521 auto bit_width = insn.word(2);
522 return (bit_width + 31) / 32;
523 }
524 case spv::OpTypeInt: {
525 auto bit_width = insn.word(2);
526 return (bit_width + 31) / 32;
527 }
528 case spv::OpConstant:
529 return GetComponentsConsumedByType(src, insn.word(1), false);
530 default:
531 return 0;
532 }
533}
534
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600535static unsigned GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -0700536 switch (format) {
537 case VK_FORMAT_R64G64B64A64_SFLOAT:
538 case VK_FORMAT_R64G64B64A64_SINT:
539 case VK_FORMAT_R64G64B64A64_UINT:
540 case VK_FORMAT_R64G64B64_SFLOAT:
541 case VK_FORMAT_R64G64B64_SINT:
542 case VK_FORMAT_R64G64B64_UINT:
543 return 2;
544 default:
545 return 1;
546 }
547}
548
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600549static unsigned GetFormatType(VkFormat fmt) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700550 if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT;
551 if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT;
552 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
553 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700554 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
555 return FORMAT_TYPE_FLOAT;
556}
557
558// 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 -0700559// also used for input attachments, as we statically know their format.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600560static unsigned GetFundamentalType(SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700561 auto insn = src->get_def(type);
562 assert(insn != src->end());
563
564 switch (insn.opcode()) {
565 case spv::OpTypeInt:
566 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
567 case spv::OpTypeFloat:
568 return FORMAT_TYPE_FLOAT;
569 case spv::OpTypeVector:
Chris Forbes47567b72017-06-09 12:09:45 -0700570 case spv::OpTypeMatrix:
Chris Forbes47567b72017-06-09 12:09:45 -0700571 case spv::OpTypeArray:
Chris Forbes062f1222018-08-21 15:34:15 -0700572 case spv::OpTypeRuntimeArray:
573 case spv::OpTypeImage:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600574 return GetFundamentalType(src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700575 case spv::OpTypePointer:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600576 return GetFundamentalType(src, insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700577
578 default:
579 return 0;
580 }
581}
582
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600583static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700584 uint32_t bit_pos = uint32_t(u_ffs(stage));
585 return bit_pos - 1;
586}
587
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600588static 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 -0700589 while (true) {
590 if (def.opcode() == spv::OpTypePointer) {
591 def = src->get_def(def.word(3));
592 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
593 def = src->get_def(def.word(2));
594 is_array_of_verts = false;
595 } else if (def.opcode() == spv::OpTypeStruct) {
596 return def;
597 } else {
598 return src->end();
599 }
600 }
601}
602
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600603static bool CollectInterfaceBlockMembers(SHADER_MODULE_STATE const *src, std::map<location_t, interface_var> *out,
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800604 bool is_array_of_verts, uint32_t id, uint32_t type_id, bool is_patch,
605 int /*first_location*/) {
Chris Forbes47567b72017-06-09 12:09:45 -0700606 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600607 auto type = GetStructType(src, src->get_def(type_id), is_array_of_verts && !is_patch);
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800608 if (type == src->end() || !(src->get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700609 // This isn't an interface block.
Chris Forbesa313d772017-06-13 13:59:41 -0700610 return false;
Chris Forbes47567b72017-06-09 12:09:45 -0700611 }
612
613 std::unordered_map<unsigned, unsigned> member_components;
614 std::unordered_map<unsigned, unsigned> member_relaxed_precision;
Chris Forbesa313d772017-06-13 13:59:41 -0700615 std::unordered_map<unsigned, unsigned> member_patch;
Chris Forbes47567b72017-06-09 12:09:45 -0700616
617 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
618 for (auto insn : *src) {
619 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
620 unsigned member_index = insn.word(2);
621
622 if (insn.word(3) == spv::DecorationComponent) {
623 unsigned component = insn.word(4);
624 member_components[member_index] = component;
625 }
626
627 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
628 member_relaxed_precision[member_index] = 1;
629 }
Chris Forbesa313d772017-06-13 13:59:41 -0700630
631 if (insn.word(3) == spv::DecorationPatch) {
632 member_patch[member_index] = 1;
633 }
Chris Forbes47567b72017-06-09 12:09:45 -0700634 }
635 }
636
Chris Forbesa313d772017-06-13 13:59:41 -0700637 // TODO: correctly handle location assignment from outside
638
Chris Forbes47567b72017-06-09 12:09:45 -0700639 // Second pass -- produce the output, from Location decorations
640 for (auto insn : *src) {
641 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
642 unsigned member_index = insn.word(2);
643 unsigned member_type_id = type.word(2 + member_index);
644
645 if (insn.word(3) == spv::DecorationLocation) {
646 unsigned location = insn.word(4);
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600647 unsigned num_locations = GetLocationsConsumedByType(src, member_type_id, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700648 auto component_it = member_components.find(member_index);
649 unsigned component = component_it == member_components.end() ? 0 : component_it->second;
650 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
Dave Houltona9df0ce2018-02-07 10:51:23 -0700651 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700652
653 for (unsigned int offset = 0; offset < num_locations; offset++) {
654 interface_var v = {};
655 v.id = id;
656 // TODO: member index in interface_var too?
657 v.type_id = member_type_id;
658 v.offset = offset;
Chris Forbesa313d772017-06-13 13:59:41 -0700659 v.is_patch = member_is_patch;
Chris Forbes47567b72017-06-09 12:09:45 -0700660 v.is_block_member = true;
661 v.is_relaxed_precision = is_relaxed_precision;
662 (*out)[std::make_pair(location + offset, component)] = v;
663 }
664 }
665 }
666 }
Chris Forbesa313d772017-06-13 13:59:41 -0700667
668 return true;
Chris Forbes47567b72017-06-09 12:09:45 -0700669}
670
Ari Suonpaa696b3432019-03-11 14:02:57 +0200671static std::vector<uint32_t> FindEntrypointInterfaces(spirv_inst_iter entrypoint) {
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800672 assert(entrypoint.opcode() == spv::OpEntryPoint);
673
Ari Suonpaa696b3432019-03-11 14:02:57 +0200674 std::vector<uint32_t> interfaces;
675 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
676 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
677 uint32_t word = 3;
678 while (entrypoint.word(word) & 0xff000000u) {
679 ++word;
680 }
681 ++word;
682
683 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
684
685 return interfaces;
686}
687
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600688static std::map<location_t, interface_var> CollectInterfaceByLocation(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600689 spv::StorageClass sinterface, bool is_array_of_verts) {
Chris Forbes47567b72017-06-09 12:09:45 -0700690 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
691
Chris Forbes47567b72017-06-09 12:09:45 -0700692 std::map<location_t, interface_var> out;
693
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800694 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
695 auto insn = src->get_def(iid);
Chris Forbes47567b72017-06-09 12:09:45 -0700696 assert(insn != src->end());
697 assert(insn.opcode() == spv::OpVariable);
698
699 if (insn.word(3) == static_cast<uint32_t>(sinterface)) {
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800700 auto d = src->get_decorations(iid);
Chris Forbes47567b72017-06-09 12:09:45 -0700701 unsigned id = insn.word(2);
702 unsigned type = insn.word(1);
703
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800704 int location = d.location;
705 int builtin = d.builtin;
706 unsigned component = d.component;
707 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
708 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700709
Dave Houltona9df0ce2018-02-07 10:51:23 -0700710 if (builtin != -1)
711 continue;
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800712 else if (!CollectInterfaceBlockMembers(src, &out, is_array_of_verts, id, type, is_patch, location)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700713 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
714 // one result for each.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600715 unsigned num_locations = GetLocationsConsumedByType(src, type, is_array_of_verts && !is_patch);
Chris Forbes47567b72017-06-09 12:09:45 -0700716 for (unsigned int offset = 0; offset < num_locations; offset++) {
717 interface_var v = {};
718 v.id = id;
719 v.type_id = type;
720 v.offset = offset;
721 v.is_patch = is_patch;
722 v.is_relaxed_precision = is_relaxed_precision;
723 out[std::make_pair(location + offset, component)] = v;
724 }
Chris Forbes47567b72017-06-09 12:09:45 -0700725 }
726 }
727 }
728
729 return out;
730}
731
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600732static std::vector<uint32_t> CollectBuiltinBlockMembers(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint,
Ari Suonpaa696b3432019-03-11 14:02:57 +0200733 uint32_t storageClass) {
734 std::vector<uint32_t> variables;
735 std::vector<uint32_t> builtinStructMembers;
736 std::vector<uint32_t> builtinDecorations;
737
738 for (auto insn : *src) {
739 switch (insn.opcode()) {
740 // Find all built-in member decorations
741 case spv::OpMemberDecorate:
742 if (insn.word(3) == spv::DecorationBuiltIn) {
743 builtinStructMembers.push_back(insn.word(1));
744 }
745 break;
746 // Find all built-in decorations
747 case spv::OpDecorate:
748 switch (insn.word(2)) {
749 case spv::DecorationBlock: {
750 uint32_t blockID = insn.word(1);
751 for (auto builtInBlockID : builtinStructMembers) {
752 // Check if one of the members of the block are built-in -> the block is built-in
753 if (blockID == builtInBlockID) {
754 builtinDecorations.push_back(blockID);
755 break;
756 }
757 }
758 break;
759 }
760 case spv::DecorationBuiltIn:
761 builtinDecorations.push_back(insn.word(1));
762 break;
763 default:
764 break;
765 }
766 break;
767 default:
768 break;
769 }
770 }
771
772 // Find all interface variables belonging to the entrypoint and matching the storage class
773 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
774 auto def = src->get_def(id);
775 assert(def != src->end());
776 assert(def.opcode() == spv::OpVariable);
777
778 if (def.word(3) == storageClass) variables.push_back(def.word(1));
779 }
780
781 // Find all members belonging to the builtin block selected
782 std::vector<uint32_t> builtinBlockMembers;
783 for (auto &var : variables) {
784 auto def = src->get_def(src->get_def(var).word(3));
785
786 // It could be an array of IO blocks. The element type should be the struct defining the block contents
787 if (def.opcode() == spv::OpTypeArray) def = src->get_def(def.word(2));
788
789 // Now find all members belonging to the struct defining the IO block
790 if (def.opcode() == spv::OpTypeStruct) {
791 for (auto builtInID : builtinDecorations) {
792 if (builtInID == def.word(1)) {
793 for (int i = 2; i < (int)def.len(); i++)
794 builtinBlockMembers.push_back(spv::BuiltInMax); // Start with undefined builtin for each struct member.
795 // These shouldn't be left after replacing.
796 for (auto insn : *src) {
797 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == builtInID &&
798 insn.word(3) == spv::DecorationBuiltIn) {
799 auto structIndex = insn.word(2);
800 assert(structIndex < builtinBlockMembers.size());
801 builtinBlockMembers[structIndex] = insn.word(4);
802 }
803 }
804 }
805 }
806 }
807 }
808
809 return builtinBlockMembers;
810}
811
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600812static std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex(
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600813 SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids) {
Chris Forbes47567b72017-06-09 12:09:45 -0700814 std::vector<std::pair<uint32_t, interface_var>> out;
815
816 for (auto insn : *src) {
817 if (insn.opcode() == spv::OpDecorate) {
818 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
819 auto attachment_index = insn.word(3);
820 auto id = insn.word(1);
821
822 if (accessible_ids.count(id)) {
823 auto def = src->get_def(id);
824 assert(def != src->end());
825
826 if (def.opcode() == spv::OpVariable && insn.word(3) == spv::StorageClassUniformConstant) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600827 auto num_locations = GetLocationsConsumedByType(src, def.word(1), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700828 for (unsigned int offset = 0; offset < num_locations; offset++) {
829 interface_var v = {};
830 v.id = id;
831 v.type_id = def.word(1);
832 v.offset = offset;
833 out.emplace_back(attachment_index + offset, v);
834 }
835 }
836 }
837 }
838 }
839 }
840
841 return out;
842}
843
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600844static bool IsWritableDescriptorType(SHADER_MODULE_STATE const *module, uint32_t type_id, bool is_storage_buffer) {
Chris Forbes8af24522018-03-07 11:37:45 -0800845 auto type = module->get_def(type_id);
846
847 // 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 -0700848 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
849 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray) {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700850 type = module->get_def(type.word(2)); // Element type
Chris Forbes8af24522018-03-07 11:37:45 -0800851 } else {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700852 type = module->get_def(type.word(3)); // Pointee type
Chris Forbes8af24522018-03-07 11:37:45 -0800853 }
854 }
855
856 switch (type.opcode()) {
857 case spv::OpTypeImage: {
858 auto dim = type.word(3);
859 auto sampled = type.word(7);
860 return sampled == 2 && dim != spv::DimSubpassData;
861 }
862
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700863 case spv::OpTypeStruct: {
864 std::unordered_set<unsigned> nonwritable_members;
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800865 if (module->get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Chris Forbes8af24522018-03-07 11:37:45 -0800866 for (auto insn : *module) {
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800867 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1) &&
868 insn.word(3) == spv::DecorationNonWritable) {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700869 nonwritable_members.insert(insn.word(2));
Chris Forbes8af24522018-03-07 11:37:45 -0800870 }
871 }
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700872
873 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
874 // as nonwritable.
875 return is_storage_buffer && nonwritable_members.size() != type.len() - 2;
876 }
Chris Forbes8af24522018-03-07 11:37:45 -0800877 }
878
879 return false;
880}
881
locke-lunargd9a069d2019-09-17 01:50:19 -0600882std::vector<std::pair<descriptor_slot_t, interface_var>> CollectInterfaceByDescriptorSlot(
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700883 SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids, bool *has_writable_descriptor) {
Chris Forbes47567b72017-06-09 12:09:45 -0700884 std::vector<std::pair<descriptor_slot_t, interface_var>> out;
885
886 for (auto id : accessible_ids) {
887 auto insn = src->get_def(id);
888 assert(insn != src->end());
889
890 if (insn.opcode() == spv::OpVariable &&
Chris Forbes9f89d752018-03-07 12:57:48 -0800891 (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant ||
892 insn.word(3) == spv::StorageClassStorageBuffer)) {
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800893 auto d = src->get_decorations(insn.word(2));
894 unsigned set = d.descriptor_set;
895 unsigned binding = d.binding;
Chris Forbes47567b72017-06-09 12:09:45 -0700896
897 interface_var v = {};
898 v.id = insn.word(2);
899 v.type_id = insn.word(1);
900 out.emplace_back(std::make_pair(set, binding), v);
Chris Forbes8af24522018-03-07 11:37:45 -0800901
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800902 if (!(d.flags & decoration_set::nonwritable_bit) &&
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700903 IsWritableDescriptorType(src, insn.word(1), insn.word(3) == spv::StorageClassStorageBuffer)) {
Chris Forbes8af24522018-03-07 11:37:45 -0800904 *has_writable_descriptor = true;
905 }
Chris Forbes47567b72017-06-09 12:09:45 -0700906 }
907 }
908
909 return out;
910}
911
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700912bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700913 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
914 // be specified only once.
915 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
916 bool skip = false;
917
918 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
919 auto desc = &vi->pVertexBindingDescriptions[i];
920 auto &binding = bindings[desc->binding];
921 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600922 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600923 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 -0600924 kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
Chris Forbes47567b72017-06-09 12:09:45 -0700925 desc->binding);
926 } else {
927 binding = desc;
928 }
929 }
930
931 return skip;
932}
933
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700934bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs,
935 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700936 bool skip = false;
937
Petr Kraus25810d02019-08-27 17:41:15 +0200938 const auto inputs = CollectInterfaceByLocation(vs, entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700939
940 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200941 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700942 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200943 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
944 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
945 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700946 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
947 }
948 }
949 }
950
Petr Kraus25810d02019-08-27 17:41:15 +0200951 struct AttribInputPair {
952 const VkVertexInputAttributeDescription *attrib = nullptr;
953 const interface_var *input = nullptr;
954 };
955 std::map<uint32_t, AttribInputPair> location_map;
956 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
957 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700958
Petr Kraus25810d02019-08-27 17:41:15 +0200959 for (const auto location_it : location_map) {
960 const auto location = location_it.first;
961 const auto attrib = location_it.second.attrib;
962 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600963
Petr Kraus25810d02019-08-27 17:41:15 +0200964 if (attrib && !input) {
965 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
966 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed,
967 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
968 } else if (!attrib && input) {
Mark Young4e919b22018-05-21 15:53:59 -0600969 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 -0600970 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
Petr Kraus25810d02019-08-27 17:41:15 +0200971 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
972 } else if (attrib && input) {
973 const auto attrib_type = GetFormatType(attrib->format);
974 const auto input_type = GetFundamentalType(vs, input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700975
976 // Type checking
977 if (!(attrib_type & input_type)) {
Mark Young4e919b22018-05-21 15:53:59 -0600978 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 -0600979 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Petr Kraus25810d02019-08-27 17:41:15 +0200980 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
981 string_VkFormat(attrib->format), location, DescribeType(vs, input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700982 }
Petr Kraus25810d02019-08-27 17:41:15 +0200983 } else { // !attrib && !input
984 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700985 }
986 }
987
988 return skip;
989}
990
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700991bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint,
992 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200993 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700994
Petr Kraus25810d02019-08-27 17:41:15 +0200995 const auto rpci = pipeline->rp_state->createInfo.ptr();
996
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600997 struct Attachment {
998 const VkAttachmentReference2KHR *reference = nullptr;
999 const VkAttachmentDescription2KHR *attachment = nullptr;
1000 const interface_var *output = nullptr;
1001 };
1002 std::map<uint32_t, Attachment> location_map;
1003
Petr Kraus25810d02019-08-27 17:41:15 +02001004 const auto subpass = rpci->pSubpasses[subpass_index];
1005 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -06001006 auto const &reference = subpass.pColorAttachments[i];
1007 location_map[i].reference = &reference;
1008 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
1009 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
1010 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
Chris Forbes47567b72017-06-09 12:09:45 -07001011 }
1012 }
1013
Chris Forbes47567b72017-06-09 12:09:45 -07001014 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
1015
Petr Kraus25810d02019-08-27 17:41:15 +02001016 const auto outputs = CollectInterfaceByLocation(fs, entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -06001017 for (const auto &output_it : outputs) {
1018 auto const location = output_it.first.first;
1019 location_map[location].output = &output_it.second;
1020 }
Chris Forbes47567b72017-06-09 12:09:45 -07001021
Petr Kraus25810d02019-08-27 17:41:15 +02001022 const bool alphaToCoverageEnabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL &&
1023 pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -07001024
Petr Kraus25810d02019-08-27 17:41:15 +02001025 for (const auto location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -06001026 const auto reference = location_it.second.reference;
1027 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
1028 continue;
1029 }
1030
Petr Kraus25810d02019-08-27 17:41:15 +02001031 const auto location = location_it.first;
1032 const auto attachment = location_it.second.attachment;
1033 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +02001034 if (attachment && !output) {
1035 if (pipeline->attachments[location].colorWriteMask != 0) {
1036 skip |=
1037 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1038 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
1039 "Attachment %" PRIu32 " not written by fragment shader; undefined values will be written to attachment",
1040 location);
1041 }
1042 } else if (!attachment && output) {
1043 if (!(alphaToCoverageEnabled && location == 0)) {
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001044 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1045 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed,
Petr Kraus25810d02019-08-27 17:41:15 +02001046 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001047 }
Petr Kraus25810d02019-08-27 17:41:15 +02001048 } else if (attachment && output) {
1049 const auto attachment_type = GetFormatType(attachment->format);
1050 const auto output_type = GetFundamentalType(fs, output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -07001051
1052 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +02001053 if (!(output_type & attachment_type)) {
1054 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1055 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
1056 "Attachment %" PRIu32
1057 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
1058 location, string_VkFormat(attachment->format), DescribeType(fs, output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001059 }
Petr Kraus25810d02019-08-27 17:41:15 +02001060 } else { // !attachment && !output
1061 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -07001062 }
1063 }
1064
Petr Kraus25810d02019-08-27 17:41:15 +02001065 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
1066 bool locationZeroHasAlpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
1067 GetComponentsConsumedByType(fs, output_zero->type_id, false) == 4;
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001068 if (alphaToCoverageEnabled && !locationZeroHasAlpha) {
1069 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1070 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
1071 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
1072 }
1073
Chris Forbes47567b72017-06-09 12:09:45 -07001074 return skip;
1075}
1076
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001077// For PointSize analysis we need to know if the variable decorated with the PointSize built-in was actually written to.
1078// This function examines instructions in the static call tree for a write to this variable.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001079static bool IsPointSizeWritten(SHADER_MODULE_STATE const *src, spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001080 auto type = builtin_instr.opcode();
1081 uint32_t target_id = builtin_instr.word(1);
1082 bool init_complete = false;
1083
1084 if (type == spv::OpMemberDecorate) {
1085 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1086 auto insn = entrypoint;
1087 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1088 switch (insn.opcode()) {
1089 case spv::OpTypePointer:
1090 if ((insn.word(3) == target_id) && (insn.word(2) == spv::StorageClassOutput)) {
1091 target_id = insn.word(1);
1092 }
1093 break;
1094 case spv::OpVariable:
1095 if (insn.word(1) == target_id) {
1096 target_id = insn.word(2);
1097 init_complete = true;
1098 }
1099 break;
1100 }
1101 insn++;
1102 }
1103 }
1104
Mark Lobodzinskif84b0b42018-09-11 14:54:32 -06001105 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1106
1107 bool found_write = false;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001108 std::unordered_set<uint32_t> worklist;
1109 worklist.insert(entrypoint.word(2));
1110
1111 // Follow instructions in call graph looking for writes to target
1112 while (!worklist.empty() && !found_write) {
1113 auto id_iter = worklist.begin();
1114 auto id = *id_iter;
1115 worklist.erase(id_iter);
1116
1117 auto insn = src->get_def(id);
1118 if (insn == src->end()) {
1119 continue;
1120 }
1121
1122 if (insn.opcode() == spv::OpFunction) {
1123 // Scan body of function looking for other function calls or items in our ID chain
1124 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
1125 switch (insn.opcode()) {
1126 case spv::OpAccessChain:
1127 if (insn.word(3) == target_id) {
1128 if (type == spv::OpMemberDecorate) {
1129 auto value = GetConstantValue(src, insn.word(4));
1130 if (value == builtin_instr.word(2)) {
1131 target_id = insn.word(2);
1132 }
1133 } else {
1134 target_id = insn.word(2);
1135 }
1136 }
1137 break;
1138 case spv::OpStore:
1139 if (insn.word(1) == target_id) {
1140 found_write = true;
1141 }
1142 break;
1143 case spv::OpFunctionCall:
1144 worklist.insert(insn.word(3));
1145 break;
1146 }
1147 }
1148 }
1149 }
1150 return found_write;
1151}
1152
Chris Forbes47567b72017-06-09 12:09:45 -07001153// For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is
1154// important for identifying the set of shader resources actually used by an entrypoint, for example.
1155// Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
1156// - NOT the shader input/output interfaces.
1157//
1158// TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
1159// converting parts of this to be generated from the machine-readable spec instead.
locke-lunargd9a069d2019-09-17 01:50:19 -06001160std::unordered_set<uint32_t> MarkAccessibleIds(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) {
Chris Forbes47567b72017-06-09 12:09:45 -07001161 std::unordered_set<uint32_t> ids;
1162 std::unordered_set<uint32_t> worklist;
1163 worklist.insert(entrypoint.word(2));
1164
1165 while (!worklist.empty()) {
1166 auto id_iter = worklist.begin();
1167 auto id = *id_iter;
1168 worklist.erase(id_iter);
1169
1170 auto insn = src->get_def(id);
1171 if (insn == src->end()) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001172 // 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 -07001173 // that we may not care about.
1174 continue;
1175 }
1176
1177 // Try to add to the output set
1178 if (!ids.insert(id).second) {
1179 continue; // If we already saw this id, we don't want to walk it again.
1180 }
1181
1182 switch (insn.opcode()) {
1183 case spv::OpFunction:
1184 // Scan whole body of the function, enlisting anything interesting
1185 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
1186 switch (insn.opcode()) {
1187 case spv::OpLoad:
1188 case spv::OpAtomicLoad:
1189 case spv::OpAtomicExchange:
1190 case spv::OpAtomicCompareExchange:
1191 case spv::OpAtomicCompareExchangeWeak:
1192 case spv::OpAtomicIIncrement:
1193 case spv::OpAtomicIDecrement:
1194 case spv::OpAtomicIAdd:
1195 case spv::OpAtomicISub:
1196 case spv::OpAtomicSMin:
1197 case spv::OpAtomicUMin:
1198 case spv::OpAtomicSMax:
1199 case spv::OpAtomicUMax:
1200 case spv::OpAtomicAnd:
1201 case spv::OpAtomicOr:
1202 case spv::OpAtomicXor:
1203 worklist.insert(insn.word(3)); // ptr
1204 break;
1205 case spv::OpStore:
1206 case spv::OpAtomicStore:
1207 worklist.insert(insn.word(1)); // ptr
1208 break;
1209 case spv::OpAccessChain:
1210 case spv::OpInBoundsAccessChain:
1211 worklist.insert(insn.word(3)); // base ptr
1212 break;
1213 case spv::OpSampledImage:
1214 case spv::OpImageSampleImplicitLod:
1215 case spv::OpImageSampleExplicitLod:
1216 case spv::OpImageSampleDrefImplicitLod:
1217 case spv::OpImageSampleDrefExplicitLod:
1218 case spv::OpImageSampleProjImplicitLod:
1219 case spv::OpImageSampleProjExplicitLod:
1220 case spv::OpImageSampleProjDrefImplicitLod:
1221 case spv::OpImageSampleProjDrefExplicitLod:
1222 case spv::OpImageFetch:
1223 case spv::OpImageGather:
1224 case spv::OpImageDrefGather:
1225 case spv::OpImageRead:
1226 case spv::OpImage:
1227 case spv::OpImageQueryFormat:
1228 case spv::OpImageQueryOrder:
1229 case spv::OpImageQuerySizeLod:
1230 case spv::OpImageQuerySize:
1231 case spv::OpImageQueryLod:
1232 case spv::OpImageQueryLevels:
1233 case spv::OpImageQuerySamples:
1234 case spv::OpImageSparseSampleImplicitLod:
1235 case spv::OpImageSparseSampleExplicitLod:
1236 case spv::OpImageSparseSampleDrefImplicitLod:
1237 case spv::OpImageSparseSampleDrefExplicitLod:
1238 case spv::OpImageSparseSampleProjImplicitLod:
1239 case spv::OpImageSparseSampleProjExplicitLod:
1240 case spv::OpImageSparseSampleProjDrefImplicitLod:
1241 case spv::OpImageSparseSampleProjDrefExplicitLod:
1242 case spv::OpImageSparseFetch:
1243 case spv::OpImageSparseGather:
1244 case spv::OpImageSparseDrefGather:
1245 case spv::OpImageTexelPointer:
1246 worklist.insert(insn.word(3)); // Image or sampled image
1247 break;
1248 case spv::OpImageWrite:
1249 worklist.insert(insn.word(1)); // Image -- different operand order to above
1250 break;
1251 case spv::OpFunctionCall:
1252 for (uint32_t i = 3; i < insn.len(); i++) {
1253 worklist.insert(insn.word(i)); // fn itself, and all args
1254 }
1255 break;
1256
1257 case spv::OpExtInst:
1258 for (uint32_t i = 5; i < insn.len(); i++) {
1259 worklist.insert(insn.word(i)); // Operands to ext inst
1260 }
1261 break;
1262 }
1263 }
1264 break;
1265 }
1266 }
1267
1268 return ids;
1269}
1270
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001271bool CoreChecks::ValidatePushConstantBlockAgainstPipeline(std::vector<VkPushConstantRange> const *push_constant_ranges,
1272 SHADER_MODULE_STATE const *src, spirv_inst_iter type,
1273 VkShaderStageFlagBits stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07001274 bool skip = false;
1275
1276 // Strip off ptrs etc
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001277 type = GetStructType(src, type, false);
Chris Forbes47567b72017-06-09 12:09:45 -07001278 assert(type != src->end());
1279
1280 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
1281 // TODO: arrays, matrices, weird sizes
1282 for (auto insn : *src) {
1283 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
1284 if (insn.word(3) == spv::DecorationOffset) {
1285 unsigned offset = insn.word(4);
1286 auto size = 4; // Bytes; TODO: calculate this based on the type
1287
1288 bool found_range = false;
1289 for (auto const &range : *push_constant_ranges) {
Jeremy Hayese883b362019-12-10 15:12:26 -07001290 if ((range.offset <= offset) && ((range.offset + range.size) >= (offset + size)) &&
1291 (range.stageFlags & stage)) {
Chris Forbes47567b72017-06-09 12:09:45 -07001292 found_range = true;
1293
Chris Forbes47567b72017-06-09 12:09:45 -07001294 break;
1295 }
1296 }
1297
1298 if (!found_range) {
1299 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 -06001300 kVUID_Core_Shader_PushConstantOutOfRange,
Dave Houltona9df0ce2018-02-07 10:51:23 -07001301 "Push constant range covering variable starting at offset %u not declared in layout", offset);
Chris Forbes47567b72017-06-09 12:09:45 -07001302 }
1303 }
1304 }
1305 }
1306
1307 return skip;
1308}
1309
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001310bool CoreChecks::ValidatePushConstantUsage(std::vector<VkPushConstantRange> const *push_constant_ranges,
1311 SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> accessible_ids,
1312 VkShaderStageFlagBits stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07001313 bool skip = false;
1314
1315 for (auto id : accessible_ids) {
1316 auto def_insn = src->get_def(id);
1317 if (def_insn.opcode() == spv::OpVariable && def_insn.word(3) == spv::StorageClassPushConstant) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001318 skip |= ValidatePushConstantBlockAgainstPipeline(push_constant_ranges, src, src->get_def(def_insn.word(1)), stage);
Chris Forbes47567b72017-06-09 12:09:45 -07001319 }
1320 }
1321
1322 return skip;
1323}
1324
1325// Validate that data for each specialization entry is fully contained within the buffer.
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001326bool CoreChecks::ValidateSpecializationOffsets(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -07001327 bool skip = false;
1328
1329 VkSpecializationInfo const *spec = info->pSpecializationInfo;
1330
1331 if (spec) {
1332 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -06001333 if (spec->pMapEntries[i].offset >= spec->dataSize) {
1334 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0,
1335 "VUID-VkSpecializationInfo-offset-00773",
1336 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
1337 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..",
1338 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
1339 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
1340
1341 continue;
1342 }
Chris Forbes47567b72017-06-09 12:09:45 -07001343 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001344 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 -06001345 "VUID-VkSpecializationInfo-pMapEntries-00774",
Dave Houltona9df0ce2018-02-07 10:51:23 -07001346 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001347 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..",
Dave Houltona9df0ce2018-02-07 10:51:23 -07001348 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001349 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -07001350 }
1351 }
1352 }
1353
1354 return skip;
1355}
1356
Jeff Bolz38b3ce72018-09-19 12:53:38 -05001357// TODO (jbolz): Can this return a const reference?
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001358static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count) {
Chris Forbes47567b72017-06-09 12:09:45 -07001359 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -08001360 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -07001361 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -05001362 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001363
1364 // 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 -05001365 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
1366 if (type.opcode() == spv::OpTypeRuntimeArray) {
1367 descriptor_count = 0;
1368 type = module->get_def(type.word(2));
1369 } else if (type.opcode() == spv::OpTypeArray) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001370 descriptor_count *= GetConstantValue(module, type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -07001371 type = module->get_def(type.word(2));
1372 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -08001373 if (type.word(2) == spv::StorageClassStorageBuffer) {
1374 is_storage_buffer = true;
1375 }
Chris Forbes47567b72017-06-09 12:09:45 -07001376 type = module->get_def(type.word(3));
1377 }
1378 }
1379
1380 switch (type.opcode()) {
1381 case spv::OpTypeStruct: {
1382 for (auto insn : *module) {
1383 if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) {
1384 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -08001385 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001386 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1387 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
1388 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -08001389 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001390 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
1391 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
1392 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
1393 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -08001394 }
Chris Forbes47567b72017-06-09 12:09:45 -07001395 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001396 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1397 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
1398 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001399 }
1400 }
1401 }
1402
1403 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -05001404 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001405 }
1406
1407 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -05001408 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
1409 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1410 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001411
Chris Forbes73c00bf2018-06-22 16:28:06 -07001412 case spv::OpTypeSampledImage: {
1413 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
1414 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
1415 auto image_type = module->get_def(type.word(2));
1416 auto dim = image_type.word(3);
1417 auto sampled = image_type.word(7);
1418 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001419 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
1420 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001421 }
Chris Forbes73c00bf2018-06-22 16:28:06 -07001422 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001423 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1424 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001425
1426 case spv::OpTypeImage: {
1427 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
1428 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
1429 auto dim = type.word(3);
1430 auto sampled = type.word(7);
1431
1432 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001433 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
1434 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001435 } else if (dim == spv::DimBuffer) {
1436 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001437 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
1438 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001439 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001440 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
1441 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001442 }
1443 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001444 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
1445 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1446 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001447 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001448 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
1449 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001450 }
1451 }
Shannon McPherson0fa28232018-11-01 11:59:02 -06001452 case spv::OpTypeAccelerationStructureNV:
Eric Werness30127fd2018-10-31 21:01:03 -07001453 ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -05001454 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001455
1456 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
1457 default:
Jeff Bolze54ae892018-09-08 12:16:29 -05001458 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -07001459 }
1460}
1461
Jeff Bolze54ae892018-09-08 12:16:29 -05001462static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -07001463 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -05001464 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
1465 if (ss.tellp()) ss << ", ";
1466 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -07001467 }
1468 return ss.str();
1469}
1470
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001471bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure) const {
Jeff Bolzee743412019-06-20 22:24:32 -05001472 if (!check) {
1473 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
1474 kVUID_Core_Shader_ExceedDeviceLimit, "Shader requires flag %s set in %s but it is not set on the device", flag,
1475 structure)) {
1476 return true;
1477 }
1478 }
1479
1480 return false;
1481}
1482
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001483bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name) const {
Chris Forbes47567b72017-06-09 12:09:45 -07001484 if (!feature) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001485 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 -06001486 kVUID_Core_Shader_FeatureNotEnabled, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -07001487 return true;
1488 }
1489 }
1490
1491 return false;
1492}
1493
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001494bool CoreChecks::RequireExtension(bool extension, char const *extension_name) const {
Chris Forbes47567b72017-06-09 12:09:45 -07001495 if (!extension) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001496 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 -06001497 kVUID_Core_Shader_FeatureNotEnabled, "Shader requires extension %s but is not enabled on the device",
Chris Forbes47567b72017-06-09 12:09:45 -07001498 extension_name)) {
1499 return true;
1500 }
1501 }
1502
1503 return false;
1504}
1505
John Zulaufac4c6e12019-07-01 16:05:58 -06001506bool CoreChecks::ValidateShaderCapabilities(SHADER_MODULE_STATE const *src, VkShaderStageFlagBits stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07001507 bool skip = false;
1508
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001509 struct FeaturePointer {
1510 // Callable object to test if this feature is enabled in the given aggregate feature struct
1511 const std::function<VkBool32(const DeviceFeatures &)> IsEnabled;
1512
1513 // Test if feature pointer is populated
1514 explicit operator bool() const { return static_cast<bool>(IsEnabled); }
1515
1516 // Default and nullptr constructor to create an empty FeaturePointer
1517 FeaturePointer() : IsEnabled(nullptr) {}
1518 FeaturePointer(std::nullptr_t ptr) : IsEnabled(nullptr) {}
1519
1520 // Constructors to populate FeaturePointer based on given pointer to member
1521 FeaturePointer(VkBool32 VkPhysicalDeviceFeatures::*ptr)
1522 : IsEnabled([=](const DeviceFeatures &features) { return features.core.*ptr; }) {}
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001523 FeaturePointer(VkBool32 VkPhysicalDeviceVulkan11Features::*ptr)
1524 : IsEnabled([=](const DeviceFeatures &features) { return features.core11.*ptr; }) {}
1525 FeaturePointer(VkBool32 VkPhysicalDeviceVulkan12Features::*ptr)
1526 : IsEnabled([=](const DeviceFeatures &features) { return features.core12.*ptr; }) {}
Brett Lawsonbebfb6f2018-10-23 16:58:50 -07001527 FeaturePointer(VkBool32 VkPhysicalDeviceTransformFeedbackFeaturesEXT::*ptr)
1528 : IsEnabled([=](const DeviceFeatures &features) { return features.transform_feedback_features.*ptr; }) {}
Jeff Bolze4356752019-03-07 11:23:46 -06001529 FeaturePointer(VkBool32 VkPhysicalDeviceCooperativeMatrixFeaturesNV::*ptr)
1530 : IsEnabled([=](const DeviceFeatures &features) { return features.cooperative_matrix_features.*ptr; }) {}
Jason Macnakc5a621d2019-06-10 12:42:50 -07001531 FeaturePointer(VkBool32 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::*ptr)
1532 : IsEnabled([=](const DeviceFeatures &features) { return features.compute_shader_derivatives_features.*ptr; }) {}
Jason Macnak325e8b52019-06-10 13:33:10 -07001533 FeaturePointer(VkBool32 VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::*ptr)
1534 : IsEnabled([=](const DeviceFeatures &features) { return features.fragment_shader_barycentric_features.*ptr; }) {}
Jason Macnakd7fddf82019-06-13 09:52:49 -07001535 FeaturePointer(VkBool32 VkPhysicalDeviceShaderImageFootprintFeaturesNV::*ptr)
1536 : IsEnabled([=](const DeviceFeatures &features) { return features.shader_image_footprint_features.*ptr; }) {}
Jeff Bolz38f6cb52019-06-30 16:26:44 -05001537 FeaturePointer(VkBool32 VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::*ptr)
1538 : IsEnabled([=](const DeviceFeatures &features) { return features.fragment_shader_interlock_features.*ptr; }) {}
Jeff Bolza38fd3b2019-07-21 11:42:11 -05001539 FeaturePointer(VkBool32 VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::*ptr)
1540 : IsEnabled([=](const DeviceFeatures &features) { return features.demote_to_helper_invocation_features.*ptr; }) {}
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001541 };
1542
Chris Forbes47567b72017-06-09 12:09:45 -07001543 struct CapabilityInfo {
1544 char const *name;
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001545 FeaturePointer feature;
Tony-LunarG2ec96bb2019-11-26 13:43:02 -07001546 ExtEnabled DeviceExtensions::*extension;
Chris Forbes47567b72017-06-09 12:09:45 -07001547 };
1548
Chris Forbes47567b72017-06-09 12:09:45 -07001549 // clang-format off
Dave Houltoneb10ea82017-12-22 12:21:50 -07001550 static const std::unordered_multimap<uint32_t, CapabilityInfo> capabilities = {
Chris Forbes47567b72017-06-09 12:09:45 -07001551 // Capabilities always supported by a Vulkan 1.0 implementation -- no
1552 // feature bits.
1553 {spv::CapabilityMatrix, {nullptr}},
1554 {spv::CapabilityShader, {nullptr}},
1555 {spv::CapabilityInputAttachment, {nullptr}},
1556 {spv::CapabilitySampled1D, {nullptr}},
1557 {spv::CapabilityImage1D, {nullptr}},
1558 {spv::CapabilitySampledBuffer, {nullptr}},
Toni Merilehtib13a4a22019-05-21 12:58:44 +03001559 {spv::CapabilityStorageImageExtendedFormats, {nullptr}},
Chris Forbes47567b72017-06-09 12:09:45 -07001560 {spv::CapabilityImageQuery, {nullptr}},
1561 {spv::CapabilityDerivativeControl, {nullptr}},
1562
1563 // Capabilities that are optionally supported, but require a feature to
1564 // be enabled on the device
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001565 {spv::CapabilityGeometry, {"VkPhysicalDeviceFeatures::geometryShader", &VkPhysicalDeviceFeatures::geometryShader}},
1566 {spv::CapabilityTessellation, {"VkPhysicalDeviceFeatures::tessellationShader", &VkPhysicalDeviceFeatures::tessellationShader}},
1567 {spv::CapabilityFloat64, {"VkPhysicalDeviceFeatures::shaderFloat64", &VkPhysicalDeviceFeatures::shaderFloat64}},
1568 {spv::CapabilityInt64, {"VkPhysicalDeviceFeatures::shaderInt64", &VkPhysicalDeviceFeatures::shaderInt64}},
1569 {spv::CapabilityTessellationPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}},
1570 {spv::CapabilityGeometryPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}},
1571 {spv::CapabilityImageGatherExtended, {"VkPhysicalDeviceFeatures::shaderImageGatherExtended", &VkPhysicalDeviceFeatures::shaderImageGatherExtended}},
1572 {spv::CapabilityStorageImageMultisample, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}},
1573 {spv::CapabilityUniformBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing}},
1574 {spv::CapabilitySampledImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing}},
1575 {spv::CapabilityStorageBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}},
1576 {spv::CapabilityStorageImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}},
1577 {spv::CapabilityClipDistance, {"VkPhysicalDeviceFeatures::shaderClipDistance", &VkPhysicalDeviceFeatures::shaderClipDistance}},
1578 {spv::CapabilityCullDistance, {"VkPhysicalDeviceFeatures::shaderCullDistance", &VkPhysicalDeviceFeatures::shaderCullDistance}},
1579 {spv::CapabilityImageCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}},
1580 {spv::CapabilitySampleRateShading, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}},
1581 {spv::CapabilitySparseResidency, {"VkPhysicalDeviceFeatures::shaderResourceResidency", &VkPhysicalDeviceFeatures::shaderResourceResidency}},
1582 {spv::CapabilityMinLod, {"VkPhysicalDeviceFeatures::shaderResourceMinLod", &VkPhysicalDeviceFeatures::shaderResourceMinLod}},
1583 {spv::CapabilitySampledCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}},
1584 {spv::CapabilityImageMSArray, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}},
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001585 {spv::CapabilityInterpolationFunction, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}},
1586 {spv::CapabilityStorageImageReadWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat}},
1587 {spv::CapabilityStorageImageWriteWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat}},
1588 {spv::CapabilityMultiViewport, {"VkPhysicalDeviceFeatures::multiViewport", &VkPhysicalDeviceFeatures::multiViewport}},
Jeff Bolzfdf96072018-04-10 14:32:18 -05001589
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001590 {spv::CapabilityShaderNonUniformEXT, {VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_descriptor_indexing}},
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001591 {spv::CapabilityRuntimeDescriptorArrayEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::runtimeDescriptorArray", &VkPhysicalDeviceVulkan12Features::runtimeDescriptorArray}},
1592 {spv::CapabilityInputAttachmentArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderInputAttachmentArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderInputAttachmentArrayDynamicIndexing}},
1593 {spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformTexelBufferArrayDynamicIndexing}},
1594 {spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageTexelBufferArrayDynamicIndexing}},
1595 {spv::CapabilityUniformBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformBufferArrayNonUniformIndexing}},
1596 {spv::CapabilitySampledImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderSampledImageArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderSampledImageArrayNonUniformIndexing}},
1597 {spv::CapabilityStorageBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageBufferArrayNonUniformIndexing}},
1598 {spv::CapabilityStorageImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageImageArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageImageArrayNonUniformIndexing}},
1599 {spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderInputAttachmentArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderInputAttachmentArrayNonUniformIndexing}},
1600 {spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformTexelBufferArrayNonUniformIndexing}},
1601 {spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageTexelBufferArrayNonUniformIndexing}},
Chris Forbes47567b72017-06-09 12:09:45 -07001602
1603 // Capabilities that require an extension
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001604 {spv::CapabilityDrawParameters, {VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_draw_parameters}},
1605 {spv::CapabilityGeometryShaderPassthroughNV, {VK_NV_GEOMETRY_SHADER_PASSTHROUGH_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_geometry_shader_passthrough}},
1606 {spv::CapabilitySampleMaskOverrideCoverageNV, {VK_NV_SAMPLE_MASK_OVERRIDE_COVERAGE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_sample_mask_override_coverage}},
1607 {spv::CapabilityShaderViewportIndexLayerEXT, {VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_viewport_index_layer}},
1608 {spv::CapabilityShaderViewportIndexLayerNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}},
1609 {spv::CapabilityShaderViewportMaskNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}},
1610 {spv::CapabilitySubgroupBallotKHR, {VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_ballot }},
1611 {spv::CapabilitySubgroupVoteKHR, {VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_vote }},
Jason Macnakb7d091c2019-06-10 11:13:11 -07001612 {spv::CapabilityGroupNonUniformPartitionedNV, {VK_NV_SHADER_SUBGROUP_PARTITIONED_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_shader_subgroup_partitioned}},
aqnuep7033c702018-09-11 18:03:29 +02001613 {spv::CapabilityInt64Atomics, {VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_atomic_int64 }},
amhaganfa0b34d2019-10-15 16:03:53 -04001614 {spv::CapabilityShaderClockKHR, {VK_KHR_SHADER_CLOCK_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_clock }},
Alexander Galazin3bd8e342018-06-14 15:49:07 +02001615
Jason Macnakc5a621d2019-06-10 12:42:50 -07001616 {spv::CapabilityComputeDerivativeGroupQuadsNV, {"VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupQuads", &VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupQuads, &DeviceExtensions::vk_nv_compute_shader_derivatives}},
1617 {spv::CapabilityComputeDerivativeGroupLinearNV, {"VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupLinear", &VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupLinear, &DeviceExtensions::vk_nv_compute_shader_derivatives}},
Jason Macnakf7019582019-06-13 10:07:26 -07001618 {spv::CapabilityFragmentBarycentricNV, {"VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::fragmentShaderBarycentric", &VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::fragmentShaderBarycentric, &DeviceExtensions::vk_nv_fragment_shader_barycentric}},
Jason Macnakc5a621d2019-06-10 12:42:50 -07001619
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001620 {spv::CapabilityStorageBuffer8BitAccess, {"VkPhysicalDevice8BitStorageFeaturesKHR::storageBuffer8BitAccess", &VkPhysicalDeviceVulkan12Features::storageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}},
1621 {spv::CapabilityUniformAndStorageBuffer8BitAccess, {"VkPhysicalDevice8BitStorageFeaturesKHR::uniformAndStorageBuffer8BitAccess", &VkPhysicalDeviceVulkan12Features::uniformAndStorageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}},
1622 {spv::CapabilityStoragePushConstant8, {"VkPhysicalDevice8BitStorageFeaturesKHR::storagePushConstant8", &VkPhysicalDeviceVulkan12Features::storagePushConstant8, &DeviceExtensions::vk_khr_8bit_storage}},
Brett Lawsonbebfb6f2018-10-23 16:58:50 -07001623
Jason Macnakf7019582019-06-13 10:07:26 -07001624 {spv::CapabilityTransformFeedback, { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback, &DeviceExtensions::vk_ext_transform_feedback}},
1625 {spv::CapabilityGeometryStreams, { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams, &DeviceExtensions::vk_ext_transform_feedback}},
Jose-Emilio Munoz-Lopez1109b452018-08-21 09:44:07 +01001626
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001627 {spv::CapabilityFloat16, {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderFloat16", &VkPhysicalDeviceVulkan12Features::shaderFloat16, &DeviceExtensions::vk_khr_shader_float16_int8}},
1628 {spv::CapabilityInt8, {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderInt8", &VkPhysicalDeviceVulkan12Features::shaderInt8, &DeviceExtensions::vk_khr_shader_float16_int8}},
Jeff Bolze4356752019-03-07 11:23:46 -06001629
Jason Macnakd7fddf82019-06-13 09:52:49 -07001630 {spv::CapabilityImageFootprintNV, {"VkPhysicalDeviceShaderImageFootprintFeaturesNV::imageFootprint", &VkPhysicalDeviceShaderImageFootprintFeaturesNV::imageFootprint, &DeviceExtensions::vk_nv_shader_image_footprint}},
1631
Jeff Bolze4356752019-03-07 11:23:46 -06001632 {spv::CapabilityCooperativeMatrixNV, {"VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix", &VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix, &DeviceExtensions::vk_nv_cooperative_matrix}},
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001633
Graeme Leese41e6b842019-08-02 10:49:14 +01001634 {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserve", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
1635 {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserve", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
1636 {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZero", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
1637 {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTE", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
1638 {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZ", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
Jeff Bolz38f6cb52019-06-30 16:26:44 -05001639
1640 {spv::CapabilityFragmentShaderSampleInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderSampleInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderSampleInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
1641 {spv::CapabilityFragmentShaderPixelInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderPixelInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderPixelInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
1642 {spv::CapabilityFragmentShaderShadingRateInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderShadingRateInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderShadingRateInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
Jeff Bolza38fd3b2019-07-21 11:42:11 -05001643 {spv::CapabilityDemoteToHelperInvocationEXT, {"VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::shaderDemoteToHelperInvocation", &VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::shaderDemoteToHelperInvocation, &DeviceExtensions::vk_ext_shader_demote_to_helper_invocation}},
Jeff Bolz4563f2a2019-12-10 13:30:30 -06001644
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001645 {spv::CapabilityPhysicalStorageBufferAddresses, {"VkPhysicalDeviceBufferDeviceAddressFeatures::bufferDeviceAddress", &VkPhysicalDeviceVulkan12Features::bufferDeviceAddress, &DeviceExtensions::vk_ext_buffer_device_address}},
Jeff Bolz4563f2a2019-12-10 13:30:30 -06001646 // Should be non-EXT token, but Android SPIRV-Headers are out of date, and the token value is the same anyway
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001647 {spv::CapabilityPhysicalStorageBufferAddressesEXT, {"VkPhysicalDeviceBufferDeviceAddressFeaturesEXT::bufferDeviceAddress", &VkPhysicalDeviceVulkan12Features::bufferDeviceAddress, &DeviceExtensions::vk_khr_buffer_device_address}},
Chris Forbes47567b72017-06-09 12:09:45 -07001648 };
1649 // clang-format on
1650
1651 for (auto insn : *src) {
1652 if (insn.opcode() == spv::OpCapability) {
Dave Houltoneb10ea82017-12-22 12:21:50 -07001653 size_t n = capabilities.count(insn.word(1));
1654 if (1 == n) { // key occurs exactly once
1655 auto it = capabilities.find(insn.word(1));
1656 if (it != capabilities.end()) {
1657 if (it->second.feature) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001658 skip |= RequireFeature(it->second.feature.IsEnabled(enabled_features), it->second.name);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001659 }
1660 if (it->second.extension) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001661 skip |= RequireExtension(IsExtEnabled((device_extensions.*(it->second.extension))), it->second.name);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001662 }
Chris Forbes47567b72017-06-09 12:09:45 -07001663 }
Dave Houltoneb10ea82017-12-22 12:21:50 -07001664 } else if (1 < n) { // key occurs multiple times, at least one must be enabled
1665 bool needs_feature = false, has_feature = false;
1666 bool needs_ext = false, has_ext = false;
1667 std::string feature_names = "(one of) [ ";
1668 std::string extension_names = feature_names;
1669 auto caps = capabilities.equal_range(insn.word(1));
1670 for (auto it = caps.first; it != caps.second; ++it) {
1671 if (it->second.feature) {
1672 needs_feature = true;
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001673 has_feature = has_feature || it->second.feature.IsEnabled(enabled_features);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001674 feature_names += it->second.name;
1675 feature_names += " ";
1676 }
1677 if (it->second.extension) {
1678 needs_ext = true;
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06001679 has_ext = has_ext || device_extensions.*(it->second.extension);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001680 extension_names += it->second.name;
1681 extension_names += " ";
1682 }
1683 }
1684 if (needs_feature) {
1685 feature_names += "]";
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001686 skip |= RequireFeature(has_feature, feature_names.c_str());
Dave Houltoneb10ea82017-12-22 12:21:50 -07001687 }
1688 if (needs_ext) {
1689 extension_names += "]";
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001690 skip |= RequireExtension(has_ext, extension_names.c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001691 }
Graeme Leesec82dbe02019-08-02 10:44:21 +01001692 }
1693
1694 { // Do group non-uniform checks
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001695 const VkSubgroupFeatureFlags supportedOperations = phys_dev_props_core11.subgroupSupportedOperations;
1696 const VkSubgroupFeatureFlags supportedStages = phys_dev_props_core11.subgroupSupportedStages;
Jeff Bolzee743412019-06-20 22:24:32 -05001697
1698 switch (insn.word(1)) {
1699 default:
1700 break;
1701 case spv::CapabilityGroupNonUniform:
1702 case spv::CapabilityGroupNonUniformVote:
1703 case spv::CapabilityGroupNonUniformArithmetic:
1704 case spv::CapabilityGroupNonUniformBallot:
1705 case spv::CapabilityGroupNonUniformShuffle:
1706 case spv::CapabilityGroupNonUniformShuffleRelative:
1707 case spv::CapabilityGroupNonUniformClustered:
1708 case spv::CapabilityGroupNonUniformQuad:
1709 case spv::CapabilityGroupNonUniformPartitionedNV:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001710 RequirePropertyFlag(supportedStages & stage, string_VkShaderStageFlagBits(stage),
Jeff Bolzee743412019-06-20 22:24:32 -05001711 "VkPhysicalDeviceSubgroupProperties::supportedStages");
1712 break;
1713 }
1714
1715 switch (insn.word(1)) {
1716 default:
1717 break;
1718 case spv::CapabilityGroupNonUniform:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001719 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT, "VK_SUBGROUP_FEATURE_BASIC_BIT",
Jeff Bolzee743412019-06-20 22:24:32 -05001720 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1721 break;
1722 case spv::CapabilityGroupNonUniformVote:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001723 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT, "VK_SUBGROUP_FEATURE_VOTE_BIT",
Jeff Bolzee743412019-06-20 22:24:32 -05001724 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1725 break;
1726 case spv::CapabilityGroupNonUniformArithmetic:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001727 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT,
Jeff Bolzee743412019-06-20 22:24:32 -05001728 "VK_SUBGROUP_FEATURE_ARITHMETIC_BIT",
1729 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1730 break;
1731 case spv::CapabilityGroupNonUniformBallot:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001732 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT, "VK_SUBGROUP_FEATURE_BALLOT_BIT",
Jeff Bolzee743412019-06-20 22:24:32 -05001733 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1734 break;
1735 case spv::CapabilityGroupNonUniformShuffle:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001736 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_BIT,
Jeff Bolzee743412019-06-20 22:24:32 -05001737 "VK_SUBGROUP_FEATURE_SHUFFLE_BIT",
1738 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1739 break;
1740 case spv::CapabilityGroupNonUniformShuffleRelative:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001741 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT,
Jeff Bolzee743412019-06-20 22:24:32 -05001742 "VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT",
1743 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1744 break;
1745 case spv::CapabilityGroupNonUniformClustered:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001746 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_CLUSTERED_BIT,
Jeff Bolzee743412019-06-20 22:24:32 -05001747 "VK_SUBGROUP_FEATURE_CLUSTERED_BIT",
1748 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1749 break;
1750 case spv::CapabilityGroupNonUniformQuad:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001751 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_QUAD_BIT, "VK_SUBGROUP_FEATURE_QUAD_BIT",
Jeff Bolzee743412019-06-20 22:24:32 -05001752 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1753 break;
1754 case spv::CapabilityGroupNonUniformPartitionedNV:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001755 RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV,
Jeff Bolzee743412019-06-20 22:24:32 -05001756 "VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV",
1757 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1758 break;
1759 }
Chris Forbes47567b72017-06-09 12:09:45 -07001760 }
1761 }
1762 }
1763
Jeff Bolzee743412019-06-20 22:24:32 -05001764 return skip;
1765}
1766
John Zulaufac4c6e12019-07-01 16:05:58 -06001767bool CoreChecks::ValidateShaderStageWritableDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -05001768 bool skip = false;
1769
Chris Forbes349b3132018-03-07 11:38:08 -08001770 if (has_writable_descriptor) {
1771 switch (stage) {
1772 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -06001773 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1774 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1775 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1776 case VK_SHADER_STAGE_MISS_BIT_NV:
1777 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1778 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1779 case VK_SHADER_STAGE_TASK_BIT_NV:
1780 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -08001781 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -06001782 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -08001783 break;
1784 case VK_SHADER_STAGE_FRAGMENT_BIT:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001785 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics");
Chris Forbes349b3132018-03-07 11:38:08 -08001786 break;
1787 default:
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001788 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics");
Chris Forbes349b3132018-03-07 11:38:08 -08001789 break;
1790 }
1791 }
1792
Chris Forbes47567b72017-06-09 12:09:45 -07001793 return skip;
1794}
1795
Jeff Bolz526f2d52019-09-18 13:18:08 -05001796bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage) const {
Jeff Bolzee743412019-06-20 22:24:32 -05001797 bool skip = false;
1798
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001799 auto const subgroup_props = phys_dev_props_core11;
Jeff Bolzee743412019-06-20 22:24:32 -05001800
Jeff Bolz526f2d52019-09-18 13:18:08 -05001801 for (auto inst : *module) {
Jeff Bolzee743412019-06-20 22:24:32 -05001802 // Check the quad operations.
1803 switch (inst.opcode()) {
1804 default:
1805 break;
1806 case spv::OpGroupNonUniformQuadBroadcast:
1807 case spv::OpGroupNonUniformQuadSwap:
1808 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001809 skip |= RequireFeature(subgroup_props.subgroupQuadOperationsInAllStages,
Jeff Bolzee743412019-06-20 22:24:32 -05001810 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages");
1811 }
1812 break;
1813 }
Jeff Bolz526f2d52019-09-18 13:18:08 -05001814
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001815 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
Jeff Bolz526f2d52019-09-18 13:18:08 -05001816 switch (inst.opcode()) {
1817 default:
1818 break;
1819 case spv::OpGroupNonUniformAllEqual:
1820 case spv::OpGroupNonUniformBroadcast:
1821 case spv::OpGroupNonUniformBroadcastFirst:
1822 case spv::OpGroupNonUniformShuffle:
1823 case spv::OpGroupNonUniformShuffleXor:
1824 case spv::OpGroupNonUniformShuffleUp:
1825 case spv::OpGroupNonUniformShuffleDown:
1826 case spv::OpGroupNonUniformIAdd:
1827 case spv::OpGroupNonUniformFAdd:
1828 case spv::OpGroupNonUniformIMul:
1829 case spv::OpGroupNonUniformFMul:
1830 case spv::OpGroupNonUniformSMin:
1831 case spv::OpGroupNonUniformUMin:
1832 case spv::OpGroupNonUniformFMin:
1833 case spv::OpGroupNonUniformSMax:
1834 case spv::OpGroupNonUniformUMax:
1835 case spv::OpGroupNonUniformFMax:
1836 case spv::OpGroupNonUniformBitwiseAnd:
1837 case spv::OpGroupNonUniformBitwiseOr:
1838 case spv::OpGroupNonUniformBitwiseXor:
1839 case spv::OpGroupNonUniformLogicalAnd:
1840 case spv::OpGroupNonUniformLogicalOr:
1841 case spv::OpGroupNonUniformLogicalXor:
1842 case spv::OpGroupNonUniformQuadBroadcast:
1843 case spv::OpGroupNonUniformQuadSwap: {
1844 auto type = module->get_def(inst.word(1));
1845
1846 if (type.opcode() == spv::OpTypeVector) {
1847 // Get the element type
1848 type = module->get_def(type.word(2));
1849 }
1850
1851 if (type.opcode() == spv::OpTypeBool) {
1852 break;
1853 }
1854
1855 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
1856 const uint32_t width = type.word(2);
1857
1858 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
1859 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001860 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
Tony-LunarGa74d3fe2019-11-22 15:43:20 -07001861 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes");
Jeff Bolz526f2d52019-09-18 13:18:08 -05001862 }
1863 break;
1864 }
1865 }
1866 }
Jeff Bolzee743412019-06-20 22:24:32 -05001867 }
1868
1869 return skip;
1870}
1871
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001872bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001873 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001874 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
1875 pStage->stage == VK_SHADER_STAGE_ALL) {
1876 return false;
1877 }
1878
1879 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07001880 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001881
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001882 std::set<uint32_t> patchIDs;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001883 struct Variable {
1884 uint32_t baseTypePtrID;
1885 uint32_t ID;
1886 uint32_t storageClass;
1887 };
1888 std::vector<Variable> variables;
1889
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001890 uint32_t numVertices = 0;
1891
Jeff Bolzf234bf82019-11-04 14:07:15 -06001892 auto entrypointVariables = FindEntrypointInterfaces(entrypoint);
1893
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001894 for (auto insn : *src) {
1895 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001896 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001897 case spv::OpDecorate:
1898 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001899 case spv::DecorationPatch: {
1900 patchIDs.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001901 break;
1902 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001903 default:
1904 break;
1905 }
1906 break;
1907 // Find all input and output variables
1908 case spv::OpVariable: {
1909 Variable var = {};
1910 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001911 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
1912 // Only include variables in the entrypoint's interface
1913 find(entrypointVariables.begin(), entrypointVariables.end(), insn.word(2)) != entrypointVariables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001914 var.baseTypePtrID = insn.word(1);
1915 var.ID = insn.word(2);
1916 variables.push_back(var);
1917 }
1918 break;
1919 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001920 case spv::OpExecutionMode:
1921 if (insn.word(1) == entrypoint.word(2)) {
1922 switch (insn.word(2)) {
1923 default:
1924 break;
1925 case spv::ExecutionModeOutputVertices:
1926 numVertices = insn.word(3);
1927 break;
1928 }
1929 }
1930 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001931 default:
1932 break;
1933 }
1934 }
1935
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001936 bool strip_output_array_level =
1937 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
1938 bool strip_input_array_level =
1939 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
1940 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
1941
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001942 uint32_t numCompIn = 0, numCompOut = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -06001943 int maxCompIn = 0, maxCompOut = 0;
1944
1945 auto inputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassInput, strip_input_array_level);
1946 auto outputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassOutput, strip_output_array_level);
1947
1948 // Find max component location used for input variables.
1949 for (auto &var : inputs) {
1950 int location = var.first.first;
1951 int component = var.first.second;
1952 interface_var &iv = var.second;
1953
1954 // Only need to look at the first location, since we use the type's whole size
1955 if (iv.offset != 0) {
1956 continue;
1957 }
1958
1959 if (iv.is_patch) {
1960 continue;
1961 }
1962
1963 int numComponents = GetComponentsConsumedByType(src, iv.type_id, strip_input_array_level);
1964 maxCompIn = std::max(maxCompIn, location * 4 + component + numComponents);
1965 }
1966
1967 // Find max component location used for output variables.
1968 for (auto &var : outputs) {
1969 int location = var.first.first;
1970 int component = var.first.second;
1971 interface_var &iv = var.second;
1972
1973 // Only need to look at the first location, since we use the type's whole size
1974 if (iv.offset != 0) {
1975 continue;
1976 }
1977
1978 if (iv.is_patch) {
1979 continue;
1980 }
1981
1982 int numComponents = GetComponentsConsumedByType(src, iv.type_id, strip_output_array_level);
1983 maxCompOut = std::max(maxCompOut, location * 4 + component + numComponents);
1984 }
1985
1986 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
1987 // but that doesn't include builtins.
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001988 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001989 // Check if the variable is a patch. Patches can also be members of blocks,
1990 // but if they are then the top-level arrayness has already been stripped
1991 // by the time GetComponentsConsumedByType gets to it.
1992 bool isPatch = patchIDs.find(var.ID) != patchIDs.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001993
1994 if (var.storageClass == spv::StorageClassInput) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001995 numCompIn += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_input_array_level && !isPatch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001996 } else { // var.storageClass == spv::StorageClassOutput
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001997 numCompOut += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_output_array_level && !isPatch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001998 }
1999 }
2000
2001 switch (pStage->stage) {
2002 case VK_SHADER_STAGE_VERTEX_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002003 if (numCompOut > limits.maxVertexOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002004 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2005 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2006 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
2007 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
2008 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002009 limits.maxVertexOutputComponents, numCompOut - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002010 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002011 if (maxCompOut > (int)limits.maxVertexOutputComponents) {
2012 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2013 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2014 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
2015 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
2016 limits.maxVertexOutputComponents);
2017 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002018 break;
2019
2020 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002021 if (numCompIn > limits.maxTessellationControlPerVertexInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002022 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2023 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2024 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
2025 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
2026 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002027 limits.maxTessellationControlPerVertexInputComponents,
2028 numCompIn - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002029 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002030 if (maxCompIn > (int)limits.maxTessellationControlPerVertexInputComponents) {
2031 skip |=
2032 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2033 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2034 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
2035 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
2036 limits.maxTessellationControlPerVertexInputComponents);
2037 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002038 if (numCompOut > limits.maxTessellationControlPerVertexOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002039 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2040 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2041 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
2042 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
2043 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002044 limits.maxTessellationControlPerVertexOutputComponents,
2045 numCompOut - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002046 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002047 if (maxCompOut > (int)limits.maxTessellationControlPerVertexOutputComponents) {
2048 skip |=
2049 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2050 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2051 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
2052 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
2053 limits.maxTessellationControlPerVertexOutputComponents);
2054 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002055 break;
2056
2057 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002058 if (numCompIn > limits.maxTessellationEvaluationInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002059 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2060 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2061 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
2062 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
2063 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002064 limits.maxTessellationEvaluationInputComponents,
2065 numCompIn - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002066 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002067 if (maxCompIn > (int)limits.maxTessellationEvaluationInputComponents) {
2068 skip |=
2069 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2070 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2071 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
2072 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
2073 limits.maxTessellationEvaluationInputComponents);
2074 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002075 if (numCompOut > limits.maxTessellationEvaluationOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002076 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2077 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2078 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
2079 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
2080 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002081 limits.maxTessellationEvaluationOutputComponents,
2082 numCompOut - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002083 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002084 if (maxCompOut > (int)limits.maxTessellationEvaluationOutputComponents) {
2085 skip |=
2086 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2087 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2088 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
2089 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
2090 limits.maxTessellationEvaluationOutputComponents);
2091 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002092 break;
2093
2094 case VK_SHADER_STAGE_GEOMETRY_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002095 if (numCompIn > limits.maxGeometryInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002096 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2097 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2098 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
2099 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
2100 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002101 limits.maxGeometryInputComponents, numCompIn - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002102 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002103 if (maxCompIn > (int)limits.maxGeometryInputComponents) {
2104 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2105 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2106 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
2107 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
2108 limits.maxGeometryInputComponents);
2109 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002110 if (numCompOut > limits.maxGeometryOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002111 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2112 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2113 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
2114 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
2115 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002116 limits.maxGeometryOutputComponents, numCompOut - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002117 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002118 if (maxCompOut > (int)limits.maxGeometryOutputComponents) {
2119 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2120 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2121 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
2122 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
2123 limits.maxGeometryOutputComponents);
2124 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002125 if (numCompOut * numVertices > limits.maxGeometryTotalOutputComponents) {
2126 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2127 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2128 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
2129 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
2130 "components by %u components",
2131 limits.maxGeometryTotalOutputComponents,
2132 numCompOut * numVertices - limits.maxGeometryTotalOutputComponents);
2133 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002134 break;
2135
2136 case VK_SHADER_STAGE_FRAGMENT_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002137 if (numCompIn > limits.maxFragmentInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002138 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2139 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2140 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
2141 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
2142 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002143 limits.maxFragmentInputComponents, numCompIn - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002144 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002145 if (maxCompIn > (int)limits.maxFragmentInputComponents) {
2146 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2147 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2148 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
2149 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
2150 limits.maxFragmentInputComponents);
2151 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002152 break;
2153
Jeff Bolz148d94e2018-12-13 21:25:56 -06002154 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
2155 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
2156 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
2157 case VK_SHADER_STAGE_MISS_BIT_NV:
2158 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
2159 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
2160 case VK_SHADER_STAGE_TASK_BIT_NV:
2161 case VK_SHADER_STAGE_MESH_BIT_NV:
2162 break;
2163
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002164 default:
2165 assert(false); // This should never happen
2166 }
2167 return skip;
2168}
2169
Jeff Bolze4356752019-03-07 11:23:46 -06002170// copy the specialization constant value into buf, if it is present
2171void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
2172 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
2173
2174 if (spec && spec_id < spec->mapEntryCount) {
2175 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
2176 }
2177}
2178
2179// Fill in value with the constant or specialization constant value, if available.
2180// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002181static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Bolze4356752019-03-07 11:23:46 -06002182 const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
2183 auto type_id = src->get_def(insn.word(1));
2184 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
2185 return false;
2186 }
2187 switch (insn.opcode()) {
2188 case spv::OpSpecConstant:
2189 *value = insn.word(3);
2190 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
2191 return true;
2192 case spv::OpConstant:
2193 *value = insn.word(3);
2194 return true;
2195 default:
2196 return false;
2197 }
2198}
2199
2200// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002201VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06002202 switch (insn.opcode()) {
2203 case spv::OpTypeInt:
2204 switch (insn.word(2)) {
2205 case 8:
2206 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
2207 case 16:
2208 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
2209 case 32:
2210 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
2211 case 64:
2212 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
2213 default:
2214 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2215 }
2216 case spv::OpTypeFloat:
2217 switch (insn.word(2)) {
2218 case 16:
2219 return VK_COMPONENT_TYPE_FLOAT16_NV;
2220 case 32:
2221 return VK_COMPONENT_TYPE_FLOAT32_NV;
2222 case 64:
2223 return VK_COMPONENT_TYPE_FLOAT64_NV;
2224 default:
2225 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2226 }
2227 default:
2228 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2229 }
2230}
2231
2232// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
2233// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002234bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06002235 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06002236 bool skip = false;
2237
2238 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
2239 std::unordered_map<uint32_t, uint32_t> id_to_spec_id;
2240 // Map SPIR-V result ID to the ID of its type.
2241 std::unordered_map<uint32_t, uint32_t> id_to_type_id;
2242
2243 struct CoopMatType {
2244 uint32_t scope, rows, cols;
2245 VkComponentTypeNV component_type;
2246 bool all_constant;
2247
2248 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
2249
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002250 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Bolze4356752019-03-07 11:23:46 -06002251 const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
2252 spirv_inst_iter insn = src->get_def(id);
2253 uint32_t component_type_id = insn.word(2);
2254 uint32_t scope_id = insn.word(3);
2255 uint32_t rows_id = insn.word(4);
2256 uint32_t cols_id = insn.word(5);
2257 auto component_type_iter = src->get_def(component_type_id);
2258 auto scope_iter = src->get_def(scope_id);
2259 auto rows_iter = src->get_def(rows_id);
2260 auto cols_iter = src->get_def(cols_id);
2261
2262 all_constant = true;
2263 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
2264 all_constant = false;
2265 }
2266 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
2267 all_constant = false;
2268 }
2269 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
2270 all_constant = false;
2271 }
2272 component_type = GetComponentType(component_type_iter, src);
2273 }
2274 };
2275
2276 bool seen_coopmat_capability = false;
2277
2278 for (auto insn : *src) {
2279 // Whitelist instructions whose result can be a cooperative matrix type, and
2280 // keep track of their types. It would be nice if SPIRV-Headers generated code
2281 // to identify which instructions have a result type and result id. Lacking that,
2282 // this whitelist is based on the set of instructions that
2283 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
2284 switch (insn.opcode()) {
2285 case spv::OpLoad:
2286 case spv::OpCooperativeMatrixLoadNV:
2287 case spv::OpCooperativeMatrixMulAddNV:
2288 case spv::OpSNegate:
2289 case spv::OpFNegate:
2290 case spv::OpIAdd:
2291 case spv::OpFAdd:
2292 case spv::OpISub:
2293 case spv::OpFSub:
2294 case spv::OpFDiv:
2295 case spv::OpSDiv:
2296 case spv::OpUDiv:
2297 case spv::OpMatrixTimesScalar:
2298 case spv::OpConstantComposite:
2299 case spv::OpCompositeConstruct:
2300 case spv::OpConvertFToU:
2301 case spv::OpConvertFToS:
2302 case spv::OpConvertSToF:
2303 case spv::OpConvertUToF:
2304 case spv::OpUConvert:
2305 case spv::OpSConvert:
2306 case spv::OpFConvert:
2307 id_to_type_id[insn.word(2)] = insn.word(1);
2308 break;
2309 default:
2310 break;
2311 }
2312
2313 switch (insn.opcode()) {
2314 case spv::OpDecorate:
2315 if (insn.word(2) == spv::DecorationSpecId) {
2316 id_to_spec_id[insn.word(1)] = insn.word(3);
2317 }
2318 break;
2319 case spv::OpCapability:
2320 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
2321 seen_coopmat_capability = true;
2322
2323 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
2324 skip |=
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002325 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Jeff Bolze4356752019-03-07 11:23:46 -06002326 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixSupportedStages,
2327 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
2328 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
2329 }
2330 }
2331 break;
2332 case spv::OpMemoryModel:
2333 // If the capability isn't enabled, don't bother with the rest of this function.
2334 // OpMemoryModel is the first required instruction after all OpCapability instructions.
2335 if (!seen_coopmat_capability) {
2336 return skip;
2337 }
2338 break;
2339 case spv::OpTypeCooperativeMatrixNV: {
2340 CoopMatType M;
2341 M.Init(insn.word(1), src, pStage, id_to_spec_id);
2342
2343 if (M.all_constant) {
2344 // Validate that the type parameters are all supported for one of the
2345 // operands of a cooperative matrix property.
2346 bool valid = false;
2347 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
2348 if (cooperative_matrix_properties[i].AType == M.component_type &&
2349 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].KSize == M.cols &&
2350 cooperative_matrix_properties[i].scope == M.scope) {
2351 valid = true;
2352 break;
2353 }
2354 if (cooperative_matrix_properties[i].BType == M.component_type &&
2355 cooperative_matrix_properties[i].KSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2356 cooperative_matrix_properties[i].scope == M.scope) {
2357 valid = true;
2358 break;
2359 }
2360 if (cooperative_matrix_properties[i].CType == M.component_type &&
2361 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2362 cooperative_matrix_properties[i].scope == M.scope) {
2363 valid = true;
2364 break;
2365 }
2366 if (cooperative_matrix_properties[i].DType == M.component_type &&
2367 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2368 cooperative_matrix_properties[i].scope == M.scope) {
2369 valid = true;
2370 break;
2371 }
2372 }
2373 if (!valid) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002374 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 -06002375 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixType,
2376 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
2377 insn.word(1));
2378 }
2379 }
2380 break;
2381 }
2382 case spv::OpCooperativeMatrixMulAddNV: {
2383 CoopMatType A, B, C, D;
2384 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
2385 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
2386 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
2387 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07002388 // Couldn't find type of matrix
2389 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06002390 break;
2391 }
2392 D.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
2393 A.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
2394 B.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
2395 C.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
2396
2397 if (A.all_constant && B.all_constant && C.all_constant && D.all_constant) {
2398 // Validate that the type parameters are all supported for the same
2399 // cooperative matrix property.
2400 bool valid = false;
2401 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
2402 if (cooperative_matrix_properties[i].AType == A.component_type &&
2403 cooperative_matrix_properties[i].MSize == A.rows && cooperative_matrix_properties[i].KSize == A.cols &&
2404 cooperative_matrix_properties[i].scope == A.scope &&
2405
2406 cooperative_matrix_properties[i].BType == B.component_type &&
2407 cooperative_matrix_properties[i].KSize == B.rows && cooperative_matrix_properties[i].NSize == B.cols &&
2408 cooperative_matrix_properties[i].scope == B.scope &&
2409
2410 cooperative_matrix_properties[i].CType == C.component_type &&
2411 cooperative_matrix_properties[i].MSize == C.rows && cooperative_matrix_properties[i].NSize == C.cols &&
2412 cooperative_matrix_properties[i].scope == C.scope &&
2413
2414 cooperative_matrix_properties[i].DType == D.component_type &&
2415 cooperative_matrix_properties[i].MSize == D.rows && cooperative_matrix_properties[i].NSize == D.cols &&
2416 cooperative_matrix_properties[i].scope == D.scope) {
2417 valid = true;
2418 break;
2419 }
2420 }
2421 if (!valid) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002422 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 -06002423 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixMulAdd,
2424 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
2425 "VkCooperativeMatrixPropertiesNV",
2426 insn.word(2));
2427 }
2428 }
2429 break;
2430 }
2431 default:
2432 break;
2433 }
2434 }
2435
2436 return skip;
2437}
2438
John Zulaufac4c6e12019-07-01 16:05:58 -06002439bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002440 auto entrypoint_id = entrypoint.word(2);
2441
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002442 // The first denorm execution mode encountered, along with its bit width.
2443 // Used to check if SeparateDenormSettings is respected.
2444 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002445
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002446 // The first rounding mode encountered, along with its bit width.
2447 // Used to check if SeparateRoundingModeSettings is respected.
2448 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002449
2450 bool skip = false;
2451
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002452 uint32_t verticesOut = 0;
2453 uint32_t invocations = 0;
2454
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002455 for (auto insn : *src) {
2456 if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) {
2457 auto mode = insn.word(2);
2458 switch (mode) {
2459 case spv::ExecutionModeSignedZeroInfNanPreserve: {
2460 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002461 if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) ||
2462 (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) ||
2463 (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002464 skip |=
2465 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2466 kVUID_Core_Shader_FeatureNotEnabled,
2467 "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device",
2468 bit_width);
2469 }
2470 break;
2471 }
2472
2473 case spv::ExecutionModeDenormPreserve: {
2474 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002475 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) ||
2476 (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) ||
2477 (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002478 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2479 kVUID_Core_Shader_FeatureNotEnabled,
2480 "Shader requires DenormPreserve for bit width %d but it is not enabled on the device",
2481 bit_width);
2482 }
2483
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002484 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
2485 // Register the first denorm execution mode found
2486 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002487 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002488 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002489 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR:
2490 if (first_rounding_mode.second != 32 && bit_width != 32) {
2491 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2492 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_FeatureNotEnabled,
2493 "Shader uses different denorm execution modes for 16 and 64-bit but "
2494 "denormBehaviorIndependence is "
2495 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device");
2496 }
2497 break;
2498
2499 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR:
2500 break;
2501
2502 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR:
2503 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2504 0, kVUID_Core_Shader_FeatureNotEnabled,
2505 "Shader uses different denorm execution modes for different bit widths but "
2506 "denormBehaviorIndependence is "
2507 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device");
2508 break;
2509
2510 default:
2511 break;
2512 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002513 }
2514 break;
2515 }
2516
2517 case spv::ExecutionModeDenormFlushToZero: {
2518 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002519 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) ||
2520 (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) ||
2521 (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002522 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2523 kVUID_Core_Shader_FeatureNotEnabled,
2524 "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device",
2525 bit_width);
2526 }
2527
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002528 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
2529 // Register the first denorm execution mode found
2530 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002531 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002532 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002533 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR:
2534 if (first_rounding_mode.second != 32 && bit_width != 32) {
2535 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2536 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_FeatureNotEnabled,
2537 "Shader uses different denorm execution modes for 16 and 64-bit but "
2538 "denormBehaviorIndependence is "
2539 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device");
2540 }
2541 break;
2542
2543 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR:
2544 break;
2545
2546 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR:
2547 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2548 0, kVUID_Core_Shader_FeatureNotEnabled,
2549 "Shader uses different denorm execution modes for different bit widths but "
2550 "denormBehaviorIndependence is "
2551 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device");
2552 break;
2553
2554 default:
2555 break;
2556 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002557 }
2558 break;
2559 }
2560
2561 case spv::ExecutionModeRoundingModeRTE: {
2562 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002563 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) ||
2564 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) ||
2565 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002566 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2567 kVUID_Core_Shader_FeatureNotEnabled,
2568 "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device",
2569 bit_width);
2570 }
2571
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002572 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2573 // Register the first rounding mode found
2574 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002575 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002576 switch (phys_dev_props_core12.roundingModeIndependence) {
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002577 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR:
2578 if (first_rounding_mode.second != 32 && bit_width != 32) {
2579 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2580 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_FeatureNotEnabled,
2581 "Shader uses different rounding modes for 16 and 64-bit but "
2582 "roundingModeIndependence is "
2583 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device");
2584 }
2585 break;
2586
2587 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR:
2588 break;
2589
2590 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR:
2591 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2592 0, kVUID_Core_Shader_FeatureNotEnabled,
2593 "Shader uses different rounding modes for different bit widths but "
2594 "roundingModeIndependence is "
2595 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device");
2596 break;
2597
2598 default:
2599 break;
2600 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002601 }
2602 break;
2603 }
2604
2605 case spv::ExecutionModeRoundingModeRTZ: {
2606 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002607 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) ||
2608 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) ||
2609 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002610 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2611 kVUID_Core_Shader_FeatureNotEnabled,
2612 "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device",
2613 bit_width);
2614 }
2615
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002616 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2617 // Register the first rounding mode found
2618 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002619 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002620 switch (phys_dev_props_core12.roundingModeIndependence) {
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002621 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR:
2622 if (first_rounding_mode.second != 32 && bit_width != 32) {
2623 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2624 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_FeatureNotEnabled,
2625 "Shader uses different rounding modes for 16 and 64-bit but "
2626 "roundingModeIndependence is "
2627 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device");
2628 }
2629 break;
2630
2631 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR:
2632 break;
2633
2634 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR:
2635 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2636 0, kVUID_Core_Shader_FeatureNotEnabled,
2637 "Shader uses different rounding modes for different bit widths but "
2638 "roundingModeIndependence is "
2639 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device");
2640 break;
2641
2642 default:
2643 break;
2644 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002645 }
2646 break;
2647 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002648
2649 case spv::ExecutionModeOutputVertices: {
2650 verticesOut = insn.word(3);
2651 break;
2652 }
2653
2654 case spv::ExecutionModeInvocations: {
2655 invocations = insn.word(3);
2656 break;
2657 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002658 }
2659 }
2660 }
2661
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002662 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
2663 if (verticesOut == 0 || verticesOut > phys_dev_props.limits.maxGeometryOutputVertices) {
2664 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2665 "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2666 "Geometry shader entry point must have an OpExecutionMode instruction that "
2667 "specifies a maximum output vertex count that is greater than 0 and less "
2668 "than or equal to maxGeometryOutputVertices. "
2669 "OutputVertices=%d, maxGeometryOutputVertices=%d",
2670 verticesOut, phys_dev_props.limits.maxGeometryOutputVertices);
2671 }
2672
2673 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
2674 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2675 "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2676 "Geometry shader entry point must have an OpExecutionMode instruction that "
2677 "specifies an invocation count that is greater than 0 and less "
2678 "than or equal to maxGeometryShaderInvocations. "
2679 "Invocations=%d, maxGeometryShaderInvocations=%d",
2680 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
2681 }
2682 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002683 return skip;
2684}
2685
locke-lunargd9a069d2019-09-17 01:50:19 -06002686uint32_t DescriptorTypeToReqs(SHADER_MODULE_STATE const *module, uint32_t type_id) {
Chris Forbes47567b72017-06-09 12:09:45 -07002687 auto type = module->get_def(type_id);
2688
2689 while (true) {
2690 switch (type.opcode()) {
2691 case spv::OpTypeArray:
Chris Forbes062f1222018-08-21 15:34:15 -07002692 case spv::OpTypeRuntimeArray:
Chris Forbes47567b72017-06-09 12:09:45 -07002693 case spv::OpTypeSampledImage:
2694 type = module->get_def(type.word(2));
2695 break;
2696 case spv::OpTypePointer:
2697 type = module->get_def(type.word(3));
2698 break;
2699 case spv::OpTypeImage: {
2700 auto dim = type.word(3);
2701 auto arrayed = type.word(5);
2702 auto msaa = type.word(6);
2703
Chris Forbes74ba2232018-08-27 15:19:27 -07002704 uint32_t bits = 0;
2705 switch (GetFundamentalType(module, type.word(2))) {
2706 case FORMAT_TYPE_FLOAT:
2707 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
2708 break;
2709 case FORMAT_TYPE_UINT:
2710 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
2711 break;
2712 case FORMAT_TYPE_SINT:
2713 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
2714 break;
2715 default:
2716 break;
2717 }
2718
Chris Forbes47567b72017-06-09 12:09:45 -07002719 switch (dim) {
2720 case spv::Dim1D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002721 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
2722 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002723 case spv::Dim2D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002724 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
2725 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
2726 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002727 case spv::Dim3D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002728 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
2729 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002730 case spv::DimCube:
Chris Forbes74ba2232018-08-27 15:19:27 -07002731 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
2732 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002733 case spv::DimSubpassData:
Chris Forbes74ba2232018-08-27 15:19:27 -07002734 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
2735 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002736 default: // buffer, etc.
Chris Forbes74ba2232018-08-27 15:19:27 -07002737 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002738 }
2739 }
2740 default:
2741 return 0;
2742 }
2743 }
2744}
2745
2746// For given pipelineLayout verify that the set_layout_node at slot.first
2747// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002748static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002749 descriptor_slot_t slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002750 if (!pipelineLayout) return nullptr;
2751
2752 if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr;
2753
2754 return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second);
2755}
2756
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002757static 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 -06002758 for (auto insn : *src) {
2759 if (insn.opcode() == spv::OpEntryPoint) {
2760 auto executionModel = insn.word(1);
2761 auto entrypointStageBits = ExecutionModelToShaderStageFlagBits(executionModel);
2762 if (entrypointStageBits == VK_SHADER_STAGE_COMPUTE_BIT) {
2763 auto entrypoint_id = insn.word(2);
2764 for (auto insn1 : *src) {
2765 if (insn1.opcode() == spv::OpExecutionMode && insn1.word(1) == entrypoint_id &&
2766 insn1.word(2) == spv::ExecutionModeLocalSize) {
2767 local_size_x = insn1.word(3);
2768 local_size_y = insn1.word(4);
2769 local_size_z = insn1.word(5);
2770 return true;
2771 }
2772 }
2773 }
2774 }
2775 }
2776 return false;
2777}
2778
locke-lunargd9a069d2019-09-17 01:50:19 -06002779void ProcessExecutionModes(SHADER_MODULE_STATE const *src, const spirv_inst_iter &entrypoint, PIPELINE_STATE *pipeline) {
Jeff Bolz105d6492018-09-29 15:46:44 -05002780 auto entrypoint_id = entrypoint.word(2);
Chris Forbes0771b672018-03-22 21:13:46 -07002781 bool is_point_mode = false;
2782
2783 for (auto insn : *src) {
2784 if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) {
2785 switch (insn.word(2)) {
2786 case spv::ExecutionModePointMode:
2787 // In tessellation shaders, PointMode is separate and trumps the tessellation topology.
2788 is_point_mode = true;
2789 break;
2790
2791 case spv::ExecutionModeOutputPoints:
2792 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
2793 break;
2794
2795 case spv::ExecutionModeIsolines:
2796 case spv::ExecutionModeOutputLineStrip:
2797 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
2798 break;
2799
2800 case spv::ExecutionModeTriangles:
2801 case spv::ExecutionModeQuads:
2802 case spv::ExecutionModeOutputTriangleStrip:
2803 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
2804 break;
2805 }
2806 }
2807 }
2808
2809 if (is_point_mode) pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
2810}
2811
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002812// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2813// o If there is only a vertex shader : gl_PointSize must be written when using points
2814// o If there is a geometry or tessellation shader:
2815// - If shaderTessellationAndGeometryPointSize feature is enabled:
2816// * gl_PointSize must be written in the final geometry stage
2817// - If shaderTessellationAndGeometryPointSize feature is disabled:
2818// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002819bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06002820 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002821 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2822 return false;
2823 }
2824
2825 bool pointsize_written = false;
2826 bool skip = false;
2827
2828 // Search for PointSize built-in decorations
2829 std::vector<uint32_t> pointsize_builtin_offsets;
2830 spirv_inst_iter insn = entrypoint;
2831 while (!pointsize_written && (insn.opcode() != spv::OpFunction)) {
2832 if (insn.opcode() == spv::OpMemberDecorate) {
2833 if (insn.word(3) == spv::DecorationBuiltIn) {
2834 if (insn.word(4) == spv::BuiltInPointSize) {
2835 pointsize_written = IsPointSizeWritten(src, insn, entrypoint);
2836 }
2837 }
2838 } else if (insn.opcode() == spv::OpDecorate) {
2839 if (insn.word(2) == spv::DecorationBuiltIn) {
2840 if (insn.word(3) == spv::BuiltInPointSize) {
2841 pointsize_written = IsPointSizeWritten(src, insn, entrypoint);
2842 }
2843 }
2844 }
2845
2846 insn++;
2847 }
2848
2849 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002850 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002851 if (pointsize_written) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002852 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 -06002853 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
2854 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2855 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
2856 }
2857 } else if (!pointsize_written) {
2858 skip |=
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002859 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002860 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_MissingPointSizeBuiltIn,
2861 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2862 string_VkShaderStageFlagBits(stage));
2863 }
2864 return skip;
2865}
John Zulauf14c355b2019-06-27 16:09:37 -06002866
2867bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline,
2868 const PIPELINE_STATE::StageState &stage_state, const SHADER_MODULE_STATE *module,
John Zulaufac4c6e12019-07-01 16:05:58 -06002869 const spirv_inst_iter &entrypoint, bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002870 bool skip = false;
2871
2872 // Check the module
2873 if (!module->has_valid_spirv) {
2874 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2875 "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2876 report_data->FormatHandle(module->vk_shader_module).c_str(), string_VkShaderStageFlagBits(pStage->stage));
2877 }
2878
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002879 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2880 // specializations should be applied and validated.
2881 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2882 pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) {
2883 // Gather the specialization-constant values.
2884 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002885 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002886 std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map;
2887 id_value_map.reserve(specialization_info->mapEntryCount);
2888 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2889 auto const &map_entry = specialization_info->pMapEntries[i];
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002890
Jeremy Hayes521221d2020-01-15 16:48:49 -07002891 // Expect only scalar types.
2892 assert(map_entry.size == 1 || map_entry.size == 2 || map_entry.size == 4 || map_entry.size == 8);
2893 auto entry = id_value_map.emplace(map_entry.constantID, std::vector<uint32_t>(map_entry.size > 4 ? 2 : 1));
2894 memcpy(entry.first->second.data(), specialization_data + map_entry.offset, map_entry.size);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002895 }
2896
2897 // Apply the specialization-constant values and revalidate the shader module.
Tony-LunarG1a9cd5a2020-02-03 15:59:57 -07002898 spv_target_env spirv_environment;
2899 if (api_version >= VK_API_VERSION_1_2)
2900 spirv_environment = SPV_ENV_VULKAN_1_2;
2901 else if (api_version >= VK_API_VERSION_1_1)
2902 spirv_environment = SPV_ENV_VULKAN_1_1;
2903 else
2904 spirv_environment = SPV_ENV_VULKAN_1_0;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002905 spvtools::Optimizer optimizer(spirv_environment);
2906 spvtools::MessageConsumer consumer = [&skip, &module, &pStage, this](spv_message_level_t level, const char *source,
2907 const spv_position_t &position, const char *message) {
2908 skip |= log_msg(
2909 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2910 "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s. %s",
2911 report_data->FormatHandle(module->vk_shader_module).c_str(), string_VkShaderStageFlagBits(pStage->stage), message);
2912 };
2913 optimizer.SetMessageConsumer(consumer);
2914 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2915 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2916 std::vector<uint32_t> specialized_spirv;
2917 auto const optimized =
2918 optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, spvtools::ValidatorOptions(), true);
2919 assert(optimized == true);
2920
2921 if (optimized) {
2922 spv_context ctx = spvContextCreate(spirv_environment);
2923 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2924 spv_diagnostic diag = nullptr;
2925 spv_validator_options options = spvValidatorOptionsCreate();
2926 if (device_extensions.vk_khr_relaxed_block_layout) {
2927 spvValidatorOptionsSetRelaxBlockLayout(options, true);
2928 }
2929 if (device_extensions.vk_khr_uniform_buffer_standard_layout &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002930 enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002931 spvValidatorOptionsSetUniformBufferStandardLayout(options, true);
2932 }
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002933 if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002934 spvValidatorOptionsSetScalarBlockLayout(options, true);
2935 }
2936 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2937 if (spv_valid != SPV_SUCCESS) {
2938 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2939 "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2940 "After specialization was applied, %s does not contain valid spirv for stage %s.",
2941 report_data->FormatHandle(module->vk_shader_module).c_str(),
2942 string_VkShaderStageFlagBits(pStage->stage));
2943 }
2944
2945 spvValidatorOptionsDestroy(options);
2946 spvDiagnosticDestroy(diag);
2947 spvContextDestroy(ctx);
2948 }
2949 }
2950
John Zulauf14c355b2019-06-27 16:09:37 -06002951 // Check the entrypoint
2952 if (entrypoint == module->end()) {
2953 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2954 "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s..",
2955 pStage->pName, string_VkShaderStageFlagBits(pStage->stage));
2956 }
2957 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2958
2959 // Mark accessible ids
2960 auto &accessible_ids = stage_state.accessible_ids;
2961
Chris Forbes47567b72017-06-09 12:09:45 -07002962 // Validate descriptor set layout against what the entrypoint actually uses
John Zulauf14c355b2019-06-27 16:09:37 -06002963 bool has_writable_descriptor = stage_state.has_writable_descriptor;
2964 auto &descriptor_uses = stage_state.descriptor_uses;
Chris Forbes47567b72017-06-09 12:09:45 -07002965
Chris Forbes349b3132018-03-07 11:38:08 -08002966 // Validate shader capabilities against enabled device features
Jeff Bolzee743412019-06-20 22:24:32 -05002967 skip |= ValidateShaderCapabilities(module, pStage->stage);
2968 skip |= ValidateShaderStageWritableDescriptor(pStage->stage, has_writable_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002969 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Jeff Bolz526f2d52019-09-18 13:18:08 -05002970 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002971 skip |= ValidateExecutionModes(module, entrypoint);
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002972 skip |= ValidateSpecializationOffsets(pStage);
2973 skip |= ValidatePushConstantUsage(pipeline->pipeline_layout->push_constant_ranges.get(), module, accessible_ids, pStage->stage);
Jeff Bolze54ae892018-09-08 12:16:29 -05002974 if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002975 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002976 }
Jeff Bolze4356752019-03-07 11:23:46 -06002977 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
Chris Forbes47567b72017-06-09 12:09:45 -07002978
2979 // Validate descriptor use
2980 for (auto use : descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002981 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002982 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002983 unsigned required_descriptor_count;
Jeff Bolze54ae892018-09-08 12:16:29 -05002984 std::set<uint32_t> descriptor_types = TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count);
Chris Forbes47567b72017-06-09 12:09:45 -07002985
2986 if (!binding) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002987 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 -06002988 kVUID_Core_Shader_MissingDescriptor,
Chris Forbes73c00bf2018-06-22 16:28:06 -07002989 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeff Bolze54ae892018-09-08 12:16:29 -05002990 use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002991 } else if (~binding->stageFlags & pStage->stage) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002992 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 -06002993 kVUID_Core_Shader_DescriptorNotAccessibleFromStage,
Chris Forbes73c00bf2018-06-22 16:28:06 -07002994 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first,
2995 use.first.second, string_VkShaderStageFlagBits(pStage->stage));
Jeff Bolze54ae892018-09-08 12:16:29 -05002996 } else if (descriptor_types.find(binding->descriptorType) == descriptor_types.end()) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002997 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 -06002998 kVUID_Core_Shader_DescriptorTypeMismatch,
Chris Forbes73c00bf2018-06-22 16:28:06 -07002999 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first,
Jeff Bolze54ae892018-09-08 12:16:29 -05003000 use.first.second, string_descriptorTypes(descriptor_types).c_str(),
Chris Forbes47567b72017-06-09 12:09:45 -07003001 string_VkDescriptorType(binding->descriptorType));
3002 } else if (binding->descriptorCount < required_descriptor_count) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06003003 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 -06003004 kVUID_Core_Shader_DescriptorTypeMismatch,
Chris Forbes73c00bf2018-06-22 16:28:06 -07003005 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
3006 required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07003007 }
3008 }
3009
3010 // Validate use of input attachments against subpass structure
3011 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003012 auto input_attachment_uses = CollectInterfaceByInputAttachmentIndex(module, accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07003013
Petr Krause91f7a12017-12-14 20:57:36 +01003014 auto rpci = pipeline->rp_state->createInfo.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07003015 auto subpass = pipeline->graphicsPipelineCI.subpass;
3016
3017 for (auto use : input_attachment_uses) {
3018 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
3019 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07003020 ? input_attachments[use.first].attachment
3021 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07003022
3023 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06003024 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 -06003025 kVUID_Core_Shader_MissingInputAttachment,
Chris Forbes47567b72017-06-09 12:09:45 -07003026 "Shader consumes input attachment index %d but not provided in subpass", use.first);
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003027 } else if (!(GetFormatType(rpci->pAttachments[index].format) & GetFundamentalType(module, use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07003028 skip |=
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06003029 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 -06003030 kVUID_Core_Shader_InputAttachmentTypeMismatch,
Chris Forbes47567b72017-06-09 12:09:45 -07003031 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003032 string_VkFormat(rpci->pAttachments[index].format), DescribeType(module, use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07003033 }
3034 }
3035 }
Lockeaa8fdc02019-04-02 11:59:20 -06003036 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
3037 skip |= ValidateComputeWorkGroupSizes(module);
3038 }
Chris Forbes47567b72017-06-09 12:09:45 -07003039 return skip;
3040}
3041
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003042bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
3043 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
3044 spirv_inst_iter consumer_entrypoint,
3045 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07003046 bool skip = false;
3047
3048 auto outputs =
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003049 CollectInterfaceByLocation(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
3050 auto inputs = CollectInterfaceByLocation(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07003051
3052 auto a_it = outputs.begin();
3053 auto b_it = inputs.begin();
3054
3055 // Maps sorted by key (location); walk them together to find mismatches
3056 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
3057 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
3058 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
3059 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
3060 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
3061
3062 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Mark Young4e919b22018-05-21 15:53:59 -06003063 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 -06003064 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed,
Mark Young4e919b22018-05-21 15:53:59 -06003065 "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, a_first.first,
3066 a_first.second, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07003067 a_it++;
3068 } else if (a_at_end || a_first > b_first) {
Mark Young4e919b22018-05-21 15:53:59 -06003069 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 -06003070 HandleToUint64(consumer->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
Mark Young4e919b22018-05-21 15:53:59 -06003071 "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first,
3072 b_first.second, producer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07003073 b_it++;
3074 } else {
3075 // subtleties of arrayed interfaces:
3076 // - if is_patch, then the member is not arrayed, even though the interface may be.
3077 // - if is_block_member, then the extra array level of an arrayed interface is not
3078 // expressed in the member type -- it's expressed in the block type.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003079 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id,
3080 producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member,
3081 consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) {
Mark Young4e919b22018-05-21 15:53:59 -06003082 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 -06003083 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Young4e919b22018-05-21 15:53:59 -06003084 "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003085 DescribeType(producer, a_it->second.type_id).c_str(),
3086 DescribeType(consumer, b_it->second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07003087 }
3088 if (a_it->second.is_patch != b_it->second.is_patch) {
Mark Young4e919b22018-05-21 15:53:59 -06003089 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 -06003090 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Dave Houltona9df0ce2018-02-07 10:51:23 -07003091 "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 -07003092 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
3093 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
3094 }
3095 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Mark Young4e919b22018-05-21 15:53:59 -06003096 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 -06003097 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Chris Forbes47567b72017-06-09 12:09:45 -07003098 "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first,
3099 a_first.second, producer_stage->name, consumer_stage->name);
3100 }
3101 a_it++;
3102 b_it++;
3103 }
3104 }
3105
Ari Suonpaa696b3432019-03-11 14:02:57 +02003106 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
3107 auto builtins_producer = CollectBuiltinBlockMembers(producer, producer_entrypoint, spv::StorageClassOutput);
3108 auto builtins_consumer = CollectBuiltinBlockMembers(consumer, consumer_entrypoint, spv::StorageClassInput);
3109
3110 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
3111 if (builtins_producer.size() != builtins_consumer.size()) {
3112 skip |=
3113 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3114 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
3115 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).", producer_stage->name,
3116 (int)builtins_producer.size(), consumer_stage->name, (int)builtins_consumer.size());
3117 } else {
3118 auto it_producer = builtins_producer.begin();
3119 auto it_consumer = builtins_consumer.begin();
3120 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
3121 if (*it_producer != *it_consumer) {
3122 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3123 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
3124 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
3125 consumer_stage->name);
3126 break;
3127 }
3128 it_producer++;
3129 it_consumer++;
3130 }
3131 }
3132 }
3133 }
3134
Chris Forbes47567b72017-06-09 12:09:45 -07003135 return skip;
3136}
3137
John Zulauf14c355b2019-06-27 16:09:37 -06003138static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003139 uint32_t stage_mask = 0;
3140 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
3141 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
3142 stage_mask |= pCreateInfo->pStages[i].stage;
3143 }
3144 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05003145 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
3146 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
3147 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003148 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
3149 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
3150 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
3151 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
3152 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003153 }
3154 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003155 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003156}
3157
Chris Forbes47567b72017-06-09 12:09:45 -07003158// Validate that the shaders used by the given pipeline and store the active_slots
3159// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06003160bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Chris Forbesa400a8a2017-07-20 13:10:24 -07003161 auto pCreateInfo = pipeline->graphicsPipelineCI.ptr();
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003162 int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
3163 int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07003164
John Zulauf14c355b2019-06-27 16:09:37 -06003165 const SHADER_MODULE_STATE *shaders[32];
Chris Forbes47567b72017-06-09 12:09:45 -07003166 memset(shaders, 0, sizeof(shaders));
Jeff Bolz7e35c392018-09-04 15:30:41 -05003167 spirv_inst_iter entrypoints[32];
Chris Forbes47567b72017-06-09 12:09:45 -07003168 memset(entrypoints, 0, sizeof(entrypoints));
3169 bool skip = false;
3170
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003171 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, pCreateInfo);
3172
Chris Forbes47567b72017-06-09 12:09:45 -07003173 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
3174 auto pStage = &pCreateInfo->pStages[i];
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003175 auto stage_id = GetShaderStageId(pStage->stage);
John Zulauf14c355b2019-06-27 16:09:37 -06003176 shaders[stage_id] = GetShaderModuleState(pStage->module);
3177 entrypoints[stage_id] = FindEntrypoint(shaders[stage_id], pStage->pName, pStage->stage);
3178 skip |= ValidatePipelineShaderStage(pStage, pipeline, pipeline->stage_state[i], shaders[stage_id], entrypoints[stage_id],
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003179 (pointlist_stage_mask == pStage->stage));
Chris Forbes47567b72017-06-09 12:09:45 -07003180 }
3181
3182 // if the shader stages are no good individually, cross-stage validation is pointless.
3183 if (skip) return true;
3184
3185 auto vi = pCreateInfo->pVertexInputState;
3186
3187 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003188 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07003189 }
3190
3191 if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003192 skip |= ValidateViAgainstVsInputs(vi, shaders[vertex_stage], entrypoints[vertex_stage]);
Chris Forbes47567b72017-06-09 12:09:45 -07003193 }
3194
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003195 int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
3196 int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07003197
3198 while (!shaders[producer] && producer != fragment_stage) {
3199 producer++;
3200 consumer++;
3201 }
3202
3203 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
3204 assert(shaders[producer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08003205 if (shaders[consumer]) {
3206 if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003207 skip |= ValidateInterfaceBetweenStages(shaders[producer], entrypoints[producer], &shader_stage_attribs[producer],
3208 shaders[consumer], entrypoints[consumer], &shader_stage_attribs[consumer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08003209 }
Chris Forbes47567b72017-06-09 12:09:45 -07003210
3211 producer = consumer;
3212 }
3213 }
3214
3215 if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003216 skip |= ValidateFsOutputsAgainstRenderPass(shaders[fragment_stage], entrypoints[fragment_stage], pipeline,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003217 pCreateInfo->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07003218 }
3219
3220 return skip;
3221}
3222
John Zulaufac4c6e12019-07-01 16:05:58 -06003223bool CoreChecks::ValidateComputePipeline(PIPELINE_STATE *pipeline) const {
John Zulauf14c355b2019-06-27 16:09:37 -06003224 const auto &stage = *pipeline->computePipelineCI.stage.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07003225
John Zulauf14c355b2019-06-27 16:09:37 -06003226 const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module);
3227 const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage);
Chris Forbes47567b72017-06-09 12:09:45 -07003228
John Zulauf14c355b2019-06-27 16:09:37 -06003229 return ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[0], module, entrypoint, false);
Chris Forbes47567b72017-06-09 12:09:45 -07003230}
Chris Forbes4ae55b32017-06-09 14:42:56 -07003231
John Zulaufac4c6e12019-07-01 16:05:58 -06003232bool CoreChecks::ValidateRayTracingPipelineNV(PIPELINE_STATE *pipeline) const {
John Zulaufe4474e72019-07-01 17:28:27 -06003233 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04003234
3235 if (pipeline->raytracingPipelineCI.maxRecursionDepth > phys_dev_ext_props.ray_tracing_props.maxRecursionDepth) {
3236 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3237 "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-02412", ": %d > %d",
3238 pipeline->raytracingPipelineCI.maxRecursionDepth, phys_dev_ext_props.ray_tracing_props.maxRecursionDepth);
3239 }
3240
3241 const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages;
3242 const auto *groups = pipeline->raytracingPipelineCI.ptr()->pGroups;
3243
3244 uint32_t raygen_stages_found = 0;
John Zulaufe4474e72019-07-01 17:28:27 -06003245 for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003246 const auto &stage = stages[stage_index];
Jeff Bolzfbe51582018-09-13 10:01:35 -05003247
John Zulaufe4474e72019-07-01 17:28:27 -06003248 const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module);
3249 const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage);
Jeff Bolzfbe51582018-09-13 10:01:35 -05003250
John Zulaufe4474e72019-07-01 17:28:27 -06003251 skip |= ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[stage_index], module, entrypoint, false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003252
3253 if (stage.stage == VK_SHADER_STAGE_RAYGEN_BIT_NV) {
3254 raygen_stages_found++;
3255 }
3256 }
3257 if (raygen_stages_found != 1) {
3258 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3259 "VUID-VkRayTracingPipelineCreateInfoNV-stage-02408", " : %d raygen stages specified", raygen_stages_found);
3260 }
3261
3262 for (uint32_t group_index = 0; group_index < pipeline->raytracingPipelineCI.groupCount; group_index++) {
3263 const auto &group = groups[group_index];
3264
3265 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
3266 if (group.generalShader >= pipeline->raytracingPipelineCI.stageCount ||
3267 (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV &&
3268 stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV &&
3269 stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) {
3270 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3271 "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413", ": pGroups[%d]", group_index);
3272 }
3273 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3274 group.intersectionShader != VK_SHADER_UNUSED_NV) {
3275 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3276 "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414", ": pGroups[%d]", group_index);
3277 }
3278 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
3279 if (group.intersectionShader >= pipeline->raytracingPipelineCI.stageCount ||
3280 stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) {
3281 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3282 "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415", ": pGroups[%d]", group_index);
3283 }
3284 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3285 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
3286 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3287 "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416", ": pGroups[%d]", group_index);
3288 }
3289 }
3290
3291 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3292 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3293 if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= pipeline->raytracingPipelineCI.stageCount ||
3294 stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
3295 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3296 "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418", ": pGroups[%d]", group_index);
3297 }
3298 if (group.closestHitShader != VK_SHADER_UNUSED_NV &&
3299 (group.closestHitShader >= pipeline->raytracingPipelineCI.stageCount ||
3300 stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
3301 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3302 "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417", ": pGroups[%d]", group_index);
3303 }
3304 }
John Zulaufe4474e72019-07-01 17:28:27 -06003305 }
3306 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003307}
3308
Dave Houltona9df0ce2018-02-07 10:51:23 -07003309uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003310
Dave Houltona9df0ce2018-02-07 10:51:23 -07003311static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
John Zulauf25ea2432019-04-05 10:07:38 -06003312 const auto validation_cache_ci = lvl_find_in_chain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
3313 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003314 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003315 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003316 return nullptr;
3317}
3318
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003319bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003320 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003321 bool skip = false;
3322 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003323
Mark Lobodzinskib02a4852019-04-19 12:35:30 -06003324 if (disabled.shader_validation) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003325 return false;
3326 }
3327
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06003328 auto have_glsl_shader = device_extensions.vk_nv_glsl_shader;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003329
3330 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski7767ad82019-03-09 13:35:25 -07003331 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 -06003332 "VUID-VkShaderModuleCreateInfo-pCode-01376",
3333 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3334 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003335 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003336 auto cache = GetValidationCacheInfo(pCreateInfo);
3337 uint32_t hash = 0;
3338 if (cache) {
3339 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003340 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003341 }
3342
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003343 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3344 // the default values will be used during validation.
Jeremy Hayes0be25de2019-09-11 18:13:49 -06003345 spv_target_env spirv_environment = SPV_ENV_VULKAN_1_0;
Tony-LunarG034e63a2020-01-16 13:39:24 -07003346 if (api_version >= VK_API_VERSION_1_2) {
3347 spirv_environment = SPV_ENV_VULKAN_1_2;
3348 } else if (api_version >= VK_API_VERSION_1_1) {
Jesse Halla0389fc2019-09-25 16:46:21 -05003349 if (device_extensions.vk_khr_spirv_1_4) {
3350 spirv_environment = SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3351 } else {
3352 spirv_environment = SPV_ENV_VULKAN_1_1;
3353 }
Jeremy Hayes0be25de2019-09-11 18:13:49 -06003354 }
Dave Houlton0ea2d012018-06-21 14:00:26 -06003355 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003356 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003357 spv_diagnostic diag = nullptr;
Karl Schultzfda1b382018-08-08 18:56:11 -06003358 spv_validator_options options = spvValidatorOptionsCreate();
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06003359 if (device_extensions.vk_khr_relaxed_block_layout) {
Karl Schultzfda1b382018-08-08 18:56:11 -06003360 spvValidatorOptionsSetRelaxBlockLayout(options, true);
3361 }
Graeme Leese9b6a1522019-06-07 20:49:45 +01003362 if (device_extensions.vk_khr_uniform_buffer_standard_layout &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -07003363 enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
Graeme Leese9b6a1522019-06-07 20:49:45 +01003364 spvValidatorOptionsSetUniformBufferStandardLayout(options, true);
3365 }
Piers Daniell41b8c5d2020-01-10 15:42:00 -07003366 if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) {
Tobias Hector6a0ece72018-12-10 12:24:05 +00003367 spvValidatorOptionsSetScalarBlockLayout(options, true);
3368 }
Karl Schultzfda1b382018-08-08 18:56:11 -06003369 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003370 if (spv_valid != SPV_SUCCESS) {
3371 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski7767ad82019-03-09 13:35:25 -07003372 skip |=
3373 log_msg(report_data, spv_valid == SPV_WARNING ? VK_DEBUG_REPORT_WARNING_BIT_EXT : VK_DEBUG_REPORT_ERROR_BIT_EXT,
3374 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_InconsistentSpirv,
3375 "SPIR-V module not valid: %s", diag && diag->error ? diag->error : "(no error text)");
Chris Forbes4ae55b32017-06-09 14:42:56 -07003376 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003377 } else {
3378 if (cache) {
3379 cache->Insert(hash);
3380 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003381 }
3382
Karl Schultzfda1b382018-08-08 18:56:11 -06003383 spvValidatorOptionsDestroy(options);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003384 spvDiagnosticDestroy(diag);
3385 spvContextDestroy(ctx);
3386 }
3387
Chris Forbes4ae55b32017-06-09 14:42:56 -07003388 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003389}
3390
John Zulaufac4c6e12019-07-01 16:05:58 -06003391bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003392 bool skip = false;
3393 uint32_t local_size_x = 0;
3394 uint32_t local_size_y = 0;
3395 uint32_t local_size_z = 0;
3396 if (FindLocalSize(shader, local_size_x, local_size_y, local_size_z)) {
3397 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003398 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3399 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3400 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
3401 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3402 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06003403 }
3404 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003405 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3406 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3407 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
3408 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3409 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06003410 }
3411 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003412 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3413 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3414 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
3415 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3416 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06003417 }
3418
3419 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3420 uint64_t invocations = local_size_x * local_size_y;
3421 // Prevent overflow.
3422 bool fail = false;
3423 if (invocations > UINT32_MAX || invocations > limit) {
3424 fail = true;
3425 }
3426 if (!fail) {
3427 invocations *= local_size_z;
3428 if (invocations > UINT32_MAX || invocations > limit) {
3429 fail = true;
3430 }
3431 }
3432 if (fail) {
3433 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3434 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations",
locke-lunarg9edc2812019-06-17 23:18:52 -06003435 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
Lockeaa8fdc02019-04-02 11:59:20 -06003436 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
3437 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, local_size_y, local_size_z,
3438 limit);
3439 }
3440 }
3441 return skip;
3442}