blob: 99a4209c1075ec8ff231b7e738f78a2239e5b562 [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.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200454bool CoreChecks::ValidateSpecializations(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 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200477 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
478 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
479 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
480 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
481 j, spec->pMapEntries[i].constantID);
482 }
483 }
Chris Forbes47567b72017-06-09 12:09:45 -0700484 }
485 }
486
487 return skip;
488}
489
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500490// TODO (jbolz): Can this return a const reference?
sourav parmarcd5fb182020-07-17 12:58:44 -0700491static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count,
492 bool is_khr) {
Chris Forbes47567b72017-06-09 12:09:45 -0700493 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800494 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700495 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500496 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700497
498 // 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 -0500499 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
500 if (type.opcode() == spv::OpTypeRuntimeArray) {
501 descriptor_count = 0;
502 type = module->get_def(type.word(2));
503 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700504 descriptor_count *= module->GetConstantValueById(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700505 type = module->get_def(type.word(2));
506 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800507 if (type.word(2) == spv::StorageClassStorageBuffer) {
508 is_storage_buffer = true;
509 }
Chris Forbes47567b72017-06-09 12:09:45 -0700510 type = module->get_def(type.word(3));
511 }
512 }
513
514 switch (type.opcode()) {
515 case spv::OpTypeStruct: {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800516 for (auto insn : module->decoration_inst) {
517 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700518 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800519 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500520 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
521 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
522 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800523 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500524 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
525 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
526 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
527 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800528 }
Chris Forbes47567b72017-06-09 12:09:45 -0700529 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500530 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
531 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
532 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700533 }
534 }
535 }
536
537 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500538 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700539 }
540
541 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500542 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
543 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
544 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700545
Chris Forbes73c00bf2018-06-22 16:28:06 -0700546 case spv::OpTypeSampledImage: {
547 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
548 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
549 auto image_type = module->get_def(type.word(2));
550 auto dim = image_type.word(3);
551 auto sampled = image_type.word(7);
552 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500553 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
554 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700555 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700556 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500557 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
558 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700559
560 case spv::OpTypeImage: {
561 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
562 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
563 auto dim = type.word(3);
564 auto sampled = type.word(7);
565
566 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500567 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
568 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700569 } else if (dim == spv::DimBuffer) {
570 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500571 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
572 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700573 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500574 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
575 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700576 }
577 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500578 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
579 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
580 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700581 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500582 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
583 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700584 }
585 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600586 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700587 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
588 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500589 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700590
591 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
592 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500593 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700594 }
595}
596
Jeff Bolze54ae892018-09-08 12:16:29 -0500597static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700598 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500599 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
600 if (ss.tellp()) ss << ", ";
601 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700602 }
603 return ss.str();
604}
605
sfricke-samsung0065ce02020-12-03 22:46:37 -0800606bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500607 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800608 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 -0500609 return true;
610 }
611 }
612
613 return false;
614}
615
sfricke-samsung0065ce02020-12-03 22:46:37 -0800616bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700617 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800618 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700619 return true;
620 }
621 }
622
623 return false;
624}
625
locke-lunarg63e4daf2020-08-17 17:53:25 -0600626bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
627 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500628 bool skip = false;
629
locke-lunarg63e4daf2020-08-17 17:53:25 -0600630 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800631 switch (stage) {
632 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -0600633 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
634 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
635 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
636 case VK_SHADER_STAGE_MISS_BIT_NV:
637 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
638 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
639 case VK_SHADER_STAGE_TASK_BIT_NV:
640 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -0800641 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -0600642 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -0800643 break;
644 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800645 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
646 kVUID_Core_Shader_FeatureNotEnabled);
Chris Forbes349b3132018-03-07 11:38:08 -0800647 break;
648 default:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800649 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
650 kVUID_Core_Shader_FeatureNotEnabled);
Chris Forbes349b3132018-03-07 11:38:08 -0800651 break;
652 }
653 }
654
Chris Forbes47567b72017-06-09 12:09:45 -0700655 return skip;
656}
657
sfricke-samsung94167ca2021-02-26 04:14:59 -0800658bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
659 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500660 bool skip = false;
661
sfricke-samsung94167ca2021-02-26 04:14:59 -0800662 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
663 if (GroupOperation(insn.opcode()) == true) {
664 // Check the quad operations.
665 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
666 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
667 skip |= RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
668 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages",
669 kVUID_Core_Shader_FeatureNotEnabled);
sfricke-samsung0065ce02020-12-03 22:46:37 -0800670 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800671 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500672
sfricke-samsung94167ca2021-02-26 04:14:59 -0800673 uint32_t scope_type = spv::ScopeMax;
674 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
675 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
676 scope_type = spv::ScopeSubgroup;
677 } else {
678 // "All <id> used for Scope <id> must be of an OpConstant"
679 auto scope_id = module->get_def(insn.word(3));
680 scope_type = scope_id.word(3);
681 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800682
sfricke-samsung94167ca2021-02-26 04:14:59 -0800683 if (scope_type == spv::ScopeSubgroup) {
684 // "Group operations with subgroup scope" must have stage support
685 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
686 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung0065ce02020-12-03 22:46:37 -0800687 "VkPhysicalDeviceSubgroupProperties::supportedStages", kVUID_Core_Shader_ExceedDeviceLimit);
sfricke-samsung94167ca2021-02-26 04:14:59 -0800688 }
689
690 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
691 auto type = module->get_def(insn.word(1));
692
693 if (type.opcode() == spv::OpTypeVector) {
694 // Get the element type
695 type = module->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800696 }
697
sfricke-samsung94167ca2021-02-26 04:14:59 -0800698 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800699 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
700 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500701
sfricke-samsung0065ce02020-12-03 22:46:37 -0800702 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
703 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
704 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
705 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
706 kVUID_Core_Shader_FeatureNotEnabled);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500707 }
708 }
709 }
Jeff Bolzee743412019-06-20 22:24:32 -0500710 }
711
712 return skip;
713}
714
ziga-lunarg2818f492021-08-12 14:30:51 +0200715bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
716 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
717 bool skip = false;
718
719 std::array<uint32_t, 3> work_group_size = src->GetWorkgroupSize(pStage, id_value_map);
720
721 for (uint32_t i = 0; i < 3; ++i) {
722 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
723 const char member = 'x' + static_cast<int8_t>(i);
724 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
725 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
726 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
727 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
728 }
729 }
730 return skip;
731}
732
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600733bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600734 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200735 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
736 pStage->stage == VK_SHADER_STAGE_ALL) {
737 return false;
738 }
739
740 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700741 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200742
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700743 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200744 struct Variable {
745 uint32_t baseTypePtrID;
746 uint32_t ID;
747 uint32_t storageClass;
748 };
749 std::vector<Variable> variables;
750
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700751 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700752 bool is_iso_lines = false;
753 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500754
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700755 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600756
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200757 for (auto insn : *src) {
758 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500759 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200760 case spv::OpDecorate:
761 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500762 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700763 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200764 break;
765 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200766 default:
767 break;
768 }
769 break;
770 // Find all input and output variables
771 case spv::OpVariable: {
772 Variable var = {};
773 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600774 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
775 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700776 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200777 var.baseTypePtrID = insn.word(1);
778 var.ID = insn.word(2);
779 variables.push_back(var);
780 }
781 break;
782 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500783 case spv::OpExecutionMode:
784 if (insn.word(1) == entrypoint.word(2)) {
785 switch (insn.word(2)) {
786 default:
787 break;
788 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700789 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500790 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700791 case spv::ExecutionModeIsolines:
792 is_iso_lines = true;
793 break;
794 case spv::ExecutionModePointMode:
795 is_point_mode = true;
796 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500797 }
798 }
799 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200800 default:
801 break;
802 }
803 }
804
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500805 bool strip_output_array_level =
806 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
807 bool strip_input_array_level =
808 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
809 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
810
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700811 uint32_t num_comp_in = 0, num_comp_out = 0;
812 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600813
sfricke-samsung962cad92021-04-13 00:46:29 -0700814 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
815 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600816
817 // Find max component location used for input variables.
818 for (auto &var : inputs) {
819 int location = var.first.first;
820 int component = var.first.second;
821 interface_var &iv = var.second;
822
823 // Only need to look at the first location, since we use the type's whole size
824 if (iv.offset != 0) {
825 continue;
826 }
827
828 if (iv.is_patch) {
829 continue;
830 }
831
sfricke-samsung962cad92021-04-13 00:46:29 -0700832 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700833 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600834 }
835
836 // Find max component location used for output variables.
837 for (auto &var : outputs) {
838 int location = var.first.first;
839 int component = var.first.second;
840 interface_var &iv = var.second;
841
842 // Only need to look at the first location, since we use the type's whole size
843 if (iv.offset != 0) {
844 continue;
845 }
846
847 if (iv.is_patch) {
848 continue;
849 }
850
sfricke-samsung962cad92021-04-13 00:46:29 -0700851 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700852 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600853 }
854
855 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
856 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700857 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
858 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200859 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500860 // Check if the variable is a patch. Patches can also be members of blocks,
861 // but if they are then the top-level arrayness has already been stripped
862 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700863 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200864
865 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700866 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200867 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700868 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200869 }
870 }
871
872 switch (pStage->stage) {
873 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700874 if (num_comp_out > limits.maxVertexOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600875 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700876 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
877 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
878 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700879 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200880 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700881 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600882 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700883 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
884 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
885 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600886 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200887 break;
888
889 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700890 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600891 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700892 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
893 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
894 "components by %u components",
895 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700896 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200897 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700898 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600899 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600900 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700901 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
902 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
903 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600904 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700905 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600906 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700907 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
908 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
909 "components by %u components",
910 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700911 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200912 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700913 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600914 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600915 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700916 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
917 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
918 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600919 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200920 break;
921
922 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700923 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600924 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700925 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
926 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
927 "components by %u components",
928 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700929 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200930 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700931 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600932 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600933 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700934 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
935 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
936 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600937 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700938 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600939 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700940 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
941 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
942 "components by %u components",
943 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700944 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200945 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700946 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600947 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600948 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700949 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
950 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
951 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600952 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700953 // Portability validation
954 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
955 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600956 skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_Isolines,
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700957 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
958 " is using abstract patch type IsoLines, but this is not supported on this platform");
959 }
960 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600961 skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_PointMode,
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700962 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
963 " is using abstract patch type PointMode, but this is not supported on this platform");
964 }
965 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200966 break;
967
968 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700969 if (num_comp_in > limits.maxGeometryInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600970 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700971 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
972 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
973 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700974 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200975 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700976 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600977 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700978 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
979 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
980 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600981 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700982 if (num_comp_out > limits.maxGeometryOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600983 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700984 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
985 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
986 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700987 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200988 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700989 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600990 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700991 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
992 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
993 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600994 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700995 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600996 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700997 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
998 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
999 "components by %u components",
1000 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001001 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001002 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001003 break;
1004
1005 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001006 if (num_comp_in > limits.maxFragmentInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001007 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001008 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
1009 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
1010 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001011 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001012 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001013 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001014 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001015 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1016 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1017 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001018 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001019 break;
1020
Jeff Bolz148d94e2018-12-13 21:25:56 -06001021 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1022 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1023 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1024 case VK_SHADER_STAGE_MISS_BIT_NV:
1025 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1026 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1027 case VK_SHADER_STAGE_TASK_BIT_NV:
1028 case VK_SHADER_STAGE_MESH_BIT_NV:
1029 break;
1030
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001031 default:
1032 assert(false); // This should never happen
1033 }
1034 return skip;
1035}
1036
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001037bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const {
1038 bool skip = false;
1039
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001040 // Got through all ImageRead/Write instructions
1041 for (auto insn : *src) {
1042 switch (insn.opcode()) {
1043 case spv::OpImageSparseRead:
1044 case spv::OpImageRead: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001045 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001046 if (type_def != src->end()) {
Tim Van Pattenffe91322021-07-26 10:20:50 -06001047 const auto dim = type_def.word(3);
1048 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1049 // StorageImageReadWithoutFormat Capability was declared.
1050 if (dim != spv::DimSubpassData && type_def.word(8) == spv::ImageFormatUnknown) {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001051 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1052 "shaderStorageImageReadWithoutFormat",
1053 kVUID_Features_shaderStorageImageReadWithoutFormat);
1054 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001055 }
1056 break;
1057 }
1058 case spv::OpImageWrite: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001059 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001060 if (type_def != src->end()) {
1061 if (type_def.word(8) == spv::ImageFormatUnknown) {
1062 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1063 "shaderStorageImageWriteWithoutFormat",
1064 kVUID_Features_shaderStorageImageWriteWithoutFormat);
1065 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001066 }
1067 break;
1068 }
1069
1070 }
1071 }
1072
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001073 // Go through all variables for images and check decorations
1074 for (auto insn : *src) {
1075 if (insn.opcode() != spv::OpVariable)
1076 continue;
1077
1078 uint32_t var = insn.word(2);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001079 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001080 if (type_def == src->end())
1081 continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001082 // Only check if the Image Dim operand is not SubpassData
1083 const auto dim = type_def.word(3);
1084 if (dim == spv::DimSubpassData) continue;
Corentin Wallez91f8b6d2021-07-23 10:11:31 +02001085 // Only check storage images
1086 if (type_def.word(7) != 2) continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001087 if (type_def.word(8) != spv::ImageFormatUnknown) continue;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001088
1089 decoration_set img_decorations = src->get_decorations(var);
1090
1091 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1092 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1093 skip |= LogError(device,
1094 kVUID_Features_shaderStorageImageReadWithoutFormat_NonReadable,
1095 "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32 " "
1096 " without format not marked a NonReadable", var);
1097 }
1098
1099 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1100 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1101 skip |= LogError(device,
1102 kVUID_Features_shaderStorageImageWriteWithoutFormat_NonWritable,
1103 "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32 " "
1104 "without format not marked a NonWritable", var);
1105 }
1106 }
1107
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001108 return skip;
1109}
1110
sfricke-samsungdc96f302020-03-18 20:42:10 -07001111bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1112 bool skip = false;
1113 uint32_t total_resources = 0;
1114
1115 // Only currently testing for graphics and compute pipelines
1116 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1117 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1118 return false;
1119 }
1120
1121 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1122 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
1123 total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].colorAttachmentCount;
1124 }
1125
1126 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1127 // input from CreatePipeline and CreatePipelineLayout level
1128 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1129 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1130 continue;
1131 }
1132
1133 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1134 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1135 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1136 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1137 // Check only descriptor types listed in maxPerStageResources description in spec
1138 switch (binding->descriptorType) {
1139 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1140 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1141 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1142 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1143 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1144 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1145 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1146 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1147 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1148 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1149 total_resources += binding->descriptorCount;
1150 break;
1151 default:
1152 break;
1153 }
1154 }
1155 }
1156 }
1157
1158 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1159 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1160 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001161 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001162 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1163 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1164 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1165 }
1166
1167 return skip;
1168}
1169
Jeff Bolze4356752019-03-07 11:23:46 -06001170// copy the specialization constant value into buf, if it is present
1171void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1172 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1173
1174 if (spec && spec_id < spec->mapEntryCount) {
1175 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1176 }
1177}
1178
1179// Fill in value with the constant or specialization constant value, if available.
1180// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001181static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001182 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001183 auto type_id = src->get_def(insn.word(1));
1184 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1185 return false;
1186 }
1187 switch (insn.opcode()) {
1188 case spv::OpSpecConstant:
1189 *value = insn.word(3);
1190 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1191 return true;
1192 case spv::OpConstant:
1193 *value = insn.word(3);
1194 return true;
1195 default:
1196 return false;
1197 }
1198}
1199
1200// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001201VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001202 switch (insn.opcode()) {
1203 case spv::OpTypeInt:
1204 switch (insn.word(2)) {
1205 case 8:
1206 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1207 case 16:
1208 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1209 case 32:
1210 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1211 case 64:
1212 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1213 default:
1214 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1215 }
1216 case spv::OpTypeFloat:
1217 switch (insn.word(2)) {
1218 case 16:
1219 return VK_COMPONENT_TYPE_FLOAT16_NV;
1220 case 32:
1221 return VK_COMPONENT_TYPE_FLOAT32_NV;
1222 case 64:
1223 return VK_COMPONENT_TYPE_FLOAT64_NV;
1224 default:
1225 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1226 }
1227 default:
1228 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1229 }
1230}
1231
1232// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1233// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001234bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001235 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001236 bool skip = false;
1237
1238 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001239 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001240 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001241 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001242
1243 struct CoopMatType {
1244 uint32_t scope, rows, cols;
1245 VkComponentTypeNV component_type;
1246 bool all_constant;
1247
1248 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1249
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001250 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001251 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001252 spirv_inst_iter insn = src->get_def(id);
1253 uint32_t component_type_id = insn.word(2);
1254 uint32_t scope_id = insn.word(3);
1255 uint32_t rows_id = insn.word(4);
1256 uint32_t cols_id = insn.word(5);
1257 auto component_type_iter = src->get_def(component_type_id);
1258 auto scope_iter = src->get_def(scope_id);
1259 auto rows_iter = src->get_def(rows_id);
1260 auto cols_iter = src->get_def(cols_id);
1261
1262 all_constant = true;
1263 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1264 all_constant = false;
1265 }
1266 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1267 all_constant = false;
1268 }
1269 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1270 all_constant = false;
1271 }
1272 component_type = GetComponentType(component_type_iter, src);
1273 }
1274 };
1275
1276 bool seen_coopmat_capability = false;
1277
1278 for (auto insn : *src) {
1279 // Whitelist instructions whose result can be a cooperative matrix type, and
1280 // keep track of their types. It would be nice if SPIRV-Headers generated code
1281 // to identify which instructions have a result type and result id. Lacking that,
1282 // this whitelist is based on the set of instructions that
1283 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1284 switch (insn.opcode()) {
1285 case spv::OpLoad:
1286 case spv::OpCooperativeMatrixLoadNV:
1287 case spv::OpCooperativeMatrixMulAddNV:
1288 case spv::OpSNegate:
1289 case spv::OpFNegate:
1290 case spv::OpIAdd:
1291 case spv::OpFAdd:
1292 case spv::OpISub:
1293 case spv::OpFSub:
1294 case spv::OpFDiv:
1295 case spv::OpSDiv:
1296 case spv::OpUDiv:
1297 case spv::OpMatrixTimesScalar:
1298 case spv::OpConstantComposite:
1299 case spv::OpCompositeConstruct:
1300 case spv::OpConvertFToU:
1301 case spv::OpConvertFToS:
1302 case spv::OpConvertSToF:
1303 case spv::OpConvertUToF:
1304 case spv::OpUConvert:
1305 case spv::OpSConvert:
1306 case spv::OpFConvert:
1307 id_to_type_id[insn.word(2)] = insn.word(1);
1308 break;
1309 default:
1310 break;
1311 }
1312
1313 switch (insn.opcode()) {
1314 case spv::OpDecorate:
1315 if (insn.word(2) == spv::DecorationSpecId) {
1316 id_to_spec_id[insn.word(1)] = insn.word(3);
1317 }
1318 break;
1319 case spv::OpCapability:
1320 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1321 seen_coopmat_capability = true;
1322
1323 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001324 skip |= LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001325 pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixSupportedStages,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001326 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1327 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001328 }
1329 }
1330 break;
1331 case spv::OpMemoryModel:
1332 // If the capability isn't enabled, don't bother with the rest of this function.
1333 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1334 if (!seen_coopmat_capability) {
1335 return skip;
1336 }
1337 break;
1338 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001339 CoopMatType m;
1340 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001341
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001342 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001343 // Validate that the type parameters are all supported for one of the
1344 // operands of a cooperative matrix property.
1345 bool valid = false;
1346 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001347 if (cooperative_matrix_properties[i].AType == m.component_type &&
1348 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1349 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001350 valid = true;
1351 break;
1352 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001353 if (cooperative_matrix_properties[i].BType == m.component_type &&
1354 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1355 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001356 valid = true;
1357 break;
1358 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001359 if (cooperative_matrix_properties[i].CType == m.component_type &&
1360 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1361 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001362 valid = true;
1363 break;
1364 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001365 if (cooperative_matrix_properties[i].DType == m.component_type &&
1366 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1367 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001368 valid = true;
1369 break;
1370 }
1371 }
1372 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001373 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001374 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1375 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001376 }
1377 }
1378 break;
1379 }
1380 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001381 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001382 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1383 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1384 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1385 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001386 // Couldn't find type of matrix
1387 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001388 break;
1389 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001390 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1391 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1392 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1393 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001394
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001395 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001396 // Validate that the type parameters are all supported for the same
1397 // cooperative matrix property.
1398 bool valid = false;
1399 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001400 if (cooperative_matrix_properties[i].AType == a.component_type &&
1401 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1402 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001403
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001404 cooperative_matrix_properties[i].BType == b.component_type &&
1405 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1406 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001407
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001408 cooperative_matrix_properties[i].CType == c.component_type &&
1409 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1410 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001411
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001412 cooperative_matrix_properties[i].DType == d.component_type &&
1413 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1414 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001415 valid = true;
1416 break;
1417 }
1418 }
1419 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001420 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001421 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1422 "VkCooperativeMatrixPropertiesNV",
1423 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001424 }
1425 }
1426 break;
1427 }
1428 default:
1429 break;
1430 }
1431 }
1432
1433 return skip;
1434}
1435
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001436bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1437 const PIPELINE_STATE *pipeline) const {
1438 bool skip = false;
1439
1440 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1441 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1442 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1443 for (auto insn : *src) {
1444 switch (insn.opcode()) {
1445 case spv::OpCapability:
1446 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1447 auto subpass_flags =
1448 (pipeline->rp_state == nullptr)
1449 ? 0
1450 : pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].flags;
1451 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1452 skip |=
1453 LogError(pipeline->pipeline(), kVUID_Core_Shader_ResolveQCOM_InvalidCapability,
1454 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1455 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1456 }
1457 }
1458 break;
1459 default:
1460 break;
1461 }
1462 }
1463 }
1464
1465 return skip;
1466}
1467
sfricke-samsung58b84352021-07-31 21:41:04 -07001468bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *src) const {
1469 bool skip = false;
1470
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001471 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001472 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001473
sfricke-samsungf5042b12021-08-05 01:09:40 -07001474 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1475 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1476
1477 const bool valid_storage_buffer_float = (
1478 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1479 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1480 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1481 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1482 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1483 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1484 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1485 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1486 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1487
1488 const bool valid_workgroup_float = (
1489 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1490 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1491 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1492 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1493 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1494 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1495 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1496 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1497 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1498
1499 const bool valid_image_float = (
1500 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1501 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1502 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1503
1504 const bool valid_16_float = (
1505 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1506 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1507 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1508 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1509 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1510 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1511
1512 const bool valid_32_float = (
1513 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1514 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1515 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1516 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1517 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1518 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1519 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1520 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1521 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1522
1523 const bool valid_64_float = (
1524 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1525 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1526 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1527 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1528 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1529 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1530 // clang-format on
1531
sfricke-samsung58b84352021-07-31 21:41:04 -07001532 for (auto &atomic_inst : src->atomic_inst) {
1533 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungf5042b12021-08-05 01:09:40 -07001534 const uint32_t opcode = src->at(atomic_inst.first).opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001535
1536 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
1537 // Validate 64-bit atomics
1538 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1539 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
1540 skip |= LogError(
1541 device, kVUID_Core_Shader_AtomicFeature,
1542 "%s: Can't use 64-bit int atomics operations with %s storage class without shaderBufferInt64Atomics enabled.",
1543 report_data->FormatHandle(src->vk_shader_module()).c_str(), StorageClassName(atomic.storage_class));
1544 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1545 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
1546 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1547 "%s: Can't use 64-bit int atomics operations with Workgroup storage class without "
1548 "shaderSharedInt64Atomics enabled.",
1549 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001550 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
1551 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1552 "%s: Can't use 64-bit int atomics operations with Image storage class without "
1553 "shaderImageInt64Atomics enabled.",
1554 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001555 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001556 } else if (atomic.type == spv::OpTypeFloat) {
1557 // Validate Floats
1558 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1559 if (valid_storage_buffer_float == false) {
1560 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1561 "%s: Can't use float atomics operations with StorageBuffer storage class without "
1562 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1563 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1564 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1565 "shaderBufferFloat64AtomicMinMax enabled.",
1566 report_data->FormatHandle(src->vk_shader_module()).c_str());
1567 } else if (opcode == spv::OpAtomicFAddEXT) {
1568 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1569 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1570 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with "
1571 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
1572 report_data->FormatHandle(src->vk_shader_module()).c_str());
1573 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1574 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1575 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with "
1576 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
1577 report_data->FormatHandle(src->vk_shader_module()).c_str());
1578 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1579 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1580 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with "
1581 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
1582 report_data->FormatHandle(src->vk_shader_module()).c_str());
1583 }
1584 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1585 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
1586 skip |= LogError(
1587 device, kVUID_Core_Shader_AtomicFeature,
1588 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1589 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
1590 report_data->FormatHandle(src->vk_shader_module()).c_str());
1591 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
1592 skip |= LogError(
1593 device, kVUID_Core_Shader_AtomicFeature,
1594 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1595 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
1596 report_data->FormatHandle(src->vk_shader_module()).c_str());
1597 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
1598 skip |= LogError(
1599 device, kVUID_Core_Shader_AtomicFeature,
1600 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1601 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
1602 report_data->FormatHandle(src->vk_shader_module()).c_str());
1603 }
1604 } else {
1605 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1606 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
1607 skip |= LogError(
1608 device, kVUID_Core_Shader_AtomicFeature,
1609 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1610 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
1611 report_data->FormatHandle(src->vk_shader_module()).c_str());
1612 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
1613 skip |= LogError(
1614 device, kVUID_Core_Shader_AtomicFeature,
1615 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1616 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
1617 report_data->FormatHandle(src->vk_shader_module()).c_str());
1618 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
1619 skip |= LogError(
1620 device, kVUID_Core_Shader_AtomicFeature,
1621 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1622 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
1623 report_data->FormatHandle(src->vk_shader_module()).c_str());
1624 }
1625 }
1626 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1627 if (valid_workgroup_float == false) {
1628 skip |= LogError(
1629 device, kVUID_Core_Shader_AtomicFeature,
1630 "%s: Can't use float atomics operations with Workgroup storage class without shaderSharedFloat32Atomics or "
1631 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1632 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1633 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1634 report_data->FormatHandle(src->vk_shader_module()).c_str());
1635 } else if (opcode == spv::OpAtomicFAddEXT) {
1636 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1637 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1638 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1639 "storage class without shaderSharedFloat16AtomicAdd enabled.",
1640 report_data->FormatHandle(src->vk_shader_module()).c_str());
1641 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1642 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1643 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1644 "storage class without shaderSharedFloat32AtomicAdd enabled.",
1645 report_data->FormatHandle(src->vk_shader_module()).c_str());
1646 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1647 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1648 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1649 "storage class without shaderSharedFloat64AtomicAdd enabled.",
1650 report_data->FormatHandle(src->vk_shader_module()).c_str());
1651 }
1652 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1653 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
1654 skip |= LogError(
1655 device, kVUID_Core_Shader_AtomicFeature,
1656 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1657 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
1658 report_data->FormatHandle(src->vk_shader_module()).c_str());
1659 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
1660 skip |= LogError(
1661 device, kVUID_Core_Shader_AtomicFeature,
1662 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1663 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
1664 report_data->FormatHandle(src->vk_shader_module()).c_str());
1665 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
1666 skip |= LogError(
1667 device, kVUID_Core_Shader_AtomicFeature,
1668 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1669 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
1670 report_data->FormatHandle(src->vk_shader_module()).c_str());
1671 }
1672 } else {
1673 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1674 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
1675 skip |= LogError(
1676 device, kVUID_Core_Shader_AtomicFeature,
1677 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1678 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat16Atomics enabled.",
1679 report_data->FormatHandle(src->vk_shader_module()).c_str());
1680 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
1681 skip |= LogError(
1682 device, kVUID_Core_Shader_AtomicFeature,
1683 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1684 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat32Atomics enabled.",
1685 report_data->FormatHandle(src->vk_shader_module()).c_str());
1686 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
1687 skip |= LogError(
1688 device, kVUID_Core_Shader_AtomicFeature,
1689 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1690 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat64Atomics enabled.",
1691 report_data->FormatHandle(src->vk_shader_module()).c_str());
1692 }
1693 }
1694 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
1695 skip |=
1696 LogError(device, kVUID_Core_Shader_AtomicFeature,
1697 "%s: Can't use float atomics operations with Image storage class without shaderImageFloat32Atomics or "
1698 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
1699 report_data->FormatHandle(src->vk_shader_module()).c_str());
1700 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
1701 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1702 "%s: Can't use 16-bit float atomics operations without shaderBufferFloat16Atomics, "
1703 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1704 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
1705 report_data->FormatHandle(src->vk_shader_module()).c_str());
1706 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
1707 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1708 "%s: Can't use 32-bit float atomics operations without shaderBufferFloat32AtomicMinMax, "
1709 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1710 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1711 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1712 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
1713 report_data->FormatHandle(src->vk_shader_module()).c_str());
1714 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
1715 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1716 "%s: Can't use 64-bit float atomics operations without shaderBufferFloat64AtomicMinMax, "
1717 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1718 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
1719 report_data->FormatHandle(src->vk_shader_module()).c_str());
1720 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001721 }
1722 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001723 return skip;
1724}
1725
John Zulaufac4c6e12019-07-01 16:05:58 -06001726bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001727 auto entrypoint_id = entrypoint.word(2);
1728
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001729 // The first denorm execution mode encountered, along with its bit width.
1730 // Used to check if SeparateDenormSettings is respected.
1731 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001732
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001733 // The first rounding mode encountered, along with its bit width.
1734 // Used to check if SeparateRoundingModeSettings is respected.
1735 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001736
1737 bool skip = false;
1738
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001739 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001740 uint32_t invocations = 0;
1741
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001742 auto it = src->execution_mode_inst.find(entrypoint_id);
1743 if (it != src->execution_mode_inst.end()) {
1744 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001745 auto mode = insn.word(2);
1746 switch (mode) {
1747 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1748 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001749 if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) ||
1750 (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) ||
1751 (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001752 skip |= LogError(
1753 device, kVUID_Core_Shader_FeatureNotEnabled,
1754 "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device",
1755 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001756 }
1757 break;
1758 }
1759
1760 case spv::ExecutionModeDenormPreserve: {
1761 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001762 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) ||
1763 (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) ||
1764 (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001765 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1766 "Shader requires DenormPreserve for bit width %d but it is not enabled on the device",
1767 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001768 }
1769
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001770 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1771 // Register the first denorm execution mode found
1772 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001773 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001774 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001775 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001776 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001777 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1778 "Shader uses different denorm execution modes for 16 and 64-bit but "
1779 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001780 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001781 }
1782 break;
1783
Mike Schuchardt2df08912020-12-15 16:28:09 -08001784 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001785 break;
1786
Mike Schuchardt2df08912020-12-15 16:28:09 -08001787 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001788 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1789 "Shader uses different denorm execution modes for different bit widths but "
1790 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001791 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001792 break;
1793
1794 default:
1795 break;
1796 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001797 }
1798 break;
1799 }
1800
1801 case spv::ExecutionModeDenormFlushToZero: {
1802 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001803 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) ||
1804 (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) ||
1805 (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001806 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1807 "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device",
1808 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001809 }
1810
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001811 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1812 // Register the first denorm execution mode found
1813 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001814 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001815 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001816 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001817 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001818 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1819 "Shader uses different denorm execution modes for 16 and 64-bit but "
1820 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001821 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001822 }
1823 break;
1824
Mike Schuchardt2df08912020-12-15 16:28:09 -08001825 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001826 break;
1827
Mike Schuchardt2df08912020-12-15 16:28:09 -08001828 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001829 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1830 "Shader uses different denorm execution modes for different bit widths but "
1831 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001832 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001833 break;
1834
1835 default:
1836 break;
1837 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001838 }
1839 break;
1840 }
1841
1842 case spv::ExecutionModeRoundingModeRTE: {
1843 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001844 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) ||
1845 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) ||
1846 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001847 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1848 "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device",
1849 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001850 }
1851
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001852 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1853 // Register the first rounding mode found
1854 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001855 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001856 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001857 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001858 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001859 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1860 "Shader uses different rounding modes for 16 and 64-bit but "
1861 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001862 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001863 }
1864 break;
1865
Mike Schuchardt2df08912020-12-15 16:28:09 -08001866 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001867 break;
1868
Mike Schuchardt2df08912020-12-15 16:28:09 -08001869 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001870 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1871 "Shader uses different rounding modes for different bit widths but "
1872 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001873 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001874 break;
1875
1876 default:
1877 break;
1878 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001879 }
1880 break;
1881 }
1882
1883 case spv::ExecutionModeRoundingModeRTZ: {
1884 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001885 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) ||
1886 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) ||
1887 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001888 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1889 "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device",
1890 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001891 }
1892
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001893 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1894 // Register the first rounding mode found
1895 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001896 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001897 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001898 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001899 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001900 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1901 "Shader uses different rounding modes for 16 and 64-bit but "
1902 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001903 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001904 }
1905 break;
1906
Mike Schuchardt2df08912020-12-15 16:28:09 -08001907 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001908 break;
1909
Mike Schuchardt2df08912020-12-15 16:28:09 -08001910 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001911 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1912 "Shader uses different rounding modes for different bit widths but "
1913 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001914 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001915 break;
1916
1917 default:
1918 break;
1919 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001920 }
1921 break;
1922 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001923
1924 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001925 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001926 break;
1927 }
1928
1929 case spv::ExecutionModeInvocations: {
1930 invocations = insn.word(3);
1931 break;
1932 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001933 }
1934 }
1935 }
1936
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001937 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001938 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001939 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
1940 "Geometry shader entry point must have an OpExecutionMode instruction that "
1941 "specifies a maximum output vertex count that is greater than 0 and less "
1942 "than or equal to maxGeometryOutputVertices. "
1943 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001944 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001945 }
1946
1947 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001948 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
1949 "Geometry shader entry point must have an OpExecutionMode instruction that "
1950 "specifies an invocation count that is greater than 0 and less "
1951 "than or equal to maxGeometryShaderInvocations. "
1952 "Invocations=%d, maxGeometryShaderInvocations=%d",
1953 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001954 }
1955 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001956 return skip;
1957}
1958
Chris Forbes47567b72017-06-09 12:09:45 -07001959// For given pipelineLayout verify that the set_layout_node at slot.first
1960// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06001961static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001962 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07001963 if (!pipelineLayout) return nullptr;
1964
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001965 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07001966
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001967 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07001968}
1969
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001970// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
1971// o If there is only a vertex shader : gl_PointSize must be written when using points
1972// o If there is a geometry or tessellation shader:
1973// - If shaderTessellationAndGeometryPointSize feature is enabled:
1974// * gl_PointSize must be written in the final geometry stage
1975// - If shaderTessellationAndGeometryPointSize feature is disabled:
1976// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001977bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06001978 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001979 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
1980 return false;
1981 }
1982
1983 bool pointsize_written = false;
1984 bool skip = false;
1985
1986 // Search for PointSize built-in decorations
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001987 for (auto set : src->builtin_decoration_list) {
1988 auto insn = src->at(set.offset);
1989 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001990 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001991 if (pointsize_written) {
1992 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001993 }
1994 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001995 }
1996
1997 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001998 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001999 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002000 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002001 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2002 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002003 }
2004 } else if (!pointsize_written) {
2005 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002006 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002007 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2008 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002009 }
2010 return skip;
2011}
John Zulauf14c355b2019-06-27 16:09:37 -06002012
Tobias Hector6663c9b2020-11-05 10:18:02 +00002013bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
2014 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2015 bool primitiverate_written = false;
2016 bool viewportindex_written = false;
2017 bool viewportmask_written = false;
2018 bool skip = false;
2019
2020 // Check if the primitive shading rate is written
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002021 for (auto set : src->builtin_decoration_list) {
2022 auto insn = src->at(set.offset);
2023 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002024 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002025 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002026 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002027 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002028 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002029 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002030 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2031 break;
2032 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002033 }
2034
Tony-LunarGd44844c2021-01-22 13:24:37 -07002035 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2036 pipeline->graphicsPipelineCI.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002037 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
2038 pipeline->graphicsPipelineCI.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002039 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002040 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2041 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2042 "multiple viewports "
2043 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2044 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002045 }
2046
2047 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002048 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002049 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2050 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2051 "ViewportIndex built-ins,"
2052 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2053 string_VkShaderStageFlagBits(stage));
2054 }
2055
2056 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002057 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002058 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2059 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2060 "ViewportMaskNV built-ins,"
2061 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2062 string_VkShaderStageFlagBits(stage));
2063 }
2064 }
2065 return skip;
2066}
2067
sfricke-samsung486a51e2021-01-02 00:10:15 -08002068// Validate runtime usage of various opcodes that depends on what Vulkan properties or features are exposed
sfricke-samsung94167ca2021-02-26 04:14:59 -08002069bool CoreChecks::ValidatePropertiesAndFeatures(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002070 bool skip = false;
2071
sfricke-samsung94167ca2021-02-26 04:14:59 -08002072 switch (insn.opcode()) {
2073 case spv::OpReadClockKHR: {
2074 auto scope_id = module->get_def(insn.word(3));
2075 auto scope_type = scope_id.word(3);
2076 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002077 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung94167ca2021-02-26 04:14:59 -08002078 skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderSubgroupClock",
2079 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002080 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002081 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung94167ca2021-02-26 04:14:59 -08002082 skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderDeviceClock",
2083 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002084 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002085 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002086 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002087 }
2088 }
2089 return skip;
2090}
2091
John Zulauf14c355b2019-06-27 16:09:37 -06002092bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline,
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002093 const PipelineStageState &stage_state, const SHADER_MODULE_STATE *module,
John Zulaufac4c6e12019-07-01 16:05:58 -06002094 const spirv_inst_iter &entrypoint, bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002095 bool skip = false;
2096
2097 // Check the module
2098 if (!module->has_valid_spirv) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002099 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2100 "%s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002101 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(pStage->stage));
John Zulauf14c355b2019-06-27 16:09:37 -06002102 }
2103
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002104 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2105 // specializations should be applied and validated.
2106 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2107 pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) {
2108 // Gather the specialization-constant values.
2109 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002110 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002111 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 -06002112 id_value_map.reserve(specialization_info->mapEntryCount);
2113 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2114 auto const &map_entry = specialization_info->pMapEntries[i];
sfricke-samsung033b0262021-07-09 00:53:06 -07002115 auto itr = module->spec_const_map.find(map_entry.constantID);
2116 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2117 // behavior of the pipeline."
2118 if (itr != module->spec_const_map.cend()) {
2119 // Make sure map_entry.size matches the spec constant's size
2120 uint32_t spec_const_size = decoration_set::kInvalidValue;
2121 const auto def_ins = module->get_def(itr->second);
2122 const auto type_ins = module->get_def(def_ins.word(1));
2123 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2124 switch (type_ins.opcode()) {
2125 case spv::OpTypeBool:
2126 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2127 spec_const_size = sizeof(VkBool32);
2128 break;
2129 case spv::OpTypeInt:
2130 case spv::OpTypeFloat:
2131 spec_const_size = type_ins.word(2) / 8;
2132 break;
2133 default:
2134 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2135 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2136 break;
2137 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002138
sfricke-samsung033b0262021-07-09 00:53:06 -07002139 if (map_entry.size != spec_const_size) {
2140 skip |=
2141 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002142 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2143 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2144 map_entry.constantID, i, map_entry.size,
2145 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002146 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002147 }
2148
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002149 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002150 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2151 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2152 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2153 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2154 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2155
2156 std::copy(start_in_p, end_in_p, out_p);
2157 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002158 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002159 }
2160
2161 // Apply the specialization-constant values and revalidate the shader module.
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002162 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002163 spvtools::Optimizer optimizer(spirv_environment);
2164 spvtools::MessageConsumer consumer = [&skip, &module, &pStage, this](spv_message_level_t level, const char *source,
2165 const spv_position_t &position, const char *message) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002166 skip |= LogError(
2167 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s. %s",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002168 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(pStage->stage), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002169 };
2170 optimizer.SetMessageConsumer(consumer);
2171 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2172 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2173 std::vector<uint32_t> specialized_spirv;
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002174 auto const optimized = optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002175 assert(optimized == true);
2176
2177 if (optimized) {
2178 spv_context ctx = spvContextCreate(spirv_environment);
2179 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2180 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002181 spvtools::ValidatorOptions options;
2182 AdjustValidatorOptions(device_extensions, enabled_features, options);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002183 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2184 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002185 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002186 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002187 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002188 string_VkShaderStageFlagBits(pStage->stage));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002189 }
2190
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002191 spvDiagnosticDestroy(diag);
2192 spvContextDestroy(ctx);
2193 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002194
2195 skip |= ValidateWorkgroupSize(module, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002196 }
2197
John Zulauf14c355b2019-06-27 16:09:37 -06002198 // Check the entrypoint
2199 if (entrypoint == module->end()) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002200 skip |=
Petr Krausb0d5e592021-05-21 23:37:11 +02002201 LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002202 pStage->pName, string_VkShaderStageFlagBits(pStage->stage));
John Zulauf14c355b2019-06-27 16:09:37 -06002203 }
2204 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2205
2206 // Mark accessible ids
2207 auto &accessible_ids = stage_state.accessible_ids;
2208
Chris Forbes47567b72017-06-09 12:09:45 -07002209 // Validate descriptor set layout against what the entrypoint actually uses
John Zulauf14c355b2019-06-27 16:09:37 -06002210 bool has_writable_descriptor = stage_state.has_writable_descriptor;
2211 auto &descriptor_uses = stage_state.descriptor_uses;
Chris Forbes47567b72017-06-09 12:09:45 -07002212
sfricke-samsung94167ca2021-02-26 04:14:59 -08002213 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2214 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002215 uint32_t total_shared_size = 0;
sfricke-samsung94167ca2021-02-26 04:14:59 -08002216 for (auto insn : *module) {
2217 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
2218 skip |= ValidatePropertiesAndFeatures(module, insn);
2219 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02002220 total_shared_size += module->CalcComputeSharedMemory(pStage->stage, insn);
2221 }
2222
2223 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2224 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002225 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002226 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002227 }
2228
locke-lunarg63e4daf2020-08-17 17:53:25 -06002229 skip |=
2230 ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, has_writable_descriptor, stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002231 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03002232 skip |= ValidateShaderStorageImageFormats(module);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002233 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsung58b84352021-07-31 21:41:04 -07002234 skip |= ValidateAtomicsTypes(module);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002235 skip |= ValidateExecutionModes(module, entrypoint);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002236 skip |= ValidateSpecializations(pStage);
Jeff Bolze54ae892018-09-08 12:16:29 -05002237 if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002238 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002239 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07002240 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002241 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
2242 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
2243 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002244 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
2245 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
2246 }
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002247 if (device_extensions.vk_qcom_render_pass_shader_resolve != kNotEnabled) {
2248 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
2249 }
Chris Forbes47567b72017-06-09 12:09:45 -07002250
sfricke-samsung7699b912021-04-12 23:01:51 -07002251 // "layout must be consistent with the layout of the * shader"
2252 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002253 std::string vuid_layout_mismatch;
2254 if (pipeline->graphicsPipelineCI.sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) {
2255 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2256 } else if (pipeline->computePipelineCI.sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO) {
2257 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2258 } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR) {
2259 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2260 } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV) {
2261 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2262 }
2263
sfricke-samsung7699b912021-04-12 23:01:51 -07002264 // Validate Push Constants use
2265 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
2266
Chris Forbes47567b72017-06-09 12:09:45 -07002267 // Validate descriptor use
2268 for (auto use : descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002269 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002270 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002271 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002272 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2273 std::set<uint32_t> descriptor_types =
2274 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002275
2276 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002277 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002278 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002279 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002280 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002281 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002282 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2283 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002284 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2285 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002286 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002287 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2288 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002289 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002290 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002291 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002292 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002293 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002294 }
2295 }
2296
2297 // Validate use of input attachments against subpass structure
2298 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002299 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002300
Petr Krause91f7a12017-12-14 20:57:36 +01002301 auto rpci = pipeline->rp_state->createInfo.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002302 auto subpass = pipeline->graphicsPipelineCI.subpass;
2303
2304 for (auto use : input_attachment_uses) {
2305 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2306 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07002307 ? input_attachments[use.first].attachment
2308 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002309
2310 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002311 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2312 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsung962cad92021-04-13 00:46:29 -07002313 } else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002314 skip |=
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002315 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2316 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
sfricke-samsung962cad92021-04-13 00:46:29 -07002317 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002318 }
2319 }
2320 }
Lockeaa8fdc02019-04-02 11:59:20 -06002321 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08002322 skip |= ValidateComputeWorkGroupSizes(module, entrypoint);
Lockeaa8fdc02019-04-02 11:59:20 -06002323 }
Chris Forbes47567b72017-06-09 12:09:45 -07002324 return skip;
2325}
2326
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002327bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2328 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2329 spirv_inst_iter consumer_entrypoint,
2330 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002331 bool skip = false;
2332
2333 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002334 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2335 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002336
2337 auto a_it = outputs.begin();
2338 auto b_it = inputs.begin();
2339
2340 // Maps sorted by key (location); walk them together to find mismatches
2341 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2342 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2343 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2344 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2345 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2346
2347 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002348 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002349 "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name,
2350 a_first.first, a_first.second, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002351 a_it++;
2352 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002353 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002354 "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first,
2355 b_first.second, producer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002356 b_it++;
2357 } else {
2358 // subtleties of arrayed interfaces:
2359 // - if is_patch, then the member is not arrayed, even though the interface may be.
2360 // - if is_block_member, then the extra array level of an arrayed interface is not
2361 // expressed in the member type -- it's expressed in the block type.
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002362 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id,
2363 producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member,
2364 consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002365 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002366 "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002367 producer->DescribeType(a_it->second.type_id).c_str(),
2368 consumer->DescribeType(b_it->second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002369 }
2370 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002371 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002372 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2373 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2374 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002375 }
2376 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002377 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002378 "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first,
2379 a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002380 }
2381 a_it++;
2382 b_it++;
2383 }
2384 }
2385
Ari Suonpaa696b3432019-03-11 14:02:57 +02002386 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002387 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2388 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002389
2390 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2391 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002392 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002393 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002394 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2395 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002396 } else {
2397 auto it_producer = builtins_producer.begin();
2398 auto it_consumer = builtins_consumer.begin();
2399 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2400 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002401 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002402 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2403 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002404 break;
2405 }
2406 it_producer++;
2407 it_consumer++;
2408 }
2409 }
2410 }
2411 }
2412
Chris Forbes47567b72017-06-09 12:09:45 -07002413 return skip;
2414}
2415
John Zulauf14c355b2019-06-27 16:09:37 -06002416static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002417 uint32_t stage_mask = 0;
2418 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2419 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2420 stage_mask |= pCreateInfo->pStages[i].stage;
2421 }
2422 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002423 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2424 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2425 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002426 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2427 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2428 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2429 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2430 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002431 }
2432 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002433 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002434}
2435
Chris Forbes47567b72017-06-09 12:09:45 -07002436// Validate that the shaders used by the given pipeline and store the active_slots
2437// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002438bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002439 auto create_info = pipeline->graphicsPipelineCI.ptr();
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002440 int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
2441 int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07002442
John Zulauf14c355b2019-06-27 16:09:37 -06002443 const SHADER_MODULE_STATE *shaders[32];
Chris Forbes47567b72017-06-09 12:09:45 -07002444 memset(shaders, 0, sizeof(shaders));
Jeff Bolz7e35c392018-09-04 15:30:41 -05002445 spirv_inst_iter entrypoints[32];
Chris Forbes47567b72017-06-09 12:09:45 -07002446 bool skip = false;
2447
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002448 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002449
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002450 for (uint32_t i = 0; i < create_info->stageCount; i++) {
2451 auto stage = &create_info->pStages[i];
2452 auto stage_id = GetShaderStageId(stage->stage);
2453 shaders[stage_id] = GetShaderModuleState(stage->module);
sfricke-samsung962cad92021-04-13 00:46:29 -07002454 entrypoints[stage_id] = shaders[stage_id]->FindEntrypoint(stage->pName, stage->stage);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002455 skip |= ValidatePipelineShaderStage(stage, pipeline, pipeline->stage_state[i], shaders[stage_id], entrypoints[stage_id],
2456 (pointlist_stage_mask == stage->stage));
Chris Forbes47567b72017-06-09 12:09:45 -07002457 }
2458
2459 // if the shader stages are no good individually, cross-stage validation is pointless.
2460 if (skip) return true;
2461
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002462 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002463
2464 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002465 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002466 }
2467
Piers Daniell924cd832021-05-18 13:48:47 -06002468 if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv &&
2469 !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002470 skip |= ValidateViAgainstVsInputs(vi, shaders[vertex_stage], entrypoints[vertex_stage]);
Chris Forbes47567b72017-06-09 12:09:45 -07002471 }
2472
Shannon McPhersonc06c33d2018-06-28 17:21:12 -06002473 int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT);
2474 int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT);
Chris Forbes47567b72017-06-09 12:09:45 -07002475
2476 while (!shaders[producer] && producer != fragment_stage) {
2477 producer++;
2478 consumer++;
2479 }
2480
2481 for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) {
2482 assert(shaders[producer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002483 if (shaders[consumer]) {
2484 if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002485 skip |= ValidateInterfaceBetweenStages(shaders[producer], entrypoints[producer], &shader_stage_attribs[producer],
2486 shaders[consumer], entrypoints[consumer], &shader_stage_attribs[consumer]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002487 }
Chris Forbes47567b72017-06-09 12:09:45 -07002488
2489 producer = consumer;
2490 }
2491 }
2492
2493 if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002494 skip |= ValidateFsOutputsAgainstRenderPass(shaders[fragment_stage], entrypoints[fragment_stage], pipeline,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002495 create_info->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07002496 }
2497
2498 return skip;
2499}
2500
Tony-LunarGb2ded512021-02-02 16:03:30 -07002501void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) {
2502 auto create_info = pipeline_state->graphicsPipelineCI.ptr();
2503
2504 if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports ||
2505 !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) {
2506 return;
2507 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002508
Nathaniel Cesario1c3d3652021-01-25 18:35:12 -07002509 std::array<const SHADER_MODULE_STATE *, 32> shaders;
2510 std::fill(shaders.begin(), shaders.end(), nullptr);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002511 spirv_inst_iter entrypoints[32];
Tobias Hector6663c9b2020-11-05 10:18:02 +00002512
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002513 for (uint32_t i = 0; i < create_info->stageCount; i++) {
2514 auto stage = &create_info->pStages[i];
2515 auto stage_id = GetShaderStageId(stage->stage);
2516 shaders[stage_id] = GetShaderModuleState(stage->module);
sfricke-samsung962cad92021-04-13 00:46:29 -07002517 entrypoints[stage_id] = shaders[stage_id]->FindEntrypoint(stage->pName, stage->stage);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002518
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002519 if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT ||
2520 stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002521 bool primitiverate_written = false;
Tobias Hector6663c9b2020-11-05 10:18:02 +00002522
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002523 for (auto set : shaders[stage_id]->builtin_decoration_list) {
2524 auto insn = shaders[stage_id]->at(set.offset);
2525 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002526 primitiverate_written = shaders[stage_id]->IsBuiltInWritten(insn, entrypoints[stage_id]);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002527 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002528 if (primitiverate_written) {
2529 break;
2530 }
Tony-LunarGb2ded512021-02-02 16:03:30 -07002531 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002532
Tony-LunarGb2ded512021-02-02 16:03:30 -07002533 if (primitiverate_written) {
2534 pipeline_state->wrote_primitive_shading_rate.insert(stage->stage);
2535 }
2536 }
2537 }
2538}
2539
2540bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2541 const char *caller, const DrawDispatchVuid &vuid) const {
2542 auto create_info = pipeline->graphicsPipelineCI.ptr();
2543 bool skip = false;
2544
2545 for (uint32_t i = 0; i < create_info->stageCount; i++) {
2546 auto stage = &create_info->pStages[i];
2547 if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT ||
2548 stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) {
2549 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2550 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
2551 if (pipeline->wrote_primitive_shading_rate.find(stage->stage) != pipeline->wrote_primitive_shading_rate.end()) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002552 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002553 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002554 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2555 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2556 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002557 caller, string_VkShaderStageFlagBits(stage->stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002558 }
2559 }
2560 }
2561 }
2562
2563 return skip;
2564}
2565
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002566bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002567 const auto &stage = *pipeline->computePipelineCI.stage.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002568
John Zulauf14c355b2019-06-27 16:09:37 -06002569 const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module);
sfricke-samsung962cad92021-04-13 00:46:29 -07002570 const spirv_inst_iter entrypoint = module->FindEntrypoint(stage.pName, stage.stage);
Chris Forbes47567b72017-06-09 12:09:45 -07002571
John Zulauf14c355b2019-06-27 16:09:37 -06002572 return ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[0], module, entrypoint, false);
Chris Forbes47567b72017-06-09 12:09:45 -07002573}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002574
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002575uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2576 uint32_t total = 0;
2577
2578 const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages;
2579 for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) {
2580 if (stages[stage_index].stage == stageBit) {
2581 total++;
2582 }
2583 }
2584
2585 if (pipeline->raytracingPipelineCI.pLibraryInfo) {
2586 for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) {
2587 const PIPELINE_STATE *library_pipeline = GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]);
2588 total += CalcShaderStageCount(library_pipeline, stageBit);
2589 }
2590 }
2591
2592 return total;
2593}
2594
sourav parmarcd5fb182020-07-17 12:58:44 -07002595bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06002596 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04002597
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002598 if (isKHR) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002599 if (pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth >
2600 phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
2601 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
2602 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
2603 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
2604 pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth,
2605 phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002606 }
sourav parmarcd5fb182020-07-17 12:58:44 -07002607 if (pipeline->raytracingPipelineCI.pLibraryInfo) {
2608 for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002609 const PIPELINE_STATE *library_pipelinestate =
sourav parmarcd5fb182020-07-17 12:58:44 -07002610 GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002611 if (library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth !=
sourav parmarcd5fb182020-07-17 12:58:44 -07002612 pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth) {
2613 skip |= LogError(
2614 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
2615 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
2616 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002617 i, library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth,
sourav parmarcd5fb182020-07-17 12:58:44 -07002618 pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth);
2619 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002620 if (library_pipelinestate->raytracingPipelineCI.pLibraryInfo &&
2621 (library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize !=
sourav parmarcd5fb182020-07-17 12:58:44 -07002622 pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize ||
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002623 library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize !=
sourav parmarcd5fb182020-07-17 12:58:44 -07002624 pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize)) {
2625 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
2626 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
2627 "member must have been created with values of the maxPipelineRayPayloadSize and "
2628 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
2629 }
2630 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002631 !(library_pipelinestate->raytracingPipelineCI.flags &
sourav parmarcd5fb182020-07-17 12:58:44 -07002632 VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
2633 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
2634 "vkCreateRayTracingPipelinesKHR: If flags includes "
2635 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
2636 "the pLibraries member of libraries must have been created with the "
2637 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
2638 }
sourav parmar83c31b12020-05-06 12:30:54 -07002639 }
2640 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002641 } else {
2642 if (pipeline->raytracingPipelineCI.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002643 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
2644 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
2645 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002646 pipeline->raytracingPipelineCI.maxRecursionDepth,
2647 phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
2648 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002649 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002650 const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages;
2651 const auto *groups = pipeline->raytracingPipelineCI.ptr()->pGroups;
2652
John Zulaufe4474e72019-07-01 17:28:27 -06002653 for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04002654 const auto &stage = stages[stage_index];
Jeff Bolzfbe51582018-09-13 10:01:35 -05002655
John Zulaufe4474e72019-07-01 17:28:27 -06002656 const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module);
sfricke-samsung962cad92021-04-13 00:46:29 -07002657 const spirv_inst_iter entrypoint = module->FindEntrypoint(stage.pName, stage.stage);
Jeff Bolzfbe51582018-09-13 10:01:35 -05002658
John Zulaufe4474e72019-07-01 17:28:27 -06002659 skip |= ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[stage_index], module, entrypoint, false);
Jason Macnak15f95e82019-08-21 21:52:02 -04002660 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002661
2662 if ((pipeline->raytracingPipelineCI.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
2663 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
2664 if (raygen_stages_count == 0) {
2665 skip |= LogError(
2666 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07002667 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002668 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
2669 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002670 }
2671
2672 for (uint32_t group_index = 0; group_index < pipeline->raytracingPipelineCI.groupCount; group_index++) {
2673 const auto &group = groups[group_index];
2674
2675 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
2676 if (group.generalShader >= pipeline->raytracingPipelineCI.stageCount ||
2677 (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV &&
2678 stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV &&
2679 stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002680 skip |= LogError(device,
2681 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
2682 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
2683 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002684 }
2685 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
2686 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002687 skip |= LogError(device,
2688 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
2689 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
2690 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002691 }
2692 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
2693 if (group.intersectionShader >= pipeline->raytracingPipelineCI.stageCount ||
2694 stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002695 skip |= LogError(device,
2696 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
2697 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
2698 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002699 }
2700 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2701 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002702 skip |= LogError(device,
2703 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
2704 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
2705 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002706 }
2707 }
2708
2709 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
2710 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2711 if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= pipeline->raytracingPipelineCI.stageCount ||
2712 stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002713 skip |= LogError(device,
2714 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
2715 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
2716 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002717 }
2718 if (group.closestHitShader != VK_SHADER_UNUSED_NV &&
2719 (group.closestHitShader >= pipeline->raytracingPipelineCI.stageCount ||
2720 stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002721 skip |= LogError(device,
2722 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
2723 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
2724 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002725 }
2726 }
John Zulaufe4474e72019-07-01 17:28:27 -06002727 }
2728 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002729}
2730
Dave Houltona9df0ce2018-02-07 10:51:23 -07002731uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07002732
Dave Houltona9df0ce2018-02-07 10:51:23 -07002733static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002734 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06002735 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06002736 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002737 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002738 return nullptr;
2739}
2740
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002741bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002742 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002743 bool skip = false;
2744 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002745
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06002746 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002747 return false;
2748 }
2749
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002750 auto have_glsl_shader = device_extensions.vk_nv_glsl_shader;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002751
2752 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002753 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
2754 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
2755 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002756 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07002757 auto cache = GetValidationCacheInfo(pCreateInfo);
2758 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07002759 // If app isn't using a shader validation cache, use the default one from CoreChecks
2760 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002761 if (cache) {
2762 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002763 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07002764 }
2765
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002766 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
2767 // the default values will be used during validation.
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002768 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
Dave Houlton0ea2d012018-06-21 14:00:26 -06002769 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002770 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07002771 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002772 spvtools::ValidatorOptions options;
2773 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06002774 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002775 if (spv_valid != SPV_SUCCESS) {
2776 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002777 if (spv_valid == SPV_WARNING) {
2778 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2779 diag && diag->error ? diag->error : "(no error text)");
2780 } else {
2781 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2782 diag && diag->error ? diag->error : "(no error text)");
2783 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002784 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002785 } else {
2786 if (cache) {
2787 cache->Insert(hash);
2788 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002789 }
2790
2791 spvDiagnosticDestroy(diag);
2792 spvContextDestroy(ctx);
2793 }
2794
Chris Forbes4ae55b32017-06-09 14:42:56 -07002795 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07002796}
2797
sfricke-samsung8a7341a2021-02-28 07:30:21 -08002798bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint) const {
Lockeaa8fdc02019-04-02 11:59:20 -06002799 bool skip = false;
2800 uint32_t local_size_x = 0;
2801 uint32_t local_size_y = 0;
2802 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07002803 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06002804 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002805 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002806 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002807 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002808 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06002809 }
2810 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002811 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002812 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002813 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002814 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06002815 }
2816 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002817 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002818 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002819 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002820 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06002821 }
2822
2823 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
2824 uint64_t invocations = local_size_x * local_size_y;
2825 // Prevent overflow.
2826 bool fail = false;
2827 if (invocations > UINT32_MAX || invocations > limit) {
2828 fail = true;
2829 }
2830 if (!fail) {
2831 invocations *= local_size_z;
2832 if (invocations > UINT32_MAX || invocations > limit) {
2833 fail = true;
2834 }
2835 }
2836 if (fail) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002837 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002838 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
2839 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002840 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 -07002841 limit);
Lockeaa8fdc02019-04-02 11:59:20 -06002842 }
2843 }
2844 return skip;
2845}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002846
2847spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
2848 if (api_version >= VK_API_VERSION_1_2) {
2849 return SPV_ENV_VULKAN_1_2;
2850 } else if (api_version >= VK_API_VERSION_1_1) {
2851 if (spirv_1_4) {
2852 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
2853 } else {
2854 return SPV_ENV_VULKAN_1_1;
2855 }
2856 }
2857 return SPV_ENV_VULKAN_1_0;
2858}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002859
Jeremy Gebben5d970742021-05-31 16:04:14 -06002860void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002861 spvtools::ValidatorOptions &options) {
2862 if (device_extensions.vk_khr_relaxed_block_layout) {
2863 options.SetRelaxBlockLayout(true);
2864 }
2865 if (device_extensions.vk_khr_uniform_buffer_standard_layout && enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
2866 options.SetUniformBufferStandardLayout(true);
2867 }
2868 if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) {
2869 options.SetScalarBlockLayout(true);
2870 }
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08002871 if (device_extensions.vk_khr_workgroup_memory_explicit_layout &&
2872 enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
2873 options.SetWorkgroupScalarBlockLayout(true);
2874 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002875}