blob: bbba08bcf1c5193a6027da6ef701cc793752f0f0 [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
Jeff Bolzf234bf82019-11-04 14:07:15 -060022#define NOMINMAX
23
Petr Kraus25810d02019-08-27 17:41:15 +020024#include "shader_validation.h"
25
Chris Forbes47567b72017-06-09 12:09:45 -070026#include <cassert>
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +020027#include <chrono>
Petr Kraus25810d02019-08-27 17:41:15 +020028#include <cinttypes>
Jeff Bolzf234bf82019-11-04 14:07:15 -060029#include <cmath>
Petr Kraus25810d02019-08-27 17:41:15 +020030#include <map>
Chris Forbes47567b72017-06-09 12:09:45 -070031#include <sstream>
Petr Kraus25810d02019-08-27 17:41:15 +020032#include <string>
33#include <unordered_map>
34#include <vector>
35
Chris Forbes47567b72017-06-09 12:09:45 -070036#include <SPIRV/spirv.hpp>
37#include "vk_loader_platform.h"
38#include "vk_enum_string_helper.h"
Chris Forbes47567b72017-06-09 12:09:45 -070039#include "vk_layer_data.h"
40#include "vk_layer_extension_utils.h"
41#include "vk_layer_utils.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070042#include "chassis.h"
Chris Forbes47567b72017-06-09 12:09:45 -070043#include "core_validation.h"
Petr Kraus25810d02019-08-27 17:41:15 +020044
Chris Forbes4ae55b32017-06-09 14:42:56 -070045#include "spirv-tools/libspirv.h"
Chris Forbes9a61e082017-07-24 15:35:29 -070046#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070047
Chris Forbes8a6d8cb2019-02-14 14:33:08 -080048void decoration_set::add(uint32_t decoration, uint32_t value) {
49 switch (decoration) {
50 case spv::DecorationLocation:
51 flags |= location_bit;
52 location = value;
53 break;
54 case spv::DecorationPatch:
55 flags |= patch_bit;
56 break;
57 case spv::DecorationRelaxedPrecision:
58 flags |= relaxed_precision_bit;
59 break;
60 case spv::DecorationBlock:
61 flags |= block_bit;
62 break;
63 case spv::DecorationBufferBlock:
64 flags |= buffer_block_bit;
65 break;
66 case spv::DecorationComponent:
67 flags |= component_bit;
68 component = value;
69 break;
70 case spv::DecorationInputAttachmentIndex:
71 flags |= input_attachment_index_bit;
72 input_attachment_index = value;
73 break;
74 case spv::DecorationDescriptorSet:
75 flags |= descriptor_set_bit;
76 descriptor_set = value;
77 break;
78 case spv::DecorationBinding:
79 flags |= binding_bit;
80 binding = value;
81 break;
82 case spv::DecorationNonWritable:
83 flags |= nonwritable_bit;
84 break;
85 case spv::DecorationBuiltIn:
86 flags |= builtin_bit;
87 builtin = value;
88 break;
89 }
90}
91
Chris Forbes47567b72017-06-09 12:09:45 -070092enum FORMAT_TYPE {
93 FORMAT_TYPE_FLOAT = 1, // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader
94 FORMAT_TYPE_SINT = 2,
95 FORMAT_TYPE_UINT = 4,
96};
97
98typedef std::pair<unsigned, unsigned> location_t;
99
Chris Forbes47567b72017-06-09 12:09:45 -0700100struct shader_stage_attributes {
101 char const *const name;
102 bool arrayed_input;
103 bool arrayed_output;
Ari Suonpaa696b3432019-03-11 14:02:57 +0200104 VkShaderStageFlags stage;
Chris Forbes47567b72017-06-09 12:09:45 -0700105};
106
107static shader_stage_attributes shader_stage_attribs[] = {
Ari Suonpaa696b3432019-03-11 14:02:57 +0200108 {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT},
109 {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT},
110 {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT},
111 {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT},
112 {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT},
Chris Forbes47567b72017-06-09 12:09:45 -0700113};
114
John Zulauf14c355b2019-06-27 16:09:37 -0600115unsigned ExecutionModelToShaderStageFlagBits(unsigned mode);
116
Chris Forbes47567b72017-06-09 12:09:45 -0700117// SPIRV utility functions
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600118void SHADER_MODULE_STATE::BuildDefIndex() {
Chris Forbes47567b72017-06-09 12:09:45 -0700119 for (auto insn : *this) {
120 switch (insn.opcode()) {
121 // Types
122 case spv::OpTypeVoid:
123 case spv::OpTypeBool:
124 case spv::OpTypeInt:
125 case spv::OpTypeFloat:
126 case spv::OpTypeVector:
127 case spv::OpTypeMatrix:
128 case spv::OpTypeImage:
129 case spv::OpTypeSampler:
130 case spv::OpTypeSampledImage:
131 case spv::OpTypeArray:
132 case spv::OpTypeRuntimeArray:
133 case spv::OpTypeStruct:
134 case spv::OpTypeOpaque:
135 case spv::OpTypePointer:
136 case spv::OpTypeFunction:
137 case spv::OpTypeEvent:
138 case spv::OpTypeDeviceEvent:
139 case spv::OpTypeReserveId:
140 case spv::OpTypeQueue:
141 case spv::OpTypePipe:
Shannon McPherson0fa28232018-11-01 11:59:02 -0600142 case spv::OpTypeAccelerationStructureNV:
Jeff Bolze4356752019-03-07 11:23:46 -0600143 case spv::OpTypeCooperativeMatrixNV:
Chris Forbes47567b72017-06-09 12:09:45 -0700144 def_index[insn.word(1)] = insn.offset();
145 break;
146
147 // Fixed constants
148 case spv::OpConstantTrue:
149 case spv::OpConstantFalse:
150 case spv::OpConstant:
151 case spv::OpConstantComposite:
152 case spv::OpConstantSampler:
153 case spv::OpConstantNull:
154 def_index[insn.word(2)] = insn.offset();
155 break;
156
157 // Specialization constants
158 case spv::OpSpecConstantTrue:
159 case spv::OpSpecConstantFalse:
160 case spv::OpSpecConstant:
161 case spv::OpSpecConstantComposite:
162 case spv::OpSpecConstantOp:
163 def_index[insn.word(2)] = insn.offset();
164 break;
165
166 // Variables
167 case spv::OpVariable:
168 def_index[insn.word(2)] = insn.offset();
169 break;
170
171 // Functions
172 case spv::OpFunction:
173 def_index[insn.word(2)] = insn.offset();
174 break;
175
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800176 // Decorations
177 case spv::OpDecorate: {
178 auto targetId = insn.word(1);
179 decorations[targetId].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u);
180 } break;
181 case spv::OpGroupDecorate: {
182 auto const &src = decorations[insn.word(1)];
183 for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src);
184 } break;
185
John Zulauf14c355b2019-06-27 16:09:37 -0600186 // Entry points ... add to the entrypoint table
187 case spv::OpEntryPoint: {
188 // Entry points do not have an id (the id is the function id) and thus need their own table
189 auto entrypoint_name = (char const *)&insn.word(3);
190 auto execution_model = insn.word(1);
191 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
192 entry_points.emplace(entrypoint_name, EntryPoint{insn.offset(), entrypoint_stage});
193 break;
194 }
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800195
Chris Forbes47567b72017-06-09 12:09:45 -0700196 default:
197 // We don't care about any other defs for now.
198 break;
199 }
200 }
201}
202
Jeff Bolz105d6492018-09-29 15:46:44 -0500203unsigned ExecutionModelToShaderStageFlagBits(unsigned mode) {
204 switch (mode) {
205 case spv::ExecutionModelVertex:
206 return VK_SHADER_STAGE_VERTEX_BIT;
207 case spv::ExecutionModelTessellationControl:
208 return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
209 case spv::ExecutionModelTessellationEvaluation:
210 return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
211 case spv::ExecutionModelGeometry:
212 return VK_SHADER_STAGE_GEOMETRY_BIT;
213 case spv::ExecutionModelFragment:
214 return VK_SHADER_STAGE_FRAGMENT_BIT;
215 case spv::ExecutionModelGLCompute:
216 return VK_SHADER_STAGE_COMPUTE_BIT;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600217 case spv::ExecutionModelRayGenerationNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700218 return VK_SHADER_STAGE_RAYGEN_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600219 case spv::ExecutionModelAnyHitNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700220 return VK_SHADER_STAGE_ANY_HIT_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600221 case spv::ExecutionModelClosestHitNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700222 return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600223 case spv::ExecutionModelMissNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700224 return VK_SHADER_STAGE_MISS_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600225 case spv::ExecutionModelIntersectionNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700226 return VK_SHADER_STAGE_INTERSECTION_BIT_NV;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600227 case spv::ExecutionModelCallableNV:
Eric Werness30127fd2018-10-31 21:01:03 -0700228 return VK_SHADER_STAGE_CALLABLE_BIT_NV;
Jeff Bolz105d6492018-09-29 15:46:44 -0500229 case spv::ExecutionModelTaskNV:
230 return VK_SHADER_STAGE_TASK_BIT_NV;
231 case spv::ExecutionModelMeshNV:
232 return VK_SHADER_STAGE_MESH_BIT_NV;
233 default:
234 return 0;
235 }
236}
237
locke-lunargd9a069d2019-09-17 01:50:19 -0600238spirv_inst_iter FindEntrypoint(SHADER_MODULE_STATE const *src, char const *name, VkShaderStageFlagBits stageBits) {
John Zulauf14c355b2019-06-27 16:09:37 -0600239 auto range = src->entry_points.equal_range(name);
240 for (auto it = range.first; it != range.second; ++it) {
241 if (it->second.stage == stageBits) {
242 return src->at(it->second.offset);
Chris Forbes47567b72017-06-09 12:09:45 -0700243 }
244 }
Chris Forbes47567b72017-06-09 12:09:45 -0700245 return src->end();
246}
247
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600248static char const *StorageClassName(unsigned sc) {
Chris Forbes47567b72017-06-09 12:09:45 -0700249 switch (sc) {
250 case spv::StorageClassInput:
251 return "input";
252 case spv::StorageClassOutput:
253 return "output";
254 case spv::StorageClassUniformConstant:
255 return "const uniform";
256 case spv::StorageClassUniform:
257 return "uniform";
258 case spv::StorageClassWorkgroup:
259 return "workgroup local";
260 case spv::StorageClassCrossWorkgroup:
261 return "workgroup global";
262 case spv::StorageClassPrivate:
263 return "private global";
264 case spv::StorageClassFunction:
265 return "function";
266 case spv::StorageClassGeneric:
267 return "generic";
268 case spv::StorageClassAtomicCounter:
269 return "atomic counter";
270 case spv::StorageClassImage:
271 return "image";
272 case spv::StorageClassPushConstant:
273 return "push constant";
Chris Forbes9f89d752018-03-07 12:57:48 -0800274 case spv::StorageClassStorageBuffer:
275 return "storage buffer";
Chris Forbes47567b72017-06-09 12:09:45 -0700276 default:
277 return "unknown";
278 }
279}
280
281// Get the value of an integral constant
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600282unsigned GetConstantValue(SHADER_MODULE_STATE const *src, unsigned id) {
Chris Forbes47567b72017-06-09 12:09:45 -0700283 auto value = src->get_def(id);
284 assert(value != src->end());
285
286 if (value.opcode() != spv::OpConstant) {
287 // TODO: Either ensure that the specialization transform is already performed on a module we're
288 // considering here, OR -- specialize on the fly now.
289 return 1;
290 }
291
292 return value.word(3);
293}
294
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600295static void DescribeTypeInner(std::ostringstream &ss, SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700296 auto insn = src->get_def(type);
297 assert(insn != src->end());
298
299 switch (insn.opcode()) {
300 case spv::OpTypeBool:
301 ss << "bool";
302 break;
303 case spv::OpTypeInt:
304 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
305 break;
306 case spv::OpTypeFloat:
307 ss << "float" << insn.word(2);
308 break;
309 case spv::OpTypeVector:
310 ss << "vec" << insn.word(3) << " of ";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600311 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700312 break;
313 case spv::OpTypeMatrix:
314 ss << "mat" << insn.word(3) << " of ";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600315 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700316 break;
317 case spv::OpTypeArray:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600318 ss << "arr[" << GetConstantValue(src, insn.word(3)) << "] of ";
319 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700320 break;
Chris Forbes062f1222018-08-21 15:34:15 -0700321 case spv::OpTypeRuntimeArray:
322 ss << "runtime arr[] of ";
323 DescribeTypeInner(ss, src, insn.word(2));
324 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700325 case spv::OpTypePointer:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600326 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
327 DescribeTypeInner(ss, src, insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700328 break;
329 case spv::OpTypeStruct: {
330 ss << "struct of (";
331 for (unsigned i = 2; i < insn.len(); i++) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600332 DescribeTypeInner(ss, src, insn.word(i));
Chris Forbes47567b72017-06-09 12:09:45 -0700333 if (i == insn.len() - 1) {
334 ss << ")";
335 } else {
336 ss << ", ";
337 }
338 }
339 break;
340 }
341 case spv::OpTypeSampler:
342 ss << "sampler";
343 break;
344 case spv::OpTypeSampledImage:
345 ss << "sampler+";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600346 DescribeTypeInner(ss, src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700347 break;
348 case spv::OpTypeImage:
349 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
350 break;
Shannon McPherson0fa28232018-11-01 11:59:02 -0600351 case spv::OpTypeAccelerationStructureNV:
Jeff Bolz105d6492018-09-29 15:46:44 -0500352 ss << "accelerationStruture";
353 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700354 default:
355 ss << "oddtype";
356 break;
357 }
358}
359
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600360static std::string DescribeType(SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700361 std::ostringstream ss;
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600362 DescribeTypeInner(ss, src, type);
Chris Forbes47567b72017-06-09 12:09:45 -0700363 return ss.str();
364}
365
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600366static bool IsNarrowNumericType(spirv_inst_iter type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700367 if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false;
368 return type.word(2) < 64;
369}
370
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600371static 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 -0600372 bool b_arrayed, bool relaxed) {
Chris Forbes47567b72017-06-09 12:09:45 -0700373 // Walk two type trees together, and complain about differences
374 auto a_insn = a->get_def(a_type);
375 auto b_insn = b->get_def(b_type);
376 assert(a_insn != a->end());
377 assert(b_insn != b->end());
378
Chris Forbes062f1222018-08-21 15:34:15 -0700379 // Ignore runtime-sized arrays-- they cannot appear in these interfaces.
380
Chris Forbes47567b72017-06-09 12:09:45 -0700381 if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600382 return TypesMatch(a, b, a_insn.word(2), b_type, false, b_arrayed, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700383 }
384
385 if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) {
386 // 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 -0600387 return TypesMatch(a, b, a_type, b_insn.word(2), a_arrayed, false, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700388 }
389
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600390 if (a_insn.opcode() == spv::OpTypeVector && relaxed && IsNarrowNumericType(b_insn)) {
391 return TypesMatch(a, b, a_insn.word(2), b_type, a_arrayed, b_arrayed, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700392 }
393
394 if (a_insn.opcode() != b_insn.opcode()) {
395 return false;
396 }
397
398 if (a_insn.opcode() == spv::OpTypePointer) {
399 // Match on pointee type. storage class is expected to differ
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600400 return TypesMatch(a, b, a_insn.word(3), b_insn.word(3), a_arrayed, b_arrayed, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -0700401 }
402
403 if (a_arrayed || b_arrayed) {
404 // If we havent resolved array-of-verts by here, we're not going to.
405 return false;
406 }
407
408 switch (a_insn.opcode()) {
409 case spv::OpTypeBool:
410 return true;
411 case spv::OpTypeInt:
412 // Match on width, signedness
413 return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3);
414 case spv::OpTypeFloat:
415 // Match on width
416 return a_insn.word(2) == b_insn.word(2);
417 case spv::OpTypeVector:
418 // Match on element type, count.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600419 if (!TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false;
420 if (relaxed && IsNarrowNumericType(a->get_def(a_insn.word(2)))) {
Chris Forbes47567b72017-06-09 12:09:45 -0700421 return a_insn.word(3) >= b_insn.word(3);
422 } else {
423 return a_insn.word(3) == b_insn.word(3);
424 }
425 case spv::OpTypeMatrix:
426 // Match on element type, count.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600427 return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
Dave Houltona9df0ce2018-02-07 10:51:23 -0700428 a_insn.word(3) == b_insn.word(3);
Chris Forbes47567b72017-06-09 12:09:45 -0700429 case spv::OpTypeArray:
430 // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from
431 // 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 -0600432 return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
433 GetConstantValue(a, a_insn.word(3)) == GetConstantValue(b, b_insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700434 case spv::OpTypeStruct:
435 // Match on all element types
Dave Houltona9df0ce2018-02-07 10:51:23 -0700436 {
437 if (a_insn.len() != b_insn.len()) {
438 return false; // Structs cannot match if member counts differ
Chris Forbes47567b72017-06-09 12:09:45 -0700439 }
Chris Forbes47567b72017-06-09 12:09:45 -0700440
Dave Houltona9df0ce2018-02-07 10:51:23 -0700441 for (unsigned i = 2; i < a_insn.len(); i++) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600442 if (!TypesMatch(a, b, a_insn.word(i), b_insn.word(i), a_arrayed, b_arrayed, false)) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700443 return false;
444 }
445 }
446
447 return true;
448 }
Chris Forbes47567b72017-06-09 12:09:45 -0700449 default:
450 // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match.
451 return false;
452 }
453}
454
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600455static unsigned ValueOrDefault(std::unordered_map<unsigned, unsigned> const &map, unsigned id, unsigned def) {
Chris Forbes47567b72017-06-09 12:09:45 -0700456 auto it = map.find(id);
457 if (it == map.end())
458 return def;
459 else
460 return it->second;
461}
462
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600463static unsigned GetLocationsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) {
Chris Forbes47567b72017-06-09 12:09:45 -0700464 auto insn = src->get_def(type);
465 assert(insn != src->end());
466
467 switch (insn.opcode()) {
468 case spv::OpTypePointer:
469 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
470 // pointers around.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600471 return GetLocationsConsumedByType(src, insn.word(3), strip_array_level);
Chris Forbes47567b72017-06-09 12:09:45 -0700472 case spv::OpTypeArray:
473 if (strip_array_level) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600474 return GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700475 } else {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600476 return GetConstantValue(src, insn.word(3)) * GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700477 }
478 case spv::OpTypeMatrix:
479 // Num locations is the dimension * element size
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600480 return insn.word(3) * GetLocationsConsumedByType(src, insn.word(2), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700481 case spv::OpTypeVector: {
482 auto scalar_type = src->get_def(insn.word(2));
483 auto bit_width =
484 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
485
486 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
487 return (bit_width * insn.word(3) + 127) / 128;
488 }
489 default:
490 // Everything else is just 1.
491 return 1;
492
493 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
494 }
495}
496
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600497static unsigned GetComponentsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200498 auto insn = src->get_def(type);
499 assert(insn != src->end());
500
501 switch (insn.opcode()) {
502 case spv::OpTypePointer:
503 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
504 // pointers around.
505 return GetComponentsConsumedByType(src, insn.word(3), strip_array_level);
506 case spv::OpTypeStruct: {
507 uint32_t sum = 0;
508 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
509 sum += GetComponentsConsumedByType(src, insn.word(i), false);
510 }
511 return sum;
512 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500513 case spv::OpTypeArray:
514 if (strip_array_level) {
515 return GetComponentsConsumedByType(src, insn.word(2), false);
516 } else {
517 return GetConstantValue(src, insn.word(3)) * GetComponentsConsumedByType(src, insn.word(2), false);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200518 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200519 case spv::OpTypeMatrix:
520 // Num locations is the dimension * element size
521 return insn.word(3) * GetComponentsConsumedByType(src, insn.word(2), false);
522 case spv::OpTypeVector: {
523 auto scalar_type = src->get_def(insn.word(2));
524 auto bit_width =
525 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
526 // One component is 32-bit
527 return (bit_width * insn.word(3) + 31) / 32;
528 }
529 case spv::OpTypeFloat: {
530 auto bit_width = insn.word(2);
531 return (bit_width + 31) / 32;
532 }
533 case spv::OpTypeInt: {
534 auto bit_width = insn.word(2);
535 return (bit_width + 31) / 32;
536 }
537 case spv::OpConstant:
538 return GetComponentsConsumedByType(src, insn.word(1), false);
539 default:
540 return 0;
541 }
542}
543
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600544static unsigned GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -0700545 switch (format) {
546 case VK_FORMAT_R64G64B64A64_SFLOAT:
547 case VK_FORMAT_R64G64B64A64_SINT:
548 case VK_FORMAT_R64G64B64A64_UINT:
549 case VK_FORMAT_R64G64B64_SFLOAT:
550 case VK_FORMAT_R64G64B64_SINT:
551 case VK_FORMAT_R64G64B64_UINT:
552 return 2;
553 default:
554 return 1;
555 }
556}
557
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600558static unsigned GetFormatType(VkFormat fmt) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700559 if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT;
560 if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT;
561 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
562 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700563 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
564 return FORMAT_TYPE_FLOAT;
565}
566
567// 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 -0700568// also used for input attachments, as we statically know their format.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600569static unsigned GetFundamentalType(SHADER_MODULE_STATE const *src, unsigned type) {
Chris Forbes47567b72017-06-09 12:09:45 -0700570 auto insn = src->get_def(type);
571 assert(insn != src->end());
572
573 switch (insn.opcode()) {
574 case spv::OpTypeInt:
575 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
576 case spv::OpTypeFloat:
577 return FORMAT_TYPE_FLOAT;
578 case spv::OpTypeVector:
Chris Forbes47567b72017-06-09 12:09:45 -0700579 case spv::OpTypeMatrix:
Chris Forbes47567b72017-06-09 12:09:45 -0700580 case spv::OpTypeArray:
Chris Forbes062f1222018-08-21 15:34:15 -0700581 case spv::OpTypeRuntimeArray:
582 case spv::OpTypeImage:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600583 return GetFundamentalType(src, insn.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700584 case spv::OpTypePointer:
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600585 return GetFundamentalType(src, insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700586
587 default:
588 return 0;
589 }
590}
591
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600592static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700593 uint32_t bit_pos = uint32_t(u_ffs(stage));
594 return bit_pos - 1;
595}
596
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600597static 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 -0700598 while (true) {
599 if (def.opcode() == spv::OpTypePointer) {
600 def = src->get_def(def.word(3));
601 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
602 def = src->get_def(def.word(2));
603 is_array_of_verts = false;
604 } else if (def.opcode() == spv::OpTypeStruct) {
605 return def;
606 } else {
607 return src->end();
608 }
609 }
610}
611
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600612static bool CollectInterfaceBlockMembers(SHADER_MODULE_STATE const *src, std::map<location_t, interface_var> *out,
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800613 bool is_array_of_verts, uint32_t id, uint32_t type_id, bool is_patch,
614 int /*first_location*/) {
Chris Forbes47567b72017-06-09 12:09:45 -0700615 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600616 auto type = GetStructType(src, src->get_def(type_id), is_array_of_verts && !is_patch);
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800617 if (type == src->end() || !(src->get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700618 // This isn't an interface block.
Chris Forbesa313d772017-06-13 13:59:41 -0700619 return false;
Chris Forbes47567b72017-06-09 12:09:45 -0700620 }
621
622 std::unordered_map<unsigned, unsigned> member_components;
623 std::unordered_map<unsigned, unsigned> member_relaxed_precision;
Chris Forbesa313d772017-06-13 13:59:41 -0700624 std::unordered_map<unsigned, unsigned> member_patch;
Chris Forbes47567b72017-06-09 12:09:45 -0700625
626 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
627 for (auto insn : *src) {
628 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
629 unsigned member_index = insn.word(2);
630
631 if (insn.word(3) == spv::DecorationComponent) {
632 unsigned component = insn.word(4);
633 member_components[member_index] = component;
634 }
635
636 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
637 member_relaxed_precision[member_index] = 1;
638 }
Chris Forbesa313d772017-06-13 13:59:41 -0700639
640 if (insn.word(3) == spv::DecorationPatch) {
641 member_patch[member_index] = 1;
642 }
Chris Forbes47567b72017-06-09 12:09:45 -0700643 }
644 }
645
Chris Forbesa313d772017-06-13 13:59:41 -0700646 // TODO: correctly handle location assignment from outside
647
Chris Forbes47567b72017-06-09 12:09:45 -0700648 // Second pass -- produce the output, from Location decorations
649 for (auto insn : *src) {
650 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
651 unsigned member_index = insn.word(2);
652 unsigned member_type_id = type.word(2 + member_index);
653
654 if (insn.word(3) == spv::DecorationLocation) {
655 unsigned location = insn.word(4);
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600656 unsigned num_locations = GetLocationsConsumedByType(src, member_type_id, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700657 auto component_it = member_components.find(member_index);
658 unsigned component = component_it == member_components.end() ? 0 : component_it->second;
659 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
Dave Houltona9df0ce2018-02-07 10:51:23 -0700660 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700661
662 for (unsigned int offset = 0; offset < num_locations; offset++) {
663 interface_var v = {};
664 v.id = id;
665 // TODO: member index in interface_var too?
666 v.type_id = member_type_id;
667 v.offset = offset;
Chris Forbesa313d772017-06-13 13:59:41 -0700668 v.is_patch = member_is_patch;
Chris Forbes47567b72017-06-09 12:09:45 -0700669 v.is_block_member = true;
670 v.is_relaxed_precision = is_relaxed_precision;
671 (*out)[std::make_pair(location + offset, component)] = v;
672 }
673 }
674 }
675 }
Chris Forbesa313d772017-06-13 13:59:41 -0700676
677 return true;
Chris Forbes47567b72017-06-09 12:09:45 -0700678}
679
Ari Suonpaa696b3432019-03-11 14:02:57 +0200680static std::vector<uint32_t> FindEntrypointInterfaces(spirv_inst_iter entrypoint) {
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800681 assert(entrypoint.opcode() == spv::OpEntryPoint);
682
Ari Suonpaa696b3432019-03-11 14:02:57 +0200683 std::vector<uint32_t> interfaces;
684 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
685 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
686 uint32_t word = 3;
687 while (entrypoint.word(word) & 0xff000000u) {
688 ++word;
689 }
690 ++word;
691
692 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
693
694 return interfaces;
695}
696
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600697static std::map<location_t, interface_var> CollectInterfaceByLocation(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600698 spv::StorageClass sinterface, bool is_array_of_verts) {
Chris Forbes47567b72017-06-09 12:09:45 -0700699 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
700
Chris Forbes47567b72017-06-09 12:09:45 -0700701 std::map<location_t, interface_var> out;
702
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800703 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
704 auto insn = src->get_def(iid);
Chris Forbes47567b72017-06-09 12:09:45 -0700705 assert(insn != src->end());
706 assert(insn.opcode() == spv::OpVariable);
707
708 if (insn.word(3) == static_cast<uint32_t>(sinterface)) {
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800709 auto d = src->get_decorations(iid);
Chris Forbes47567b72017-06-09 12:09:45 -0700710 unsigned id = insn.word(2);
711 unsigned type = insn.word(1);
712
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800713 int location = d.location;
714 int builtin = d.builtin;
715 unsigned component = d.component;
716 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
717 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700718
Dave Houltona9df0ce2018-02-07 10:51:23 -0700719 if (builtin != -1)
720 continue;
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800721 else if (!CollectInterfaceBlockMembers(src, &out, is_array_of_verts, id, type, is_patch, location)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700722 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
723 // one result for each.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600724 unsigned num_locations = GetLocationsConsumedByType(src, type, is_array_of_verts && !is_patch);
Chris Forbes47567b72017-06-09 12:09:45 -0700725 for (unsigned int offset = 0; offset < num_locations; offset++) {
726 interface_var v = {};
727 v.id = id;
728 v.type_id = type;
729 v.offset = offset;
730 v.is_patch = is_patch;
731 v.is_relaxed_precision = is_relaxed_precision;
732 out[std::make_pair(location + offset, component)] = v;
733 }
Chris Forbes47567b72017-06-09 12:09:45 -0700734 }
735 }
736 }
737
738 return out;
739}
740
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600741static std::vector<uint32_t> CollectBuiltinBlockMembers(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint,
Ari Suonpaa696b3432019-03-11 14:02:57 +0200742 uint32_t storageClass) {
743 std::vector<uint32_t> variables;
744 std::vector<uint32_t> builtinStructMembers;
745 std::vector<uint32_t> builtinDecorations;
746
747 for (auto insn : *src) {
748 switch (insn.opcode()) {
749 // Find all built-in member decorations
750 case spv::OpMemberDecorate:
751 if (insn.word(3) == spv::DecorationBuiltIn) {
752 builtinStructMembers.push_back(insn.word(1));
753 }
754 break;
755 // Find all built-in decorations
756 case spv::OpDecorate:
757 switch (insn.word(2)) {
758 case spv::DecorationBlock: {
759 uint32_t blockID = insn.word(1);
760 for (auto builtInBlockID : builtinStructMembers) {
761 // Check if one of the members of the block are built-in -> the block is built-in
762 if (blockID == builtInBlockID) {
763 builtinDecorations.push_back(blockID);
764 break;
765 }
766 }
767 break;
768 }
769 case spv::DecorationBuiltIn:
770 builtinDecorations.push_back(insn.word(1));
771 break;
772 default:
773 break;
774 }
775 break;
776 default:
777 break;
778 }
779 }
780
781 // Find all interface variables belonging to the entrypoint and matching the storage class
782 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
783 auto def = src->get_def(id);
784 assert(def != src->end());
785 assert(def.opcode() == spv::OpVariable);
786
787 if (def.word(3) == storageClass) variables.push_back(def.word(1));
788 }
789
790 // Find all members belonging to the builtin block selected
791 std::vector<uint32_t> builtinBlockMembers;
792 for (auto &var : variables) {
793 auto def = src->get_def(src->get_def(var).word(3));
794
795 // It could be an array of IO blocks. The element type should be the struct defining the block contents
796 if (def.opcode() == spv::OpTypeArray) def = src->get_def(def.word(2));
797
798 // Now find all members belonging to the struct defining the IO block
799 if (def.opcode() == spv::OpTypeStruct) {
800 for (auto builtInID : builtinDecorations) {
801 if (builtInID == def.word(1)) {
802 for (int i = 2; i < (int)def.len(); i++)
803 builtinBlockMembers.push_back(spv::BuiltInMax); // Start with undefined builtin for each struct member.
804 // These shouldn't be left after replacing.
805 for (auto insn : *src) {
806 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == builtInID &&
807 insn.word(3) == spv::DecorationBuiltIn) {
808 auto structIndex = insn.word(2);
809 assert(structIndex < builtinBlockMembers.size());
810 builtinBlockMembers[structIndex] = insn.word(4);
811 }
812 }
813 }
814 }
815 }
816 }
817
818 return builtinBlockMembers;
819}
820
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600821static std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex(
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600822 SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids) {
Chris Forbes47567b72017-06-09 12:09:45 -0700823 std::vector<std::pair<uint32_t, interface_var>> out;
824
825 for (auto insn : *src) {
826 if (insn.opcode() == spv::OpDecorate) {
827 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
828 auto attachment_index = insn.word(3);
829 auto id = insn.word(1);
830
831 if (accessible_ids.count(id)) {
832 auto def = src->get_def(id);
833 assert(def != src->end());
834
835 if (def.opcode() == spv::OpVariable && insn.word(3) == spv::StorageClassUniformConstant) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600836 auto num_locations = GetLocationsConsumedByType(src, def.word(1), false);
Chris Forbes47567b72017-06-09 12:09:45 -0700837 for (unsigned int offset = 0; offset < num_locations; offset++) {
838 interface_var v = {};
839 v.id = id;
840 v.type_id = def.word(1);
841 v.offset = offset;
842 out.emplace_back(attachment_index + offset, v);
843 }
844 }
845 }
846 }
847 }
848 }
849
850 return out;
851}
852
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600853static bool IsWritableDescriptorType(SHADER_MODULE_STATE const *module, uint32_t type_id, bool is_storage_buffer) {
Chris Forbes8af24522018-03-07 11:37:45 -0800854 auto type = module->get_def(type_id);
855
856 // 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 -0700857 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
858 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray) {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700859 type = module->get_def(type.word(2)); // Element type
Chris Forbes8af24522018-03-07 11:37:45 -0800860 } else {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700861 type = module->get_def(type.word(3)); // Pointee type
Chris Forbes8af24522018-03-07 11:37:45 -0800862 }
863 }
864
865 switch (type.opcode()) {
866 case spv::OpTypeImage: {
867 auto dim = type.word(3);
868 auto sampled = type.word(7);
869 return sampled == 2 && dim != spv::DimSubpassData;
870 }
871
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700872 case spv::OpTypeStruct: {
873 std::unordered_set<unsigned> nonwritable_members;
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800874 if (module->get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Chris Forbes8af24522018-03-07 11:37:45 -0800875 for (auto insn : *module) {
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800876 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1) &&
877 insn.word(3) == spv::DecorationNonWritable) {
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700878 nonwritable_members.insert(insn.word(2));
Chris Forbes8af24522018-03-07 11:37:45 -0800879 }
880 }
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700881
882 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
883 // as nonwritable.
884 return is_storage_buffer && nonwritable_members.size() != type.len() - 2;
885 }
Chris Forbes8af24522018-03-07 11:37:45 -0800886 }
887
888 return false;
889}
890
locke-lunargd9a069d2019-09-17 01:50:19 -0600891std::vector<std::pair<descriptor_slot_t, interface_var>> CollectInterfaceByDescriptorSlot(
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600892 debug_report_data const *report_data, SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids,
Chris Forbes8af24522018-03-07 11:37:45 -0800893 bool *has_writable_descriptor) {
Chris Forbes47567b72017-06-09 12:09:45 -0700894 std::vector<std::pair<descriptor_slot_t, interface_var>> out;
895
896 for (auto id : accessible_ids) {
897 auto insn = src->get_def(id);
898 assert(insn != src->end());
899
900 if (insn.opcode() == spv::OpVariable &&
Chris Forbes9f89d752018-03-07 12:57:48 -0800901 (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant ||
902 insn.word(3) == spv::StorageClassStorageBuffer)) {
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800903 auto d = src->get_decorations(insn.word(2));
904 unsigned set = d.descriptor_set;
905 unsigned binding = d.binding;
Chris Forbes47567b72017-06-09 12:09:45 -0700906
907 interface_var v = {};
908 v.id = insn.word(2);
909 v.type_id = insn.word(1);
910 out.emplace_back(std::make_pair(set, binding), v);
Chris Forbes8af24522018-03-07 11:37:45 -0800911
Chris Forbes8a6d8cb2019-02-14 14:33:08 -0800912 if (!(d.flags & decoration_set::nonwritable_bit) &&
Chris Forbes8d31e5d2018-10-08 17:19:15 -0700913 IsWritableDescriptorType(src, insn.word(1), insn.word(3) == spv::StorageClassStorageBuffer)) {
Chris Forbes8af24522018-03-07 11:37:45 -0800914 *has_writable_descriptor = true;
915 }
Chris Forbes47567b72017-06-09 12:09:45 -0700916 }
917 }
918
919 return out;
920}
921
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600922static bool ValidateViConsistency(debug_report_data const *report_data, VkPipelineVertexInputStateCreateInfo const *vi) {
Chris Forbes47567b72017-06-09 12:09:45 -0700923 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
924 // be specified only once.
925 std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
926 bool skip = false;
927
928 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
929 auto desc = &vi->pVertexBindingDescriptions[i];
930 auto &binding = bindings[desc->binding];
931 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600932 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -0600933 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 -0600934 kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
Chris Forbes47567b72017-06-09 12:09:45 -0700935 desc->binding);
936 } else {
937 binding = desc;
938 }
939 }
940
941 return skip;
942}
943
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600944static bool ValidateViAgainstVsInputs(debug_report_data const *report_data, VkPipelineVertexInputStateCreateInfo const *vi,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600945 SHADER_MODULE_STATE const *vs, spirv_inst_iter entrypoint) {
Chris Forbes47567b72017-06-09 12:09:45 -0700946 bool skip = false;
947
Petr Kraus25810d02019-08-27 17:41:15 +0200948 const auto inputs = CollectInterfaceByLocation(vs, entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700949
950 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200951 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700952 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200953 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
954 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
955 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700956 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
957 }
958 }
959 }
960
Petr Kraus25810d02019-08-27 17:41:15 +0200961 struct AttribInputPair {
962 const VkVertexInputAttributeDescription *attrib = nullptr;
963 const interface_var *input = nullptr;
964 };
965 std::map<uint32_t, AttribInputPair> location_map;
966 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
967 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700968
Petr Kraus25810d02019-08-27 17:41:15 +0200969 for (const auto location_it : location_map) {
970 const auto location = location_it.first;
971 const auto attrib = location_it.second.attrib;
972 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600973
Petr Kraus25810d02019-08-27 17:41:15 +0200974 if (attrib && !input) {
975 skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
976 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed,
977 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
978 } else if (!attrib && input) {
Mark Young4e919b22018-05-21 15:53:59 -0600979 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 -0600980 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
Petr Kraus25810d02019-08-27 17:41:15 +0200981 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
982 } else if (attrib && input) {
983 const auto attrib_type = GetFormatType(attrib->format);
984 const auto input_type = GetFundamentalType(vs, input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700985
986 // Type checking
987 if (!(attrib_type & input_type)) {
Mark Young4e919b22018-05-21 15:53:59 -0600988 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 -0600989 HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Petr Kraus25810d02019-08-27 17:41:15 +0200990 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
991 string_VkFormat(attrib->format), location, DescribeType(vs, input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700992 }
Petr Kraus25810d02019-08-27 17:41:15 +0200993 } else { // !attrib && !input
994 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700995 }
996 }
997
998 return skip;
999}
1000
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001001static bool ValidateFsOutputsAgainstRenderPass(debug_report_data const *report_data, SHADER_MODULE_STATE const *fs,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001002 spirv_inst_iter entrypoint, PIPELINE_STATE const *pipeline, uint32_t subpass_index) {
Petr Kraus25810d02019-08-27 17:41:15 +02001003 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -07001004
Petr Kraus25810d02019-08-27 17:41:15 +02001005 const auto rpci = pipeline->rp_state->createInfo.ptr();
1006
Jeremy Hayes3699c7c2019-10-09 12:24:55 -06001007 struct Attachment {
1008 const VkAttachmentReference2KHR *reference = nullptr;
1009 const VkAttachmentDescription2KHR *attachment = nullptr;
1010 const interface_var *output = nullptr;
1011 };
1012 std::map<uint32_t, Attachment> location_map;
1013
Petr Kraus25810d02019-08-27 17:41:15 +02001014 const auto subpass = rpci->pSubpasses[subpass_index];
1015 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -06001016 auto const &reference = subpass.pColorAttachments[i];
1017 location_map[i].reference = &reference;
1018 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
1019 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
1020 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
Chris Forbes47567b72017-06-09 12:09:45 -07001021 }
1022 }
1023
Chris Forbes47567b72017-06-09 12:09:45 -07001024 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
1025
Petr Kraus25810d02019-08-27 17:41:15 +02001026 const auto outputs = CollectInterfaceByLocation(fs, entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -06001027 for (const auto &output_it : outputs) {
1028 auto const location = output_it.first.first;
1029 location_map[location].output = &output_it.second;
1030 }
Chris Forbes47567b72017-06-09 12:09:45 -07001031
Petr Kraus25810d02019-08-27 17:41:15 +02001032 const bool alphaToCoverageEnabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL &&
1033 pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -07001034
Petr Kraus25810d02019-08-27 17:41:15 +02001035 for (const auto location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -06001036 const auto reference = location_it.second.reference;
1037 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
1038 continue;
1039 }
1040
Petr Kraus25810d02019-08-27 17:41:15 +02001041 const auto location = location_it.first;
1042 const auto attachment = location_it.second.attachment;
1043 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +02001044 if (attachment && !output) {
1045 if (pipeline->attachments[location].colorWriteMask != 0) {
1046 skip |=
1047 log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1048 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
1049 "Attachment %" PRIu32 " not written by fragment shader; undefined values will be written to attachment",
1050 location);
1051 }
1052 } else if (!attachment && output) {
1053 if (!(alphaToCoverageEnabled && location == 0)) {
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001054 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_OutputNotConsumed,
Petr Kraus25810d02019-08-27 17:41:15 +02001056 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001057 }
Petr Kraus25810d02019-08-27 17:41:15 +02001058 } else if (attachment && output) {
1059 const auto attachment_type = GetFormatType(attachment->format);
1060 const auto output_type = GetFundamentalType(fs, output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -07001061
1062 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +02001063 if (!(output_type & attachment_type)) {
1064 skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1065 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
1066 "Attachment %" PRIu32
1067 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
1068 location, string_VkFormat(attachment->format), DescribeType(fs, output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001069 }
Petr Kraus25810d02019-08-27 17:41:15 +02001070 } else { // !attachment && !output
1071 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -07001072 }
1073 }
1074
Petr Kraus25810d02019-08-27 17:41:15 +02001075 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
1076 bool locationZeroHasAlpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
1077 GetComponentsConsumedByType(fs, output_zero->type_id, false) == 4;
Ari Suonpaa412b23b2019-02-26 07:56:58 +02001078 if (alphaToCoverageEnabled && !locationZeroHasAlpha) {
1079 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
1080 HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
1081 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
1082 }
1083
Chris Forbes47567b72017-06-09 12:09:45 -07001084 return skip;
1085}
1086
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001087// For PointSize analysis we need to know if the variable decorated with the PointSize built-in was actually written to.
1088// This function examines instructions in the static call tree for a write to this variable.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001089static bool IsPointSizeWritten(SHADER_MODULE_STATE const *src, spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001090 auto type = builtin_instr.opcode();
1091 uint32_t target_id = builtin_instr.word(1);
1092 bool init_complete = false;
1093
1094 if (type == spv::OpMemberDecorate) {
1095 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1096 auto insn = entrypoint;
1097 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1098 switch (insn.opcode()) {
1099 case spv::OpTypePointer:
1100 if ((insn.word(3) == target_id) && (insn.word(2) == spv::StorageClassOutput)) {
1101 target_id = insn.word(1);
1102 }
1103 break;
1104 case spv::OpVariable:
1105 if (insn.word(1) == target_id) {
1106 target_id = insn.word(2);
1107 init_complete = true;
1108 }
1109 break;
1110 }
1111 insn++;
1112 }
1113 }
1114
Mark Lobodzinskif84b0b42018-09-11 14:54:32 -06001115 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1116
1117 bool found_write = false;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001118 std::unordered_set<uint32_t> worklist;
1119 worklist.insert(entrypoint.word(2));
1120
1121 // Follow instructions in call graph looking for writes to target
1122 while (!worklist.empty() && !found_write) {
1123 auto id_iter = worklist.begin();
1124 auto id = *id_iter;
1125 worklist.erase(id_iter);
1126
1127 auto insn = src->get_def(id);
1128 if (insn == src->end()) {
1129 continue;
1130 }
1131
1132 if (insn.opcode() == spv::OpFunction) {
1133 // Scan body of function looking for other function calls or items in our ID chain
1134 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
1135 switch (insn.opcode()) {
1136 case spv::OpAccessChain:
1137 if (insn.word(3) == target_id) {
1138 if (type == spv::OpMemberDecorate) {
1139 auto value = GetConstantValue(src, insn.word(4));
1140 if (value == builtin_instr.word(2)) {
1141 target_id = insn.word(2);
1142 }
1143 } else {
1144 target_id = insn.word(2);
1145 }
1146 }
1147 break;
1148 case spv::OpStore:
1149 if (insn.word(1) == target_id) {
1150 found_write = true;
1151 }
1152 break;
1153 case spv::OpFunctionCall:
1154 worklist.insert(insn.word(3));
1155 break;
1156 }
1157 }
1158 }
1159 }
1160 return found_write;
1161}
1162
Chris Forbes47567b72017-06-09 12:09:45 -07001163// For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is
1164// important for identifying the set of shader resources actually used by an entrypoint, for example.
1165// Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
1166// - NOT the shader input/output interfaces.
1167//
1168// TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
1169// converting parts of this to be generated from the machine-readable spec instead.
locke-lunargd9a069d2019-09-17 01:50:19 -06001170std::unordered_set<uint32_t> MarkAccessibleIds(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) {
Chris Forbes47567b72017-06-09 12:09:45 -07001171 std::unordered_set<uint32_t> ids;
1172 std::unordered_set<uint32_t> worklist;
1173 worklist.insert(entrypoint.word(2));
1174
1175 while (!worklist.empty()) {
1176 auto id_iter = worklist.begin();
1177 auto id = *id_iter;
1178 worklist.erase(id_iter);
1179
1180 auto insn = src->get_def(id);
1181 if (insn == src->end()) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001182 // 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 -07001183 // that we may not care about.
1184 continue;
1185 }
1186
1187 // Try to add to the output set
1188 if (!ids.insert(id).second) {
1189 continue; // If we already saw this id, we don't want to walk it again.
1190 }
1191
1192 switch (insn.opcode()) {
1193 case spv::OpFunction:
1194 // Scan whole body of the function, enlisting anything interesting
1195 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
1196 switch (insn.opcode()) {
1197 case spv::OpLoad:
1198 case spv::OpAtomicLoad:
1199 case spv::OpAtomicExchange:
1200 case spv::OpAtomicCompareExchange:
1201 case spv::OpAtomicCompareExchangeWeak:
1202 case spv::OpAtomicIIncrement:
1203 case spv::OpAtomicIDecrement:
1204 case spv::OpAtomicIAdd:
1205 case spv::OpAtomicISub:
1206 case spv::OpAtomicSMin:
1207 case spv::OpAtomicUMin:
1208 case spv::OpAtomicSMax:
1209 case spv::OpAtomicUMax:
1210 case spv::OpAtomicAnd:
1211 case spv::OpAtomicOr:
1212 case spv::OpAtomicXor:
1213 worklist.insert(insn.word(3)); // ptr
1214 break;
1215 case spv::OpStore:
1216 case spv::OpAtomicStore:
1217 worklist.insert(insn.word(1)); // ptr
1218 break;
1219 case spv::OpAccessChain:
1220 case spv::OpInBoundsAccessChain:
1221 worklist.insert(insn.word(3)); // base ptr
1222 break;
1223 case spv::OpSampledImage:
1224 case spv::OpImageSampleImplicitLod:
1225 case spv::OpImageSampleExplicitLod:
1226 case spv::OpImageSampleDrefImplicitLod:
1227 case spv::OpImageSampleDrefExplicitLod:
1228 case spv::OpImageSampleProjImplicitLod:
1229 case spv::OpImageSampleProjExplicitLod:
1230 case spv::OpImageSampleProjDrefImplicitLod:
1231 case spv::OpImageSampleProjDrefExplicitLod:
1232 case spv::OpImageFetch:
1233 case spv::OpImageGather:
1234 case spv::OpImageDrefGather:
1235 case spv::OpImageRead:
1236 case spv::OpImage:
1237 case spv::OpImageQueryFormat:
1238 case spv::OpImageQueryOrder:
1239 case spv::OpImageQuerySizeLod:
1240 case spv::OpImageQuerySize:
1241 case spv::OpImageQueryLod:
1242 case spv::OpImageQueryLevels:
1243 case spv::OpImageQuerySamples:
1244 case spv::OpImageSparseSampleImplicitLod:
1245 case spv::OpImageSparseSampleExplicitLod:
1246 case spv::OpImageSparseSampleDrefImplicitLod:
1247 case spv::OpImageSparseSampleDrefExplicitLod:
1248 case spv::OpImageSparseSampleProjImplicitLod:
1249 case spv::OpImageSparseSampleProjExplicitLod:
1250 case spv::OpImageSparseSampleProjDrefImplicitLod:
1251 case spv::OpImageSparseSampleProjDrefExplicitLod:
1252 case spv::OpImageSparseFetch:
1253 case spv::OpImageSparseGather:
1254 case spv::OpImageSparseDrefGather:
1255 case spv::OpImageTexelPointer:
1256 worklist.insert(insn.word(3)); // Image or sampled image
1257 break;
1258 case spv::OpImageWrite:
1259 worklist.insert(insn.word(1)); // Image -- different operand order to above
1260 break;
1261 case spv::OpFunctionCall:
1262 for (uint32_t i = 3; i < insn.len(); i++) {
1263 worklist.insert(insn.word(i)); // fn itself, and all args
1264 }
1265 break;
1266
1267 case spv::OpExtInst:
1268 for (uint32_t i = 5; i < insn.len(); i++) {
1269 worklist.insert(insn.word(i)); // Operands to ext inst
1270 }
1271 break;
1272 }
1273 }
1274 break;
1275 }
1276 }
1277
1278 return ids;
1279}
1280
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001281static bool ValidatePushConstantBlockAgainstPipeline(debug_report_data const *report_data,
1282 std::vector<VkPushConstantRange> const *push_constant_ranges,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001283 SHADER_MODULE_STATE const *src, spirv_inst_iter type,
1284 VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -07001285 bool skip = false;
1286
1287 // Strip off ptrs etc
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001288 type = GetStructType(src, type, false);
Chris Forbes47567b72017-06-09 12:09:45 -07001289 assert(type != src->end());
1290
1291 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
1292 // TODO: arrays, matrices, weird sizes
1293 for (auto insn : *src) {
1294 if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) {
1295 if (insn.word(3) == spv::DecorationOffset) {
1296 unsigned offset = insn.word(4);
1297 auto size = 4; // Bytes; TODO: calculate this based on the type
1298
1299 bool found_range = false;
1300 for (auto const &range : *push_constant_ranges) {
Jeremy Hayese883b362019-12-10 15:12:26 -07001301 if ((range.offset <= offset) && ((range.offset + range.size) >= (offset + size)) &&
1302 (range.stageFlags & stage)) {
Chris Forbes47567b72017-06-09 12:09:45 -07001303 found_range = true;
1304
Chris Forbes47567b72017-06-09 12:09:45 -07001305 break;
1306 }
1307 }
1308
1309 if (!found_range) {
1310 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 -06001311 kVUID_Core_Shader_PushConstantOutOfRange,
Dave Houltona9df0ce2018-02-07 10:51:23 -07001312 "Push constant range covering variable starting at offset %u not declared in layout", offset);
Chris Forbes47567b72017-06-09 12:09:45 -07001313 }
1314 }
1315 }
1316 }
1317
1318 return skip;
1319}
1320
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001321static bool ValidatePushConstantUsage(debug_report_data const *report_data,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001322 std::vector<VkPushConstantRange> const *push_constant_ranges, SHADER_MODULE_STATE const *src,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001323 std::unordered_set<uint32_t> accessible_ids, VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -07001324 bool skip = false;
1325
1326 for (auto id : accessible_ids) {
1327 auto def_insn = src->get_def(id);
1328 if (def_insn.opcode() == spv::OpVariable && def_insn.word(3) == spv::StorageClassPushConstant) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001329 skip |= ValidatePushConstantBlockAgainstPipeline(report_data, push_constant_ranges, src, src->get_def(def_insn.word(1)),
1330 stage);
Chris Forbes47567b72017-06-09 12:09:45 -07001331 }
1332 }
1333
1334 return skip;
1335}
1336
1337// Validate that data for each specialization entry is fully contained within the buffer.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001338static bool ValidateSpecializationOffsets(debug_report_data const *report_data, VkPipelineShaderStageCreateInfo const *info) {
Chris Forbes47567b72017-06-09 12:09:45 -07001339 bool skip = false;
1340
1341 VkSpecializationInfo const *spec = info->pSpecializationInfo;
1342
1343 if (spec) {
1344 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -06001345 if (spec->pMapEntries[i].offset >= spec->dataSize) {
1346 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0,
1347 "VUID-VkSpecializationInfo-offset-00773",
1348 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
1349 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..",
1350 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
1351 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
1352
1353 continue;
1354 }
Chris Forbes47567b72017-06-09 12:09:45 -07001355 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001356 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 -06001357 "VUID-VkSpecializationInfo-pMapEntries-00774",
Dave Houltona9df0ce2018-02-07 10:51:23 -07001358 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001359 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..",
Dave Houltona9df0ce2018-02-07 10:51:23 -07001360 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
Mark Lobodzinski487a0d12018-03-30 10:09:03 -06001361 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -07001362 }
1363 }
1364 }
1365
1366 return skip;
1367}
1368
Jeff Bolz38b3ce72018-09-19 12:53:38 -05001369// TODO (jbolz): Can this return a const reference?
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001370static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count) {
Chris Forbes47567b72017-06-09 12:09:45 -07001371 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -08001372 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -07001373 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -05001374 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001375
1376 // 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 -05001377 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
1378 if (type.opcode() == spv::OpTypeRuntimeArray) {
1379 descriptor_count = 0;
1380 type = module->get_def(type.word(2));
1381 } else if (type.opcode() == spv::OpTypeArray) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001382 descriptor_count *= GetConstantValue(module, type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -07001383 type = module->get_def(type.word(2));
1384 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -08001385 if (type.word(2) == spv::StorageClassStorageBuffer) {
1386 is_storage_buffer = true;
1387 }
Chris Forbes47567b72017-06-09 12:09:45 -07001388 type = module->get_def(type.word(3));
1389 }
1390 }
1391
1392 switch (type.opcode()) {
1393 case spv::OpTypeStruct: {
1394 for (auto insn : *module) {
1395 if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) {
1396 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -08001397 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001398 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1399 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
1400 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -08001401 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001402 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
1403 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
1404 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
1405 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -08001406 }
Chris Forbes47567b72017-06-09 12:09:45 -07001407 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001408 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
1409 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
1410 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001411 }
1412 }
1413 }
1414
1415 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -05001416 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001417 }
1418
1419 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -05001420 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
1421 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1422 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001423
Chris Forbes73c00bf2018-06-22 16:28:06 -07001424 case spv::OpTypeSampledImage: {
1425 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
1426 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
1427 auto image_type = module->get_def(type.word(2));
1428 auto dim = image_type.word(3);
1429 auto sampled = image_type.word(7);
1430 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001431 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
1432 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001433 }
Chris Forbes73c00bf2018-06-22 16:28:06 -07001434 }
Jeff Bolze54ae892018-09-08 12:16:29 -05001435 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1436 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001437
1438 case spv::OpTypeImage: {
1439 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
1440 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
1441 auto dim = type.word(3);
1442 auto sampled = type.word(7);
1443
1444 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001445 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
1446 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001447 } else if (dim == spv::DimBuffer) {
1448 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001449 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
1450 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001451 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001452 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
1453 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001454 }
1455 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -05001456 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
1457 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
1458 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001459 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -05001460 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
1461 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001462 }
1463 }
Shannon McPherson0fa28232018-11-01 11:59:02 -06001464 case spv::OpTypeAccelerationStructureNV:
Eric Werness30127fd2018-10-31 21:01:03 -07001465 ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -05001466 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -07001467
1468 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
1469 default:
Jeff Bolze54ae892018-09-08 12:16:29 -05001470 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -07001471 }
1472}
1473
Jeff Bolze54ae892018-09-08 12:16:29 -05001474static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -07001475 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -05001476 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
1477 if (ss.tellp()) ss << ", ";
1478 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -07001479 }
1480 return ss.str();
1481}
1482
Jeff Bolzee743412019-06-20 22:24:32 -05001483static bool RequirePropertyFlag(debug_report_data const *report_data, VkBool32 check, char const *flag, char const *structure) {
1484 if (!check) {
1485 if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
1486 kVUID_Core_Shader_ExceedDeviceLimit, "Shader requires flag %s set in %s but it is not set on the device", flag,
1487 structure)) {
1488 return true;
1489 }
1490 }
1491
1492 return false;
1493}
1494
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001495static bool RequireFeature(debug_report_data const *report_data, VkBool32 feature, char const *feature_name) {
Chris Forbes47567b72017-06-09 12:09:45 -07001496 if (!feature) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001497 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 -06001498 kVUID_Core_Shader_FeatureNotEnabled, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -07001499 return true;
1500 }
1501 }
1502
1503 return false;
1504}
1505
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001506static bool RequireExtension(debug_report_data const *report_data, bool extension, char const *extension_name) {
Chris Forbes47567b72017-06-09 12:09:45 -07001507 if (!extension) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06001508 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 -06001509 kVUID_Core_Shader_FeatureNotEnabled, "Shader requires extension %s but is not enabled on the device",
Chris Forbes47567b72017-06-09 12:09:45 -07001510 extension_name)) {
1511 return true;
1512 }
1513 }
1514
1515 return false;
1516}
1517
John Zulaufac4c6e12019-07-01 16:05:58 -06001518bool CoreChecks::ValidateShaderCapabilities(SHADER_MODULE_STATE const *src, VkShaderStageFlagBits stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07001519 bool skip = false;
1520
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001521 struct FeaturePointer {
1522 // Callable object to test if this feature is enabled in the given aggregate feature struct
1523 const std::function<VkBool32(const DeviceFeatures &)> IsEnabled;
1524
1525 // Test if feature pointer is populated
1526 explicit operator bool() const { return static_cast<bool>(IsEnabled); }
1527
1528 // Default and nullptr constructor to create an empty FeaturePointer
1529 FeaturePointer() : IsEnabled(nullptr) {}
1530 FeaturePointer(std::nullptr_t ptr) : IsEnabled(nullptr) {}
1531
1532 // Constructors to populate FeaturePointer based on given pointer to member
1533 FeaturePointer(VkBool32 VkPhysicalDeviceFeatures::*ptr)
1534 : IsEnabled([=](const DeviceFeatures &features) { return features.core.*ptr; }) {}
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001535 FeaturePointer(VkBool32 VkPhysicalDeviceVulkan11Features::*ptr)
1536 : IsEnabled([=](const DeviceFeatures &features) { return features.core11.*ptr; }) {}
1537 FeaturePointer(VkBool32 VkPhysicalDeviceVulkan12Features::*ptr)
1538 : IsEnabled([=](const DeviceFeatures &features) { return features.core12.*ptr; }) {}
Brett Lawsonbebfb6f2018-10-23 16:58:50 -07001539 FeaturePointer(VkBool32 VkPhysicalDeviceTransformFeedbackFeaturesEXT::*ptr)
1540 : IsEnabled([=](const DeviceFeatures &features) { return features.transform_feedback_features.*ptr; }) {}
Jeff Bolze4356752019-03-07 11:23:46 -06001541 FeaturePointer(VkBool32 VkPhysicalDeviceCooperativeMatrixFeaturesNV::*ptr)
1542 : IsEnabled([=](const DeviceFeatures &features) { return features.cooperative_matrix_features.*ptr; }) {}
Jason Macnakc5a621d2019-06-10 12:42:50 -07001543 FeaturePointer(VkBool32 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::*ptr)
1544 : IsEnabled([=](const DeviceFeatures &features) { return features.compute_shader_derivatives_features.*ptr; }) {}
Jason Macnak325e8b52019-06-10 13:33:10 -07001545 FeaturePointer(VkBool32 VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::*ptr)
1546 : IsEnabled([=](const DeviceFeatures &features) { return features.fragment_shader_barycentric_features.*ptr; }) {}
Jason Macnakd7fddf82019-06-13 09:52:49 -07001547 FeaturePointer(VkBool32 VkPhysicalDeviceShaderImageFootprintFeaturesNV::*ptr)
1548 : IsEnabled([=](const DeviceFeatures &features) { return features.shader_image_footprint_features.*ptr; }) {}
Jeff Bolz38f6cb52019-06-30 16:26:44 -05001549 FeaturePointer(VkBool32 VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::*ptr)
1550 : IsEnabled([=](const DeviceFeatures &features) { return features.fragment_shader_interlock_features.*ptr; }) {}
Jeff Bolza38fd3b2019-07-21 11:42:11 -05001551 FeaturePointer(VkBool32 VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::*ptr)
1552 : IsEnabled([=](const DeviceFeatures &features) { return features.demote_to_helper_invocation_features.*ptr; }) {}
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001553 };
1554
Chris Forbes47567b72017-06-09 12:09:45 -07001555 struct CapabilityInfo {
1556 char const *name;
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001557 FeaturePointer feature;
Tony-LunarG2ec96bb2019-11-26 13:43:02 -07001558 ExtEnabled DeviceExtensions::*extension;
Chris Forbes47567b72017-06-09 12:09:45 -07001559 };
1560
Chris Forbes47567b72017-06-09 12:09:45 -07001561 // clang-format off
Dave Houltoneb10ea82017-12-22 12:21:50 -07001562 static const std::unordered_multimap<uint32_t, CapabilityInfo> capabilities = {
Chris Forbes47567b72017-06-09 12:09:45 -07001563 // Capabilities always supported by a Vulkan 1.0 implementation -- no
1564 // feature bits.
1565 {spv::CapabilityMatrix, {nullptr}},
1566 {spv::CapabilityShader, {nullptr}},
1567 {spv::CapabilityInputAttachment, {nullptr}},
1568 {spv::CapabilitySampled1D, {nullptr}},
1569 {spv::CapabilityImage1D, {nullptr}},
1570 {spv::CapabilitySampledBuffer, {nullptr}},
Toni Merilehtib13a4a22019-05-21 12:58:44 +03001571 {spv::CapabilityStorageImageExtendedFormats, {nullptr}},
Chris Forbes47567b72017-06-09 12:09:45 -07001572 {spv::CapabilityImageQuery, {nullptr}},
1573 {spv::CapabilityDerivativeControl, {nullptr}},
1574
1575 // Capabilities that are optionally supported, but require a feature to
1576 // be enabled on the device
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001577 {spv::CapabilityGeometry, {"VkPhysicalDeviceFeatures::geometryShader", &VkPhysicalDeviceFeatures::geometryShader}},
1578 {spv::CapabilityTessellation, {"VkPhysicalDeviceFeatures::tessellationShader", &VkPhysicalDeviceFeatures::tessellationShader}},
1579 {spv::CapabilityFloat64, {"VkPhysicalDeviceFeatures::shaderFloat64", &VkPhysicalDeviceFeatures::shaderFloat64}},
1580 {spv::CapabilityInt64, {"VkPhysicalDeviceFeatures::shaderInt64", &VkPhysicalDeviceFeatures::shaderInt64}},
1581 {spv::CapabilityTessellationPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}},
1582 {spv::CapabilityGeometryPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}},
1583 {spv::CapabilityImageGatherExtended, {"VkPhysicalDeviceFeatures::shaderImageGatherExtended", &VkPhysicalDeviceFeatures::shaderImageGatherExtended}},
1584 {spv::CapabilityStorageImageMultisample, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}},
1585 {spv::CapabilityUniformBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing}},
1586 {spv::CapabilitySampledImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing}},
1587 {spv::CapabilityStorageBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}},
1588 {spv::CapabilityStorageImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}},
1589 {spv::CapabilityClipDistance, {"VkPhysicalDeviceFeatures::shaderClipDistance", &VkPhysicalDeviceFeatures::shaderClipDistance}},
1590 {spv::CapabilityCullDistance, {"VkPhysicalDeviceFeatures::shaderCullDistance", &VkPhysicalDeviceFeatures::shaderCullDistance}},
1591 {spv::CapabilityImageCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}},
1592 {spv::CapabilitySampleRateShading, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}},
1593 {spv::CapabilitySparseResidency, {"VkPhysicalDeviceFeatures::shaderResourceResidency", &VkPhysicalDeviceFeatures::shaderResourceResidency}},
1594 {spv::CapabilityMinLod, {"VkPhysicalDeviceFeatures::shaderResourceMinLod", &VkPhysicalDeviceFeatures::shaderResourceMinLod}},
1595 {spv::CapabilitySampledCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}},
1596 {spv::CapabilityImageMSArray, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}},
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001597 {spv::CapabilityInterpolationFunction, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}},
1598 {spv::CapabilityStorageImageReadWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat}},
1599 {spv::CapabilityStorageImageWriteWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat}},
1600 {spv::CapabilityMultiViewport, {"VkPhysicalDeviceFeatures::multiViewport", &VkPhysicalDeviceFeatures::multiViewport}},
Jeff Bolzfdf96072018-04-10 14:32:18 -05001601
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001602 {spv::CapabilityShaderNonUniformEXT, {VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_descriptor_indexing}},
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001603 {spv::CapabilityRuntimeDescriptorArrayEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::runtimeDescriptorArray", &VkPhysicalDeviceVulkan12Features::runtimeDescriptorArray}},
1604 {spv::CapabilityInputAttachmentArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderInputAttachmentArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderInputAttachmentArrayDynamicIndexing}},
1605 {spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformTexelBufferArrayDynamicIndexing}},
1606 {spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageTexelBufferArrayDynamicIndexing}},
1607 {spv::CapabilityUniformBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformBufferArrayNonUniformIndexing}},
1608 {spv::CapabilitySampledImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderSampledImageArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderSampledImageArrayNonUniformIndexing}},
1609 {spv::CapabilityStorageBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageBufferArrayNonUniformIndexing}},
1610 {spv::CapabilityStorageImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageImageArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageImageArrayNonUniformIndexing}},
1611 {spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderInputAttachmentArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderInputAttachmentArrayNonUniformIndexing}},
1612 {spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformTexelBufferArrayNonUniformIndexing}},
1613 {spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageTexelBufferArrayNonUniformIndexing}},
Chris Forbes47567b72017-06-09 12:09:45 -07001614
1615 // Capabilities that require an extension
Mike Schuchardt8ed5ea02018-07-20 18:24:17 -06001616 {spv::CapabilityDrawParameters, {VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_draw_parameters}},
1617 {spv::CapabilityGeometryShaderPassthroughNV, {VK_NV_GEOMETRY_SHADER_PASSTHROUGH_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_geometry_shader_passthrough}},
1618 {spv::CapabilitySampleMaskOverrideCoverageNV, {VK_NV_SAMPLE_MASK_OVERRIDE_COVERAGE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_sample_mask_override_coverage}},
1619 {spv::CapabilityShaderViewportIndexLayerEXT, {VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_viewport_index_layer}},
1620 {spv::CapabilityShaderViewportIndexLayerNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}},
1621 {spv::CapabilityShaderViewportMaskNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}},
1622 {spv::CapabilitySubgroupBallotKHR, {VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_ballot }},
1623 {spv::CapabilitySubgroupVoteKHR, {VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_vote }},
Jason Macnakb7d091c2019-06-10 11:13:11 -07001624 {spv::CapabilityGroupNonUniformPartitionedNV, {VK_NV_SHADER_SUBGROUP_PARTITIONED_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_shader_subgroup_partitioned}},
aqnuep7033c702018-09-11 18:03:29 +02001625 {spv::CapabilityInt64Atomics, {VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_atomic_int64 }},
amhaganfa0b34d2019-10-15 16:03:53 -04001626 {spv::CapabilityShaderClockKHR, {VK_KHR_SHADER_CLOCK_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_clock }},
Alexander Galazin3bd8e342018-06-14 15:49:07 +02001627
Jason Macnakc5a621d2019-06-10 12:42:50 -07001628 {spv::CapabilityComputeDerivativeGroupQuadsNV, {"VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupQuads", &VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupQuads, &DeviceExtensions::vk_nv_compute_shader_derivatives}},
1629 {spv::CapabilityComputeDerivativeGroupLinearNV, {"VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupLinear", &VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupLinear, &DeviceExtensions::vk_nv_compute_shader_derivatives}},
Jason Macnakf7019582019-06-13 10:07:26 -07001630 {spv::CapabilityFragmentBarycentricNV, {"VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::fragmentShaderBarycentric", &VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::fragmentShaderBarycentric, &DeviceExtensions::vk_nv_fragment_shader_barycentric}},
Jason Macnakc5a621d2019-06-10 12:42:50 -07001631
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001632 {spv::CapabilityStorageBuffer8BitAccess, {"VkPhysicalDevice8BitStorageFeaturesKHR::storageBuffer8BitAccess", &VkPhysicalDeviceVulkan12Features::storageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}},
1633 {spv::CapabilityUniformAndStorageBuffer8BitAccess, {"VkPhysicalDevice8BitStorageFeaturesKHR::uniformAndStorageBuffer8BitAccess", &VkPhysicalDeviceVulkan12Features::uniformAndStorageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}},
1634 {spv::CapabilityStoragePushConstant8, {"VkPhysicalDevice8BitStorageFeaturesKHR::storagePushConstant8", &VkPhysicalDeviceVulkan12Features::storagePushConstant8, &DeviceExtensions::vk_khr_8bit_storage}},
Brett Lawsonbebfb6f2018-10-23 16:58:50 -07001635
Jason Macnakf7019582019-06-13 10:07:26 -07001636 {spv::CapabilityTransformFeedback, { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback, &DeviceExtensions::vk_ext_transform_feedback}},
1637 {spv::CapabilityGeometryStreams, { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams, &DeviceExtensions::vk_ext_transform_feedback}},
Jose-Emilio Munoz-Lopez1109b452018-08-21 09:44:07 +01001638
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001639 {spv::CapabilityFloat16, {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderFloat16", &VkPhysicalDeviceVulkan12Features::shaderFloat16, &DeviceExtensions::vk_khr_shader_float16_int8}},
1640 {spv::CapabilityInt8, {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderInt8", &VkPhysicalDeviceVulkan12Features::shaderInt8, &DeviceExtensions::vk_khr_shader_float16_int8}},
Jeff Bolze4356752019-03-07 11:23:46 -06001641
Jason Macnakd7fddf82019-06-13 09:52:49 -07001642 {spv::CapabilityImageFootprintNV, {"VkPhysicalDeviceShaderImageFootprintFeaturesNV::imageFootprint", &VkPhysicalDeviceShaderImageFootprintFeaturesNV::imageFootprint, &DeviceExtensions::vk_nv_shader_image_footprint}},
1643
Jeff Bolze4356752019-03-07 11:23:46 -06001644 {spv::CapabilityCooperativeMatrixNV, {"VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix", &VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix, &DeviceExtensions::vk_nv_cooperative_matrix}},
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001645
Graeme Leese41e6b842019-08-02 10:49:14 +01001646 {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserve", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
1647 {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserve", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
1648 {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZero", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
1649 {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTE", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
1650 {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZ", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}},
Jeff Bolz38f6cb52019-06-30 16:26:44 -05001651
1652 {spv::CapabilityFragmentShaderSampleInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderSampleInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderSampleInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
1653 {spv::CapabilityFragmentShaderPixelInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderPixelInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderPixelInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
1654 {spv::CapabilityFragmentShaderShadingRateInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderShadingRateInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderShadingRateInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}},
Jeff Bolza38fd3b2019-07-21 11:42:11 -05001655 {spv::CapabilityDemoteToHelperInvocationEXT, {"VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::shaderDemoteToHelperInvocation", &VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::shaderDemoteToHelperInvocation, &DeviceExtensions::vk_ext_shader_demote_to_helper_invocation}},
Jeff Bolz4563f2a2019-12-10 13:30:30 -06001656
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001657 {spv::CapabilityPhysicalStorageBufferAddresses, {"VkPhysicalDeviceBufferDeviceAddressFeatures::bufferDeviceAddress", &VkPhysicalDeviceVulkan12Features::bufferDeviceAddress, &DeviceExtensions::vk_ext_buffer_device_address}},
Jeff Bolz4563f2a2019-12-10 13:30:30 -06001658 // 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 -07001659 {spv::CapabilityPhysicalStorageBufferAddressesEXT, {"VkPhysicalDeviceBufferDeviceAddressFeaturesEXT::bufferDeviceAddress", &VkPhysicalDeviceVulkan12Features::bufferDeviceAddress, &DeviceExtensions::vk_khr_buffer_device_address}},
Chris Forbes47567b72017-06-09 12:09:45 -07001660 };
1661 // clang-format on
1662
1663 for (auto insn : *src) {
1664 if (insn.opcode() == spv::OpCapability) {
Dave Houltoneb10ea82017-12-22 12:21:50 -07001665 size_t n = capabilities.count(insn.word(1));
1666 if (1 == n) { // key occurs exactly once
1667 auto it = capabilities.find(insn.word(1));
1668 if (it != capabilities.end()) {
1669 if (it->second.feature) {
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001670 skip |= RequireFeature(report_data, it->second.feature.IsEnabled(enabled_features), it->second.name);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001671 }
1672 if (it->second.extension) {
Tony-LunarG2ec96bb2019-11-26 13:43:02 -07001673 skip |= RequireExtension(report_data, IsExtEnabled((device_extensions.*(it->second.extension))),
1674 it->second.name);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001675 }
Chris Forbes47567b72017-06-09 12:09:45 -07001676 }
Dave Houltoneb10ea82017-12-22 12:21:50 -07001677 } else if (1 < n) { // key occurs multiple times, at least one must be enabled
1678 bool needs_feature = false, has_feature = false;
1679 bool needs_ext = false, has_ext = false;
1680 std::string feature_names = "(one of) [ ";
1681 std::string extension_names = feature_names;
1682 auto caps = capabilities.equal_range(insn.word(1));
1683 for (auto it = caps.first; it != caps.second; ++it) {
1684 if (it->second.feature) {
1685 needs_feature = true;
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001686 has_feature = has_feature || it->second.feature.IsEnabled(enabled_features);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001687 feature_names += it->second.name;
1688 feature_names += " ";
1689 }
1690 if (it->second.extension) {
1691 needs_ext = true;
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06001692 has_ext = has_ext || device_extensions.*(it->second.extension);
Dave Houltoneb10ea82017-12-22 12:21:50 -07001693 extension_names += it->second.name;
1694 extension_names += " ";
1695 }
1696 }
1697 if (needs_feature) {
1698 feature_names += "]";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001699 skip |= RequireFeature(report_data, has_feature, feature_names.c_str());
Dave Houltoneb10ea82017-12-22 12:21:50 -07001700 }
1701 if (needs_ext) {
1702 extension_names += "]";
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001703 skip |= RequireExtension(report_data, has_ext, extension_names.c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001704 }
Graeme Leesec82dbe02019-08-02 10:44:21 +01001705 }
1706
1707 { // Do group non-uniform checks
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001708 const VkSubgroupFeatureFlags supportedOperations = phys_dev_props_core11.subgroupSupportedOperations;
1709 const VkSubgroupFeatureFlags supportedStages = phys_dev_props_core11.subgroupSupportedStages;
Jeff Bolzee743412019-06-20 22:24:32 -05001710
1711 switch (insn.word(1)) {
1712 default:
1713 break;
1714 case spv::CapabilityGroupNonUniform:
1715 case spv::CapabilityGroupNonUniformVote:
1716 case spv::CapabilityGroupNonUniformArithmetic:
1717 case spv::CapabilityGroupNonUniformBallot:
1718 case spv::CapabilityGroupNonUniformShuffle:
1719 case spv::CapabilityGroupNonUniformShuffleRelative:
1720 case spv::CapabilityGroupNonUniformClustered:
1721 case spv::CapabilityGroupNonUniformQuad:
1722 case spv::CapabilityGroupNonUniformPartitionedNV:
1723 RequirePropertyFlag(report_data, supportedStages & stage, string_VkShaderStageFlagBits(stage),
1724 "VkPhysicalDeviceSubgroupProperties::supportedStages");
1725 break;
1726 }
1727
1728 switch (insn.word(1)) {
1729 default:
1730 break;
1731 case spv::CapabilityGroupNonUniform:
1732 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT,
1733 "VK_SUBGROUP_FEATURE_BASIC_BIT",
1734 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1735 break;
1736 case spv::CapabilityGroupNonUniformVote:
1737 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT,
1738 "VK_SUBGROUP_FEATURE_VOTE_BIT",
1739 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1740 break;
1741 case spv::CapabilityGroupNonUniformArithmetic:
1742 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT,
1743 "VK_SUBGROUP_FEATURE_ARITHMETIC_BIT",
1744 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1745 break;
1746 case spv::CapabilityGroupNonUniformBallot:
1747 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT,
1748 "VK_SUBGROUP_FEATURE_BALLOT_BIT",
1749 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1750 break;
1751 case spv::CapabilityGroupNonUniformShuffle:
1752 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_BIT,
1753 "VK_SUBGROUP_FEATURE_SHUFFLE_BIT",
1754 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1755 break;
1756 case spv::CapabilityGroupNonUniformShuffleRelative:
1757 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT,
1758 "VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT",
1759 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1760 break;
1761 case spv::CapabilityGroupNonUniformClustered:
1762 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_CLUSTERED_BIT,
1763 "VK_SUBGROUP_FEATURE_CLUSTERED_BIT",
1764 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1765 break;
1766 case spv::CapabilityGroupNonUniformQuad:
1767 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_QUAD_BIT,
1768 "VK_SUBGROUP_FEATURE_QUAD_BIT",
1769 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1770 break;
1771 case spv::CapabilityGroupNonUniformPartitionedNV:
1772 RequirePropertyFlag(report_data, supportedOperations & VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV,
1773 "VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV",
1774 "VkPhysicalDeviceSubgroupProperties::supportedOperations");
1775 break;
1776 }
Chris Forbes47567b72017-06-09 12:09:45 -07001777 }
1778 }
1779 }
1780
Jeff Bolzee743412019-06-20 22:24:32 -05001781 return skip;
1782}
1783
John Zulaufac4c6e12019-07-01 16:05:58 -06001784bool CoreChecks::ValidateShaderStageWritableDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -05001785 bool skip = false;
1786
Chris Forbes349b3132018-03-07 11:38:08 -08001787 if (has_writable_descriptor) {
1788 switch (stage) {
1789 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -06001790 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1791 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1792 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1793 case VK_SHADER_STAGE_MISS_BIT_NV:
1794 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1795 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1796 case VK_SHADER_STAGE_TASK_BIT_NV:
1797 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -08001798 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -06001799 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -08001800 break;
1801 case VK_SHADER_STAGE_FRAGMENT_BIT:
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001802 skip |= RequireFeature(report_data, enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics");
Chris Forbes349b3132018-03-07 11:38:08 -08001803 break;
1804 default:
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001805 skip |= RequireFeature(report_data, enabled_features.core.vertexPipelineStoresAndAtomics,
1806 "vertexPipelineStoresAndAtomics");
Chris Forbes349b3132018-03-07 11:38:08 -08001807 break;
1808 }
1809 }
1810
Chris Forbes47567b72017-06-09 12:09:45 -07001811 return skip;
1812}
1813
Jeff Bolz526f2d52019-09-18 13:18:08 -05001814bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage) const {
Jeff Bolzee743412019-06-20 22:24:32 -05001815 bool skip = false;
1816
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001817 auto const subgroup_props = phys_dev_props_core11;
Jeff Bolzee743412019-06-20 22:24:32 -05001818
Jeff Bolz526f2d52019-09-18 13:18:08 -05001819 for (auto inst : *module) {
Jeff Bolzee743412019-06-20 22:24:32 -05001820 // Check the quad operations.
1821 switch (inst.opcode()) {
1822 default:
1823 break;
1824 case spv::OpGroupNonUniformQuadBroadcast:
1825 case spv::OpGroupNonUniformQuadSwap:
1826 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001827 skip |= RequireFeature(report_data, subgroup_props.subgroupQuadOperationsInAllStages,
Jeff Bolzee743412019-06-20 22:24:32 -05001828 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages");
1829 }
1830 break;
1831 }
Jeff Bolz526f2d52019-09-18 13:18:08 -05001832
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001833 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
Jeff Bolz526f2d52019-09-18 13:18:08 -05001834 switch (inst.opcode()) {
1835 default:
1836 break;
1837 case spv::OpGroupNonUniformAllEqual:
1838 case spv::OpGroupNonUniformBroadcast:
1839 case spv::OpGroupNonUniformBroadcastFirst:
1840 case spv::OpGroupNonUniformShuffle:
1841 case spv::OpGroupNonUniformShuffleXor:
1842 case spv::OpGroupNonUniformShuffleUp:
1843 case spv::OpGroupNonUniformShuffleDown:
1844 case spv::OpGroupNonUniformIAdd:
1845 case spv::OpGroupNonUniformFAdd:
1846 case spv::OpGroupNonUniformIMul:
1847 case spv::OpGroupNonUniformFMul:
1848 case spv::OpGroupNonUniformSMin:
1849 case spv::OpGroupNonUniformUMin:
1850 case spv::OpGroupNonUniformFMin:
1851 case spv::OpGroupNonUniformSMax:
1852 case spv::OpGroupNonUniformUMax:
1853 case spv::OpGroupNonUniformFMax:
1854 case spv::OpGroupNonUniformBitwiseAnd:
1855 case spv::OpGroupNonUniformBitwiseOr:
1856 case spv::OpGroupNonUniformBitwiseXor:
1857 case spv::OpGroupNonUniformLogicalAnd:
1858 case spv::OpGroupNonUniformLogicalOr:
1859 case spv::OpGroupNonUniformLogicalXor:
1860 case spv::OpGroupNonUniformQuadBroadcast:
1861 case spv::OpGroupNonUniformQuadSwap: {
1862 auto type = module->get_def(inst.word(1));
1863
1864 if (type.opcode() == spv::OpTypeVector) {
1865 // Get the element type
1866 type = module->get_def(type.word(2));
1867 }
1868
1869 if (type.opcode() == spv::OpTypeBool) {
1870 break;
1871 }
1872
1873 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
1874 const uint32_t width = type.word(2);
1875
1876 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
1877 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001878 skip |= RequireFeature(report_data, enabled_features.core12.shaderSubgroupExtendedTypes,
Tony-LunarGa74d3fe2019-11-22 15:43:20 -07001879 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes");
Jeff Bolz526f2d52019-09-18 13:18:08 -05001880 }
1881 break;
1882 }
1883 }
1884 }
Jeff Bolzee743412019-06-20 22:24:32 -05001885 }
1886
1887 return skip;
1888}
1889
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001890bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001891 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001892 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
1893 pStage->stage == VK_SHADER_STAGE_ALL) {
1894 return false;
1895 }
1896
1897 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07001898 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001899
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001900 std::set<uint32_t> patchIDs;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001901 struct Variable {
1902 uint32_t baseTypePtrID;
1903 uint32_t ID;
1904 uint32_t storageClass;
1905 };
1906 std::vector<Variable> variables;
1907
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001908 uint32_t numVertices = 0;
1909
Jeff Bolzf234bf82019-11-04 14:07:15 -06001910 auto entrypointVariables = FindEntrypointInterfaces(entrypoint);
1911
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001912 for (auto insn : *src) {
1913 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001914 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001915 case spv::OpDecorate:
1916 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001917 case spv::DecorationPatch: {
1918 patchIDs.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001919 break;
1920 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001921 default:
1922 break;
1923 }
1924 break;
1925 // Find all input and output variables
1926 case spv::OpVariable: {
1927 Variable var = {};
1928 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001929 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
1930 // Only include variables in the entrypoint's interface
1931 find(entrypointVariables.begin(), entrypointVariables.end(), insn.word(2)) != entrypointVariables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001932 var.baseTypePtrID = insn.word(1);
1933 var.ID = insn.word(2);
1934 variables.push_back(var);
1935 }
1936 break;
1937 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001938 case spv::OpExecutionMode:
1939 if (insn.word(1) == entrypoint.word(2)) {
1940 switch (insn.word(2)) {
1941 default:
1942 break;
1943 case spv::ExecutionModeOutputVertices:
1944 numVertices = insn.word(3);
1945 break;
1946 }
1947 }
1948 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001949 default:
1950 break;
1951 }
1952 }
1953
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001954 bool strip_output_array_level =
1955 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
1956 bool strip_input_array_level =
1957 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
1958 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
1959
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001960 uint32_t numCompIn = 0, numCompOut = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -06001961 int maxCompIn = 0, maxCompOut = 0;
1962
1963 auto inputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassInput, strip_input_array_level);
1964 auto outputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassOutput, strip_output_array_level);
1965
1966 // Find max component location used for input variables.
1967 for (auto &var : inputs) {
1968 int location = var.first.first;
1969 int component = var.first.second;
1970 interface_var &iv = var.second;
1971
1972 // Only need to look at the first location, since we use the type's whole size
1973 if (iv.offset != 0) {
1974 continue;
1975 }
1976
1977 if (iv.is_patch) {
1978 continue;
1979 }
1980
1981 int numComponents = GetComponentsConsumedByType(src, iv.type_id, strip_input_array_level);
1982 maxCompIn = std::max(maxCompIn, location * 4 + component + numComponents);
1983 }
1984
1985 // Find max component location used for output variables.
1986 for (auto &var : outputs) {
1987 int location = var.first.first;
1988 int component = var.first.second;
1989 interface_var &iv = var.second;
1990
1991 // Only need to look at the first location, since we use the type's whole size
1992 if (iv.offset != 0) {
1993 continue;
1994 }
1995
1996 if (iv.is_patch) {
1997 continue;
1998 }
1999
2000 int numComponents = GetComponentsConsumedByType(src, iv.type_id, strip_output_array_level);
2001 maxCompOut = std::max(maxCompOut, location * 4 + component + numComponents);
2002 }
2003
2004 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
2005 // but that doesn't include builtins.
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002006 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002007 // Check if the variable is a patch. Patches can also be members of blocks,
2008 // but if they are then the top-level arrayness has already been stripped
2009 // by the time GetComponentsConsumedByType gets to it.
2010 bool isPatch = patchIDs.find(var.ID) != patchIDs.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002011
2012 if (var.storageClass == spv::StorageClassInput) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002013 numCompIn += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_input_array_level && !isPatch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002014 } else { // var.storageClass == spv::StorageClassOutput
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002015 numCompOut += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_output_array_level && !isPatch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002016 }
2017 }
2018
2019 switch (pStage->stage) {
2020 case VK_SHADER_STAGE_VERTEX_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002021 if (numCompOut > limits.maxVertexOutputComponents) {
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: Vertex shader exceeds "
2025 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
2026 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002027 limits.maxVertexOutputComponents, numCompOut - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002028 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002029 if (maxCompOut > (int)limits.maxVertexOutputComponents) {
2030 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2031 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2032 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
2033 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
2034 limits.maxVertexOutputComponents);
2035 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002036 break;
2037
2038 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002039 if (numCompIn > limits.maxTessellationControlPerVertexInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002040 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2041 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2042 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
2043 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
2044 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002045 limits.maxTessellationControlPerVertexInputComponents,
2046 numCompIn - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002047 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002048 if (maxCompIn > (int)limits.maxTessellationControlPerVertexInputComponents) {
2049 skip |=
2050 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2051 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2052 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
2053 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
2054 limits.maxTessellationControlPerVertexInputComponents);
2055 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002056 if (numCompOut > limits.maxTessellationControlPerVertexOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002057 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2058 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2059 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
2060 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
2061 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002062 limits.maxTessellationControlPerVertexOutputComponents,
2063 numCompOut - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002064 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002065 if (maxCompOut > (int)limits.maxTessellationControlPerVertexOutputComponents) {
2066 skip |=
2067 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2068 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2069 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
2070 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
2071 limits.maxTessellationControlPerVertexOutputComponents);
2072 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002073 break;
2074
2075 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002076 if (numCompIn > limits.maxTessellationEvaluationInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002077 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2078 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2079 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
2080 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
2081 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002082 limits.maxTessellationEvaluationInputComponents,
2083 numCompIn - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002084 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002085 if (maxCompIn > (int)limits.maxTessellationEvaluationInputComponents) {
2086 skip |=
2087 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2088 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2089 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
2090 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
2091 limits.maxTessellationEvaluationInputComponents);
2092 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002093 if (numCompOut > limits.maxTessellationEvaluationOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002094 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2095 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2096 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
2097 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
2098 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002099 limits.maxTessellationEvaluationOutputComponents,
2100 numCompOut - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002101 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002102 if (maxCompOut > (int)limits.maxTessellationEvaluationOutputComponents) {
2103 skip |=
2104 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: Tessellation evaluation shader output variable uses location that "
2107 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
2108 limits.maxTessellationEvaluationOutputComponents);
2109 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002110 break;
2111
2112 case VK_SHADER_STAGE_GEOMETRY_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002113 if (numCompIn > limits.maxGeometryInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002114 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2115 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2116 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
2117 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
2118 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002119 limits.maxGeometryInputComponents, numCompIn - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002120 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002121 if (maxCompIn > (int)limits.maxGeometryInputComponents) {
2122 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2123 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2124 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
2125 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
2126 limits.maxGeometryInputComponents);
2127 }
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002128 if (numCompOut > limits.maxGeometryOutputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002129 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2130 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2131 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
2132 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
2133 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002134 limits.maxGeometryOutputComponents, numCompOut - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002135 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002136 if (maxCompOut > (int)limits.maxGeometryOutputComponents) {
2137 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2138 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2139 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
2140 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
2141 limits.maxGeometryOutputComponents);
2142 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002143 if (numCompOut * numVertices > limits.maxGeometryTotalOutputComponents) {
2144 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2145 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2146 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
2147 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
2148 "components by %u components",
2149 limits.maxGeometryTotalOutputComponents,
2150 numCompOut * numVertices - limits.maxGeometryTotalOutputComponents);
2151 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002152 break;
2153
2154 case VK_SHADER_STAGE_FRAGMENT_BIT:
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002155 if (numCompIn > limits.maxFragmentInputComponents) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002156 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2157 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2158 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
2159 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
2160 "components by %u components",
Mark Lobodzinski57a44272019-02-27 12:40:50 -07002161 limits.maxFragmentInputComponents, numCompIn - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002162 }
Jeff Bolzf234bf82019-11-04 14:07:15 -06002163 if (maxCompIn > (int)limits.maxFragmentInputComponents) {
2164 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
2165 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit,
2166 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
2167 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
2168 limits.maxFragmentInputComponents);
2169 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002170 break;
2171
Jeff Bolz148d94e2018-12-13 21:25:56 -06002172 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
2173 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
2174 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
2175 case VK_SHADER_STAGE_MISS_BIT_NV:
2176 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
2177 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
2178 case VK_SHADER_STAGE_TASK_BIT_NV:
2179 case VK_SHADER_STAGE_MESH_BIT_NV:
2180 break;
2181
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02002182 default:
2183 assert(false); // This should never happen
2184 }
2185 return skip;
2186}
2187
Jeff Bolze4356752019-03-07 11:23:46 -06002188// copy the specialization constant value into buf, if it is present
2189void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
2190 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
2191
2192 if (spec && spec_id < spec->mapEntryCount) {
2193 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
2194 }
2195}
2196
2197// Fill in value with the constant or specialization constant value, if available.
2198// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002199static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Bolze4356752019-03-07 11:23:46 -06002200 const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
2201 auto type_id = src->get_def(insn.word(1));
2202 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
2203 return false;
2204 }
2205 switch (insn.opcode()) {
2206 case spv::OpSpecConstant:
2207 *value = insn.word(3);
2208 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
2209 return true;
2210 case spv::OpConstant:
2211 *value = insn.word(3);
2212 return true;
2213 default:
2214 return false;
2215 }
2216}
2217
2218// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002219VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06002220 switch (insn.opcode()) {
2221 case spv::OpTypeInt:
2222 switch (insn.word(2)) {
2223 case 8:
2224 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
2225 case 16:
2226 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
2227 case 32:
2228 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
2229 case 64:
2230 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
2231 default:
2232 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2233 }
2234 case spv::OpTypeFloat:
2235 switch (insn.word(2)) {
2236 case 16:
2237 return VK_COMPONENT_TYPE_FLOAT16_NV;
2238 case 32:
2239 return VK_COMPONENT_TYPE_FLOAT32_NV;
2240 case 64:
2241 return VK_COMPONENT_TYPE_FLOAT64_NV;
2242 default:
2243 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2244 }
2245 default:
2246 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
2247 }
2248}
2249
2250// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
2251// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002252bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06002253 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06002254 bool skip = false;
2255
2256 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
2257 std::unordered_map<uint32_t, uint32_t> id_to_spec_id;
2258 // Map SPIR-V result ID to the ID of its type.
2259 std::unordered_map<uint32_t, uint32_t> id_to_type_id;
2260
2261 struct CoopMatType {
2262 uint32_t scope, rows, cols;
2263 VkComponentTypeNV component_type;
2264 bool all_constant;
2265
2266 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
2267
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002268 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Bolze4356752019-03-07 11:23:46 -06002269 const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
2270 spirv_inst_iter insn = src->get_def(id);
2271 uint32_t component_type_id = insn.word(2);
2272 uint32_t scope_id = insn.word(3);
2273 uint32_t rows_id = insn.word(4);
2274 uint32_t cols_id = insn.word(5);
2275 auto component_type_iter = src->get_def(component_type_id);
2276 auto scope_iter = src->get_def(scope_id);
2277 auto rows_iter = src->get_def(rows_id);
2278 auto cols_iter = src->get_def(cols_id);
2279
2280 all_constant = true;
2281 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
2282 all_constant = false;
2283 }
2284 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
2285 all_constant = false;
2286 }
2287 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
2288 all_constant = false;
2289 }
2290 component_type = GetComponentType(component_type_iter, src);
2291 }
2292 };
2293
2294 bool seen_coopmat_capability = false;
2295
2296 for (auto insn : *src) {
2297 // Whitelist instructions whose result can be a cooperative matrix type, and
2298 // keep track of their types. It would be nice if SPIRV-Headers generated code
2299 // to identify which instructions have a result type and result id. Lacking that,
2300 // this whitelist is based on the set of instructions that
2301 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
2302 switch (insn.opcode()) {
2303 case spv::OpLoad:
2304 case spv::OpCooperativeMatrixLoadNV:
2305 case spv::OpCooperativeMatrixMulAddNV:
2306 case spv::OpSNegate:
2307 case spv::OpFNegate:
2308 case spv::OpIAdd:
2309 case spv::OpFAdd:
2310 case spv::OpISub:
2311 case spv::OpFSub:
2312 case spv::OpFDiv:
2313 case spv::OpSDiv:
2314 case spv::OpUDiv:
2315 case spv::OpMatrixTimesScalar:
2316 case spv::OpConstantComposite:
2317 case spv::OpCompositeConstruct:
2318 case spv::OpConvertFToU:
2319 case spv::OpConvertFToS:
2320 case spv::OpConvertSToF:
2321 case spv::OpConvertUToF:
2322 case spv::OpUConvert:
2323 case spv::OpSConvert:
2324 case spv::OpFConvert:
2325 id_to_type_id[insn.word(2)] = insn.word(1);
2326 break;
2327 default:
2328 break;
2329 }
2330
2331 switch (insn.opcode()) {
2332 case spv::OpDecorate:
2333 if (insn.word(2) == spv::DecorationSpecId) {
2334 id_to_spec_id[insn.word(1)] = insn.word(3);
2335 }
2336 break;
2337 case spv::OpCapability:
2338 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
2339 seen_coopmat_capability = true;
2340
2341 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
2342 skip |=
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002343 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Jeff Bolze4356752019-03-07 11:23:46 -06002344 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixSupportedStages,
2345 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
2346 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
2347 }
2348 }
2349 break;
2350 case spv::OpMemoryModel:
2351 // If the capability isn't enabled, don't bother with the rest of this function.
2352 // OpMemoryModel is the first required instruction after all OpCapability instructions.
2353 if (!seen_coopmat_capability) {
2354 return skip;
2355 }
2356 break;
2357 case spv::OpTypeCooperativeMatrixNV: {
2358 CoopMatType M;
2359 M.Init(insn.word(1), src, pStage, id_to_spec_id);
2360
2361 if (M.all_constant) {
2362 // Validate that the type parameters are all supported for one of the
2363 // operands of a cooperative matrix property.
2364 bool valid = false;
2365 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
2366 if (cooperative_matrix_properties[i].AType == M.component_type &&
2367 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].KSize == M.cols &&
2368 cooperative_matrix_properties[i].scope == M.scope) {
2369 valid = true;
2370 break;
2371 }
2372 if (cooperative_matrix_properties[i].BType == M.component_type &&
2373 cooperative_matrix_properties[i].KSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2374 cooperative_matrix_properties[i].scope == M.scope) {
2375 valid = true;
2376 break;
2377 }
2378 if (cooperative_matrix_properties[i].CType == M.component_type &&
2379 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2380 cooperative_matrix_properties[i].scope == M.scope) {
2381 valid = true;
2382 break;
2383 }
2384 if (cooperative_matrix_properties[i].DType == M.component_type &&
2385 cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols &&
2386 cooperative_matrix_properties[i].scope == M.scope) {
2387 valid = true;
2388 break;
2389 }
2390 }
2391 if (!valid) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002392 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 -06002393 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixType,
2394 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
2395 insn.word(1));
2396 }
2397 }
2398 break;
2399 }
2400 case spv::OpCooperativeMatrixMulAddNV: {
2401 CoopMatType A, B, C, D;
2402 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
2403 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
2404 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
2405 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07002406 // Couldn't find type of matrix
2407 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06002408 break;
2409 }
2410 D.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
2411 A.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
2412 B.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
2413 C.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
2414
2415 if (A.all_constant && B.all_constant && C.all_constant && D.all_constant) {
2416 // Validate that the type parameters are all supported for the same
2417 // cooperative matrix property.
2418 bool valid = false;
2419 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
2420 if (cooperative_matrix_properties[i].AType == A.component_type &&
2421 cooperative_matrix_properties[i].MSize == A.rows && cooperative_matrix_properties[i].KSize == A.cols &&
2422 cooperative_matrix_properties[i].scope == A.scope &&
2423
2424 cooperative_matrix_properties[i].BType == B.component_type &&
2425 cooperative_matrix_properties[i].KSize == B.rows && cooperative_matrix_properties[i].NSize == B.cols &&
2426 cooperative_matrix_properties[i].scope == B.scope &&
2427
2428 cooperative_matrix_properties[i].CType == C.component_type &&
2429 cooperative_matrix_properties[i].MSize == C.rows && cooperative_matrix_properties[i].NSize == C.cols &&
2430 cooperative_matrix_properties[i].scope == C.scope &&
2431
2432 cooperative_matrix_properties[i].DType == D.component_type &&
2433 cooperative_matrix_properties[i].MSize == D.rows && cooperative_matrix_properties[i].NSize == D.cols &&
2434 cooperative_matrix_properties[i].scope == D.scope) {
2435 valid = true;
2436 break;
2437 }
2438 }
2439 if (!valid) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002440 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 -06002441 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixMulAdd,
2442 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
2443 "VkCooperativeMatrixPropertiesNV",
2444 insn.word(2));
2445 }
2446 }
2447 break;
2448 }
2449 default:
2450 break;
2451 }
2452 }
2453
2454 return skip;
2455}
2456
John Zulaufac4c6e12019-07-01 16:05:58 -06002457bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002458 auto entrypoint_id = entrypoint.word(2);
2459
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002460 // The first denorm execution mode encountered, along with its bit width.
2461 // Used to check if SeparateDenormSettings is respected.
2462 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002463
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002464 // The first rounding mode encountered, along with its bit width.
2465 // Used to check if SeparateRoundingModeSettings is respected.
2466 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002467
2468 bool skip = false;
2469
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002470 uint32_t verticesOut = 0;
2471 uint32_t invocations = 0;
2472
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002473 for (auto insn : *src) {
2474 if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) {
2475 auto mode = insn.word(2);
2476 switch (mode) {
2477 case spv::ExecutionModeSignedZeroInfNanPreserve: {
2478 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002479 if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) ||
2480 (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) ||
2481 (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002482 skip |=
2483 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2484 kVUID_Core_Shader_FeatureNotEnabled,
2485 "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device",
2486 bit_width);
2487 }
2488 break;
2489 }
2490
2491 case spv::ExecutionModeDenormPreserve: {
2492 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002493 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) ||
2494 (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) ||
2495 (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002496 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2497 kVUID_Core_Shader_FeatureNotEnabled,
2498 "Shader requires DenormPreserve for bit width %d but it is not enabled on the device",
2499 bit_width);
2500 }
2501
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002502 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
2503 // Register the first denorm execution mode found
2504 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002505 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002506 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002507 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR:
2508 if (first_rounding_mode.second != 32 && bit_width != 32) {
2509 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2510 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_FeatureNotEnabled,
2511 "Shader uses different denorm execution modes for 16 and 64-bit but "
2512 "denormBehaviorIndependence is "
2513 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device");
2514 }
2515 break;
2516
2517 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR:
2518 break;
2519
2520 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR:
2521 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2522 0, kVUID_Core_Shader_FeatureNotEnabled,
2523 "Shader uses different denorm execution modes for different bit widths but "
2524 "denormBehaviorIndependence is "
2525 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device");
2526 break;
2527
2528 default:
2529 break;
2530 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002531 }
2532 break;
2533 }
2534
2535 case spv::ExecutionModeDenormFlushToZero: {
2536 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002537 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) ||
2538 (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) ||
2539 (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002540 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2541 kVUID_Core_Shader_FeatureNotEnabled,
2542 "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device",
2543 bit_width);
2544 }
2545
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002546 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
2547 // Register the first denorm execution mode found
2548 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002549 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002550 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002551 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR:
2552 if (first_rounding_mode.second != 32 && bit_width != 32) {
2553 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2554 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_FeatureNotEnabled,
2555 "Shader uses different denorm execution modes for 16 and 64-bit but "
2556 "denormBehaviorIndependence is "
2557 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device");
2558 }
2559 break;
2560
2561 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR:
2562 break;
2563
2564 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR:
2565 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2566 0, kVUID_Core_Shader_FeatureNotEnabled,
2567 "Shader uses different denorm execution modes for different bit widths but "
2568 "denormBehaviorIndependence is "
2569 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device");
2570 break;
2571
2572 default:
2573 break;
2574 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002575 }
2576 break;
2577 }
2578
2579 case spv::ExecutionModeRoundingModeRTE: {
2580 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002581 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) ||
2582 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) ||
2583 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002584 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2585 kVUID_Core_Shader_FeatureNotEnabled,
2586 "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device",
2587 bit_width);
2588 }
2589
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002590 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2591 // Register the first rounding mode found
2592 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002593 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002594 switch (phys_dev_props_core12.roundingModeIndependence) {
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002595 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR:
2596 if (first_rounding_mode.second != 32 && bit_width != 32) {
2597 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2598 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_FeatureNotEnabled,
2599 "Shader uses different rounding modes for 16 and 64-bit but "
2600 "roundingModeIndependence is "
2601 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device");
2602 }
2603 break;
2604
2605 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR:
2606 break;
2607
2608 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR:
2609 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2610 0, kVUID_Core_Shader_FeatureNotEnabled,
2611 "Shader uses different rounding modes for different bit widths but "
2612 "roundingModeIndependence is "
2613 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device");
2614 break;
2615
2616 default:
2617 break;
2618 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002619 }
2620 break;
2621 }
2622
2623 case spv::ExecutionModeRoundingModeRTZ: {
2624 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002625 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) ||
2626 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) ||
2627 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002628 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2629 kVUID_Core_Shader_FeatureNotEnabled,
2630 "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device",
2631 bit_width);
2632 }
2633
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002634 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2635 // Register the first rounding mode found
2636 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002637 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002638 switch (phys_dev_props_core12.roundingModeIndependence) {
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002639 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR:
2640 if (first_rounding_mode.second != 32 && bit_width != 32) {
2641 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT,
2642 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_FeatureNotEnabled,
2643 "Shader uses different rounding modes for 16 and 64-bit but "
2644 "roundingModeIndependence is "
2645 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device");
2646 }
2647 break;
2648
2649 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR:
2650 break;
2651
2652 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR:
2653 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT,
2654 0, kVUID_Core_Shader_FeatureNotEnabled,
2655 "Shader uses different rounding modes for different bit widths but "
2656 "roundingModeIndependence is "
2657 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device");
2658 break;
2659
2660 default:
2661 break;
2662 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002663 }
2664 break;
2665 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002666
2667 case spv::ExecutionModeOutputVertices: {
2668 verticesOut = insn.word(3);
2669 break;
2670 }
2671
2672 case spv::ExecutionModeInvocations: {
2673 invocations = insn.word(3);
2674 break;
2675 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002676 }
2677 }
2678 }
2679
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002680 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
2681 if (verticesOut == 0 || verticesOut > phys_dev_props.limits.maxGeometryOutputVertices) {
2682 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2683 "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2684 "Geometry shader entry point must have an OpExecutionMode instruction that "
2685 "specifies a maximum output vertex count that is greater than 0 and less "
2686 "than or equal to maxGeometryOutputVertices. "
2687 "OutputVertices=%d, maxGeometryOutputVertices=%d",
2688 verticesOut, phys_dev_props.limits.maxGeometryOutputVertices);
2689 }
2690
2691 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
2692 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2693 "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2694 "Geometry shader entry point must have an OpExecutionMode instruction that "
2695 "specifies an invocation count that is greater than 0 and less "
2696 "than or equal to maxGeometryShaderInvocations. "
2697 "Invocations=%d, maxGeometryShaderInvocations=%d",
2698 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
2699 }
2700 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002701 return skip;
2702}
2703
locke-lunargd9a069d2019-09-17 01:50:19 -06002704uint32_t DescriptorTypeToReqs(SHADER_MODULE_STATE const *module, uint32_t type_id) {
Chris Forbes47567b72017-06-09 12:09:45 -07002705 auto type = module->get_def(type_id);
2706
2707 while (true) {
2708 switch (type.opcode()) {
2709 case spv::OpTypeArray:
Chris Forbes062f1222018-08-21 15:34:15 -07002710 case spv::OpTypeRuntimeArray:
Chris Forbes47567b72017-06-09 12:09:45 -07002711 case spv::OpTypeSampledImage:
2712 type = module->get_def(type.word(2));
2713 break;
2714 case spv::OpTypePointer:
2715 type = module->get_def(type.word(3));
2716 break;
2717 case spv::OpTypeImage: {
2718 auto dim = type.word(3);
2719 auto arrayed = type.word(5);
2720 auto msaa = type.word(6);
2721
Chris Forbes74ba2232018-08-27 15:19:27 -07002722 uint32_t bits = 0;
2723 switch (GetFundamentalType(module, type.word(2))) {
2724 case FORMAT_TYPE_FLOAT:
2725 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
2726 break;
2727 case FORMAT_TYPE_UINT:
2728 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
2729 break;
2730 case FORMAT_TYPE_SINT:
2731 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
2732 break;
2733 default:
2734 break;
2735 }
2736
Chris Forbes47567b72017-06-09 12:09:45 -07002737 switch (dim) {
2738 case spv::Dim1D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002739 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
2740 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002741 case spv::Dim2D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002742 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
2743 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
2744 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002745 case spv::Dim3D:
Chris Forbes74ba2232018-08-27 15:19:27 -07002746 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
2747 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002748 case spv::DimCube:
Chris Forbes74ba2232018-08-27 15:19:27 -07002749 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
2750 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002751 case spv::DimSubpassData:
Chris Forbes74ba2232018-08-27 15:19:27 -07002752 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
2753 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002754 default: // buffer, etc.
Chris Forbes74ba2232018-08-27 15:19:27 -07002755 return bits;
Chris Forbes47567b72017-06-09 12:09:45 -07002756 }
2757 }
2758 default:
2759 return 0;
2760 }
2761 }
2762}
2763
2764// For given pipelineLayout verify that the set_layout_node at slot.first
2765// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002766static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002767 descriptor_slot_t slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002768 if (!pipelineLayout) return nullptr;
2769
2770 if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr;
2771
2772 return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second);
2773}
2774
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002775static 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 -06002776 for (auto insn : *src) {
2777 if (insn.opcode() == spv::OpEntryPoint) {
2778 auto executionModel = insn.word(1);
2779 auto entrypointStageBits = ExecutionModelToShaderStageFlagBits(executionModel);
2780 if (entrypointStageBits == VK_SHADER_STAGE_COMPUTE_BIT) {
2781 auto entrypoint_id = insn.word(2);
2782 for (auto insn1 : *src) {
2783 if (insn1.opcode() == spv::OpExecutionMode && insn1.word(1) == entrypoint_id &&
2784 insn1.word(2) == spv::ExecutionModeLocalSize) {
2785 local_size_x = insn1.word(3);
2786 local_size_y = insn1.word(4);
2787 local_size_z = insn1.word(5);
2788 return true;
2789 }
2790 }
2791 }
2792 }
2793 }
2794 return false;
2795}
2796
locke-lunargd9a069d2019-09-17 01:50:19 -06002797void ProcessExecutionModes(SHADER_MODULE_STATE const *src, const spirv_inst_iter &entrypoint, PIPELINE_STATE *pipeline) {
Jeff Bolz105d6492018-09-29 15:46:44 -05002798 auto entrypoint_id = entrypoint.word(2);
Chris Forbes0771b672018-03-22 21:13:46 -07002799 bool is_point_mode = false;
2800
2801 for (auto insn : *src) {
2802 if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) {
2803 switch (insn.word(2)) {
2804 case spv::ExecutionModePointMode:
2805 // In tessellation shaders, PointMode is separate and trumps the tessellation topology.
2806 is_point_mode = true;
2807 break;
2808
2809 case spv::ExecutionModeOutputPoints:
2810 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
2811 break;
2812
2813 case spv::ExecutionModeIsolines:
2814 case spv::ExecutionModeOutputLineStrip:
2815 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP;
2816 break;
2817
2818 case spv::ExecutionModeTriangles:
2819 case spv::ExecutionModeQuads:
2820 case spv::ExecutionModeOutputTriangleStrip:
2821 pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
2822 break;
2823 }
2824 }
2825 }
2826
2827 if (is_point_mode) pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST;
2828}
2829
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002830// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2831// o If there is only a vertex shader : gl_PointSize must be written when using points
2832// o If there is a geometry or tessellation shader:
2833// - If shaderTessellationAndGeometryPointSize feature is enabled:
2834// * gl_PointSize must be written in the final geometry stage
2835// - If shaderTessellationAndGeometryPointSize feature is disabled:
2836// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002837bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06002838 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002839 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2840 return false;
2841 }
2842
2843 bool pointsize_written = false;
2844 bool skip = false;
2845
2846 // Search for PointSize built-in decorations
2847 std::vector<uint32_t> pointsize_builtin_offsets;
2848 spirv_inst_iter insn = entrypoint;
2849 while (!pointsize_written && (insn.opcode() != spv::OpFunction)) {
2850 if (insn.opcode() == spv::OpMemberDecorate) {
2851 if (insn.word(3) == spv::DecorationBuiltIn) {
2852 if (insn.word(4) == spv::BuiltInPointSize) {
2853 pointsize_written = IsPointSizeWritten(src, insn, entrypoint);
2854 }
2855 }
2856 } else if (insn.opcode() == spv::OpDecorate) {
2857 if (insn.word(2) == spv::DecorationBuiltIn) {
2858 if (insn.word(3) == spv::BuiltInPointSize) {
2859 pointsize_written = IsPointSizeWritten(src, insn, entrypoint);
2860 }
2861 }
2862 }
2863
2864 insn++;
2865 }
2866
2867 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002868 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002869 if (pointsize_written) {
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002870 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 -06002871 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
2872 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2873 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
2874 }
2875 } else if (!pointsize_written) {
2876 skip |=
Mark Lobodzinski93a1fa72019-04-19 12:12:25 -06002877 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT,
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002878 HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_MissingPointSizeBuiltIn,
2879 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2880 string_VkShaderStageFlagBits(stage));
2881 }
2882 return skip;
2883}
John Zulauf14c355b2019-06-27 16:09:37 -06002884
2885bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline,
2886 const PIPELINE_STATE::StageState &stage_state, const SHADER_MODULE_STATE *module,
John Zulaufac4c6e12019-07-01 16:05:58 -06002887 const spirv_inst_iter &entrypoint, bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002888 bool skip = false;
2889
2890 // Check the module
2891 if (!module->has_valid_spirv) {
2892 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2893 "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2894 report_data->FormatHandle(module->vk_shader_module).c_str(), string_VkShaderStageFlagBits(pStage->stage));
2895 }
2896
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002897 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2898 // specializations should be applied and validated.
2899 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2900 pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) {
2901 // Gather the specialization-constant values.
2902 auto const &specialization_info = pStage->pSpecializationInfo;
2903 std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map;
2904 id_value_map.reserve(specialization_info->mapEntryCount);
2905 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2906 auto const &map_entry = specialization_info->pMapEntries[i];
2907 assert(map_entry.size % 4 == 0);
2908
2909 auto const begin = reinterpret_cast<uint32_t const *>(specialization_info->pData) + map_entry.offset / 4;
2910 auto const end = begin + map_entry.size / 4;
2911 id_value_map.emplace(map_entry.constantID, std::vector<uint32_t>(begin, end));
2912 }
2913
2914 // Apply the specialization-constant values and revalidate the shader module.
2915 spv_target_env const spirv_environment = ((api_version >= VK_API_VERSION_1_1) ? SPV_ENV_VULKAN_1_1 : SPV_ENV_VULKAN_1_0);
2916 spvtools::Optimizer optimizer(spirv_environment);
2917 spvtools::MessageConsumer consumer = [&skip, &module, &pStage, this](spv_message_level_t level, const char *source,
2918 const spv_position_t &position, const char *message) {
2919 skip |= log_msg(
2920 report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2921 "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s. %s",
2922 report_data->FormatHandle(module->vk_shader_module).c_str(), string_VkShaderStageFlagBits(pStage->stage), message);
2923 };
2924 optimizer.SetMessageConsumer(consumer);
2925 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2926 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2927 std::vector<uint32_t> specialized_spirv;
2928 auto const optimized =
2929 optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, spvtools::ValidatorOptions(), true);
2930 assert(optimized == true);
2931
2932 if (optimized) {
2933 spv_context ctx = spvContextCreate(spirv_environment);
2934 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2935 spv_diagnostic diag = nullptr;
2936 spv_validator_options options = spvValidatorOptionsCreate();
2937 if (device_extensions.vk_khr_relaxed_block_layout) {
2938 spvValidatorOptionsSetRelaxBlockLayout(options, true);
2939 }
2940 if (device_extensions.vk_khr_uniform_buffer_standard_layout &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002941 enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002942 spvValidatorOptionsSetUniformBufferStandardLayout(options, true);
2943 }
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002944 if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002945 spvValidatorOptionsSetScalarBlockLayout(options, true);
2946 }
2947 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2948 if (spv_valid != SPV_SUCCESS) {
2949 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2950 "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2951 "After specialization was applied, %s does not contain valid spirv for stage %s.",
2952 report_data->FormatHandle(module->vk_shader_module).c_str(),
2953 string_VkShaderStageFlagBits(pStage->stage));
2954 }
2955
2956 spvValidatorOptionsDestroy(options);
2957 spvDiagnosticDestroy(diag);
2958 spvContextDestroy(ctx);
2959 }
2960 }
2961
John Zulauf14c355b2019-06-27 16:09:37 -06002962 // Check the entrypoint
2963 if (entrypoint == module->end()) {
2964 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
2965 "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s..",
2966 pStage->pName, string_VkShaderStageFlagBits(pStage->stage));
2967 }
2968 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2969
2970 // Mark accessible ids
2971 auto &accessible_ids = stage_state.accessible_ids;
2972
Chris Forbes47567b72017-06-09 12:09:45 -07002973 // Validate descriptor set layout against what the entrypoint actually uses
John Zulauf14c355b2019-06-27 16:09:37 -06002974 bool has_writable_descriptor = stage_state.has_writable_descriptor;
2975 auto &descriptor_uses = stage_state.descriptor_uses;
Chris Forbes47567b72017-06-09 12:09:45 -07002976
Chris Forbes349b3132018-03-07 11:38:08 -08002977 // Validate shader capabilities against enabled device features
Jeff Bolzee743412019-06-20 22:24:32 -05002978 skip |= ValidateShaderCapabilities(module, pStage->stage);
2979 skip |= ValidateShaderStageWritableDescriptor(pStage->stage, has_writable_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002980 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Jeff Bolz526f2d52019-09-18 13:18:08 -05002981 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002982 skip |= ValidateExecutionModes(module, entrypoint);
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002983 skip |= ValidateSpecializationOffsets(report_data, pStage);
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002984 skip |= ValidatePushConstantUsage(report_data, pipeline->pipeline_layout->push_constant_ranges.get(), module, accessible_ids,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002985 pStage->stage);
Jeff Bolze54ae892018-09-08 12:16:29 -05002986 if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002987 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002988 }
Jeff Bolze4356752019-03-07 11:23:46 -06002989 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
Chris Forbes47567b72017-06-09 12:09:45 -07002990
2991 // Validate descriptor use
2992 for (auto use : descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002993 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002994 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002995 unsigned required_descriptor_count;
Jeff Bolze54ae892018-09-08 12:16:29 -05002996 std::set<uint32_t> descriptor_types = TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count);
Chris Forbes47567b72017-06-09 12:09:45 -07002997
2998 if (!binding) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06002999 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 -06003000 kVUID_Core_Shader_MissingDescriptor,
Chris Forbes73c00bf2018-06-22 16:28:06 -07003001 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeff Bolze54ae892018-09-08 12:16:29 -05003002 use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07003003 } else if (~binding->stageFlags & pStage->stage) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06003004 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 -06003005 kVUID_Core_Shader_DescriptorNotAccessibleFromStage,
Chris Forbes73c00bf2018-06-22 16:28:06 -07003006 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first,
3007 use.first.second, string_VkShaderStageFlagBits(pStage->stage));
Jeff Bolze54ae892018-09-08 12:16:29 -05003008 } else if (descriptor_types.find(binding->descriptorType) == descriptor_types.end()) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06003009 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 -06003010 kVUID_Core_Shader_DescriptorTypeMismatch,
Chris Forbes73c00bf2018-06-22 16:28:06 -07003011 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first,
Jeff Bolze54ae892018-09-08 12:16:29 -05003012 use.first.second, string_descriptorTypes(descriptor_types).c_str(),
Chris Forbes47567b72017-06-09 12:09:45 -07003013 string_VkDescriptorType(binding->descriptorType));
3014 } else if (binding->descriptorCount < required_descriptor_count) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06003015 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 -06003016 kVUID_Core_Shader_DescriptorTypeMismatch,
Chris Forbes73c00bf2018-06-22 16:28:06 -07003017 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
3018 required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07003019 }
3020 }
3021
3022 // Validate use of input attachments against subpass structure
3023 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003024 auto input_attachment_uses = CollectInterfaceByInputAttachmentIndex(module, accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07003025
Petr Krause91f7a12017-12-14 20:57:36 +01003026 auto rpci = pipeline->rp_state->createInfo.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07003027 auto subpass = pipeline->graphicsPipelineCI.subpass;
3028
3029 for (auto use : input_attachment_uses) {
3030 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
3031 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07003032 ? input_attachments[use.first].attachment
3033 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07003034
3035 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06003036 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 -06003037 kVUID_Core_Shader_MissingInputAttachment,
Chris Forbes47567b72017-06-09 12:09:45 -07003038 "Shader consumes input attachment index %d but not provided in subpass", use.first);
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003039 } else if (!(GetFormatType(rpci->pAttachments[index].format) & GetFundamentalType(module, use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07003040 skip |=
Mark Lobodzinskib1fd9d12018-03-30 14:26:00 -06003041 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 -06003042 kVUID_Core_Shader_InputAttachmentTypeMismatch,
Chris Forbes47567b72017-06-09 12:09:45 -07003043 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003044 string_VkFormat(rpci->pAttachments[index].format), DescribeType(module, use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07003045 }
3046 }
3047 }
Lockeaa8fdc02019-04-02 11:59:20 -06003048 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
3049 skip |= ValidateComputeWorkGroupSizes(module);
3050 }
Chris Forbes47567b72017-06-09 12:09:45 -07003051 return skip;
3052}
3053
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06003054static bool ValidateInterfaceBetweenStages(debug_report_data const *report_data, SHADER_MODULE_STATE const *producer,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003055 spirv_inst_iter producer_entrypoint, shader_stage_attributes const *producer_stage,
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06003056 SHADER_MODULE_STATE const *consumer, spirv_inst_iter consumer_entrypoint,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003057 shader_stage_attributes const *consumer_stage) {
Chris Forbes47567b72017-06-09 12:09:45 -07003058 bool skip = false;
3059
3060 auto outputs =
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003061 CollectInterfaceByLocation(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
3062 auto inputs = CollectInterfaceByLocation(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07003063
3064 auto a_it = outputs.begin();
3065 auto b_it = inputs.begin();
3066
3067 // Maps sorted by key (location); walk them together to find mismatches
3068 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
3069 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
3070 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
3071 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
3072 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
3073
3074 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Mark Young4e919b22018-05-21 15:53:59 -06003075 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 -06003076 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed,
Mark Young4e919b22018-05-21 15:53:59 -06003077 "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, a_first.first,
3078 a_first.second, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07003079 a_it++;
3080 } else if (a_at_end || a_first > b_first) {
Mark Young4e919b22018-05-21 15:53:59 -06003081 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 -06003082 HandleToUint64(consumer->vk_shader_module), kVUID_Core_Shader_InputNotProduced,
Mark Young4e919b22018-05-21 15:53:59 -06003083 "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first,
3084 b_first.second, producer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07003085 b_it++;
3086 } else {
3087 // subtleties of arrayed interfaces:
3088 // - if is_patch, then the member is not arrayed, even though the interface may be.
3089 // - if is_block_member, then the extra array level of an arrayed interface is not
3090 // expressed in the member type -- it's expressed in the block type.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003091 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id,
3092 producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member,
3093 consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) {
Mark Young4e919b22018-05-21 15:53:59 -06003094 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 -06003095 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Young4e919b22018-05-21 15:53:59 -06003096 "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003097 DescribeType(producer, a_it->second.type_id).c_str(),
3098 DescribeType(consumer, b_it->second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07003099 }
3100 if (a_it->second.is_patch != b_it->second.is_patch) {
Mark Young4e919b22018-05-21 15:53:59 -06003101 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 -06003102 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Dave Houltona9df0ce2018-02-07 10:51:23 -07003103 "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 -07003104 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
3105 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
3106 }
3107 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Mark Young4e919b22018-05-21 15:53:59 -06003108 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 -06003109 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
Chris Forbes47567b72017-06-09 12:09:45 -07003110 "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first,
3111 a_first.second, producer_stage->name, consumer_stage->name);
3112 }
3113 a_it++;
3114 b_it++;
3115 }
3116 }
3117
Ari Suonpaa696b3432019-03-11 14:02:57 +02003118 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
3119 auto builtins_producer = CollectBuiltinBlockMembers(producer, producer_entrypoint, spv::StorageClassOutput);
3120 auto builtins_consumer = CollectBuiltinBlockMembers(consumer, consumer_entrypoint, spv::StorageClassInput);
3121
3122 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
3123 if (builtins_producer.size() != builtins_consumer.size()) {
3124 skip |=
3125 log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3126 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
3127 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).", producer_stage->name,
3128 (int)builtins_producer.size(), consumer_stage->name, (int)builtins_consumer.size());
3129 } else {
3130 auto it_producer = builtins_producer.begin();
3131 auto it_consumer = builtins_consumer.begin();
3132 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
3133 if (*it_producer != *it_consumer) {
3134 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3135 HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch,
3136 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
3137 consumer_stage->name);
3138 break;
3139 }
3140 it_producer++;
3141 it_consumer++;
3142 }
3143 }
3144 }
3145 }
3146
Chris Forbes47567b72017-06-09 12:09:45 -07003147 return skip;
3148}
3149
John Zulauf14c355b2019-06-27 16:09:37 -06003150static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003151 uint32_t stage_mask = 0;
3152 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
3153 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
3154 stage_mask |= pCreateInfo->pStages[i].stage;
3155 }
3156 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05003157 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
3158 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
3159 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003160 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
3161 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
3162 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
3163 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
3164 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003165 }
3166 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003167 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003168}
3169
Chris Forbes47567b72017-06-09 12:09:45 -07003170// Validate that the shaders used by the given pipeline and store the active_slots
3171// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06003172bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Chris Forbesa400a8a2017-07-20 13:10:24 -07003173 auto pCreateInfo = pipeline->graphicsPipelineCI.ptr();
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003174 int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
3175 int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07003176
John Zulauf14c355b2019-06-27 16:09:37 -06003177 const SHADER_MODULE_STATE *shaders[32];
Chris Forbes47567b72017-06-09 12:09:45 -07003178 memset(shaders, 0, sizeof(shaders));
Jeff Bolz7e35c392018-09-04 15:30:41 -05003179 spirv_inst_iter entrypoints[32];
Chris Forbes47567b72017-06-09 12:09:45 -07003180 memset(entrypoints, 0, sizeof(entrypoints));
3181 bool skip = false;
3182
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003183 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, pCreateInfo);
3184
Chris Forbes47567b72017-06-09 12:09:45 -07003185 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
3186 auto pStage = &pCreateInfo->pStages[i];
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003187 auto stage_id = GetShaderStageId(pStage->stage);
John Zulauf14c355b2019-06-27 16:09:37 -06003188 shaders[stage_id] = GetShaderModuleState(pStage->module);
3189 entrypoints[stage_id] = FindEntrypoint(shaders[stage_id], pStage->pName, pStage->stage);
3190 skip |= ValidatePipelineShaderStage(pStage, pipeline, pipeline->stage_state[i], shaders[stage_id], entrypoints[stage_id],
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003191 (pointlist_stage_mask == pStage->stage));
Chris Forbes47567b72017-06-09 12:09:45 -07003192 }
3193
3194 // if the shader stages are no good individually, cross-stage validation is pointless.
3195 if (skip) return true;
3196
3197 auto vi = pCreateInfo->pVertexInputState;
3198
3199 if (vi) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003200 skip |= ValidateViConsistency(report_data, vi);
Chris Forbes47567b72017-06-09 12:09:45 -07003201 }
3202
3203 if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003204 skip |= ValidateViAgainstVsInputs(report_data, vi, shaders[vertex_stage], entrypoints[vertex_stage]);
Chris Forbes47567b72017-06-09 12:09:45 -07003205 }
3206
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003207 int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
3208 int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07003209
3210 while (!shaders[producer] && producer != fragment_stage) {
3211 producer++;
3212 consumer++;
3213 }
3214
3215 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
3216 assert(shaders[producer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08003217 if (shaders[consumer]) {
3218 if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003219 skip |= ValidateInterfaceBetweenStages(report_data, shaders[producer], entrypoints[producer],
3220 &shader_stage_attribs[producer], shaders[consumer], entrypoints[consumer],
3221 &shader_stage_attribs[consumer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08003222 }
Chris Forbes47567b72017-06-09 12:09:45 -07003223
3224 producer = consumer;
3225 }
3226 }
3227
3228 if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06003229 skip |= ValidateFsOutputsAgainstRenderPass(report_data, shaders[fragment_stage], entrypoints[fragment_stage], pipeline,
3230 pCreateInfo->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07003231 }
3232
3233 return skip;
3234}
3235
John Zulaufac4c6e12019-07-01 16:05:58 -06003236bool CoreChecks::ValidateComputePipeline(PIPELINE_STATE *pipeline) const {
John Zulauf14c355b2019-06-27 16:09:37 -06003237 const auto &stage = *pipeline->computePipelineCI.stage.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07003238
John Zulauf14c355b2019-06-27 16:09:37 -06003239 const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module);
3240 const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage);
Chris Forbes47567b72017-06-09 12:09:45 -07003241
John Zulauf14c355b2019-06-27 16:09:37 -06003242 return ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[0], module, entrypoint, false);
Chris Forbes47567b72017-06-09 12:09:45 -07003243}
Chris Forbes4ae55b32017-06-09 14:42:56 -07003244
John Zulaufac4c6e12019-07-01 16:05:58 -06003245bool CoreChecks::ValidateRayTracingPipelineNV(PIPELINE_STATE *pipeline) const {
John Zulaufe4474e72019-07-01 17:28:27 -06003246 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04003247
3248 if (pipeline->raytracingPipelineCI.maxRecursionDepth > phys_dev_ext_props.ray_tracing_props.maxRecursionDepth) {
3249 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3250 "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-02412", ": %d > %d",
3251 pipeline->raytracingPipelineCI.maxRecursionDepth, phys_dev_ext_props.ray_tracing_props.maxRecursionDepth);
3252 }
3253
3254 const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages;
3255 const auto *groups = pipeline->raytracingPipelineCI.ptr()->pGroups;
3256
3257 uint32_t raygen_stages_found = 0;
John Zulaufe4474e72019-07-01 17:28:27 -06003258 for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003259 const auto &stage = stages[stage_index];
Jeff Bolzfbe51582018-09-13 10:01:35 -05003260
John Zulaufe4474e72019-07-01 17:28:27 -06003261 const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module);
3262 const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage);
Jeff Bolzfbe51582018-09-13 10:01:35 -05003263
John Zulaufe4474e72019-07-01 17:28:27 -06003264 skip |= ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[stage_index], module, entrypoint, false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003265
3266 if (stage.stage == VK_SHADER_STAGE_RAYGEN_BIT_NV) {
3267 raygen_stages_found++;
3268 }
3269 }
3270 if (raygen_stages_found != 1) {
3271 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3272 "VUID-VkRayTracingPipelineCreateInfoNV-stage-02408", " : %d raygen stages specified", raygen_stages_found);
3273 }
3274
3275 for (uint32_t group_index = 0; group_index < pipeline->raytracingPipelineCI.groupCount; group_index++) {
3276 const auto &group = groups[group_index];
3277
3278 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
3279 if (group.generalShader >= pipeline->raytracingPipelineCI.stageCount ||
3280 (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV &&
3281 stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV &&
3282 stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) {
3283 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3284 "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413", ": pGroups[%d]", group_index);
3285 }
3286 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3287 group.intersectionShader != VK_SHADER_UNUSED_NV) {
3288 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3289 "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414", ": pGroups[%d]", group_index);
3290 }
3291 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
3292 if (group.intersectionShader >= pipeline->raytracingPipelineCI.stageCount ||
3293 stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) {
3294 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3295 "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415", ": pGroups[%d]", group_index);
3296 }
3297 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3298 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
3299 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3300 "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416", ": pGroups[%d]", group_index);
3301 }
3302 }
3303
3304 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3305 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3306 if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= pipeline->raytracingPipelineCI.stageCount ||
3307 stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
3308 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3309 "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418", ": pGroups[%d]", group_index);
3310 }
3311 if (group.closestHitShader != VK_SHADER_UNUSED_NV &&
3312 (group.closestHitShader >= pipeline->raytracingPipelineCI.stageCount ||
3313 stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
3314 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
3315 "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417", ": pGroups[%d]", group_index);
3316 }
3317 }
John Zulaufe4474e72019-07-01 17:28:27 -06003318 }
3319 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003320}
3321
Dave Houltona9df0ce2018-02-07 10:51:23 -07003322uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003323
Dave Houltona9df0ce2018-02-07 10:51:23 -07003324static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
John Zulauf25ea2432019-04-05 10:07:38 -06003325 const auto validation_cache_ci = lvl_find_in_chain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
3326 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003327 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003328 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003329 return nullptr;
3330}
3331
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003332bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003333 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003334 bool skip = false;
3335 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003336
Mark Lobodzinskib02a4852019-04-19 12:35:30 -06003337 if (disabled.shader_validation) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003338 return false;
3339 }
3340
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06003341 auto have_glsl_shader = device_extensions.vk_nv_glsl_shader;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003342
3343 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski7767ad82019-03-09 13:35:25 -07003344 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 -06003345 "VUID-VkShaderModuleCreateInfo-pCode-01376",
3346 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3347 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003348 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003349 auto cache = GetValidationCacheInfo(pCreateInfo);
3350 uint32_t hash = 0;
3351 if (cache) {
3352 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003353 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003354 }
3355
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003356 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3357 // the default values will be used during validation.
Jeremy Hayes0be25de2019-09-11 18:13:49 -06003358 spv_target_env spirv_environment = SPV_ENV_VULKAN_1_0;
3359 if (api_version >= VK_API_VERSION_1_1) {
Jesse Halla0389fc2019-09-25 16:46:21 -05003360 if (device_extensions.vk_khr_spirv_1_4) {
3361 spirv_environment = SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3362 } else {
3363 spirv_environment = SPV_ENV_VULKAN_1_1;
3364 }
Jeremy Hayes0be25de2019-09-11 18:13:49 -06003365 }
Dave Houlton0ea2d012018-06-21 14:00:26 -06003366 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003367 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003368 spv_diagnostic diag = nullptr;
Karl Schultzfda1b382018-08-08 18:56:11 -06003369 spv_validator_options options = spvValidatorOptionsCreate();
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06003370 if (device_extensions.vk_khr_relaxed_block_layout) {
Karl Schultzfda1b382018-08-08 18:56:11 -06003371 spvValidatorOptionsSetRelaxBlockLayout(options, true);
3372 }
Graeme Leese9b6a1522019-06-07 20:49:45 +01003373 if (device_extensions.vk_khr_uniform_buffer_standard_layout &&
Piers Daniell41b8c5d2020-01-10 15:42:00 -07003374 enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
Graeme Leese9b6a1522019-06-07 20:49:45 +01003375 spvValidatorOptionsSetUniformBufferStandardLayout(options, true);
3376 }
Piers Daniell41b8c5d2020-01-10 15:42:00 -07003377 if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) {
Tobias Hector6a0ece72018-12-10 12:24:05 +00003378 spvValidatorOptionsSetScalarBlockLayout(options, true);
3379 }
Karl Schultzfda1b382018-08-08 18:56:11 -06003380 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003381 if (spv_valid != SPV_SUCCESS) {
3382 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski7767ad82019-03-09 13:35:25 -07003383 skip |=
3384 log_msg(report_data, spv_valid == SPV_WARNING ? VK_DEBUG_REPORT_WARNING_BIT_EXT : VK_DEBUG_REPORT_ERROR_BIT_EXT,
3385 VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_InconsistentSpirv,
3386 "SPIR-V module not valid: %s", diag && diag->error ? diag->error : "(no error text)");
Chris Forbes4ae55b32017-06-09 14:42:56 -07003387 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003388 } else {
3389 if (cache) {
3390 cache->Insert(hash);
3391 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003392 }
3393
Karl Schultzfda1b382018-08-08 18:56:11 -06003394 spvValidatorOptionsDestroy(options);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003395 spvDiagnosticDestroy(diag);
3396 spvContextDestroy(ctx);
3397 }
3398
Chris Forbes4ae55b32017-06-09 14:42:56 -07003399 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003400}
3401
John Zulaufac4c6e12019-07-01 16:05:58 -06003402bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003403 bool skip = false;
3404 uint32_t local_size_x = 0;
3405 uint32_t local_size_y = 0;
3406 uint32_t local_size_z = 0;
3407 if (FindLocalSize(shader, local_size_x, local_size_y, local_size_z)) {
3408 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003409 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3410 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3411 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
3412 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3413 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06003414 }
3415 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003416 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3417 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3418 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
3419 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3420 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06003421 }
3422 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
locke-lunarg9edc2812019-06-17 23:18:52 -06003423 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3424 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
3425 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
3426 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x,
3427 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06003428 }
3429
3430 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3431 uint64_t invocations = local_size_x * local_size_y;
3432 // Prevent overflow.
3433 bool fail = false;
3434 if (invocations > UINT32_MAX || invocations > limit) {
3435 fail = true;
3436 }
3437 if (!fail) {
3438 invocations *= local_size_z;
3439 if (invocations > UINT32_MAX || invocations > limit) {
3440 fail = true;
3441 }
3442 }
3443 if (fail) {
3444 skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT,
3445 HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations",
locke-lunarg9edc2812019-06-17 23:18:52 -06003446 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
Lockeaa8fdc02019-04-02 11:59:20 -06003447 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
3448 report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, local_size_y, local_size_z,
3449 limit);
3450 }
3451 }
3452 return skip;
3453}