blob: e14a41c098fa046e41fe6fd4ab5e37b5fe70b2f1 [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"
sfricke-samsung3c5dee22021-10-14 09:58:14 -070039#include "spirv_grammar_helper.h"
Petr Kraus25810d02019-08-27 17:41:15 +020040
Chris Forbes9a61e082017-07-24 15:35:29 -070041#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070042
Chris Forbes47567b72017-06-09 12:09:45 -070043static shader_stage_attributes shader_stage_attribs[] = {
Ari Suonpaa696b3432019-03-11 14:02:57 +020044 {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT},
45 {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT},
46 {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT},
47 {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT},
48 {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT},
Chris Forbes47567b72017-06-09 12:09:45 -070049};
50
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020051static const spirv_inst_iter GetBaseTypeIter(SHADER_MODULE_STATE const *src, uint32_t type) {
52 const auto &insn = src->get_def(type);
53 const uint32_t base_insn_id = src->GetBaseType(insn);
54 return src->get_def(base_insn_id);
55}
56
ziga-lunarg8346fe82021-08-22 17:30:50 +020057static bool BaseTypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, const spirv_inst_iter &a_base_insn,
58 const spirv_inst_iter &b_base_insn) {
59 const uint32_t a_opcode = a_base_insn.opcode();
60 const uint32_t b_opcode = b_base_insn.opcode();
61 if (a_opcode == b_opcode) {
62 if (a_opcode == spv::OpTypeInt) {
63 // Match width and signedness
64 return a_base_insn.word(2) == b_base_insn.word(2) && a_base_insn.word(3) == b_base_insn.word(3);
65 } else if (a_opcode == spv::OpTypeFloat) {
66 // Match width
67 return a_base_insn.word(2) == b_base_insn.word(2);
68 } else if (a_opcode == spv::OpTypeStruct) {
69 // Match on all element types
70 if (a_base_insn.len() != b_base_insn.len()) {
71 return false; // Structs cannot match if member counts differ
72 }
73
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020074 for (uint32_t i = 2; i < a_base_insn.len(); i++) {
75 const auto &c_base_insn = GetBaseTypeIter(a, a_base_insn.word(i));
76 const auto &d_base_insn = GetBaseTypeIter(b, b_base_insn.word(i));
77 if (!BaseTypesMatch(a, b, c_base_insn, d_base_insn)) {
ziga-lunarg8346fe82021-08-22 17:30:50 +020078 return false;
79 }
80 }
81
82 return true;
83 }
84 }
85 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070086}
87
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020088static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, uint32_t a_type, uint32_t b_type) {
89 const auto &a_base_insn = GetBaseTypeIter(a, a_type);
90 const auto &b_base_insn = GetBaseTypeIter(b, b_type);
Chris Forbes47567b72017-06-09 12:09:45 -070091
ziga-lunarg8346fe82021-08-22 17:30:50 +020092 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070093}
94
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060095static unsigned GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -070096 switch (format) {
97 case VK_FORMAT_R64G64B64A64_SFLOAT:
98 case VK_FORMAT_R64G64B64A64_SINT:
99 case VK_FORMAT_R64G64B64A64_UINT:
100 case VK_FORMAT_R64G64B64_SFLOAT:
101 case VK_FORMAT_R64G64B64_SINT:
102 case VK_FORMAT_R64G64B64_UINT:
103 return 2;
104 default:
105 return 1;
106 }
107}
108
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600109static unsigned GetFormatType(VkFormat fmt) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700110 if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT;
111 if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT;
sfricke-samsunged028b02021-09-06 23:14:51 -0700112 // Formats such as VK_FORMAT_D16_UNORM_S8_UINT are both
Dave Houltona9df0ce2018-02-07 10:51:23 -0700113 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
114 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700115 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
116 return FORMAT_TYPE_FLOAT;
117}
118
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600119static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700120 uint32_t bit_pos = uint32_t(u_ffs(stage));
121 return bit_pos - 1;
122}
123
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700124bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700125 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
126 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700127 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700128 bool skip = false;
129
130 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
131 auto desc = &vi->pVertexBindingDescriptions[i];
132 auto &binding = bindings[desc->binding];
133 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600134 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700135 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
136 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700137 } else {
138 binding = desc;
139 }
140 }
141
142 return skip;
143}
144
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700145bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs,
146 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700147 bool skip = false;
148
sfricke-samsung962cad92021-04-13 00:46:29 -0700149 const auto inputs = vs->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700150
151 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200152 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700153 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200154 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
155 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
156 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700157 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
158 }
159 }
160 }
161
Petr Kraus25810d02019-08-27 17:41:15 +0200162 struct AttribInputPair {
163 const VkVertexInputAttributeDescription *attrib = nullptr;
164 const interface_var *input = nullptr;
165 };
166 std::map<uint32_t, AttribInputPair> location_map;
167 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
168 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700169
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400170 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200171 const auto location = location_it.first;
172 const auto attrib = location_it.second.attrib;
173 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600174
Petr Kraus25810d02019-08-27 17:41:15 +0200175 if (attrib && !input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600176 skip |= LogPerformanceWarning(vs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700177 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200178 } else if (!attrib && input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600179 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700180 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200181 } else if (attrib && input) {
182 const auto attrib_type = GetFormatType(attrib->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700183 const auto input_type = vs->GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700184
185 // Type checking
186 if (!(attrib_type & input_type)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600187 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700188 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sfricke-samsung962cad92021-04-13 00:46:29 -0700189 string_VkFormat(attrib->format), location, vs->DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700190 }
Petr Kraus25810d02019-08-27 17:41:15 +0200191 } else { // !attrib && !input
192 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700193 }
194 }
195
196 return skip;
197}
198
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700199bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint,
200 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200201 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700202
Petr Kraus25810d02019-08-27 17:41:15 +0200203 const auto rpci = pipeline->rp_state->createInfo.ptr();
204
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600205 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800206 const VkAttachmentReference2 *reference = nullptr;
207 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600208 const interface_var *output = nullptr;
209 };
210 std::map<uint32_t, Attachment> location_map;
211
Petr Kraus25810d02019-08-27 17:41:15 +0200212 const auto subpass = rpci->pSubpasses[subpass_index];
213 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600214 auto const &reference = subpass.pColorAttachments[i];
215 location_map[i].reference = &reference;
216 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
217 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
218 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
Chris Forbes47567b72017-06-09 12:09:45 -0700219 }
220 }
221
Chris Forbes47567b72017-06-09 12:09:45 -0700222 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
223
sfricke-samsung962cad92021-04-13 00:46:29 -0700224 const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600225 for (const auto &output_it : outputs) {
226 auto const location = output_it.first.first;
227 location_map[location].output = &output_it.second;
228 }
Chris Forbes47567b72017-06-09 12:09:45 -0700229
Jeremy Gebben11af9792021-08-20 10:20:09 -0600230 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
231 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700232
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400233 for (const auto &location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600234 const auto reference = location_it.second.reference;
235 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
236 continue;
237 }
238
Petr Kraus25810d02019-08-27 17:41:15 +0200239 const auto location = location_it.first;
240 const auto attachment = location_it.second.attachment;
241 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +0200242 if (attachment && !output) {
243 if (pipeline->attachments[location].colorWriteMask != 0) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600244 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700245 "Attachment %" PRIu32
246 " not written by fragment shader; undefined values will be written to attachment",
247 location);
Petr Kraus25810d02019-08-27 17:41:15 +0200248 }
249 } else if (!attachment && output) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700250 if (!(alpha_to_coverage_enabled && location == 0)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600251 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700252 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200253 }
Petr Kraus25810d02019-08-27 17:41:15 +0200254 } else if (attachment && output) {
255 const auto attachment_type = GetFormatType(attachment->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700256 const auto output_type = fs->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700257
258 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +0200259 if (!(output_type & attachment_type)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700260 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600261 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700262 "Attachment %" PRIu32
263 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sfricke-samsung962cad92021-04-13 00:46:29 -0700264 location, string_VkFormat(attachment->format), fs->DescribeType(output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700265 }
Petr Kraus25810d02019-08-27 17:41:15 +0200266 } else { // !attachment && !output
267 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700268 }
269 }
270
Petr Kraus25810d02019-08-27 17:41:15 +0200271 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700272 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
sfricke-samsung962cad92021-04-13 00:46:29 -0700273 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700274 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600275 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700276 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200277 }
278
Chris Forbes47567b72017-06-09 12:09:45 -0700279 return skip;
280}
281
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600282PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
283 const shader_struct_member &push_constant_used_in_shader,
284 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600285 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600286 const auto used_bytes_size = used_bytes->size();
287 if (used_bytes_size == 0) return PC_Byte_Updated;
288
289 const auto push_constant_data_update_size = push_constant_data_update.size();
290 const auto *data = push_constant_data_update.data();
291 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
292 if (used_bytes_size <= push_constant_data_update_size) {
293 return PC_Byte_Updated;
294 }
295 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
296
297 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
298 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
299 return PC_Byte_Updated;
300 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600301 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600302
locke-lunargde3f0fa2020-09-10 11:55:31 -0600303 uint32_t i = 0;
304 for (const auto used : *used_bytes) {
305 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600306 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600307 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600308 return PC_Byte_Not_Set;
309 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600310 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600311 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600312 }
313 }
314 ++i;
315 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600316 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600317}
318
319bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src,
sfricke-samsung7699b912021-04-12 23:01:51 -0700320 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700321 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700322 // Temp workaround to prevent false positive errors
323 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600324 if (src->HasMultipleEntryPoints()) {
sfricke-samsung5c65b372021-03-25 05:39:57 -0700325 return skip;
326 }
327
Chris Forbes47567b72017-06-09 12:09:45 -0700328 // 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 -0700329 const auto *entrypoint = src->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600330 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
331 return skip;
332 }
333 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700334
locke-lunargde3f0fa2020-09-10 11:55:31 -0600335 bool found_stage = false;
336 for (auto const &range : *push_constant_ranges) {
337 if (range.stageFlags & pStage->stage) {
338 found_stage = true;
339 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600340 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600341 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600342 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600343 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600344 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600345 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600346 const auto ret =
347 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700348
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600349 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600350 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600351 LogObjectList objlist(src->vk_shader_module());
352 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700353 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 -0600354 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600355 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600356 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700357 }
358 }
359 }
360
locke-lunargde3f0fa2020-09-10 11:55:31 -0600361 if (!found_stage) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600362 LogObjectList objlist(src->vk_shader_module());
363 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700364 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 -0600365 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module()).c_str(),
366 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700367 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700368 }
Chris Forbes47567b72017-06-09 12:09:45 -0700369 return skip;
370}
371
sfricke-samsungcfb44592021-07-25 00:36:28 -0700372bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700373 bool skip = false;
374
375 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700376 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700377 return skip;
378 }
379
sfricke-samsungcfb44592021-07-25 00:36:28 -0700380 // Find all builtin from just the interface variables
381 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700382 auto insn = src->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700383 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700384 const decoration_set decorations = src->get_decorations(insn.word(2));
385
sfricke-samsungcfb44592021-07-25 00:36:28 -0700386 // Currently don't need to search in structs
387 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700388 auto type_pointer = src->get_def(insn.word(1));
389 assert(type_pointer.opcode() == spv::OpTypePointer);
390
391 auto type = src->get_def(type_pointer.word(3));
392 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700393 uint32_t length = static_cast<uint32_t>(src->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700394 // Handles both the input and output sampleMask
395 if (length > phys_dev_props.limits.maxSampleMaskWords) {
396 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
397 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
398 "maxSampleMaskWords of %u in %s.",
399 length, phys_dev_props.limits.maxSampleMaskWords,
400 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700401 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700402 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700403 }
404 }
405 }
406
407 return skip;
408}
409
Chris Forbes47567b72017-06-09 12:09:45 -0700410// Validate that data for each specialization entry is fully contained within the buffer.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200411bool CoreChecks::ValidateSpecializations(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700412 bool skip = false;
413
414 VkSpecializationInfo const *spec = info->pSpecializationInfo;
415
416 if (spec) {
417 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600418 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700419 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
420 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200421 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700422 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
423 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600424
425 continue;
426 }
Chris Forbes47567b72017-06-09 12:09:45 -0700427 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700428 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
429 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200430 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700431 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
432 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700433 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200434 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
435 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
436 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
437 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
438 j, spec->pMapEntries[i].constantID);
439 }
440 }
Chris Forbes47567b72017-06-09 12:09:45 -0700441 }
442 }
443
444 return skip;
445}
446
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500447// TODO (jbolz): Can this return a const reference?
sourav parmarcd5fb182020-07-17 12:58:44 -0700448static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count,
449 bool is_khr) {
Chris Forbes47567b72017-06-09 12:09:45 -0700450 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800451 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700452 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500453 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700454
455 // 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 -0500456 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
457 if (type.opcode() == spv::OpTypeRuntimeArray) {
458 descriptor_count = 0;
459 type = module->get_def(type.word(2));
460 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700461 descriptor_count *= module->GetConstantValueById(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700462 type = module->get_def(type.word(2));
463 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800464 if (type.word(2) == spv::StorageClassStorageBuffer) {
465 is_storage_buffer = true;
466 }
Chris Forbes47567b72017-06-09 12:09:45 -0700467 type = module->get_def(type.word(3));
468 }
469 }
470
471 switch (type.opcode()) {
472 case spv::OpTypeStruct: {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600473 for (const auto insn : module->GetDecorationInstructions()) {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800474 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700475 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800476 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500477 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
478 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
479 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800480 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500481 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
482 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
483 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
484 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800485 }
Chris Forbes47567b72017-06-09 12:09:45 -0700486 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500487 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
488 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
489 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700490 }
491 }
492 }
493
494 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500495 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700496 }
497
498 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500499 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
500 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
501 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700502
Chris Forbes73c00bf2018-06-22 16:28:06 -0700503 case spv::OpTypeSampledImage: {
504 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
505 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
506 auto image_type = module->get_def(type.word(2));
507 auto dim = image_type.word(3);
508 auto sampled = image_type.word(7);
509 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500510 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
511 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700512 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700513 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500514 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
515 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700516
517 case spv::OpTypeImage: {
518 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
519 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
520 auto dim = type.word(3);
521 auto sampled = type.word(7);
522
523 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500524 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
525 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700526 } else if (dim == spv::DimBuffer) {
527 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500528 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
529 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700530 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500531 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
532 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700533 }
534 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500535 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
536 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
537 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700538 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500539 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
540 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700541 }
542 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600543 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700544 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
545 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500546 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700547
548 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
549 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500550 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700551 }
552}
553
Jeff Bolze54ae892018-09-08 12:16:29 -0500554static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700555 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500556 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
557 if (ss.tellp()) ss << ", ";
558 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700559 }
560 return ss.str();
561}
562
sfricke-samsung0065ce02020-12-03 22:46:37 -0800563bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500564 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800565 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 -0500566 return true;
567 }
568 }
569
570 return false;
571}
572
sfricke-samsung0065ce02020-12-03 22:46:37 -0800573bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700574 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800575 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700576 return true;
577 }
578 }
579
580 return false;
581}
582
locke-lunarg63e4daf2020-08-17 17:53:25 -0600583bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
584 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500585 bool skip = false;
586
locke-lunarg63e4daf2020-08-17 17:53:25 -0600587 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800588 switch (stage) {
589 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -0600590 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
591 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
592 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
593 case VK_SHADER_STAGE_MISS_BIT_NV:
594 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
595 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
596 case VK_SHADER_STAGE_TASK_BIT_NV:
597 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -0800598 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -0600599 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -0800600 break;
601 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800602 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700603 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800604 break;
605 default:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800606 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700607 "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800608 break;
609 }
610 }
611
Chris Forbes47567b72017-06-09 12:09:45 -0700612 return skip;
613}
614
sfricke-samsung94167ca2021-02-26 04:14:59 -0800615bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
616 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500617 bool skip = false;
618
sfricke-samsung94167ca2021-02-26 04:14:59 -0800619 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
620 if (GroupOperation(insn.opcode()) == true) {
621 // Check the quad operations.
622 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
623 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700624 skip |=
625 RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
626 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800627 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800628 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500629
sfricke-samsung94167ca2021-02-26 04:14:59 -0800630 uint32_t scope_type = spv::ScopeMax;
631 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
632 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
633 scope_type = spv::ScopeSubgroup;
634 } else {
635 // "All <id> used for Scope <id> must be of an OpConstant"
636 auto scope_id = module->get_def(insn.word(3));
637 scope_type = scope_id.word(3);
638 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800639
sfricke-samsung94167ca2021-02-26 04:14:59 -0800640 if (scope_type == spv::ScopeSubgroup) {
641 // "Group operations with subgroup scope" must have stage support
642 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
643 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700644 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800645 }
646
647 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
648 auto type = module->get_def(insn.word(1));
649
650 if (type.opcode() == spv::OpTypeVector) {
651 // Get the element type
652 type = module->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800653 }
654
sfricke-samsung94167ca2021-02-26 04:14:59 -0800655 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800656 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
657 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500658
sfricke-samsung0065ce02020-12-03 22:46:37 -0800659 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
660 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
661 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
662 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700663 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500664 }
665 }
666 }
Jeff Bolzee743412019-06-20 22:24:32 -0500667 }
668
669 return skip;
670}
671
ziga-lunarg70651522021-10-11 17:23:30 +0200672bool CoreChecks::ValidateMemoryScope(SHADER_MODULE_STATE const *src, const spirv_inst_iter &insn) const {
673 bool skip = false;
674
sfricke-samsung3511e312021-11-04 21:14:31 -0700675 const auto &entry = MemoryScopeParamPosition(insn.opcode());
ziga-lunarg70651522021-10-11 17:23:30 +0200676 if (entry > 0) {
677 const uint32_t scope_id = insn.word(entry);
678 if (enabled_features.core12.vulkanMemoryModel && !enabled_features.core12.vulkanMemoryModelDeviceScope) {
679 const auto &iter = src->GetConstantDef(scope_id);
680 if (iter != src->end()) {
681 if (GetConstantValue(iter) == spv::Scope::ScopeDevice) {
682 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06265",
683 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is enabled and "
684 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModelDeviceScope is disabled, but Device "
685 "memory scope is used.");
686 }
687 }
688 } else if (!enabled_features.core12.vulkanMemoryModel) {
689 const auto &iter = src->GetConstantDef(scope_id);
690 if (iter != src->end()) {
691 if (GetConstantValue(iter) == spv::Scope::ScopeQueueFamily) {
692 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06266",
693 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is not enabled, but QueueFamily "
694 "memory scope is used.");
695 }
696 }
697 }
698 }
699
700 return skip;
701}
702
ziga-lunarg2818f492021-08-12 14:30:51 +0200703bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
704 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
705 bool skip = false;
706
707 std::array<uint32_t, 3> work_group_size = src->GetWorkgroupSize(pStage, id_value_map);
708
709 for (uint32_t i = 0; i < 3; ++i) {
710 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
711 const char member = 'x' + static_cast<int8_t>(i);
712 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
713 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
714 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
715 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
716 }
717 }
718 return skip;
719}
720
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600721bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600722 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200723 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
724 pStage->stage == VK_SHADER_STAGE_ALL) {
725 return false;
726 }
727
728 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700729 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200730
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700731 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200732 struct Variable {
733 uint32_t baseTypePtrID;
734 uint32_t ID;
735 uint32_t storageClass;
736 };
737 std::vector<Variable> variables;
738
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700739 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700740 bool is_iso_lines = false;
741 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500742
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700743 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600744
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200745 for (auto insn : *src) {
746 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500747 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200748 case spv::OpDecorate:
749 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500750 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700751 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200752 break;
753 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200754 default:
755 break;
756 }
757 break;
758 // Find all input and output variables
759 case spv::OpVariable: {
760 Variable var = {};
761 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600762 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
763 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700764 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200765 var.baseTypePtrID = insn.word(1);
766 var.ID = insn.word(2);
767 variables.push_back(var);
768 }
769 break;
770 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500771 case spv::OpExecutionMode:
772 if (insn.word(1) == entrypoint.word(2)) {
773 switch (insn.word(2)) {
774 default:
775 break;
776 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700777 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500778 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700779 case spv::ExecutionModeIsolines:
780 is_iso_lines = true;
781 break;
782 case spv::ExecutionModePointMode:
783 is_point_mode = true;
784 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500785 }
786 }
787 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200788 default:
789 break;
790 }
791 }
792
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500793 bool strip_output_array_level =
794 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
795 bool strip_input_array_level =
796 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
797 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
798
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700799 uint32_t num_comp_in = 0, num_comp_out = 0;
800 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600801
sfricke-samsung962cad92021-04-13 00:46:29 -0700802 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
803 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600804
805 // Find max component location used for input variables.
806 for (auto &var : inputs) {
807 int location = var.first.first;
808 int component = var.first.second;
809 interface_var &iv = var.second;
810
811 // Only need to look at the first location, since we use the type's whole size
812 if (iv.offset != 0) {
813 continue;
814 }
815
816 if (iv.is_patch) {
817 continue;
818 }
819
sfricke-samsung962cad92021-04-13 00:46:29 -0700820 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700821 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600822 }
823
824 // Find max component location used for output variables.
825 for (auto &var : outputs) {
826 int location = var.first.first;
827 int component = var.first.second;
828 interface_var &iv = var.second;
829
830 // Only need to look at the first location, since we use the type's whole size
831 if (iv.offset != 0) {
832 continue;
833 }
834
835 if (iv.is_patch) {
836 continue;
837 }
838
sfricke-samsung962cad92021-04-13 00:46:29 -0700839 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700840 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600841 }
842
843 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
844 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700845 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
846 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200847 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500848 // Check if the variable is a patch. Patches can also be members of blocks,
849 // but if they are then the top-level arrayness has already been stripped
850 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700851 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200852
853 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700854 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200855 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700856 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200857 }
858 }
859
860 switch (pStage->stage) {
861 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700862 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700863 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700864 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
865 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
866 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700867 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
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.maxVertexOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700870 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700871 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
872 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
873 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600874 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200875 break;
876
877 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700878 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700879 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700880 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
881 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
882 "components by %u components",
883 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700884 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200885 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700886 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600887 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700888 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700889 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
890 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
891 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600892 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700893 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700894 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700895 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
896 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
897 "components by %u components",
898 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700899 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200900 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700901 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600902 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700903 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700904 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
905 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
906 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600907 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200908 break;
909
910 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700911 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700912 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700913 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
914 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
915 "components by %u components",
916 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700917 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200918 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700919 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600920 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700921 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700922 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
923 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
924 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600925 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700926 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700927 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700928 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
929 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
930 "components by %u components",
931 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700932 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200933 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700934 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600935 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700936 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700937 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
938 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
939 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600940 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700941 // Portability validation
942 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
943 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700944 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700945 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
946 " is using abstract patch type IsoLines, but this is not supported on this platform");
947 }
948 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700949 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700950 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
951 " is using abstract patch type PointMode, but this is not supported on this platform");
952 }
953 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200954 break;
955
956 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700957 if (num_comp_in > limits.maxGeometryInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700958 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700959 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
960 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
961 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700962 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200963 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700964 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700965 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700966 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
967 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
968 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600969 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700970 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700971 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700972 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
973 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
974 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700975 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200976 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700977 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700978 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700979 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
980 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
981 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600982 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700983 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700984 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700985 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
986 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
987 "components by %u components",
988 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700989 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500990 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200991 break;
992
993 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700994 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700995 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700996 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
997 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
998 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700999 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001000 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001001 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001002 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001003 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1004 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1005 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001006 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001007 break;
1008
Jeff Bolz148d94e2018-12-13 21:25:56 -06001009 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1010 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1011 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1012 case VK_SHADER_STAGE_MISS_BIT_NV:
1013 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1014 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1015 case VK_SHADER_STAGE_TASK_BIT_NV:
1016 case VK_SHADER_STAGE_MESH_BIT_NV:
1017 break;
1018
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001019 default:
1020 assert(false); // This should never happen
1021 }
1022 return skip;
1023}
1024
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001025bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const {
1026 bool skip = false;
1027
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001028 // Got through all ImageRead/Write instructions
1029 for (auto insn : *src) {
1030 switch (insn.opcode()) {
1031 case spv::OpImageSparseRead:
1032 case spv::OpImageRead: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001033 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001034 if (type_def != src->end()) {
Tim Van Pattenffe91322021-07-26 10:20:50 -06001035 const auto dim = type_def.word(3);
1036 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1037 // StorageImageReadWithoutFormat Capability was declared.
1038 if (dim != spv::DimSubpassData && type_def.word(8) == spv::ImageFormatUnknown) {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001039 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1040 "shaderStorageImageReadWithoutFormat",
1041 kVUID_Features_shaderStorageImageReadWithoutFormat);
1042 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001043 }
1044 break;
1045 }
1046 case spv::OpImageWrite: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001047 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001048 if (type_def != src->end()) {
1049 if (type_def.word(8) == spv::ImageFormatUnknown) {
1050 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1051 "shaderStorageImageWriteWithoutFormat",
1052 kVUID_Features_shaderStorageImageWriteWithoutFormat);
1053 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001054 }
1055 break;
1056 }
1057
1058 }
1059 }
1060
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001061 // Go through all variables for images and check decorations
1062 for (auto insn : *src) {
1063 if (insn.opcode() != spv::OpVariable)
1064 continue;
1065
1066 uint32_t var = insn.word(2);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001067 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001068 if (type_def == src->end())
1069 continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001070 // Only check if the Image Dim operand is not SubpassData
1071 const auto dim = type_def.word(3);
1072 if (dim == spv::DimSubpassData) continue;
Corentin Wallez91f8b6d2021-07-23 10:11:31 +02001073 // Only check storage images
1074 if (type_def.word(7) != 2) continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001075 if (type_def.word(8) != spv::ImageFormatUnknown) continue;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001076
1077 decoration_set img_decorations = src->get_decorations(var);
1078
1079 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1080 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001081 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
1082 "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32
1083 " "
1084 " without format not marked a NonReadable",
1085 var);
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001086 }
1087
1088 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1089 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001090 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
1091 "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32
1092 " "
1093 "without format not marked a NonWritable",
1094 var);
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001095 }
1096 }
1097
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001098 return skip;
1099}
1100
sfricke-samsungdc96f302020-03-18 20:42:10 -07001101bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1102 bool skip = false;
1103 uint32_t total_resources = 0;
1104
1105 // Only currently testing for graphics and compute pipelines
1106 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1107 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1108 return false;
1109 }
1110
1111 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1112 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
Jeremy Gebben11af9792021-08-20 10:20:09 -06001113 total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
sfricke-samsungdc96f302020-03-18 20:42:10 -07001114 }
1115
1116 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1117 // input from CreatePipeline and CreatePipelineLayout level
1118 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1119 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1120 continue;
1121 }
1122
1123 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1124 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1125 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1126 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1127 // Check only descriptor types listed in maxPerStageResources description in spec
1128 switch (binding->descriptorType) {
1129 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1130 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1131 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1132 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1133 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1134 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1135 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1136 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1137 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1138 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1139 total_resources += binding->descriptorCount;
1140 break;
1141 default:
1142 break;
1143 }
1144 }
1145 }
1146 }
1147
1148 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1149 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1150 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001151 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001152 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1153 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1154 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1155 }
1156
1157 return skip;
1158}
1159
Jeff Bolze4356752019-03-07 11:23:46 -06001160// copy the specialization constant value into buf, if it is present
1161void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1162 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1163
1164 if (spec && spec_id < spec->mapEntryCount) {
1165 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1166 }
1167}
1168
1169// Fill in value with the constant or specialization constant value, if available.
1170// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001171static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001172 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001173 auto type_id = src->get_def(insn.word(1));
1174 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1175 return false;
1176 }
1177 switch (insn.opcode()) {
1178 case spv::OpSpecConstant:
1179 *value = insn.word(3);
1180 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1181 return true;
1182 case spv::OpConstant:
1183 *value = insn.word(3);
1184 return true;
1185 default:
1186 return false;
1187 }
1188}
1189
1190// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001191VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001192 switch (insn.opcode()) {
1193 case spv::OpTypeInt:
1194 switch (insn.word(2)) {
1195 case 8:
1196 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1197 case 16:
1198 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1199 case 32:
1200 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1201 case 64:
1202 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1203 default:
1204 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1205 }
1206 case spv::OpTypeFloat:
1207 switch (insn.word(2)) {
1208 case 16:
1209 return VK_COMPONENT_TYPE_FLOAT16_NV;
1210 case 32:
1211 return VK_COMPONENT_TYPE_FLOAT32_NV;
1212 case 64:
1213 return VK_COMPONENT_TYPE_FLOAT64_NV;
1214 default:
1215 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1216 }
1217 default:
1218 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1219 }
1220}
1221
1222// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1223// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001224bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001225 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001226 bool skip = false;
1227
1228 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001229 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001230 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001231 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001232
1233 struct CoopMatType {
1234 uint32_t scope, rows, cols;
1235 VkComponentTypeNV component_type;
1236 bool all_constant;
1237
1238 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1239
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001240 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001241 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001242 spirv_inst_iter insn = src->get_def(id);
1243 uint32_t component_type_id = insn.word(2);
1244 uint32_t scope_id = insn.word(3);
1245 uint32_t rows_id = insn.word(4);
1246 uint32_t cols_id = insn.word(5);
1247 auto component_type_iter = src->get_def(component_type_id);
1248 auto scope_iter = src->get_def(scope_id);
1249 auto rows_iter = src->get_def(rows_id);
1250 auto cols_iter = src->get_def(cols_id);
1251
1252 all_constant = true;
1253 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1254 all_constant = false;
1255 }
1256 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1257 all_constant = false;
1258 }
1259 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1260 all_constant = false;
1261 }
1262 component_type = GetComponentType(component_type_iter, src);
1263 }
1264 };
1265
1266 bool seen_coopmat_capability = false;
1267
1268 for (auto insn : *src) {
1269 // Whitelist instructions whose result can be a cooperative matrix type, and
1270 // keep track of their types. It would be nice if SPIRV-Headers generated code
1271 // to identify which instructions have a result type and result id. Lacking that,
1272 // this whitelist is based on the set of instructions that
1273 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1274 switch (insn.opcode()) {
1275 case spv::OpLoad:
1276 case spv::OpCooperativeMatrixLoadNV:
1277 case spv::OpCooperativeMatrixMulAddNV:
1278 case spv::OpSNegate:
1279 case spv::OpFNegate:
1280 case spv::OpIAdd:
1281 case spv::OpFAdd:
1282 case spv::OpISub:
1283 case spv::OpFSub:
1284 case spv::OpFDiv:
1285 case spv::OpSDiv:
1286 case spv::OpUDiv:
1287 case spv::OpMatrixTimesScalar:
1288 case spv::OpConstantComposite:
1289 case spv::OpCompositeConstruct:
1290 case spv::OpConvertFToU:
1291 case spv::OpConvertFToS:
1292 case spv::OpConvertSToF:
1293 case spv::OpConvertUToF:
1294 case spv::OpUConvert:
1295 case spv::OpSConvert:
1296 case spv::OpFConvert:
1297 id_to_type_id[insn.word(2)] = insn.word(1);
1298 break;
1299 default:
1300 break;
1301 }
1302
1303 switch (insn.opcode()) {
1304 case spv::OpDecorate:
1305 if (insn.word(2) == spv::DecorationSpecId) {
1306 id_to_spec_id[insn.word(1)] = insn.word(3);
1307 }
1308 break;
1309 case spv::OpCapability:
1310 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1311 seen_coopmat_capability = true;
1312
1313 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001314 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001315 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001316 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1317 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001318 }
1319 }
1320 break;
1321 case spv::OpMemoryModel:
1322 // If the capability isn't enabled, don't bother with the rest of this function.
1323 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1324 if (!seen_coopmat_capability) {
1325 return skip;
1326 }
1327 break;
1328 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001329 CoopMatType m;
1330 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001331
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001332 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001333 // Validate that the type parameters are all supported for one of the
1334 // operands of a cooperative matrix property.
1335 bool valid = false;
1336 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001337 if (cooperative_matrix_properties[i].AType == m.component_type &&
1338 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1339 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001340 valid = true;
1341 break;
1342 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001343 if (cooperative_matrix_properties[i].BType == m.component_type &&
1344 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1345 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001346 valid = true;
1347 break;
1348 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001349 if (cooperative_matrix_properties[i].CType == m.component_type &&
1350 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1351 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001352 valid = true;
1353 break;
1354 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001355 if (cooperative_matrix_properties[i].DType == m.component_type &&
1356 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1357 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001358 valid = true;
1359 break;
1360 }
1361 }
1362 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001363 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001364 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1365 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001366 }
1367 }
1368 break;
1369 }
1370 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001371 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001372 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1373 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1374 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1375 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001376 // Couldn't find type of matrix
1377 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001378 break;
1379 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001380 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1381 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1382 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1383 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001384
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001385 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001386 // Validate that the type parameters are all supported for the same
1387 // cooperative matrix property.
1388 bool valid = false;
1389 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001390 if (cooperative_matrix_properties[i].AType == a.component_type &&
1391 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1392 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001393
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001394 cooperative_matrix_properties[i].BType == b.component_type &&
1395 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1396 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001397
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001398 cooperative_matrix_properties[i].CType == c.component_type &&
1399 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1400 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001401
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001402 cooperative_matrix_properties[i].DType == d.component_type &&
1403 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1404 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001405 valid = true;
1406 break;
1407 }
1408 }
1409 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001410 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001411 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1412 "VkCooperativeMatrixPropertiesNV",
1413 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001414 }
1415 }
1416 break;
1417 }
1418 default:
1419 break;
1420 }
1421 }
1422
1423 return skip;
1424}
1425
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001426bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1427 const PIPELINE_STATE *pipeline) const {
1428 bool skip = false;
1429
1430 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1431 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1432 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1433 for (auto insn : *src) {
1434 switch (insn.opcode()) {
1435 case spv::OpCapability:
1436 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1437 auto subpass_flags =
1438 (pipeline->rp_state == nullptr)
1439 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001440 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001441 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1442 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001443 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001444 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1445 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1446 }
1447 }
1448 break;
1449 default:
1450 break;
1451 }
1452 }
1453 }
1454
1455 return skip;
1456}
1457
ziga-lunarg73163742021-08-25 13:15:29 +02001458bool CoreChecks::ValidateShaderSubgroupSizeControl(VkPipelineShaderStageCreateInfo const *pStage) const {
1459 bool skip = false;
1460
1461 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
1462 !enabled_features.subgroup_size_control_features.subgroupSizeControl) {
1463 skip |= LogError(
1464 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1465 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1466 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1467 }
1468
1469 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
1470 !enabled_features.subgroup_size_control_features.computeFullSubgroups) {
1471 skip |= LogError(
1472 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1473 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1474 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1475 }
1476
1477 return skip;
1478}
1479
sfricke-samsung58b84352021-07-31 21:41:04 -07001480bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *src) const {
1481 bool skip = false;
1482
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001483 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001484 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001485
sfricke-samsungf5042b12021-08-05 01:09:40 -07001486 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1487 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1488
1489 const bool valid_storage_buffer_float = (
1490 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1491 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1492 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1493 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1494 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1495 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1496 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1497 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1498 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1499
1500 const bool valid_workgroup_float = (
1501 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1502 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1503 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1504 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1505 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1506 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1507 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1508 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1509 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1510
1511 const bool valid_image_float = (
1512 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1513 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1514 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1515
1516 const bool valid_16_float = (
1517 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1518 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1519 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1520 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1521 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1522 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1523
1524 const bool valid_32_float = (
1525 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1526 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1527 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1528 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1529 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1530 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1531 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1532 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1533 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1534
1535 const bool valid_64_float = (
1536 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1537 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1538 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1539 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1540 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1541 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1542 // clang-format on
1543
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001544 for (const auto &atomic_inst : src->GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001545 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungf5042b12021-08-05 01:09:40 -07001546 const uint32_t opcode = src->at(atomic_inst.first).opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001547
1548 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001549 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001550 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1551 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001552 skip |= LogError(device, "VUID-RuntimeSpirv-None-06278",
1553 "%s: Can't use 64-bit int atomics operations (%s) with %s storage class without "
1554 "shaderBufferInt64Atomics enabled.",
1555 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode),
1556 StorageClassName(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001557 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1558 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001559 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001560 "%s: Can't use 64-bit int atomics operations (%s) with Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001561 "shaderSharedInt64Atomics enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001562 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001563 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001564 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001565 "%s: Can't use 64-bit int atomics operations (%s) with Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001566 "shaderImageInt64Atomics enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001567 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsung58b84352021-07-31 21:41:04 -07001568 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001569 } else if (atomic.type == spv::OpTypeFloat) {
1570 // Validate Floats
1571 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1572 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001573 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1574 : "VUID-RuntimeSpirv-None-06280";
1575 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001576 "%s: Can't use float atomics operations (%s) with StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001577 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1578 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1579 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1580 "shaderBufferFloat64AtomicMinMax enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001581 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001582 } else if (opcode == spv::OpAtomicFAddEXT) {
1583 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1584 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1585 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with "
1586 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
1587 report_data->FormatHandle(src->vk_shader_module()).c_str());
1588 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1589 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1590 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with "
1591 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
1592 report_data->FormatHandle(src->vk_shader_module()).c_str());
1593 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1594 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1595 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with "
1596 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
1597 report_data->FormatHandle(src->vk_shader_module()).c_str());
1598 }
1599 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1600 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
1601 skip |= LogError(
1602 device, kVUID_Core_Shader_AtomicFeature,
1603 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1604 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
1605 report_data->FormatHandle(src->vk_shader_module()).c_str());
1606 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
1607 skip |= LogError(
1608 device, kVUID_Core_Shader_AtomicFeature,
1609 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1610 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
1611 report_data->FormatHandle(src->vk_shader_module()).c_str());
1612 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
1613 skip |= LogError(
1614 device, kVUID_Core_Shader_AtomicFeature,
1615 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1616 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
1617 report_data->FormatHandle(src->vk_shader_module()).c_str());
1618 }
1619 } else {
1620 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1621 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
1622 skip |= LogError(
1623 device, kVUID_Core_Shader_AtomicFeature,
1624 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1625 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
1626 report_data->FormatHandle(src->vk_shader_module()).c_str());
1627 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
1628 skip |= LogError(
1629 device, kVUID_Core_Shader_AtomicFeature,
1630 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1631 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
1632 report_data->FormatHandle(src->vk_shader_module()).c_str());
1633 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
1634 skip |= LogError(
1635 device, kVUID_Core_Shader_AtomicFeature,
1636 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1637 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
1638 report_data->FormatHandle(src->vk_shader_module()).c_str());
1639 }
1640 }
1641 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1642 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001643 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1644 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001645 skip |=
1646 LogError(device, vuid,
1647 "%s: Can't use float atomics operations (%s) with Workgroup storage class without "
1648 "shaderSharedFloat32Atomics or "
1649 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1650 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1651 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1652 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001653 } else if (opcode == spv::OpAtomicFAddEXT) {
1654 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1655 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1656 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1657 "storage class without shaderSharedFloat16AtomicAdd enabled.",
1658 report_data->FormatHandle(src->vk_shader_module()).c_str());
1659 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1660 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1661 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1662 "storage class without shaderSharedFloat32AtomicAdd enabled.",
1663 report_data->FormatHandle(src->vk_shader_module()).c_str());
1664 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1665 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1666 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1667 "storage class without shaderSharedFloat64AtomicAdd enabled.",
1668 report_data->FormatHandle(src->vk_shader_module()).c_str());
1669 }
1670 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1671 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
1672 skip |= LogError(
1673 device, kVUID_Core_Shader_AtomicFeature,
1674 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1675 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
1676 report_data->FormatHandle(src->vk_shader_module()).c_str());
1677 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
1678 skip |= LogError(
1679 device, kVUID_Core_Shader_AtomicFeature,
1680 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1681 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
1682 report_data->FormatHandle(src->vk_shader_module()).c_str());
1683 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
1684 skip |= LogError(
1685 device, kVUID_Core_Shader_AtomicFeature,
1686 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1687 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
1688 report_data->FormatHandle(src->vk_shader_module()).c_str());
1689 }
1690 } else {
1691 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1692 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
1693 skip |= LogError(
1694 device, kVUID_Core_Shader_AtomicFeature,
1695 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1696 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat16Atomics enabled.",
1697 report_data->FormatHandle(src->vk_shader_module()).c_str());
1698 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
1699 skip |= LogError(
1700 device, kVUID_Core_Shader_AtomicFeature,
1701 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1702 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat32Atomics enabled.",
1703 report_data->FormatHandle(src->vk_shader_module()).c_str());
1704 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
1705 skip |= LogError(
1706 device, kVUID_Core_Shader_AtomicFeature,
1707 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1708 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat64Atomics enabled.",
1709 report_data->FormatHandle(src->vk_shader_module()).c_str());
1710 }
1711 }
1712 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001713 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1714 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001715 skip |= LogError(
1716 device, vuid,
1717 "%s: Can't use float atomics operations (%s) with Image storage class without shaderImageFloat32Atomics or "
1718 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
1719 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001720 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001721 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001722 "%s: Can't use 16-bit float atomics operations (%s) without shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001723 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1724 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001725 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001726 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001727 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1728 : "VUID-RuntimeSpirv-None-06335";
1729 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001730 "%s: Can't use 32-bit float atomics operations (%s) without shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001731 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1732 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1733 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1734 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001735 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001736 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001737 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1738 : "VUID-RuntimeSpirv-None-06336";
1739 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001740 "%s: Can't use 64-bit float atomics operations (%s) without shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001741 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1742 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001743 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001744 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001745 }
1746 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001747 return skip;
1748}
1749
John Zulaufac4c6e12019-07-01 16:05:58 -06001750bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001751 auto entrypoint_id = entrypoint.word(2);
1752
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001753 // The first denorm execution mode encountered, along with its bit width.
1754 // Used to check if SeparateDenormSettings is respected.
1755 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001756
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001757 // The first rounding mode encountered, along with its bit width.
1758 // Used to check if SeparateRoundingModeSettings is respected.
1759 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001760
1761 bool skip = false;
1762
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001763 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001764 uint32_t invocations = 0;
1765
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001766 const auto &execution_mode_inst = src->GetExecutionModeInstructions();
1767 auto it = execution_mode_inst.find(entrypoint_id);
1768 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001769 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001770 auto mode = insn.word(2);
1771 switch (mode) {
1772 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1773 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001774 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001775 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001776 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
1777 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device");
1778 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1779 skip |= LogError(
1780 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
1781 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device");
1782 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1783 skip |= LogError(
1784 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
1785 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001786 }
1787 break;
1788 }
1789
1790 case spv::ExecutionModeDenormPreserve: {
1791 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001792 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1793 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
1794 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device");
1795 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1796 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
1797 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device");
1798 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1799 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
1800 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001801 }
1802
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001803 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1804 // Register the first denorm execution mode found
1805 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001806 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001807 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001808 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001809 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001810 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001811 "Shader uses different denorm execution modes for 16 and 64-bit but "
1812 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001813 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001814 }
1815 break;
1816
Mike Schuchardt2df08912020-12-15 16:28:09 -08001817 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001818 break;
1819
Mike Schuchardt2df08912020-12-15 16:28:09 -08001820 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001821 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001822 "Shader uses different denorm execution modes for different bit widths but "
1823 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001824 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001825 break;
1826
1827 default:
1828 break;
1829 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001830 }
1831 break;
1832 }
1833
1834 case spv::ExecutionModeDenormFlushToZero: {
1835 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001836 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
1837 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1838 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device");
1839 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
1840 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1841 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device");
1842 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
1843 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1844 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001845 }
1846
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001847 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1848 // Register the first denorm execution mode found
1849 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001850 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001851 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001852 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001853 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001854 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001855 "Shader uses different denorm execution modes for 16 and 64-bit but "
1856 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001857 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001858 }
1859 break;
1860
Mike Schuchardt2df08912020-12-15 16:28:09 -08001861 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001862 break;
1863
Mike Schuchardt2df08912020-12-15 16:28:09 -08001864 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001865 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001866 "Shader uses different denorm execution modes for different bit widths but "
1867 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001868 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001869 break;
1870
1871 default:
1872 break;
1873 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001874 }
1875 break;
1876 }
1877
1878 case spv::ExecutionModeRoundingModeRTE: {
1879 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001880 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1881 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
1882 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device");
1883 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1884 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
1885 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device");
1886 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1887 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
1888 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001889 }
1890
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001891 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1892 // Register the first rounding mode found
1893 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001894 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001895 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001896 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001897 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001898 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001899 "Shader uses different rounding modes for 16 and 64-bit but "
1900 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001901 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001902 }
1903 break;
1904
Mike Schuchardt2df08912020-12-15 16:28:09 -08001905 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001906 break;
1907
Mike Schuchardt2df08912020-12-15 16:28:09 -08001908 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001909 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001910 "Shader uses different rounding modes for different bit widths but "
1911 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001912 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001913 break;
1914
1915 default:
1916 break;
1917 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001918 }
1919 break;
1920 }
1921
1922 case spv::ExecutionModeRoundingModeRTZ: {
1923 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001924 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
1925 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
1926 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device");
1927 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
1928 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
1929 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device");
1930 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
1931 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
1932 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001933 }
1934
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001935 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1936 // Register the first rounding mode found
1937 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001938 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001939 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001940 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001941 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001942 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001943 "Shader uses different rounding modes for 16 and 64-bit but "
1944 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001945 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001946 }
1947 break;
1948
Mike Schuchardt2df08912020-12-15 16:28:09 -08001949 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001950 break;
1951
Mike Schuchardt2df08912020-12-15 16:28:09 -08001952 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001953 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001954 "Shader uses different rounding modes for different bit widths but "
1955 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001956 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001957 break;
1958
1959 default:
1960 break;
1961 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001962 }
1963 break;
1964 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001965
1966 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001967 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001968 break;
1969 }
1970
1971 case spv::ExecutionModeInvocations: {
1972 invocations = insn.word(3);
1973 break;
1974 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001975 }
1976 }
1977 }
1978
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001979 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001980 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001981 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
1982 "Geometry shader entry point must have an OpExecutionMode instruction that "
1983 "specifies a maximum output vertex count that is greater than 0 and less "
1984 "than or equal to maxGeometryOutputVertices. "
1985 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001986 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001987 }
1988
1989 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001990 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
1991 "Geometry shader entry point must have an OpExecutionMode instruction that "
1992 "specifies an invocation count that is greater than 0 and less "
1993 "than or equal to maxGeometryShaderInvocations. "
1994 "Invocations=%d, maxGeometryShaderInvocations=%d",
1995 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001996 }
1997 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001998 return skip;
1999}
2000
Chris Forbes47567b72017-06-09 12:09:45 -07002001// For given pipelineLayout verify that the set_layout_node at slot.first
2002// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002003static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002004 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002005 if (!pipelineLayout) return nullptr;
2006
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002007 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07002008
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002009 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07002010}
2011
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002012// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2013// o If there is only a vertex shader : gl_PointSize must be written when using points
2014// o If there is a geometry or tessellation shader:
2015// - If shaderTessellationAndGeometryPointSize feature is enabled:
2016// * gl_PointSize must be written in the final geometry stage
2017// - If shaderTessellationAndGeometryPointSize feature is disabled:
2018// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002019bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06002020 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002021 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2022 return false;
2023 }
2024
2025 bool pointsize_written = false;
2026 bool skip = false;
2027
2028 // Search for PointSize built-in decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002029 for (const auto &set : src->GetBuiltinDecorationList()) {
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002030 auto insn = src->at(set.offset);
2031 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002032 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002033 if (pointsize_written) {
2034 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002035 }
2036 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002037 }
2038
2039 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002040 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002041 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002042 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002043 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2044 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002045 }
2046 } else if (!pointsize_written) {
2047 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002048 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002049 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2050 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002051 }
2052 return skip;
2053}
John Zulauf14c355b2019-06-27 16:09:37 -06002054
Tobias Hector6663c9b2020-11-05 10:18:02 +00002055bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
2056 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2057 bool primitiverate_written = false;
2058 bool viewportindex_written = false;
2059 bool viewportmask_written = false;
2060 bool skip = false;
2061
2062 // Check if the primitive shading rate is written
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002063 for (const auto &set : src->GetBuiltinDecorationList()) {
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002064 auto insn = src->at(set.offset);
2065 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002066 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002067 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002068 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002069 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002070 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002071 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002072 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2073 break;
2074 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002075 }
2076
Tony-LunarGd44844c2021-01-22 13:24:37 -07002077 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002078 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002079 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002080 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002081 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002082 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2083 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2084 "multiple viewports "
2085 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2086 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002087 }
2088
2089 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002090 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002091 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2092 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2093 "ViewportIndex built-ins,"
2094 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2095 string_VkShaderStageFlagBits(stage));
2096 }
2097
2098 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002099 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002100 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2101 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2102 "ViewportMaskNV built-ins,"
2103 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2104 string_VkShaderStageFlagBits(stage));
2105 }
2106 }
2107 return skip;
2108}
2109
ziga-lunargce66e542021-09-19 00:11:14 +02002110bool CoreChecks::ValidateDecorations(SHADER_MODULE_STATE const* module) const {
2111 bool skip = false;
2112
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002113 for (const auto &op_decorate : module->GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002114 uint32_t decoration = op_decorate.word(2);
2115 if (decoration == spv::DecorationXfbStride) {
2116 uint32_t stride = op_decorate.word(3);
2117 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2118 skip |= LogError(
2119 device, "VUID-RuntimeSpirv-XfbStride-06313",
2120 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2121 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2122 ").",
2123 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2124 }
2125 }
ziga-lunarg423cf212021-11-07 00:00:27 +01002126 if (decoration == spv::DecorationStream) {
2127 uint32_t stream = op_decorate.word(3);
2128 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2129 skip |= LogError(
2130 device, "VUID-RuntimeSpirv-Stream-06312",
2131 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2132 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32 ").",
2133 stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
2134 }
2135 }
ziga-lunargce66e542021-09-19 00:11:14 +02002136 }
2137
2138 return skip;
2139}
2140
ziga-lunarg28d08792021-10-13 15:42:59 +02002141bool CoreChecks::ValidateTransformFeedback(SHADER_MODULE_STATE const *src) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002142 bool skip = false;
2143
ziga-lunarg28d08792021-10-13 15:42:59 +02002144 // Temp workaround to prevent false positive errors
2145 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
2146 if (src->HasMultipleEntryPoints()) {
2147 return skip;
2148 }
2149
2150 layer_data::unordered_set<uint32_t> emitted_streams;
2151 bool output_points = false;
2152 for (const auto& insn : *src) {
2153 const uint32_t opcode = insn.opcode();
2154 if (opcode == spv::OpEmitStreamVertex) {
2155 emitted_streams.emplace(static_cast<uint32_t>(src->GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002156 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002157 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
2158 uint32_t stream = static_cast<uint32_t>(src->GetConstantValueById(insn.word(1)));
2159 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2160 skip |= LogError(
2161 device, "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002162 "vkCreateGraphicsPipelines(): shader uses transform feedback stream (%s) with index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002163 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2164 ").",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002165 string_SpvOpcode(opcode), stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002166 }
2167 }
2168 if (opcode == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeOutputPoints) {
2169 output_points = true;
2170 }
2171 }
2172
2173 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2174 if (emitted_streams_size > 1 && !output_points &&
2175 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
2176 skip |= LogError(
2177 device, "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2178 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles is VK_FALSE, but execution mode is not OutputPoints.",
2179 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002180 }
2181
2182 return skip;
2183}
2184
sfricke-samsung864162a2021-11-01 21:58:01 -07002185// Checks for both TexelOffset and TexelGatherOffset limits
2186bool CoreChecks::ValidateTexelOffsetLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter &insn) const {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002187 bool skip = false;
2188
2189 const uint32_t opcode = insn.opcode();
sfricke-samsung864162a2021-11-01 21:58:01 -07002190 if (ImageGatherOperation(opcode) || ImageSampleOperation(opcode) || ImageFetchOperation(opcode)) {
sfricke-samsung3511e312021-11-04 21:14:31 -07002191 uint32_t image_operand_position = ImageOperandsParamPosition(opcode);
sfricke-samsung864162a2021-11-01 21:58:01 -07002192 // Image operands can be optional
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002193 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2194 auto image_operand = insn.word(image_operand_position);
sfricke-samsung864162a2021-11-01 21:58:01 -07002195 // Bits we are validating (sample/fetch only check ConstOffset)
ziga-lunarga12c75a2021-09-16 16:36:16 +02002196 uint32_t offset_bits =
sfricke-samsung864162a2021-11-01 21:58:01 -07002197 ImageGatherOperation(opcode)
2198 ? (spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask)
2199 : (spv::ImageOperandsConstOffsetMask);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002200 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002201 // Operand values follow
2202 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002203 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2204 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2205 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2206 if (image_operand & i) { // If the bit is set, consume operand
2207 if (insn.len() > index && (i & offset_bits)) {
2208 uint32_t constant_id = insn.word(index);
2209 const auto &constant = src->get_def(constant_id);
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002210 const bool is_dynamic_offset = constant == src->end();
2211 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002212 for (uint32_t j = 3; j < constant.len(); ++j) {
2213 uint32_t comp_id = constant.word(j);
2214 const auto &comp = src->get_def(comp_id);
sfricke-samsungef3fe742021-10-06 10:51:34 -07002215 const auto &comp_type = src->get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002216 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002217 const uint32_t offset = comp.word(3);
sfricke-samsung864162a2021-11-01 21:58:01 -07002218 // spec requires minTexelGatherOffset/minTexelOffset to be -8 or less so never can compare if
2219 // unsigned spec requires maxTexelGatherOffset/maxTexelOffset to be 7 or greater so never can
2220 // compare if signed is less then zero
sfricke-samsungef3fe742021-10-06 10:51:34 -07002221 const int32_t signed_offset = static_cast<int32_t>(offset);
2222 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2223
sfricke-samsung864162a2021-11-01 21:58:01 -07002224 // There are 2 sets of VU being covered where the only main difference is the opcode
2225 if (ImageGatherOperation(opcode)) {
2226 // min/maxTexelGatherOffset
2227 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
2228 skip |=
2229 LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002230 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002231 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002232 string_SpvOpcode(opcode), signed_offset,
2233 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002234 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2235 (!use_signed || (use_signed && signed_offset > 0))) {
2236 skip |= LogError(
2237 device, "VUID-RuntimeSpirv-OpImage-06377",
2238 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIu32
2239 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32 ").",
2240 string_SpvOpcode(opcode), offset, phys_dev_props.limits.maxTexelGatherOffset);
2241 }
2242 } else {
2243 // min/maxTexelOffset
2244 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelOffset)) {
2245 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06435",
2246 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIi32
2247 ") less than VkPhysicalDeviceLimits::minTexelOffset (%" PRIi32 ").",
2248 string_SpvOpcode(opcode), signed_offset,
2249 phys_dev_props.limits.minTexelOffset);
2250 } else if ((offset > phys_dev_props.limits.maxTexelOffset) &&
2251 (!use_signed || (use_signed && signed_offset > 0))) {
2252 skip |=
2253 LogError(device, "VUID-RuntimeSpirv-OpImageSample-06436",
2254 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIu32
2255 ") greater than VkPhysicalDeviceLimits::maxTexelOffset (%" PRIu32 ").",
2256 string_SpvOpcode(opcode), offset, phys_dev_props.limits.maxTexelOffset);
2257 }
ziga-lunarga12c75a2021-09-16 16:36:16 +02002258 }
2259 }
2260 }
2261 }
sfricke-samsung3511e312021-11-04 21:14:31 -07002262 index += ImageOperandsParamCount(i);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002263 }
2264 }
2265 }
2266 }
2267 }
2268
2269 return skip;
2270}
2271
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002272bool CoreChecks::ValidateShaderClock(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002273 bool skip = false;
2274
sfricke-samsung94167ca2021-02-26 04:14:59 -08002275 switch (insn.opcode()) {
2276 case spv::OpReadClockKHR: {
2277 auto scope_id = module->get_def(insn.word(3));
2278 auto scope_type = scope_id.word(3);
2279 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002280 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002281 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002282 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002283 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002284 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002285 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002286 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002287 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002288 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002289 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002290 }
2291 }
2292 return skip;
2293}
2294
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002295bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2296 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002297 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002298 const auto *pStage = stage_state.create_info;
2299 const auto *module = stage_state.module.get();
2300 const auto &entrypoint = stage_state.entrypoint;
John Zulauf14c355b2019-06-27 16:09:37 -06002301 // Check the module
2302 if (!module->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002303 skip |= LogError(
2304 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2305 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002306 }
2307
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002308 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2309 // specializations should be applied and validated.
2310 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002311 pStage->pSpecializationInfo->pMapEntries != nullptr && module->HasSpecConstants()) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002312 // Gather the specialization-constant values.
2313 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002314 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002315 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 -06002316 id_value_map.reserve(specialization_info->mapEntryCount);
2317 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2318 auto const &map_entry = specialization_info->pMapEntries[i];
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002319 const auto itr = module->GetSpecConstMap().find(map_entry.constantID);
sfricke-samsung033b0262021-07-09 00:53:06 -07002320 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2321 // behavior of the pipeline."
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002322 if (itr != module->GetSpecConstMap().cend()) {
sfricke-samsung033b0262021-07-09 00:53:06 -07002323 // Make sure map_entry.size matches the spec constant's size
2324 uint32_t spec_const_size = decoration_set::kInvalidValue;
2325 const auto def_ins = module->get_def(itr->second);
2326 const auto type_ins = module->get_def(def_ins.word(1));
2327 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2328 switch (type_ins.opcode()) {
2329 case spv::OpTypeBool:
2330 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2331 spec_const_size = sizeof(VkBool32);
2332 break;
2333 case spv::OpTypeInt:
2334 case spv::OpTypeFloat:
2335 spec_const_size = type_ins.word(2) / 8;
2336 break;
2337 default:
2338 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2339 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2340 break;
2341 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002342
sfricke-samsung033b0262021-07-09 00:53:06 -07002343 if (map_entry.size != spec_const_size) {
2344 skip |=
2345 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002346 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2347 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2348 map_entry.constantID, i, map_entry.size,
2349 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002350 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002351 }
2352
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002353 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002354 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2355 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2356 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2357 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2358 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2359
2360 std::copy(start_in_p, end_in_p, out_p);
2361 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002362 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002363 }
2364
sfricke-samsung5628f982021-10-19 09:21:59 -07002365 // both spirv-opt and spirv-val will use the same flags
2366 spvtools::ValidatorOptions options;
2367 AdjustValidatorOptions(device_extensions, enabled_features, options);
2368
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002369 // Apply the specialization-constant values and revalidate the shader module.
sfricke-samsung45996a42021-09-16 13:45:27 -07002370 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002371 spvtools::Optimizer optimizer(spirv_environment);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002372 spvtools::MessageConsumer consumer = [&skip, &module, &stage_state, this](spv_message_level_t level, const char *source,
2373 const spv_position_t &position,
2374 const char *message) {
2375 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2376 "%s does not contain valid spirv for stage %s. %s",
2377 report_data->FormatHandle(module->vk_shader_module()).c_str(),
2378 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002379 };
2380 optimizer.SetMessageConsumer(consumer);
2381 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2382 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2383 std::vector<uint32_t> specialized_spirv;
sfricke-samsung5628f982021-10-19 09:21:59 -07002384 auto const optimized = optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002385 assert(optimized == true);
2386
2387 if (optimized) {
2388 spv_context ctx = spvContextCreate(spirv_environment);
2389 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2390 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002391 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2392 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002393 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002394 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002395 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002396 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002397 }
2398
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002399 spvDiagnosticDestroy(diag);
2400 spvContextDestroy(ctx);
2401 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002402
2403 skip |= ValidateWorkgroupSize(module, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002404 }
2405
John Zulauf14c355b2019-06-27 16:09:37 -06002406 // Check the entrypoint
2407 if (entrypoint == module->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002408 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2409 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002410 }
2411 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2412
2413 // Mark accessible ids
2414 auto &accessible_ids = stage_state.accessible_ids;
2415
Chris Forbes47567b72017-06-09 12:09:45 -07002416 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002417
sfricke-samsung94167ca2021-02-26 04:14:59 -08002418 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2419 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002420 uint32_t total_shared_size = 0;
sfricke-samsung94167ca2021-02-26 04:14:59 -08002421 for (auto insn : *module) {
sfricke-samsung864162a2021-11-01 21:58:01 -07002422 skip |= ValidateTexelOffsetLimits(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002423 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002424 skip |= ValidateShaderClock(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002425 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
ziga-lunarg70651522021-10-11 17:23:30 +02002426 skip |= ValidateMemoryScope(module, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02002427 total_shared_size += module->CalcComputeSharedMemory(pStage->stage, insn);
2428 }
2429
2430 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2431 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002432 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002433 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002434 }
2435
ziga-lunarg28d08792021-10-13 15:42:59 +02002436 skip |= ValidateTransformFeedback(module);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002437 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2438 stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002439 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03002440 skip |= ValidateShaderStorageImageFormats(module);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002441 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsung58b84352021-07-31 21:41:04 -07002442 skip |= ValidateAtomicsTypes(module);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002443 skip |= ValidateExecutionModes(module, entrypoint);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002444 skip |= ValidateSpecializations(pStage);
ziga-lunargce66e542021-09-19 00:11:14 +02002445 skip |= ValidateDecorations(module);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002446 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002447 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002448 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07002449 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002450 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
2451 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
2452 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002453 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
2454 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
2455 }
sfricke-samsung45996a42021-09-16 13:45:27 -07002456 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002457 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
2458 }
ziga-lunarg73163742021-08-25 13:15:29 +02002459 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
2460 skip |= ValidateShaderSubgroupSizeControl(pStage);
2461 }
Chris Forbes47567b72017-06-09 12:09:45 -07002462
sfricke-samsung7699b912021-04-12 23:01:51 -07002463 // "layout must be consistent with the layout of the * shader"
2464 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002465 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002466 switch (pipeline->create_info.graphics.sType) {
2467 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2468 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2469 break;
2470 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2471 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2472 break;
2473 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2474 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2475 break;
2476 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2477 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2478 break;
2479 default:
2480 assert(false);
2481 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002482 }
2483
sfricke-samsung7699b912021-04-12 23:01:51 -07002484 // Validate Push Constants use
2485 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
2486
Chris Forbes47567b72017-06-09 12:09:45 -07002487 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002488 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002489 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002490 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002491 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002492 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2493 std::set<uint32_t> descriptor_types =
2494 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002495
2496 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002497 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002498 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002499 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002500 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002501 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002502 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2503 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002504 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2505 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002506 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002507 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2508 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002509 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002510 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002511 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002512 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002513 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002514 }
2515 }
2516
2517 // Validate use of input attachments against subpass structure
2518 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002519 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002520
Petr Krause91f7a12017-12-14 20:57:36 +01002521 auto rpci = pipeline->rp_state->createInfo.ptr();
Jeremy Gebben11af9792021-08-20 10:20:09 -06002522 auto subpass = pipeline->create_info.graphics.subpass;
Chris Forbes47567b72017-06-09 12:09:45 -07002523
2524 for (auto use : input_attachment_uses) {
2525 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2526 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07002527 ? input_attachments[use.first].attachment
2528 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002529
2530 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002531 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2532 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsung962cad92021-04-13 00:46:29 -07002533 } else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002534 skip |=
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002535 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2536 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
sfricke-samsung962cad92021-04-13 00:46:29 -07002537 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002538 }
2539 }
2540 }
Lockeaa8fdc02019-04-02 11:59:20 -06002541 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02002542 skip |= ValidateComputeWorkGroupSizes(module, entrypoint, stage_state);
Lockeaa8fdc02019-04-02 11:59:20 -06002543 }
ziga-lunarg73163742021-08-25 13:15:29 +02002544
Chris Forbes47567b72017-06-09 12:09:45 -07002545 return skip;
2546}
2547
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002548bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2549 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2550 spirv_inst_iter consumer_entrypoint,
2551 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002552 bool skip = false;
2553
2554 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002555 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2556 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002557
2558 auto a_it = outputs.begin();
2559 auto b_it = inputs.begin();
2560
ziga-lunarg8346fe82021-08-22 17:30:50 +02002561 uint32_t a_component = 0;
2562 uint32_t b_component = 0;
2563
Chris Forbes47567b72017-06-09 12:09:45 -07002564 // Maps sorted by key (location); walk them together to find mismatches
2565 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2566 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2567 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2568 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2569 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2570
ziga-lunarg8346fe82021-08-22 17:30:50 +02002571 a_first.second += a_component;
2572 b_first.second += b_component;
2573
2574 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2575 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2576 assert(a_at_end || a_component < a_length);
2577 assert(b_at_end || b_component < b_length);
2578
Chris Forbes47567b72017-06-09 12:09:45 -07002579 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002580 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002581 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2582 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2583 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2584 a_it++;
2585 a_component = 0;
2586 } else {
2587 a_component++;
2588 }
Chris Forbes47567b72017-06-09 12:09:45 -07002589 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002590 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002591 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2592 b_first.first, b_first.second, producer_stage->name);
2593 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2594 b_it++;
2595 b_component = 0;
2596 } else {
2597 b_component++;
2598 }
Chris Forbes47567b72017-06-09 12:09:45 -07002599 } else {
2600 // subtleties of arrayed interfaces:
2601 // - if is_patch, then the member is not arrayed, even though the interface may be.
2602 // - if is_block_member, then the extra array level of an arrayed interface is not
2603 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002604 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002605 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002606 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002607 producer->DescribeType(a_it->second.type_id).c_str(),
2608 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002609 a_it++;
2610 b_it++;
2611 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002612 }
2613 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002614 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002615 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2616 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2617 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002618 }
2619 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002620 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002621 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2622 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002623 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002624 uint32_t a_remaining = a_length - a_component;
2625 uint32_t b_remaining = b_length - b_component;
2626 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2627 a_it++;
2628 b_it++;
2629 a_component = 0;
2630 b_component = 0;
2631 } else if (a_remaining > b_remaining) { // a has more components remaining
2632 a_component += b_remaining;
2633 b_component = 0;
2634 b_it++;
2635 } else if (b_remaining > a_remaining) { // b has more components remaining
2636 b_component += a_remaining;
2637 a_component = 0;
2638 a_it++;
2639 }
Chris Forbes47567b72017-06-09 12:09:45 -07002640 }
2641 }
2642
Ari Suonpaa696b3432019-03-11 14:02:57 +02002643 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002644 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2645 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002646
2647 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2648 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002649 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002650 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002651 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2652 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002653 } else {
2654 auto it_producer = builtins_producer.begin();
2655 auto it_consumer = builtins_consumer.begin();
2656 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2657 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002658 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002659 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2660 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002661 break;
2662 }
2663 it_producer++;
2664 it_consumer++;
2665 }
2666 }
2667 }
2668 }
2669
Chris Forbes47567b72017-06-09 12:09:45 -07002670 return skip;
2671}
2672
John Zulauf14c355b2019-06-27 16:09:37 -06002673static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002674 uint32_t stage_mask = 0;
2675 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2676 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2677 stage_mask |= pCreateInfo->pStages[i].stage;
2678 }
2679 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002680 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2681 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2682 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002683 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2684 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2685 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2686 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2687 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002688 }
2689 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002690 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002691}
2692
Chris Forbes47567b72017-06-09 12:09:45 -07002693// Validate that the shaders used by the given pipeline and store the active_slots
2694// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002695bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002696 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002697
Chris Forbes47567b72017-06-09 12:09:45 -07002698 bool skip = false;
2699
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002700 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002701
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002702 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
2703 for (auto &stage : pipeline->stage_state) {
2704 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
2705 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
2706 vertex_stage = &stage;
2707 }
2708 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
2709 fragment_stage = &stage;
2710 }
Chris Forbes47567b72017-06-09 12:09:45 -07002711 }
2712
2713 // if the shader stages are no good individually, cross-stage validation is pointless.
2714 if (skip) return true;
2715
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002716 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002717
2718 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002719 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002720 }
2721
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002722 if (vertex_stage && vertex_stage->module->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
2723 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07002724 }
2725
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002726 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
2727 const auto &producer = pipeline->stage_state[i - 1];
2728 const auto &consumer = pipeline->stage_state[i];
2729 assert(producer.module);
2730 if (&producer == fragment_stage) {
2731 break;
2732 }
2733 if (consumer.module) {
2734 if (consumer.module->has_valid_spirv && producer.module->has_valid_spirv) {
2735 auto producer_id = GetShaderStageId(producer.stage_flag);
2736 auto consumer_id = GetShaderStageId(consumer.stage_flag);
2737 skip |=
2738 ValidateInterfaceBetweenStages(producer.module.get(), producer.entrypoint, &shader_stage_attribs[producer_id],
2739 consumer.module.get(), consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002740 }
Chris Forbes47567b72017-06-09 12:09:45 -07002741
Chris Forbes47567b72017-06-09 12:09:45 -07002742 }
2743 }
2744
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002745 if (fragment_stage && fragment_stage->module->has_valid_spirv) {
2746 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002747 create_info->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07002748 }
2749
2750 return skip;
2751}
2752
Tony-LunarGb2ded512021-02-02 16:03:30 -07002753void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002754 if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports ||
2755 !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) {
2756 return;
2757 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002758
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002759 for (auto &stage : pipeline_state->stage_state) {
2760 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2761 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002762 bool primitiverate_written = false;
Tobias Hector6663c9b2020-11-05 10:18:02 +00002763
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002764 for (const auto &set : stage.module->GetBuiltinDecorationList()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002765 auto insn = stage.module->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002766 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002767 primitiverate_written = stage.module->IsBuiltInWritten(insn, stage.entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002768 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002769 if (primitiverate_written) {
2770 break;
2771 }
Tony-LunarGb2ded512021-02-02 16:03:30 -07002772 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002773
Tony-LunarGb2ded512021-02-02 16:03:30 -07002774 if (primitiverate_written) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002775 pipeline_state->wrote_primitive_shading_rate.insert(stage.stage_flag);
Tony-LunarGb2ded512021-02-02 16:03:30 -07002776 }
2777 }
2778 }
2779}
2780
2781bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2782 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002783 bool skip = false;
2784
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002785 for (auto &stage : pipeline->stage_state) {
2786 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2787 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002788 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2789 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002790 if (pipeline->wrote_primitive_shading_rate.find(stage.stage_flag) != pipeline->wrote_primitive_shading_rate.end()) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002791 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002792 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002793 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2794 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2795 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002796 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002797 }
2798 }
2799 }
2800 }
2801
2802 return skip;
2803}
2804
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002805bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002806 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07002807}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002808
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002809uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2810 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002811 const auto &create_info = pipeline->create_info.raytracing;
2812 const auto *stages = create_info.ptr()->pStages;
2813 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002814 if (stages[stage_index].stage == stageBit) {
2815 total++;
2816 }
2817 }
2818
Jeremy Gebben11af9792021-08-20 10:20:09 -06002819 if (create_info.pLibraryInfo) {
2820 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2821 const PIPELINE_STATE *library_pipeline = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002822 total += CalcShaderStageCount(library_pipeline, stageBit);
2823 }
2824 }
2825
2826 return total;
2827}
2828
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002829bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE *pipeline, uint32_t group, uint32_t stage) const {
2830 if (group == VK_SHADER_UNUSED_NV) {
2831 return true;
2832 }
2833
2834 const auto &create_info = pipeline->create_info.raytracing;
2835 const auto *stages = create_info.ptr()->pStages;
2836
2837 if (group < create_info.stageCount) {
2838 return (stages[group].stage & stage) != 0;
2839 }
2840 group -= create_info.stageCount;
2841
2842 // Search libraries
2843 if (create_info.pLibraryInfo) {
2844 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2845 const PIPELINE_STATE *library_pipeline = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
2846 const uint32_t stage_count = library_pipeline->create_info.raytracing.ptr()->stageCount;
2847 if (group < stage_count) {
2848 return (library_pipeline->create_info.raytracing.ptr()->pStages[group].stage & stage) != 0;
2849 }
2850 group -= stage_count;
2851 }
2852 }
2853
2854 // group index too large
2855 return false;
2856}
2857
sourav parmarcd5fb182020-07-17 12:58:44 -07002858bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06002859 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04002860
Jeremy Gebben11af9792021-08-20 10:20:09 -06002861 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002862 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002863 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
2864 skip |=
2865 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
2866 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
2867 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
2868 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002869 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002870 if (create_info.pLibraryInfo) {
2871 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2872 const PIPELINE_STATE *library_pipelinestate = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
2873 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
2874 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002875 skip |= LogError(
2876 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
2877 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
2878 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002879 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07002880 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002881 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
2882 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
2883 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
2884 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002885 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
2886 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
2887 "member must have been created with values of the maxPipelineRayPayloadSize and "
2888 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
2889 }
2890 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002891 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002892 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
2893 "vkCreateRayTracingPipelinesKHR: If flags includes "
2894 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
2895 "the pLibraries member of libraries must have been created with the "
2896 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
2897 }
sourav parmar83c31b12020-05-06 12:30:54 -07002898 }
2899 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002900 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002901 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002902 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
2903 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
2904 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002905 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002906 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002907 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002908 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04002909
Jeremy Gebben11af9792021-08-20 10:20:09 -06002910 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002911 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04002912 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002913
Jeremy Gebben11af9792021-08-20 10:20:09 -06002914 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002915 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
2916 if (raygen_stages_count == 0) {
2917 skip |= LogError(
2918 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07002919 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002920 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
2921 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002922 }
2923
Jeremy Gebben11af9792021-08-20 10:20:09 -06002924 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04002925 const auto &group = groups[group_index];
2926
2927 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002928 if (!GroupHasValidIndex(
2929 pipeline, group.generalShader,
2930 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 -05002931 skip |= LogError(device,
2932 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
2933 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
2934 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002935 }
2936 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
2937 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002938 skip |= LogError(device,
2939 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
2940 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
2941 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002942 }
2943 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002944 if (!GroupHasValidIndex(pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002945 skip |= LogError(device,
2946 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
2947 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
2948 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002949 }
2950 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2951 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002952 skip |= LogError(device,
2953 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
2954 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
2955 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002956 }
2957 }
2958
2959 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
2960 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002961 if (!GroupHasValidIndex(pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002962 skip |= LogError(device,
2963 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
2964 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
2965 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002966 }
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002967 if (!GroupHasValidIndex(pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002968 skip |= LogError(device,
2969 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
2970 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
2971 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002972 }
2973 }
John Zulaufe4474e72019-07-01 17:28:27 -06002974 }
2975 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002976}
2977
Dave Houltona9df0ce2018-02-07 10:51:23 -07002978uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07002979
Dave Houltona9df0ce2018-02-07 10:51:23 -07002980static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002981 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06002982 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06002983 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002984 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002985 return nullptr;
2986}
2987
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002988bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002989 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002990 bool skip = false;
2991 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002992
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06002993 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002994 return false;
2995 }
2996
sfricke-samsung45996a42021-09-16 13:45:27 -07002997 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002998
2999 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003000 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
3001 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3002 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003003 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003004 auto cache = GetValidationCacheInfo(pCreateInfo);
3005 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07003006 // If app isn't using a shader validation cache, use the default one from CoreChecks
3007 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003008 if (cache) {
3009 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003010 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003011 }
3012
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003013 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3014 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07003015 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06003016 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003017 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003018 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003019 spvtools::ValidatorOptions options;
3020 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06003021 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003022 if (spv_valid != SPV_SUCCESS) {
3023 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003024 if (spv_valid == SPV_WARNING) {
3025 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3026 diag && diag->error ? diag->error : "(no error text)");
3027 } else {
3028 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3029 diag && diag->error ? diag->error : "(no error text)");
3030 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003031 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003032 } else {
3033 if (cache) {
3034 cache->Insert(hash);
3035 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003036 }
3037
3038 spvDiagnosticDestroy(diag);
3039 spvContextDestroy(ctx);
3040 }
3041
Chris Forbes4ae55b32017-06-09 14:42:56 -07003042 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003043}
3044
ziga-lunarg11fecb92021-09-20 16:48:06 +02003045bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint,
3046 const PipelineStageState &stage_state) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003047 bool skip = false;
3048 uint32_t local_size_x = 0;
3049 uint32_t local_size_y = 0;
3050 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07003051 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06003052 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003053 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003054 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003055 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003056 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06003057 }
3058 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003059 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003060 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003061 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003062 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06003063 }
3064 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003065 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003066 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003067 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003068 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06003069 }
3070
3071 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3072 uint64_t invocations = local_size_x * local_size_y;
3073 // Prevent overflow.
3074 bool fail = false;
3075 if (invocations > UINT32_MAX || invocations > limit) {
3076 fail = true;
3077 }
3078 if (!fail) {
3079 invocations *= local_size_z;
3080 if (invocations > UINT32_MAX || invocations > limit) {
3081 fail = true;
3082 }
3083 }
3084 if (fail) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003085 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003086 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3087 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sfricke-samsung1ff329f2021-09-16 10:06:47 -07003088 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, local_size_y,
3089 local_size_z, limit);
Lockeaa8fdc02019-04-02 11:59:20 -06003090 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003091
3092 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3093 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
3094 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3095 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3096 skip |= LogError(
3097 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
3098 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3099 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3100 "dimension (%" PRIu32
3101 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
3102 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
3103 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3104 }
3105 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3106 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
3107 const auto *required_subgroup_size_features =
3108 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
3109 if (!required_subgroup_size_features) {
3110 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
3111 skip |= LogError(
3112 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
3113 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3114 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3115 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3116 ").",
3117 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
3118 phys_dev_props_core11.subgroupSize);
3119 }
3120 }
3121 }
Lockeaa8fdc02019-04-02 11:59:20 -06003122 }
3123 return skip;
3124}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003125
3126spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
3127 if (api_version >= VK_API_VERSION_1_2) {
3128 return SPV_ENV_VULKAN_1_2;
3129 } else if (api_version >= VK_API_VERSION_1_1) {
3130 if (spirv_1_4) {
3131 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3132 } else {
3133 return SPV_ENV_VULKAN_1_1;
3134 }
3135 }
3136 return SPV_ENV_VULKAN_1_0;
3137}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003138
sfricke-samsungecc112a2021-09-03 05:32:17 -07003139// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003140void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003141 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003142 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3143 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003144 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003145 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003146 options.SetRelaxBlockLayout(true);
3147 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003148
3149 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3150 // Vulkan version used, the feature bit is needed (also described in the spec).
3151
3152 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3153 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003154 options.SetUniformBufferStandardLayout(true);
3155 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003156 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3157 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003158 options.SetScalarBlockLayout(true);
3159 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003160 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3161 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003162 options.SetWorkgroupScalarBlockLayout(true);
3163 }
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003164 if (enabled_features.maintenance4_features.maintenance4) {
3165 // --allow-localsizeid
3166 options.SetAllowLocalSizeId(true);
3167 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003168}