blob: 5356ecefd25de568b5911cf6805d006b172f3c46 [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);
54 return module_state.get_def(base_insn_id);
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020055}
56
sjfricke4f600c82022-06-09 14:21:32 +090057static 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 +020058 const spirv_inst_iter &b_base_insn) {
59 const uint32_t a_opcode = a_base_insn.opcode();
60 const uint32_t b_opcode = b_base_insn.opcode();
61 if (a_opcode == b_opcode) {
62 if (a_opcode == spv::OpTypeInt) {
63 // Match width and signedness
64 return a_base_insn.word(2) == b_base_insn.word(2) && a_base_insn.word(3) == b_base_insn.word(3);
65 } else if (a_opcode == spv::OpTypeFloat) {
66 // Match width
67 return a_base_insn.word(2) == b_base_insn.word(2);
68 } else if (a_opcode == spv::OpTypeStruct) {
69 // Match on all element types
70 if (a_base_insn.len() != b_base_insn.len()) {
71 return false; // Structs cannot match if member counts differ
72 }
73
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020074 for (uint32_t i = 2; i < a_base_insn.len(); i++) {
75 const auto &c_base_insn = GetBaseTypeIter(a, a_base_insn.word(i));
76 const auto &d_base_insn = GetBaseTypeIter(b, b_base_insn.word(i));
77 if (!BaseTypesMatch(a, b, c_base_insn, d_base_insn)) {
ziga-lunarg8346fe82021-08-22 17:30:50 +020078 return false;
79 }
80 }
81
82 return true;
83 }
84 }
85 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070086}
87
sjfricke4f600c82022-06-09 14:21:32 +090088static 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 +020089 const auto &a_base_insn = GetBaseTypeIter(a, a_type);
90 const auto &b_base_insn = GetBaseTypeIter(b, b_type);
Chris Forbes47567b72017-06-09 12:09:45 -070091
ziga-lunarg8346fe82021-08-22 17:30:50 +020092 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070093}
94
sfricke-samsung7fac88a2022-01-26 11:44:22 -080095static uint32_t GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -070096 switch (format) {
97 case VK_FORMAT_R64G64B64A64_SFLOAT:
98 case VK_FORMAT_R64G64B64A64_SINT:
99 case VK_FORMAT_R64G64B64A64_UINT:
100 case VK_FORMAT_R64G64B64_SFLOAT:
101 case VK_FORMAT_R64G64B64_SINT:
102 case VK_FORMAT_R64G64B64_UINT:
103 return 2;
104 default:
105 return 1;
106 }
107}
108
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800109static uint32_t GetFormatType(VkFormat fmt) {
sfricke-samsunge3086292021-11-18 23:02:35 -0800110 if (FormatIsSINT(fmt)) return FORMAT_TYPE_SINT;
111 if (FormatIsUINT(fmt)) return FORMAT_TYPE_UINT;
sfricke-samsunged028b02021-09-06 23:14:51 -0700112 // Formats such as VK_FORMAT_D16_UNORM_S8_UINT are both
Dave Houltona9df0ce2018-02-07 10:51:23 -0700113 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
114 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700115 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
116 return FORMAT_TYPE_FLOAT;
117}
118
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600119static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700120 uint32_t bit_pos = uint32_t(u_ffs(stage));
121 return bit_pos - 1;
122}
123
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700124bool CoreChecks::ValidateViConsistency(safe_VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700125 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
126 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700127 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700128 bool skip = false;
129
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800130 for (uint32_t i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes47567b72017-06-09 12:09:45 -0700131 auto desc = &vi->pVertexBindingDescriptions[i];
132 auto &binding = bindings[desc->binding];
133 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600134 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700135 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
136 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700137 } else {
138 binding = desc;
139 }
140 }
141
142 return skip;
143}
144
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700145bool CoreChecks::ValidateViAgainstVsInputs(safe_VkPipelineVertexInputStateCreateInfo const *vi,
sjfricke4f600c82022-06-09 14:21:32 +0900146 const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700147 bool skip = false;
148
sjfricke4f600c82022-06-09 14:21:32 +0900149 const auto inputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700150
151 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200152 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700153 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200154 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
155 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
156 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700157 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
158 }
159 }
160 }
161
Petr Kraus25810d02019-08-27 17:41:15 +0200162 struct AttribInputPair {
163 const VkVertexInputAttributeDescription *attrib = nullptr;
164 const interface_var *input = nullptr;
165 };
166 std::map<uint32_t, AttribInputPair> location_map;
167 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
168 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700169
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400170 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200171 const auto location = location_it.first;
172 const auto attrib = location_it.second.attrib;
173 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600174
Petr Kraus25810d02019-08-27 17:41:15 +0200175 if (attrib && !input) {
sjfricke4f600c82022-06-09 14:21:32 +0900176 skip |= LogPerformanceWarning(module_state.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700177 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200178 } else if (!attrib && input) {
sjfricke4f600c82022-06-09 14:21:32 +0900179 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700180 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200181 } else if (attrib && input) {
182 const auto attrib_type = GetFormatType(attrib->format);
sjfricke4f600c82022-06-09 14:21:32 +0900183 const auto input_type = module_state.GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700184
185 // Type checking
186 if (!(attrib_type & input_type)) {
sjfricke4f600c82022-06-09 14:21:32 +0900187 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700188 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sjfricke4f600c82022-06-09 14:21:32 +0900189 string_VkFormat(attrib->format), location, module_state.DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700190 }
Petr Kraus25810d02019-08-27 17:41:15 +0200191 } else { // !attrib && !input
192 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700193 }
194 }
195
196 return skip;
197}
198
sjfricke4f600c82022-06-09 14:21:32 +0900199bool CoreChecks::ValidateFsOutputsAgainstDynamicRenderingRenderPass(const SHADER_MODULE_STATE &module_state,
sfricke-samsungef15e482022-01-26 11:32:49 -0800200 spirv_inst_iter entrypoint,
201 PIPELINE_STATE const *pipeline) const {
Aaron Hagan1209c782021-11-22 19:37:14 -0500202 bool skip = false;
203
204 struct Attachment {
205 const interface_var* output = nullptr;
206 };
207 std::map<uint32_t, Attachment> location_map;
208
209 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
sjfricke4f600c82022-06-09 14:21:32 +0900210 const auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Aaron Hagan1209c782021-11-22 19:37:14 -0500211 for (const auto& output_it : outputs) {
212 auto const location = output_it.first.first;
213 location_map[location].output = &output_it.second;
214 }
215
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700216 const auto ms_state = pipeline->MultisampleState();
217 const bool alpha_to_coverage_enabled = ms_state && (ms_state->alphaToCoverageEnable == VK_TRUE);
Aaron Hagan1209c782021-11-22 19:37:14 -0500218
Aaron Haganaca50442021-12-07 22:26:29 -0500219 for (uint32_t location = 0; location < location_map.size(); ++location) {
Aaron Hagan1209c782021-11-22 19:37:14 -0500220 const auto output = location_map[location].output;
221
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700222 const auto &rp_state = pipeline->RenderPassState();
223 const auto &attachments = pipeline->Attachments();
224 if (!output && location < attachments.size() && attachments[location].colorWriteMask != 0) {
225 skip |= LogWarning(
sjfricke4f600c82022-06-09 14:21:32 +0900226 module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700227 "Attachment %" PRIu32 " not written by fragment shader; undefined values will be written to attachment", location);
228 } else if (output && (location < rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount)) {
229 auto format = rp_state->dynamic_rendering_pipeline_create_info.pColorAttachmentFormats[location];
230 const auto attachment_type = GetFormatType(format);
sjfricke4f600c82022-06-09 14:21:32 +0900231 const auto output_type = module_state.GetFundamentalType(output->type_id);
Aaron Hagan1209c782021-11-22 19:37:14 -0500232
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700233 // Type checking
234 if (!(output_type & attachment_type)) {
235 skip |=
sjfricke4f600c82022-06-09 14:21:32 +0900236 LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700237 "Attachment %" PRIu32
238 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sjfricke4f600c82022-06-09 14:21:32 +0900239 location, string_VkFormat(format), module_state.DescribeType(output->type_id).c_str());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700240 }
241 }
Aaron Hagan1209c782021-11-22 19:37:14 -0500242 }
243
244 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sjfricke4f600c82022-06-09 14:21:32 +0900245 bool location_zero_has_alpha = output_zero && module_state.get_def(output_zero->type_id) != module_state.end() &&
246 module_state.GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Aaron Hagan1209c782021-11-22 19:37:14 -0500247 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sjfricke4f600c82022-06-09 14:21:32 +0900248 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
sfricke-samsungef15e482022-01-26 11:32:49 -0800249 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Aaron Hagan1209c782021-11-22 19:37:14 -0500250 }
251
252 return skip;
Aaron Hagan1209c782021-11-22 19:37:14 -0500253}
254
sjfricke4f600c82022-06-09 14:21:32 +0900255bool CoreChecks::ValidateFsOutputsAgainstRenderPass(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700256 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200257 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700258
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600259 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800260 const VkAttachmentReference2 *reference = nullptr;
261 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600262 const interface_var *output = nullptr;
263 };
264 std::map<uint32_t, Attachment> location_map;
265
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700266 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -0600267 if (rp_state && !rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700268 const auto rpci = rp_state->createInfo.ptr();
amhagana448ea52021-11-02 14:09:14 -0400269 const auto subpass = rpci->pSubpasses[subpass_index];
270 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
271 auto const &reference = subpass.pColorAttachments[i];
272 location_map[i].reference = &reference;
273 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
274 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
275 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
276 }
Chris Forbes47567b72017-06-09 12:09:45 -0700277 }
278 }
279
Chris Forbes47567b72017-06-09 12:09:45 -0700280 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
281
sjfricke4f600c82022-06-09 14:21:32 +0900282 const auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600283 for (const auto &output_it : outputs) {
284 auto const location = output_it.first.first;
285 location_map[location].output = &output_it.second;
286 }
Chris Forbes47567b72017-06-09 12:09:45 -0700287
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700288 const auto *ms_state = pipeline->MultisampleState();
289 const bool alpha_to_coverage_enabled = ms_state && (ms_state->alphaToCoverageEnable == VK_TRUE);
Chris Forbes47567b72017-06-09 12:09:45 -0700290
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700291 // Don't check any color attachments if rasterization is disabled
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700292 const auto raster_state = pipeline->RasterizationState();
Nathaniel Cesario81257cb2022-02-16 17:15:58 -0700293 if (raster_state && !raster_state->rasterizerDiscardEnable) {
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700294 for (const auto &location_it : location_map) {
295 const auto reference = location_it.second.reference;
296 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
297 continue;
Petr Kraus25810d02019-08-27 17:41:15 +0200298 }
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700299
300 const auto location = location_it.first;
301 const auto attachment = location_it.second.attachment;
302 const auto output = location_it.second.output;
303 if (attachment && !output) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700304 const auto &attachments = pipeline->Attachments();
305 if (location < attachments.size() && attachments[location].colorWriteMask != 0) {
sjfricke4f600c82022-06-09 14:21:32 +0900306 skip |= LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700307 "Attachment %" PRIu32
308 " not written by fragment shader; undefined values will be written to attachment",
309 location);
310 }
311 } else if (!attachment && output) {
312 if (!(alpha_to_coverage_enabled && location == 0)) {
313 skip |=
sjfricke4f600c82022-06-09 14:21:32 +0900314 LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700315 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700316 }
317 } else if (attachment && output) {
318 const auto attachment_type = GetFormatType(attachment->format);
sjfricke4f600c82022-06-09 14:21:32 +0900319 const auto output_type = module_state.GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700320
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700321 // Type checking
322 if (!(output_type & attachment_type)) {
323 skip |= LogWarning(
sjfricke4f600c82022-06-09 14:21:32 +0900324 module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700325 "Attachment %" PRIu32
326 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sjfricke4f600c82022-06-09 14:21:32 +0900327 location, string_VkFormat(attachment->format), module_state.DescribeType(output->type_id).c_str());
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700328 }
329 } else { // !attachment && !output
330 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700331 }
Chris Forbes47567b72017-06-09 12:09:45 -0700332 }
333 }
334
Petr Kraus25810d02019-08-27 17:41:15 +0200335 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sjfricke4f600c82022-06-09 14:21:32 +0900336 bool location_zero_has_alpha = output_zero && module_state.get_def(output_zero->type_id) != module_state.end() &&
337 module_state.GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700338 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sjfricke4f600c82022-06-09 14:21:32 +0900339 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700340 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200341 }
342
Chris Forbes47567b72017-06-09 12:09:45 -0700343 return skip;
344}
345
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600346PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
347 const shader_struct_member &push_constant_used_in_shader,
348 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600349 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600350 const auto used_bytes_size = used_bytes->size();
351 if (used_bytes_size == 0) return PC_Byte_Updated;
352
353 const auto push_constant_data_update_size = push_constant_data_update.size();
354 const auto *data = push_constant_data_update.data();
355 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
356 if (used_bytes_size <= push_constant_data_update_size) {
357 return PC_Byte_Updated;
358 }
359 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
360
361 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
362 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
363 return PC_Byte_Updated;
364 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600365 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600366
locke-lunargde3f0fa2020-09-10 11:55:31 -0600367 uint32_t i = 0;
368 for (const auto used : *used_bytes) {
369 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600370 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600371 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600372 return PC_Byte_Not_Set;
373 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600374 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600375 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600376 }
377 }
378 ++i;
379 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600380 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600381}
382
sjfricke4f600c82022-06-09 14:21:32 +0900383bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700384 safe_VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700385 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700386 // Temp workaround to prevent false positive errors
387 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sjfricke4f600c82022-06-09 14:21:32 +0900388 if (module_state.HasMultipleEntryPoints()) {
sfricke-samsung5c65b372021-03-25 05:39:57 -0700389 return skip;
390 }
391
Chris Forbes47567b72017-06-09 12:09:45 -0700392 // 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 +0900393 const auto *entrypoint = module_state.FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600394 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
395 return skip;
396 }
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700397 const auto &pipeline_layout = pipeline.PipelineLayoutState();
398 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700399
locke-lunargde3f0fa2020-09-10 11:55:31 -0600400 bool found_stage = false;
401 for (auto const &range : *push_constant_ranges) {
402 if (range.stageFlags & pStage->stage) {
403 found_stage = true;
404 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600405 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600406 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600407 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600408 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600409 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600410 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600411 const auto ret =
412 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700413
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600414 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600415 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
sjfricke4f600c82022-06-09 14:21:32 +0900416 LogObjectList objlist(module_state.vk_shader_module());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700417 objlist.add(pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700418 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 -0600419 string_VkShaderStageFlags(pStage->stage).c_str(),
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700420 report_data->FormatHandle(pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600421 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700422 }
423 }
424 }
425
locke-lunargde3f0fa2020-09-10 11:55:31 -0600426 if (!found_stage) {
sjfricke4f600c82022-06-09 14:21:32 +0900427 LogObjectList objlist(module_state.vk_shader_module());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700428 objlist.add(pipeline_layout->layout());
429 skip |= LogError(
430 objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.",
sjfricke4f600c82022-06-09 14:21:32 +0900431 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700432 report_data->FormatHandle(pipeline_layout->layout()).c_str(), string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700433 }
Chris Forbes47567b72017-06-09 12:09:45 -0700434 return skip;
435}
436
sjfricke4f600c82022-06-09 14:21:32 +0900437bool CoreChecks::ValidateBuiltinLimits(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700438 bool skip = false;
439
440 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700441 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700442 return skip;
443 }
444
sfricke-samsungcfb44592021-07-25 00:36:28 -0700445 // Find all builtin from just the interface variables
446 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sjfricke4f600c82022-06-09 14:21:32 +0900447 auto insn = module_state.get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700448 assert(insn.opcode() == spv::OpVariable);
sjfricke4f600c82022-06-09 14:21:32 +0900449 const decoration_set decorations = module_state.get_decorations(insn.word(2));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700450
sfricke-samsungcfb44592021-07-25 00:36:28 -0700451 // Currently don't need to search in structs
452 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sjfricke4f600c82022-06-09 14:21:32 +0900453 auto type_pointer = module_state.get_def(insn.word(1));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700454 assert(type_pointer.opcode() == spv::OpTypePointer);
455
sjfricke4f600c82022-06-09 14:21:32 +0900456 auto type = module_state.get_def(type_pointer.word(3));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700457 if (type.opcode() == spv::OpTypeArray) {
sjfricke4f600c82022-06-09 14:21:32 +0900458 uint32_t length = static_cast<uint32_t>(module_state.GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700459 // Handles both the input and output sampleMask
460 if (length > phys_dev_props.limits.maxSampleMaskWords) {
461 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
462 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
463 "maxSampleMaskWords of %u in %s.",
464 length, phys_dev_props.limits.maxSampleMaskWords,
sjfricke4f600c82022-06-09 14:21:32 +0900465 report_data->FormatHandle(module_state.vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700466 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700467 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700468 }
469 }
470 }
471
472 return skip;
473}
474
Chris Forbes47567b72017-06-09 12:09:45 -0700475// Validate that data for each specialization entry is fully contained within the buffer.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700476bool CoreChecks::ValidateSpecializations(safe_VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700477 bool skip = false;
478
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700479 const auto *spec = info->pSpecializationInfo;
Chris Forbes47567b72017-06-09 12:09:45 -0700480
481 if (spec) {
482 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600483 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700484 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
485 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200486 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700487 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
488 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600489
490 continue;
491 }
Chris Forbes47567b72017-06-09 12:09:45 -0700492 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700493 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
494 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200495 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700496 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
497 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700498 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200499 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
500 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
501 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
502 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
503 j, spec->pMapEntries[i].constantID);
504 }
505 }
Chris Forbes47567b72017-06-09 12:09:45 -0700506 }
507 }
508
509 return skip;
510}
511
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500512// TODO (jbolz): Can this return a const reference?
sjfricke4f600c82022-06-09 14:21:32 +0900513static std::set<uint32_t> TypeToDescriptorTypeSet(const SHADER_MODULE_STATE &module_state, uint32_t type_id,
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800514 uint32_t &descriptor_count, bool is_khr) {
sjfricke4f600c82022-06-09 14:21:32 +0900515 auto type = module_state.get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800516 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700517 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500518 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700519
520 // 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 -0500521 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
522 if (type.opcode() == spv::OpTypeRuntimeArray) {
523 descriptor_count = 0;
sjfricke4f600c82022-06-09 14:21:32 +0900524 type = module_state.get_def(type.word(2));
Jeff Bolzfdf96072018-04-10 14:32:18 -0500525 } else if (type.opcode() == spv::OpTypeArray) {
sjfricke4f600c82022-06-09 14:21:32 +0900526 descriptor_count *= module_state.GetConstantValueById(type.word(3));
527 type = module_state.get_def(type.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700528 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800529 if (type.word(2) == spv::StorageClassStorageBuffer) {
530 is_storage_buffer = true;
531 }
sjfricke4f600c82022-06-09 14:21:32 +0900532 type = module_state.get_def(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700533 }
534 }
535
536 switch (type.opcode()) {
537 case spv::OpTypeStruct: {
sjfricke4f600c82022-06-09 14:21:32 +0900538 for (const auto insn : module_state.GetDecorationInstructions()) {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800539 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700540 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800541 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500542 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
543 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
544 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800545 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500546 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
547 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
548 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
549 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800550 }
Chris Forbes47567b72017-06-09 12:09:45 -0700551 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500552 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
553 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
554 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700555 }
556 }
557 }
558
559 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500560 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700561 }
562
563 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500564 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
565 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
566 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700567
Chris Forbes73c00bf2018-06-22 16:28:06 -0700568 case spv::OpTypeSampledImage: {
569 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
570 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
sjfricke4f600c82022-06-09 14:21:32 +0900571 auto image_type = module_state.get_def(type.word(2));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700572 auto dim = image_type.word(3);
573 auto sampled = image_type.word(7);
574 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500575 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
576 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700577 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700578 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500579 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
580 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700581
582 case spv::OpTypeImage: {
583 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
584 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
585 auto dim = type.word(3);
586 auto sampled = type.word(7);
587
588 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500589 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
590 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700591 } else if (dim == spv::DimBuffer) {
592 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500593 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
594 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700595 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500596 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
597 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700598 }
599 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500600 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
601 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
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_IMAGE);
605 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700606 }
607 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600608 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700609 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
610 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500611 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700612
613 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
614 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500615 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700616 }
617}
618
Jeff Bolze54ae892018-09-08 12:16:29 -0500619static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700620 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500621 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
622 if (ss.tellp()) ss << ", ";
623 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700624 }
625 return ss.str();
626}
627
sfricke-samsung0065ce02020-12-03 22:46:37 -0800628bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500629 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800630 if (LogError(device, vuid, "Shader requires flag %s set in %s but it is not set on the device", flag, structure)) {
Jeff Bolzee743412019-06-20 22:24:32 -0500631 return true;
632 }
633 }
634
635 return false;
636}
637
sfricke-samsung0065ce02020-12-03 22:46:37 -0800638bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700639 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800640 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700641 return true;
642 }
643 }
644
645 return false;
646}
647
locke-lunarg63e4daf2020-08-17 17:53:25 -0600648bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
649 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500650 bool skip = false;
651
locke-lunarg63e4daf2020-08-17 17:53:25 -0600652 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800653 switch (stage) {
Chris Forbes349b3132018-03-07 11:38:08 -0800654 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800655 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700656 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800657 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800658 case VK_SHADER_STAGE_VERTEX_BIT:
659 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
660 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
661 case VK_SHADER_STAGE_GEOMETRY_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800662 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700663 "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800664 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800665 default:
666 // No feature requirements for writes and atomics for other stages
667 break;
Chris Forbes349b3132018-03-07 11:38:08 -0800668 }
669 }
670
Chris Forbes47567b72017-06-09 12:09:45 -0700671 return skip;
672}
673
sjfricke4f600c82022-06-09 14:21:32 +0900674bool CoreChecks::ValidateShaderStageGroupNonUniform(const SHADER_MODULE_STATE &module_state, VkShaderStageFlagBits stage,
sfricke-samsung94167ca2021-02-26 04:14:59 -0800675 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500676 bool skip = false;
677
sfricke-samsung94167ca2021-02-26 04:14:59 -0800678 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
679 if (GroupOperation(insn.opcode()) == true) {
680 // Check the quad operations.
681 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
682 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700683 skip |=
684 RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
685 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800686 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800687 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500688
sfricke-samsung94167ca2021-02-26 04:14:59 -0800689 uint32_t scope_type = spv::ScopeMax;
690 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
691 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
692 scope_type = spv::ScopeSubgroup;
693 } else {
694 // "All <id> used for Scope <id> must be of an OpConstant"
sjfricke4f600c82022-06-09 14:21:32 +0900695 auto scope_id = module_state.get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800696 scope_type = scope_id.word(3);
697 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800698
sfricke-samsung94167ca2021-02-26 04:14:59 -0800699 if (scope_type == spv::ScopeSubgroup) {
700 // "Group operations with subgroup scope" must have stage support
701 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
702 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700703 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800704 }
705
706 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
sjfricke4f600c82022-06-09 14:21:32 +0900707 auto type = module_state.get_def(insn.word(1));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800708
709 if (type.opcode() == spv::OpTypeVector) {
710 // Get the element type
sjfricke4f600c82022-06-09 14:21:32 +0900711 type = module_state.get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800712 }
713
sfricke-samsung94167ca2021-02-26 04:14:59 -0800714 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800715 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
716 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500717
sfricke-samsung0065ce02020-12-03 22:46:37 -0800718 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
719 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
720 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
721 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700722 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500723 }
724 }
725 }
Jeff Bolzee743412019-06-20 22:24:32 -0500726 }
727
728 return skip;
729}
730
sjfricke4f600c82022-06-09 14:21:32 +0900731bool CoreChecks::ValidateMemoryScope(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &insn) const {
ziga-lunarg70651522021-10-11 17:23:30 +0200732 bool skip = false;
733
sfricke-samsung3a25ed52022-01-20 02:24:36 -0800734 const auto &entry = OpcodeMemoryScopePosition(insn.opcode());
ziga-lunarg70651522021-10-11 17:23:30 +0200735 if (entry > 0) {
736 const uint32_t scope_id = insn.word(entry);
sjfricke4f600c82022-06-09 14:21:32 +0900737 const auto &scope_def = module_state.GetConstantDef(scope_id);
738 if (scope_def != module_state.end()) {
sjfricke3b0cb102022-08-10 16:27:45 +0900739 const auto scope_type = module_state.GetConstantValue(scope_def);
sfricke-samsunged00aa42022-01-27 19:03:01 -0800740 if (enabled_features.core12.vulkanMemoryModel && !enabled_features.core12.vulkanMemoryModelDeviceScope &&
741 scope_type == spv::Scope::ScopeDevice) {
742 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06265",
743 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is enabled and "
744 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModelDeviceScope is disabled, but\n%s\nuses "
745 "Device memory scope.",
sjfricke4f600c82022-06-09 14:21:32 +0900746 module_state.DescribeInstruction(insn).c_str());
sfricke-samsunged00aa42022-01-27 19:03:01 -0800747 } else if (!enabled_features.core12.vulkanMemoryModel && scope_type == spv::Scope::ScopeQueueFamily) {
748 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06266",
749 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is not enabled, but\n%s\nuses "
750 "QueueFamily memory scope.",
sjfricke4f600c82022-06-09 14:21:32 +0900751 module_state.DescribeInstruction(insn).c_str());
ziga-lunarg70651522021-10-11 17:23:30 +0200752 }
753 }
754 }
755
756 return skip;
757}
758
sjfricke4f600c82022-06-09 14:21:32 +0900759bool CoreChecks::ValidateShaderStageInputOutputLimits(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700760 safe_VkPipelineShaderStageCreateInfo const *pStage,
761 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200762 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
763 pStage->stage == VK_SHADER_STAGE_ALL) {
764 return false;
765 }
766
767 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700768 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200769
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700770 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200771 struct Variable {
772 uint32_t baseTypePtrID;
773 uint32_t ID;
774 uint32_t storageClass;
775 };
776 std::vector<Variable> variables;
777
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700778 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700779 bool is_iso_lines = false;
780 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500781
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700782 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600783
sjfricke4f600c82022-06-09 14:21:32 +0900784 for (auto insn : module_state) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200785 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500786 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200787 case spv::OpDecorate:
788 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500789 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700790 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200791 break;
792 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200793 default:
794 break;
795 }
796 break;
797 // Find all input and output variables
798 case spv::OpVariable: {
799 Variable var = {};
800 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600801 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
802 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700803 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200804 var.baseTypePtrID = insn.word(1);
805 var.ID = insn.word(2);
806 variables.push_back(var);
807 }
808 break;
809 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500810 case spv::OpExecutionMode:
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800811 case spv::OpExecutionModeId:
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500812 if (insn.word(1) == entrypoint.word(2)) {
813 switch (insn.word(2)) {
814 default:
815 break;
816 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700817 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500818 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700819 case spv::ExecutionModeIsolines:
820 is_iso_lines = true;
821 break;
822 case spv::ExecutionModePointMode:
823 is_point_mode = true;
824 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500825 }
826 }
827 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200828 default:
829 break;
830 }
831 }
832
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500833 bool strip_output_array_level =
834 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
835 bool strip_input_array_level =
836 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
837 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
838
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700839 uint32_t num_comp_in = 0, num_comp_out = 0;
840 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600841
sjfricke4f600c82022-06-09 14:21:32 +0900842 auto inputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
843 auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600844
845 // Find max component location used for input variables.
846 for (auto &var : inputs) {
847 int location = var.first.first;
848 int component = var.first.second;
849 interface_var &iv = var.second;
850
851 // Only need to look at the first location, since we use the type's whole size
852 if (iv.offset != 0) {
853 continue;
854 }
855
856 if (iv.is_patch) {
857 continue;
858 }
859
sjfricke4f600c82022-06-09 14:21:32 +0900860 int num_components = module_state.GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700861 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600862 }
863
864 // Find max component location used for output variables.
865 for (auto &var : outputs) {
866 int location = var.first.first;
867 int component = var.first.second;
868 interface_var &iv = var.second;
869
870 // Only need to look at the first location, since we use the type's whole size
871 if (iv.offset != 0) {
872 continue;
873 }
874
875 if (iv.is_patch) {
876 continue;
877 }
878
sjfricke4f600c82022-06-09 14:21:32 +0900879 int num_components = module_state.GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700880 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600881 }
882
883 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
884 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700885 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
886 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200887 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500888 // Check if the variable is a patch. Patches can also be members of blocks,
889 // but if they are then the top-level arrayness has already been stripped
890 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700891 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200892
893 if (var.storageClass == spv::StorageClassInput) {
sjfricke4f600c82022-06-09 14:21:32 +0900894 num_comp_in += module_state.GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200895 } else { // var.storageClass == spv::StorageClassOutput
sjfricke4f600c82022-06-09 14:21:32 +0900896 num_comp_out += module_state.GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200897 }
898 }
899
900 switch (pStage->stage) {
901 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700902 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700903 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700904 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
905 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
906 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700907 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200908 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700909 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700910 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700911 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
912 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
913 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600914 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200915 break;
916
917 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700918 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700919 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700920 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
921 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
922 "components by %u components",
923 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700924 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200925 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700926 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600927 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700928 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700929 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
930 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
931 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600932 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700933 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700934 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700935 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
936 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
937 "components by %u components",
938 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700939 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200940 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700941 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600942 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700943 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700944 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
945 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
946 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600947 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200948 break;
949
950 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700951 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700952 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700953 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
954 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
955 "components by %u components",
956 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700957 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200958 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700959 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600960 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700961 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700962 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
963 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
964 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600965 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700966 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700967 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700968 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
969 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
970 "components by %u components",
971 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700972 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200973 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700974 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600975 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700976 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700977 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
978 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
979 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600980 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700981 // Portability validation
982 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
983 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700984 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700985 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
986 " is using abstract patch type IsoLines, but this is not supported on this platform");
987 }
988 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700989 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700990 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
991 " is using abstract patch type PointMode, but this is not supported on this platform");
992 }
993 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200994 break;
995
996 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700997 if (num_comp_in > limits.maxGeometryInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700998 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700999 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1000 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
1001 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001002 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001003 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001004 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001005 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001006 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
1007 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
1008 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001009 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001010 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001011 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001012 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1013 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
1014 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001015 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001016 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001017 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001018 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001019 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
1020 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
1021 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001022 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001023 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001024 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001025 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1026 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
1027 "components by %u components",
1028 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001029 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001030 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001031 break;
1032
1033 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001034 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001035 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001036 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
1037 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
1038 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001039 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001040 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001041 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001042 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001043 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1044 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1045 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001046 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001047 break;
1048
sjfricke62366d32022-08-01 21:04:10 +09001049 case VK_SHADER_STAGE_RAYGEN_BIT_KHR:
1050 case VK_SHADER_STAGE_ANY_HIT_BIT_KHR:
1051 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR:
1052 case VK_SHADER_STAGE_MISS_BIT_KHR:
1053 case VK_SHADER_STAGE_INTERSECTION_BIT_KHR:
1054 case VK_SHADER_STAGE_CALLABLE_BIT_KHR:
Jeff Bolz148d94e2018-12-13 21:25:56 -06001055 case VK_SHADER_STAGE_TASK_BIT_NV:
1056 case VK_SHADER_STAGE_MESH_BIT_NV:
1057 break;
1058
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001059 default:
1060 assert(false); // This should never happen
1061 }
1062 return skip;
1063}
1064
sjfricke4f600c82022-06-09 14:21:32 +09001065bool CoreChecks::ValidateShaderStorageImageFormats(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &insn) const {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001066 bool skip = false;
1067
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001068 switch (insn.opcode()) {
1069 // Go through all ImageRead/Write instructions
1070 case spv::OpImageSparseRead:
1071 case spv::OpImageRead: {
1072 // spirv-val validates this is an OpTypeImage
sjfricke4f600c82022-06-09 14:21:32 +09001073 const uint32_t image = module_state.GetTypeId(insn.word(3));
1074 const spirv_inst_iter image_def = module_state.get_def(image);
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001075
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001076 const uint32_t dim = image_def.word(3);
1077 const uint32_t image_format = image_def.word(8);
1078 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1079 // StorageImageReadWithoutFormat Capability was declared.
1080 if (dim != spv::DimSubpassData && image_format == spv::ImageFormatUnknown) {
1081 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1082 "shaderStorageImageReadWithoutFormat", kVUID_Features_shaderStorageImageReadWithoutFormat);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001083 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001084 break;
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001085 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001086 case spv::OpImageWrite: {
1087 // spirv-val validates this is an OpTypeImage
sjfricke4f600c82022-06-09 14:21:32 +09001088 const uint32_t image = module_state.GetTypeId(insn.word(1));
1089 const spirv_inst_iter image_def = module_state.get_def(image);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001090
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001091 const uint32_t image_format = image_def.word(8);
1092 if (image_format == spv::ImageFormatUnknown) {
1093 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1094 "shaderStorageImageWriteWithoutFormat", kVUID_Features_shaderStorageImageWriteWithoutFormat);
1095 }
1096 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001097 }
1098
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001099 // Go through all variables for images and check decorations
1100 case spv::OpVariable: {
1101 // spirv-val validates this is an OpTypePointer
sjfricke4f600c82022-06-09 14:21:32 +09001102 const spirv_inst_iter pointer_def = module_state.get_def(insn.word(1));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001103 if (pointer_def.word(2) != spv::StorageClassUniformConstant) {
1104 break; // Vulkan Spec says storage image must be UniformConstant
1105 }
sjfricke4f600c82022-06-09 14:21:32 +09001106 spirv_inst_iter type_def = module_state.get_def(pointer_def.word(3));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001107
1108 // Unpack an optional level of arraying
1109 if (type_def.opcode() == spv::OpTypeArray || type_def.opcode() == spv::OpTypeRuntimeArray) {
sjfricke4f600c82022-06-09 14:21:32 +09001110 type_def = module_state.get_def(type_def.word(2));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001111 }
1112
sjfricke4f600c82022-06-09 14:21:32 +09001113 if (type_def != module_state.end() && type_def.opcode() == spv::OpTypeImage) {
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001114 // Only check if the Image Dim operand is not SubpassData
1115 const uint32_t dim = type_def.word(3);
1116 // Only check storage images
1117 const uint32_t sampled = type_def.word(7);
1118 const uint32_t image_format = type_def.word(8);
1119 if ((dim == spv::DimSubpassData) || (sampled != 2) || (image_format != spv::ImageFormatUnknown)) {
1120 break;
1121 }
1122
1123 const uint32_t var_id = insn.word(2);
sjfricke4f600c82022-06-09 14:21:32 +09001124 decoration_set img_decorations = module_state.get_decorations(var_id);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001125
1126 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1127 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1128 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001129 "shaderStorageImageReadWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith Unknown "
1130 "format and is not decorated with NonReadable",
sjfricke4f600c82022-06-09 14:21:32 +09001131 module_state.DescribeInstruction(module_state.get_def(var_id)).c_str(),
1132 module_state.DescribeInstruction(type_def).c_str());
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001133 }
1134
1135 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1136 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1137 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001138 "shaderStorageImageWriteWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith "
1139 "Unknown format and is not decorated with NonWritable",
sjfricke4f600c82022-06-09 14:21:32 +09001140 module_state.DescribeInstruction(module_state.get_def(var_id)).c_str(),
1141 module_state.DescribeInstruction(type_def).c_str());
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001142 }
1143 }
1144 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001145 }
1146 }
1147
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001148 return skip;
1149}
1150
sfricke-samsungdc96f302020-03-18 20:42:10 -07001151bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1152 bool skip = false;
1153 uint32_t total_resources = 0;
1154
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001155 const auto &rp_state = pipeline->RenderPassState();
1156 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) && rp_state) {
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06001157 if (rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001158 total_resources += rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001159 } else {
1160 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001161 total_resources += rp_state->createInfo.pSubpasses[pipeline->Subpass()].colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001162 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001163 }
1164
1165 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1166 // input from CreatePipeline and CreatePipelineLayout level
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001167 const auto &layout_state = pipeline->PipelineLayoutState();
1168 if (layout_state) {
1169 for (auto set_layout : layout_state->set_layouts) {
1170 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1171 continue;
1172 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001173
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001174 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1175 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1176 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1177 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1178 // Check only descriptor types listed in maxPerStageResources description in spec
1179 switch (binding->descriptorType) {
1180 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1181 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1182 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1183 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1184 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1185 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1186 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1187 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1188 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1189 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1190 total_resources += binding->descriptorCount;
1191 break;
1192 default:
1193 break;
1194 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001195 }
1196 }
1197 }
1198 }
1199
1200 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
ziga-lunarg7d53c822022-05-08 23:06:10 +02001201 const char *vuid = nullptr;
1202 if (stage == VK_SHADER_STAGE_COMPUTE_BIT) {
1203 vuid = "VUID-VkComputePipelineCreateInfo-layout-01687";
1204 } else if ((stage & VK_SHADER_STAGE_ALL_GRAPHICS) == 0) {
1205 vuid = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03428";
1206 } else {
1207 vuid = "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
1208 }
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001209 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001210 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1211 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1212 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1213 }
1214
1215 return skip;
1216}
1217
Jeff Bolze4356752019-03-07 11:23:46 -06001218// copy the specialization constant value into buf, if it is present
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001219template <typename StageCreateInfo>
1220void GetSpecConstantValue(StageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1221 const auto *spec = pStage->pSpecializationInfo;
Jeff Bolze4356752019-03-07 11:23:46 -06001222
1223 if (spec && spec_id < spec->mapEntryCount) {
1224 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1225 }
1226}
1227
1228// Fill in value with the constant or specialization constant value, if available.
1229// Returns true if the value has been accurately filled out.
sjfricke4f600c82022-06-09 14:21:32 +09001230static bool GetIntConstantValue(spirv_inst_iter insn, const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001231 safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001232 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
sjfricke4f600c82022-06-09 14:21:32 +09001233 auto type_id = module_state.get_def(insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001234 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1235 return false;
1236 }
1237 switch (insn.opcode()) {
1238 case spv::OpSpecConstant:
1239 *value = insn.word(3);
1240 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1241 return true;
1242 case spv::OpConstant:
1243 *value = insn.word(3);
1244 return true;
1245 default:
1246 return false;
1247 }
1248}
1249
1250// Map SPIR-V type to VK_COMPONENT_TYPE enum
sjfricke4f600c82022-06-09 14:21:32 +09001251VkComponentTypeNV GetComponentType(spirv_inst_iter insn) {
Jeff Bolze4356752019-03-07 11:23:46 -06001252 switch (insn.opcode()) {
1253 case spv::OpTypeInt:
1254 switch (insn.word(2)) {
1255 case 8:
1256 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1257 case 16:
1258 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1259 case 32:
1260 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1261 case 64:
1262 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1263 default:
1264 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1265 }
1266 case spv::OpTypeFloat:
1267 switch (insn.word(2)) {
1268 case 16:
1269 return VK_COMPONENT_TYPE_FLOAT16_NV;
1270 case 32:
1271 return VK_COMPONENT_TYPE_FLOAT32_NV;
1272 case 64:
1273 return VK_COMPONENT_TYPE_FLOAT64_NV;
1274 default:
1275 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1276 }
1277 default:
1278 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1279 }
1280}
1281
1282// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1283// in SPIRV-Tools (e.g. due to specialization constant usage).
sjfricke4f600c82022-06-09 14:21:32 +09001284bool CoreChecks::ValidateCooperativeMatrix(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001285 safe_VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001286 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001287 bool skip = false;
1288
1289 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001290 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001291 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001292 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001293
1294 struct CoopMatType {
1295 uint32_t scope, rows, cols;
1296 VkComponentTypeNV component_type;
1297 bool all_constant;
1298
1299 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1300
sjfricke4f600c82022-06-09 14:21:32 +09001301 void Init(uint32_t id, const SHADER_MODULE_STATE &module_state, safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001302 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
sjfricke4f600c82022-06-09 14:21:32 +09001303 spirv_inst_iter insn = module_state.get_def(id);
Jeff Bolze4356752019-03-07 11:23:46 -06001304 uint32_t component_type_id = insn.word(2);
1305 uint32_t scope_id = insn.word(3);
1306 uint32_t rows_id = insn.word(4);
1307 uint32_t cols_id = insn.word(5);
sjfricke4f600c82022-06-09 14:21:32 +09001308 auto component_type_iter = module_state.get_def(component_type_id);
1309 auto scope_iter = module_state.get_def(scope_id);
1310 auto rows_iter = module_state.get_def(rows_id);
1311 auto cols_iter = module_state.get_def(cols_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001312
1313 all_constant = true;
sfricke-samsungef15e482022-01-26 11:32:49 -08001314 if (!GetIntConstantValue(scope_iter, module_state, pStage, id_to_spec_id, &scope)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001315 all_constant = false;
1316 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001317 if (!GetIntConstantValue(rows_iter, module_state, pStage, id_to_spec_id, &rows)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001318 all_constant = false;
1319 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001320 if (!GetIntConstantValue(cols_iter, module_state, pStage, id_to_spec_id, &cols)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001321 all_constant = false;
1322 }
sjfricke4f600c82022-06-09 14:21:32 +09001323 component_type = GetComponentType(component_type_iter);
Jeff Bolze4356752019-03-07 11:23:46 -06001324 }
1325 };
1326
1327 bool seen_coopmat_capability = false;
1328
sjfricke4f600c82022-06-09 14:21:32 +09001329 for (auto insn : module_state) {
Jeff Bolze4356752019-03-07 11:23:46 -06001330 // Whitelist instructions whose result can be a cooperative matrix type, and
1331 // keep track of their types. It would be nice if SPIRV-Headers generated code
1332 // to identify which instructions have a result type and result id. Lacking that,
1333 // this whitelist is based on the set of instructions that
1334 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1335 switch (insn.opcode()) {
1336 case spv::OpLoad:
1337 case spv::OpCooperativeMatrixLoadNV:
1338 case spv::OpCooperativeMatrixMulAddNV:
1339 case spv::OpSNegate:
1340 case spv::OpFNegate:
1341 case spv::OpIAdd:
1342 case spv::OpFAdd:
1343 case spv::OpISub:
1344 case spv::OpFSub:
1345 case spv::OpFDiv:
1346 case spv::OpSDiv:
1347 case spv::OpUDiv:
1348 case spv::OpMatrixTimesScalar:
1349 case spv::OpConstantComposite:
1350 case spv::OpCompositeConstruct:
1351 case spv::OpConvertFToU:
1352 case spv::OpConvertFToS:
1353 case spv::OpConvertSToF:
1354 case spv::OpConvertUToF:
1355 case spv::OpUConvert:
1356 case spv::OpSConvert:
1357 case spv::OpFConvert:
1358 id_to_type_id[insn.word(2)] = insn.word(1);
1359 break;
1360 default:
1361 break;
1362 }
1363
1364 switch (insn.opcode()) {
1365 case spv::OpDecorate:
1366 if (insn.word(2) == spv::DecorationSpecId) {
1367 id_to_spec_id[insn.word(1)] = insn.word(3);
1368 }
1369 break;
1370 case spv::OpCapability:
1371 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1372 seen_coopmat_capability = true;
1373
1374 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001375 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001376 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001377 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1378 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001379 }
1380 }
1381 break;
1382 case spv::OpMemoryModel:
1383 // If the capability isn't enabled, don't bother with the rest of this function.
1384 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1385 if (!seen_coopmat_capability) {
1386 return skip;
1387 }
1388 break;
1389 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001390 CoopMatType m;
sfricke-samsungef15e482022-01-26 11:32:49 -08001391 m.Init(insn.word(1), module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001392
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001393 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001394 // Validate that the type parameters are all supported for one of the
1395 // operands of a cooperative matrix property.
1396 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001397 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001398 if (cooperative_matrix_properties[i].AType == m.component_type &&
1399 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1400 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001401 valid = true;
1402 break;
1403 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001404 if (cooperative_matrix_properties[i].BType == m.component_type &&
1405 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1406 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001407 valid = true;
1408 break;
1409 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001410 if (cooperative_matrix_properties[i].CType == m.component_type &&
1411 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1412 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001413 valid = true;
1414 break;
1415 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001416 if (cooperative_matrix_properties[i].DType == m.component_type &&
1417 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1418 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001419 valid = true;
1420 break;
1421 }
1422 }
1423 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001424 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001425 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1426 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001427 }
1428 }
1429 break;
1430 }
1431 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001432 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001433 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1434 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1435 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1436 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001437 // Couldn't find type of matrix
1438 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001439 break;
1440 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001441 d.Init(id_to_type_id[insn.word(2)], module_state, pStage, id_to_spec_id);
1442 a.Init(id_to_type_id[insn.word(3)], module_state, pStage, id_to_spec_id);
1443 b.Init(id_to_type_id[insn.word(4)], module_state, pStage, id_to_spec_id);
1444 c.Init(id_to_type_id[insn.word(5)], module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001445
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001446 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001447 // Validate that the type parameters are all supported for the same
1448 // cooperative matrix property.
1449 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001450 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001451 if (cooperative_matrix_properties[i].AType == a.component_type &&
1452 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1453 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001454
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001455 cooperative_matrix_properties[i].BType == b.component_type &&
1456 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1457 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001458
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001459 cooperative_matrix_properties[i].CType == c.component_type &&
1460 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1461 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001462
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001463 cooperative_matrix_properties[i].DType == d.component_type &&
1464 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1465 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001466 valid = true;
1467 break;
1468 }
1469 }
1470 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001471 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001472 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1473 "VkCooperativeMatrixPropertiesNV",
1474 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001475 }
1476 }
1477 break;
1478 }
1479 default:
1480 break;
1481 }
1482 }
1483
1484 return skip;
1485}
1486
sjfricke4f600c82022-06-09 14:21:32 +09001487bool CoreChecks::ValidateShaderResolveQCOM(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001488 safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001489 const PIPELINE_STATE *pipeline) const {
1490 bool skip = false;
1491
1492 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1493 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1494 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09001495 for (auto insn : module_state) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001496 switch (insn.opcode()) {
1497 case spv::OpCapability:
1498 if (insn.word(1) == spv::CapabilitySampleRateShading) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001499 const auto &rp_state = pipeline->RenderPassState();
1500 auto subpass_flags = (!rp_state) ? 0 : rp_state->createInfo.pSubpasses[pipeline->Subpass()].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001501 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1502 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001503 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001504 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1505 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1506 }
1507 }
1508 break;
1509 default:
1510 break;
1511 }
1512 }
1513 }
1514
1515 return skip;
1516}
1517
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001518bool CoreChecks::ValidateShaderSubgroupSizeControl(safe_VkPipelineShaderStageCreateInfo const *pStage) const {
ziga-lunarg73163742021-08-25 13:15:29 +02001519 bool skip = false;
1520
1521 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001522 !enabled_features.core13.subgroupSizeControl) {
ziga-lunarg73163742021-08-25 13:15:29 +02001523 skip |= LogError(
1524 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1525 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1526 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1527 }
1528
1529 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001530 !enabled_features.core13.computeFullSubgroups) {
ziga-lunarg73163742021-08-25 13:15:29 +02001531 skip |= LogError(
1532 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1533 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1534 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1535 }
1536
1537 return skip;
1538}
1539
sjfricke4f600c82022-06-09 14:21:32 +09001540bool CoreChecks::ValidateAtomicsTypes(const SHADER_MODULE_STATE &module_state) const {
sfricke-samsung58b84352021-07-31 21:41:04 -07001541 bool skip = false;
1542
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001543 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001544 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001545
sfricke-samsungf5042b12021-08-05 01:09:40 -07001546 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1547 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1548
1549 const bool valid_storage_buffer_float = (
1550 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1551 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1552 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1553 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1554 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1555 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1556 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1557 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1558 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1559
1560 const bool valid_workgroup_float = (
1561 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1562 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1563 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1564 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1565 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1566 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1567 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1568 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1569 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1570
1571 const bool valid_image_float = (
1572 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1573 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1574 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1575
1576 const bool valid_16_float = (
1577 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1578 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1579 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1580 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1581 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1582 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1583
1584 const bool valid_32_float = (
1585 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1586 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1587 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1588 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1589 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1590 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1591 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1592 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1593 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1594
1595 const bool valid_64_float = (
1596 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1597 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1598 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1599 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1600 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1601 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1602 // clang-format on
1603
sjfricke4f600c82022-06-09 14:21:32 +09001604 for (const auto &atomic_inst : module_state.GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001605 const atomic_instruction &atomic = atomic_inst.second;
sjfricke4f600c82022-06-09 14:21:32 +09001606 const spirv_inst_iter atomic_def = module_state.at(atomic_inst.first);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001607 const uint32_t opcode = atomic_def.opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001608
1609 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001610 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001611 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1612 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001613 skip |= LogError(device, "VUID-RuntimeSpirv-None-06278",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001614 "%s: Can't use 64-bit int atomics operations\n%s\nwith %s storage class without "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001615 "shaderBufferInt64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001616 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1617 module_state.DescribeInstruction(atomic_def).c_str(), StorageClassName(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001618 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1619 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001620 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001621 "%s: Can't use 64-bit int atomics operations\n%s\nwith Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001622 "shaderSharedInt64Atomics 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-samsung6c9eb712021-08-04 09:38:54 -07001625 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001626 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001627 "%s: Can't use 64-bit int atomics operations\n%s\nwith Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001628 "shaderImageInt64Atomics 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-samsung58b84352021-07-31 21:41:04 -07001631 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001632 } else if (atomic.type == spv::OpTypeFloat) {
1633 // Validate Floats
1634 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1635 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001636 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1637 : "VUID-RuntimeSpirv-None-06280";
1638 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001639 "%s: Can't use float atomics operations\n%s\nwith StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001640 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1641 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1642 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1643 "shaderBufferFloat64AtomicMinMax 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 (opcode == spv::OpAtomicFAddEXT) {
1647 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1648 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001649 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001650 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001651 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1652 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001653 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1654 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001655 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001656 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001657 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1658 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001659 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1660 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001661 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001662 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001663 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1664 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001665 }
1666 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1667 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001668 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1669 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1670 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax 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 ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001674 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1675 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1676 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001677 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1678 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001679 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001680 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1681 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1682 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001683 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1684 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001685 }
1686 } else {
1687 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1688 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001689 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1690 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith "
1691 "StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001692 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1693 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001694 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001695 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1696 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith "
1697 "StorageBuffer storage class without shaderBufferFloat32Atomics 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 == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001701 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1702 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith "
1703 "StorageBuffer storage class without shaderBufferFloat64Atomics 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 }
1707 }
1708 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1709 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001710 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1711 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsungef15e482022-01-26 11:32:49 -08001712 skip |=
1713 LogError(device, vuid,
1714 "%s: Can't use float atomics operations\n%s\nwith Workgroup storage class without "
1715 "shaderSharedFloat32Atomics or "
1716 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1717 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1718 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax 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 (opcode == spv::OpAtomicFAddEXT) {
1722 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1723 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001724 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001725 "storage class without shaderSharedFloat16AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001726 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1727 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001728 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1729 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001730 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001731 "storage class without shaderSharedFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001732 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1733 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001734 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1735 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001736 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001737 "storage class without shaderSharedFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001738 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1739 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001740 }
1741 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1742 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001743 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1744 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1745 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001746 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1747 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001748 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001749 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1750 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1751 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001752 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1753 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001754 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001755 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1756 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1757 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001758 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1759 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001760 }
1761 } else {
1762 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1763 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001764 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1765 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1766 "storage class without shaderSharedFloat16Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001767 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1768 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001769 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001770 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1771 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1772 "storage class without shaderSharedFloat32Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001773 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1774 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001775 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001776 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1777 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1778 "storage class without shaderSharedFloat64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001779 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1780 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001781 }
1782 }
1783 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001784 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1785 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001786 skip |= LogError(
1787 device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001788 "%s: Can't use float atomics operations\n%s\nwith Image storage class without shaderImageFloat32Atomics or "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001789 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001790 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1791 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001792 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001793 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001794 "%s: Can't use 16-bit float atomics operations\n%s\nwithout shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001795 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1796 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001797 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1798 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001799 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001800 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1801 : "VUID-RuntimeSpirv-None-06335";
1802 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001803 "%s: Can't use 32-bit float atomics operations\n%s\nwithout shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001804 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1805 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1806 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1807 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001808 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1809 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001810 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001811 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1812 : "VUID-RuntimeSpirv-None-06336";
1813 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001814 "%s: Can't use 64-bit float atomics operations\n%s\nwithout shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001815 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1816 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001817 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1818 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001819 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001820 }
1821 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001822 return skip;
1823}
1824
sjfricke4f600c82022-06-09 14:21:32 +09001825bool CoreChecks::ValidateExecutionModes(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint,
sfricke-samsungef15e482022-01-26 11:32:49 -08001826 VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001827 auto entrypoint_id = entrypoint.word(2);
1828
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001829 // The first denorm execution mode encountered, along with its bit width.
1830 // Used to check if SeparateDenormSettings is respected.
1831 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001832
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001833 // The first rounding mode encountered, along with its bit width.
1834 // Used to check if SeparateRoundingModeSettings is respected.
1835 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001836
1837 bool skip = false;
1838
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001839 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001840 uint32_t invocations = 0;
1841
sjfricke4f600c82022-06-09 14:21:32 +09001842 const auto &execution_mode_inst = module_state.GetExecutionModeInstructions();
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001843 auto it = execution_mode_inst.find(entrypoint_id);
1844 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001845 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001846 auto mode = insn.word(2);
1847 switch (mode) {
1848 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1849 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001850 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001851 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001852 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001853 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001854 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001855 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1856 skip |= LogError(
1857 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001858 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001859 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001860 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1861 skip |= LogError(
1862 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001863 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001864 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001865 }
1866 break;
1867 }
1868
1869 case spv::ExecutionModeDenormPreserve: {
1870 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001871 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1872 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001873 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001874 module_state.DescribeInstruction(insn).c_str());
sfricke-samsunged00aa42022-01-27 19:03:01 -08001875 ;
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001876 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1877 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001878 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001879 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001880 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1881 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001882 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001883 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001884 }
1885
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001886 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1887 // Register the first denorm execution mode found
1888 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001889 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001890 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001891 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001892 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001893 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001894 "Shader uses different denorm execution modes for 16 and 64-bit but "
1895 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001896 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001897 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001898 }
1899 break;
1900
Mike Schuchardt2df08912020-12-15 16:28:09 -08001901 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001902 break;
1903
Mike Schuchardt2df08912020-12-15 16:28:09 -08001904 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001905 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001906 "Shader uses different denorm execution modes for different bit widths but "
1907 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001908 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001909 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001910 break;
1911
1912 default:
1913 break;
1914 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001915 }
1916 break;
1917 }
1918
1919 case spv::ExecutionModeDenormFlushToZero: {
1920 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001921 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001922 skip |=
1923 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1924 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001925 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001926 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001927 skip |=
1928 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1929 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001930 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001931 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001932 skip |=
1933 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1934 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001935 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001936 }
1937
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001938 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1939 // Register the first denorm execution mode found
1940 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001941 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001942 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001943 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001944 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001945 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001946 "Shader uses different denorm execution modes for 16 and 64-bit but "
1947 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001948 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001949 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001950 }
1951 break;
1952
Mike Schuchardt2df08912020-12-15 16:28:09 -08001953 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001954 break;
1955
Mike Schuchardt2df08912020-12-15 16:28:09 -08001956 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001957 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001958 "Shader uses different denorm execution modes for different bit widths but "
1959 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001960 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001961 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001962 break;
1963
1964 default:
1965 break;
1966 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001967 }
1968 break;
1969 }
1970
1971 case spv::ExecutionModeRoundingModeRTE: {
1972 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001973 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1974 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001975 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001976 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001977 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1978 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001979 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001980 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001981 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1982 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001983 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001984 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001985 }
1986
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001987 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1988 // Register the first rounding mode found
1989 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001990 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001991 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001992 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001993 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001994 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001995 "Shader uses different rounding modes for 16 and 64-bit but "
1996 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001997 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001998 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001999 }
2000 break;
2001
Mike Schuchardt2df08912020-12-15 16:28:09 -08002002 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002003 break;
2004
Mike Schuchardt2df08912020-12-15 16:28:09 -08002005 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002006 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002007 "Shader uses different rounding modes for different bit widths but "
2008 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002009 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002010 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002011 break;
2012
2013 default:
2014 break;
2015 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002016 }
2017 break;
2018 }
2019
2020 case spv::ExecutionModeRoundingModeRTZ: {
2021 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002022 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
2023 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002024 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002025 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002026 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
2027 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002028 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002029 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002030 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
2031 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002032 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002033 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002034 }
2035
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002036 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2037 // Register the first rounding mode found
2038 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002039 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002040 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08002041 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002042 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002043 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002044 "Shader uses different rounding modes for 16 and 64-bit but "
2045 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002046 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002047 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002048 }
2049 break;
2050
Mike Schuchardt2df08912020-12-15 16:28:09 -08002051 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002052 break;
2053
Mike Schuchardt2df08912020-12-15 16:28:09 -08002054 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002055 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002056 "Shader uses different rounding modes for different bit widths but "
2057 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002058 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002059 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002060 break;
2061
2062 default:
2063 break;
2064 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002065 }
2066 break;
2067 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002068
2069 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002070 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002071 break;
2072 }
2073
2074 case spv::ExecutionModeInvocations: {
2075 invocations = insn.word(3);
2076 break;
2077 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002078
2079 case spv::ExecutionModeLocalSizeId: {
Tony-LunarG273f32f2021-09-28 08:56:30 -06002080 if (!enabled_features.core13.maintenance4) {
Piers Daniella7f93b62021-11-20 12:32:04 -07002081 skip |= LogError(device, "VUID-RuntimeSpirv-LocalSizeId-06434",
2082 "LocalSizeId execution mode used but maintenance4 feature not enabled");
2083 }
ziga-lunargf2aa8152022-04-17 13:03:29 +02002084 if (!IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
2085 skip |= LogError(device, "VUID-RuntimeSpirv-LocalSizeId-06433",
2086 "LocalSizeId execution mode used but maintenance4 extension is not enabled and used Vulkan api version is 1.2 or less");
2087 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002088 break;
2089 }
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002090
2091 case spv::ExecutionModeEarlyFragmentTests: {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002092 const auto *ds_state = (pipeline) ? pipeline->DepthStencilState() : nullptr;
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002093 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) &&
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002094 (ds_state &&
2095 (ds_state->flags &
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002096 (VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_DEPTH_ACCESS_BIT_ARM |
2097 VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_STENCIL_ACCESS_BIT_ARM)) != 0)) {
2098 skip |= LogError(
Nathaniel Cesario6a0ce2f2022-04-02 21:47:54 -06002099 device, "VUID-VkGraphicsPipelineCreateInfo-flags-06591",
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002100 "The fragment shader enables early fragment tests, but VkPipelineDepthStencilStateCreateInfo::flags == "
2101 "%s",
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002102 string_VkPipelineDepthStencilStateCreateFlags(ds_state->flags).c_str());
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002103 }
2104 break;
2105 }
ziga-lunarge25f5f02022-04-16 15:07:35 +02002106 case spv::ExecutionModeSubgroupUniformControlFlowKHR: {
2107 if (!enabled_features.shader_subgroup_uniform_control_flow_features.shaderSubgroupUniformControlFlow ||
2108 (phys_dev_ext_props.subgroup_properties.supportedStages & stage) == 0 ||
sjfricke4f600c82022-06-09 14:21:32 +09002109 module_state.static_data_.has_invocation_repack_instruction) {
ziga-lunarge25f5f02022-04-16 15:07:35 +02002110 std::stringstream msg;
2111 if (!enabled_features.shader_subgroup_uniform_control_flow_features.shaderSubgroupUniformControlFlow) {
2112 msg << "shaderSubgroupUniformControlFlow feature must be enabled";
2113 } else if ((phys_dev_ext_props.subgroup_properties.supportedStages & stage) == 0) {
2114 msg << "stage" << string_VkShaderStageFlagBits(stage)
2115 << " must be in VkPhysicalDeviceSubgroupProperties::supportedStages("
2116 << string_VkShaderStageFlags(phys_dev_ext_props.subgroup_properties.supportedStages) << ")";
2117 } else {
2118 msg << "the shader must not use any invocation repack instructions";
2119 }
2120 skip |= LogError(device, "VUID-RuntimeSpirv-SubgroupUniformControlFlowKHR-06379",
2121 "If ExecutionModeSubgroupUniformControlFlowKHR is used %s.", msg.str().c_str());
2122 }
2123 } break;
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002124 }
2125 }
2126 }
2127
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002128 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002129 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002130 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2131 "Geometry shader entry point must have an OpExecutionMode instruction that "
2132 "specifies a maximum output vertex count that is greater than 0 and less "
2133 "than or equal to maxGeometryOutputVertices. "
2134 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002135 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002136 }
2137
2138 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002139 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2140 "Geometry shader entry point must have an OpExecutionMode instruction that "
2141 "specifies an invocation count that is greater than 0 and less "
2142 "than or equal to maxGeometryShaderInvocations. "
2143 "Invocations=%d, maxGeometryShaderInvocations=%d",
2144 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002145 }
2146 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002147 return skip;
2148}
2149
Chris Forbes47567b72017-06-09 12:09:45 -07002150// For given pipelineLayout verify that the set_layout_node at slot.first
2151// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002152static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002153 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002154 if (!pipelineLayout) return nullptr;
2155
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002156 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07002157
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002158 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07002159}
2160
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002161// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2162// o If there is only a vertex shader : gl_PointSize must be written when using points
2163// o If there is a geometry or tessellation shader:
2164// - If shaderTessellationAndGeometryPointSize feature is enabled:
2165// * gl_PointSize must be written in the final geometry stage
2166// - If shaderTessellationAndGeometryPointSize feature is disabled:
2167// * gl_PointSize must NOT be written and a default of 1.0 is assumed
sjfricke4f600c82022-06-09 14:21:32 +09002168bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, const SHADER_MODULE_STATE &module_state,
John Zulaufac4c6e12019-07-01 16:05:58 -06002169 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002170 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2171 return false;
2172 }
2173
2174 bool pointsize_written = false;
2175 bool skip = false;
2176
2177 // Search for PointSize built-in decorations
sjfricke4f600c82022-06-09 14:21:32 +09002178 for (const auto &set : module_state.GetBuiltinDecorationList()) {
2179 auto insn = module_state.at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002180 if (set.builtin == spv::BuiltInPointSize) {
sjfricke4f600c82022-06-09 14:21:32 +09002181 pointsize_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002182 if (pointsize_written) {
2183 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002184 }
2185 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002186 }
2187
2188 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002189 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002190 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002191 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002192 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2193 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002194 }
2195 } else if (!pointsize_written) {
2196 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002197 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002198 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2199 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002200 }
2201 return skip;
2202}
John Zulauf14c355b2019-06-27 16:09:37 -06002203
sjfricke4f600c82022-06-09 14:21:32 +09002204bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, const SHADER_MODULE_STATE &module_state,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002205 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2206 bool primitiverate_written = false;
2207 bool viewportindex_written = false;
2208 bool viewportmask_written = false;
2209 bool skip = false;
2210
2211 // Check if the primitive shading rate is written
sjfricke4f600c82022-06-09 14:21:32 +09002212 for (const auto &set : module_state.GetBuiltinDecorationList()) {
2213 auto insn = module_state.at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002214 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sjfricke4f600c82022-06-09 14:21:32 +09002215 primitiverate_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002216 } else if (set.builtin == spv::BuiltInViewportIndex) {
sjfricke4f600c82022-06-09 14:21:32 +09002217 viewportindex_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002218 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sjfricke4f600c82022-06-09 14:21:32 +09002219 viewportmask_written = module_state.IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002220 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002221 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2222 break;
2223 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002224 }
2225
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002226 const auto viewport_state = pipeline->ViewportState();
Tony-LunarGd44844c2021-01-22 13:24:37 -07002227 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002228 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && viewport_state) {
2229 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && viewport_state->viewportCount > 1 &&
2230 primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002231 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002232 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2233 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2234 "multiple viewports "
2235 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2236 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002237 }
2238
2239 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002240 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002241 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2242 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2243 "ViewportIndex built-ins,"
2244 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2245 string_VkShaderStageFlagBits(stage));
2246 }
2247
2248 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002249 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002250 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2251 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2252 "ViewportMaskNV built-ins,"
2253 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2254 string_VkShaderStageFlagBits(stage));
2255 }
2256 }
2257 return skip;
2258}
2259
sjfricke4f600c82022-06-09 14:21:32 +09002260bool CoreChecks::ValidateDecorations(const SHADER_MODULE_STATE &module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002261 bool skip = false;
2262
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002263 std::vector<spirv_inst_iter> xfb_streams;
2264 std::vector<spirv_inst_iter> xfb_buffers;
ziga-lunargef2c3172021-11-07 10:35:29 +01002265 std::vector<spirv_inst_iter> xfb_offsets;
2266
sjfricke4f600c82022-06-09 14:21:32 +09002267 for (const auto &op_decorate : module_state.GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002268 uint32_t decoration = op_decorate.word(2);
2269 if (decoration == spv::DecorationXfbStride) {
2270 uint32_t stride = op_decorate.word(3);
2271 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2272 skip |= LogError(
2273 device, "VUID-RuntimeSpirv-XfbStride-06313",
2274 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2275 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2276 ").",
2277 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2278 }
2279 }
ziga-lunarg423cf212021-11-07 00:00:27 +01002280 if (decoration == spv::DecorationStream) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002281 xfb_streams.push_back(op_decorate);
ziga-lunarg423cf212021-11-07 00:00:27 +01002282 uint32_t stream = op_decorate.word(3);
2283 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2284 skip |= LogError(
2285 device, "VUID-RuntimeSpirv-Stream-06312",
2286 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2287 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32 ").",
2288 stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
2289 }
2290 }
ziga-lunargef2c3172021-11-07 10:35:29 +01002291 if (decoration == spv::DecorationXfbBuffer) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002292 xfb_buffers.push_back(op_decorate);
ziga-lunargef2c3172021-11-07 10:35:29 +01002293 }
2294 if (decoration == spv::DecorationOffset) {
2295 xfb_offsets.push_back(op_decorate);
2296 }
2297 }
2298
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002299 // XfbBuffer, buffer data size
2300 std::vector<std::pair<uint32_t, uint32_t>> buffer_data_sizes;
ziga-lunargef2c3172021-11-07 10:35:29 +01002301 for (const auto &op_decorate : xfb_offsets) {
2302 for (const auto xfb_buffer : xfb_buffers) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002303 if (xfb_buffer.word(1) == op_decorate.word(1)) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002304 const auto offset = op_decorate.word(3);
sjfricke4f600c82022-06-09 14:21:32 +09002305 const auto def = module_state.get_def(xfb_buffer.word(1));
2306 const auto size = module_state.GetTypeBytesSize(def);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002307 const uint32_t buffer_data_size = offset + size;
2308 if (buffer_data_size > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002309 skip |= LogError(
2310 device, "VUID-RuntimeSpirv-Offset-06308",
2311 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_offset (%" PRIu32
2312 ") + size of variable (%" PRIu32 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2313 "(%" PRIu32 ").",
2314 offset, size, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2315 }
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002316
2317 bool found = false;
2318 for (auto &bds : buffer_data_sizes) {
2319 if (bds.first == xfb_buffer.word(1)) {
2320 bds.second = std::max(bds.second, buffer_data_size);
2321 found = true;
2322 break;
2323 }
2324 }
2325 if (!found) {
2326 buffer_data_sizes.emplace_back(xfb_buffer.word(1), buffer_data_size);
2327 }
2328
ziga-lunargef2c3172021-11-07 10:35:29 +01002329 break;
2330 }
2331 }
ziga-lunargce66e542021-09-19 00:11:14 +02002332 }
2333
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002334 std::unordered_map<uint32_t, uint32_t> stream_data_size;
2335 for (const auto &xfb_stream : xfb_streams) {
2336 for (const auto& bds : buffer_data_sizes) {
2337 if (xfb_stream.word(1) == bds.first) {
2338 uint32_t stream = xfb_stream.word(3);
2339 const auto itr = stream_data_size.find(stream);
2340 if (itr != stream_data_size.end()) {
2341 itr->second += bds.second;
2342 } else {
2343 stream_data_size.insert({stream, bds.second});
2344 }
2345 }
2346 }
2347 }
2348
2349 for (const auto& stream : stream_data_size) {
2350 if (stream.second > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreamDataSize) {
2351 skip |= LogError(device, "VUID-RuntimeSpirv-XfbBuffer-06309",
2352 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2353 ") having the sum of buffer data sizes (%" PRIu32
2354 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2355 "(%" PRIu32 ").",
2356 stream.first, stream.second,
2357 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2358 }
2359 }
2360
ziga-lunargce66e542021-09-19 00:11:14 +02002361 return skip;
2362}
2363
sjfrickede734312022-07-14 19:22:43 +09002364bool CoreChecks::ValidateComputeSharedMemory(const SHADER_MODULE_STATE &module_state, uint32_t total_shared_size) const {
sjfricke44d663c2022-06-01 06:42:58 +09002365 bool skip = false;
sjfrickede734312022-07-14 19:22:43 +09002366
2367 // If not found before with spec constants, find here
2368 if (total_shared_size == 0) {
2369 // when using WorkgroupMemoryExplicitLayoutKHR
2370 // either all or none the structs are decorated with Block,
2371 // if using block, all must decorated with Aliased.
2372 // In this case we want to find the MAX not ADD the block sizes
2373 bool find_max_block = false;
2374
sjfricke44d663c2022-06-01 06:42:58 +09002375 for (auto insn : module_state.static_data_.variable_inst) {
sjfrickede734312022-07-14 19:22:43 +09002376 // StorageClass Workgroup is shared memory
2377 if (insn.word(3) == spv::StorageClassWorkgroup) {
2378 if (module_state.get_decorations(insn.word(2)).flags & decoration_set::aliased_bit) {
2379 find_max_block = true;
2380 }
2381
sjfricke44d663c2022-06-01 06:42:58 +09002382 const uint32_t result_type_id = insn.word(1);
2383 const auto result_type = module_state.get_def(result_type_id);
2384 const auto type = module_state.get_def(result_type.word(3));
sjfrickede734312022-07-14 19:22:43 +09002385 const uint32_t variable_shared_size = module_state.GetTypeBytesSize(type);
2386
2387 if (find_max_block) {
2388 total_shared_size = std::max(total_shared_size, variable_shared_size);
2389 } else {
2390 total_shared_size += variable_shared_size;
2391 }
sjfricke44d663c2022-06-01 06:42:58 +09002392 }
2393 }
sjfrickede734312022-07-14 19:22:43 +09002394 }
2395
2396 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2397 skip |=
2398 LogError(device, "VUID-RuntimeSpirv-Workgroup-06530",
2399 "Shader uses %" PRIu32
2400 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
2401 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sjfricke44d663c2022-06-01 06:42:58 +09002402 }
2403 return skip;
2404}
2405
Tony-LunarG1672d002022-08-03 14:35:34 -06002406bool CoreChecks::ValidateShaderModuleId(const SHADER_MODULE_STATE &module_state, const PipelineStageState &stage_state,
2407 const safe_VkPipelineShaderStageCreateInfo *pStage, const VkPipelineCreateFlags flags) const {
2408 bool skip = false;
2409 const auto module_identifier = LvlFindInChain<VkPipelineShaderStageModuleIdentifierCreateInfoEXT>(pStage->pNext);
2410 const auto module_create_info = LvlFindInChain<VkShaderModuleCreateInfo>(pStage->pNext);
2411 if (module_identifier && (module_identifier->identifierSize > 0)) {
2412 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
2413 skip |= LogError(
2414 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-pNext-06850",
2415 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2416 "struct in the pNext chain but the shaderModuleIdentifier feature is not enabled",
2417 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2418 string_VkShaderStageFlagBits(stage_state.stage_flag));
2419 }
2420 if (!(flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT)) {
2421 skip |= LogError(
2422 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-pNext-06851",
2423 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2424 "struct in the pNext chain whose identifierSize is > 0 (%" PRIu32
2425 "), but the "
2426 "VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT bit is not set in the pipeline create flags",
2427 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2428 string_VkShaderStageFlagBits(stage_state.stage_flag), module_identifier->identifierSize);
2429 }
2430 if (module_identifier->identifierSize > VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT) {
2431 skip |= LogError(
2432 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-identifierSize-06852",
2433 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2434 "struct in the pNext chain whose identifierSize (%" PRIu32
2435 ") is > VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT (%" PRIu32 ")",
2436 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2437 string_VkShaderStageFlagBits(stage_state.stage_flag), module_identifier->identifierSize,
2438 VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT);
2439 }
2440 }
2441 if (module_identifier && module_create_info) {
2442 skip |= LogError(
2443 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06844",
2444 "%s module (stage %s) VkPipelineShaderStageCreateInfo has both a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2445 "struct and a VkShaderModuleCreateInfo struct in the pNext chain",
2446 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2447 string_VkShaderStageFlagBits(stage_state.stage_flag));
2448 }
2449 if (enabled_features.graphics_pipeline_library_features.graphicsPipelineLibrary) {
2450 if (!module_identifier && pStage->module == VK_NULL_HANDLE && !module_create_info) {
2451 skip |= LogError(
2452 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06845",
2453 "%s module (stage %s) VkPipelineShaderStageCreateInfo has no VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2454 "struct and no VkShaderModuleCreateInfo struct in the pNext chain, and module is not a valid VkShaderModule",
2455 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2456 string_VkShaderStageFlagBits(stage_state.stage_flag));
2457 }
2458 } else {
2459 if (!module_identifier && pStage->module == VK_NULL_HANDLE) {
2460 const char *vuid = IsExtEnabled(device_extensions.vk_khr_pipeline_library)
2461 ? "VUID-VkPipelineShaderStageCreateInfo-stage-06846"
2462 : "VUID-VkPipelineShaderStageCreateInfo-stage-06847";
2463 skip |= LogError(
2464 device, vuid,
2465 "%s module (stage %s) VkPipelineShaderStageCreateInfo has no VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2466 "struct in the pNext chain, the graphicsPipelineLibrary feature is not enabled, and module is not a valid "
2467 "VkShaderModule",
2468 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2469 string_VkShaderStageFlagBits(stage_state.stage_flag));
2470 }
2471 }
2472 if (module_identifier && pStage->module != VK_NULL_HANDLE) {
2473 skip |= LogError(
2474 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06848",
2475 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2476 "struct in the pNext chain, and module is not VK_NULL_HANDLE",
2477 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2478 string_VkShaderStageFlagBits(stage_state.stage_flag));
2479 }
2480 return skip;
2481}
2482
sjfricke6a03e012022-06-23 17:54:11 +09002483// Temporary data of a OpVariable when validating it.
2484// If found useful in another location, can move out to the header
2485struct VariableInstInfo {
2486 bool has_8bit = false;
2487 bool has_16bit = false;
2488};
2489
2490// easier to use recursion to traverse the OpTypeStruct
2491static void GetVariableInfo(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &insn, VariableInstInfo &info) {
2492 if (insn.opcode() == spv::OpTypeFloat || insn.opcode() == spv::OpTypeInt) {
2493 const uint32_t bit_width = insn.word(2);
2494 info.has_8bit |= (bit_width == 8);
2495 info.has_16bit |= (bit_width == 16);
2496 } else if (insn.opcode() == spv::OpTypeStruct) {
2497 for (uint32_t i = 2; i < insn.len(); i++) {
2498 const auto &base_insn = GetBaseTypeIter(module_state, insn.word(i));
2499 GetVariableInfo(module_state, base_insn, info);
2500 }
2501 }
2502}
2503
sjfricke44d663c2022-06-01 06:42:58 +09002504bool CoreChecks::ValidateVariables(const SHADER_MODULE_STATE &module_state) const {
2505 bool skip = false;
2506
2507 for (auto insn : module_state.static_data_.variable_inst) {
2508 const uint32_t storage_class = insn.word(3);
2509
2510 if (storage_class == spv::StorageClassWorkgroup) {
2511 // If Workgroup variable is initalized, make sure the feature is enabled
2512 if (insn.len() > 4 &&
2513 !enabled_features.zero_initialize_work_group_memory_features.shaderZeroInitializeWorkgroupMemory) {
2514 const char *vuid = IsExtEnabled(device_extensions.vk_khr_zero_initialize_workgroup_memory)
2515 ? "VUID-RuntimeSpirv-shaderZeroInitializeWorkgroupMemory-06372"
2516 : "VUID-RuntimeSpirv-OpVariable-06373";
2517 skip |= LogError(
2518 device, vuid,
2519 "vkCreateShaderModule(): "
2520 "VkPhysicalDeviceZeroInitializeWorkgroupMemoryFeaturesKHR::shaderZeroInitializeWorkgroupMemory is not enabled, "
2521 "but shader contains an OpVariable with Workgroup Storage Class with an Initializer operand.\n%s",
2522 module_state.DescribeInstruction(insn).c_str());
2523 }
2524 }
sjfricke6a03e012022-06-23 17:54:11 +09002525
2526 const auto type_pointer = module_state.get_def(insn.word(1));
2527 const auto type = module_state.get_def(type_pointer.word(3));
2528 // type will either be a float, int, or struct and if struct need to traverse it
2529 VariableInstInfo info;
2530 GetVariableInfo(module_state, type, info);
2531
2532 if (info.has_8bit) {
2533 if (!enabled_features.core12.storageBuffer8BitAccess &&
2534 (storage_class == spv::StorageClassStorageBuffer || storage_class == spv::StorageClassShaderRecordBufferKHR || storage_class == spv::StorageClassPhysicalStorageBuffer)) {
2535 skip |= LogError(device, "VUID-RuntimeSpirv-storageBuffer8BitAccess-06328",
2536 "vkCreateShaderModule(): storageBuffer8BitAccess is not enabled, but shader contains an 8-bit "
2537 "OpVariable with %s Storage Class.\n%s",
2538 StorageClassName(storage_class), module_state.DescribeInstruction(insn).c_str());
2539 }
2540 if (!enabled_features.core12.uniformAndStorageBuffer8BitAccess && storage_class == spv::StorageClassUniform) {
2541 skip |= LogError(device, "VUID-RuntimeSpirv-uniformAndStorageBuffer8BitAccess-06329",
2542 "vkCreateShaderModule(): uniformAndStorageBuffer8BitAccess is not enabled, but shader contains an "
2543 "8-bit OpVariable with Uniform Storage Class.\n%s",
2544 module_state.DescribeInstruction(insn).c_str());
2545 }
2546 if (!enabled_features.core12.storagePushConstant8 && storage_class == spv::StorageClassPushConstant) {
2547 skip |= LogError(device, "VUID-RuntimeSpirv-storagePushConstant8-06330",
2548 "vkCreateShaderModule(): storagePushConstant8 is not enabled, but shader contains an 8-bit "
2549 "OpVariable with PushConstant Storage Class.\n%s",
2550 module_state.DescribeInstruction(insn).c_str());
2551 }
2552 }
2553
2554 if (info.has_16bit) {
2555 if (!enabled_features.core11.storageBuffer16BitAccess &&
2556 (storage_class == spv::StorageClassStorageBuffer || storage_class == spv::StorageClassShaderRecordBufferKHR || storage_class == spv::StorageClassPhysicalStorageBuffer)) {
2557 skip |= LogError(device, "VUID-RuntimeSpirv-storageBuffer16BitAccess-06331",
2558 "vkCreateShaderModule(): storageBuffer16BitAccess is not enabled, but shader contains an 16-bit "
2559 "OpVariable with %s Storage Class.\n%s",
2560 StorageClassName(storage_class), module_state.DescribeInstruction(insn).c_str());
2561 }
2562 if (!enabled_features.core11.uniformAndStorageBuffer16BitAccess && storage_class == spv::StorageClassUniform) {
2563 skip |= LogError(device, "VUID-RuntimeSpirv-uniformAndStorageBuffer16BitAccess-06332",
2564 "vkCreateShaderModule(): uniformAndStorageBuffer16BitAccess is not enabled, but shader contains an "
2565 "16-bit OpVariable with Uniform Storage Class.\n%s",
2566 module_state.DescribeInstruction(insn).c_str());
2567 }
2568 if (!enabled_features.core11.storagePushConstant16 && storage_class == spv::StorageClassPushConstant) {
2569 skip |= LogError(device, "VUID-RuntimeSpirv-storagePushConstant16-06333",
2570 "vkCreateShaderModule(): storagePushConstant16 is not enabled, but shader contains an 16-bit "
2571 "OpVariable with PushConstant Storage Class.\n%s",
2572 module_state.DescribeInstruction(insn).c_str());
2573 }
2574 if (!enabled_features.core11.storageInputOutput16 &&
2575 (storage_class == spv::StorageClassInput || storage_class == spv::StorageClassOutput)) {
2576 skip |= LogError(device, "VUID-RuntimeSpirv-storageInputOutput16-06334",
2577 "vkCreateShaderModule(): storageInputOutput16 is not enabled, but shader contains an 16-bit "
2578 "OpVariable with %s Storage Class.\n%s",
2579 StorageClassName(storage_class), module_state.DescribeInstruction(insn).c_str());
2580 }
2581 }
sjfricke44d663c2022-06-01 06:42:58 +09002582 }
2583
2584 return skip;
2585}
2586
sjfricke4f600c82022-06-09 14:21:32 +09002587bool CoreChecks::ValidateTransformFeedback(const SHADER_MODULE_STATE &module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002588 bool skip = false;
2589
ziga-lunarg28d08792021-10-13 15:42:59 +02002590 // Temp workaround to prevent false positive errors
2591 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sjfricke4f600c82022-06-09 14:21:32 +09002592 if (module_state.HasMultipleEntryPoints()) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002593 return skip;
2594 }
2595
2596 layer_data::unordered_set<uint32_t> emitted_streams;
2597 bool output_points = false;
sjfricke4f600c82022-06-09 14:21:32 +09002598 for (const auto &insn : module_state) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002599 const uint32_t opcode = insn.opcode();
2600 if (opcode == spv::OpEmitStreamVertex) {
sjfricke4f600c82022-06-09 14:21:32 +09002601 emitted_streams.emplace(static_cast<uint32_t>(module_state.GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002602 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002603 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
sjfricke4f600c82022-06-09 14:21:32 +09002604 uint32_t stream = static_cast<uint32_t>(module_state.GetConstantValueById(insn.word(1)));
ziga-lunarg28d08792021-10-13 15:42:59 +02002605 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2606 skip |= LogError(
2607 device, "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002608 "vkCreateGraphicsPipelines(): shader uses transform feedback stream\n%s\nwith index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002609 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2610 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002611 module_state.DescribeInstruction(insn).c_str(), stream,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002612 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002613 }
2614 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002615 if ((opcode == spv::OpExecutionMode || opcode == spv::OpExecutionModeId) &&
2616 insn.word(2) == spv::ExecutionModeOutputPoints) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002617 output_points = true;
2618 }
2619 }
2620
2621 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2622 if (emitted_streams_size > 1 && !output_points &&
2623 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
2624 skip |= LogError(
2625 device, "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2626 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles is VK_FALSE, but execution mode is not OutputPoints.",
2627 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002628 }
2629
2630 return skip;
2631}
2632
sfricke-samsung864162a2021-11-01 21:58:01 -07002633// Checks for both TexelOffset and TexelGatherOffset limits
sjfricke4f600c82022-06-09 14:21:32 +09002634bool CoreChecks::ValidateTexelOffsetLimits(const SHADER_MODULE_STATE &module_state, spirv_inst_iter &insn) const {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002635 bool skip = false;
2636
2637 const uint32_t opcode = insn.opcode();
sfricke-samsung864162a2021-11-01 21:58:01 -07002638 if (ImageGatherOperation(opcode) || ImageSampleOperation(opcode) || ImageFetchOperation(opcode)) {
sfricke-samsung3a25ed52022-01-20 02:24:36 -08002639 uint32_t image_operand_position = OpcodeImageOperandsPosition(opcode);
sfricke-samsung864162a2021-11-01 21:58:01 -07002640 // Image operands can be optional
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002641 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2642 auto image_operand = insn.word(image_operand_position);
sfricke-samsung864162a2021-11-01 21:58:01 -07002643 // Bits we are validating (sample/fetch only check ConstOffset)
ziga-lunarga12c75a2021-09-16 16:36:16 +02002644 uint32_t offset_bits =
sfricke-samsung864162a2021-11-01 21:58:01 -07002645 ImageGatherOperation(opcode)
2646 ? (spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask)
2647 : (spv::ImageOperandsConstOffsetMask);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002648 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002649 // Operand values follow
2650 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002651 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2652 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2653 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2654 if (image_operand & i) { // If the bit is set, consume operand
2655 if (insn.len() > index && (i & offset_bits)) {
2656 uint32_t constant_id = insn.word(index);
sjfricke4f600c82022-06-09 14:21:32 +09002657 const auto &constant = module_state.get_def(constant_id);
2658 const bool is_dynamic_offset = constant == module_state.end();
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002659 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002660 for (uint32_t j = 3; j < constant.len(); ++j) {
2661 uint32_t comp_id = constant.word(j);
sjfricke4f600c82022-06-09 14:21:32 +09002662 const auto &comp = module_state.get_def(comp_id);
2663 const auto &comp_type = module_state.get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002664 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002665 const uint32_t offset = comp.word(3);
sfricke-samsung864162a2021-11-01 21:58:01 -07002666 // spec requires minTexelGatherOffset/minTexelOffset to be -8 or less so never can compare if
2667 // unsigned spec requires maxTexelGatherOffset/maxTexelOffset to be 7 or greater so never can
2668 // compare if signed is less then zero
sfricke-samsungef3fe742021-10-06 10:51:34 -07002669 const int32_t signed_offset = static_cast<int32_t>(offset);
2670 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2671
sfricke-samsung864162a2021-11-01 21:58:01 -07002672 // There are 2 sets of VU being covered where the only main difference is the opcode
2673 if (ImageGatherOperation(opcode)) {
2674 // min/maxTexelGatherOffset
2675 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
2676 skip |=
2677 LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002678 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002679 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002680 module_state.DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002681 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002682 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2683 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002684 skip |= LogError(device, "VUID-RuntimeSpirv-OpImage-06377",
2685 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2686 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32
2687 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002688 module_state.DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002689 phys_dev_props.limits.maxTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002690 }
2691 } else {
2692 // min/maxTexelOffset
2693 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelOffset)) {
2694 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06435",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002695 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsung864162a2021-11-01 21:58:01 -07002696 ") less than VkPhysicalDeviceLimits::minTexelOffset (%" PRIi32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002697 module_state.DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung864162a2021-11-01 21:58:01 -07002698 phys_dev_props.limits.minTexelOffset);
2699 } else if ((offset > phys_dev_props.limits.maxTexelOffset) &&
2700 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002701 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06436",
2702 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2703 ") greater than VkPhysicalDeviceLimits::maxTexelOffset (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002704 module_state.DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002705 phys_dev_props.limits.maxTexelOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002706 }
ziga-lunarga12c75a2021-09-16 16:36:16 +02002707 }
2708 }
2709 }
2710 }
sfricke-samsung3511e312021-11-04 21:14:31 -07002711 index += ImageOperandsParamCount(i);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002712 }
2713 }
2714 }
2715 }
2716 }
2717
2718 return skip;
2719}
2720
sjfricke4f600c82022-06-09 14:21:32 +09002721bool CoreChecks::ValidateShaderClock(const SHADER_MODULE_STATE &module_state, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002722 bool skip = false;
2723
sfricke-samsung94167ca2021-02-26 04:14:59 -08002724 switch (insn.opcode()) {
2725 case spv::OpReadClockKHR: {
sjfricke4f600c82022-06-09 14:21:32 +09002726 auto scope_id = module_state.get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -08002727 auto scope_type = scope_id.word(3);
2728 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002729 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002730 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002731 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002732 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2733 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002734 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002735 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002736 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002737 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2738 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002739 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002740 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002741 }
2742 }
2743 return skip;
2744}
2745
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002746bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2747 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002748 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002749 const auto *pStage = stage_state.create_info;
sjfricke4f600c82022-06-09 14:21:32 +09002750 const SHADER_MODULE_STATE &module_state = *stage_state.module_state.get();
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002751 const auto &entrypoint = stage_state.entrypoint;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002752
Tony-LunarG1672d002022-08-03 14:35:34 -06002753 skip |= ValidateShaderModuleId(module_state, stage_state, pStage, pipeline->GetPipelineCreateFlags());
2754
Tony-LunarGcab5d812022-08-04 14:07:32 -06002755 if (module_state.vk_shader_module() == VK_NULL_HANDLE) return skip; // No real shader for further validation
2756
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002757 // to prevent const_cast on pipeline object, just store here as not needed outside function anyway
2758 uint32_t local_size_x = 0;
2759 uint32_t local_size_y = 0;
2760 uint32_t local_size_z = 0;
sjfrickede734312022-07-14 19:22:43 +09002761 uint32_t total_shared_size = 0;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002762
John Zulauf14c355b2019-06-27 16:09:37 -06002763 // Check the module
sjfricke4f600c82022-06-09 14:21:32 +09002764 if (!module_state.has_valid_spirv) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002765 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2766 "%s does not contain valid spirv for stage %s.",
sjfricke4f600c82022-06-09 14:21:32 +09002767 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
sfricke-samsungef15e482022-01-26 11:32:49 -08002768 string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002769 }
2770
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002771 // If specialization-constant instructions are present in the shader, the specializations should be applied.
sjfricke4f600c82022-06-09 14:21:32 +09002772 if (module_state.HasSpecConstants()) {
sfricke-samsung5628f982021-10-19 09:21:59 -07002773 // both spirv-opt and spirv-val will use the same flags
2774 spvtools::ValidatorOptions options;
2775 AdjustValidatorOptions(device_extensions, enabled_features, options);
2776
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002777 // setup the call back if the optimizer fails
sfricke-samsung45996a42021-09-16 13:45:27 -07002778 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002779 spvtools::Optimizer optimizer(spirv_environment);
sfricke-samsungef15e482022-01-26 11:32:49 -08002780 spvtools::MessageConsumer consumer = [&skip, &module_state, &stage_state, this](
2781 spv_message_level_t level, const char *source, const spv_position_t &position,
2782 const char *message) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002783 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2784 "%s does not contain valid spirv for stage %s. %s",
sjfricke4f600c82022-06-09 14:21:32 +09002785 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002786 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002787 };
2788 optimizer.SetMessageConsumer(consumer);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002789
2790 // The app might be using the default spec constant values, but if they pass values at runtime to the pipeline then need to
2791 // use those values to apply to the spec constants
2792 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2793 pStage->pSpecializationInfo->pMapEntries != nullptr) {
2794 // Gather the specialization-constant values.
2795 auto const &specialization_info = pStage->pSpecializationInfo;
2796 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
2797 std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; // note: this must be std:: to work with spvtools
2798 id_value_map.reserve(specialization_info->mapEntryCount);
2799 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2800 auto const &map_entry = specialization_info->pMapEntries[i];
sjfricke4f600c82022-06-09 14:21:32 +09002801 const auto itr = module_state.GetSpecConstMap().find(map_entry.constantID);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002802 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2803 // behavior of the pipeline."
sjfricke4f600c82022-06-09 14:21:32 +09002804 if (itr != module_state.GetSpecConstMap().cend()) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002805 // Make sure map_entry.size matches the spec constant's size
2806 uint32_t spec_const_size = decoration_set::kInvalidValue;
sjfricke4f600c82022-06-09 14:21:32 +09002807 const auto def_ins = module_state.get_def(itr->second);
2808 const auto type_ins = module_state.get_def(def_ins.word(1));
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002809 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2810 switch (type_ins.opcode()) {
2811 case spv::OpTypeBool:
2812 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2813 spec_const_size = sizeof(VkBool32);
2814 break;
2815 case spv::OpTypeInt:
2816 case spv::OpTypeFloat:
2817 spec_const_size = type_ins.word(2) / 8;
2818 break;
2819 default:
2820 // spirv-val should catch if SpecId is not used on a
2821 // OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant and OpSpecConstant is validated to be a
2822 // OpTypeInt or OpTypeFloat
2823 break;
2824 }
2825
2826 if (map_entry.size != spec_const_size) {
2827 skip |= LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
2828 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2829 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32
2830 " from shader definition.",
2831 map_entry.constantID, i, map_entry.size,
sjfricke4f600c82022-06-09 14:21:32 +09002832 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002833 }
2834 }
2835
2836 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
2837 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2838 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2839 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2840 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2841 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2842
2843 std::copy(start_in_p, end_in_p, out_p);
2844 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
2845 }
2846 }
2847
2848 // This pass takes the runtime spec const values and applies it into the SPIR-V
2849 // will turn a spec constant like
2850 // OpSpecConstant %uint 1
2851 // to a use the value passed in instead (for example if the value is 32) so now it looks like
2852 // OpSpecConstant %uint 32
2853 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2854 }
2855
2856 // This pass will turn OpSpecConstant into a OpConstant (also OpSpecConstantTrue/OpSpecConstantFalse)
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002857 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002858 // Using the new frozen OpConstant all OpSpecConstantComposite can be resolved turning them into OpConstantComposite
2859 // This is need incase a shdaer looks like:
2860 //
2861 // layout(constant_id = 0) const uint x = 64;
2862 // shared uint arr[x > 64 ? 64 : x];
2863 //
2864 // this will generate branch/switch statements that we want to leverage spirv-opt to apply to make parsing easier
2865 optimizer.RegisterPass(spvtools::CreateFoldSpecConstantOpAndCompositePass());
sjfricke284a13f2022-08-16 15:34:31 +09002866 // Currently need to re-run the pass as spirv-opt has a bug and not folding everything sometimes
2867 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/pull/4399#issuecomment-1216203563
2868 optimizer.RegisterPass(spvtools::CreateFoldSpecConstantOpAndCompositePass());
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002869
2870 // Apply the specialization-constant values and revalidate the shader module is valid.
Tony-LunarG1672d002022-08-03 14:35:34 -06002871 const char *pSpecializationInfo_vuid = IsExtEnabled(device_extensions.vk_ext_shader_module_identifier)
2872 ? "VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06849"
2873 : "VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06719";
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002874 std::vector<uint32_t> specialized_spirv;
sfricke-samsungef15e482022-01-26 11:32:49 -08002875 auto const optimized =
sjfricke4f600c82022-06-09 14:21:32 +09002876 optimizer.Run(module_state.words.data(), module_state.words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002877 if (optimized) {
2878 spv_context ctx = spvContextCreate(spirv_environment);
2879 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2880 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002881 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2882 if (spv_valid != SPV_SUCCESS) {
Tony-LunarG1672d002022-08-03 14:35:34 -06002883 skip |= LogError(device, pSpecializationInfo_vuid,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002884 "After specialization was applied, %s does not contain valid spirv for stage %s.",
sjfricke4f600c82022-06-09 14:21:32 +09002885 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002886 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002887 }
2888
sjfrickea11e42e2022-07-20 14:27:01 +09002889 // The new optimized SPIR-V will NOT match the original SHADER_MODULE_STATE object parsing, so a new SHADER_MODULE_STATE
2890 // object is needed. This an issue due to each pipeline being able to reuse the same shader module but with different
2891 // spec constant values.
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002892 SHADER_MODULE_STATE spec_mod(specialized_spirv);
2893
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002894 // According to https://github.com/KhronosGroup/Vulkan-Docs/issues/1671 anything labeled as "static use" (such as if an
2895 // input is used or not) don't have to be checked post spec constants freezing since the device compiler is not
2896 // guaranteed to run things such as dead-code elimination. The following checks are things that don't follow under
2897 // "static use" rules and need to be validated still.
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002898 auto specialized_it = spec_mod.begin();
sjfrickede734312022-07-14 19:22:43 +09002899
2900 // see ValidateComputeSharedMemory() for details why we might track max block size
2901 layer_data::unordered_set<uint32_t> aliased_id;
2902 bool find_max_block = false;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002903
2904 uint32_t workgroup_size_id = 0; // result id can't be zero
2905 uint32_t local_size_id_x = 0;
2906 uint32_t local_size_id_y = 0;
2907 uint32_t local_size_id_z = 0;
2908
2909 // make single interation through new shader
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002910 while (specialized_it != spec_mod.end()) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002911 const uint32_t opcode = specialized_it.opcode();
2912
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002913 if (opcode == spv::OpExecutionModeId && specialized_it.word(2) == spv::ExecutionModeLocalSizeId) {
2914 local_size_id_x = specialized_it.word(3);
2915 local_size_id_y = specialized_it.word(4);
2916 local_size_id_z = specialized_it.word(5);
2917 }
2918
sjfrickede734312022-07-14 19:22:43 +09002919 if (opcode == spv::OpDecorate) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002920 // Validate applied WorkgroupSize is still below maxComputeWorkGroupSize limit
sjfrickede734312022-07-14 19:22:43 +09002921 if (specialized_it.word(2) == spv::DecorationBuiltIn && specialized_it.word(3) == spv::BuiltInWorkgroupSize) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002922 // Will be a OpConstantComposite and always have the OpDecorate section
2923 workgroup_size_id = specialized_it.word(1);
2924 }
sjfrickede734312022-07-14 19:22:43 +09002925 if (specialized_it.word(2) == spv::DecorationAliased) {
2926 aliased_id.emplace(specialized_it.word(1));
2927 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002928 }
2929
2930 if (opcode == spv::OpConstantComposite && workgroup_size_id == specialized_it.word(2)) {
2931 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32 so this can be assuemd
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002932 local_size_x = spec_mod.get_def(specialized_it.word(3)).word(3);
2933 local_size_y = spec_mod.get_def(specialized_it.word(4)).word(3);
2934 local_size_z = spec_mod.get_def(specialized_it.word(5)).word(3);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002935 }
sjfrickede734312022-07-14 19:22:43 +09002936
2937 if (opcode == spv::OpVariable && specialized_it.word(3) == spv::StorageClassWorkgroup) {
2938 if (aliased_id.find(specialized_it.word(2)) != aliased_id.end()) {
2939 find_max_block = true;
2940 }
2941
2942 const uint32_t result_type_id = specialized_it.word(1);
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002943 const auto result_type = spec_mod.get_def(result_type_id);
2944 const auto type = spec_mod.get_def(result_type.word(3));
2945 const uint32_t variable_shared_size = spec_mod.GetTypeBitsSize(type) / 8;
sjfrickede734312022-07-14 19:22:43 +09002946
2947 if (find_max_block) {
2948 total_shared_size = std::max(total_shared_size, variable_shared_size);
2949 } else {
2950 total_shared_size += variable_shared_size;
2951 }
2952 }
2953
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002954 ++specialized_it;
2955 }
2956
2957 // if after no WorkgroupSize is found, then can apply any possible LocalSizeId due to precedence order
2958 if (local_size_x == 0 && local_size_id_x != 0) {
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002959 local_size_x = spec_mod.get_def(local_size_id_x).word(3);
2960 local_size_y = spec_mod.get_def(local_size_id_y).word(3);
2961 local_size_z = spec_mod.get_def(local_size_id_z).word(3);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002962 }
2963
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002964 spvDiagnosticDestroy(diag);
2965 spvContextDestroy(ctx);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002966 } else {
2967 // Should never get here, but better then asserting
Tony-LunarG1672d002022-08-03 14:35:34 -06002968 skip |= LogError(device, pSpecializationInfo_vuid,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002969 "%s module (stage %s) attempted to apply specialization constants with spirv-opt but failed.",
sjfricke4f600c82022-06-09 14:21:32 +09002970 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002971 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002972 }
2973 }
2974
John Zulauf14c355b2019-06-27 16:09:37 -06002975 // Check the entrypoint
sjfricke4f600c82022-06-09 14:21:32 +09002976 if (entrypoint == module_state.end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002977 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2978 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002979 }
2980 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2981
2982 // Mark accessible ids
2983 auto &accessible_ids = stage_state.accessible_ids;
2984
Chris Forbes47567b72017-06-09 12:09:45 -07002985 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002986
sfricke-samsung94167ca2021-02-26 04:14:59 -08002987 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2988 // and mainly only checking the instruction in detail for a single operation
sjfricke4f600c82022-06-09 14:21:32 +09002989 for (auto insn : module_state) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002990 skip |= ValidateTexelOffsetLimits(module_state, insn);
2991 skip |= ValidateShaderCapabilitiesAndExtensions(insn);
2992 skip |= ValidateShaderClock(module_state, insn);
2993 skip |= ValidateShaderStageGroupNonUniform(module_state, pStage->stage, insn);
2994 skip |= ValidateMemoryScope(module_state, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08002995
2996 // Checks based off shaderStorageImage(Read|Write)WithoutFormat are
2997 // disabled if VK_KHR_format_feature_flags2 is supported.
2998 //
2999 // https://github.com/KhronosGroup/Vulkan-Docs/blob/6177645341afc/appendices/spirvenv.txt#L553
3000 //
3001 // The other checks need to take into account the format features and so
3002 // we apply that in the descriptor set matching validation code (see
3003 // descriptor_sets.cpp).
3004 if (!has_format_feature2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003005 skip |= ValidateShaderStorageImageFormats(module_state, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08003006 }
ziga-lunarga26b3602021-08-08 15:53:00 +02003007 }
3008
sfricke-samsungef15e482022-01-26 11:32:49 -08003009 skip |= ValidateTransformFeedback(module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003010 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
3011 stage_state.has_atomic_descriptor);
sfricke-samsungef15e482022-01-26 11:32:49 -08003012 skip |= ValidateShaderStageInputOutputLimits(module_state, pStage, pipeline, entrypoint);
sfricke-samsungdc96f302020-03-18 20:42:10 -07003013 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsungef15e482022-01-26 11:32:49 -08003014 skip |= ValidateAtomicsTypes(module_state);
3015 skip |= ValidateExecutionModes(module_state, entrypoint, pStage->stage, pipeline);
ziga-lunargae2a5c42021-07-23 16:18:09 +02003016 skip |= ValidateSpecializations(pStage);
sfricke-samsungef15e482022-01-26 11:32:49 -08003017 skip |= ValidateDecorations(module_state);
sjfricke4f600c82022-06-09 14:21:32 +09003018 skip |= ValidateVariables(module_state);
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003019 const auto *raster_state = pipeline->RasterizationState();
Nathaniel Cesario81257cb2022-02-16 17:15:58 -07003020 if (check_point_size && raster_state && !raster_state->rasterizerDiscardEnable) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003021 skip |= ValidatePointListShaderState(pipeline, module_state, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003022 }
sfricke-samsungef15e482022-01-26 11:32:49 -08003023 skip |= ValidateBuiltinLimits(module_state, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08003024 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003025 skip |= ValidateCooperativeMatrix(module_state, pStage, pipeline);
sfricke-samsungd093e522021-02-26 04:17:45 -08003026 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00003027 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003028 skip |= ValidatePrimitiveRateShaderState(pipeline, module_state, entrypoint, pStage->stage);
Tobias Hector6663c9b2020-11-05 10:18:02 +00003029 }
sfricke-samsung45996a42021-09-16 13:45:27 -07003030 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003031 skip |= ValidateShaderResolveQCOM(module_state, pStage, pipeline);
Jeff Leger9b3dcff2021-05-27 15:40:20 -04003032 }
ziga-lunarg73163742021-08-25 13:15:29 +02003033 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
3034 skip |= ValidateShaderSubgroupSizeControl(pStage);
3035 }
Chris Forbes47567b72017-06-09 12:09:45 -07003036
sfricke-samsung7699b912021-04-12 23:01:51 -07003037 // "layout must be consistent with the layout of the * shader"
3038 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003039 std::string vuid_layout_mismatch;
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003040 switch (pipeline->GetCreateInfoSType()) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003041 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
3042 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
3043 break;
3044 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
3045 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
3046 break;
3047 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
3048 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
3049 break;
3050 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
3051 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
3052 break;
3053 default:
3054 assert(false);
3055 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003056 }
3057
sfricke-samsung7699b912021-04-12 23:01:51 -07003058 // Validate Push Constants use
sfricke-samsungef15e482022-01-26 11:32:49 -08003059 skip |= ValidatePushConstantUsage(*pipeline, module_state, pStage, vuid_layout_mismatch);
sfricke-samsung7699b912021-04-12 23:01:51 -07003060
Chris Forbes47567b72017-06-09 12:09:45 -07003061 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003062 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07003063 // Verify given pipelineLayout has requested setLayout with requested binding
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003064 // const auto& layout_state = (stage_state.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) ?
3065 // pipeline->PreRasterPipelineLayoutState() : pipeline->FragmentShaderPipelineLayoutState();
3066 const auto &binding = GetDescriptorBinding(pipeline->PipelineLayoutState().get(), use.first);
3067 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07003068 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
3069 std::set<uint32_t> descriptor_types =
sfricke-samsungef15e482022-01-26 11:32:49 -08003070 TypeToDescriptorTypeSet(module_state, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07003071
3072 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003073 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003074 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003075 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07003076 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003077 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003078 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
3079 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06003080 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
3081 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003082 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003083 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
3084 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003085 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07003086 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003087 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003088 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003089 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07003090 }
3091 }
3092
3093 // Validate use of input attachments against subpass structure
3094 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09003095 auto input_attachment_uses = module_state.CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07003096
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003097 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06003098 if (rp_state && !rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003099 auto rpci = rp_state->createInfo.ptr();
3100 auto subpass = pipeline->Subpass();
amhagana448ea52021-11-02 14:09:14 -04003101 for (auto use : input_attachment_uses) {
3102 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
3103 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
3104 ? input_attachments[use.first].attachment
3105 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07003106
amhagana448ea52021-11-02 14:09:14 -04003107 if (index == VK_ATTACHMENT_UNUSED) {
3108 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
3109 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsungef15e482022-01-26 11:32:49 -08003110 } else if (!(GetFormatType(rpci->pAttachments[index].format) &
sjfricke4f600c82022-06-09 14:21:32 +09003111 module_state.GetFundamentalType(use.second.type_id))) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003112 skip |= LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
3113 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
3114 string_VkFormat(rpci->pAttachments[index].format),
sjfricke4f600c82022-06-09 14:21:32 +09003115 module_state.DescribeType(use.second.type_id).c_str());
amhagana448ea52021-11-02 14:09:14 -04003116 }
Chris Forbes47567b72017-06-09 12:09:45 -07003117 }
3118 }
3119 }
Lockeaa8fdc02019-04-02 11:59:20 -06003120 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003121 skip |= ValidateComputeWorkGroupSizes(module_state, entrypoint, stage_state, local_size_x, local_size_y, local_size_z);
sjfrickede734312022-07-14 19:22:43 +09003122 skip |= ValidateComputeSharedMemory(module_state, total_shared_size);
Lockeaa8fdc02019-04-02 11:59:20 -06003123 }
ziga-lunarg73163742021-08-25 13:15:29 +02003124
Chris Forbes47567b72017-06-09 12:09:45 -07003125 return skip;
3126}
3127
sjfricke4f600c82022-06-09 14:21:32 +09003128bool CoreChecks::ValidateInterfaceBetweenStages(const SHADER_MODULE_STATE &producer, spirv_inst_iter producer_entrypoint,
3129 shader_stage_attributes const *producer_stage, const SHADER_MODULE_STATE &consumer,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003130 spirv_inst_iter consumer_entrypoint,
3131 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07003132 bool skip = false;
3133
3134 auto outputs =
sjfricke4f600c82022-06-09 14:21:32 +09003135 producer.CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
3136 auto inputs = consumer.CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07003137
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003138 auto output_it = outputs.begin();
3139 auto input_it = inputs.begin();
Chris Forbes47567b72017-06-09 12:09:45 -07003140
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003141 uint32_t output_component = 0;
3142 uint32_t input_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003143
Chris Forbes47567b72017-06-09 12:09:45 -07003144 // Maps sorted by key (location); walk them together to find mismatches
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003145 while ((outputs.size() > 0 && output_it != outputs.end()) || (inputs.size() && input_it != inputs.end())) {
3146 bool output_at_end = outputs.size() == 0 || output_it == outputs.end();
3147 bool input_at_end = inputs.size() == 0 || input_it == inputs.end();
3148 auto output_first = output_at_end ? std::make_pair(0u, 0u) : output_it->first;
3149 auto input_first = input_at_end ? std::make_pair(0u, 0u) : input_it->first;
Chris Forbes47567b72017-06-09 12:09:45 -07003150
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003151 output_first.second += output_component;
3152 input_first.second += input_component;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003153
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003154 const auto output_length =
3155 output_at_end ? 0 : producer.GetNumComponentsInBaseType(producer.get_def(output_it->second.type_id));
3156 const auto input_length =
3157 input_at_end ? 0 : consumer.GetNumComponentsInBaseType(consumer.get_def(input_it->second.type_id));
3158 assert(output_at_end || output_component < output_length);
3159 assert(input_at_end || input_component < input_length);
ziga-lunarg8346fe82021-08-22 17:30:50 +02003160
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003161 if (input_at_end || ((!output_at_end) && (output_first < input_first))) {
Stefan Dobrica43c84ca2022-05-30 16:22:36 +02003162 if (!enabled_features.core13.maintenance4) {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003163 const std::string msg = std::string{producer_stage->name} + " writes to output location " +
3164 std::to_string(output_first.first) + "." + std::to_string(output_first.second) +
3165 " which is not consumed by " + consumer_stage->name +
Nathaniel Cesario09fbe8a2022-08-03 16:24:25 -06003166 ". "
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003167 "Enable VK_KHR_maintenance4 device extension to allow relaxed interface matching between "
3168 "input and output vectors.";
Nathaniel Cesario09fbe8a2022-08-03 16:24:25 -06003169 // It is not an error if a stage does not consume all outputs from the previous stage
3170 skip |= LogPerformanceWarning(producer.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed, "%s", msg.c_str());
Stefan Dobrica43c84ca2022-05-30 16:22:36 +02003171 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003172 if ((input_first.first > output_first.first) || input_at_end || (output_component + 1 == output_length)) {
3173 output_it++;
3174 output_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003175 } else {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003176 output_component++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003177 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003178 } else if (output_at_end || output_first > input_first) {
sjfricke4f600c82022-06-09 14:21:32 +09003179 skip |= LogError(consumer.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02003180 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003181 input_first.first, input_first.second, producer_stage->name);
3182 if ((output_first.first > input_first.first) || output_at_end || (input_component + 1 == input_length)) {
3183 input_it++;
3184 input_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003185 } else {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003186 input_component++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003187 }
Chris Forbes47567b72017-06-09 12:09:45 -07003188 } else {
3189 // subtleties of arrayed interfaces:
3190 // - if is_patch, then the member is not arrayed, even though the interface may be.
3191 // - if is_block_member, then the extra array level of an arrayed interface is not
3192 // expressed in the member type -- it's expressed in the block type.
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003193 if (!TypesMatch(producer, consumer, output_it->second.type_id, input_it->second.type_id)) {
sjfricke4f600c82022-06-09 14:21:32 +09003194 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarge640e802022-04-04 21:36:53 +02003195 "Type mismatch on location %" PRIu32 ".%" PRIu32 ", between %s and %s: '%s' vs '%s'",
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003196 output_first.first, output_first.second, producer_stage->name, consumer_stage->name,
3197 producer.DescribeType(output_it->second.type_id).c_str(),
3198 consumer.DescribeType(input_it->second.type_id).c_str());
3199 output_it++;
3200 input_it++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003201 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07003202 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003203 if (output_it->second.is_patch != input_it->second.is_patch) {
sjfricke4f600c82022-06-09 14:21:32 +09003204 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
3205 "Decoration mismatch on location %" PRIu32 ".%" PRIu32
3206 ": is per-%s in %s stage but per-%s in %s stage",
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003207 output_first.first, output_first.second, output_it->second.is_patch ? "patch" : "vertex",
3208 producer_stage->name, input_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07003209 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003210 uint32_t output_remaining = output_length - output_component;
3211 uint32_t input_remaining = input_length - input_component;
3212 if (output_remaining == input_remaining) { // Sizes match so we can advance both output_it and input_it
3213 output_it++;
3214 input_it++;
3215 output_component = 0;
3216 input_component = 0;
3217 } else if (output_remaining > input_remaining) { // a has more components remaining
3218 output_component += input_remaining;
3219 input_component = 0;
3220 input_it++;
3221 } else if (input_remaining > output_remaining) { // b has more components remaining
3222 input_component += output_remaining;
3223 output_component = 0;
3224 output_it++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003225 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003226 if (output_component == 4) {
3227 output_component = 0;
3228 output_it++;
ziga-lunargb9fa0eb2022-04-01 23:31:06 +02003229 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003230 if (input_component == 4) {
3231 input_component = 0;
3232 input_it++;
ziga-lunargb9fa0eb2022-04-01 23:31:06 +02003233 }
Chris Forbes47567b72017-06-09 12:09:45 -07003234 }
3235 }
3236
Ari Suonpaa696b3432019-03-11 14:02:57 +02003237 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09003238 auto builtins_producer = producer.CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
3239 auto builtins_consumer = consumer.CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02003240
3241 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
3242 if (builtins_producer.size() != builtins_consumer.size()) {
sjfricke4f600c82022-06-09 14:21:32 +09003243 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003244 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003245 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
3246 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02003247 } else {
3248 auto it_producer = builtins_producer.begin();
3249 auto it_consumer = builtins_consumer.begin();
3250 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
3251 if (*it_producer != *it_consumer) {
sjfricke4f600c82022-06-09 14:21:32 +09003252 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003253 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
3254 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02003255 break;
3256 }
3257 it_producer++;
3258 it_consumer++;
3259 }
3260 }
3261 }
3262 }
3263
Chris Forbes47567b72017-06-09 12:09:45 -07003264 return skip;
3265}
3266
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003267static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE &pipeline) {
3268 uint32_t stage_mask = pipeline.active_shaders;
3269 if (pipeline.topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003270 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05003271 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
3272 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
3273 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003274 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
3275 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
3276 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
3277 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
3278 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003279 }
3280 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003281 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003282}
3283
Chris Forbes47567b72017-06-09 12:09:45 -07003284// Validate that the shaders used by the given pipeline and store the active_slots
3285// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06003286bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Chris Forbes47567b72017-06-09 12:09:45 -07003287 bool skip = false;
3288
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003289 if (pipeline->IsGraphicsLibrary()) {
3290 // Only validate stages in an executable pipeline, not a graphics library
3291 // TODO This currently makes executing executable pipeline more expensive than they need to be since we could be validating
3292 // more per library.
3293 return skip;
3294 }
3295
3296 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(*pipeline);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003297
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003298 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
3299 for (auto &stage : pipeline->stage_state) {
3300 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
3301 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
3302 vertex_stage = &stage;
3303 }
3304 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
3305 fragment_stage = &stage;
3306 }
Chris Forbes47567b72017-06-09 12:09:45 -07003307 }
3308
3309 // if the shader stages are no good individually, cross-stage validation is pointless.
3310 if (skip) return true;
3311
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003312 auto vi_state = pipeline->InputState();
Chris Forbes47567b72017-06-09 12:09:45 -07003313
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003314 if (vi_state) {
3315 skip |= ValidateViConsistency(vi_state);
Chris Forbes47567b72017-06-09 12:09:45 -07003316 }
3317
sfricke-samsungef15e482022-01-26 11:32:49 -08003318 if (vertex_stage && vertex_stage->module_state->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
sjfricke4f600c82022-06-09 14:21:32 +09003319 skip |= ValidateViAgainstVsInputs(vi_state, *vertex_stage->module_state.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07003320 }
3321
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003322 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
3323 const auto &producer = pipeline->stage_state[i - 1];
3324 const auto &consumer = pipeline->stage_state[i];
sfricke-samsungef15e482022-01-26 11:32:49 -08003325 assert(producer.module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003326 if (&producer == fragment_stage) {
3327 break;
3328 }
sfricke-samsungef15e482022-01-26 11:32:49 -08003329 if (consumer.module_state) {
3330 if (consumer.module_state->has_valid_spirv && producer.module_state->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003331 auto producer_id = GetShaderStageId(producer.stage_flag);
3332 auto consumer_id = GetShaderStageId(consumer.stage_flag);
sjfricke4f600c82022-06-09 14:21:32 +09003333 skip |= ValidateInterfaceBetweenStages(*producer.module_state.get(), producer.entrypoint,
3334 &shader_stage_attribs[producer_id], *consumer.module_state.get(),
sfricke-samsungef15e482022-01-26 11:32:49 -08003335 consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08003336 }
Chris Forbes47567b72017-06-09 12:09:45 -07003337 }
3338 }
3339
sfricke-samsungef15e482022-01-26 11:32:49 -08003340 if (fragment_stage && fragment_stage->module_state->has_valid_spirv) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003341 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06003342 if (rp_state && rp_state->UsesDynamicRendering()) {
sjfricke4f600c82022-06-09 14:21:32 +09003343 skip |= ValidateFsOutputsAgainstDynamicRenderingRenderPass(*fragment_stage->module_state.get(),
sfricke-samsungef15e482022-01-26 11:32:49 -08003344 fragment_stage->entrypoint, pipeline);
Aaron Hagan1209c782021-11-22 19:37:14 -05003345 } else {
sjfricke4f600c82022-06-09 14:21:32 +09003346 skip |= ValidateFsOutputsAgainstRenderPass(*fragment_stage->module_state.get(), fragment_stage->entrypoint, pipeline,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003347 pipeline->Subpass());
Aaron Hagan1209c782021-11-22 19:37:14 -05003348 }
Chris Forbes47567b72017-06-09 12:09:45 -07003349 }
3350
3351 return skip;
3352}
3353
Tony-LunarGb2ded512021-02-02 16:03:30 -07003354bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
3355 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07003356 bool skip = false;
3357
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003358 for (auto &stage : pipeline->stage_state) {
3359 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
3360 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07003361 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
3362 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben3dfeacf2021-12-02 08:46:39 -07003363 if (stage.wrote_primitive_shading_rate) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00003364 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003365 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00003366 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
3367 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
3368 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003369 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00003370 }
3371 }
3372 }
3373 }
3374
3375 return skip;
3376}
3377
sfricke-samsunge72a85e2020-02-29 21:48:37 -08003378bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003379 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07003380}
Chris Forbes4ae55b32017-06-09 14:42:56 -07003381
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003382uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE &pipeline, VkShaderStageFlagBits stageBit) const {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003383 uint32_t total = 0;
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003384 const auto stages = pipeline.GetShaderStages();
3385 for (const auto &stage : stages) {
3386 if (stage.stage == stageBit) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003387 total++;
3388 }
3389 }
3390
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003391 const auto rt_lib_info = pipeline.GetRayTracingLibraryCreateInfo();
3392 if (rt_lib_info) {
3393 for (uint32_t i = 0; i < rt_lib_info->libraryCount; ++i) {
3394 auto library_pipeline = Get<PIPELINE_STATE>(rt_lib_info->pLibraries[i]);
3395 total += CalcShaderStageCount(*library_pipeline, stageBit);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003396 }
3397 }
3398
3399 return total;
3400}
3401
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003402bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE &pipeline, uint32_t group, uint32_t stage) const {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003403 if (group == VK_SHADER_UNUSED_NV) {
3404 return true;
3405 }
3406
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003407 const auto stages = pipeline.GetShaderStages();
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003408
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003409 const auto num_stages = static_cast<uint32_t>(stages.size());
3410 if (group < num_stages) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003411 return (stages[group].stage & stage) != 0;
3412 }
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003413 group -= num_stages;
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003414
3415 // Search libraries
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003416 const auto rt_lib_info = pipeline.GetRayTracingLibraryCreateInfo();
3417 if (rt_lib_info) {
3418 for (uint32_t i = 0; i < rt_lib_info->libraryCount; ++i) {
3419 auto library_pipeline = Get<PIPELINE_STATE>(rt_lib_info->pLibraries[i]);
3420 const auto lib_stages = library_pipeline->GetShaderStages();
3421 const uint32_t stage_count = static_cast<uint32_t>(lib_stages.size());
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003422 if (group < stage_count) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003423 return (stages[group].stage & stage) != 0;
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003424 }
3425 group -= stage_count;
3426 }
3427 }
3428
3429 // group index too large
3430 return false;
3431}
3432
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003433bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, const safe_VkRayTracingPipelineCreateInfoCommon &create_info,
3434 VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06003435 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04003436
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003437 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003438 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
3439 skip |=
3440 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
3441 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
3442 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
3443 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003444 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003445 if (create_info.pLibraryInfo) {
3446 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003447 const auto library_pipelinestate = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003448 const auto &library_create_info = library_pipelinestate->GetCreateInfo<VkRayTracingPipelineCreateInfoKHR>();
Jeremy Gebben11af9792021-08-20 10:20:09 -06003449 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003450 skip |= LogError(
3451 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
3452 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
3453 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003454 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07003455 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003456 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
3457 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
3458 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
3459 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003460 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
3461 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
3462 "member must have been created with values of the maxPipelineRayPayloadSize and "
3463 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
3464 }
3465 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06003466 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003467 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
3468 "vkCreateRayTracingPipelinesKHR: If flags includes "
3469 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
3470 "the pLibraries member of libraries must have been created with the "
3471 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
3472 }
sourav parmar83c31b12020-05-06 12:30:54 -07003473 }
3474 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003475 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003476 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003477 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
3478 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
3479 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003480 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003481 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003482 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003483 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04003484
Jeremy Gebben11af9792021-08-20 10:20:09 -06003485 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003486 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003487 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003488
Jeremy Gebben11af9792021-08-20 10:20:09 -06003489 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003490 const uint32_t raygen_stages_count = CalcShaderStageCount(*pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003491 if (raygen_stages_count == 0) {
3492 skip |= LogError(
3493 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07003494 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003495 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
3496 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003497 }
ziga-lunarg22f96832022-05-08 22:20:15 +02003498 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR) != 0 &&
3499 (flags & VK_PIPELINE_CREATE_RAY_TRACING_SKIP_AABBS_BIT_KHR) != 0) {
3500 skip |= LogError(
3501 device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-06546",
3502 "vkCreateRayTracingPipelinesKHR: flags (%s) contains both VK_PIPELINE_CREATE_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR and "
3503 "VK_PIPELINE_CREATE_RAY_TRACING_SKIP_AABBS_BIT_KHR bits.",
3504 string_VkPipelineCreateFlags(flags).c_str());
3505 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003506
Jeremy Gebben11af9792021-08-20 10:20:09 -06003507 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003508 const auto &group = groups[group_index];
3509
3510 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003511 if (!GroupHasValidIndex(
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003512 *pipeline, group.generalShader,
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003513 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 -05003514 skip |= LogError(device,
3515 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
3516 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
3517 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003518 }
3519 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3520 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003521 skip |= LogError(device,
3522 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
3523 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
3524 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003525 }
3526 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003527 if (!GroupHasValidIndex(*pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003528 skip |= LogError(device,
3529 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
3530 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
3531 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003532 }
3533 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3534 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003535 skip |= LogError(device,
3536 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
3537 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
3538 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003539 }
3540 }
3541
3542 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3543 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
sjfricke62366d32022-08-01 21:04:10 +09003544 if (!GroupHasValidIndex(*pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_KHR)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003545 skip |= LogError(device,
3546 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
3547 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
3548 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003549 }
sjfricke62366d32022-08-01 21:04:10 +09003550 if (!GroupHasValidIndex(*pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003551 skip |= LogError(device,
3552 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
3553 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
3554 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003555 }
3556 }
John Zulaufe4474e72019-07-01 17:28:27 -06003557 }
3558 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003559}
3560
Dave Houltona9df0ce2018-02-07 10:51:23 -07003561uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003562
Dave Houltona9df0ce2018-02-07 10:51:23 -07003563static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003564 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06003565 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003566 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003567 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003568 return nullptr;
3569}
3570
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003571bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003572 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003573 bool skip = false;
3574 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003575
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06003576 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003577 return false;
3578 }
3579
sfricke-samsung45996a42021-09-16 13:45:27 -07003580 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003581
3582 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003583 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
3584 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3585 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003586 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003587 auto cache = GetValidationCacheInfo(pCreateInfo);
3588 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07003589 // If app isn't using a shader validation cache, use the default one from CoreChecks
3590 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003591 if (cache) {
3592 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003593 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003594 }
3595
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003596 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3597 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07003598 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06003599 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003600 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003601 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003602 spvtools::ValidatorOptions options;
3603 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06003604 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003605 if (spv_valid != SPV_SUCCESS) {
3606 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003607 if (spv_valid == SPV_WARNING) {
3608 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3609 diag && diag->error ? diag->error : "(no error text)");
3610 } else {
3611 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3612 diag && diag->error ? diag->error : "(no error text)");
3613 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003614 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003615 } else {
3616 if (cache) {
3617 cache->Insert(hash);
3618 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003619 }
3620
3621 spvDiagnosticDestroy(diag);
3622 spvContextDestroy(ctx);
3623 }
3624
Chris Forbes4ae55b32017-06-09 14:42:56 -07003625 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003626}
3627
Tony-LunarG1672d002022-08-03 14:35:34 -06003628bool CoreChecks::PreCallValidateGetShaderModuleIdentifierEXT(VkDevice device, VkShaderModule shaderModule,
3629 VkShaderModuleIdentifierEXT *pIdentifier) const {
3630 bool skip = false;
3631 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
3632 skip |= LogError(device, "VUID-vkGetShaderModuleIdentifierEXT-shaderModuleIdentifier-06884",
3633 "vkGetShaderModuleIdentifierEXT() was called when the shaderModuleIdentifier feature was not enabled");
3634 }
3635 return skip;
3636}
3637
3638bool CoreChecks::PreCallValidateGetShaderModuleCreateInfoIdentifierEXT(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
3639 VkShaderModuleIdentifierEXT *pIdentifier) const {
3640 bool skip = false;
3641 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
3642 skip |= LogError(
3643 device, "VUID-vkGetShaderModuleCreateInfoIdentifierEXT-shaderModuleIdentifier-06885",
3644 "vkGetShaderModuleCreateInfoIdentifierEXT() was called when the shaderModuleIdentifier feature was not enabled");
3645 }
3646 return skip;
3647}
3648
sjfricke4f600c82022-06-09 14:21:32 +09003649bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &entrypoint,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003650 const PipelineStageState &stage_state, uint32_t local_size_x, uint32_t local_size_y,
3651 uint32_t local_size_z) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003652 bool skip = false;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003653 // If spec constants were used then the local size are already found if possible
3654 if (local_size_x == 0) {
sjfricke4f600c82022-06-09 14:21:32 +09003655 if (!module_state.FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003656 return skip; // no local size found
Lockeaa8fdc02019-04-02 11:59:20 -06003657 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003658 }
Lockeaa8fdc02019-04-02 11:59:20 -06003659
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003660 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
sjfricke4f600c82022-06-09 14:21:32 +09003661 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003662 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003663 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003664 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
3665 }
3666 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
sjfricke4f600c82022-06-09 14:21:32 +09003667 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003668 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003669 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003670 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
3671 }
3672 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
sjfricke4f600c82022-06-09 14:21:32 +09003673 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003674 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003675 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003676 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
3677 }
3678
3679 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3680 uint64_t invocations = local_size_x * local_size_y;
3681 // Prevent overflow.
3682 bool fail = false;
3683 if (invocations > UINT32_MAX || invocations > limit) {
3684 fail = true;
3685 }
3686 if (!fail) {
3687 invocations *= local_size_z;
Lockeaa8fdc02019-04-02 11:59:20 -06003688 if (invocations > UINT32_MAX || invocations > limit) {
3689 fail = true;
3690 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003691 }
3692 if (fail) {
sjfricke4f600c82022-06-09 14:21:32 +09003693 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003694 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3695 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003696 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x, local_size_y,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003697 local_size_z, limit);
3698 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003699
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003700 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3701 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
ziga-lunargd46c7af2022-04-16 14:05:38 +02003702 const auto *required_subgroup_size_features =
3703 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
ziga-lunarg561d7222022-05-08 20:07:02 +02003704 if (required_subgroup_size_features) {
sjfrickef05418b2022-08-01 18:57:20 +09003705 const uint32_t requiredSubgroupSize = required_subgroup_size_features->requiredSubgroupSize;
ziga-lunarg561d7222022-05-08 20:07:02 +02003706 skip |= RequireFeature(enabled_features.core13.subgroupSizeControl, "subgroupSizeControl",
3707 "VUID-VkPipelineShaderStageCreateInfo-pNext-02755");
3708 if ((phys_dev_ext_props.subgroup_size_control_props.requiredSubgroupSizeStages & stage_state.stage_flag) == 0) {
3709 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003710 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02755",
ziga-lunarg561d7222022-05-08 20:07:02 +02003711 "Stage %s is not in VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%s).",
3712 string_VkShaderStageFlagBits(stage_state.stage_flag),
3713 string_VkShaderStageFlags(phys_dev_ext_props.subgroup_size_control_props.requiredSubgroupSizeStages).c_str());
3714 }
sjfrickef05418b2022-08-01 18:57:20 +09003715 if ((invocations > requiredSubgroupSize * phys_dev_ext_props.subgroup_size_control_props.maxComputeWorkgroupSubgroups)) {
ziga-lunarg561d7222022-05-08 20:07:02 +02003716 skip |=
sjfricke4f600c82022-06-09 14:21:32 +09003717 LogError(module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02756",
ziga-lunargd46c7af2022-04-16 14:05:38 +02003718 "Local workgroup size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3719 ") is greater than VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT::requiredSubgroupSize (%" PRIu32
3720 ") * maxComputeWorkgroupSubgroups (%" PRIu32 ").",
sjfrickef05418b2022-08-01 18:57:20 +09003721 local_size_x, local_size_y, local_size_z, requiredSubgroupSize,
ziga-lunargd46c7af2022-04-16 14:05:38 +02003722 phys_dev_ext_props.subgroup_size_control_props.maxComputeWorkgroupSubgroups);
ziga-lunarg561d7222022-05-08 20:07:02 +02003723 }
3724 if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT) > 0) {
sjfrickef05418b2022-08-01 18:57:20 +09003725 if (SafeModulo(local_size_x, requiredSubgroupSize) != 0) {
ziga-lunarg561d7222022-05-08 20:07:02 +02003726 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003727 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02757",
ziga-lunarg561d7222022-05-08 20:07:02 +02003728 "Local workgroup size x (%" PRIu32
3729 ") is not a multiple of VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT::requiredSubgroupSize (%" PRIu32
3730 ").",
sjfrickef05418b2022-08-01 18:57:20 +09003731 local_size_x, requiredSubgroupSize);
ziga-lunarg561d7222022-05-08 20:07:02 +02003732 }
ziga-lunargd46c7af2022-04-16 14:05:38 +02003733 }
sjfrickef05418b2022-08-01 18:57:20 +09003734 if (!IsPowerOfTwo(requiredSubgroupSize)) {
3735 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003736 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02760",
sjfrickef05418b2022-08-01 18:57:20 +09003737 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3738 ") is not a power of 2.",
3739 requiredSubgroupSize);
3740 }
3741 if (requiredSubgroupSize < phys_dev_ext_props.subgroup_size_control_props.minSubgroupSize) {
3742 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003743 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02761",
sjfrickef05418b2022-08-01 18:57:20 +09003744 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3745 ") is less than minSubgroupSize (%" PRIu32 ").",
3746 requiredSubgroupSize, phys_dev_ext_props.subgroup_size_control_props.minSubgroupSize);
3747 }
3748 if (requiredSubgroupSize > phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) {
3749 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003750 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02762",
sjfrickef05418b2022-08-01 18:57:20 +09003751 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3752 ") is greater than maxSubgroupSize (%" PRIu32 ").",
3753 requiredSubgroupSize, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3754 }
ziga-lunargd46c7af2022-04-16 14:05:38 +02003755 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003756 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3757 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3758 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003759 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003760 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3761 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3762 "dimension (%" PRIu32
3763 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003764 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003765 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3766 }
3767 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3768 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003769 if (!required_subgroup_size_features) {
3770 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02003771 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003772 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003773 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3774 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3775 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3776 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003777 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003778 phys_dev_props_core11.subgroupSize);
ziga-lunarg11fecb92021-09-20 16:48:06 +02003779 }
3780 }
Lockeaa8fdc02019-04-02 11:59:20 -06003781 }
3782 return skip;
3783}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003784
3785spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
Tony-LunarGe67fcc22022-01-03 16:40:53 -07003786 if (api_version >= VK_API_VERSION_1_3) {
3787 return SPV_ENV_VULKAN_1_3;
3788 } else if (api_version >= VK_API_VERSION_1_2) {
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003789 return SPV_ENV_VULKAN_1_2;
3790 } else if (api_version >= VK_API_VERSION_1_1) {
3791 if (spirv_1_4) {
3792 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3793 } else {
3794 return SPV_ENV_VULKAN_1_1;
3795 }
3796 }
3797 return SPV_ENV_VULKAN_1_0;
3798}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003799
sfricke-samsungecc112a2021-09-03 05:32:17 -07003800// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003801void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003802 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003803 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3804 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003805 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003806 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003807 options.SetRelaxBlockLayout(true);
3808 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003809
3810 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3811 // Vulkan version used, the feature bit is needed (also described in the spec).
3812
3813 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3814 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003815 options.SetUniformBufferStandardLayout(true);
3816 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003817 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3818 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003819 options.SetScalarBlockLayout(true);
3820 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003821 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3822 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003823 options.SetWorkgroupScalarBlockLayout(true);
3824 }
Tony-LunarG273f32f2021-09-28 08:56:30 -06003825 if (enabled_features.core13.maintenance4) {
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003826 // --allow-localsizeid
3827 options.SetAllowLocalSizeId(true);
3828 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003829}