blob: 6df238039f5429bb043d0208d0e635e6a5f6efc0 [file] [log] [blame]
sfricke-samsung691299b2021-01-01 20:48:48 -08001/* Copyright (c) 2015-2021 The Khronos Group Inc.
2 * Copyright (c) 2015-2021 Valve Corporation
3 * Copyright (c) 2015-2021 LunarG, Inc.
4 * Copyright (C) 2015-2021 Google Inc.
Tobias Hector6663c9b2020-11-05 10:18:02 +00005 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Chris Forbes47567b72017-06-09 12:09:45 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Chris Forbes <chrisf@ijw.co.nz>
Dave Houlton51653902018-06-22 17:32:13 -060020 * Author: Dave Houlton <daveh@lunarg.com>
Tobias Hector6663c9b2020-11-05 10:18:02 +000021 * Author: Tobias Hector <tobias.hector@amd.com>
Chris Forbes47567b72017-06-09 12:09:45 -070022 */
23
Petr Kraus25810d02019-08-27 17:41:15 +020024#include "shader_validation.h"
25
Chris Forbes47567b72017-06-09 12:09:45 -070026#include <cassert>
Petr Kraus25810d02019-08-27 17:41:15 +020027#include <cinttypes>
Jeff Bolzf234bf82019-11-04 14:07:15 -060028#include <cmath>
Chris Forbes47567b72017-06-09 12:09:45 -070029#include <sstream>
Petr Kraus25810d02019-08-27 17:41:15 +020030#include <string>
Petr Kraus25810d02019-08-27 17:41:15 +020031#include <vector>
32
Mark Lobodzinski102687e2020-04-28 11:03:28 -060033#include <spirv/unified1/spirv.hpp>
Chris Forbes47567b72017-06-09 12:09:45 -070034#include "vk_enum_string_helper.h"
Chris Forbes47567b72017-06-09 12:09:45 -070035#include "vk_layer_data.h"
Chris Forbes47567b72017-06-09 12:09:45 -070036#include "vk_layer_utils.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070037#include "chassis.h"
Chris Forbes47567b72017-06-09 12:09:45 -070038#include "core_validation.h"
Petr Kraus25810d02019-08-27 17:41:15 +020039
Chris Forbes9a61e082017-07-24 15:35:29 -070040#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070041
Chris Forbes47567b72017-06-09 12:09:45 -070042static shader_stage_attributes shader_stage_attribs[] = {
Ari Suonpaa696b3432019-03-11 14:02:57 +020043 {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT},
44 {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT},
45 {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT},
46 {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT},
47 {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT},
Chris Forbes47567b72017-06-09 12:09:45 -070048};
49
ziga-lunarg8346fe82021-08-22 17:30:50 +020050static bool BaseTypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, const spirv_inst_iter &a_base_insn,
51 const spirv_inst_iter &b_base_insn) {
52 const uint32_t a_opcode = a_base_insn.opcode();
53 const uint32_t b_opcode = b_base_insn.opcode();
54 if (a_opcode == b_opcode) {
55 if (a_opcode == spv::OpTypeInt) {
56 // Match width and signedness
57 return a_base_insn.word(2) == b_base_insn.word(2) && a_base_insn.word(3) == b_base_insn.word(3);
58 } else if (a_opcode == spv::OpTypeFloat) {
59 // Match width
60 return a_base_insn.word(2) == b_base_insn.word(2);
61 } else if (a_opcode == spv::OpTypeStruct) {
62 // Match on all element types
63 if (a_base_insn.len() != b_base_insn.len()) {
64 return false; // Structs cannot match if member counts differ
65 }
66
67 for (unsigned i = 2; i < a_base_insn.len(); i++) {
68 if (!BaseTypesMatch(a, b, a->get_def(a_base_insn.word(i)), b->get_def(b_base_insn.word(i)))) {
69 return false;
70 }
71 }
72
73 return true;
74 }
75 }
76 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070077}
78
ziga-lunarg8346fe82021-08-22 17:30:50 +020079static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, unsigned a_type, unsigned b_type) {
80 const auto &a_insn = a->get_def(a_type);
81 const auto &b_insn = b->get_def(b_type);
82 const uint32_t a_base_insn_id = a->GetBaseType(a_insn);
83 const uint32_t b_base_insn_id = b->GetBaseType(b_insn);
84 const auto &a_base_insn = a->get_def(a_base_insn_id);
85 const auto &b_base_insn = b->get_def(b_base_insn_id);
Chris Forbes47567b72017-06-09 12:09:45 -070086
ziga-lunarg8346fe82021-08-22 17:30:50 +020087 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070088}
89
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060090static unsigned GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -070091 switch (format) {
92 case VK_FORMAT_R64G64B64A64_SFLOAT:
93 case VK_FORMAT_R64G64B64A64_SINT:
94 case VK_FORMAT_R64G64B64A64_UINT:
95 case VK_FORMAT_R64G64B64_SFLOAT:
96 case VK_FORMAT_R64G64B64_SINT:
97 case VK_FORMAT_R64G64B64_UINT:
98 return 2;
99 default:
100 return 1;
101 }
102}
103
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600104static unsigned GetFormatType(VkFormat fmt) {
Dave Houltona9df0ce2018-02-07 10:51:23 -0700105 if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT;
106 if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT;
107 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
108 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700109 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
110 return FORMAT_TYPE_FLOAT;
111}
112
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600113static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700114 uint32_t bit_pos = uint32_t(u_ffs(stage));
115 return bit_pos - 1;
116}
117
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700118bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700119 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
120 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700121 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700122 bool skip = false;
123
124 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
125 auto desc = &vi->pVertexBindingDescriptions[i];
126 auto &binding = bindings[desc->binding];
127 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600128 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700129 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
130 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700131 } else {
132 binding = desc;
133 }
134 }
135
136 return skip;
137}
138
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700139bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs,
140 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700141 bool skip = false;
142
sfricke-samsung962cad92021-04-13 00:46:29 -0700143 const auto inputs = vs->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700144
145 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200146 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700147 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200148 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
149 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
150 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700151 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
152 }
153 }
154 }
155
Petr Kraus25810d02019-08-27 17:41:15 +0200156 struct AttribInputPair {
157 const VkVertexInputAttributeDescription *attrib = nullptr;
158 const interface_var *input = nullptr;
159 };
160 std::map<uint32_t, AttribInputPair> location_map;
161 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
162 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700163
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400164 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200165 const auto location = location_it.first;
166 const auto attrib = location_it.second.attrib;
167 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600168
Petr Kraus25810d02019-08-27 17:41:15 +0200169 if (attrib && !input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600170 skip |= LogPerformanceWarning(vs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700171 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200172 } else if (!attrib && input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600173 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700174 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200175 } else if (attrib && input) {
176 const auto attrib_type = GetFormatType(attrib->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700177 const auto input_type = vs->GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700178
179 // Type checking
180 if (!(attrib_type & input_type)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600181 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700182 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sfricke-samsung962cad92021-04-13 00:46:29 -0700183 string_VkFormat(attrib->format), location, vs->DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700184 }
Petr Kraus25810d02019-08-27 17:41:15 +0200185 } else { // !attrib && !input
186 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700187 }
188 }
189
190 return skip;
191}
192
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700193bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint,
194 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200195 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700196
Petr Kraus25810d02019-08-27 17:41:15 +0200197 const auto rpci = pipeline->rp_state->createInfo.ptr();
198
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600199 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800200 const VkAttachmentReference2 *reference = nullptr;
201 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600202 const interface_var *output = nullptr;
203 };
204 std::map<uint32_t, Attachment> location_map;
205
Petr Kraus25810d02019-08-27 17:41:15 +0200206 const auto subpass = rpci->pSubpasses[subpass_index];
207 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600208 auto const &reference = subpass.pColorAttachments[i];
209 location_map[i].reference = &reference;
210 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
211 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
212 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
Chris Forbes47567b72017-06-09 12:09:45 -0700213 }
214 }
215
Chris Forbes47567b72017-06-09 12:09:45 -0700216 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
217
sfricke-samsung962cad92021-04-13 00:46:29 -0700218 const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600219 for (const auto &output_it : outputs) {
220 auto const location = output_it.first.first;
221 location_map[location].output = &output_it.second;
222 }
Chris Forbes47567b72017-06-09 12:09:45 -0700223
Jeremy Gebben11af9792021-08-20 10:20:09 -0600224 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
225 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700226
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400227 for (const auto &location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600228 const auto reference = location_it.second.reference;
229 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
230 continue;
231 }
232
Petr Kraus25810d02019-08-27 17:41:15 +0200233 const auto location = location_it.first;
234 const auto attachment = location_it.second.attachment;
235 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +0200236 if (attachment && !output) {
237 if (pipeline->attachments[location].colorWriteMask != 0) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600238 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700239 "Attachment %" PRIu32
240 " not written by fragment shader; undefined values will be written to attachment",
241 location);
Petr Kraus25810d02019-08-27 17:41:15 +0200242 }
243 } else if (!attachment && output) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700244 if (!(alpha_to_coverage_enabled && location == 0)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600245 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700246 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200247 }
Petr Kraus25810d02019-08-27 17:41:15 +0200248 } else if (attachment && output) {
249 const auto attachment_type = GetFormatType(attachment->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700250 const auto output_type = fs->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700251
252 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +0200253 if (!(output_type & attachment_type)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700254 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600255 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700256 "Attachment %" PRIu32
257 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sfricke-samsung962cad92021-04-13 00:46:29 -0700258 location, string_VkFormat(attachment->format), fs->DescribeType(output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700259 }
Petr Kraus25810d02019-08-27 17:41:15 +0200260 } else { // !attachment && !output
261 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700262 }
263 }
264
Petr Kraus25810d02019-08-27 17:41:15 +0200265 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700266 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
sfricke-samsung962cad92021-04-13 00:46:29 -0700267 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700268 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600269 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700270 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200271 }
272
Chris Forbes47567b72017-06-09 12:09:45 -0700273 return skip;
274}
275
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600276PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
277 const shader_struct_member &push_constant_used_in_shader,
278 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600279 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600280 const auto used_bytes_size = used_bytes->size();
281 if (used_bytes_size == 0) return PC_Byte_Updated;
282
283 const auto push_constant_data_update_size = push_constant_data_update.size();
284 const auto *data = push_constant_data_update.data();
285 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
286 if (used_bytes_size <= push_constant_data_update_size) {
287 return PC_Byte_Updated;
288 }
289 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
290
291 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
292 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
293 return PC_Byte_Updated;
294 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600295 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600296
locke-lunargde3f0fa2020-09-10 11:55:31 -0600297 uint32_t i = 0;
298 for (const auto used : *used_bytes) {
299 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600300 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600301 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600302 return PC_Byte_Not_Set;
303 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600304 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600305 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600306 }
307 }
308 ++i;
309 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600310 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600311}
312
313bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src,
sfricke-samsung7699b912021-04-12 23:01:51 -0700314 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700315 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700316 // Temp workaround to prevent false positive errors
317 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
318 if (src->multiple_entry_points) {
319 return skip;
320 }
321
Chris Forbes47567b72017-06-09 12:09:45 -0700322 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
sfricke-samsung962cad92021-04-13 00:46:29 -0700323 const auto *entrypoint = src->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600324 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
325 return skip;
326 }
327 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700328
locke-lunargde3f0fa2020-09-10 11:55:31 -0600329 bool found_stage = false;
330 for (auto const &range : *push_constant_ranges) {
331 if (range.stageFlags & pStage->stage) {
332 found_stage = true;
333 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600334 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600335 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600336 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600337 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600338 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600339 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600340 const auto ret =
341 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700342
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600343 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600344 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600345 LogObjectList objlist(src->vk_shader_module());
346 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700347 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 -0600348 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600349 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600350 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700351 }
352 }
353 }
354
locke-lunargde3f0fa2020-09-10 11:55:31 -0600355 if (!found_stage) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600356 LogObjectList objlist(src->vk_shader_module());
357 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700358 skip |= LogError(objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600359 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module()).c_str(),
360 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700361 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700362 }
Chris Forbes47567b72017-06-09 12:09:45 -0700363 return skip;
364}
365
sfricke-samsungcfb44592021-07-25 00:36:28 -0700366bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700367 bool skip = false;
368
369 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700370 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700371 return skip;
372 }
373
sfricke-samsungcfb44592021-07-25 00:36:28 -0700374 // Find all builtin from just the interface variables
375 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700376 auto insn = src->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700377 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700378 const decoration_set decorations = src->get_decorations(insn.word(2));
379
sfricke-samsungcfb44592021-07-25 00:36:28 -0700380 // Currently don't need to search in structs
381 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700382 auto type_pointer = src->get_def(insn.word(1));
383 assert(type_pointer.opcode() == spv::OpTypePointer);
384
385 auto type = src->get_def(type_pointer.word(3));
386 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700387 uint32_t length = static_cast<uint32_t>(src->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700388 // Handles both the input and output sampleMask
389 if (length > phys_dev_props.limits.maxSampleMaskWords) {
390 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
391 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
392 "maxSampleMaskWords of %u in %s.",
393 length, phys_dev_props.limits.maxSampleMaskWords,
394 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700395 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700396 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700397 }
398 }
399 }
400
401 return skip;
402}
403
Chris Forbes47567b72017-06-09 12:09:45 -0700404// Validate that data for each specialization entry is fully contained within the buffer.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200405bool CoreChecks::ValidateSpecializations(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700406 bool skip = false;
407
408 VkSpecializationInfo const *spec = info->pSpecializationInfo;
409
410 if (spec) {
411 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600412 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700413 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
414 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200415 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700416 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
417 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600418
419 continue;
420 }
Chris Forbes47567b72017-06-09 12:09:45 -0700421 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700422 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
423 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200424 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700425 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
426 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700427 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200428 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
429 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
430 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
431 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
432 j, spec->pMapEntries[i].constantID);
433 }
434 }
Chris Forbes47567b72017-06-09 12:09:45 -0700435 }
436 }
437
438 return skip;
439}
440
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500441// TODO (jbolz): Can this return a const reference?
sourav parmarcd5fb182020-07-17 12:58:44 -0700442static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count,
443 bool is_khr) {
Chris Forbes47567b72017-06-09 12:09:45 -0700444 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800445 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700446 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500447 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700448
449 // 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 -0500450 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
451 if (type.opcode() == spv::OpTypeRuntimeArray) {
452 descriptor_count = 0;
453 type = module->get_def(type.word(2));
454 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700455 descriptor_count *= module->GetConstantValueById(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700456 type = module->get_def(type.word(2));
457 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800458 if (type.word(2) == spv::StorageClassStorageBuffer) {
459 is_storage_buffer = true;
460 }
Chris Forbes47567b72017-06-09 12:09:45 -0700461 type = module->get_def(type.word(3));
462 }
463 }
464
465 switch (type.opcode()) {
466 case spv::OpTypeStruct: {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800467 for (auto insn : module->decoration_inst) {
468 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700469 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800470 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500471 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
472 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
473 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800474 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500475 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
476 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
477 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
478 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800479 }
Chris Forbes47567b72017-06-09 12:09:45 -0700480 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500481 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
482 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
483 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700484 }
485 }
486 }
487
488 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500489 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700490 }
491
492 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500493 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
494 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
495 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700496
Chris Forbes73c00bf2018-06-22 16:28:06 -0700497 case spv::OpTypeSampledImage: {
498 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
499 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
500 auto image_type = module->get_def(type.word(2));
501 auto dim = image_type.word(3);
502 auto sampled = image_type.word(7);
503 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500504 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
505 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700506 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700507 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500508 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
509 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700510
511 case spv::OpTypeImage: {
512 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
513 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
514 auto dim = type.word(3);
515 auto sampled = type.word(7);
516
517 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500518 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
519 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700520 } else if (dim == spv::DimBuffer) {
521 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500522 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
523 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700524 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500525 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
526 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700527 }
528 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500529 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
530 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
531 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700532 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500533 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
534 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700535 }
536 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600537 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700538 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
539 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500540 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700541
542 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
543 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500544 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700545 }
546}
547
Jeff Bolze54ae892018-09-08 12:16:29 -0500548static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700549 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500550 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
551 if (ss.tellp()) ss << ", ";
552 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700553 }
554 return ss.str();
555}
556
sfricke-samsung0065ce02020-12-03 22:46:37 -0800557bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500558 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800559 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 -0500560 return true;
561 }
562 }
563
564 return false;
565}
566
sfricke-samsung0065ce02020-12-03 22:46:37 -0800567bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700568 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800569 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700570 return true;
571 }
572 }
573
574 return false;
575}
576
locke-lunarg63e4daf2020-08-17 17:53:25 -0600577bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
578 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500579 bool skip = false;
580
locke-lunarg63e4daf2020-08-17 17:53:25 -0600581 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800582 switch (stage) {
583 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -0600584 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
585 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
586 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
587 case VK_SHADER_STAGE_MISS_BIT_NV:
588 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
589 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
590 case VK_SHADER_STAGE_TASK_BIT_NV:
591 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -0800592 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -0600593 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -0800594 break;
595 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800596 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
597 kVUID_Core_Shader_FeatureNotEnabled);
Chris Forbes349b3132018-03-07 11:38:08 -0800598 break;
599 default:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800600 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
601 kVUID_Core_Shader_FeatureNotEnabled);
Chris Forbes349b3132018-03-07 11:38:08 -0800602 break;
603 }
604 }
605
Chris Forbes47567b72017-06-09 12:09:45 -0700606 return skip;
607}
608
sfricke-samsung94167ca2021-02-26 04:14:59 -0800609bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
610 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500611 bool skip = false;
612
sfricke-samsung94167ca2021-02-26 04:14:59 -0800613 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
614 if (GroupOperation(insn.opcode()) == true) {
615 // Check the quad operations.
616 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
617 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
618 skip |= RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
619 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages",
620 kVUID_Core_Shader_FeatureNotEnabled);
sfricke-samsung0065ce02020-12-03 22:46:37 -0800621 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800622 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500623
sfricke-samsung94167ca2021-02-26 04:14:59 -0800624 uint32_t scope_type = spv::ScopeMax;
625 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
626 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
627 scope_type = spv::ScopeSubgroup;
628 } else {
629 // "All <id> used for Scope <id> must be of an OpConstant"
630 auto scope_id = module->get_def(insn.word(3));
631 scope_type = scope_id.word(3);
632 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800633
sfricke-samsung94167ca2021-02-26 04:14:59 -0800634 if (scope_type == spv::ScopeSubgroup) {
635 // "Group operations with subgroup scope" must have stage support
636 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
637 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung0065ce02020-12-03 22:46:37 -0800638 "VkPhysicalDeviceSubgroupProperties::supportedStages", kVUID_Core_Shader_ExceedDeviceLimit);
sfricke-samsung94167ca2021-02-26 04:14:59 -0800639 }
640
641 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
642 auto type = module->get_def(insn.word(1));
643
644 if (type.opcode() == spv::OpTypeVector) {
645 // Get the element type
646 type = module->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800647 }
648
sfricke-samsung94167ca2021-02-26 04:14:59 -0800649 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800650 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
651 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500652
sfricke-samsung0065ce02020-12-03 22:46:37 -0800653 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
654 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
655 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
656 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
657 kVUID_Core_Shader_FeatureNotEnabled);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500658 }
659 }
660 }
Jeff Bolzee743412019-06-20 22:24:32 -0500661 }
662
663 return skip;
664}
665
ziga-lunarg2818f492021-08-12 14:30:51 +0200666bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
667 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
668 bool skip = false;
669
670 std::array<uint32_t, 3> work_group_size = src->GetWorkgroupSize(pStage, id_value_map);
671
672 for (uint32_t i = 0; i < 3; ++i) {
673 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
674 const char member = 'x' + static_cast<int8_t>(i);
675 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
676 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
677 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
678 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
679 }
680 }
681 return skip;
682}
683
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600684bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600685 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200686 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
687 pStage->stage == VK_SHADER_STAGE_ALL) {
688 return false;
689 }
690
691 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700692 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200693
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700694 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200695 struct Variable {
696 uint32_t baseTypePtrID;
697 uint32_t ID;
698 uint32_t storageClass;
699 };
700 std::vector<Variable> variables;
701
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700702 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700703 bool is_iso_lines = false;
704 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500705
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700706 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600707
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200708 for (auto insn : *src) {
709 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500710 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200711 case spv::OpDecorate:
712 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500713 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700714 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200715 break;
716 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200717 default:
718 break;
719 }
720 break;
721 // Find all input and output variables
722 case spv::OpVariable: {
723 Variable var = {};
724 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600725 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
726 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700727 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200728 var.baseTypePtrID = insn.word(1);
729 var.ID = insn.word(2);
730 variables.push_back(var);
731 }
732 break;
733 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500734 case spv::OpExecutionMode:
735 if (insn.word(1) == entrypoint.word(2)) {
736 switch (insn.word(2)) {
737 default:
738 break;
739 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700740 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500741 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700742 case spv::ExecutionModeIsolines:
743 is_iso_lines = true;
744 break;
745 case spv::ExecutionModePointMode:
746 is_point_mode = true;
747 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500748 }
749 }
750 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200751 default:
752 break;
753 }
754 }
755
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500756 bool strip_output_array_level =
757 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
758 bool strip_input_array_level =
759 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
760 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
761
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700762 uint32_t num_comp_in = 0, num_comp_out = 0;
763 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600764
sfricke-samsung962cad92021-04-13 00:46:29 -0700765 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
766 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600767
768 // Find max component location used for input variables.
769 for (auto &var : inputs) {
770 int location = var.first.first;
771 int component = var.first.second;
772 interface_var &iv = var.second;
773
774 // Only need to look at the first location, since we use the type's whole size
775 if (iv.offset != 0) {
776 continue;
777 }
778
779 if (iv.is_patch) {
780 continue;
781 }
782
sfricke-samsung962cad92021-04-13 00:46:29 -0700783 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700784 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600785 }
786
787 // Find max component location used for output variables.
788 for (auto &var : outputs) {
789 int location = var.first.first;
790 int component = var.first.second;
791 interface_var &iv = var.second;
792
793 // Only need to look at the first location, since we use the type's whole size
794 if (iv.offset != 0) {
795 continue;
796 }
797
798 if (iv.is_patch) {
799 continue;
800 }
801
sfricke-samsung962cad92021-04-13 00:46:29 -0700802 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700803 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600804 }
805
806 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
807 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700808 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
809 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200810 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500811 // Check if the variable is a patch. Patches can also be members of blocks,
812 // but if they are then the top-level arrayness has already been stripped
813 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700814 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200815
816 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700817 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200818 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700819 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200820 }
821 }
822
823 switch (pStage->stage) {
824 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700825 if (num_comp_out > limits.maxVertexOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600826 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700827 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
828 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
829 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700830 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200831 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700832 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600833 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700834 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
835 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
836 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600837 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200838 break;
839
840 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700841 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600842 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700843 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
844 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
845 "components by %u components",
846 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700847 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200848 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700849 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600850 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600851 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700852 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
853 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
854 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600855 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700856 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600857 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700858 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
859 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
860 "components by %u components",
861 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700862 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200863 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700864 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600865 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600866 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700867 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
868 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
869 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600870 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200871 break;
872
873 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700874 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600875 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700876 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
877 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
878 "components by %u components",
879 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700880 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200881 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700882 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600883 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600884 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700885 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
886 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
887 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600888 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700889 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600890 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700891 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
892 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
893 "components by %u components",
894 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700895 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200896 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700897 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600898 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600899 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700900 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
901 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
902 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600903 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700904 // Portability validation
905 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
906 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600907 skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_Isolines,
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700908 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
909 " is using abstract patch type IsoLines, but this is not supported on this platform");
910 }
911 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600912 skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_PointMode,
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700913 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
914 " is using abstract patch type PointMode, but this is not supported on this platform");
915 }
916 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200917 break;
918
919 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700920 if (num_comp_in > limits.maxGeometryInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600921 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700922 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
923 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
924 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700925 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200926 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700927 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600928 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700929 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
930 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
931 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600932 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700933 if (num_comp_out > limits.maxGeometryOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600934 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700935 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
936 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
937 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700938 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200939 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700940 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600941 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700942 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
943 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
944 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600945 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700946 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600947 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700948 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
949 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
950 "components by %u components",
951 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700952 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500953 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200954 break;
955
956 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700957 if (num_comp_in > limits.maxFragmentInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600958 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700959 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
960 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
961 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700962 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200963 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700964 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600965 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700966 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
967 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
968 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600969 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200970 break;
971
Jeff Bolz148d94e2018-12-13 21:25:56 -0600972 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
973 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
974 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
975 case VK_SHADER_STAGE_MISS_BIT_NV:
976 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
977 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
978 case VK_SHADER_STAGE_TASK_BIT_NV:
979 case VK_SHADER_STAGE_MESH_BIT_NV:
980 break;
981
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200982 default:
983 assert(false); // This should never happen
984 }
985 return skip;
986}
987
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300988bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const {
989 bool skip = false;
990
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300991 // Got through all ImageRead/Write instructions
992 for (auto insn : *src) {
993 switch (insn.opcode()) {
994 case spv::OpImageSparseRead:
995 case spv::OpImageRead: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +0300996 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300997 if (type_def != src->end()) {
Tim Van Pattenffe91322021-07-26 10:20:50 -0600998 const auto dim = type_def.word(3);
999 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1000 // StorageImageReadWithoutFormat Capability was declared.
1001 if (dim != spv::DimSubpassData && type_def.word(8) == spv::ImageFormatUnknown) {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001002 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1003 "shaderStorageImageReadWithoutFormat",
1004 kVUID_Features_shaderStorageImageReadWithoutFormat);
1005 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001006 }
1007 break;
1008 }
1009 case spv::OpImageWrite: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001010 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001011 if (type_def != src->end()) {
1012 if (type_def.word(8) == spv::ImageFormatUnknown) {
1013 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1014 "shaderStorageImageWriteWithoutFormat",
1015 kVUID_Features_shaderStorageImageWriteWithoutFormat);
1016 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001017 }
1018 break;
1019 }
1020
1021 }
1022 }
1023
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001024 // Go through all variables for images and check decorations
1025 for (auto insn : *src) {
1026 if (insn.opcode() != spv::OpVariable)
1027 continue;
1028
1029 uint32_t var = insn.word(2);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001030 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001031 if (type_def == src->end())
1032 continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001033 // Only check if the Image Dim operand is not SubpassData
1034 const auto dim = type_def.word(3);
1035 if (dim == spv::DimSubpassData) continue;
Corentin Wallez91f8b6d2021-07-23 10:11:31 +02001036 // Only check storage images
1037 if (type_def.word(7) != 2) continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001038 if (type_def.word(8) != spv::ImageFormatUnknown) continue;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001039
1040 decoration_set img_decorations = src->get_decorations(var);
1041
1042 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1043 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1044 skip |= LogError(device,
1045 kVUID_Features_shaderStorageImageReadWithoutFormat_NonReadable,
1046 "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32 " "
1047 " without format not marked a NonReadable", var);
1048 }
1049
1050 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1051 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1052 skip |= LogError(device,
1053 kVUID_Features_shaderStorageImageWriteWithoutFormat_NonWritable,
1054 "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32 " "
1055 "without format not marked a NonWritable", var);
1056 }
1057 }
1058
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001059 return skip;
1060}
1061
sfricke-samsungdc96f302020-03-18 20:42:10 -07001062bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1063 bool skip = false;
1064 uint32_t total_resources = 0;
1065
1066 // Only currently testing for graphics and compute pipelines
1067 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1068 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1069 return false;
1070 }
1071
1072 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1073 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
Jeremy Gebben11af9792021-08-20 10:20:09 -06001074 total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
sfricke-samsungdc96f302020-03-18 20:42:10 -07001075 }
1076
1077 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1078 // input from CreatePipeline and CreatePipelineLayout level
1079 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1080 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1081 continue;
1082 }
1083
1084 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1085 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1086 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1087 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1088 // Check only descriptor types listed in maxPerStageResources description in spec
1089 switch (binding->descriptorType) {
1090 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1091 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1092 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1093 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1094 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1095 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1096 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1097 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1098 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1099 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1100 total_resources += binding->descriptorCount;
1101 break;
1102 default:
1103 break;
1104 }
1105 }
1106 }
1107 }
1108
1109 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1110 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1111 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001112 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001113 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1114 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1115 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1116 }
1117
1118 return skip;
1119}
1120
Jeff Bolze4356752019-03-07 11:23:46 -06001121// copy the specialization constant value into buf, if it is present
1122void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1123 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1124
1125 if (spec && spec_id < spec->mapEntryCount) {
1126 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1127 }
1128}
1129
1130// Fill in value with the constant or specialization constant value, if available.
1131// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001132static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001133 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001134 auto type_id = src->get_def(insn.word(1));
1135 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1136 return false;
1137 }
1138 switch (insn.opcode()) {
1139 case spv::OpSpecConstant:
1140 *value = insn.word(3);
1141 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1142 return true;
1143 case spv::OpConstant:
1144 *value = insn.word(3);
1145 return true;
1146 default:
1147 return false;
1148 }
1149}
1150
1151// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001152VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001153 switch (insn.opcode()) {
1154 case spv::OpTypeInt:
1155 switch (insn.word(2)) {
1156 case 8:
1157 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1158 case 16:
1159 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1160 case 32:
1161 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1162 case 64:
1163 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1164 default:
1165 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1166 }
1167 case spv::OpTypeFloat:
1168 switch (insn.word(2)) {
1169 case 16:
1170 return VK_COMPONENT_TYPE_FLOAT16_NV;
1171 case 32:
1172 return VK_COMPONENT_TYPE_FLOAT32_NV;
1173 case 64:
1174 return VK_COMPONENT_TYPE_FLOAT64_NV;
1175 default:
1176 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1177 }
1178 default:
1179 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1180 }
1181}
1182
1183// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1184// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001185bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001186 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001187 bool skip = false;
1188
1189 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001190 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001191 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001192 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001193
1194 struct CoopMatType {
1195 uint32_t scope, rows, cols;
1196 VkComponentTypeNV component_type;
1197 bool all_constant;
1198
1199 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1200
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001201 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001202 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001203 spirv_inst_iter insn = src->get_def(id);
1204 uint32_t component_type_id = insn.word(2);
1205 uint32_t scope_id = insn.word(3);
1206 uint32_t rows_id = insn.word(4);
1207 uint32_t cols_id = insn.word(5);
1208 auto component_type_iter = src->get_def(component_type_id);
1209 auto scope_iter = src->get_def(scope_id);
1210 auto rows_iter = src->get_def(rows_id);
1211 auto cols_iter = src->get_def(cols_id);
1212
1213 all_constant = true;
1214 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1215 all_constant = false;
1216 }
1217 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1218 all_constant = false;
1219 }
1220 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1221 all_constant = false;
1222 }
1223 component_type = GetComponentType(component_type_iter, src);
1224 }
1225 };
1226
1227 bool seen_coopmat_capability = false;
1228
1229 for (auto insn : *src) {
1230 // Whitelist instructions whose result can be a cooperative matrix type, and
1231 // keep track of their types. It would be nice if SPIRV-Headers generated code
1232 // to identify which instructions have a result type and result id. Lacking that,
1233 // this whitelist is based on the set of instructions that
1234 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1235 switch (insn.opcode()) {
1236 case spv::OpLoad:
1237 case spv::OpCooperativeMatrixLoadNV:
1238 case spv::OpCooperativeMatrixMulAddNV:
1239 case spv::OpSNegate:
1240 case spv::OpFNegate:
1241 case spv::OpIAdd:
1242 case spv::OpFAdd:
1243 case spv::OpISub:
1244 case spv::OpFSub:
1245 case spv::OpFDiv:
1246 case spv::OpSDiv:
1247 case spv::OpUDiv:
1248 case spv::OpMatrixTimesScalar:
1249 case spv::OpConstantComposite:
1250 case spv::OpCompositeConstruct:
1251 case spv::OpConvertFToU:
1252 case spv::OpConvertFToS:
1253 case spv::OpConvertSToF:
1254 case spv::OpConvertUToF:
1255 case spv::OpUConvert:
1256 case spv::OpSConvert:
1257 case spv::OpFConvert:
1258 id_to_type_id[insn.word(2)] = insn.word(1);
1259 break;
1260 default:
1261 break;
1262 }
1263
1264 switch (insn.opcode()) {
1265 case spv::OpDecorate:
1266 if (insn.word(2) == spv::DecorationSpecId) {
1267 id_to_spec_id[insn.word(1)] = insn.word(3);
1268 }
1269 break;
1270 case spv::OpCapability:
1271 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1272 seen_coopmat_capability = true;
1273
1274 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001275 skip |= LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001276 pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixSupportedStages,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001277 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1278 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001279 }
1280 }
1281 break;
1282 case spv::OpMemoryModel:
1283 // If the capability isn't enabled, don't bother with the rest of this function.
1284 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1285 if (!seen_coopmat_capability) {
1286 return skip;
1287 }
1288 break;
1289 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001290 CoopMatType m;
1291 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001292
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001293 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001294 // Validate that the type parameters are all supported for one of the
1295 // operands of a cooperative matrix property.
1296 bool valid = false;
1297 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001298 if (cooperative_matrix_properties[i].AType == m.component_type &&
1299 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1300 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001301 valid = true;
1302 break;
1303 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001304 if (cooperative_matrix_properties[i].BType == m.component_type &&
1305 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1306 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001307 valid = true;
1308 break;
1309 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001310 if (cooperative_matrix_properties[i].CType == m.component_type &&
1311 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1312 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001313 valid = true;
1314 break;
1315 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001316 if (cooperative_matrix_properties[i].DType == m.component_type &&
1317 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1318 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001319 valid = true;
1320 break;
1321 }
1322 }
1323 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001324 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001325 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1326 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001327 }
1328 }
1329 break;
1330 }
1331 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001332 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001333 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1334 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1335 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1336 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001337 // Couldn't find type of matrix
1338 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001339 break;
1340 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001341 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1342 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1343 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1344 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001345
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001346 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001347 // Validate that the type parameters are all supported for the same
1348 // cooperative matrix property.
1349 bool valid = false;
1350 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001351 if (cooperative_matrix_properties[i].AType == a.component_type &&
1352 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1353 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001354
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001355 cooperative_matrix_properties[i].BType == b.component_type &&
1356 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1357 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001358
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001359 cooperative_matrix_properties[i].CType == c.component_type &&
1360 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1361 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001362
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001363 cooperative_matrix_properties[i].DType == d.component_type &&
1364 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1365 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001366 valid = true;
1367 break;
1368 }
1369 }
1370 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001371 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001372 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1373 "VkCooperativeMatrixPropertiesNV",
1374 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001375 }
1376 }
1377 break;
1378 }
1379 default:
1380 break;
1381 }
1382 }
1383
1384 return skip;
1385}
1386
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001387bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1388 const PIPELINE_STATE *pipeline) const {
1389 bool skip = false;
1390
1391 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1392 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1393 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1394 for (auto insn : *src) {
1395 switch (insn.opcode()) {
1396 case spv::OpCapability:
1397 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1398 auto subpass_flags =
1399 (pipeline->rp_state == nullptr)
1400 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001401 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001402 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1403 skip |=
1404 LogError(pipeline->pipeline(), kVUID_Core_Shader_ResolveQCOM_InvalidCapability,
1405 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1406 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1407 }
1408 }
1409 break;
1410 default:
1411 break;
1412 }
1413 }
1414 }
1415
1416 return skip;
1417}
1418
sfricke-samsung58b84352021-07-31 21:41:04 -07001419bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *src) const {
1420 bool skip = false;
1421
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001422 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001423 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001424
sfricke-samsungf5042b12021-08-05 01:09:40 -07001425 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1426 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1427
1428 const bool valid_storage_buffer_float = (
1429 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1430 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1431 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1432 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1433 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1434 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1435 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1436 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1437 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1438
1439 const bool valid_workgroup_float = (
1440 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1441 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1442 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1443 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1444 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1445 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1446 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1447 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1448 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1449
1450 const bool valid_image_float = (
1451 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1452 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1453 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1454
1455 const bool valid_16_float = (
1456 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1457 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1458 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1459 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1460 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1461 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1462
1463 const bool valid_32_float = (
1464 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1465 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1466 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1467 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1468 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1469 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1470 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1471 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1472 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1473
1474 const bool valid_64_float = (
1475 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1476 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1477 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1478 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1479 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1480 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1481 // clang-format on
1482
sfricke-samsung58b84352021-07-31 21:41:04 -07001483 for (auto &atomic_inst : src->atomic_inst) {
1484 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungf5042b12021-08-05 01:09:40 -07001485 const uint32_t opcode = src->at(atomic_inst.first).opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001486
1487 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
1488 // Validate 64-bit atomics
1489 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1490 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
1491 skip |= LogError(
1492 device, kVUID_Core_Shader_AtomicFeature,
1493 "%s: Can't use 64-bit int atomics operations with %s storage class without shaderBufferInt64Atomics enabled.",
1494 report_data->FormatHandle(src->vk_shader_module()).c_str(), StorageClassName(atomic.storage_class));
1495 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1496 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
1497 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1498 "%s: Can't use 64-bit int atomics operations with Workgroup storage class without "
1499 "shaderSharedInt64Atomics enabled.",
1500 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001501 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
1502 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1503 "%s: Can't use 64-bit int atomics operations with Image storage class without "
1504 "shaderImageInt64Atomics enabled.",
1505 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001506 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001507 } else if (atomic.type == spv::OpTypeFloat) {
1508 // Validate Floats
1509 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1510 if (valid_storage_buffer_float == false) {
1511 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1512 "%s: Can't use float atomics operations with StorageBuffer storage class without "
1513 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1514 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1515 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1516 "shaderBufferFloat64AtomicMinMax enabled.",
1517 report_data->FormatHandle(src->vk_shader_module()).c_str());
1518 } else if (opcode == spv::OpAtomicFAddEXT) {
1519 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1520 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1521 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with "
1522 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
1523 report_data->FormatHandle(src->vk_shader_module()).c_str());
1524 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1525 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1526 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with "
1527 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
1528 report_data->FormatHandle(src->vk_shader_module()).c_str());
1529 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1530 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1531 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with "
1532 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
1533 report_data->FormatHandle(src->vk_shader_module()).c_str());
1534 }
1535 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1536 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
1537 skip |= LogError(
1538 device, kVUID_Core_Shader_AtomicFeature,
1539 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1540 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
1541 report_data->FormatHandle(src->vk_shader_module()).c_str());
1542 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
1543 skip |= LogError(
1544 device, kVUID_Core_Shader_AtomicFeature,
1545 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1546 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
1547 report_data->FormatHandle(src->vk_shader_module()).c_str());
1548 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
1549 skip |= LogError(
1550 device, kVUID_Core_Shader_AtomicFeature,
1551 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1552 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
1553 report_data->FormatHandle(src->vk_shader_module()).c_str());
1554 }
1555 } else {
1556 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1557 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
1558 skip |= LogError(
1559 device, kVUID_Core_Shader_AtomicFeature,
1560 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1561 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
1562 report_data->FormatHandle(src->vk_shader_module()).c_str());
1563 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
1564 skip |= LogError(
1565 device, kVUID_Core_Shader_AtomicFeature,
1566 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1567 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
1568 report_data->FormatHandle(src->vk_shader_module()).c_str());
1569 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
1570 skip |= LogError(
1571 device, kVUID_Core_Shader_AtomicFeature,
1572 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1573 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
1574 report_data->FormatHandle(src->vk_shader_module()).c_str());
1575 }
1576 }
1577 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1578 if (valid_workgroup_float == false) {
1579 skip |= LogError(
1580 device, kVUID_Core_Shader_AtomicFeature,
1581 "%s: Can't use float atomics operations with Workgroup storage class without shaderSharedFloat32Atomics or "
1582 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1583 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1584 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1585 report_data->FormatHandle(src->vk_shader_module()).c_str());
1586 } else if (opcode == spv::OpAtomicFAddEXT) {
1587 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1588 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1589 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1590 "storage class without shaderSharedFloat16AtomicAdd enabled.",
1591 report_data->FormatHandle(src->vk_shader_module()).c_str());
1592 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1593 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1594 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1595 "storage class without shaderSharedFloat32AtomicAdd enabled.",
1596 report_data->FormatHandle(src->vk_shader_module()).c_str());
1597 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1598 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1599 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1600 "storage class without shaderSharedFloat64AtomicAdd enabled.",
1601 report_data->FormatHandle(src->vk_shader_module()).c_str());
1602 }
1603 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1604 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
1605 skip |= LogError(
1606 device, kVUID_Core_Shader_AtomicFeature,
1607 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1608 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
1609 report_data->FormatHandle(src->vk_shader_module()).c_str());
1610 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
1611 skip |= LogError(
1612 device, kVUID_Core_Shader_AtomicFeature,
1613 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1614 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
1615 report_data->FormatHandle(src->vk_shader_module()).c_str());
1616 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
1617 skip |= LogError(
1618 device, kVUID_Core_Shader_AtomicFeature,
1619 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1620 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
1621 report_data->FormatHandle(src->vk_shader_module()).c_str());
1622 }
1623 } else {
1624 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1625 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
1626 skip |= LogError(
1627 device, kVUID_Core_Shader_AtomicFeature,
1628 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1629 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat16Atomics enabled.",
1630 report_data->FormatHandle(src->vk_shader_module()).c_str());
1631 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
1632 skip |= LogError(
1633 device, kVUID_Core_Shader_AtomicFeature,
1634 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1635 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat32Atomics enabled.",
1636 report_data->FormatHandle(src->vk_shader_module()).c_str());
1637 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
1638 skip |= LogError(
1639 device, kVUID_Core_Shader_AtomicFeature,
1640 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1641 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat64Atomics enabled.",
1642 report_data->FormatHandle(src->vk_shader_module()).c_str());
1643 }
1644 }
1645 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
1646 skip |=
1647 LogError(device, kVUID_Core_Shader_AtomicFeature,
1648 "%s: Can't use float atomics operations with Image storage class without shaderImageFloat32Atomics or "
1649 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
1650 report_data->FormatHandle(src->vk_shader_module()).c_str());
1651 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
1652 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1653 "%s: Can't use 16-bit float atomics operations without shaderBufferFloat16Atomics, "
1654 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1655 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
1656 report_data->FormatHandle(src->vk_shader_module()).c_str());
1657 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
1658 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1659 "%s: Can't use 32-bit float atomics operations without shaderBufferFloat32AtomicMinMax, "
1660 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1661 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1662 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1663 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
1664 report_data->FormatHandle(src->vk_shader_module()).c_str());
1665 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
1666 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1667 "%s: Can't use 64-bit float atomics operations without shaderBufferFloat64AtomicMinMax, "
1668 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1669 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
1670 report_data->FormatHandle(src->vk_shader_module()).c_str());
1671 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001672 }
1673 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001674 return skip;
1675}
1676
John Zulaufac4c6e12019-07-01 16:05:58 -06001677bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001678 auto entrypoint_id = entrypoint.word(2);
1679
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001680 // The first denorm execution mode encountered, along with its bit width.
1681 // Used to check if SeparateDenormSettings is respected.
1682 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001683
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001684 // The first rounding mode encountered, along with its bit width.
1685 // Used to check if SeparateRoundingModeSettings is respected.
1686 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001687
1688 bool skip = false;
1689
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001690 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001691 uint32_t invocations = 0;
1692
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001693 auto it = src->execution_mode_inst.find(entrypoint_id);
1694 if (it != src->execution_mode_inst.end()) {
1695 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001696 auto mode = insn.word(2);
1697 switch (mode) {
1698 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1699 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001700 if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) ||
1701 (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) ||
1702 (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001703 skip |= LogError(
1704 device, kVUID_Core_Shader_FeatureNotEnabled,
1705 "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device",
1706 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001707 }
1708 break;
1709 }
1710
1711 case spv::ExecutionModeDenormPreserve: {
1712 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001713 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) ||
1714 (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) ||
1715 (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001716 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1717 "Shader requires DenormPreserve for bit width %d but it is not enabled on the device",
1718 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001719 }
1720
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001721 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1722 // Register the first denorm execution mode found
1723 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001724 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001725 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001726 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001727 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001728 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1729 "Shader uses different denorm execution modes for 16 and 64-bit but "
1730 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001731 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001732 }
1733 break;
1734
Mike Schuchardt2df08912020-12-15 16:28:09 -08001735 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001736 break;
1737
Mike Schuchardt2df08912020-12-15 16:28:09 -08001738 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001739 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1740 "Shader uses different denorm execution modes for different bit widths but "
1741 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001742 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001743 break;
1744
1745 default:
1746 break;
1747 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001748 }
1749 break;
1750 }
1751
1752 case spv::ExecutionModeDenormFlushToZero: {
1753 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001754 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) ||
1755 (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) ||
1756 (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001757 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1758 "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device",
1759 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001760 }
1761
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001762 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1763 // Register the first denorm execution mode found
1764 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001765 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001766 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001767 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001768 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001769 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1770 "Shader uses different denorm execution modes for 16 and 64-bit but "
1771 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001772 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001773 }
1774 break;
1775
Mike Schuchardt2df08912020-12-15 16:28:09 -08001776 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001777 break;
1778
Mike Schuchardt2df08912020-12-15 16:28:09 -08001779 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001780 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1781 "Shader uses different denorm execution modes for different bit widths but "
1782 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001783 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001784 break;
1785
1786 default:
1787 break;
1788 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001789 }
1790 break;
1791 }
1792
1793 case spv::ExecutionModeRoundingModeRTE: {
1794 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001795 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) ||
1796 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) ||
1797 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001798 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1799 "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device",
1800 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001801 }
1802
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001803 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1804 // Register the first rounding mode found
1805 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001806 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001807 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001808 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001809 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001810 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1811 "Shader uses different rounding modes for 16 and 64-bit but "
1812 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001813 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001814 }
1815 break;
1816
Mike Schuchardt2df08912020-12-15 16:28:09 -08001817 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001818 break;
1819
Mike Schuchardt2df08912020-12-15 16:28:09 -08001820 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001821 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1822 "Shader uses different rounding modes for different bit widths but "
1823 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001824 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001825 break;
1826
1827 default:
1828 break;
1829 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001830 }
1831 break;
1832 }
1833
1834 case spv::ExecutionModeRoundingModeRTZ: {
1835 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001836 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) ||
1837 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) ||
1838 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001839 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1840 "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device",
1841 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001842 }
1843
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001844 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1845 // Register the first rounding mode found
1846 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001847 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001848 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001849 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001850 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001851 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1852 "Shader uses different rounding modes for 16 and 64-bit but "
1853 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001854 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001855 }
1856 break;
1857
Mike Schuchardt2df08912020-12-15 16:28:09 -08001858 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001859 break;
1860
Mike Schuchardt2df08912020-12-15 16:28:09 -08001861 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001862 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1863 "Shader uses different rounding modes for different bit widths but "
1864 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001865 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001866 break;
1867
1868 default:
1869 break;
1870 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001871 }
1872 break;
1873 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001874
1875 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001876 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001877 break;
1878 }
1879
1880 case spv::ExecutionModeInvocations: {
1881 invocations = insn.word(3);
1882 break;
1883 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001884 }
1885 }
1886 }
1887
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001888 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001889 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001890 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
1891 "Geometry shader entry point must have an OpExecutionMode instruction that "
1892 "specifies a maximum output vertex count that is greater than 0 and less "
1893 "than or equal to maxGeometryOutputVertices. "
1894 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001895 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001896 }
1897
1898 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001899 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
1900 "Geometry shader entry point must have an OpExecutionMode instruction that "
1901 "specifies an invocation count that is greater than 0 and less "
1902 "than or equal to maxGeometryShaderInvocations. "
1903 "Invocations=%d, maxGeometryShaderInvocations=%d",
1904 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001905 }
1906 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001907 return skip;
1908}
1909
Chris Forbes47567b72017-06-09 12:09:45 -07001910// For given pipelineLayout verify that the set_layout_node at slot.first
1911// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06001912static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001913 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07001914 if (!pipelineLayout) return nullptr;
1915
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001916 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07001917
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001918 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07001919}
1920
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001921// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
1922// o If there is only a vertex shader : gl_PointSize must be written when using points
1923// o If there is a geometry or tessellation shader:
1924// - If shaderTessellationAndGeometryPointSize feature is enabled:
1925// * gl_PointSize must be written in the final geometry stage
1926// - If shaderTessellationAndGeometryPointSize feature is disabled:
1927// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001928bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06001929 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001930 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
1931 return false;
1932 }
1933
1934 bool pointsize_written = false;
1935 bool skip = false;
1936
1937 // Search for PointSize built-in decorations
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001938 for (auto set : src->builtin_decoration_list) {
1939 auto insn = src->at(set.offset);
1940 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001941 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001942 if (pointsize_written) {
1943 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001944 }
1945 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001946 }
1947
1948 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001949 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001950 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001951 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001952 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
1953 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001954 }
1955 } else if (!pointsize_written) {
1956 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001957 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001958 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
1959 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001960 }
1961 return skip;
1962}
John Zulauf14c355b2019-06-27 16:09:37 -06001963
Tobias Hector6663c9b2020-11-05 10:18:02 +00001964bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
1965 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
1966 bool primitiverate_written = false;
1967 bool viewportindex_written = false;
1968 bool viewportmask_written = false;
1969 bool skip = false;
1970
1971 // Check if the primitive shading rate is written
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001972 for (auto set : src->builtin_decoration_list) {
1973 auto insn = src->at(set.offset);
1974 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001975 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001976 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001977 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001978 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001979 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00001980 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001981 if (primitiverate_written && viewportindex_written && viewportmask_written) {
1982 break;
1983 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00001984 }
1985
Tony-LunarGd44844c2021-01-22 13:24:37 -07001986 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06001987 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00001988 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06001989 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001990 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001991 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
1992 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
1993 "multiple viewports "
1994 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
1995 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00001996 }
1997
1998 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001999 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002000 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2001 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2002 "ViewportIndex built-ins,"
2003 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2004 string_VkShaderStageFlagBits(stage));
2005 }
2006
2007 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002008 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002009 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2010 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2011 "ViewportMaskNV built-ins,"
2012 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2013 string_VkShaderStageFlagBits(stage));
2014 }
2015 }
2016 return skip;
2017}
2018
sfricke-samsung486a51e2021-01-02 00:10:15 -08002019// Validate runtime usage of various opcodes that depends on what Vulkan properties or features are exposed
sfricke-samsung94167ca2021-02-26 04:14:59 -08002020bool CoreChecks::ValidatePropertiesAndFeatures(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002021 bool skip = false;
2022
sfricke-samsung94167ca2021-02-26 04:14:59 -08002023 switch (insn.opcode()) {
2024 case spv::OpReadClockKHR: {
2025 auto scope_id = module->get_def(insn.word(3));
2026 auto scope_type = scope_id.word(3);
2027 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002028 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung94167ca2021-02-26 04:14:59 -08002029 skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderSubgroupClock",
2030 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002031 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002032 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung94167ca2021-02-26 04:14:59 -08002033 skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderDeviceClock",
2034 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002035 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002036 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002037 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002038 }
2039 }
2040 return skip;
2041}
2042
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002043bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2044 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002045 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002046 const auto *pStage = stage_state.create_info;
2047 const auto *module = stage_state.module.get();
2048 const auto &entrypoint = stage_state.entrypoint;
John Zulauf14c355b2019-06-27 16:09:37 -06002049 // Check the module
2050 if (!module->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002051 skip |= LogError(
2052 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2053 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002054 }
2055
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002056 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2057 // specializations should be applied and validated.
2058 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2059 pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) {
2060 // Gather the specialization-constant values.
2061 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002062 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002063 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 -06002064 id_value_map.reserve(specialization_info->mapEntryCount);
2065 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2066 auto const &map_entry = specialization_info->pMapEntries[i];
sfricke-samsung033b0262021-07-09 00:53:06 -07002067 auto itr = module->spec_const_map.find(map_entry.constantID);
2068 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2069 // behavior of the pipeline."
2070 if (itr != module->spec_const_map.cend()) {
2071 // Make sure map_entry.size matches the spec constant's size
2072 uint32_t spec_const_size = decoration_set::kInvalidValue;
2073 const auto def_ins = module->get_def(itr->second);
2074 const auto type_ins = module->get_def(def_ins.word(1));
2075 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2076 switch (type_ins.opcode()) {
2077 case spv::OpTypeBool:
2078 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2079 spec_const_size = sizeof(VkBool32);
2080 break;
2081 case spv::OpTypeInt:
2082 case spv::OpTypeFloat:
2083 spec_const_size = type_ins.word(2) / 8;
2084 break;
2085 default:
2086 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2087 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2088 break;
2089 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002090
sfricke-samsung033b0262021-07-09 00:53:06 -07002091 if (map_entry.size != spec_const_size) {
2092 skip |=
2093 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002094 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2095 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2096 map_entry.constantID, i, map_entry.size,
2097 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002098 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002099 }
2100
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002101 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002102 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2103 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2104 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2105 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2106 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2107
2108 std::copy(start_in_p, end_in_p, out_p);
2109 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002110 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002111 }
2112
2113 // Apply the specialization-constant values and revalidate the shader module.
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002114 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002115 spvtools::Optimizer optimizer(spirv_environment);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002116 spvtools::MessageConsumer consumer = [&skip, &module, &stage_state, this](spv_message_level_t level, const char *source,
2117 const spv_position_t &position,
2118 const char *message) {
2119 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2120 "%s does not contain valid spirv for stage %s. %s",
2121 report_data->FormatHandle(module->vk_shader_module()).c_str(),
2122 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002123 };
2124 optimizer.SetMessageConsumer(consumer);
2125 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2126 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2127 std::vector<uint32_t> specialized_spirv;
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002128 auto const optimized = optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002129 assert(optimized == true);
2130
2131 if (optimized) {
2132 spv_context ctx = spvContextCreate(spirv_environment);
2133 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2134 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002135 spvtools::ValidatorOptions options;
2136 AdjustValidatorOptions(device_extensions, enabled_features, options);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002137 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2138 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002139 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002140 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002141 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002142 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002143 }
2144
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002145 spvDiagnosticDestroy(diag);
2146 spvContextDestroy(ctx);
2147 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002148
2149 skip |= ValidateWorkgroupSize(module, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002150 }
2151
John Zulauf14c355b2019-06-27 16:09:37 -06002152 // Check the entrypoint
2153 if (entrypoint == module->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002154 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2155 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002156 }
2157 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2158
2159 // Mark accessible ids
2160 auto &accessible_ids = stage_state.accessible_ids;
2161
Chris Forbes47567b72017-06-09 12:09:45 -07002162 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002163
sfricke-samsung94167ca2021-02-26 04:14:59 -08002164 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2165 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002166 uint32_t total_shared_size = 0;
sfricke-samsung94167ca2021-02-26 04:14:59 -08002167 for (auto insn : *module) {
2168 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
2169 skip |= ValidatePropertiesAndFeatures(module, insn);
2170 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02002171 total_shared_size += module->CalcComputeSharedMemory(pStage->stage, insn);
2172 }
2173
2174 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2175 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002176 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002177 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002178 }
2179
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002180 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2181 stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002182 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03002183 skip |= ValidateShaderStorageImageFormats(module);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002184 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsung58b84352021-07-31 21:41:04 -07002185 skip |= ValidateAtomicsTypes(module);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002186 skip |= ValidateExecutionModes(module, entrypoint);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002187 skip |= ValidateSpecializations(pStage);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002188 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002189 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002190 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07002191 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002192 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
2193 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
2194 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002195 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
2196 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
2197 }
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002198 if (device_extensions.vk_qcom_render_pass_shader_resolve != kNotEnabled) {
2199 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
2200 }
Chris Forbes47567b72017-06-09 12:09:45 -07002201
sfricke-samsung7699b912021-04-12 23:01:51 -07002202 // "layout must be consistent with the layout of the * shader"
2203 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002204 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002205 switch (pipeline->create_info.graphics.sType) {
2206 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2207 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2208 break;
2209 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2210 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2211 break;
2212 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2213 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2214 break;
2215 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2216 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2217 break;
2218 default:
2219 assert(false);
2220 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002221 }
2222
sfricke-samsung7699b912021-04-12 23:01:51 -07002223 // Validate Push Constants use
2224 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
2225
Chris Forbes47567b72017-06-09 12:09:45 -07002226 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002227 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002228 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002229 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002230 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002231 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2232 std::set<uint32_t> descriptor_types =
2233 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002234
2235 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002236 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002237 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002238 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002239 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002240 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002241 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2242 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002243 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2244 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002245 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002246 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2247 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002248 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002249 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002250 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002251 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002252 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002253 }
2254 }
2255
2256 // Validate use of input attachments against subpass structure
2257 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002258 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002259
Petr Krause91f7a12017-12-14 20:57:36 +01002260 auto rpci = pipeline->rp_state->createInfo.ptr();
Jeremy Gebben11af9792021-08-20 10:20:09 -06002261 auto subpass = pipeline->create_info.graphics.subpass;
Chris Forbes47567b72017-06-09 12:09:45 -07002262
2263 for (auto use : input_attachment_uses) {
2264 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2265 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07002266 ? input_attachments[use.first].attachment
2267 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002268
2269 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002270 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2271 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsung962cad92021-04-13 00:46:29 -07002272 } else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002273 skip |=
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002274 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2275 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
sfricke-samsung962cad92021-04-13 00:46:29 -07002276 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002277 }
2278 }
2279 }
Lockeaa8fdc02019-04-02 11:59:20 -06002280 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08002281 skip |= ValidateComputeWorkGroupSizes(module, entrypoint);
Lockeaa8fdc02019-04-02 11:59:20 -06002282 }
Chris Forbes47567b72017-06-09 12:09:45 -07002283 return skip;
2284}
2285
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002286bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2287 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2288 spirv_inst_iter consumer_entrypoint,
2289 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002290 bool skip = false;
2291
2292 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002293 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2294 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002295
2296 auto a_it = outputs.begin();
2297 auto b_it = inputs.begin();
2298
ziga-lunarg8346fe82021-08-22 17:30:50 +02002299 uint32_t a_component = 0;
2300 uint32_t b_component = 0;
2301
Chris Forbes47567b72017-06-09 12:09:45 -07002302 // Maps sorted by key (location); walk them together to find mismatches
2303 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2304 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2305 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2306 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2307 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2308
ziga-lunarg8346fe82021-08-22 17:30:50 +02002309 a_first.second += a_component;
2310 b_first.second += b_component;
2311
2312 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2313 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2314 assert(a_at_end || a_component < a_length);
2315 assert(b_at_end || b_component < b_length);
2316
Chris Forbes47567b72017-06-09 12:09:45 -07002317 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002318 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002319 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2320 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2321 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2322 a_it++;
2323 a_component = 0;
2324 } else {
2325 a_component++;
2326 }
Chris Forbes47567b72017-06-09 12:09:45 -07002327 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002328 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002329 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2330 b_first.first, b_first.second, producer_stage->name);
2331 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2332 b_it++;
2333 b_component = 0;
2334 } else {
2335 b_component++;
2336 }
Chris Forbes47567b72017-06-09 12:09:45 -07002337 } else {
2338 // subtleties of arrayed interfaces:
2339 // - if is_patch, then the member is not arrayed, even though the interface may be.
2340 // - if is_block_member, then the extra array level of an arrayed interface is not
2341 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002342 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002343 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002344 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002345 producer->DescribeType(a_it->second.type_id).c_str(),
2346 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002347 a_it++;
2348 b_it++;
2349 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002350 }
2351 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002352 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002353 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2354 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2355 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002356 }
2357 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002358 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002359 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2360 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002361 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002362 uint32_t a_remaining = a_length - a_component;
2363 uint32_t b_remaining = b_length - b_component;
2364 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2365 a_it++;
2366 b_it++;
2367 a_component = 0;
2368 b_component = 0;
2369 } else if (a_remaining > b_remaining) { // a has more components remaining
2370 a_component += b_remaining;
2371 b_component = 0;
2372 b_it++;
2373 } else if (b_remaining > a_remaining) { // b has more components remaining
2374 b_component += a_remaining;
2375 a_component = 0;
2376 a_it++;
2377 }
Chris Forbes47567b72017-06-09 12:09:45 -07002378 }
2379 }
2380
Ari Suonpaa696b3432019-03-11 14:02:57 +02002381 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002382 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2383 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002384
2385 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2386 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002387 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002388 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002389 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2390 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002391 } else {
2392 auto it_producer = builtins_producer.begin();
2393 auto it_consumer = builtins_consumer.begin();
2394 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2395 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002396 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002397 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2398 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002399 break;
2400 }
2401 it_producer++;
2402 it_consumer++;
2403 }
2404 }
2405 }
2406 }
2407
Chris Forbes47567b72017-06-09 12:09:45 -07002408 return skip;
2409}
2410
John Zulauf14c355b2019-06-27 16:09:37 -06002411static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002412 uint32_t stage_mask = 0;
2413 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2414 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2415 stage_mask |= pCreateInfo->pStages[i].stage;
2416 }
2417 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002418 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2419 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2420 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002421 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2422 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2423 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2424 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2425 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002426 }
2427 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002428 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002429}
2430
Chris Forbes47567b72017-06-09 12:09:45 -07002431// Validate that the shaders used by the given pipeline and store the active_slots
2432// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002433bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002434 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002435
Chris Forbes47567b72017-06-09 12:09:45 -07002436 bool skip = false;
2437
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002438 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002439
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002440 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
2441 for (auto &stage : pipeline->stage_state) {
2442 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
2443 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
2444 vertex_stage = &stage;
2445 }
2446 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
2447 fragment_stage = &stage;
2448 }
Chris Forbes47567b72017-06-09 12:09:45 -07002449 }
2450
2451 // if the shader stages are no good individually, cross-stage validation is pointless.
2452 if (skip) return true;
2453
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002454 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002455
2456 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002457 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002458 }
2459
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002460 if (vertex_stage && vertex_stage->module->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
2461 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07002462 }
2463
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002464 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
2465 const auto &producer = pipeline->stage_state[i - 1];
2466 const auto &consumer = pipeline->stage_state[i];
2467 assert(producer.module);
2468 if (&producer == fragment_stage) {
2469 break;
2470 }
2471 if (consumer.module) {
2472 if (consumer.module->has_valid_spirv && producer.module->has_valid_spirv) {
2473 auto producer_id = GetShaderStageId(producer.stage_flag);
2474 auto consumer_id = GetShaderStageId(consumer.stage_flag);
2475 skip |=
2476 ValidateInterfaceBetweenStages(producer.module.get(), producer.entrypoint, &shader_stage_attribs[producer_id],
2477 consumer.module.get(), consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002478 }
Chris Forbes47567b72017-06-09 12:09:45 -07002479
Chris Forbes47567b72017-06-09 12:09:45 -07002480 }
2481 }
2482
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002483 if (fragment_stage && fragment_stage->module->has_valid_spirv) {
2484 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002485 create_info->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07002486 }
2487
2488 return skip;
2489}
2490
Tony-LunarGb2ded512021-02-02 16:03:30 -07002491void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002492 if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports ||
2493 !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) {
2494 return;
2495 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002496
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002497 for (auto &stage : pipeline_state->stage_state) {
2498 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2499 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002500 bool primitiverate_written = false;
Tobias Hector6663c9b2020-11-05 10:18:02 +00002501
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002502 for (auto set : stage.module->builtin_decoration_list) {
2503 auto insn = stage.module->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002504 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002505 primitiverate_written = stage.module->IsBuiltInWritten(insn, stage.entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002506 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002507 if (primitiverate_written) {
2508 break;
2509 }
Tony-LunarGb2ded512021-02-02 16:03:30 -07002510 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002511
Tony-LunarGb2ded512021-02-02 16:03:30 -07002512 if (primitiverate_written) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002513 pipeline_state->wrote_primitive_shading_rate.insert(stage.stage_flag);
Tony-LunarGb2ded512021-02-02 16:03:30 -07002514 }
2515 }
2516 }
2517}
2518
2519bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2520 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002521 bool skip = false;
2522
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002523 for (auto &stage : pipeline->stage_state) {
2524 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2525 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002526 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2527 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002528 if (pipeline->wrote_primitive_shading_rate.find(stage.stage_flag) != pipeline->wrote_primitive_shading_rate.end()) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002529 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002530 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002531 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2532 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2533 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002534 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002535 }
2536 }
2537 }
2538 }
2539
2540 return skip;
2541}
2542
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002543bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002544 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07002545}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002546
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002547uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2548 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002549 const auto &create_info = pipeline->create_info.raytracing;
2550 const auto *stages = create_info.ptr()->pStages;
2551 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002552 if (stages[stage_index].stage == stageBit) {
2553 total++;
2554 }
2555 }
2556
Jeremy Gebben11af9792021-08-20 10:20:09 -06002557 if (create_info.pLibraryInfo) {
2558 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2559 const PIPELINE_STATE *library_pipeline = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002560 total += CalcShaderStageCount(library_pipeline, stageBit);
2561 }
2562 }
2563
2564 return total;
2565}
2566
sourav parmarcd5fb182020-07-17 12:58:44 -07002567bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06002568 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04002569
Jeremy Gebben11af9792021-08-20 10:20:09 -06002570 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002571 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002572 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
2573 skip |=
2574 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
2575 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
2576 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
2577 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002578 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002579 if (create_info.pLibraryInfo) {
2580 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2581 const PIPELINE_STATE *library_pipelinestate = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
2582 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
2583 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002584 skip |= LogError(
2585 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
2586 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
2587 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002588 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07002589 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002590 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
2591 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
2592 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
2593 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002594 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
2595 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
2596 "member must have been created with values of the maxPipelineRayPayloadSize and "
2597 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
2598 }
2599 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002600 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002601 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
2602 "vkCreateRayTracingPipelinesKHR: If flags includes "
2603 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
2604 "the pLibraries member of libraries must have been created with the "
2605 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
2606 }
sourav parmar83c31b12020-05-06 12:30:54 -07002607 }
2608 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002609 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002610 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002611 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
2612 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
2613 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002614 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002615 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002616 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002617 const auto *stages = create_info.ptr()->pStages;
2618 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04002619
Jeremy Gebben11af9792021-08-20 10:20:09 -06002620 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002621 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04002622 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002623
Jeremy Gebben11af9792021-08-20 10:20:09 -06002624 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002625 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
2626 if (raygen_stages_count == 0) {
2627 skip |= LogError(
2628 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07002629 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002630 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
2631 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002632 }
2633
Jeremy Gebben11af9792021-08-20 10:20:09 -06002634 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04002635 const auto &group = groups[group_index];
2636
2637 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002638 if (group.generalShader >= create_info.stageCount ||
Jason Macnak15f95e82019-08-21 21:52:02 -04002639 (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV &&
2640 stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV &&
2641 stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002642 skip |= LogError(device,
2643 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
2644 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
2645 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002646 }
2647 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
2648 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002649 skip |= LogError(device,
2650 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
2651 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
2652 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002653 }
2654 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002655 if (group.intersectionShader >= create_info.stageCount ||
Jason Macnak15f95e82019-08-21 21:52:02 -04002656 stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002657 skip |= LogError(device,
2658 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
2659 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
2660 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002661 }
2662 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2663 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002664 skip |= LogError(device,
2665 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
2666 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
2667 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002668 }
2669 }
2670
2671 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
2672 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002673 if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= create_info.stageCount ||
Jason Macnak15f95e82019-08-21 21:52:02 -04002674 stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002675 skip |= LogError(device,
2676 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
2677 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
2678 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002679 }
2680 if (group.closestHitShader != VK_SHADER_UNUSED_NV &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002681 (group.closestHitShader >= create_info.stageCount ||
Jason Macnak15f95e82019-08-21 21:52:02 -04002682 stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002683 skip |= LogError(device,
2684 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
2685 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
2686 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002687 }
2688 }
John Zulaufe4474e72019-07-01 17:28:27 -06002689 }
2690 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002691}
2692
Dave Houltona9df0ce2018-02-07 10:51:23 -07002693uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07002694
Dave Houltona9df0ce2018-02-07 10:51:23 -07002695static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002696 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06002697 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06002698 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002699 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002700 return nullptr;
2701}
2702
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002703bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002704 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002705 bool skip = false;
2706 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002707
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06002708 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002709 return false;
2710 }
2711
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002712 auto have_glsl_shader = device_extensions.vk_nv_glsl_shader;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002713
2714 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002715 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
2716 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
2717 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002718 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07002719 auto cache = GetValidationCacheInfo(pCreateInfo);
2720 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07002721 // If app isn't using a shader validation cache, use the default one from CoreChecks
2722 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002723 if (cache) {
2724 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002725 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07002726 }
2727
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002728 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
2729 // the default values will be used during validation.
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002730 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
Dave Houlton0ea2d012018-06-21 14:00:26 -06002731 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002732 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07002733 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002734 spvtools::ValidatorOptions options;
2735 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06002736 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002737 if (spv_valid != SPV_SUCCESS) {
2738 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002739 if (spv_valid == SPV_WARNING) {
2740 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2741 diag && diag->error ? diag->error : "(no error text)");
2742 } else {
2743 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2744 diag && diag->error ? diag->error : "(no error text)");
2745 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002746 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002747 } else {
2748 if (cache) {
2749 cache->Insert(hash);
2750 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002751 }
2752
2753 spvDiagnosticDestroy(diag);
2754 spvContextDestroy(ctx);
2755 }
2756
Chris Forbes4ae55b32017-06-09 14:42:56 -07002757 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07002758}
2759
sfricke-samsung8a7341a2021-02-28 07:30:21 -08002760bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint) const {
Lockeaa8fdc02019-04-02 11:59:20 -06002761 bool skip = false;
2762 uint32_t local_size_x = 0;
2763 uint32_t local_size_y = 0;
2764 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07002765 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06002766 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002767 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002768 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002769 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002770 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06002771 }
2772 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002773 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002774 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002775 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002776 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06002777 }
2778 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002779 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002780 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002781 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002782 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06002783 }
2784
2785 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
2786 uint64_t invocations = local_size_x * local_size_y;
2787 // Prevent overflow.
2788 bool fail = false;
2789 if (invocations > UINT32_MAX || invocations > limit) {
2790 fail = true;
2791 }
2792 if (!fail) {
2793 invocations *= local_size_z;
2794 if (invocations > UINT32_MAX || invocations > limit) {
2795 fail = true;
2796 }
2797 }
2798 if (fail) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002799 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002800 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
2801 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002802 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, local_size_y, local_size_z,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002803 limit);
Lockeaa8fdc02019-04-02 11:59:20 -06002804 }
2805 }
2806 return skip;
2807}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002808
2809spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
2810 if (api_version >= VK_API_VERSION_1_2) {
2811 return SPV_ENV_VULKAN_1_2;
2812 } else if (api_version >= VK_API_VERSION_1_1) {
2813 if (spirv_1_4) {
2814 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
2815 } else {
2816 return SPV_ENV_VULKAN_1_1;
2817 }
2818 }
2819 return SPV_ENV_VULKAN_1_0;
2820}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002821
sfricke-samsungecc112a2021-09-03 05:32:17 -07002822// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06002823void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002824 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07002825 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
2826 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002827 if (device_extensions.vk_khr_relaxed_block_layout) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07002828 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002829 options.SetRelaxBlockLayout(true);
2830 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07002831
2832 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
2833 // Vulkan version used, the feature bit is needed (also described in the spec).
2834
2835 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
2836 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002837 options.SetUniformBufferStandardLayout(true);
2838 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07002839 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
2840 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002841 options.SetScalarBlockLayout(true);
2842 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07002843 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
2844 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08002845 options.SetWorkgroupScalarBlockLayout(true);
2846 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002847}