blob: 4164665aa75a2474fada98c661e9572a43cd5f17 [file] [log] [blame]
Lionel Landwerlin2d9f5632022-01-08 01:12:47 +02001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
4 * Copyright (C) 2015-2022 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
sjfricke4f600c82022-06-09 14:21:32 +090051static const spirv_inst_iter GetBaseTypeIter(const SHADER_MODULE_STATE &module_state, uint32_t type) {
52 const auto &insn = module_state.get_def(type);
53 const uint32_t base_insn_id = module_state.GetBaseType(insn);
sjfricke6086f792022-08-25 16:38:15 +090054 // Will return end() if an invalid/unknown base_insn_id is returned
sjfricke4f600c82022-06-09 14:21:32 +090055 return module_state.get_def(base_insn_id);
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020056}
57
sjfricke4f600c82022-06-09 14:21:32 +090058static bool BaseTypesMatch(const SHADER_MODULE_STATE &a, const SHADER_MODULE_STATE &b, const spirv_inst_iter &a_base_insn,
ziga-lunarg8346fe82021-08-22 17:30:50 +020059 const spirv_inst_iter &b_base_insn) {
sjfricke6086f792022-08-25 16:38:15 +090060 if (a_base_insn == a.end() || b_base_insn == b.end()) {
61 return false;
62 }
ziga-lunarg8346fe82021-08-22 17:30:50 +020063 const uint32_t a_opcode = a_base_insn.opcode();
64 const uint32_t b_opcode = b_base_insn.opcode();
65 if (a_opcode == b_opcode) {
66 if (a_opcode == spv::OpTypeInt) {
67 // Match width and signedness
68 return a_base_insn.word(2) == b_base_insn.word(2) && a_base_insn.word(3) == b_base_insn.word(3);
69 } else if (a_opcode == spv::OpTypeFloat) {
70 // Match width
71 return a_base_insn.word(2) == b_base_insn.word(2);
sjfricke10f74a82022-08-18 18:12:56 +090072 } else if (a_opcode == spv::OpTypeBool) {
73 return true;
ziga-lunarg8346fe82021-08-22 17:30:50 +020074 } else if (a_opcode == spv::OpTypeStruct) {
75 // Match on all element types
76 if (a_base_insn.len() != b_base_insn.len()) {
77 return false; // Structs cannot match if member counts differ
78 }
79
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020080 for (uint32_t i = 2; i < a_base_insn.len(); i++) {
81 const auto &c_base_insn = GetBaseTypeIter(a, a_base_insn.word(i));
82 const auto &d_base_insn = GetBaseTypeIter(b, b_base_insn.word(i));
83 if (!BaseTypesMatch(a, b, c_base_insn, d_base_insn)) {
ziga-lunarg8346fe82021-08-22 17:30:50 +020084 return false;
85 }
86 }
87
88 return true;
89 }
90 }
91 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070092}
93
sjfricke4f600c82022-06-09 14:21:32 +090094static bool TypesMatch(const SHADER_MODULE_STATE &a, const SHADER_MODULE_STATE &b, uint32_t a_type, uint32_t b_type) {
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020095 const auto &a_base_insn = GetBaseTypeIter(a, a_type);
96 const auto &b_base_insn = GetBaseTypeIter(b, b_type);
Chris Forbes47567b72017-06-09 12:09:45 -070097
ziga-lunarg8346fe82021-08-22 17:30:50 +020098 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070099}
100
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800101static uint32_t GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -0700102 switch (format) {
103 case VK_FORMAT_R64G64B64A64_SFLOAT:
104 case VK_FORMAT_R64G64B64A64_SINT:
105 case VK_FORMAT_R64G64B64A64_UINT:
106 case VK_FORMAT_R64G64B64_SFLOAT:
107 case VK_FORMAT_R64G64B64_SINT:
108 case VK_FORMAT_R64G64B64_UINT:
109 return 2;
110 default:
111 return 1;
112 }
113}
114
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800115static uint32_t GetFormatType(VkFormat fmt) {
sfricke-samsunge3086292021-11-18 23:02:35 -0800116 if (FormatIsSINT(fmt)) return FORMAT_TYPE_SINT;
117 if (FormatIsUINT(fmt)) return FORMAT_TYPE_UINT;
sfricke-samsunged028b02021-09-06 23:14:51 -0700118 // Formats such as VK_FORMAT_D16_UNORM_S8_UINT are both
Dave Houltona9df0ce2018-02-07 10:51:23 -0700119 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
120 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700121 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
122 return FORMAT_TYPE_FLOAT;
123}
124
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600125static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700126 uint32_t bit_pos = uint32_t(u_ffs(stage));
127 return bit_pos - 1;
128}
129
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700130bool CoreChecks::ValidateViConsistency(safe_VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700131 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
132 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700133 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700134 bool skip = false;
135
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800136 for (uint32_t i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes47567b72017-06-09 12:09:45 -0700137 auto desc = &vi->pVertexBindingDescriptions[i];
138 auto &binding = bindings[desc->binding];
139 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600140 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700141 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
142 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700143 } else {
144 binding = desc;
145 }
146 }
147
148 return skip;
149}
150
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700151bool CoreChecks::ValidateViAgainstVsInputs(safe_VkPipelineVertexInputStateCreateInfo const *vi,
sjfricke4f600c82022-06-09 14:21:32 +0900152 const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700153 bool skip = false;
154
sjfricke4f600c82022-06-09 14:21:32 +0900155 const auto inputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700156
157 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200158 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700159 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200160 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
161 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
162 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700163 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
164 }
165 }
166 }
167
Petr Kraus25810d02019-08-27 17:41:15 +0200168 struct AttribInputPair {
169 const VkVertexInputAttributeDescription *attrib = nullptr;
170 const interface_var *input = nullptr;
171 };
172 std::map<uint32_t, AttribInputPair> location_map;
173 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
174 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700175
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400176 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200177 const auto location = location_it.first;
178 const auto attrib = location_it.second.attrib;
179 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600180
Petr Kraus25810d02019-08-27 17:41:15 +0200181 if (attrib && !input) {
sjfricke4f600c82022-06-09 14:21:32 +0900182 skip |= LogPerformanceWarning(module_state.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700183 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200184 } else if (!attrib && input) {
sjfricke4f600c82022-06-09 14:21:32 +0900185 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700186 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200187 } else if (attrib && input) {
188 const auto attrib_type = GetFormatType(attrib->format);
sjfricke4f600c82022-06-09 14:21:32 +0900189 const auto input_type = module_state.GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700190
191 // Type checking
192 if (!(attrib_type & input_type)) {
sjfricke4f600c82022-06-09 14:21:32 +0900193 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700194 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sjfricke4f600c82022-06-09 14:21:32 +0900195 string_VkFormat(attrib->format), location, module_state.DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700196 }
Petr Kraus25810d02019-08-27 17:41:15 +0200197 } else { // !attrib && !input
198 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700199 }
200 }
201
202 return skip;
203}
204
sjfricke4f600c82022-06-09 14:21:32 +0900205bool CoreChecks::ValidateFsOutputsAgainstDynamicRenderingRenderPass(const SHADER_MODULE_STATE &module_state,
sfricke-samsungef15e482022-01-26 11:32:49 -0800206 spirv_inst_iter entrypoint,
207 PIPELINE_STATE const *pipeline) const {
Aaron Hagan1209c782021-11-22 19:37:14 -0500208 bool skip = false;
209
210 struct Attachment {
211 const interface_var* output = nullptr;
212 };
213 std::map<uint32_t, Attachment> location_map;
214
215 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
sjfricke4f600c82022-06-09 14:21:32 +0900216 const auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Aaron Hagan1209c782021-11-22 19:37:14 -0500217 for (const auto& output_it : outputs) {
218 auto const location = output_it.first.first;
219 location_map[location].output = &output_it.second;
220 }
221
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700222 const auto ms_state = pipeline->MultisampleState();
223 const bool alpha_to_coverage_enabled = ms_state && (ms_state->alphaToCoverageEnable == VK_TRUE);
Aaron Hagan1209c782021-11-22 19:37:14 -0500224
Aaron Haganaca50442021-12-07 22:26:29 -0500225 for (uint32_t location = 0; location < location_map.size(); ++location) {
Aaron Hagan1209c782021-11-22 19:37:14 -0500226 const auto output = location_map[location].output;
227
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700228 const auto &rp_state = pipeline->RenderPassState();
229 const auto &attachments = pipeline->Attachments();
230 if (!output && location < attachments.size() && attachments[location].colorWriteMask != 0) {
231 skip |= LogWarning(
sjfricke4f600c82022-06-09 14:21:32 +0900232 module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700233 "Attachment %" PRIu32 " not written by fragment shader; undefined values will be written to attachment", location);
234 } else if (output && (location < rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount)) {
235 auto format = rp_state->dynamic_rendering_pipeline_create_info.pColorAttachmentFormats[location];
236 const auto attachment_type = GetFormatType(format);
sjfricke4f600c82022-06-09 14:21:32 +0900237 const auto output_type = module_state.GetFundamentalType(output->type_id);
Aaron Hagan1209c782021-11-22 19:37:14 -0500238
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700239 // Type checking
240 if (!(output_type & attachment_type)) {
241 skip |=
sjfricke4f600c82022-06-09 14:21:32 +0900242 LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700243 "Attachment %" PRIu32
244 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sjfricke4f600c82022-06-09 14:21:32 +0900245 location, string_VkFormat(format), module_state.DescribeType(output->type_id).c_str());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700246 }
247 }
Aaron Hagan1209c782021-11-22 19:37:14 -0500248 }
249
250 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sjfricke4f600c82022-06-09 14:21:32 +0900251 bool location_zero_has_alpha = output_zero && module_state.get_def(output_zero->type_id) != module_state.end() &&
252 module_state.GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Aaron Hagan1209c782021-11-22 19:37:14 -0500253 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sjfricke4f600c82022-06-09 14:21:32 +0900254 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
sfricke-samsungef15e482022-01-26 11:32:49 -0800255 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Aaron Hagan1209c782021-11-22 19:37:14 -0500256 }
257
258 return skip;
Aaron Hagan1209c782021-11-22 19:37:14 -0500259}
260
sjfricke4f600c82022-06-09 14:21:32 +0900261bool CoreChecks::ValidateFsOutputsAgainstRenderPass(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700262 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200263 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700264
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600265 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800266 const VkAttachmentReference2 *reference = nullptr;
267 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600268 const interface_var *output = nullptr;
269 };
270 std::map<uint32_t, Attachment> location_map;
271
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700272 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -0600273 if (rp_state && !rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700274 const auto rpci = rp_state->createInfo.ptr();
amhagana448ea52021-11-02 14:09:14 -0400275 const auto subpass = rpci->pSubpasses[subpass_index];
276 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
277 auto const &reference = subpass.pColorAttachments[i];
278 location_map[i].reference = &reference;
279 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
280 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
281 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
282 }
Chris Forbes47567b72017-06-09 12:09:45 -0700283 }
284 }
285
Chris Forbes47567b72017-06-09 12:09:45 -0700286 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
287
sjfricke4f600c82022-06-09 14:21:32 +0900288 const auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600289 for (const auto &output_it : outputs) {
290 auto const location = output_it.first.first;
291 location_map[location].output = &output_it.second;
292 }
Chris Forbes47567b72017-06-09 12:09:45 -0700293
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700294 const auto *ms_state = pipeline->MultisampleState();
295 const bool alpha_to_coverage_enabled = ms_state && (ms_state->alphaToCoverageEnable == VK_TRUE);
Chris Forbes47567b72017-06-09 12:09:45 -0700296
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700297 // Don't check any color attachments if rasterization is disabled
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700298 const auto raster_state = pipeline->RasterizationState();
Nathaniel Cesario81257cb2022-02-16 17:15:58 -0700299 if (raster_state && !raster_state->rasterizerDiscardEnable) {
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700300 for (const auto &location_it : location_map) {
301 const auto reference = location_it.second.reference;
302 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
303 continue;
Petr Kraus25810d02019-08-27 17:41:15 +0200304 }
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700305
306 const auto location = location_it.first;
307 const auto attachment = location_it.second.attachment;
308 const auto output = location_it.second.output;
309 if (attachment && !output) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700310 const auto &attachments = pipeline->Attachments();
311 if (location < attachments.size() && attachments[location].colorWriteMask != 0) {
sjfricke4f600c82022-06-09 14:21:32 +0900312 skip |= LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700313 "Attachment %" PRIu32
314 " not written by fragment shader; undefined values will be written to attachment",
315 location);
316 }
317 } else if (!attachment && output) {
318 if (!(alpha_to_coverage_enabled && location == 0)) {
319 skip |=
sjfricke4f600c82022-06-09 14:21:32 +0900320 LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700321 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700322 }
323 } else if (attachment && output) {
324 const auto attachment_type = GetFormatType(attachment->format);
sjfricke4f600c82022-06-09 14:21:32 +0900325 const auto output_type = module_state.GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700326
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700327 // Type checking
328 if (!(output_type & attachment_type)) {
329 skip |= LogWarning(
sjfricke4f600c82022-06-09 14:21:32 +0900330 module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700331 "Attachment %" PRIu32
332 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sjfricke4f600c82022-06-09 14:21:32 +0900333 location, string_VkFormat(attachment->format), module_state.DescribeType(output->type_id).c_str());
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700334 }
335 } else { // !attachment && !output
336 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700337 }
Chris Forbes47567b72017-06-09 12:09:45 -0700338 }
339 }
340
Petr Kraus25810d02019-08-27 17:41:15 +0200341 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sjfricke4f600c82022-06-09 14:21:32 +0900342 bool location_zero_has_alpha = output_zero && module_state.get_def(output_zero->type_id) != module_state.end() &&
343 module_state.GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700344 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sjfricke4f600c82022-06-09 14:21:32 +0900345 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700346 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200347 }
348
Chris Forbes47567b72017-06-09 12:09:45 -0700349 return skip;
350}
351
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600352PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
353 const shader_struct_member &push_constant_used_in_shader,
354 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600355 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600356 const auto used_bytes_size = used_bytes->size();
357 if (used_bytes_size == 0) return PC_Byte_Updated;
358
359 const auto push_constant_data_update_size = push_constant_data_update.size();
360 const auto *data = push_constant_data_update.data();
361 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
362 if (used_bytes_size <= push_constant_data_update_size) {
363 return PC_Byte_Updated;
364 }
365 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
366
367 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
368 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
369 return PC_Byte_Updated;
370 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600371 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600372
locke-lunargde3f0fa2020-09-10 11:55:31 -0600373 uint32_t i = 0;
374 for (const auto used : *used_bytes) {
375 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600376 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600377 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600378 return PC_Byte_Not_Set;
379 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600380 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600381 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600382 }
383 }
384 ++i;
385 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600386 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600387}
388
sjfricke4f600c82022-06-09 14:21:32 +0900389bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700390 safe_VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700391 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700392 // Temp workaround to prevent false positive errors
393 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sjfricke4f600c82022-06-09 14:21:32 +0900394 if (module_state.HasMultipleEntryPoints()) {
sfricke-samsung5c65b372021-03-25 05:39:57 -0700395 return skip;
396 }
397
Chris Forbes47567b72017-06-09 12:09:45 -0700398 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
sjfricke4f600c82022-06-09 14:21:32 +0900399 const auto *entrypoint = module_state.FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600400 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
401 return skip;
402 }
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700403 const auto &pipeline_layout = pipeline.PipelineLayoutState();
404 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700405
locke-lunargde3f0fa2020-09-10 11:55:31 -0600406 bool found_stage = false;
407 for (auto const &range : *push_constant_ranges) {
408 if (range.stageFlags & pStage->stage) {
409 found_stage = true;
410 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600411 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600412 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600413 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600414 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600415 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600416 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600417 const auto ret =
418 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700419
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600420 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600421 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
sjfricke4f600c82022-06-09 14:21:32 +0900422 LogObjectList objlist(module_state.vk_shader_module());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700423 objlist.add(pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700424 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 -0600425 string_VkShaderStageFlags(pStage->stage).c_str(),
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700426 report_data->FormatHandle(pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600427 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700428 }
429 }
430 }
431
locke-lunargde3f0fa2020-09-10 11:55:31 -0600432 if (!found_stage) {
sjfricke4f600c82022-06-09 14:21:32 +0900433 LogObjectList objlist(module_state.vk_shader_module());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700434 objlist.add(pipeline_layout->layout());
435 skip |= LogError(
436 objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.",
sjfricke4f600c82022-06-09 14:21:32 +0900437 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700438 report_data->FormatHandle(pipeline_layout->layout()).c_str(), string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700439 }
Chris Forbes47567b72017-06-09 12:09:45 -0700440 return skip;
441}
442
sjfricke4f600c82022-06-09 14:21:32 +0900443bool CoreChecks::ValidateBuiltinLimits(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700444 bool skip = false;
445
446 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700447 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700448 return skip;
449 }
450
sfricke-samsungcfb44592021-07-25 00:36:28 -0700451 // Find all builtin from just the interface variables
452 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sjfricke4f600c82022-06-09 14:21:32 +0900453 auto insn = module_state.get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700454 assert(insn.opcode() == spv::OpVariable);
sjfricke4f600c82022-06-09 14:21:32 +0900455 const decoration_set decorations = module_state.get_decorations(insn.word(2));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700456
sfricke-samsungcfb44592021-07-25 00:36:28 -0700457 // Currently don't need to search in structs
458 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sjfricke4f600c82022-06-09 14:21:32 +0900459 auto type_pointer = module_state.get_def(insn.word(1));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700460 assert(type_pointer.opcode() == spv::OpTypePointer);
461
sjfricke4f600c82022-06-09 14:21:32 +0900462 auto type = module_state.get_def(type_pointer.word(3));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700463 if (type.opcode() == spv::OpTypeArray) {
sjfricke4f600c82022-06-09 14:21:32 +0900464 uint32_t length = static_cast<uint32_t>(module_state.GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700465 // Handles both the input and output sampleMask
466 if (length > phys_dev_props.limits.maxSampleMaskWords) {
sjfrickedbd15712022-08-29 16:12:13 +0900467 skip |=
468 LogError(module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
469 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
470 "maxSampleMaskWords of %u in %s.",
471 length, phys_dev_props.limits.maxSampleMaskWords,
472 report_data->FormatHandle(module_state.vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700473 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700474 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700475 }
476 }
477 }
478
479 return skip;
480}
481
Chris Forbes47567b72017-06-09 12:09:45 -0700482// Validate that data for each specialization entry is fully contained within the buffer.
sjfrickedbd15712022-08-29 16:12:13 +0900483bool CoreChecks::ValidateSpecializations(const SHADER_MODULE_STATE &module_state,
484 safe_VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700485 bool skip = false;
486
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700487 const auto *spec = info->pSpecializationInfo;
Chris Forbes47567b72017-06-09 12:09:45 -0700488
489 if (spec) {
490 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600491 if (spec->pMapEntries[i].offset >= spec->dataSize) {
sjfrickedbd15712022-08-29 16:12:13 +0900492 skip |= LogError(module_state.vk_shader_module(), "VUID-VkSpecializationInfo-offset-00773",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700493 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Jeremy Gebbena4fc6742022-09-15 14:57:30 -0600494 "data (bytes %u..%zu; %zu bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700495 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
496 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600497
498 continue;
499 }
Chris Forbes47567b72017-06-09 12:09:45 -0700500 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
sjfrickedbd15712022-08-29 16:12:13 +0900501 skip |= LogError(module_state.vk_shader_module(), "VUID-VkSpecializationInfo-pMapEntries-00774",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700502 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Jeremy Gebbena4fc6742022-09-15 14:57:30 -0600503 "data (bytes %u..%zu; %zu bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700504 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
505 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700506 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200507 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
508 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
sjfrickedbd15712022-08-29 16:12:13 +0900509 skip |= LogError(module_state.vk_shader_module(), "VUID-VkSpecializationInfo-constantID-04911",
ziga-lunargae2a5c42021-07-23 16:18:09 +0200510 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
511 j, spec->pMapEntries[i].constantID);
512 }
513 }
Chris Forbes47567b72017-06-09 12:09:45 -0700514 }
515 }
516
517 return skip;
518}
519
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500520// TODO (jbolz): Can this return a const reference?
sjfricke4f600c82022-06-09 14:21:32 +0900521static std::set<uint32_t> TypeToDescriptorTypeSet(const SHADER_MODULE_STATE &module_state, uint32_t type_id,
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800522 uint32_t &descriptor_count, bool is_khr) {
sjfricke4f600c82022-06-09 14:21:32 +0900523 auto type = module_state.get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800524 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700525 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500526 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700527
528 // 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 -0500529 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
530 if (type.opcode() == spv::OpTypeRuntimeArray) {
531 descriptor_count = 0;
sjfricke4f600c82022-06-09 14:21:32 +0900532 type = module_state.get_def(type.word(2));
Jeff Bolzfdf96072018-04-10 14:32:18 -0500533 } else if (type.opcode() == spv::OpTypeArray) {
sjfricke4f600c82022-06-09 14:21:32 +0900534 descriptor_count *= module_state.GetConstantValueById(type.word(3));
535 type = module_state.get_def(type.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700536 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800537 if (type.word(2) == spv::StorageClassStorageBuffer) {
538 is_storage_buffer = true;
539 }
sjfricke4f600c82022-06-09 14:21:32 +0900540 type = module_state.get_def(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700541 }
542 }
543
544 switch (type.opcode()) {
545 case spv::OpTypeStruct: {
sjfricke4f600c82022-06-09 14:21:32 +0900546 for (const auto insn : module_state.GetDecorationInstructions()) {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800547 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700548 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800549 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500550 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
551 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
552 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800553 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500554 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
555 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
556 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
557 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800558 }
Chris Forbes47567b72017-06-09 12:09:45 -0700559 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500560 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
561 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
562 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700563 }
564 }
565 }
566
567 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500568 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700569 }
570
571 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500572 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
573 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
574 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700575
Chris Forbes73c00bf2018-06-22 16:28:06 -0700576 case spv::OpTypeSampledImage: {
577 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
578 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
sjfricke4f600c82022-06-09 14:21:32 +0900579 auto image_type = module_state.get_def(type.word(2));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700580 auto dim = image_type.word(3);
581 auto sampled = image_type.word(7);
582 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500583 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
584 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700585 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700586 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500587 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
588 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700589
590 case spv::OpTypeImage: {
591 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
592 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
593 auto dim = type.word(3);
594 auto sampled = type.word(7);
595
596 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500597 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
598 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700599 } else if (dim == spv::DimBuffer) {
600 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500601 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
602 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700603 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500604 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
605 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700606 }
607 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500608 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
609 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
610 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700611 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500612 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
613 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700614 }
615 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600616 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700617 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
618 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500619 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700620
621 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
622 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500623 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700624 }
625}
626
Jeff Bolze54ae892018-09-08 12:16:29 -0500627static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700628 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500629 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
630 if (ss.tellp()) ss << ", ";
631 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700632 }
633 return ss.str();
634}
635
sjfrickedbd15712022-08-29 16:12:13 +0900636bool CoreChecks::RequirePropertyFlag(const SHADER_MODULE_STATE &module_state, VkBool32 check, char const *flag,
637 char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500638 if (!check) {
sjfrickedbd15712022-08-29 16:12:13 +0900639 if (LogError(module_state.vk_shader_module(), vuid, "Shader requires flag %s set in %s but it is not set on the device",
640 flag, structure)) {
Jeff Bolzee743412019-06-20 22:24:32 -0500641 return true;
642 }
643 }
644
645 return false;
646}
647
sjfrickedbd15712022-08-29 16:12:13 +0900648bool CoreChecks::RequireFeature(const SHADER_MODULE_STATE &module_state, VkBool32 feature, char const *feature_name,
649 const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700650 if (!feature) {
sjfrickedbd15712022-08-29 16:12:13 +0900651 if (LogError(module_state.vk_shader_module(), vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700652 return true;
653 }
654 }
655
656 return false;
657}
658
sjfrickedbd15712022-08-29 16:12:13 +0900659bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(const SHADER_MODULE_STATE &module_state, VkShaderStageFlagBits stage,
660 bool has_writable_descriptor, bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500661 bool skip = false;
662
locke-lunarg63e4daf2020-08-17 17:53:25 -0600663 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800664 switch (stage) {
Chris Forbes349b3132018-03-07 11:38:08 -0800665 case VK_SHADER_STAGE_FRAGMENT_BIT:
sjfrickedbd15712022-08-29 16:12:13 +0900666 skip |= RequireFeature(module_state, enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700667 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800668 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800669 case VK_SHADER_STAGE_VERTEX_BIT:
670 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
671 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
672 case VK_SHADER_STAGE_GEOMETRY_BIT:
sjfrickedbd15712022-08-29 16:12:13 +0900673 skip |= RequireFeature(module_state, enabled_features.core.vertexPipelineStoresAndAtomics,
674 "vertexPipelineStoresAndAtomics", "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800675 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800676 default:
677 // No feature requirements for writes and atomics for other stages
678 break;
Chris Forbes349b3132018-03-07 11:38:08 -0800679 }
680 }
681
Chris Forbes47567b72017-06-09 12:09:45 -0700682 return skip;
683}
684
sjfricke4f600c82022-06-09 14:21:32 +0900685bool CoreChecks::ValidateShaderStageGroupNonUniform(const SHADER_MODULE_STATE &module_state, VkShaderStageFlagBits stage,
sfricke-samsung94167ca2021-02-26 04:14:59 -0800686 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500687 bool skip = false;
688
sfricke-samsung94167ca2021-02-26 04:14:59 -0800689 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
690 if (GroupOperation(insn.opcode()) == true) {
691 // Check the quad operations.
692 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
693 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700694 skip |=
sjfrickedbd15712022-08-29 16:12:13 +0900695 RequireFeature(module_state, phys_dev_props_core11.subgroupQuadOperationsInAllStages,
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700696 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800697 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800698 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500699
sfricke-samsung94167ca2021-02-26 04:14:59 -0800700 uint32_t scope_type = spv::ScopeMax;
701 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
702 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
703 scope_type = spv::ScopeSubgroup;
704 } else {
705 // "All <id> used for Scope <id> must be of an OpConstant"
sjfricke4f600c82022-06-09 14:21:32 +0900706 auto scope_id = module_state.get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800707 scope_type = scope_id.word(3);
708 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800709
sfricke-samsung94167ca2021-02-26 04:14:59 -0800710 if (scope_type == spv::ScopeSubgroup) {
711 // "Group operations with subgroup scope" must have stage support
712 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
sjfrickedbd15712022-08-29 16:12:13 +0900713 skip |= RequirePropertyFlag(module_state, supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700714 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800715 }
716
717 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
sjfricke4f600c82022-06-09 14:21:32 +0900718 auto type = module_state.get_def(insn.word(1));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800719
720 if (type.opcode() == spv::OpTypeVector) {
721 // Get the element type
sjfricke4f600c82022-06-09 14:21:32 +0900722 type = module_state.get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800723 }
724
sfricke-samsung94167ca2021-02-26 04:14:59 -0800725 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800726 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
727 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500728
sfricke-samsung0065ce02020-12-03 22:46:37 -0800729 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
730 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
sjfrickedbd15712022-08-29 16:12:13 +0900731 skip |= RequireFeature(module_state, enabled_features.core12.shaderSubgroupExtendedTypes,
sfricke-samsung0065ce02020-12-03 22:46:37 -0800732 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700733 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500734 }
735 }
736 }
Jeff Bolzee743412019-06-20 22:24:32 -0500737 }
738
739 return skip;
740}
741
sjfricke4f600c82022-06-09 14:21:32 +0900742bool CoreChecks::ValidateMemoryScope(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &insn) const {
ziga-lunarg70651522021-10-11 17:23:30 +0200743 bool skip = false;
744
sfricke-samsung3a25ed52022-01-20 02:24:36 -0800745 const auto &entry = OpcodeMemoryScopePosition(insn.opcode());
ziga-lunarg70651522021-10-11 17:23:30 +0200746 if (entry > 0) {
747 const uint32_t scope_id = insn.word(entry);
sjfricke4f600c82022-06-09 14:21:32 +0900748 const auto &scope_def = module_state.GetConstantDef(scope_id);
749 if (scope_def != module_state.end()) {
sjfricke3b0cb102022-08-10 16:27:45 +0900750 const auto scope_type = module_state.GetConstantValue(scope_def);
sfricke-samsunged00aa42022-01-27 19:03:01 -0800751 if (enabled_features.core12.vulkanMemoryModel && !enabled_features.core12.vulkanMemoryModelDeviceScope &&
752 scope_type == spv::Scope::ScopeDevice) {
sjfrickedbd15712022-08-29 16:12:13 +0900753 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-vulkanMemoryModel-06265",
sfricke-samsunged00aa42022-01-27 19:03:01 -0800754 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is enabled and "
755 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModelDeviceScope is disabled, but\n%s\nuses "
756 "Device memory scope.",
sjfricke4f600c82022-06-09 14:21:32 +0900757 module_state.DescribeInstruction(insn).c_str());
sfricke-samsunged00aa42022-01-27 19:03:01 -0800758 } else if (!enabled_features.core12.vulkanMemoryModel && scope_type == spv::Scope::ScopeQueueFamily) {
sjfrickedbd15712022-08-29 16:12:13 +0900759 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-vulkanMemoryModel-06266",
sfricke-samsunged00aa42022-01-27 19:03:01 -0800760 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is not enabled, but\n%s\nuses "
761 "QueueFamily memory scope.",
sjfricke4f600c82022-06-09 14:21:32 +0900762 module_state.DescribeInstruction(insn).c_str());
ziga-lunarg70651522021-10-11 17:23:30 +0200763 }
764 }
765 }
766
767 return skip;
768}
769
sjfricke4f600c82022-06-09 14:21:32 +0900770bool CoreChecks::ValidateShaderStageInputOutputLimits(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700771 safe_VkPipelineShaderStageCreateInfo const *pStage,
772 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200773 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
774 pStage->stage == VK_SHADER_STAGE_ALL) {
775 return false;
776 }
777
778 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700779 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200780
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700781 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200782 struct Variable {
783 uint32_t baseTypePtrID;
784 uint32_t ID;
785 uint32_t storageClass;
786 };
787 std::vector<Variable> variables;
788
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700789 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700790 bool is_iso_lines = false;
791 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500792
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700793 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600794
sjfricke4f600c82022-06-09 14:21:32 +0900795 for (auto insn : module_state) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200796 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500797 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200798 case spv::OpDecorate:
799 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500800 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700801 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200802 break;
803 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200804 default:
805 break;
806 }
807 break;
808 // Find all input and output variables
809 case spv::OpVariable: {
810 Variable var = {};
811 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600812 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
813 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700814 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200815 var.baseTypePtrID = insn.word(1);
816 var.ID = insn.word(2);
817 variables.push_back(var);
818 }
819 break;
820 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500821 case spv::OpExecutionMode:
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800822 case spv::OpExecutionModeId:
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500823 if (insn.word(1) == entrypoint.word(2)) {
824 switch (insn.word(2)) {
825 default:
826 break;
827 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700828 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500829 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700830 case spv::ExecutionModeIsolines:
831 is_iso_lines = true;
832 break;
833 case spv::ExecutionModePointMode:
834 is_point_mode = true;
835 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500836 }
837 }
838 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200839 default:
840 break;
841 }
842 }
843
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500844 bool strip_output_array_level =
845 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
846 bool strip_input_array_level =
847 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
848 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
849
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700850 uint32_t num_comp_in = 0, num_comp_out = 0;
851 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600852
sjfricke4f600c82022-06-09 14:21:32 +0900853 auto inputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
854 auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600855
856 // Find max component location used for input variables.
857 for (auto &var : inputs) {
858 int location = var.first.first;
859 int component = var.first.second;
860 interface_var &iv = var.second;
861
862 // Only need to look at the first location, since we use the type's whole size
863 if (iv.offset != 0) {
864 continue;
865 }
866
867 if (iv.is_patch) {
868 continue;
869 }
870
sjfricke4f600c82022-06-09 14:21:32 +0900871 int num_components = module_state.GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700872 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600873 }
874
875 // Find max component location used for output variables.
876 for (auto &var : outputs) {
877 int location = var.first.first;
878 int component = var.first.second;
879 interface_var &iv = var.second;
880
881 // Only need to look at the first location, since we use the type's whole size
882 if (iv.offset != 0) {
883 continue;
884 }
885
886 if (iv.is_patch) {
887 continue;
888 }
889
sjfricke4f600c82022-06-09 14:21:32 +0900890 int num_components = module_state.GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700891 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600892 }
893
894 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
895 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700896 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
897 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200898 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500899 // Check if the variable is a patch. Patches can also be members of blocks,
900 // but if they are then the top-level arrayness has already been stripped
901 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700902 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200903
904 if (var.storageClass == spv::StorageClassInput) {
sjfricke4f600c82022-06-09 14:21:32 +0900905 num_comp_in += module_state.GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200906 } else { // var.storageClass == spv::StorageClassOutput
sjfricke4f600c82022-06-09 14:21:32 +0900907 num_comp_out += module_state.GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200908 }
909 }
910
911 switch (pStage->stage) {
912 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700913 if (num_comp_out > limits.maxVertexOutputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +0900914 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700915 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
916 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
917 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700918 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200919 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700920 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
sjfrickedbd15712022-08-29 16:12:13 +0900921 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700922 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
923 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
924 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600925 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200926 break;
927
928 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700929 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +0900930 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700931 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
932 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
933 "components by %u components",
934 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700935 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200936 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700937 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600938 skip |=
sjfrickedbd15712022-08-29 16:12:13 +0900939 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700940 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
941 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
942 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600943 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700944 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +0900945 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700946 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
947 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
948 "components by %u components",
949 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700950 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200951 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700952 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600953 skip |=
sjfrickedbd15712022-08-29 16:12:13 +0900954 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700955 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
956 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
957 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600958 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200959 break;
960
961 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700962 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +0900963 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700964 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
965 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
966 "components by %u components",
967 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700968 num_comp_in - limits.maxTessellationEvaluationInputComponents);
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.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600971 skip |=
sjfrickedbd15712022-08-29 16:12:13 +0900972 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700973 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
974 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
975 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600976 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700977 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +0900978 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700979 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
980 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
981 "components by %u components",
982 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700983 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200984 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700985 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600986 skip |=
sjfrickedbd15712022-08-29 16:12:13 +0900987 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700988 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
989 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
990 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600991 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700992 // Portability validation
993 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
994 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sjfrickedbd15712022-08-29 16:12:13 +0900995 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700996 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
997 " is using abstract patch type IsoLines, but this is not supported on this platform");
998 }
999 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sjfrickedbd15712022-08-29 16:12:13 +09001000 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -07001001 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
1002 " is using abstract patch type PointMode, but this is not supported on this platform");
1003 }
1004 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001005 break;
1006
1007 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001008 if (num_comp_in > limits.maxGeometryInputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +09001009 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001010 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1011 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
1012 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001013 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001014 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001015 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sjfrickedbd15712022-08-29 16:12:13 +09001016 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001017 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
1018 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
1019 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001020 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001021 if (num_comp_out > limits.maxGeometryOutputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +09001022 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001023 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1024 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
1025 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001026 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001027 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001028 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
sjfrickedbd15712022-08-29 16:12:13 +09001029 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001030 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
1031 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
1032 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001033 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001034 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +09001035 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001036 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1037 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
1038 "components by %u components",
1039 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001040 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001041 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001042 break;
1043
1044 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001045 if (num_comp_in > limits.maxFragmentInputComponents) {
sjfrickedbd15712022-08-29 16:12:13 +09001046 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001047 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
1048 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
1049 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001050 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001051 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001052 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sjfrickedbd15712022-08-29 16:12:13 +09001053 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001054 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1055 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1056 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001057 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001058 break;
1059
sjfricke62366d32022-08-01 21:04:10 +09001060 case VK_SHADER_STAGE_RAYGEN_BIT_KHR:
1061 case VK_SHADER_STAGE_ANY_HIT_BIT_KHR:
1062 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR:
1063 case VK_SHADER_STAGE_MISS_BIT_KHR:
1064 case VK_SHADER_STAGE_INTERSECTION_BIT_KHR:
1065 case VK_SHADER_STAGE_CALLABLE_BIT_KHR:
Jeff Bolz148d94e2018-12-13 21:25:56 -06001066 case VK_SHADER_STAGE_TASK_BIT_NV:
1067 case VK_SHADER_STAGE_MESH_BIT_NV:
1068 break;
1069
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001070 default:
1071 assert(false); // This should never happen
1072 }
1073 return skip;
1074}
1075
sjfricke29ca0762022-08-24 14:26:33 +09001076bool CoreChecks::ValidateShaderStorageImageFormatsVariables(const SHADER_MODULE_STATE &module_state,
1077 const spirv_inst_iter &insn) const {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001078 bool skip = false;
sjfricke29ca0762022-08-24 14:26:33 +09001079 // Go through all variables for images and check decorations
1080 assert(insn.opcode() == spv::OpVariable);
1081 // spirv-val validates this is an OpTypePointer
1082 const spirv_inst_iter pointer_def = module_state.get_def(insn.word(1));
1083 if (pointer_def.word(2) != spv::StorageClassUniformConstant) {
1084 return skip; // Vulkan Spec says storage image must be UniformConstant
1085 }
1086 spirv_inst_iter type_def = module_state.get_def(pointer_def.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001087
sjfricke29ca0762022-08-24 14:26:33 +09001088 // Unpack an optional level of arraying
1089 if (type_def.opcode() == spv::OpTypeArray || type_def.opcode() == spv::OpTypeRuntimeArray) {
1090 type_def = module_state.get_def(type_def.word(2));
1091 }
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001092
sjfricke29ca0762022-08-24 14:26:33 +09001093 if (type_def != module_state.end() && type_def.opcode() == spv::OpTypeImage) {
1094 // Only check if the Image Dim operand is not SubpassData
1095 const uint32_t dim = type_def.word(3);
1096 // Only check storage images
1097 const uint32_t sampled = type_def.word(7);
1098 const uint32_t image_format = type_def.word(8);
1099 if ((dim == spv::DimSubpassData) || (sampled != 2) || (image_format != spv::ImageFormatUnknown)) {
1100 return skip;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001101 }
1102
sjfricke29ca0762022-08-24 14:26:33 +09001103 const uint32_t var_id = insn.word(2);
1104 decoration_set img_decorations = module_state.get_decorations(var_id);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001105
sjfricke29ca0762022-08-24 14:26:33 +09001106 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1107 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
sjfrickedbd15712022-08-29 16:12:13 +09001108 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-OpTypeImage-06270",
sjfricke29ca0762022-08-24 14:26:33 +09001109 "shaderStorageImageReadWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith Unknown "
1110 "format and is not decorated with NonReadable",
1111 module_state.DescribeInstruction(module_state.get_def(var_id)).c_str(),
1112 module_state.DescribeInstruction(type_def).c_str());
1113 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001114
sjfricke29ca0762022-08-24 14:26:33 +09001115 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1116 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
sjfrickedbd15712022-08-29 16:12:13 +09001117 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-OpTypeImage-06269",
sjfricke29ca0762022-08-24 14:26:33 +09001118 "shaderStorageImageWriteWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith "
1119 "Unknown format and is not decorated with NonWritable",
1120 module_state.DescribeInstruction(module_state.get_def(var_id)).c_str(),
1121 module_state.DescribeInstruction(type_def).c_str());
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001122 }
1123 }
1124
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001125 return skip;
1126}
1127
sjfrickedbd15712022-08-29 16:12:13 +09001128bool CoreChecks::ValidateShaderStageMaxResources(const SHADER_MODULE_STATE &module_state, VkShaderStageFlagBits stage,
1129 const PIPELINE_STATE *pipeline) const {
sfricke-samsungdc96f302020-03-18 20:42:10 -07001130 bool skip = false;
1131 uint32_t total_resources = 0;
1132
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001133 const auto &rp_state = pipeline->RenderPassState();
1134 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) && rp_state) {
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06001135 if (rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001136 total_resources += rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001137 } else {
1138 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001139 total_resources += rp_state->createInfo.pSubpasses[pipeline->Subpass()].colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001140 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001141 }
1142
1143 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1144 // input from CreatePipeline and CreatePipelineLayout level
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001145 const auto &layout_state = pipeline->PipelineLayoutState();
1146 if (layout_state) {
1147 for (auto set_layout : layout_state->set_layouts) {
1148 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1149 continue;
1150 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001151
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001152 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1153 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1154 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1155 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1156 // Check only descriptor types listed in maxPerStageResources description in spec
1157 switch (binding->descriptorType) {
1158 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1159 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1160 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1161 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1162 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1163 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1164 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1165 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1166 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1167 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1168 total_resources += binding->descriptorCount;
1169 break;
1170 default:
1171 break;
1172 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001173 }
1174 }
1175 }
1176 }
1177
1178 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
ziga-lunarg7d53c822022-05-08 23:06:10 +02001179 const char *vuid = nullptr;
1180 if (stage == VK_SHADER_STAGE_COMPUTE_BIT) {
1181 vuid = "VUID-VkComputePipelineCreateInfo-layout-01687";
1182 } else if ((stage & VK_SHADER_STAGE_ALL_GRAPHICS) == 0) {
1183 vuid = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03428";
1184 } else {
1185 vuid = "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
1186 }
sjfrickedbd15712022-08-29 16:12:13 +09001187 skip |= LogError(module_state.vk_shader_module(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001188 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1189 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1190 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1191 }
1192
1193 return skip;
1194}
1195
Jeff Bolze4356752019-03-07 11:23:46 -06001196// copy the specialization constant value into buf, if it is present
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001197template <typename StageCreateInfo>
1198void GetSpecConstantValue(StageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1199 const auto *spec = pStage->pSpecializationInfo;
Jeff Bolze4356752019-03-07 11:23:46 -06001200
1201 if (spec && spec_id < spec->mapEntryCount) {
1202 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1203 }
1204}
1205
1206// Fill in value with the constant or specialization constant value, if available.
1207// Returns true if the value has been accurately filled out.
sjfricke4f600c82022-06-09 14:21:32 +09001208static bool GetIntConstantValue(spirv_inst_iter insn, const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001209 safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001210 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
sjfricke4f600c82022-06-09 14:21:32 +09001211 auto type_id = module_state.get_def(insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001212 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1213 return false;
1214 }
1215 switch (insn.opcode()) {
1216 case spv::OpSpecConstant:
1217 *value = insn.word(3);
1218 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1219 return true;
1220 case spv::OpConstant:
1221 *value = insn.word(3);
1222 return true;
1223 default:
1224 return false;
1225 }
1226}
1227
1228// Map SPIR-V type to VK_COMPONENT_TYPE enum
sjfricke4f600c82022-06-09 14:21:32 +09001229VkComponentTypeNV GetComponentType(spirv_inst_iter insn) {
Jeff Bolze4356752019-03-07 11:23:46 -06001230 switch (insn.opcode()) {
1231 case spv::OpTypeInt:
1232 switch (insn.word(2)) {
1233 case 8:
1234 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1235 case 16:
1236 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1237 case 32:
1238 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1239 case 64:
1240 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1241 default:
1242 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1243 }
1244 case spv::OpTypeFloat:
1245 switch (insn.word(2)) {
1246 case 16:
1247 return VK_COMPONENT_TYPE_FLOAT16_NV;
1248 case 32:
1249 return VK_COMPONENT_TYPE_FLOAT32_NV;
1250 case 64:
1251 return VK_COMPONENT_TYPE_FLOAT64_NV;
1252 default:
1253 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1254 }
1255 default:
1256 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1257 }
1258}
1259
1260// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1261// in SPIRV-Tools (e.g. due to specialization constant usage).
sjfricke4f600c82022-06-09 14:21:32 +09001262bool CoreChecks::ValidateCooperativeMatrix(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001263 safe_VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001264 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001265 bool skip = false;
1266
1267 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001268 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001269 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001270 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001271
1272 struct CoopMatType {
1273 uint32_t scope, rows, cols;
1274 VkComponentTypeNV component_type;
1275 bool all_constant;
1276
1277 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1278
sjfricke4f600c82022-06-09 14:21:32 +09001279 void Init(uint32_t id, const SHADER_MODULE_STATE &module_state, safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001280 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
sjfricke4f600c82022-06-09 14:21:32 +09001281 spirv_inst_iter insn = module_state.get_def(id);
Jeff Bolze4356752019-03-07 11:23:46 -06001282 uint32_t component_type_id = insn.word(2);
1283 uint32_t scope_id = insn.word(3);
1284 uint32_t rows_id = insn.word(4);
1285 uint32_t cols_id = insn.word(5);
sjfricke4f600c82022-06-09 14:21:32 +09001286 auto component_type_iter = module_state.get_def(component_type_id);
1287 auto scope_iter = module_state.get_def(scope_id);
1288 auto rows_iter = module_state.get_def(rows_id);
1289 auto cols_iter = module_state.get_def(cols_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001290
1291 all_constant = true;
sfricke-samsungef15e482022-01-26 11:32:49 -08001292 if (!GetIntConstantValue(scope_iter, module_state, pStage, id_to_spec_id, &scope)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001293 all_constant = false;
1294 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001295 if (!GetIntConstantValue(rows_iter, module_state, pStage, id_to_spec_id, &rows)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001296 all_constant = false;
1297 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001298 if (!GetIntConstantValue(cols_iter, module_state, pStage, id_to_spec_id, &cols)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001299 all_constant = false;
1300 }
sjfricke4f600c82022-06-09 14:21:32 +09001301 component_type = GetComponentType(component_type_iter);
Jeff Bolze4356752019-03-07 11:23:46 -06001302 }
1303 };
1304
1305 bool seen_coopmat_capability = false;
1306
sjfricke4f600c82022-06-09 14:21:32 +09001307 for (auto insn : module_state) {
sjfrickeb0943832022-08-18 16:06:54 +09001308 if (OpcodeHasType(insn.opcode()) && OpcodeHasResult(insn.opcode())) {
1309 id_to_type_id[insn.word(2)] = insn.word(1);
Jeff Bolze4356752019-03-07 11:23:46 -06001310 }
1311
1312 switch (insn.opcode()) {
1313 case spv::OpDecorate:
1314 if (insn.word(2) == spv::DecorationSpecId) {
1315 id_to_spec_id[insn.word(1)] = insn.word(3);
1316 }
1317 break;
1318 case spv::OpCapability:
1319 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1320 seen_coopmat_capability = true;
1321
1322 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001323 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09001324 module_state.vk_shader_module(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001325 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1326 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001327 }
1328 }
1329 break;
1330 case spv::OpMemoryModel:
1331 // If the capability isn't enabled, don't bother with the rest of this function.
1332 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1333 if (!seen_coopmat_capability) {
1334 return skip;
1335 }
1336 break;
1337 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001338 CoopMatType m;
sfricke-samsungef15e482022-01-26 11:32:49 -08001339 m.Init(insn.word(1), module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001340
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001341 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001342 // Validate that the type parameters are all supported for one of the
1343 // operands of a cooperative matrix property.
1344 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001345 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001346 if (cooperative_matrix_properties[i].AType == m.component_type &&
1347 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1348 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001349 valid = true;
1350 break;
1351 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001352 if (cooperative_matrix_properties[i].BType == m.component_type &&
1353 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1354 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001355 valid = true;
1356 break;
1357 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001358 if (cooperative_matrix_properties[i].CType == m.component_type &&
1359 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1360 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001361 valid = true;
1362 break;
1363 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001364 if (cooperative_matrix_properties[i].DType == m.component_type &&
1365 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1366 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001367 valid = true;
1368 break;
1369 }
1370 }
1371 if (!valid) {
sjfrickedbd15712022-08-29 16:12:13 +09001372 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001373 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1374 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001375 }
1376 }
1377 break;
1378 }
1379 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001380 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001381 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1382 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1383 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1384 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001385 // Couldn't find type of matrix
1386 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001387 break;
1388 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001389 d.Init(id_to_type_id[insn.word(2)], module_state, pStage, id_to_spec_id);
1390 a.Init(id_to_type_id[insn.word(3)], module_state, pStage, id_to_spec_id);
1391 b.Init(id_to_type_id[insn.word(4)], module_state, pStage, id_to_spec_id);
1392 c.Init(id_to_type_id[insn.word(5)], module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001393
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001394 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001395 // Validate that the type parameters are all supported for the same
1396 // cooperative matrix property.
1397 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001398 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001399 if (cooperative_matrix_properties[i].AType == a.component_type &&
1400 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1401 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001402
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001403 cooperative_matrix_properties[i].BType == b.component_type &&
1404 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1405 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001406
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001407 cooperative_matrix_properties[i].CType == c.component_type &&
1408 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1409 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001410
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001411 cooperative_matrix_properties[i].DType == d.component_type &&
1412 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1413 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001414 valid = true;
1415 break;
1416 }
1417 }
1418 if (!valid) {
sjfrickedbd15712022-08-29 16:12:13 +09001419 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001420 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1421 "VkCooperativeMatrixPropertiesNV",
1422 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001423 }
1424 }
1425 break;
1426 }
1427 default:
1428 break;
1429 }
1430 }
1431
1432 return skip;
1433}
1434
sjfricke4f600c82022-06-09 14:21:32 +09001435bool CoreChecks::ValidateShaderResolveQCOM(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001436 safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001437 const PIPELINE_STATE *pipeline) const {
1438 bool skip = false;
1439
1440 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1441 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1442 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09001443 for (auto insn : module_state) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001444 switch (insn.opcode()) {
1445 case spv::OpCapability:
1446 if (insn.word(1) == spv::CapabilitySampleRateShading) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001447 const auto &rp_state = pipeline->RenderPassState();
1448 auto subpass_flags = (!rp_state) ? 0 : rp_state->createInfo.pSubpasses[pipeline->Subpass()].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001449 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
sjfrickedbd15712022-08-29 16:12:13 +09001450 LogObjectList objlist(module_state.vk_shader_module());
1451 objlist.add(rp_state->renderPass());
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001452 skip |=
sjfrickedbd15712022-08-29 16:12:13 +09001453 LogError(objlist, "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001454 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1455 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1456 }
1457 }
1458 break;
1459 default:
1460 break;
1461 }
1462 }
1463 }
1464
1465 return skip;
1466}
1467
sjfrickedbd15712022-08-29 16:12:13 +09001468bool CoreChecks::ValidateShaderSubgroupSizeControl(const SHADER_MODULE_STATE &module_state,
1469 safe_VkPipelineShaderStageCreateInfo const *pStage) const {
ziga-lunarg73163742021-08-25 13:15:29 +02001470 bool skip = false;
1471
1472 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001473 !enabled_features.core13.subgroupSizeControl) {
ziga-lunarg73163742021-08-25 13:15:29 +02001474 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09001475 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
ziga-lunarg73163742021-08-25 13:15:29 +02001476 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1477 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1478 }
1479
1480 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001481 !enabled_features.core13.computeFullSubgroups) {
ziga-lunarg73163742021-08-25 13:15:29 +02001482 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09001483 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
ziga-lunarg73163742021-08-25 13:15:29 +02001484 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1485 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1486 }
1487
1488 return skip;
1489}
1490
sjfricke4f600c82022-06-09 14:21:32 +09001491bool CoreChecks::ValidateAtomicsTypes(const SHADER_MODULE_STATE &module_state) const {
sfricke-samsung58b84352021-07-31 21:41:04 -07001492 bool skip = false;
1493
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001494 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001495 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001496
sfricke-samsungf5042b12021-08-05 01:09:40 -07001497 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1498 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1499
1500 const bool valid_storage_buffer_float = (
1501 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1502 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1503 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1504 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1505 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1506 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1507 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1508 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1509 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1510
1511 const bool valid_workgroup_float = (
1512 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1513 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1514 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1515 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1516 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1517 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1518 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1519 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1520 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1521
1522 const bool valid_image_float = (
1523 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1524 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1525 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1526
1527 const bool valid_16_float = (
1528 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1529 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1530 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1531 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1532 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1533 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1534
1535 const bool valid_32_float = (
1536 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1537 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1538 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1539 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1540 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1541 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1542 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1543 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1544 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1545
1546 const bool valid_64_float = (
1547 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1548 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1549 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1550 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1551 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1552 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1553 // clang-format on
1554
sjfricke4f600c82022-06-09 14:21:32 +09001555 for (const auto &atomic_inst : module_state.GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001556 const atomic_instruction &atomic = atomic_inst.second;
sjfricke4f600c82022-06-09 14:21:32 +09001557 const spirv_inst_iter atomic_def = module_state.at(atomic_inst.first);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001558 const uint32_t opcode = atomic_def.opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001559
1560 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001561 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001562 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1563 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sjfricke657dfdc2022-08-25 23:40:32 +09001564 skip |=
1565 LogError(device, "VUID-RuntimeSpirv-None-06278",
1566 "%s: Can't use 64-bit int atomics operations\n%s\nwith %s storage class without "
1567 "shaderBufferInt64Atomics enabled.",
1568 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1569 module_state.DescribeInstruction(atomic_def).c_str(), string_SpvStorageClass(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001570 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1571 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001572 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001573 "%s: Can't use 64-bit int atomics operations\n%s\nwith Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001574 "shaderSharedInt64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001575 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1576 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001577 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001578 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001579 "%s: Can't use 64-bit int atomics operations\n%s\nwith Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001580 "shaderImageInt64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001581 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1582 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001583 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001584 } else if (atomic.type == spv::OpTypeFloat) {
1585 // Validate Floats
1586 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1587 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001588 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1589 : "VUID-RuntimeSpirv-None-06280";
1590 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001591 "%s: Can't use float atomics operations\n%s\nwith StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001592 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1593 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1594 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1595 "shaderBufferFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001596 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1597 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001598 } else if (opcode == spv::OpAtomicFAddEXT) {
1599 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1600 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001601 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001602 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001603 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1604 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001605 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1606 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001607 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001608 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001609 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1610 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001611 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1612 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001613 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001614 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001615 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1616 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001617 }
1618 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1619 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001620 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1621 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1622 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001623 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1624 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001625 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001626 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1627 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1628 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001629 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1630 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001631 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001632 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1633 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1634 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001635 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1636 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001637 }
1638 } else {
1639 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1640 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001641 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1642 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith "
1643 "StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001644 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1645 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001646 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001647 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1648 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith "
1649 "StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001650 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1651 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001652 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001653 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1654 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith "
1655 "StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001656 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1657 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001658 }
1659 }
1660 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1661 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001662 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1663 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsungef15e482022-01-26 11:32:49 -08001664 skip |=
1665 LogError(device, vuid,
1666 "%s: Can't use float atomics operations\n%s\nwith Workgroup storage class without "
1667 "shaderSharedFloat32Atomics or "
1668 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1669 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1670 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001671 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1672 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001673 } else if (opcode == spv::OpAtomicFAddEXT) {
1674 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1675 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001676 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001677 "storage class without shaderSharedFloat16AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001678 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1679 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001680 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1681 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001682 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001683 "storage class without shaderSharedFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001684 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1685 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001686 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1687 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001688 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001689 "storage class without shaderSharedFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001690 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1691 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001692 }
1693 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1694 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001695 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1696 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1697 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001698 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1699 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001700 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001701 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1702 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1703 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001704 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1705 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001706 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001707 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1708 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1709 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001710 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1711 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001712 }
1713 } else {
1714 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1715 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001716 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1717 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1718 "storage class without shaderSharedFloat16Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001719 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1720 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001721 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001722 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1723 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1724 "storage class without shaderSharedFloat32Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001725 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1726 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001727 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001728 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1729 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1730 "storage class without shaderSharedFloat64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001731 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1732 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001733 }
1734 }
1735 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001736 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1737 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001738 skip |= LogError(
1739 device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001740 "%s: Can't use float atomics operations\n%s\nwith Image storage class without shaderImageFloat32Atomics or "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001741 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001742 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1743 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001744 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001745 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001746 "%s: Can't use 16-bit float atomics operations\n%s\nwithout shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001747 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1748 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001749 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1750 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001751 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001752 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1753 : "VUID-RuntimeSpirv-None-06335";
1754 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001755 "%s: Can't use 32-bit float atomics operations\n%s\nwithout shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001756 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1757 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1758 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1759 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001760 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1761 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001762 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001763 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1764 : "VUID-RuntimeSpirv-None-06336";
1765 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001766 "%s: Can't use 64-bit float atomics operations\n%s\nwithout shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001767 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1768 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001769 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1770 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001771 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001772 }
1773 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001774 return skip;
1775}
1776
sjfricke4f600c82022-06-09 14:21:32 +09001777bool CoreChecks::ValidateExecutionModes(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint,
sfricke-samsungef15e482022-01-26 11:32:49 -08001778 VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001779 auto entrypoint_id = entrypoint.word(2);
1780
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001781 // The first denorm execution mode encountered, along with its bit width.
1782 // Used to check if SeparateDenormSettings is respected.
1783 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001784
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001785 // The first rounding mode encountered, along with its bit width.
1786 // Used to check if SeparateRoundingModeSettings is respected.
1787 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001788
1789 bool skip = false;
1790
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001791 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001792 uint32_t invocations = 0;
1793
sjfricke4f600c82022-06-09 14:21:32 +09001794 const auto &execution_mode_inst = module_state.GetExecutionModeInstructions();
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001795 auto it = execution_mode_inst.find(entrypoint_id);
1796 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001797 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001798 auto mode = insn.word(2);
1799 switch (mode) {
1800 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1801 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001802 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001803 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09001804 module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001805 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001806 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001807 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1808 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09001809 module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001810 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001811 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001812 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1813 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09001814 module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001815 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001816 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001817 }
1818 break;
1819 }
1820
1821 case spv::ExecutionModeDenormPreserve: {
1822 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001823 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
sjfrickedbd15712022-08-29 16:12:13 +09001824 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001825 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001826 module_state.DescribeInstruction(insn).c_str());
sfricke-samsunged00aa42022-01-27 19:03:01 -08001827 ;
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001828 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
sjfrickedbd15712022-08-29 16:12:13 +09001829 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001830 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001831 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001832 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
sjfrickedbd15712022-08-29 16:12:13 +09001833 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001834 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001835 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001836 }
1837
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001838 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1839 // Register the first denorm execution mode found
1840 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001841 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001842 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001843 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001844 if (first_rounding_mode.second != 32 && bit_width != 32) {
sjfrickedbd15712022-08-29 16:12:13 +09001845 skip |= LogError(module_state.vk_shader_module(),
1846 "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001847 "Shader uses different denorm execution modes for 16 and 64-bit but "
1848 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001849 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001850 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001851 }
1852 break;
1853
Mike Schuchardt2df08912020-12-15 16:28:09 -08001854 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001855 break;
1856
sjfrickedbd15712022-08-29 16:12:13 +09001857 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: {
1858 skip |=
1859 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
1860 "Shader uses different denorm execution modes for different bit widths but "
1861 "denormBehaviorIndependence is "
1862 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
1863 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001864 break;
sjfrickedbd15712022-08-29 16:12:13 +09001865 }
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001866
1867 default:
1868 break;
1869 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001870 }
1871 break;
1872 }
1873
1874 case spv::ExecutionModeDenormFlushToZero: {
1875 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001876 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001877 skip |=
sjfrickedbd15712022-08-29 16:12:13 +09001878 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001879 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001880 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001881 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001882 skip |=
sjfrickedbd15712022-08-29 16:12:13 +09001883 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001884 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001885 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001886 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001887 skip |=
sjfrickedbd15712022-08-29 16:12:13 +09001888 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001889 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001890 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001891 }
1892
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001893 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1894 // Register the first denorm execution mode found
1895 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001896 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001897 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001898 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001899 if (first_rounding_mode.second != 32 && bit_width != 32) {
sjfrickedbd15712022-08-29 16:12:13 +09001900 skip |= LogError(module_state.vk_shader_module(),
1901 "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001902 "Shader uses different denorm execution modes for 16 and 64-bit but "
1903 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001904 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001905 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001906 }
1907 break;
1908
Mike Schuchardt2df08912020-12-15 16:28:09 -08001909 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001910 break;
1911
sjfrickedbd15712022-08-29 16:12:13 +09001912 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: {
1913 skip |=
1914 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
1915 "Shader uses different denorm execution modes for different bit widths but "
1916 "denormBehaviorIndependence is "
1917 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
1918 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001919 break;
sjfrickedbd15712022-08-29 16:12:13 +09001920 }
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001921
1922 default:
1923 break;
1924 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001925 }
1926 break;
1927 }
1928
1929 case spv::ExecutionModeRoundingModeRTE: {
1930 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001931 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
sjfrickedbd15712022-08-29 16:12:13 +09001932 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001933 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001934 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001935 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
sjfrickedbd15712022-08-29 16:12:13 +09001936 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001937 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001938 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001939 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
sjfrickedbd15712022-08-29 16:12:13 +09001940 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001941 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001942 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001943 }
1944
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001945 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1946 // Register the first rounding mode found
1947 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001948 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001949 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001950 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001951 if (first_rounding_mode.second != 32 && bit_width != 32) {
sjfrickedbd15712022-08-29 16:12:13 +09001952 skip |= LogError(module_state.vk_shader_module(),
1953 "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001954 "Shader uses different rounding modes for 16 and 64-bit but "
1955 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001956 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001957 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001958 }
1959 break;
1960
Mike Schuchardt2df08912020-12-15 16:28:09 -08001961 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001962 break;
1963
sjfrickedbd15712022-08-29 16:12:13 +09001964 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: {
1965 skip |=
1966 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-roundingModeIndependence-06292",
1967 "Shader uses different rounding modes for different bit widths but "
1968 "roundingModeIndependence is "
1969 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
1970 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001971 break;
sjfrickedbd15712022-08-29 16:12:13 +09001972 }
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001973
1974 default:
1975 break;
1976 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001977 }
1978 break;
1979 }
1980
1981 case spv::ExecutionModeRoundingModeRTZ: {
1982 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001983 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
sjfrickedbd15712022-08-29 16:12:13 +09001984 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001985 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001986 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001987 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
sjfrickedbd15712022-08-29 16:12:13 +09001988 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001989 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001990 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001991 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
sjfrickedbd15712022-08-29 16:12:13 +09001992 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001993 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001994 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001995 }
1996
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001997 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1998 // Register the first rounding mode found
1999 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002000 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002001 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08002002 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002003 if (first_rounding_mode.second != 32 && bit_width != 32) {
sjfrickedbd15712022-08-29 16:12:13 +09002004 skip |= LogError(module_state.vk_shader_module(),
2005 "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002006 "Shader uses different rounding modes for 16 and 64-bit but "
2007 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002008 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002009 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002010 }
2011 break;
2012
Mike Schuchardt2df08912020-12-15 16:28:09 -08002013 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002014 break;
2015
sjfrickedbd15712022-08-29 16:12:13 +09002016 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: {
2017 skip |=
2018 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-roundingModeIndependence-06292",
2019 "Shader uses different rounding modes for different bit widths but "
2020 "roundingModeIndependence is "
2021 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
2022 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002023 break;
sjfrickedbd15712022-08-29 16:12:13 +09002024 }
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002025
2026 default:
2027 break;
2028 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002029 }
2030 break;
2031 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002032
2033 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002034 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002035 break;
2036 }
2037
2038 case spv::ExecutionModeInvocations: {
2039 invocations = insn.word(3);
2040 break;
2041 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002042
2043 case spv::ExecutionModeLocalSizeId: {
Tony-LunarG273f32f2021-09-28 08:56:30 -06002044 if (!enabled_features.core13.maintenance4) {
sjfrickedbd15712022-08-29 16:12:13 +09002045 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-LocalSizeId-06434",
Piers Daniella7f93b62021-11-20 12:32:04 -07002046 "LocalSizeId execution mode used but maintenance4 feature not enabled");
2047 }
ziga-lunargf2aa8152022-04-17 13:03:29 +02002048 if (!IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
sjfrickedbd15712022-08-29 16:12:13 +09002049 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-LocalSizeId-06433",
2050 "LocalSizeId execution mode used but maintenance4 extension is not enabled and used "
2051 "Vulkan api version is 1.2 or less");
ziga-lunargf2aa8152022-04-17 13:03:29 +02002052 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002053 break;
2054 }
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002055
2056 case spv::ExecutionModeEarlyFragmentTests: {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002057 const auto *ds_state = (pipeline) ? pipeline->DepthStencilState() : nullptr;
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002058 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) &&
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002059 (ds_state &&
2060 (ds_state->flags &
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002061 (VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_DEPTH_ACCESS_BIT_ARM |
2062 VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_STENCIL_ACCESS_BIT_ARM)) != 0)) {
2063 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09002064 module_state.vk_shader_module(), "VUID-VkGraphicsPipelineCreateInfo-flags-06591",
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002065 "The fragment shader enables early fragment tests, but VkPipelineDepthStencilStateCreateInfo::flags == "
2066 "%s",
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002067 string_VkPipelineDepthStencilStateCreateFlags(ds_state->flags).c_str());
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002068 }
2069 break;
2070 }
ziga-lunarge25f5f02022-04-16 15:07:35 +02002071 case spv::ExecutionModeSubgroupUniformControlFlowKHR: {
2072 if (!enabled_features.shader_subgroup_uniform_control_flow_features.shaderSubgroupUniformControlFlow ||
2073 (phys_dev_ext_props.subgroup_properties.supportedStages & stage) == 0 ||
sjfricke4f600c82022-06-09 14:21:32 +09002074 module_state.static_data_.has_invocation_repack_instruction) {
ziga-lunarge25f5f02022-04-16 15:07:35 +02002075 std::stringstream msg;
2076 if (!enabled_features.shader_subgroup_uniform_control_flow_features.shaderSubgroupUniformControlFlow) {
2077 msg << "shaderSubgroupUniformControlFlow feature must be enabled";
2078 } else if ((phys_dev_ext_props.subgroup_properties.supportedStages & stage) == 0) {
2079 msg << "stage" << string_VkShaderStageFlagBits(stage)
2080 << " must be in VkPhysicalDeviceSubgroupProperties::supportedStages("
2081 << string_VkShaderStageFlags(phys_dev_ext_props.subgroup_properties.supportedStages) << ")";
2082 } else {
2083 msg << "the shader must not use any invocation repack instructions";
2084 }
sjfrickedbd15712022-08-29 16:12:13 +09002085 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-SubgroupUniformControlFlowKHR-06379",
ziga-lunarge25f5f02022-04-16 15:07:35 +02002086 "If ExecutionModeSubgroupUniformControlFlowKHR is used %s.", msg.str().c_str());
2087 }
2088 } break;
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002089 }
2090 }
2091 }
2092
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002093 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002094 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
sjfrickedbd15712022-08-29 16:12:13 +09002095 skip |= LogError(module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002096 "Geometry shader entry point must have an OpExecutionMode instruction that "
2097 "specifies a maximum output vertex count that is greater than 0 and less "
2098 "than or equal to maxGeometryOutputVertices. "
2099 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002100 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002101 }
2102
2103 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
sjfrickedbd15712022-08-29 16:12:13 +09002104 skip |= LogError(module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002105 "Geometry shader entry point must have an OpExecutionMode instruction that "
2106 "specifies an invocation count that is greater than 0 and less "
2107 "than or equal to maxGeometryShaderInvocations. "
2108 "Invocations=%d, maxGeometryShaderInvocations=%d",
2109 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002110 }
2111 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002112 return skip;
2113}
2114
Chris Forbes47567b72017-06-09 12:09:45 -07002115// For given pipelineLayout verify that the set_layout_node at slot.first
2116// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002117static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002118 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002119 if (!pipelineLayout) return nullptr;
2120
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002121 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07002122
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002123 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07002124}
2125
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002126// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2127// o If there is only a vertex shader : gl_PointSize must be written when using points
2128// o If there is a geometry or tessellation shader:
2129// - If shaderTessellationAndGeometryPointSize feature is enabled:
2130// * gl_PointSize must be written in the final geometry stage
2131// - If shaderTessellationAndGeometryPointSize feature is disabled:
2132// * gl_PointSize must NOT be written and a default of 1.0 is assumed
sjfricke4f600c82022-06-09 14:21:32 +09002133bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, const SHADER_MODULE_STATE &module_state,
John Zulaufac4c6e12019-07-01 16:05:58 -06002134 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002135 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2136 return false;
2137 }
2138
2139 bool pointsize_written = false;
2140 bool skip = false;
2141
2142 // Search for PointSize built-in decorations
sjfricke4f600c82022-06-09 14:21:32 +09002143 for (const auto &set : module_state.GetBuiltinDecorationList()) {
2144 auto insn = module_state.at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002145 if (set.builtin == spv::BuiltInPointSize) {
sjfricke4f600c82022-06-09 14:21:32 +09002146 pointsize_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002147 if (pointsize_written) {
2148 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002149 }
2150 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002151 }
2152
2153 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002154 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002155 if (pointsize_written) {
sjfrickedbd15712022-08-29 16:12:13 +09002156 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002157 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2158 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002159 }
2160 } else if (!pointsize_written) {
2161 skip |=
sjfrickedbd15712022-08-29 16:12:13 +09002162 LogError(module_state.vk_shader_module(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002163 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2164 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002165 }
2166 return skip;
2167}
John Zulauf14c355b2019-06-27 16:09:37 -06002168
sjfricke4f600c82022-06-09 14:21:32 +09002169bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, const SHADER_MODULE_STATE &module_state,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002170 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2171 bool primitiverate_written = false;
2172 bool viewportindex_written = false;
2173 bool viewportmask_written = false;
2174 bool skip = false;
2175
2176 // Check if the primitive shading rate is written
sjfricke4f600c82022-06-09 14:21:32 +09002177 for (const auto &set : module_state.GetBuiltinDecorationList()) {
2178 auto insn = module_state.at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002179 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sjfricke4f600c82022-06-09 14:21:32 +09002180 primitiverate_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002181 } else if (set.builtin == spv::BuiltInViewportIndex) {
sjfricke4f600c82022-06-09 14:21:32 +09002182 viewportindex_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002183 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sjfricke4f600c82022-06-09 14:21:32 +09002184 viewportmask_written = module_state.IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002185 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002186 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2187 break;
2188 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002189 }
2190
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002191 const auto viewport_state = pipeline->ViewportState();
Tony-LunarGd44844c2021-01-22 13:24:37 -07002192 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002193 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && viewport_state) {
2194 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && viewport_state->viewportCount > 1 &&
2195 primitiverate_written) {
sjfrickedbd15712022-08-29 16:12:13 +09002196 skip |= LogError(module_state.vk_shader_module(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002197 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2198 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2199 "multiple viewports "
2200 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2201 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002202 }
2203
2204 if (primitiverate_written && viewportindex_written) {
sjfrickedbd15712022-08-29 16:12:13 +09002205 skip |= LogError(module_state.vk_shader_module(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002206 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2207 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2208 "ViewportIndex built-ins,"
2209 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2210 string_VkShaderStageFlagBits(stage));
2211 }
2212
2213 if (primitiverate_written && viewportmask_written) {
sjfrickedbd15712022-08-29 16:12:13 +09002214 skip |= LogError(module_state.vk_shader_module(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002215 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2216 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2217 "ViewportMaskNV built-ins,"
2218 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2219 string_VkShaderStageFlagBits(stage));
2220 }
2221 }
2222 return skip;
2223}
2224
sjfricke4f600c82022-06-09 14:21:32 +09002225bool CoreChecks::ValidateDecorations(const SHADER_MODULE_STATE &module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002226 bool skip = false;
2227
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002228 std::vector<spirv_inst_iter> xfb_streams;
2229 std::vector<spirv_inst_iter> xfb_buffers;
ziga-lunargef2c3172021-11-07 10:35:29 +01002230 std::vector<spirv_inst_iter> xfb_offsets;
2231
sjfricke4f600c82022-06-09 14:21:32 +09002232 for (const auto &op_decorate : module_state.GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002233 uint32_t decoration = op_decorate.word(2);
2234 if (decoration == spv::DecorationXfbStride) {
2235 uint32_t stride = op_decorate.word(3);
2236 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2237 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09002238 module_state.vk_shader_module(), "VUID-RuntimeSpirv-XfbStride-06313",
ziga-lunargce66e542021-09-19 00:11:14 +02002239 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2240 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2241 ").",
2242 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2243 }
2244 }
ziga-lunarg423cf212021-11-07 00:00:27 +01002245 if (decoration == spv::DecorationStream) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002246 xfb_streams.push_back(op_decorate);
ziga-lunarg423cf212021-11-07 00:00:27 +01002247 uint32_t stream = op_decorate.word(3);
2248 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2249 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09002250 module_state.vk_shader_module(), "VUID-RuntimeSpirv-Stream-06312",
ziga-lunarg423cf212021-11-07 00:00:27 +01002251 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2252 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32 ").",
2253 stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
2254 }
2255 }
ziga-lunargef2c3172021-11-07 10:35:29 +01002256 if (decoration == spv::DecorationXfbBuffer) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002257 xfb_buffers.push_back(op_decorate);
ziga-lunargef2c3172021-11-07 10:35:29 +01002258 }
2259 if (decoration == spv::DecorationOffset) {
2260 xfb_offsets.push_back(op_decorate);
2261 }
2262 }
2263
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002264 // XfbBuffer, buffer data size
2265 std::vector<std::pair<uint32_t, uint32_t>> buffer_data_sizes;
ziga-lunargef2c3172021-11-07 10:35:29 +01002266 for (const auto &op_decorate : xfb_offsets) {
2267 for (const auto xfb_buffer : xfb_buffers) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002268 if (xfb_buffer.word(1) == op_decorate.word(1)) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002269 const auto offset = op_decorate.word(3);
sjfricke4f600c82022-06-09 14:21:32 +09002270 const auto def = module_state.get_def(xfb_buffer.word(1));
2271 const auto size = module_state.GetTypeBytesSize(def);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002272 const uint32_t buffer_data_size = offset + size;
2273 if (buffer_data_size > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002274 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09002275 module_state.vk_shader_module(), "VUID-RuntimeSpirv-Offset-06308",
ziga-lunargef2c3172021-11-07 10:35:29 +01002276 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_offset (%" PRIu32
sjfrickedbd15712022-08-29 16:12:13 +09002277 ") + size of variable (%" PRIu32
2278 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
ziga-lunargef2c3172021-11-07 10:35:29 +01002279 "(%" PRIu32 ").",
2280 offset, size, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2281 }
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002282
2283 bool found = false;
2284 for (auto &bds : buffer_data_sizes) {
2285 if (bds.first == xfb_buffer.word(1)) {
2286 bds.second = std::max(bds.second, buffer_data_size);
2287 found = true;
2288 break;
2289 }
2290 }
2291 if (!found) {
2292 buffer_data_sizes.emplace_back(xfb_buffer.word(1), buffer_data_size);
2293 }
2294
ziga-lunargef2c3172021-11-07 10:35:29 +01002295 break;
2296 }
2297 }
ziga-lunargce66e542021-09-19 00:11:14 +02002298 }
2299
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002300 std::unordered_map<uint32_t, uint32_t> stream_data_size;
2301 for (const auto &xfb_stream : xfb_streams) {
2302 for (const auto& bds : buffer_data_sizes) {
2303 if (xfb_stream.word(1) == bds.first) {
2304 uint32_t stream = xfb_stream.word(3);
2305 const auto itr = stream_data_size.find(stream);
2306 if (itr != stream_data_size.end()) {
2307 itr->second += bds.second;
2308 } else {
2309 stream_data_size.insert({stream, bds.second});
2310 }
2311 }
2312 }
2313 }
2314
2315 for (const auto& stream : stream_data_size) {
2316 if (stream.second > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreamDataSize) {
sjfrickedbd15712022-08-29 16:12:13 +09002317 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-XfbBuffer-06309",
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002318 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2319 ") having the sum of buffer data sizes (%" PRIu32
2320 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2321 "(%" PRIu32 ").",
2322 stream.first, stream.second,
2323 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2324 }
2325 }
2326
ziga-lunargce66e542021-09-19 00:11:14 +02002327 return skip;
2328}
2329
sjfrickede734312022-07-14 19:22:43 +09002330bool CoreChecks::ValidateComputeSharedMemory(const SHADER_MODULE_STATE &module_state, uint32_t total_shared_size) const {
sjfricke44d663c2022-06-01 06:42:58 +09002331 bool skip = false;
sjfrickede734312022-07-14 19:22:43 +09002332
2333 // If not found before with spec constants, find here
2334 if (total_shared_size == 0) {
2335 // when using WorkgroupMemoryExplicitLayoutKHR
2336 // either all or none the structs are decorated with Block,
2337 // if using block, all must decorated with Aliased.
2338 // In this case we want to find the MAX not ADD the block sizes
2339 bool find_max_block = false;
2340
sjfricke44d663c2022-06-01 06:42:58 +09002341 for (auto insn : module_state.static_data_.variable_inst) {
sjfrickede734312022-07-14 19:22:43 +09002342 // StorageClass Workgroup is shared memory
2343 if (insn.word(3) == spv::StorageClassWorkgroup) {
2344 if (module_state.get_decorations(insn.word(2)).flags & decoration_set::aliased_bit) {
2345 find_max_block = true;
2346 }
2347
sjfricke44d663c2022-06-01 06:42:58 +09002348 const uint32_t result_type_id = insn.word(1);
2349 const auto result_type = module_state.get_def(result_type_id);
2350 const auto type = module_state.get_def(result_type.word(3));
sjfrickede734312022-07-14 19:22:43 +09002351 const uint32_t variable_shared_size = module_state.GetTypeBytesSize(type);
2352
2353 if (find_max_block) {
2354 total_shared_size = std::max(total_shared_size, variable_shared_size);
2355 } else {
2356 total_shared_size += variable_shared_size;
2357 }
sjfricke44d663c2022-06-01 06:42:58 +09002358 }
2359 }
sjfrickede734312022-07-14 19:22:43 +09002360 }
2361
2362 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2363 skip |=
sjfrickedbd15712022-08-29 16:12:13 +09002364 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-Workgroup-06530",
sjfrickede734312022-07-14 19:22:43 +09002365 "Shader uses %" PRIu32
2366 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
2367 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sjfricke44d663c2022-06-01 06:42:58 +09002368 }
2369 return skip;
2370}
2371
Tony-LunarG1672d002022-08-03 14:35:34 -06002372bool CoreChecks::ValidateShaderModuleId(const SHADER_MODULE_STATE &module_state, const PipelineStageState &stage_state,
2373 const safe_VkPipelineShaderStageCreateInfo *pStage, const VkPipelineCreateFlags flags) const {
2374 bool skip = false;
2375 const auto module_identifier = LvlFindInChain<VkPipelineShaderStageModuleIdentifierCreateInfoEXT>(pStage->pNext);
2376 const auto module_create_info = LvlFindInChain<VkShaderModuleCreateInfo>(pStage->pNext);
2377 if (module_identifier && (module_identifier->identifierSize > 0)) {
2378 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
2379 skip |= LogError(
2380 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-pNext-06850",
2381 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2382 "struct in the pNext chain but the shaderModuleIdentifier feature is not enabled",
2383 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2384 string_VkShaderStageFlagBits(stage_state.stage_flag));
2385 }
2386 if (!(flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT)) {
2387 skip |= LogError(
2388 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-pNext-06851",
2389 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2390 "struct in the pNext chain whose identifierSize is > 0 (%" PRIu32
2391 "), but the "
2392 "VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT bit is not set in the pipeline create flags",
2393 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2394 string_VkShaderStageFlagBits(stage_state.stage_flag), module_identifier->identifierSize);
2395 }
2396 if (module_identifier->identifierSize > VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT) {
2397 skip |= LogError(
2398 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-identifierSize-06852",
2399 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2400 "struct in the pNext chain whose identifierSize (%" PRIu32
2401 ") is > VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT (%" PRIu32 ")",
2402 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2403 string_VkShaderStageFlagBits(stage_state.stage_flag), module_identifier->identifierSize,
2404 VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT);
2405 }
2406 }
2407 if (module_identifier && module_create_info) {
2408 skip |= LogError(
2409 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06844",
2410 "%s module (stage %s) VkPipelineShaderStageCreateInfo has both a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2411 "struct and a VkShaderModuleCreateInfo struct in the pNext chain",
2412 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2413 string_VkShaderStageFlagBits(stage_state.stage_flag));
2414 }
2415 if (enabled_features.graphics_pipeline_library_features.graphicsPipelineLibrary) {
2416 if (!module_identifier && pStage->module == VK_NULL_HANDLE && !module_create_info) {
2417 skip |= LogError(
2418 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06845",
2419 "%s module (stage %s) VkPipelineShaderStageCreateInfo has no VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2420 "struct and no VkShaderModuleCreateInfo struct in the pNext chain, and module is not a valid VkShaderModule",
2421 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2422 string_VkShaderStageFlagBits(stage_state.stage_flag));
2423 }
2424 } else {
2425 if (!module_identifier && pStage->module == VK_NULL_HANDLE) {
2426 const char *vuid = IsExtEnabled(device_extensions.vk_khr_pipeline_library)
2427 ? "VUID-VkPipelineShaderStageCreateInfo-stage-06846"
2428 : "VUID-VkPipelineShaderStageCreateInfo-stage-06847";
2429 skip |= LogError(
2430 device, vuid,
2431 "%s module (stage %s) VkPipelineShaderStageCreateInfo has no VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2432 "struct in the pNext chain, the graphicsPipelineLibrary feature is not enabled, and module is not a valid "
2433 "VkShaderModule",
2434 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2435 string_VkShaderStageFlagBits(stage_state.stage_flag));
2436 }
2437 }
2438 if (module_identifier && pStage->module != VK_NULL_HANDLE) {
2439 skip |= LogError(
2440 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06848",
2441 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2442 "struct in the pNext chain, and module is not VK_NULL_HANDLE",
2443 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2444 string_VkShaderStageFlagBits(stage_state.stage_flag));
2445 }
2446 return skip;
2447}
2448
sjfricke6a03e012022-06-23 17:54:11 +09002449// Temporary data of a OpVariable when validating it.
2450// If found useful in another location, can move out to the header
2451struct VariableInstInfo {
2452 bool has_8bit = false;
2453 bool has_16bit = false;
2454};
2455
2456// easier to use recursion to traverse the OpTypeStruct
2457static void GetVariableInfo(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &insn, VariableInstInfo &info) {
sjfricke6086f792022-08-25 16:38:15 +09002458 if (insn == module_state.end()) {
2459 return;
2460 } else if (insn.opcode() == spv::OpTypeFloat || insn.opcode() == spv::OpTypeInt) {
2461 const uint32_t bit_width = insn.word(2);
2462 info.has_8bit |= (bit_width == 8);
2463 info.has_16bit |= (bit_width == 16);
sjfricke6a03e012022-06-23 17:54:11 +09002464 } else if (insn.opcode() == spv::OpTypeStruct) {
2465 for (uint32_t i = 2; i < insn.len(); i++) {
2466 const auto &base_insn = GetBaseTypeIter(module_state, insn.word(i));
2467 GetVariableInfo(module_state, base_insn, info);
2468 }
2469 }
2470}
2471
sjfricke44d663c2022-06-01 06:42:58 +09002472bool CoreChecks::ValidateVariables(const SHADER_MODULE_STATE &module_state) const {
2473 bool skip = false;
2474
2475 for (auto insn : module_state.static_data_.variable_inst) {
2476 const uint32_t storage_class = insn.word(3);
2477
2478 if (storage_class == spv::StorageClassWorkgroup) {
2479 // If Workgroup variable is initalized, make sure the feature is enabled
2480 if (insn.len() > 4 &&
ziga-lunarg3ae53422022-09-15 02:28:35 +02002481 !enabled_features.core13.shaderZeroInitializeWorkgroupMemory) {
sjfricke44d663c2022-06-01 06:42:58 +09002482 const char *vuid = IsExtEnabled(device_extensions.vk_khr_zero_initialize_workgroup_memory)
2483 ? "VUID-RuntimeSpirv-shaderZeroInitializeWorkgroupMemory-06372"
2484 : "VUID-RuntimeSpirv-OpVariable-06373";
2485 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09002486 module_state.vk_shader_module(), vuid,
sjfricke44d663c2022-06-01 06:42:58 +09002487 "vkCreateShaderModule(): "
2488 "VkPhysicalDeviceZeroInitializeWorkgroupMemoryFeaturesKHR::shaderZeroInitializeWorkgroupMemory is not enabled, "
2489 "but shader contains an OpVariable with Workgroup Storage Class with an Initializer operand.\n%s",
2490 module_state.DescribeInstruction(insn).c_str());
2491 }
2492 }
sjfricke6a03e012022-06-23 17:54:11 +09002493
2494 const auto type_pointer = module_state.get_def(insn.word(1));
2495 const auto type = module_state.get_def(type_pointer.word(3));
2496 // type will either be a float, int, or struct and if struct need to traverse it
2497 VariableInstInfo info;
2498 GetVariableInfo(module_state, type, info);
2499
2500 if (info.has_8bit) {
2501 if (!enabled_features.core12.storageBuffer8BitAccess &&
2502 (storage_class == spv::StorageClassStorageBuffer || storage_class == spv::StorageClassShaderRecordBufferKHR || storage_class == spv::StorageClassPhysicalStorageBuffer)) {
sjfrickedbd15712022-08-29 16:12:13 +09002503 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-storageBuffer8BitAccess-06328",
sjfricke6a03e012022-06-23 17:54:11 +09002504 "vkCreateShaderModule(): storageBuffer8BitAccess is not enabled, but shader contains an 8-bit "
2505 "OpVariable with %s Storage Class.\n%s",
sjfricke657dfdc2022-08-25 23:40:32 +09002506 string_SpvStorageClass(storage_class), module_state.DescribeInstruction(insn).c_str());
sjfricke6a03e012022-06-23 17:54:11 +09002507 }
2508 if (!enabled_features.core12.uniformAndStorageBuffer8BitAccess && storage_class == spv::StorageClassUniform) {
sjfrickedbd15712022-08-29 16:12:13 +09002509 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-uniformAndStorageBuffer8BitAccess-06329",
sjfricke6a03e012022-06-23 17:54:11 +09002510 "vkCreateShaderModule(): uniformAndStorageBuffer8BitAccess is not enabled, but shader contains an "
2511 "8-bit OpVariable with Uniform Storage Class.\n%s",
2512 module_state.DescribeInstruction(insn).c_str());
2513 }
2514 if (!enabled_features.core12.storagePushConstant8 && storage_class == spv::StorageClassPushConstant) {
sjfrickedbd15712022-08-29 16:12:13 +09002515 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-storagePushConstant8-06330",
sjfricke6a03e012022-06-23 17:54:11 +09002516 "vkCreateShaderModule(): storagePushConstant8 is not enabled, but shader contains an 8-bit "
2517 "OpVariable with PushConstant Storage Class.\n%s",
2518 module_state.DescribeInstruction(insn).c_str());
2519 }
2520 }
2521
2522 if (info.has_16bit) {
2523 if (!enabled_features.core11.storageBuffer16BitAccess &&
2524 (storage_class == spv::StorageClassStorageBuffer || storage_class == spv::StorageClassShaderRecordBufferKHR || storage_class == spv::StorageClassPhysicalStorageBuffer)) {
sjfrickedbd15712022-08-29 16:12:13 +09002525 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-storageBuffer16BitAccess-06331",
sjfricke6a03e012022-06-23 17:54:11 +09002526 "vkCreateShaderModule(): storageBuffer16BitAccess is not enabled, but shader contains an 16-bit "
2527 "OpVariable with %s Storage Class.\n%s",
sjfricke657dfdc2022-08-25 23:40:32 +09002528 string_SpvStorageClass(storage_class), module_state.DescribeInstruction(insn).c_str());
sjfricke6a03e012022-06-23 17:54:11 +09002529 }
2530 if (!enabled_features.core11.uniformAndStorageBuffer16BitAccess && storage_class == spv::StorageClassUniform) {
sjfrickedbd15712022-08-29 16:12:13 +09002531 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-uniformAndStorageBuffer16BitAccess-06332",
sjfricke6a03e012022-06-23 17:54:11 +09002532 "vkCreateShaderModule(): uniformAndStorageBuffer16BitAccess is not enabled, but shader contains an "
2533 "16-bit OpVariable with Uniform Storage Class.\n%s",
2534 module_state.DescribeInstruction(insn).c_str());
2535 }
2536 if (!enabled_features.core11.storagePushConstant16 && storage_class == spv::StorageClassPushConstant) {
sjfrickedbd15712022-08-29 16:12:13 +09002537 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-storagePushConstant16-06333",
sjfricke6a03e012022-06-23 17:54:11 +09002538 "vkCreateShaderModule(): storagePushConstant16 is not enabled, but shader contains an 16-bit "
2539 "OpVariable with PushConstant Storage Class.\n%s",
2540 module_state.DescribeInstruction(insn).c_str());
2541 }
2542 if (!enabled_features.core11.storageInputOutput16 &&
2543 (storage_class == spv::StorageClassInput || storage_class == spv::StorageClassOutput)) {
sjfrickedbd15712022-08-29 16:12:13 +09002544 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-storageInputOutput16-06334",
sjfricke6a03e012022-06-23 17:54:11 +09002545 "vkCreateShaderModule(): storageInputOutput16 is not enabled, but shader contains an 16-bit "
2546 "OpVariable with %s Storage Class.\n%s",
sjfricke657dfdc2022-08-25 23:40:32 +09002547 string_SpvStorageClass(storage_class), module_state.DescribeInstruction(insn).c_str());
sjfricke6a03e012022-06-23 17:54:11 +09002548 }
2549 }
sjfricke29ca0762022-08-24 14:26:33 +09002550
2551 // Checks based off shaderStorageImage(Read|Write)WithoutFormat are
2552 // disabled if VK_KHR_format_feature_flags2 is supported.
2553 //
2554 // https://github.com/KhronosGroup/Vulkan-Docs/blob/6177645341afc/appendices/spirvenv.txt#L553
2555 //
2556 // The other checks need to take into account the format features and so
2557 // we apply that in the descriptor set matching validation code (see
2558 // descriptor_sets.cpp).
2559 if (!has_format_feature2) {
2560 skip |= ValidateShaderStorageImageFormatsVariables(module_state, insn);
2561 }
sjfricke44d663c2022-06-01 06:42:58 +09002562 }
2563
2564 return skip;
2565}
2566
sjfricke4f600c82022-06-09 14:21:32 +09002567bool CoreChecks::ValidateTransformFeedback(const SHADER_MODULE_STATE &module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002568 bool skip = false;
2569
ziga-lunarg28d08792021-10-13 15:42:59 +02002570 // Temp workaround to prevent false positive errors
2571 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sjfricke4f600c82022-06-09 14:21:32 +09002572 if (module_state.HasMultipleEntryPoints()) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002573 return skip;
2574 }
2575
2576 layer_data::unordered_set<uint32_t> emitted_streams;
2577 bool output_points = false;
sjfricke4f600c82022-06-09 14:21:32 +09002578 for (const auto &insn : module_state) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002579 const uint32_t opcode = insn.opcode();
2580 if (opcode == spv::OpEmitStreamVertex) {
sjfricke4f600c82022-06-09 14:21:32 +09002581 emitted_streams.emplace(static_cast<uint32_t>(module_state.GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002582 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002583 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
sjfricke4f600c82022-06-09 14:21:32 +09002584 uint32_t stream = static_cast<uint32_t>(module_state.GetConstantValueById(insn.word(1)));
ziga-lunarg28d08792021-10-13 15:42:59 +02002585 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2586 skip |= LogError(
sjfrickedbd15712022-08-29 16:12:13 +09002587 module_state.vk_shader_module(), "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002588 "vkCreateGraphicsPipelines(): shader uses transform feedback stream\n%s\nwith index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002589 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2590 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002591 module_state.DescribeInstruction(insn).c_str(), stream,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002592 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002593 }
2594 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002595 if ((opcode == spv::OpExecutionMode || opcode == spv::OpExecutionModeId) &&
2596 insn.word(2) == spv::ExecutionModeOutputPoints) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002597 output_points = true;
2598 }
2599 }
2600
2601 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2602 if (emitted_streams_size > 1 && !output_points &&
2603 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
sjfrickedbd15712022-08-29 16:12:13 +09002604 skip |=
2605 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2606 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32
2607 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles "
2608 "is VK_FALSE, but execution mode is not OutputPoints.",
ziga-lunarg28d08792021-10-13 15:42:59 +02002609 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002610 }
2611
2612 return skip;
2613}
2614
sfricke-samsung864162a2021-11-01 21:58:01 -07002615// Checks for both TexelOffset and TexelGatherOffset limits
sjfricke4f600c82022-06-09 14:21:32 +09002616bool CoreChecks::ValidateTexelOffsetLimits(const SHADER_MODULE_STATE &module_state, spirv_inst_iter &insn) const {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002617 bool skip = false;
2618
2619 const uint32_t opcode = insn.opcode();
sfricke-samsung864162a2021-11-01 21:58:01 -07002620 if (ImageGatherOperation(opcode) || ImageSampleOperation(opcode) || ImageFetchOperation(opcode)) {
sfricke-samsung3a25ed52022-01-20 02:24:36 -08002621 uint32_t image_operand_position = OpcodeImageOperandsPosition(opcode);
sfricke-samsung864162a2021-11-01 21:58:01 -07002622 // Image operands can be optional
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002623 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2624 auto image_operand = insn.word(image_operand_position);
sfricke-samsung864162a2021-11-01 21:58:01 -07002625 // Bits we are validating (sample/fetch only check ConstOffset)
ziga-lunarga12c75a2021-09-16 16:36:16 +02002626 uint32_t offset_bits =
sfricke-samsung864162a2021-11-01 21:58:01 -07002627 ImageGatherOperation(opcode)
2628 ? (spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask)
2629 : (spv::ImageOperandsConstOffsetMask);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002630 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002631 // Operand values follow
2632 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002633 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2634 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2635 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2636 if (image_operand & i) { // If the bit is set, consume operand
2637 if (insn.len() > index && (i & offset_bits)) {
2638 uint32_t constant_id = insn.word(index);
sjfricke4f600c82022-06-09 14:21:32 +09002639 const auto &constant = module_state.get_def(constant_id);
2640 const bool is_dynamic_offset = constant == module_state.end();
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002641 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002642 for (uint32_t j = 3; j < constant.len(); ++j) {
2643 uint32_t comp_id = constant.word(j);
sjfricke4f600c82022-06-09 14:21:32 +09002644 const auto &comp = module_state.get_def(comp_id);
2645 const auto &comp_type = module_state.get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002646 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002647 const uint32_t offset = comp.word(3);
sfricke-samsung864162a2021-11-01 21:58:01 -07002648 // spec requires minTexelGatherOffset/minTexelOffset to be -8 or less so never can compare if
2649 // unsigned spec requires maxTexelGatherOffset/maxTexelOffset to be 7 or greater so never can
2650 // compare if signed is less then zero
sfricke-samsungef3fe742021-10-06 10:51:34 -07002651 const int32_t signed_offset = static_cast<int32_t>(offset);
2652 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2653
sfricke-samsung864162a2021-11-01 21:58:01 -07002654 // There are 2 sets of VU being covered where the only main difference is the opcode
2655 if (ImageGatherOperation(opcode)) {
2656 // min/maxTexelGatherOffset
2657 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
2658 skip |=
sjfrickedbd15712022-08-29 16:12:13 +09002659 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002660 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002661 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002662 module_state.DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002663 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002664 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2665 (!use_signed || (use_signed && signed_offset > 0))) {
sjfrickedbd15712022-08-29 16:12:13 +09002666 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-OpImage-06377",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002667 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2668 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32
2669 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002670 module_state.DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002671 phys_dev_props.limits.maxTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002672 }
2673 } else {
2674 // min/maxTexelOffset
2675 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelOffset)) {
sjfrickedbd15712022-08-29 16:12:13 +09002676 skip |=
2677 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-OpImageSample-06435",
2678 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
2679 ") less than VkPhysicalDeviceLimits::minTexelOffset (%" PRIi32 ").",
2680 module_state.DescribeInstruction(insn).c_str(), signed_offset,
2681 phys_dev_props.limits.minTexelOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002682 } else if ((offset > phys_dev_props.limits.maxTexelOffset) &&
2683 (!use_signed || (use_signed && signed_offset > 0))) {
sjfrickedbd15712022-08-29 16:12:13 +09002684 skip |=
2685 LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-OpImageSample-06436",
2686 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2687 ") greater than VkPhysicalDeviceLimits::maxTexelOffset (%" PRIu32 ").",
2688 module_state.DescribeInstruction(insn).c_str(), offset,
2689 phys_dev_props.limits.maxTexelOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002690 }
ziga-lunarga12c75a2021-09-16 16:36:16 +02002691 }
2692 }
2693 }
2694 }
sfricke-samsung3511e312021-11-04 21:14:31 -07002695 index += ImageOperandsParamCount(i);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002696 }
2697 }
2698 }
2699 }
2700 }
2701
2702 return skip;
2703}
2704
sjfricke4f600c82022-06-09 14:21:32 +09002705bool CoreChecks::ValidateShaderClock(const SHADER_MODULE_STATE &module_state, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002706 bool skip = false;
2707
sfricke-samsung94167ca2021-02-26 04:14:59 -08002708 switch (insn.opcode()) {
2709 case spv::OpReadClockKHR: {
sjfricke4f600c82022-06-09 14:21:32 +09002710 auto scope_id = module_state.get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -08002711 auto scope_type = scope_id.word(3);
2712 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002713 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002714 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002715 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002716 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2717 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002718 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002719 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002720 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002721 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2722 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002723 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002724 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002725 }
2726 }
2727 return skip;
2728}
2729
sjfricke986f45b2022-09-12 16:35:56 +09002730bool CoreChecks::ValidateImageWrite(const SHADER_MODULE_STATE &module_state, spirv_inst_iter &insn) const {
2731 bool skip = false;
2732
2733 if (insn.opcode() == spv::OpImageWrite) {
2734 // guaranteed by spirv-val to be an OpTypeImage
2735 const uint32_t image = module_state.GetTypeId(insn.word(1));
2736 const spirv_inst_iter image_def = module_state.get_def(image);
2737 const uint32_t image_format = image_def.word(8);
2738 // If format is 'Unknown' then need to wait until a descriptor is bound to it
2739 if (image_format != spv::ImageFormatUnknown) {
2740 const VkFormat compatible_format = CompatibleSpirvImageFormat(image_format);
2741 if (compatible_format != VK_FORMAT_UNDEFINED) {
2742 const uint32_t format_component_count = FormatComponentCount(compatible_format);
2743 const spirv_inst_iter texel_def = module_state.get_def(insn.word(3));
2744 const spirv_inst_iter texel_type = module_state.get_def(texel_def.word(1));
2745 const uint32_t texel_component_count = (texel_type.opcode() == spv::OpTypeVector) ? texel_type.word(3) : 1;
2746 if (texel_component_count < format_component_count) {
2747 skip |= LogError(device, " VUID-RuntimeSpirv-OpImageWrite-07112",
2748 "%s: OpImageWrite Texel operand only contains %" PRIu32
2749 " components, but the OpImage format mapping to %s has %" PRIu32 " components.\n%s\n%s",
2750 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), texel_component_count,
2751 string_VkFormat(compatible_format), format_component_count,
2752 module_state.DescribeInstruction(insn).c_str(),
2753 module_state.DescribeInstruction(image_def).c_str());
2754 }
2755 }
2756 }
2757 }
2758 return skip;
2759}
2760
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002761bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2762 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002763 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002764 const auto *pStage = stage_state.create_info;
sjfricke4f600c82022-06-09 14:21:32 +09002765 const SHADER_MODULE_STATE &module_state = *stage_state.module_state.get();
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002766 const auto &entrypoint = stage_state.entrypoint;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002767
Tony-LunarG1672d002022-08-03 14:35:34 -06002768 skip |= ValidateShaderModuleId(module_state, stage_state, pStage, pipeline->GetPipelineCreateFlags());
2769
Tony-LunarGcab5d812022-08-04 14:07:32 -06002770 if (module_state.vk_shader_module() == VK_NULL_HANDLE) return skip; // No real shader for further validation
2771
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002772 // to prevent const_cast on pipeline object, just store here as not needed outside function anyway
2773 uint32_t local_size_x = 0;
2774 uint32_t local_size_y = 0;
2775 uint32_t local_size_z = 0;
sjfrickede734312022-07-14 19:22:43 +09002776 uint32_t total_shared_size = 0;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002777
John Zulauf14c355b2019-06-27 16:09:37 -06002778 // Check the module
sjfricke4f600c82022-06-09 14:21:32 +09002779 if (!module_state.has_valid_spirv) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002780 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2781 "%s does not contain valid spirv for stage %s.",
sjfricke4f600c82022-06-09 14:21:32 +09002782 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
sfricke-samsungef15e482022-01-26 11:32:49 -08002783 string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002784 }
2785
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002786 // If specialization-constant instructions are present in the shader, the specializations should be applied.
sjfricke4f600c82022-06-09 14:21:32 +09002787 if (module_state.HasSpecConstants()) {
sfricke-samsung5628f982021-10-19 09:21:59 -07002788 // both spirv-opt and spirv-val will use the same flags
2789 spvtools::ValidatorOptions options;
2790 AdjustValidatorOptions(device_extensions, enabled_features, options);
2791
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002792 // setup the call back if the optimizer fails
sfricke-samsung45996a42021-09-16 13:45:27 -07002793 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002794 spvtools::Optimizer optimizer(spirv_environment);
sfricke-samsungef15e482022-01-26 11:32:49 -08002795 spvtools::MessageConsumer consumer = [&skip, &module_state, &stage_state, this](
2796 spv_message_level_t level, const char *source, const spv_position_t &position,
2797 const char *message) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002798 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2799 "%s does not contain valid spirv for stage %s. %s",
sjfricke4f600c82022-06-09 14:21:32 +09002800 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002801 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002802 };
2803 optimizer.SetMessageConsumer(consumer);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002804
2805 // The app might be using the default spec constant values, but if they pass values at runtime to the pipeline then need to
2806 // use those values to apply to the spec constants
2807 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2808 pStage->pSpecializationInfo->pMapEntries != nullptr) {
2809 // Gather the specialization-constant values.
2810 auto const &specialization_info = pStage->pSpecializationInfo;
2811 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
2812 std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; // note: this must be std:: to work with spvtools
2813 id_value_map.reserve(specialization_info->mapEntryCount);
2814 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2815 auto const &map_entry = specialization_info->pMapEntries[i];
sjfricke4f600c82022-06-09 14:21:32 +09002816 const auto itr = module_state.GetSpecConstMap().find(map_entry.constantID);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002817 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2818 // behavior of the pipeline."
sjfricke4f600c82022-06-09 14:21:32 +09002819 if (itr != module_state.GetSpecConstMap().cend()) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002820 // Make sure map_entry.size matches the spec constant's size
2821 uint32_t spec_const_size = decoration_set::kInvalidValue;
sjfricke4f600c82022-06-09 14:21:32 +09002822 const auto def_ins = module_state.get_def(itr->second);
2823 const auto type_ins = module_state.get_def(def_ins.word(1));
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002824 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2825 switch (type_ins.opcode()) {
2826 case spv::OpTypeBool:
2827 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2828 spec_const_size = sizeof(VkBool32);
2829 break;
2830 case spv::OpTypeInt:
2831 case spv::OpTypeFloat:
2832 spec_const_size = type_ins.word(2) / 8;
2833 break;
2834 default:
2835 // spirv-val should catch if SpecId is not used on a
2836 // OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant and OpSpecConstant is validated to be a
2837 // OpTypeInt or OpTypeFloat
2838 break;
2839 }
2840
2841 if (map_entry.size != spec_const_size) {
2842 skip |= LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
2843 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2844 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32
2845 " from shader definition.",
2846 map_entry.constantID, i, map_entry.size,
sjfricke4f600c82022-06-09 14:21:32 +09002847 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002848 }
2849 }
2850
2851 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
2852 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2853 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2854 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2855 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2856 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2857
2858 std::copy(start_in_p, end_in_p, out_p);
2859 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
2860 }
2861 }
2862
2863 // This pass takes the runtime spec const values and applies it into the SPIR-V
2864 // will turn a spec constant like
2865 // OpSpecConstant %uint 1
2866 // to a use the value passed in instead (for example if the value is 32) so now it looks like
2867 // OpSpecConstant %uint 32
2868 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2869 }
2870
2871 // This pass will turn OpSpecConstant into a OpConstant (also OpSpecConstantTrue/OpSpecConstantFalse)
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002872 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002873 // Using the new frozen OpConstant all OpSpecConstantComposite can be resolved turning them into OpConstantComposite
2874 // This is need incase a shdaer looks like:
2875 //
2876 // layout(constant_id = 0) const uint x = 64;
2877 // shared uint arr[x > 64 ? 64 : x];
2878 //
2879 // this will generate branch/switch statements that we want to leverage spirv-opt to apply to make parsing easier
2880 optimizer.RegisterPass(spvtools::CreateFoldSpecConstantOpAndCompositePass());
sjfricke284a13f2022-08-16 15:34:31 +09002881 // Currently need to re-run the pass as spirv-opt has a bug and not folding everything sometimes
2882 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/pull/4399#issuecomment-1216203563
2883 optimizer.RegisterPass(spvtools::CreateFoldSpecConstantOpAndCompositePass());
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002884
2885 // Apply the specialization-constant values and revalidate the shader module is valid.
Tony-LunarG1672d002022-08-03 14:35:34 -06002886 const char *pSpecializationInfo_vuid = IsExtEnabled(device_extensions.vk_ext_shader_module_identifier)
2887 ? "VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06849"
2888 : "VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06719";
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002889 std::vector<uint32_t> specialized_spirv;
sfricke-samsungef15e482022-01-26 11:32:49 -08002890 auto const optimized =
sjfricke4f600c82022-06-09 14:21:32 +09002891 optimizer.Run(module_state.words.data(), module_state.words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002892 if (optimized) {
2893 spv_context ctx = spvContextCreate(spirv_environment);
2894 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2895 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002896 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2897 if (spv_valid != SPV_SUCCESS) {
Tony-LunarG1672d002022-08-03 14:35:34 -06002898 skip |= LogError(device, pSpecializationInfo_vuid,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002899 "After specialization was applied, %s does not contain valid spirv for stage %s.",
sjfricke4f600c82022-06-09 14:21:32 +09002900 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002901 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002902 }
2903
sjfrickea11e42e2022-07-20 14:27:01 +09002904 // The new optimized SPIR-V will NOT match the original SHADER_MODULE_STATE object parsing, so a new SHADER_MODULE_STATE
2905 // object is needed. This an issue due to each pipeline being able to reuse the same shader module but with different
2906 // spec constant values.
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002907 SHADER_MODULE_STATE spec_mod(specialized_spirv);
2908
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002909 // According to https://github.com/KhronosGroup/Vulkan-Docs/issues/1671 anything labeled as "static use" (such as if an
2910 // input is used or not) don't have to be checked post spec constants freezing since the device compiler is not
2911 // guaranteed to run things such as dead-code elimination. The following checks are things that don't follow under
2912 // "static use" rules and need to be validated still.
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002913 auto specialized_it = spec_mod.begin();
sjfrickede734312022-07-14 19:22:43 +09002914
2915 // see ValidateComputeSharedMemory() for details why we might track max block size
2916 layer_data::unordered_set<uint32_t> aliased_id;
2917 bool find_max_block = false;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002918
2919 uint32_t workgroup_size_id = 0; // result id can't be zero
2920 uint32_t local_size_id_x = 0;
2921 uint32_t local_size_id_y = 0;
2922 uint32_t local_size_id_z = 0;
2923
2924 // make single interation through new shader
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002925 while (specialized_it != spec_mod.end()) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002926 const uint32_t opcode = specialized_it.opcode();
2927
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002928 if (opcode == spv::OpExecutionModeId && specialized_it.word(2) == spv::ExecutionModeLocalSizeId) {
2929 local_size_id_x = specialized_it.word(3);
2930 local_size_id_y = specialized_it.word(4);
2931 local_size_id_z = specialized_it.word(5);
2932 }
2933
sjfrickede734312022-07-14 19:22:43 +09002934 if (opcode == spv::OpDecorate) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002935 // Validate applied WorkgroupSize is still below maxComputeWorkGroupSize limit
sjfrickede734312022-07-14 19:22:43 +09002936 if (specialized_it.word(2) == spv::DecorationBuiltIn && specialized_it.word(3) == spv::BuiltInWorkgroupSize) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002937 // Will be a OpConstantComposite and always have the OpDecorate section
2938 workgroup_size_id = specialized_it.word(1);
2939 }
sjfrickede734312022-07-14 19:22:43 +09002940 if (specialized_it.word(2) == spv::DecorationAliased) {
2941 aliased_id.emplace(specialized_it.word(1));
2942 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002943 }
2944
2945 if (opcode == spv::OpConstantComposite && workgroup_size_id == specialized_it.word(2)) {
2946 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32 so this can be assuemd
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002947 local_size_x = spec_mod.get_def(specialized_it.word(3)).word(3);
2948 local_size_y = spec_mod.get_def(specialized_it.word(4)).word(3);
2949 local_size_z = spec_mod.get_def(specialized_it.word(5)).word(3);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002950 }
sjfrickede734312022-07-14 19:22:43 +09002951
2952 if (opcode == spv::OpVariable && specialized_it.word(3) == spv::StorageClassWorkgroup) {
2953 if (aliased_id.find(specialized_it.word(2)) != aliased_id.end()) {
2954 find_max_block = true;
2955 }
2956
2957 const uint32_t result_type_id = specialized_it.word(1);
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002958 const auto result_type = spec_mod.get_def(result_type_id);
2959 const auto type = spec_mod.get_def(result_type.word(3));
2960 const uint32_t variable_shared_size = spec_mod.GetTypeBitsSize(type) / 8;
sjfrickede734312022-07-14 19:22:43 +09002961
2962 if (find_max_block) {
2963 total_shared_size = std::max(total_shared_size, variable_shared_size);
2964 } else {
2965 total_shared_size += variable_shared_size;
2966 }
2967 }
2968
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002969 ++specialized_it;
2970 }
2971
2972 // if after no WorkgroupSize is found, then can apply any possible LocalSizeId due to precedence order
2973 if (local_size_x == 0 && local_size_id_x != 0) {
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002974 local_size_x = spec_mod.get_def(local_size_id_x).word(3);
2975 local_size_y = spec_mod.get_def(local_size_id_y).word(3);
2976 local_size_z = spec_mod.get_def(local_size_id_z).word(3);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002977 }
2978
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002979 spvDiagnosticDestroy(diag);
2980 spvContextDestroy(ctx);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002981 } else {
2982 // Should never get here, but better then asserting
Tony-LunarG1672d002022-08-03 14:35:34 -06002983 skip |= LogError(device, pSpecializationInfo_vuid,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002984 "%s module (stage %s) attempted to apply specialization constants with spirv-opt but failed.",
sjfricke4f600c82022-06-09 14:21:32 +09002985 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002986 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002987 }
2988 }
2989
John Zulauf14c355b2019-06-27 16:09:37 -06002990 // Check the entrypoint
sjfricke4f600c82022-06-09 14:21:32 +09002991 if (entrypoint == module_state.end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002992 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2993 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002994 }
2995 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2996
2997 // Mark accessible ids
2998 auto &accessible_ids = stage_state.accessible_ids;
2999
Chris Forbes47567b72017-06-09 12:09:45 -07003000 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07003001
sfricke-samsung94167ca2021-02-26 04:14:59 -08003002 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
3003 // and mainly only checking the instruction in detail for a single operation
sjfricke4f600c82022-06-09 14:21:32 +09003004 for (auto insn : module_state) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003005 skip |= ValidateTexelOffsetLimits(module_state, insn);
3006 skip |= ValidateShaderCapabilitiesAndExtensions(insn);
3007 skip |= ValidateShaderClock(module_state, insn);
3008 skip |= ValidateShaderStageGroupNonUniform(module_state, pStage->stage, insn);
3009 skip |= ValidateMemoryScope(module_state, insn);
sjfricke986f45b2022-09-12 16:35:56 +09003010 skip |= ValidateImageWrite(module_state, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02003011 }
3012
sfricke-samsungef15e482022-01-26 11:32:49 -08003013 skip |= ValidateTransformFeedback(module_state);
sjfrickedbd15712022-08-29 16:12:13 +09003014 skip |= ValidateShaderStageWritableOrAtomicDescriptor(module_state, pStage->stage, stage_state.has_writable_descriptor,
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003015 stage_state.has_atomic_descriptor);
sfricke-samsungef15e482022-01-26 11:32:49 -08003016 skip |= ValidateShaderStageInputOutputLimits(module_state, pStage, pipeline, entrypoint);
sjfrickedbd15712022-08-29 16:12:13 +09003017 skip |= ValidateShaderStageMaxResources(module_state, pStage->stage, pipeline);
sfricke-samsungef15e482022-01-26 11:32:49 -08003018 skip |= ValidateAtomicsTypes(module_state);
3019 skip |= ValidateExecutionModes(module_state, entrypoint, pStage->stage, pipeline);
sjfrickedbd15712022-08-29 16:12:13 +09003020 skip |= ValidateSpecializations(module_state, pStage);
sfricke-samsungef15e482022-01-26 11:32:49 -08003021 skip |= ValidateDecorations(module_state);
sjfricke4f600c82022-06-09 14:21:32 +09003022 skip |= ValidateVariables(module_state);
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003023 const auto *raster_state = pipeline->RasterizationState();
Nathaniel Cesario81257cb2022-02-16 17:15:58 -07003024 if (check_point_size && raster_state && !raster_state->rasterizerDiscardEnable) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003025 skip |= ValidatePointListShaderState(pipeline, module_state, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003026 }
sfricke-samsungef15e482022-01-26 11:32:49 -08003027 skip |= ValidateBuiltinLimits(module_state, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08003028 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003029 skip |= ValidateCooperativeMatrix(module_state, pStage, pipeline);
sfricke-samsungd093e522021-02-26 04:17:45 -08003030 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00003031 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003032 skip |= ValidatePrimitiveRateShaderState(pipeline, module_state, entrypoint, pStage->stage);
Tobias Hector6663c9b2020-11-05 10:18:02 +00003033 }
sfricke-samsung45996a42021-09-16 13:45:27 -07003034 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003035 skip |= ValidateShaderResolveQCOM(module_state, pStage, pipeline);
Jeff Leger9b3dcff2021-05-27 15:40:20 -04003036 }
ziga-lunarg73163742021-08-25 13:15:29 +02003037 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
sjfrickedbd15712022-08-29 16:12:13 +09003038 skip |= ValidateShaderSubgroupSizeControl(module_state, pStage);
ziga-lunarg73163742021-08-25 13:15:29 +02003039 }
Chris Forbes47567b72017-06-09 12:09:45 -07003040
sfricke-samsung7699b912021-04-12 23:01:51 -07003041 // "layout must be consistent with the layout of the * shader"
3042 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003043 std::string vuid_layout_mismatch;
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003044 switch (pipeline->GetCreateInfoSType()) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003045 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
3046 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
3047 break;
3048 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
3049 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
3050 break;
3051 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
3052 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
3053 break;
3054 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
3055 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
3056 break;
3057 default:
3058 assert(false);
3059 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003060 }
3061
sfricke-samsung7699b912021-04-12 23:01:51 -07003062 // Validate Push Constants use
sfricke-samsungef15e482022-01-26 11:32:49 -08003063 skip |= ValidatePushConstantUsage(*pipeline, module_state, pStage, vuid_layout_mismatch);
sfricke-samsung7699b912021-04-12 23:01:51 -07003064
Chris Forbes47567b72017-06-09 12:09:45 -07003065 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003066 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07003067 // Verify given pipelineLayout has requested setLayout with requested binding
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003068 // const auto& layout_state = (stage_state.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) ?
3069 // pipeline->PreRasterPipelineLayoutState() : pipeline->FragmentShaderPipelineLayoutState();
3070 const auto &binding = GetDescriptorBinding(pipeline->PipelineLayoutState().get(), use.first);
3071 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07003072 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
3073 std::set<uint32_t> descriptor_types =
sfricke-samsungef15e482022-01-26 11:32:49 -08003074 TypeToDescriptorTypeSet(module_state, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07003075
3076 if (!binding) {
sjfrickedbd15712022-08-29 16:12:13 +09003077 LogObjectList objlist(module_state.vk_shader_module());
3078 objlist.add(pipeline->PipelineLayoutState()->layout());
3079 skip |= LogError(objlist, vuid_layout_mismatch,
3080 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout", use.first.set,
3081 use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07003082 } else if (~binding->stageFlags & pStage->stage) {
sjfrickedbd15712022-08-29 16:12:13 +09003083 LogObjectList objlist(module_state.vk_shader_module());
3084 objlist.add(pipeline->PipelineLayoutState()->layout());
3085 skip |= LogError(objlist, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003086 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
3087 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Mike Schuchardt2d523e52022-09-15 12:25:58 -07003088 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_EXT) &&
Tony-LunarGf563b362021-03-18 16:13:18 -06003089 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
sjfrickedbd15712022-08-29 16:12:13 +09003090 LogObjectList objlist(module_state.vk_shader_module());
3091 objlist.add(pipeline->PipelineLayoutState()->layout());
3092 skip |= LogError(objlist, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003093 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
3094 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003095 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07003096 } else if (binding->descriptorCount < required_descriptor_count) {
sjfrickedbd15712022-08-29 16:12:13 +09003097 LogObjectList objlist(module_state.vk_shader_module());
3098 objlist.add(pipeline->PipelineLayoutState()->layout());
3099 skip |= LogError(objlist, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003100 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003101 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07003102 }
3103 }
3104
3105 // Validate use of input attachments against subpass structure
3106 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09003107 auto input_attachment_uses = module_state.CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07003108
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003109 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06003110 if (rp_state && !rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003111 auto rpci = rp_state->createInfo.ptr();
3112 auto subpass = pipeline->Subpass();
amhagana448ea52021-11-02 14:09:14 -04003113 for (auto use : input_attachment_uses) {
3114 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
3115 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
3116 ? input_attachments[use.first].attachment
3117 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07003118
amhagana448ea52021-11-02 14:09:14 -04003119 if (index == VK_ATTACHMENT_UNUSED) {
sjfrickedbd15712022-08-29 16:12:13 +09003120 LogObjectList objlist(module_state.vk_shader_module());
3121 objlist.add(pipeline->PipelineLayoutState()->layout());
3122 skip |= LogError(objlist, kVUID_Core_Shader_MissingInputAttachment,
3123 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsungef15e482022-01-26 11:32:49 -08003124 } else if (!(GetFormatType(rpci->pAttachments[index].format) &
sjfricke4f600c82022-06-09 14:21:32 +09003125 module_state.GetFundamentalType(use.second.type_id))) {
sjfrickedbd15712022-08-29 16:12:13 +09003126 LogObjectList objlist(module_state.vk_shader_module());
3127 objlist.add(pipeline->PipelineLayoutState()->layout());
3128 skip |= LogError(objlist, kVUID_Core_Shader_InputAttachmentTypeMismatch,
sfricke-samsungef15e482022-01-26 11:32:49 -08003129 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
3130 string_VkFormat(rpci->pAttachments[index].format),
sjfricke4f600c82022-06-09 14:21:32 +09003131 module_state.DescribeType(use.second.type_id).c_str());
amhagana448ea52021-11-02 14:09:14 -04003132 }
Chris Forbes47567b72017-06-09 12:09:45 -07003133 }
3134 }
3135 }
Lockeaa8fdc02019-04-02 11:59:20 -06003136 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003137 skip |= ValidateComputeWorkGroupSizes(module_state, entrypoint, stage_state, local_size_x, local_size_y, local_size_z);
sjfrickede734312022-07-14 19:22:43 +09003138 skip |= ValidateComputeSharedMemory(module_state, total_shared_size);
Lockeaa8fdc02019-04-02 11:59:20 -06003139 }
ziga-lunarg73163742021-08-25 13:15:29 +02003140
Chris Forbes47567b72017-06-09 12:09:45 -07003141 return skip;
3142}
3143
sjfricke4f600c82022-06-09 14:21:32 +09003144bool CoreChecks::ValidateInterfaceBetweenStages(const SHADER_MODULE_STATE &producer, spirv_inst_iter producer_entrypoint,
3145 shader_stage_attributes const *producer_stage, const SHADER_MODULE_STATE &consumer,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003146 spirv_inst_iter consumer_entrypoint,
3147 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07003148 bool skip = false;
3149
3150 auto outputs =
sjfricke4f600c82022-06-09 14:21:32 +09003151 producer.CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
3152 auto inputs = consumer.CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07003153
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003154 auto output_it = outputs.begin();
3155 auto input_it = inputs.begin();
Chris Forbes47567b72017-06-09 12:09:45 -07003156
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003157 uint32_t output_component = 0;
3158 uint32_t input_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003159
Chris Forbes47567b72017-06-09 12:09:45 -07003160 // Maps sorted by key (location); walk them together to find mismatches
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003161 while ((outputs.size() > 0 && output_it != outputs.end()) || (inputs.size() && input_it != inputs.end())) {
3162 bool output_at_end = outputs.size() == 0 || output_it == outputs.end();
3163 bool input_at_end = inputs.size() == 0 || input_it == inputs.end();
3164 auto output_first = output_at_end ? std::make_pair(0u, 0u) : output_it->first;
3165 auto input_first = input_at_end ? std::make_pair(0u, 0u) : input_it->first;
Chris Forbes47567b72017-06-09 12:09:45 -07003166
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003167 output_first.second += output_component;
3168 input_first.second += input_component;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003169
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003170 const auto output_length =
3171 output_at_end ? 0 : producer.GetNumComponentsInBaseType(producer.get_def(output_it->second.type_id));
3172 const auto input_length =
3173 input_at_end ? 0 : consumer.GetNumComponentsInBaseType(consumer.get_def(input_it->second.type_id));
3174 assert(output_at_end || output_component < output_length);
3175 assert(input_at_end || input_component < input_length);
ziga-lunarg8346fe82021-08-22 17:30:50 +02003176
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003177 if (input_at_end || ((!output_at_end) && (output_first < input_first))) {
Stefan Dobrica43c84ca2022-05-30 16:22:36 +02003178 if (!enabled_features.core13.maintenance4) {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003179 const std::string msg = std::string{producer_stage->name} + " writes to output location " +
3180 std::to_string(output_first.first) + "." + std::to_string(output_first.second) +
3181 " which is not consumed by " + consumer_stage->name +
Nathaniel Cesario09fbe8a2022-08-03 16:24:25 -06003182 ". "
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003183 "Enable VK_KHR_maintenance4 device extension to allow relaxed interface matching between "
3184 "input and output vectors.";
Nathaniel Cesario09fbe8a2022-08-03 16:24:25 -06003185 // It is not an error if a stage does not consume all outputs from the previous stage
3186 skip |= LogPerformanceWarning(producer.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed, "%s", msg.c_str());
Stefan Dobrica43c84ca2022-05-30 16:22:36 +02003187 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003188 if ((input_first.first > output_first.first) || input_at_end || (output_component + 1 == output_length)) {
3189 output_it++;
3190 output_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003191 } else {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003192 output_component++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003193 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003194 } else if (output_at_end || output_first > input_first) {
sjfricke4f600c82022-06-09 14:21:32 +09003195 skip |= LogError(consumer.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02003196 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003197 input_first.first, input_first.second, producer_stage->name);
3198 if ((output_first.first > input_first.first) || output_at_end || (input_component + 1 == input_length)) {
3199 input_it++;
3200 input_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003201 } else {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003202 input_component++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003203 }
Chris Forbes47567b72017-06-09 12:09:45 -07003204 } else {
3205 // subtleties of arrayed interfaces:
3206 // - if is_patch, then the member is not arrayed, even though the interface may be.
3207 // - if is_block_member, then the extra array level of an arrayed interface is not
3208 // expressed in the member type -- it's expressed in the block type.
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003209 if (!TypesMatch(producer, consumer, output_it->second.type_id, input_it->second.type_id)) {
sjfricke4f600c82022-06-09 14:21:32 +09003210 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarge640e802022-04-04 21:36:53 +02003211 "Type mismatch on location %" PRIu32 ".%" PRIu32 ", between %s and %s: '%s' vs '%s'",
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003212 output_first.first, output_first.second, producer_stage->name, consumer_stage->name,
3213 producer.DescribeType(output_it->second.type_id).c_str(),
3214 consumer.DescribeType(input_it->second.type_id).c_str());
3215 output_it++;
3216 input_it++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003217 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07003218 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003219 if (output_it->second.is_patch != input_it->second.is_patch) {
sjfricke4f600c82022-06-09 14:21:32 +09003220 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
3221 "Decoration mismatch on location %" PRIu32 ".%" PRIu32
3222 ": is per-%s in %s stage but per-%s in %s stage",
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003223 output_first.first, output_first.second, output_it->second.is_patch ? "patch" : "vertex",
3224 producer_stage->name, input_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07003225 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003226 uint32_t output_remaining = output_length - output_component;
3227 uint32_t input_remaining = input_length - input_component;
3228 if (output_remaining == input_remaining) { // Sizes match so we can advance both output_it and input_it
3229 output_it++;
3230 input_it++;
3231 output_component = 0;
3232 input_component = 0;
3233 } else if (output_remaining > input_remaining) { // a has more components remaining
3234 output_component += input_remaining;
3235 input_component = 0;
3236 input_it++;
3237 } else if (input_remaining > output_remaining) { // b has more components remaining
3238 input_component += output_remaining;
3239 output_component = 0;
3240 output_it++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003241 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003242 if (output_component == 4) {
3243 output_component = 0;
3244 output_it++;
ziga-lunargb9fa0eb2022-04-01 23:31:06 +02003245 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003246 if (input_component == 4) {
3247 input_component = 0;
3248 input_it++;
ziga-lunargb9fa0eb2022-04-01 23:31:06 +02003249 }
Chris Forbes47567b72017-06-09 12:09:45 -07003250 }
3251 }
3252
Ari Suonpaa696b3432019-03-11 14:02:57 +02003253 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09003254 auto builtins_producer = producer.CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
3255 auto builtins_consumer = consumer.CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02003256
3257 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
3258 if (builtins_producer.size() != builtins_consumer.size()) {
sjfricke4f600c82022-06-09 14:21:32 +09003259 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003260 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003261 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
3262 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02003263 } else {
3264 auto it_producer = builtins_producer.begin();
3265 auto it_consumer = builtins_consumer.begin();
3266 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
3267 if (*it_producer != *it_consumer) {
sjfricke4f600c82022-06-09 14:21:32 +09003268 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003269 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
3270 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02003271 break;
3272 }
3273 it_producer++;
3274 it_consumer++;
3275 }
3276 }
3277 }
3278 }
3279
Chris Forbes47567b72017-06-09 12:09:45 -07003280 return skip;
3281}
3282
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003283static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE &pipeline) {
3284 uint32_t stage_mask = pipeline.active_shaders;
3285 if (pipeline.topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003286 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05003287 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
3288 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
3289 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003290 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
3291 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
3292 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
3293 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
3294 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003295 }
3296 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003297 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003298}
3299
Chris Forbes47567b72017-06-09 12:09:45 -07003300// Validate that the shaders used by the given pipeline and store the active_slots
3301// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06003302bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Chris Forbes47567b72017-06-09 12:09:45 -07003303 bool skip = false;
3304
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003305 if (pipeline->IsGraphicsLibrary()) {
3306 // Only validate stages in an executable pipeline, not a graphics library
3307 // TODO This currently makes executing executable pipeline more expensive than they need to be since we could be validating
3308 // more per library.
3309 return skip;
3310 }
3311
3312 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(*pipeline);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003313
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003314 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
3315 for (auto &stage : pipeline->stage_state) {
3316 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
3317 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
3318 vertex_stage = &stage;
3319 }
3320 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
3321 fragment_stage = &stage;
3322 }
Chris Forbes47567b72017-06-09 12:09:45 -07003323 }
3324
3325 // if the shader stages are no good individually, cross-stage validation is pointless.
3326 if (skip) return true;
3327
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003328 auto vi_state = pipeline->InputState();
Chris Forbes47567b72017-06-09 12:09:45 -07003329
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003330 if (vi_state) {
3331 skip |= ValidateViConsistency(vi_state);
Chris Forbes47567b72017-06-09 12:09:45 -07003332 }
3333
sfricke-samsungef15e482022-01-26 11:32:49 -08003334 if (vertex_stage && vertex_stage->module_state->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
sjfricke4f600c82022-06-09 14:21:32 +09003335 skip |= ValidateViAgainstVsInputs(vi_state, *vertex_stage->module_state.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07003336 }
3337
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003338 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
3339 const auto &producer = pipeline->stage_state[i - 1];
3340 const auto &consumer = pipeline->stage_state[i];
sfricke-samsungef15e482022-01-26 11:32:49 -08003341 assert(producer.module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003342 if (&producer == fragment_stage) {
3343 break;
3344 }
sfricke-samsungef15e482022-01-26 11:32:49 -08003345 if (consumer.module_state) {
3346 if (consumer.module_state->has_valid_spirv && producer.module_state->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003347 auto producer_id = GetShaderStageId(producer.stage_flag);
3348 auto consumer_id = GetShaderStageId(consumer.stage_flag);
sjfricke4f600c82022-06-09 14:21:32 +09003349 skip |= ValidateInterfaceBetweenStages(*producer.module_state.get(), producer.entrypoint,
3350 &shader_stage_attribs[producer_id], *consumer.module_state.get(),
sfricke-samsungef15e482022-01-26 11:32:49 -08003351 consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08003352 }
Chris Forbes47567b72017-06-09 12:09:45 -07003353 }
3354 }
3355
sfricke-samsungef15e482022-01-26 11:32:49 -08003356 if (fragment_stage && fragment_stage->module_state->has_valid_spirv) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003357 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06003358 if (rp_state && rp_state->UsesDynamicRendering()) {
sjfricke4f600c82022-06-09 14:21:32 +09003359 skip |= ValidateFsOutputsAgainstDynamicRenderingRenderPass(*fragment_stage->module_state.get(),
sfricke-samsungef15e482022-01-26 11:32:49 -08003360 fragment_stage->entrypoint, pipeline);
Aaron Hagan1209c782021-11-22 19:37:14 -05003361 } else {
sjfricke4f600c82022-06-09 14:21:32 +09003362 skip |= ValidateFsOutputsAgainstRenderPass(*fragment_stage->module_state.get(), fragment_stage->entrypoint, pipeline,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003363 pipeline->Subpass());
Aaron Hagan1209c782021-11-22 19:37:14 -05003364 }
Chris Forbes47567b72017-06-09 12:09:45 -07003365 }
3366
3367 return skip;
3368}
3369
Tony-LunarGb2ded512021-02-02 16:03:30 -07003370bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
3371 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07003372 bool skip = false;
3373
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003374 for (auto &stage : pipeline->stage_state) {
3375 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
3376 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07003377 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
3378 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben3dfeacf2021-12-02 08:46:39 -07003379 if (stage.wrote_primitive_shading_rate) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00003380 skip |=
sjfrickedbd15712022-08-29 16:12:13 +09003381 LogError(stage.module_state.get()->vk_shader_module(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00003382 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
3383 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
3384 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003385 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00003386 }
3387 }
3388 }
3389 }
3390
3391 return skip;
3392}
3393
sfricke-samsunge72a85e2020-02-29 21:48:37 -08003394bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003395 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07003396}
Chris Forbes4ae55b32017-06-09 14:42:56 -07003397
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003398uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE &pipeline, VkShaderStageFlagBits stageBit) const {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003399 uint32_t total = 0;
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003400 const auto stages = pipeline.GetShaderStages();
3401 for (const auto &stage : stages) {
3402 if (stage.stage == stageBit) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003403 total++;
3404 }
3405 }
3406
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003407 const auto rt_lib_info = pipeline.GetRayTracingLibraryCreateInfo();
3408 if (rt_lib_info) {
3409 for (uint32_t i = 0; i < rt_lib_info->libraryCount; ++i) {
3410 auto library_pipeline = Get<PIPELINE_STATE>(rt_lib_info->pLibraries[i]);
3411 total += CalcShaderStageCount(*library_pipeline, stageBit);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003412 }
3413 }
3414
3415 return total;
3416}
3417
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003418bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE &pipeline, uint32_t group, uint32_t stage) const {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003419 if (group == VK_SHADER_UNUSED_NV) {
3420 return true;
3421 }
3422
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003423 const auto stages = pipeline.GetShaderStages();
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003424
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003425 const auto num_stages = static_cast<uint32_t>(stages.size());
3426 if (group < num_stages) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003427 return (stages[group].stage & stage) != 0;
3428 }
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003429 group -= num_stages;
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003430
3431 // Search libraries
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003432 const auto rt_lib_info = pipeline.GetRayTracingLibraryCreateInfo();
3433 if (rt_lib_info) {
3434 for (uint32_t i = 0; i < rt_lib_info->libraryCount; ++i) {
3435 auto library_pipeline = Get<PIPELINE_STATE>(rt_lib_info->pLibraries[i]);
3436 const auto lib_stages = library_pipeline->GetShaderStages();
3437 const uint32_t stage_count = static_cast<uint32_t>(lib_stages.size());
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003438 if (group < stage_count) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003439 return (stages[group].stage & stage) != 0;
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003440 }
3441 group -= stage_count;
3442 }
3443 }
3444
3445 // group index too large
3446 return false;
3447}
3448
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003449bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, const safe_VkRayTracingPipelineCreateInfoCommon &create_info,
3450 VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06003451 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04003452
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003453 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003454 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
3455 skip |=
3456 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
3457 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
3458 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
3459 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003460 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003461 if (create_info.pLibraryInfo) {
3462 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003463 const auto library_pipelinestate = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003464 const auto &library_create_info = library_pipelinestate->GetCreateInfo<VkRayTracingPipelineCreateInfoKHR>();
Jeremy Gebben11af9792021-08-20 10:20:09 -06003465 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003466 skip |= LogError(
3467 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
3468 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
3469 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003470 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07003471 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003472 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
3473 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
3474 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
3475 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003476 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
3477 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
3478 "member must have been created with values of the maxPipelineRayPayloadSize and "
3479 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
3480 }
3481 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06003482 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003483 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
3484 "vkCreateRayTracingPipelinesKHR: If flags includes "
3485 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
3486 "the pLibraries member of libraries must have been created with the "
3487 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
3488 }
sourav parmar83c31b12020-05-06 12:30:54 -07003489 }
3490 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003491 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003492 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003493 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
3494 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
3495 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003496 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003497 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003498 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003499 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04003500
Jeremy Gebben11af9792021-08-20 10:20:09 -06003501 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003502 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003503 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003504
Jeremy Gebben11af9792021-08-20 10:20:09 -06003505 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003506 const uint32_t raygen_stages_count = CalcShaderStageCount(*pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003507 if (raygen_stages_count == 0) {
3508 skip |= LogError(
3509 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07003510 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003511 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
3512 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003513 }
ziga-lunarg22f96832022-05-08 22:20:15 +02003514 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR) != 0 &&
3515 (flags & VK_PIPELINE_CREATE_RAY_TRACING_SKIP_AABBS_BIT_KHR) != 0) {
3516 skip |= LogError(
3517 device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-06546",
3518 "vkCreateRayTracingPipelinesKHR: flags (%s) contains both VK_PIPELINE_CREATE_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR and "
3519 "VK_PIPELINE_CREATE_RAY_TRACING_SKIP_AABBS_BIT_KHR bits.",
3520 string_VkPipelineCreateFlags(flags).c_str());
3521 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003522
Jeremy Gebben11af9792021-08-20 10:20:09 -06003523 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003524 const auto &group = groups[group_index];
3525
3526 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003527 if (!GroupHasValidIndex(
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003528 *pipeline, group.generalShader,
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003529 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 -05003530 skip |= LogError(device,
3531 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
3532 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
3533 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003534 }
3535 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3536 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003537 skip |= LogError(device,
3538 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
3539 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
3540 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003541 }
3542 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003543 if (!GroupHasValidIndex(*pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003544 skip |= LogError(device,
3545 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
3546 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
3547 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003548 }
3549 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3550 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003551 skip |= LogError(device,
3552 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
3553 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
3554 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003555 }
3556 }
3557
3558 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3559 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
sjfricke62366d32022-08-01 21:04:10 +09003560 if (!GroupHasValidIndex(*pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_KHR)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003561 skip |= LogError(device,
3562 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
3563 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
3564 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003565 }
sjfricke62366d32022-08-01 21:04:10 +09003566 if (!GroupHasValidIndex(*pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003567 skip |= LogError(device,
3568 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
3569 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
3570 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003571 }
3572 }
John Zulaufe4474e72019-07-01 17:28:27 -06003573 }
3574 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003575}
3576
Dave Houltona9df0ce2018-02-07 10:51:23 -07003577uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003578
Dave Houltona9df0ce2018-02-07 10:51:23 -07003579static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003580 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06003581 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003582 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003583 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003584 return nullptr;
3585}
3586
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003587bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003588 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003589 bool skip = false;
3590 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003591
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06003592 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003593 return false;
3594 }
3595
sfricke-samsung45996a42021-09-16 13:45:27 -07003596 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003597
3598 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003599 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
Jeremy Gebbena4fc6742022-09-15 14:57:30 -06003600 "SPIR-V module not valid: Codesize must be a multiple of 4 but is %zu", pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003601 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003602 auto cache = GetValidationCacheInfo(pCreateInfo);
3603 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07003604 // If app isn't using a shader validation cache, use the default one from CoreChecks
3605 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003606 if (cache) {
3607 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003608 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003609 }
3610
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003611 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3612 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07003613 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06003614 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003615 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003616 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003617 spvtools::ValidatorOptions options;
3618 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06003619 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003620 if (spv_valid != SPV_SUCCESS) {
3621 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003622 if (spv_valid == SPV_WARNING) {
3623 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3624 diag && diag->error ? diag->error : "(no error text)");
3625 } else {
3626 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3627 diag && diag->error ? diag->error : "(no error text)");
3628 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003629 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003630 } else {
3631 if (cache) {
3632 cache->Insert(hash);
3633 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003634 }
3635
3636 spvDiagnosticDestroy(diag);
3637 spvContextDestroy(ctx);
3638 }
3639
Chris Forbes4ae55b32017-06-09 14:42:56 -07003640 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003641}
3642
Tony-LunarG1672d002022-08-03 14:35:34 -06003643bool CoreChecks::PreCallValidateGetShaderModuleIdentifierEXT(VkDevice device, VkShaderModule shaderModule,
3644 VkShaderModuleIdentifierEXT *pIdentifier) const {
3645 bool skip = false;
3646 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
sjfrickedbd15712022-08-29 16:12:13 +09003647 skip |= LogError(shaderModule, "VUID-vkGetShaderModuleIdentifierEXT-shaderModuleIdentifier-06884",
Tony-LunarG1672d002022-08-03 14:35:34 -06003648 "vkGetShaderModuleIdentifierEXT() was called when the shaderModuleIdentifier feature was not enabled");
3649 }
3650 return skip;
3651}
3652
3653bool CoreChecks::PreCallValidateGetShaderModuleCreateInfoIdentifierEXT(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
3654 VkShaderModuleIdentifierEXT *pIdentifier) const {
3655 bool skip = false;
3656 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
3657 skip |= LogError(
3658 device, "VUID-vkGetShaderModuleCreateInfoIdentifierEXT-shaderModuleIdentifier-06885",
3659 "vkGetShaderModuleCreateInfoIdentifierEXT() was called when the shaderModuleIdentifier feature was not enabled");
3660 }
3661 return skip;
3662}
3663
sjfricke4f600c82022-06-09 14:21:32 +09003664bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &entrypoint,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003665 const PipelineStageState &stage_state, uint32_t local_size_x, uint32_t local_size_y,
3666 uint32_t local_size_z) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003667 bool skip = false;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003668 // If spec constants were used then the local size are already found if possible
3669 if (local_size_x == 0) {
sjfricke4f600c82022-06-09 14:21:32 +09003670 if (!module_state.FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003671 return skip; // no local size found
Lockeaa8fdc02019-04-02 11:59:20 -06003672 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003673 }
Lockeaa8fdc02019-04-02 11:59:20 -06003674
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003675 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
sjfricke4f600c82022-06-09 14:21:32 +09003676 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003677 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003678 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003679 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
3680 }
3681 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
sjfricke4f600c82022-06-09 14:21:32 +09003682 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003683 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003684 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003685 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
3686 }
3687 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
sjfricke4f600c82022-06-09 14:21:32 +09003688 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003689 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003690 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003691 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
3692 }
3693
3694 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3695 uint64_t invocations = local_size_x * local_size_y;
3696 // Prevent overflow.
3697 bool fail = false;
3698 if (invocations > UINT32_MAX || invocations > limit) {
3699 fail = true;
3700 }
3701 if (!fail) {
3702 invocations *= local_size_z;
Lockeaa8fdc02019-04-02 11:59:20 -06003703 if (invocations > UINT32_MAX || invocations > limit) {
3704 fail = true;
3705 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003706 }
3707 if (fail) {
sjfricke4f600c82022-06-09 14:21:32 +09003708 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003709 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3710 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003711 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x, local_size_y,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003712 local_size_z, limit);
3713 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003714
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003715 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3716 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
ziga-lunargd46c7af2022-04-16 14:05:38 +02003717 const auto *required_subgroup_size_features =
3718 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
ziga-lunarg561d7222022-05-08 20:07:02 +02003719 if (required_subgroup_size_features) {
sjfrickef05418b2022-08-01 18:57:20 +09003720 const uint32_t requiredSubgroupSize = required_subgroup_size_features->requiredSubgroupSize;
sjfrickedbd15712022-08-29 16:12:13 +09003721 skip |= RequireFeature(module_state, enabled_features.core13.subgroupSizeControl, "subgroupSizeControl",
ziga-lunarg561d7222022-05-08 20:07:02 +02003722 "VUID-VkPipelineShaderStageCreateInfo-pNext-02755");
3723 if ((phys_dev_ext_props.subgroup_size_control_props.requiredSubgroupSizeStages & stage_state.stage_flag) == 0) {
3724 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003725 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02755",
ziga-lunarg561d7222022-05-08 20:07:02 +02003726 "Stage %s is not in VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%s).",
3727 string_VkShaderStageFlagBits(stage_state.stage_flag),
3728 string_VkShaderStageFlags(phys_dev_ext_props.subgroup_size_control_props.requiredSubgroupSizeStages).c_str());
3729 }
sjfrickef05418b2022-08-01 18:57:20 +09003730 if ((invocations > requiredSubgroupSize * phys_dev_ext_props.subgroup_size_control_props.maxComputeWorkgroupSubgroups)) {
ziga-lunarg561d7222022-05-08 20:07:02 +02003731 skip |=
sjfricke4f600c82022-06-09 14:21:32 +09003732 LogError(module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02756",
ziga-lunargd46c7af2022-04-16 14:05:38 +02003733 "Local workgroup size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3734 ") is greater than VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT::requiredSubgroupSize (%" PRIu32
3735 ") * maxComputeWorkgroupSubgroups (%" PRIu32 ").",
sjfrickef05418b2022-08-01 18:57:20 +09003736 local_size_x, local_size_y, local_size_z, requiredSubgroupSize,
ziga-lunargd46c7af2022-04-16 14:05:38 +02003737 phys_dev_ext_props.subgroup_size_control_props.maxComputeWorkgroupSubgroups);
ziga-lunarg561d7222022-05-08 20:07:02 +02003738 }
3739 if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT) > 0) {
sjfrickef05418b2022-08-01 18:57:20 +09003740 if (SafeModulo(local_size_x, requiredSubgroupSize) != 0) {
ziga-lunarg561d7222022-05-08 20:07:02 +02003741 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003742 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02757",
ziga-lunarg561d7222022-05-08 20:07:02 +02003743 "Local workgroup size x (%" PRIu32
3744 ") is not a multiple of VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT::requiredSubgroupSize (%" PRIu32
3745 ").",
sjfrickef05418b2022-08-01 18:57:20 +09003746 local_size_x, requiredSubgroupSize);
ziga-lunarg561d7222022-05-08 20:07:02 +02003747 }
ziga-lunargd46c7af2022-04-16 14:05:38 +02003748 }
sjfrickef05418b2022-08-01 18:57:20 +09003749 if (!IsPowerOfTwo(requiredSubgroupSize)) {
3750 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003751 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02760",
sjfrickef05418b2022-08-01 18:57:20 +09003752 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3753 ") is not a power of 2.",
3754 requiredSubgroupSize);
3755 }
3756 if (requiredSubgroupSize < phys_dev_ext_props.subgroup_size_control_props.minSubgroupSize) {
3757 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003758 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02761",
sjfrickef05418b2022-08-01 18:57:20 +09003759 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3760 ") is less than minSubgroupSize (%" PRIu32 ").",
3761 requiredSubgroupSize, phys_dev_ext_props.subgroup_size_control_props.minSubgroupSize);
3762 }
3763 if (requiredSubgroupSize > phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) {
3764 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003765 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02762",
sjfrickef05418b2022-08-01 18:57:20 +09003766 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3767 ") is greater than maxSubgroupSize (%" PRIu32 ").",
3768 requiredSubgroupSize, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3769 }
ziga-lunargd46c7af2022-04-16 14:05:38 +02003770 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003771 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3772 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3773 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003774 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003775 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3776 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3777 "dimension (%" PRIu32
3778 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003779 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003780 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3781 }
3782 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3783 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003784 if (!required_subgroup_size_features) {
3785 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02003786 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003787 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003788 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3789 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3790 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3791 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003792 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003793 phys_dev_props_core11.subgroupSize);
ziga-lunarg11fecb92021-09-20 16:48:06 +02003794 }
3795 }
Lockeaa8fdc02019-04-02 11:59:20 -06003796 }
3797 return skip;
3798}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003799
3800spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
Tony-LunarGe67fcc22022-01-03 16:40:53 -07003801 if (api_version >= VK_API_VERSION_1_3) {
3802 return SPV_ENV_VULKAN_1_3;
3803 } else if (api_version >= VK_API_VERSION_1_2) {
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003804 return SPV_ENV_VULKAN_1_2;
3805 } else if (api_version >= VK_API_VERSION_1_1) {
3806 if (spirv_1_4) {
3807 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3808 } else {
3809 return SPV_ENV_VULKAN_1_1;
3810 }
3811 }
3812 return SPV_ENV_VULKAN_1_0;
3813}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003814
sfricke-samsungecc112a2021-09-03 05:32:17 -07003815// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003816void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003817 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003818 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3819 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003820 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003821 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003822 options.SetRelaxBlockLayout(true);
3823 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003824
3825 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3826 // Vulkan version used, the feature bit is needed (also described in the spec).
3827
3828 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3829 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003830 options.SetUniformBufferStandardLayout(true);
3831 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003832 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3833 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003834 options.SetScalarBlockLayout(true);
3835 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003836 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3837 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003838 options.SetWorkgroupScalarBlockLayout(true);
3839 }
Tony-LunarG273f32f2021-09-28 08:56:30 -06003840 if (enabled_features.core13.maintenance4) {
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003841 // --allow-localsizeid
3842 options.SetAllowLocalSizeId(true);
3843 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003844}