blob: ddca563bcd6e7ed6d12c206bd051fb43b6543648 [file] [log] [blame]
Lionel Landwerlin2d9f5632022-01-08 01:12:47 +02001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
4 * Copyright (C) 2015-2022 Google Inc.
Tobias Hector6663c9b2020-11-05 10:18:02 +00005 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Chris Forbes47567b72017-06-09 12:09:45 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Chris Forbes <chrisf@ijw.co.nz>
Dave Houlton51653902018-06-22 17:32:13 -060020 * Author: Dave Houlton <daveh@lunarg.com>
Tobias Hector6663c9b2020-11-05 10:18:02 +000021 * Author: Tobias Hector <tobias.hector@amd.com>
Chris Forbes47567b72017-06-09 12:09:45 -070022 */
23
Petr Kraus25810d02019-08-27 17:41:15 +020024#include "shader_validation.h"
25
Chris Forbes47567b72017-06-09 12:09:45 -070026#include <cassert>
Petr Kraus25810d02019-08-27 17:41:15 +020027#include <cinttypes>
Jeff Bolzf234bf82019-11-04 14:07:15 -060028#include <cmath>
Chris Forbes47567b72017-06-09 12:09:45 -070029#include <sstream>
Petr Kraus25810d02019-08-27 17:41:15 +020030#include <string>
Petr Kraus25810d02019-08-27 17:41:15 +020031#include <vector>
32
Mark Lobodzinski102687e2020-04-28 11:03:28 -060033#include <spirv/unified1/spirv.hpp>
Chris Forbes47567b72017-06-09 12:09:45 -070034#include "vk_enum_string_helper.h"
Chris Forbes47567b72017-06-09 12:09:45 -070035#include "vk_layer_data.h"
Chris Forbes47567b72017-06-09 12:09:45 -070036#include "vk_layer_utils.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070037#include "chassis.h"
Chris Forbes47567b72017-06-09 12:09:45 -070038#include "core_validation.h"
sfricke-samsung3c5dee22021-10-14 09:58:14 -070039#include "spirv_grammar_helper.h"
Petr Kraus25810d02019-08-27 17:41:15 +020040
Chris Forbes9a61e082017-07-24 15:35:29 -070041#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070042
Chris Forbes47567b72017-06-09 12:09:45 -070043static shader_stage_attributes shader_stage_attribs[] = {
Ari Suonpaa696b3432019-03-11 14:02:57 +020044 {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT},
45 {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT},
46 {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT},
47 {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT},
48 {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT},
Chris Forbes47567b72017-06-09 12:09:45 -070049};
50
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020051static const spirv_inst_iter GetBaseTypeIter(SHADER_MODULE_STATE const *src, uint32_t type) {
52 const auto &insn = src->get_def(type);
53 const uint32_t base_insn_id = src->GetBaseType(insn);
54 return src->get_def(base_insn_id);
55}
56
ziga-lunarg8346fe82021-08-22 17:30:50 +020057static bool BaseTypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, const spirv_inst_iter &a_base_insn,
58 const spirv_inst_iter &b_base_insn) {
59 const uint32_t a_opcode = a_base_insn.opcode();
60 const uint32_t b_opcode = b_base_insn.opcode();
61 if (a_opcode == b_opcode) {
62 if (a_opcode == spv::OpTypeInt) {
63 // Match width and signedness
64 return a_base_insn.word(2) == b_base_insn.word(2) && a_base_insn.word(3) == b_base_insn.word(3);
65 } else if (a_opcode == spv::OpTypeFloat) {
66 // Match width
67 return a_base_insn.word(2) == b_base_insn.word(2);
68 } else if (a_opcode == spv::OpTypeStruct) {
69 // Match on all element types
70 if (a_base_insn.len() != b_base_insn.len()) {
71 return false; // Structs cannot match if member counts differ
72 }
73
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020074 for (uint32_t i = 2; i < a_base_insn.len(); i++) {
75 const auto &c_base_insn = GetBaseTypeIter(a, a_base_insn.word(i));
76 const auto &d_base_insn = GetBaseTypeIter(b, b_base_insn.word(i));
77 if (!BaseTypesMatch(a, b, c_base_insn, d_base_insn)) {
ziga-lunarg8346fe82021-08-22 17:30:50 +020078 return false;
79 }
80 }
81
82 return true;
83 }
84 }
85 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070086}
87
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020088static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, uint32_t a_type, uint32_t b_type) {
89 const auto &a_base_insn = GetBaseTypeIter(a, a_type);
90 const auto &b_base_insn = GetBaseTypeIter(b, b_type);
Chris Forbes47567b72017-06-09 12:09:45 -070091
ziga-lunarg8346fe82021-08-22 17:30:50 +020092 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070093}
94
Shannon McPhersonc06c33d2018-06-28 17:21:12 -060095static unsigned GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -070096 switch (format) {
97 case VK_FORMAT_R64G64B64A64_SFLOAT:
98 case VK_FORMAT_R64G64B64A64_SINT:
99 case VK_FORMAT_R64G64B64A64_UINT:
100 case VK_FORMAT_R64G64B64_SFLOAT:
101 case VK_FORMAT_R64G64B64_SINT:
102 case VK_FORMAT_R64G64B64_UINT:
103 return 2;
104 default:
105 return 1;
106 }
107}
108
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600109static unsigned GetFormatType(VkFormat fmt) {
sfricke-samsunge3086292021-11-18 23:02:35 -0800110 if (FormatIsSINT(fmt)) return FORMAT_TYPE_SINT;
111 if (FormatIsUINT(fmt)) return FORMAT_TYPE_UINT;
sfricke-samsunged028b02021-09-06 23:14:51 -0700112 // Formats such as VK_FORMAT_D16_UNORM_S8_UINT are both
Dave Houltona9df0ce2018-02-07 10:51:23 -0700113 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
114 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700115 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
116 return FORMAT_TYPE_FLOAT;
117}
118
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600119static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700120 uint32_t bit_pos = uint32_t(u_ffs(stage));
121 return bit_pos - 1;
122}
123
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700124bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700125 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
126 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700127 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700128 bool skip = false;
129
130 for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) {
131 auto desc = &vi->pVertexBindingDescriptions[i];
132 auto &binding = bindings[desc->binding];
133 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600134 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700135 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
136 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700137 } else {
138 binding = desc;
139 }
140 }
141
142 return skip;
143}
144
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700145bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs,
146 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700147 bool skip = false;
148
sfricke-samsung962cad92021-04-13 00:46:29 -0700149 const auto inputs = vs->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700150
151 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200152 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700153 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200154 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
155 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
156 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700157 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
158 }
159 }
160 }
161
Petr Kraus25810d02019-08-27 17:41:15 +0200162 struct AttribInputPair {
163 const VkVertexInputAttributeDescription *attrib = nullptr;
164 const interface_var *input = nullptr;
165 };
166 std::map<uint32_t, AttribInputPair> location_map;
167 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
168 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700169
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400170 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200171 const auto location = location_it.first;
172 const auto attrib = location_it.second.attrib;
173 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600174
Petr Kraus25810d02019-08-27 17:41:15 +0200175 if (attrib && !input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600176 skip |= LogPerformanceWarning(vs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700177 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200178 } else if (!attrib && input) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600179 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700180 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200181 } else if (attrib && input) {
182 const auto attrib_type = GetFormatType(attrib->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700183 const auto input_type = vs->GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700184
185 // Type checking
186 if (!(attrib_type & input_type)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600187 skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700188 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sfricke-samsung962cad92021-04-13 00:46:29 -0700189 string_VkFormat(attrib->format), location, vs->DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700190 }
Petr Kraus25810d02019-08-27 17:41:15 +0200191 } else { // !attrib && !input
192 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700193 }
194 }
195
196 return skip;
197}
198
Aaron Hagan1209c782021-11-22 19:37:14 -0500199bool CoreChecks::ValidateFsOutputsAgainstDynamicRenderingRenderPass(SHADER_MODULE_STATE const* fs, spirv_inst_iter entrypoint,
200 PIPELINE_STATE const* pipeline) const {
201 bool skip = false;
202
203 struct Attachment {
204 const interface_var* output = nullptr;
205 };
206 std::map<uint32_t, Attachment> location_map;
207
208 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
209 const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
210 for (const auto& output_it : outputs) {
211 auto const location = output_it.first.first;
212 location_map[location].output = &output_it.second;
213 }
214
215 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
216 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
217
Aaron Haganaca50442021-12-07 22:26:29 -0500218 for (uint32_t location = 0; location < location_map.size(); ++location) {
Aaron Hagan1209c782021-11-22 19:37:14 -0500219 const auto output = location_map[location].output;
220
221 if (!output && pipeline->attachments[location].colorWriteMask != 0) {
222 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
223 "Attachment %" PRIu32
224 " not written by fragment shader; undefined values will be written to attachment",
225 location);
Aaron Haganaca50442021-12-07 22:26:29 -0500226 } else if (output &&
227 (location < pipeline->rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount)) {
Aaron Hagan1209c782021-11-22 19:37:14 -0500228 auto format = pipeline->rp_state->dynamic_rendering_pipeline_create_info.pColorAttachmentFormats[location];
229 const auto attachment_type = GetFormatType(format);
230 const auto output_type = fs->GetFundamentalType(output->type_id);
231
232 // Type checking
233 if (!(output_type & attachment_type)) {
234 skip |=
235 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
236 "Attachment %" PRIu32
237 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
238 location, string_VkFormat(format), fs->DescribeType(output->type_id).c_str());
239 }
240 }
241 }
242
243 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
244 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
245 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
246 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
247 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
248 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
249 }
250
251 return skip;
252
253}
254
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700255bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint,
256 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200257 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700258
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600259 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800260 const VkAttachmentReference2 *reference = nullptr;
261 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600262 const interface_var *output = nullptr;
263 };
264 std::map<uint32_t, Attachment> location_map;
265
amhagana448ea52021-11-02 14:09:14 -0400266 if (pipeline->rp_state && !pipeline->rp_state->use_dynamic_rendering) {
267 const auto rpci = pipeline->rp_state->createInfo.ptr();
268 const auto subpass = rpci->pSubpasses[subpass_index];
269 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
270 auto const &reference = subpass.pColorAttachments[i];
271 location_map[i].reference = &reference;
272 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
273 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
274 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
275 }
Chris Forbes47567b72017-06-09 12:09:45 -0700276 }
277 }
278
Chris Forbes47567b72017-06-09 12:09:45 -0700279 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
280
sfricke-samsung962cad92021-04-13 00:46:29 -0700281 const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600282 for (const auto &output_it : outputs) {
283 auto const location = output_it.first.first;
284 location_map[location].output = &output_it.second;
285 }
Chris Forbes47567b72017-06-09 12:09:45 -0700286
Jeremy Gebben11af9792021-08-20 10:20:09 -0600287 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
288 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700289
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400290 for (const auto &location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600291 const auto reference = location_it.second.reference;
292 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
293 continue;
294 }
295
Petr Kraus25810d02019-08-27 17:41:15 +0200296 const auto location = location_it.first;
297 const auto attachment = location_it.second.attachment;
298 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +0200299 if (attachment && !output) {
300 if (pipeline->attachments[location].colorWriteMask != 0) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600301 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700302 "Attachment %" PRIu32
303 " not written by fragment shader; undefined values will be written to attachment",
304 location);
Petr Kraus25810d02019-08-27 17:41:15 +0200305 }
306 } else if (!attachment && output) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700307 if (!(alpha_to_coverage_enabled && location == 0)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600308 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700309 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200310 }
Petr Kraus25810d02019-08-27 17:41:15 +0200311 } else if (attachment && output) {
312 const auto attachment_type = GetFormatType(attachment->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700313 const auto output_type = fs->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700314
315 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +0200316 if (!(output_type & attachment_type)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700317 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600318 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700319 "Attachment %" PRIu32
320 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sfricke-samsung962cad92021-04-13 00:46:29 -0700321 location, string_VkFormat(attachment->format), fs->DescribeType(output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700322 }
Petr Kraus25810d02019-08-27 17:41:15 +0200323 } else { // !attachment && !output
324 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700325 }
326 }
327
Petr Kraus25810d02019-08-27 17:41:15 +0200328 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700329 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
sfricke-samsung962cad92021-04-13 00:46:29 -0700330 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700331 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600332 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700333 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200334 }
335
Chris Forbes47567b72017-06-09 12:09:45 -0700336 return skip;
337}
338
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600339PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
340 const shader_struct_member &push_constant_used_in_shader,
341 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600342 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600343 const auto used_bytes_size = used_bytes->size();
344 if (used_bytes_size == 0) return PC_Byte_Updated;
345
346 const auto push_constant_data_update_size = push_constant_data_update.size();
347 const auto *data = push_constant_data_update.data();
348 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
349 if (used_bytes_size <= push_constant_data_update_size) {
350 return PC_Byte_Updated;
351 }
352 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
353
354 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
355 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
356 return PC_Byte_Updated;
357 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600358 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600359
locke-lunargde3f0fa2020-09-10 11:55:31 -0600360 uint32_t i = 0;
361 for (const auto used : *used_bytes) {
362 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600363 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600364 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600365 return PC_Byte_Not_Set;
366 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600367 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600368 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600369 }
370 }
371 ++i;
372 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600373 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600374}
375
376bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src,
sfricke-samsung7699b912021-04-12 23:01:51 -0700377 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700378 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700379 // Temp workaround to prevent false positive errors
380 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600381 if (src->HasMultipleEntryPoints()) {
sfricke-samsung5c65b372021-03-25 05:39:57 -0700382 return skip;
383 }
384
Chris Forbes47567b72017-06-09 12:09:45 -0700385 // 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 -0700386 const auto *entrypoint = src->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600387 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
388 return skip;
389 }
390 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700391
locke-lunargde3f0fa2020-09-10 11:55:31 -0600392 bool found_stage = false;
393 for (auto const &range : *push_constant_ranges) {
394 if (range.stageFlags & pStage->stage) {
395 found_stage = true;
396 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600397 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600398 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600399 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600400 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600401 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600402 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600403 const auto ret =
404 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700405
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600406 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600407 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600408 LogObjectList objlist(src->vk_shader_module());
409 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700410 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 -0600411 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600412 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600413 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700414 }
415 }
416 }
417
locke-lunargde3f0fa2020-09-10 11:55:31 -0600418 if (!found_stage) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600419 LogObjectList objlist(src->vk_shader_module());
420 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700421 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 -0600422 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module()).c_str(),
423 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700424 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700425 }
Chris Forbes47567b72017-06-09 12:09:45 -0700426 return skip;
427}
428
sfricke-samsungcfb44592021-07-25 00:36:28 -0700429bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700430 bool skip = false;
431
432 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700433 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700434 return skip;
435 }
436
sfricke-samsungcfb44592021-07-25 00:36:28 -0700437 // Find all builtin from just the interface variables
438 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700439 auto insn = src->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700440 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700441 const decoration_set decorations = src->get_decorations(insn.word(2));
442
sfricke-samsungcfb44592021-07-25 00:36:28 -0700443 // Currently don't need to search in structs
444 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700445 auto type_pointer = src->get_def(insn.word(1));
446 assert(type_pointer.opcode() == spv::OpTypePointer);
447
448 auto type = src->get_def(type_pointer.word(3));
449 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700450 uint32_t length = static_cast<uint32_t>(src->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700451 // Handles both the input and output sampleMask
452 if (length > phys_dev_props.limits.maxSampleMaskWords) {
453 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
454 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
455 "maxSampleMaskWords of %u in %s.",
456 length, phys_dev_props.limits.maxSampleMaskWords,
457 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700458 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700459 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700460 }
461 }
462 }
463
464 return skip;
465}
466
Chris Forbes47567b72017-06-09 12:09:45 -0700467// Validate that data for each specialization entry is fully contained within the buffer.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200468bool CoreChecks::ValidateSpecializations(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700469 bool skip = false;
470
471 VkSpecializationInfo const *spec = info->pSpecializationInfo;
472
473 if (spec) {
474 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600475 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700476 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
477 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200478 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700479 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
480 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600481
482 continue;
483 }
Chris Forbes47567b72017-06-09 12:09:45 -0700484 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700485 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
486 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200487 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700488 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
489 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700490 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200491 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
492 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
493 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
494 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
495 j, spec->pMapEntries[i].constantID);
496 }
497 }
Chris Forbes47567b72017-06-09 12:09:45 -0700498 }
499 }
500
501 return skip;
502}
503
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500504// TODO (jbolz): Can this return a const reference?
sourav parmarcd5fb182020-07-17 12:58:44 -0700505static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count,
506 bool is_khr) {
Chris Forbes47567b72017-06-09 12:09:45 -0700507 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800508 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700509 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500510 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700511
512 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
Jeff Bolzfdf96072018-04-10 14:32:18 -0500513 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
514 if (type.opcode() == spv::OpTypeRuntimeArray) {
515 descriptor_count = 0;
516 type = module->get_def(type.word(2));
517 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700518 descriptor_count *= module->GetConstantValueById(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700519 type = module->get_def(type.word(2));
520 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800521 if (type.word(2) == spv::StorageClassStorageBuffer) {
522 is_storage_buffer = true;
523 }
Chris Forbes47567b72017-06-09 12:09:45 -0700524 type = module->get_def(type.word(3));
525 }
526 }
527
528 switch (type.opcode()) {
529 case spv::OpTypeStruct: {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600530 for (const auto insn : module->GetDecorationInstructions()) {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800531 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700532 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800533 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500534 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
535 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
536 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800537 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500538 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
539 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
540 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
541 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800542 }
Chris Forbes47567b72017-06-09 12:09:45 -0700543 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500544 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
545 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
546 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700547 }
548 }
549 }
550
551 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500552 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700553 }
554
555 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500556 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
557 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
558 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700559
Chris Forbes73c00bf2018-06-22 16:28:06 -0700560 case spv::OpTypeSampledImage: {
561 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
562 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
563 auto image_type = module->get_def(type.word(2));
564 auto dim = image_type.word(3);
565 auto sampled = image_type.word(7);
566 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500567 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
568 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700569 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700570 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500571 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
572 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700573
574 case spv::OpTypeImage: {
575 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
576 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
577 auto dim = type.word(3);
578 auto sampled = type.word(7);
579
580 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500581 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
582 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700583 } else if (dim == spv::DimBuffer) {
584 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500585 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
586 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700587 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500588 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
589 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700590 }
591 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500592 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
593 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
594 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700595 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500596 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
597 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700598 }
599 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600600 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700601 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
602 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500603 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700604
605 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
606 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500607 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700608 }
609}
610
Jeff Bolze54ae892018-09-08 12:16:29 -0500611static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700612 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500613 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
614 if (ss.tellp()) ss << ", ";
615 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700616 }
617 return ss.str();
618}
619
sfricke-samsung0065ce02020-12-03 22:46:37 -0800620bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500621 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800622 if (LogError(device, vuid, "Shader requires flag %s set in %s but it is not set on the device", flag, structure)) {
Jeff Bolzee743412019-06-20 22:24:32 -0500623 return true;
624 }
625 }
626
627 return false;
628}
629
sfricke-samsung0065ce02020-12-03 22:46:37 -0800630bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700631 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800632 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700633 return true;
634 }
635 }
636
637 return false;
638}
639
locke-lunarg63e4daf2020-08-17 17:53:25 -0600640bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
641 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500642 bool skip = false;
643
locke-lunarg63e4daf2020-08-17 17:53:25 -0600644 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800645 switch (stage) {
646 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -0600647 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
648 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
649 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
650 case VK_SHADER_STAGE_MISS_BIT_NV:
651 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
652 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
653 case VK_SHADER_STAGE_TASK_BIT_NV:
654 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -0800655 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -0600656 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -0800657 break;
658 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800659 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700660 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800661 break;
662 default:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800663 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700664 "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800665 break;
666 }
667 }
668
Chris Forbes47567b72017-06-09 12:09:45 -0700669 return skip;
670}
671
sfricke-samsung94167ca2021-02-26 04:14:59 -0800672bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
673 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500674 bool skip = false;
675
sfricke-samsung94167ca2021-02-26 04:14:59 -0800676 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
677 if (GroupOperation(insn.opcode()) == true) {
678 // Check the quad operations.
679 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
680 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700681 skip |=
682 RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
683 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800684 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800685 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500686
sfricke-samsung94167ca2021-02-26 04:14:59 -0800687 uint32_t scope_type = spv::ScopeMax;
688 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
689 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
690 scope_type = spv::ScopeSubgroup;
691 } else {
692 // "All <id> used for Scope <id> must be of an OpConstant"
693 auto scope_id = module->get_def(insn.word(3));
694 scope_type = scope_id.word(3);
695 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800696
sfricke-samsung94167ca2021-02-26 04:14:59 -0800697 if (scope_type == spv::ScopeSubgroup) {
698 // "Group operations with subgroup scope" must have stage support
699 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
700 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700701 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800702 }
703
704 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
705 auto type = module->get_def(insn.word(1));
706
707 if (type.opcode() == spv::OpTypeVector) {
708 // Get the element type
709 type = module->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800710 }
711
sfricke-samsung94167ca2021-02-26 04:14:59 -0800712 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800713 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
714 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500715
sfricke-samsung0065ce02020-12-03 22:46:37 -0800716 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
717 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
718 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
719 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700720 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500721 }
722 }
723 }
Jeff Bolzee743412019-06-20 22:24:32 -0500724 }
725
726 return skip;
727}
728
ziga-lunarg70651522021-10-11 17:23:30 +0200729bool CoreChecks::ValidateMemoryScope(SHADER_MODULE_STATE const *src, const spirv_inst_iter &insn) const {
730 bool skip = false;
731
sfricke-samsung3a25ed52022-01-20 02:24:36 -0800732 const auto &entry = OpcodeMemoryScopePosition(insn.opcode());
ziga-lunarg70651522021-10-11 17:23:30 +0200733 if (entry > 0) {
734 const uint32_t scope_id = insn.word(entry);
735 if (enabled_features.core12.vulkanMemoryModel && !enabled_features.core12.vulkanMemoryModelDeviceScope) {
736 const auto &iter = src->GetConstantDef(scope_id);
737 if (iter != src->end()) {
738 if (GetConstantValue(iter) == spv::Scope::ScopeDevice) {
739 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06265",
740 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is enabled and "
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800741 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModelDeviceScope is disabled, but\n%s\nuses "
742 "Device memory scope.",
743 src->DescribeInstruction(insn).c_str());
ziga-lunarg70651522021-10-11 17:23:30 +0200744 }
745 }
746 } else if (!enabled_features.core12.vulkanMemoryModel) {
747 const auto &iter = src->GetConstantDef(scope_id);
748 if (iter != src->end()) {
749 if (GetConstantValue(iter) == spv::Scope::ScopeQueueFamily) {
750 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06266",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800751 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is not enabled, but\n%s\nuses "
752 "QueueFamily memory scope.",
753 src->DescribeInstruction(insn).c_str());
ziga-lunarg70651522021-10-11 17:23:30 +0200754 }
755 }
756 }
757 }
758
759 return skip;
760}
761
ziga-lunarg2818f492021-08-12 14:30:51 +0200762bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
763 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
764 bool skip = false;
765
766 std::array<uint32_t, 3> work_group_size = src->GetWorkgroupSize(pStage, id_value_map);
767
768 for (uint32_t i = 0; i < 3; ++i) {
769 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
770 const char member = 'x' + static_cast<int8_t>(i);
771 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
772 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
773 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
774 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
775 }
776 }
777 return skip;
778}
779
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600780bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600781 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200782 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
783 pStage->stage == VK_SHADER_STAGE_ALL) {
784 return false;
785 }
786
787 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700788 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200789
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700790 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200791 struct Variable {
792 uint32_t baseTypePtrID;
793 uint32_t ID;
794 uint32_t storageClass;
795 };
796 std::vector<Variable> variables;
797
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700798 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700799 bool is_iso_lines = false;
800 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500801
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700802 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600803
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200804 for (auto insn : *src) {
805 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500806 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200807 case spv::OpDecorate:
808 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500809 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700810 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200811 break;
812 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200813 default:
814 break;
815 }
816 break;
817 // Find all input and output variables
818 case spv::OpVariable: {
819 Variable var = {};
820 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600821 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
822 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700823 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200824 var.baseTypePtrID = insn.word(1);
825 var.ID = insn.word(2);
826 variables.push_back(var);
827 }
828 break;
829 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500830 case spv::OpExecutionMode:
831 if (insn.word(1) == entrypoint.word(2)) {
832 switch (insn.word(2)) {
833 default:
834 break;
835 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700836 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500837 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700838 case spv::ExecutionModeIsolines:
839 is_iso_lines = true;
840 break;
841 case spv::ExecutionModePointMode:
842 is_point_mode = true;
843 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500844 }
845 }
846 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200847 default:
848 break;
849 }
850 }
851
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500852 bool strip_output_array_level =
853 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
854 bool strip_input_array_level =
855 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
856 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
857
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700858 uint32_t num_comp_in = 0, num_comp_out = 0;
859 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600860
sfricke-samsung962cad92021-04-13 00:46:29 -0700861 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
862 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600863
864 // Find max component location used for input variables.
865 for (auto &var : inputs) {
866 int location = var.first.first;
867 int component = var.first.second;
868 interface_var &iv = var.second;
869
870 // Only need to look at the first location, since we use the type's whole size
871 if (iv.offset != 0) {
872 continue;
873 }
874
875 if (iv.is_patch) {
876 continue;
877 }
878
sfricke-samsung962cad92021-04-13 00:46:29 -0700879 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700880 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600881 }
882
883 // Find max component location used for output variables.
884 for (auto &var : outputs) {
885 int location = var.first.first;
886 int component = var.first.second;
887 interface_var &iv = var.second;
888
889 // Only need to look at the first location, since we use the type's whole size
890 if (iv.offset != 0) {
891 continue;
892 }
893
894 if (iv.is_patch) {
895 continue;
896 }
897
sfricke-samsung962cad92021-04-13 00:46:29 -0700898 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700899 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600900 }
901
902 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
903 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700904 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
905 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200906 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500907 // Check if the variable is a patch. Patches can also be members of blocks,
908 // but if they are then the top-level arrayness has already been stripped
909 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700910 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200911
912 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700913 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200914 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700915 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200916 }
917 }
918
919 switch (pStage->stage) {
920 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700921 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700922 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700923 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
924 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
925 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700926 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200927 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700928 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700929 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700930 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
931 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
932 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600933 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200934 break;
935
936 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700937 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700938 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700939 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
940 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
941 "components by %u components",
942 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700943 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200944 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700945 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600946 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700947 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700948 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
949 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
950 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600951 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700952 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700953 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700954 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
955 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
956 "components by %u components",
957 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700958 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200959 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700960 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600961 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700962 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700963 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
964 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
965 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600966 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200967 break;
968
969 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700970 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700971 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700972 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
973 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
974 "components by %u components",
975 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700976 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200977 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700978 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600979 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700980 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700981 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
982 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
983 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600984 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700985 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700986 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700987 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
988 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
989 "components by %u components",
990 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700991 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200992 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700993 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600994 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700995 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700996 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
997 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
998 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600999 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -07001000 // Portability validation
1001 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
1002 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001003 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -07001004 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
1005 " is using abstract patch type IsoLines, but this is not supported on this platform");
1006 }
1007 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001008 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -07001009 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
1010 " is using abstract patch type PointMode, but this is not supported on this platform");
1011 }
1012 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001013 break;
1014
1015 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001016 if (num_comp_in > limits.maxGeometryInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001017 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001018 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1019 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
1020 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001021 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001022 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001023 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001024 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001025 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
1026 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
1027 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001028 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001029 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001030 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001031 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1032 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
1033 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001034 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001035 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001036 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001037 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001038 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
1039 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
1040 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001041 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001042 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001043 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001044 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1045 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
1046 "components by %u components",
1047 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001048 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001049 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001050 break;
1051
1052 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001053 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001054 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001055 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
1056 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
1057 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001058 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001059 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001060 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001061 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001062 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1063 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1064 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001065 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001066 break;
1067
Jeff Bolz148d94e2018-12-13 21:25:56 -06001068 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1069 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1070 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1071 case VK_SHADER_STAGE_MISS_BIT_NV:
1072 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1073 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1074 case VK_SHADER_STAGE_TASK_BIT_NV:
1075 case VK_SHADER_STAGE_MESH_BIT_NV:
1076 break;
1077
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001078 default:
1079 assert(false); // This should never happen
1080 }
1081 return skip;
1082}
1083
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001084bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src, const spirv_inst_iter &insn) const {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001085 bool skip = false;
1086
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001087 switch (insn.opcode()) {
1088 // Go through all ImageRead/Write instructions
1089 case spv::OpImageSparseRead:
1090 case spv::OpImageRead: {
1091 // spirv-val validates this is an OpTypeImage
1092 const uint32_t image = src->GetTypeId(insn.word(3));
1093 const spirv_inst_iter image_def = src->get_def(image);
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001094
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001095 const uint32_t dim = image_def.word(3);
1096 const uint32_t image_format = image_def.word(8);
1097 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1098 // StorageImageReadWithoutFormat Capability was declared.
1099 if (dim != spv::DimSubpassData && image_format == spv::ImageFormatUnknown) {
1100 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1101 "shaderStorageImageReadWithoutFormat", kVUID_Features_shaderStorageImageReadWithoutFormat);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001102 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001103 break;
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001104 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001105 case spv::OpImageWrite: {
1106 // spirv-val validates this is an OpTypeImage
1107 const uint32_t image = src->GetTypeId(insn.word(1));
1108 const spirv_inst_iter image_def = src->get_def(image);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001109
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001110 const uint32_t image_format = image_def.word(8);
1111 if (image_format == spv::ImageFormatUnknown) {
1112 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1113 "shaderStorageImageWriteWithoutFormat", kVUID_Features_shaderStorageImageWriteWithoutFormat);
1114 }
1115 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001116 }
1117
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001118 // Go through all variables for images and check decorations
1119 case spv::OpVariable: {
1120 // spirv-val validates this is an OpTypePointer
1121 const spirv_inst_iter pointer_def = src->get_def(insn.word(1));
1122 if (pointer_def.word(2) != spv::StorageClassUniformConstant) {
1123 break; // Vulkan Spec says storage image must be UniformConstant
1124 }
1125 spirv_inst_iter type_def = src->get_def(pointer_def.word(3));
1126
1127 // Unpack an optional level of arraying
1128 if (type_def.opcode() == spv::OpTypeArray || type_def.opcode() == spv::OpTypeRuntimeArray) {
1129 type_def = src->get_def(type_def.word(2));
1130 }
1131
1132 if (type_def != src->end() && type_def.opcode() == spv::OpTypeImage) {
1133 // Only check if the Image Dim operand is not SubpassData
1134 const uint32_t dim = type_def.word(3);
1135 // Only check storage images
1136 const uint32_t sampled = type_def.word(7);
1137 const uint32_t image_format = type_def.word(8);
1138 if ((dim == spv::DimSubpassData) || (sampled != 2) || (image_format != spv::ImageFormatUnknown)) {
1139 break;
1140 }
1141
1142 const uint32_t var_id = insn.word(2);
1143 decoration_set img_decorations = src->get_decorations(var_id);
1144
1145 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1146 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1147 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
1148 "shaderStorageImageReadWithoutFormat not supported but OpVariable (ID %" PRIu32
1149 ") with a Unknown format is not decorated with NonReadable",
1150 var_id);
1151 }
1152
1153 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1154 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1155 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
1156 "shaderStorageImageWriteWithoutFormat not supported but OpVariable (ID %" PRIu32
1157 ") with a Unknown format is not decorated with NonWritable",
1158 var_id);
1159 }
1160 }
1161 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001162 }
1163 }
1164
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001165 return skip;
1166}
1167
sfricke-samsungdc96f302020-03-18 20:42:10 -07001168bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1169 bool skip = false;
1170 uint32_t total_resources = 0;
1171
1172 // Only currently testing for graphics and compute pipelines
1173 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1174 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1175 return false;
1176 }
1177
1178 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
amhagana448ea52021-11-02 14:09:14 -04001179 if (pipeline->rp_state->use_dynamic_rendering) {
Aaron Hagan92a44f82021-11-19 09:34:56 -05001180 total_resources += pipeline->rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001181 } else {
1182 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
1183 total_resources +=
1184 pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
1185 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001186 }
1187
1188 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1189 // input from CreatePipeline and CreatePipelineLayout level
1190 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1191 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1192 continue;
1193 }
1194
1195 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1196 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1197 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1198 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1199 // Check only descriptor types listed in maxPerStageResources description in spec
1200 switch (binding->descriptorType) {
1201 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1202 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1203 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1204 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1205 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1206 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1207 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1208 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1209 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1210 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1211 total_resources += binding->descriptorCount;
1212 break;
1213 default:
1214 break;
1215 }
1216 }
1217 }
1218 }
1219
1220 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1221 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1222 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001223 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001224 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1225 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1226 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1227 }
1228
1229 return skip;
1230}
1231
Jeff Bolze4356752019-03-07 11:23:46 -06001232// copy the specialization constant value into buf, if it is present
1233void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1234 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1235
1236 if (spec && spec_id < spec->mapEntryCount) {
1237 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1238 }
1239}
1240
1241// Fill in value with the constant or specialization constant value, if available.
1242// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001243static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001244 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001245 auto type_id = src->get_def(insn.word(1));
1246 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1247 return false;
1248 }
1249 switch (insn.opcode()) {
1250 case spv::OpSpecConstant:
1251 *value = insn.word(3);
1252 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1253 return true;
1254 case spv::OpConstant:
1255 *value = insn.word(3);
1256 return true;
1257 default:
1258 return false;
1259 }
1260}
1261
1262// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001263VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001264 switch (insn.opcode()) {
1265 case spv::OpTypeInt:
1266 switch (insn.word(2)) {
1267 case 8:
1268 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1269 case 16:
1270 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1271 case 32:
1272 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1273 case 64:
1274 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1275 default:
1276 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1277 }
1278 case spv::OpTypeFloat:
1279 switch (insn.word(2)) {
1280 case 16:
1281 return VK_COMPONENT_TYPE_FLOAT16_NV;
1282 case 32:
1283 return VK_COMPONENT_TYPE_FLOAT32_NV;
1284 case 64:
1285 return VK_COMPONENT_TYPE_FLOAT64_NV;
1286 default:
1287 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1288 }
1289 default:
1290 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1291 }
1292}
1293
1294// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1295// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001296bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001297 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001298 bool skip = false;
1299
1300 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001301 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001302 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001303 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001304
1305 struct CoopMatType {
1306 uint32_t scope, rows, cols;
1307 VkComponentTypeNV component_type;
1308 bool all_constant;
1309
1310 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1311
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001312 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001313 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001314 spirv_inst_iter insn = src->get_def(id);
1315 uint32_t component_type_id = insn.word(2);
1316 uint32_t scope_id = insn.word(3);
1317 uint32_t rows_id = insn.word(4);
1318 uint32_t cols_id = insn.word(5);
1319 auto component_type_iter = src->get_def(component_type_id);
1320 auto scope_iter = src->get_def(scope_id);
1321 auto rows_iter = src->get_def(rows_id);
1322 auto cols_iter = src->get_def(cols_id);
1323
1324 all_constant = true;
1325 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1326 all_constant = false;
1327 }
1328 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1329 all_constant = false;
1330 }
1331 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1332 all_constant = false;
1333 }
1334 component_type = GetComponentType(component_type_iter, src);
1335 }
1336 };
1337
1338 bool seen_coopmat_capability = false;
1339
1340 for (auto insn : *src) {
1341 // Whitelist instructions whose result can be a cooperative matrix type, and
1342 // keep track of their types. It would be nice if SPIRV-Headers generated code
1343 // to identify which instructions have a result type and result id. Lacking that,
1344 // this whitelist is based on the set of instructions that
1345 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1346 switch (insn.opcode()) {
1347 case spv::OpLoad:
1348 case spv::OpCooperativeMatrixLoadNV:
1349 case spv::OpCooperativeMatrixMulAddNV:
1350 case spv::OpSNegate:
1351 case spv::OpFNegate:
1352 case spv::OpIAdd:
1353 case spv::OpFAdd:
1354 case spv::OpISub:
1355 case spv::OpFSub:
1356 case spv::OpFDiv:
1357 case spv::OpSDiv:
1358 case spv::OpUDiv:
1359 case spv::OpMatrixTimesScalar:
1360 case spv::OpConstantComposite:
1361 case spv::OpCompositeConstruct:
1362 case spv::OpConvertFToU:
1363 case spv::OpConvertFToS:
1364 case spv::OpConvertSToF:
1365 case spv::OpConvertUToF:
1366 case spv::OpUConvert:
1367 case spv::OpSConvert:
1368 case spv::OpFConvert:
1369 id_to_type_id[insn.word(2)] = insn.word(1);
1370 break;
1371 default:
1372 break;
1373 }
1374
1375 switch (insn.opcode()) {
1376 case spv::OpDecorate:
1377 if (insn.word(2) == spv::DecorationSpecId) {
1378 id_to_spec_id[insn.word(1)] = insn.word(3);
1379 }
1380 break;
1381 case spv::OpCapability:
1382 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1383 seen_coopmat_capability = true;
1384
1385 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001386 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001387 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001388 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1389 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001390 }
1391 }
1392 break;
1393 case spv::OpMemoryModel:
1394 // If the capability isn't enabled, don't bother with the rest of this function.
1395 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1396 if (!seen_coopmat_capability) {
1397 return skip;
1398 }
1399 break;
1400 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001401 CoopMatType m;
1402 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001403
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001404 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001405 // Validate that the type parameters are all supported for one of the
1406 // operands of a cooperative matrix property.
1407 bool valid = false;
1408 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001409 if (cooperative_matrix_properties[i].AType == m.component_type &&
1410 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1411 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001412 valid = true;
1413 break;
1414 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001415 if (cooperative_matrix_properties[i].BType == m.component_type &&
1416 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1417 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001418 valid = true;
1419 break;
1420 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001421 if (cooperative_matrix_properties[i].CType == m.component_type &&
1422 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1423 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001424 valid = true;
1425 break;
1426 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001427 if (cooperative_matrix_properties[i].DType == m.component_type &&
1428 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1429 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001430 valid = true;
1431 break;
1432 }
1433 }
1434 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001435 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001436 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1437 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001438 }
1439 }
1440 break;
1441 }
1442 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001443 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001444 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1445 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1446 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1447 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001448 // Couldn't find type of matrix
1449 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001450 break;
1451 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001452 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1453 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1454 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1455 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001456
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001457 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001458 // Validate that the type parameters are all supported for the same
1459 // cooperative matrix property.
1460 bool valid = false;
1461 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001462 if (cooperative_matrix_properties[i].AType == a.component_type &&
1463 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1464 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001465
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001466 cooperative_matrix_properties[i].BType == b.component_type &&
1467 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1468 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001469
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001470 cooperative_matrix_properties[i].CType == c.component_type &&
1471 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1472 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001473
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001474 cooperative_matrix_properties[i].DType == d.component_type &&
1475 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1476 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001477 valid = true;
1478 break;
1479 }
1480 }
1481 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001482 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001483 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1484 "VkCooperativeMatrixPropertiesNV",
1485 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001486 }
1487 }
1488 break;
1489 }
1490 default:
1491 break;
1492 }
1493 }
1494
1495 return skip;
1496}
1497
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001498bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1499 const PIPELINE_STATE *pipeline) const {
1500 bool skip = false;
1501
1502 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1503 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1504 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1505 for (auto insn : *src) {
1506 switch (insn.opcode()) {
1507 case spv::OpCapability:
1508 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1509 auto subpass_flags =
1510 (pipeline->rp_state == nullptr)
1511 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001512 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001513 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1514 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001515 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001516 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1517 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1518 }
1519 }
1520 break;
1521 default:
1522 break;
1523 }
1524 }
1525 }
1526
1527 return skip;
1528}
1529
ziga-lunarg73163742021-08-25 13:15:29 +02001530bool CoreChecks::ValidateShaderSubgroupSizeControl(VkPipelineShaderStageCreateInfo const *pStage) const {
1531 bool skip = false;
1532
1533 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001534 !enabled_features.core13.subgroupSizeControl) {
ziga-lunarg73163742021-08-25 13:15:29 +02001535 skip |= LogError(
1536 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1537 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1538 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1539 }
1540
1541 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001542 !enabled_features.core13.computeFullSubgroups) {
ziga-lunarg73163742021-08-25 13:15:29 +02001543 skip |= LogError(
1544 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1545 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1546 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1547 }
1548
1549 return skip;
1550}
1551
sfricke-samsung58b84352021-07-31 21:41:04 -07001552bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *src) const {
1553 bool skip = false;
1554
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001555 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001556 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001557
sfricke-samsungf5042b12021-08-05 01:09:40 -07001558 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1559 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1560
1561 const bool valid_storage_buffer_float = (
1562 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1563 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1564 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1565 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1566 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1567 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1568 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1569 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1570 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1571
1572 const bool valid_workgroup_float = (
1573 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1574 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1575 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1576 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1577 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1578 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1579 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1580 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1581 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1582
1583 const bool valid_image_float = (
1584 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1585 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1586 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1587
1588 const bool valid_16_float = (
1589 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1590 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1591 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1592 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1593 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1594 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1595
1596 const bool valid_32_float = (
1597 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1598 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1599 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1600 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1601 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1602 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1603 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1604 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1605 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1606
1607 const bool valid_64_float = (
1608 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1609 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1610 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1611 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1612 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1613 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1614 // clang-format on
1615
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001616 for (const auto &atomic_inst : src->GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001617 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001618 const spirv_inst_iter atomic_def = src->at(atomic_inst.first);
1619 const uint32_t opcode = atomic_def.opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001620
1621 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001622 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001623 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1624 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001625 skip |= LogError(device, "VUID-RuntimeSpirv-None-06278",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001626 "%s: Can't use 64-bit int atomics operations\n%s\nwith %s storage class without "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001627 "shaderBufferInt64Atomics enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001628 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1629 src->DescribeInstruction(atomic_def).c_str(), StorageClassName(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001630 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1631 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001632 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001633 "%s: Can't use 64-bit int atomics operations\n%s\nwith Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001634 "shaderSharedInt64Atomics enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001635 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1636 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001637 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001638 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001639 "%s: Can't use 64-bit int atomics operations\n%s\nwith Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001640 "shaderImageInt64Atomics enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001641 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1642 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001643 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001644 } else if (atomic.type == spv::OpTypeFloat) {
1645 // Validate Floats
1646 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1647 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001648 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1649 : "VUID-RuntimeSpirv-None-06280";
1650 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001651 "%s: Can't use float atomics operations\n%s\nwith StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001652 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1653 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1654 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1655 "shaderBufferFloat64AtomicMinMax enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001656 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1657 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001658 } else if (opcode == spv::OpAtomicFAddEXT) {
1659 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1660 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001661 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001662 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001663 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1664 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001665 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1666 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001667 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001668 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001669 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1670 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001671 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1672 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001673 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001674 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001675 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1676 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001677 }
1678 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1679 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001680 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1681 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1682 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
1683 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1684 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001685 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001686 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1687 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1688 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
1689 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1690 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001691 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001692 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1693 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1694 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
1695 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1696 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001697 }
1698 } else {
1699 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1700 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001701 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1702 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith "
1703 "StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
1704 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1705 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001706 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001707 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1708 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith "
1709 "StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
1710 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1711 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001712 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001713 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1714 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith "
1715 "StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
1716 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1717 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001718 }
1719 }
1720 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1721 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001722 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1723 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001724 skip |= LogError(
1725 device, vuid,
1726 "%s: Can't use float atomics operations\n%s\nwith Workgroup storage class without "
1727 "shaderSharedFloat32Atomics or "
1728 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1729 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1730 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1731 report_data->FormatHandle(src->vk_shader_module()).c_str(), src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001732 } else if (opcode == spv::OpAtomicFAddEXT) {
1733 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1734 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001735 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001736 "storage class without shaderSharedFloat16AtomicAdd enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001737 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1738 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001739 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1740 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001741 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001742 "storage class without shaderSharedFloat32AtomicAdd enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001743 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1744 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001745 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1746 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001747 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001748 "storage class without shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001749 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1750 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001751 }
1752 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1753 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001754 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1755 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1756 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
1757 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1758 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001759 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001760 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1761 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1762 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
1763 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1764 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001765 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001766 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1767 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1768 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
1769 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1770 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001771 }
1772 } else {
1773 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1774 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001775 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1776 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1777 "storage class without shaderSharedFloat16Atomics enabled.",
1778 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1779 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001780 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001781 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1782 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1783 "storage class without shaderSharedFloat32Atomics enabled.",
1784 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1785 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001786 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001787 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1788 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1789 "storage class without shaderSharedFloat64Atomics enabled.",
1790 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1791 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001792 }
1793 }
1794 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001795 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1796 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001797 skip |= LogError(
1798 device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001799 "%s: Can't use float atomics operations\n%s\nwith Image storage class without shaderImageFloat32Atomics or "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001800 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001801 report_data->FormatHandle(src->vk_shader_module()).c_str(), src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001802 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001803 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001804 "%s: Can't use 16-bit float atomics operations\n%s\nwithout shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001805 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1806 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001807 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1808 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001809 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001810 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1811 : "VUID-RuntimeSpirv-None-06335";
1812 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001813 "%s: Can't use 32-bit float atomics operations\n%s\nwithout shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001814 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1815 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1816 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1817 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001818 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1819 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001820 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001821 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1822 : "VUID-RuntimeSpirv-None-06336";
1823 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001824 "%s: Can't use 64-bit float atomics operations\n%s\nwithout shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001825 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1826 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001827 report_data->FormatHandle(src->vk_shader_module()).c_str(),
1828 src->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001829 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001830 }
1831 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001832 return skip;
1833}
1834
Younggwan Kim26b9abd2021-12-07 21:22:03 +00001835bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint, VkShaderStageFlagBits stage,
1836 const PIPELINE_STATE *pipeline) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001837 auto entrypoint_id = entrypoint.word(2);
1838
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001839 // The first denorm execution mode encountered, along with its bit width.
1840 // Used to check if SeparateDenormSettings is respected.
1841 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001842
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001843 // The first rounding mode encountered, along with its bit width.
1844 // Used to check if SeparateRoundingModeSettings is respected.
1845 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001846
1847 bool skip = false;
1848
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001849 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001850 uint32_t invocations = 0;
1851
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001852 const auto &execution_mode_inst = src->GetExecutionModeInstructions();
1853 auto it = execution_mode_inst.find(entrypoint_id);
1854 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001855 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001856 auto mode = insn.word(2);
1857 switch (mode) {
1858 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1859 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001860 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001861 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001862 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
1863 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device");
1864 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1865 skip |= LogError(
1866 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
1867 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device");
1868 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1869 skip |= LogError(
1870 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
1871 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001872 }
1873 break;
1874 }
1875
1876 case spv::ExecutionModeDenormPreserve: {
1877 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001878 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1879 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
1880 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device");
1881 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1882 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
1883 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device");
1884 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1885 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
1886 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001887 }
1888
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001889 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1890 // Register the first denorm execution mode found
1891 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001892 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001893 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001894 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001895 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001896 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001897 "Shader uses different denorm execution modes for 16 and 64-bit but "
1898 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001899 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001900 }
1901 break;
1902
Mike Schuchardt2df08912020-12-15 16:28:09 -08001903 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001904 break;
1905
Mike Schuchardt2df08912020-12-15 16:28:09 -08001906 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001907 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001908 "Shader uses different denorm execution modes for different bit widths but "
1909 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001910 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001911 break;
1912
1913 default:
1914 break;
1915 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001916 }
1917 break;
1918 }
1919
1920 case spv::ExecutionModeDenormFlushToZero: {
1921 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001922 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
1923 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1924 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device");
1925 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
1926 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1927 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device");
1928 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
1929 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1930 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001931 }
1932
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001933 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1934 // Register the first denorm execution mode found
1935 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001936 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001937 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001938 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001939 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001940 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001941 "Shader uses different denorm execution modes for 16 and 64-bit but "
1942 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001943 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001944 }
1945 break;
1946
Mike Schuchardt2df08912020-12-15 16:28:09 -08001947 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001948 break;
1949
Mike Schuchardt2df08912020-12-15 16:28:09 -08001950 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001951 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001952 "Shader uses different denorm execution modes for different bit widths but "
1953 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001954 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001955 break;
1956
1957 default:
1958 break;
1959 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001960 }
1961 break;
1962 }
1963
1964 case spv::ExecutionModeRoundingModeRTE: {
1965 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001966 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1967 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
1968 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device");
1969 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1970 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
1971 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device");
1972 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1973 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
1974 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001975 }
1976
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001977 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1978 // Register the first rounding mode found
1979 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001980 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001981 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001982 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001983 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001984 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001985 "Shader uses different rounding modes for 16 and 64-bit but "
1986 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001987 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001988 }
1989 break;
1990
Mike Schuchardt2df08912020-12-15 16:28:09 -08001991 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001992 break;
1993
Mike Schuchardt2df08912020-12-15 16:28:09 -08001994 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001995 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001996 "Shader uses different rounding modes for different bit widths but "
1997 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001998 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001999 break;
2000
2001 default:
2002 break;
2003 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002004 }
2005 break;
2006 }
2007
2008 case spv::ExecutionModeRoundingModeRTZ: {
2009 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002010 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
2011 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
2012 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device");
2013 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
2014 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
2015 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device");
2016 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
2017 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
2018 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002019 }
2020
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002021 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2022 // Register the first rounding mode found
2023 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002024 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002025 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08002026 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002027 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002028 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002029 "Shader uses different rounding modes for 16 and 64-bit but "
2030 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08002031 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002032 }
2033 break;
2034
Mike Schuchardt2df08912020-12-15 16:28:09 -08002035 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002036 break;
2037
Mike Schuchardt2df08912020-12-15 16:28:09 -08002038 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002039 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002040 "Shader uses different rounding modes for different bit widths but "
2041 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08002042 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002043 break;
2044
2045 default:
2046 break;
2047 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002048 }
2049 break;
2050 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002051
2052 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002053 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002054 break;
2055 }
2056
2057 case spv::ExecutionModeInvocations: {
2058 invocations = insn.word(3);
2059 break;
2060 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002061
2062 case spv::ExecutionModeLocalSizeId: {
Tony-LunarG273f32f2021-09-28 08:56:30 -06002063 if (!enabled_features.core13.maintenance4) {
Piers Daniella7f93b62021-11-20 12:32:04 -07002064 skip |= LogError(device, "VUID-RuntimeSpirv-LocalSizeId-06434",
2065 "LocalSizeId execution mode used but maintenance4 feature not enabled");
2066 }
2067 break;
2068 }
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002069
2070 case spv::ExecutionModeEarlyFragmentTests: {
2071 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) &&
Younggwan Kimf8601f92021-12-17 09:38:07 +00002072 (pipeline && pipeline->create_info.graphics.pDepthStencilState &&
2073 (pipeline->create_info.graphics.pDepthStencilState->flags &
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002074 (VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_DEPTH_ACCESS_BIT_ARM |
2075 VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_STENCIL_ACCESS_BIT_ARM)) != 0)) {
2076 skip |= LogError(
2077 device, " VUID-VkGraphicsPipelineCreateInfo-pStages-06466",
2078 "The fragment shader enables early fragment tests, but VkPipelineDepthStencilStateCreateInfo::flags == "
2079 "%s",
2080 string_VkPipelineDepthStencilStateCreateFlags(pipeline->create_info.graphics.pDepthStencilState->flags)
2081 .c_str());
2082 }
2083 break;
2084 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002085 }
2086 }
2087 }
2088
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002089 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002090 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002091 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2092 "Geometry shader entry point must have an OpExecutionMode instruction that "
2093 "specifies a maximum output vertex count that is greater than 0 and less "
2094 "than or equal to maxGeometryOutputVertices. "
2095 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002096 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002097 }
2098
2099 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002100 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2101 "Geometry shader entry point must have an OpExecutionMode instruction that "
2102 "specifies an invocation count that is greater than 0 and less "
2103 "than or equal to maxGeometryShaderInvocations. "
2104 "Invocations=%d, maxGeometryShaderInvocations=%d",
2105 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002106 }
2107 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002108 return skip;
2109}
2110
Chris Forbes47567b72017-06-09 12:09:45 -07002111// For given pipelineLayout verify that the set_layout_node at slot.first
2112// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002113static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002114 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002115 if (!pipelineLayout) return nullptr;
2116
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002117 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07002118
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002119 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07002120}
2121
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002122// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2123// o If there is only a vertex shader : gl_PointSize must be written when using points
2124// o If there is a geometry or tessellation shader:
2125// - If shaderTessellationAndGeometryPointSize feature is enabled:
2126// * gl_PointSize must be written in the final geometry stage
2127// - If shaderTessellationAndGeometryPointSize feature is disabled:
2128// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002129bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06002130 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002131 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2132 return false;
2133 }
2134
2135 bool pointsize_written = false;
2136 bool skip = false;
2137
2138 // Search for PointSize built-in decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002139 for (const auto &set : src->GetBuiltinDecorationList()) {
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002140 auto insn = src->at(set.offset);
2141 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002142 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002143 if (pointsize_written) {
2144 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002145 }
2146 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002147 }
2148
2149 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002150 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002151 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002152 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002153 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2154 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002155 }
2156 } else if (!pointsize_written) {
2157 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002158 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002159 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2160 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002161 }
2162 return skip;
2163}
John Zulauf14c355b2019-06-27 16:09:37 -06002164
Tobias Hector6663c9b2020-11-05 10:18:02 +00002165bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
2166 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2167 bool primitiverate_written = false;
2168 bool viewportindex_written = false;
2169 bool viewportmask_written = false;
2170 bool skip = false;
2171
2172 // Check if the primitive shading rate is written
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002173 for (const auto &set : src->GetBuiltinDecorationList()) {
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002174 auto insn = src->at(set.offset);
2175 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002176 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002177 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002178 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002179 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002180 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002181 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002182 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2183 break;
2184 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002185 }
2186
Tony-LunarGd44844c2021-01-22 13:24:37 -07002187 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002188 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002189 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002190 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002191 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002192 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2193 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2194 "multiple viewports "
2195 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2196 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002197 }
2198
2199 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002200 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002201 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2202 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2203 "ViewportIndex built-ins,"
2204 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2205 string_VkShaderStageFlagBits(stage));
2206 }
2207
2208 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002209 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002210 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2211 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2212 "ViewportMaskNV built-ins,"
2213 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2214 string_VkShaderStageFlagBits(stage));
2215 }
2216 }
2217 return skip;
2218}
2219
ziga-lunargce66e542021-09-19 00:11:14 +02002220bool CoreChecks::ValidateDecorations(SHADER_MODULE_STATE const* module) const {
2221 bool skip = false;
2222
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002223 std::vector<spirv_inst_iter> xfb_streams;
2224 std::vector<spirv_inst_iter> xfb_buffers;
ziga-lunargef2c3172021-11-07 10:35:29 +01002225 std::vector<spirv_inst_iter> xfb_offsets;
2226
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002227 for (const auto &op_decorate : module->GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002228 uint32_t decoration = op_decorate.word(2);
2229 if (decoration == spv::DecorationXfbStride) {
2230 uint32_t stride = op_decorate.word(3);
2231 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2232 skip |= LogError(
2233 device, "VUID-RuntimeSpirv-XfbStride-06313",
2234 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2235 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2236 ").",
2237 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2238 }
2239 }
ziga-lunarg423cf212021-11-07 00:00:27 +01002240 if (decoration == spv::DecorationStream) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002241 xfb_streams.push_back(op_decorate);
ziga-lunarg423cf212021-11-07 00:00:27 +01002242 uint32_t stream = op_decorate.word(3);
2243 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2244 skip |= LogError(
2245 device, "VUID-RuntimeSpirv-Stream-06312",
2246 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2247 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32 ").",
2248 stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
2249 }
2250 }
ziga-lunargef2c3172021-11-07 10:35:29 +01002251 if (decoration == spv::DecorationXfbBuffer) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002252 xfb_buffers.push_back(op_decorate);
ziga-lunargef2c3172021-11-07 10:35:29 +01002253 }
2254 if (decoration == spv::DecorationOffset) {
2255 xfb_offsets.push_back(op_decorate);
2256 }
2257 }
2258
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002259 // XfbBuffer, buffer data size
2260 std::vector<std::pair<uint32_t, uint32_t>> buffer_data_sizes;
ziga-lunargef2c3172021-11-07 10:35:29 +01002261 for (const auto &op_decorate : xfb_offsets) {
2262 for (const auto xfb_buffer : xfb_buffers) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002263 if (xfb_buffer.word(1) == op_decorate.word(1)) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002264 const auto offset = op_decorate.word(3);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002265 const auto def = module->get_def(xfb_buffer.word(1));
ziga-lunargef2c3172021-11-07 10:35:29 +01002266 const auto size = module->GetTypeBytesSize(def);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002267 const uint32_t buffer_data_size = offset + size;
2268 if (buffer_data_size > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002269 skip |= LogError(
2270 device, "VUID-RuntimeSpirv-Offset-06308",
2271 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_offset (%" PRIu32
2272 ") + size of variable (%" PRIu32 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2273 "(%" PRIu32 ").",
2274 offset, size, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2275 }
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002276
2277 bool found = false;
2278 for (auto &bds : buffer_data_sizes) {
2279 if (bds.first == xfb_buffer.word(1)) {
2280 bds.second = std::max(bds.second, buffer_data_size);
2281 found = true;
2282 break;
2283 }
2284 }
2285 if (!found) {
2286 buffer_data_sizes.emplace_back(xfb_buffer.word(1), buffer_data_size);
2287 }
2288
ziga-lunargef2c3172021-11-07 10:35:29 +01002289 break;
2290 }
2291 }
ziga-lunargce66e542021-09-19 00:11:14 +02002292 }
2293
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002294 std::unordered_map<uint32_t, uint32_t> stream_data_size;
2295 for (const auto &xfb_stream : xfb_streams) {
2296 for (const auto& bds : buffer_data_sizes) {
2297 if (xfb_stream.word(1) == bds.first) {
2298 uint32_t stream = xfb_stream.word(3);
2299 const auto itr = stream_data_size.find(stream);
2300 if (itr != stream_data_size.end()) {
2301 itr->second += bds.second;
2302 } else {
2303 stream_data_size.insert({stream, bds.second});
2304 }
2305 }
2306 }
2307 }
2308
2309 for (const auto& stream : stream_data_size) {
2310 if (stream.second > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreamDataSize) {
2311 skip |= LogError(device, "VUID-RuntimeSpirv-XfbBuffer-06309",
2312 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2313 ") having the sum of buffer data sizes (%" PRIu32
2314 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2315 "(%" PRIu32 ").",
2316 stream.first, stream.second,
2317 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2318 }
2319 }
2320
ziga-lunargce66e542021-09-19 00:11:14 +02002321 return skip;
2322}
2323
ziga-lunarg28d08792021-10-13 15:42:59 +02002324bool CoreChecks::ValidateTransformFeedback(SHADER_MODULE_STATE const *src) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002325 bool skip = false;
2326
ziga-lunarg28d08792021-10-13 15:42:59 +02002327 // Temp workaround to prevent false positive errors
2328 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
2329 if (src->HasMultipleEntryPoints()) {
2330 return skip;
2331 }
2332
2333 layer_data::unordered_set<uint32_t> emitted_streams;
2334 bool output_points = false;
2335 for (const auto& insn : *src) {
2336 const uint32_t opcode = insn.opcode();
2337 if (opcode == spv::OpEmitStreamVertex) {
2338 emitted_streams.emplace(static_cast<uint32_t>(src->GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002339 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002340 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
2341 uint32_t stream = static_cast<uint32_t>(src->GetConstantValueById(insn.word(1)));
2342 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2343 skip |= LogError(
2344 device, "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002345 "vkCreateGraphicsPipelines(): shader uses transform feedback stream\n%s\nwith index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002346 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2347 ").",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002348 src->DescribeInstruction(insn).c_str(), stream,
2349 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002350 }
2351 }
2352 if (opcode == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeOutputPoints) {
2353 output_points = true;
2354 }
2355 }
2356
2357 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2358 if (emitted_streams_size > 1 && !output_points &&
2359 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
2360 skip |= LogError(
2361 device, "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2362 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles is VK_FALSE, but execution mode is not OutputPoints.",
2363 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002364 }
2365
2366 return skip;
2367}
2368
sfricke-samsung864162a2021-11-01 21:58:01 -07002369// Checks for both TexelOffset and TexelGatherOffset limits
2370bool CoreChecks::ValidateTexelOffsetLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter &insn) const {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002371 bool skip = false;
2372
2373 const uint32_t opcode = insn.opcode();
sfricke-samsung864162a2021-11-01 21:58:01 -07002374 if (ImageGatherOperation(opcode) || ImageSampleOperation(opcode) || ImageFetchOperation(opcode)) {
sfricke-samsung3a25ed52022-01-20 02:24:36 -08002375 uint32_t image_operand_position = OpcodeImageOperandsPosition(opcode);
sfricke-samsung864162a2021-11-01 21:58:01 -07002376 // Image operands can be optional
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002377 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2378 auto image_operand = insn.word(image_operand_position);
sfricke-samsung864162a2021-11-01 21:58:01 -07002379 // Bits we are validating (sample/fetch only check ConstOffset)
ziga-lunarga12c75a2021-09-16 16:36:16 +02002380 uint32_t offset_bits =
sfricke-samsung864162a2021-11-01 21:58:01 -07002381 ImageGatherOperation(opcode)
2382 ? (spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask)
2383 : (spv::ImageOperandsConstOffsetMask);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002384 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002385 // Operand values follow
2386 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002387 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2388 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2389 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2390 if (image_operand & i) { // If the bit is set, consume operand
2391 if (insn.len() > index && (i & offset_bits)) {
2392 uint32_t constant_id = insn.word(index);
2393 const auto &constant = src->get_def(constant_id);
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002394 const bool is_dynamic_offset = constant == src->end();
2395 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002396 for (uint32_t j = 3; j < constant.len(); ++j) {
2397 uint32_t comp_id = constant.word(j);
2398 const auto &comp = src->get_def(comp_id);
sfricke-samsungef3fe742021-10-06 10:51:34 -07002399 const auto &comp_type = src->get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002400 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002401 const uint32_t offset = comp.word(3);
sfricke-samsung864162a2021-11-01 21:58:01 -07002402 // spec requires minTexelGatherOffset/minTexelOffset to be -8 or less so never can compare if
2403 // unsigned spec requires maxTexelGatherOffset/maxTexelOffset to be 7 or greater so never can
2404 // compare if signed is less then zero
sfricke-samsungef3fe742021-10-06 10:51:34 -07002405 const int32_t signed_offset = static_cast<int32_t>(offset);
2406 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2407
sfricke-samsung864162a2021-11-01 21:58:01 -07002408 // There are 2 sets of VU being covered where the only main difference is the opcode
2409 if (ImageGatherOperation(opcode)) {
2410 // min/maxTexelGatherOffset
2411 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
2412 skip |=
2413 LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002414 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002415 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002416 src->DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002417 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002418 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2419 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002420 skip |= LogError(device, "VUID-RuntimeSpirv-OpImage-06377",
2421 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2422 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32
2423 ").",
2424 src->DescribeInstruction(insn).c_str(), offset,
2425 phys_dev_props.limits.maxTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002426 }
2427 } else {
2428 // min/maxTexelOffset
2429 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelOffset)) {
2430 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06435",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002431 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsung864162a2021-11-01 21:58:01 -07002432 ") less than VkPhysicalDeviceLimits::minTexelOffset (%" PRIi32 ").",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002433 src->DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung864162a2021-11-01 21:58:01 -07002434 phys_dev_props.limits.minTexelOffset);
2435 } else if ((offset > phys_dev_props.limits.maxTexelOffset) &&
2436 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002437 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06436",
2438 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2439 ") greater than VkPhysicalDeviceLimits::maxTexelOffset (%" PRIu32 ").",
2440 src->DescribeInstruction(insn).c_str(), offset,
2441 phys_dev_props.limits.maxTexelOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002442 }
ziga-lunarga12c75a2021-09-16 16:36:16 +02002443 }
2444 }
2445 }
2446 }
sfricke-samsung3511e312021-11-04 21:14:31 -07002447 index += ImageOperandsParamCount(i);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002448 }
2449 }
2450 }
2451 }
2452 }
2453
2454 return skip;
2455}
2456
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002457bool CoreChecks::ValidateShaderClock(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002458 bool skip = false;
2459
sfricke-samsung94167ca2021-02-26 04:14:59 -08002460 switch (insn.opcode()) {
2461 case spv::OpReadClockKHR: {
2462 auto scope_id = module->get_def(insn.word(3));
2463 auto scope_type = scope_id.word(3);
2464 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002465 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002466 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002467 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002468 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002469 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002470 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002471 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002472 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002473 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002474 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002475 }
2476 }
2477 return skip;
2478}
2479
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002480bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2481 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002482 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002483 const auto *pStage = stage_state.create_info;
2484 const auto *module = stage_state.module.get();
2485 const auto &entrypoint = stage_state.entrypoint;
John Zulauf14c355b2019-06-27 16:09:37 -06002486 // Check the module
2487 if (!module->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002488 skip |= LogError(
2489 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2490 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002491 }
2492
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002493 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2494 // specializations should be applied and validated.
2495 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002496 pStage->pSpecializationInfo->pMapEntries != nullptr && module->HasSpecConstants()) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002497 // Gather the specialization-constant values.
2498 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002499 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002500 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 -06002501 id_value_map.reserve(specialization_info->mapEntryCount);
2502 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2503 auto const &map_entry = specialization_info->pMapEntries[i];
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002504 const auto itr = module->GetSpecConstMap().find(map_entry.constantID);
sfricke-samsung033b0262021-07-09 00:53:06 -07002505 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2506 // behavior of the pipeline."
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002507 if (itr != module->GetSpecConstMap().cend()) {
sfricke-samsung033b0262021-07-09 00:53:06 -07002508 // Make sure map_entry.size matches the spec constant's size
2509 uint32_t spec_const_size = decoration_set::kInvalidValue;
2510 const auto def_ins = module->get_def(itr->second);
2511 const auto type_ins = module->get_def(def_ins.word(1));
2512 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2513 switch (type_ins.opcode()) {
2514 case spv::OpTypeBool:
2515 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2516 spec_const_size = sizeof(VkBool32);
2517 break;
2518 case spv::OpTypeInt:
2519 case spv::OpTypeFloat:
2520 spec_const_size = type_ins.word(2) / 8;
2521 break;
2522 default:
2523 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2524 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2525 break;
2526 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002527
sfricke-samsung033b0262021-07-09 00:53:06 -07002528 if (map_entry.size != spec_const_size) {
2529 skip |=
2530 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002531 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2532 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2533 map_entry.constantID, i, map_entry.size,
2534 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002535 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002536 }
2537
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002538 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002539 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2540 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2541 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2542 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2543 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2544
2545 std::copy(start_in_p, end_in_p, out_p);
2546 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002547 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002548 }
2549
sfricke-samsung5628f982021-10-19 09:21:59 -07002550 // both spirv-opt and spirv-val will use the same flags
2551 spvtools::ValidatorOptions options;
2552 AdjustValidatorOptions(device_extensions, enabled_features, options);
2553
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002554 // Apply the specialization-constant values and revalidate the shader module.
sfricke-samsung45996a42021-09-16 13:45:27 -07002555 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002556 spvtools::Optimizer optimizer(spirv_environment);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002557 spvtools::MessageConsumer consumer = [&skip, &module, &stage_state, this](spv_message_level_t level, const char *source,
2558 const spv_position_t &position,
2559 const char *message) {
2560 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2561 "%s does not contain valid spirv for stage %s. %s",
2562 report_data->FormatHandle(module->vk_shader_module()).c_str(),
2563 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002564 };
2565 optimizer.SetMessageConsumer(consumer);
2566 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2567 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2568 std::vector<uint32_t> specialized_spirv;
sfricke-samsung5628f982021-10-19 09:21:59 -07002569 auto const optimized = optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002570 assert(optimized == true);
2571
2572 if (optimized) {
2573 spv_context ctx = spvContextCreate(spirv_environment);
2574 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2575 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002576 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2577 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002578 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002579 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002580 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002581 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002582 }
2583
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002584 spvDiagnosticDestroy(diag);
2585 spvContextDestroy(ctx);
2586 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002587
2588 skip |= ValidateWorkgroupSize(module, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002589 }
2590
John Zulauf14c355b2019-06-27 16:09:37 -06002591 // Check the entrypoint
2592 if (entrypoint == module->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002593 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2594 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002595 }
2596 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2597
2598 // Mark accessible ids
2599 auto &accessible_ids = stage_state.accessible_ids;
2600
Chris Forbes47567b72017-06-09 12:09:45 -07002601 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002602
sfricke-samsung94167ca2021-02-26 04:14:59 -08002603 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2604 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002605 uint32_t total_shared_size = 0;
sfricke-samsung94167ca2021-02-26 04:14:59 -08002606 for (auto insn : *module) {
sfricke-samsung864162a2021-11-01 21:58:01 -07002607 skip |= ValidateTexelOffsetLimits(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002608 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002609 skip |= ValidateShaderClock(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002610 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
ziga-lunarg70651522021-10-11 17:23:30 +02002611 skip |= ValidateMemoryScope(module, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02002612 total_shared_size += module->CalcComputeSharedMemory(pStage->stage, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08002613
2614 // Checks based off shaderStorageImage(Read|Write)WithoutFormat are
2615 // disabled if VK_KHR_format_feature_flags2 is supported.
2616 //
2617 // https://github.com/KhronosGroup/Vulkan-Docs/blob/6177645341afc/appendices/spirvenv.txt#L553
2618 //
2619 // The other checks need to take into account the format features and so
2620 // we apply that in the descriptor set matching validation code (see
2621 // descriptor_sets.cpp).
2622 if (!has_format_feature2) {
2623 skip |= ValidateShaderStorageImageFormats(module, insn);
2624 }
ziga-lunarga26b3602021-08-08 15:53:00 +02002625 }
2626
2627 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2628 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002629 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002630 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002631 }
2632
ziga-lunarg28d08792021-10-13 15:42:59 +02002633 skip |= ValidateTransformFeedback(module);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002634 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2635 stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002636 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002637 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsung58b84352021-07-31 21:41:04 -07002638 skip |= ValidateAtomicsTypes(module);
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002639 skip |= ValidateExecutionModes(module, entrypoint, pStage->stage, pipeline);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002640 skip |= ValidateSpecializations(pStage);
ziga-lunargce66e542021-09-19 00:11:14 +02002641 skip |= ValidateDecorations(module);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002642 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002643 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002644 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07002645 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002646 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
2647 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
2648 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002649 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
2650 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
2651 }
sfricke-samsung45996a42021-09-16 13:45:27 -07002652 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002653 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
2654 }
ziga-lunarg73163742021-08-25 13:15:29 +02002655 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
2656 skip |= ValidateShaderSubgroupSizeControl(pStage);
2657 }
Chris Forbes47567b72017-06-09 12:09:45 -07002658
sfricke-samsung7699b912021-04-12 23:01:51 -07002659 // "layout must be consistent with the layout of the * shader"
2660 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002661 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002662 switch (pipeline->create_info.graphics.sType) {
2663 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2664 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2665 break;
2666 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2667 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2668 break;
2669 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2670 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2671 break;
2672 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2673 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2674 break;
2675 default:
2676 assert(false);
2677 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002678 }
2679
sfricke-samsung7699b912021-04-12 23:01:51 -07002680 // Validate Push Constants use
2681 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
2682
Chris Forbes47567b72017-06-09 12:09:45 -07002683 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002684 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002685 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002686 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002687 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002688 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2689 std::set<uint32_t> descriptor_types =
2690 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002691
2692 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002693 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002694 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002695 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002696 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002697 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002698 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2699 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002700 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2701 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002702 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002703 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2704 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002705 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002706 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002707 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002708 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002709 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002710 }
2711 }
2712
2713 // Validate use of input attachments against subpass structure
2714 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002715 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002716
amhagana448ea52021-11-02 14:09:14 -04002717 if (!pipeline->rp_state->use_dynamic_rendering) {
2718 auto rpci = pipeline->rp_state->createInfo.ptr();
2719 auto subpass = pipeline->create_info.graphics.subpass;
2720 for (auto use : input_attachment_uses) {
2721 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2722 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
2723 ? input_attachments[use.first].attachment
2724 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002725
amhagana448ea52021-11-02 14:09:14 -04002726 if (index == VK_ATTACHMENT_UNUSED) {
2727 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2728 "Shader consumes input attachment index %d but not provided in subpass", use.first);
2729 }
2730 else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
2731 skip |=
2732 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2733 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
2734 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
2735 }
Chris Forbes47567b72017-06-09 12:09:45 -07002736 }
2737 }
2738 }
Lockeaa8fdc02019-04-02 11:59:20 -06002739 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02002740 skip |= ValidateComputeWorkGroupSizes(module, entrypoint, stage_state);
Lockeaa8fdc02019-04-02 11:59:20 -06002741 }
ziga-lunarg73163742021-08-25 13:15:29 +02002742
Chris Forbes47567b72017-06-09 12:09:45 -07002743 return skip;
2744}
2745
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002746bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2747 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2748 spirv_inst_iter consumer_entrypoint,
2749 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002750 bool skip = false;
2751
2752 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002753 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2754 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002755
2756 auto a_it = outputs.begin();
2757 auto b_it = inputs.begin();
2758
ziga-lunarg8346fe82021-08-22 17:30:50 +02002759 uint32_t a_component = 0;
2760 uint32_t b_component = 0;
2761
Chris Forbes47567b72017-06-09 12:09:45 -07002762 // Maps sorted by key (location); walk them together to find mismatches
2763 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2764 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2765 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2766 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2767 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2768
ziga-lunarg8346fe82021-08-22 17:30:50 +02002769 a_first.second += a_component;
2770 b_first.second += b_component;
2771
2772 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2773 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2774 assert(a_at_end || a_component < a_length);
2775 assert(b_at_end || b_component < b_length);
2776
Chris Forbes47567b72017-06-09 12:09:45 -07002777 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002778 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002779 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2780 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2781 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2782 a_it++;
2783 a_component = 0;
2784 } else {
2785 a_component++;
2786 }
Chris Forbes47567b72017-06-09 12:09:45 -07002787 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002788 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002789 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2790 b_first.first, b_first.second, producer_stage->name);
2791 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2792 b_it++;
2793 b_component = 0;
2794 } else {
2795 b_component++;
2796 }
Chris Forbes47567b72017-06-09 12:09:45 -07002797 } else {
2798 // subtleties of arrayed interfaces:
2799 // - if is_patch, then the member is not arrayed, even though the interface may be.
2800 // - if is_block_member, then the extra array level of an arrayed interface is not
2801 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002802 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002803 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002804 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002805 producer->DescribeType(a_it->second.type_id).c_str(),
2806 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002807 a_it++;
2808 b_it++;
2809 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002810 }
2811 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002812 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002813 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2814 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2815 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002816 }
2817 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002818 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002819 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2820 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002821 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002822 uint32_t a_remaining = a_length - a_component;
2823 uint32_t b_remaining = b_length - b_component;
2824 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2825 a_it++;
2826 b_it++;
2827 a_component = 0;
2828 b_component = 0;
2829 } else if (a_remaining > b_remaining) { // a has more components remaining
2830 a_component += b_remaining;
2831 b_component = 0;
2832 b_it++;
2833 } else if (b_remaining > a_remaining) { // b has more components remaining
2834 b_component += a_remaining;
2835 a_component = 0;
2836 a_it++;
2837 }
Chris Forbes47567b72017-06-09 12:09:45 -07002838 }
2839 }
2840
Ari Suonpaa696b3432019-03-11 14:02:57 +02002841 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002842 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2843 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002844
2845 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2846 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002847 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002848 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002849 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2850 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002851 } else {
2852 auto it_producer = builtins_producer.begin();
2853 auto it_consumer = builtins_consumer.begin();
2854 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2855 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002856 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002857 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2858 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002859 break;
2860 }
2861 it_producer++;
2862 it_consumer++;
2863 }
2864 }
2865 }
2866 }
2867
Chris Forbes47567b72017-06-09 12:09:45 -07002868 return skip;
2869}
2870
John Zulauf14c355b2019-06-27 16:09:37 -06002871static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002872 uint32_t stage_mask = 0;
2873 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2874 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2875 stage_mask |= pCreateInfo->pStages[i].stage;
2876 }
2877 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002878 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2879 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2880 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002881 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2882 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2883 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2884 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2885 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002886 }
2887 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002888 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002889}
2890
Chris Forbes47567b72017-06-09 12:09:45 -07002891// Validate that the shaders used by the given pipeline and store the active_slots
2892// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002893bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002894 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002895
Chris Forbes47567b72017-06-09 12:09:45 -07002896 bool skip = false;
2897
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002898 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002899
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002900 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
2901 for (auto &stage : pipeline->stage_state) {
2902 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
2903 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
2904 vertex_stage = &stage;
2905 }
2906 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
2907 fragment_stage = &stage;
2908 }
Chris Forbes47567b72017-06-09 12:09:45 -07002909 }
2910
2911 // if the shader stages are no good individually, cross-stage validation is pointless.
2912 if (skip) return true;
2913
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002914 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002915
2916 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002917 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002918 }
2919
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002920 if (vertex_stage && vertex_stage->module->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
2921 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07002922 }
2923
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002924 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
2925 const auto &producer = pipeline->stage_state[i - 1];
2926 const auto &consumer = pipeline->stage_state[i];
2927 assert(producer.module);
2928 if (&producer == fragment_stage) {
2929 break;
2930 }
2931 if (consumer.module) {
2932 if (consumer.module->has_valid_spirv && producer.module->has_valid_spirv) {
2933 auto producer_id = GetShaderStageId(producer.stage_flag);
2934 auto consumer_id = GetShaderStageId(consumer.stage_flag);
2935 skip |=
2936 ValidateInterfaceBetweenStages(producer.module.get(), producer.entrypoint, &shader_stage_attribs[producer_id],
2937 consumer.module.get(), consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002938 }
Chris Forbes47567b72017-06-09 12:09:45 -07002939 }
2940 }
2941
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002942 if (fragment_stage && fragment_stage->module->has_valid_spirv) {
Aaron Hagan1209c782021-11-22 19:37:14 -05002943 if (pipeline->rp_state->use_dynamic_rendering) {
2944 skip |= ValidateFsOutputsAgainstDynamicRenderingRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline);
2945 } else {
2946 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline,
2947 create_info->subpass);
2948 }
Chris Forbes47567b72017-06-09 12:09:45 -07002949 }
2950
2951 return skip;
2952}
2953
Tony-LunarGb2ded512021-02-02 16:03:30 -07002954bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2955 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002956 bool skip = false;
2957
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002958 for (auto &stage : pipeline->stage_state) {
2959 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2960 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002961 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2962 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben3dfeacf2021-12-02 08:46:39 -07002963 if (stage.wrote_primitive_shading_rate) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002964 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002965 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002966 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2967 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2968 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002969 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002970 }
2971 }
2972 }
2973 }
2974
2975 return skip;
2976}
2977
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002978bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002979 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07002980}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002981
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002982uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2983 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002984 const auto &create_info = pipeline->create_info.raytracing;
2985 const auto *stages = create_info.ptr()->pStages;
2986 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002987 if (stages[stage_index].stage == stageBit) {
2988 total++;
2989 }
2990 }
2991
Jeremy Gebben11af9792021-08-20 10:20:09 -06002992 if (create_info.pLibraryInfo) {
2993 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002994 const auto library_pipeline = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Jeremy Gebben9f537102021-10-05 16:37:12 -06002995 total += CalcShaderStageCount(library_pipeline.get(), stageBit);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002996 }
2997 }
2998
2999 return total;
3000}
3001
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003002bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE *pipeline, uint32_t group, uint32_t stage) const {
3003 if (group == VK_SHADER_UNUSED_NV) {
3004 return true;
3005 }
3006
3007 const auto &create_info = pipeline->create_info.raytracing;
3008 const auto *stages = create_info.ptr()->pStages;
3009
3010 if (group < create_info.stageCount) {
3011 return (stages[group].stage & stage) != 0;
3012 }
3013 group -= create_info.stageCount;
3014
3015 // Search libraries
3016 if (create_info.pLibraryInfo) {
3017 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003018 auto library_pipeline = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003019 const uint32_t stage_count = library_pipeline->create_info.raytracing.ptr()->stageCount;
3020 if (group < stage_count) {
3021 return (library_pipeline->create_info.raytracing.ptr()->pStages[group].stage & stage) != 0;
3022 }
3023 group -= stage_count;
3024 }
3025 }
3026
3027 // group index too large
3028 return false;
3029}
3030
sourav parmarcd5fb182020-07-17 12:58:44 -07003031bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06003032 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04003033
Jeremy Gebben11af9792021-08-20 10:20:09 -06003034 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003035 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003036 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
3037 skip |=
3038 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
3039 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
3040 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
3041 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003042 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003043 if (create_info.pLibraryInfo) {
3044 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06003045 const auto library_pipelinestate = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Jeremy Gebben11af9792021-08-20 10:20:09 -06003046 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
3047 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003048 skip |= LogError(
3049 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
3050 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
3051 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003052 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07003053 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003054 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
3055 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
3056 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
3057 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003058 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
3059 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
3060 "member must have been created with values of the maxPipelineRayPayloadSize and "
3061 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
3062 }
3063 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06003064 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003065 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
3066 "vkCreateRayTracingPipelinesKHR: If flags includes "
3067 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
3068 "the pLibraries member of libraries must have been created with the "
3069 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
3070 }
sourav parmar83c31b12020-05-06 12:30:54 -07003071 }
3072 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003073 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003074 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003075 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
3076 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
3077 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003078 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003079 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003080 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003081 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04003082
Jeremy Gebben11af9792021-08-20 10:20:09 -06003083 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003084 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003085 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003086
Jeremy Gebben11af9792021-08-20 10:20:09 -06003087 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003088 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
3089 if (raygen_stages_count == 0) {
3090 skip |= LogError(
3091 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07003092 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003093 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
3094 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003095 }
3096
Jeremy Gebben11af9792021-08-20 10:20:09 -06003097 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003098 const auto &group = groups[group_index];
3099
3100 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003101 if (!GroupHasValidIndex(
3102 pipeline, group.generalShader,
3103 VK_SHADER_STAGE_RAYGEN_BIT_NV | VK_SHADER_STAGE_MISS_BIT_NV | VK_SHADER_STAGE_CALLABLE_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003104 skip |= LogError(device,
3105 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
3106 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
3107 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003108 }
3109 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3110 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003111 skip |= LogError(device,
3112 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
3113 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
3114 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003115 }
3116 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003117 if (!GroupHasValidIndex(pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003118 skip |= LogError(device,
3119 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
3120 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
3121 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003122 }
3123 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3124 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003125 skip |= LogError(device,
3126 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
3127 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
3128 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003129 }
3130 }
3131
3132 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3133 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003134 if (!GroupHasValidIndex(pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003135 skip |= LogError(device,
3136 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
3137 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
3138 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003139 }
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003140 if (!GroupHasValidIndex(pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003141 skip |= LogError(device,
3142 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
3143 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
3144 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003145 }
3146 }
John Zulaufe4474e72019-07-01 17:28:27 -06003147 }
3148 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003149}
3150
Dave Houltona9df0ce2018-02-07 10:51:23 -07003151uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003152
Dave Houltona9df0ce2018-02-07 10:51:23 -07003153static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003154 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06003155 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003156 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003157 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003158 return nullptr;
3159}
3160
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003161bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003162 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003163 bool skip = false;
3164 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003165
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06003166 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003167 return false;
3168 }
3169
sfricke-samsung45996a42021-09-16 13:45:27 -07003170 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003171
3172 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003173 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
3174 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3175 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003176 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003177 auto cache = GetValidationCacheInfo(pCreateInfo);
3178 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07003179 // If app isn't using a shader validation cache, use the default one from CoreChecks
3180 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003181 if (cache) {
3182 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003183 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003184 }
3185
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003186 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3187 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07003188 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06003189 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003190 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003191 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003192 spvtools::ValidatorOptions options;
3193 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06003194 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003195 if (spv_valid != SPV_SUCCESS) {
3196 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003197 if (spv_valid == SPV_WARNING) {
3198 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3199 diag && diag->error ? diag->error : "(no error text)");
3200 } else {
3201 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3202 diag && diag->error ? diag->error : "(no error text)");
3203 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003204 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003205 } else {
3206 if (cache) {
3207 cache->Insert(hash);
3208 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003209 }
3210
3211 spvDiagnosticDestroy(diag);
3212 spvContextDestroy(ctx);
3213 }
3214
Chris Forbes4ae55b32017-06-09 14:42:56 -07003215 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003216}
3217
ziga-lunarg11fecb92021-09-20 16:48:06 +02003218bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint,
3219 const PipelineStageState &stage_state) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003220 bool skip = false;
3221 uint32_t local_size_x = 0;
3222 uint32_t local_size_y = 0;
3223 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07003224 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06003225 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003226 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003227 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003228 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003229 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06003230 }
3231 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003232 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003233 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003234 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003235 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06003236 }
3237 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003238 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003239 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003240 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003241 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06003242 }
3243
3244 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3245 uint64_t invocations = local_size_x * local_size_y;
3246 // Prevent overflow.
3247 bool fail = false;
3248 if (invocations > UINT32_MAX || invocations > limit) {
3249 fail = true;
3250 }
3251 if (!fail) {
3252 invocations *= local_size_z;
3253 if (invocations > UINT32_MAX || invocations > limit) {
3254 fail = true;
3255 }
3256 }
3257 if (fail) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003258 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003259 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3260 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sfricke-samsung1ff329f2021-09-16 10:06:47 -07003261 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, local_size_y,
3262 local_size_z, limit);
Lockeaa8fdc02019-04-02 11:59:20 -06003263 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003264
3265 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3266 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
3267 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3268 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3269 skip |= LogError(
3270 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
3271 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3272 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3273 "dimension (%" PRIu32
3274 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
3275 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
3276 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3277 }
3278 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3279 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
3280 const auto *required_subgroup_size_features =
3281 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
3282 if (!required_subgroup_size_features) {
3283 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
3284 skip |= LogError(
3285 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
3286 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3287 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3288 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3289 ").",
3290 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
3291 phys_dev_props_core11.subgroupSize);
3292 }
3293 }
3294 }
Lockeaa8fdc02019-04-02 11:59:20 -06003295 }
3296 return skip;
3297}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003298
3299spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
Tony-LunarGe67fcc22022-01-03 16:40:53 -07003300 if (api_version >= VK_API_VERSION_1_3) {
3301 return SPV_ENV_VULKAN_1_3;
3302 } else if (api_version >= VK_API_VERSION_1_2) {
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003303 return SPV_ENV_VULKAN_1_2;
3304 } else if (api_version >= VK_API_VERSION_1_1) {
3305 if (spirv_1_4) {
3306 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3307 } else {
3308 return SPV_ENV_VULKAN_1_1;
3309 }
3310 }
3311 return SPV_ENV_VULKAN_1_0;
3312}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003313
sfricke-samsungecc112a2021-09-03 05:32:17 -07003314// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003315void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003316 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003317 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3318 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003319 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003320 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003321 options.SetRelaxBlockLayout(true);
3322 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003323
3324 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3325 // Vulkan version used, the feature bit is needed (also described in the spec).
3326
3327 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3328 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003329 options.SetUniformBufferStandardLayout(true);
3330 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003331 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3332 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003333 options.SetScalarBlockLayout(true);
3334 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003335 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3336 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003337 options.SetWorkgroupScalarBlockLayout(true);
3338 }
Tony-LunarG273f32f2021-09-28 08:56:30 -06003339 if (enabled_features.core13.maintenance4) {
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003340 // --allow-localsizeid
3341 options.SetAllowLocalSizeId(true);
3342 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003343}