blob: e7242d64d66184c37169d708987b32e92af76a4a [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
sfricke-samsungef15e482022-01-26 11:32:49 -080051static const spirv_inst_iter GetBaseTypeIter(SHADER_MODULE_STATE const *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
ziga-lunarg8346fe82021-08-22 17:30:50 +020057static bool BaseTypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, const spirv_inst_iter &a_base_insn,
58 const spirv_inst_iter &b_base_insn) {
59 const uint32_t a_opcode = a_base_insn.opcode();
60 const uint32_t b_opcode = b_base_insn.opcode();
61 if (a_opcode == b_opcode) {
62 if (a_opcode == spv::OpTypeInt) {
63 // Match width and signedness
64 return a_base_insn.word(2) == b_base_insn.word(2) && a_base_insn.word(3) == b_base_insn.word(3);
65 } else if (a_opcode == spv::OpTypeFloat) {
66 // Match width
67 return a_base_insn.word(2) == b_base_insn.word(2);
68 } else if (a_opcode == spv::OpTypeStruct) {
69 // Match on all element types
70 if (a_base_insn.len() != b_base_insn.len()) {
71 return false; // Structs cannot match if member counts differ
72 }
73
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020074 for (uint32_t i = 2; i < a_base_insn.len(); i++) {
75 const auto &c_base_insn = GetBaseTypeIter(a, a_base_insn.word(i));
76 const auto &d_base_insn = GetBaseTypeIter(b, b_base_insn.word(i));
77 if (!BaseTypesMatch(a, b, c_base_insn, d_base_insn)) {
ziga-lunarg8346fe82021-08-22 17:30:50 +020078 return false;
79 }
80 }
81
82 return true;
83 }
84 }
85 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070086}
87
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020088static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, uint32_t a_type, uint32_t b_type) {
89 const auto &a_base_insn = GetBaseTypeIter(a, a_type);
90 const auto &b_base_insn = GetBaseTypeIter(b, b_type);
Chris Forbes47567b72017-06-09 12:09:45 -070091
ziga-lunarg8346fe82021-08-22 17:30:50 +020092 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070093}
94
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
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700124bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700125 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
126 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700127 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700128 bool skip = false;
129
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
sfricke-samsungef15e482022-01-26 11:32:49 -0800145bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *module_state,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700146 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700147 bool skip = false;
148
sfricke-samsungef15e482022-01-26 11:32:49 -0800149 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) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800176 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) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800179 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);
sfricke-samsungef15e482022-01-26 11:32:49 -0800183 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)) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800187 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`",
sfricke-samsungef15e482022-01-26 11:32:49 -0800189 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
sfricke-samsungef15e482022-01-26 11:32:49 -0800199bool CoreChecks::ValidateFsOutputsAgainstDynamicRenderingRenderPass(SHADER_MODULE_STATE const *module_state,
200 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)
sfricke-samsungef15e482022-01-26 11:32:49 -0800210 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
216 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
217 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
218
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
222 if (!output && pipeline->attachments[location].colorWriteMask != 0) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800223 skip |= LogWarning(
224 module_state->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
225 "Attachment %" PRIu32 " not written by fragment shader; undefined values will be written to attachment", location);
Aaron Haganaca50442021-12-07 22:26:29 -0500226 } else if (output &&
227 (location < pipeline->rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount)) {
Aaron Hagan1209c782021-11-22 19:37:14 -0500228 auto format = pipeline->rp_state->dynamic_rendering_pipeline_create_info.pColorAttachmentFormats[location];
229 const auto attachment_type = GetFormatType(format);
sfricke-samsungef15e482022-01-26 11:32:49 -0800230 const auto output_type = module_state->GetFundamentalType(output->type_id);
Aaron Hagan1209c782021-11-22 19:37:14 -0500231
232 // Type checking
233 if (!(output_type & attachment_type)) {
234 skip |=
sfricke-samsungef15e482022-01-26 11:32:49 -0800235 LogWarning(module_state->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
236 "Attachment %" PRIu32
237 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
238 location, string_VkFormat(format), module_state->DescribeType(output->type_id).c_str());
Aaron Hagan1209c782021-11-22 19:37:14 -0500239 }
240 }
241 }
242
243 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sfricke-samsungef15e482022-01-26 11:32:49 -0800244 bool location_zero_has_alpha = output_zero && module_state->get_def(output_zero->type_id) != module_state->end() &&
245 module_state->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Aaron Hagan1209c782021-11-22 19:37:14 -0500246 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800247 skip |= LogError(module_state->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
248 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Aaron Hagan1209c782021-11-22 19:37:14 -0500249 }
250
251 return skip;
Aaron Hagan1209c782021-11-22 19:37:14 -0500252}
253
sfricke-samsungef15e482022-01-26 11:32:49 -0800254bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *module_state, spirv_inst_iter entrypoint,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700255 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200256 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700257
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600258 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800259 const VkAttachmentReference2 *reference = nullptr;
260 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600261 const interface_var *output = nullptr;
262 };
263 std::map<uint32_t, Attachment> location_map;
264
amhagana448ea52021-11-02 14:09:14 -0400265 if (pipeline->rp_state && !pipeline->rp_state->use_dynamic_rendering) {
266 const auto rpci = pipeline->rp_state->createInfo.ptr();
267 const auto subpass = rpci->pSubpasses[subpass_index];
268 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
269 auto const &reference = subpass.pColorAttachments[i];
270 location_map[i].reference = &reference;
271 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
272 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
273 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
274 }
Chris Forbes47567b72017-06-09 12:09:45 -0700275 }
276 }
277
Chris Forbes47567b72017-06-09 12:09:45 -0700278 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
279
sfricke-samsungef15e482022-01-26 11:32:49 -0800280 const auto outputs = module_state->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600281 for (const auto &output_it : outputs) {
282 auto const location = output_it.first.first;
283 location_map[location].output = &output_it.second;
284 }
Chris Forbes47567b72017-06-09 12:09:45 -0700285
Jeremy Gebben11af9792021-08-20 10:20:09 -0600286 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
287 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700288
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400289 for (const auto &location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600290 const auto reference = location_it.second.reference;
291 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
292 continue;
293 }
294
Petr Kraus25810d02019-08-27 17:41:15 +0200295 const auto location = location_it.first;
296 const auto attachment = location_it.second.attachment;
297 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +0200298 if (attachment && !output) {
299 if (pipeline->attachments[location].colorWriteMask != 0) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800300 skip |= LogWarning(module_state->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700301 "Attachment %" PRIu32
302 " not written by fragment shader; undefined values will be written to attachment",
303 location);
Petr Kraus25810d02019-08-27 17:41:15 +0200304 }
305 } else if (!attachment && output) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700306 if (!(alpha_to_coverage_enabled && location == 0)) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800307 skip |= LogWarning(module_state->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700308 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200309 }
Petr Kraus25810d02019-08-27 17:41:15 +0200310 } else if (attachment && output) {
311 const auto attachment_type = GetFormatType(attachment->format);
sfricke-samsungef15e482022-01-26 11:32:49 -0800312 const auto output_type = module_state->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700313
314 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +0200315 if (!(output_type & attachment_type)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700316 skip |=
sfricke-samsungef15e482022-01-26 11:32:49 -0800317 LogWarning(module_state->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700318 "Attachment %" PRIu32
319 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sfricke-samsungef15e482022-01-26 11:32:49 -0800320 location, string_VkFormat(attachment->format), module_state->DescribeType(output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700321 }
Petr Kraus25810d02019-08-27 17:41:15 +0200322 } else { // !attachment && !output
323 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700324 }
325 }
326
Petr Kraus25810d02019-08-27 17:41:15 +0200327 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sfricke-samsungef15e482022-01-26 11:32:49 -0800328 bool location_zero_has_alpha = output_zero && module_state->get_def(output_zero->type_id) != module_state->end() &&
329 module_state->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700330 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800331 skip |= LogError(module_state->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700332 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200333 }
334
Chris Forbes47567b72017-06-09 12:09:45 -0700335 return skip;
336}
337
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600338PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
339 const shader_struct_member &push_constant_used_in_shader,
340 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600341 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600342 const auto used_bytes_size = used_bytes->size();
343 if (used_bytes_size == 0) return PC_Byte_Updated;
344
345 const auto push_constant_data_update_size = push_constant_data_update.size();
346 const auto *data = push_constant_data_update.data();
347 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
348 if (used_bytes_size <= push_constant_data_update_size) {
349 return PC_Byte_Updated;
350 }
351 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
352
353 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
354 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
355 return PC_Byte_Updated;
356 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600357 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600358
locke-lunargde3f0fa2020-09-10 11:55:31 -0600359 uint32_t i = 0;
360 for (const auto used : *used_bytes) {
361 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600362 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600363 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600364 return PC_Byte_Not_Set;
365 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600366 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600367 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600368 }
369 }
370 ++i;
371 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600372 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600373}
374
sfricke-samsungef15e482022-01-26 11:32:49 -0800375bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *module_state,
sfricke-samsung7699b912021-04-12 23:01:51 -0700376 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700377 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700378 // Temp workaround to prevent false positive errors
379 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sfricke-samsungef15e482022-01-26 11:32:49 -0800380 if (module_state->HasMultipleEntryPoints()) {
sfricke-samsung5c65b372021-03-25 05:39:57 -0700381 return skip;
382 }
383
Chris Forbes47567b72017-06-09 12:09:45 -0700384 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
sfricke-samsungef15e482022-01-26 11:32:49 -0800385 const auto *entrypoint = module_state->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600386 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
387 return skip;
388 }
389 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700390
locke-lunargde3f0fa2020-09-10 11:55:31 -0600391 bool found_stage = false;
392 for (auto const &range : *push_constant_ranges) {
393 if (range.stageFlags & pStage->stage) {
394 found_stage = true;
395 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600396 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600397 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600398 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600399 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600400 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600401 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600402 const auto ret =
403 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700404
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600405 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600406 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
sfricke-samsungef15e482022-01-26 11:32:49 -0800407 LogObjectList objlist(module_state->vk_shader_module());
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600408 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700409 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 -0600410 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600411 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600412 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700413 }
414 }
415 }
416
locke-lunargde3f0fa2020-09-10 11:55:31 -0600417 if (!found_stage) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800418 LogObjectList objlist(module_state->vk_shader_module());
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600419 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700420 skip |= LogError(objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.",
sfricke-samsungef15e482022-01-26 11:32:49 -0800421 string_VkShaderStageFlags(pStage->stage).c_str(),
422 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600423 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700424 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700425 }
Chris Forbes47567b72017-06-09 12:09:45 -0700426 return skip;
427}
428
sfricke-samsungef15e482022-01-26 11:32:49 -0800429bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *module_state, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700430 bool skip = false;
431
432 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700433 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700434 return skip;
435 }
436
sfricke-samsungcfb44592021-07-25 00:36:28 -0700437 // Find all builtin from just the interface variables
438 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800439 auto insn = module_state->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700440 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef15e482022-01-26 11:32:49 -0800441 const decoration_set decorations = module_state->get_decorations(insn.word(2));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700442
sfricke-samsungcfb44592021-07-25 00:36:28 -0700443 // Currently don't need to search in structs
444 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800445 auto type_pointer = module_state->get_def(insn.word(1));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700446 assert(type_pointer.opcode() == spv::OpTypePointer);
447
sfricke-samsungef15e482022-01-26 11:32:49 -0800448 auto type = module_state->get_def(type_pointer.word(3));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700449 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800450 uint32_t length = static_cast<uint32_t>(module_state->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700451 // Handles both the input and output sampleMask
452 if (length > phys_dev_props.limits.maxSampleMaskWords) {
453 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
454 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
455 "maxSampleMaskWords of %u in %s.",
456 length, phys_dev_props.limits.maxSampleMaskWords,
sfricke-samsungef15e482022-01-26 11:32:49 -0800457 report_data->FormatHandle(module_state->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700458 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700459 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700460 }
461 }
462 }
463
464 return skip;
465}
466
Chris Forbes47567b72017-06-09 12:09:45 -0700467// Validate that data for each specialization entry is fully contained within the buffer.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200468bool CoreChecks::ValidateSpecializations(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700469 bool skip = false;
470
471 VkSpecializationInfo const *spec = info->pSpecializationInfo;
472
473 if (spec) {
474 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600475 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700476 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
477 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200478 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700479 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
480 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600481
482 continue;
483 }
Chris Forbes47567b72017-06-09 12:09:45 -0700484 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700485 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
486 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200487 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700488 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
489 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700490 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200491 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
492 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
493 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
494 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
495 j, spec->pMapEntries[i].constantID);
496 }
497 }
Chris Forbes47567b72017-06-09 12:09:45 -0700498 }
499 }
500
501 return skip;
502}
503
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500504// TODO (jbolz): Can this return a const reference?
sfricke-samsungef15e482022-01-26 11:32:49 -0800505static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module_state, uint32_t type_id,
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800506 uint32_t &descriptor_count, bool is_khr) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800507 auto type = module_state->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800508 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700509 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500510 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700511
512 // 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 -0500513 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
514 if (type.opcode() == spv::OpTypeRuntimeArray) {
515 descriptor_count = 0;
sfricke-samsungef15e482022-01-26 11:32:49 -0800516 type = module_state->get_def(type.word(2));
Jeff Bolzfdf96072018-04-10 14:32:18 -0500517 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800518 descriptor_count *= module_state->GetConstantValueById(type.word(3));
519 type = module_state->get_def(type.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700520 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800521 if (type.word(2) == spv::StorageClassStorageBuffer) {
522 is_storage_buffer = true;
523 }
sfricke-samsungef15e482022-01-26 11:32:49 -0800524 type = module_state->get_def(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700525 }
526 }
527
528 switch (type.opcode()) {
529 case spv::OpTypeStruct: {
sfricke-samsungef15e482022-01-26 11:32:49 -0800530 for (const auto insn : module_state->GetDecorationInstructions()) {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800531 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700532 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800533 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500534 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
535 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
536 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800537 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500538 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
539 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
540 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
541 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800542 }
Chris Forbes47567b72017-06-09 12:09:45 -0700543 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500544 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
545 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
546 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700547 }
548 }
549 }
550
551 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500552 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700553 }
554
555 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500556 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
557 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
558 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700559
Chris Forbes73c00bf2018-06-22 16:28:06 -0700560 case spv::OpTypeSampledImage: {
561 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
562 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
sfricke-samsungef15e482022-01-26 11:32:49 -0800563 auto image_type = module_state->get_def(type.word(2));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700564 auto dim = image_type.word(3);
565 auto sampled = image_type.word(7);
566 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500567 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
568 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700569 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700570 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500571 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
572 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700573
574 case spv::OpTypeImage: {
575 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
576 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
577 auto dim = type.word(3);
578 auto sampled = type.word(7);
579
580 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500581 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
582 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700583 } else if (dim == spv::DimBuffer) {
584 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500585 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
586 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700587 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500588 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
589 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700590 }
591 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500592 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
593 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
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_IMAGE);
597 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700598 }
599 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600600 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700601 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
602 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500603 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700604
605 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
606 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500607 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700608 }
609}
610
Jeff Bolze54ae892018-09-08 12:16:29 -0500611static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700612 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500613 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
614 if (ss.tellp()) ss << ", ";
615 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700616 }
617 return ss.str();
618}
619
sfricke-samsung0065ce02020-12-03 22:46:37 -0800620bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500621 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800622 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 -0500623 return true;
624 }
625 }
626
627 return false;
628}
629
sfricke-samsung0065ce02020-12-03 22:46:37 -0800630bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700631 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800632 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700633 return true;
634 }
635 }
636
637 return false;
638}
639
locke-lunarg63e4daf2020-08-17 17:53:25 -0600640bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
641 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500642 bool skip = false;
643
locke-lunarg63e4daf2020-08-17 17:53:25 -0600644 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800645 switch (stage) {
Chris Forbes349b3132018-03-07 11:38:08 -0800646 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800647 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700648 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800649 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800650 case VK_SHADER_STAGE_VERTEX_BIT:
651 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
652 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
653 case VK_SHADER_STAGE_GEOMETRY_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800654 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700655 "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800656 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800657 default:
658 // No feature requirements for writes and atomics for other stages
659 break;
Chris Forbes349b3132018-03-07 11:38:08 -0800660 }
661 }
662
Chris Forbes47567b72017-06-09 12:09:45 -0700663 return skip;
664}
665
sfricke-samsungef15e482022-01-26 11:32:49 -0800666bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module_state, VkShaderStageFlagBits stage,
sfricke-samsung94167ca2021-02-26 04:14:59 -0800667 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500668 bool skip = false;
669
sfricke-samsung94167ca2021-02-26 04:14:59 -0800670 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
671 if (GroupOperation(insn.opcode()) == true) {
672 // Check the quad operations.
673 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
674 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700675 skip |=
676 RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
677 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800678 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800679 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500680
sfricke-samsung94167ca2021-02-26 04:14:59 -0800681 uint32_t scope_type = spv::ScopeMax;
682 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
683 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
684 scope_type = spv::ScopeSubgroup;
685 } else {
686 // "All <id> used for Scope <id> must be of an OpConstant"
sfricke-samsungef15e482022-01-26 11:32:49 -0800687 auto scope_id = module_state->get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800688 scope_type = scope_id.word(3);
689 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800690
sfricke-samsung94167ca2021-02-26 04:14:59 -0800691 if (scope_type == spv::ScopeSubgroup) {
692 // "Group operations with subgroup scope" must have stage support
693 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
694 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700695 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800696 }
697
698 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800699 auto type = module_state->get_def(insn.word(1));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800700
701 if (type.opcode() == spv::OpTypeVector) {
702 // Get the element type
sfricke-samsungef15e482022-01-26 11:32:49 -0800703 type = module_state->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800704 }
705
sfricke-samsung94167ca2021-02-26 04:14:59 -0800706 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800707 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
708 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500709
sfricke-samsung0065ce02020-12-03 22:46:37 -0800710 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
711 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
712 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
713 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700714 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500715 }
716 }
717 }
Jeff Bolzee743412019-06-20 22:24:32 -0500718 }
719
720 return skip;
721}
722
sfricke-samsungef15e482022-01-26 11:32:49 -0800723bool CoreChecks::ValidateMemoryScope(SHADER_MODULE_STATE const *module_state, const spirv_inst_iter &insn) const {
ziga-lunarg70651522021-10-11 17:23:30 +0200724 bool skip = false;
725
sfricke-samsung3a25ed52022-01-20 02:24:36 -0800726 const auto &entry = OpcodeMemoryScopePosition(insn.opcode());
ziga-lunarg70651522021-10-11 17:23:30 +0200727 if (entry > 0) {
728 const uint32_t scope_id = insn.word(entry);
sfricke-samsunged00aa42022-01-27 19:03:01 -0800729 const auto &scope_def = module_state->GetConstantDef(scope_id);
730 if (scope_def != module_state->end()) {
731 const auto scope_type = GetConstantValue(scope_def);
732 if (enabled_features.core12.vulkanMemoryModel && !enabled_features.core12.vulkanMemoryModelDeviceScope &&
733 scope_type == spv::Scope::ScopeDevice) {
734 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06265",
735 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is enabled and "
736 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModelDeviceScope is disabled, but\n%s\nuses "
737 "Device memory scope.",
738 module_state->DescribeInstruction(insn).c_str());
739 } else if (!enabled_features.core12.vulkanMemoryModel && scope_type == spv::Scope::ScopeQueueFamily) {
740 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06266",
741 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is not enabled, but\n%s\nuses "
742 "QueueFamily memory scope.",
743 module_state->DescribeInstruction(insn).c_str());
ziga-lunarg70651522021-10-11 17:23:30 +0200744 }
745 }
746 }
747
748 return skip;
749}
750
sfricke-samsungef15e482022-01-26 11:32:49 -0800751bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *module_state, VkPipelineShaderStageCreateInfo const *pStage,
752 const std::unordered_map<uint32_t, std::vector<uint32_t>> &id_value_map) const {
ziga-lunarg2818f492021-08-12 14:30:51 +0200753 bool skip = false;
754
sfricke-samsungef15e482022-01-26 11:32:49 -0800755 std::array<uint32_t, 3> work_group_size = module_state->GetWorkgroupSize(pStage, id_value_map);
ziga-lunarg2818f492021-08-12 14:30:51 +0200756
757 for (uint32_t i = 0; i < 3; ++i) {
758 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
759 const char member = 'x' + static_cast<int8_t>(i);
760 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
761 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
762 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
763 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
764 }
765 }
766 return skip;
767}
768
sfricke-samsungef15e482022-01-26 11:32:49 -0800769bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *module_state,
770 VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline,
771 spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200772 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
773 pStage->stage == VK_SHADER_STAGE_ALL) {
774 return false;
775 }
776
777 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700778 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200779
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700780 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200781 struct Variable {
782 uint32_t baseTypePtrID;
783 uint32_t ID;
784 uint32_t storageClass;
785 };
786 std::vector<Variable> variables;
787
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700788 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700789 bool is_iso_lines = false;
790 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500791
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700792 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600793
sfricke-samsungef15e482022-01-26 11:32:49 -0800794 for (auto insn : *module_state) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200795 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500796 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200797 case spv::OpDecorate:
798 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500799 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700800 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200801 break;
802 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200803 default:
804 break;
805 }
806 break;
807 // Find all input and output variables
808 case spv::OpVariable: {
809 Variable var = {};
810 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600811 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
812 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700813 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200814 var.baseTypePtrID = insn.word(1);
815 var.ID = insn.word(2);
816 variables.push_back(var);
817 }
818 break;
819 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500820 case spv::OpExecutionMode:
821 if (insn.word(1) == entrypoint.word(2)) {
822 switch (insn.word(2)) {
823 default:
824 break;
825 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700826 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500827 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700828 case spv::ExecutionModeIsolines:
829 is_iso_lines = true;
830 break;
831 case spv::ExecutionModePointMode:
832 is_point_mode = true;
833 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500834 }
835 }
836 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200837 default:
838 break;
839 }
840 }
841
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500842 bool strip_output_array_level =
843 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
844 bool strip_input_array_level =
845 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
846 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
847
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700848 uint32_t num_comp_in = 0, num_comp_out = 0;
849 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600850
sfricke-samsungef15e482022-01-26 11:32:49 -0800851 auto inputs = module_state->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
852 auto outputs = module_state->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600853
854 // Find max component location used for input variables.
855 for (auto &var : inputs) {
856 int location = var.first.first;
857 int component = var.first.second;
858 interface_var &iv = var.second;
859
860 // Only need to look at the first location, since we use the type's whole size
861 if (iv.offset != 0) {
862 continue;
863 }
864
865 if (iv.is_patch) {
866 continue;
867 }
868
sfricke-samsungef15e482022-01-26 11:32:49 -0800869 int num_components = module_state->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700870 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600871 }
872
873 // Find max component location used for output variables.
874 for (auto &var : outputs) {
875 int location = var.first.first;
876 int component = var.first.second;
877 interface_var &iv = var.second;
878
879 // Only need to look at the first location, since we use the type's whole size
880 if (iv.offset != 0) {
881 continue;
882 }
883
884 if (iv.is_patch) {
885 continue;
886 }
887
sfricke-samsungef15e482022-01-26 11:32:49 -0800888 int num_components = module_state->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700889 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600890 }
891
892 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
893 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700894 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
895 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200896 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500897 // Check if the variable is a patch. Patches can also be members of blocks,
898 // but if they are then the top-level arrayness has already been stripped
899 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700900 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200901
902 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800903 num_comp_in += module_state->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200904 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsungef15e482022-01-26 11:32:49 -0800905 num_comp_out += module_state->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200906 }
907 }
908
909 switch (pStage->stage) {
910 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700911 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700912 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700913 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
914 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
915 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700916 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200917 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700918 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
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: Vertex shader output variable uses location that "
921 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
922 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600923 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200924 break;
925
926 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700927 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700928 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700929 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
930 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
931 "components by %u components",
932 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700933 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200934 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700935 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600936 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700937 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700938 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
939 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
940 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600941 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700942 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700943 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700944 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
945 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
946 "components by %u components",
947 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700948 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200949 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700950 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600951 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700952 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700953 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
954 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
955 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600956 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200957 break;
958
959 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700960 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700961 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700962 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
963 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
964 "components by %u components",
965 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700966 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200967 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700968 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600969 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700970 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700971 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
972 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
973 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600974 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700975 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700976 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700977 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
978 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
979 "components by %u components",
980 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700981 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200982 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700983 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600984 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700985 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700986 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
987 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
988 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600989 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700990 // Portability validation
991 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
992 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700993 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700994 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
995 " is using abstract patch type IsoLines, but this is not supported on this platform");
996 }
997 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700998 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700999 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
1000 " is using abstract patch type PointMode, but this is not supported on this platform");
1001 }
1002 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001003 break;
1004
1005 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001006 if (num_comp_in > limits.maxGeometryInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001007 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001008 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1009 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
1010 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001011 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001012 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001013 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001014 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001015 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
1016 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
1017 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001018 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001019 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001020 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001021 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1022 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
1023 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001024 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001025 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001026 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001027 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001028 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
1029 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
1030 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001031 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001032 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001033 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001034 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1035 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
1036 "components by %u components",
1037 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001038 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001039 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001040 break;
1041
1042 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001043 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001044 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001045 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
1046 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
1047 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001048 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001049 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001050 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001051 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001052 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1053 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1054 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001055 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001056 break;
1057
Jeff Bolz148d94e2018-12-13 21:25:56 -06001058 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1059 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1060 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1061 case VK_SHADER_STAGE_MISS_BIT_NV:
1062 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1063 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1064 case VK_SHADER_STAGE_TASK_BIT_NV:
1065 case VK_SHADER_STAGE_MESH_BIT_NV:
1066 break;
1067
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001068 default:
1069 assert(false); // This should never happen
1070 }
1071 return skip;
1072}
1073
sfricke-samsungef15e482022-01-26 11:32:49 -08001074bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *module_state, const spirv_inst_iter &insn) const {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001075 bool skip = false;
1076
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001077 switch (insn.opcode()) {
1078 // Go through all ImageRead/Write instructions
1079 case spv::OpImageSparseRead:
1080 case spv::OpImageRead: {
1081 // spirv-val validates this is an OpTypeImage
sfricke-samsungef15e482022-01-26 11:32:49 -08001082 const uint32_t image = module_state->GetTypeId(insn.word(3));
1083 const spirv_inst_iter image_def = module_state->get_def(image);
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001084
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001085 const uint32_t dim = image_def.word(3);
1086 const uint32_t image_format = image_def.word(8);
1087 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1088 // StorageImageReadWithoutFormat Capability was declared.
1089 if (dim != spv::DimSubpassData && image_format == spv::ImageFormatUnknown) {
1090 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1091 "shaderStorageImageReadWithoutFormat", kVUID_Features_shaderStorageImageReadWithoutFormat);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001092 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001093 break;
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001094 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001095 case spv::OpImageWrite: {
1096 // spirv-val validates this is an OpTypeImage
sfricke-samsungef15e482022-01-26 11:32:49 -08001097 const uint32_t image = module_state->GetTypeId(insn.word(1));
1098 const spirv_inst_iter image_def = module_state->get_def(image);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001099
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001100 const uint32_t image_format = image_def.word(8);
1101 if (image_format == spv::ImageFormatUnknown) {
1102 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1103 "shaderStorageImageWriteWithoutFormat", kVUID_Features_shaderStorageImageWriteWithoutFormat);
1104 }
1105 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001106 }
1107
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001108 // Go through all variables for images and check decorations
1109 case spv::OpVariable: {
1110 // spirv-val validates this is an OpTypePointer
sfricke-samsungef15e482022-01-26 11:32:49 -08001111 const spirv_inst_iter pointer_def = module_state->get_def(insn.word(1));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001112 if (pointer_def.word(2) != spv::StorageClassUniformConstant) {
1113 break; // Vulkan Spec says storage image must be UniformConstant
1114 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001115 spirv_inst_iter type_def = module_state->get_def(pointer_def.word(3));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001116
1117 // Unpack an optional level of arraying
1118 if (type_def.opcode() == spv::OpTypeArray || type_def.opcode() == spv::OpTypeRuntimeArray) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001119 type_def = module_state->get_def(type_def.word(2));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001120 }
1121
sfricke-samsungef15e482022-01-26 11:32:49 -08001122 if (type_def != module_state->end() && type_def.opcode() == spv::OpTypeImage) {
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001123 // Only check if the Image Dim operand is not SubpassData
1124 const uint32_t dim = type_def.word(3);
1125 // Only check storage images
1126 const uint32_t sampled = type_def.word(7);
1127 const uint32_t image_format = type_def.word(8);
1128 if ((dim == spv::DimSubpassData) || (sampled != 2) || (image_format != spv::ImageFormatUnknown)) {
1129 break;
1130 }
1131
1132 const uint32_t var_id = insn.word(2);
sfricke-samsungef15e482022-01-26 11:32:49 -08001133 decoration_set img_decorations = module_state->get_decorations(var_id);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001134
1135 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1136 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1137 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001138 "shaderStorageImageReadWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith Unknown "
1139 "format and is not decorated with NonReadable",
1140 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 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1145 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1146 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001147 "shaderStorageImageWriteWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith "
1148 "Unknown format and is not decorated with NonWritable",
1149 module_state->DescribeInstruction(module_state->get_def(var_id)).c_str(),
1150 module_state->DescribeInstruction(type_def).c_str());
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001151 }
1152 }
1153 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001154 }
1155 }
1156
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001157 return skip;
1158}
1159
sfricke-samsungdc96f302020-03-18 20:42:10 -07001160bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1161 bool skip = false;
1162 uint32_t total_resources = 0;
1163
1164 // Only currently testing for graphics and compute pipelines
1165 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1166 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1167 return false;
1168 }
1169
1170 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
amhagana448ea52021-11-02 14:09:14 -04001171 if (pipeline->rp_state->use_dynamic_rendering) {
Aaron Hagan92a44f82021-11-19 09:34:56 -05001172 total_resources += pipeline->rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001173 } else {
1174 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
1175 total_resources +=
1176 pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
1177 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001178 }
1179
1180 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1181 // input from CreatePipeline and CreatePipelineLayout level
1182 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1183 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1184 continue;
1185 }
1186
1187 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1188 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1189 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1190 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1191 // Check only descriptor types listed in maxPerStageResources description in spec
1192 switch (binding->descriptorType) {
1193 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1194 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1195 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1196 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1197 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1198 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1199 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1200 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1201 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1202 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1203 total_resources += binding->descriptorCount;
1204 break;
1205 default:
1206 break;
1207 }
1208 }
1209 }
1210 }
1211
1212 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1213 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1214 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001215 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001216 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1217 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1218 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1219 }
1220
1221 return skip;
1222}
1223
Jeff Bolze4356752019-03-07 11:23:46 -06001224// copy the specialization constant value into buf, if it is present
1225void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1226 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1227
1228 if (spec && spec_id < spec->mapEntryCount) {
1229 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1230 }
1231}
1232
1233// Fill in value with the constant or specialization constant value, if available.
1234// Returns true if the value has been accurately filled out.
sfricke-samsungef15e482022-01-26 11:32:49 -08001235static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *module_state,
1236 VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001237 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001238 auto type_id = module_state->get_def(insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001239 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1240 return false;
1241 }
1242 switch (insn.opcode()) {
1243 case spv::OpSpecConstant:
1244 *value = insn.word(3);
1245 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1246 return true;
1247 case spv::OpConstant:
1248 *value = insn.word(3);
1249 return true;
1250 default:
1251 return false;
1252 }
1253}
1254
1255// Map SPIR-V type to VK_COMPONENT_TYPE enum
sfricke-samsungef15e482022-01-26 11:32:49 -08001256VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *module_state) {
Jeff Bolze4356752019-03-07 11:23:46 -06001257 switch (insn.opcode()) {
1258 case spv::OpTypeInt:
1259 switch (insn.word(2)) {
1260 case 8:
1261 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1262 case 16:
1263 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1264 case 32:
1265 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1266 case 64:
1267 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1268 default:
1269 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1270 }
1271 case spv::OpTypeFloat:
1272 switch (insn.word(2)) {
1273 case 16:
1274 return VK_COMPONENT_TYPE_FLOAT16_NV;
1275 case 32:
1276 return VK_COMPONENT_TYPE_FLOAT32_NV;
1277 case 64:
1278 return VK_COMPONENT_TYPE_FLOAT64_NV;
1279 default:
1280 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1281 }
1282 default:
1283 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1284 }
1285}
1286
1287// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1288// in SPIRV-Tools (e.g. due to specialization constant usage).
sfricke-samsungef15e482022-01-26 11:32:49 -08001289bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *module_state, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001290 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001291 bool skip = false;
1292
1293 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001294 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001295 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001296 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001297
1298 struct CoopMatType {
1299 uint32_t scope, rows, cols;
1300 VkComponentTypeNV component_type;
1301 bool all_constant;
1302
1303 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1304
sfricke-samsungef15e482022-01-26 11:32:49 -08001305 void Init(uint32_t id, SHADER_MODULE_STATE const *module_state, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001306 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001307 spirv_inst_iter insn = module_state->get_def(id);
Jeff Bolze4356752019-03-07 11:23:46 -06001308 uint32_t component_type_id = insn.word(2);
1309 uint32_t scope_id = insn.word(3);
1310 uint32_t rows_id = insn.word(4);
1311 uint32_t cols_id = insn.word(5);
sfricke-samsungef15e482022-01-26 11:32:49 -08001312 auto component_type_iter = module_state->get_def(component_type_id);
1313 auto scope_iter = module_state->get_def(scope_id);
1314 auto rows_iter = module_state->get_def(rows_id);
1315 auto cols_iter = module_state->get_def(cols_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001316
1317 all_constant = true;
sfricke-samsungef15e482022-01-26 11:32:49 -08001318 if (!GetIntConstantValue(scope_iter, module_state, pStage, id_to_spec_id, &scope)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001319 all_constant = false;
1320 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001321 if (!GetIntConstantValue(rows_iter, module_state, pStage, id_to_spec_id, &rows)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001322 all_constant = false;
1323 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001324 if (!GetIntConstantValue(cols_iter, module_state, pStage, id_to_spec_id, &cols)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001325 all_constant = false;
1326 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001327 component_type = GetComponentType(component_type_iter, module_state);
Jeff Bolze4356752019-03-07 11:23:46 -06001328 }
1329 };
1330
1331 bool seen_coopmat_capability = false;
1332
sfricke-samsungef15e482022-01-26 11:32:49 -08001333 for (auto insn : *module_state) {
Jeff Bolze4356752019-03-07 11:23:46 -06001334 // Whitelist instructions whose result can be a cooperative matrix type, and
1335 // keep track of their types. It would be nice if SPIRV-Headers generated code
1336 // to identify which instructions have a result type and result id. Lacking that,
1337 // this whitelist is based on the set of instructions that
1338 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1339 switch (insn.opcode()) {
1340 case spv::OpLoad:
1341 case spv::OpCooperativeMatrixLoadNV:
1342 case spv::OpCooperativeMatrixMulAddNV:
1343 case spv::OpSNegate:
1344 case spv::OpFNegate:
1345 case spv::OpIAdd:
1346 case spv::OpFAdd:
1347 case spv::OpISub:
1348 case spv::OpFSub:
1349 case spv::OpFDiv:
1350 case spv::OpSDiv:
1351 case spv::OpUDiv:
1352 case spv::OpMatrixTimesScalar:
1353 case spv::OpConstantComposite:
1354 case spv::OpCompositeConstruct:
1355 case spv::OpConvertFToU:
1356 case spv::OpConvertFToS:
1357 case spv::OpConvertSToF:
1358 case spv::OpConvertUToF:
1359 case spv::OpUConvert:
1360 case spv::OpSConvert:
1361 case spv::OpFConvert:
1362 id_to_type_id[insn.word(2)] = insn.word(1);
1363 break;
1364 default:
1365 break;
1366 }
1367
1368 switch (insn.opcode()) {
1369 case spv::OpDecorate:
1370 if (insn.word(2) == spv::DecorationSpecId) {
1371 id_to_spec_id[insn.word(1)] = insn.word(3);
1372 }
1373 break;
1374 case spv::OpCapability:
1375 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1376 seen_coopmat_capability = true;
1377
1378 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001379 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001380 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001381 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1382 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001383 }
1384 }
1385 break;
1386 case spv::OpMemoryModel:
1387 // If the capability isn't enabled, don't bother with the rest of this function.
1388 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1389 if (!seen_coopmat_capability) {
1390 return skip;
1391 }
1392 break;
1393 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001394 CoopMatType m;
sfricke-samsungef15e482022-01-26 11:32:49 -08001395 m.Init(insn.word(1), module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001396
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001397 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001398 // Validate that the type parameters are all supported for one of the
1399 // operands of a cooperative matrix property.
1400 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001401 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001402 if (cooperative_matrix_properties[i].AType == m.component_type &&
1403 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1404 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001405 valid = true;
1406 break;
1407 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001408 if (cooperative_matrix_properties[i].BType == m.component_type &&
1409 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1410 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001411 valid = true;
1412 break;
1413 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001414 if (cooperative_matrix_properties[i].CType == m.component_type &&
1415 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1416 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001417 valid = true;
1418 break;
1419 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001420 if (cooperative_matrix_properties[i].DType == m.component_type &&
1421 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1422 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001423 valid = true;
1424 break;
1425 }
1426 }
1427 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001428 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001429 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1430 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001431 }
1432 }
1433 break;
1434 }
1435 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001436 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001437 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1438 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1439 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1440 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001441 // Couldn't find type of matrix
1442 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001443 break;
1444 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001445 d.Init(id_to_type_id[insn.word(2)], module_state, pStage, id_to_spec_id);
1446 a.Init(id_to_type_id[insn.word(3)], module_state, pStage, id_to_spec_id);
1447 b.Init(id_to_type_id[insn.word(4)], module_state, pStage, id_to_spec_id);
1448 c.Init(id_to_type_id[insn.word(5)], module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001449
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001450 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001451 // Validate that the type parameters are all supported for the same
1452 // cooperative matrix property.
1453 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001454 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001455 if (cooperative_matrix_properties[i].AType == a.component_type &&
1456 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1457 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001458
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001459 cooperative_matrix_properties[i].BType == b.component_type &&
1460 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1461 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001462
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001463 cooperative_matrix_properties[i].CType == c.component_type &&
1464 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1465 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001466
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001467 cooperative_matrix_properties[i].DType == d.component_type &&
1468 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1469 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001470 valid = true;
1471 break;
1472 }
1473 }
1474 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001475 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001476 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1477 "VkCooperativeMatrixPropertiesNV",
1478 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001479 }
1480 }
1481 break;
1482 }
1483 default:
1484 break;
1485 }
1486 }
1487
1488 return skip;
1489}
1490
sfricke-samsungef15e482022-01-26 11:32:49 -08001491bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *module_state, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001492 const PIPELINE_STATE *pipeline) const {
1493 bool skip = false;
1494
1495 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1496 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1497 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001498 for (auto insn : *module_state) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001499 switch (insn.opcode()) {
1500 case spv::OpCapability:
1501 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1502 auto subpass_flags =
1503 (pipeline->rp_state == nullptr)
1504 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001505 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001506 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1507 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001508 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001509 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1510 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1511 }
1512 }
1513 break;
1514 default:
1515 break;
1516 }
1517 }
1518 }
1519
1520 return skip;
1521}
1522
ziga-lunarg73163742021-08-25 13:15:29 +02001523bool CoreChecks::ValidateShaderSubgroupSizeControl(VkPipelineShaderStageCreateInfo const *pStage) const {
1524 bool skip = false;
1525
1526 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001527 !enabled_features.core13.subgroupSizeControl) {
ziga-lunarg73163742021-08-25 13:15:29 +02001528 skip |= LogError(
1529 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1530 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1531 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1532 }
1533
1534 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001535 !enabled_features.core13.computeFullSubgroups) {
ziga-lunarg73163742021-08-25 13:15:29 +02001536 skip |= LogError(
1537 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1538 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1539 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1540 }
1541
1542 return skip;
1543}
1544
sfricke-samsungef15e482022-01-26 11:32:49 -08001545bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *module_state) const {
sfricke-samsung58b84352021-07-31 21:41:04 -07001546 bool skip = false;
1547
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001548 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001549 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001550
sfricke-samsungf5042b12021-08-05 01:09:40 -07001551 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1552 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1553
1554 const bool valid_storage_buffer_float = (
1555 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1556 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1557 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1558 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1559 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1560 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1561 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1562 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1563 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1564
1565 const bool valid_workgroup_float = (
1566 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1567 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1568 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1569 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1570 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1571 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1572 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1573 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1574 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1575
1576 const bool valid_image_float = (
1577 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1578 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1579 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1580
1581 const bool valid_16_float = (
1582 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1583 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1584 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1585 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1586 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1587 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1588
1589 const bool valid_32_float = (
1590 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1591 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1592 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1593 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1594 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1595 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1596 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1597 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1598 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1599
1600 const bool valid_64_float = (
1601 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1602 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1603 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1604 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1605 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1606 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1607 // clang-format on
1608
sfricke-samsungef15e482022-01-26 11:32:49 -08001609 for (const auto &atomic_inst : module_state->GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001610 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungef15e482022-01-26 11:32:49 -08001611 const spirv_inst_iter atomic_def = module_state->at(atomic_inst.first);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001612 const uint32_t opcode = atomic_def.opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001613
1614 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001615 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001616 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1617 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001618 skip |= LogError(device, "VUID-RuntimeSpirv-None-06278",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001619 "%s: Can't use 64-bit int atomics operations\n%s\nwith %s storage class without "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001620 "shaderBufferInt64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001621 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1622 module_state->DescribeInstruction(atomic_def).c_str(), StorageClassName(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001623 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1624 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001625 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001626 "%s: Can't use 64-bit int atomics operations\n%s\nwith Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001627 "shaderSharedInt64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001628 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1629 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001630 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001631 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001632 "%s: Can't use 64-bit int atomics operations\n%s\nwith Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001633 "shaderImageInt64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001634 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1635 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001636 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001637 } else if (atomic.type == spv::OpTypeFloat) {
1638 // Validate Floats
1639 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1640 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001641 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1642 : "VUID-RuntimeSpirv-None-06280";
1643 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001644 "%s: Can't use float atomics operations\n%s\nwith StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001645 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1646 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1647 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1648 "shaderBufferFloat64AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001649 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1650 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001651 } else if (opcode == spv::OpAtomicFAddEXT) {
1652 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1653 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001654 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001655 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001656 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1657 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001658 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1659 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001660 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001661 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001662 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1663 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001664 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1665 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001666 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001667 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001668 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1669 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001670 }
1671 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1672 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001673 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1674 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1675 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001676 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1677 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001678 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001679 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1680 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1681 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001682 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1683 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001684 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001685 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1686 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1687 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001688 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1689 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001690 }
1691 } else {
1692 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1693 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001694 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1695 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith "
1696 "StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001697 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1698 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001699 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001700 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1701 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith "
1702 "StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001703 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1704 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001705 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001706 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1707 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith "
1708 "StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001709 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1710 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001711 }
1712 }
1713 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1714 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001715 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1716 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsungef15e482022-01-26 11:32:49 -08001717 skip |=
1718 LogError(device, vuid,
1719 "%s: Can't use float atomics operations\n%s\nwith Workgroup storage class without "
1720 "shaderSharedFloat32Atomics or "
1721 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1722 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1723 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1724 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1725 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001726 } else if (opcode == spv::OpAtomicFAddEXT) {
1727 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1728 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001729 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001730 "storage class without shaderSharedFloat16AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001731 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1732 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001733 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1734 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001735 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001736 "storage class without shaderSharedFloat32AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001737 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1738 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001739 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1740 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001741 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001742 "storage class without shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001743 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1744 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001745 }
1746 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1747 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001748 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1749 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1750 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001751 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1752 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001753 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001754 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1755 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1756 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001757 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1758 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001759 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001760 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1761 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1762 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001763 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1764 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001765 }
1766 } else {
1767 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1768 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001769 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1770 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1771 "storage class without shaderSharedFloat16Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001772 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1773 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001774 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001775 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1776 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1777 "storage class without shaderSharedFloat32Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001778 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1779 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001780 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001781 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1782 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1783 "storage class without shaderSharedFloat64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001784 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1785 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001786 }
1787 }
1788 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001789 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1790 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001791 skip |= LogError(
1792 device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001793 "%s: Can't use float atomics operations\n%s\nwith Image storage class without shaderImageFloat32Atomics or "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001794 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001795 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1796 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001797 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001798 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001799 "%s: Can't use 16-bit float atomics operations\n%s\nwithout shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001800 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1801 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001802 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1803 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001804 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001805 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1806 : "VUID-RuntimeSpirv-None-06335";
1807 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001808 "%s: Can't use 32-bit float atomics operations\n%s\nwithout shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001809 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1810 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1811 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1812 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001813 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1814 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001815 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001816 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1817 : "VUID-RuntimeSpirv-None-06336";
1818 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001819 "%s: Can't use 64-bit float atomics operations\n%s\nwithout shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001820 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1821 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001822 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1823 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001824 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001825 }
1826 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001827 return skip;
1828}
1829
sfricke-samsungef15e482022-01-26 11:32:49 -08001830bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *module_state, spirv_inst_iter entrypoint,
1831 VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001832 auto entrypoint_id = entrypoint.word(2);
1833
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001834 // The first denorm execution mode encountered, along with its bit width.
1835 // Used to check if SeparateDenormSettings is respected.
1836 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001837
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001838 // The first rounding mode encountered, along with its bit width.
1839 // Used to check if SeparateRoundingModeSettings is respected.
1840 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001841
1842 bool skip = false;
1843
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001844 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001845 uint32_t invocations = 0;
1846
sfricke-samsungef15e482022-01-26 11:32:49 -08001847 const auto &execution_mode_inst = module_state->GetExecutionModeInstructions();
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001848 auto it = execution_mode_inst.find(entrypoint_id);
1849 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001850 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001851 auto mode = insn.word(2);
1852 switch (mode) {
1853 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1854 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001855 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001856 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001857 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001858 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device\n%s",
1859 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001860 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1861 skip |= LogError(
1862 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001863 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device\n%s",
1864 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001865 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1866 skip |= LogError(
1867 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001868 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device\n%s",
1869 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001870 }
1871 break;
1872 }
1873
1874 case spv::ExecutionModeDenormPreserve: {
1875 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001876 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1877 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001878 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device\n%s",
1879 module_state->DescribeInstruction(insn).c_str());
1880 ;
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001881 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1882 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001883 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device\n%s",
1884 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001885 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1886 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001887 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device\n%s",
1888 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001889 }
1890
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001891 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1892 // Register the first denorm execution mode found
1893 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001894 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001895 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001896 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001897 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001898 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001899 "Shader uses different denorm execution modes for 16 and 64-bit but "
1900 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001901 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
1902 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001903 }
1904 break;
1905
Mike Schuchardt2df08912020-12-15 16:28:09 -08001906 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001907 break;
1908
Mike Schuchardt2df08912020-12-15 16:28:09 -08001909 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001910 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001911 "Shader uses different denorm execution modes for different bit widths but "
1912 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001913 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
1914 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001915 break;
1916
1917 default:
1918 break;
1919 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001920 }
1921 break;
1922 }
1923
1924 case spv::ExecutionModeDenormFlushToZero: {
1925 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001926 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001927 skip |=
1928 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1929 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device\n%s",
1930 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001931 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001932 skip |=
1933 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1934 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device\n%s",
1935 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001936 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001937 skip |=
1938 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1939 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device\n%s",
1940 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001941 }
1942
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001943 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1944 // Register the first denorm execution mode found
1945 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001946 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001947 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001948 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001949 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001950 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001951 "Shader uses different denorm execution modes for 16 and 64-bit but "
1952 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001953 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
1954 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001955 }
1956 break;
1957
Mike Schuchardt2df08912020-12-15 16:28:09 -08001958 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001959 break;
1960
Mike Schuchardt2df08912020-12-15 16:28:09 -08001961 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001962 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001963 "Shader uses different denorm execution modes for different bit widths but "
1964 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001965 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
1966 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001967 break;
1968
1969 default:
1970 break;
1971 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001972 }
1973 break;
1974 }
1975
1976 case spv::ExecutionModeRoundingModeRTE: {
1977 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001978 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1979 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001980 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device\n%s",
1981 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001982 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1983 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001984 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device\n%s",
1985 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001986 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1987 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001988 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device\n%s",
1989 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001990 }
1991
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001992 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1993 // Register the first rounding mode found
1994 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001995 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001996 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001997 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001998 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001999 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002000 "Shader uses different rounding modes for 16 and 64-bit but "
2001 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002002 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
2003 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002004 }
2005 break;
2006
Mike Schuchardt2df08912020-12-15 16:28:09 -08002007 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002008 break;
2009
Mike Schuchardt2df08912020-12-15 16:28:09 -08002010 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002011 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002012 "Shader uses different rounding modes for different bit widths but "
2013 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002014 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
2015 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002016 break;
2017
2018 default:
2019 break;
2020 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002021 }
2022 break;
2023 }
2024
2025 case spv::ExecutionModeRoundingModeRTZ: {
2026 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002027 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
2028 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002029 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device\n%s",
2030 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002031 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
2032 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002033 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device\n%s",
2034 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002035 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
2036 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002037 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device\n%s",
2038 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002039 }
2040
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002041 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2042 // Register the first rounding mode found
2043 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002044 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002045 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08002046 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002047 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002048 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002049 "Shader uses different rounding modes for 16 and 64-bit but "
2050 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002051 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
2052 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002053 }
2054 break;
2055
Mike Schuchardt2df08912020-12-15 16:28:09 -08002056 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002057 break;
2058
Mike Schuchardt2df08912020-12-15 16:28:09 -08002059 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002060 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002061 "Shader uses different rounding modes for different bit widths but "
2062 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002063 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
2064 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002065 break;
2066
2067 default:
2068 break;
2069 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002070 }
2071 break;
2072 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002073
2074 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002075 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002076 break;
2077 }
2078
2079 case spv::ExecutionModeInvocations: {
2080 invocations = insn.word(3);
2081 break;
2082 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002083
2084 case spv::ExecutionModeLocalSizeId: {
Tony-LunarG273f32f2021-09-28 08:56:30 -06002085 if (!enabled_features.core13.maintenance4) {
Piers Daniella7f93b62021-11-20 12:32:04 -07002086 skip |= LogError(device, "VUID-RuntimeSpirv-LocalSizeId-06434",
2087 "LocalSizeId execution mode used but maintenance4 feature not enabled");
2088 }
2089 break;
2090 }
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002091
2092 case spv::ExecutionModeEarlyFragmentTests: {
2093 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) &&
Younggwan Kimf8601f92021-12-17 09:38:07 +00002094 (pipeline && pipeline->create_info.graphics.pDepthStencilState &&
2095 (pipeline->create_info.graphics.pDepthStencilState->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(
2099 device, " VUID-VkGraphicsPipelineCreateInfo-pStages-06466",
2100 "The fragment shader enables early fragment tests, but VkPipelineDepthStencilStateCreateInfo::flags == "
2101 "%s",
2102 string_VkPipelineDepthStencilStateCreateFlags(pipeline->create_info.graphics.pDepthStencilState->flags)
2103 .c_str());
2104 }
2105 break;
2106 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002107 }
2108 }
2109 }
2110
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002111 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002112 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002113 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2114 "Geometry shader entry point must have an OpExecutionMode instruction that "
2115 "specifies a maximum output vertex count that is greater than 0 and less "
2116 "than or equal to maxGeometryOutputVertices. "
2117 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002118 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002119 }
2120
2121 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002122 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2123 "Geometry shader entry point must have an OpExecutionMode instruction that "
2124 "specifies an invocation count that is greater than 0 and less "
2125 "than or equal to maxGeometryShaderInvocations. "
2126 "Invocations=%d, maxGeometryShaderInvocations=%d",
2127 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002128 }
2129 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002130 return skip;
2131}
2132
Chris Forbes47567b72017-06-09 12:09:45 -07002133// For given pipelineLayout verify that the set_layout_node at slot.first
2134// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002135static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002136 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002137 if (!pipelineLayout) return nullptr;
2138
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002139 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07002140
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002141 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07002142}
2143
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002144// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2145// o If there is only a vertex shader : gl_PointSize must be written when using points
2146// o If there is a geometry or tessellation shader:
2147// - If shaderTessellationAndGeometryPointSize feature is enabled:
2148// * gl_PointSize must be written in the final geometry stage
2149// - If shaderTessellationAndGeometryPointSize feature is disabled:
2150// * gl_PointSize must NOT be written and a default of 1.0 is assumed
sfricke-samsungef15e482022-01-26 11:32:49 -08002151bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *module_state,
John Zulaufac4c6e12019-07-01 16:05:58 -06002152 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002153 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2154 return false;
2155 }
2156
2157 bool pointsize_written = false;
2158 bool skip = false;
2159
2160 // Search for PointSize built-in decorations
sfricke-samsungef15e482022-01-26 11:32:49 -08002161 for (const auto &set : module_state->GetBuiltinDecorationList()) {
2162 auto insn = module_state->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002163 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002164 pointsize_written = module_state->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002165 if (pointsize_written) {
2166 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002167 }
2168 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002169 }
2170
2171 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002172 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002173 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002174 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002175 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2176 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002177 }
2178 } else if (!pointsize_written) {
2179 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002180 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002181 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2182 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002183 }
2184 return skip;
2185}
John Zulauf14c355b2019-06-27 16:09:37 -06002186
sfricke-samsungef15e482022-01-26 11:32:49 -08002187bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *module_state,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002188 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2189 bool primitiverate_written = false;
2190 bool viewportindex_written = false;
2191 bool viewportmask_written = false;
2192 bool skip = false;
2193
2194 // Check if the primitive shading rate is written
sfricke-samsungef15e482022-01-26 11:32:49 -08002195 for (const auto &set : module_state->GetBuiltinDecorationList()) {
2196 auto insn = module_state->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002197 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002198 primitiverate_written = module_state->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002199 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002200 viewportindex_written = module_state->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002201 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002202 viewportmask_written = module_state->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002203 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002204 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2205 break;
2206 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002207 }
2208
Tony-LunarGd44844c2021-01-22 13:24:37 -07002209 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002210 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002211 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002212 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002213 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002214 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2215 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2216 "multiple viewports "
2217 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2218 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002219 }
2220
2221 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002222 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002223 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2224 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2225 "ViewportIndex built-ins,"
2226 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2227 string_VkShaderStageFlagBits(stage));
2228 }
2229
2230 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002231 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002232 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2233 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2234 "ViewportMaskNV built-ins,"
2235 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2236 string_VkShaderStageFlagBits(stage));
2237 }
2238 }
2239 return skip;
2240}
2241
sfricke-samsungef15e482022-01-26 11:32:49 -08002242bool CoreChecks::ValidateDecorations(SHADER_MODULE_STATE const *module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002243 bool skip = false;
2244
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002245 std::vector<spirv_inst_iter> xfb_streams;
2246 std::vector<spirv_inst_iter> xfb_buffers;
ziga-lunargef2c3172021-11-07 10:35:29 +01002247 std::vector<spirv_inst_iter> xfb_offsets;
2248
sfricke-samsungef15e482022-01-26 11:32:49 -08002249 for (const auto &op_decorate : module_state->GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002250 uint32_t decoration = op_decorate.word(2);
2251 if (decoration == spv::DecorationXfbStride) {
2252 uint32_t stride = op_decorate.word(3);
2253 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2254 skip |= LogError(
2255 device, "VUID-RuntimeSpirv-XfbStride-06313",
2256 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2257 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2258 ").",
2259 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2260 }
2261 }
ziga-lunarg423cf212021-11-07 00:00:27 +01002262 if (decoration == spv::DecorationStream) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002263 xfb_streams.push_back(op_decorate);
ziga-lunarg423cf212021-11-07 00:00:27 +01002264 uint32_t stream = op_decorate.word(3);
2265 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2266 skip |= LogError(
2267 device, "VUID-RuntimeSpirv-Stream-06312",
2268 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2269 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32 ").",
2270 stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
2271 }
2272 }
ziga-lunargef2c3172021-11-07 10:35:29 +01002273 if (decoration == spv::DecorationXfbBuffer) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002274 xfb_buffers.push_back(op_decorate);
ziga-lunargef2c3172021-11-07 10:35:29 +01002275 }
2276 if (decoration == spv::DecorationOffset) {
2277 xfb_offsets.push_back(op_decorate);
2278 }
2279 }
2280
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002281 // XfbBuffer, buffer data size
2282 std::vector<std::pair<uint32_t, uint32_t>> buffer_data_sizes;
ziga-lunargef2c3172021-11-07 10:35:29 +01002283 for (const auto &op_decorate : xfb_offsets) {
2284 for (const auto xfb_buffer : xfb_buffers) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002285 if (xfb_buffer.word(1) == op_decorate.word(1)) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002286 const auto offset = op_decorate.word(3);
sfricke-samsungef15e482022-01-26 11:32:49 -08002287 const auto def = module_state->get_def(xfb_buffer.word(1));
2288 const auto size = module_state->GetTypeBytesSize(def);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002289 const uint32_t buffer_data_size = offset + size;
2290 if (buffer_data_size > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002291 skip |= LogError(
2292 device, "VUID-RuntimeSpirv-Offset-06308",
2293 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_offset (%" PRIu32
2294 ") + size of variable (%" PRIu32 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2295 "(%" PRIu32 ").",
2296 offset, size, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2297 }
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002298
2299 bool found = false;
2300 for (auto &bds : buffer_data_sizes) {
2301 if (bds.first == xfb_buffer.word(1)) {
2302 bds.second = std::max(bds.second, buffer_data_size);
2303 found = true;
2304 break;
2305 }
2306 }
2307 if (!found) {
2308 buffer_data_sizes.emplace_back(xfb_buffer.word(1), buffer_data_size);
2309 }
2310
ziga-lunargef2c3172021-11-07 10:35:29 +01002311 break;
2312 }
2313 }
ziga-lunargce66e542021-09-19 00:11:14 +02002314 }
2315
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002316 std::unordered_map<uint32_t, uint32_t> stream_data_size;
2317 for (const auto &xfb_stream : xfb_streams) {
2318 for (const auto& bds : buffer_data_sizes) {
2319 if (xfb_stream.word(1) == bds.first) {
2320 uint32_t stream = xfb_stream.word(3);
2321 const auto itr = stream_data_size.find(stream);
2322 if (itr != stream_data_size.end()) {
2323 itr->second += bds.second;
2324 } else {
2325 stream_data_size.insert({stream, bds.second});
2326 }
2327 }
2328 }
2329 }
2330
2331 for (const auto& stream : stream_data_size) {
2332 if (stream.second > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreamDataSize) {
2333 skip |= LogError(device, "VUID-RuntimeSpirv-XfbBuffer-06309",
2334 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2335 ") having the sum of buffer data sizes (%" PRIu32
2336 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2337 "(%" PRIu32 ").",
2338 stream.first, stream.second,
2339 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2340 }
2341 }
2342
ziga-lunargce66e542021-09-19 00:11:14 +02002343 return skip;
2344}
2345
sfricke-samsungef15e482022-01-26 11:32:49 -08002346bool CoreChecks::ValidateTransformFeedback(SHADER_MODULE_STATE const *module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002347 bool skip = false;
2348
ziga-lunarg28d08792021-10-13 15:42:59 +02002349 // Temp workaround to prevent false positive errors
2350 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sfricke-samsungef15e482022-01-26 11:32:49 -08002351 if (module_state->HasMultipleEntryPoints()) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002352 return skip;
2353 }
2354
2355 layer_data::unordered_set<uint32_t> emitted_streams;
2356 bool output_points = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08002357 for (const auto &insn : *module_state) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002358 const uint32_t opcode = insn.opcode();
2359 if (opcode == spv::OpEmitStreamVertex) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002360 emitted_streams.emplace(static_cast<uint32_t>(module_state->GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002361 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002362 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002363 uint32_t stream = static_cast<uint32_t>(module_state->GetConstantValueById(insn.word(1)));
ziga-lunarg28d08792021-10-13 15:42:59 +02002364 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2365 skip |= LogError(
2366 device, "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002367 "vkCreateGraphicsPipelines(): shader uses transform feedback stream\n%s\nwith index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002368 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2369 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002370 module_state->DescribeInstruction(insn).c_str(), stream,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002371 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002372 }
2373 }
2374 if (opcode == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeOutputPoints) {
2375 output_points = true;
2376 }
2377 }
2378
2379 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2380 if (emitted_streams_size > 1 && !output_points &&
2381 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
2382 skip |= LogError(
2383 device, "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2384 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles is VK_FALSE, but execution mode is not OutputPoints.",
2385 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002386 }
2387
2388 return skip;
2389}
2390
sfricke-samsung864162a2021-11-01 21:58:01 -07002391// Checks for both TexelOffset and TexelGatherOffset limits
sfricke-samsungef15e482022-01-26 11:32:49 -08002392bool CoreChecks::ValidateTexelOffsetLimits(SHADER_MODULE_STATE const *module_state, spirv_inst_iter &insn) const {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002393 bool skip = false;
2394
2395 const uint32_t opcode = insn.opcode();
sfricke-samsung864162a2021-11-01 21:58:01 -07002396 if (ImageGatherOperation(opcode) || ImageSampleOperation(opcode) || ImageFetchOperation(opcode)) {
sfricke-samsung3a25ed52022-01-20 02:24:36 -08002397 uint32_t image_operand_position = OpcodeImageOperandsPosition(opcode);
sfricke-samsung864162a2021-11-01 21:58:01 -07002398 // Image operands can be optional
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002399 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2400 auto image_operand = insn.word(image_operand_position);
sfricke-samsung864162a2021-11-01 21:58:01 -07002401 // Bits we are validating (sample/fetch only check ConstOffset)
ziga-lunarga12c75a2021-09-16 16:36:16 +02002402 uint32_t offset_bits =
sfricke-samsung864162a2021-11-01 21:58:01 -07002403 ImageGatherOperation(opcode)
2404 ? (spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask)
2405 : (spv::ImageOperandsConstOffsetMask);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002406 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002407 // Operand values follow
2408 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002409 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2410 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2411 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2412 if (image_operand & i) { // If the bit is set, consume operand
2413 if (insn.len() > index && (i & offset_bits)) {
2414 uint32_t constant_id = insn.word(index);
sfricke-samsungef15e482022-01-26 11:32:49 -08002415 const auto &constant = module_state->get_def(constant_id);
2416 const bool is_dynamic_offset = constant == module_state->end();
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002417 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002418 for (uint32_t j = 3; j < constant.len(); ++j) {
2419 uint32_t comp_id = constant.word(j);
sfricke-samsungef15e482022-01-26 11:32:49 -08002420 const auto &comp = module_state->get_def(comp_id);
2421 const auto &comp_type = module_state->get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002422 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002423 const uint32_t offset = comp.word(3);
sfricke-samsung864162a2021-11-01 21:58:01 -07002424 // spec requires minTexelGatherOffset/minTexelOffset to be -8 or less so never can compare if
2425 // unsigned spec requires maxTexelGatherOffset/maxTexelOffset to be 7 or greater so never can
2426 // compare if signed is less then zero
sfricke-samsungef3fe742021-10-06 10:51:34 -07002427 const int32_t signed_offset = static_cast<int32_t>(offset);
2428 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2429
sfricke-samsung864162a2021-11-01 21:58:01 -07002430 // There are 2 sets of VU being covered where the only main difference is the opcode
2431 if (ImageGatherOperation(opcode)) {
2432 // min/maxTexelGatherOffset
2433 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
2434 skip |=
2435 LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002436 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002437 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002438 module_state->DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002439 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002440 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2441 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002442 skip |= LogError(device, "VUID-RuntimeSpirv-OpImage-06377",
2443 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2444 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32
2445 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002446 module_state->DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002447 phys_dev_props.limits.maxTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002448 }
2449 } else {
2450 // min/maxTexelOffset
2451 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelOffset)) {
2452 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06435",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002453 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsung864162a2021-11-01 21:58:01 -07002454 ") less than VkPhysicalDeviceLimits::minTexelOffset (%" PRIi32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002455 module_state->DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung864162a2021-11-01 21:58:01 -07002456 phys_dev_props.limits.minTexelOffset);
2457 } else if ((offset > phys_dev_props.limits.maxTexelOffset) &&
2458 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002459 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06436",
2460 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2461 ") greater than VkPhysicalDeviceLimits::maxTexelOffset (%" PRIu32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002462 module_state->DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002463 phys_dev_props.limits.maxTexelOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002464 }
ziga-lunarga12c75a2021-09-16 16:36:16 +02002465 }
2466 }
2467 }
2468 }
sfricke-samsung3511e312021-11-04 21:14:31 -07002469 index += ImageOperandsParamCount(i);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002470 }
2471 }
2472 }
2473 }
2474 }
2475
2476 return skip;
2477}
2478
sfricke-samsungef15e482022-01-26 11:32:49 -08002479bool CoreChecks::ValidateShaderClock(SHADER_MODULE_STATE const *module_state, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002480 bool skip = false;
2481
sfricke-samsung94167ca2021-02-26 04:14:59 -08002482 switch (insn.opcode()) {
2483 case spv::OpReadClockKHR: {
sfricke-samsungef15e482022-01-26 11:32:49 -08002484 auto scope_id = module_state->get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -08002485 auto scope_type = scope_id.word(3);
2486 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002487 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002488 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002489 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.\n%s",
2490 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
2491 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002492 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002493 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002494 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.\n%s",
2495 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
2496 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002497 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002498 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002499 }
2500 }
2501 return skip;
2502}
2503
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002504bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2505 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002506 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002507 const auto *pStage = stage_state.create_info;
sfricke-samsungef15e482022-01-26 11:32:49 -08002508 const auto *module_state = stage_state.module_state.get();
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002509 const auto &entrypoint = stage_state.entrypoint;
John Zulauf14c355b2019-06-27 16:09:37 -06002510 // Check the module
sfricke-samsungef15e482022-01-26 11:32:49 -08002511 if (!module_state->has_valid_spirv) {
2512 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2513 "%s does not contain valid spirv for stage %s.",
2514 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
2515 string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002516 }
2517
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002518 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2519 // specializations should be applied and validated.
2520 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
sfricke-samsungef15e482022-01-26 11:32:49 -08002521 pStage->pSpecializationInfo->pMapEntries != nullptr && module_state->HasSpecConstants()) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002522 // Gather the specialization-constant values.
2523 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002524 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002525 std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; // note: this must be std:: to work with spvtools
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002526 id_value_map.reserve(specialization_info->mapEntryCount);
2527 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2528 auto const &map_entry = specialization_info->pMapEntries[i];
sfricke-samsungef15e482022-01-26 11:32:49 -08002529 const auto itr = module_state->GetSpecConstMap().find(map_entry.constantID);
sfricke-samsung033b0262021-07-09 00:53:06 -07002530 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2531 // behavior of the pipeline."
sfricke-samsungef15e482022-01-26 11:32:49 -08002532 if (itr != module_state->GetSpecConstMap().cend()) {
sfricke-samsung033b0262021-07-09 00:53:06 -07002533 // Make sure map_entry.size matches the spec constant's size
2534 uint32_t spec_const_size = decoration_set::kInvalidValue;
sfricke-samsungef15e482022-01-26 11:32:49 -08002535 const auto def_ins = module_state->get_def(itr->second);
2536 const auto type_ins = module_state->get_def(def_ins.word(1));
sfricke-samsung033b0262021-07-09 00:53:06 -07002537 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2538 switch (type_ins.opcode()) {
2539 case spv::OpTypeBool:
2540 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2541 spec_const_size = sizeof(VkBool32);
2542 break;
2543 case spv::OpTypeInt:
2544 case spv::OpTypeFloat:
2545 spec_const_size = type_ins.word(2) / 8;
2546 break;
2547 default:
2548 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2549 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2550 break;
2551 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002552
sfricke-samsung033b0262021-07-09 00:53:06 -07002553 if (map_entry.size != spec_const_size) {
2554 skip |=
2555 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002556 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2557 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2558 map_entry.constantID, i, map_entry.size,
sfricke-samsungef15e482022-01-26 11:32:49 -08002559 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002560 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002561 }
2562
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002563 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002564 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2565 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2566 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2567 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2568 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2569
2570 std::copy(start_in_p, end_in_p, out_p);
2571 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002572 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002573 }
2574
sfricke-samsung5628f982021-10-19 09:21:59 -07002575 // both spirv-opt and spirv-val will use the same flags
2576 spvtools::ValidatorOptions options;
2577 AdjustValidatorOptions(device_extensions, enabled_features, options);
2578
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002579 // Apply the specialization-constant values and revalidate the shader module.
sfricke-samsung45996a42021-09-16 13:45:27 -07002580 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002581 spvtools::Optimizer optimizer(spirv_environment);
sfricke-samsungef15e482022-01-26 11:32:49 -08002582 spvtools::MessageConsumer consumer = [&skip, &module_state, &stage_state, this](
2583 spv_message_level_t level, const char *source, const spv_position_t &position,
2584 const char *message) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002585 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2586 "%s does not contain valid spirv for stage %s. %s",
sfricke-samsungef15e482022-01-26 11:32:49 -08002587 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002588 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002589 };
2590 optimizer.SetMessageConsumer(consumer);
2591 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2592 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2593 std::vector<uint32_t> specialized_spirv;
sfricke-samsungef15e482022-01-26 11:32:49 -08002594 auto const optimized =
2595 optimizer.Run(module_state->words.data(), module_state->words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002596 assert(optimized == true);
2597
2598 if (optimized) {
2599 spv_context ctx = spvContextCreate(spirv_environment);
2600 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2601 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002602 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2603 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002604 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002605 "After specialization was applied, %s does not contain valid spirv for stage %s.",
sfricke-samsungef15e482022-01-26 11:32:49 -08002606 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002607 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002608 }
2609
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002610 spvDiagnosticDestroy(diag);
2611 spvContextDestroy(ctx);
2612 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002613
sfricke-samsungef15e482022-01-26 11:32:49 -08002614 skip |= ValidateWorkgroupSize(module_state, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002615 }
2616
John Zulauf14c355b2019-06-27 16:09:37 -06002617 // Check the entrypoint
sfricke-samsungef15e482022-01-26 11:32:49 -08002618 if (entrypoint == module_state->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002619 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2620 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002621 }
2622 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2623
2624 // Mark accessible ids
2625 auto &accessible_ids = stage_state.accessible_ids;
2626
Chris Forbes47567b72017-06-09 12:09:45 -07002627 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002628
sfricke-samsung94167ca2021-02-26 04:14:59 -08002629 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2630 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002631 uint32_t total_shared_size = 0;
sfricke-samsungef15e482022-01-26 11:32:49 -08002632 for (auto insn : *module_state) {
2633 skip |= ValidateTexelOffsetLimits(module_state, insn);
2634 skip |= ValidateShaderCapabilitiesAndExtensions(insn);
2635 skip |= ValidateShaderClock(module_state, insn);
2636 skip |= ValidateShaderStageGroupNonUniform(module_state, pStage->stage, insn);
2637 skip |= ValidateMemoryScope(module_state, insn);
2638 total_shared_size += module_state->CalcComputeSharedMemory(pStage->stage, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08002639
2640 // Checks based off shaderStorageImage(Read|Write)WithoutFormat are
2641 // disabled if VK_KHR_format_feature_flags2 is supported.
2642 //
2643 // https://github.com/KhronosGroup/Vulkan-Docs/blob/6177645341afc/appendices/spirvenv.txt#L553
2644 //
2645 // The other checks need to take into account the format features and so
2646 // we apply that in the descriptor set matching validation code (see
2647 // descriptor_sets.cpp).
2648 if (!has_format_feature2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002649 skip |= ValidateShaderStorageImageFormats(module_state, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08002650 }
ziga-lunarga26b3602021-08-08 15:53:00 +02002651 }
2652
2653 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2654 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002655 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002656 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002657 }
2658
sfricke-samsungef15e482022-01-26 11:32:49 -08002659 skip |= ValidateTransformFeedback(module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002660 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2661 stage_state.has_atomic_descriptor);
sfricke-samsungef15e482022-01-26 11:32:49 -08002662 skip |= ValidateShaderStageInputOutputLimits(module_state, pStage, pipeline, entrypoint);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002663 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsungef15e482022-01-26 11:32:49 -08002664 skip |= ValidateAtomicsTypes(module_state);
2665 skip |= ValidateExecutionModes(module_state, entrypoint, pStage->stage, pipeline);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002666 skip |= ValidateSpecializations(pStage);
sfricke-samsungef15e482022-01-26 11:32:49 -08002667 skip |= ValidateDecorations(module_state);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002668 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002669 skip |= ValidatePointListShaderState(pipeline, module_state, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002670 }
sfricke-samsungef15e482022-01-26 11:32:49 -08002671 skip |= ValidateBuiltinLimits(module_state, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002672 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002673 skip |= ValidateCooperativeMatrix(module_state, pStage, pipeline);
sfricke-samsungd093e522021-02-26 04:17:45 -08002674 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002675 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002676 skip |= ValidatePrimitiveRateShaderState(pipeline, module_state, entrypoint, pStage->stage);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002677 }
sfricke-samsung45996a42021-09-16 13:45:27 -07002678 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002679 skip |= ValidateShaderResolveQCOM(module_state, pStage, pipeline);
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002680 }
ziga-lunarg73163742021-08-25 13:15:29 +02002681 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
2682 skip |= ValidateShaderSubgroupSizeControl(pStage);
2683 }
Chris Forbes47567b72017-06-09 12:09:45 -07002684
sfricke-samsung7699b912021-04-12 23:01:51 -07002685 // "layout must be consistent with the layout of the * shader"
2686 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002687 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002688 switch (pipeline->create_info.graphics.sType) {
2689 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2690 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2691 break;
2692 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2693 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2694 break;
2695 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2696 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2697 break;
2698 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2699 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2700 break;
2701 default:
2702 assert(false);
2703 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002704 }
2705
sfricke-samsung7699b912021-04-12 23:01:51 -07002706 // Validate Push Constants use
sfricke-samsungef15e482022-01-26 11:32:49 -08002707 skip |= ValidatePushConstantUsage(*pipeline, module_state, pStage, vuid_layout_mismatch);
sfricke-samsung7699b912021-04-12 23:01:51 -07002708
Chris Forbes47567b72017-06-09 12:09:45 -07002709 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002710 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002711 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002712 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08002713 uint32_t required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002714 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2715 std::set<uint32_t> descriptor_types =
sfricke-samsungef15e482022-01-26 11:32:49 -08002716 TypeToDescriptorTypeSet(module_state, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002717
2718 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002719 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002720 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002721 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002722 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002723 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002724 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2725 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002726 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2727 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002728 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002729 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2730 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002731 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002732 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002733 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002734 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002735 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002736 }
2737 }
2738
2739 // Validate use of input attachments against subpass structure
2740 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002741 auto input_attachment_uses = module_state->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002742
amhagana448ea52021-11-02 14:09:14 -04002743 if (!pipeline->rp_state->use_dynamic_rendering) {
2744 auto rpci = pipeline->rp_state->createInfo.ptr();
2745 auto subpass = pipeline->create_info.graphics.subpass;
2746 for (auto use : input_attachment_uses) {
2747 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2748 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
2749 ? input_attachments[use.first].attachment
2750 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002751
amhagana448ea52021-11-02 14:09:14 -04002752 if (index == VK_ATTACHMENT_UNUSED) {
2753 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2754 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsungef15e482022-01-26 11:32:49 -08002755 } else if (!(GetFormatType(rpci->pAttachments[index].format) &
2756 module_state->GetFundamentalType(use.second.type_id))) {
2757 skip |= LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2758 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
2759 string_VkFormat(rpci->pAttachments[index].format),
2760 module_state->DescribeType(use.second.type_id).c_str());
amhagana448ea52021-11-02 14:09:14 -04002761 }
Chris Forbes47567b72017-06-09 12:09:45 -07002762 }
2763 }
2764 }
Lockeaa8fdc02019-04-02 11:59:20 -06002765 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002766 skip |= ValidateComputeWorkGroupSizes(module_state, entrypoint, stage_state);
Lockeaa8fdc02019-04-02 11:59:20 -06002767 }
ziga-lunarg73163742021-08-25 13:15:29 +02002768
Chris Forbes47567b72017-06-09 12:09:45 -07002769 return skip;
2770}
2771
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002772bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2773 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2774 spirv_inst_iter consumer_entrypoint,
2775 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002776 bool skip = false;
2777
2778 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002779 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2780 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002781
2782 auto a_it = outputs.begin();
2783 auto b_it = inputs.begin();
2784
ziga-lunarg8346fe82021-08-22 17:30:50 +02002785 uint32_t a_component = 0;
2786 uint32_t b_component = 0;
2787
Chris Forbes47567b72017-06-09 12:09:45 -07002788 // Maps sorted by key (location); walk them together to find mismatches
2789 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2790 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2791 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2792 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2793 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2794
ziga-lunarg8346fe82021-08-22 17:30:50 +02002795 a_first.second += a_component;
2796 b_first.second += b_component;
2797
2798 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2799 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2800 assert(a_at_end || a_component < a_length);
2801 assert(b_at_end || b_component < b_length);
2802
Chris Forbes47567b72017-06-09 12:09:45 -07002803 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002804 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002805 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2806 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2807 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2808 a_it++;
2809 a_component = 0;
2810 } else {
2811 a_component++;
2812 }
Chris Forbes47567b72017-06-09 12:09:45 -07002813 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002814 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002815 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2816 b_first.first, b_first.second, producer_stage->name);
2817 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2818 b_it++;
2819 b_component = 0;
2820 } else {
2821 b_component++;
2822 }
Chris Forbes47567b72017-06-09 12:09:45 -07002823 } else {
2824 // subtleties of arrayed interfaces:
2825 // - if is_patch, then the member is not arrayed, even though the interface may be.
2826 // - if is_block_member, then the extra array level of an arrayed interface is not
2827 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002828 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002829 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002830 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002831 producer->DescribeType(a_it->second.type_id).c_str(),
2832 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002833 a_it++;
2834 b_it++;
2835 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002836 }
2837 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002838 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002839 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2840 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2841 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002842 }
2843 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002844 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002845 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2846 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002847 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002848 uint32_t a_remaining = a_length - a_component;
2849 uint32_t b_remaining = b_length - b_component;
2850 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2851 a_it++;
2852 b_it++;
2853 a_component = 0;
2854 b_component = 0;
2855 } else if (a_remaining > b_remaining) { // a has more components remaining
2856 a_component += b_remaining;
2857 b_component = 0;
2858 b_it++;
2859 } else if (b_remaining > a_remaining) { // b has more components remaining
2860 b_component += a_remaining;
2861 a_component = 0;
2862 a_it++;
2863 }
Chris Forbes47567b72017-06-09 12:09:45 -07002864 }
2865 }
2866
Ari Suonpaa696b3432019-03-11 14:02:57 +02002867 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002868 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2869 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002870
2871 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2872 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002873 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002874 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002875 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2876 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002877 } else {
2878 auto it_producer = builtins_producer.begin();
2879 auto it_consumer = builtins_consumer.begin();
2880 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2881 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002882 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002883 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2884 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002885 break;
2886 }
2887 it_producer++;
2888 it_consumer++;
2889 }
2890 }
2891 }
2892 }
2893
Chris Forbes47567b72017-06-09 12:09:45 -07002894 return skip;
2895}
2896
John Zulauf14c355b2019-06-27 16:09:37 -06002897static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002898 uint32_t stage_mask = 0;
2899 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2900 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2901 stage_mask |= pCreateInfo->pStages[i].stage;
2902 }
2903 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002904 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2905 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2906 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002907 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2908 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2909 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2910 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2911 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002912 }
2913 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002914 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002915}
2916
Chris Forbes47567b72017-06-09 12:09:45 -07002917// Validate that the shaders used by the given pipeline and store the active_slots
2918// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002919bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002920 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002921
Chris Forbes47567b72017-06-09 12:09:45 -07002922 bool skip = false;
2923
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002924 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002925
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002926 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
2927 for (auto &stage : pipeline->stage_state) {
2928 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
2929 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
2930 vertex_stage = &stage;
2931 }
2932 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
2933 fragment_stage = &stage;
2934 }
Chris Forbes47567b72017-06-09 12:09:45 -07002935 }
2936
2937 // if the shader stages are no good individually, cross-stage validation is pointless.
2938 if (skip) return true;
2939
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002940 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002941
2942 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002943 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002944 }
2945
sfricke-samsungef15e482022-01-26 11:32:49 -08002946 if (vertex_stage && vertex_stage->module_state->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
2947 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module_state.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07002948 }
2949
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002950 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
2951 const auto &producer = pipeline->stage_state[i - 1];
2952 const auto &consumer = pipeline->stage_state[i];
sfricke-samsungef15e482022-01-26 11:32:49 -08002953 assert(producer.module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002954 if (&producer == fragment_stage) {
2955 break;
2956 }
sfricke-samsungef15e482022-01-26 11:32:49 -08002957 if (consumer.module_state) {
2958 if (consumer.module_state->has_valid_spirv && producer.module_state->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002959 auto producer_id = GetShaderStageId(producer.stage_flag);
2960 auto consumer_id = GetShaderStageId(consumer.stage_flag);
sfricke-samsungef15e482022-01-26 11:32:49 -08002961 skip |= ValidateInterfaceBetweenStages(producer.module_state.get(), producer.entrypoint,
2962 &shader_stage_attribs[producer_id], consumer.module_state.get(),
2963 consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002964 }
Chris Forbes47567b72017-06-09 12:09:45 -07002965 }
2966 }
2967
sfricke-samsungef15e482022-01-26 11:32:49 -08002968 if (fragment_stage && fragment_stage->module_state->has_valid_spirv) {
Aaron Hagan1209c782021-11-22 19:37:14 -05002969 if (pipeline->rp_state->use_dynamic_rendering) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002970 skip |= ValidateFsOutputsAgainstDynamicRenderingRenderPass(fragment_stage->module_state.get(),
2971 fragment_stage->entrypoint, pipeline);
Aaron Hagan1209c782021-11-22 19:37:14 -05002972 } else {
sfricke-samsungef15e482022-01-26 11:32:49 -08002973 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module_state.get(), fragment_stage->entrypoint, pipeline,
Aaron Hagan1209c782021-11-22 19:37:14 -05002974 create_info->subpass);
2975 }
Chris Forbes47567b72017-06-09 12:09:45 -07002976 }
2977
2978 return skip;
2979}
2980
Tony-LunarGb2ded512021-02-02 16:03:30 -07002981bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2982 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002983 bool skip = false;
2984
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002985 for (auto &stage : pipeline->stage_state) {
2986 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2987 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002988 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2989 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben3dfeacf2021-12-02 08:46:39 -07002990 if (stage.wrote_primitive_shading_rate) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002991 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002992 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002993 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2994 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2995 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002996 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002997 }
2998 }
2999 }
3000 }
3001
3002 return skip;
3003}
3004
sfricke-samsunge72a85e2020-02-29 21:48:37 -08003005bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003006 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07003007}
Chris Forbes4ae55b32017-06-09 14:42:56 -07003008
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003009uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
3010 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06003011 const auto &create_info = pipeline->create_info.raytracing;
3012 const auto *stages = create_info.ptr()->pStages;
3013 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003014 if (stages[stage_index].stage == stageBit) {
3015 total++;
3016 }
3017 }
3018
Jeremy Gebben11af9792021-08-20 10:20:09 -06003019 if (create_info.pLibraryInfo) {
3020 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003021 const auto library_pipeline = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003022 total += CalcShaderStageCount(library_pipeline.get(), stageBit);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003023 }
3024 }
3025
3026 return total;
3027}
3028
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003029bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE *pipeline, uint32_t group, uint32_t stage) const {
3030 if (group == VK_SHADER_UNUSED_NV) {
3031 return true;
3032 }
3033
3034 const auto &create_info = pipeline->create_info.raytracing;
3035 const auto *stages = create_info.ptr()->pStages;
3036
3037 if (group < create_info.stageCount) {
3038 return (stages[group].stage & stage) != 0;
3039 }
3040 group -= create_info.stageCount;
3041
3042 // Search libraries
3043 if (create_info.pLibraryInfo) {
3044 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003045 auto library_pipeline = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003046 const uint32_t stage_count = library_pipeline->create_info.raytracing.ptr()->stageCount;
3047 if (group < stage_count) {
3048 return (library_pipeline->create_info.raytracing.ptr()->pStages[group].stage & stage) != 0;
3049 }
3050 group -= stage_count;
3051 }
3052 }
3053
3054 // group index too large
3055 return false;
3056}
3057
sourav parmarcd5fb182020-07-17 12:58:44 -07003058bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06003059 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04003060
Jeremy Gebben11af9792021-08-20 10:20:09 -06003061 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003062 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003063 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
3064 skip |=
3065 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
3066 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
3067 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
3068 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003069 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003070 if (create_info.pLibraryInfo) {
3071 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003072 const auto library_pipelinestate = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Jeremy Gebben11af9792021-08-20 10:20:09 -06003073 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
3074 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003075 skip |= LogError(
3076 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
3077 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
3078 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003079 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07003080 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003081 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
3082 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
3083 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
3084 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003085 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
3086 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
3087 "member must have been created with values of the maxPipelineRayPayloadSize and "
3088 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
3089 }
3090 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06003091 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003092 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
3093 "vkCreateRayTracingPipelinesKHR: If flags includes "
3094 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
3095 "the pLibraries member of libraries must have been created with the "
3096 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
3097 }
sourav parmar83c31b12020-05-06 12:30:54 -07003098 }
3099 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003100 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003101 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003102 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
3103 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
3104 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003105 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003106 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003107 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003108 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04003109
Jeremy Gebben11af9792021-08-20 10:20:09 -06003110 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003111 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003112 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003113
Jeremy Gebben11af9792021-08-20 10:20:09 -06003114 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003115 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
3116 if (raygen_stages_count == 0) {
3117 skip |= LogError(
3118 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07003119 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003120 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
3121 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003122 }
3123
Jeremy Gebben11af9792021-08-20 10:20:09 -06003124 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003125 const auto &group = groups[group_index];
3126
3127 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003128 if (!GroupHasValidIndex(
3129 pipeline, group.generalShader,
3130 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 -05003131 skip |= LogError(device,
3132 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
3133 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
3134 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003135 }
3136 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3137 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003138 skip |= LogError(device,
3139 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
3140 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
3141 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003142 }
3143 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003144 if (!GroupHasValidIndex(pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003145 skip |= LogError(device,
3146 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
3147 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
3148 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003149 }
3150 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3151 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003152 skip |= LogError(device,
3153 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
3154 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
3155 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003156 }
3157 }
3158
3159 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3160 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003161 if (!GroupHasValidIndex(pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003162 skip |= LogError(device,
3163 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
3164 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
3165 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003166 }
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003167 if (!GroupHasValidIndex(pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003168 skip |= LogError(device,
3169 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
3170 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
3171 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003172 }
3173 }
John Zulaufe4474e72019-07-01 17:28:27 -06003174 }
3175 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003176}
3177
Dave Houltona9df0ce2018-02-07 10:51:23 -07003178uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003179
Dave Houltona9df0ce2018-02-07 10:51:23 -07003180static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003181 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06003182 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003183 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003184 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003185 return nullptr;
3186}
3187
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003188bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003189 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003190 bool skip = false;
3191 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003192
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06003193 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003194 return false;
3195 }
3196
sfricke-samsung45996a42021-09-16 13:45:27 -07003197 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003198
3199 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003200 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
3201 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3202 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003203 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003204 auto cache = GetValidationCacheInfo(pCreateInfo);
3205 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07003206 // If app isn't using a shader validation cache, use the default one from CoreChecks
3207 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003208 if (cache) {
3209 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003210 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003211 }
3212
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003213 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3214 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07003215 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06003216 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003217 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003218 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003219 spvtools::ValidatorOptions options;
3220 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06003221 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003222 if (spv_valid != SPV_SUCCESS) {
3223 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003224 if (spv_valid == SPV_WARNING) {
3225 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3226 diag && diag->error ? diag->error : "(no error text)");
3227 } else {
3228 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3229 diag && diag->error ? diag->error : "(no error text)");
3230 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003231 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003232 } else {
3233 if (cache) {
3234 cache->Insert(hash);
3235 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003236 }
3237
3238 spvDiagnosticDestroy(diag);
3239 spvContextDestroy(ctx);
3240 }
3241
Chris Forbes4ae55b32017-06-09 14:42:56 -07003242 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003243}
3244
sfricke-samsungef15e482022-01-26 11:32:49 -08003245bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *module_state, const spirv_inst_iter &entrypoint,
ziga-lunarg11fecb92021-09-20 16:48:06 +02003246 const PipelineStageState &stage_state) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003247 bool skip = false;
3248 uint32_t local_size_x = 0;
3249 uint32_t local_size_y = 0;
3250 uint32_t local_size_z = 0;
sfricke-samsungef15e482022-01-26 11:32:49 -08003251 if (module_state->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06003252 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003253 skip |= LogError(module_state->vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003254 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08003255 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003256 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06003257 }
3258 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003259 skip |= LogError(module_state->vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003260 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08003261 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003262 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06003263 }
3264 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003265 skip |= LogError(module_state->vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003266 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08003267 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003268 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06003269 }
3270
3271 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3272 uint64_t invocations = local_size_x * local_size_y;
3273 // Prevent overflow.
3274 bool fail = false;
3275 if (invocations > UINT32_MAX || invocations > limit) {
3276 fail = true;
3277 }
3278 if (!fail) {
3279 invocations *= local_size_z;
3280 if (invocations > UINT32_MAX || invocations > limit) {
3281 fail = true;
3282 }
3283 }
3284 if (fail) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003285 skip |= LogError(module_state->vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003286 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3287 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08003288 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x, local_size_y,
sfricke-samsung1ff329f2021-09-16 10:06:47 -07003289 local_size_z, limit);
Lockeaa8fdc02019-04-02 11:59:20 -06003290 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003291
3292 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3293 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
3294 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3295 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3296 skip |= LogError(
sfricke-samsungef15e482022-01-26 11:32:49 -08003297 module_state->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
ziga-lunarg11fecb92021-09-20 16:48:06 +02003298 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3299 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3300 "dimension (%" PRIu32
3301 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08003302 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
ziga-lunarg11fecb92021-09-20 16:48:06 +02003303 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3304 }
3305 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3306 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
3307 const auto *required_subgroup_size_features =
3308 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
3309 if (!required_subgroup_size_features) {
3310 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
3311 skip |= LogError(
sfricke-samsungef15e482022-01-26 11:32:49 -08003312 module_state->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
ziga-lunarg11fecb92021-09-20 16:48:06 +02003313 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3314 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3315 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3316 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08003317 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
ziga-lunarg11fecb92021-09-20 16:48:06 +02003318 phys_dev_props_core11.subgroupSize);
3319 }
3320 }
3321 }
Lockeaa8fdc02019-04-02 11:59:20 -06003322 }
3323 return skip;
3324}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003325
3326spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
Tony-LunarGe67fcc22022-01-03 16:40:53 -07003327 if (api_version >= VK_API_VERSION_1_3) {
3328 return SPV_ENV_VULKAN_1_3;
3329 } else if (api_version >= VK_API_VERSION_1_2) {
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003330 return SPV_ENV_VULKAN_1_2;
3331 } else if (api_version >= VK_API_VERSION_1_1) {
3332 if (spirv_1_4) {
3333 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3334 } else {
3335 return SPV_ENV_VULKAN_1_1;
3336 }
3337 }
3338 return SPV_ENV_VULKAN_1_0;
3339}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003340
sfricke-samsungecc112a2021-09-03 05:32:17 -07003341// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003342void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003343 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003344 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3345 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003346 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003347 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003348 options.SetRelaxBlockLayout(true);
3349 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003350
3351 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3352 // Vulkan version used, the feature bit is needed (also described in the spec).
3353
3354 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3355 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003356 options.SetUniformBufferStandardLayout(true);
3357 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003358 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3359 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003360 options.SetScalarBlockLayout(true);
3361 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003362 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3363 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003364 options.SetWorkgroupScalarBlockLayout(true);
3365 }
Tony-LunarG273f32f2021-09-28 08:56:30 -06003366 if (enabled_features.core13.maintenance4) {
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003367 // --allow-localsizeid
3368 options.SetAllowLocalSizeId(true);
3369 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003370}