blob: 039c894850ec032760054822f9d08343d02c7e1c [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;
sfricke-samsunged028b02021-09-06 23:14:51 -0700107 // Formats such as VK_FORMAT_D16_UNORM_S8_UINT are both
Dave Houltona9df0ce2018-02-07 10:51:23 -0700108 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
109 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700110 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
111 return FORMAT_TYPE_FLOAT;
112}
113
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600114static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700115 uint32_t bit_pos = uint32_t(u_ffs(stage));
116 return bit_pos - 1;
117}
118
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700119bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700120 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
121 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700122 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700123 bool skip = false;
124
125 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
126 auto desc = &vi->pVertexBindingDescriptions[i];
127 auto &binding = bindings[desc->binding];
128 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600129 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700130 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
131 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700132 } else {
133 binding = desc;
134 }
135 }
136
137 return skip;
138}
139
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700140bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs,
141 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700142 bool skip = false;
143
sfricke-samsung962cad92021-04-13 00:46:29 -0700144 const auto inputs = vs->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700145
146 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200147 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700148 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200149 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
150 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
151 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700152 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
153 }
154 }
155 }
156
Petr Kraus25810d02019-08-27 17:41:15 +0200157 struct AttribInputPair {
158 const VkVertexInputAttributeDescription *attrib = nullptr;
159 const interface_var *input = nullptr;
160 };
161 std::map<uint32_t, AttribInputPair> location_map;
162 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
163 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700164
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400165 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200166 const auto location = location_it.first;
167 const auto attrib = location_it.second.attrib;
168 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600169
Petr Kraus25810d02019-08-27 17:41:15 +0200170 if (attrib && !input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600171 skip |= LogPerformanceWarning(vs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700172 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200173 } else if (!attrib && input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600174 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700175 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200176 } else if (attrib && input) {
177 const auto attrib_type = GetFormatType(attrib->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700178 const auto input_type = vs->GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700179
180 // Type checking
181 if (!(attrib_type & input_type)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600182 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700183 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sfricke-samsung962cad92021-04-13 00:46:29 -0700184 string_VkFormat(attrib->format), location, vs->DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700185 }
Petr Kraus25810d02019-08-27 17:41:15 +0200186 } else { // !attrib && !input
187 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700188 }
189 }
190
191 return skip;
192}
193
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700194bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint,
195 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200196 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700197
Petr Kraus25810d02019-08-27 17:41:15 +0200198 const auto rpci = pipeline->rp_state->createInfo.ptr();
199
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600200 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800201 const VkAttachmentReference2 *reference = nullptr;
202 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600203 const interface_var *output = nullptr;
204 };
205 std::map<uint32_t, Attachment> location_map;
206
Petr Kraus25810d02019-08-27 17:41:15 +0200207 const auto subpass = rpci->pSubpasses[subpass_index];
208 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600209 auto const &reference = subpass.pColorAttachments[i];
210 location_map[i].reference = &reference;
211 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
212 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
213 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
Chris Forbes47567b72017-06-09 12:09:45 -0700214 }
215 }
216
Chris Forbes47567b72017-06-09 12:09:45 -0700217 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
218
sfricke-samsung962cad92021-04-13 00:46:29 -0700219 const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600220 for (const auto &output_it : outputs) {
221 auto const location = output_it.first.first;
222 location_map[location].output = &output_it.second;
223 }
Chris Forbes47567b72017-06-09 12:09:45 -0700224
Jeremy Gebben11af9792021-08-20 10:20:09 -0600225 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
226 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700227
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400228 for (const auto &location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600229 const auto reference = location_it.second.reference;
230 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
231 continue;
232 }
233
Petr Kraus25810d02019-08-27 17:41:15 +0200234 const auto location = location_it.first;
235 const auto attachment = location_it.second.attachment;
236 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +0200237 if (attachment && !output) {
238 if (pipeline->attachments[location].colorWriteMask != 0) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600239 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700240 "Attachment %" PRIu32
241 " not written by fragment shader; undefined values will be written to attachment",
242 location);
Petr Kraus25810d02019-08-27 17:41:15 +0200243 }
244 } else if (!attachment && output) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700245 if (!(alpha_to_coverage_enabled && location == 0)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600246 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700247 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200248 }
Petr Kraus25810d02019-08-27 17:41:15 +0200249 } else if (attachment && output) {
250 const auto attachment_type = GetFormatType(attachment->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700251 const auto output_type = fs->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700252
253 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +0200254 if (!(output_type & attachment_type)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700255 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600256 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700257 "Attachment %" PRIu32
258 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sfricke-samsung962cad92021-04-13 00:46:29 -0700259 location, string_VkFormat(attachment->format), fs->DescribeType(output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700260 }
Petr Kraus25810d02019-08-27 17:41:15 +0200261 } else { // !attachment && !output
262 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700263 }
264 }
265
Petr Kraus25810d02019-08-27 17:41:15 +0200266 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700267 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
sfricke-samsung962cad92021-04-13 00:46:29 -0700268 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700269 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600270 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700271 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200272 }
273
Chris Forbes47567b72017-06-09 12:09:45 -0700274 return skip;
275}
276
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600277PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
278 const shader_struct_member &push_constant_used_in_shader,
279 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600280 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600281 const auto used_bytes_size = used_bytes->size();
282 if (used_bytes_size == 0) return PC_Byte_Updated;
283
284 const auto push_constant_data_update_size = push_constant_data_update.size();
285 const auto *data = push_constant_data_update.data();
286 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
287 if (used_bytes_size <= push_constant_data_update_size) {
288 return PC_Byte_Updated;
289 }
290 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
291
292 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
293 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
294 return PC_Byte_Updated;
295 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600296 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600297
locke-lunargde3f0fa2020-09-10 11:55:31 -0600298 uint32_t i = 0;
299 for (const auto used : *used_bytes) {
300 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600301 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600302 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600303 return PC_Byte_Not_Set;
304 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600305 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600306 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600307 }
308 }
309 ++i;
310 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600311 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600312}
313
314bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src,
sfricke-samsung7699b912021-04-12 23:01:51 -0700315 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700316 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700317 // Temp workaround to prevent false positive errors
318 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
319 if (src->multiple_entry_points) {
320 return skip;
321 }
322
Chris Forbes47567b72017-06-09 12:09:45 -0700323 // 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 -0700324 const auto *entrypoint = src->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600325 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
326 return skip;
327 }
328 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700329
locke-lunargde3f0fa2020-09-10 11:55:31 -0600330 bool found_stage = false;
331 for (auto const &range : *push_constant_ranges) {
332 if (range.stageFlags & pStage->stage) {
333 found_stage = true;
334 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600335 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600336 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600337 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600338 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600339 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600340 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600341 const auto ret =
342 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700343
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600344 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600345 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600346 LogObjectList objlist(src->vk_shader_module());
347 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700348 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 -0600349 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600350 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600351 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700352 }
353 }
354 }
355
locke-lunargde3f0fa2020-09-10 11:55:31 -0600356 if (!found_stage) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600357 LogObjectList objlist(src->vk_shader_module());
358 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700359 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 -0600360 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module()).c_str(),
361 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700362 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700363 }
Chris Forbes47567b72017-06-09 12:09:45 -0700364 return skip;
365}
366
sfricke-samsungcfb44592021-07-25 00:36:28 -0700367bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700368 bool skip = false;
369
370 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700371 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700372 return skip;
373 }
374
sfricke-samsungcfb44592021-07-25 00:36:28 -0700375 // Find all builtin from just the interface variables
376 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700377 auto insn = src->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700378 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700379 const decoration_set decorations = src->get_decorations(insn.word(2));
380
sfricke-samsungcfb44592021-07-25 00:36:28 -0700381 // Currently don't need to search in structs
382 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700383 auto type_pointer = src->get_def(insn.word(1));
384 assert(type_pointer.opcode() == spv::OpTypePointer);
385
386 auto type = src->get_def(type_pointer.word(3));
387 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700388 uint32_t length = static_cast<uint32_t>(src->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700389 // Handles both the input and output sampleMask
390 if (length > phys_dev_props.limits.maxSampleMaskWords) {
391 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
392 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
393 "maxSampleMaskWords of %u in %s.",
394 length, phys_dev_props.limits.maxSampleMaskWords,
395 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700396 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700397 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700398 }
399 }
400 }
401
402 return skip;
403}
404
Chris Forbes47567b72017-06-09 12:09:45 -0700405// Validate that data for each specialization entry is fully contained within the buffer.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200406bool CoreChecks::ValidateSpecializations(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700407 bool skip = false;
408
409 VkSpecializationInfo const *spec = info->pSpecializationInfo;
410
411 if (spec) {
412 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600413 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700414 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
415 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200416 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700417 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
418 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600419
420 continue;
421 }
Chris Forbes47567b72017-06-09 12:09:45 -0700422 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700423 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
424 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200425 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700426 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
427 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700428 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200429 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
430 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
431 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
432 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
433 j, spec->pMapEntries[i].constantID);
434 }
435 }
Chris Forbes47567b72017-06-09 12:09:45 -0700436 }
437 }
438
439 return skip;
440}
441
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500442// TODO (jbolz): Can this return a const reference?
sourav parmarcd5fb182020-07-17 12:58:44 -0700443static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count,
444 bool is_khr) {
Chris Forbes47567b72017-06-09 12:09:45 -0700445 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800446 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700447 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500448 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700449
450 // 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 -0500451 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
452 if (type.opcode() == spv::OpTypeRuntimeArray) {
453 descriptor_count = 0;
454 type = module->get_def(type.word(2));
455 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700456 descriptor_count *= module->GetConstantValueById(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700457 type = module->get_def(type.word(2));
458 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800459 if (type.word(2) == spv::StorageClassStorageBuffer) {
460 is_storage_buffer = true;
461 }
Chris Forbes47567b72017-06-09 12:09:45 -0700462 type = module->get_def(type.word(3));
463 }
464 }
465
466 switch (type.opcode()) {
467 case spv::OpTypeStruct: {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800468 for (auto insn : module->decoration_inst) {
469 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700470 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800471 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500472 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
473 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
474 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800475 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500476 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
477 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
478 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
479 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800480 }
Chris Forbes47567b72017-06-09 12:09:45 -0700481 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500482 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
483 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
484 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700485 }
486 }
487 }
488
489 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500490 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700491 }
492
493 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500494 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
495 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
496 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700497
Chris Forbes73c00bf2018-06-22 16:28:06 -0700498 case spv::OpTypeSampledImage: {
499 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
500 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
501 auto image_type = module->get_def(type.word(2));
502 auto dim = image_type.word(3);
503 auto sampled = image_type.word(7);
504 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500505 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
506 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700507 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700508 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500509 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
510 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700511
512 case spv::OpTypeImage: {
513 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
514 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
515 auto dim = type.word(3);
516 auto sampled = type.word(7);
517
518 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500519 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
520 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700521 } else if (dim == spv::DimBuffer) {
522 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500523 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
524 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700525 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500526 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
527 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700528 }
529 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500530 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
531 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
532 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700533 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500534 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
535 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700536 }
537 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600538 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700539 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
540 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500541 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700542
543 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
544 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500545 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700546 }
547}
548
Jeff Bolze54ae892018-09-08 12:16:29 -0500549static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700550 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500551 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
552 if (ss.tellp()) ss << ", ";
553 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700554 }
555 return ss.str();
556}
557
sfricke-samsung0065ce02020-12-03 22:46:37 -0800558bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500559 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800560 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 -0500561 return true;
562 }
563 }
564
565 return false;
566}
567
sfricke-samsung0065ce02020-12-03 22:46:37 -0800568bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700569 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800570 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700571 return true;
572 }
573 }
574
575 return false;
576}
577
locke-lunarg63e4daf2020-08-17 17:53:25 -0600578bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
579 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500580 bool skip = false;
581
locke-lunarg63e4daf2020-08-17 17:53:25 -0600582 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800583 switch (stage) {
584 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -0600585 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
586 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
587 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
588 case VK_SHADER_STAGE_MISS_BIT_NV:
589 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
590 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
591 case VK_SHADER_STAGE_TASK_BIT_NV:
592 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -0800593 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -0600594 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -0800595 break;
596 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800597 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
598 kVUID_Core_Shader_FeatureNotEnabled);
Chris Forbes349b3132018-03-07 11:38:08 -0800599 break;
600 default:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800601 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
602 kVUID_Core_Shader_FeatureNotEnabled);
Chris Forbes349b3132018-03-07 11:38:08 -0800603 break;
604 }
605 }
606
Chris Forbes47567b72017-06-09 12:09:45 -0700607 return skip;
608}
609
sfricke-samsung94167ca2021-02-26 04:14:59 -0800610bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
611 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500612 bool skip = false;
613
sfricke-samsung94167ca2021-02-26 04:14:59 -0800614 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
615 if (GroupOperation(insn.opcode()) == true) {
616 // Check the quad operations.
617 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
618 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
619 skip |= RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
620 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages",
621 kVUID_Core_Shader_FeatureNotEnabled);
sfricke-samsung0065ce02020-12-03 22:46:37 -0800622 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800623 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500624
sfricke-samsung94167ca2021-02-26 04:14:59 -0800625 uint32_t scope_type = spv::ScopeMax;
626 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
627 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
628 scope_type = spv::ScopeSubgroup;
629 } else {
630 // "All <id> used for Scope <id> must be of an OpConstant"
631 auto scope_id = module->get_def(insn.word(3));
632 scope_type = scope_id.word(3);
633 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800634
sfricke-samsung94167ca2021-02-26 04:14:59 -0800635 if (scope_type == spv::ScopeSubgroup) {
636 // "Group operations with subgroup scope" must have stage support
637 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
638 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung0065ce02020-12-03 22:46:37 -0800639 "VkPhysicalDeviceSubgroupProperties::supportedStages", kVUID_Core_Shader_ExceedDeviceLimit);
sfricke-samsung94167ca2021-02-26 04:14:59 -0800640 }
641
642 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
643 auto type = module->get_def(insn.word(1));
644
645 if (type.opcode() == spv::OpTypeVector) {
646 // Get the element type
647 type = module->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800648 }
649
sfricke-samsung94167ca2021-02-26 04:14:59 -0800650 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800651 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
652 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500653
sfricke-samsung0065ce02020-12-03 22:46:37 -0800654 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
655 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
656 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
657 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
658 kVUID_Core_Shader_FeatureNotEnabled);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500659 }
660 }
661 }
Jeff Bolzee743412019-06-20 22:24:32 -0500662 }
663
664 return skip;
665}
666
ziga-lunarg2818f492021-08-12 14:30:51 +0200667bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
668 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
669 bool skip = false;
670
671 std::array<uint32_t, 3> work_group_size = src->GetWorkgroupSize(pStage, id_value_map);
672
673 for (uint32_t i = 0; i < 3; ++i) {
674 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
675 const char member = 'x' + static_cast<int8_t>(i);
676 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
677 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
678 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
679 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
680 }
681 }
682 return skip;
683}
684
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600685bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600686 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200687 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
688 pStage->stage == VK_SHADER_STAGE_ALL) {
689 return false;
690 }
691
692 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700693 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200694
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700695 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200696 struct Variable {
697 uint32_t baseTypePtrID;
698 uint32_t ID;
699 uint32_t storageClass;
700 };
701 std::vector<Variable> variables;
702
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700703 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700704 bool is_iso_lines = false;
705 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500706
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700707 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600708
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200709 for (auto insn : *src) {
710 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500711 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200712 case spv::OpDecorate:
713 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500714 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700715 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200716 break;
717 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200718 default:
719 break;
720 }
721 break;
722 // Find all input and output variables
723 case spv::OpVariable: {
724 Variable var = {};
725 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600726 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
727 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700728 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200729 var.baseTypePtrID = insn.word(1);
730 var.ID = insn.word(2);
731 variables.push_back(var);
732 }
733 break;
734 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500735 case spv::OpExecutionMode:
736 if (insn.word(1) == entrypoint.word(2)) {
737 switch (insn.word(2)) {
738 default:
739 break;
740 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700741 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500742 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700743 case spv::ExecutionModeIsolines:
744 is_iso_lines = true;
745 break;
746 case spv::ExecutionModePointMode:
747 is_point_mode = true;
748 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500749 }
750 }
751 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200752 default:
753 break;
754 }
755 }
756
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500757 bool strip_output_array_level =
758 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
759 bool strip_input_array_level =
760 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
761 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
762
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700763 uint32_t num_comp_in = 0, num_comp_out = 0;
764 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600765
sfricke-samsung962cad92021-04-13 00:46:29 -0700766 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
767 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600768
769 // Find max component location used for input variables.
770 for (auto &var : inputs) {
771 int location = var.first.first;
772 int component = var.first.second;
773 interface_var &iv = var.second;
774
775 // Only need to look at the first location, since we use the type's whole size
776 if (iv.offset != 0) {
777 continue;
778 }
779
780 if (iv.is_patch) {
781 continue;
782 }
783
sfricke-samsung962cad92021-04-13 00:46:29 -0700784 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700785 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600786 }
787
788 // Find max component location used for output variables.
789 for (auto &var : outputs) {
790 int location = var.first.first;
791 int component = var.first.second;
792 interface_var &iv = var.second;
793
794 // Only need to look at the first location, since we use the type's whole size
795 if (iv.offset != 0) {
796 continue;
797 }
798
799 if (iv.is_patch) {
800 continue;
801 }
802
sfricke-samsung962cad92021-04-13 00:46:29 -0700803 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700804 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600805 }
806
807 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
808 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700809 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
810 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200811 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500812 // Check if the variable is a patch. Patches can also be members of blocks,
813 // but if they are then the top-level arrayness has already been stripped
814 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700815 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200816
817 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700818 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200819 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700820 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200821 }
822 }
823
824 switch (pStage->stage) {
825 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700826 if (num_comp_out > limits.maxVertexOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600827 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700828 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
829 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
830 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700831 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200832 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700833 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600834 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700835 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
836 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
837 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600838 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200839 break;
840
841 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700842 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600843 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700844 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
845 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
846 "components by %u components",
847 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700848 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200849 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700850 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600851 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600852 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700853 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
854 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
855 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600856 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700857 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600858 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700859 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
860 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
861 "components by %u components",
862 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700863 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200864 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700865 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600866 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600867 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700868 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
869 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
870 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600871 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200872 break;
873
874 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700875 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600876 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700877 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
878 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
879 "components by %u components",
880 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700881 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200882 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700883 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600884 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600885 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700886 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
887 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
888 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600889 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700890 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600891 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700892 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
893 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
894 "components by %u components",
895 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700896 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200897 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700898 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600899 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600900 LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700901 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
902 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
903 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600904 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700905 // Portability validation
906 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
907 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600908 skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_Isolines,
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700909 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
910 " is using abstract patch type IsoLines, but this is not supported on this platform");
911 }
912 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600913 skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_PointMode,
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700914 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
915 " is using abstract patch type PointMode, but this is not supported on this platform");
916 }
917 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200918 break;
919
920 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700921 if (num_comp_in > limits.maxGeometryInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600922 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700923 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
924 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
925 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700926 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200927 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700928 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600929 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700930 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
931 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
932 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600933 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700934 if (num_comp_out > limits.maxGeometryOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600935 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700936 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
937 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
938 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700939 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200940 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700941 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600942 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700943 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
944 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
945 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600946 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700947 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600948 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700949 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
950 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
951 "components by %u components",
952 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700953 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500954 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200955 break;
956
957 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700958 if (num_comp_in > limits.maxFragmentInputComponents) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600959 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700960 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
961 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
962 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700963 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200964 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700965 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600966 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700967 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
968 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
969 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600970 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200971 break;
972
Jeff Bolz148d94e2018-12-13 21:25:56 -0600973 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
974 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
975 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
976 case VK_SHADER_STAGE_MISS_BIT_NV:
977 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
978 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
979 case VK_SHADER_STAGE_TASK_BIT_NV:
980 case VK_SHADER_STAGE_MESH_BIT_NV:
981 break;
982
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200983 default:
984 assert(false); // This should never happen
985 }
986 return skip;
987}
988
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300989bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const {
990 bool skip = false;
991
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300992 // Got through all ImageRead/Write instructions
993 for (auto insn : *src) {
994 switch (insn.opcode()) {
995 case spv::OpImageSparseRead:
996 case spv::OpImageRead: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +0300997 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +0300998 if (type_def != src->end()) {
Tim Van Pattenffe91322021-07-26 10:20:50 -0600999 const auto dim = type_def.word(3);
1000 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1001 // StorageImageReadWithoutFormat Capability was declared.
1002 if (dim != spv::DimSubpassData && type_def.word(8) == spv::ImageFormatUnknown) {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001003 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1004 "shaderStorageImageReadWithoutFormat",
1005 kVUID_Features_shaderStorageImageReadWithoutFormat);
1006 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001007 }
1008 break;
1009 }
1010 case spv::OpImageWrite: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001011 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001012 if (type_def != src->end()) {
1013 if (type_def.word(8) == spv::ImageFormatUnknown) {
1014 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1015 "shaderStorageImageWriteWithoutFormat",
1016 kVUID_Features_shaderStorageImageWriteWithoutFormat);
1017 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001018 }
1019 break;
1020 }
1021
1022 }
1023 }
1024
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001025 // Go through all variables for images and check decorations
1026 for (auto insn : *src) {
1027 if (insn.opcode() != spv::OpVariable)
1028 continue;
1029
1030 uint32_t var = insn.word(2);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001031 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001032 if (type_def == src->end())
1033 continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001034 // Only check if the Image Dim operand is not SubpassData
1035 const auto dim = type_def.word(3);
1036 if (dim == spv::DimSubpassData) continue;
Corentin Wallez91f8b6d2021-07-23 10:11:31 +02001037 // Only check storage images
1038 if (type_def.word(7) != 2) continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001039 if (type_def.word(8) != spv::ImageFormatUnknown) continue;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001040
1041 decoration_set img_decorations = src->get_decorations(var);
1042
1043 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1044 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1045 skip |= LogError(device,
1046 kVUID_Features_shaderStorageImageReadWithoutFormat_NonReadable,
1047 "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32 " "
1048 " without format not marked a NonReadable", var);
1049 }
1050
1051 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1052 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1053 skip |= LogError(device,
1054 kVUID_Features_shaderStorageImageWriteWithoutFormat_NonWritable,
1055 "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32 " "
1056 "without format not marked a NonWritable", var);
1057 }
1058 }
1059
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001060 return skip;
1061}
1062
sfricke-samsungdc96f302020-03-18 20:42:10 -07001063bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1064 bool skip = false;
1065 uint32_t total_resources = 0;
1066
1067 // Only currently testing for graphics and compute pipelines
1068 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1069 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1070 return false;
1071 }
1072
1073 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1074 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
Jeremy Gebben11af9792021-08-20 10:20:09 -06001075 total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
sfricke-samsungdc96f302020-03-18 20:42:10 -07001076 }
1077
1078 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1079 // input from CreatePipeline and CreatePipelineLayout level
1080 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1081 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1082 continue;
1083 }
1084
1085 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1086 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1087 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1088 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1089 // Check only descriptor types listed in maxPerStageResources description in spec
1090 switch (binding->descriptorType) {
1091 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1092 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1093 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1094 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1095 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1096 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1097 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1098 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1099 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1100 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1101 total_resources += binding->descriptorCount;
1102 break;
1103 default:
1104 break;
1105 }
1106 }
1107 }
1108 }
1109
1110 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1111 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1112 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001113 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001114 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1115 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1116 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1117 }
1118
1119 return skip;
1120}
1121
Jeff Bolze4356752019-03-07 11:23:46 -06001122// copy the specialization constant value into buf, if it is present
1123void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1124 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1125
1126 if (spec && spec_id < spec->mapEntryCount) {
1127 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1128 }
1129}
1130
1131// Fill in value with the constant or specialization constant value, if available.
1132// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001133static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001134 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001135 auto type_id = src->get_def(insn.word(1));
1136 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1137 return false;
1138 }
1139 switch (insn.opcode()) {
1140 case spv::OpSpecConstant:
1141 *value = insn.word(3);
1142 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1143 return true;
1144 case spv::OpConstant:
1145 *value = insn.word(3);
1146 return true;
1147 default:
1148 return false;
1149 }
1150}
1151
1152// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001153VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001154 switch (insn.opcode()) {
1155 case spv::OpTypeInt:
1156 switch (insn.word(2)) {
1157 case 8:
1158 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1159 case 16:
1160 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1161 case 32:
1162 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1163 case 64:
1164 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1165 default:
1166 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1167 }
1168 case spv::OpTypeFloat:
1169 switch (insn.word(2)) {
1170 case 16:
1171 return VK_COMPONENT_TYPE_FLOAT16_NV;
1172 case 32:
1173 return VK_COMPONENT_TYPE_FLOAT32_NV;
1174 case 64:
1175 return VK_COMPONENT_TYPE_FLOAT64_NV;
1176 default:
1177 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1178 }
1179 default:
1180 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1181 }
1182}
1183
1184// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1185// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001186bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001187 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001188 bool skip = false;
1189
1190 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001191 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001192 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001193 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001194
1195 struct CoopMatType {
1196 uint32_t scope, rows, cols;
1197 VkComponentTypeNV component_type;
1198 bool all_constant;
1199
1200 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1201
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001202 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001203 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001204 spirv_inst_iter insn = src->get_def(id);
1205 uint32_t component_type_id = insn.word(2);
1206 uint32_t scope_id = insn.word(3);
1207 uint32_t rows_id = insn.word(4);
1208 uint32_t cols_id = insn.word(5);
1209 auto component_type_iter = src->get_def(component_type_id);
1210 auto scope_iter = src->get_def(scope_id);
1211 auto rows_iter = src->get_def(rows_id);
1212 auto cols_iter = src->get_def(cols_id);
1213
1214 all_constant = true;
1215 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1216 all_constant = false;
1217 }
1218 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1219 all_constant = false;
1220 }
1221 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1222 all_constant = false;
1223 }
1224 component_type = GetComponentType(component_type_iter, src);
1225 }
1226 };
1227
1228 bool seen_coopmat_capability = false;
1229
1230 for (auto insn : *src) {
1231 // Whitelist instructions whose result can be a cooperative matrix type, and
1232 // keep track of their types. It would be nice if SPIRV-Headers generated code
1233 // to identify which instructions have a result type and result id. Lacking that,
1234 // this whitelist is based on the set of instructions that
1235 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1236 switch (insn.opcode()) {
1237 case spv::OpLoad:
1238 case spv::OpCooperativeMatrixLoadNV:
1239 case spv::OpCooperativeMatrixMulAddNV:
1240 case spv::OpSNegate:
1241 case spv::OpFNegate:
1242 case spv::OpIAdd:
1243 case spv::OpFAdd:
1244 case spv::OpISub:
1245 case spv::OpFSub:
1246 case spv::OpFDiv:
1247 case spv::OpSDiv:
1248 case spv::OpUDiv:
1249 case spv::OpMatrixTimesScalar:
1250 case spv::OpConstantComposite:
1251 case spv::OpCompositeConstruct:
1252 case spv::OpConvertFToU:
1253 case spv::OpConvertFToS:
1254 case spv::OpConvertSToF:
1255 case spv::OpConvertUToF:
1256 case spv::OpUConvert:
1257 case spv::OpSConvert:
1258 case spv::OpFConvert:
1259 id_to_type_id[insn.word(2)] = insn.word(1);
1260 break;
1261 default:
1262 break;
1263 }
1264
1265 switch (insn.opcode()) {
1266 case spv::OpDecorate:
1267 if (insn.word(2) == spv::DecorationSpecId) {
1268 id_to_spec_id[insn.word(1)] = insn.word(3);
1269 }
1270 break;
1271 case spv::OpCapability:
1272 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1273 seen_coopmat_capability = true;
1274
1275 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001276 skip |= LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001277 pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixSupportedStages,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001278 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1279 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001280 }
1281 }
1282 break;
1283 case spv::OpMemoryModel:
1284 // If the capability isn't enabled, don't bother with the rest of this function.
1285 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1286 if (!seen_coopmat_capability) {
1287 return skip;
1288 }
1289 break;
1290 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001291 CoopMatType m;
1292 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001293
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001294 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001295 // Validate that the type parameters are all supported for one of the
1296 // operands of a cooperative matrix property.
1297 bool valid = false;
1298 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001299 if (cooperative_matrix_properties[i].AType == m.component_type &&
1300 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1301 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001302 valid = true;
1303 break;
1304 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001305 if (cooperative_matrix_properties[i].BType == m.component_type &&
1306 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1307 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001308 valid = true;
1309 break;
1310 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001311 if (cooperative_matrix_properties[i].CType == m.component_type &&
1312 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1313 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001314 valid = true;
1315 break;
1316 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001317 if (cooperative_matrix_properties[i].DType == m.component_type &&
1318 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1319 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001320 valid = true;
1321 break;
1322 }
1323 }
1324 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001325 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001326 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1327 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001328 }
1329 }
1330 break;
1331 }
1332 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001333 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001334 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1335 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1336 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1337 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001338 // Couldn't find type of matrix
1339 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001340 break;
1341 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001342 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1343 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1344 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1345 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001346
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001347 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001348 // Validate that the type parameters are all supported for the same
1349 // cooperative matrix property.
1350 bool valid = false;
1351 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001352 if (cooperative_matrix_properties[i].AType == a.component_type &&
1353 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1354 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001355
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001356 cooperative_matrix_properties[i].BType == b.component_type &&
1357 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1358 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001359
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001360 cooperative_matrix_properties[i].CType == c.component_type &&
1361 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1362 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001363
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001364 cooperative_matrix_properties[i].DType == d.component_type &&
1365 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1366 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001367 valid = true;
1368 break;
1369 }
1370 }
1371 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001372 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001373 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1374 "VkCooperativeMatrixPropertiesNV",
1375 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001376 }
1377 }
1378 break;
1379 }
1380 default:
1381 break;
1382 }
1383 }
1384
1385 return skip;
1386}
1387
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001388bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1389 const PIPELINE_STATE *pipeline) const {
1390 bool skip = false;
1391
1392 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1393 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1394 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1395 for (auto insn : *src) {
1396 switch (insn.opcode()) {
1397 case spv::OpCapability:
1398 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1399 auto subpass_flags =
1400 (pipeline->rp_state == nullptr)
1401 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001402 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001403 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1404 skip |=
1405 LogError(pipeline->pipeline(), kVUID_Core_Shader_ResolveQCOM_InvalidCapability,
1406 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1407 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1408 }
1409 }
1410 break;
1411 default:
1412 break;
1413 }
1414 }
1415 }
1416
1417 return skip;
1418}
1419
sfricke-samsung58b84352021-07-31 21:41:04 -07001420bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *src) const {
1421 bool skip = false;
1422
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001423 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001424 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001425
sfricke-samsungf5042b12021-08-05 01:09:40 -07001426 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1427 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1428
1429 const bool valid_storage_buffer_float = (
1430 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1431 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1432 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1433 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1434 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1435 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1436 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1437 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1438 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1439
1440 const bool valid_workgroup_float = (
1441 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1442 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1443 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1444 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1445 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1446 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1447 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1448 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1449 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1450
1451 const bool valid_image_float = (
1452 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1453 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1454 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1455
1456 const bool valid_16_float = (
1457 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1458 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1459 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1460 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1461 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1462 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1463
1464 const bool valid_32_float = (
1465 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1466 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1467 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1468 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1469 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1470 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1471 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1472 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1473 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1474
1475 const bool valid_64_float = (
1476 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1477 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1478 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1479 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1480 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1481 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1482 // clang-format on
1483
sfricke-samsung58b84352021-07-31 21:41:04 -07001484 for (auto &atomic_inst : src->atomic_inst) {
1485 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungf5042b12021-08-05 01:09:40 -07001486 const uint32_t opcode = src->at(atomic_inst.first).opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001487
1488 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
1489 // Validate 64-bit atomics
1490 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1491 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
1492 skip |= LogError(
1493 device, kVUID_Core_Shader_AtomicFeature,
1494 "%s: Can't use 64-bit int atomics operations with %s storage class without shaderBufferInt64Atomics enabled.",
1495 report_data->FormatHandle(src->vk_shader_module()).c_str(), StorageClassName(atomic.storage_class));
1496 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1497 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
1498 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1499 "%s: Can't use 64-bit int atomics operations with Workgroup storage class without "
1500 "shaderSharedInt64Atomics enabled.",
1501 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001502 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
1503 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1504 "%s: Can't use 64-bit int atomics operations with Image storage class without "
1505 "shaderImageInt64Atomics enabled.",
1506 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001507 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001508 } else if (atomic.type == spv::OpTypeFloat) {
1509 // Validate Floats
1510 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1511 if (valid_storage_buffer_float == false) {
1512 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1513 "%s: Can't use float atomics operations with StorageBuffer storage class without "
1514 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1515 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1516 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1517 "shaderBufferFloat64AtomicMinMax enabled.",
1518 report_data->FormatHandle(src->vk_shader_module()).c_str());
1519 } else if (opcode == spv::OpAtomicFAddEXT) {
1520 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1521 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1522 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with "
1523 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
1524 report_data->FormatHandle(src->vk_shader_module()).c_str());
1525 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1526 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1527 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with "
1528 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
1529 report_data->FormatHandle(src->vk_shader_module()).c_str());
1530 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1531 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1532 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with "
1533 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
1534 report_data->FormatHandle(src->vk_shader_module()).c_str());
1535 }
1536 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1537 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
1538 skip |= LogError(
1539 device, kVUID_Core_Shader_AtomicFeature,
1540 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1541 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
1542 report_data->FormatHandle(src->vk_shader_module()).c_str());
1543 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
1544 skip |= LogError(
1545 device, kVUID_Core_Shader_AtomicFeature,
1546 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1547 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
1548 report_data->FormatHandle(src->vk_shader_module()).c_str());
1549 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
1550 skip |= LogError(
1551 device, kVUID_Core_Shader_AtomicFeature,
1552 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1553 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
1554 report_data->FormatHandle(src->vk_shader_module()).c_str());
1555 }
1556 } else {
1557 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1558 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
1559 skip |= LogError(
1560 device, kVUID_Core_Shader_AtomicFeature,
1561 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1562 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
1563 report_data->FormatHandle(src->vk_shader_module()).c_str());
1564 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
1565 skip |= LogError(
1566 device, kVUID_Core_Shader_AtomicFeature,
1567 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1568 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
1569 report_data->FormatHandle(src->vk_shader_module()).c_str());
1570 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
1571 skip |= LogError(
1572 device, kVUID_Core_Shader_AtomicFeature,
1573 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1574 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
1575 report_data->FormatHandle(src->vk_shader_module()).c_str());
1576 }
1577 }
1578 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1579 if (valid_workgroup_float == false) {
1580 skip |= LogError(
1581 device, kVUID_Core_Shader_AtomicFeature,
1582 "%s: Can't use float atomics operations with Workgroup storage class without shaderSharedFloat32Atomics or "
1583 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1584 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1585 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1586 report_data->FormatHandle(src->vk_shader_module()).c_str());
1587 } else if (opcode == spv::OpAtomicFAddEXT) {
1588 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1589 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1590 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1591 "storage class without shaderSharedFloat16AtomicAdd enabled.",
1592 report_data->FormatHandle(src->vk_shader_module()).c_str());
1593 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1594 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1595 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1596 "storage class without shaderSharedFloat32AtomicAdd enabled.",
1597 report_data->FormatHandle(src->vk_shader_module()).c_str());
1598 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1599 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1600 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1601 "storage class without shaderSharedFloat64AtomicAdd enabled.",
1602 report_data->FormatHandle(src->vk_shader_module()).c_str());
1603 }
1604 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1605 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
1606 skip |= LogError(
1607 device, kVUID_Core_Shader_AtomicFeature,
1608 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1609 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
1610 report_data->FormatHandle(src->vk_shader_module()).c_str());
1611 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
1612 skip |= LogError(
1613 device, kVUID_Core_Shader_AtomicFeature,
1614 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1615 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
1616 report_data->FormatHandle(src->vk_shader_module()).c_str());
1617 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
1618 skip |= LogError(
1619 device, kVUID_Core_Shader_AtomicFeature,
1620 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1621 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
1622 report_data->FormatHandle(src->vk_shader_module()).c_str());
1623 }
1624 } else {
1625 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1626 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
1627 skip |= LogError(
1628 device, kVUID_Core_Shader_AtomicFeature,
1629 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1630 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat16Atomics enabled.",
1631 report_data->FormatHandle(src->vk_shader_module()).c_str());
1632 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
1633 skip |= LogError(
1634 device, kVUID_Core_Shader_AtomicFeature,
1635 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1636 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat32Atomics enabled.",
1637 report_data->FormatHandle(src->vk_shader_module()).c_str());
1638 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
1639 skip |= LogError(
1640 device, kVUID_Core_Shader_AtomicFeature,
1641 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1642 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat64Atomics enabled.",
1643 report_data->FormatHandle(src->vk_shader_module()).c_str());
1644 }
1645 }
1646 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
1647 skip |=
1648 LogError(device, kVUID_Core_Shader_AtomicFeature,
1649 "%s: Can't use float atomics operations with Image storage class without shaderImageFloat32Atomics or "
1650 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
1651 report_data->FormatHandle(src->vk_shader_module()).c_str());
1652 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
1653 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1654 "%s: Can't use 16-bit float atomics operations without shaderBufferFloat16Atomics, "
1655 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1656 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
1657 report_data->FormatHandle(src->vk_shader_module()).c_str());
1658 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
1659 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1660 "%s: Can't use 32-bit float atomics operations without shaderBufferFloat32AtomicMinMax, "
1661 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1662 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1663 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1664 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
1665 report_data->FormatHandle(src->vk_shader_module()).c_str());
1666 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
1667 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1668 "%s: Can't use 64-bit float atomics operations without shaderBufferFloat64AtomicMinMax, "
1669 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1670 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
1671 report_data->FormatHandle(src->vk_shader_module()).c_str());
1672 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001673 }
1674 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001675 return skip;
1676}
1677
John Zulaufac4c6e12019-07-01 16:05:58 -06001678bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001679 auto entrypoint_id = entrypoint.word(2);
1680
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001681 // The first denorm execution mode encountered, along with its bit width.
1682 // Used to check if SeparateDenormSettings is respected.
1683 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001684
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001685 // The first rounding mode encountered, along with its bit width.
1686 // Used to check if SeparateRoundingModeSettings is respected.
1687 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001688
1689 bool skip = false;
1690
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001691 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001692 uint32_t invocations = 0;
1693
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001694 auto it = src->execution_mode_inst.find(entrypoint_id);
1695 if (it != src->execution_mode_inst.end()) {
1696 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001697 auto mode = insn.word(2);
1698 switch (mode) {
1699 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1700 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001701 if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) ||
1702 (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) ||
1703 (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001704 skip |= LogError(
1705 device, kVUID_Core_Shader_FeatureNotEnabled,
1706 "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device",
1707 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001708 }
1709 break;
1710 }
1711
1712 case spv::ExecutionModeDenormPreserve: {
1713 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001714 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) ||
1715 (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) ||
1716 (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001717 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1718 "Shader requires DenormPreserve for bit width %d but it is not enabled on the device",
1719 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001720 }
1721
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001722 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1723 // Register the first denorm execution mode found
1724 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001725 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001726 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001727 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001728 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001729 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1730 "Shader uses different denorm execution modes for 16 and 64-bit but "
1731 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001732 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001733 }
1734 break;
1735
Mike Schuchardt2df08912020-12-15 16:28:09 -08001736 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001737 break;
1738
Mike Schuchardt2df08912020-12-15 16:28:09 -08001739 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001740 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1741 "Shader uses different denorm execution modes for different bit widths but "
1742 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001743 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001744 break;
1745
1746 default:
1747 break;
1748 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001749 }
1750 break;
1751 }
1752
1753 case spv::ExecutionModeDenormFlushToZero: {
1754 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001755 if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) ||
1756 (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) ||
1757 (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001758 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1759 "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device",
1760 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001761 }
1762
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001763 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1764 // Register the first denorm execution mode found
1765 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001766 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001767 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001768 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001769 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001770 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1771 "Shader uses different denorm execution modes for 16 and 64-bit but "
1772 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001773 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001774 }
1775 break;
1776
Mike Schuchardt2df08912020-12-15 16:28:09 -08001777 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001778 break;
1779
Mike Schuchardt2df08912020-12-15 16:28:09 -08001780 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001781 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1782 "Shader uses different denorm execution modes for different bit widths but "
1783 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001784 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001785 break;
1786
1787 default:
1788 break;
1789 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001790 }
1791 break;
1792 }
1793
1794 case spv::ExecutionModeRoundingModeRTE: {
1795 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001796 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) ||
1797 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) ||
1798 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001799 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1800 "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device",
1801 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001802 }
1803
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001804 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1805 // Register the first rounding mode found
1806 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001807 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001808 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001809 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001810 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001811 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1812 "Shader uses different rounding modes for 16 and 64-bit but "
1813 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001814 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001815 }
1816 break;
1817
Mike Schuchardt2df08912020-12-15 16:28:09 -08001818 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001819 break;
1820
Mike Schuchardt2df08912020-12-15 16:28:09 -08001821 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001822 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1823 "Shader uses different rounding modes for different bit widths but "
1824 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001825 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001826 break;
1827
1828 default:
1829 break;
1830 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001831 }
1832 break;
1833 }
1834
1835 case spv::ExecutionModeRoundingModeRTZ: {
1836 auto bit_width = insn.word(3);
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001837 if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) ||
1838 (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) ||
1839 (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001840 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1841 "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device",
1842 bit_width);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001843 }
1844
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001845 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1846 // Register the first rounding mode found
1847 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001848 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001849 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001850 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001851 if (first_rounding_mode.second != 32 && bit_width != 32) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001852 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1853 "Shader uses different rounding modes for 16 and 64-bit but "
1854 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001855 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001856 }
1857 break;
1858
Mike Schuchardt2df08912020-12-15 16:28:09 -08001859 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001860 break;
1861
Mike Schuchardt2df08912020-12-15 16:28:09 -08001862 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001863 skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled,
1864 "Shader uses different rounding modes for different bit widths but "
1865 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001866 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001867 break;
1868
1869 default:
1870 break;
1871 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001872 }
1873 break;
1874 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001875
1876 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001877 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001878 break;
1879 }
1880
1881 case spv::ExecutionModeInvocations: {
1882 invocations = insn.word(3);
1883 break;
1884 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001885 }
1886 }
1887 }
1888
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001889 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001890 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001891 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
1892 "Geometry shader entry point must have an OpExecutionMode instruction that "
1893 "specifies a maximum output vertex count that is greater than 0 and less "
1894 "than or equal to maxGeometryOutputVertices. "
1895 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001896 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001897 }
1898
1899 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001900 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
1901 "Geometry shader entry point must have an OpExecutionMode instruction that "
1902 "specifies an invocation count that is greater than 0 and less "
1903 "than or equal to maxGeometryShaderInvocations. "
1904 "Invocations=%d, maxGeometryShaderInvocations=%d",
1905 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001906 }
1907 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001908 return skip;
1909}
1910
Chris Forbes47567b72017-06-09 12:09:45 -07001911// For given pipelineLayout verify that the set_layout_node at slot.first
1912// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06001913static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001914 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07001915 if (!pipelineLayout) return nullptr;
1916
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001917 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07001918
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001919 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07001920}
1921
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001922// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
1923// o If there is only a vertex shader : gl_PointSize must be written when using points
1924// o If there is a geometry or tessellation shader:
1925// - If shaderTessellationAndGeometryPointSize feature is enabled:
1926// * gl_PointSize must be written in the final geometry stage
1927// - If shaderTessellationAndGeometryPointSize feature is disabled:
1928// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001929bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06001930 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001931 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
1932 return false;
1933 }
1934
1935 bool pointsize_written = false;
1936 bool skip = false;
1937
1938 // Search for PointSize built-in decorations
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001939 for (auto set : src->builtin_decoration_list) {
1940 auto insn = src->at(set.offset);
1941 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001942 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001943 if (pointsize_written) {
1944 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001945 }
1946 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001947 }
1948
1949 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06001950 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001951 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001952 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001953 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
1954 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001955 }
1956 } else if (!pointsize_written) {
1957 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001958 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001959 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
1960 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06001961 }
1962 return skip;
1963}
John Zulauf14c355b2019-06-27 16:09:37 -06001964
Tobias Hector6663c9b2020-11-05 10:18:02 +00001965bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
1966 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
1967 bool primitiverate_written = false;
1968 bool viewportindex_written = false;
1969 bool viewportmask_written = false;
1970 bool skip = false;
1971
1972 // Check if the primitive shading rate is written
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001973 for (auto set : src->builtin_decoration_list) {
1974 auto insn = src->at(set.offset);
1975 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001976 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001977 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001978 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001979 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001980 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00001981 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08001982 if (primitiverate_written && viewportindex_written && viewportmask_written) {
1983 break;
1984 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00001985 }
1986
Tony-LunarGd44844c2021-01-22 13:24:37 -07001987 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06001988 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00001989 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06001990 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001991 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07001992 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
1993 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
1994 "multiple viewports "
1995 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
1996 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00001997 }
1998
1999 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002000 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002001 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2002 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2003 "ViewportIndex built-ins,"
2004 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2005 string_VkShaderStageFlagBits(stage));
2006 }
2007
2008 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002009 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002010 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2011 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2012 "ViewportMaskNV built-ins,"
2013 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2014 string_VkShaderStageFlagBits(stage));
2015 }
2016 }
2017 return skip;
2018}
2019
sfricke-samsung486a51e2021-01-02 00:10:15 -08002020// Validate runtime usage of various opcodes that depends on what Vulkan properties or features are exposed
sfricke-samsung94167ca2021-02-26 04:14:59 -08002021bool CoreChecks::ValidatePropertiesAndFeatures(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002022 bool skip = false;
2023
sfricke-samsung94167ca2021-02-26 04:14:59 -08002024 switch (insn.opcode()) {
2025 case spv::OpReadClockKHR: {
2026 auto scope_id = module->get_def(insn.word(3));
2027 auto scope_type = scope_id.word(3);
2028 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002029 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung94167ca2021-02-26 04:14:59 -08002030 skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderSubgroupClock",
2031 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002032 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002033 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung94167ca2021-02-26 04:14:59 -08002034 skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderDeviceClock",
2035 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002036 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002037 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002038 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002039 }
2040 }
2041 return skip;
2042}
2043
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002044bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2045 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002046 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002047 const auto *pStage = stage_state.create_info;
2048 const auto *module = stage_state.module.get();
2049 const auto &entrypoint = stage_state.entrypoint;
John Zulauf14c355b2019-06-27 16:09:37 -06002050 // Check the module
2051 if (!module->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002052 skip |= LogError(
2053 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2054 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002055 }
2056
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002057 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2058 // specializations should be applied and validated.
2059 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2060 pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) {
2061 // Gather the specialization-constant values.
2062 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002063 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002064 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 -06002065 id_value_map.reserve(specialization_info->mapEntryCount);
2066 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2067 auto const &map_entry = specialization_info->pMapEntries[i];
sfricke-samsung033b0262021-07-09 00:53:06 -07002068 auto itr = module->spec_const_map.find(map_entry.constantID);
2069 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2070 // behavior of the pipeline."
2071 if (itr != module->spec_const_map.cend()) {
2072 // Make sure map_entry.size matches the spec constant's size
2073 uint32_t spec_const_size = decoration_set::kInvalidValue;
2074 const auto def_ins = module->get_def(itr->second);
2075 const auto type_ins = module->get_def(def_ins.word(1));
2076 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2077 switch (type_ins.opcode()) {
2078 case spv::OpTypeBool:
2079 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2080 spec_const_size = sizeof(VkBool32);
2081 break;
2082 case spv::OpTypeInt:
2083 case spv::OpTypeFloat:
2084 spec_const_size = type_ins.word(2) / 8;
2085 break;
2086 default:
2087 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2088 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2089 break;
2090 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002091
sfricke-samsung033b0262021-07-09 00:53:06 -07002092 if (map_entry.size != spec_const_size) {
2093 skip |=
2094 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002095 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2096 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2097 map_entry.constantID, i, map_entry.size,
2098 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002099 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002100 }
2101
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002102 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002103 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2104 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2105 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2106 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2107 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2108
2109 std::copy(start_in_p, end_in_p, out_p);
2110 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002111 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002112 }
2113
2114 // Apply the specialization-constant values and revalidate the shader module.
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002115 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002116 spvtools::Optimizer optimizer(spirv_environment);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002117 spvtools::MessageConsumer consumer = [&skip, &module, &stage_state, this](spv_message_level_t level, const char *source,
2118 const spv_position_t &position,
2119 const char *message) {
2120 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2121 "%s does not contain valid spirv for stage %s. %s",
2122 report_data->FormatHandle(module->vk_shader_module()).c_str(),
2123 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002124 };
2125 optimizer.SetMessageConsumer(consumer);
2126 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2127 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2128 std::vector<uint32_t> specialized_spirv;
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002129 auto const optimized = optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002130 assert(optimized == true);
2131
2132 if (optimized) {
2133 spv_context ctx = spvContextCreate(spirv_environment);
2134 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2135 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002136 spvtools::ValidatorOptions options;
2137 AdjustValidatorOptions(device_extensions, enabled_features, options);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002138 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2139 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002140 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002141 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002142 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002143 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002144 }
2145
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002146 spvDiagnosticDestroy(diag);
2147 spvContextDestroy(ctx);
2148 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002149
2150 skip |= ValidateWorkgroupSize(module, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002151 }
2152
John Zulauf14c355b2019-06-27 16:09:37 -06002153 // Check the entrypoint
2154 if (entrypoint == module->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002155 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2156 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002157 }
2158 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2159
2160 // Mark accessible ids
2161 auto &accessible_ids = stage_state.accessible_ids;
2162
Chris Forbes47567b72017-06-09 12:09:45 -07002163 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002164
sfricke-samsung94167ca2021-02-26 04:14:59 -08002165 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2166 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002167 uint32_t total_shared_size = 0;
sfricke-samsung94167ca2021-02-26 04:14:59 -08002168 for (auto insn : *module) {
2169 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
2170 skip |= ValidatePropertiesAndFeatures(module, insn);
2171 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02002172 total_shared_size += module->CalcComputeSharedMemory(pStage->stage, insn);
2173 }
2174
2175 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2176 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002177 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002178 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002179 }
2180
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002181 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2182 stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002183 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03002184 skip |= ValidateShaderStorageImageFormats(module);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002185 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsung58b84352021-07-31 21:41:04 -07002186 skip |= ValidateAtomicsTypes(module);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002187 skip |= ValidateExecutionModes(module, entrypoint);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002188 skip |= ValidateSpecializations(pStage);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002189 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002190 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002191 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07002192 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002193 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
2194 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
2195 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002196 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
2197 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
2198 }
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002199 if (device_extensions.vk_qcom_render_pass_shader_resolve != kNotEnabled) {
2200 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
2201 }
Chris Forbes47567b72017-06-09 12:09:45 -07002202
sfricke-samsung7699b912021-04-12 23:01:51 -07002203 // "layout must be consistent with the layout of the * shader"
2204 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002205 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002206 switch (pipeline->create_info.graphics.sType) {
2207 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2208 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2209 break;
2210 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2211 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2212 break;
2213 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2214 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2215 break;
2216 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2217 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2218 break;
2219 default:
2220 assert(false);
2221 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002222 }
2223
sfricke-samsung7699b912021-04-12 23:01:51 -07002224 // Validate Push Constants use
2225 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
2226
Chris Forbes47567b72017-06-09 12:09:45 -07002227 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002228 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002229 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002230 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002231 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002232 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2233 std::set<uint32_t> descriptor_types =
2234 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002235
2236 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002237 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002238 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002239 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002240 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002241 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002242 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2243 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002244 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2245 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002246 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002247 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2248 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002249 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002250 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002251 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002252 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002253 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002254 }
2255 }
2256
2257 // Validate use of input attachments against subpass structure
2258 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002259 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002260
Petr Krause91f7a12017-12-14 20:57:36 +01002261 auto rpci = pipeline->rp_state->createInfo.ptr();
Jeremy Gebben11af9792021-08-20 10:20:09 -06002262 auto subpass = pipeline->create_info.graphics.subpass;
Chris Forbes47567b72017-06-09 12:09:45 -07002263
2264 for (auto use : input_attachment_uses) {
2265 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2266 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
Dave Houltona9df0ce2018-02-07 10:51:23 -07002267 ? input_attachments[use.first].attachment
2268 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002269
2270 if (index == VK_ATTACHMENT_UNUSED) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002271 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2272 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsung962cad92021-04-13 00:46:29 -07002273 } else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
Chris Forbes47567b72017-06-09 12:09:45 -07002274 skip |=
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002275 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2276 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
sfricke-samsung962cad92021-04-13 00:46:29 -07002277 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002278 }
2279 }
2280 }
Lockeaa8fdc02019-04-02 11:59:20 -06002281 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08002282 skip |= ValidateComputeWorkGroupSizes(module, entrypoint);
Lockeaa8fdc02019-04-02 11:59:20 -06002283 }
Chris Forbes47567b72017-06-09 12:09:45 -07002284 return skip;
2285}
2286
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002287bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2288 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2289 spirv_inst_iter consumer_entrypoint,
2290 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002291 bool skip = false;
2292
2293 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002294 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2295 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002296
2297 auto a_it = outputs.begin();
2298 auto b_it = inputs.begin();
2299
ziga-lunarg8346fe82021-08-22 17:30:50 +02002300 uint32_t a_component = 0;
2301 uint32_t b_component = 0;
2302
Chris Forbes47567b72017-06-09 12:09:45 -07002303 // Maps sorted by key (location); walk them together to find mismatches
2304 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2305 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2306 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2307 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2308 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2309
ziga-lunarg8346fe82021-08-22 17:30:50 +02002310 a_first.second += a_component;
2311 b_first.second += b_component;
2312
2313 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2314 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2315 assert(a_at_end || a_component < a_length);
2316 assert(b_at_end || b_component < b_length);
2317
Chris Forbes47567b72017-06-09 12:09:45 -07002318 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002319 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002320 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2321 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2322 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2323 a_it++;
2324 a_component = 0;
2325 } else {
2326 a_component++;
2327 }
Chris Forbes47567b72017-06-09 12:09:45 -07002328 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002329 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002330 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2331 b_first.first, b_first.second, producer_stage->name);
2332 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2333 b_it++;
2334 b_component = 0;
2335 } else {
2336 b_component++;
2337 }
Chris Forbes47567b72017-06-09 12:09:45 -07002338 } else {
2339 // subtleties of arrayed interfaces:
2340 // - if is_patch, then the member is not arrayed, even though the interface may be.
2341 // - if is_block_member, then the extra array level of an arrayed interface is not
2342 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002343 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002344 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002345 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002346 producer->DescribeType(a_it->second.type_id).c_str(),
2347 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002348 a_it++;
2349 b_it++;
2350 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002351 }
2352 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002353 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002354 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2355 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2356 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002357 }
2358 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002359 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002360 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2361 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002362 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002363 uint32_t a_remaining = a_length - a_component;
2364 uint32_t b_remaining = b_length - b_component;
2365 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2366 a_it++;
2367 b_it++;
2368 a_component = 0;
2369 b_component = 0;
2370 } else if (a_remaining > b_remaining) { // a has more components remaining
2371 a_component += b_remaining;
2372 b_component = 0;
2373 b_it++;
2374 } else if (b_remaining > a_remaining) { // b has more components remaining
2375 b_component += a_remaining;
2376 a_component = 0;
2377 a_it++;
2378 }
Chris Forbes47567b72017-06-09 12:09:45 -07002379 }
2380 }
2381
Ari Suonpaa696b3432019-03-11 14:02:57 +02002382 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002383 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2384 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002385
2386 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2387 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002388 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002389 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002390 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2391 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002392 } else {
2393 auto it_producer = builtins_producer.begin();
2394 auto it_consumer = builtins_consumer.begin();
2395 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2396 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002397 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002398 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2399 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002400 break;
2401 }
2402 it_producer++;
2403 it_consumer++;
2404 }
2405 }
2406 }
2407 }
2408
Chris Forbes47567b72017-06-09 12:09:45 -07002409 return skip;
2410}
2411
John Zulauf14c355b2019-06-27 16:09:37 -06002412static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002413 uint32_t stage_mask = 0;
2414 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2415 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2416 stage_mask |= pCreateInfo->pStages[i].stage;
2417 }
2418 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002419 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2420 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2421 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002422 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2423 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2424 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2425 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2426 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002427 }
2428 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002429 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002430}
2431
Chris Forbes47567b72017-06-09 12:09:45 -07002432// Validate that the shaders used by the given pipeline and store the active_slots
2433// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002434bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002435 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002436
Chris Forbes47567b72017-06-09 12:09:45 -07002437 bool skip = false;
2438
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002439 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002440
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002441 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
2442 for (auto &stage : pipeline->stage_state) {
2443 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
2444 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
2445 vertex_stage = &stage;
2446 }
2447 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
2448 fragment_stage = &stage;
2449 }
Chris Forbes47567b72017-06-09 12:09:45 -07002450 }
2451
2452 // if the shader stages are no good individually, cross-stage validation is pointless.
2453 if (skip) return true;
2454
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002455 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002456
2457 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002458 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002459 }
2460
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002461 if (vertex_stage && vertex_stage->module->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
2462 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07002463 }
2464
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002465 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
2466 const auto &producer = pipeline->stage_state[i - 1];
2467 const auto &consumer = pipeline->stage_state[i];
2468 assert(producer.module);
2469 if (&producer == fragment_stage) {
2470 break;
2471 }
2472 if (consumer.module) {
2473 if (consumer.module->has_valid_spirv && producer.module->has_valid_spirv) {
2474 auto producer_id = GetShaderStageId(producer.stage_flag);
2475 auto consumer_id = GetShaderStageId(consumer.stage_flag);
2476 skip |=
2477 ValidateInterfaceBetweenStages(producer.module.get(), producer.entrypoint, &shader_stage_attribs[producer_id],
2478 consumer.module.get(), consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002479 }
Chris Forbes47567b72017-06-09 12:09:45 -07002480
Chris Forbes47567b72017-06-09 12:09:45 -07002481 }
2482 }
2483
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002484 if (fragment_stage && fragment_stage->module->has_valid_spirv) {
2485 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002486 create_info->subpass);
Chris Forbes47567b72017-06-09 12:09:45 -07002487 }
2488
2489 return skip;
2490}
2491
Tony-LunarGb2ded512021-02-02 16:03:30 -07002492void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002493 if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports ||
2494 !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) {
2495 return;
2496 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002497
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002498 for (auto &stage : pipeline_state->stage_state) {
2499 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2500 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002501 bool primitiverate_written = false;
Tobias Hector6663c9b2020-11-05 10:18:02 +00002502
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002503 for (auto set : stage.module->builtin_decoration_list) {
2504 auto insn = stage.module->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002505 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002506 primitiverate_written = stage.module->IsBuiltInWritten(insn, stage.entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002507 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002508 if (primitiverate_written) {
2509 break;
2510 }
Tony-LunarGb2ded512021-02-02 16:03:30 -07002511 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002512
Tony-LunarGb2ded512021-02-02 16:03:30 -07002513 if (primitiverate_written) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002514 pipeline_state->wrote_primitive_shading_rate.insert(stage.stage_flag);
Tony-LunarGb2ded512021-02-02 16:03:30 -07002515 }
2516 }
2517 }
2518}
2519
2520bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2521 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002522 bool skip = false;
2523
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002524 for (auto &stage : pipeline->stage_state) {
2525 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2526 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002527 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2528 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002529 if (pipeline->wrote_primitive_shading_rate.find(stage.stage_flag) != pipeline->wrote_primitive_shading_rate.end()) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002530 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002531 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002532 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2533 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2534 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002535 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002536 }
2537 }
2538 }
2539 }
2540
2541 return skip;
2542}
2543
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002544bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002545 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07002546}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002547
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002548uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2549 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002550 const auto &create_info = pipeline->create_info.raytracing;
2551 const auto *stages = create_info.ptr()->pStages;
2552 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002553 if (stages[stage_index].stage == stageBit) {
2554 total++;
2555 }
2556 }
2557
Jeremy Gebben11af9792021-08-20 10:20:09 -06002558 if (create_info.pLibraryInfo) {
2559 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2560 const PIPELINE_STATE *library_pipeline = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002561 total += CalcShaderStageCount(library_pipeline, stageBit);
2562 }
2563 }
2564
2565 return total;
2566}
2567
sourav parmarcd5fb182020-07-17 12:58:44 -07002568bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06002569 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04002570
Jeremy Gebben11af9792021-08-20 10:20:09 -06002571 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002572 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002573 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
2574 skip |=
2575 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
2576 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
2577 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
2578 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002579 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002580 if (create_info.pLibraryInfo) {
2581 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
2582 const PIPELINE_STATE *library_pipelinestate = GetPipelineState(create_info.pLibraryInfo->pLibraries[i]);
2583 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
2584 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002585 skip |= LogError(
2586 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
2587 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
2588 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002589 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07002590 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002591 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
2592 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
2593 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
2594 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002595 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
2596 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
2597 "member must have been created with values of the maxPipelineRayPayloadSize and "
2598 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
2599 }
2600 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002601 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002602 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
2603 "vkCreateRayTracingPipelinesKHR: If flags includes "
2604 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
2605 "the pLibraries member of libraries must have been created with the "
2606 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
2607 }
sourav parmar83c31b12020-05-06 12:30:54 -07002608 }
2609 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002610 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002611 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002612 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
2613 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
2614 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002615 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002616 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002617 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002618 const auto *stages = create_info.ptr()->pStages;
2619 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04002620
Jeremy Gebben11af9792021-08-20 10:20:09 -06002621 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002622 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04002623 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002624
Jeremy Gebben11af9792021-08-20 10:20:09 -06002625 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002626 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
2627 if (raygen_stages_count == 0) {
2628 skip |= LogError(
2629 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07002630 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002631 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
2632 }
Jason Macnak15f95e82019-08-21 21:52:02 -04002633 }
2634
Jeremy Gebben11af9792021-08-20 10:20:09 -06002635 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04002636 const auto &group = groups[group_index];
2637
2638 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002639 if (group.generalShader >= create_info.stageCount ||
Jason Macnak15f95e82019-08-21 21:52:02 -04002640 (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV &&
2641 stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV &&
2642 stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002643 skip |= LogError(device,
2644 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
2645 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
2646 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002647 }
2648 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
2649 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002650 skip |= LogError(device,
2651 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
2652 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
2653 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002654 }
2655 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002656 if (group.intersectionShader >= create_info.stageCount ||
Jason Macnak15f95e82019-08-21 21:52:02 -04002657 stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002658 skip |= LogError(device,
2659 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
2660 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
2661 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002662 }
2663 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
2664 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002665 skip |= LogError(device,
2666 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
2667 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
2668 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002669 }
2670 }
2671
2672 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
2673 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002674 if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= create_info.stageCount ||
Jason Macnak15f95e82019-08-21 21:52:02 -04002675 stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002676 skip |= LogError(device,
2677 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
2678 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
2679 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002680 }
2681 if (group.closestHitShader != VK_SHADER_UNUSED_NV &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002682 (group.closestHitShader >= create_info.stageCount ||
Jason Macnak15f95e82019-08-21 21:52:02 -04002683 stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002684 skip |= LogError(device,
2685 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
2686 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
2687 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04002688 }
2689 }
John Zulaufe4474e72019-07-01 17:28:27 -06002690 }
2691 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05002692}
2693
Dave Houltona9df0ce2018-02-07 10:51:23 -07002694uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07002695
Dave Houltona9df0ce2018-02-07 10:51:23 -07002696static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002697 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06002698 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06002699 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002700 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002701 return nullptr;
2702}
2703
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07002704bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05002705 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002706 bool skip = false;
2707 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002708
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06002709 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07002710 return false;
2711 }
2712
Mark Lobodzinskif45e45f2019-04-19 14:15:39 -06002713 auto have_glsl_shader = device_extensions.vk_nv_glsl_shader;
Chris Forbes4ae55b32017-06-09 14:42:56 -07002714
2715 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002716 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
2717 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
2718 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002719 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07002720 auto cache = GetValidationCacheInfo(pCreateInfo);
2721 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07002722 // If app isn't using a shader validation cache, use the default one from CoreChecks
2723 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07002724 if (cache) {
2725 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002726 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07002727 }
2728
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002729 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
2730 // the default values will be used during validation.
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002731 spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled));
Dave Houlton0ea2d012018-06-21 14:00:26 -06002732 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07002733 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07002734 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002735 spvtools::ValidatorOptions options;
2736 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06002737 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07002738 if (spv_valid != SPV_SUCCESS) {
2739 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002740 if (spv_valid == SPV_WARNING) {
2741 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2742 diag && diag->error ? diag->error : "(no error text)");
2743 } else {
2744 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
2745 diag && diag->error ? diag->error : "(no error text)");
2746 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002747 }
Chris Forbes9a61e082017-07-24 15:35:29 -07002748 } else {
2749 if (cache) {
2750 cache->Insert(hash);
2751 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07002752 }
2753
2754 spvDiagnosticDestroy(diag);
2755 spvContextDestroy(ctx);
2756 }
2757
Chris Forbes4ae55b32017-06-09 14:42:56 -07002758 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07002759}
2760
sfricke-samsung8a7341a2021-02-28 07:30:21 -08002761bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint) const {
Lockeaa8fdc02019-04-02 11:59:20 -06002762 bool skip = false;
2763 uint32_t local_size_x = 0;
2764 uint32_t local_size_y = 0;
2765 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07002766 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06002767 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002768 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002769 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002770 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002771 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06002772 }
2773 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002774 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002775 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002776 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002777 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06002778 }
2779 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002780 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002781 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002782 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002783 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06002784 }
2785
2786 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
2787 uint64_t invocations = local_size_x * local_size_y;
2788 // Prevent overflow.
2789 bool fail = false;
2790 if (invocations > UINT32_MAX || invocations > limit) {
2791 fail = true;
2792 }
2793 if (!fail) {
2794 invocations *= local_size_z;
2795 if (invocations > UINT32_MAX || invocations > limit) {
2796 fail = true;
2797 }
2798 }
2799 if (fail) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002800 skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002801 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
2802 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002803 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 -07002804 limit);
Lockeaa8fdc02019-04-02 11:59:20 -06002805 }
2806 }
2807 return skip;
2808}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06002809
2810spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
2811 if (api_version >= VK_API_VERSION_1_2) {
2812 return SPV_ENV_VULKAN_1_2;
2813 } else if (api_version >= VK_API_VERSION_1_1) {
2814 if (spirv_1_4) {
2815 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
2816 } else {
2817 return SPV_ENV_VULKAN_1_1;
2818 }
2819 }
2820 return SPV_ENV_VULKAN_1_0;
2821}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002822
sfricke-samsungecc112a2021-09-03 05:32:17 -07002823// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06002824void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002825 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07002826 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
2827 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002828 if (device_extensions.vk_khr_relaxed_block_layout) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07002829 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002830 options.SetRelaxBlockLayout(true);
2831 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07002832
2833 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
2834 // Vulkan version used, the feature bit is needed (also described in the spec).
2835
2836 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
2837 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002838 options.SetUniformBufferStandardLayout(true);
2839 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07002840 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
2841 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002842 options.SetScalarBlockLayout(true);
2843 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07002844 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
2845 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08002846 options.SetWorkgroupScalarBlockLayout(true);
2847 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06002848}