blob: 1fec5bbe691e6639455a4e81879b2fda4c0b5306 [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-lunarg2818f492021-08-12 14:30:51 +0200672bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
673 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
674 bool skip = false;
675
676 std::array<uint32_t, 3> work_group_size = src->GetWorkgroupSize(pStage, id_value_map);
677
678 for (uint32_t i = 0; i < 3; ++i) {
679 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
680 const char member = 'x' + static_cast<int8_t>(i);
681 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
682 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
683 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
684 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
685 }
686 }
687 return skip;
688}
689
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600690bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600691 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200692 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
693 pStage->stage == VK_SHADER_STAGE_ALL) {
694 return false;
695 }
696
697 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700698 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200699
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700700 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200701 struct Variable {
702 uint32_t baseTypePtrID;
703 uint32_t ID;
704 uint32_t storageClass;
705 };
706 std::vector<Variable> variables;
707
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700708 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700709 bool is_iso_lines = false;
710 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500711
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700712 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600713
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200714 for (auto insn : *src) {
715 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500716 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200717 case spv::OpDecorate:
718 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500719 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700720 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200721 break;
722 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200723 default:
724 break;
725 }
726 break;
727 // Find all input and output variables
728 case spv::OpVariable: {
729 Variable var = {};
730 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600731 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
732 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700733 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200734 var.baseTypePtrID = insn.word(1);
735 var.ID = insn.word(2);
736 variables.push_back(var);
737 }
738 break;
739 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500740 case spv::OpExecutionMode:
741 if (insn.word(1) == entrypoint.word(2)) {
742 switch (insn.word(2)) {
743 default:
744 break;
745 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700746 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500747 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700748 case spv::ExecutionModeIsolines:
749 is_iso_lines = true;
750 break;
751 case spv::ExecutionModePointMode:
752 is_point_mode = true;
753 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500754 }
755 }
756 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200757 default:
758 break;
759 }
760 }
761
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500762 bool strip_output_array_level =
763 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
764 bool strip_input_array_level =
765 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
766 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
767
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700768 uint32_t num_comp_in = 0, num_comp_out = 0;
769 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600770
sfricke-samsung962cad92021-04-13 00:46:29 -0700771 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
772 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600773
774 // Find max component location used for input variables.
775 for (auto &var : inputs) {
776 int location = var.first.first;
777 int component = var.first.second;
778 interface_var &iv = var.second;
779
780 // Only need to look at the first location, since we use the type's whole size
781 if (iv.offset != 0) {
782 continue;
783 }
784
785 if (iv.is_patch) {
786 continue;
787 }
788
sfricke-samsung962cad92021-04-13 00:46:29 -0700789 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700790 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600791 }
792
793 // Find max component location used for output variables.
794 for (auto &var : outputs) {
795 int location = var.first.first;
796 int component = var.first.second;
797 interface_var &iv = var.second;
798
799 // Only need to look at the first location, since we use the type's whole size
800 if (iv.offset != 0) {
801 continue;
802 }
803
804 if (iv.is_patch) {
805 continue;
806 }
807
sfricke-samsung962cad92021-04-13 00:46:29 -0700808 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700809 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600810 }
811
812 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
813 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700814 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
815 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200816 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500817 // Check if the variable is a patch. Patches can also be members of blocks,
818 // but if they are then the top-level arrayness has already been stripped
819 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700820 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200821
822 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700823 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200824 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700825 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200826 }
827 }
828
829 switch (pStage->stage) {
830 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700831 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700832 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700833 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
834 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
835 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700836 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200837 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700838 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700839 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700840 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
841 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
842 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600843 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200844 break;
845
846 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700847 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700848 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700849 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
850 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
851 "components by %u components",
852 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700853 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200854 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700855 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600856 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700857 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700858 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
859 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
860 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600861 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700862 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
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: Tessellation control shader exceeds "
865 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
866 "components by %u components",
867 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700868 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200869 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700870 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600871 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700872 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700873 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
874 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
875 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600876 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200877 break;
878
879 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700880 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700881 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700882 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
883 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
884 "components by %u components",
885 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700886 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200887 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700888 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600889 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700890 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700891 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
892 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
893 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600894 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700895 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700896 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700897 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
898 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
899 "components by %u components",
900 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700901 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200902 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700903 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600904 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700905 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700906 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
907 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
908 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600909 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700910 // Portability validation
911 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
912 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700913 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700914 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
915 " is using abstract patch type IsoLines, but this is not supported on this platform");
916 }
917 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700918 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700919 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
920 " is using abstract patch type PointMode, but this is not supported on this platform");
921 }
922 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200923 break;
924
925 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700926 if (num_comp_in > limits.maxGeometryInputComponents) {
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: Geometry shader exceeds "
929 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
930 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700931 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200932 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700933 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700934 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700935 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
936 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
937 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600938 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700939 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700940 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700941 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
942 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
943 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700944 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200945 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700946 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700947 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700948 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
949 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
950 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600951 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700952 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700953 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700954 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
955 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
956 "components by %u components",
957 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700958 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500959 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200960 break;
961
962 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700963 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700964 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700965 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
966 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
967 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700968 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200969 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700970 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
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: Fragment shader input variable uses location that "
973 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
974 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600975 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200976 break;
977
Jeff Bolz148d94e2018-12-13 21:25:56 -0600978 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
979 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
980 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
981 case VK_SHADER_STAGE_MISS_BIT_NV:
982 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
983 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
984 case VK_SHADER_STAGE_TASK_BIT_NV:
985 case VK_SHADER_STAGE_MESH_BIT_NV:
986 break;
987
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200988 default:
989 assert(false); // This should never happen
990 }
991 return skip;
992}
993
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300994bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const {
995 bool skip = false;
996
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300997 // Got through all ImageRead/Write instructions
998 for (auto insn : *src) {
999 switch (insn.opcode()) {
1000 case spv::OpImageSparseRead:
1001 case spv::OpImageRead: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001002 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001003 if (type_def != src->end()) {
Tim Van Pattenffe91322021-07-26 10:20:50 -06001004 const auto dim = type_def.word(3);
1005 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1006 // StorageImageReadWithoutFormat Capability was declared.
1007 if (dim != spv::DimSubpassData && type_def.word(8) == spv::ImageFormatUnknown) {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001008 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1009 "shaderStorageImageReadWithoutFormat",
1010 kVUID_Features_shaderStorageImageReadWithoutFormat);
1011 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001012 }
1013 break;
1014 }
1015 case spv::OpImageWrite: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001016 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001017 if (type_def != src->end()) {
1018 if (type_def.word(8) == spv::ImageFormatUnknown) {
1019 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1020 "shaderStorageImageWriteWithoutFormat",
1021 kVUID_Features_shaderStorageImageWriteWithoutFormat);
1022 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001023 }
1024 break;
1025 }
1026
1027 }
1028 }
1029
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001030 // Go through all variables for images and check decorations
1031 for (auto insn : *src) {
1032 if (insn.opcode() != spv::OpVariable)
1033 continue;
1034
1035 uint32_t var = insn.word(2);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001036 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001037 if (type_def == src->end())
1038 continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001039 // Only check if the Image Dim operand is not SubpassData
1040 const auto dim = type_def.word(3);
1041 if (dim == spv::DimSubpassData) continue;
Corentin Wallez91f8b6d2021-07-23 10:11:31 +02001042 // Only check storage images
1043 if (type_def.word(7) != 2) continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001044 if (type_def.word(8) != spv::ImageFormatUnknown) continue;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001045
1046 decoration_set img_decorations = src->get_decorations(var);
1047
1048 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1049 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001050 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
1051 "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32
1052 " "
1053 " without format not marked a NonReadable",
1054 var);
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001055 }
1056
1057 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1058 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001059 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
1060 "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32
1061 " "
1062 "without format not marked a NonWritable",
1063 var);
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001064 }
1065 }
1066
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001067 return skip;
1068}
1069
sfricke-samsungdc96f302020-03-18 20:42:10 -07001070bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1071 bool skip = false;
1072 uint32_t total_resources = 0;
1073
1074 // Only currently testing for graphics and compute pipelines
1075 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1076 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1077 return false;
1078 }
1079
1080 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1081 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
Jeremy Gebben11af9792021-08-20 10:20:09 -06001082 total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
sfricke-samsungdc96f302020-03-18 20:42:10 -07001083 }
1084
1085 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1086 // input from CreatePipeline and CreatePipelineLayout level
1087 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1088 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1089 continue;
1090 }
1091
1092 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1093 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1094 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1095 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1096 // Check only descriptor types listed in maxPerStageResources description in spec
1097 switch (binding->descriptorType) {
1098 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1099 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1100 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1101 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1102 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1103 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1104 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1105 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1106 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1107 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1108 total_resources += binding->descriptorCount;
1109 break;
1110 default:
1111 break;
1112 }
1113 }
1114 }
1115 }
1116
1117 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1118 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1119 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001120 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001121 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1122 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1123 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1124 }
1125
1126 return skip;
1127}
1128
Jeff Bolze4356752019-03-07 11:23:46 -06001129// copy the specialization constant value into buf, if it is present
1130void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1131 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1132
1133 if (spec && spec_id < spec->mapEntryCount) {
1134 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1135 }
1136}
1137
1138// Fill in value with the constant or specialization constant value, if available.
1139// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001140static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001141 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001142 auto type_id = src->get_def(insn.word(1));
1143 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1144 return false;
1145 }
1146 switch (insn.opcode()) {
1147 case spv::OpSpecConstant:
1148 *value = insn.word(3);
1149 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1150 return true;
1151 case spv::OpConstant:
1152 *value = insn.word(3);
1153 return true;
1154 default:
1155 return false;
1156 }
1157}
1158
1159// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001160VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001161 switch (insn.opcode()) {
1162 case spv::OpTypeInt:
1163 switch (insn.word(2)) {
1164 case 8:
1165 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1166 case 16:
1167 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1168 case 32:
1169 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1170 case 64:
1171 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1172 default:
1173 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1174 }
1175 case spv::OpTypeFloat:
1176 switch (insn.word(2)) {
1177 case 16:
1178 return VK_COMPONENT_TYPE_FLOAT16_NV;
1179 case 32:
1180 return VK_COMPONENT_TYPE_FLOAT32_NV;
1181 case 64:
1182 return VK_COMPONENT_TYPE_FLOAT64_NV;
1183 default:
1184 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1185 }
1186 default:
1187 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1188 }
1189}
1190
1191// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1192// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001193bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001194 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001195 bool skip = false;
1196
1197 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001198 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001199 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001200 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001201
1202 struct CoopMatType {
1203 uint32_t scope, rows, cols;
1204 VkComponentTypeNV component_type;
1205 bool all_constant;
1206
1207 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1208
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001209 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001210 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001211 spirv_inst_iter insn = src->get_def(id);
1212 uint32_t component_type_id = insn.word(2);
1213 uint32_t scope_id = insn.word(3);
1214 uint32_t rows_id = insn.word(4);
1215 uint32_t cols_id = insn.word(5);
1216 auto component_type_iter = src->get_def(component_type_id);
1217 auto scope_iter = src->get_def(scope_id);
1218 auto rows_iter = src->get_def(rows_id);
1219 auto cols_iter = src->get_def(cols_id);
1220
1221 all_constant = true;
1222 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1223 all_constant = false;
1224 }
1225 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1226 all_constant = false;
1227 }
1228 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1229 all_constant = false;
1230 }
1231 component_type = GetComponentType(component_type_iter, src);
1232 }
1233 };
1234
1235 bool seen_coopmat_capability = false;
1236
1237 for (auto insn : *src) {
1238 // Whitelist instructions whose result can be a cooperative matrix type, and
1239 // keep track of their types. It would be nice if SPIRV-Headers generated code
1240 // to identify which instructions have a result type and result id. Lacking that,
1241 // this whitelist is based on the set of instructions that
1242 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1243 switch (insn.opcode()) {
1244 case spv::OpLoad:
1245 case spv::OpCooperativeMatrixLoadNV:
1246 case spv::OpCooperativeMatrixMulAddNV:
1247 case spv::OpSNegate:
1248 case spv::OpFNegate:
1249 case spv::OpIAdd:
1250 case spv::OpFAdd:
1251 case spv::OpISub:
1252 case spv::OpFSub:
1253 case spv::OpFDiv:
1254 case spv::OpSDiv:
1255 case spv::OpUDiv:
1256 case spv::OpMatrixTimesScalar:
1257 case spv::OpConstantComposite:
1258 case spv::OpCompositeConstruct:
1259 case spv::OpConvertFToU:
1260 case spv::OpConvertFToS:
1261 case spv::OpConvertSToF:
1262 case spv::OpConvertUToF:
1263 case spv::OpUConvert:
1264 case spv::OpSConvert:
1265 case spv::OpFConvert:
1266 id_to_type_id[insn.word(2)] = insn.word(1);
1267 break;
1268 default:
1269 break;
1270 }
1271
1272 switch (insn.opcode()) {
1273 case spv::OpDecorate:
1274 if (insn.word(2) == spv::DecorationSpecId) {
1275 id_to_spec_id[insn.word(1)] = insn.word(3);
1276 }
1277 break;
1278 case spv::OpCapability:
1279 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1280 seen_coopmat_capability = true;
1281
1282 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001283 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001284 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001285 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1286 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001287 }
1288 }
1289 break;
1290 case spv::OpMemoryModel:
1291 // If the capability isn't enabled, don't bother with the rest of this function.
1292 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1293 if (!seen_coopmat_capability) {
1294 return skip;
1295 }
1296 break;
1297 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001298 CoopMatType m;
1299 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001300
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001301 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001302 // Validate that the type parameters are all supported for one of the
1303 // operands of a cooperative matrix property.
1304 bool valid = false;
1305 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001306 if (cooperative_matrix_properties[i].AType == m.component_type &&
1307 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1308 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001309 valid = true;
1310 break;
1311 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001312 if (cooperative_matrix_properties[i].BType == m.component_type &&
1313 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1314 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001315 valid = true;
1316 break;
1317 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001318 if (cooperative_matrix_properties[i].CType == m.component_type &&
1319 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1320 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001321 valid = true;
1322 break;
1323 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001324 if (cooperative_matrix_properties[i].DType == m.component_type &&
1325 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1326 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001327 valid = true;
1328 break;
1329 }
1330 }
1331 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001332 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001333 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1334 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001335 }
1336 }
1337 break;
1338 }
1339 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001340 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001341 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1342 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1343 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1344 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001345 // Couldn't find type of matrix
1346 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001347 break;
1348 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001349 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1350 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1351 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1352 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001353
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001354 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001355 // Validate that the type parameters are all supported for the same
1356 // cooperative matrix property.
1357 bool valid = false;
1358 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001359 if (cooperative_matrix_properties[i].AType == a.component_type &&
1360 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1361 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001362
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001363 cooperative_matrix_properties[i].BType == b.component_type &&
1364 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1365 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001366
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001367 cooperative_matrix_properties[i].CType == c.component_type &&
1368 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1369 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001370
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001371 cooperative_matrix_properties[i].DType == d.component_type &&
1372 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1373 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001374 valid = true;
1375 break;
1376 }
1377 }
1378 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001379 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001380 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1381 "VkCooperativeMatrixPropertiesNV",
1382 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001383 }
1384 }
1385 break;
1386 }
1387 default:
1388 break;
1389 }
1390 }
1391
1392 return skip;
1393}
1394
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001395bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1396 const PIPELINE_STATE *pipeline) const {
1397 bool skip = false;
1398
1399 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1400 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1401 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1402 for (auto insn : *src) {
1403 switch (insn.opcode()) {
1404 case spv::OpCapability:
1405 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1406 auto subpass_flags =
1407 (pipeline->rp_state == nullptr)
1408 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001409 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001410 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1411 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001412 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001413 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1414 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1415 }
1416 }
1417 break;
1418 default:
1419 break;
1420 }
1421 }
1422 }
1423
1424 return skip;
1425}
1426
ziga-lunarg73163742021-08-25 13:15:29 +02001427bool CoreChecks::ValidateShaderSubgroupSizeControl(VkPipelineShaderStageCreateInfo const *pStage) const {
1428 bool skip = false;
1429
1430 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
1431 !enabled_features.subgroup_size_control_features.subgroupSizeControl) {
1432 skip |= LogError(
1433 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1434 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1435 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1436 }
1437
1438 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
1439 !enabled_features.subgroup_size_control_features.computeFullSubgroups) {
1440 skip |= LogError(
1441 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1442 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1443 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1444 }
1445
1446 return skip;
1447}
1448
sfricke-samsung58b84352021-07-31 21:41:04 -07001449bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *src) const {
1450 bool skip = false;
1451
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001452 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001453 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001454
sfricke-samsungf5042b12021-08-05 01:09:40 -07001455 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1456 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1457
1458 const bool valid_storage_buffer_float = (
1459 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1460 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1461 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1462 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1463 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1464 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1465 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1466 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1467 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1468
1469 const bool valid_workgroup_float = (
1470 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1471 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1472 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1473 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1474 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1475 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1476 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1477 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1478 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1479
1480 const bool valid_image_float = (
1481 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1482 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1483 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1484
1485 const bool valid_16_float = (
1486 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1487 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1488 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1489 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1490 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1491 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1492
1493 const bool valid_32_float = (
1494 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1495 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1496 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1497 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1498 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1499 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1500 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1501 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1502 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1503
1504 const bool valid_64_float = (
1505 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1506 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1507 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1508 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1509 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1510 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1511 // clang-format on
1512
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001513 for (const auto &atomic_inst : src->GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001514 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungf5042b12021-08-05 01:09:40 -07001515 const uint32_t opcode = src->at(atomic_inst.first).opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001516
1517 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001518 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001519 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1520 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001521 skip |= LogError(device, "VUID-RuntimeSpirv-None-06278",
1522 "%s: Can't use 64-bit int atomics operations (%s) with %s storage class without "
1523 "shaderBufferInt64Atomics enabled.",
1524 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode),
1525 StorageClassName(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001526 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1527 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001528 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001529 "%s: Can't use 64-bit int atomics operations (%s) with Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001530 "shaderSharedInt64Atomics enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001531 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001532 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001533 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001534 "%s: Can't use 64-bit int atomics operations (%s) with Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001535 "shaderImageInt64Atomics enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001536 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsung58b84352021-07-31 21:41:04 -07001537 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001538 } else if (atomic.type == spv::OpTypeFloat) {
1539 // Validate Floats
1540 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1541 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001542 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1543 : "VUID-RuntimeSpirv-None-06280";
1544 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001545 "%s: Can't use float atomics operations (%s) with StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001546 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1547 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1548 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1549 "shaderBufferFloat64AtomicMinMax enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001550 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001551 } else if (opcode == spv::OpAtomicFAddEXT) {
1552 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1553 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1554 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with "
1555 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
1556 report_data->FormatHandle(src->vk_shader_module()).c_str());
1557 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1558 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1559 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with "
1560 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
1561 report_data->FormatHandle(src->vk_shader_module()).c_str());
1562 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1563 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1564 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with "
1565 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
1566 report_data->FormatHandle(src->vk_shader_module()).c_str());
1567 }
1568 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1569 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
1570 skip |= LogError(
1571 device, kVUID_Core_Shader_AtomicFeature,
1572 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1573 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
1574 report_data->FormatHandle(src->vk_shader_module()).c_str());
1575 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
1576 skip |= LogError(
1577 device, kVUID_Core_Shader_AtomicFeature,
1578 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1579 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
1580 report_data->FormatHandle(src->vk_shader_module()).c_str());
1581 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
1582 skip |= LogError(
1583 device, kVUID_Core_Shader_AtomicFeature,
1584 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1585 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
1586 report_data->FormatHandle(src->vk_shader_module()).c_str());
1587 }
1588 } else {
1589 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1590 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
1591 skip |= LogError(
1592 device, kVUID_Core_Shader_AtomicFeature,
1593 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1594 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
1595 report_data->FormatHandle(src->vk_shader_module()).c_str());
1596 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
1597 skip |= LogError(
1598 device, kVUID_Core_Shader_AtomicFeature,
1599 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1600 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
1601 report_data->FormatHandle(src->vk_shader_module()).c_str());
1602 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
1603 skip |= LogError(
1604 device, kVUID_Core_Shader_AtomicFeature,
1605 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1606 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
1607 report_data->FormatHandle(src->vk_shader_module()).c_str());
1608 }
1609 }
1610 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1611 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001612 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1613 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001614 skip |=
1615 LogError(device, vuid,
1616 "%s: Can't use float atomics operations (%s) with Workgroup storage class without "
1617 "shaderSharedFloat32Atomics or "
1618 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1619 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1620 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1621 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001622 } else if (opcode == spv::OpAtomicFAddEXT) {
1623 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1624 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1625 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1626 "storage class without shaderSharedFloat16AtomicAdd enabled.",
1627 report_data->FormatHandle(src->vk_shader_module()).c_str());
1628 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1629 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1630 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1631 "storage class without shaderSharedFloat32AtomicAdd enabled.",
1632 report_data->FormatHandle(src->vk_shader_module()).c_str());
1633 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1634 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1635 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1636 "storage class without shaderSharedFloat64AtomicAdd enabled.",
1637 report_data->FormatHandle(src->vk_shader_module()).c_str());
1638 }
1639 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1640 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
1641 skip |= LogError(
1642 device, kVUID_Core_Shader_AtomicFeature,
1643 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1644 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
1645 report_data->FormatHandle(src->vk_shader_module()).c_str());
1646 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
1647 skip |= LogError(
1648 device, kVUID_Core_Shader_AtomicFeature,
1649 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1650 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
1651 report_data->FormatHandle(src->vk_shader_module()).c_str());
1652 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
1653 skip |= LogError(
1654 device, kVUID_Core_Shader_AtomicFeature,
1655 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1656 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
1657 report_data->FormatHandle(src->vk_shader_module()).c_str());
1658 }
1659 } else {
1660 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1661 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
1662 skip |= LogError(
1663 device, kVUID_Core_Shader_AtomicFeature,
1664 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1665 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat16Atomics enabled.",
1666 report_data->FormatHandle(src->vk_shader_module()).c_str());
1667 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
1668 skip |= LogError(
1669 device, kVUID_Core_Shader_AtomicFeature,
1670 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1671 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat32Atomics enabled.",
1672 report_data->FormatHandle(src->vk_shader_module()).c_str());
1673 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
1674 skip |= LogError(
1675 device, kVUID_Core_Shader_AtomicFeature,
1676 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1677 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat64Atomics enabled.",
1678 report_data->FormatHandle(src->vk_shader_module()).c_str());
1679 }
1680 }
1681 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001682 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1683 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001684 skip |= LogError(
1685 device, vuid,
1686 "%s: Can't use float atomics operations (%s) with Image storage class without shaderImageFloat32Atomics or "
1687 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
1688 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001689 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001690 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001691 "%s: Can't use 16-bit float atomics operations (%s) without shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001692 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1693 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001694 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001695 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001696 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1697 : "VUID-RuntimeSpirv-None-06335";
1698 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001699 "%s: Can't use 32-bit float atomics operations (%s) without shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001700 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1701 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1702 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1703 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001704 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001705 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001706 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1707 : "VUID-RuntimeSpirv-None-06336";
1708 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001709 "%s: Can't use 64-bit float atomics operations (%s) without shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001710 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1711 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001712 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001713 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001714 }
1715 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001716 return skip;
1717}
1718
John Zulaufac4c6e12019-07-01 16:05:58 -06001719bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001720 auto entrypoint_id = entrypoint.word(2);
1721
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001722 // The first denorm execution mode encountered, along with its bit width.
1723 // Used to check if SeparateDenormSettings is respected.
1724 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001725
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001726 // The first rounding mode encountered, along with its bit width.
1727 // Used to check if SeparateRoundingModeSettings is respected.
1728 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001729
1730 bool skip = false;
1731
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001732 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001733 uint32_t invocations = 0;
1734
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001735 const auto &execution_mode_inst = src->GetExecutionModeInstructions();
1736 auto it = execution_mode_inst.find(entrypoint_id);
1737 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001738 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001739 auto mode = insn.word(2);
1740 switch (mode) {
1741 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1742 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001743 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001744 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001745 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
1746 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device");
1747 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1748 skip |= LogError(
1749 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
1750 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device");
1751 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1752 skip |= LogError(
1753 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
1754 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001755 }
1756 break;
1757 }
1758
1759 case spv::ExecutionModeDenormPreserve: {
1760 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001761 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1762 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
1763 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device");
1764 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1765 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
1766 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device");
1767 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1768 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
1769 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001770 }
1771
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001772 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1773 // Register the first denorm execution mode found
1774 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001775 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001776 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001777 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001778 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001779 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001780 "Shader uses different denorm execution modes for 16 and 64-bit but "
1781 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001782 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001783 }
1784 break;
1785
Mike Schuchardt2df08912020-12-15 16:28:09 -08001786 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001787 break;
1788
Mike Schuchardt2df08912020-12-15 16:28:09 -08001789 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001790 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001791 "Shader uses different denorm execution modes for different bit widths but "
1792 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001793 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001794 break;
1795
1796 default:
1797 break;
1798 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001799 }
1800 break;
1801 }
1802
1803 case spv::ExecutionModeDenormFlushToZero: {
1804 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001805 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
1806 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1807 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device");
1808 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
1809 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1810 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device");
1811 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
1812 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1813 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001814 }
1815
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001816 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1817 // Register the first denorm execution mode found
1818 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001819 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001820 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001821 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001822 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001823 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001824 "Shader uses different denorm execution modes for 16 and 64-bit but "
1825 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001826 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001827 }
1828 break;
1829
Mike Schuchardt2df08912020-12-15 16:28:09 -08001830 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001831 break;
1832
Mike Schuchardt2df08912020-12-15 16:28:09 -08001833 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001834 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001835 "Shader uses different denorm execution modes for different bit widths but "
1836 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001837 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001838 break;
1839
1840 default:
1841 break;
1842 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001843 }
1844 break;
1845 }
1846
1847 case spv::ExecutionModeRoundingModeRTE: {
1848 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001849 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1850 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
1851 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device");
1852 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1853 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
1854 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device");
1855 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1856 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
1857 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001858 }
1859
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001860 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1861 // Register the first rounding mode found
1862 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001863 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001864 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001865 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001866 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001867 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001868 "Shader uses different rounding modes for 16 and 64-bit but "
1869 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001870 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001871 }
1872 break;
1873
Mike Schuchardt2df08912020-12-15 16:28:09 -08001874 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001875 break;
1876
Mike Schuchardt2df08912020-12-15 16:28:09 -08001877 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001878 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001879 "Shader uses different rounding modes for different bit widths but "
1880 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001881 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001882 break;
1883
1884 default:
1885 break;
1886 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001887 }
1888 break;
1889 }
1890
1891 case spv::ExecutionModeRoundingModeRTZ: {
1892 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001893 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
1894 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
1895 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device");
1896 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
1897 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
1898 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device");
1899 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
1900 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
1901 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001902 }
1903
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001904 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1905 // Register the first rounding mode found
1906 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001907 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001908 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001909 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001910 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001911 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001912 "Shader uses different rounding modes for 16 and 64-bit but "
1913 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001914 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001915 }
1916 break;
1917
Mike Schuchardt2df08912020-12-15 16:28:09 -08001918 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001919 break;
1920
Mike Schuchardt2df08912020-12-15 16:28:09 -08001921 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001922 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001923 "Shader uses different rounding modes for different bit widths but "
1924 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001925 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001926 break;
1927
1928 default:
1929 break;
1930 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001931 }
1932 break;
1933 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001934
1935 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001936 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001937 break;
1938 }
1939
1940 case spv::ExecutionModeInvocations: {
1941 invocations = insn.word(3);
1942 break;
1943 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001944 }
1945 }
1946 }
1947
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001948 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001949 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001950 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
1951 "Geometry shader entry point must have an OpExecutionMode instruction that "
1952 "specifies a maximum output vertex count that is greater than 0 and less "
1953 "than or equal to maxGeometryOutputVertices. "
1954 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001955 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001956 }
1957
1958 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001959 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
1960 "Geometry shader entry point must have an OpExecutionMode instruction that "
1961 "specifies an invocation count that is greater than 0 and less "
1962 "than or equal to maxGeometryShaderInvocations. "
1963 "Invocations=%d, maxGeometryShaderInvocations=%d",
1964 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001965 }
1966 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001967 return skip;
1968}
1969
Chris Forbes47567b72017-06-09 12:09:45 -07001970// For given pipelineLayout verify that the set_layout_node at slot.first
1971// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06001972static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001973 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07001974 if (!pipelineLayout) return nullptr;
1975
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001976 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07001977
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001978 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07001979}
1980
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001981// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
1982// o If there is only a vertex shader : gl_PointSize must be written when using points
1983// o If there is a geometry or tessellation shader:
1984// - If shaderTessellationAndGeometryPointSize feature is enabled:
1985// * gl_PointSize must be written in the final geometry stage
1986// - If shaderTessellationAndGeometryPointSize feature is disabled:
1987// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001988bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06001989 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001990 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
1991 return false;
1992 }
1993
1994 bool pointsize_written = false;
1995 bool skip = false;
1996
1997 // Search for PointSize built-in decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001998 for (const auto &set : src->GetBuiltinDecorationList()) {
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001999 auto insn = src->at(set.offset);
2000 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002001 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002002 if (pointsize_written) {
2003 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002004 }
2005 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002006 }
2007
2008 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002009 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002010 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002011 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002012 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2013 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002014 }
2015 } else if (!pointsize_written) {
2016 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002017 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002018 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2019 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002020 }
2021 return skip;
2022}
John Zulauf14c355b2019-06-27 16:09:37 -06002023
Tobias Hector6663c9b2020-11-05 10:18:02 +00002024bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
2025 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2026 bool primitiverate_written = false;
2027 bool viewportindex_written = false;
2028 bool viewportmask_written = false;
2029 bool skip = false;
2030
2031 // Check if the primitive shading rate is written
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002032 for (const auto &set : src->GetBuiltinDecorationList()) {
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002033 auto insn = src->at(set.offset);
2034 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002035 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002036 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002037 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002038 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002039 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002040 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002041 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2042 break;
2043 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002044 }
2045
Tony-LunarGd44844c2021-01-22 13:24:37 -07002046 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002047 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002048 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002049 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002050 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002051 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2052 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2053 "multiple viewports "
2054 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2055 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002056 }
2057
2058 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002059 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002060 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2061 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2062 "ViewportIndex built-ins,"
2063 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2064 string_VkShaderStageFlagBits(stage));
2065 }
2066
2067 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002068 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002069 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2070 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2071 "ViewportMaskNV built-ins,"
2072 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2073 string_VkShaderStageFlagBits(stage));
2074 }
2075 }
2076 return skip;
2077}
2078
ziga-lunargce66e542021-09-19 00:11:14 +02002079bool CoreChecks::ValidateDecorations(SHADER_MODULE_STATE const* module) const {
2080 bool skip = false;
2081
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002082 for (const auto &op_decorate : module->GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002083 uint32_t decoration = op_decorate.word(2);
2084 if (decoration == spv::DecorationXfbStride) {
2085 uint32_t stride = op_decorate.word(3);
2086 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2087 skip |= LogError(
2088 device, "VUID-RuntimeSpirv-XfbStride-06313",
2089 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2090 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2091 ").",
2092 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2093 }
2094 }
2095 }
2096
2097 return skip;
2098}
2099
ziga-lunarg28d08792021-10-13 15:42:59 +02002100bool CoreChecks::ValidateTransformFeedback(SHADER_MODULE_STATE const *src) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002101 bool skip = false;
2102
ziga-lunarg28d08792021-10-13 15:42:59 +02002103 // Temp workaround to prevent false positive errors
2104 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
2105 if (src->HasMultipleEntryPoints()) {
2106 return skip;
2107 }
2108
2109 layer_data::unordered_set<uint32_t> emitted_streams;
2110 bool output_points = false;
2111 for (const auto& insn : *src) {
2112 const uint32_t opcode = insn.opcode();
2113 if (opcode == spv::OpEmitStreamVertex) {
2114 emitted_streams.emplace(static_cast<uint32_t>(src->GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002115 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002116 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
2117 uint32_t stream = static_cast<uint32_t>(src->GetConstantValueById(insn.word(1)));
2118 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2119 skip |= LogError(
2120 device, "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002121 "vkCreateGraphicsPipelines(): shader uses transform feedback stream (%s) with index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002122 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2123 ").",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002124 string_SpvOpcode(opcode), stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002125 }
2126 }
2127 if (opcode == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeOutputPoints) {
2128 output_points = true;
2129 }
2130 }
2131
2132 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2133 if (emitted_streams_size > 1 && !output_points &&
2134 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
2135 skip |= LogError(
2136 device, "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2137 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles is VK_FALSE, but execution mode is not OutputPoints.",
2138 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002139 }
2140
2141 return skip;
2142}
2143
ziga-lunarga12c75a2021-09-16 16:36:16 +02002144bool CoreChecks::ValidateTexelGatherOffset(SHADER_MODULE_STATE const *src, spirv_inst_iter &insn) const {
2145 bool skip = false;
2146
2147 const uint32_t opcode = insn.opcode();
sfricke-samsung0ab15d42021-10-26 22:49:34 -07002148 if (ImageGatherOperation(opcode)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002149 uint32_t image_operand_position = ImageOperandsParam(opcode);
2150 // Image operands are optional
2151 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2152 auto image_operand = insn.word(image_operand_position);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002153 // Bits we are validating
2154 uint32_t offset_bits =
2155 spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask;
2156 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002157 // Operand values follow
2158 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002159 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2160 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2161 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2162 if (image_operand & i) { // If the bit is set, consume operand
2163 if (insn.len() > index && (i & offset_bits)) {
2164 uint32_t constant_id = insn.word(index);
2165 const auto &constant = src->get_def(constant_id);
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002166 const bool is_dynamic_offset = constant == src->end();
2167 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002168 for (uint32_t j = 3; j < constant.len(); ++j) {
2169 uint32_t comp_id = constant.word(j);
2170 const auto &comp = src->get_def(comp_id);
sfricke-samsungef3fe742021-10-06 10:51:34 -07002171 const auto &comp_type = src->get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002172 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002173 const uint32_t offset = comp.word(3);
2174 const int32_t signed_offset = static_cast<int32_t>(offset);
2175 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2176
2177 // spec requires minTexelGatherOffset to be -8 or less so never can compare if unsigned
2178 // spec requires maxTexelGatherOffset to be 7 or greater so never can compare if signed is less
2179 // then zero
2180 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002181 skip |= LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002182 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002183 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002184 string_SpvOpcode(opcode), signed_offset,
2185 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsungef3fe742021-10-06 10:51:34 -07002186 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2187 (!use_signed || (use_signed && signed_offset > 0))) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002188 skip |=
2189 LogError(device, "VUID-RuntimeSpirv-OpImage-06377",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002190 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIu32
ziga-lunarga12c75a2021-09-16 16:36:16 +02002191 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32 ").",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002192 string_SpvOpcode(opcode), offset, phys_dev_props.limits.maxTexelGatherOffset);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002193 }
2194 }
2195 }
2196 }
2197 index += src->ImageOperandsCount(i);
2198 }
2199 }
2200 }
2201 }
2202 }
2203
2204 return skip;
2205}
2206
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002207bool CoreChecks::ValidateShaderClock(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002208 bool skip = false;
2209
sfricke-samsung94167ca2021-02-26 04:14:59 -08002210 switch (insn.opcode()) {
2211 case spv::OpReadClockKHR: {
2212 auto scope_id = module->get_def(insn.word(3));
2213 auto scope_type = scope_id.word(3);
2214 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002215 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002216 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002217 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002218 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002219 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002220 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002221 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002222 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002223 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002224 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002225 }
2226 }
2227 return skip;
2228}
2229
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002230bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2231 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002232 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002233 const auto *pStage = stage_state.create_info;
2234 const auto *module = stage_state.module.get();
2235 const auto &entrypoint = stage_state.entrypoint;
John Zulauf14c355b2019-06-27 16:09:37 -06002236 // Check the module
2237 if (!module->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002238 skip |= LogError(
2239 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2240 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002241 }
2242
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002243 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2244 // specializations should be applied and validated.
2245 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002246 pStage->pSpecializationInfo->pMapEntries != nullptr && module->HasSpecConstants()) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002247 // Gather the specialization-constant values.
2248 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002249 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002250 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 -06002251 id_value_map.reserve(specialization_info->mapEntryCount);
2252 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2253 auto const &map_entry = specialization_info->pMapEntries[i];
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002254 const auto itr = module->GetSpecConstMap().find(map_entry.constantID);
sfricke-samsung033b0262021-07-09 00:53:06 -07002255 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2256 // behavior of the pipeline."
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002257 if (itr != module->GetSpecConstMap().cend()) {
sfricke-samsung033b0262021-07-09 00:53:06 -07002258 // Make sure map_entry.size matches the spec constant's size
2259 uint32_t spec_const_size = decoration_set::kInvalidValue;
2260 const auto def_ins = module->get_def(itr->second);
2261 const auto type_ins = module->get_def(def_ins.word(1));
2262 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2263 switch (type_ins.opcode()) {
2264 case spv::OpTypeBool:
2265 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2266 spec_const_size = sizeof(VkBool32);
2267 break;
2268 case spv::OpTypeInt:
2269 case spv::OpTypeFloat:
2270 spec_const_size = type_ins.word(2) / 8;
2271 break;
2272 default:
2273 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2274 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2275 break;
2276 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002277
sfricke-samsung033b0262021-07-09 00:53:06 -07002278 if (map_entry.size != spec_const_size) {
2279 skip |=
2280 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002281 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2282 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2283 map_entry.constantID, i, map_entry.size,
2284 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002285 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002286 }
2287
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002288 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002289 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2290 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2291 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2292 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2293 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2294
2295 std::copy(start_in_p, end_in_p, out_p);
2296 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002297 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002298 }
2299
sfricke-samsung5628f982021-10-19 09:21:59 -07002300 // both spirv-opt and spirv-val will use the same flags
2301 spvtools::ValidatorOptions options;
2302 AdjustValidatorOptions(device_extensions, enabled_features, options);
2303
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002304 // Apply the specialization-constant values and revalidate the shader module.
sfricke-samsung45996a42021-09-16 13:45:27 -07002305 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002306 spvtools::Optimizer optimizer(spirv_environment);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002307 spvtools::MessageConsumer consumer = [&skip, &module, &stage_state, this](spv_message_level_t level, const char *source,
2308 const spv_position_t &position,
2309 const char *message) {
2310 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2311 "%s does not contain valid spirv for stage %s. %s",
2312 report_data->FormatHandle(module->vk_shader_module()).c_str(),
2313 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002314 };
2315 optimizer.SetMessageConsumer(consumer);
2316 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2317 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2318 std::vector<uint32_t> specialized_spirv;
sfricke-samsung5628f982021-10-19 09:21:59 -07002319 auto const optimized = optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002320 assert(optimized == true);
2321
2322 if (optimized) {
2323 spv_context ctx = spvContextCreate(spirv_environment);
2324 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2325 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002326 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2327 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002328 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002329 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002330 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002331 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002332 }
2333
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002334 spvDiagnosticDestroy(diag);
2335 spvContextDestroy(ctx);
2336 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002337
2338 skip |= ValidateWorkgroupSize(module, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002339 }
2340
John Zulauf14c355b2019-06-27 16:09:37 -06002341 // Check the entrypoint
2342 if (entrypoint == module->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002343 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2344 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002345 }
2346 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2347
2348 // Mark accessible ids
2349 auto &accessible_ids = stage_state.accessible_ids;
2350
Chris Forbes47567b72017-06-09 12:09:45 -07002351 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002352
sfricke-samsung94167ca2021-02-26 04:14:59 -08002353 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2354 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002355 uint32_t total_shared_size = 0;
sfricke-samsung94167ca2021-02-26 04:14:59 -08002356 for (auto insn : *module) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002357 skip |= ValidateTexelGatherOffset(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002358 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002359 skip |= ValidateShaderClock(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002360 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02002361 total_shared_size += module->CalcComputeSharedMemory(pStage->stage, insn);
2362 }
2363
2364 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2365 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002366 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002367 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002368 }
2369
ziga-lunarg28d08792021-10-13 15:42:59 +02002370 skip |= ValidateTransformFeedback(module);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002371 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2372 stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002373 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03002374 skip |= ValidateShaderStorageImageFormats(module);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002375 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsung58b84352021-07-31 21:41:04 -07002376 skip |= ValidateAtomicsTypes(module);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002377 skip |= ValidateExecutionModes(module, entrypoint);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002378 skip |= ValidateSpecializations(pStage);
ziga-lunargce66e542021-09-19 00:11:14 +02002379 skip |= ValidateDecorations(module);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002380 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002381 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002382 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07002383 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002384 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
2385 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
2386 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002387 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
2388 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
2389 }
sfricke-samsung45996a42021-09-16 13:45:27 -07002390 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002391 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
2392 }
ziga-lunarg73163742021-08-25 13:15:29 +02002393 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
2394 skip |= ValidateShaderSubgroupSizeControl(pStage);
2395 }
Chris Forbes47567b72017-06-09 12:09:45 -07002396
sfricke-samsung7699b912021-04-12 23:01:51 -07002397 // "layout must be consistent with the layout of the * shader"
2398 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002399 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002400 switch (pipeline->create_info.graphics.sType) {
2401 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2402 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2403 break;
2404 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2405 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2406 break;
2407 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2408 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2409 break;
2410 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2411 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2412 break;
2413 default:
2414 assert(false);
2415 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002416 }
2417
sfricke-samsung7699b912021-04-12 23:01:51 -07002418 // Validate Push Constants use
2419 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
2420
Chris Forbes47567b72017-06-09 12:09:45 -07002421 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002422 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002423 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002424 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002425 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002426 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2427 std::set<uint32_t> descriptor_types =
2428 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002429
2430 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002431 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002432 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002433 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002434 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002435 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002436 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2437 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002438 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2439 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002440 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002441 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2442 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002443 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002444 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002445 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002446 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002447 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002448 }
2449 }
2450
2451 // Validate use of input attachments against subpass structure
2452 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002453 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002454
Petr Krause91f7a12017-12-14 20:57:36 +01002455 auto rpci = pipeline->rp_state->createInfo.ptr();
Jeremy Gebben11af9792021-08-20 10:20:09 -06002456 auto subpass = pipeline->create_info.graphics.subpass;
Chris Forbes47567b72017-06-09 12:09:45 -07002457
2458 for (auto use : input_attachment_uses) {
2459 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2460 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07002461 ? input_attachments[use.first].attachment
2462 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002463
2464 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002465 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2466 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsung962cad92021-04-13 00:46:29 -07002467 } else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002468 skip |=
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002469 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2470 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
sfricke-samsung962cad92021-04-13 00:46:29 -07002471 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002472 }
2473 }
2474 }
Lockeaa8fdc02019-04-02 11:59:20 -06002475 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02002476 skip |= ValidateComputeWorkGroupSizes(module, entrypoint, stage_state);
Lockeaa8fdc02019-04-02 11:59:20 -06002477 }
ziga-lunarg73163742021-08-25 13:15:29 +02002478
Chris Forbes47567b72017-06-09 12:09:45 -07002479 return skip;
2480}
2481
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002482bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2483 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2484 spirv_inst_iter consumer_entrypoint,
2485 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002486 bool skip = false;
2487
2488 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002489 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2490 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002491
2492 auto a_it = outputs.begin();
2493 auto b_it = inputs.begin();
2494
ziga-lunarg8346fe82021-08-22 17:30:50 +02002495 uint32_t a_component = 0;
2496 uint32_t b_component = 0;
2497
Chris Forbes47567b72017-06-09 12:09:45 -07002498 // Maps sorted by key (location); walk them together to find mismatches
2499 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2500 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2501 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2502 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2503 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2504
ziga-lunarg8346fe82021-08-22 17:30:50 +02002505 a_first.second += a_component;
2506 b_first.second += b_component;
2507
2508 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2509 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2510 assert(a_at_end || a_component < a_length);
2511 assert(b_at_end || b_component < b_length);
2512
Chris Forbes47567b72017-06-09 12:09:45 -07002513 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002514 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002515 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2516 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2517 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2518 a_it++;
2519 a_component = 0;
2520 } else {
2521 a_component++;
2522 }
Chris Forbes47567b72017-06-09 12:09:45 -07002523 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002524 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002525 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2526 b_first.first, b_first.second, producer_stage->name);
2527 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2528 b_it++;
2529 b_component = 0;
2530 } else {
2531 b_component++;
2532 }
Chris Forbes47567b72017-06-09 12:09:45 -07002533 } else {
2534 // subtleties of arrayed interfaces:
2535 // - if is_patch, then the member is not arrayed, even though the interface may be.
2536 // - if is_block_member, then the extra array level of an arrayed interface is not
2537 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002538 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002539 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002540 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002541 producer->DescribeType(a_it->second.type_id).c_str(),
2542 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002543 a_it++;
2544 b_it++;
2545 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002546 }
2547 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002548 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002549 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2550 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2551 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002552 }
2553 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002554 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002555 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2556 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002557 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002558 uint32_t a_remaining = a_length - a_component;
2559 uint32_t b_remaining = b_length - b_component;
2560 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2561 a_it++;
2562 b_it++;
2563 a_component = 0;
2564 b_component = 0;
2565 } else if (a_remaining > b_remaining) { // a has more components remaining
2566 a_component += b_remaining;
2567 b_component = 0;
2568 b_it++;
2569 } else if (b_remaining > a_remaining) { // b has more components remaining
2570 b_component += a_remaining;
2571 a_component = 0;
2572 a_it++;
2573 }
Chris Forbes47567b72017-06-09 12:09:45 -07002574 }
2575 }
2576
Ari Suonpaa696b3432019-03-11 14:02:57 +02002577 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002578 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2579 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002580
2581 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2582 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002583 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002584 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002585 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2586 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002587 } else {
2588 auto it_producer = builtins_producer.begin();
2589 auto it_consumer = builtins_consumer.begin();
2590 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2591 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002592 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002593 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2594 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002595 break;
2596 }
2597 it_producer++;
2598 it_consumer++;
2599 }
2600 }
2601 }
2602 }
2603
Chris Forbes47567b72017-06-09 12:09:45 -07002604 return skip;
2605}
2606
John Zulauf14c355b2019-06-27 16:09:37 -06002607static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002608 uint32_t stage_mask = 0;
2609 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2610 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2611 stage_mask |= pCreateInfo->pStages[i].stage;
2612 }
2613 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002614 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2615 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2616 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002617 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2618 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2619 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2620 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2621 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002622 }
2623 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002624 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002625}
2626
Chris Forbes47567b72017-06-09 12:09:45 -07002627// Validate that the shaders used by the given pipeline and store the active_slots
2628// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002629bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002630 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002631
Chris Forbes47567b72017-06-09 12:09:45 -07002632 bool skip = false;
2633
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002634 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002635
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002636 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
2637 for (auto &stage : pipeline->stage_state) {
2638 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
2639 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
2640 vertex_stage = &stage;
2641 }
2642 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
2643 fragment_stage = &stage;
2644 }
Chris Forbes47567b72017-06-09 12:09:45 -07002645 }
2646
2647 // if the shader stages are no good individually, cross-stage validation is pointless.
2648 if (skip) return true;
2649
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002650 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002651
2652 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002653 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002654 }
2655
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002656 if (vertex_stage && vertex_stage->module->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
2657 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07002658 }
2659
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002660 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
2661 const auto &producer = pipeline->stage_state[i - 1];
2662 const auto &consumer = pipeline->stage_state[i];
2663 assert(producer.module);
2664 if (&producer == fragment_stage) {
2665 break;
2666 }
2667 if (consumer.module) {
2668 if (consumer.module->has_valid_spirv && producer.module->has_valid_spirv) {
2669 auto producer_id = GetShaderStageId(producer.stage_flag);
2670 auto consumer_id = GetShaderStageId(consumer.stage_flag);
2671 skip |=
2672 ValidateInterfaceBetweenStages(producer.module.get(), producer.entrypoint, &shader_stage_attribs[producer_id],
2673 consumer.module.get(), consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002674 }
Chris Forbes47567b72017-06-09 12:09:45 -07002675
Chris Forbes47567b72017-06-09 12:09:45 -07002676 }
2677 }
2678
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002679 if (fragment_stage && fragment_stage->module->has_valid_spirv) {
2680 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002681 create_info->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07002682 }
2683
2684 return skip;
2685}
2686
Tony-LunarGb2ded512021-02-02 16:03:30 -07002687void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002688 if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports ||
2689 !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) {
2690 return;
2691 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002692
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002693 for (auto &stage : pipeline_state->stage_state) {
2694 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2695 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002696 bool primitiverate_written = false;
Tobias Hector6663c9b2020-11-05 10:18:02 +00002697
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002698 for (const auto &set : stage.module->GetBuiltinDecorationList()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002699 auto insn = stage.module->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002700 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002701 primitiverate_written = stage.module->IsBuiltInWritten(insn, stage.entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002702 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002703 if (primitiverate_written) {
2704 break;
2705 }
Tony-LunarGb2ded512021-02-02 16:03:30 -07002706 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002707
Tony-LunarGb2ded512021-02-02 16:03:30 -07002708 if (primitiverate_written) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002709 pipeline_state->wrote_primitive_shading_rate.insert(stage.stage_flag);
Tony-LunarGb2ded512021-02-02 16:03:30 -07002710 }
2711 }
2712 }
2713}
2714
2715bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2716 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002717 bool skip = false;
2718
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002719 for (auto &stage : pipeline->stage_state) {
2720 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2721 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002722 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2723 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002724 if (pipeline->wrote_primitive_shading_rate.find(stage.stage_flag) != pipeline->wrote_primitive_shading_rate.end()) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002725 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002726 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002727 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2728 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2729 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002730 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002731 }
2732 }
2733 }
2734 }
2735
2736 return skip;
2737}
2738
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002739bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002740 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07002741}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002742
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002743uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2744 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002745 const auto &create_info = pipeline->create_info.raytracing;
2746 const auto *stages = create_info.ptr()->pStages;
2747 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002748 if (stages[stage_index].stage == stageBit) {
2749 total++;
2750 }
2751 }
2752
Jeremy Gebben11af9792021-08-20 10:20:09 -06002753 if (create_info.pLibraryInfo) {
2754 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2755 const PIPELINE_STATE *library_pipeline = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002756 total += CalcShaderStageCount(library_pipeline, stageBit);
2757 }
2758 }
2759
2760 return total;
2761}
2762
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002763bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE *pipeline, uint32_t group, uint32_t stage) const {
2764 if (group == VK_SHADER_UNUSED_NV) {
2765 return true;
2766 }
2767
2768 const auto &create_info = pipeline->create_info.raytracing;
2769 const auto *stages = create_info.ptr()->pStages;
2770
2771 if (group < create_info.stageCount) {
2772 return (stages[group].stage & stage) != 0;
2773 }
2774 group -= create_info.stageCount;
2775
2776 // Search libraries
2777 if (create_info.pLibraryInfo) {
2778 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2779 const PIPELINE_STATE *library_pipeline = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
2780 const uint32_t stage_count = library_pipeline->create_info.raytracing.ptr()->stageCount;
2781 if (group < stage_count) {
2782 return (library_pipeline->create_info.raytracing.ptr()->pStages[group].stage & stage) != 0;
2783 }
2784 group -= stage_count;
2785 }
2786 }
2787
2788 // group index too large
2789 return false;
2790}
2791
sourav parmarcd5fb182020-07-17 12:58:44 -07002792bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06002793 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04002794
Jeremy Gebben11af9792021-08-20 10:20:09 -06002795 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002796 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002797 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
2798 skip |=
2799 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
2800 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
2801 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
2802 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002803 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002804 if (create_info.pLibraryInfo) {
2805 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2806 const PIPELINE_STATE *library_pipelinestate = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
2807 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
2808 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002809 skip |= LogError(
2810 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
2811 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
2812 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002813 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07002814 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002815 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
2816 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
2817 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
2818 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002819 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
2820 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
2821 "member must have been created with values of the maxPipelineRayPayloadSize and "
2822 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
2823 }
2824 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002825 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002826 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
2827 "vkCreateRayTracingPipelinesKHR: If flags includes "
2828 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
2829 "the pLibraries member of libraries must have been created with the "
2830 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
2831 }
sourav parmar83c31b12020-05-06 12:30:54 -07002832 }
2833 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002834 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002835 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002836 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
2837 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
2838 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002839 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002840 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002841 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002842 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04002843
Jeremy Gebben11af9792021-08-20 10:20:09 -06002844 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002845 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04002846 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002847
Jeremy Gebben11af9792021-08-20 10:20:09 -06002848 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002849 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
2850 if (raygen_stages_count == 0) {
2851 skip |= LogError(
2852 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07002853 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002854 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
2855 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002856 }
2857
Jeremy Gebben11af9792021-08-20 10:20:09 -06002858 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04002859 const auto &group = groups[group_index];
2860
2861 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002862 if (!GroupHasValidIndex(
2863 pipeline, group.generalShader,
2864 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 -05002865 skip |= LogError(device,
2866 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
2867 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
2868 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002869 }
2870 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
2871 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002872 skip |= LogError(device,
2873 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
2874 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
2875 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002876 }
2877 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002878 if (!GroupHasValidIndex(pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002879 skip |= LogError(device,
2880 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
2881 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
2882 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002883 }
2884 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2885 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002886 skip |= LogError(device,
2887 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
2888 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
2889 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002890 }
2891 }
2892
2893 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
2894 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002895 if (!GroupHasValidIndex(pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002896 skip |= LogError(device,
2897 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
2898 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
2899 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002900 }
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002901 if (!GroupHasValidIndex(pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002902 skip |= LogError(device,
2903 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
2904 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
2905 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002906 }
2907 }
John Zulaufe4474e72019-07-01 17:28:27 -06002908 }
2909 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002910}
2911
Dave Houltona9df0ce2018-02-07 10:51:23 -07002912uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07002913
Dave Houltona9df0ce2018-02-07 10:51:23 -07002914static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002915 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06002916 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06002917 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002918 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002919 return nullptr;
2920}
2921
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002922bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002923 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002924 bool skip = false;
2925 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002926
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06002927 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002928 return false;
2929 }
2930
sfricke-samsung45996a42021-09-16 13:45:27 -07002931 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002932
2933 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002934 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
2935 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
2936 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002937 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07002938 auto cache = GetValidationCacheInfo(pCreateInfo);
2939 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07002940 // If app isn't using a shader validation cache, use the default one from CoreChecks
2941 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002942 if (cache) {
2943 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002944 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07002945 }
2946
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002947 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
2948 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07002949 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06002950 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002951 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07002952 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002953 spvtools::ValidatorOptions options;
2954 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06002955 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002956 if (spv_valid != SPV_SUCCESS) {
2957 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002958 if (spv_valid == SPV_WARNING) {
2959 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2960 diag && diag->error ? diag->error : "(no error text)");
2961 } else {
2962 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2963 diag && diag->error ? diag->error : "(no error text)");
2964 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002965 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002966 } else {
2967 if (cache) {
2968 cache->Insert(hash);
2969 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002970 }
2971
2972 spvDiagnosticDestroy(diag);
2973 spvContextDestroy(ctx);
2974 }
2975
Chris Forbes4ae55b32017-06-09 14:42:56 -07002976 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07002977}
2978
ziga-lunarg11fecb92021-09-20 16:48:06 +02002979bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint,
2980 const PipelineStageState &stage_state) const {
Lockeaa8fdc02019-04-02 11:59:20 -06002981 bool skip = false;
2982 uint32_t local_size_x = 0;
2983 uint32_t local_size_y = 0;
2984 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07002985 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06002986 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07002987 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002988 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002989 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002990 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06002991 }
2992 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07002993 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002994 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002995 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002996 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06002997 }
2998 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07002999 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003000 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003001 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003002 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06003003 }
3004
3005 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3006 uint64_t invocations = local_size_x * local_size_y;
3007 // Prevent overflow.
3008 bool fail = false;
3009 if (invocations > UINT32_MAX || invocations > limit) {
3010 fail = true;
3011 }
3012 if (!fail) {
3013 invocations *= local_size_z;
3014 if (invocations > UINT32_MAX || invocations > limit) {
3015 fail = true;
3016 }
3017 }
3018 if (fail) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003019 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003020 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3021 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sfricke-samsung1ff329f2021-09-16 10:06:47 -07003022 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, local_size_y,
3023 local_size_z, limit);
Lockeaa8fdc02019-04-02 11:59:20 -06003024 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003025
3026 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3027 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
3028 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3029 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3030 skip |= LogError(
3031 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
3032 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3033 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3034 "dimension (%" PRIu32
3035 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
3036 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
3037 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3038 }
3039 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3040 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
3041 const auto *required_subgroup_size_features =
3042 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
3043 if (!required_subgroup_size_features) {
3044 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
3045 skip |= LogError(
3046 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
3047 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3048 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3049 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3050 ").",
3051 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
3052 phys_dev_props_core11.subgroupSize);
3053 }
3054 }
3055 }
Lockeaa8fdc02019-04-02 11:59:20 -06003056 }
3057 return skip;
3058}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003059
3060spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
3061 if (api_version >= VK_API_VERSION_1_2) {
3062 return SPV_ENV_VULKAN_1_2;
3063 } else if (api_version >= VK_API_VERSION_1_1) {
3064 if (spirv_1_4) {
3065 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3066 } else {
3067 return SPV_ENV_VULKAN_1_1;
3068 }
3069 }
3070 return SPV_ENV_VULKAN_1_0;
3071}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003072
sfricke-samsungecc112a2021-09-03 05:32:17 -07003073// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003074void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003075 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003076 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3077 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003078 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003079 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003080 options.SetRelaxBlockLayout(true);
3081 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003082
3083 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3084 // Vulkan version used, the feature bit is needed (also described in the spec).
3085
3086 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3087 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003088 options.SetUniformBufferStandardLayout(true);
3089 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003090 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3091 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003092 options.SetScalarBlockLayout(true);
3093 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003094 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3095 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003096 options.SetWorkgroupScalarBlockLayout(true);
3097 }
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003098 if (enabled_features.maintenance4_features.maintenance4) {
3099 // --allow-localsizeid
3100 options.SetAllowLocalSizeId(true);
3101 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003102}