blob: e9881932396022b4d46880b6b164265b76ee7ef3 [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
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020050static const spirv_inst_iter GetBaseTypeIter(SHADER_MODULE_STATE const *src, uint32_t type) {
51 const auto &insn = src->get_def(type);
52 const uint32_t base_insn_id = src->GetBaseType(insn);
53 return src->get_def(base_insn_id);
54}
55
ziga-lunarg8346fe82021-08-22 17:30:50 +020056static bool BaseTypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, const spirv_inst_iter &a_base_insn,
57 const spirv_inst_iter &b_base_insn) {
58 const uint32_t a_opcode = a_base_insn.opcode();
59 const uint32_t b_opcode = b_base_insn.opcode();
60 if (a_opcode == b_opcode) {
61 if (a_opcode == spv::OpTypeInt) {
62 // Match width and signedness
63 return a_base_insn.word(2) == b_base_insn.word(2) && a_base_insn.word(3) == b_base_insn.word(3);
64 } else if (a_opcode == spv::OpTypeFloat) {
65 // Match width
66 return a_base_insn.word(2) == b_base_insn.word(2);
67 } else if (a_opcode == spv::OpTypeStruct) {
68 // Match on all element types
69 if (a_base_insn.len() != b_base_insn.len()) {
70 return false; // Structs cannot match if member counts differ
71 }
72
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020073 for (uint32_t i = 2; i < a_base_insn.len(); i++) {
74 const auto &c_base_insn = GetBaseTypeIter(a, a_base_insn.word(i));
75 const auto &d_base_insn = GetBaseTypeIter(b, b_base_insn.word(i));
76 if (!BaseTypesMatch(a, b, c_base_insn, d_base_insn)) {
ziga-lunarg8346fe82021-08-22 17:30:50 +020077 return false;
78 }
79 }
80
81 return true;
82 }
83 }
84 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070085}
86
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020087static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, uint32_t a_type, uint32_t b_type) {
88 const auto &a_base_insn = GetBaseTypeIter(a, a_type);
89 const auto &b_base_insn = GetBaseTypeIter(b, b_type);
Chris Forbes47567b72017-06-09 12:09:45 -070090
ziga-lunarg8346fe82021-08-22 17:30:50 +020091 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070092}
93
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060094static unsigned GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -070095 switch (format) {
96 case VK_FORMAT_R64G64B64A64_SFLOAT:
97 case VK_FORMAT_R64G64B64A64_SINT:
98 case VK_FORMAT_R64G64B64A64_UINT:
99 case VK_FORMAT_R64G64B64_SFLOAT:
100 case VK_FORMAT_R64G64B64_SINT:
101 case VK_FORMAT_R64G64B64_UINT:
102 return 2;
103 default:
104 return 1;
105 }
106}
107
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600108static unsigned GetFormatType(VkFormat fmt) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700109 if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT;
110 if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT;
sfricke-samsunged028b02021-09-06 23:14:51 -0700111 // Formats such as VK_FORMAT_D16_UNORM_S8_UINT are both
Dave Houltona9df0ce2018-02-07 10:51:23 -0700112 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
113 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700114 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
115 return FORMAT_TYPE_FLOAT;
116}
117
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600118static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700119 uint32_t bit_pos = uint32_t(u_ffs(stage));
120 return bit_pos - 1;
121}
122
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700123bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700124 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
125 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700126 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700127 bool skip = false;
128
129 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
130 auto desc = &vi->pVertexBindingDescriptions[i];
131 auto &binding = bindings[desc->binding];
132 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600133 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700134 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
135 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700136 } else {
137 binding = desc;
138 }
139 }
140
141 return skip;
142}
143
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700144bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs,
145 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700146 bool skip = false;
147
sfricke-samsung962cad92021-04-13 00:46:29 -0700148 const auto inputs = vs->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700149
150 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200151 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700152 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200153 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
154 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
155 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700156 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
157 }
158 }
159 }
160
Petr Kraus25810d02019-08-27 17:41:15 +0200161 struct AttribInputPair {
162 const VkVertexInputAttributeDescription *attrib = nullptr;
163 const interface_var *input = nullptr;
164 };
165 std::map<uint32_t, AttribInputPair> location_map;
166 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
167 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700168
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400169 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200170 const auto location = location_it.first;
171 const auto attrib = location_it.second.attrib;
172 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600173
Petr Kraus25810d02019-08-27 17:41:15 +0200174 if (attrib && !input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600175 skip |= LogPerformanceWarning(vs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700176 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200177 } else if (!attrib && input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600178 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700179 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200180 } else if (attrib && input) {
181 const auto attrib_type = GetFormatType(attrib->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700182 const auto input_type = vs->GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700183
184 // Type checking
185 if (!(attrib_type & input_type)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600186 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700187 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sfricke-samsung962cad92021-04-13 00:46:29 -0700188 string_VkFormat(attrib->format), location, vs->DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700189 }
Petr Kraus25810d02019-08-27 17:41:15 +0200190 } else { // !attrib && !input
191 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700192 }
193 }
194
195 return skip;
196}
197
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700198bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint,
199 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200200 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700201
Petr Kraus25810d02019-08-27 17:41:15 +0200202 const auto rpci = pipeline->rp_state->createInfo.ptr();
203
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600204 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800205 const VkAttachmentReference2 *reference = nullptr;
206 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600207 const interface_var *output = nullptr;
208 };
209 std::map<uint32_t, Attachment> location_map;
210
Petr Kraus25810d02019-08-27 17:41:15 +0200211 const auto subpass = rpci->pSubpasses[subpass_index];
212 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600213 auto const &reference = subpass.pColorAttachments[i];
214 location_map[i].reference = &reference;
215 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
216 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
217 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
Chris Forbes47567b72017-06-09 12:09:45 -0700218 }
219 }
220
Chris Forbes47567b72017-06-09 12:09:45 -0700221 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
222
sfricke-samsung962cad92021-04-13 00:46:29 -0700223 const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600224 for (const auto &output_it : outputs) {
225 auto const location = output_it.first.first;
226 location_map[location].output = &output_it.second;
227 }
Chris Forbes47567b72017-06-09 12:09:45 -0700228
Jeremy Gebben11af9792021-08-20 10:20:09 -0600229 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
230 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700231
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400232 for (const auto &location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600233 const auto reference = location_it.second.reference;
234 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
235 continue;
236 }
237
Petr Kraus25810d02019-08-27 17:41:15 +0200238 const auto location = location_it.first;
239 const auto attachment = location_it.second.attachment;
240 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +0200241 if (attachment && !output) {
242 if (pipeline->attachments[location].colorWriteMask != 0) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600243 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700244 "Attachment %" PRIu32
245 " not written by fragment shader; undefined values will be written to attachment",
246 location);
Petr Kraus25810d02019-08-27 17:41:15 +0200247 }
248 } else if (!attachment && output) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700249 if (!(alpha_to_coverage_enabled && location == 0)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600250 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700251 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200252 }
Petr Kraus25810d02019-08-27 17:41:15 +0200253 } else if (attachment && output) {
254 const auto attachment_type = GetFormatType(attachment->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700255 const auto output_type = fs->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700256
257 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +0200258 if (!(output_type & attachment_type)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700259 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600260 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700261 "Attachment %" PRIu32
262 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sfricke-samsung962cad92021-04-13 00:46:29 -0700263 location, string_VkFormat(attachment->format), fs->DescribeType(output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700264 }
Petr Kraus25810d02019-08-27 17:41:15 +0200265 } else { // !attachment && !output
266 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700267 }
268 }
269
Petr Kraus25810d02019-08-27 17:41:15 +0200270 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700271 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
sfricke-samsung962cad92021-04-13 00:46:29 -0700272 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700273 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600274 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700275 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200276 }
277
Chris Forbes47567b72017-06-09 12:09:45 -0700278 return skip;
279}
280
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600281PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
282 const shader_struct_member &push_constant_used_in_shader,
283 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600284 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600285 const auto used_bytes_size = used_bytes->size();
286 if (used_bytes_size == 0) return PC_Byte_Updated;
287
288 const auto push_constant_data_update_size = push_constant_data_update.size();
289 const auto *data = push_constant_data_update.data();
290 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
291 if (used_bytes_size <= push_constant_data_update_size) {
292 return PC_Byte_Updated;
293 }
294 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
295
296 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
297 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
298 return PC_Byte_Updated;
299 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600300 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600301
locke-lunargde3f0fa2020-09-10 11:55:31 -0600302 uint32_t i = 0;
303 for (const auto used : *used_bytes) {
304 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600305 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600306 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600307 return PC_Byte_Not_Set;
308 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600309 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600310 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600311 }
312 }
313 ++i;
314 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600315 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600316}
317
318bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src,
sfricke-samsung7699b912021-04-12 23:01:51 -0700319 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700320 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700321 // Temp workaround to prevent false positive errors
322 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
323 if (src->multiple_entry_points) {
324 return skip;
325 }
326
Chris Forbes47567b72017-06-09 12:09:45 -0700327 // 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 -0700328 const auto *entrypoint = src->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600329 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
330 return skip;
331 }
332 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700333
locke-lunargde3f0fa2020-09-10 11:55:31 -0600334 bool found_stage = false;
335 for (auto const &range : *push_constant_ranges) {
336 if (range.stageFlags & pStage->stage) {
337 found_stage = true;
338 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600339 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600340 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600341 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600342 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600343 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600344 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600345 const auto ret =
346 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700347
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600348 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600349 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600350 LogObjectList objlist(src->vk_shader_module());
351 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700352 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 -0600353 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600354 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600355 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700356 }
357 }
358 }
359
locke-lunargde3f0fa2020-09-10 11:55:31 -0600360 if (!found_stage) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600361 LogObjectList objlist(src->vk_shader_module());
362 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700363 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 -0600364 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module()).c_str(),
365 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700366 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700367 }
Chris Forbes47567b72017-06-09 12:09:45 -0700368 return skip;
369}
370
sfricke-samsungcfb44592021-07-25 00:36:28 -0700371bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700372 bool skip = false;
373
374 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700375 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700376 return skip;
377 }
378
sfricke-samsungcfb44592021-07-25 00:36:28 -0700379 // Find all builtin from just the interface variables
380 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700381 auto insn = src->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700382 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700383 const decoration_set decorations = src->get_decorations(insn.word(2));
384
sfricke-samsungcfb44592021-07-25 00:36:28 -0700385 // Currently don't need to search in structs
386 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700387 auto type_pointer = src->get_def(insn.word(1));
388 assert(type_pointer.opcode() == spv::OpTypePointer);
389
390 auto type = src->get_def(type_pointer.word(3));
391 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700392 uint32_t length = static_cast<uint32_t>(src->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700393 // Handles both the input and output sampleMask
394 if (length > phys_dev_props.limits.maxSampleMaskWords) {
395 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
396 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
397 "maxSampleMaskWords of %u in %s.",
398 length, phys_dev_props.limits.maxSampleMaskWords,
399 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700400 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700401 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700402 }
403 }
404 }
405
406 return skip;
407}
408
Chris Forbes47567b72017-06-09 12:09:45 -0700409// Validate that data for each specialization entry is fully contained within the buffer.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200410bool CoreChecks::ValidateSpecializations(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700411 bool skip = false;
412
413 VkSpecializationInfo const *spec = info->pSpecializationInfo;
414
415 if (spec) {
416 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600417 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700418 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
419 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200420 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700421 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
422 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600423
424 continue;
425 }
Chris Forbes47567b72017-06-09 12:09:45 -0700426 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700427 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
428 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200429 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700430 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
431 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700432 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200433 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
434 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
435 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
436 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
437 j, spec->pMapEntries[i].constantID);
438 }
439 }
Chris Forbes47567b72017-06-09 12:09:45 -0700440 }
441 }
442
443 return skip;
444}
445
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500446// TODO (jbolz): Can this return a const reference?
sourav parmarcd5fb182020-07-17 12:58:44 -0700447static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count,
448 bool is_khr) {
Chris Forbes47567b72017-06-09 12:09:45 -0700449 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800450 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700451 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500452 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700453
454 // 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 -0500455 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
456 if (type.opcode() == spv::OpTypeRuntimeArray) {
457 descriptor_count = 0;
458 type = module->get_def(type.word(2));
459 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700460 descriptor_count *= module->GetConstantValueById(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700461 type = module->get_def(type.word(2));
462 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800463 if (type.word(2) == spv::StorageClassStorageBuffer) {
464 is_storage_buffer = true;
465 }
Chris Forbes47567b72017-06-09 12:09:45 -0700466 type = module->get_def(type.word(3));
467 }
468 }
469
470 switch (type.opcode()) {
471 case spv::OpTypeStruct: {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800472 for (auto insn : module->decoration_inst) {
473 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700474 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800475 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500476 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
477 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
478 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800479 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500480 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
481 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
482 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
483 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800484 }
Chris Forbes47567b72017-06-09 12:09:45 -0700485 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500486 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
487 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
488 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700489 }
490 }
491 }
492
493 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500494 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700495 }
496
497 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500498 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
499 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
500 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700501
Chris Forbes73c00bf2018-06-22 16:28:06 -0700502 case spv::OpTypeSampledImage: {
503 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
504 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
505 auto image_type = module->get_def(type.word(2));
506 auto dim = image_type.word(3);
507 auto sampled = image_type.word(7);
508 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500509 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
510 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700511 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700512 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500513 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
514 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700515
516 case spv::OpTypeImage: {
517 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
518 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
519 auto dim = type.word(3);
520 auto sampled = type.word(7);
521
522 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500523 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
524 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700525 } else if (dim == spv::DimBuffer) {
526 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500527 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
528 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700529 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500530 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
531 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700532 }
533 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500534 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
535 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
536 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700537 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500538 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
539 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700540 }
541 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600542 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700543 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
544 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500545 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700546
547 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
548 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500549 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700550 }
551}
552
Jeff Bolze54ae892018-09-08 12:16:29 -0500553static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700554 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500555 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
556 if (ss.tellp()) ss << ", ";
557 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700558 }
559 return ss.str();
560}
561
sfricke-samsung0065ce02020-12-03 22:46:37 -0800562bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500563 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800564 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 -0500565 return true;
566 }
567 }
568
569 return false;
570}
571
sfricke-samsung0065ce02020-12-03 22:46:37 -0800572bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700573 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800574 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700575 return true;
576 }
577 }
578
579 return false;
580}
581
locke-lunarg63e4daf2020-08-17 17:53:25 -0600582bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
583 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500584 bool skip = false;
585
locke-lunarg63e4daf2020-08-17 17:53:25 -0600586 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800587 switch (stage) {
588 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -0600589 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
590 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
591 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
592 case VK_SHADER_STAGE_MISS_BIT_NV:
593 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
594 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
595 case VK_SHADER_STAGE_TASK_BIT_NV:
596 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -0800597 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -0600598 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -0800599 break;
600 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800601 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700602 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800603 break;
604 default:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800605 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700606 "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800607 break;
608 }
609 }
610
Chris Forbes47567b72017-06-09 12:09:45 -0700611 return skip;
612}
613
sfricke-samsung94167ca2021-02-26 04:14:59 -0800614bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
615 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500616 bool skip = false;
617
sfricke-samsung94167ca2021-02-26 04:14:59 -0800618 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
619 if (GroupOperation(insn.opcode()) == true) {
620 // Check the quad operations.
621 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
622 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700623 skip |=
624 RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
625 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800626 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800627 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500628
sfricke-samsung94167ca2021-02-26 04:14:59 -0800629 uint32_t scope_type = spv::ScopeMax;
630 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
631 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
632 scope_type = spv::ScopeSubgroup;
633 } else {
634 // "All <id> used for Scope <id> must be of an OpConstant"
635 auto scope_id = module->get_def(insn.word(3));
636 scope_type = scope_id.word(3);
637 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800638
sfricke-samsung94167ca2021-02-26 04:14:59 -0800639 if (scope_type == spv::ScopeSubgroup) {
640 // "Group operations with subgroup scope" must have stage support
641 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
642 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700643 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800644 }
645
646 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
647 auto type = module->get_def(insn.word(1));
648
649 if (type.opcode() == spv::OpTypeVector) {
650 // Get the element type
651 type = module->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800652 }
653
sfricke-samsung94167ca2021-02-26 04:14:59 -0800654 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800655 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
656 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500657
sfricke-samsung0065ce02020-12-03 22:46:37 -0800658 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
659 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
660 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
661 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700662 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500663 }
664 }
665 }
Jeff Bolzee743412019-06-20 22:24:32 -0500666 }
667
668 return skip;
669}
670
ziga-lunarg2818f492021-08-12 14:30:51 +0200671bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
672 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
673 bool skip = false;
674
675 std::array<uint32_t, 3> work_group_size = src->GetWorkgroupSize(pStage, id_value_map);
676
677 for (uint32_t i = 0; i < 3; ++i) {
678 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
679 const char member = 'x' + static_cast<int8_t>(i);
680 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
681 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
682 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
683 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
684 }
685 }
686 return skip;
687}
688
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600689bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600690 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200691 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
692 pStage->stage == VK_SHADER_STAGE_ALL) {
693 return false;
694 }
695
696 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700697 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200698
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700699 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200700 struct Variable {
701 uint32_t baseTypePtrID;
702 uint32_t ID;
703 uint32_t storageClass;
704 };
705 std::vector<Variable> variables;
706
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700707 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700708 bool is_iso_lines = false;
709 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500710
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700711 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600712
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200713 for (auto insn : *src) {
714 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500715 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200716 case spv::OpDecorate:
717 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500718 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700719 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200720 break;
721 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200722 default:
723 break;
724 }
725 break;
726 // Find all input and output variables
727 case spv::OpVariable: {
728 Variable var = {};
729 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600730 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
731 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700732 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200733 var.baseTypePtrID = insn.word(1);
734 var.ID = insn.word(2);
735 variables.push_back(var);
736 }
737 break;
738 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500739 case spv::OpExecutionMode:
740 if (insn.word(1) == entrypoint.word(2)) {
741 switch (insn.word(2)) {
742 default:
743 break;
744 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700745 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500746 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700747 case spv::ExecutionModeIsolines:
748 is_iso_lines = true;
749 break;
750 case spv::ExecutionModePointMode:
751 is_point_mode = true;
752 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500753 }
754 }
755 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200756 default:
757 break;
758 }
759 }
760
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500761 bool strip_output_array_level =
762 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
763 bool strip_input_array_level =
764 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
765 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
766
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700767 uint32_t num_comp_in = 0, num_comp_out = 0;
768 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600769
sfricke-samsung962cad92021-04-13 00:46:29 -0700770 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
771 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600772
773 // Find max component location used for input variables.
774 for (auto &var : inputs) {
775 int location = var.first.first;
776 int component = var.first.second;
777 interface_var &iv = var.second;
778
779 // Only need to look at the first location, since we use the type's whole size
780 if (iv.offset != 0) {
781 continue;
782 }
783
784 if (iv.is_patch) {
785 continue;
786 }
787
sfricke-samsung962cad92021-04-13 00:46:29 -0700788 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700789 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600790 }
791
792 // Find max component location used for output variables.
793 for (auto &var : outputs) {
794 int location = var.first.first;
795 int component = var.first.second;
796 interface_var &iv = var.second;
797
798 // Only need to look at the first location, since we use the type's whole size
799 if (iv.offset != 0) {
800 continue;
801 }
802
803 if (iv.is_patch) {
804 continue;
805 }
806
sfricke-samsung962cad92021-04-13 00:46:29 -0700807 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700808 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600809 }
810
811 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
812 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700813 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
814 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200815 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500816 // Check if the variable is a patch. Patches can also be members of blocks,
817 // but if they are then the top-level arrayness has already been stripped
818 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700819 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200820
821 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700822 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200823 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700824 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200825 }
826 }
827
828 switch (pStage->stage) {
829 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700830 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700831 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700832 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
833 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
834 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700835 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200836 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700837 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700838 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700839 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
840 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
841 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600842 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200843 break;
844
845 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700846 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700847 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700848 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
849 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
850 "components by %u components",
851 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700852 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200853 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700854 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600855 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700856 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700857 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
858 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
859 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600860 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700861 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700862 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700863 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
864 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
865 "components by %u components",
866 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700867 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200868 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700869 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600870 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700871 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700872 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
873 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
874 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600875 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200876 break;
877
878 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700879 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700880 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700881 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
882 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
883 "components by %u components",
884 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700885 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200886 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700887 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600888 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700889 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700890 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
891 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
892 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600893 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700894 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700895 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700896 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
897 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
898 "components by %u components",
899 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700900 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200901 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700902 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600903 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700904 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700905 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
906 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
907 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600908 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700909 // Portability validation
910 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
911 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700912 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700913 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
914 " is using abstract patch type IsoLines, but this is not supported on this platform");
915 }
916 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700917 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700918 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
919 " is using abstract patch type PointMode, but this is not supported on this platform");
920 }
921 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200922 break;
923
924 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700925 if (num_comp_in > limits.maxGeometryInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700926 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700927 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
928 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
929 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700930 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200931 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700932 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700933 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700934 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
935 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
936 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600937 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700938 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700939 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700940 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
941 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
942 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700943 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200944 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700945 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700946 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700947 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
948 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
949 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600950 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700951 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700952 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700953 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
954 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
955 "components by %u components",
956 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700957 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500958 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200959 break;
960
961 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700962 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700963 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700964 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
965 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
966 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700967 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200968 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700969 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700970 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700971 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
972 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
973 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600974 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200975 break;
976
Jeff Bolz148d94e2018-12-13 21:25:56 -0600977 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
978 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
979 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
980 case VK_SHADER_STAGE_MISS_BIT_NV:
981 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
982 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
983 case VK_SHADER_STAGE_TASK_BIT_NV:
984 case VK_SHADER_STAGE_MESH_BIT_NV:
985 break;
986
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200987 default:
988 assert(false); // This should never happen
989 }
990 return skip;
991}
992
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300993bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const {
994 bool skip = false;
995
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300996 // Got through all ImageRead/Write instructions
997 for (auto insn : *src) {
998 switch (insn.opcode()) {
999 case spv::OpImageSparseRead:
1000 case spv::OpImageRead: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001001 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001002 if (type_def != src->end()) {
Tim Van Pattenffe91322021-07-26 10:20:50 -06001003 const auto dim = type_def.word(3);
1004 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1005 // StorageImageReadWithoutFormat Capability was declared.
1006 if (dim != spv::DimSubpassData && type_def.word(8) == spv::ImageFormatUnknown) {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001007 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1008 "shaderStorageImageReadWithoutFormat",
1009 kVUID_Features_shaderStorageImageReadWithoutFormat);
1010 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001011 }
1012 break;
1013 }
1014 case spv::OpImageWrite: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001015 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001016 if (type_def != src->end()) {
1017 if (type_def.word(8) == spv::ImageFormatUnknown) {
1018 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1019 "shaderStorageImageWriteWithoutFormat",
1020 kVUID_Features_shaderStorageImageWriteWithoutFormat);
1021 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001022 }
1023 break;
1024 }
1025
1026 }
1027 }
1028
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001029 // Go through all variables for images and check decorations
1030 for (auto insn : *src) {
1031 if (insn.opcode() != spv::OpVariable)
1032 continue;
1033
1034 uint32_t var = insn.word(2);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001035 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001036 if (type_def == src->end())
1037 continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001038 // Only check if the Image Dim operand is not SubpassData
1039 const auto dim = type_def.word(3);
1040 if (dim == spv::DimSubpassData) continue;
Corentin Wallez91f8b6d2021-07-23 10:11:31 +02001041 // Only check storage images
1042 if (type_def.word(7) != 2) continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001043 if (type_def.word(8) != spv::ImageFormatUnknown) continue;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001044
1045 decoration_set img_decorations = src->get_decorations(var);
1046
1047 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1048 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001049 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
1050 "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32
1051 " "
1052 " without format not marked a NonReadable",
1053 var);
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001054 }
1055
1056 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1057 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001058 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
1059 "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32
1060 " "
1061 "without format not marked a NonWritable",
1062 var);
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001063 }
1064 }
1065
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001066 return skip;
1067}
1068
sfricke-samsungdc96f302020-03-18 20:42:10 -07001069bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1070 bool skip = false;
1071 uint32_t total_resources = 0;
1072
1073 // Only currently testing for graphics and compute pipelines
1074 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1075 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1076 return false;
1077 }
1078
1079 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1080 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
Jeremy Gebben11af9792021-08-20 10:20:09 -06001081 total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
sfricke-samsungdc96f302020-03-18 20:42:10 -07001082 }
1083
1084 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1085 // input from CreatePipeline and CreatePipelineLayout level
1086 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1087 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1088 continue;
1089 }
1090
1091 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1092 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1093 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1094 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1095 // Check only descriptor types listed in maxPerStageResources description in spec
1096 switch (binding->descriptorType) {
1097 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1098 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1099 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1100 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1101 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1102 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1103 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1104 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1105 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1106 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1107 total_resources += binding->descriptorCount;
1108 break;
1109 default:
1110 break;
1111 }
1112 }
1113 }
1114 }
1115
1116 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1117 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1118 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001119 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001120 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1121 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1122 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1123 }
1124
1125 return skip;
1126}
1127
Jeff Bolze4356752019-03-07 11:23:46 -06001128// copy the specialization constant value into buf, if it is present
1129void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1130 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1131
1132 if (spec && spec_id < spec->mapEntryCount) {
1133 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1134 }
1135}
1136
1137// Fill in value with the constant or specialization constant value, if available.
1138// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001139static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001140 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001141 auto type_id = src->get_def(insn.word(1));
1142 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1143 return false;
1144 }
1145 switch (insn.opcode()) {
1146 case spv::OpSpecConstant:
1147 *value = insn.word(3);
1148 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1149 return true;
1150 case spv::OpConstant:
1151 *value = insn.word(3);
1152 return true;
1153 default:
1154 return false;
1155 }
1156}
1157
1158// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001159VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001160 switch (insn.opcode()) {
1161 case spv::OpTypeInt:
1162 switch (insn.word(2)) {
1163 case 8:
1164 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1165 case 16:
1166 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1167 case 32:
1168 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1169 case 64:
1170 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1171 default:
1172 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1173 }
1174 case spv::OpTypeFloat:
1175 switch (insn.word(2)) {
1176 case 16:
1177 return VK_COMPONENT_TYPE_FLOAT16_NV;
1178 case 32:
1179 return VK_COMPONENT_TYPE_FLOAT32_NV;
1180 case 64:
1181 return VK_COMPONENT_TYPE_FLOAT64_NV;
1182 default:
1183 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1184 }
1185 default:
1186 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1187 }
1188}
1189
1190// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1191// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001192bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001193 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001194 bool skip = false;
1195
1196 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001197 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001198 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001199 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001200
1201 struct CoopMatType {
1202 uint32_t scope, rows, cols;
1203 VkComponentTypeNV component_type;
1204 bool all_constant;
1205
1206 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1207
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001208 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001209 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001210 spirv_inst_iter insn = src->get_def(id);
1211 uint32_t component_type_id = insn.word(2);
1212 uint32_t scope_id = insn.word(3);
1213 uint32_t rows_id = insn.word(4);
1214 uint32_t cols_id = insn.word(5);
1215 auto component_type_iter = src->get_def(component_type_id);
1216 auto scope_iter = src->get_def(scope_id);
1217 auto rows_iter = src->get_def(rows_id);
1218 auto cols_iter = src->get_def(cols_id);
1219
1220 all_constant = true;
1221 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1222 all_constant = false;
1223 }
1224 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1225 all_constant = false;
1226 }
1227 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1228 all_constant = false;
1229 }
1230 component_type = GetComponentType(component_type_iter, src);
1231 }
1232 };
1233
1234 bool seen_coopmat_capability = false;
1235
1236 for (auto insn : *src) {
1237 // Whitelist instructions whose result can be a cooperative matrix type, and
1238 // keep track of their types. It would be nice if SPIRV-Headers generated code
1239 // to identify which instructions have a result type and result id. Lacking that,
1240 // this whitelist is based on the set of instructions that
1241 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1242 switch (insn.opcode()) {
1243 case spv::OpLoad:
1244 case spv::OpCooperativeMatrixLoadNV:
1245 case spv::OpCooperativeMatrixMulAddNV:
1246 case spv::OpSNegate:
1247 case spv::OpFNegate:
1248 case spv::OpIAdd:
1249 case spv::OpFAdd:
1250 case spv::OpISub:
1251 case spv::OpFSub:
1252 case spv::OpFDiv:
1253 case spv::OpSDiv:
1254 case spv::OpUDiv:
1255 case spv::OpMatrixTimesScalar:
1256 case spv::OpConstantComposite:
1257 case spv::OpCompositeConstruct:
1258 case spv::OpConvertFToU:
1259 case spv::OpConvertFToS:
1260 case spv::OpConvertSToF:
1261 case spv::OpConvertUToF:
1262 case spv::OpUConvert:
1263 case spv::OpSConvert:
1264 case spv::OpFConvert:
1265 id_to_type_id[insn.word(2)] = insn.word(1);
1266 break;
1267 default:
1268 break;
1269 }
1270
1271 switch (insn.opcode()) {
1272 case spv::OpDecorate:
1273 if (insn.word(2) == spv::DecorationSpecId) {
1274 id_to_spec_id[insn.word(1)] = insn.word(3);
1275 }
1276 break;
1277 case spv::OpCapability:
1278 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1279 seen_coopmat_capability = true;
1280
1281 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001282 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001283 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001284 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1285 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001286 }
1287 }
1288 break;
1289 case spv::OpMemoryModel:
1290 // If the capability isn't enabled, don't bother with the rest of this function.
1291 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1292 if (!seen_coopmat_capability) {
1293 return skip;
1294 }
1295 break;
1296 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001297 CoopMatType m;
1298 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001299
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001300 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001301 // Validate that the type parameters are all supported for one of the
1302 // operands of a cooperative matrix property.
1303 bool valid = false;
1304 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001305 if (cooperative_matrix_properties[i].AType == m.component_type &&
1306 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1307 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001308 valid = true;
1309 break;
1310 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001311 if (cooperative_matrix_properties[i].BType == m.component_type &&
1312 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1313 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001314 valid = true;
1315 break;
1316 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001317 if (cooperative_matrix_properties[i].CType == m.component_type &&
1318 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1319 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001320 valid = true;
1321 break;
1322 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001323 if (cooperative_matrix_properties[i].DType == m.component_type &&
1324 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1325 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001326 valid = true;
1327 break;
1328 }
1329 }
1330 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001331 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001332 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1333 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001334 }
1335 }
1336 break;
1337 }
1338 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001339 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001340 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1341 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1342 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1343 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001344 // Couldn't find type of matrix
1345 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001346 break;
1347 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001348 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1349 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1350 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1351 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001352
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001353 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001354 // Validate that the type parameters are all supported for the same
1355 // cooperative matrix property.
1356 bool valid = false;
1357 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001358 if (cooperative_matrix_properties[i].AType == a.component_type &&
1359 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1360 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001361
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001362 cooperative_matrix_properties[i].BType == b.component_type &&
1363 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1364 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001365
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001366 cooperative_matrix_properties[i].CType == c.component_type &&
1367 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1368 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001369
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001370 cooperative_matrix_properties[i].DType == d.component_type &&
1371 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1372 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001373 valid = true;
1374 break;
1375 }
1376 }
1377 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001378 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001379 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1380 "VkCooperativeMatrixPropertiesNV",
1381 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001382 }
1383 }
1384 break;
1385 }
1386 default:
1387 break;
1388 }
1389 }
1390
1391 return skip;
1392}
1393
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001394bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1395 const PIPELINE_STATE *pipeline) const {
1396 bool skip = false;
1397
1398 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1399 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1400 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1401 for (auto insn : *src) {
1402 switch (insn.opcode()) {
1403 case spv::OpCapability:
1404 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1405 auto subpass_flags =
1406 (pipeline->rp_state == nullptr)
1407 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001408 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001409 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1410 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001411 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001412 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1413 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1414 }
1415 }
1416 break;
1417 default:
1418 break;
1419 }
1420 }
1421 }
1422
1423 return skip;
1424}
1425
ziga-lunarg73163742021-08-25 13:15:29 +02001426bool CoreChecks::ValidateShaderSubgroupSizeControl(VkPipelineShaderStageCreateInfo const *pStage) const {
1427 bool skip = false;
1428
1429 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
1430 !enabled_features.subgroup_size_control_features.subgroupSizeControl) {
1431 skip |= LogError(
1432 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1433 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1434 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1435 }
1436
1437 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
1438 !enabled_features.subgroup_size_control_features.computeFullSubgroups) {
1439 skip |= LogError(
1440 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1441 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1442 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1443 }
1444
1445 return skip;
1446}
1447
sfricke-samsung58b84352021-07-31 21:41:04 -07001448bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *src) const {
1449 bool skip = false;
1450
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001451 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001452 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001453
sfricke-samsungf5042b12021-08-05 01:09:40 -07001454 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1455 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1456
1457 const bool valid_storage_buffer_float = (
1458 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1459 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1460 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1461 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1462 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1463 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1464 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1465 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1466 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1467
1468 const bool valid_workgroup_float = (
1469 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1470 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1471 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1472 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1473 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1474 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1475 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1476 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1477 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1478
1479 const bool valid_image_float = (
1480 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1481 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1482 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1483
1484 const bool valid_16_float = (
1485 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1486 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1487 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1488 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1489 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1490 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1491
1492 const bool valid_32_float = (
1493 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1494 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1495 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1496 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1497 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1498 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1499 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1500 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1501 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1502
1503 const bool valid_64_float = (
1504 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1505 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1506 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1507 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1508 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1509 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1510 // clang-format on
1511
sfricke-samsung58b84352021-07-31 21:41:04 -07001512 for (auto &atomic_inst : src->atomic_inst) {
1513 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungf5042b12021-08-05 01:09:40 -07001514 const uint32_t opcode = src->at(atomic_inst.first).opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001515
1516 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001517 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001518 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1519 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
1520 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001521 device, "VUID-RuntimeSpirv-None-06278",
sfricke-samsung58b84352021-07-31 21:41:04 -07001522 "%s: Can't use 64-bit int atomics operations with %s storage class without shaderBufferInt64Atomics enabled.",
1523 report_data->FormatHandle(src->vk_shader_module()).c_str(), StorageClassName(atomic.storage_class));
1524 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1525 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001526 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung58b84352021-07-31 21:41:04 -07001527 "%s: Can't use 64-bit int atomics operations with Workgroup storage class without "
1528 "shaderSharedInt64Atomics enabled.",
1529 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001530 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001531 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001532 "%s: Can't use 64-bit int atomics operations with Image storage class without "
1533 "shaderImageInt64Atomics enabled.",
1534 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001535 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001536 } else if (atomic.type == spv::OpTypeFloat) {
1537 // Validate Floats
1538 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1539 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001540 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1541 : "VUID-RuntimeSpirv-None-06280";
1542 skip |= LogError(device, vuid,
sfricke-samsungf5042b12021-08-05 01:09:40 -07001543 "%s: Can't use float atomics operations with StorageBuffer storage class without "
1544 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1545 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1546 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1547 "shaderBufferFloat64AtomicMinMax enabled.",
1548 report_data->FormatHandle(src->vk_shader_module()).c_str());
1549 } else if (opcode == spv::OpAtomicFAddEXT) {
1550 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1551 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1552 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with "
1553 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
1554 report_data->FormatHandle(src->vk_shader_module()).c_str());
1555 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1556 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1557 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with "
1558 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
1559 report_data->FormatHandle(src->vk_shader_module()).c_str());
1560 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1561 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1562 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with "
1563 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
1564 report_data->FormatHandle(src->vk_shader_module()).c_str());
1565 }
1566 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1567 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
1568 skip |= LogError(
1569 device, kVUID_Core_Shader_AtomicFeature,
1570 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1571 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
1572 report_data->FormatHandle(src->vk_shader_module()).c_str());
1573 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
1574 skip |= LogError(
1575 device, kVUID_Core_Shader_AtomicFeature,
1576 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1577 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
1578 report_data->FormatHandle(src->vk_shader_module()).c_str());
1579 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
1580 skip |= LogError(
1581 device, kVUID_Core_Shader_AtomicFeature,
1582 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1583 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
1584 report_data->FormatHandle(src->vk_shader_module()).c_str());
1585 }
1586 } else {
1587 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1588 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
1589 skip |= LogError(
1590 device, kVUID_Core_Shader_AtomicFeature,
1591 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1592 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
1593 report_data->FormatHandle(src->vk_shader_module()).c_str());
1594 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
1595 skip |= LogError(
1596 device, kVUID_Core_Shader_AtomicFeature,
1597 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1598 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
1599 report_data->FormatHandle(src->vk_shader_module()).c_str());
1600 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
1601 skip |= LogError(
1602 device, kVUID_Core_Shader_AtomicFeature,
1603 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1604 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
1605 report_data->FormatHandle(src->vk_shader_module()).c_str());
1606 }
1607 }
1608 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1609 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001610 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1611 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsungf5042b12021-08-05 01:09:40 -07001612 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001613 device, vuid,
sfricke-samsungf5042b12021-08-05 01:09:40 -07001614 "%s: Can't use float atomics operations with Workgroup storage class without shaderSharedFloat32Atomics or "
1615 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1616 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1617 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1618 report_data->FormatHandle(src->vk_shader_module()).c_str());
1619 } else if (opcode == spv::OpAtomicFAddEXT) {
1620 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1621 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1622 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1623 "storage class without shaderSharedFloat16AtomicAdd enabled.",
1624 report_data->FormatHandle(src->vk_shader_module()).c_str());
1625 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1626 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1627 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1628 "storage class without shaderSharedFloat32AtomicAdd enabled.",
1629 report_data->FormatHandle(src->vk_shader_module()).c_str());
1630 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1631 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1632 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1633 "storage class without shaderSharedFloat64AtomicAdd enabled.",
1634 report_data->FormatHandle(src->vk_shader_module()).c_str());
1635 }
1636 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1637 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
1638 skip |= LogError(
1639 device, kVUID_Core_Shader_AtomicFeature,
1640 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1641 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
1642 report_data->FormatHandle(src->vk_shader_module()).c_str());
1643 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
1644 skip |= LogError(
1645 device, kVUID_Core_Shader_AtomicFeature,
1646 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1647 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
1648 report_data->FormatHandle(src->vk_shader_module()).c_str());
1649 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
1650 skip |= LogError(
1651 device, kVUID_Core_Shader_AtomicFeature,
1652 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1653 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
1654 report_data->FormatHandle(src->vk_shader_module()).c_str());
1655 }
1656 } else {
1657 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1658 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
1659 skip |= LogError(
1660 device, kVUID_Core_Shader_AtomicFeature,
1661 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1662 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat16Atomics enabled.",
1663 report_data->FormatHandle(src->vk_shader_module()).c_str());
1664 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
1665 skip |= LogError(
1666 device, kVUID_Core_Shader_AtomicFeature,
1667 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1668 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat32Atomics enabled.",
1669 report_data->FormatHandle(src->vk_shader_module()).c_str());
1670 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
1671 skip |= LogError(
1672 device, kVUID_Core_Shader_AtomicFeature,
1673 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1674 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat64Atomics enabled.",
1675 report_data->FormatHandle(src->vk_shader_module()).c_str());
1676 }
1677 }
1678 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001679 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1680 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsungf5042b12021-08-05 01:09:40 -07001681 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001682 LogError(device, vuid,
sfricke-samsungf5042b12021-08-05 01:09:40 -07001683 "%s: Can't use float atomics operations with Image storage class without shaderImageFloat32Atomics or "
1684 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
1685 report_data->FormatHandle(src->vk_shader_module()).c_str());
1686 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001687 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsungf5042b12021-08-05 01:09:40 -07001688 "%s: Can't use 16-bit float atomics operations without shaderBufferFloat16Atomics, "
1689 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1690 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
1691 report_data->FormatHandle(src->vk_shader_module()).c_str());
1692 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001693 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1694 : "VUID-RuntimeSpirv-None-06335";
1695 skip |= LogError(device, vuid,
sfricke-samsungf5042b12021-08-05 01:09:40 -07001696 "%s: Can't use 32-bit float atomics operations without shaderBufferFloat32AtomicMinMax, "
1697 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1698 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1699 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1700 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
1701 report_data->FormatHandle(src->vk_shader_module()).c_str());
1702 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001703 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1704 : "VUID-RuntimeSpirv-None-06336";
1705 skip |= LogError(device, vuid,
sfricke-samsungf5042b12021-08-05 01:09:40 -07001706 "%s: Can't use 64-bit float atomics operations without shaderBufferFloat64AtomicMinMax, "
1707 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1708 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
1709 report_data->FormatHandle(src->vk_shader_module()).c_str());
1710 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001711 }
1712 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001713 return skip;
1714}
1715
John Zulaufac4c6e12019-07-01 16:05:58 -06001716bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001717 auto entrypoint_id = entrypoint.word(2);
1718
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001719 // The first denorm execution mode encountered, along with its bit width.
1720 // Used to check if SeparateDenormSettings is respected.
1721 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001722
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001723 // The first rounding mode encountered, along with its bit width.
1724 // Used to check if SeparateRoundingModeSettings is respected.
1725 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001726
1727 bool skip = false;
1728
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001729 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001730 uint32_t invocations = 0;
1731
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001732 auto it = src->execution_mode_inst.find(entrypoint_id);
1733 if (it != src->execution_mode_inst.end()) {
1734 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001735 auto mode = insn.word(2);
1736 switch (mode) {
1737 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1738 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001739 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001740 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001741 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
1742 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device");
1743 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1744 skip |= LogError(
1745 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
1746 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device");
1747 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1748 skip |= LogError(
1749 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
1750 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001751 }
1752 break;
1753 }
1754
1755 case spv::ExecutionModeDenormPreserve: {
1756 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001757 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1758 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
1759 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device");
1760 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1761 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
1762 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device");
1763 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1764 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
1765 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001766 }
1767
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001768 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1769 // Register the first denorm execution mode found
1770 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001771 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001772 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001773 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001774 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001775 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001776 "Shader uses different denorm execution modes for 16 and 64-bit but "
1777 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001778 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001779 }
1780 break;
1781
Mike Schuchardt2df08912020-12-15 16:28:09 -08001782 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001783 break;
1784
Mike Schuchardt2df08912020-12-15 16:28:09 -08001785 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001786 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001787 "Shader uses different denorm execution modes for different bit widths but "
1788 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001789 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001790 break;
1791
1792 default:
1793 break;
1794 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001795 }
1796 break;
1797 }
1798
1799 case spv::ExecutionModeDenormFlushToZero: {
1800 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001801 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
1802 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1803 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device");
1804 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
1805 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1806 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device");
1807 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
1808 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1809 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001810 }
1811
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001812 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1813 // Register the first denorm execution mode found
1814 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001815 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001816 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001817 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001818 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001819 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001820 "Shader uses different denorm execution modes for 16 and 64-bit but "
1821 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001822 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001823 }
1824 break;
1825
Mike Schuchardt2df08912020-12-15 16:28:09 -08001826 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001827 break;
1828
Mike Schuchardt2df08912020-12-15 16:28:09 -08001829 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001830 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001831 "Shader uses different denorm execution modes for different bit widths but "
1832 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001833 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001834 break;
1835
1836 default:
1837 break;
1838 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001839 }
1840 break;
1841 }
1842
1843 case spv::ExecutionModeRoundingModeRTE: {
1844 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001845 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1846 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
1847 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device");
1848 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1849 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
1850 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device");
1851 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1852 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
1853 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001854 }
1855
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001856 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1857 // Register the first rounding mode found
1858 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001859 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001860 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001861 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001862 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001863 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001864 "Shader uses different rounding modes for 16 and 64-bit but "
1865 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001866 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001867 }
1868 break;
1869
Mike Schuchardt2df08912020-12-15 16:28:09 -08001870 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001871 break;
1872
Mike Schuchardt2df08912020-12-15 16:28:09 -08001873 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001874 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001875 "Shader uses different rounding modes for different bit widths but "
1876 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001877 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001878 break;
1879
1880 default:
1881 break;
1882 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001883 }
1884 break;
1885 }
1886
1887 case spv::ExecutionModeRoundingModeRTZ: {
1888 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001889 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
1890 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
1891 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device");
1892 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
1893 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
1894 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device");
1895 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
1896 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
1897 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001898 }
1899
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001900 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1901 // Register the first rounding mode found
1902 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001903 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001904 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001905 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001906 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001907 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001908 "Shader uses different rounding modes for 16 and 64-bit but "
1909 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001910 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001911 }
1912 break;
1913
Mike Schuchardt2df08912020-12-15 16:28:09 -08001914 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001915 break;
1916
Mike Schuchardt2df08912020-12-15 16:28:09 -08001917 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001918 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001919 "Shader uses different rounding modes for different bit widths but "
1920 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001921 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001922 break;
1923
1924 default:
1925 break;
1926 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001927 }
1928 break;
1929 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001930
1931 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001932 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001933 break;
1934 }
1935
1936 case spv::ExecutionModeInvocations: {
1937 invocations = insn.word(3);
1938 break;
1939 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001940 }
1941 }
1942 }
1943
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001944 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001945 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001946 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
1947 "Geometry shader entry point must have an OpExecutionMode instruction that "
1948 "specifies a maximum output vertex count that is greater than 0 and less "
1949 "than or equal to maxGeometryOutputVertices. "
1950 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001951 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001952 }
1953
1954 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001955 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
1956 "Geometry shader entry point must have an OpExecutionMode instruction that "
1957 "specifies an invocation count that is greater than 0 and less "
1958 "than or equal to maxGeometryShaderInvocations. "
1959 "Invocations=%d, maxGeometryShaderInvocations=%d",
1960 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001961 }
1962 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001963 return skip;
1964}
1965
Chris Forbes47567b72017-06-09 12:09:45 -07001966// For given pipelineLayout verify that the set_layout_node at slot.first
1967// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06001968static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001969 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07001970 if (!pipelineLayout) return nullptr;
1971
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001972 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07001973
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001974 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07001975}
1976
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001977// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
1978// o If there is only a vertex shader : gl_PointSize must be written when using points
1979// o If there is a geometry or tessellation shader:
1980// - If shaderTessellationAndGeometryPointSize feature is enabled:
1981// * gl_PointSize must be written in the final geometry stage
1982// - If shaderTessellationAndGeometryPointSize feature is disabled:
1983// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001984bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06001985 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001986 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
1987 return false;
1988 }
1989
1990 bool pointsize_written = false;
1991 bool skip = false;
1992
1993 // Search for PointSize built-in decorations
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001994 for (auto set : src->builtin_decoration_list) {
1995 auto insn = src->at(set.offset);
1996 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001997 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001998 if (pointsize_written) {
1999 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002000 }
2001 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002002 }
2003
2004 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002005 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002006 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002007 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002008 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2009 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002010 }
2011 } else if (!pointsize_written) {
2012 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002013 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002014 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2015 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002016 }
2017 return skip;
2018}
John Zulauf14c355b2019-06-27 16:09:37 -06002019
Tobias Hector6663c9b2020-11-05 10:18:02 +00002020bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
2021 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2022 bool primitiverate_written = false;
2023 bool viewportindex_written = false;
2024 bool viewportmask_written = false;
2025 bool skip = false;
2026
2027 // Check if the primitive shading rate is written
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002028 for (auto set : src->builtin_decoration_list) {
2029 auto insn = src->at(set.offset);
2030 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002031 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002032 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002033 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002034 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002035 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002036 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002037 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2038 break;
2039 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002040 }
2041
Tony-LunarGd44844c2021-01-22 13:24:37 -07002042 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002043 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002044 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002045 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002046 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002047 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2048 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2049 "multiple viewports "
2050 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2051 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002052 }
2053
2054 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002055 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002056 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2057 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2058 "ViewportIndex built-ins,"
2059 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2060 string_VkShaderStageFlagBits(stage));
2061 }
2062
2063 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002064 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002065 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2066 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2067 "ViewportMaskNV built-ins,"
2068 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2069 string_VkShaderStageFlagBits(stage));
2070 }
2071 }
2072 return skip;
2073}
2074
ziga-lunarga12c75a2021-09-16 16:36:16 +02002075bool CoreChecks::ValidateTexelGatherOffset(SHADER_MODULE_STATE const *src, spirv_inst_iter &insn) const {
2076 bool skip = false;
2077
2078 const uint32_t opcode = insn.opcode();
2079 // If opcode is OpImage*Gather
2080 if (opcode == spv::OpImageGather || opcode == spv::OpImageDrefGather || opcode == spv::OpImageSparseGather ||
2081 opcode == spv::OpImageSparseDrefGather) {
2082 if (insn.len() > 6) { // Image operands are optional
2083 auto image_operand = insn.word(6);
2084 // Bits we are validating
2085 uint32_t offset_bits =
2086 spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask;
2087 if (image_operand & (offset_bits)) {
2088 // Operand values start at word 7
2089 uint32_t index = 7;
2090 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2091 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2092 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2093 if (image_operand & i) { // If the bit is set, consume operand
2094 if (insn.len() > index && (i & offset_bits)) {
2095 uint32_t constant_id = insn.word(index);
2096 const auto &constant = src->get_def(constant_id);
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002097 const bool is_dynamic_offset = constant == src->end();
2098 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002099 for (uint32_t j = 3; j < constant.len(); ++j) {
2100 uint32_t comp_id = constant.word(j);
2101 const auto &comp = src->get_def(comp_id);
sfricke-samsungef3fe742021-10-06 10:51:34 -07002102 const auto &comp_type = src->get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002103 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002104 const uint32_t offset = comp.word(3);
2105 const int32_t signed_offset = static_cast<int32_t>(offset);
2106 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2107
2108 // spec requires minTexelGatherOffset to be -8 or less so never can compare if unsigned
2109 // spec requires maxTexelGatherOffset to be 7 or greater so never can compare if signed is less
2110 // then zero
2111 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002112 skip |= LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
2113 "vkCreateShaderModule(): Shader uses OpImageGather with offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002114 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
2115 signed_offset, phys_dev_props.limits.minTexelGatherOffset);
2116 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2117 (!use_signed || (use_signed && signed_offset > 0))) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002118 skip |=
2119 LogError(device, "VUID-RuntimeSpirv-OpImage-06377",
sfricke-samsungef3fe742021-10-06 10:51:34 -07002120 "vkCreateShaderModule(): Shader uses OpImageGather with offset (%" PRIu32
ziga-lunarga12c75a2021-09-16 16:36:16 +02002121 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32 ").",
2122 offset, phys_dev_props.limits.maxTexelGatherOffset);
2123 }
2124 }
2125 }
2126 }
2127 index += src->ImageOperandsCount(i);
2128 }
2129 }
2130 }
2131 }
2132 }
2133
2134 return skip;
2135}
2136
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002137bool CoreChecks::ValidateShaderClock(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002138 bool skip = false;
2139
sfricke-samsung94167ca2021-02-26 04:14:59 -08002140 switch (insn.opcode()) {
2141 case spv::OpReadClockKHR: {
2142 auto scope_id = module->get_def(insn.word(3));
2143 auto scope_type = scope_id.word(3);
2144 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002145 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002146 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002147 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002148 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002149 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002150 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002151 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002152 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002153 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002154 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002155 }
2156 }
2157 return skip;
2158}
2159
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002160bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2161 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002162 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002163 const auto *pStage = stage_state.create_info;
2164 const auto *module = stage_state.module.get();
2165 const auto &entrypoint = stage_state.entrypoint;
John Zulauf14c355b2019-06-27 16:09:37 -06002166 // Check the module
2167 if (!module->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002168 skip |= LogError(
2169 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2170 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002171 }
2172
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002173 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2174 // specializations should be applied and validated.
2175 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2176 pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) {
2177 // Gather the specialization-constant values.
2178 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002179 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002180 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 -06002181 id_value_map.reserve(specialization_info->mapEntryCount);
2182 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2183 auto const &map_entry = specialization_info->pMapEntries[i];
sfricke-samsung033b0262021-07-09 00:53:06 -07002184 auto itr = module->spec_const_map.find(map_entry.constantID);
2185 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2186 // behavior of the pipeline."
2187 if (itr != module->spec_const_map.cend()) {
2188 // Make sure map_entry.size matches the spec constant's size
2189 uint32_t spec_const_size = decoration_set::kInvalidValue;
2190 const auto def_ins = module->get_def(itr->second);
2191 const auto type_ins = module->get_def(def_ins.word(1));
2192 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2193 switch (type_ins.opcode()) {
2194 case spv::OpTypeBool:
2195 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2196 spec_const_size = sizeof(VkBool32);
2197 break;
2198 case spv::OpTypeInt:
2199 case spv::OpTypeFloat:
2200 spec_const_size = type_ins.word(2) / 8;
2201 break;
2202 default:
2203 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2204 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2205 break;
2206 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002207
sfricke-samsung033b0262021-07-09 00:53:06 -07002208 if (map_entry.size != spec_const_size) {
2209 skip |=
2210 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002211 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2212 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2213 map_entry.constantID, i, map_entry.size,
2214 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002215 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002216 }
2217
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002218 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002219 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2220 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2221 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2222 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2223 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2224
2225 std::copy(start_in_p, end_in_p, out_p);
2226 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002227 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002228 }
2229
2230 // Apply the specialization-constant values and revalidate the shader module.
sfricke-samsung45996a42021-09-16 13:45:27 -07002231 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002232 spvtools::Optimizer optimizer(spirv_environment);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002233 spvtools::MessageConsumer consumer = [&skip, &module, &stage_state, this](spv_message_level_t level, const char *source,
2234 const spv_position_t &position,
2235 const char *message) {
2236 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2237 "%s does not contain valid spirv for stage %s. %s",
2238 report_data->FormatHandle(module->vk_shader_module()).c_str(),
2239 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002240 };
2241 optimizer.SetMessageConsumer(consumer);
2242 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2243 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2244 std::vector<uint32_t> specialized_spirv;
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002245 auto const optimized = optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002246 assert(optimized == true);
2247
2248 if (optimized) {
2249 spv_context ctx = spvContextCreate(spirv_environment);
2250 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2251 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002252 spvtools::ValidatorOptions options;
2253 AdjustValidatorOptions(device_extensions, enabled_features, options);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002254 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2255 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002256 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002257 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002258 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002259 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002260 }
2261
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002262 spvDiagnosticDestroy(diag);
2263 spvContextDestroy(ctx);
2264 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002265
2266 skip |= ValidateWorkgroupSize(module, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002267 }
2268
John Zulauf14c355b2019-06-27 16:09:37 -06002269 // Check the entrypoint
2270 if (entrypoint == module->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002271 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2272 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002273 }
2274 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2275
2276 // Mark accessible ids
2277 auto &accessible_ids = stage_state.accessible_ids;
2278
Chris Forbes47567b72017-06-09 12:09:45 -07002279 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002280
sfricke-samsung94167ca2021-02-26 04:14:59 -08002281 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2282 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002283 uint32_t total_shared_size = 0;
sfricke-samsung94167ca2021-02-26 04:14:59 -08002284 for (auto insn : *module) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002285 skip |= ValidateTexelGatherOffset(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002286 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002287 skip |= ValidateShaderClock(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002288 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02002289 total_shared_size += module->CalcComputeSharedMemory(pStage->stage, insn);
2290 }
2291
2292 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2293 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002294 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002295 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002296 }
2297
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002298 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2299 stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002300 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03002301 skip |= ValidateShaderStorageImageFormats(module);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002302 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsung58b84352021-07-31 21:41:04 -07002303 skip |= ValidateAtomicsTypes(module);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002304 skip |= ValidateExecutionModes(module, entrypoint);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002305 skip |= ValidateSpecializations(pStage);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002306 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002307 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002308 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07002309 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002310 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
2311 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
2312 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002313 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
2314 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
2315 }
sfricke-samsung45996a42021-09-16 13:45:27 -07002316 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002317 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
2318 }
ziga-lunarg73163742021-08-25 13:15:29 +02002319 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
2320 skip |= ValidateShaderSubgroupSizeControl(pStage);
2321 }
Chris Forbes47567b72017-06-09 12:09:45 -07002322
sfricke-samsung7699b912021-04-12 23:01:51 -07002323 // "layout must be consistent with the layout of the * shader"
2324 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002325 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002326 switch (pipeline->create_info.graphics.sType) {
2327 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2328 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2329 break;
2330 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2331 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2332 break;
2333 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2334 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2335 break;
2336 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2337 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2338 break;
2339 default:
2340 assert(false);
2341 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002342 }
2343
sfricke-samsung7699b912021-04-12 23:01:51 -07002344 // Validate Push Constants use
2345 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
2346
Chris Forbes47567b72017-06-09 12:09:45 -07002347 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002348 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002349 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002350 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002351 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002352 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2353 std::set<uint32_t> descriptor_types =
2354 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002355
2356 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002357 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002358 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002359 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002360 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002361 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002362 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2363 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002364 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2365 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002366 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002367 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2368 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002369 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002370 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002371 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002372 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002373 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002374 }
2375 }
2376
2377 // Validate use of input attachments against subpass structure
2378 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002379 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002380
Petr Krause91f7a12017-12-14 20:57:36 +01002381 auto rpci = pipeline->rp_state->createInfo.ptr();
Jeremy Gebben11af9792021-08-20 10:20:09 -06002382 auto subpass = pipeline->create_info.graphics.subpass;
Chris Forbes47567b72017-06-09 12:09:45 -07002383
2384 for (auto use : input_attachment_uses) {
2385 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2386 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07002387 ? input_attachments[use.first].attachment
2388 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002389
2390 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002391 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2392 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsung962cad92021-04-13 00:46:29 -07002393 } else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002394 skip |=
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002395 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2396 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
sfricke-samsung962cad92021-04-13 00:46:29 -07002397 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002398 }
2399 }
2400 }
Lockeaa8fdc02019-04-02 11:59:20 -06002401 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02002402 skip |= ValidateComputeWorkGroupSizes(module, entrypoint, stage_state);
Lockeaa8fdc02019-04-02 11:59:20 -06002403 }
ziga-lunarg73163742021-08-25 13:15:29 +02002404
Chris Forbes47567b72017-06-09 12:09:45 -07002405 return skip;
2406}
2407
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002408bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2409 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2410 spirv_inst_iter consumer_entrypoint,
2411 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002412 bool skip = false;
2413
2414 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002415 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2416 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002417
2418 auto a_it = outputs.begin();
2419 auto b_it = inputs.begin();
2420
ziga-lunarg8346fe82021-08-22 17:30:50 +02002421 uint32_t a_component = 0;
2422 uint32_t b_component = 0;
2423
Chris Forbes47567b72017-06-09 12:09:45 -07002424 // Maps sorted by key (location); walk them together to find mismatches
2425 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2426 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2427 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2428 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2429 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2430
ziga-lunarg8346fe82021-08-22 17:30:50 +02002431 a_first.second += a_component;
2432 b_first.second += b_component;
2433
2434 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2435 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2436 assert(a_at_end || a_component < a_length);
2437 assert(b_at_end || b_component < b_length);
2438
Chris Forbes47567b72017-06-09 12:09:45 -07002439 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002440 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002441 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2442 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2443 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2444 a_it++;
2445 a_component = 0;
2446 } else {
2447 a_component++;
2448 }
Chris Forbes47567b72017-06-09 12:09:45 -07002449 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002450 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002451 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2452 b_first.first, b_first.second, producer_stage->name);
2453 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2454 b_it++;
2455 b_component = 0;
2456 } else {
2457 b_component++;
2458 }
Chris Forbes47567b72017-06-09 12:09:45 -07002459 } else {
2460 // subtleties of arrayed interfaces:
2461 // - if is_patch, then the member is not arrayed, even though the interface may be.
2462 // - if is_block_member, then the extra array level of an arrayed interface is not
2463 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002464 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002465 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002466 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002467 producer->DescribeType(a_it->second.type_id).c_str(),
2468 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002469 a_it++;
2470 b_it++;
2471 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002472 }
2473 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002474 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002475 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2476 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2477 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002478 }
2479 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002480 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002481 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2482 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002483 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002484 uint32_t a_remaining = a_length - a_component;
2485 uint32_t b_remaining = b_length - b_component;
2486 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2487 a_it++;
2488 b_it++;
2489 a_component = 0;
2490 b_component = 0;
2491 } else if (a_remaining > b_remaining) { // a has more components remaining
2492 a_component += b_remaining;
2493 b_component = 0;
2494 b_it++;
2495 } else if (b_remaining > a_remaining) { // b has more components remaining
2496 b_component += a_remaining;
2497 a_component = 0;
2498 a_it++;
2499 }
Chris Forbes47567b72017-06-09 12:09:45 -07002500 }
2501 }
2502
Ari Suonpaa696b3432019-03-11 14:02:57 +02002503 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002504 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2505 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002506
2507 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2508 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002509 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002510 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002511 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2512 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002513 } else {
2514 auto it_producer = builtins_producer.begin();
2515 auto it_consumer = builtins_consumer.begin();
2516 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2517 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002518 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002519 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2520 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002521 break;
2522 }
2523 it_producer++;
2524 it_consumer++;
2525 }
2526 }
2527 }
2528 }
2529
Chris Forbes47567b72017-06-09 12:09:45 -07002530 return skip;
2531}
2532
John Zulauf14c355b2019-06-27 16:09:37 -06002533static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002534 uint32_t stage_mask = 0;
2535 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2536 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2537 stage_mask |= pCreateInfo->pStages[i].stage;
2538 }
2539 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002540 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2541 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2542 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002543 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2544 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2545 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2546 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2547 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002548 }
2549 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002550 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002551}
2552
Chris Forbes47567b72017-06-09 12:09:45 -07002553// Validate that the shaders used by the given pipeline and store the active_slots
2554// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002555bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002556 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002557
Chris Forbes47567b72017-06-09 12:09:45 -07002558 bool skip = false;
2559
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002560 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002561
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002562 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
2563 for (auto &stage : pipeline->stage_state) {
2564 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
2565 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
2566 vertex_stage = &stage;
2567 }
2568 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
2569 fragment_stage = &stage;
2570 }
Chris Forbes47567b72017-06-09 12:09:45 -07002571 }
2572
2573 // if the shader stages are no good individually, cross-stage validation is pointless.
2574 if (skip) return true;
2575
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002576 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002577
2578 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002579 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002580 }
2581
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002582 if (vertex_stage && vertex_stage->module->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
2583 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07002584 }
2585
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002586 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
2587 const auto &producer = pipeline->stage_state[i - 1];
2588 const auto &consumer = pipeline->stage_state[i];
2589 assert(producer.module);
2590 if (&producer == fragment_stage) {
2591 break;
2592 }
2593 if (consumer.module) {
2594 if (consumer.module->has_valid_spirv && producer.module->has_valid_spirv) {
2595 auto producer_id = GetShaderStageId(producer.stage_flag);
2596 auto consumer_id = GetShaderStageId(consumer.stage_flag);
2597 skip |=
2598 ValidateInterfaceBetweenStages(producer.module.get(), producer.entrypoint, &shader_stage_attribs[producer_id],
2599 consumer.module.get(), consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002600 }
Chris Forbes47567b72017-06-09 12:09:45 -07002601
Chris Forbes47567b72017-06-09 12:09:45 -07002602 }
2603 }
2604
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002605 if (fragment_stage && fragment_stage->module->has_valid_spirv) {
2606 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002607 create_info->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07002608 }
2609
2610 return skip;
2611}
2612
Tony-LunarGb2ded512021-02-02 16:03:30 -07002613void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002614 if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports ||
2615 !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) {
2616 return;
2617 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002618
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002619 for (auto &stage : pipeline_state->stage_state) {
2620 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2621 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002622 bool primitiverate_written = false;
Tobias Hector6663c9b2020-11-05 10:18:02 +00002623
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002624 for (auto set : stage.module->builtin_decoration_list) {
2625 auto insn = stage.module->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002626 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002627 primitiverate_written = stage.module->IsBuiltInWritten(insn, stage.entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002628 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002629 if (primitiverate_written) {
2630 break;
2631 }
Tony-LunarGb2ded512021-02-02 16:03:30 -07002632 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002633
Tony-LunarGb2ded512021-02-02 16:03:30 -07002634 if (primitiverate_written) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002635 pipeline_state->wrote_primitive_shading_rate.insert(stage.stage_flag);
Tony-LunarGb2ded512021-02-02 16:03:30 -07002636 }
2637 }
2638 }
2639}
2640
2641bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2642 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002643 bool skip = false;
2644
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002645 for (auto &stage : pipeline->stage_state) {
2646 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2647 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002648 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2649 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002650 if (pipeline->wrote_primitive_shading_rate.find(stage.stage_flag) != pipeline->wrote_primitive_shading_rate.end()) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002651 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002652 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002653 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2654 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2655 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002656 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002657 }
2658 }
2659 }
2660 }
2661
2662 return skip;
2663}
2664
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002665bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002666 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07002667}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002668
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002669uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2670 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002671 const auto &create_info = pipeline->create_info.raytracing;
2672 const auto *stages = create_info.ptr()->pStages;
2673 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002674 if (stages[stage_index].stage == stageBit) {
2675 total++;
2676 }
2677 }
2678
Jeremy Gebben11af9792021-08-20 10:20:09 -06002679 if (create_info.pLibraryInfo) {
2680 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2681 const PIPELINE_STATE *library_pipeline = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002682 total += CalcShaderStageCount(library_pipeline, stageBit);
2683 }
2684 }
2685
2686 return total;
2687}
2688
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002689bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE *pipeline, uint32_t group, uint32_t stage) const {
2690 if (group == VK_SHADER_UNUSED_NV) {
2691 return true;
2692 }
2693
2694 const auto &create_info = pipeline->create_info.raytracing;
2695 const auto *stages = create_info.ptr()->pStages;
2696
2697 if (group < create_info.stageCount) {
2698 return (stages[group].stage & stage) != 0;
2699 }
2700 group -= create_info.stageCount;
2701
2702 // Search libraries
2703 if (create_info.pLibraryInfo) {
2704 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2705 const PIPELINE_STATE *library_pipeline = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
2706 const uint32_t stage_count = library_pipeline->create_info.raytracing.ptr()->stageCount;
2707 if (group < stage_count) {
2708 return (library_pipeline->create_info.raytracing.ptr()->pStages[group].stage & stage) != 0;
2709 }
2710 group -= stage_count;
2711 }
2712 }
2713
2714 // group index too large
2715 return false;
2716}
2717
sourav parmarcd5fb182020-07-17 12:58:44 -07002718bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06002719 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04002720
Jeremy Gebben11af9792021-08-20 10:20:09 -06002721 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002722 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002723 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
2724 skip |=
2725 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
2726 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
2727 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
2728 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002729 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002730 if (create_info.pLibraryInfo) {
2731 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2732 const PIPELINE_STATE *library_pipelinestate = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
2733 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
2734 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002735 skip |= LogError(
2736 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
2737 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
2738 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002739 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07002740 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002741 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
2742 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
2743 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
2744 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002745 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
2746 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
2747 "member must have been created with values of the maxPipelineRayPayloadSize and "
2748 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
2749 }
2750 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002751 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002752 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
2753 "vkCreateRayTracingPipelinesKHR: If flags includes "
2754 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
2755 "the pLibraries member of libraries must have been created with the "
2756 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
2757 }
sourav parmar83c31b12020-05-06 12:30:54 -07002758 }
2759 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002760 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002761 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002762 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
2763 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
2764 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002765 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002766 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002767 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002768 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04002769
Jeremy Gebben11af9792021-08-20 10:20:09 -06002770 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002771 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04002772 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002773
Jeremy Gebben11af9792021-08-20 10:20:09 -06002774 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002775 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
2776 if (raygen_stages_count == 0) {
2777 skip |= LogError(
2778 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07002779 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002780 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
2781 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002782 }
2783
Jeremy Gebben11af9792021-08-20 10:20:09 -06002784 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04002785 const auto &group = groups[group_index];
2786
2787 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002788 if (!GroupHasValidIndex(
2789 pipeline, group.generalShader,
2790 VK_SHADER_STAGE_RAYGEN_BIT_NV | VK_SHADER_STAGE_MISS_BIT_NV | VK_SHADER_STAGE_CALLABLE_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002791 skip |= LogError(device,
2792 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
2793 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
2794 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002795 }
2796 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
2797 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002798 skip |= LogError(device,
2799 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
2800 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
2801 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002802 }
2803 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002804 if (!GroupHasValidIndex(pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002805 skip |= LogError(device,
2806 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
2807 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
2808 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002809 }
2810 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2811 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002812 skip |= LogError(device,
2813 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
2814 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
2815 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002816 }
2817 }
2818
2819 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
2820 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002821 if (!GroupHasValidIndex(pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002822 skip |= LogError(device,
2823 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
2824 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
2825 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002826 }
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002827 if (!GroupHasValidIndex(pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002828 skip |= LogError(device,
2829 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
2830 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
2831 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002832 }
2833 }
John Zulaufe4474e72019-07-01 17:28:27 -06002834 }
2835 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002836}
2837
Dave Houltona9df0ce2018-02-07 10:51:23 -07002838uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07002839
Dave Houltona9df0ce2018-02-07 10:51:23 -07002840static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002841 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06002842 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06002843 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002844 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002845 return nullptr;
2846}
2847
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002848bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002849 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002850 bool skip = false;
2851 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002852
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06002853 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002854 return false;
2855 }
2856
sfricke-samsung45996a42021-09-16 13:45:27 -07002857 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002858
2859 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002860 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
2861 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
2862 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002863 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07002864 auto cache = GetValidationCacheInfo(pCreateInfo);
2865 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07002866 // If app isn't using a shader validation cache, use the default one from CoreChecks
2867 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002868 if (cache) {
2869 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002870 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07002871 }
2872
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002873 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
2874 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07002875 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06002876 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002877 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07002878 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002879 spvtools::ValidatorOptions options;
2880 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06002881 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002882 if (spv_valid != SPV_SUCCESS) {
2883 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002884 if (spv_valid == SPV_WARNING) {
2885 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2886 diag && diag->error ? diag->error : "(no error text)");
2887 } else {
2888 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2889 diag && diag->error ? diag->error : "(no error text)");
2890 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002891 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002892 } else {
2893 if (cache) {
2894 cache->Insert(hash);
2895 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002896 }
2897
2898 spvDiagnosticDestroy(diag);
2899 spvContextDestroy(ctx);
2900 }
2901
Chris Forbes4ae55b32017-06-09 14:42:56 -07002902 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07002903}
2904
ziga-lunarg11fecb92021-09-20 16:48:06 +02002905bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint,
2906 const PipelineStageState &stage_state) const {
Lockeaa8fdc02019-04-02 11:59:20 -06002907 bool skip = false;
2908 uint32_t local_size_x = 0;
2909 uint32_t local_size_y = 0;
2910 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07002911 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06002912 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07002913 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002914 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002915 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002916 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06002917 }
2918 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07002919 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002920 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002921 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002922 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06002923 }
2924 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07002925 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002926 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002927 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002928 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06002929 }
2930
2931 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
2932 uint64_t invocations = local_size_x * local_size_y;
2933 // Prevent overflow.
2934 bool fail = false;
2935 if (invocations > UINT32_MAX || invocations > limit) {
2936 fail = true;
2937 }
2938 if (!fail) {
2939 invocations *= local_size_z;
2940 if (invocations > UINT32_MAX || invocations > limit) {
2941 fail = true;
2942 }
2943 }
2944 if (fail) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07002945 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002946 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
2947 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002948 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, local_size_y,
2949 local_size_z, limit);
Lockeaa8fdc02019-04-02 11:59:20 -06002950 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02002951
2952 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
2953 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
2954 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
2955 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
2956 skip |= LogError(
2957 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
2958 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
2959 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
2960 "dimension (%" PRIu32
2961 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
2962 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
2963 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
2964 }
2965 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
2966 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
2967 const auto *required_subgroup_size_features =
2968 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
2969 if (!required_subgroup_size_features) {
2970 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
2971 skip |= LogError(
2972 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
2973 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
2974 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
2975 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
2976 ").",
2977 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
2978 phys_dev_props_core11.subgroupSize);
2979 }
2980 }
2981 }
Lockeaa8fdc02019-04-02 11:59:20 -06002982 }
2983 return skip;
2984}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002985
2986spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
2987 if (api_version >= VK_API_VERSION_1_2) {
2988 return SPV_ENV_VULKAN_1_2;
2989 } else if (api_version >= VK_API_VERSION_1_1) {
2990 if (spirv_1_4) {
2991 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
2992 } else {
2993 return SPV_ENV_VULKAN_1_1;
2994 }
2995 }
2996 return SPV_ENV_VULKAN_1_0;
2997}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002998
sfricke-samsungecc112a2021-09-03 05:32:17 -07002999// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003000void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003001 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003002 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3003 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003004 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003005 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003006 options.SetRelaxBlockLayout(true);
3007 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003008
3009 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3010 // Vulkan version used, the feature bit is needed (also described in the spec).
3011
3012 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3013 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003014 options.SetUniformBufferStandardLayout(true);
3015 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003016 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3017 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003018 options.SetScalarBlockLayout(true);
3019 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003020 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3021 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003022 options.SetWorkgroupScalarBlockLayout(true);
3023 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003024}