blob: 0a21d7832d8030520851ebb924fe7689a3bf51e8 [file] [log] [blame]
sfricke-samsung691299b2021-01-01 20:48:48 -08001/* Copyright (c) 2015-2021 The Khronos Group Inc.
2 * Copyright (c) 2015-2021 Valve Corporation
3 * Copyright (c) 2015-2021 LunarG, Inc.
4 * Copyright (C) 2015-2021 Google Inc.
Tobias Hector6663c9b2020-11-05 10:18:02 +00005 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Chris Forbes47567b72017-06-09 12:09:45 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Chris Forbes <chrisf@ijw.co.nz>
Dave Houlton51653902018-06-22 17:32:13 -060020 * Author: Dave Houlton <daveh@lunarg.com>
Tobias Hector6663c9b2020-11-05 10:18:02 +000021 * Author: Tobias Hector <tobias.hector@amd.com>
Chris Forbes47567b72017-06-09 12:09:45 -070022 */
23
Petr Kraus25810d02019-08-27 17:41:15 +020024#include "shader_validation.h"
25
Chris Forbes47567b72017-06-09 12:09:45 -070026#include <cassert>
Petr Kraus25810d02019-08-27 17:41:15 +020027#include <cinttypes>
Jeff Bolzf234bf82019-11-04 14:07:15 -060028#include <cmath>
Chris Forbes47567b72017-06-09 12:09:45 -070029#include <sstream>
Petr Kraus25810d02019-08-27 17:41:15 +020030#include <string>
Petr Kraus25810d02019-08-27 17:41:15 +020031#include <vector>
32
Mark Lobodzinski102687e2020-04-28 11:03:28 -060033#include <spirv/unified1/spirv.hpp>
Chris Forbes47567b72017-06-09 12:09:45 -070034#include "vk_enum_string_helper.h"
Chris Forbes47567b72017-06-09 12:09:45 -070035#include "vk_layer_data.h"
Chris Forbes47567b72017-06-09 12:09:45 -070036#include "vk_layer_utils.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070037#include "chassis.h"
Chris Forbes47567b72017-06-09 12:09:45 -070038#include "core_validation.h"
Petr Kraus25810d02019-08-27 17:41:15 +020039
Chris Forbes9a61e082017-07-24 15:35:29 -070040#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070041
Chris Forbes47567b72017-06-09 12:09:45 -070042static shader_stage_attributes shader_stage_attribs[] = {
Ari Suonpaa696b3432019-03-11 14:02:57 +020043 {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT},
44 {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT},
45 {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT},
46 {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT},
47 {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT},
Chris Forbes47567b72017-06-09 12:09:45 -070048};
49
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060050static bool IsNarrowNumericType(spirv_inst_iter type) {
Chris Forbes47567b72017-06-09 12:09:45 -070051 if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false;
52 return type.word(2) < 64;
53}
54
Mark Lobodzinski3c59d972019-04-25 11:28:14 -060055static 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 -060056 bool b_arrayed, bool relaxed) {
Chris Forbes47567b72017-06-09 12:09:45 -070057 // Walk two type trees together, and complain about differences
58 auto a_insn = a->get_def(a_type);
59 auto b_insn = b->get_def(b_type);
60 assert(a_insn != a->end());
61 assert(b_insn != b->end());
62
Chris Forbes062f1222018-08-21 15:34:15 -070063 // Ignore runtime-sized arrays-- they cannot appear in these interfaces.
64
Chris Forbes47567b72017-06-09 12:09:45 -070065 if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060066 return TypesMatch(a, b, a_insn.word(2), b_type, false, b_arrayed, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -070067 }
68
69 if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) {
70 // 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 -060071 return TypesMatch(a, b, a_type, b_insn.word(2), a_arrayed, false, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -070072 }
73
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060074 if (a_insn.opcode() == spv::OpTypeVector && relaxed && IsNarrowNumericType(b_insn)) {
75 return TypesMatch(a, b, a_insn.word(2), b_type, a_arrayed, b_arrayed, false);
Chris Forbes47567b72017-06-09 12:09:45 -070076 }
77
78 if (a_insn.opcode() != b_insn.opcode()) {
79 return false;
80 }
81
82 if (a_insn.opcode() == spv::OpTypePointer) {
83 // Match on pointee type. storage class is expected to differ
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060084 return TypesMatch(a, b, a_insn.word(3), b_insn.word(3), a_arrayed, b_arrayed, relaxed);
Chris Forbes47567b72017-06-09 12:09:45 -070085 }
86
87 if (a_arrayed || b_arrayed) {
88 // If we havent resolved array-of-verts by here, we're not going to.
89 return false;
90 }
91
92 switch (a_insn.opcode()) {
93 case spv::OpTypeBool:
94 return true;
95 case spv::OpTypeInt:
96 // Match on width, signedness
97 return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3);
98 case spv::OpTypeFloat:
99 // Match on width
100 return a_insn.word(2) == b_insn.word(2);
101 case spv::OpTypeVector:
102 // Match on element type, count.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600103 if (!TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false;
104 if (relaxed && IsNarrowNumericType(a->get_def(a_insn.word(2)))) {
Chris Forbes47567b72017-06-09 12:09:45 -0700105 return a_insn.word(3) >= b_insn.word(3);
106 } else {
107 return a_insn.word(3) == b_insn.word(3);
108 }
109 case spv::OpTypeMatrix:
110 // Match on element type, count.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600111 return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
Dave Houltona9df0ce2018-02-07 10:51:23 -0700112 a_insn.word(3) == b_insn.word(3);
Chris Forbes47567b72017-06-09 12:09:45 -0700113 case spv::OpTypeArray:
114 // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from
115 // 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 -0600116 return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) &&
sfricke-samsung962cad92021-04-13 00:46:29 -0700117 a->GetConstantValueById(a_insn.word(3)) == b->GetConstantValueById(b_insn.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700118 case spv::OpTypeStruct:
119 // Match on all element types
Dave Houltona9df0ce2018-02-07 10:51:23 -0700120 {
121 if (a_insn.len() != b_insn.len()) {
122 return false; // Structs cannot match if member counts differ
Chris Forbes47567b72017-06-09 12:09:45 -0700123 }
Chris Forbes47567b72017-06-09 12:09:45 -0700124
Dave Houltona9df0ce2018-02-07 10:51:23 -0700125 for (unsigned i = 2; i < a_insn.len(); i++) {
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600126 if (!TypesMatch(a, b, a_insn.word(i), b_insn.word(i), a_arrayed, b_arrayed, false)) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700127 return false;
128 }
129 }
130
131 return true;
132 }
Chris Forbes47567b72017-06-09 12:09:45 -0700133 default:
134 // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match.
135 return false;
136 }
137}
138
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600139static unsigned GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -0700140 switch (format) {
141 case VK_FORMAT_R64G64B64A64_SFLOAT:
142 case VK_FORMAT_R64G64B64A64_SINT:
143 case VK_FORMAT_R64G64B64A64_UINT:
144 case VK_FORMAT_R64G64B64_SFLOAT:
145 case VK_FORMAT_R64G64B64_SINT:
146 case VK_FORMAT_R64G64B64_UINT:
147 return 2;
148 default:
149 return 1;
150 }
151}
152
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600153static unsigned GetFormatType(VkFormat fmt) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700154 if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT;
155 if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT;
156 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
157 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700158 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
159 return FORMAT_TYPE_FLOAT;
160}
161
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600162static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700163 uint32_t bit_pos = uint32_t(u_ffs(stage));
164 return bit_pos - 1;
165}
166
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700167bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700168 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
169 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700170 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700171 bool skip = false;
172
173 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
174 auto desc = &vi->pVertexBindingDescriptions[i];
175 auto &binding = bindings[desc->binding];
176 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600177 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700178 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
179 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700180 } else {
181 binding = desc;
182 }
183 }
184
185 return skip;
186}
187
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700188bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs,
189 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700190 bool skip = false;
191
sfricke-samsung962cad92021-04-13 00:46:29 -0700192 const auto inputs = vs->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700193
194 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200195 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700196 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200197 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
198 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
199 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700200 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
201 }
202 }
203 }
204
Petr Kraus25810d02019-08-27 17:41:15 +0200205 struct AttribInputPair {
206 const VkVertexInputAttributeDescription *attrib = nullptr;
207 const interface_var *input = nullptr;
208 };
209 std::map<uint32_t, AttribInputPair> location_map;
210 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
211 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700212
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400213 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200214 const auto location = location_it.first;
215 const auto attrib = location_it.second.attrib;
216 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600217
Petr Kraus25810d02019-08-27 17:41:15 +0200218 if (attrib && !input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600219 skip |= LogPerformanceWarning(vs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700220 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200221 } else if (!attrib && input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600222 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700223 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200224 } else if (attrib && input) {
225 const auto attrib_type = GetFormatType(attrib->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700226 const auto input_type = vs->GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700227
228 // Type checking
229 if (!(attrib_type & input_type)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600230 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700231 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sfricke-samsung962cad92021-04-13 00:46:29 -0700232 string_VkFormat(attrib->format), location, vs->DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700233 }
Petr Kraus25810d02019-08-27 17:41:15 +0200234 } else { // !attrib && !input
235 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700236 }
237 }
238
239 return skip;
240}
241
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700242bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint,
243 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200244 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700245
Petr Kraus25810d02019-08-27 17:41:15 +0200246 const auto rpci = pipeline->rp_state->createInfo.ptr();
247
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600248 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800249 const VkAttachmentReference2 *reference = nullptr;
250 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600251 const interface_var *output = nullptr;
252 };
253 std::map<uint32_t, Attachment> location_map;
254
Petr Kraus25810d02019-08-27 17:41:15 +0200255 const auto subpass = rpci->pSubpasses[subpass_index];
256 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600257 auto const &reference = subpass.pColorAttachments[i];
258 location_map[i].reference = &reference;
259 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
260 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
261 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
Chris Forbes47567b72017-06-09 12:09:45 -0700262 }
263 }
264
Chris Forbes47567b72017-06-09 12:09:45 -0700265 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
266
sfricke-samsung962cad92021-04-13 00:46:29 -0700267 const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600268 for (const auto &output_it : outputs) {
269 auto const location = output_it.first.first;
270 location_map[location].output = &output_it.second;
271 }
Chris Forbes47567b72017-06-09 12:09:45 -0700272
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700273 const bool alpha_to_coverage_enabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL &&
274 pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700275
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400276 for (const auto &location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600277 const auto reference = location_it.second.reference;
278 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
279 continue;
280 }
281
Petr Kraus25810d02019-08-27 17:41:15 +0200282 const auto location = location_it.first;
283 const auto attachment = location_it.second.attachment;
284 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +0200285 if (attachment && !output) {
286 if (pipeline->attachments[location].colorWriteMask != 0) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600287 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700288 "Attachment %" PRIu32
289 " not written by fragment shader; undefined values will be written to attachment",
290 location);
Petr Kraus25810d02019-08-27 17:41:15 +0200291 }
292 } else if (!attachment && output) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700293 if (!(alpha_to_coverage_enabled && location == 0)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600294 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700295 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200296 }
Petr Kraus25810d02019-08-27 17:41:15 +0200297 } else if (attachment && output) {
298 const auto attachment_type = GetFormatType(attachment->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700299 const auto output_type = fs->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700300
301 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +0200302 if (!(output_type & attachment_type)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700303 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600304 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700305 "Attachment %" PRIu32
306 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sfricke-samsung962cad92021-04-13 00:46:29 -0700307 location, string_VkFormat(attachment->format), fs->DescribeType(output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700308 }
Petr Kraus25810d02019-08-27 17:41:15 +0200309 } else { // !attachment && !output
310 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700311 }
312 }
313
Petr Kraus25810d02019-08-27 17:41:15 +0200314 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700315 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
sfricke-samsung962cad92021-04-13 00:46:29 -0700316 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700317 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600318 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700319 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200320 }
321
Chris Forbes47567b72017-06-09 12:09:45 -0700322 return skip;
323}
324
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600325PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
326 const shader_struct_member &push_constant_used_in_shader,
327 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600328 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600329 const auto used_bytes_size = used_bytes->size();
330 if (used_bytes_size == 0) return PC_Byte_Updated;
331
332 const auto push_constant_data_update_size = push_constant_data_update.size();
333 const auto *data = push_constant_data_update.data();
334 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
335 if (used_bytes_size <= push_constant_data_update_size) {
336 return PC_Byte_Updated;
337 }
338 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
339
340 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
341 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
342 return PC_Byte_Updated;
343 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600344 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600345
locke-lunargde3f0fa2020-09-10 11:55:31 -0600346 uint32_t i = 0;
347 for (const auto used : *used_bytes) {
348 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600349 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600350 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600351 return PC_Byte_Not_Set;
352 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600353 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600354 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600355 }
356 }
357 ++i;
358 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600359 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600360}
361
362bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src,
sfricke-samsung7699b912021-04-12 23:01:51 -0700363 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700364 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700365 // Temp workaround to prevent false positive errors
366 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
367 if (src->multiple_entry_points) {
368 return skip;
369 }
370
Chris Forbes47567b72017-06-09 12:09:45 -0700371 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
sfricke-samsung962cad92021-04-13 00:46:29 -0700372 const auto *entrypoint = src->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600373 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
374 return skip;
375 }
376 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700377
locke-lunargde3f0fa2020-09-10 11:55:31 -0600378 bool found_stage = false;
379 for (auto const &range : *push_constant_ranges) {
380 if (range.stageFlags & pStage->stage) {
381 found_stage = true;
382 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600383 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600384 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600385 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600386 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600387 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600388 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600389 const auto ret =
390 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700391
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600392 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600393 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600394 LogObjectList objlist(src->vk_shader_module());
395 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700396 skip |= LogError(objlist, vuid, "Push constant buffer:%s in %s is out of range in %s.", loc_descr.c_str(),
locke-lunargde3f0fa2020-09-10 11:55:31 -0600397 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600398 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600399 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700400 }
401 }
402 }
403
locke-lunargde3f0fa2020-09-10 11:55:31 -0600404 if (!found_stage) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600405 LogObjectList objlist(src->vk_shader_module());
406 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700407 skip |= LogError(objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600408 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module()).c_str(),
409 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700410 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700411 }
Chris Forbes47567b72017-06-09 12:09:45 -0700412 return skip;
413}
414
sfricke-samsungcfb44592021-07-25 00:36:28 -0700415bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700416 bool skip = false;
417
418 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700419 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700420 return skip;
421 }
422
sfricke-samsungcfb44592021-07-25 00:36:28 -0700423 // Find all builtin from just the interface variables
424 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700425 auto insn = src->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700426 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700427 const decoration_set decorations = src->get_decorations(insn.word(2));
428
sfricke-samsungcfb44592021-07-25 00:36:28 -0700429 // Currently don't need to search in structs
430 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700431 auto type_pointer = src->get_def(insn.word(1));
432 assert(type_pointer.opcode() == spv::OpTypePointer);
433
434 auto type = src->get_def(type_pointer.word(3));
435 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700436 uint32_t length = static_cast<uint32_t>(src->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700437 // Handles both the input and output sampleMask
438 if (length > phys_dev_props.limits.maxSampleMaskWords) {
439 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
440 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
441 "maxSampleMaskWords of %u in %s.",
442 length, phys_dev_props.limits.maxSampleMaskWords,
443 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700444 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700445 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700446 }
447 }
448 }
449
450 return skip;
451}
452
Chris Forbes47567b72017-06-09 12:09:45 -0700453// Validate that data for each specialization entry is fully contained within the buffer.
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700454bool CoreChecks::ValidateSpecializationOffsets(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700455 bool skip = false;
456
457 VkSpecializationInfo const *spec = info->pSpecializationInfo;
458
459 if (spec) {
460 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600461 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700462 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
463 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200464 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700465 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
466 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600467
468 continue;
469 }
Chris Forbes47567b72017-06-09 12:09:45 -0700470 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700471 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
472 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200473 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700474 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
475 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700476 }
477 }
478 }
479
480 return skip;
481}
482
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500483// TODO (jbolz): Can this return a const reference?
sourav parmarcd5fb182020-07-17 12:58:44 -0700484static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count,
485 bool is_khr) {
Chris Forbes47567b72017-06-09 12:09:45 -0700486 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800487 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700488 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500489 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700490
491 // 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 -0500492 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
493 if (type.opcode() == spv::OpTypeRuntimeArray) {
494 descriptor_count = 0;
495 type = module->get_def(type.word(2));
496 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700497 descriptor_count *= module->GetConstantValueById(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700498 type = module->get_def(type.word(2));
499 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800500 if (type.word(2) == spv::StorageClassStorageBuffer) {
501 is_storage_buffer = true;
502 }
Chris Forbes47567b72017-06-09 12:09:45 -0700503 type = module->get_def(type.word(3));
504 }
505 }
506
507 switch (type.opcode()) {
508 case spv::OpTypeStruct: {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800509 for (auto insn : module->decoration_inst) {
510 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700511 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800512 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500513 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
514 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
515 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800516 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500517 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
518 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
519 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
520 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800521 }
Chris Forbes47567b72017-06-09 12:09:45 -0700522 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500523 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
524 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
525 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700526 }
527 }
528 }
529
530 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500531 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700532 }
533
534 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500535 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
536 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
537 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700538
Chris Forbes73c00bf2018-06-22 16:28:06 -0700539 case spv::OpTypeSampledImage: {
540 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
541 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
542 auto image_type = module->get_def(type.word(2));
543 auto dim = image_type.word(3);
544 auto sampled = image_type.word(7);
545 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500546 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
547 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700548 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700549 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500550 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
551 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700552
553 case spv::OpTypeImage: {
554 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
555 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
556 auto dim = type.word(3);
557 auto sampled = type.word(7);
558
559 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500560 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
561 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700562 } else if (dim == spv::DimBuffer) {
563 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500564 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
565 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700566 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500567 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
568 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700569 }
570 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500571 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
572 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
573 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700574 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500575 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
576 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700577 }
578 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600579 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700580 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
581 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500582 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700583
584 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
585 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500586 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700587 }
588}
589
Jeff Bolze54ae892018-09-08 12:16:29 -0500590static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700591 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500592 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
593 if (ss.tellp()) ss << ", ";
594 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700595 }
596 return ss.str();
597}
598
sfricke-samsung0065ce02020-12-03 22:46:37 -0800599bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500600 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800601 if (LogError(device, vuid, "Shader requires flag %s set in %s but it is not set on the device", flag, structure)) {
Jeff Bolzee743412019-06-20 22:24:32 -0500602 return true;
603 }
604 }
605
606 return false;
607}
608
sfricke-samsung0065ce02020-12-03 22:46:37 -0800609bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700610 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800611 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700612 return true;
613 }
614 }
615
616 return false;
617}
618
locke-lunarg63e4daf2020-08-17 17:53:25 -0600619bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
620 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500621 bool skip = false;
622
locke-lunarg63e4daf2020-08-17 17:53:25 -0600623 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800624 switch (stage) {
625 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -0600626 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
627 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
628 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
629 case VK_SHADER_STAGE_MISS_BIT_NV:
630 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
631 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
632 case VK_SHADER_STAGE_TASK_BIT_NV:
633 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -0800634 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -0600635 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -0800636 break;
637 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800638 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
639 kVUID_Core_Shader_FeatureNotEnabled);
Chris Forbes349b3132018-03-07 11:38:08 -0800640 break;
641 default:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800642 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
643 kVUID_Core_Shader_FeatureNotEnabled);
Chris Forbes349b3132018-03-07 11:38:08 -0800644 break;
645 }
646 }
647
Chris Forbes47567b72017-06-09 12:09:45 -0700648 return skip;
649}
650
sfricke-samsung94167ca2021-02-26 04:14:59 -0800651bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
652 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500653 bool skip = false;
654
sfricke-samsung94167ca2021-02-26 04:14:59 -0800655 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
656 if (GroupOperation(insn.opcode()) == true) {
657 // Check the quad operations.
658 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
659 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
660 skip |= RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
661 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages",
662 kVUID_Core_Shader_FeatureNotEnabled);
sfricke-samsung0065ce02020-12-03 22:46:37 -0800663 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800664 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500665
sfricke-samsung94167ca2021-02-26 04:14:59 -0800666 uint32_t scope_type = spv::ScopeMax;
667 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
668 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
669 scope_type = spv::ScopeSubgroup;
670 } else {
671 // "All <id> used for Scope <id> must be of an OpConstant"
672 auto scope_id = module->get_def(insn.word(3));
673 scope_type = scope_id.word(3);
674 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800675
sfricke-samsung94167ca2021-02-26 04:14:59 -0800676 if (scope_type == spv::ScopeSubgroup) {
677 // "Group operations with subgroup scope" must have stage support
678 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
679 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung0065ce02020-12-03 22:46:37 -0800680 "VkPhysicalDeviceSubgroupProperties::supportedStages", kVUID_Core_Shader_ExceedDeviceLimit);
sfricke-samsung94167ca2021-02-26 04:14:59 -0800681 }
682
683 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
684 auto type = module->get_def(insn.word(1));
685
686 if (type.opcode() == spv::OpTypeVector) {
687 // Get the element type
688 type = module->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800689 }
690
sfricke-samsung94167ca2021-02-26 04:14:59 -0800691 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800692 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
693 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500694
sfricke-samsung0065ce02020-12-03 22:46:37 -0800695 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
696 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
697 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
698 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
699 kVUID_Core_Shader_FeatureNotEnabled);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500700 }
701 }
702 }
Jeff Bolzee743412019-06-20 22:24:32 -0500703 }
704
705 return skip;
706}
707
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600708bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600709 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200710 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
711 pStage->stage == VK_SHADER_STAGE_ALL) {
712 return false;
713 }
714
715 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700716 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200717
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700718 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200719 struct Variable {
720 uint32_t baseTypePtrID;
721 uint32_t ID;
722 uint32_t storageClass;
723 };
724 std::vector<Variable> variables;
725
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700726 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700727 bool is_iso_lines = false;
728 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500729
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700730 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600731
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200732 for (auto insn : *src) {
733 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500734 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200735 case spv::OpDecorate:
736 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500737 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700738 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200739 break;
740 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200741 default:
742 break;
743 }
744 break;
745 // Find all input and output variables
746 case spv::OpVariable: {
747 Variable var = {};
748 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600749 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
750 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700751 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200752 var.baseTypePtrID = insn.word(1);
753 var.ID = insn.word(2);
754 variables.push_back(var);
755 }
756 break;
757 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500758 case spv::OpExecutionMode:
759 if (insn.word(1) == entrypoint.word(2)) {
760 switch (insn.word(2)) {
761 default:
762 break;
763 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700764 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500765 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700766 case spv::ExecutionModeIsolines:
767 is_iso_lines = true;
768 break;
769 case spv::ExecutionModePointMode:
770 is_point_mode = true;
771 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500772 }
773 }
774 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200775 default:
776 break;
777 }
778 }
779
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500780 bool strip_output_array_level =
781 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
782 bool strip_input_array_level =
783 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
784 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
785
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700786 uint32_t num_comp_in = 0, num_comp_out = 0;
787 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600788
sfricke-samsung962cad92021-04-13 00:46:29 -0700789 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
790 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600791
792 // Find max component location used for input variables.
793 for (auto &var : inputs) {
794 int location = var.first.first;
795 int component = var.first.second;
796 interface_var &iv = var.second;
797
798 // Only need to look at the first location, since we use the type's whole size
799 if (iv.offset != 0) {
800 continue;
801 }
802
803 if (iv.is_patch) {
804 continue;
805 }
806
sfricke-samsung962cad92021-04-13 00:46:29 -0700807 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700808 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600809 }
810
811 // Find max component location used for output variables.
812 for (auto &var : outputs) {
813 int location = var.first.first;
814 int component = var.first.second;
815 interface_var &iv = var.second;
816
817 // Only need to look at the first location, since we use the type's whole size
818 if (iv.offset != 0) {
819 continue;
820 }
821
822 if (iv.is_patch) {
823 continue;
824 }
825
sfricke-samsung962cad92021-04-13 00:46:29 -0700826 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700827 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600828 }
829
830 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
831 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700832 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
833 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200834 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500835 // Check if the variable is a patch. Patches can also be members of blocks,
836 // but if they are then the top-level arrayness has already been stripped
837 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700838 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200839
840 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700841 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200842 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700843 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200844 }
845 }
846
847 switch (pStage->stage) {
848 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700849 if (num_comp_out > limits.maxVertexOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600850 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700851 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
852 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
853 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700854 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200855 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700856 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600857 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700858 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
859 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
860 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600861 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200862 break;
863
864 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700865 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600866 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700867 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
868 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
869 "components by %u components",
870 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700871 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200872 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700873 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600874 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600875 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700876 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
877 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
878 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600879 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700880 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600881 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700882 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
883 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
884 "components by %u components",
885 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700886 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200887 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700888 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600889 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600890 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700891 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
892 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
893 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600894 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200895 break;
896
897 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700898 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600899 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700900 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
901 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
902 "components by %u components",
903 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700904 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200905 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700906 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600907 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600908 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700909 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
910 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
911 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600912 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700913 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600914 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700915 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
916 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
917 "components by %u components",
918 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700919 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200920 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700921 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600922 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600923 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700924 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
925 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
926 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600927 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700928 // Portability validation
929 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
930 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600931 skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_Isolines,
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700932 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
933 " is using abstract patch type IsoLines, but this is not supported on this platform");
934 }
935 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600936 skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_PointMode,
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700937 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
938 " is using abstract patch type PointMode, but this is not supported on this platform");
939 }
940 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200941 break;
942
943 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700944 if (num_comp_in > limits.maxGeometryInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600945 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700946 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
947 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
948 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700949 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200950 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700951 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600952 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700953 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
954 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
955 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600956 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700957 if (num_comp_out > limits.maxGeometryOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600958 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700959 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
960 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
961 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700962 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200963 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700964 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600965 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700966 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
967 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
968 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600969 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700970 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600971 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700972 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
973 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
974 "components by %u components",
975 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700976 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500977 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200978 break;
979
980 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700981 if (num_comp_in > limits.maxFragmentInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600982 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700983 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
984 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
985 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700986 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200987 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700988 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600989 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700990 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
991 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
992 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600993 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200994 break;
995
Jeff Bolz148d94e2018-12-13 21:25:56 -0600996 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
997 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
998 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
999 case VK_SHADER_STAGE_MISS_BIT_NV:
1000 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1001 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1002 case VK_SHADER_STAGE_TASK_BIT_NV:
1003 case VK_SHADER_STAGE_MESH_BIT_NV:
1004 break;
1005
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001006 default:
1007 assert(false); // This should never happen
1008 }
1009 return skip;
1010}
1011
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001012bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const {
1013 bool skip = false;
1014
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001015 // Got through all ImageRead/Write instructions
1016 for (auto insn : *src) {
1017 switch (insn.opcode()) {
1018 case spv::OpImageSparseRead:
1019 case spv::OpImageRead: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001020 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001021 if (type_def != src->end()) {
1022 if (type_def.word(8) == spv::ImageFormatUnknown) {
1023 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1024 "shaderStorageImageReadWithoutFormat",
1025 kVUID_Features_shaderStorageImageReadWithoutFormat);
1026 }
1027 } else {
1028 skip |= LogWarning(device, kVUIDUndefined,
1029 "Cannot find image definition (id = %" PRIu32 ")",
1030 insn.word(3));
1031 }
1032 break;
1033 }
1034 case spv::OpImageWrite: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001035 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001036 if (type_def != src->end()) {
1037 if (type_def.word(8) == spv::ImageFormatUnknown) {
1038 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1039 "shaderStorageImageWriteWithoutFormat",
1040 kVUID_Features_shaderStorageImageWriteWithoutFormat);
1041 }
1042 } else {
1043 skip |= LogWarning(device, kVUIDUndefined,
1044 "Cannot find image definition (id = %" PRIu32 ")",
1045 insn.word(1));
1046 }
1047 break;
1048 }
1049
1050 }
1051 }
1052
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001053 // Go through all variables for images and check decorations
1054 for (auto insn : *src) {
1055 if (insn.opcode() != spv::OpVariable)
1056 continue;
1057
1058 uint32_t var = insn.word(2);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001059 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001060 if (type_def == src->end())
1061 continue;
Corentin Wallez91f8b6d2021-07-23 10:11:31 +02001062 // Only check storage images
1063 if (type_def.word(7) != 2) continue;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001064 if (type_def.word(8) != spv::ImageFormatUnknown)
1065 continue;
1066
1067 decoration_set img_decorations = src->get_decorations(var);
1068
1069 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1070 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1071 skip |= LogError(device,
1072 kVUID_Features_shaderStorageImageReadWithoutFormat_NonReadable,
1073 "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32 " "
1074 " without format not marked a NonReadable", var);
1075 }
1076
1077 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1078 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1079 skip |= LogError(device,
1080 kVUID_Features_shaderStorageImageWriteWithoutFormat_NonWritable,
1081 "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32 " "
1082 "without format not marked a NonWritable", var);
1083 }
1084 }
1085
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001086 return skip;
1087}
1088
sfricke-samsungdc96f302020-03-18 20:42:10 -07001089bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1090 bool skip = false;
1091 uint32_t total_resources = 0;
1092
1093 // Only currently testing for graphics and compute pipelines
1094 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1095 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1096 return false;
1097 }
1098
1099 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1100 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
1101 total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].colorAttachmentCount;
1102 }
1103
1104 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1105 // input from CreatePipeline and CreatePipelineLayout level
1106 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1107 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1108 continue;
1109 }
1110
1111 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1112 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1113 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1114 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1115 // Check only descriptor types listed in maxPerStageResources description in spec
1116 switch (binding->descriptorType) {
1117 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1118 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1119 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1120 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1121 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1122 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1123 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1124 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1125 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1126 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1127 total_resources += binding->descriptorCount;
1128 break;
1129 default:
1130 break;
1131 }
1132 }
1133 }
1134 }
1135
1136 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1137 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1138 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001139 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001140 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1141 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1142 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1143 }
1144
1145 return skip;
1146}
1147
Jeff Bolze4356752019-03-07 11:23:46 -06001148// copy the specialization constant value into buf, if it is present
1149void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1150 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1151
1152 if (spec && spec_id < spec->mapEntryCount) {
1153 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1154 }
1155}
1156
1157// Fill in value with the constant or specialization constant value, if available.
1158// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001159static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001160 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001161 auto type_id = src->get_def(insn.word(1));
1162 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1163 return false;
1164 }
1165 switch (insn.opcode()) {
1166 case spv::OpSpecConstant:
1167 *value = insn.word(3);
1168 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1169 return true;
1170 case spv::OpConstant:
1171 *value = insn.word(3);
1172 return true;
1173 default:
1174 return false;
1175 }
1176}
1177
1178// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001179VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001180 switch (insn.opcode()) {
1181 case spv::OpTypeInt:
1182 switch (insn.word(2)) {
1183 case 8:
1184 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1185 case 16:
1186 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1187 case 32:
1188 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1189 case 64:
1190 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1191 default:
1192 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1193 }
1194 case spv::OpTypeFloat:
1195 switch (insn.word(2)) {
1196 case 16:
1197 return VK_COMPONENT_TYPE_FLOAT16_NV;
1198 case 32:
1199 return VK_COMPONENT_TYPE_FLOAT32_NV;
1200 case 64:
1201 return VK_COMPONENT_TYPE_FLOAT64_NV;
1202 default:
1203 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1204 }
1205 default:
1206 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1207 }
1208}
1209
1210// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1211// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001212bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001213 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001214 bool skip = false;
1215
1216 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001217 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001218 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001219 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001220
1221 struct CoopMatType {
1222 uint32_t scope, rows, cols;
1223 VkComponentTypeNV component_type;
1224 bool all_constant;
1225
1226 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1227
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001228 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001229 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001230 spirv_inst_iter insn = src->get_def(id);
1231 uint32_t component_type_id = insn.word(2);
1232 uint32_t scope_id = insn.word(3);
1233 uint32_t rows_id = insn.word(4);
1234 uint32_t cols_id = insn.word(5);
1235 auto component_type_iter = src->get_def(component_type_id);
1236 auto scope_iter = src->get_def(scope_id);
1237 auto rows_iter = src->get_def(rows_id);
1238 auto cols_iter = src->get_def(cols_id);
1239
1240 all_constant = true;
1241 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1242 all_constant = false;
1243 }
1244 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1245 all_constant = false;
1246 }
1247 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1248 all_constant = false;
1249 }
1250 component_type = GetComponentType(component_type_iter, src);
1251 }
1252 };
1253
1254 bool seen_coopmat_capability = false;
1255
1256 for (auto insn : *src) {
1257 // Whitelist instructions whose result can be a cooperative matrix type, and
1258 // keep track of their types. It would be nice if SPIRV-Headers generated code
1259 // to identify which instructions have a result type and result id. Lacking that,
1260 // this whitelist is based on the set of instructions that
1261 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1262 switch (insn.opcode()) {
1263 case spv::OpLoad:
1264 case spv::OpCooperativeMatrixLoadNV:
1265 case spv::OpCooperativeMatrixMulAddNV:
1266 case spv::OpSNegate:
1267 case spv::OpFNegate:
1268 case spv::OpIAdd:
1269 case spv::OpFAdd:
1270 case spv::OpISub:
1271 case spv::OpFSub:
1272 case spv::OpFDiv:
1273 case spv::OpSDiv:
1274 case spv::OpUDiv:
1275 case spv::OpMatrixTimesScalar:
1276 case spv::OpConstantComposite:
1277 case spv::OpCompositeConstruct:
1278 case spv::OpConvertFToU:
1279 case spv::OpConvertFToS:
1280 case spv::OpConvertSToF:
1281 case spv::OpConvertUToF:
1282 case spv::OpUConvert:
1283 case spv::OpSConvert:
1284 case spv::OpFConvert:
1285 id_to_type_id[insn.word(2)] = insn.word(1);
1286 break;
1287 default:
1288 break;
1289 }
1290
1291 switch (insn.opcode()) {
1292 case spv::OpDecorate:
1293 if (insn.word(2) == spv::DecorationSpecId) {
1294 id_to_spec_id[insn.word(1)] = insn.word(3);
1295 }
1296 break;
1297 case spv::OpCapability:
1298 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1299 seen_coopmat_capability = true;
1300
1301 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001302 skip |= LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001303 pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixSupportedStages,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001304 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1305 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001306 }
1307 }
1308 break;
1309 case spv::OpMemoryModel:
1310 // If the capability isn't enabled, don't bother with the rest of this function.
1311 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1312 if (!seen_coopmat_capability) {
1313 return skip;
1314 }
1315 break;
1316 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001317 CoopMatType m;
1318 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001319
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001320 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001321 // Validate that the type parameters are all supported for one of the
1322 // operands of a cooperative matrix property.
1323 bool valid = false;
1324 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001325 if (cooperative_matrix_properties[i].AType == m.component_type &&
1326 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1327 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001328 valid = true;
1329 break;
1330 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001331 if (cooperative_matrix_properties[i].BType == m.component_type &&
1332 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1333 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001334 valid = true;
1335 break;
1336 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001337 if (cooperative_matrix_properties[i].CType == m.component_type &&
1338 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1339 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001340 valid = true;
1341 break;
1342 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001343 if (cooperative_matrix_properties[i].DType == m.component_type &&
1344 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1345 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001346 valid = true;
1347 break;
1348 }
1349 }
1350 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001351 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001352 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1353 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001354 }
1355 }
1356 break;
1357 }
1358 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001359 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001360 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1361 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1362 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1363 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001364 // Couldn't find type of matrix
1365 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001366 break;
1367 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001368 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1369 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1370 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1371 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001372
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001373 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001374 // Validate that the type parameters are all supported for the same
1375 // cooperative matrix property.
1376 bool valid = false;
1377 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001378 if (cooperative_matrix_properties[i].AType == a.component_type &&
1379 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1380 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001381
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001382 cooperative_matrix_properties[i].BType == b.component_type &&
1383 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1384 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001385
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001386 cooperative_matrix_properties[i].CType == c.component_type &&
1387 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1388 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001389
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001390 cooperative_matrix_properties[i].DType == d.component_type &&
1391 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1392 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001393 valid = true;
1394 break;
1395 }
1396 }
1397 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001398 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001399 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1400 "VkCooperativeMatrixPropertiesNV",
1401 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001402 }
1403 }
1404 break;
1405 }
1406 default:
1407 break;
1408 }
1409 }
1410
1411 return skip;
1412}
1413
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001414bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1415 const PIPELINE_STATE *pipeline) const {
1416 bool skip = false;
1417
1418 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1419 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1420 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1421 for (auto insn : *src) {
1422 switch (insn.opcode()) {
1423 case spv::OpCapability:
1424 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1425 auto subpass_flags =
1426 (pipeline->rp_state == nullptr)
1427 ? 0
1428 : pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].flags;
1429 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1430 skip |=
1431 LogError(pipeline->pipeline(), kVUID_Core_Shader_ResolveQCOM_InvalidCapability,
1432 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1433 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1434 }
1435 }
1436 break;
1437 default:
1438 break;
1439 }
1440 }
1441 }
1442
1443 return skip;
1444}
1445
John Zulaufac4c6e12019-07-01 16:05:58 -06001446bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001447 auto entrypoint_id = entrypoint.word(2);
1448
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001449 // The first denorm execution mode encountered, along with its bit width.
1450 // Used to check if SeparateDenormSettings is respected.
1451 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001452
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001453 // The first rounding mode encountered, along with its bit width.
1454 // Used to check if SeparateRoundingModeSettings is respected.
1455 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001456
1457 bool skip = false;
1458
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001459 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001460 uint32_t invocations = 0;
1461
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001462 auto it = src->execution_mode_inst.find(entrypoint_id);
1463 if (it != src->execution_mode_inst.end()) {
1464 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001465 auto mode = insn.word(2);
1466 switch (mode) {
1467 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1468 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001469 if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) ||
1470 (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) ||
1471 (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001472 skip |= LogError(
1473 device, kVUID_Core_Shader_FeatureNotEnabled,
1474 "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device",
1475 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001476 }
1477 break;
1478 }
1479
1480 case spv::ExecutionModeDenormPreserve: {
1481 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001482 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) ||
1483 (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) ||
1484 (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001485 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1486 "Shader requires DenormPreserve for bit width %d but it is not enabled on the device",
1487 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001488 }
1489
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001490 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1491 // Register the first denorm execution mode found
1492 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001493 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001494 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001495 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001496 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001497 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1498 "Shader uses different denorm execution modes for 16 and 64-bit but "
1499 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001500 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001501 }
1502 break;
1503
Mike Schuchardt2df08912020-12-15 16:28:09 -08001504 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001505 break;
1506
Mike Schuchardt2df08912020-12-15 16:28:09 -08001507 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001508 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1509 "Shader uses different denorm execution modes for different bit widths but "
1510 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001511 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001512 break;
1513
1514 default:
1515 break;
1516 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001517 }
1518 break;
1519 }
1520
1521 case spv::ExecutionModeDenormFlushToZero: {
1522 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001523 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) ||
1524 (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) ||
1525 (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001526 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1527 "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device",
1528 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001529 }
1530
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001531 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1532 // Register the first denorm execution mode found
1533 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001534 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001535 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001536 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001537 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001538 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1539 "Shader uses different denorm execution modes for 16 and 64-bit but "
1540 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001541 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001542 }
1543 break;
1544
Mike Schuchardt2df08912020-12-15 16:28:09 -08001545 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001546 break;
1547
Mike Schuchardt2df08912020-12-15 16:28:09 -08001548 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001549 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1550 "Shader uses different denorm execution modes for different bit widths but "
1551 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001552 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001553 break;
1554
1555 default:
1556 break;
1557 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001558 }
1559 break;
1560 }
1561
1562 case spv::ExecutionModeRoundingModeRTE: {
1563 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001564 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) ||
1565 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) ||
1566 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001567 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1568 "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device",
1569 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001570 }
1571
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001572 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1573 // Register the first rounding mode found
1574 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001575 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001576 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001577 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001578 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001579 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1580 "Shader uses different rounding modes for 16 and 64-bit but "
1581 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001582 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001583 }
1584 break;
1585
Mike Schuchardt2df08912020-12-15 16:28:09 -08001586 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001587 break;
1588
Mike Schuchardt2df08912020-12-15 16:28:09 -08001589 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001590 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1591 "Shader uses different rounding modes for different bit widths but "
1592 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001593 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001594 break;
1595
1596 default:
1597 break;
1598 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001599 }
1600 break;
1601 }
1602
1603 case spv::ExecutionModeRoundingModeRTZ: {
1604 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001605 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) ||
1606 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) ||
1607 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001608 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1609 "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device",
1610 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001611 }
1612
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001613 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1614 // Register the first rounding mode found
1615 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001616 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001617 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001618 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001619 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001620 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1621 "Shader uses different rounding modes for 16 and 64-bit but "
1622 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001623 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001624 }
1625 break;
1626
Mike Schuchardt2df08912020-12-15 16:28:09 -08001627 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001628 break;
1629
Mike Schuchardt2df08912020-12-15 16:28:09 -08001630 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001631 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1632 "Shader uses different rounding modes for different bit widths but "
1633 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001634 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001635 break;
1636
1637 default:
1638 break;
1639 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001640 }
1641 break;
1642 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001643
1644 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001645 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001646 break;
1647 }
1648
1649 case spv::ExecutionModeInvocations: {
1650 invocations = insn.word(3);
1651 break;
1652 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001653 }
1654 }
1655 }
1656
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001657 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001658 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001659 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
1660 "Geometry shader entry point must have an OpExecutionMode instruction that "
1661 "specifies a maximum output vertex count that is greater than 0 and less "
1662 "than or equal to maxGeometryOutputVertices. "
1663 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001664 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001665 }
1666
1667 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001668 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
1669 "Geometry shader entry point must have an OpExecutionMode instruction that "
1670 "specifies an invocation count that is greater than 0 and less "
1671 "than or equal to maxGeometryShaderInvocations. "
1672 "Invocations=%d, maxGeometryShaderInvocations=%d",
1673 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001674 }
1675 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001676 return skip;
1677}
1678
Chris Forbes47567b72017-06-09 12:09:45 -07001679// For given pipelineLayout verify that the set_layout_node at slot.first
1680// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06001681static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06001682 descriptor_slot_t slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07001683 if (!pipelineLayout) return nullptr;
1684
1685 if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr;
1686
1687 return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second);
1688}
1689
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001690// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
1691// o If there is only a vertex shader : gl_PointSize must be written when using points
1692// o If there is a geometry or tessellation shader:
1693// - If shaderTessellationAndGeometryPointSize feature is enabled:
1694// * gl_PointSize must be written in the final geometry stage
1695// - If shaderTessellationAndGeometryPointSize feature is disabled:
1696// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001697bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06001698 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001699 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
1700 return false;
1701 }
1702
1703 bool pointsize_written = false;
1704 bool skip = false;
1705
1706 // Search for PointSize built-in decorations
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001707 for (auto set : src->builtin_decoration_list) {
1708 auto insn = src->at(set.offset);
1709 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001710 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001711 if (pointsize_written) {
1712 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001713 }
1714 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001715 }
1716
1717 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001718 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001719 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001720 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001721 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
1722 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001723 }
1724 } else if (!pointsize_written) {
1725 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001726 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001727 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
1728 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001729 }
1730 return skip;
1731}
John Zulauf14c355b2019-06-27 16:09:37 -06001732
Tobias Hector6663c9b2020-11-05 10:18:02 +00001733bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
1734 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
1735 bool primitiverate_written = false;
1736 bool viewportindex_written = false;
1737 bool viewportmask_written = false;
1738 bool skip = false;
1739
1740 // Check if the primitive shading rate is written
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001741 for (auto set : src->builtin_decoration_list) {
1742 auto insn = src->at(set.offset);
1743 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001744 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001745 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001746 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001747 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001748 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00001749 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001750 if (primitiverate_written && viewportindex_written && viewportmask_written) {
1751 break;
1752 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00001753 }
1754
Tony-LunarGd44844c2021-01-22 13:24:37 -07001755 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
1756 pipeline->graphicsPipelineCI.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00001757 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
1758 pipeline->graphicsPipelineCI.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001759 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001760 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
1761 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
1762 "multiple viewports "
1763 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
1764 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00001765 }
1766
1767 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001768 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00001769 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
1770 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
1771 "ViewportIndex built-ins,"
1772 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
1773 string_VkShaderStageFlagBits(stage));
1774 }
1775
1776 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001777 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00001778 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
1779 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
1780 "ViewportMaskNV built-ins,"
1781 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
1782 string_VkShaderStageFlagBits(stage));
1783 }
1784 }
1785 return skip;
1786}
1787
sfricke-samsung486a51e2021-01-02 00:10:15 -08001788// Validate runtime usage of various opcodes that depends on what Vulkan properties or features are exposed
sfricke-samsung94167ca2021-02-26 04:14:59 -08001789bool CoreChecks::ValidatePropertiesAndFeatures(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08001790 bool skip = false;
1791
sfricke-samsung94167ca2021-02-26 04:14:59 -08001792 switch (insn.opcode()) {
1793 case spv::OpReadClockKHR: {
1794 auto scope_id = module->get_def(insn.word(3));
1795 auto scope_type = scope_id.word(3);
1796 // if scope isn't Subgroup or Device, spirv-val will catch
1797 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_feature.shaderSubgroupClock == VK_FALSE)) {
1798 skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderSubgroupClock",
1799 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001800 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung94167ca2021-02-26 04:14:59 -08001801 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_feature.shaderDeviceClock == VK_FALSE)) {
1802 skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderDeviceClock",
1803 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001804 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08001805 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08001806 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08001807 }
1808 }
1809 return skip;
1810}
1811
John Zulauf14c355b2019-06-27 16:09:37 -06001812bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline,
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001813 const PipelineStageState &stage_state, const SHADER_MODULE_STATE *module,
John Zulaufac4c6e12019-07-01 16:05:58 -06001814 const spirv_inst_iter &entrypoint, bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06001815 bool skip = false;
1816
1817 // Check the module
1818 if (!module->has_valid_spirv) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001819 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
1820 "%s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001821 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(pStage->stage));
John Zulauf14c355b2019-06-27 16:09:37 -06001822 }
1823
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001824 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
1825 // specializations should be applied and validated.
1826 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
1827 pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) {
1828 // Gather the specialization-constant values.
1829 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07001830 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001831 std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; // note: this must be std:: to work with spvtools
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001832 id_value_map.reserve(specialization_info->mapEntryCount);
1833 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
1834 auto const &map_entry = specialization_info->pMapEntries[i];
sfricke-samsung033b0262021-07-09 00:53:06 -07001835 auto itr = module->spec_const_map.find(map_entry.constantID);
1836 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
1837 // behavior of the pipeline."
1838 if (itr != module->spec_const_map.cend()) {
1839 // Make sure map_entry.size matches the spec constant's size
1840 uint32_t spec_const_size = decoration_set::kInvalidValue;
1841 const auto def_ins = module->get_def(itr->second);
1842 const auto type_ins = module->get_def(def_ins.word(1));
1843 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
1844 switch (type_ins.opcode()) {
1845 case spv::OpTypeBool:
1846 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
1847 spec_const_size = sizeof(VkBool32);
1848 break;
1849 case spv::OpTypeInt:
1850 case spv::OpTypeFloat:
1851 spec_const_size = type_ins.word(2) / 8;
1852 break;
1853 default:
1854 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
1855 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
1856 break;
1857 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001858
sfricke-samsung033b0262021-07-09 00:53:06 -07001859 if (map_entry.size != spec_const_size) {
1860 skip |=
1861 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06001862 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
1863 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
1864 map_entry.constantID, i, map_entry.size,
1865 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07001866 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06001867 }
1868
Jeremy Gebben12933ef2021-05-12 17:16:27 -06001869 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
1870 auto entry = id_value_map.emplace(map_entry.constantID, std::vector<uint32_t>(map_entry.size > 4 ? 2 : 1));
1871 memcpy(entry.first->second.data(), specialization_data + map_entry.offset, map_entry.size);
1872 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001873 }
1874
1875 // Apply the specialization-constant values and revalidate the shader module.
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06001876 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001877 spvtools::Optimizer optimizer(spirv_environment);
1878 spvtools::MessageConsumer consumer = [&skip, &module, &pStage, this](spv_message_level_t level, const char *source,
1879 const spv_position_t &position, const char *message) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001880 skip |= LogError(
1881 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s. %s",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001882 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(pStage->stage), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001883 };
1884 optimizer.SetMessageConsumer(consumer);
1885 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
1886 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
1887 std::vector<uint32_t> specialized_spirv;
1888 auto const optimized =
1889 optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, spvtools::ValidatorOptions(), true);
1890 assert(optimized == true);
1891
1892 if (optimized) {
1893 spv_context ctx = spvContextCreate(spirv_environment);
1894 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
1895 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06001896 spvtools::ValidatorOptions options;
1897 AdjustValidatorOptions(device_extensions, enabled_features, options);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001898 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
1899 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07001900 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001901 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001902 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001903 string_VkShaderStageFlagBits(pStage->stage));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001904 }
1905
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06001906 spvDiagnosticDestroy(diag);
1907 spvContextDestroy(ctx);
1908 }
1909 }
1910
John Zulauf14c355b2019-06-27 16:09:37 -06001911 // Check the entrypoint
1912 if (entrypoint == module->end()) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001913 skip |=
Petr Krausb0d5e592021-05-21 23:37:11 +02001914 LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001915 pStage->pName, string_VkShaderStageFlagBits(pStage->stage));
John Zulauf14c355b2019-06-27 16:09:37 -06001916 }
1917 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
1918
1919 // Mark accessible ids
1920 auto &accessible_ids = stage_state.accessible_ids;
1921
Chris Forbes47567b72017-06-09 12:09:45 -07001922 // Validate descriptor set layout against what the entrypoint actually uses
John Zulauf14c355b2019-06-27 16:09:37 -06001923 bool has_writable_descriptor = stage_state.has_writable_descriptor;
1924 auto &descriptor_uses = stage_state.descriptor_uses;
Chris Forbes47567b72017-06-09 12:09:45 -07001925
sfricke-samsung94167ca2021-02-26 04:14:59 -08001926 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
1927 // and mainly only checking the instruction in detail for a single operation
1928 for (auto insn : *module) {
1929 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
1930 skip |= ValidatePropertiesAndFeatures(module, insn);
1931 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
1932 }
1933
locke-lunarg63e4daf2020-08-17 17:53:25 -06001934 skip |=
1935 ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, has_writable_descriptor, stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001936 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001937 skip |= ValidateShaderStorageImageFormats(module);
sfricke-samsungdc96f302020-03-18 20:42:10 -07001938 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001939 skip |= ValidateExecutionModes(module, entrypoint);
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07001940 skip |= ValidateSpecializationOffsets(pStage);
Jeff Bolze54ae892018-09-08 12:16:29 -05001941 if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07001942 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001943 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07001944 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08001945 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
1946 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
1947 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00001948 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
1949 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
1950 }
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001951 if (device_extensions.vk_qcom_render_pass_shader_resolve != kNotEnabled) {
1952 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
1953 }
Chris Forbes47567b72017-06-09 12:09:45 -07001954
sfricke-samsung7699b912021-04-12 23:01:51 -07001955 // "layout must be consistent with the layout of the * shader"
1956 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06001957 std::string vuid_layout_mismatch;
1958 if (pipeline->graphicsPipelineCI.sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) {
1959 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
1960 } else if (pipeline->computePipelineCI.sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO) {
1961 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
1962 } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR) {
1963 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
1964 } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV) {
1965 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
1966 }
1967
sfricke-samsung7699b912021-04-12 23:01:51 -07001968 // Validate Push Constants use
1969 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
1970
Chris Forbes47567b72017-06-09 12:09:45 -07001971 // Validate descriptor use
1972 for (auto use : descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07001973 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05001974 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07001975 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07001976 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
1977 std::set<uint32_t> descriptor_types =
1978 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07001979
1980 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06001981 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001982 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
1983 use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07001984 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06001985 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001986 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first,
1987 use.first.second, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06001988 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
1989 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06001990 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001991 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first,
1992 use.first.second, string_descriptorTypes(descriptor_types).c_str(),
1993 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07001994 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06001995 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001996 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
1997 required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07001998 }
1999 }
2000
2001 // Validate use of input attachments against subpass structure
2002 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002003 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002004
Petr Krause91f7a12017-12-14 20:57:36 +01002005 auto rpci = pipeline->rp_state->createInfo.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002006 auto subpass = pipeline->graphicsPipelineCI.subpass;
2007
2008 for (auto use : input_attachment_uses) {
2009 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2010 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07002011 ? input_attachments[use.first].attachment
2012 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002013
2014 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002015 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2016 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsung962cad92021-04-13 00:46:29 -07002017 } else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002018 skip |=
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002019 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2020 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
sfricke-samsung962cad92021-04-13 00:46:29 -07002021 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002022 }
2023 }
2024 }
Lockeaa8fdc02019-04-02 11:59:20 -06002025 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08002026 skip |= ValidateComputeWorkGroupSizes(module, entrypoint);
Lockeaa8fdc02019-04-02 11:59:20 -06002027 }
Chris Forbes47567b72017-06-09 12:09:45 -07002028 return skip;
2029}
2030
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002031bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2032 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2033 spirv_inst_iter consumer_entrypoint,
2034 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002035 bool skip = false;
2036
2037 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002038 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2039 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002040
2041 auto a_it = outputs.begin();
2042 auto b_it = inputs.begin();
2043
2044 // Maps sorted by key (location); walk them together to find mismatches
2045 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2046 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2047 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2048 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2049 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2050
2051 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002052 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002053 "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name,
2054 a_first.first, a_first.second, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002055 a_it++;
2056 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002057 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002058 "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first,
2059 b_first.second, producer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002060 b_it++;
2061 } else {
2062 // subtleties of arrayed interfaces:
2063 // - if is_patch, then the member is not arrayed, even though the interface may be.
2064 // - if is_block_member, then the extra array level of an arrayed interface is not
2065 // expressed in the member type -- it's expressed in the block type.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002066 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id,
2067 producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member,
2068 consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002069 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002070 "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002071 producer->DescribeType(a_it->second.type_id).c_str(),
2072 consumer->DescribeType(b_it->second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002073 }
2074 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002075 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002076 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2077 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2078 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002079 }
2080 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002081 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002082 "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first,
2083 a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002084 }
2085 a_it++;
2086 b_it++;
2087 }
2088 }
2089
Ari Suonpaa696b3432019-03-11 14:02:57 +02002090 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002091 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2092 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002093
2094 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2095 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002096 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002097 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002098 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2099 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002100 } else {
2101 auto it_producer = builtins_producer.begin();
2102 auto it_consumer = builtins_consumer.begin();
2103 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2104 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002105 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002106 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2107 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002108 break;
2109 }
2110 it_producer++;
2111 it_consumer++;
2112 }
2113 }
2114 }
2115 }
2116
Chris Forbes47567b72017-06-09 12:09:45 -07002117 return skip;
2118}
2119
John Zulauf14c355b2019-06-27 16:09:37 -06002120static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002121 uint32_t stage_mask = 0;
2122 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2123 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2124 stage_mask |= pCreateInfo->pStages[i].stage;
2125 }
2126 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002127 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2128 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2129 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002130 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2131 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2132 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2133 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2134 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002135 }
2136 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002137 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002138}
2139
Chris Forbes47567b72017-06-09 12:09:45 -07002140// Validate that the shaders used by the given pipeline and store the active_slots
2141// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002142bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002143 auto create_info = pipeline->graphicsPipelineCI.ptr();
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002144 int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
2145 int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07002146
John Zulauf14c355b2019-06-27 16:09:37 -06002147 const SHADER_MODULE_STATE *shaders[32];
Chris Forbes47567b72017-06-09 12:09:45 -07002148 memset(shaders, 0, sizeof(shaders));
Jeff Bolz7e35c392018-09-04 15:30:41 -05002149 spirv_inst_iter entrypoints[32];
Chris Forbes47567b72017-06-09 12:09:45 -07002150 bool skip = false;
2151
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002152 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002153
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002154 for (uint32_t i = 0; i < create_info->stageCount; i++) {
2155 auto stage = &create_info->pStages[i];
2156 auto stage_id = GetShaderStageId(stage->stage);
2157 shaders[stage_id] = GetShaderModuleState(stage->module);
sfricke-samsung962cad92021-04-13 00:46:29 -07002158 entrypoints[stage_id] = shaders[stage_id]->FindEntrypoint(stage->pName, stage->stage);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002159 skip |= ValidatePipelineShaderStage(stage, pipeline, pipeline->stage_state[i], shaders[stage_id], entrypoints[stage_id],
2160 (pointlist_stage_mask == stage->stage));
Chris Forbes47567b72017-06-09 12:09:45 -07002161 }
2162
2163 // if the shader stages are no good individually, cross-stage validation is pointless.
2164 if (skip) return true;
2165
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002166 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002167
2168 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002169 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002170 }
2171
Piers Daniell924cd832021-05-18 13:48:47 -06002172 if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv &&
2173 !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002174 skip |= ValidateViAgainstVsInputs(vi, shaders[vertex_stage], entrypoints[vertex_stage]);
Chris Forbes47567b72017-06-09 12:09:45 -07002175 }
2176
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002177 int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
2178 int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07002179
2180 while (!shaders[producer] && producer != fragment_stage) {
2181 producer++;
2182 consumer++;
2183 }
2184
2185 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
2186 assert(shaders[producer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002187 if (shaders[consumer]) {
2188 if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002189 skip |= ValidateInterfaceBetweenStages(shaders[producer], entrypoints[producer], &shader_stage_attribs[producer],
2190 shaders[consumer], entrypoints[consumer], &shader_stage_attribs[consumer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002191 }
Chris Forbes47567b72017-06-09 12:09:45 -07002192
2193 producer = consumer;
2194 }
2195 }
2196
2197 if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002198 skip |= ValidateFsOutputsAgainstRenderPass(shaders[fragment_stage], entrypoints[fragment_stage], pipeline,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002199 create_info->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07002200 }
2201
2202 return skip;
2203}
2204
Tony-LunarGb2ded512021-02-02 16:03:30 -07002205void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) {
2206 auto create_info = pipeline_state->graphicsPipelineCI.ptr();
2207
2208 if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports ||
2209 !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) {
2210 return;
2211 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002212
Nathaniel Cesario1c3d3652021-01-25 18:35:12 -07002213 std::array<const SHADER_MODULE_STATE *, 32> shaders;
2214 std::fill(shaders.begin(), shaders.end(), nullptr);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002215 spirv_inst_iter entrypoints[32];
Tobias Hector6663c9b2020-11-05 10:18:02 +00002216
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002217 for (uint32_t i = 0; i < create_info->stageCount; i++) {
2218 auto stage = &create_info->pStages[i];
2219 auto stage_id = GetShaderStageId(stage->stage);
2220 shaders[stage_id] = GetShaderModuleState(stage->module);
sfricke-samsung962cad92021-04-13 00:46:29 -07002221 entrypoints[stage_id] = shaders[stage_id]->FindEntrypoint(stage->pName, stage->stage);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002222
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002223 if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT ||
2224 stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002225 bool primitiverate_written = false;
Tobias Hector6663c9b2020-11-05 10:18:02 +00002226
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002227 for (auto set : shaders[stage_id]->builtin_decoration_list) {
2228 auto insn = shaders[stage_id]->at(set.offset);
2229 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002230 primitiverate_written = shaders[stage_id]->IsBuiltInWritten(insn, entrypoints[stage_id]);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002231 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002232 if (primitiverate_written) {
2233 break;
2234 }
Tony-LunarGb2ded512021-02-02 16:03:30 -07002235 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002236
Tony-LunarGb2ded512021-02-02 16:03:30 -07002237 if (primitiverate_written) {
2238 pipeline_state->wrote_primitive_shading_rate.insert(stage->stage);
2239 }
2240 }
2241 }
2242}
2243
2244bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2245 const char *caller, const DrawDispatchVuid &vuid) const {
2246 auto create_info = pipeline->graphicsPipelineCI.ptr();
2247 bool skip = false;
2248
2249 for (uint32_t i = 0; i < create_info->stageCount; i++) {
2250 auto stage = &create_info->pStages[i];
2251 if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT ||
2252 stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) {
2253 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2254 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
2255 if (pipeline->wrote_primitive_shading_rate.find(stage->stage) != pipeline->wrote_primitive_shading_rate.end()) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002256 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002257 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002258 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2259 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2260 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002261 caller, string_VkShaderStageFlagBits(stage->stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002262 }
2263 }
2264 }
2265 }
2266
2267 return skip;
2268}
2269
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002270bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002271 const auto &stage = *pipeline->computePipelineCI.stage.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002272
John Zulauf14c355b2019-06-27 16:09:37 -06002273 const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module);
sfricke-samsung962cad92021-04-13 00:46:29 -07002274 const spirv_inst_iter entrypoint = module->FindEntrypoint(stage.pName, stage.stage);
Chris Forbes47567b72017-06-09 12:09:45 -07002275
John Zulauf14c355b2019-06-27 16:09:37 -06002276 return ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[0], module, entrypoint, false);
Chris Forbes47567b72017-06-09 12:09:45 -07002277}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002278
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002279uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2280 uint32_t total = 0;
2281
2282 const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages;
2283 for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) {
2284 if (stages[stage_index].stage == stageBit) {
2285 total++;
2286 }
2287 }
2288
2289 if (pipeline->raytracingPipelineCI.pLibraryInfo) {
2290 for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) {
2291 const PIPELINE_STATE *library_pipeline = GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]);
2292 total += CalcShaderStageCount(library_pipeline, stageBit);
2293 }
2294 }
2295
2296 return total;
2297}
2298
sourav parmarcd5fb182020-07-17 12:58:44 -07002299bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06002300 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04002301
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002302 if (isKHR) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002303 if (pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth >
2304 phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
2305 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
2306 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
2307 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
2308 pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth,
2309 phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002310 }
sourav parmarcd5fb182020-07-17 12:58:44 -07002311 if (pipeline->raytracingPipelineCI.pLibraryInfo) {
2312 for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002313 const PIPELINE_STATE *library_pipelinestate =
sourav parmarcd5fb182020-07-17 12:58:44 -07002314 GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002315 if (library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth !=
sourav parmarcd5fb182020-07-17 12:58:44 -07002316 pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth) {
2317 skip |= LogError(
2318 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
2319 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
2320 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002321 i, library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth,
sourav parmarcd5fb182020-07-17 12:58:44 -07002322 pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth);
2323 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002324 if (library_pipelinestate->raytracingPipelineCI.pLibraryInfo &&
2325 (library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize !=
sourav parmarcd5fb182020-07-17 12:58:44 -07002326 pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize ||
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002327 library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize !=
sourav parmarcd5fb182020-07-17 12:58:44 -07002328 pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize)) {
2329 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
2330 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
2331 "member must have been created with values of the maxPipelineRayPayloadSize and "
2332 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
2333 }
2334 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002335 !(library_pipelinestate->raytracingPipelineCI.flags &
sourav parmarcd5fb182020-07-17 12:58:44 -07002336 VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
2337 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
2338 "vkCreateRayTracingPipelinesKHR: If flags includes "
2339 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
2340 "the pLibraries member of libraries must have been created with the "
2341 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
2342 }
sourav parmar83c31b12020-05-06 12:30:54 -07002343 }
2344 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002345 } else {
2346 if (pipeline->raytracingPipelineCI.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002347 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
2348 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
2349 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002350 pipeline->raytracingPipelineCI.maxRecursionDepth,
2351 phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
2352 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002353 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002354 const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages;
2355 const auto *groups = pipeline->raytracingPipelineCI.ptr()->pGroups;
2356
John Zulaufe4474e72019-07-01 17:28:27 -06002357 for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04002358 const auto &stage = stages[stage_index];
Jeff Bolzfbe51582018-09-13 10:01:35 -05002359
John Zulaufe4474e72019-07-01 17:28:27 -06002360 const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module);
sfricke-samsung962cad92021-04-13 00:46:29 -07002361 const spirv_inst_iter entrypoint = module->FindEntrypoint(stage.pName, stage.stage);
Jeff Bolzfbe51582018-09-13 10:01:35 -05002362
John Zulaufe4474e72019-07-01 17:28:27 -06002363 skip |= ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[stage_index], module, entrypoint, false);
Jason Macnak15f95e82019-08-21 21:52:02 -04002364 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002365
2366 if ((pipeline->raytracingPipelineCI.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
2367 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
2368 if (raygen_stages_count == 0) {
2369 skip |= LogError(
2370 device,
2371 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-03425",
2372 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
2373 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002374 }
2375
2376 for (uint32_t group_index = 0; group_index < pipeline->raytracingPipelineCI.groupCount; group_index++) {
2377 const auto &group = groups[group_index];
2378
2379 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
2380 if (group.generalShader >= pipeline->raytracingPipelineCI.stageCount ||
2381 (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV &&
2382 stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV &&
2383 stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002384 skip |= LogError(device,
2385 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
2386 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
2387 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002388 }
2389 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
2390 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002391 skip |= LogError(device,
2392 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
2393 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
2394 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002395 }
2396 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
2397 if (group.intersectionShader >= pipeline->raytracingPipelineCI.stageCount ||
2398 stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002399 skip |= LogError(device,
2400 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
2401 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
2402 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002403 }
2404 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2405 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002406 skip |= LogError(device,
2407 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
2408 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
2409 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002410 }
2411 }
2412
2413 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
2414 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2415 if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= pipeline->raytracingPipelineCI.stageCount ||
2416 stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002417 skip |= LogError(device,
2418 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
2419 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
2420 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002421 }
2422 if (group.closestHitShader != VK_SHADER_UNUSED_NV &&
2423 (group.closestHitShader >= pipeline->raytracingPipelineCI.stageCount ||
2424 stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002425 skip |= LogError(device,
2426 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
2427 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
2428 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002429 }
2430 }
John Zulaufe4474e72019-07-01 17:28:27 -06002431 }
2432 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002433}
2434
Dave Houltona9df0ce2018-02-07 10:51:23 -07002435uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07002436
Dave Houltona9df0ce2018-02-07 10:51:23 -07002437static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002438 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06002439 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06002440 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002441 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002442 return nullptr;
2443}
2444
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002445bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002446 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002447 bool skip = false;
2448 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002449
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06002450 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002451 return false;
2452 }
2453
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002454 auto have_glsl_shader = device_extensions.vk_nv_glsl_shader;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002455
2456 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002457 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
2458 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
2459 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002460 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07002461 auto cache = GetValidationCacheInfo(pCreateInfo);
2462 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07002463 // If app isn't using a shader validation cache, use the default one from CoreChecks
2464 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002465 if (cache) {
2466 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002467 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07002468 }
2469
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002470 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
2471 // the default values will be used during validation.
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002472 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
Dave Houlton0ea2d012018-06-21 14:00:26 -06002473 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002474 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07002475 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002476 spvtools::ValidatorOptions options;
2477 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06002478 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002479 if (spv_valid != SPV_SUCCESS) {
2480 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002481 if (spv_valid == SPV_WARNING) {
2482 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2483 diag && diag->error ? diag->error : "(no error text)");
2484 } else {
2485 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2486 diag && diag->error ? diag->error : "(no error text)");
2487 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002488 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002489 } else {
2490 if (cache) {
2491 cache->Insert(hash);
2492 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002493 }
2494
2495 spvDiagnosticDestroy(diag);
2496 spvContextDestroy(ctx);
2497 }
2498
Chris Forbes4ae55b32017-06-09 14:42:56 -07002499 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07002500}
2501
sfricke-samsung8a7341a2021-02-28 07:30:21 -08002502bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint) const {
Lockeaa8fdc02019-04-02 11:59:20 -06002503 bool skip = false;
2504 uint32_t local_size_x = 0;
2505 uint32_t local_size_y = 0;
2506 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07002507 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06002508 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002509 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002510 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002511 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002512 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06002513 }
2514 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002515 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002516 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002517 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002518 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06002519 }
2520 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002521 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002522 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002523 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002524 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06002525 }
2526
2527 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
2528 uint64_t invocations = local_size_x * local_size_y;
2529 // Prevent overflow.
2530 bool fail = false;
2531 if (invocations > UINT32_MAX || invocations > limit) {
2532 fail = true;
2533 }
2534 if (!fail) {
2535 invocations *= local_size_z;
2536 if (invocations > UINT32_MAX || invocations > limit) {
2537 fail = true;
2538 }
2539 }
2540 if (fail) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002541 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002542 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
2543 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002544 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, local_size_y, local_size_z,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002545 limit);
Lockeaa8fdc02019-04-02 11:59:20 -06002546 }
2547 }
2548 return skip;
2549}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002550
2551spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
2552 if (api_version >= VK_API_VERSION_1_2) {
2553 return SPV_ENV_VULKAN_1_2;
2554 } else if (api_version >= VK_API_VERSION_1_1) {
2555 if (spirv_1_4) {
2556 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
2557 } else {
2558 return SPV_ENV_VULKAN_1_1;
2559 }
2560 }
2561 return SPV_ENV_VULKAN_1_0;
2562}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002563
Jeremy Gebben5d970742021-05-31 16:04:14 -06002564void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002565 spvtools::ValidatorOptions &options) {
2566 if (device_extensions.vk_khr_relaxed_block_layout) {
2567 options.SetRelaxBlockLayout(true);
2568 }
2569 if (device_extensions.vk_khr_uniform_buffer_standard_layout && enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
2570 options.SetUniformBufferStandardLayout(true);
2571 }
2572 if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) {
2573 options.SetScalarBlockLayout(true);
2574 }
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08002575 if (device_extensions.vk_khr_workgroup_memory_explicit_layout &&
2576 enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
2577 options.SetWorkgroupScalarBlockLayout(true);
2578 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002579}