blob: 66e3482ac078c417b844a624adb4f956de135647 [file] [log] [blame]
sfricke-samsung691299b2021-01-01 20:48:48 -08001/* Copyright (c) 2015-2021 The Khronos Group Inc.
2 * Copyright (c) 2015-2021 Valve Corporation
3 * Copyright (c) 2015-2021 LunarG, Inc.
4 * Copyright (C) 2015-2021 Google Inc.
Tobias Hector6663c9b2020-11-05 10:18:02 +00005 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Chris Forbes47567b72017-06-09 12:09:45 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Chris Forbes <chrisf@ijw.co.nz>
Dave Houlton51653902018-06-22 17:32:13 -060020 * Author: Dave Houlton <daveh@lunarg.com>
Tobias Hector6663c9b2020-11-05 10:18:02 +000021 * Author: Tobias Hector <tobias.hector@amd.com>
Chris Forbes47567b72017-06-09 12:09:45 -070022 */
23
Petr Kraus25810d02019-08-27 17:41:15 +020024#include "shader_validation.h"
25
Chris Forbes47567b72017-06-09 12:09:45 -070026#include <cassert>
Petr Kraus25810d02019-08-27 17:41:15 +020027#include <cinttypes>
Jeff Bolzf234bf82019-11-04 14:07:15 -060028#include <cmath>
Chris Forbes47567b72017-06-09 12:09:45 -070029#include <sstream>
Petr Kraus25810d02019-08-27 17:41:15 +020030#include <string>
Petr Kraus25810d02019-08-27 17:41:15 +020031#include <vector>
32
Mark Lobodzinski102687e2020-04-28 11:03:28 -060033#include <spirv/unified1/spirv.hpp>
Chris Forbes47567b72017-06-09 12:09:45 -070034#include "vk_enum_string_helper.h"
Chris Forbes47567b72017-06-09 12:09:45 -070035#include "vk_layer_data.h"
Chris Forbes47567b72017-06-09 12:09:45 -070036#include "vk_layer_utils.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070037#include "chassis.h"
Chris Forbes47567b72017-06-09 12:09:45 -070038#include "core_validation.h"
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
218 for (uint32_t location = 0; location < pipeline->rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount; ++location) {
219 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);
226 } else if (output) {
227 auto format = pipeline->rp_state->dynamic_rendering_pipeline_create_info.pColorAttachmentFormats[location];
228 const auto attachment_type = GetFormatType(format);
229 const auto output_type = fs->GetFundamentalType(output->type_id);
230
231 // Type checking
232 if (!(output_type & attachment_type)) {
233 skip |=
234 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
235 "Attachment %" PRIu32
236 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
237 location, string_VkFormat(format), fs->DescribeType(output->type_id).c_str());
238 }
239 }
240 }
241
242 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
243 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
244 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
245 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
246 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
247 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
248 }
249
250 return skip;
251
252}
253
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700254bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint,
255 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200256 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700257
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600258 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800259 const VkAttachmentReference2 *reference = nullptr;
260 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600261 const interface_var *output = nullptr;
262 };
263 std::map<uint32_t, Attachment> location_map;
264
amhagana448ea52021-11-02 14:09:14 -0400265 if (pipeline->rp_state && !pipeline->rp_state->use_dynamic_rendering) {
266 const auto rpci = pipeline->rp_state->createInfo.ptr();
267 const auto subpass = rpci->pSubpasses[subpass_index];
268 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
269 auto const &reference = subpass.pColorAttachments[i];
270 location_map[i].reference = &reference;
271 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
272 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
273 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
274 }
Chris Forbes47567b72017-06-09 12:09:45 -0700275 }
276 }
277
Chris Forbes47567b72017-06-09 12:09:45 -0700278 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
279
sfricke-samsung962cad92021-04-13 00:46:29 -0700280 const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600281 for (const auto &output_it : outputs) {
282 auto const location = output_it.first.first;
283 location_map[location].output = &output_it.second;
284 }
Chris Forbes47567b72017-06-09 12:09:45 -0700285
Jeremy Gebben11af9792021-08-20 10:20:09 -0600286 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
287 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700288
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400289 for (const auto &location_it : location_map) {
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600290 const auto reference = location_it.second.reference;
291 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
292 continue;
293 }
294
Petr Kraus25810d02019-08-27 17:41:15 +0200295 const auto location = location_it.first;
296 const auto attachment = location_it.second.attachment;
297 const auto output = location_it.second.output;
Petr Kraus25810d02019-08-27 17:41:15 +0200298 if (attachment && !output) {
299 if (pipeline->attachments[location].colorWriteMask != 0) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600300 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700301 "Attachment %" PRIu32
302 " not written by fragment shader; undefined values will be written to attachment",
303 location);
Petr Kraus25810d02019-08-27 17:41:15 +0200304 }
305 } else if (!attachment && output) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700306 if (!(alpha_to_coverage_enabled && location == 0)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600307 skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700308 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200309 }
Petr Kraus25810d02019-08-27 17:41:15 +0200310 } else if (attachment && output) {
311 const auto attachment_type = GetFormatType(attachment->format);
sfricke-samsung962cad92021-04-13 00:46:29 -0700312 const auto output_type = fs->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700313
314 // Type checking
Petr Kraus25810d02019-08-27 17:41:15 +0200315 if (!(output_type & attachment_type)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700316 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600317 LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700318 "Attachment %" PRIu32
319 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sfricke-samsung962cad92021-04-13 00:46:29 -0700320 location, string_VkFormat(attachment->format), fs->DescribeType(output->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700321 }
Petr Kraus25810d02019-08-27 17:41:15 +0200322 } else { // !attachment && !output
323 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700324 }
325 }
326
Petr Kraus25810d02019-08-27 17:41:15 +0200327 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700328 bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() &&
sfricke-samsung962cad92021-04-13 00:46:29 -0700329 fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700330 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600331 skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700332 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200333 }
334
Chris Forbes47567b72017-06-09 12:09:45 -0700335 return skip;
336}
337
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600338PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
339 const shader_struct_member &push_constant_used_in_shader,
340 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600341 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600342 const auto used_bytes_size = used_bytes->size();
343 if (used_bytes_size == 0) return PC_Byte_Updated;
344
345 const auto push_constant_data_update_size = push_constant_data_update.size();
346 const auto *data = push_constant_data_update.data();
347 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
348 if (used_bytes_size <= push_constant_data_update_size) {
349 return PC_Byte_Updated;
350 }
351 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
352
353 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
354 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
355 return PC_Byte_Updated;
356 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600357 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600358
locke-lunargde3f0fa2020-09-10 11:55:31 -0600359 uint32_t i = 0;
360 for (const auto used : *used_bytes) {
361 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600362 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600363 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600364 return PC_Byte_Not_Set;
365 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600366 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600367 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600368 }
369 }
370 ++i;
371 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600372 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600373}
374
375bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src,
sfricke-samsung7699b912021-04-12 23:01:51 -0700376 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700377 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700378 // Temp workaround to prevent false positive errors
379 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600380 if (src->HasMultipleEntryPoints()) {
sfricke-samsung5c65b372021-03-25 05:39:57 -0700381 return skip;
382 }
383
Chris Forbes47567b72017-06-09 12:09:45 -0700384 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
sfricke-samsung962cad92021-04-13 00:46:29 -0700385 const auto *entrypoint = src->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600386 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
387 return skip;
388 }
389 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700390
locke-lunargde3f0fa2020-09-10 11:55:31 -0600391 bool found_stage = false;
392 for (auto const &range : *push_constant_ranges) {
393 if (range.stageFlags & pStage->stage) {
394 found_stage = true;
395 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600396 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600397 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600398 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600399 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600400 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600401 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600402 const auto ret =
403 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700404
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600405 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600406 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600407 LogObjectList objlist(src->vk_shader_module());
408 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700409 skip |= LogError(objlist, vuid, "Push constant buffer:%s in %s is out of range in %s.", loc_descr.c_str(),
locke-lunargde3f0fa2020-09-10 11:55:31 -0600410 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600411 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600412 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700413 }
414 }
415 }
416
locke-lunargde3f0fa2020-09-10 11:55:31 -0600417 if (!found_stage) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600418 LogObjectList objlist(src->vk_shader_module());
419 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700420 skip |= LogError(objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600421 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module()).c_str(),
422 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700423 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700424 }
Chris Forbes47567b72017-06-09 12:09:45 -0700425 return skip;
426}
427
sfricke-samsungcfb44592021-07-25 00:36:28 -0700428bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700429 bool skip = false;
430
431 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700432 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700433 return skip;
434 }
435
sfricke-samsungcfb44592021-07-25 00:36:28 -0700436 // Find all builtin from just the interface variables
437 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700438 auto insn = src->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700439 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700440 const decoration_set decorations = src->get_decorations(insn.word(2));
441
sfricke-samsungcfb44592021-07-25 00:36:28 -0700442 // Currently don't need to search in structs
443 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700444 auto type_pointer = src->get_def(insn.word(1));
445 assert(type_pointer.opcode() == spv::OpTypePointer);
446
447 auto type = src->get_def(type_pointer.word(3));
448 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700449 uint32_t length = static_cast<uint32_t>(src->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700450 // Handles both the input and output sampleMask
451 if (length > phys_dev_props.limits.maxSampleMaskWords) {
452 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
453 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
454 "maxSampleMaskWords of %u in %s.",
455 length, phys_dev_props.limits.maxSampleMaskWords,
456 report_data->FormatHandle(src->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700457 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700458 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700459 }
460 }
461 }
462
463 return skip;
464}
465
Chris Forbes47567b72017-06-09 12:09:45 -0700466// Validate that data for each specialization entry is fully contained within the buffer.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200467bool CoreChecks::ValidateSpecializations(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700468 bool skip = false;
469
470 VkSpecializationInfo const *spec = info->pSpecializationInfo;
471
472 if (spec) {
473 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600474 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700475 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
476 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200477 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700478 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
479 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600480
481 continue;
482 }
Chris Forbes47567b72017-06-09 12:09:45 -0700483 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700484 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
485 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200486 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700487 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
488 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700489 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200490 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
491 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
492 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
493 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
494 j, spec->pMapEntries[i].constantID);
495 }
496 }
Chris Forbes47567b72017-06-09 12:09:45 -0700497 }
498 }
499
500 return skip;
501}
502
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500503// TODO (jbolz): Can this return a const reference?
sourav parmarcd5fb182020-07-17 12:58:44 -0700504static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count,
505 bool is_khr) {
Chris Forbes47567b72017-06-09 12:09:45 -0700506 auto type = module->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800507 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700508 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500509 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700510
511 // 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 -0500512 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
513 if (type.opcode() == spv::OpTypeRuntimeArray) {
514 descriptor_count = 0;
515 type = module->get_def(type.word(2));
516 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700517 descriptor_count *= module->GetConstantValueById(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700518 type = module->get_def(type.word(2));
519 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800520 if (type.word(2) == spv::StorageClassStorageBuffer) {
521 is_storage_buffer = true;
522 }
Chris Forbes47567b72017-06-09 12:09:45 -0700523 type = module->get_def(type.word(3));
524 }
525 }
526
527 switch (type.opcode()) {
528 case spv::OpTypeStruct: {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600529 for (const auto insn : module->GetDecorationInstructions()) {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800530 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700531 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800532 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500533 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
534 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
535 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800536 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500537 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
538 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
539 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
540 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800541 }
Chris Forbes47567b72017-06-09 12:09:45 -0700542 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500543 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
544 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
545 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700546 }
547 }
548 }
549
550 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500551 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700552 }
553
554 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500555 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
556 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
557 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700558
Chris Forbes73c00bf2018-06-22 16:28:06 -0700559 case spv::OpTypeSampledImage: {
560 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
561 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
562 auto image_type = module->get_def(type.word(2));
563 auto dim = image_type.word(3);
564 auto sampled = image_type.word(7);
565 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500566 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
567 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700568 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700569 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500570 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
571 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700572
573 case spv::OpTypeImage: {
574 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
575 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
576 auto dim = type.word(3);
577 auto sampled = type.word(7);
578
579 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500580 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
581 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700582 } else if (dim == spv::DimBuffer) {
583 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500584 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
585 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700586 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500587 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
588 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700589 }
590 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500591 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
592 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
593 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700594 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500595 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
596 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700597 }
598 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600599 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700600 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
601 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500602 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700603
604 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
605 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500606 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700607 }
608}
609
Jeff Bolze54ae892018-09-08 12:16:29 -0500610static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700611 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500612 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
613 if (ss.tellp()) ss << ", ";
614 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700615 }
616 return ss.str();
617}
618
sfricke-samsung0065ce02020-12-03 22:46:37 -0800619bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500620 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800621 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 -0500622 return true;
623 }
624 }
625
626 return false;
627}
628
sfricke-samsung0065ce02020-12-03 22:46:37 -0800629bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700630 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800631 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700632 return true;
633 }
634 }
635
636 return false;
637}
638
locke-lunarg63e4daf2020-08-17 17:53:25 -0600639bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
640 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500641 bool skip = false;
642
locke-lunarg63e4daf2020-08-17 17:53:25 -0600643 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800644 switch (stage) {
645 case VK_SHADER_STAGE_COMPUTE_BIT:
Jeff Bolz148d94e2018-12-13 21:25:56 -0600646 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
647 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
648 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
649 case VK_SHADER_STAGE_MISS_BIT_NV:
650 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
651 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
652 case VK_SHADER_STAGE_TASK_BIT_NV:
653 case VK_SHADER_STAGE_MESH_BIT_NV:
Chris Forbes349b3132018-03-07 11:38:08 -0800654 /* No feature requirements for writes and atomics from compute
Jeff Bolz148d94e2018-12-13 21:25:56 -0600655 * raytracing, or mesh stages */
Chris Forbes349b3132018-03-07 11:38:08 -0800656 break;
657 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800658 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700659 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800660 break;
661 default:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800662 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700663 "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800664 break;
665 }
666 }
667
Chris Forbes47567b72017-06-09 12:09:45 -0700668 return skip;
669}
670
sfricke-samsung94167ca2021-02-26 04:14:59 -0800671bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage,
672 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500673 bool skip = false;
674
sfricke-samsung94167ca2021-02-26 04:14:59 -0800675 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
676 if (GroupOperation(insn.opcode()) == true) {
677 // Check the quad operations.
678 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
679 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700680 skip |=
681 RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
682 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800683 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800684 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500685
sfricke-samsung94167ca2021-02-26 04:14:59 -0800686 uint32_t scope_type = spv::ScopeMax;
687 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
688 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
689 scope_type = spv::ScopeSubgroup;
690 } else {
691 // "All <id> used for Scope <id> must be of an OpConstant"
692 auto scope_id = module->get_def(insn.word(3));
693 scope_type = scope_id.word(3);
694 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800695
sfricke-samsung94167ca2021-02-26 04:14:59 -0800696 if (scope_type == spv::ScopeSubgroup) {
697 // "Group operations with subgroup scope" must have stage support
698 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
699 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700700 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800701 }
702
703 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
704 auto type = module->get_def(insn.word(1));
705
706 if (type.opcode() == spv::OpTypeVector) {
707 // Get the element type
708 type = module->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800709 }
710
sfricke-samsung94167ca2021-02-26 04:14:59 -0800711 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800712 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
713 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500714
sfricke-samsung0065ce02020-12-03 22:46:37 -0800715 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
716 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
717 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
718 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700719 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500720 }
721 }
722 }
Jeff Bolzee743412019-06-20 22:24:32 -0500723 }
724
725 return skip;
726}
727
ziga-lunarg70651522021-10-11 17:23:30 +0200728bool CoreChecks::ValidateMemoryScope(SHADER_MODULE_STATE const *src, const spirv_inst_iter &insn) const {
729 bool skip = false;
730
sfricke-samsung3511e312021-11-04 21:14:31 -0700731 const auto &entry = MemoryScopeParamPosition(insn.opcode());
ziga-lunarg70651522021-10-11 17:23:30 +0200732 if (entry > 0) {
733 const uint32_t scope_id = insn.word(entry);
734 if (enabled_features.core12.vulkanMemoryModel && !enabled_features.core12.vulkanMemoryModelDeviceScope) {
735 const auto &iter = src->GetConstantDef(scope_id);
736 if (iter != src->end()) {
737 if (GetConstantValue(iter) == spv::Scope::ScopeDevice) {
738 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06265",
739 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is enabled and "
740 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModelDeviceScope is disabled, but Device "
741 "memory scope is used.");
742 }
743 }
744 } else if (!enabled_features.core12.vulkanMemoryModel) {
745 const auto &iter = src->GetConstantDef(scope_id);
746 if (iter != src->end()) {
747 if (GetConstantValue(iter) == spv::Scope::ScopeQueueFamily) {
748 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06266",
749 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is not enabled, but QueueFamily "
750 "memory scope is used.");
751 }
752 }
753 }
754 }
755
756 return skip;
757}
758
ziga-lunarg2818f492021-08-12 14:30:51 +0200759bool CoreChecks::ValidateWorkgroupSize(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
760 const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
761 bool skip = false;
762
763 std::array<uint32_t, 3> work_group_size = src->GetWorkgroupSize(pStage, id_value_map);
764
765 for (uint32_t i = 0; i < 3; ++i) {
766 if (work_group_size[i] > phys_dev_props.limits.maxComputeWorkGroupSize[i]) {
767 const char member = 'x' + static_cast<int8_t>(i);
768 skip |= LogError(device, kVUID_Core_Shader_MaxComputeWorkGroupSize,
769 "Specialization constant is being used to specialize WorkGroupSize.%c, but value (%" PRIu32
770 ") is greater than VkPhysicalDeviceLimits::maxComputeWorkGroupSize[%" PRIu32 "] = %" PRIu32 ".",
771 member, work_group_size[i], i, phys_dev_props.limits.maxComputeWorkGroupSize[i]);
772 }
773 }
774 return skip;
775}
776
Mark Lobodzinski3c59d972019-04-25 11:28:14 -0600777bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -0600778 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200779 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
780 pStage->stage == VK_SHADER_STAGE_ALL) {
781 return false;
782 }
783
784 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700785 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200786
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700787 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200788 struct Variable {
789 uint32_t baseTypePtrID;
790 uint32_t ID;
791 uint32_t storageClass;
792 };
793 std::vector<Variable> variables;
794
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700795 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700796 bool is_iso_lines = false;
797 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500798
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700799 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600800
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200801 for (auto insn : *src) {
802 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500803 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200804 case spv::OpDecorate:
805 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500806 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700807 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200808 break;
809 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200810 default:
811 break;
812 }
813 break;
814 // Find all input and output variables
815 case spv::OpVariable: {
816 Variable var = {};
817 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600818 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
819 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700820 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200821 var.baseTypePtrID = insn.word(1);
822 var.ID = insn.word(2);
823 variables.push_back(var);
824 }
825 break;
826 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500827 case spv::OpExecutionMode:
828 if (insn.word(1) == entrypoint.word(2)) {
829 switch (insn.word(2)) {
830 default:
831 break;
832 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700833 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500834 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700835 case spv::ExecutionModeIsolines:
836 is_iso_lines = true;
837 break;
838 case spv::ExecutionModePointMode:
839 is_point_mode = true;
840 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500841 }
842 }
843 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200844 default:
845 break;
846 }
847 }
848
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500849 bool strip_output_array_level =
850 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
851 bool strip_input_array_level =
852 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
853 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
854
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700855 uint32_t num_comp_in = 0, num_comp_out = 0;
856 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600857
sfricke-samsung962cad92021-04-13 00:46:29 -0700858 auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
859 auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600860
861 // Find max component location used for input variables.
862 for (auto &var : inputs) {
863 int location = var.first.first;
864 int component = var.first.second;
865 interface_var &iv = var.second;
866
867 // Only need to look at the first location, since we use the type's whole size
868 if (iv.offset != 0) {
869 continue;
870 }
871
872 if (iv.is_patch) {
873 continue;
874 }
875
sfricke-samsung962cad92021-04-13 00:46:29 -0700876 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700877 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600878 }
879
880 // Find max component location used for output variables.
881 for (auto &var : outputs) {
882 int location = var.first.first;
883 int component = var.first.second;
884 interface_var &iv = var.second;
885
886 // Only need to look at the first location, since we use the type's whole size
887 if (iv.offset != 0) {
888 continue;
889 }
890
891 if (iv.is_patch) {
892 continue;
893 }
894
sfricke-samsung962cad92021-04-13 00:46:29 -0700895 int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700896 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600897 }
898
899 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
900 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700901 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
902 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200903 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500904 // Check if the variable is a patch. Patches can also be members of blocks,
905 // but if they are then the top-level arrayness has already been stripped
906 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700907 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200908
909 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700910 num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200911 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsung962cad92021-04-13 00:46:29 -0700912 num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200913 }
914 }
915
916 switch (pStage->stage) {
917 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700918 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700919 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700920 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
921 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
922 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700923 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200924 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700925 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700926 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700927 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
928 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
929 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600930 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200931 break;
932
933 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700934 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700935 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700936 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
937 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
938 "components by %u components",
939 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700940 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200941 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700942 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600943 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700944 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700945 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
946 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
947 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600948 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700949 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700950 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700951 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
952 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
953 "components by %u components",
954 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700955 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200956 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700957 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600958 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700959 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700960 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
961 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
962 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600963 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200964 break;
965
966 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700967 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700968 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700969 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
970 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
971 "components by %u components",
972 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700973 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200974 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700975 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600976 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700977 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700978 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
979 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
980 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600981 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700982 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700983 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700984 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
985 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
986 "components by %u components",
987 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700988 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200989 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700990 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600991 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700992 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700993 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
994 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
995 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600996 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700997 // Portability validation
998 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
999 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001000 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -07001001 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
1002 " is using abstract patch type IsoLines, but this is not supported on this platform");
1003 }
1004 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001005 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -07001006 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
1007 " is using abstract patch type PointMode, but this is not supported on this platform");
1008 }
1009 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001010 break;
1011
1012 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001013 if (num_comp_in > limits.maxGeometryInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001014 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001015 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1016 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
1017 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001018 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001019 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001020 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001021 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001022 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
1023 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
1024 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001025 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001026 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001027 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001028 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1029 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
1030 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001031 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001032 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001033 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001034 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001035 "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that "
1036 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
1037 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001038 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001039 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001040 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001041 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1042 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
1043 "components by %u components",
1044 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001045 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001046 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001047 break;
1048
1049 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001050 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001051 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001052 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
1053 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
1054 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001055 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001056 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001057 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001058 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001059 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1060 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1061 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001062 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001063 break;
1064
Jeff Bolz148d94e2018-12-13 21:25:56 -06001065 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1066 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1067 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1068 case VK_SHADER_STAGE_MISS_BIT_NV:
1069 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1070 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1071 case VK_SHADER_STAGE_TASK_BIT_NV:
1072 case VK_SHADER_STAGE_MESH_BIT_NV:
1073 break;
1074
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001075 default:
1076 assert(false); // This should never happen
1077 }
1078 return skip;
1079}
1080
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001081bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const {
1082 bool skip = false;
1083
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001084 // Got through all ImageRead/Write instructions
1085 for (auto insn : *src) {
1086 switch (insn.opcode()) {
1087 case spv::OpImageSparseRead:
1088 case spv::OpImageRead: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001089 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(3));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001090 if (type_def != src->end()) {
Tim Van Pattenffe91322021-07-26 10:20:50 -06001091 const auto dim = type_def.word(3);
1092 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1093 // StorageImageReadWithoutFormat Capability was declared.
1094 if (dim != spv::DimSubpassData && type_def.word(8) == spv::ImageFormatUnknown) {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001095 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1096 "shaderStorageImageReadWithoutFormat",
1097 kVUID_Features_shaderStorageImageReadWithoutFormat);
1098 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001099 }
1100 break;
1101 }
1102 case spv::OpImageWrite: {
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001103 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001104 if (type_def != src->end()) {
1105 if (type_def.word(8) == spv::ImageFormatUnknown) {
1106 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1107 "shaderStorageImageWriteWithoutFormat",
1108 kVUID_Features_shaderStorageImageWriteWithoutFormat);
1109 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001110 }
1111 break;
1112 }
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001113 }
1114 }
1115
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001116 // Go through all variables for images and check decorations
1117 for (auto insn : *src) {
1118 if (insn.opcode() != spv::OpVariable)
1119 continue;
1120
1121 uint32_t var = insn.word(2);
Lionel Landwerlin5f2065a2021-07-23 11:51:28 +03001122 spirv_inst_iter type_def = src->GetImageFormatInst(insn.word(1));
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001123 if (type_def == src->end())
1124 continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001125 // Only check if the Image Dim operand is not SubpassData
1126 const auto dim = type_def.word(3);
1127 if (dim == spv::DimSubpassData) continue;
Corentin Wallez91f8b6d2021-07-23 10:11:31 +02001128 // Only check storage images
1129 if (type_def.word(7) != 2) continue;
Tim Van Pattenffe91322021-07-26 10:20:50 -06001130 if (type_def.word(8) != spv::ImageFormatUnknown) continue;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001131
1132 decoration_set img_decorations = src->get_decorations(var);
1133
1134 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1135 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001136 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
1137 "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32
1138 " "
1139 " without format not marked a NonReadable",
1140 var);
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001141 }
1142
1143 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1144 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001145 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
1146 "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32
1147 " "
1148 "without format not marked a NonWritable",
1149 var);
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001150 }
1151 }
1152
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001153 return skip;
1154}
1155
sfricke-samsungdc96f302020-03-18 20:42:10 -07001156bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1157 bool skip = false;
1158 uint32_t total_resources = 0;
1159
1160 // Only currently testing for graphics and compute pipelines
1161 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1162 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1163 return false;
1164 }
1165
1166 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
amhagana448ea52021-11-02 14:09:14 -04001167 if (pipeline->rp_state->use_dynamic_rendering) {
Aaron Hagan92a44f82021-11-19 09:34:56 -05001168 total_resources += pipeline->rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001169 } else {
1170 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
1171 total_resources +=
1172 pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
1173 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001174 }
1175
1176 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1177 // input from CreatePipeline and CreatePipelineLayout level
1178 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1179 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1180 continue;
1181 }
1182
1183 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1184 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1185 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1186 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1187 // Check only descriptor types listed in maxPerStageResources description in spec
1188 switch (binding->descriptorType) {
1189 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1190 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1191 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1192 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1193 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1194 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1195 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1196 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1197 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1198 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1199 total_resources += binding->descriptorCount;
1200 break;
1201 default:
1202 break;
1203 }
1204 }
1205 }
1206 }
1207
1208 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1209 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1210 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001211 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001212 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1213 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1214 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1215 }
1216
1217 return skip;
1218}
1219
Jeff Bolze4356752019-03-07 11:23:46 -06001220// copy the specialization constant value into buf, if it is present
1221void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1222 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1223
1224 if (spec && spec_id < spec->mapEntryCount) {
1225 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1226 }
1227}
1228
1229// Fill in value with the constant or specialization constant value, if available.
1230// Returns true if the value has been accurately filled out.
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001231static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001232 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
Jeff Bolze4356752019-03-07 11:23:46 -06001233 auto type_id = src->get_def(insn.word(1));
1234 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1235 return false;
1236 }
1237 switch (insn.opcode()) {
1238 case spv::OpSpecConstant:
1239 *value = insn.word(3);
1240 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1241 return true;
1242 case spv::OpConstant:
1243 *value = insn.word(3);
1244 return true;
1245 default:
1246 return false;
1247 }
1248}
1249
1250// Map SPIR-V type to VK_COMPONENT_TYPE enum
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001251VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) {
Jeff Bolze4356752019-03-07 11:23:46 -06001252 switch (insn.opcode()) {
1253 case spv::OpTypeInt:
1254 switch (insn.word(2)) {
1255 case 8:
1256 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1257 case 16:
1258 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1259 case 32:
1260 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1261 case 64:
1262 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1263 default:
1264 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1265 }
1266 case spv::OpTypeFloat:
1267 switch (insn.word(2)) {
1268 case 16:
1269 return VK_COMPONENT_TYPE_FLOAT16_NV;
1270 case 32:
1271 return VK_COMPONENT_TYPE_FLOAT32_NV;
1272 case 64:
1273 return VK_COMPONENT_TYPE_FLOAT64_NV;
1274 default:
1275 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1276 }
1277 default:
1278 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1279 }
1280}
1281
1282// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1283// in SPIRV-Tools (e.g. due to specialization constant usage).
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001284bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001285 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001286 bool skip = false;
1287
1288 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001289 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001290 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001291 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001292
1293 struct CoopMatType {
1294 uint32_t scope, rows, cols;
1295 VkComponentTypeNV component_type;
1296 bool all_constant;
1297
1298 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1299
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06001300 void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001301 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
Jeff Bolze4356752019-03-07 11:23:46 -06001302 spirv_inst_iter insn = src->get_def(id);
1303 uint32_t component_type_id = insn.word(2);
1304 uint32_t scope_id = insn.word(3);
1305 uint32_t rows_id = insn.word(4);
1306 uint32_t cols_id = insn.word(5);
1307 auto component_type_iter = src->get_def(component_type_id);
1308 auto scope_iter = src->get_def(scope_id);
1309 auto rows_iter = src->get_def(rows_id);
1310 auto cols_iter = src->get_def(cols_id);
1311
1312 all_constant = true;
1313 if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) {
1314 all_constant = false;
1315 }
1316 if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) {
1317 all_constant = false;
1318 }
1319 if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) {
1320 all_constant = false;
1321 }
1322 component_type = GetComponentType(component_type_iter, src);
1323 }
1324 };
1325
1326 bool seen_coopmat_capability = false;
1327
1328 for (auto insn : *src) {
1329 // Whitelist instructions whose result can be a cooperative matrix type, and
1330 // keep track of their types. It would be nice if SPIRV-Headers generated code
1331 // to identify which instructions have a result type and result id. Lacking that,
1332 // this whitelist is based on the set of instructions that
1333 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1334 switch (insn.opcode()) {
1335 case spv::OpLoad:
1336 case spv::OpCooperativeMatrixLoadNV:
1337 case spv::OpCooperativeMatrixMulAddNV:
1338 case spv::OpSNegate:
1339 case spv::OpFNegate:
1340 case spv::OpIAdd:
1341 case spv::OpFAdd:
1342 case spv::OpISub:
1343 case spv::OpFSub:
1344 case spv::OpFDiv:
1345 case spv::OpSDiv:
1346 case spv::OpUDiv:
1347 case spv::OpMatrixTimesScalar:
1348 case spv::OpConstantComposite:
1349 case spv::OpCompositeConstruct:
1350 case spv::OpConvertFToU:
1351 case spv::OpConvertFToS:
1352 case spv::OpConvertSToF:
1353 case spv::OpConvertUToF:
1354 case spv::OpUConvert:
1355 case spv::OpSConvert:
1356 case spv::OpFConvert:
1357 id_to_type_id[insn.word(2)] = insn.word(1);
1358 break;
1359 default:
1360 break;
1361 }
1362
1363 switch (insn.opcode()) {
1364 case spv::OpDecorate:
1365 if (insn.word(2) == spv::DecorationSpecId) {
1366 id_to_spec_id[insn.word(1)] = insn.word(3);
1367 }
1368 break;
1369 case spv::OpCapability:
1370 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1371 seen_coopmat_capability = true;
1372
1373 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001374 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001375 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001376 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1377 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001378 }
1379 }
1380 break;
1381 case spv::OpMemoryModel:
1382 // If the capability isn't enabled, don't bother with the rest of this function.
1383 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1384 if (!seen_coopmat_capability) {
1385 return skip;
1386 }
1387 break;
1388 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001389 CoopMatType m;
1390 m.Init(insn.word(1), src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001391
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001392 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001393 // Validate that the type parameters are all supported for one of the
1394 // operands of a cooperative matrix property.
1395 bool valid = false;
1396 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001397 if (cooperative_matrix_properties[i].AType == m.component_type &&
1398 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1399 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001400 valid = true;
1401 break;
1402 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001403 if (cooperative_matrix_properties[i].BType == m.component_type &&
1404 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1405 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001406 valid = true;
1407 break;
1408 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001409 if (cooperative_matrix_properties[i].CType == m.component_type &&
1410 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == 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].DType == m.component_type &&
1416 cooperative_matrix_properties[i].MSize == 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 }
1421 }
1422 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001423 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001424 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1425 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001426 }
1427 }
1428 break;
1429 }
1430 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001431 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001432 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1433 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1434 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1435 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001436 // Couldn't find type of matrix
1437 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001438 break;
1439 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001440 d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id);
1441 a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id);
1442 b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id);
1443 c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001444
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001445 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001446 // Validate that the type parameters are all supported for the same
1447 // cooperative matrix property.
1448 bool valid = false;
1449 for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001450 if (cooperative_matrix_properties[i].AType == a.component_type &&
1451 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1452 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001453
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001454 cooperative_matrix_properties[i].BType == b.component_type &&
1455 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1456 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001457
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001458 cooperative_matrix_properties[i].CType == c.component_type &&
1459 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1460 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001461
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001462 cooperative_matrix_properties[i].DType == d.component_type &&
1463 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1464 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001465 valid = true;
1466 break;
1467 }
1468 }
1469 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001470 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001471 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1472 "VkCooperativeMatrixPropertiesNV",
1473 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001474 }
1475 }
1476 break;
1477 }
1478 default:
1479 break;
1480 }
1481 }
1482
1483 return skip;
1484}
1485
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001486bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage,
1487 const PIPELINE_STATE *pipeline) const {
1488 bool skip = false;
1489
1490 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1491 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1492 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
1493 for (auto insn : *src) {
1494 switch (insn.opcode()) {
1495 case spv::OpCapability:
1496 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1497 auto subpass_flags =
1498 (pipeline->rp_state == nullptr)
1499 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001500 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001501 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1502 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001503 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001504 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1505 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1506 }
1507 }
1508 break;
1509 default:
1510 break;
1511 }
1512 }
1513 }
1514
1515 return skip;
1516}
1517
ziga-lunarg73163742021-08-25 13:15:29 +02001518bool CoreChecks::ValidateShaderSubgroupSizeControl(VkPipelineShaderStageCreateInfo const *pStage) const {
1519 bool skip = false;
1520
1521 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
1522 !enabled_features.subgroup_size_control_features.subgroupSizeControl) {
1523 skip |= LogError(
1524 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1525 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1526 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1527 }
1528
1529 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
1530 !enabled_features.subgroup_size_control_features.computeFullSubgroups) {
1531 skip |= LogError(
1532 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1533 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1534 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1535 }
1536
1537 return skip;
1538}
1539
sfricke-samsung58b84352021-07-31 21:41:04 -07001540bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *src) const {
1541 bool skip = false;
1542
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001543 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001544 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001545
sfricke-samsungf5042b12021-08-05 01:09:40 -07001546 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1547 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1548
1549 const bool valid_storage_buffer_float = (
1550 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1551 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1552 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1553 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1554 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1555 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1556 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1557 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1558 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1559
1560 const bool valid_workgroup_float = (
1561 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1562 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1563 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1564 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1565 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1566 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1567 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1568 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1569 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1570
1571 const bool valid_image_float = (
1572 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1573 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1574 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1575
1576 const bool valid_16_float = (
1577 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1578 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1579 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1580 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1581 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1582 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1583
1584 const bool valid_32_float = (
1585 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1586 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1587 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1588 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1589 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1590 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1591 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1592 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1593 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1594
1595 const bool valid_64_float = (
1596 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1597 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1598 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1599 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1600 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1601 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1602 // clang-format on
1603
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001604 for (const auto &atomic_inst : src->GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001605 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungf5042b12021-08-05 01:09:40 -07001606 const uint32_t opcode = src->at(atomic_inst.first).opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001607
1608 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001609 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001610 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1611 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001612 skip |= LogError(device, "VUID-RuntimeSpirv-None-06278",
1613 "%s: Can't use 64-bit int atomics operations (%s) with %s storage class without "
1614 "shaderBufferInt64Atomics enabled.",
1615 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode),
1616 StorageClassName(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001617 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1618 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001619 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001620 "%s: Can't use 64-bit int atomics operations (%s) with Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001621 "shaderSharedInt64Atomics enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001622 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001623 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001624 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001625 "%s: Can't use 64-bit int atomics operations (%s) with Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001626 "shaderImageInt64Atomics enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001627 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsung58b84352021-07-31 21:41:04 -07001628 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001629 } else if (atomic.type == spv::OpTypeFloat) {
1630 // Validate Floats
1631 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1632 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001633 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1634 : "VUID-RuntimeSpirv-None-06280";
1635 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001636 "%s: Can't use float atomics operations (%s) with StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001637 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1638 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1639 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1640 "shaderBufferFloat64AtomicMinMax enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001641 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001642 } else if (opcode == spv::OpAtomicFAddEXT) {
1643 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1644 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1645 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with "
1646 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
1647 report_data->FormatHandle(src->vk_shader_module()).c_str());
1648 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1649 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1650 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with "
1651 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
1652 report_data->FormatHandle(src->vk_shader_module()).c_str());
1653 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1654 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1655 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with "
1656 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
1657 report_data->FormatHandle(src->vk_shader_module()).c_str());
1658 }
1659 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1660 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
1661 skip |= LogError(
1662 device, kVUID_Core_Shader_AtomicFeature,
1663 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1664 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
1665 report_data->FormatHandle(src->vk_shader_module()).c_str());
1666 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
1667 skip |= LogError(
1668 device, kVUID_Core_Shader_AtomicFeature,
1669 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1670 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
1671 report_data->FormatHandle(src->vk_shader_module()).c_str());
1672 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
1673 skip |= LogError(
1674 device, kVUID_Core_Shader_AtomicFeature,
1675 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1676 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
1677 report_data->FormatHandle(src->vk_shader_module()).c_str());
1678 }
1679 } else {
1680 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1681 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
1682 skip |= LogError(
1683 device, kVUID_Core_Shader_AtomicFeature,
1684 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1685 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
1686 report_data->FormatHandle(src->vk_shader_module()).c_str());
1687 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
1688 skip |= LogError(
1689 device, kVUID_Core_Shader_AtomicFeature,
1690 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1691 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
1692 report_data->FormatHandle(src->vk_shader_module()).c_str());
1693 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
1694 skip |= LogError(
1695 device, kVUID_Core_Shader_AtomicFeature,
1696 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1697 "OpAtomicExchange) with StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
1698 report_data->FormatHandle(src->vk_shader_module()).c_str());
1699 }
1700 }
1701 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1702 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001703 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1704 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001705 skip |=
1706 LogError(device, vuid,
1707 "%s: Can't use float atomics operations (%s) with Workgroup storage class without "
1708 "shaderSharedFloat32Atomics or "
1709 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1710 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1711 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1712 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001713 } else if (opcode == spv::OpAtomicFAddEXT) {
1714 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1715 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1716 "%s: Can't use 16-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1717 "storage class without shaderSharedFloat16AtomicAdd enabled.",
1718 report_data->FormatHandle(src->vk_shader_module()).c_str());
1719 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1720 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1721 "%s: Can't use 32-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1722 "storage class without shaderSharedFloat32AtomicAdd enabled.",
1723 report_data->FormatHandle(src->vk_shader_module()).c_str());
1724 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1725 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1726 "%s: Can't use 64-bit float atomics for add operations (OpAtomicFAddEXT) with Workgroup "
1727 "storage class without shaderSharedFloat64AtomicAdd enabled.",
1728 report_data->FormatHandle(src->vk_shader_module()).c_str());
1729 }
1730 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1731 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
1732 skip |= LogError(
1733 device, kVUID_Core_Shader_AtomicFeature,
1734 "%s: Can't use 16-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1735 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
1736 report_data->FormatHandle(src->vk_shader_module()).c_str());
1737 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
1738 skip |= LogError(
1739 device, kVUID_Core_Shader_AtomicFeature,
1740 "%s: Can't use 32-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1741 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
1742 report_data->FormatHandle(src->vk_shader_module()).c_str());
1743 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
1744 skip |= LogError(
1745 device, kVUID_Core_Shader_AtomicFeature,
1746 "%s: Can't use 64-bit float atomics for min/max operations (OpAtomicFMinEXT or OpAtomicFMaxEXT) with "
1747 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
1748 report_data->FormatHandle(src->vk_shader_module()).c_str());
1749 }
1750 } else {
1751 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1752 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
1753 skip |= LogError(
1754 device, kVUID_Core_Shader_AtomicFeature,
1755 "%s: Can't use 16-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1756 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat16Atomics enabled.",
1757 report_data->FormatHandle(src->vk_shader_module()).c_str());
1758 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
1759 skip |= LogError(
1760 device, kVUID_Core_Shader_AtomicFeature,
1761 "%s: Can't use 32-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1762 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat32Atomics enabled.",
1763 report_data->FormatHandle(src->vk_shader_module()).c_str());
1764 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
1765 skip |= LogError(
1766 device, kVUID_Core_Shader_AtomicFeature,
1767 "%s: Can't use 64-bit float atomics for load/store/exhange operations (OpAtomicLoad, OpAtomicStore, "
1768 "OpAtomicExchange) with Workgroup storage class without shaderSharedFloat64Atomics enabled.",
1769 report_data->FormatHandle(src->vk_shader_module()).c_str());
1770 }
1771 }
1772 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001773 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1774 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001775 skip |= LogError(
1776 device, vuid,
1777 "%s: Can't use float atomics operations (%s) with Image storage class without shaderImageFloat32Atomics or "
1778 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
1779 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001780 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001781 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001782 "%s: Can't use 16-bit float atomics operations (%s) without shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001783 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1784 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001785 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001786 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001787 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1788 : "VUID-RuntimeSpirv-None-06335";
1789 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001790 "%s: Can't use 32-bit float atomics operations (%s) without shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001791 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1792 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1793 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1794 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001795 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001796 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001797 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1798 : "VUID-RuntimeSpirv-None-06336";
1799 skip |= LogError(device, vuid,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001800 "%s: Can't use 64-bit float atomics operations (%s) without shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001801 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1802 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001803 report_data->FormatHandle(src->vk_shader_module()).c_str(), string_SpvOpcode(opcode));
sfricke-samsungf5042b12021-08-05 01:09:40 -07001804 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001805 }
1806 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001807 return skip;
1808}
1809
John Zulaufac4c6e12019-07-01 16:05:58 -06001810bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001811 auto entrypoint_id = entrypoint.word(2);
1812
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001813 // The first denorm execution mode encountered, along with its bit width.
1814 // Used to check if SeparateDenormSettings is respected.
1815 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001816
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001817 // The first rounding mode encountered, along with its bit width.
1818 // Used to check if SeparateRoundingModeSettings is respected.
1819 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001820
1821 bool skip = false;
1822
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001823 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001824 uint32_t invocations = 0;
1825
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001826 const auto &execution_mode_inst = src->GetExecutionModeInstructions();
1827 auto it = execution_mode_inst.find(entrypoint_id);
1828 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001829 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001830 auto mode = insn.word(2);
1831 switch (mode) {
1832 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1833 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001834 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001835 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001836 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
1837 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device");
1838 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1839 skip |= LogError(
1840 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
1841 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device");
1842 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1843 skip |= LogError(
1844 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
1845 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001846 }
1847 break;
1848 }
1849
1850 case spv::ExecutionModeDenormPreserve: {
1851 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001852 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1853 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
1854 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device");
1855 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1856 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
1857 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device");
1858 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1859 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
1860 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001861 }
1862
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001863 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1864 // Register the first denorm execution mode found
1865 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001866 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001867 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001868 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001869 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001870 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001871 "Shader uses different denorm execution modes for 16 and 64-bit but "
1872 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001873 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001874 }
1875 break;
1876
Mike Schuchardt2df08912020-12-15 16:28:09 -08001877 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001878 break;
1879
Mike Schuchardt2df08912020-12-15 16:28:09 -08001880 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001881 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001882 "Shader uses different denorm execution modes for different bit widths but "
1883 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001884 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001885 break;
1886
1887 default:
1888 break;
1889 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001890 }
1891 break;
1892 }
1893
1894 case spv::ExecutionModeDenormFlushToZero: {
1895 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001896 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
1897 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1898 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device");
1899 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
1900 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1901 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device");
1902 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
1903 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1904 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001905 }
1906
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001907 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1908 // Register the first denorm execution mode found
1909 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001910 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001911 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001912 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001913 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001914 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001915 "Shader uses different denorm execution modes for 16 and 64-bit but "
1916 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001917 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001918 }
1919 break;
1920
Mike Schuchardt2df08912020-12-15 16:28:09 -08001921 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001922 break;
1923
Mike Schuchardt2df08912020-12-15 16:28:09 -08001924 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001925 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001926 "Shader uses different denorm execution modes for different bit widths but "
1927 "denormBehaviorIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001928 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001929 break;
1930
1931 default:
1932 break;
1933 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001934 }
1935 break;
1936 }
1937
1938 case spv::ExecutionModeRoundingModeRTE: {
1939 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001940 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1941 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
1942 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device");
1943 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1944 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
1945 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device");
1946 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1947 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
1948 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001949 }
1950
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001951 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1952 // Register the first rounding mode found
1953 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001954 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001955 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001956 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001957 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001958 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001959 "Shader uses different rounding modes for 16 and 64-bit but "
1960 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001961 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001962 }
1963 break;
1964
Mike Schuchardt2df08912020-12-15 16:28:09 -08001965 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001966 break;
1967
Mike Schuchardt2df08912020-12-15 16:28:09 -08001968 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001969 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001970 "Shader uses different rounding modes for different bit widths but "
1971 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08001972 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001973 break;
1974
1975 default:
1976 break;
1977 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001978 }
1979 break;
1980 }
1981
1982 case spv::ExecutionModeRoundingModeRTZ: {
1983 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001984 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
1985 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
1986 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device");
1987 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
1988 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
1989 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device");
1990 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
1991 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
1992 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device");
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001993 }
1994
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001995 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1996 // Register the first rounding mode found
1997 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001998 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001999 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08002000 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002001 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002002 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002003 "Shader uses different rounding modes for 16 and 64-bit but "
2004 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08002005 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002006 }
2007 break;
2008
Mike Schuchardt2df08912020-12-15 16:28:09 -08002009 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002010 break;
2011
Mike Schuchardt2df08912020-12-15 16:28:09 -08002012 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002013 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002014 "Shader uses different rounding modes for different bit widths but "
2015 "roundingModeIndependence is "
Mike Schuchardt2df08912020-12-15 16:28:09 -08002016 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device");
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002017 break;
2018
2019 default:
2020 break;
2021 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002022 }
2023 break;
2024 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002025
2026 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002027 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002028 break;
2029 }
2030
2031 case spv::ExecutionModeInvocations: {
2032 invocations = insn.word(3);
2033 break;
2034 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002035
2036 case spv::ExecutionModeLocalSizeId: {
2037 if (!enabled_features.maintenance4_features.maintenance4) {
2038 skip |= LogError(device, "VUID-RuntimeSpirv-LocalSizeId-06434",
2039 "LocalSizeId execution mode used but maintenance4 feature not enabled");
2040 }
2041 break;
2042 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002043 }
2044 }
2045 }
2046
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002047 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002048 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002049 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2050 "Geometry shader entry point must have an OpExecutionMode instruction that "
2051 "specifies a maximum output vertex count that is greater than 0 and less "
2052 "than or equal to maxGeometryOutputVertices. "
2053 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002054 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002055 }
2056
2057 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002058 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2059 "Geometry shader entry point must have an OpExecutionMode instruction that "
2060 "specifies an invocation count that is greater than 0 and less "
2061 "than or equal to maxGeometryShaderInvocations. "
2062 "Invocations=%d, maxGeometryShaderInvocations=%d",
2063 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002064 }
2065 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002066 return skip;
2067}
2068
Chris Forbes47567b72017-06-09 12:09:45 -07002069// For given pipelineLayout verify that the set_layout_node at slot.first
2070// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002071static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002072 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002073 if (!pipelineLayout) return nullptr;
2074
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002075 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07002076
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002077 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07002078}
2079
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002080// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2081// o If there is only a vertex shader : gl_PointSize must be written when using points
2082// o If there is a geometry or tessellation shader:
2083// - If shaderTessellationAndGeometryPointSize feature is enabled:
2084// * gl_PointSize must be written in the final geometry stage
2085// - If shaderTessellationAndGeometryPointSize feature is disabled:
2086// * gl_PointSize must NOT be written and a default of 1.0 is assumed
Mark Lobodzinski3c59d972019-04-25 11:28:14 -06002087bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
John Zulaufac4c6e12019-07-01 16:05:58 -06002088 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002089 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2090 return false;
2091 }
2092
2093 bool pointsize_written = false;
2094 bool skip = false;
2095
2096 // Search for PointSize built-in decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002097 for (const auto &set : src->GetBuiltinDecorationList()) {
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002098 auto insn = src->at(set.offset);
2099 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002100 pointsize_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002101 if (pointsize_written) {
2102 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002103 }
2104 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002105 }
2106
2107 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002108 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002109 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002110 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002111 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2112 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002113 }
2114 } else if (!pointsize_written) {
2115 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002116 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002117 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2118 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002119 }
2120 return skip;
2121}
John Zulauf14c355b2019-06-27 16:09:37 -06002122
Tobias Hector6663c9b2020-11-05 10:18:02 +00002123bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src,
2124 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2125 bool primitiverate_written = false;
2126 bool viewportindex_written = false;
2127 bool viewportmask_written = false;
2128 bool skip = false;
2129
2130 // Check if the primitive shading rate is written
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002131 for (const auto &set : src->GetBuiltinDecorationList()) {
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002132 auto insn = src->at(set.offset);
2133 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002134 primitiverate_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002135 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002136 viewportindex_written = src->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002137 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002138 viewportmask_written = src->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002139 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002140 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2141 break;
2142 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002143 }
2144
Tony-LunarGd44844c2021-01-22 13:24:37 -07002145 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002146 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002147 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002148 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002149 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002150 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2151 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2152 "multiple viewports "
2153 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2154 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002155 }
2156
2157 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002158 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002159 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2160 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2161 "ViewportIndex built-ins,"
2162 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2163 string_VkShaderStageFlagBits(stage));
2164 }
2165
2166 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002167 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002168 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2169 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2170 "ViewportMaskNV built-ins,"
2171 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2172 string_VkShaderStageFlagBits(stage));
2173 }
2174 }
2175 return skip;
2176}
2177
ziga-lunargce66e542021-09-19 00:11:14 +02002178bool CoreChecks::ValidateDecorations(SHADER_MODULE_STATE const* module) const {
2179 bool skip = false;
2180
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002181 std::vector<spirv_inst_iter> xfb_streams;
2182 std::vector<spirv_inst_iter> xfb_buffers;
ziga-lunargef2c3172021-11-07 10:35:29 +01002183 std::vector<spirv_inst_iter> xfb_offsets;
2184
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002185 for (const auto &op_decorate : module->GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002186 uint32_t decoration = op_decorate.word(2);
2187 if (decoration == spv::DecorationXfbStride) {
2188 uint32_t stride = op_decorate.word(3);
2189 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2190 skip |= LogError(
2191 device, "VUID-RuntimeSpirv-XfbStride-06313",
2192 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2193 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2194 ").",
2195 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2196 }
2197 }
ziga-lunarg423cf212021-11-07 00:00:27 +01002198 if (decoration == spv::DecorationStream) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002199 xfb_streams.push_back(op_decorate);
ziga-lunarg423cf212021-11-07 00:00:27 +01002200 uint32_t stream = op_decorate.word(3);
2201 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2202 skip |= LogError(
2203 device, "VUID-RuntimeSpirv-Stream-06312",
2204 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2205 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32 ").",
2206 stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
2207 }
2208 }
ziga-lunargef2c3172021-11-07 10:35:29 +01002209 if (decoration == spv::DecorationXfbBuffer) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002210 xfb_buffers.push_back(op_decorate);
ziga-lunargef2c3172021-11-07 10:35:29 +01002211 }
2212 if (decoration == spv::DecorationOffset) {
2213 xfb_offsets.push_back(op_decorate);
2214 }
2215 }
2216
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002217 // XfbBuffer, buffer data size
2218 std::vector<std::pair<uint32_t, uint32_t>> buffer_data_sizes;
ziga-lunargef2c3172021-11-07 10:35:29 +01002219 for (const auto &op_decorate : xfb_offsets) {
2220 for (const auto xfb_buffer : xfb_buffers) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002221 if (xfb_buffer.word(1) == op_decorate.word(1)) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002222 const auto offset = op_decorate.word(3);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002223 const auto def = module->get_def(xfb_buffer.word(1));
ziga-lunargef2c3172021-11-07 10:35:29 +01002224 const auto size = module->GetTypeBytesSize(def);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002225 const uint32_t buffer_data_size = offset + size;
2226 if (buffer_data_size > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002227 skip |= LogError(
2228 device, "VUID-RuntimeSpirv-Offset-06308",
2229 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_offset (%" PRIu32
2230 ") + size of variable (%" PRIu32 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2231 "(%" PRIu32 ").",
2232 offset, size, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2233 }
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002234
2235 bool found = false;
2236 for (auto &bds : buffer_data_sizes) {
2237 if (bds.first == xfb_buffer.word(1)) {
2238 bds.second = std::max(bds.second, buffer_data_size);
2239 found = true;
2240 break;
2241 }
2242 }
2243 if (!found) {
2244 buffer_data_sizes.emplace_back(xfb_buffer.word(1), buffer_data_size);
2245 }
2246
ziga-lunargef2c3172021-11-07 10:35:29 +01002247 break;
2248 }
2249 }
ziga-lunargce66e542021-09-19 00:11:14 +02002250 }
2251
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002252 std::unordered_map<uint32_t, uint32_t> stream_data_size;
2253 for (const auto &xfb_stream : xfb_streams) {
2254 for (const auto& bds : buffer_data_sizes) {
2255 if (xfb_stream.word(1) == bds.first) {
2256 uint32_t stream = xfb_stream.word(3);
2257 const auto itr = stream_data_size.find(stream);
2258 if (itr != stream_data_size.end()) {
2259 itr->second += bds.second;
2260 } else {
2261 stream_data_size.insert({stream, bds.second});
2262 }
2263 }
2264 }
2265 }
2266
2267 for (const auto& stream : stream_data_size) {
2268 if (stream.second > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreamDataSize) {
2269 skip |= LogError(device, "VUID-RuntimeSpirv-XfbBuffer-06309",
2270 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2271 ") having the sum of buffer data sizes (%" PRIu32
2272 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2273 "(%" PRIu32 ").",
2274 stream.first, stream.second,
2275 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2276 }
2277 }
2278
ziga-lunargce66e542021-09-19 00:11:14 +02002279 return skip;
2280}
2281
ziga-lunarg28d08792021-10-13 15:42:59 +02002282bool CoreChecks::ValidateTransformFeedback(SHADER_MODULE_STATE const *src) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002283 bool skip = false;
2284
ziga-lunarg28d08792021-10-13 15:42:59 +02002285 // Temp workaround to prevent false positive errors
2286 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
2287 if (src->HasMultipleEntryPoints()) {
2288 return skip;
2289 }
2290
2291 layer_data::unordered_set<uint32_t> emitted_streams;
2292 bool output_points = false;
2293 for (const auto& insn : *src) {
2294 const uint32_t opcode = insn.opcode();
2295 if (opcode == spv::OpEmitStreamVertex) {
2296 emitted_streams.emplace(static_cast<uint32_t>(src->GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002297 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002298 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
2299 uint32_t stream = static_cast<uint32_t>(src->GetConstantValueById(insn.word(1)));
2300 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2301 skip |= LogError(
2302 device, "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002303 "vkCreateGraphicsPipelines(): shader uses transform feedback stream (%s) with index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002304 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2305 ").",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002306 string_SpvOpcode(opcode), stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002307 }
2308 }
2309 if (opcode == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeOutputPoints) {
2310 output_points = true;
2311 }
2312 }
2313
2314 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2315 if (emitted_streams_size > 1 && !output_points &&
2316 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
2317 skip |= LogError(
2318 device, "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2319 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles is VK_FALSE, but execution mode is not OutputPoints.",
2320 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002321 }
2322
2323 return skip;
2324}
2325
sfricke-samsung864162a2021-11-01 21:58:01 -07002326// Checks for both TexelOffset and TexelGatherOffset limits
2327bool CoreChecks::ValidateTexelOffsetLimits(SHADER_MODULE_STATE const *src, spirv_inst_iter &insn) const {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002328 bool skip = false;
2329
2330 const uint32_t opcode = insn.opcode();
sfricke-samsung864162a2021-11-01 21:58:01 -07002331 if (ImageGatherOperation(opcode) || ImageSampleOperation(opcode) || ImageFetchOperation(opcode)) {
sfricke-samsung3511e312021-11-04 21:14:31 -07002332 uint32_t image_operand_position = ImageOperandsParamPosition(opcode);
sfricke-samsung864162a2021-11-01 21:58:01 -07002333 // Image operands can be optional
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002334 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2335 auto image_operand = insn.word(image_operand_position);
sfricke-samsung864162a2021-11-01 21:58:01 -07002336 // Bits we are validating (sample/fetch only check ConstOffset)
ziga-lunarga12c75a2021-09-16 16:36:16 +02002337 uint32_t offset_bits =
sfricke-samsung864162a2021-11-01 21:58:01 -07002338 ImageGatherOperation(opcode)
2339 ? (spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask)
2340 : (spv::ImageOperandsConstOffsetMask);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002341 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002342 // Operand values follow
2343 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002344 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2345 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2346 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2347 if (image_operand & i) { // If the bit is set, consume operand
2348 if (insn.len() > index && (i & offset_bits)) {
2349 uint32_t constant_id = insn.word(index);
2350 const auto &constant = src->get_def(constant_id);
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002351 const bool is_dynamic_offset = constant == src->end();
2352 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002353 for (uint32_t j = 3; j < constant.len(); ++j) {
2354 uint32_t comp_id = constant.word(j);
2355 const auto &comp = src->get_def(comp_id);
sfricke-samsungef3fe742021-10-06 10:51:34 -07002356 const auto &comp_type = src->get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002357 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002358 const uint32_t offset = comp.word(3);
sfricke-samsung864162a2021-11-01 21:58:01 -07002359 // spec requires minTexelGatherOffset/minTexelOffset to be -8 or less so never can compare if
2360 // unsigned spec requires maxTexelGatherOffset/maxTexelOffset to be 7 or greater so never can
2361 // compare if signed is less then zero
sfricke-samsungef3fe742021-10-06 10:51:34 -07002362 const int32_t signed_offset = static_cast<int32_t>(offset);
2363 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2364
sfricke-samsung864162a2021-11-01 21:58:01 -07002365 // There are 2 sets of VU being covered where the only main difference is the opcode
2366 if (ImageGatherOperation(opcode)) {
2367 // min/maxTexelGatherOffset
2368 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
2369 skip |=
2370 LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002371 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002372 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002373 string_SpvOpcode(opcode), signed_offset,
2374 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002375 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2376 (!use_signed || (use_signed && signed_offset > 0))) {
2377 skip |= LogError(
2378 device, "VUID-RuntimeSpirv-OpImage-06377",
2379 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIu32
2380 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32 ").",
2381 string_SpvOpcode(opcode), offset, phys_dev_props.limits.maxTexelGatherOffset);
2382 }
2383 } else {
2384 // min/maxTexelOffset
2385 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelOffset)) {
2386 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06435",
2387 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIi32
2388 ") less than VkPhysicalDeviceLimits::minTexelOffset (%" PRIi32 ").",
2389 string_SpvOpcode(opcode), signed_offset,
2390 phys_dev_props.limits.minTexelOffset);
2391 } else if ((offset > phys_dev_props.limits.maxTexelOffset) &&
2392 (!use_signed || (use_signed && signed_offset > 0))) {
2393 skip |=
2394 LogError(device, "VUID-RuntimeSpirv-OpImageSample-06436",
2395 "vkCreateShaderModule(): Shader uses %s with offset (%" PRIu32
2396 ") greater than VkPhysicalDeviceLimits::maxTexelOffset (%" PRIu32 ").",
2397 string_SpvOpcode(opcode), offset, phys_dev_props.limits.maxTexelOffset);
2398 }
ziga-lunarga12c75a2021-09-16 16:36:16 +02002399 }
2400 }
2401 }
2402 }
sfricke-samsung3511e312021-11-04 21:14:31 -07002403 index += ImageOperandsParamCount(i);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002404 }
2405 }
2406 }
2407 }
2408 }
2409
2410 return skip;
2411}
2412
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002413bool CoreChecks::ValidateShaderClock(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002414 bool skip = false;
2415
sfricke-samsung94167ca2021-02-26 04:14:59 -08002416 switch (insn.opcode()) {
2417 case spv::OpReadClockKHR: {
2418 auto scope_id = module->get_def(insn.word(3));
2419 auto scope_type = scope_id.word(3);
2420 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002421 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002422 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002423 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002424 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002425 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002426 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsung94167ca2021-02-26 04:14:59 -08002427 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002428 report_data->FormatHandle(module->vk_shader_module()).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002429 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002430 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002431 }
2432 }
2433 return skip;
2434}
2435
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002436bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2437 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002438 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002439 const auto *pStage = stage_state.create_info;
2440 const auto *module = stage_state.module.get();
2441 const auto &entrypoint = stage_state.entrypoint;
John Zulauf14c355b2019-06-27 16:09:37 -06002442 // Check the module
2443 if (!module->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002444 skip |= LogError(
2445 device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s.",
2446 report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002447 }
2448
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002449 // If specialization-constant values are given and specialization-constant instructions are present in the shader, the
2450 // specializations should be applied and validated.
2451 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002452 pStage->pSpecializationInfo->pMapEntries != nullptr && module->HasSpecConstants()) {
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002453 // Gather the specialization-constant values.
2454 auto const &specialization_info = pStage->pSpecializationInfo;
Jeremy Hayes521221d2020-01-15 16:48:49 -07002455 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
Jeremy Gebbencbf22862021-03-03 12:01:22 -07002456 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 -06002457 id_value_map.reserve(specialization_info->mapEntryCount);
2458 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2459 auto const &map_entry = specialization_info->pMapEntries[i];
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002460 const auto itr = module->GetSpecConstMap().find(map_entry.constantID);
sfricke-samsung033b0262021-07-09 00:53:06 -07002461 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2462 // behavior of the pipeline."
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06002463 if (itr != module->GetSpecConstMap().cend()) {
sfricke-samsung033b0262021-07-09 00:53:06 -07002464 // Make sure map_entry.size matches the spec constant's size
2465 uint32_t spec_const_size = decoration_set::kInvalidValue;
2466 const auto def_ins = module->get_def(itr->second);
2467 const auto type_ins = module->get_def(def_ins.word(1));
2468 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2469 switch (type_ins.opcode()) {
2470 case spv::OpTypeBool:
2471 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2472 spec_const_size = sizeof(VkBool32);
2473 break;
2474 case spv::OpTypeInt:
2475 case spv::OpTypeFloat:
2476 spec_const_size = type_ins.word(2) / 8;
2477 break;
2478 default:
2479 // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant
2480 // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat
2481 break;
2482 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002483
sfricke-samsung033b0262021-07-09 00:53:06 -07002484 if (map_entry.size != spec_const_size) {
2485 skip |=
2486 LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002487 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2488 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.",
2489 map_entry.constantID, i, map_entry.size,
2490 report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung033b0262021-07-09 00:53:06 -07002491 }
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -06002492 }
2493
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002494 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
Nathaniel Cesario0b2a6422021-07-13 16:04:57 -06002495 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2496 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2497 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2498 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2499 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2500
2501 std::copy(start_in_p, end_in_p, out_p);
2502 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
Jeremy Gebben12933ef2021-05-12 17:16:27 -06002503 }
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002504 }
2505
sfricke-samsung5628f982021-10-19 09:21:59 -07002506 // both spirv-opt and spirv-val will use the same flags
2507 spvtools::ValidatorOptions options;
2508 AdjustValidatorOptions(device_extensions, enabled_features, options);
2509
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002510 // Apply the specialization-constant values and revalidate the shader module.
sfricke-samsung45996a42021-09-16 13:45:27 -07002511 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002512 spvtools::Optimizer optimizer(spirv_environment);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002513 spvtools::MessageConsumer consumer = [&skip, &module, &stage_state, this](spv_message_level_t level, const char *source,
2514 const spv_position_t &position,
2515 const char *message) {
2516 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2517 "%s does not contain valid spirv for stage %s. %s",
2518 report_data->FormatHandle(module->vk_shader_module()).c_str(),
2519 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002520 };
2521 optimizer.SetMessageConsumer(consumer);
2522 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2523 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
2524 std::vector<uint32_t> specialized_spirv;
sfricke-samsung5628f982021-10-19 09:21:59 -07002525 auto const optimized = optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002526 assert(optimized == true);
2527
2528 if (optimized) {
2529 spv_context ctx = spvContextCreate(spirv_environment);
2530 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2531 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002532 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2533 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002534 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002535 "After specialization was applied, %s does not contain valid spirv for stage %s.",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002536 report_data->FormatHandle(module->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002537 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002538 }
2539
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002540 spvDiagnosticDestroy(diag);
2541 spvContextDestroy(ctx);
2542 }
ziga-lunarg2818f492021-08-12 14:30:51 +02002543
2544 skip |= ValidateWorkgroupSize(module, pStage, id_value_map);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002545 }
2546
John Zulauf14c355b2019-06-27 16:09:37 -06002547 // Check the entrypoint
2548 if (entrypoint == module->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002549 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2550 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002551 }
2552 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2553
2554 // Mark accessible ids
2555 auto &accessible_ids = stage_state.accessible_ids;
2556
Chris Forbes47567b72017-06-09 12:09:45 -07002557 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002558
sfricke-samsung94167ca2021-02-26 04:14:59 -08002559 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2560 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002561 uint32_t total_shared_size = 0;
sfricke-samsung94167ca2021-02-26 04:14:59 -08002562 for (auto insn : *module) {
sfricke-samsung864162a2021-11-01 21:58:01 -07002563 skip |= ValidateTexelOffsetLimits(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002564 skip |= ValidateShaderCapabilitiesAndExtensions(module, insn);
sfricke-samsung306dc4c2021-09-20 15:25:18 -07002565 skip |= ValidateShaderClock(module, insn);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002566 skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn);
ziga-lunarg70651522021-10-11 17:23:30 +02002567 skip |= ValidateMemoryScope(module, insn);
ziga-lunarga26b3602021-08-08 15:53:00 +02002568 total_shared_size += module->CalcComputeSharedMemory(pStage->stage, insn);
2569 }
2570
2571 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2572 skip |= LogError(device, kVUID_Core_Shader_MaxComputeSharedMemorySize,
ziga-lunarg76a2e6c2021-08-08 15:55:03 +02002573 "Shader uses %" PRIu32 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
ziga-lunarga26b3602021-08-08 15:53:00 +02002574 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002575 }
2576
ziga-lunarg28d08792021-10-13 15:42:59 +02002577 skip |= ValidateTransformFeedback(module);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002578 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2579 stage_state.has_atomic_descriptor);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002580 skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03002581 skip |= ValidateShaderStorageImageFormats(module);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002582 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsung58b84352021-07-31 21:41:04 -07002583 skip |= ValidateAtomicsTypes(module);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002584 skip |= ValidateExecutionModes(module, entrypoint);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002585 skip |= ValidateSpecializations(pStage);
ziga-lunargce66e542021-09-19 00:11:14 +02002586 skip |= ValidateDecorations(module);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002587 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
Mark Lobodzinski518eadc2019-03-09 12:07:30 -07002588 skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002589 }
sfricke-samsungcfb44592021-07-25 00:36:28 -07002590 skip |= ValidateBuiltinLimits(module, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002591 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
2592 skip |= ValidateCooperativeMatrix(module, pStage, pipeline);
2593 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002594 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
2595 skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage);
2596 }
sfricke-samsung45996a42021-09-16 13:45:27 -07002597 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002598 skip |= ValidateShaderResolveQCOM(module, pStage, pipeline);
2599 }
ziga-lunarg73163742021-08-25 13:15:29 +02002600 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
2601 skip |= ValidateShaderSubgroupSizeControl(pStage);
2602 }
Chris Forbes47567b72017-06-09 12:09:45 -07002603
sfricke-samsung7699b912021-04-12 23:01:51 -07002604 // "layout must be consistent with the layout of the * shader"
2605 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002606 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002607 switch (pipeline->create_info.graphics.sType) {
2608 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2609 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2610 break;
2611 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2612 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2613 break;
2614 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2615 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2616 break;
2617 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2618 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2619 break;
2620 default:
2621 assert(false);
2622 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002623 }
2624
sfricke-samsung7699b912021-04-12 23:01:51 -07002625 // Validate Push Constants use
2626 skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch);
2627
Chris Forbes47567b72017-06-09 12:09:45 -07002628 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002629 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002630 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002631 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
Chris Forbes47567b72017-06-09 12:09:45 -07002632 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002633 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2634 std::set<uint32_t> descriptor_types =
2635 TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002636
2637 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002638 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002639 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002640 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002641 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002642 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002643 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2644 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002645 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2646 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002647 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002648 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2649 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002650 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002651 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002652 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002653 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002654 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002655 }
2656 }
2657
2658 // Validate use of input attachments against subpass structure
2659 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002660 auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002661
amhagana448ea52021-11-02 14:09:14 -04002662 if (!pipeline->rp_state->use_dynamic_rendering) {
2663 auto rpci = pipeline->rp_state->createInfo.ptr();
2664 auto subpass = pipeline->create_info.graphics.subpass;
2665 for (auto use : input_attachment_uses) {
2666 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2667 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
2668 ? input_attachments[use.first].attachment
2669 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002670
amhagana448ea52021-11-02 14:09:14 -04002671 if (index == VK_ATTACHMENT_UNUSED) {
2672 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2673 "Shader consumes input attachment index %d but not provided in subpass", use.first);
2674 }
2675 else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) {
2676 skip |=
2677 LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2678 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
2679 string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str());
2680 }
Chris Forbes47567b72017-06-09 12:09:45 -07002681 }
2682 }
2683 }
Lockeaa8fdc02019-04-02 11:59:20 -06002684 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02002685 skip |= ValidateComputeWorkGroupSizes(module, entrypoint, stage_state);
Lockeaa8fdc02019-04-02 11:59:20 -06002686 }
ziga-lunarg73163742021-08-25 13:15:29 +02002687
Chris Forbes47567b72017-06-09 12:09:45 -07002688 return skip;
2689}
2690
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002691bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2692 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2693 spirv_inst_iter consumer_entrypoint,
2694 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002695 bool skip = false;
2696
2697 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002698 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2699 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002700
2701 auto a_it = outputs.begin();
2702 auto b_it = inputs.begin();
2703
ziga-lunarg8346fe82021-08-22 17:30:50 +02002704 uint32_t a_component = 0;
2705 uint32_t b_component = 0;
2706
Chris Forbes47567b72017-06-09 12:09:45 -07002707 // Maps sorted by key (location); walk them together to find mismatches
2708 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2709 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2710 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2711 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2712 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2713
ziga-lunarg8346fe82021-08-22 17:30:50 +02002714 a_first.second += a_component;
2715 b_first.second += b_component;
2716
2717 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2718 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2719 assert(a_at_end || a_component < a_length);
2720 assert(b_at_end || b_component < b_length);
2721
Chris Forbes47567b72017-06-09 12:09:45 -07002722 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002723 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002724 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2725 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2726 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2727 a_it++;
2728 a_component = 0;
2729 } else {
2730 a_component++;
2731 }
Chris Forbes47567b72017-06-09 12:09:45 -07002732 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002733 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002734 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2735 b_first.first, b_first.second, producer_stage->name);
2736 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2737 b_it++;
2738 b_component = 0;
2739 } else {
2740 b_component++;
2741 }
Chris Forbes47567b72017-06-09 12:09:45 -07002742 } else {
2743 // subtleties of arrayed interfaces:
2744 // - if is_patch, then the member is not arrayed, even though the interface may be.
2745 // - if is_block_member, then the extra array level of an arrayed interface is not
2746 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002747 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002748 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002749 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002750 producer->DescribeType(a_it->second.type_id).c_str(),
2751 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002752 a_it++;
2753 b_it++;
2754 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002755 }
2756 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002757 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002758 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2759 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2760 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002761 }
2762 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002763 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002764 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2765 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002766 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002767 uint32_t a_remaining = a_length - a_component;
2768 uint32_t b_remaining = b_length - b_component;
2769 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2770 a_it++;
2771 b_it++;
2772 a_component = 0;
2773 b_component = 0;
2774 } else if (a_remaining > b_remaining) { // a has more components remaining
2775 a_component += b_remaining;
2776 b_component = 0;
2777 b_it++;
2778 } else if (b_remaining > a_remaining) { // b has more components remaining
2779 b_component += a_remaining;
2780 a_component = 0;
2781 a_it++;
2782 }
Chris Forbes47567b72017-06-09 12:09:45 -07002783 }
2784 }
2785
Ari Suonpaa696b3432019-03-11 14:02:57 +02002786 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002787 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2788 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002789
2790 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2791 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002792 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002793 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002794 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2795 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002796 } else {
2797 auto it_producer = builtins_producer.begin();
2798 auto it_consumer = builtins_consumer.begin();
2799 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2800 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002801 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002802 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2803 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002804 break;
2805 }
2806 it_producer++;
2807 it_consumer++;
2808 }
2809 }
2810 }
2811 }
2812
Chris Forbes47567b72017-06-09 12:09:45 -07002813 return skip;
2814}
2815
John Zulauf14c355b2019-06-27 16:09:37 -06002816static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002817 uint32_t stage_mask = 0;
2818 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2819 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
2820 stage_mask |= pCreateInfo->pStages[i].stage;
2821 }
2822 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05002823 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
2824 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
2825 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002826 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
2827 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
2828 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
2829 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
2830 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002831 }
2832 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002833 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06002834}
2835
Chris Forbes47567b72017-06-09 12:09:45 -07002836// Validate that the shaders used by the given pipeline and store the active_slots
2837// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06002838bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002839 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07002840
Chris Forbes47567b72017-06-09 12:09:45 -07002841 bool skip = false;
2842
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002843 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002844
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002845 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
2846 for (auto &stage : pipeline->stage_state) {
2847 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
2848 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
2849 vertex_stage = &stage;
2850 }
2851 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
2852 fragment_stage = &stage;
2853 }
Chris Forbes47567b72017-06-09 12:09:45 -07002854 }
2855
2856 // if the shader stages are no good individually, cross-stage validation is pointless.
2857 if (skip) return true;
2858
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002859 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07002860
2861 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002862 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07002863 }
2864
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002865 if (vertex_stage && vertex_stage->module->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
2866 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07002867 }
2868
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002869 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
2870 const auto &producer = pipeline->stage_state[i - 1];
2871 const auto &consumer = pipeline->stage_state[i];
2872 assert(producer.module);
2873 if (&producer == fragment_stage) {
2874 break;
2875 }
2876 if (consumer.module) {
2877 if (consumer.module->has_valid_spirv && producer.module->has_valid_spirv) {
2878 auto producer_id = GetShaderStageId(producer.stage_flag);
2879 auto consumer_id = GetShaderStageId(consumer.stage_flag);
2880 skip |=
2881 ValidateInterfaceBetweenStages(producer.module.get(), producer.entrypoint, &shader_stage_attribs[producer_id],
2882 consumer.module.get(), consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08002883 }
Chris Forbes47567b72017-06-09 12:09:45 -07002884 }
2885 }
2886
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002887 if (fragment_stage && fragment_stage->module->has_valid_spirv) {
Aaron Hagan1209c782021-11-22 19:37:14 -05002888 if (pipeline->rp_state->use_dynamic_rendering) {
2889 skip |= ValidateFsOutputsAgainstDynamicRenderingRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline);
2890 } else {
2891 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module.get(), fragment_stage->entrypoint, pipeline,
2892 create_info->subpass);
2893 }
Chris Forbes47567b72017-06-09 12:09:45 -07002894 }
2895
2896 return skip;
2897}
2898
Tony-LunarGb2ded512021-02-02 16:03:30 -07002899bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
2900 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002901 bool skip = false;
2902
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002903 for (auto &stage : pipeline->stage_state) {
2904 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
2905 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07002906 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
2907 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben3dfeacf2021-12-02 08:46:39 -07002908 if (stage.wrote_primitive_shading_rate) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002909 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002910 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002911 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
2912 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
2913 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002914 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002915 }
2916 }
2917 }
2918 }
2919
2920 return skip;
2921}
2922
sfricke-samsunge72a85e2020-02-29 21:48:37 -08002923bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002924 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07002925}
Chris Forbes4ae55b32017-06-09 14:42:56 -07002926
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002927uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
2928 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002929 const auto &create_info = pipeline->create_info.raytracing;
2930 const auto *stages = create_info.ptr()->pStages;
2931 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002932 if (stages[stage_index].stage == stageBit) {
2933 total++;
2934 }
2935 }
2936
Jeremy Gebben11af9792021-08-20 10:20:09 -06002937 if (create_info.pLibraryInfo) {
2938 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002939 const auto library_pipeline = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Jeremy Gebben9f537102021-10-05 16:37:12 -06002940 total += CalcShaderStageCount(library_pipeline.get(), stageBit);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02002941 }
2942 }
2943
2944 return total;
2945}
2946
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002947bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE *pipeline, uint32_t group, uint32_t stage) const {
2948 if (group == VK_SHADER_UNUSED_NV) {
2949 return true;
2950 }
2951
2952 const auto &create_info = pipeline->create_info.raytracing;
2953 const auto *stages = create_info.ptr()->pStages;
2954
2955 if (group < create_info.stageCount) {
2956 return (stages[group].stage & stage) != 0;
2957 }
2958 group -= create_info.stageCount;
2959
2960 // Search libraries
2961 if (create_info.pLibraryInfo) {
2962 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002963 auto library_pipeline = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Sebastian Neubauer7c826172021-10-04 12:05:51 +02002964 const uint32_t stage_count = library_pipeline->create_info.raytracing.ptr()->stageCount;
2965 if (group < stage_count) {
2966 return (library_pipeline->create_info.raytracing.ptr()->pStages[group].stage & stage) != 0;
2967 }
2968 group -= stage_count;
2969 }
2970 }
2971
2972 // group index too large
2973 return false;
2974}
2975
sourav parmarcd5fb182020-07-17 12:58:44 -07002976bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06002977 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04002978
Jeremy Gebben11af9792021-08-20 10:20:09 -06002979 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002980 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06002981 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
2982 skip |=
2983 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
2984 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
2985 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
2986 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05002987 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002988 if (create_info.pLibraryInfo) {
2989 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebbenb20a8242021-11-05 15:14:43 -06002990 const auto library_pipelinestate = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002991 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
2992 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07002993 skip |= LogError(
2994 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
2995 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
2996 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06002997 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07002998 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06002999 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
3000 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
3001 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
3002 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003003 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
3004 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
3005 "member must have been created with values of the maxPipelineRayPayloadSize and "
3006 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
3007 }
3008 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06003009 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003010 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
3011 "vkCreateRayTracingPipelinesKHR: If flags includes "
3012 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
3013 "the pLibraries member of libraries must have been created with the "
3014 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
3015 }
sourav parmar83c31b12020-05-06 12:30:54 -07003016 }
3017 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003018 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003019 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003020 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
3021 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
3022 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003023 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003024 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003025 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003026 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04003027
Jeremy Gebben11af9792021-08-20 10:20:09 -06003028 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003029 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003030 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003031
Jeremy Gebben11af9792021-08-20 10:20:09 -06003032 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003033 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
3034 if (raygen_stages_count == 0) {
3035 skip |= LogError(
3036 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07003037 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003038 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
3039 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003040 }
3041
Jeremy Gebben11af9792021-08-20 10:20:09 -06003042 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003043 const auto &group = groups[group_index];
3044
3045 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003046 if (!GroupHasValidIndex(
3047 pipeline, group.generalShader,
3048 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 -05003049 skip |= LogError(device,
3050 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
3051 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
3052 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003053 }
3054 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3055 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003056 skip |= LogError(device,
3057 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
3058 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
3059 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003060 }
3061 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003062 if (!GroupHasValidIndex(pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003063 skip |= LogError(device,
3064 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
3065 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
3066 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003067 }
3068 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3069 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003070 skip |= LogError(device,
3071 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
3072 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
3073 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003074 }
3075 }
3076
3077 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3078 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003079 if (!GroupHasValidIndex(pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003080 skip |= LogError(device,
3081 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
3082 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
3083 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003084 }
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003085 if (!GroupHasValidIndex(pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003086 skip |= LogError(device,
3087 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
3088 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
3089 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003090 }
3091 }
John Zulaufe4474e72019-07-01 17:28:27 -06003092 }
3093 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003094}
3095
Dave Houltona9df0ce2018-02-07 10:51:23 -07003096uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003097
Dave Houltona9df0ce2018-02-07 10:51:23 -07003098static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003099 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06003100 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003101 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003102 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003103 return nullptr;
3104}
3105
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003106bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003107 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003108 bool skip = false;
3109 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003110
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06003111 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003112 return false;
3113 }
3114
sfricke-samsung45996a42021-09-16 13:45:27 -07003115 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003116
3117 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003118 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
3119 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3120 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003121 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003122 auto cache = GetValidationCacheInfo(pCreateInfo);
3123 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07003124 // If app isn't using a shader validation cache, use the default one from CoreChecks
3125 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003126 if (cache) {
3127 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003128 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003129 }
3130
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003131 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3132 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07003133 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06003134 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003135 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003136 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003137 spvtools::ValidatorOptions options;
3138 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06003139 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003140 if (spv_valid != SPV_SUCCESS) {
3141 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003142 if (spv_valid == SPV_WARNING) {
3143 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3144 diag && diag->error ? diag->error : "(no error text)");
3145 } else {
3146 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3147 diag && diag->error ? diag->error : "(no error text)");
3148 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003149 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003150 } else {
3151 if (cache) {
3152 cache->Insert(hash);
3153 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003154 }
3155
3156 spvDiagnosticDestroy(diag);
3157 spvContextDestroy(ctx);
3158 }
3159
Chris Forbes4ae55b32017-06-09 14:42:56 -07003160 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003161}
3162
ziga-lunarg11fecb92021-09-20 16:48:06 +02003163bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint,
3164 const PipelineStageState &stage_state) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003165 bool skip = false;
3166 uint32_t local_size_x = 0;
3167 uint32_t local_size_y = 0;
3168 uint32_t local_size_z = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07003169 if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
Lockeaa8fdc02019-04-02 11:59:20 -06003170 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003171 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003172 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003173 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003174 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
Lockeaa8fdc02019-04-02 11:59:20 -06003175 }
3176 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003177 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003178 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003179 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003180 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
Lockeaa8fdc02019-04-02 11:59:20 -06003181 }
3182 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003183 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003184 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003185 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003186 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
Lockeaa8fdc02019-04-02 11:59:20 -06003187 }
3188
3189 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3190 uint64_t invocations = local_size_x * local_size_y;
3191 // Prevent overflow.
3192 bool fail = false;
3193 if (invocations > UINT32_MAX || invocations > limit) {
3194 fail = true;
3195 }
3196 if (!fail) {
3197 invocations *= local_size_z;
3198 if (invocations > UINT32_MAX || invocations > limit) {
3199 fail = true;
3200 }
3201 }
3202 if (fail) {
Mike Schuchardt37b8cc12021-10-05 14:31:11 -07003203 skip |= LogError(shader->vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003204 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3205 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sfricke-samsung1ff329f2021-09-16 10:06:47 -07003206 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, local_size_y,
3207 local_size_z, limit);
Lockeaa8fdc02019-04-02 11:59:20 -06003208 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003209
3210 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3211 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
3212 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3213 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3214 skip |= LogError(
3215 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
3216 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3217 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3218 "dimension (%" PRIu32
3219 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
3220 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
3221 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3222 }
3223 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3224 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
3225 const auto *required_subgroup_size_features =
3226 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
3227 if (!required_subgroup_size_features) {
3228 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
3229 skip |= LogError(
3230 shader->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
3231 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3232 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3233 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3234 ").",
3235 report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x,
3236 phys_dev_props_core11.subgroupSize);
3237 }
3238 }
3239 }
Lockeaa8fdc02019-04-02 11:59:20 -06003240 }
3241 return skip;
3242}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003243
3244spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
3245 if (api_version >= VK_API_VERSION_1_2) {
3246 return SPV_ENV_VULKAN_1_2;
3247 } else if (api_version >= VK_API_VERSION_1_1) {
3248 if (spirv_1_4) {
3249 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3250 } else {
3251 return SPV_ENV_VULKAN_1_1;
3252 }
3253 }
3254 return SPV_ENV_VULKAN_1_0;
3255}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003256
sfricke-samsungecc112a2021-09-03 05:32:17 -07003257// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003258void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003259 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003260 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3261 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003262 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003263 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003264 options.SetRelaxBlockLayout(true);
3265 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003266
3267 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3268 // Vulkan version used, the feature bit is needed (also described in the spec).
3269
3270 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3271 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003272 options.SetUniformBufferStandardLayout(true);
3273 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003274 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3275 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003276 options.SetScalarBlockLayout(true);
3277 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003278 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3279 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003280 options.SetWorkgroupScalarBlockLayout(true);
3281 }
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003282 if (enabled_features.maintenance4_features.maintenance4) {
3283 // --allow-localsizeid
3284 options.SetAllowLocalSizeId(true);
3285 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003286}