blob: a7ffe103cd1338900bb7d7d0bd810cba54772930 [file] [log] [blame]
Lionel Landwerlin2d9f5632022-01-08 01:12:47 +02001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
4 * Copyright (C) 2015-2022 Google Inc.
Tobias Hector6663c9b2020-11-05 10:18:02 +00005 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
Chris Forbes47567b72017-06-09 12:09:45 -07006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Chris Forbes <chrisf@ijw.co.nz>
Dave Houlton51653902018-06-22 17:32:13 -060020 * Author: Dave Houlton <daveh@lunarg.com>
Tobias Hector6663c9b2020-11-05 10:18:02 +000021 * Author: Tobias Hector <tobias.hector@amd.com>
Chris Forbes47567b72017-06-09 12:09:45 -070022 */
23
Petr Kraus25810d02019-08-27 17:41:15 +020024#include "shader_validation.h"
25
Chris Forbes47567b72017-06-09 12:09:45 -070026#include <cassert>
Petr Kraus25810d02019-08-27 17:41:15 +020027#include <cinttypes>
Jeff Bolzf234bf82019-11-04 14:07:15 -060028#include <cmath>
Chris Forbes47567b72017-06-09 12:09:45 -070029#include <sstream>
Petr Kraus25810d02019-08-27 17:41:15 +020030#include <string>
Petr Kraus25810d02019-08-27 17:41:15 +020031#include <vector>
32
Mark Lobodzinski102687e2020-04-28 11:03:28 -060033#include <spirv/unified1/spirv.hpp>
Chris Forbes47567b72017-06-09 12:09:45 -070034#include "vk_enum_string_helper.h"
Chris Forbes47567b72017-06-09 12:09:45 -070035#include "vk_layer_data.h"
Chris Forbes47567b72017-06-09 12:09:45 -070036#include "vk_layer_utils.h"
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -070037#include "chassis.h"
Chris Forbes47567b72017-06-09 12:09:45 -070038#include "core_validation.h"
sfricke-samsung3c5dee22021-10-14 09:58:14 -070039#include "spirv_grammar_helper.h"
Petr Kraus25810d02019-08-27 17:41:15 +020040
Chris Forbes9a61e082017-07-24 15:35:29 -070041#include "xxhash.h"
Chris Forbes47567b72017-06-09 12:09:45 -070042
Chris Forbes47567b72017-06-09 12:09:45 -070043static shader_stage_attributes shader_stage_attribs[] = {
Ari Suonpaa696b3432019-03-11 14:02:57 +020044 {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT},
45 {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT},
46 {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT},
47 {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT},
48 {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT},
Chris Forbes47567b72017-06-09 12:09:45 -070049};
50
sfricke-samsungef15e482022-01-26 11:32:49 -080051static const spirv_inst_iter GetBaseTypeIter(SHADER_MODULE_STATE const *module_state, uint32_t type) {
52 const auto &insn = module_state->get_def(type);
53 const uint32_t base_insn_id = module_state->GetBaseType(insn);
54 return module_state->get_def(base_insn_id);
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020055}
56
ziga-lunarg8346fe82021-08-22 17:30:50 +020057static bool BaseTypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, const spirv_inst_iter &a_base_insn,
58 const spirv_inst_iter &b_base_insn) {
59 const uint32_t a_opcode = a_base_insn.opcode();
60 const uint32_t b_opcode = b_base_insn.opcode();
61 if (a_opcode == b_opcode) {
62 if (a_opcode == spv::OpTypeInt) {
63 // Match width and signedness
64 return a_base_insn.word(2) == b_base_insn.word(2) && a_base_insn.word(3) == b_base_insn.word(3);
65 } else if (a_opcode == spv::OpTypeFloat) {
66 // Match width
67 return a_base_insn.word(2) == b_base_insn.word(2);
68 } else if (a_opcode == spv::OpTypeStruct) {
69 // Match on all element types
70 if (a_base_insn.len() != b_base_insn.len()) {
71 return false; // Structs cannot match if member counts differ
72 }
73
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020074 for (uint32_t i = 2; i < a_base_insn.len(); i++) {
75 const auto &c_base_insn = GetBaseTypeIter(a, a_base_insn.word(i));
76 const auto &d_base_insn = GetBaseTypeIter(b, b_base_insn.word(i));
77 if (!BaseTypesMatch(a, b, c_base_insn, d_base_insn)) {
ziga-lunarg8346fe82021-08-22 17:30:50 +020078 return false;
79 }
80 }
81
82 return true;
83 }
84 }
85 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070086}
87
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020088static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, uint32_t a_type, uint32_t b_type) {
89 const auto &a_base_insn = GetBaseTypeIter(a, a_type);
90 const auto &b_base_insn = GetBaseTypeIter(b, b_type);
Chris Forbes47567b72017-06-09 12:09:45 -070091
ziga-lunarg8346fe82021-08-22 17:30:50 +020092 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070093}
94
sfricke-samsung7fac88a2022-01-26 11:44:22 -080095static uint32_t GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -070096 switch (format) {
97 case VK_FORMAT_R64G64B64A64_SFLOAT:
98 case VK_FORMAT_R64G64B64A64_SINT:
99 case VK_FORMAT_R64G64B64A64_UINT:
100 case VK_FORMAT_R64G64B64_SFLOAT:
101 case VK_FORMAT_R64G64B64_SINT:
102 case VK_FORMAT_R64G64B64_UINT:
103 return 2;
104 default:
105 return 1;
106 }
107}
108
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800109static uint32_t GetFormatType(VkFormat fmt) {
sfricke-samsunge3086292021-11-18 23:02:35 -0800110 if (FormatIsSINT(fmt)) return FORMAT_TYPE_SINT;
111 if (FormatIsUINT(fmt)) return FORMAT_TYPE_UINT;
sfricke-samsunged028b02021-09-06 23:14:51 -0700112 // Formats such as VK_FORMAT_D16_UNORM_S8_UINT are both
Dave Houltona9df0ce2018-02-07 10:51:23 -0700113 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
114 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700115 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
116 return FORMAT_TYPE_FLOAT;
117}
118
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600119static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700120 uint32_t bit_pos = uint32_t(u_ffs(stage));
121 return bit_pos - 1;
122}
123
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700124bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700125 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
126 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700127 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700128 bool skip = false;
129
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800130 for (uint32_t i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes47567b72017-06-09 12:09:45 -0700131 auto desc = &vi->pVertexBindingDescriptions[i];
132 auto &binding = bindings[desc->binding];
133 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600134 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700135 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
136 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700137 } else {
138 binding = desc;
139 }
140 }
141
142 return skip;
143}
144
sfricke-samsungef15e482022-01-26 11:32:49 -0800145bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *module_state,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700146 spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700147 bool skip = false;
148
sfricke-samsungef15e482022-01-26 11:32:49 -0800149 const auto inputs = module_state->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700150
151 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200152 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700153 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200154 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
155 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
156 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700157 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
158 }
159 }
160 }
161
Petr Kraus25810d02019-08-27 17:41:15 +0200162 struct AttribInputPair {
163 const VkVertexInputAttributeDescription *attrib = nullptr;
164 const interface_var *input = nullptr;
165 };
166 std::map<uint32_t, AttribInputPair> location_map;
167 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
168 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700169
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400170 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200171 const auto location = location_it.first;
172 const auto attrib = location_it.second.attrib;
173 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600174
Petr Kraus25810d02019-08-27 17:41:15 +0200175 if (attrib && !input) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800176 skip |= LogPerformanceWarning(module_state->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700177 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200178 } else if (!attrib && input) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800179 skip |= LogError(module_state->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700180 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200181 } else if (attrib && input) {
182 const auto attrib_type = GetFormatType(attrib->format);
sfricke-samsungef15e482022-01-26 11:32:49 -0800183 const auto input_type = module_state->GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700184
185 // Type checking
186 if (!(attrib_type & input_type)) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800187 skip |= LogError(module_state->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700188 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sfricke-samsungef15e482022-01-26 11:32:49 -0800189 string_VkFormat(attrib->format), location, module_state->DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700190 }
Petr Kraus25810d02019-08-27 17:41:15 +0200191 } else { // !attrib && !input
192 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700193 }
194 }
195
196 return skip;
197}
198
sfricke-samsungef15e482022-01-26 11:32:49 -0800199bool CoreChecks::ValidateFsOutputsAgainstDynamicRenderingRenderPass(SHADER_MODULE_STATE const *module_state,
200 spirv_inst_iter entrypoint,
201 PIPELINE_STATE const *pipeline) const {
Aaron Hagan1209c782021-11-22 19:37:14 -0500202 bool skip = false;
203
204 struct Attachment {
205 const interface_var* output = nullptr;
206 };
207 std::map<uint32_t, Attachment> location_map;
208
209 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
sfricke-samsungef15e482022-01-26 11:32:49 -0800210 const auto outputs = module_state->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Aaron Hagan1209c782021-11-22 19:37:14 -0500211 for (const auto& output_it : outputs) {
212 auto const location = output_it.first.first;
213 location_map[location].output = &output_it.second;
214 }
215
216 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
217 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
218
Aaron Haganaca50442021-12-07 22:26:29 -0500219 for (uint32_t location = 0; location < location_map.size(); ++location) {
Aaron Hagan1209c782021-11-22 19:37:14 -0500220 const auto output = location_map[location].output;
221
222 if (!output && pipeline->attachments[location].colorWriteMask != 0) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800223 skip |= LogWarning(
224 module_state->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
225 "Attachment %" PRIu32 " not written by fragment shader; undefined values will be written to attachment", location);
Aaron Haganaca50442021-12-07 22:26:29 -0500226 } else if (output &&
227 (location < pipeline->rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount)) {
Aaron Hagan1209c782021-11-22 19:37:14 -0500228 auto format = pipeline->rp_state->dynamic_rendering_pipeline_create_info.pColorAttachmentFormats[location];
229 const auto attachment_type = GetFormatType(format);
sfricke-samsungef15e482022-01-26 11:32:49 -0800230 const auto output_type = module_state->GetFundamentalType(output->type_id);
Aaron Hagan1209c782021-11-22 19:37:14 -0500231
232 // Type checking
233 if (!(output_type & attachment_type)) {
234 skip |=
sfricke-samsungef15e482022-01-26 11:32:49 -0800235 LogWarning(module_state->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
236 "Attachment %" PRIu32
237 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
238 location, string_VkFormat(format), module_state->DescribeType(output->type_id).c_str());
Aaron Hagan1209c782021-11-22 19:37:14 -0500239 }
240 }
241 }
242
243 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sfricke-samsungef15e482022-01-26 11:32:49 -0800244 bool location_zero_has_alpha = output_zero && module_state->get_def(output_zero->type_id) != module_state->end() &&
245 module_state->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Aaron Hagan1209c782021-11-22 19:37:14 -0500246 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800247 skip |= LogError(module_state->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
248 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Aaron Hagan1209c782021-11-22 19:37:14 -0500249 }
250
251 return skip;
Aaron Hagan1209c782021-11-22 19:37:14 -0500252}
253
sfricke-samsungef15e482022-01-26 11:32:49 -0800254bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *module_state, spirv_inst_iter entrypoint,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700255 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200256 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700257
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600258 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800259 const VkAttachmentReference2 *reference = nullptr;
260 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600261 const interface_var *output = nullptr;
262 };
263 std::map<uint32_t, Attachment> location_map;
264
amhagana448ea52021-11-02 14:09:14 -0400265 if (pipeline->rp_state && !pipeline->rp_state->use_dynamic_rendering) {
266 const auto rpci = pipeline->rp_state->createInfo.ptr();
267 const auto subpass = rpci->pSubpasses[subpass_index];
268 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
269 auto const &reference = subpass.pColorAttachments[i];
270 location_map[i].reference = &reference;
271 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
272 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
273 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
274 }
Chris Forbes47567b72017-06-09 12:09:45 -0700275 }
276 }
277
Chris Forbes47567b72017-06-09 12:09:45 -0700278 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
279
sfricke-samsungef15e482022-01-26 11:32:49 -0800280 const auto outputs = module_state->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600281 for (const auto &output_it : outputs) {
282 auto const location = output_it.first.first;
283 location_map[location].output = &output_it.second;
284 }
Chris Forbes47567b72017-06-09 12:09:45 -0700285
Jeremy Gebben11af9792021-08-20 10:20:09 -0600286 const bool alpha_to_coverage_enabled = pipeline->create_info.graphics.pMultisampleState != NULL &&
287 pipeline->create_info.graphics.pMultisampleState->alphaToCoverageEnable == VK_TRUE;
Chris Forbes47567b72017-06-09 12:09:45 -0700288
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700289 // Don't check any color attachments if rasterization is disabled
290 if (!pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
291 for (const auto &location_it : location_map) {
292 const auto reference = location_it.second.reference;
293 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
294 continue;
Petr Kraus25810d02019-08-27 17:41:15 +0200295 }
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700296
297 const auto location = location_it.first;
298 const auto attachment = location_it.second.attachment;
299 const auto output = location_it.second.output;
300 if (attachment && !output) {
301 if (pipeline->attachments[location].colorWriteMask != 0) {
302 skip |= LogWarning(module_state->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
303 "Attachment %" PRIu32
304 " not written by fragment shader; undefined values will be written to attachment",
305 location);
306 }
307 } else if (!attachment && output) {
308 if (!(alpha_to_coverage_enabled && location == 0)) {
309 skip |=
310 LogWarning(module_state->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700311 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700312 }
313 } else if (attachment && output) {
314 const auto attachment_type = GetFormatType(attachment->format);
315 const auto output_type = module_state->GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700316
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700317 // Type checking
318 if (!(output_type & attachment_type)) {
319 skip |= LogWarning(
320 module_state->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
321 "Attachment %" PRIu32
322 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
323 location, string_VkFormat(attachment->format), module_state->DescribeType(output->type_id).c_str());
324 }
325 } else { // !attachment && !output
326 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700327 }
Chris Forbes47567b72017-06-09 12:09:45 -0700328 }
329 }
330
Petr Kraus25810d02019-08-27 17:41:15 +0200331 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sfricke-samsungef15e482022-01-26 11:32:49 -0800332 bool location_zero_has_alpha = output_zero && module_state->get_def(output_zero->type_id) != module_state->end() &&
333 module_state->GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700334 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800335 skip |= LogError(module_state->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700336 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200337 }
338
Chris Forbes47567b72017-06-09 12:09:45 -0700339 return skip;
340}
341
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600342PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
343 const shader_struct_member &push_constant_used_in_shader,
344 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600345 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600346 const auto used_bytes_size = used_bytes->size();
347 if (used_bytes_size == 0) return PC_Byte_Updated;
348
349 const auto push_constant_data_update_size = push_constant_data_update.size();
350 const auto *data = push_constant_data_update.data();
351 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
352 if (used_bytes_size <= push_constant_data_update_size) {
353 return PC_Byte_Updated;
354 }
355 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
356
357 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
358 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
359 return PC_Byte_Updated;
360 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600361 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600362
locke-lunargde3f0fa2020-09-10 11:55:31 -0600363 uint32_t i = 0;
364 for (const auto used : *used_bytes) {
365 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600366 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600367 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600368 return PC_Byte_Not_Set;
369 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600370 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600371 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600372 }
373 }
374 ++i;
375 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600376 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600377}
378
sfricke-samsungef15e482022-01-26 11:32:49 -0800379bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *module_state,
sfricke-samsung7699b912021-04-12 23:01:51 -0700380 VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700381 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700382 // Temp workaround to prevent false positive errors
383 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sfricke-samsungef15e482022-01-26 11:32:49 -0800384 if (module_state->HasMultipleEntryPoints()) {
sfricke-samsung5c65b372021-03-25 05:39:57 -0700385 return skip;
386 }
387
Chris Forbes47567b72017-06-09 12:09:45 -0700388 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
sfricke-samsungef15e482022-01-26 11:32:49 -0800389 const auto *entrypoint = module_state->FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600390 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
391 return skip;
392 }
393 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700394
locke-lunargde3f0fa2020-09-10 11:55:31 -0600395 bool found_stage = false;
396 for (auto const &range : *push_constant_ranges) {
397 if (range.stageFlags & pStage->stage) {
398 found_stage = true;
399 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600400 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600401 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600402 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600403 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600404 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600405 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600406 const auto ret =
407 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700408
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600409 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600410 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
sfricke-samsungef15e482022-01-26 11:32:49 -0800411 LogObjectList objlist(module_state->vk_shader_module());
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600412 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700413 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 -0600414 string_VkShaderStageFlags(pStage->stage).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600415 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600416 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700417 }
418 }
419 }
420
locke-lunargde3f0fa2020-09-10 11:55:31 -0600421 if (!found_stage) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800422 LogObjectList objlist(module_state->vk_shader_module());
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600423 objlist.add(pipeline.pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700424 skip |= LogError(objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.",
sfricke-samsungef15e482022-01-26 11:32:49 -0800425 string_VkShaderStageFlags(pStage->stage).c_str(),
426 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600427 report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(),
sfricke-samsung7699b912021-04-12 23:01:51 -0700428 string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700429 }
Chris Forbes47567b72017-06-09 12:09:45 -0700430 return skip;
431}
432
sfricke-samsungef15e482022-01-26 11:32:49 -0800433bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *module_state, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700434 bool skip = false;
435
436 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700437 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700438 return skip;
439 }
440
sfricke-samsungcfb44592021-07-25 00:36:28 -0700441 // Find all builtin from just the interface variables
442 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800443 auto insn = module_state->get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700444 assert(insn.opcode() == spv::OpVariable);
sfricke-samsungef15e482022-01-26 11:32:49 -0800445 const decoration_set decorations = module_state->get_decorations(insn.word(2));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700446
sfricke-samsungcfb44592021-07-25 00:36:28 -0700447 // Currently don't need to search in structs
448 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800449 auto type_pointer = module_state->get_def(insn.word(1));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700450 assert(type_pointer.opcode() == spv::OpTypePointer);
451
sfricke-samsungef15e482022-01-26 11:32:49 -0800452 auto type = module_state->get_def(type_pointer.word(3));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700453 if (type.opcode() == spv::OpTypeArray) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800454 uint32_t length = static_cast<uint32_t>(module_state->GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700455 // Handles both the input and output sampleMask
456 if (length > phys_dev_props.limits.maxSampleMaskWords) {
457 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
458 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
459 "maxSampleMaskWords of %u in %s.",
460 length, phys_dev_props.limits.maxSampleMaskWords,
sfricke-samsungef15e482022-01-26 11:32:49 -0800461 report_data->FormatHandle(module_state->vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700462 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700463 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700464 }
465 }
466 }
467
468 return skip;
469}
470
Chris Forbes47567b72017-06-09 12:09:45 -0700471// Validate that data for each specialization entry is fully contained within the buffer.
ziga-lunargae2a5c42021-07-23 16:18:09 +0200472bool CoreChecks::ValidateSpecializations(VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700473 bool skip = false;
474
475 VkSpecializationInfo const *spec = info->pSpecializationInfo;
476
477 if (spec) {
478 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600479 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700480 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
481 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200482 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700483 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
484 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600485
486 continue;
487 }
Chris Forbes47567b72017-06-09 12:09:45 -0700488 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700489 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
490 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200491 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700492 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
493 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700494 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200495 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
496 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
497 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
498 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
499 j, spec->pMapEntries[i].constantID);
500 }
501 }
Chris Forbes47567b72017-06-09 12:09:45 -0700502 }
503 }
504
505 return skip;
506}
507
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500508// TODO (jbolz): Can this return a const reference?
sfricke-samsungef15e482022-01-26 11:32:49 -0800509static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module_state, uint32_t type_id,
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800510 uint32_t &descriptor_count, bool is_khr) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800511 auto type = module_state->get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800512 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700513 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500514 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700515
516 // 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 -0500517 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
518 if (type.opcode() == spv::OpTypeRuntimeArray) {
519 descriptor_count = 0;
sfricke-samsungef15e482022-01-26 11:32:49 -0800520 type = module_state->get_def(type.word(2));
Jeff Bolzfdf96072018-04-10 14:32:18 -0500521 } else if (type.opcode() == spv::OpTypeArray) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800522 descriptor_count *= module_state->GetConstantValueById(type.word(3));
523 type = module_state->get_def(type.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700524 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800525 if (type.word(2) == spv::StorageClassStorageBuffer) {
526 is_storage_buffer = true;
527 }
sfricke-samsungef15e482022-01-26 11:32:49 -0800528 type = module_state->get_def(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700529 }
530 }
531
532 switch (type.opcode()) {
533 case spv::OpTypeStruct: {
sfricke-samsungef15e482022-01-26 11:32:49 -0800534 for (const auto insn : module_state->GetDecorationInstructions()) {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800535 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700536 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800537 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500538 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
539 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
540 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800541 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500542 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
543 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
544 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
545 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800546 }
Chris Forbes47567b72017-06-09 12:09:45 -0700547 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500548 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
549 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
550 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700551 }
552 }
553 }
554
555 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500556 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700557 }
558
559 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500560 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
561 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
562 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700563
Chris Forbes73c00bf2018-06-22 16:28:06 -0700564 case spv::OpTypeSampledImage: {
565 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
566 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
sfricke-samsungef15e482022-01-26 11:32:49 -0800567 auto image_type = module_state->get_def(type.word(2));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700568 auto dim = image_type.word(3);
569 auto sampled = image_type.word(7);
570 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500571 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
572 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700573 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700574 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500575 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
576 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700577
578 case spv::OpTypeImage: {
579 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
580 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
581 auto dim = type.word(3);
582 auto sampled = type.word(7);
583
584 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500585 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
586 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700587 } else if (dim == spv::DimBuffer) {
588 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500589 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
590 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700591 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500592 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
593 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700594 }
595 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500596 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
597 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
598 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700599 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500600 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
601 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700602 }
603 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600604 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700605 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
606 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500607 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700608
609 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
610 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500611 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700612 }
613}
614
Jeff Bolze54ae892018-09-08 12:16:29 -0500615static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700616 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500617 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
618 if (ss.tellp()) ss << ", ";
619 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700620 }
621 return ss.str();
622}
623
sfricke-samsung0065ce02020-12-03 22:46:37 -0800624bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500625 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800626 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 -0500627 return true;
628 }
629 }
630
631 return false;
632}
633
sfricke-samsung0065ce02020-12-03 22:46:37 -0800634bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700635 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800636 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700637 return true;
638 }
639 }
640
641 return false;
642}
643
locke-lunarg63e4daf2020-08-17 17:53:25 -0600644bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
645 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500646 bool skip = false;
647
locke-lunarg63e4daf2020-08-17 17:53:25 -0600648 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800649 switch (stage) {
Chris Forbes349b3132018-03-07 11:38:08 -0800650 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800651 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700652 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800653 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800654 case VK_SHADER_STAGE_VERTEX_BIT:
655 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
656 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
657 case VK_SHADER_STAGE_GEOMETRY_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800658 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700659 "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800660 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800661 default:
662 // No feature requirements for writes and atomics for other stages
663 break;
Chris Forbes349b3132018-03-07 11:38:08 -0800664 }
665 }
666
Chris Forbes47567b72017-06-09 12:09:45 -0700667 return skip;
668}
669
sfricke-samsungef15e482022-01-26 11:32:49 -0800670bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module_state, VkShaderStageFlagBits stage,
sfricke-samsung94167ca2021-02-26 04:14:59 -0800671 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500672 bool skip = false;
673
sfricke-samsung94167ca2021-02-26 04:14:59 -0800674 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
675 if (GroupOperation(insn.opcode()) == true) {
676 // Check the quad operations.
677 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
678 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700679 skip |=
680 RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
681 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800682 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800683 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500684
sfricke-samsung94167ca2021-02-26 04:14:59 -0800685 uint32_t scope_type = spv::ScopeMax;
686 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
687 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
688 scope_type = spv::ScopeSubgroup;
689 } else {
690 // "All <id> used for Scope <id> must be of an OpConstant"
sfricke-samsungef15e482022-01-26 11:32:49 -0800691 auto scope_id = module_state->get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800692 scope_type = scope_id.word(3);
693 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800694
sfricke-samsung94167ca2021-02-26 04:14:59 -0800695 if (scope_type == spv::ScopeSubgroup) {
696 // "Group operations with subgroup scope" must have stage support
697 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
698 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700699 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800700 }
701
702 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800703 auto type = module_state->get_def(insn.word(1));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800704
705 if (type.opcode() == spv::OpTypeVector) {
706 // Get the element type
sfricke-samsungef15e482022-01-26 11:32:49 -0800707 type = module_state->get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800708 }
709
sfricke-samsung94167ca2021-02-26 04:14:59 -0800710 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800711 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
712 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500713
sfricke-samsung0065ce02020-12-03 22:46:37 -0800714 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
715 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
716 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
717 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700718 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500719 }
720 }
721 }
Jeff Bolzee743412019-06-20 22:24:32 -0500722 }
723
724 return skip;
725}
726
sfricke-samsungef15e482022-01-26 11:32:49 -0800727bool CoreChecks::ValidateMemoryScope(SHADER_MODULE_STATE const *module_state, const spirv_inst_iter &insn) const {
ziga-lunarg70651522021-10-11 17:23:30 +0200728 bool skip = false;
729
sfricke-samsung3a25ed52022-01-20 02:24:36 -0800730 const auto &entry = OpcodeMemoryScopePosition(insn.opcode());
ziga-lunarg70651522021-10-11 17:23:30 +0200731 if (entry > 0) {
732 const uint32_t scope_id = insn.word(entry);
sfricke-samsunged00aa42022-01-27 19:03:01 -0800733 const auto &scope_def = module_state->GetConstantDef(scope_id);
734 if (scope_def != module_state->end()) {
735 const auto scope_type = GetConstantValue(scope_def);
736 if (enabled_features.core12.vulkanMemoryModel && !enabled_features.core12.vulkanMemoryModelDeviceScope &&
737 scope_type == spv::Scope::ScopeDevice) {
738 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06265",
739 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is enabled and "
740 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModelDeviceScope is disabled, but\n%s\nuses "
741 "Device memory scope.",
742 module_state->DescribeInstruction(insn).c_str());
743 } else if (!enabled_features.core12.vulkanMemoryModel && scope_type == spv::Scope::ScopeQueueFamily) {
744 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06266",
745 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is not enabled, but\n%s\nuses "
746 "QueueFamily memory scope.",
747 module_state->DescribeInstruction(insn).c_str());
ziga-lunarg70651522021-10-11 17:23:30 +0200748 }
749 }
750 }
751
752 return skip;
753}
754
sfricke-samsungef15e482022-01-26 11:32:49 -0800755bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *module_state,
756 VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline,
757 spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200758 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
759 pStage->stage == VK_SHADER_STAGE_ALL) {
760 return false;
761 }
762
763 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700764 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200765
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700766 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200767 struct Variable {
768 uint32_t baseTypePtrID;
769 uint32_t ID;
770 uint32_t storageClass;
771 };
772 std::vector<Variable> variables;
773
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700774 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700775 bool is_iso_lines = false;
776 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500777
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700778 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600779
sfricke-samsungef15e482022-01-26 11:32:49 -0800780 for (auto insn : *module_state) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200781 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500782 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200783 case spv::OpDecorate:
784 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500785 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700786 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200787 break;
788 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200789 default:
790 break;
791 }
792 break;
793 // Find all input and output variables
794 case spv::OpVariable: {
795 Variable var = {};
796 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600797 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
798 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700799 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200800 var.baseTypePtrID = insn.word(1);
801 var.ID = insn.word(2);
802 variables.push_back(var);
803 }
804 break;
805 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500806 case spv::OpExecutionMode:
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800807 case spv::OpExecutionModeId:
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500808 if (insn.word(1) == entrypoint.word(2)) {
809 switch (insn.word(2)) {
810 default:
811 break;
812 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700813 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500814 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700815 case spv::ExecutionModeIsolines:
816 is_iso_lines = true;
817 break;
818 case spv::ExecutionModePointMode:
819 is_point_mode = true;
820 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500821 }
822 }
823 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200824 default:
825 break;
826 }
827 }
828
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500829 bool strip_output_array_level =
830 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
831 bool strip_input_array_level =
832 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
833 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
834
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700835 uint32_t num_comp_in = 0, num_comp_out = 0;
836 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600837
sfricke-samsungef15e482022-01-26 11:32:49 -0800838 auto inputs = module_state->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
839 auto outputs = module_state->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600840
841 // Find max component location used for input variables.
842 for (auto &var : inputs) {
843 int location = var.first.first;
844 int component = var.first.second;
845 interface_var &iv = var.second;
846
847 // Only need to look at the first location, since we use the type's whole size
848 if (iv.offset != 0) {
849 continue;
850 }
851
852 if (iv.is_patch) {
853 continue;
854 }
855
sfricke-samsungef15e482022-01-26 11:32:49 -0800856 int num_components = module_state->GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700857 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600858 }
859
860 // Find max component location used for output variables.
861 for (auto &var : outputs) {
862 int location = var.first.first;
863 int component = var.first.second;
864 interface_var &iv = var.second;
865
866 // Only need to look at the first location, since we use the type's whole size
867 if (iv.offset != 0) {
868 continue;
869 }
870
871 if (iv.is_patch) {
872 continue;
873 }
874
sfricke-samsungef15e482022-01-26 11:32:49 -0800875 int num_components = module_state->GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700876 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600877 }
878
879 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
880 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700881 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
882 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200883 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500884 // Check if the variable is a patch. Patches can also be members of blocks,
885 // but if they are then the top-level arrayness has already been stripped
886 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700887 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200888
889 if (var.storageClass == spv::StorageClassInput) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800890 num_comp_in += module_state->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200891 } else { // var.storageClass == spv::StorageClassOutput
sfricke-samsungef15e482022-01-26 11:32:49 -0800892 num_comp_out += module_state->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200893 }
894 }
895
896 switch (pStage->stage) {
897 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700898 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700899 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700900 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
901 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
902 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700903 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200904 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700905 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700906 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700907 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
908 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
909 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600910 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200911 break;
912
913 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700914 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700915 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700916 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
917 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
918 "components by %u components",
919 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700920 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200921 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700922 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600923 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700924 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700925 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
926 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
927 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600928 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700929 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700930 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700931 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
932 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
933 "components by %u components",
934 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700935 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200936 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700937 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600938 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700939 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700940 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
941 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
942 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600943 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200944 break;
945
946 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700947 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700948 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700949 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
950 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
951 "components by %u components",
952 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700953 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200954 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700955 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600956 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700957 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700958 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
959 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
960 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600961 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700962 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700963 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700964 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
965 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
966 "components by %u components",
967 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700968 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200969 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700970 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600971 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700972 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700973 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
974 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
975 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600976 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700977 // Portability validation
978 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
979 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700980 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700981 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
982 " is using abstract patch type IsoLines, but this is not supported on this platform");
983 }
984 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700985 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700986 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
987 " is using abstract patch type PointMode, but this is not supported on this platform");
988 }
989 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200990 break;
991
992 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700993 if (num_comp_in > limits.maxGeometryInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700994 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700995 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
996 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
997 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700998 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200999 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001000 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001001 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001002 "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that "
1003 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
1004 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001005 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001006 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001007 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001008 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1009 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
1010 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001011 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001012 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001013 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
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 output variable uses location that "
1016 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
1017 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001018 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001019 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001020 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001021 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1022 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
1023 "components by %u components",
1024 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001025 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001026 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001027 break;
1028
1029 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001030 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001031 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001032 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
1033 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
1034 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001035 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001036 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001037 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001038 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001039 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1040 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1041 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001042 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001043 break;
1044
Jeff Bolz148d94e2018-12-13 21:25:56 -06001045 case VK_SHADER_STAGE_RAYGEN_BIT_NV:
1046 case VK_SHADER_STAGE_ANY_HIT_BIT_NV:
1047 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV:
1048 case VK_SHADER_STAGE_MISS_BIT_NV:
1049 case VK_SHADER_STAGE_INTERSECTION_BIT_NV:
1050 case VK_SHADER_STAGE_CALLABLE_BIT_NV:
1051 case VK_SHADER_STAGE_TASK_BIT_NV:
1052 case VK_SHADER_STAGE_MESH_BIT_NV:
1053 break;
1054
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001055 default:
1056 assert(false); // This should never happen
1057 }
1058 return skip;
1059}
1060
sfricke-samsungef15e482022-01-26 11:32:49 -08001061bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *module_state, const spirv_inst_iter &insn) const {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001062 bool skip = false;
1063
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001064 switch (insn.opcode()) {
1065 // Go through all ImageRead/Write instructions
1066 case spv::OpImageSparseRead:
1067 case spv::OpImageRead: {
1068 // spirv-val validates this is an OpTypeImage
sfricke-samsungef15e482022-01-26 11:32:49 -08001069 const uint32_t image = module_state->GetTypeId(insn.word(3));
1070 const spirv_inst_iter image_def = module_state->get_def(image);
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001071
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001072 const uint32_t dim = image_def.word(3);
1073 const uint32_t image_format = image_def.word(8);
1074 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1075 // StorageImageReadWithoutFormat Capability was declared.
1076 if (dim != spv::DimSubpassData && image_format == spv::ImageFormatUnknown) {
1077 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1078 "shaderStorageImageReadWithoutFormat", kVUID_Features_shaderStorageImageReadWithoutFormat);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001079 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001080 break;
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001081 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001082 case spv::OpImageWrite: {
1083 // spirv-val validates this is an OpTypeImage
sfricke-samsungef15e482022-01-26 11:32:49 -08001084 const uint32_t image = module_state->GetTypeId(insn.word(1));
1085 const spirv_inst_iter image_def = module_state->get_def(image);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001086
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001087 const uint32_t image_format = image_def.word(8);
1088 if (image_format == spv::ImageFormatUnknown) {
1089 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1090 "shaderStorageImageWriteWithoutFormat", kVUID_Features_shaderStorageImageWriteWithoutFormat);
1091 }
1092 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001093 }
1094
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001095 // Go through all variables for images and check decorations
1096 case spv::OpVariable: {
1097 // spirv-val validates this is an OpTypePointer
sfricke-samsungef15e482022-01-26 11:32:49 -08001098 const spirv_inst_iter pointer_def = module_state->get_def(insn.word(1));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001099 if (pointer_def.word(2) != spv::StorageClassUniformConstant) {
1100 break; // Vulkan Spec says storage image must be UniformConstant
1101 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001102 spirv_inst_iter type_def = module_state->get_def(pointer_def.word(3));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001103
1104 // Unpack an optional level of arraying
1105 if (type_def.opcode() == spv::OpTypeArray || type_def.opcode() == spv::OpTypeRuntimeArray) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001106 type_def = module_state->get_def(type_def.word(2));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001107 }
1108
sfricke-samsungef15e482022-01-26 11:32:49 -08001109 if (type_def != module_state->end() && type_def.opcode() == spv::OpTypeImage) {
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001110 // Only check if the Image Dim operand is not SubpassData
1111 const uint32_t dim = type_def.word(3);
1112 // Only check storage images
1113 const uint32_t sampled = type_def.word(7);
1114 const uint32_t image_format = type_def.word(8);
1115 if ((dim == spv::DimSubpassData) || (sampled != 2) || (image_format != spv::ImageFormatUnknown)) {
1116 break;
1117 }
1118
1119 const uint32_t var_id = insn.word(2);
sfricke-samsungef15e482022-01-26 11:32:49 -08001120 decoration_set img_decorations = module_state->get_decorations(var_id);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001121
1122 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1123 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1124 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001125 "shaderStorageImageReadWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith Unknown "
1126 "format and is not decorated with NonReadable",
1127 module_state->DescribeInstruction(module_state->get_def(var_id)).c_str(),
1128 module_state->DescribeInstruction(type_def).c_str());
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001129 }
1130
1131 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1132 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1133 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001134 "shaderStorageImageWriteWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith "
1135 "Unknown format and is not decorated with NonWritable",
1136 module_state->DescribeInstruction(module_state->get_def(var_id)).c_str(),
1137 module_state->DescribeInstruction(type_def).c_str());
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001138 }
1139 }
1140 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001141 }
1142 }
1143
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001144 return skip;
1145}
1146
sfricke-samsungdc96f302020-03-18 20:42:10 -07001147bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1148 bool skip = false;
1149 uint32_t total_resources = 0;
1150
1151 // Only currently testing for graphics and compute pipelines
1152 // TODO: Add check and support for Ray Tracing pipeline VUID 03428
1153 if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) {
1154 return false;
1155 }
1156
1157 if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
amhagana448ea52021-11-02 14:09:14 -04001158 if (pipeline->rp_state->use_dynamic_rendering) {
Aaron Hagan92a44f82021-11-19 09:34:56 -05001159 total_resources += pipeline->rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001160 } else {
1161 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
1162 total_resources +=
1163 pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].colorAttachmentCount;
1164 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001165 }
1166
1167 // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle
1168 // input from CreatePipeline and CreatePipelineLayout level
1169 for (auto set_layout : pipeline->pipeline_layout->set_layouts) {
1170 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1171 continue;
1172 }
1173
1174 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1175 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1176 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1177 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1178 // Check only descriptor types listed in maxPerStageResources description in spec
1179 switch (binding->descriptorType) {
1180 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1181 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1182 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1183 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1184 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1185 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1186 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1187 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1188 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1189 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1190 total_resources += binding->descriptorCount;
1191 break;
1192 default:
1193 break;
1194 }
1195 }
1196 }
1197 }
1198
1199 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
1200 const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687"
1201 : "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001202 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001203 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1204 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1205 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1206 }
1207
1208 return skip;
1209}
1210
Jeff Bolze4356752019-03-07 11:23:46 -06001211// copy the specialization constant value into buf, if it is present
1212void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1213 VkSpecializationInfo const *spec = pStage->pSpecializationInfo;
1214
1215 if (spec && spec_id < spec->mapEntryCount) {
1216 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1217 }
1218}
1219
1220// Fill in value with the constant or specialization constant value, if available.
1221// Returns true if the value has been accurately filled out.
sfricke-samsungef15e482022-01-26 11:32:49 -08001222static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *module_state,
1223 VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001224 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001225 auto type_id = module_state->get_def(insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001226 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1227 return false;
1228 }
1229 switch (insn.opcode()) {
1230 case spv::OpSpecConstant:
1231 *value = insn.word(3);
1232 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1233 return true;
1234 case spv::OpConstant:
1235 *value = insn.word(3);
1236 return true;
1237 default:
1238 return false;
1239 }
1240}
1241
1242// Map SPIR-V type to VK_COMPONENT_TYPE enum
sfricke-samsungef15e482022-01-26 11:32:49 -08001243VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *module_state) {
Jeff Bolze4356752019-03-07 11:23:46 -06001244 switch (insn.opcode()) {
1245 case spv::OpTypeInt:
1246 switch (insn.word(2)) {
1247 case 8:
1248 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1249 case 16:
1250 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1251 case 32:
1252 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1253 case 64:
1254 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1255 default:
1256 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1257 }
1258 case spv::OpTypeFloat:
1259 switch (insn.word(2)) {
1260 case 16:
1261 return VK_COMPONENT_TYPE_FLOAT16_NV;
1262 case 32:
1263 return VK_COMPONENT_TYPE_FLOAT32_NV;
1264 case 64:
1265 return VK_COMPONENT_TYPE_FLOAT64_NV;
1266 default:
1267 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1268 }
1269 default:
1270 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1271 }
1272}
1273
1274// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1275// in SPIRV-Tools (e.g. due to specialization constant usage).
sfricke-samsungef15e482022-01-26 11:32:49 -08001276bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *module_state, VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001277 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001278 bool skip = false;
1279
1280 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001281 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001282 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001283 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001284
1285 struct CoopMatType {
1286 uint32_t scope, rows, cols;
1287 VkComponentTypeNV component_type;
1288 bool all_constant;
1289
1290 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1291
sfricke-samsungef15e482022-01-26 11:32:49 -08001292 void Init(uint32_t id, SHADER_MODULE_STATE const *module_state, VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001293 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001294 spirv_inst_iter insn = module_state->get_def(id);
Jeff Bolze4356752019-03-07 11:23:46 -06001295 uint32_t component_type_id = insn.word(2);
1296 uint32_t scope_id = insn.word(3);
1297 uint32_t rows_id = insn.word(4);
1298 uint32_t cols_id = insn.word(5);
sfricke-samsungef15e482022-01-26 11:32:49 -08001299 auto component_type_iter = module_state->get_def(component_type_id);
1300 auto scope_iter = module_state->get_def(scope_id);
1301 auto rows_iter = module_state->get_def(rows_id);
1302 auto cols_iter = module_state->get_def(cols_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001303
1304 all_constant = true;
sfricke-samsungef15e482022-01-26 11:32:49 -08001305 if (!GetIntConstantValue(scope_iter, module_state, pStage, id_to_spec_id, &scope)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001306 all_constant = false;
1307 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001308 if (!GetIntConstantValue(rows_iter, module_state, pStage, id_to_spec_id, &rows)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001309 all_constant = false;
1310 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001311 if (!GetIntConstantValue(cols_iter, module_state, pStage, id_to_spec_id, &cols)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001312 all_constant = false;
1313 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001314 component_type = GetComponentType(component_type_iter, module_state);
Jeff Bolze4356752019-03-07 11:23:46 -06001315 }
1316 };
1317
1318 bool seen_coopmat_capability = false;
1319
sfricke-samsungef15e482022-01-26 11:32:49 -08001320 for (auto insn : *module_state) {
Jeff Bolze4356752019-03-07 11:23:46 -06001321 // Whitelist instructions whose result can be a cooperative matrix type, and
1322 // keep track of their types. It would be nice if SPIRV-Headers generated code
1323 // to identify which instructions have a result type and result id. Lacking that,
1324 // this whitelist is based on the set of instructions that
1325 // SPV_NV_cooperative_matrix says can be used with cooperative matrix types.
1326 switch (insn.opcode()) {
1327 case spv::OpLoad:
1328 case spv::OpCooperativeMatrixLoadNV:
1329 case spv::OpCooperativeMatrixMulAddNV:
1330 case spv::OpSNegate:
1331 case spv::OpFNegate:
1332 case spv::OpIAdd:
1333 case spv::OpFAdd:
1334 case spv::OpISub:
1335 case spv::OpFSub:
1336 case spv::OpFDiv:
1337 case spv::OpSDiv:
1338 case spv::OpUDiv:
1339 case spv::OpMatrixTimesScalar:
1340 case spv::OpConstantComposite:
1341 case spv::OpCompositeConstruct:
1342 case spv::OpConvertFToU:
1343 case spv::OpConvertFToS:
1344 case spv::OpConvertSToF:
1345 case spv::OpConvertUToF:
1346 case spv::OpUConvert:
1347 case spv::OpSConvert:
1348 case spv::OpFConvert:
1349 id_to_type_id[insn.word(2)] = insn.word(1);
1350 break;
1351 default:
1352 break;
1353 }
1354
1355 switch (insn.opcode()) {
1356 case spv::OpDecorate:
1357 if (insn.word(2) == spv::DecorationSpecId) {
1358 id_to_spec_id[insn.word(1)] = insn.word(3);
1359 }
1360 break;
1361 case spv::OpCapability:
1362 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1363 seen_coopmat_capability = true;
1364
1365 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001366 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001367 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001368 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1369 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001370 }
1371 }
1372 break;
1373 case spv::OpMemoryModel:
1374 // If the capability isn't enabled, don't bother with the rest of this function.
1375 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1376 if (!seen_coopmat_capability) {
1377 return skip;
1378 }
1379 break;
1380 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001381 CoopMatType m;
sfricke-samsungef15e482022-01-26 11:32:49 -08001382 m.Init(insn.word(1), module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001383
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001384 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001385 // Validate that the type parameters are all supported for one of the
1386 // operands of a cooperative matrix property.
1387 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001388 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001389 if (cooperative_matrix_properties[i].AType == m.component_type &&
1390 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1391 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001392 valid = true;
1393 break;
1394 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001395 if (cooperative_matrix_properties[i].BType == m.component_type &&
1396 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1397 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001398 valid = true;
1399 break;
1400 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001401 if (cooperative_matrix_properties[i].CType == m.component_type &&
1402 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1403 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001404 valid = true;
1405 break;
1406 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001407 if (cooperative_matrix_properties[i].DType == m.component_type &&
1408 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1409 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001410 valid = true;
1411 break;
1412 }
1413 }
1414 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001415 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001416 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1417 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001418 }
1419 }
1420 break;
1421 }
1422 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001423 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001424 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1425 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1426 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1427 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001428 // Couldn't find type of matrix
1429 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001430 break;
1431 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001432 d.Init(id_to_type_id[insn.word(2)], module_state, pStage, id_to_spec_id);
1433 a.Init(id_to_type_id[insn.word(3)], module_state, pStage, id_to_spec_id);
1434 b.Init(id_to_type_id[insn.word(4)], module_state, pStage, id_to_spec_id);
1435 c.Init(id_to_type_id[insn.word(5)], module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001436
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001437 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001438 // Validate that the type parameters are all supported for the same
1439 // cooperative matrix property.
1440 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001441 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001442 if (cooperative_matrix_properties[i].AType == a.component_type &&
1443 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1444 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001445
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001446 cooperative_matrix_properties[i].BType == b.component_type &&
1447 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1448 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001449
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001450 cooperative_matrix_properties[i].CType == c.component_type &&
1451 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1452 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001453
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001454 cooperative_matrix_properties[i].DType == d.component_type &&
1455 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1456 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001457 valid = true;
1458 break;
1459 }
1460 }
1461 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001462 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001463 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1464 "VkCooperativeMatrixPropertiesNV",
1465 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001466 }
1467 }
1468 break;
1469 }
1470 default:
1471 break;
1472 }
1473 }
1474
1475 return skip;
1476}
1477
sfricke-samsungef15e482022-01-26 11:32:49 -08001478bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *module_state, VkPipelineShaderStageCreateInfo const *pStage,
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001479 const PIPELINE_STATE *pipeline) const {
1480 bool skip = false;
1481
1482 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1483 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1484 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001485 for (auto insn : *module_state) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001486 switch (insn.opcode()) {
1487 case spv::OpCapability:
1488 if (insn.word(1) == spv::CapabilitySampleRateShading) {
1489 auto subpass_flags =
1490 (pipeline->rp_state == nullptr)
1491 ? 0
Jeremy Gebben11af9792021-08-20 10:20:09 -06001492 : pipeline->rp_state->createInfo.pSubpasses[pipeline->create_info.graphics.subpass].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001493 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1494 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001495 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001496 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1497 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1498 }
1499 }
1500 break;
1501 default:
1502 break;
1503 }
1504 }
1505 }
1506
1507 return skip;
1508}
1509
ziga-lunarg73163742021-08-25 13:15:29 +02001510bool CoreChecks::ValidateShaderSubgroupSizeControl(VkPipelineShaderStageCreateInfo const *pStage) const {
1511 bool skip = false;
1512
1513 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001514 !enabled_features.core13.subgroupSizeControl) {
ziga-lunarg73163742021-08-25 13:15:29 +02001515 skip |= LogError(
1516 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1517 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1518 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1519 }
1520
1521 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001522 !enabled_features.core13.computeFullSubgroups) {
ziga-lunarg73163742021-08-25 13:15:29 +02001523 skip |= LogError(
1524 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1525 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1526 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1527 }
1528
1529 return skip;
1530}
1531
sfricke-samsungef15e482022-01-26 11:32:49 -08001532bool CoreChecks::ValidateAtomicsTypes(SHADER_MODULE_STATE const *module_state) const {
sfricke-samsung58b84352021-07-31 21:41:04 -07001533 bool skip = false;
1534
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001535 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001536 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001537
sfricke-samsungf5042b12021-08-05 01:09:40 -07001538 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1539 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1540
1541 const bool valid_storage_buffer_float = (
1542 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1543 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1544 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1545 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1546 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1547 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1548 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1549 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1550 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1551
1552 const bool valid_workgroup_float = (
1553 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1554 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1555 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1556 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1557 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1558 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1559 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1560 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1561 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1562
1563 const bool valid_image_float = (
1564 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1565 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1566 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1567
1568 const bool valid_16_float = (
1569 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1570 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1571 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1572 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1573 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1574 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1575
1576 const bool valid_32_float = (
1577 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1578 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1579 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1580 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1581 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1582 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1583 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1584 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1585 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1586
1587 const bool valid_64_float = (
1588 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1589 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1590 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1591 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1592 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1593 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1594 // clang-format on
1595
sfricke-samsungef15e482022-01-26 11:32:49 -08001596 for (const auto &atomic_inst : module_state->GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001597 const atomic_instruction &atomic = atomic_inst.second;
sfricke-samsungef15e482022-01-26 11:32:49 -08001598 const spirv_inst_iter atomic_def = module_state->at(atomic_inst.first);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001599 const uint32_t opcode = atomic_def.opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001600
1601 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001602 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001603 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1604 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001605 skip |= LogError(device, "VUID-RuntimeSpirv-None-06278",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001606 "%s: Can't use 64-bit int atomics operations\n%s\nwith %s storage class without "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001607 "shaderBufferInt64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001608 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1609 module_state->DescribeInstruction(atomic_def).c_str(), StorageClassName(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001610 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1611 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001612 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001613 "%s: Can't use 64-bit int atomics operations\n%s\nwith Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001614 "shaderSharedInt64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001615 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1616 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001617 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001618 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001619 "%s: Can't use 64-bit int atomics operations\n%s\nwith Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001620 "shaderImageInt64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001621 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1622 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001623 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001624 } else if (atomic.type == spv::OpTypeFloat) {
1625 // Validate Floats
1626 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1627 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001628 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1629 : "VUID-RuntimeSpirv-None-06280";
1630 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001631 "%s: Can't use float atomics operations\n%s\nwith StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001632 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1633 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1634 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1635 "shaderBufferFloat64AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001636 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1637 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001638 } else if (opcode == spv::OpAtomicFAddEXT) {
1639 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1640 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001641 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001642 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001643 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1644 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001645 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1646 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001647 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001648 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001649 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1650 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001651 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1652 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001653 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001654 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001655 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1656 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001657 }
1658 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1659 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001660 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1661 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1662 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001663 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1664 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001665 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001666 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1667 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1668 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001669 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1670 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001671 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001672 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1673 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1674 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001675 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1676 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001677 }
1678 } else {
1679 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1680 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001681 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1682 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith "
1683 "StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001684 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1685 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001686 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001687 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1688 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith "
1689 "StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001690 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1691 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001692 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001693 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1694 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith "
1695 "StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001696 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1697 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001698 }
1699 }
1700 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1701 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001702 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1703 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsungef15e482022-01-26 11:32:49 -08001704 skip |=
1705 LogError(device, vuid,
1706 "%s: Can't use float atomics operations\n%s\nwith Workgroup storage class without "
1707 "shaderSharedFloat32Atomics or "
1708 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1709 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1710 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
1711 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1712 module_state->DescribeInstruction(atomic_def).c_str());
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,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001716 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001717 "storage class without shaderSharedFloat16AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001718 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1719 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001720 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1721 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001722 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001723 "storage class without shaderSharedFloat32AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001724 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1725 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001726 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1727 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001728 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001729 "storage class without shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001730 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1731 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001732 }
1733 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1734 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001735 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1736 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1737 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001738 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1739 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001740 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001741 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1742 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1743 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001744 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1745 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001746 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001747 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1748 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1749 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001750 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1751 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001752 }
1753 } else {
1754 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1755 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001756 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1757 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1758 "storage class without shaderSharedFloat16Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001759 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1760 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001761 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001762 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1763 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1764 "storage class without shaderSharedFloat32Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001765 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1766 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001767 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001768 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1769 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1770 "storage class without shaderSharedFloat64Atomics enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001771 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1772 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001773 }
1774 }
1775 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001776 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1777 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001778 skip |= LogError(
1779 device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001780 "%s: Can't use float atomics operations\n%s\nwith Image storage class without shaderImageFloat32Atomics or "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001781 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001782 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1783 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001784 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001785 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001786 "%s: Can't use 16-bit float atomics operations\n%s\nwithout shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001787 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1788 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001789 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1790 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001791 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001792 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1793 : "VUID-RuntimeSpirv-None-06335";
1794 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001795 "%s: Can't use 32-bit float atomics operations\n%s\nwithout shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001796 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1797 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1798 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1799 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001800 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1801 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001802 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001803 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1804 : "VUID-RuntimeSpirv-None-06336";
1805 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001806 "%s: Can't use 64-bit float atomics operations\n%s\nwithout shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001807 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1808 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sfricke-samsungef15e482022-01-26 11:32:49 -08001809 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
1810 module_state->DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001811 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001812 }
1813 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001814 return skip;
1815}
1816
sfricke-samsungef15e482022-01-26 11:32:49 -08001817bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *module_state, spirv_inst_iter entrypoint,
1818 VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001819 auto entrypoint_id = entrypoint.word(2);
1820
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001821 // The first denorm execution mode encountered, along with its bit width.
1822 // Used to check if SeparateDenormSettings is respected.
1823 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001824
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001825 // The first rounding mode encountered, along with its bit width.
1826 // Used to check if SeparateRoundingModeSettings is respected.
1827 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001828
1829 bool skip = false;
1830
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001831 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001832 uint32_t invocations = 0;
1833
sfricke-samsungef15e482022-01-26 11:32:49 -08001834 const auto &execution_mode_inst = module_state->GetExecutionModeInstructions();
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001835 auto it = execution_mode_inst.find(entrypoint_id);
1836 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001837 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001838 auto mode = insn.word(2);
1839 switch (mode) {
1840 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1841 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001842 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001843 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001844 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001845 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device\n%s",
1846 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001847 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1848 skip |= LogError(
1849 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001850 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device\n%s",
1851 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001852 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1853 skip |= LogError(
1854 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001855 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device\n%s",
1856 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001857 }
1858 break;
1859 }
1860
1861 case spv::ExecutionModeDenormPreserve: {
1862 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001863 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1864 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001865 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device\n%s",
1866 module_state->DescribeInstruction(insn).c_str());
1867 ;
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001868 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1869 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001870 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device\n%s",
1871 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001872 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1873 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001874 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device\n%s",
1875 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001876 }
1877
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001878 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1879 // Register the first denorm execution mode found
1880 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001881 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001882 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001883 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001884 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001885 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001886 "Shader uses different denorm execution modes for 16 and 64-bit but "
1887 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001888 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
1889 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001890 }
1891 break;
1892
Mike Schuchardt2df08912020-12-15 16:28:09 -08001893 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001894 break;
1895
Mike Schuchardt2df08912020-12-15 16:28:09 -08001896 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001897 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001898 "Shader uses different denorm execution modes for different bit widths but "
1899 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001900 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
1901 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001902 break;
1903
1904 default:
1905 break;
1906 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001907 }
1908 break;
1909 }
1910
1911 case spv::ExecutionModeDenormFlushToZero: {
1912 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001913 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001914 skip |=
1915 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1916 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device\n%s",
1917 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001918 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001919 skip |=
1920 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1921 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device\n%s",
1922 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001923 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001924 skip |=
1925 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1926 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device\n%s",
1927 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001928 }
1929
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001930 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1931 // Register the first denorm execution mode found
1932 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001933 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001934 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001935 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001936 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001937 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001938 "Shader uses different denorm execution modes for 16 and 64-bit but "
1939 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001940 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
1941 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001942 }
1943 break;
1944
Mike Schuchardt2df08912020-12-15 16:28:09 -08001945 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001946 break;
1947
Mike Schuchardt2df08912020-12-15 16:28:09 -08001948 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001949 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001950 "Shader uses different denorm execution modes for different bit widths but "
1951 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001952 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
1953 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001954 break;
1955
1956 default:
1957 break;
1958 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001959 }
1960 break;
1961 }
1962
1963 case spv::ExecutionModeRoundingModeRTE: {
1964 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001965 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1966 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001967 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device\n%s",
1968 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001969 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1970 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001971 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device\n%s",
1972 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001973 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1974 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001975 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device\n%s",
1976 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001977 }
1978
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001979 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1980 // Register the first rounding mode found
1981 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001982 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001983 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001984 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001985 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001986 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001987 "Shader uses different rounding modes for 16 and 64-bit but "
1988 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001989 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
1990 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001991 }
1992 break;
1993
Mike Schuchardt2df08912020-12-15 16:28:09 -08001994 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001995 break;
1996
Mike Schuchardt2df08912020-12-15 16:28:09 -08001997 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001998 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001999 "Shader uses different rounding modes for different bit widths but "
2000 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002001 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
2002 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002003 break;
2004
2005 default:
2006 break;
2007 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002008 }
2009 break;
2010 }
2011
2012 case spv::ExecutionModeRoundingModeRTZ: {
2013 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002014 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
2015 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002016 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device\n%s",
2017 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002018 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
2019 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002020 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device\n%s",
2021 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002022 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
2023 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002024 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device\n%s",
2025 module_state->DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002026 }
2027
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002028 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2029 // Register the first rounding mode found
2030 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002031 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002032 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08002033 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002034 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002035 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002036 "Shader uses different rounding modes for 16 and 64-bit but "
2037 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002038 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
2039 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002040 }
2041 break;
2042
Mike Schuchardt2df08912020-12-15 16:28:09 -08002043 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002044 break;
2045
Mike Schuchardt2df08912020-12-15 16:28:09 -08002046 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002047 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002048 "Shader uses different rounding modes for different bit widths but "
2049 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002050 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
2051 module_state->DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002052 break;
2053
2054 default:
2055 break;
2056 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002057 }
2058 break;
2059 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002060
2061 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002062 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002063 break;
2064 }
2065
2066 case spv::ExecutionModeInvocations: {
2067 invocations = insn.word(3);
2068 break;
2069 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002070
2071 case spv::ExecutionModeLocalSizeId: {
Tony-LunarG273f32f2021-09-28 08:56:30 -06002072 if (!enabled_features.core13.maintenance4) {
Piers Daniella7f93b62021-11-20 12:32:04 -07002073 skip |= LogError(device, "VUID-RuntimeSpirv-LocalSizeId-06434",
2074 "LocalSizeId execution mode used but maintenance4 feature not enabled");
2075 }
2076 break;
2077 }
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002078
2079 case spv::ExecutionModeEarlyFragmentTests: {
2080 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) &&
Younggwan Kimf8601f92021-12-17 09:38:07 +00002081 (pipeline && pipeline->create_info.graphics.pDepthStencilState &&
2082 (pipeline->create_info.graphics.pDepthStencilState->flags &
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002083 (VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_DEPTH_ACCESS_BIT_ARM |
2084 VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_STENCIL_ACCESS_BIT_ARM)) != 0)) {
2085 skip |= LogError(
2086 device, " VUID-VkGraphicsPipelineCreateInfo-pStages-06466",
2087 "The fragment shader enables early fragment tests, but VkPipelineDepthStencilStateCreateInfo::flags == "
2088 "%s",
2089 string_VkPipelineDepthStencilStateCreateFlags(pipeline->create_info.graphics.pDepthStencilState->flags)
2090 .c_str());
2091 }
2092 break;
2093 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002094 }
2095 }
2096 }
2097
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002098 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002099 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002100 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2101 "Geometry shader entry point must have an OpExecutionMode instruction that "
2102 "specifies a maximum output vertex count that is greater than 0 and less "
2103 "than or equal to maxGeometryOutputVertices. "
2104 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002105 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002106 }
2107
2108 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002109 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2110 "Geometry shader entry point must have an OpExecutionMode instruction that "
2111 "specifies an invocation count that is greater than 0 and less "
2112 "than or equal to maxGeometryShaderInvocations. "
2113 "Invocations=%d, maxGeometryShaderInvocations=%d",
2114 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002115 }
2116 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002117 return skip;
2118}
2119
Chris Forbes47567b72017-06-09 12:09:45 -07002120// For given pipelineLayout verify that the set_layout_node at slot.first
2121// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002122static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002123 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002124 if (!pipelineLayout) return nullptr;
2125
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002126 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07002127
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002128 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07002129}
2130
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002131// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2132// o If there is only a vertex shader : gl_PointSize must be written when using points
2133// o If there is a geometry or tessellation shader:
2134// - If shaderTessellationAndGeometryPointSize feature is enabled:
2135// * gl_PointSize must be written in the final geometry stage
2136// - If shaderTessellationAndGeometryPointSize feature is disabled:
2137// * gl_PointSize must NOT be written and a default of 1.0 is assumed
sfricke-samsungef15e482022-01-26 11:32:49 -08002138bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *module_state,
John Zulaufac4c6e12019-07-01 16:05:58 -06002139 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002140 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2141 return false;
2142 }
2143
2144 bool pointsize_written = false;
2145 bool skip = false;
2146
2147 // Search for PointSize built-in decorations
sfricke-samsungef15e482022-01-26 11:32:49 -08002148 for (const auto &set : module_state->GetBuiltinDecorationList()) {
2149 auto insn = module_state->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002150 if (set.builtin == spv::BuiltInPointSize) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002151 pointsize_written = module_state->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002152 if (pointsize_written) {
2153 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002154 }
2155 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002156 }
2157
2158 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002159 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002160 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002161 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002162 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2163 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002164 }
2165 } else if (!pointsize_written) {
2166 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002167 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002168 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2169 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002170 }
2171 return skip;
2172}
John Zulauf14c355b2019-06-27 16:09:37 -06002173
sfricke-samsungef15e482022-01-26 11:32:49 -08002174bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *module_state,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002175 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2176 bool primitiverate_written = false;
2177 bool viewportindex_written = false;
2178 bool viewportmask_written = false;
2179 bool skip = false;
2180
2181 // Check if the primitive shading rate is written
sfricke-samsungef15e482022-01-26 11:32:49 -08002182 for (const auto &set : module_state->GetBuiltinDecorationList()) {
2183 auto insn = module_state->at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002184 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002185 primitiverate_written = module_state->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002186 } else if (set.builtin == spv::BuiltInViewportIndex) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002187 viewportindex_written = module_state->IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002188 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002189 viewportmask_written = module_state->IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002190 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002191 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2192 break;
2193 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002194 }
2195
Tony-LunarGd44844c2021-01-22 13:24:37 -07002196 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002197 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && pipeline->create_info.graphics.pViewportState) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00002198 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06002199 pipeline->create_info.graphics.pViewportState->viewportCount > 1 && primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002200 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002201 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2202 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2203 "multiple viewports "
2204 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2205 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002206 }
2207
2208 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002209 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002210 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2211 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2212 "ViewportIndex built-ins,"
2213 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2214 string_VkShaderStageFlagBits(stage));
2215 }
2216
2217 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002218 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002219 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2220 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2221 "ViewportMaskNV built-ins,"
2222 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2223 string_VkShaderStageFlagBits(stage));
2224 }
2225 }
2226 return skip;
2227}
2228
sfricke-samsungef15e482022-01-26 11:32:49 -08002229bool CoreChecks::ValidateDecorations(SHADER_MODULE_STATE const *module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002230 bool skip = false;
2231
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002232 std::vector<spirv_inst_iter> xfb_streams;
2233 std::vector<spirv_inst_iter> xfb_buffers;
ziga-lunargef2c3172021-11-07 10:35:29 +01002234 std::vector<spirv_inst_iter> xfb_offsets;
2235
sfricke-samsungef15e482022-01-26 11:32:49 -08002236 for (const auto &op_decorate : module_state->GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002237 uint32_t decoration = op_decorate.word(2);
2238 if (decoration == spv::DecorationXfbStride) {
2239 uint32_t stride = op_decorate.word(3);
2240 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2241 skip |= LogError(
2242 device, "VUID-RuntimeSpirv-XfbStride-06313",
2243 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2244 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2245 ").",
2246 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2247 }
2248 }
ziga-lunarg423cf212021-11-07 00:00:27 +01002249 if (decoration == spv::DecorationStream) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002250 xfb_streams.push_back(op_decorate);
ziga-lunarg423cf212021-11-07 00:00:27 +01002251 uint32_t stream = op_decorate.word(3);
2252 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2253 skip |= LogError(
2254 device, "VUID-RuntimeSpirv-Stream-06312",
2255 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2256 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32 ").",
2257 stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
2258 }
2259 }
ziga-lunargef2c3172021-11-07 10:35:29 +01002260 if (decoration == spv::DecorationXfbBuffer) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002261 xfb_buffers.push_back(op_decorate);
ziga-lunargef2c3172021-11-07 10:35:29 +01002262 }
2263 if (decoration == spv::DecorationOffset) {
2264 xfb_offsets.push_back(op_decorate);
2265 }
2266 }
2267
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002268 // XfbBuffer, buffer data size
2269 std::vector<std::pair<uint32_t, uint32_t>> buffer_data_sizes;
ziga-lunargef2c3172021-11-07 10:35:29 +01002270 for (const auto &op_decorate : xfb_offsets) {
2271 for (const auto xfb_buffer : xfb_buffers) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002272 if (xfb_buffer.word(1) == op_decorate.word(1)) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002273 const auto offset = op_decorate.word(3);
sfricke-samsungef15e482022-01-26 11:32:49 -08002274 const auto def = module_state->get_def(xfb_buffer.word(1));
2275 const auto size = module_state->GetTypeBytesSize(def);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002276 const uint32_t buffer_data_size = offset + size;
2277 if (buffer_data_size > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002278 skip |= LogError(
2279 device, "VUID-RuntimeSpirv-Offset-06308",
2280 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_offset (%" PRIu32
2281 ") + size of variable (%" PRIu32 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2282 "(%" PRIu32 ").",
2283 offset, size, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2284 }
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002285
2286 bool found = false;
2287 for (auto &bds : buffer_data_sizes) {
2288 if (bds.first == xfb_buffer.word(1)) {
2289 bds.second = std::max(bds.second, buffer_data_size);
2290 found = true;
2291 break;
2292 }
2293 }
2294 if (!found) {
2295 buffer_data_sizes.emplace_back(xfb_buffer.word(1), buffer_data_size);
2296 }
2297
ziga-lunargef2c3172021-11-07 10:35:29 +01002298 break;
2299 }
2300 }
ziga-lunargce66e542021-09-19 00:11:14 +02002301 }
2302
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002303 std::unordered_map<uint32_t, uint32_t> stream_data_size;
2304 for (const auto &xfb_stream : xfb_streams) {
2305 for (const auto& bds : buffer_data_sizes) {
2306 if (xfb_stream.word(1) == bds.first) {
2307 uint32_t stream = xfb_stream.word(3);
2308 const auto itr = stream_data_size.find(stream);
2309 if (itr != stream_data_size.end()) {
2310 itr->second += bds.second;
2311 } else {
2312 stream_data_size.insert({stream, bds.second});
2313 }
2314 }
2315 }
2316 }
2317
2318 for (const auto& stream : stream_data_size) {
2319 if (stream.second > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreamDataSize) {
2320 skip |= LogError(device, "VUID-RuntimeSpirv-XfbBuffer-06309",
2321 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2322 ") having the sum of buffer data sizes (%" PRIu32
2323 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2324 "(%" PRIu32 ").",
2325 stream.first, stream.second,
2326 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2327 }
2328 }
2329
ziga-lunargce66e542021-09-19 00:11:14 +02002330 return skip;
2331}
2332
sfricke-samsungef15e482022-01-26 11:32:49 -08002333bool CoreChecks::ValidateTransformFeedback(SHADER_MODULE_STATE const *module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002334 bool skip = false;
2335
ziga-lunarg28d08792021-10-13 15:42:59 +02002336 // Temp workaround to prevent false positive errors
2337 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sfricke-samsungef15e482022-01-26 11:32:49 -08002338 if (module_state->HasMultipleEntryPoints()) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002339 return skip;
2340 }
2341
2342 layer_data::unordered_set<uint32_t> emitted_streams;
2343 bool output_points = false;
sfricke-samsungef15e482022-01-26 11:32:49 -08002344 for (const auto &insn : *module_state) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002345 const uint32_t opcode = insn.opcode();
2346 if (opcode == spv::OpEmitStreamVertex) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002347 emitted_streams.emplace(static_cast<uint32_t>(module_state->GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002348 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002349 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002350 uint32_t stream = static_cast<uint32_t>(module_state->GetConstantValueById(insn.word(1)));
ziga-lunarg28d08792021-10-13 15:42:59 +02002351 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2352 skip |= LogError(
2353 device, "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002354 "vkCreateGraphicsPipelines(): shader uses transform feedback stream\n%s\nwith index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002355 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2356 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002357 module_state->DescribeInstruction(insn).c_str(), stream,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002358 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002359 }
2360 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002361 if ((opcode == spv::OpExecutionMode || opcode == spv::OpExecutionModeId) &&
2362 insn.word(2) == spv::ExecutionModeOutputPoints) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002363 output_points = true;
2364 }
2365 }
2366
2367 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2368 if (emitted_streams_size > 1 && !output_points &&
2369 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
2370 skip |= LogError(
2371 device, "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2372 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles is VK_FALSE, but execution mode is not OutputPoints.",
2373 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002374 }
2375
2376 return skip;
2377}
2378
ziga-lunarge1988962021-09-16 13:32:34 +02002379bool CoreChecks::ValidateWorkgroupInitialization(SHADER_MODULE_STATE const *src, spirv_inst_iter &insn) const {
2380 bool skip = false;
2381
2382 const uint32_t opcode = insn.opcode();
2383 if (opcode == spv::OpVariable) {
2384 uint32_t storage_class = insn.word(3);
2385 if (storage_class == spv::StorageClassWorkgroup) {
2386 if (insn.len() > 4 &&
2387 !enabled_features.zero_initialize_work_group_memory_features.shaderZeroInitializeWorkgroupMemory) {
2388 const char *vuid = IsExtEnabled(device_extensions.vk_khr_zero_initialize_workgroup_memory)
2389 ? "VUID-RuntimeSpirv-shaderZeroInitializeWorkgroupMemory-06372"
2390 : "VUID-RuntimeSpirv-OpVariable-06373";
2391 skip |= LogError(
2392 device, vuid,
2393 "vkCreateShaderModule(): "
2394 "VkPhysicalDeviceZeroInitializeWorkgroupMemoryFeaturesKHR::shaderZeroInitializeWorkgroupMemory is not enabled, "
2395 "but shader contains an OpVariable with Workgroup Storage Class with an Initializer operand.\n%s",
2396 src->DescribeInstruction(insn).c_str());
2397 }
2398 }
2399 }
2400
2401 return skip;
2402}
2403
sfricke-samsung864162a2021-11-01 21:58:01 -07002404// Checks for both TexelOffset and TexelGatherOffset limits
sfricke-samsungef15e482022-01-26 11:32:49 -08002405bool CoreChecks::ValidateTexelOffsetLimits(SHADER_MODULE_STATE const *module_state, spirv_inst_iter &insn) const {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002406 bool skip = false;
2407
2408 const uint32_t opcode = insn.opcode();
sfricke-samsung864162a2021-11-01 21:58:01 -07002409 if (ImageGatherOperation(opcode) || ImageSampleOperation(opcode) || ImageFetchOperation(opcode)) {
sfricke-samsung3a25ed52022-01-20 02:24:36 -08002410 uint32_t image_operand_position = OpcodeImageOperandsPosition(opcode);
sfricke-samsung864162a2021-11-01 21:58:01 -07002411 // Image operands can be optional
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002412 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2413 auto image_operand = insn.word(image_operand_position);
sfricke-samsung864162a2021-11-01 21:58:01 -07002414 // Bits we are validating (sample/fetch only check ConstOffset)
ziga-lunarga12c75a2021-09-16 16:36:16 +02002415 uint32_t offset_bits =
sfricke-samsung864162a2021-11-01 21:58:01 -07002416 ImageGatherOperation(opcode)
2417 ? (spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask)
2418 : (spv::ImageOperandsConstOffsetMask);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002419 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002420 // Operand values follow
2421 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002422 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2423 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2424 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2425 if (image_operand & i) { // If the bit is set, consume operand
2426 if (insn.len() > index && (i & offset_bits)) {
2427 uint32_t constant_id = insn.word(index);
sfricke-samsungef15e482022-01-26 11:32:49 -08002428 const auto &constant = module_state->get_def(constant_id);
2429 const bool is_dynamic_offset = constant == module_state->end();
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002430 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002431 for (uint32_t j = 3; j < constant.len(); ++j) {
2432 uint32_t comp_id = constant.word(j);
sfricke-samsungef15e482022-01-26 11:32:49 -08002433 const auto &comp = module_state->get_def(comp_id);
2434 const auto &comp_type = module_state->get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002435 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002436 const uint32_t offset = comp.word(3);
sfricke-samsung864162a2021-11-01 21:58:01 -07002437 // spec requires minTexelGatherOffset/minTexelOffset to be -8 or less so never can compare if
2438 // unsigned spec requires maxTexelGatherOffset/maxTexelOffset to be 7 or greater so never can
2439 // compare if signed is less then zero
sfricke-samsungef3fe742021-10-06 10:51:34 -07002440 const int32_t signed_offset = static_cast<int32_t>(offset);
2441 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2442
sfricke-samsung864162a2021-11-01 21:58:01 -07002443 // There are 2 sets of VU being covered where the only main difference is the opcode
2444 if (ImageGatherOperation(opcode)) {
2445 // min/maxTexelGatherOffset
2446 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
2447 skip |=
2448 LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002449 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002450 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002451 module_state->DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002452 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002453 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2454 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002455 skip |= LogError(device, "VUID-RuntimeSpirv-OpImage-06377",
2456 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2457 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32
2458 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002459 module_state->DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002460 phys_dev_props.limits.maxTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002461 }
2462 } else {
2463 // min/maxTexelOffset
2464 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelOffset)) {
2465 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06435",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002466 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsung864162a2021-11-01 21:58:01 -07002467 ") less than VkPhysicalDeviceLimits::minTexelOffset (%" PRIi32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002468 module_state->DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung864162a2021-11-01 21:58:01 -07002469 phys_dev_props.limits.minTexelOffset);
2470 } else if ((offset > phys_dev_props.limits.maxTexelOffset) &&
2471 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002472 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06436",
2473 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2474 ") greater than VkPhysicalDeviceLimits::maxTexelOffset (%" PRIu32 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08002475 module_state->DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002476 phys_dev_props.limits.maxTexelOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002477 }
ziga-lunarga12c75a2021-09-16 16:36:16 +02002478 }
2479 }
2480 }
2481 }
sfricke-samsung3511e312021-11-04 21:14:31 -07002482 index += ImageOperandsParamCount(i);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002483 }
2484 }
2485 }
2486 }
2487 }
2488
2489 return skip;
2490}
2491
sfricke-samsungef15e482022-01-26 11:32:49 -08002492bool CoreChecks::ValidateShaderClock(SHADER_MODULE_STATE const *module_state, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002493 bool skip = false;
2494
sfricke-samsung94167ca2021-02-26 04:14:59 -08002495 switch (insn.opcode()) {
2496 case spv::OpReadClockKHR: {
sfricke-samsungef15e482022-01-26 11:32:49 -08002497 auto scope_id = module_state->get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -08002498 auto scope_type = scope_id.word(3);
2499 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002500 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002501 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002502 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.\n%s",
2503 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
2504 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002505 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002506 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002507 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.\n%s",
2508 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
2509 module_state->DescribeInstruction(insn).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002510 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002511 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002512 }
2513 }
2514 return skip;
2515}
2516
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002517bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2518 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002519 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002520 const auto *pStage = stage_state.create_info;
sfricke-samsungef15e482022-01-26 11:32:49 -08002521 const auto *module_state = stage_state.module_state.get();
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002522 const auto &entrypoint = stage_state.entrypoint;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002523
2524 // to prevent const_cast on pipeline object, just store here as not needed outside function anyway
2525 uint32_t local_size_x = 0;
2526 uint32_t local_size_y = 0;
2527 uint32_t local_size_z = 0;
2528
John Zulauf14c355b2019-06-27 16:09:37 -06002529 // Check the module
sfricke-samsungef15e482022-01-26 11:32:49 -08002530 if (!module_state->has_valid_spirv) {
2531 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2532 "%s does not contain valid spirv for stage %s.",
2533 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
2534 string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002535 }
2536
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002537 // If specialization-constant instructions are present in the shader, the specializations should be applied.
2538 if (module_state->HasSpecConstants()) {
sfricke-samsung5628f982021-10-19 09:21:59 -07002539 // both spirv-opt and spirv-val will use the same flags
2540 spvtools::ValidatorOptions options;
2541 AdjustValidatorOptions(device_extensions, enabled_features, options);
2542
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002543 // setup the call back if the optimizer fails
sfricke-samsung45996a42021-09-16 13:45:27 -07002544 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002545 spvtools::Optimizer optimizer(spirv_environment);
sfricke-samsungef15e482022-01-26 11:32:49 -08002546 spvtools::MessageConsumer consumer = [&skip, &module_state, &stage_state, this](
2547 spv_message_level_t level, const char *source, const spv_position_t &position,
2548 const char *message) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002549 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2550 "%s does not contain valid spirv for stage %s. %s",
sfricke-samsungef15e482022-01-26 11:32:49 -08002551 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002552 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002553 };
2554 optimizer.SetMessageConsumer(consumer);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002555
2556 // The app might be using the default spec constant values, but if they pass values at runtime to the pipeline then need to
2557 // use those values to apply to the spec constants
2558 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2559 pStage->pSpecializationInfo->pMapEntries != nullptr) {
2560 // Gather the specialization-constant values.
2561 auto const &specialization_info = pStage->pSpecializationInfo;
2562 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
2563 std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; // note: this must be std:: to work with spvtools
2564 id_value_map.reserve(specialization_info->mapEntryCount);
2565 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2566 auto const &map_entry = specialization_info->pMapEntries[i];
2567 const auto itr = module_state->GetSpecConstMap().find(map_entry.constantID);
2568 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2569 // behavior of the pipeline."
2570 if (itr != module_state->GetSpecConstMap().cend()) {
2571 // Make sure map_entry.size matches the spec constant's size
2572 uint32_t spec_const_size = decoration_set::kInvalidValue;
2573 const auto def_ins = module_state->get_def(itr->second);
2574 const auto type_ins = module_state->get_def(def_ins.word(1));
2575 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2576 switch (type_ins.opcode()) {
2577 case spv::OpTypeBool:
2578 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2579 spec_const_size = sizeof(VkBool32);
2580 break;
2581 case spv::OpTypeInt:
2582 case spv::OpTypeFloat:
2583 spec_const_size = type_ins.word(2) / 8;
2584 break;
2585 default:
2586 // spirv-val should catch if SpecId is not used on a
2587 // OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant and OpSpecConstant is validated to be a
2588 // OpTypeInt or OpTypeFloat
2589 break;
2590 }
2591
2592 if (map_entry.size != spec_const_size) {
2593 skip |= LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
2594 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2595 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32
2596 " from shader definition.",
2597 map_entry.constantID, i, map_entry.size,
2598 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), spec_const_size);
2599 }
2600 }
2601
2602 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
2603 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2604 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2605 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2606 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2607 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2608
2609 std::copy(start_in_p, end_in_p, out_p);
2610 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
2611 }
2612 }
2613
2614 // This pass takes the runtime spec const values and applies it into the SPIR-V
2615 // will turn a spec constant like
2616 // OpSpecConstant %uint 1
2617 // to a use the value passed in instead (for example if the value is 32) so now it looks like
2618 // OpSpecConstant %uint 32
2619 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2620 }
2621
2622 // This pass will turn OpSpecConstant into a OpConstant (also OpSpecConstantTrue/OpSpecConstantFalse)
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002623 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002624 // Using the new frozen OpConstant all OpSpecConstantComposite can be resolved turning them into OpConstantComposite
2625 // This is need incase a shdaer looks like:
2626 //
2627 // layout(constant_id = 0) const uint x = 64;
2628 // shared uint arr[x > 64 ? 64 : x];
2629 //
2630 // this will generate branch/switch statements that we want to leverage spirv-opt to apply to make parsing easier
2631 optimizer.RegisterPass(spvtools::CreateFoldSpecConstantOpAndCompositePass());
2632
2633 // Apply the specialization-constant values and revalidate the shader module is valid.
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002634 std::vector<uint32_t> specialized_spirv;
sfricke-samsungef15e482022-01-26 11:32:49 -08002635 auto const optimized =
2636 optimizer.Run(module_state->words.data(), module_state->words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002637 if (optimized) {
2638 spv_context ctx = spvContextCreate(spirv_environment);
2639 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2640 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002641 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2642 if (spv_valid != SPV_SUCCESS) {
sfricke-samsungd3793802020-08-18 22:55:03 -07002643 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002644 "After specialization was applied, %s does not contain valid spirv for stage %s.",
sfricke-samsungef15e482022-01-26 11:32:49 -08002645 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002646 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002647 }
2648
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002649 // The new optimized SPIR-V will NOT match the SHADER_MODULE_STATE object parsing, so all additional logical will need
2650 // to be done without any helper functions. This an issue due to each pipeline being able to reuse the same shader
2651 // module but with different spec constant values.
2652 //
2653 // According to https://github.com/KhronosGroup/Vulkan-Docs/issues/1671 anything labeled as "static use" (such as if an
2654 // input is used or not) don't have to be checked post spec constants freezing since the device compiler is not
2655 // guaranteed to run things such as dead-code elimination. The following checks are things that don't follow under
2656 // "static use" rules and need to be validated still.
2657 spirv_inst_iter specialized_it =
2658 spirv_inst_iter(specialized_spirv.begin(), specialized_spirv.begin() + 5); // point to first instruction
2659 layer_data::unordered_map<uint32_t, uint32_t> constant_def_value;
2660
2661 uint32_t workgroup_size_id = 0; // result id can't be zero
2662 uint32_t local_size_id_x = 0;
2663 uint32_t local_size_id_y = 0;
2664 uint32_t local_size_id_z = 0;
2665
2666 // make single interation through new shader
2667 while (specialized_it != specialized_spirv.end()) {
2668 const uint32_t opcode = specialized_it.opcode();
2669
2670 // build a quick access look up for constant values
2671 if (opcode == spv::OpConstant) {
2672 constant_def_value[specialized_it.word(2)] = specialized_it.word(3);
2673 }
2674
2675 if (opcode == spv::OpExecutionModeId && specialized_it.word(2) == spv::ExecutionModeLocalSizeId) {
2676 local_size_id_x = specialized_it.word(3);
2677 local_size_id_y = specialized_it.word(4);
2678 local_size_id_z = specialized_it.word(5);
2679 }
2680
2681 if (opcode == spv::OpDecorate && specialized_it.word(2) == spv::DecorationBuiltIn) {
2682 // Validate applied WorkgroupSize is still below maxComputeWorkGroupSize limit
2683 if (specialized_it.word(3) == spv::BuiltInWorkgroupSize) {
2684 // Will be a OpConstantComposite and always have the OpDecorate section
2685 workgroup_size_id = specialized_it.word(1);
2686 }
2687 }
2688
2689 if (opcode == spv::OpConstantComposite && workgroup_size_id == specialized_it.word(2)) {
2690 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32 so this can be assuemd
2691 local_size_x = constant_def_value.find(specialized_it.word(3))->second;
2692 local_size_y = constant_def_value.find(specialized_it.word(4))->second;
2693 local_size_z = constant_def_value.find(specialized_it.word(5))->second;
2694 }
2695 ++specialized_it;
2696 }
2697
2698 // if after no WorkgroupSize is found, then can apply any possible LocalSizeId due to precedence order
2699 if (local_size_x == 0 && local_size_id_x != 0) {
2700 local_size_x = constant_def_value.find(local_size_id_x)->second;
2701 local_size_y = constant_def_value.find(local_size_id_y)->second;
2702 local_size_z = constant_def_value.find(local_size_id_z)->second;
2703 }
2704
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002705 spvDiagnosticDestroy(diag);
2706 spvContextDestroy(ctx);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002707 } else {
2708 // Should never get here, but better then asserting
2709 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145",
2710 "%s module (stage %s) attempted to apply specialization constants with spirv-opt but failed.",
2711 report_data->FormatHandle(module_state->vk_shader_module()).c_str(),
2712 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002713 }
2714 }
2715
John Zulauf14c355b2019-06-27 16:09:37 -06002716 // Check the entrypoint
sfricke-samsungef15e482022-01-26 11:32:49 -08002717 if (entrypoint == module_state->end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002718 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2719 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002720 }
2721 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2722
2723 // Mark accessible ids
2724 auto &accessible_ids = stage_state.accessible_ids;
2725
Chris Forbes47567b72017-06-09 12:09:45 -07002726 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002727
sfricke-samsung94167ca2021-02-26 04:14:59 -08002728 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2729 // and mainly only checking the instruction in detail for a single operation
ziga-lunarga26b3602021-08-08 15:53:00 +02002730 uint32_t total_shared_size = 0;
sfricke-samsungef15e482022-01-26 11:32:49 -08002731 for (auto insn : *module_state) {
ziga-lunarge1988962021-09-16 13:32:34 +02002732 skip |= ValidateWorkgroupInitialization(module_state, insn);
sfricke-samsungef15e482022-01-26 11:32:49 -08002733 skip |= ValidateTexelOffsetLimits(module_state, insn);
2734 skip |= ValidateShaderCapabilitiesAndExtensions(insn);
2735 skip |= ValidateShaderClock(module_state, insn);
2736 skip |= ValidateShaderStageGroupNonUniform(module_state, pStage->stage, insn);
2737 skip |= ValidateMemoryScope(module_state, insn);
2738 total_shared_size += module_state->CalcComputeSharedMemory(pStage->stage, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08002739
2740 // Checks based off shaderStorageImage(Read|Write)WithoutFormat are
2741 // disabled if VK_KHR_format_feature_flags2 is supported.
2742 //
2743 // https://github.com/KhronosGroup/Vulkan-Docs/blob/6177645341afc/appendices/spirvenv.txt#L553
2744 //
2745 // The other checks need to take into account the format features and so
2746 // we apply that in the descriptor set matching validation code (see
2747 // descriptor_sets.cpp).
2748 if (!has_format_feature2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002749 skip |= ValidateShaderStorageImageFormats(module_state, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08002750 }
ziga-lunarga26b3602021-08-08 15:53:00 +02002751 }
2752
2753 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
sfricke-samsung58caea82022-02-03 15:16:04 -08002754 skip |=
2755 LogError(device, "VUID-RuntimeSpirv-Workgroup-06530",
2756 "Shader uses %" PRIu32
2757 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
2758 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sfricke-samsung94167ca2021-02-26 04:14:59 -08002759 }
2760
sfricke-samsungef15e482022-01-26 11:32:49 -08002761 skip |= ValidateTransformFeedback(module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002762 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2763 stage_state.has_atomic_descriptor);
sfricke-samsungef15e482022-01-26 11:32:49 -08002764 skip |= ValidateShaderStageInputOutputLimits(module_state, pStage, pipeline, entrypoint);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002765 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsungef15e482022-01-26 11:32:49 -08002766 skip |= ValidateAtomicsTypes(module_state);
2767 skip |= ValidateExecutionModes(module_state, entrypoint, pStage->stage, pipeline);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002768 skip |= ValidateSpecializations(pStage);
sfricke-samsungef15e482022-01-26 11:32:49 -08002769 skip |= ValidateDecorations(module_state);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002770 if (check_point_size && !pipeline->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002771 skip |= ValidatePointListShaderState(pipeline, module_state, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002772 }
sfricke-samsungef15e482022-01-26 11:32:49 -08002773 skip |= ValidateBuiltinLimits(module_state, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002774 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002775 skip |= ValidateCooperativeMatrix(module_state, pStage, pipeline);
sfricke-samsungd093e522021-02-26 04:17:45 -08002776 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002777 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002778 skip |= ValidatePrimitiveRateShaderState(pipeline, module_state, entrypoint, pStage->stage);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002779 }
sfricke-samsung45996a42021-09-16 13:45:27 -07002780 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002781 skip |= ValidateShaderResolveQCOM(module_state, pStage, pipeline);
Jeff Leger9b3dcff2021-05-27 15:40:20 -04002782 }
ziga-lunarg73163742021-08-25 13:15:29 +02002783 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
2784 skip |= ValidateShaderSubgroupSizeControl(pStage);
2785 }
Chris Forbes47567b72017-06-09 12:09:45 -07002786
sfricke-samsung7699b912021-04-12 23:01:51 -07002787 // "layout must be consistent with the layout of the * shader"
2788 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002789 std::string vuid_layout_mismatch;
Jeremy Gebben11af9792021-08-20 10:20:09 -06002790 switch (pipeline->create_info.graphics.sType) {
2791 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
2792 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
2793 break;
2794 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
2795 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
2796 break;
2797 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
2798 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
2799 break;
2800 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
2801 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
2802 break;
2803 default:
2804 assert(false);
2805 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002806 }
2807
sfricke-samsung7699b912021-04-12 23:01:51 -07002808 // Validate Push Constants use
sfricke-samsungef15e482022-01-26 11:32:49 -08002809 skip |= ValidatePushConstantUsage(*pipeline, module_state, pStage, vuid_layout_mismatch);
sfricke-samsung7699b912021-04-12 23:01:51 -07002810
Chris Forbes47567b72017-06-09 12:09:45 -07002811 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002812 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07002813 // Verify given pipelineLayout has requested setLayout with requested binding
Jeff Bolze7fc67b2019-10-04 12:29:31 -05002814 const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08002815 uint32_t required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07002816 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
2817 std::set<uint32_t> descriptor_types =
sfricke-samsungef15e482022-01-26 11:32:49 -08002818 TypeToDescriptorTypeSet(module_state, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07002819
2820 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002821 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002822 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002823 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07002824 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002825 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002826 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
2827 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06002828 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
2829 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002830 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002831 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
2832 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002833 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07002834 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06002835 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002836 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002837 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07002838 }
2839 }
2840
2841 // Validate use of input attachments against subpass structure
2842 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002843 auto input_attachment_uses = module_state->CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07002844
amhagana448ea52021-11-02 14:09:14 -04002845 if (!pipeline->rp_state->use_dynamic_rendering) {
2846 auto rpci = pipeline->rp_state->createInfo.ptr();
2847 auto subpass = pipeline->create_info.graphics.subpass;
2848 for (auto use : input_attachment_uses) {
2849 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
2850 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
2851 ? input_attachments[use.first].attachment
2852 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07002853
amhagana448ea52021-11-02 14:09:14 -04002854 if (index == VK_ATTACHMENT_UNUSED) {
2855 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
2856 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsungef15e482022-01-26 11:32:49 -08002857 } else if (!(GetFormatType(rpci->pAttachments[index].format) &
2858 module_state->GetFundamentalType(use.second.type_id))) {
2859 skip |= LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
2860 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
2861 string_VkFormat(rpci->pAttachments[index].format),
2862 module_state->DescribeType(use.second.type_id).c_str());
amhagana448ea52021-11-02 14:09:14 -04002863 }
Chris Forbes47567b72017-06-09 12:09:45 -07002864 }
2865 }
2866 }
Lockeaa8fdc02019-04-02 11:59:20 -06002867 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002868 skip |= ValidateComputeWorkGroupSizes(module_state, entrypoint, stage_state, local_size_x, local_size_y, local_size_z);
Lockeaa8fdc02019-04-02 11:59:20 -06002869 }
ziga-lunarg73163742021-08-25 13:15:29 +02002870
Chris Forbes47567b72017-06-09 12:09:45 -07002871 return skip;
2872}
2873
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07002874bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint,
2875 shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer,
2876 spirv_inst_iter consumer_entrypoint,
2877 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07002878 bool skip = false;
2879
2880 auto outputs =
sfricke-samsung962cad92021-04-13 00:46:29 -07002881 producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
2882 auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07002883
2884 auto a_it = outputs.begin();
2885 auto b_it = inputs.begin();
2886
ziga-lunarg8346fe82021-08-22 17:30:50 +02002887 uint32_t a_component = 0;
2888 uint32_t b_component = 0;
2889
Chris Forbes47567b72017-06-09 12:09:45 -07002890 // Maps sorted by key (location); walk them together to find mismatches
2891 while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) {
2892 bool a_at_end = outputs.size() == 0 || a_it == outputs.end();
2893 bool b_at_end = inputs.size() == 0 || b_it == inputs.end();
2894 auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first;
2895 auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first;
2896
ziga-lunarg8346fe82021-08-22 17:30:50 +02002897 a_first.second += a_component;
2898 b_first.second += b_component;
2899
2900 const auto a_length = a_at_end ? 0 : producer->GetNumComponentsInBaseType(producer->get_def(a_it->second.type_id));
2901 const auto b_length = b_at_end ? 0 : consumer->GetNumComponentsInBaseType(consumer->get_def(b_it->second.type_id));
2902 assert(a_at_end || a_component < a_length);
2903 assert(b_at_end || b_component < b_length);
2904
Chris Forbes47567b72017-06-09 12:09:45 -07002905 if (b_at_end || ((!a_at_end) && (a_first < b_first))) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002906 skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002907 "%s writes to output location %" PRIu32 ".%" PRIu32 " which is not consumed by %s",
2908 producer_stage->name, a_first.first, a_first.second, consumer_stage->name);
2909 if ((b_first.first > a_first.first) || b_at_end || (a_component + 1 == a_length)) {
2910 a_it++;
2911 a_component = 0;
2912 } else {
2913 a_component++;
2914 }
Chris Forbes47567b72017-06-09 12:09:45 -07002915 } else if (a_at_end || a_first > b_first) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002916 skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002917 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
2918 b_first.first, b_first.second, producer_stage->name);
2919 if ((a_first.first > b_first.first) || a_at_end || (b_component + 1 == b_length)) {
2920 b_it++;
2921 b_component = 0;
2922 } else {
2923 b_component++;
2924 }
Chris Forbes47567b72017-06-09 12:09:45 -07002925 } else {
2926 // subtleties of arrayed interfaces:
2927 // - if is_patch, then the member is not arrayed, even though the interface may be.
2928 // - if is_block_member, then the extra array level of an arrayed interface is not
2929 // expressed in the member type -- it's expressed in the block type.
ziga-lunarg8346fe82021-08-22 17:30:50 +02002930 if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id)) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002931 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002932 "Type mismatch on location %" PRIu32 ".%" PRIu32 ": '%s' vs '%s'", a_first.first, a_first.second,
sfricke-samsung962cad92021-04-13 00:46:29 -07002933 producer->DescribeType(a_it->second.type_id).c_str(),
2934 consumer->DescribeType(b_it->second.type_id).c_str());
ziga-lunarg8346fe82021-08-22 17:30:50 +02002935 a_it++;
2936 b_it++;
2937 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07002938 }
2939 if (a_it->second.is_patch != b_it->second.is_patch) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002940 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002941 "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage",
2942 a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name,
2943 b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002944 }
2945 if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002946 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarg8346fe82021-08-22 17:30:50 +02002947 "Decoration mismatch on location %" PRIu32 ".%" PRIu32 ": %s and %s stages differ in precision",
2948 a_first.first, a_first.second, producer_stage->name, consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07002949 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02002950 uint32_t a_remaining = a_length - a_component;
2951 uint32_t b_remaining = b_length - b_component;
2952 if (a_remaining == b_remaining) { // Sizes match so we can advance both a_it and b_it
2953 a_it++;
2954 b_it++;
2955 a_component = 0;
2956 b_component = 0;
2957 } else if (a_remaining > b_remaining) { // a has more components remaining
2958 a_component += b_remaining;
2959 b_component = 0;
2960 b_it++;
2961 } else if (b_remaining > a_remaining) { // b has more components remaining
2962 b_component += a_remaining;
2963 a_component = 0;
2964 a_it++;
2965 }
Chris Forbes47567b72017-06-09 12:09:45 -07002966 }
2967 }
2968
Ari Suonpaa696b3432019-03-11 14:02:57 +02002969 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sfricke-samsung962cad92021-04-13 00:46:29 -07002970 auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
2971 auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002972
2973 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
2974 if (builtins_producer.size() != builtins_consumer.size()) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002975 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002976 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002977 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
2978 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02002979 } else {
2980 auto it_producer = builtins_producer.begin();
2981 auto it_consumer = builtins_consumer.begin();
2982 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
2983 if (*it_producer != *it_consumer) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002984 skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002985 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
2986 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02002987 break;
2988 }
2989 it_producer++;
2990 it_consumer++;
2991 }
2992 }
2993 }
2994 }
2995
Chris Forbes47567b72017-06-09 12:09:45 -07002996 return skip;
2997}
2998
John Zulauf14c355b2019-06-27 16:09:37 -06002999static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003000 uint32_t stage_mask = 0;
3001 if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
3002 for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) {
3003 stage_mask |= pCreateInfo->pStages[i].stage;
3004 }
3005 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05003006 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
3007 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
3008 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003009 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
3010 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
3011 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
3012 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
3013 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003014 }
3015 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003016 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003017}
3018
Chris Forbes47567b72017-06-09 12:09:45 -07003019// Validate that the shaders used by the given pipeline and store the active_slots
3020// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06003021bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003022 const auto create_info = pipeline->create_info.graphics.ptr();
Chris Forbes47567b72017-06-09 12:09:45 -07003023
Chris Forbes47567b72017-06-09 12:09:45 -07003024 bool skip = false;
3025
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003026 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003027
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003028 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
3029 for (auto &stage : pipeline->stage_state) {
3030 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
3031 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
3032 vertex_stage = &stage;
3033 }
3034 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
3035 fragment_stage = &stage;
3036 }
Chris Forbes47567b72017-06-09 12:09:45 -07003037 }
3038
3039 // if the shader stages are no good individually, cross-stage validation is pointless.
3040 if (skip) return true;
3041
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003042 auto vi = create_info->pVertexInputState;
Chris Forbes47567b72017-06-09 12:09:45 -07003043
3044 if (vi) {
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003045 skip |= ValidateViConsistency(vi);
Chris Forbes47567b72017-06-09 12:09:45 -07003046 }
3047
sfricke-samsungef15e482022-01-26 11:32:49 -08003048 if (vertex_stage && vertex_stage->module_state->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
3049 skip |= ValidateViAgainstVsInputs(vi, vertex_stage->module_state.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07003050 }
3051
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003052 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
3053 const auto &producer = pipeline->stage_state[i - 1];
3054 const auto &consumer = pipeline->stage_state[i];
sfricke-samsungef15e482022-01-26 11:32:49 -08003055 assert(producer.module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003056 if (&producer == fragment_stage) {
3057 break;
3058 }
sfricke-samsungef15e482022-01-26 11:32:49 -08003059 if (consumer.module_state) {
3060 if (consumer.module_state->has_valid_spirv && producer.module_state->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003061 auto producer_id = GetShaderStageId(producer.stage_flag);
3062 auto consumer_id = GetShaderStageId(consumer.stage_flag);
sfricke-samsungef15e482022-01-26 11:32:49 -08003063 skip |= ValidateInterfaceBetweenStages(producer.module_state.get(), producer.entrypoint,
3064 &shader_stage_attribs[producer_id], consumer.module_state.get(),
3065 consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08003066 }
Chris Forbes47567b72017-06-09 12:09:45 -07003067 }
3068 }
3069
sfricke-samsungef15e482022-01-26 11:32:49 -08003070 if (fragment_stage && fragment_stage->module_state->has_valid_spirv) {
Aaron Hagan1209c782021-11-22 19:37:14 -05003071 if (pipeline->rp_state->use_dynamic_rendering) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003072 skip |= ValidateFsOutputsAgainstDynamicRenderingRenderPass(fragment_stage->module_state.get(),
3073 fragment_stage->entrypoint, pipeline);
Aaron Hagan1209c782021-11-22 19:37:14 -05003074 } else {
sfricke-samsungef15e482022-01-26 11:32:49 -08003075 skip |= ValidateFsOutputsAgainstRenderPass(fragment_stage->module_state.get(), fragment_stage->entrypoint, pipeline,
Aaron Hagan1209c782021-11-22 19:37:14 -05003076 create_info->subpass);
3077 }
Chris Forbes47567b72017-06-09 12:09:45 -07003078 }
3079
3080 return skip;
3081}
3082
Tony-LunarGb2ded512021-02-02 16:03:30 -07003083bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
3084 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07003085 bool skip = false;
3086
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003087 for (auto &stage : pipeline->stage_state) {
3088 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
3089 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07003090 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
3091 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben3dfeacf2021-12-02 08:46:39 -07003092 if (stage.wrote_primitive_shading_rate) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00003093 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003094 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00003095 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
3096 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
3097 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003098 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00003099 }
3100 }
3101 }
3102 }
3103
3104 return skip;
3105}
3106
sfricke-samsunge72a85e2020-02-29 21:48:37 -08003107bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003108 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07003109}
Chris Forbes4ae55b32017-06-09 14:42:56 -07003110
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003111uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const {
3112 uint32_t total = 0;
Jeremy Gebben11af9792021-08-20 10:20:09 -06003113 const auto &create_info = pipeline->create_info.raytracing;
3114 const auto *stages = create_info.ptr()->pStages;
3115 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003116 if (stages[stage_index].stage == stageBit) {
3117 total++;
3118 }
3119 }
3120
Jeremy Gebben11af9792021-08-20 10:20:09 -06003121 if (create_info.pLibraryInfo) {
3122 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003123 auto library_pipeline = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Jeremy Gebben9f537102021-10-05 16:37:12 -06003124 total += CalcShaderStageCount(library_pipeline.get(), stageBit);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003125 }
3126 }
3127
3128 return total;
3129}
3130
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003131bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE *pipeline, uint32_t group, uint32_t stage) const {
3132 if (group == VK_SHADER_UNUSED_NV) {
3133 return true;
3134 }
3135
3136 const auto &create_info = pipeline->create_info.raytracing;
3137 const auto *stages = create_info.ptr()->pStages;
3138
3139 if (group < create_info.stageCount) {
3140 return (stages[group].stage & stage) != 0;
3141 }
3142 group -= create_info.stageCount;
3143
3144 // Search libraries
3145 if (create_info.pLibraryInfo) {
3146 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06003147 auto library_pipeline = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003148 const uint32_t stage_count = library_pipeline->create_info.raytracing.ptr()->stageCount;
3149 if (group < stage_count) {
3150 return (library_pipeline->create_info.raytracing.ptr()->pStages[group].stage & stage) != 0;
3151 }
3152 group -= stage_count;
3153 }
3154 }
3155
3156 // group index too large
3157 return false;
3158}
3159
sourav parmarcd5fb182020-07-17 12:58:44 -07003160bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06003161 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04003162
Jeremy Gebben11af9792021-08-20 10:20:09 -06003163 const auto &create_info = pipeline->create_info.raytracing;
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003164 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003165 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
3166 skip |=
3167 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
3168 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
3169 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
3170 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003171 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003172 if (create_info.pLibraryInfo) {
3173 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003174 auto library_pipelinestate = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Jeremy Gebben11af9792021-08-20 10:20:09 -06003175 const auto &library_create_info = library_pipelinestate->create_info.raytracing;
3176 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003177 skip |= LogError(
3178 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
3179 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
3180 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003181 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07003182 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003183 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
3184 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
3185 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
3186 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003187 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
3188 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
3189 "member must have been created with values of the maxPipelineRayPayloadSize and "
3190 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
3191 }
3192 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06003193 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003194 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
3195 "vkCreateRayTracingPipelinesKHR: If flags includes "
3196 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
3197 "the pLibraries member of libraries must have been created with the "
3198 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
3199 }
sourav parmar83c31b12020-05-06 12:30:54 -07003200 }
3201 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003202 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003203 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003204 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
3205 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
3206 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003207 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003208 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003209 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003210 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04003211
Jeremy Gebben11af9792021-08-20 10:20:09 -06003212 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003213 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003214 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003215
Jeremy Gebben11af9792021-08-20 10:20:09 -06003216 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003217 const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
3218 if (raygen_stages_count == 0) {
3219 skip |= LogError(
3220 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07003221 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003222 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
3223 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003224 }
3225
Jeremy Gebben11af9792021-08-20 10:20:09 -06003226 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003227 const auto &group = groups[group_index];
3228
3229 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003230 if (!GroupHasValidIndex(
3231 pipeline, group.generalShader,
3232 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 -05003233 skip |= LogError(device,
3234 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
3235 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
3236 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003237 }
3238 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3239 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003240 skip |= LogError(device,
3241 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
3242 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
3243 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003244 }
3245 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003246 if (!GroupHasValidIndex(pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003247 skip |= LogError(device,
3248 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
3249 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
3250 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003251 }
3252 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3253 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003254 skip |= LogError(device,
3255 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
3256 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
3257 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003258 }
3259 }
3260
3261 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3262 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003263 if (!GroupHasValidIndex(pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003264 skip |= LogError(device,
3265 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
3266 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
3267 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003268 }
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003269 if (!GroupHasValidIndex(pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003270 skip |= LogError(device,
3271 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
3272 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
3273 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003274 }
3275 }
John Zulaufe4474e72019-07-01 17:28:27 -06003276 }
3277 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003278}
3279
Dave Houltona9df0ce2018-02-07 10:51:23 -07003280uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003281
Dave Houltona9df0ce2018-02-07 10:51:23 -07003282static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003283 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06003284 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003285 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003286 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003287 return nullptr;
3288}
3289
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003290bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003291 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003292 bool skip = false;
3293 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003294
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06003295 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003296 return false;
3297 }
3298
sfricke-samsung45996a42021-09-16 13:45:27 -07003299 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003300
3301 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003302 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
3303 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3304 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003305 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003306 auto cache = GetValidationCacheInfo(pCreateInfo);
3307 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07003308 // If app isn't using a shader validation cache, use the default one from CoreChecks
3309 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003310 if (cache) {
3311 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003312 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003313 }
3314
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003315 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3316 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07003317 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06003318 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003319 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003320 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003321 spvtools::ValidatorOptions options;
3322 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06003323 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003324 if (spv_valid != SPV_SUCCESS) {
3325 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003326 if (spv_valid == SPV_WARNING) {
3327 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3328 diag && diag->error ? diag->error : "(no error text)");
3329 } else {
3330 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3331 diag && diag->error ? diag->error : "(no error text)");
3332 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003333 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003334 } else {
3335 if (cache) {
3336 cache->Insert(hash);
3337 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003338 }
3339
3340 spvDiagnosticDestroy(diag);
3341 spvContextDestroy(ctx);
3342 }
3343
Chris Forbes4ae55b32017-06-09 14:42:56 -07003344 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003345}
3346
sfricke-samsungef15e482022-01-26 11:32:49 -08003347bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *module_state, const spirv_inst_iter &entrypoint,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003348 const PipelineStageState &stage_state, uint32_t local_size_x, uint32_t local_size_y,
3349 uint32_t local_size_z) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003350 bool skip = false;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003351 // If spec constants were used then the local size are already found if possible
3352 if (local_size_x == 0) {
3353 if (!module_state->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
3354 return skip; // no local size found
Lockeaa8fdc02019-04-02 11:59:20 -06003355 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003356 }
Lockeaa8fdc02019-04-02 11:59:20 -06003357
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003358 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
3359 skip |= LogError(module_state->vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
3360 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
3361 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
3362 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
3363 }
3364 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
3365 skip |= LogError(module_state->vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
3366 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
3367 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
3368 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
3369 }
3370 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
3371 skip |= LogError(module_state->vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
3372 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
3373 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
3374 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
3375 }
3376
3377 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3378 uint64_t invocations = local_size_x * local_size_y;
3379 // Prevent overflow.
3380 bool fail = false;
3381 if (invocations > UINT32_MAX || invocations > limit) {
3382 fail = true;
3383 }
3384 if (!fail) {
3385 invocations *= local_size_z;
Lockeaa8fdc02019-04-02 11:59:20 -06003386 if (invocations > UINT32_MAX || invocations > limit) {
3387 fail = true;
3388 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003389 }
3390 if (fail) {
3391 skip |= LogError(module_state->vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
3392 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3393 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
3394 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x, local_size_y,
3395 local_size_z, limit);
3396 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003397
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003398 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3399 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
3400 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3401 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3402 skip |= LogError(
3403 module_state->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
3404 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3405 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3406 "dimension (%" PRIu32
3407 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
3408 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
3409 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3410 }
3411 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3412 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
3413 const auto *required_subgroup_size_features =
3414 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
3415 if (!required_subgroup_size_features) {
3416 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02003417 skip |= LogError(
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003418 module_state->vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
3419 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3420 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3421 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3422 ").",
sfricke-samsungef15e482022-01-26 11:32:49 -08003423 report_data->FormatHandle(module_state->vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003424 phys_dev_props_core11.subgroupSize);
ziga-lunarg11fecb92021-09-20 16:48:06 +02003425 }
3426 }
Lockeaa8fdc02019-04-02 11:59:20 -06003427 }
3428 return skip;
3429}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003430
3431spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
Tony-LunarGe67fcc22022-01-03 16:40:53 -07003432 if (api_version >= VK_API_VERSION_1_3) {
3433 return SPV_ENV_VULKAN_1_3;
3434 } else if (api_version >= VK_API_VERSION_1_2) {
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003435 return SPV_ENV_VULKAN_1_2;
3436 } else if (api_version >= VK_API_VERSION_1_1) {
3437 if (spirv_1_4) {
3438 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3439 } else {
3440 return SPV_ENV_VULKAN_1_1;
3441 }
3442 }
3443 return SPV_ENV_VULKAN_1_0;
3444}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003445
sfricke-samsungecc112a2021-09-03 05:32:17 -07003446// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003447void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003448 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003449 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3450 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003451 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003452 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003453 options.SetRelaxBlockLayout(true);
3454 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003455
3456 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3457 // Vulkan version used, the feature bit is needed (also described in the spec).
3458
3459 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3460 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003461 options.SetUniformBufferStandardLayout(true);
3462 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003463 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3464 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003465 options.SetScalarBlockLayout(true);
3466 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003467 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3468 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003469 options.SetWorkgroupScalarBlockLayout(true);
3470 }
Tony-LunarG273f32f2021-09-28 08:56:30 -06003471 if (enabled_features.core13.maintenance4) {
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003472 // --allow-localsizeid
3473 options.SetAllowLocalSizeId(true);
3474 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003475}