blob: 8db9236f261f632b37b63d64c54ad1b02d9ae5b6 [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
sjfricke4f600c82022-06-09 14:21:32 +090051static const spirv_inst_iter GetBaseTypeIter(const SHADER_MODULE_STATE &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
sjfricke4f600c82022-06-09 14:21:32 +090057static bool BaseTypesMatch(const SHADER_MODULE_STATE &a, const SHADER_MODULE_STATE &b, const spirv_inst_iter &a_base_insn,
ziga-lunarg8346fe82021-08-22 17:30:50 +020058 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);
sjfricke10f74a82022-08-18 18:12:56 +090068 } else if (a_opcode == spv::OpTypeBool) {
69 return true;
ziga-lunarg8346fe82021-08-22 17:30:50 +020070 } else if (a_opcode == spv::OpTypeStruct) {
71 // Match on all element types
72 if (a_base_insn.len() != b_base_insn.len()) {
73 return false; // Structs cannot match if member counts differ
74 }
75
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020076 for (uint32_t i = 2; i < a_base_insn.len(); i++) {
77 const auto &c_base_insn = GetBaseTypeIter(a, a_base_insn.word(i));
78 const auto &d_base_insn = GetBaseTypeIter(b, b_base_insn.word(i));
79 if (!BaseTypesMatch(a, b, c_base_insn, d_base_insn)) {
ziga-lunarg8346fe82021-08-22 17:30:50 +020080 return false;
81 }
82 }
83
84 return true;
85 }
86 }
87 return false;
Chris Forbes47567b72017-06-09 12:09:45 -070088}
89
sjfricke4f600c82022-06-09 14:21:32 +090090static bool TypesMatch(const SHADER_MODULE_STATE &a, const SHADER_MODULE_STATE &b, uint32_t a_type, uint32_t b_type) {
ziga-lunarg19fc6ae2021-09-09 00:05:19 +020091 const auto &a_base_insn = GetBaseTypeIter(a, a_type);
92 const auto &b_base_insn = GetBaseTypeIter(b, b_type);
Chris Forbes47567b72017-06-09 12:09:45 -070093
ziga-lunarg8346fe82021-08-22 17:30:50 +020094 return BaseTypesMatch(a, b, a_base_insn, b_base_insn);
Chris Forbes47567b72017-06-09 12:09:45 -070095}
96
sfricke-samsung7fac88a2022-01-26 11:44:22 -080097static uint32_t GetLocationsConsumedByFormat(VkFormat format) {
Chris Forbes47567b72017-06-09 12:09:45 -070098 switch (format) {
99 case VK_FORMAT_R64G64B64A64_SFLOAT:
100 case VK_FORMAT_R64G64B64A64_SINT:
101 case VK_FORMAT_R64G64B64A64_UINT:
102 case VK_FORMAT_R64G64B64_SFLOAT:
103 case VK_FORMAT_R64G64B64_SINT:
104 case VK_FORMAT_R64G64B64_UINT:
105 return 2;
106 default:
107 return 1;
108 }
109}
110
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800111static uint32_t GetFormatType(VkFormat fmt) {
sfricke-samsunge3086292021-11-18 23:02:35 -0800112 if (FormatIsSINT(fmt)) return FORMAT_TYPE_SINT;
113 if (FormatIsUINT(fmt)) return FORMAT_TYPE_UINT;
sfricke-samsunged028b02021-09-06 23:14:51 -0700114 // Formats such as VK_FORMAT_D16_UNORM_S8_UINT are both
Dave Houltona9df0ce2018-02-07 10:51:23 -0700115 if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT;
116 if (fmt == VK_FORMAT_UNDEFINED) return 0;
Chris Forbes47567b72017-06-09 12:09:45 -0700117 // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader.
118 return FORMAT_TYPE_FLOAT;
119}
120
Shannon McPhersonc06c33d2018-06-28 17:21:12 -0600121static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) {
Chris Forbes47567b72017-06-09 12:09:45 -0700122 uint32_t bit_pos = uint32_t(u_ffs(stage));
123 return bit_pos - 1;
124}
125
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700126bool CoreChecks::ValidateViConsistency(safe_VkPipelineVertexInputStateCreateInfo const *vi) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700127 // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should
128 // be specified only once.
Jeremy Gebbencbf22862021-03-03 12:01:22 -0700129 layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings;
Chris Forbes47567b72017-06-09 12:09:45 -0700130 bool skip = false;
131
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800132 for (uint32_t i = 0; i < vi->vertexBindingDescriptionCount; i++) {
Chris Forbes47567b72017-06-09 12:09:45 -0700133 auto desc = &vi->pVertexBindingDescriptions[i];
134 auto &binding = bindings[desc->binding];
135 if (binding) {
Dave Houlton78d09922018-05-17 15:48:45 -0600136 // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps?
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700137 skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d",
138 desc->binding);
Chris Forbes47567b72017-06-09 12:09:45 -0700139 } else {
140 binding = desc;
141 }
142 }
143
144 return skip;
145}
146
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700147bool CoreChecks::ValidateViAgainstVsInputs(safe_VkPipelineVertexInputStateCreateInfo const *vi,
sjfricke4f600c82022-06-09 14:21:32 +0900148 const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700149 bool skip = false;
150
sjfricke4f600c82022-06-09 14:21:32 +0900151 const auto inputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false);
Chris Forbes47567b72017-06-09 12:09:45 -0700152
153 // Build index by location
Petr Kraus25810d02019-08-27 17:41:15 +0200154 std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs;
Chris Forbes47567b72017-06-09 12:09:45 -0700155 if (vi) {
Petr Kraus25810d02019-08-27 17:41:15 +0200156 for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) {
157 const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format);
158 for (uint32_t j = 0; j < num_locations; ++j) {
Chris Forbes47567b72017-06-09 12:09:45 -0700159 attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i];
160 }
161 }
162 }
163
Petr Kraus25810d02019-08-27 17:41:15 +0200164 struct AttribInputPair {
165 const VkVertexInputAttributeDescription *attrib = nullptr;
166 const interface_var *input = nullptr;
167 };
168 std::map<uint32_t, AttribInputPair> location_map;
169 for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second;
170 for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second;
Chris Forbes47567b72017-06-09 12:09:45 -0700171
Jamie Madillc1f7ca82020-03-16 17:08:26 -0400172 for (const auto &location_it : location_map) {
Petr Kraus25810d02019-08-27 17:41:15 +0200173 const auto location = location_it.first;
174 const auto attrib = location_it.second.attrib;
175 const auto input = location_it.second.input;
Mark Lobodzinski7caa39c2018-07-25 15:48:34 -0600176
Petr Kraus25810d02019-08-27 17:41:15 +0200177 if (attrib && !input) {
sjfricke4f600c82022-06-09 14:21:32 +0900178 skip |= LogPerformanceWarning(module_state.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700179 "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200180 } else if (!attrib && input) {
sjfricke4f600c82022-06-09 14:21:32 +0900181 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700182 "Vertex shader consumes input at location %" PRIu32 " but not provided", location);
Petr Kraus25810d02019-08-27 17:41:15 +0200183 } else if (attrib && input) {
184 const auto attrib_type = GetFormatType(attrib->format);
sjfricke4f600c82022-06-09 14:21:32 +0900185 const auto input_type = module_state.GetFundamentalType(input->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700186
187 // Type checking
188 if (!(attrib_type & input_type)) {
sjfricke4f600c82022-06-09 14:21:32 +0900189 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700190 "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`",
sjfricke4f600c82022-06-09 14:21:32 +0900191 string_VkFormat(attrib->format), location, module_state.DescribeType(input->type_id).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700192 }
Petr Kraus25810d02019-08-27 17:41:15 +0200193 } else { // !attrib && !input
194 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700195 }
196 }
197
198 return skip;
199}
200
sjfricke4f600c82022-06-09 14:21:32 +0900201bool CoreChecks::ValidateFsOutputsAgainstDynamicRenderingRenderPass(const SHADER_MODULE_STATE &module_state,
sfricke-samsungef15e482022-01-26 11:32:49 -0800202 spirv_inst_iter entrypoint,
203 PIPELINE_STATE const *pipeline) const {
Aaron Hagan1209c782021-11-22 19:37:14 -0500204 bool skip = false;
205
206 struct Attachment {
207 const interface_var* output = nullptr;
208 };
209 std::map<uint32_t, Attachment> location_map;
210
211 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
sjfricke4f600c82022-06-09 14:21:32 +0900212 const auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Aaron Hagan1209c782021-11-22 19:37:14 -0500213 for (const auto& output_it : outputs) {
214 auto const location = output_it.first.first;
215 location_map[location].output = &output_it.second;
216 }
217
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700218 const auto ms_state = pipeline->MultisampleState();
219 const bool alpha_to_coverage_enabled = ms_state && (ms_state->alphaToCoverageEnable == VK_TRUE);
Aaron Hagan1209c782021-11-22 19:37:14 -0500220
Aaron Haganaca50442021-12-07 22:26:29 -0500221 for (uint32_t location = 0; location < location_map.size(); ++location) {
Aaron Hagan1209c782021-11-22 19:37:14 -0500222 const auto output = location_map[location].output;
223
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700224 const auto &rp_state = pipeline->RenderPassState();
225 const auto &attachments = pipeline->Attachments();
226 if (!output && location < attachments.size() && attachments[location].colorWriteMask != 0) {
227 skip |= LogWarning(
sjfricke4f600c82022-06-09 14:21:32 +0900228 module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700229 "Attachment %" PRIu32 " not written by fragment shader; undefined values will be written to attachment", location);
230 } else if (output && (location < rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount)) {
231 auto format = rp_state->dynamic_rendering_pipeline_create_info.pColorAttachmentFormats[location];
232 const auto attachment_type = GetFormatType(format);
sjfricke4f600c82022-06-09 14:21:32 +0900233 const auto output_type = module_state.GetFundamentalType(output->type_id);
Aaron Hagan1209c782021-11-22 19:37:14 -0500234
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700235 // Type checking
236 if (!(output_type & attachment_type)) {
237 skip |=
sjfricke4f600c82022-06-09 14:21:32 +0900238 LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700239 "Attachment %" PRIu32
240 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sjfricke4f600c82022-06-09 14:21:32 +0900241 location, string_VkFormat(format), module_state.DescribeType(output->type_id).c_str());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700242 }
243 }
Aaron Hagan1209c782021-11-22 19:37:14 -0500244 }
245
246 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sjfricke4f600c82022-06-09 14:21:32 +0900247 bool location_zero_has_alpha = output_zero && module_state.get_def(output_zero->type_id) != module_state.end() &&
248 module_state.GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Aaron Hagan1209c782021-11-22 19:37:14 -0500249 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sjfricke4f600c82022-06-09 14:21:32 +0900250 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
sfricke-samsungef15e482022-01-26 11:32:49 -0800251 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Aaron Hagan1209c782021-11-22 19:37:14 -0500252 }
253
254 return skip;
Aaron Hagan1209c782021-11-22 19:37:14 -0500255}
256
sjfricke4f600c82022-06-09 14:21:32 +0900257bool CoreChecks::ValidateFsOutputsAgainstRenderPass(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -0700258 PIPELINE_STATE const *pipeline, uint32_t subpass_index) const {
Petr Kraus25810d02019-08-27 17:41:15 +0200259 bool skip = false;
Chris Forbes8bca1652017-07-20 11:10:09 -0700260
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600261 struct Attachment {
Mike Schuchardt2df08912020-12-15 16:28:09 -0800262 const VkAttachmentReference2 *reference = nullptr;
263 const VkAttachmentDescription2 *attachment = nullptr;
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600264 const interface_var *output = nullptr;
265 };
266 std::map<uint32_t, Attachment> location_map;
267
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700268 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -0600269 if (rp_state && !rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700270 const auto rpci = rp_state->createInfo.ptr();
amhagana448ea52021-11-02 14:09:14 -0400271 const auto subpass = rpci->pSubpasses[subpass_index];
272 for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) {
273 auto const &reference = subpass.pColorAttachments[i];
274 location_map[i].reference = &reference;
275 if (reference.attachment != VK_ATTACHMENT_UNUSED &&
276 rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) {
277 location_map[i].attachment = &rpci->pAttachments[reference.attachment];
278 }
Chris Forbes47567b72017-06-09 12:09:45 -0700279 }
280 }
281
Chris Forbes47567b72017-06-09 12:09:45 -0700282 // TODO: dual source blend index (spv::DecIndex, zero if not provided)
283
sjfricke4f600c82022-06-09 14:21:32 +0900284 const auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
Jeremy Hayes3699c7c2019-10-09 12:24:55 -0600285 for (const auto &output_it : outputs) {
286 auto const location = output_it.first.first;
287 location_map[location].output = &output_it.second;
288 }
Chris Forbes47567b72017-06-09 12:09:45 -0700289
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700290 const auto *ms_state = pipeline->MultisampleState();
291 const bool alpha_to_coverage_enabled = ms_state && (ms_state->alphaToCoverageEnable == VK_TRUE);
Chris Forbes47567b72017-06-09 12:09:45 -0700292
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700293 // Don't check any color attachments if rasterization is disabled
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700294 const auto raster_state = pipeline->RasterizationState();
Nathaniel Cesario81257cb2022-02-16 17:15:58 -0700295 if (raster_state && !raster_state->rasterizerDiscardEnable) {
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700296 for (const auto &location_it : location_map) {
297 const auto reference = location_it.second.reference;
298 if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) {
299 continue;
Petr Kraus25810d02019-08-27 17:41:15 +0200300 }
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700301
302 const auto location = location_it.first;
303 const auto attachment = location_it.second.attachment;
304 const auto output = location_it.second.output;
305 if (attachment && !output) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700306 const auto &attachments = pipeline->Attachments();
307 if (location < attachments.size() && attachments[location].colorWriteMask != 0) {
sjfricke4f600c82022-06-09 14:21:32 +0900308 skip |= LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700309 "Attachment %" PRIu32
310 " not written by fragment shader; undefined values will be written to attachment",
311 location);
312 }
313 } else if (!attachment && output) {
314 if (!(alpha_to_coverage_enabled && location == 0)) {
315 skip |=
sjfricke4f600c82022-06-09 14:21:32 +0900316 LogWarning(module_state.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700317 "fragment shader writes to output location %" PRIu32 " with no matching attachment", location);
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700318 }
319 } else if (attachment && output) {
320 const auto attachment_type = GetFormatType(attachment->format);
sjfricke4f600c82022-06-09 14:21:32 +0900321 const auto output_type = module_state.GetFundamentalType(output->type_id);
Chris Forbes47567b72017-06-09 12:09:45 -0700322
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700323 // Type checking
324 if (!(output_type & attachment_type)) {
325 skip |= LogWarning(
sjfricke4f600c82022-06-09 14:21:32 +0900326 module_state.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700327 "Attachment %" PRIu32
328 " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined",
sjfricke4f600c82022-06-09 14:21:32 +0900329 location, string_VkFormat(attachment->format), module_state.DescribeType(output->type_id).c_str());
Nathaniel Cesariobcb86652022-01-27 14:40:20 -0700330 }
331 } else { // !attachment && !output
332 assert(false); // at least one exists in the map
Chris Forbes47567b72017-06-09 12:09:45 -0700333 }
Chris Forbes47567b72017-06-09 12:09:45 -0700334 }
335 }
336
Petr Kraus25810d02019-08-27 17:41:15 +0200337 const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr;
sjfricke4f600c82022-06-09 14:21:32 +0900338 bool location_zero_has_alpha = output_zero && module_state.get_def(output_zero->type_id) != module_state.end() &&
339 module_state.GetComponentsConsumedByType(output_zero->type_id, false) == 4;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700340 if (alpha_to_coverage_enabled && !location_zero_has_alpha) {
sjfricke4f600c82022-06-09 14:21:32 +0900341 skip |= LogError(module_state.vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700342 "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled.");
Ari Suonpaa412b23b2019-02-26 07:56:58 +0200343 }
344
Chris Forbes47567b72017-06-09 12:09:45 -0700345 return skip;
346}
347
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600348PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update,
349 const shader_struct_member &push_constant_used_in_shader,
350 uint32_t &out_issue_index) const {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600351 const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes();
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600352 const auto used_bytes_size = used_bytes->size();
353 if (used_bytes_size == 0) return PC_Byte_Updated;
354
355 const auto push_constant_data_update_size = push_constant_data_update.size();
356 const auto *data = push_constant_data_update.data();
357 if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) {
358 if (used_bytes_size <= push_constant_data_update_size) {
359 return PC_Byte_Updated;
360 }
361 const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size;
362
363 const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size;
364 if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) {
365 return PC_Byte_Updated;
366 }
locke-lunargde3f0fa2020-09-10 11:55:31 -0600367 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600368
locke-lunargde3f0fa2020-09-10 11:55:31 -0600369 uint32_t i = 0;
370 for (const auto used : *used_bytes) {
371 if (used) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600372 if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600373 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600374 return PC_Byte_Not_Set;
375 } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600376 out_issue_index = i;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600377 return PC_Byte_Not_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600378 }
379 }
380 ++i;
381 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600382 return PC_Byte_Updated;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600383}
384
sjfricke4f600c82022-06-09 14:21:32 +0900385bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700386 safe_VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700387 bool skip = false;
sfricke-samsung5c65b372021-03-25 05:39:57 -0700388 // Temp workaround to prevent false positive errors
389 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sjfricke4f600c82022-06-09 14:21:32 +0900390 if (module_state.HasMultipleEntryPoints()) {
sfricke-samsung5c65b372021-03-25 05:39:57 -0700391 return skip;
392 }
393
Chris Forbes47567b72017-06-09 12:09:45 -0700394 // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step.
sjfricke4f600c82022-06-09 14:21:32 +0900395 const auto *entrypoint = module_state.FindEntrypointStruct(pStage->pName, pStage->stage);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600396 if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) {
397 return skip;
398 }
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700399 const auto &pipeline_layout = pipeline.PipelineLayoutState();
400 std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline_layout->push_constant_ranges.get();
Chris Forbes47567b72017-06-09 12:09:45 -0700401
locke-lunargde3f0fa2020-09-10 11:55:31 -0600402 bool found_stage = false;
403 for (auto const &range : *push_constant_ranges) {
404 if (range.stageFlags & pStage->stage) {
405 found_stage = true;
406 std::string location_desc;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600407 std::vector<uint8_t> push_constant_bytes_set;
locke-lunargde3f0fa2020-09-10 11:55:31 -0600408 if (range.offset > 0) {
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600409 push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600410 }
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600411 push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated);
locke-lunargde3f0fa2020-09-10 11:55:31 -0600412 uint32_t issue_index = 0;
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600413 const auto ret =
414 ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index);
Chris Forbes47567b72017-06-09 12:09:45 -0700415
locke-lunarg3d8b8f32020-10-26 17:04:16 -0600416 if (ret == PC_Byte_Not_Set) {
locke-lunargde3f0fa2020-09-10 11:55:31 -0600417 const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index);
sjfricke4f600c82022-06-09 14:21:32 +0900418 LogObjectList objlist(module_state.vk_shader_module());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700419 objlist.add(pipeline_layout->layout());
sfricke-samsung7699b912021-04-12 23:01:51 -0700420 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 -0600421 string_VkShaderStageFlags(pStage->stage).c_str(),
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700422 report_data->FormatHandle(pipeline_layout->layout()).c_str());
locke-lunargde3f0fa2020-09-10 11:55:31 -0600423 break;
Chris Forbes47567b72017-06-09 12:09:45 -0700424 }
425 }
426 }
427
locke-lunargde3f0fa2020-09-10 11:55:31 -0600428 if (!found_stage) {
sjfricke4f600c82022-06-09 14:21:32 +0900429 LogObjectList objlist(module_state.vk_shader_module());
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700430 objlist.add(pipeline_layout->layout());
431 skip |= LogError(
432 objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.",
sjfricke4f600c82022-06-09 14:21:32 +0900433 string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700434 report_data->FormatHandle(pipeline_layout->layout()).c_str(), string_VkShaderStageFlags(pStage->stage).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -0700435 }
Chris Forbes47567b72017-06-09 12:09:45 -0700436 return skip;
437}
438
sjfricke4f600c82022-06-09 14:21:32 +0900439bool CoreChecks::ValidateBuiltinLimits(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint) const {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700440 bool skip = false;
441
442 // Currently all builtin tested are only found in fragment shaders
sfricke-samsungcfb44592021-07-25 00:36:28 -0700443 if (entrypoint.word(1) != spv::ExecutionModelFragment) {
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700444 return skip;
445 }
446
sfricke-samsungcfb44592021-07-25 00:36:28 -0700447 // Find all builtin from just the interface variables
448 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
sjfricke4f600c82022-06-09 14:21:32 +0900449 auto insn = module_state.get_def(id);
sfricke-samsungcfb44592021-07-25 00:36:28 -0700450 assert(insn.opcode() == spv::OpVariable);
sjfricke4f600c82022-06-09 14:21:32 +0900451 const decoration_set decorations = module_state.get_decorations(insn.word(2));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700452
sfricke-samsungcfb44592021-07-25 00:36:28 -0700453 // Currently don't need to search in structs
454 if (((decorations.flags & decoration_set::builtin_bit) != 0) && (decorations.builtin == spv::BuiltInSampleMask)) {
sjfricke4f600c82022-06-09 14:21:32 +0900455 auto type_pointer = module_state.get_def(insn.word(1));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700456 assert(type_pointer.opcode() == spv::OpTypePointer);
457
sjfricke4f600c82022-06-09 14:21:32 +0900458 auto type = module_state.get_def(type_pointer.word(3));
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700459 if (type.opcode() == spv::OpTypeArray) {
sjfricke4f600c82022-06-09 14:21:32 +0900460 uint32_t length = static_cast<uint32_t>(module_state.GetConstantValueById(type.word(3)));
sfricke-samsungcfb44592021-07-25 00:36:28 -0700461 // Handles both the input and output sampleMask
462 if (length > phys_dev_props.limits.maxSampleMaskWords) {
463 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711",
464 "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds "
465 "maxSampleMaskWords of %u in %s.",
466 length, phys_dev_props.limits.maxSampleMaskWords,
sjfricke4f600c82022-06-09 14:21:32 +0900467 report_data->FormatHandle(module_state.vk_shader_module()).c_str());
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700468 }
sfricke-samsungcfb44592021-07-25 00:36:28 -0700469 break;
sfricke-samsungef2a68c2020-10-26 04:22:46 -0700470 }
471 }
472 }
473
474 return skip;
475}
476
Chris Forbes47567b72017-06-09 12:09:45 -0700477// Validate that data for each specialization entry is fully contained within the buffer.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700478bool CoreChecks::ValidateSpecializations(safe_VkPipelineShaderStageCreateInfo const *info) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700479 bool skip = false;
480
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700481 const auto *spec = info->pSpecializationInfo;
Chris Forbes47567b72017-06-09 12:09:45 -0700482
483 if (spec) {
484 for (auto i = 0u; i < spec->mapEntryCount; i++) {
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600485 if (spec->pMapEntries[i].offset >= spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700486 skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773",
487 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200488 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700489 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
490 spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize);
Jeremy Hayes6c555c32019-09-09 17:14:09 -0600491
492 continue;
493 }
Chris Forbes47567b72017-06-09 12:09:45 -0700494 if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700495 skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774",
496 "Specialization entry %u (for constant id %u) references memory outside provided specialization "
Petr Krausb0d5e592021-05-21 23:37:11 +0200497 "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700498 i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset,
499 spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize);
Chris Forbes47567b72017-06-09 12:09:45 -0700500 }
ziga-lunargae2a5c42021-07-23 16:18:09 +0200501 for (uint32_t j = i + 1; j < spec->mapEntryCount; ++j) {
502 if (spec->pMapEntries[i].constantID == spec->pMapEntries[j].constantID) {
503 skip |= LogError(device, "VUID-VkSpecializationInfo-constantID-04911",
504 "Specialization entry %" PRIu32 " and %" PRIu32 " have the same constantID (%" PRIu32 ").", i,
505 j, spec->pMapEntries[i].constantID);
506 }
507 }
Chris Forbes47567b72017-06-09 12:09:45 -0700508 }
509 }
510
511 return skip;
512}
513
Jeff Bolz38b3ce72018-09-19 12:53:38 -0500514// TODO (jbolz): Can this return a const reference?
sjfricke4f600c82022-06-09 14:21:32 +0900515static std::set<uint32_t> TypeToDescriptorTypeSet(const SHADER_MODULE_STATE &module_state, uint32_t type_id,
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800516 uint32_t &descriptor_count, bool is_khr) {
sjfricke4f600c82022-06-09 14:21:32 +0900517 auto type = module_state.get_def(type_id);
Chris Forbes9f89d752018-03-07 12:57:48 -0800518 bool is_storage_buffer = false;
Chris Forbes47567b72017-06-09 12:09:45 -0700519 descriptor_count = 1;
Jeff Bolze54ae892018-09-08 12:16:29 -0500520 std::set<uint32_t> ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700521
522 // 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 -0500523 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) {
524 if (type.opcode() == spv::OpTypeRuntimeArray) {
525 descriptor_count = 0;
sjfricke4f600c82022-06-09 14:21:32 +0900526 type = module_state.get_def(type.word(2));
Jeff Bolzfdf96072018-04-10 14:32:18 -0500527 } else if (type.opcode() == spv::OpTypeArray) {
sjfricke4f600c82022-06-09 14:21:32 +0900528 descriptor_count *= module_state.GetConstantValueById(type.word(3));
529 type = module_state.get_def(type.word(2));
Chris Forbes47567b72017-06-09 12:09:45 -0700530 } else {
Chris Forbes9f89d752018-03-07 12:57:48 -0800531 if (type.word(2) == spv::StorageClassStorageBuffer) {
532 is_storage_buffer = true;
533 }
sjfricke4f600c82022-06-09 14:21:32 +0900534 type = module_state.get_def(type.word(3));
Chris Forbes47567b72017-06-09 12:09:45 -0700535 }
536 }
537
538 switch (type.opcode()) {
539 case spv::OpTypeStruct: {
sjfricke4f600c82022-06-09 14:21:32 +0900540 for (const auto insn : module_state.GetDecorationInstructions()) {
sfricke-samsung94d71a52021-02-26 05:25:43 -0800541 if (insn.word(1) == type.word(1)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700542 if (insn.word(2) == spv::DecorationBlock) {
Chris Forbes9f89d752018-03-07 12:57:48 -0800543 if (is_storage_buffer) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500544 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
545 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
546 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800547 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500548 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
549 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC);
550 ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);
551 return ret;
Chris Forbes9f89d752018-03-07 12:57:48 -0800552 }
Chris Forbes47567b72017-06-09 12:09:45 -0700553 } else if (insn.word(2) == spv::DecorationBufferBlock) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500554 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER);
555 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
556 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700557 }
558 }
559 }
560
561 // Invalid
Jeff Bolze54ae892018-09-08 12:16:29 -0500562 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700563 }
564
565 case spv::OpTypeSampler:
Jeff Bolze54ae892018-09-08 12:16:29 -0500566 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER);
567 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
568 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700569
Chris Forbes73c00bf2018-06-22 16:28:06 -0700570 case spv::OpTypeSampledImage: {
571 // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel
572 // buffer descriptor doesn't really provide one. Allow this slight mismatch.
sjfricke4f600c82022-06-09 14:21:32 +0900573 auto image_type = module_state.get_def(type.word(2));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700574 auto dim = image_type.word(3);
575 auto sampled = image_type.word(7);
576 if (dim == spv::DimBuffer && sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500577 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
578 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700579 }
Chris Forbes73c00bf2018-06-22 16:28:06 -0700580 }
Jeff Bolze54ae892018-09-08 12:16:29 -0500581 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
582 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700583
584 case spv::OpTypeImage: {
585 // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler.
586 // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable.
587 auto dim = type.word(3);
588 auto sampled = type.word(7);
589
590 if (dim == spv::DimSubpassData) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500591 ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
592 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700593 } else if (dim == spv::DimBuffer) {
594 if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500595 ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER);
596 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700597 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500598 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
599 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700600 }
601 } else if (sampled == 1) {
Jeff Bolze54ae892018-09-08 12:16:29 -0500602 ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
603 ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
604 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700605 } else {
Jeff Bolze54ae892018-09-08 12:16:29 -0500606 ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE);
607 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700608 }
609 }
Shannon McPherson0fa28232018-11-01 11:59:02 -0600610 case spv::OpTypeAccelerationStructureNV:
sourav parmarcd5fb182020-07-17 12:58:44 -0700611 is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
612 : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV);
Jeff Bolz105d6492018-09-29 15:46:44 -0500613 return ret;
Chris Forbes47567b72017-06-09 12:09:45 -0700614
615 // We shouldn't really see any other junk types -- but if we do, they're a mismatch.
616 default:
Jeff Bolze54ae892018-09-08 12:16:29 -0500617 return ret; // Matches nothing
Chris Forbes47567b72017-06-09 12:09:45 -0700618 }
619}
620
Jeff Bolze54ae892018-09-08 12:16:29 -0500621static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) {
Chris Forbes73c00bf2018-06-22 16:28:06 -0700622 std::stringstream ss;
Jeff Bolze54ae892018-09-08 12:16:29 -0500623 for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) {
624 if (ss.tellp()) ss << ", ";
625 ss << string_VkDescriptorType(VkDescriptorType(*it));
Chris Forbes73c00bf2018-06-22 16:28:06 -0700626 }
627 return ss.str();
628}
629
sfricke-samsung0065ce02020-12-03 22:46:37 -0800630bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500631 if (!check) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800632 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 -0500633 return true;
634 }
635 }
636
637 return false;
638}
639
sfricke-samsung0065ce02020-12-03 22:46:37 -0800640bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const {
Chris Forbes47567b72017-06-09 12:09:45 -0700641 if (!feature) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800642 if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) {
Chris Forbes47567b72017-06-09 12:09:45 -0700643 return true;
644 }
645 }
646
647 return false;
648}
649
locke-lunarg63e4daf2020-08-17 17:53:25 -0600650bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor,
651 bool has_atomic_descriptor) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500652 bool skip = false;
653
locke-lunarg63e4daf2020-08-17 17:53:25 -0600654 if (has_writable_descriptor || has_atomic_descriptor) {
Chris Forbes349b3132018-03-07 11:38:08 -0800655 switch (stage) {
Chris Forbes349b3132018-03-07 11:38:08 -0800656 case VK_SHADER_STAGE_FRAGMENT_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800657 skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700658 "VUID-RuntimeSpirv-NonWritable-06340");
Chris Forbes349b3132018-03-07 11:38:08 -0800659 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800660 case VK_SHADER_STAGE_VERTEX_BIT:
661 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
662 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
663 case VK_SHADER_STAGE_GEOMETRY_BIT:
sfricke-samsung0065ce02020-12-03 22:46:37 -0800664 skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700665 "VUID-RuntimeSpirv-NonWritable-06341");
Chris Forbes349b3132018-03-07 11:38:08 -0800666 break;
sfricke-samsunged00aa42022-01-27 19:03:01 -0800667 default:
668 // No feature requirements for writes and atomics for other stages
669 break;
Chris Forbes349b3132018-03-07 11:38:08 -0800670 }
671 }
672
Chris Forbes47567b72017-06-09 12:09:45 -0700673 return skip;
674}
675
sjfricke4f600c82022-06-09 14:21:32 +0900676bool CoreChecks::ValidateShaderStageGroupNonUniform(const SHADER_MODULE_STATE &module_state, VkShaderStageFlagBits stage,
sfricke-samsung94167ca2021-02-26 04:14:59 -0800677 spirv_inst_iter &insn) const {
Jeff Bolzee743412019-06-20 22:24:32 -0500678 bool skip = false;
679
sfricke-samsung94167ca2021-02-26 04:14:59 -0800680 // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations)
681 if (GroupOperation(insn.opcode()) == true) {
682 // Check the quad operations.
683 if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) {
684 if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700685 skip |=
686 RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages,
687 "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", "VUID-RuntimeSpirv-None-06342");
sfricke-samsung0065ce02020-12-03 22:46:37 -0800688 }
sfricke-samsung94167ca2021-02-26 04:14:59 -0800689 }
Jeff Bolz526f2d52019-09-18 13:18:08 -0500690
sfricke-samsung94167ca2021-02-26 04:14:59 -0800691 uint32_t scope_type = spv::ScopeMax;
692 if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) {
693 // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand
694 scope_type = spv::ScopeSubgroup;
695 } else {
696 // "All <id> used for Scope <id> must be of an OpConstant"
sjfricke4f600c82022-06-09 14:21:32 +0900697 auto scope_id = module_state.get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800698 scope_type = scope_id.word(3);
699 }
sfricke-samsung0065ce02020-12-03 22:46:37 -0800700
sfricke-samsung94167ca2021-02-26 04:14:59 -0800701 if (scope_type == spv::ScopeSubgroup) {
702 // "Group operations with subgroup scope" must have stage support
703 const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages;
704 skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage),
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700705 "VkPhysicalDeviceSubgroupProperties::supportedStages", "VUID-RuntimeSpirv-None-06343");
sfricke-samsung94167ca2021-02-26 04:14:59 -0800706 }
707
708 if (!enabled_features.core12.shaderSubgroupExtendedTypes) {
sjfricke4f600c82022-06-09 14:21:32 +0900709 auto type = module_state.get_def(insn.word(1));
sfricke-samsung94167ca2021-02-26 04:14:59 -0800710
711 if (type.opcode() == spv::OpTypeVector) {
712 // Get the element type
sjfricke4f600c82022-06-09 14:21:32 +0900713 type = module_state.get_def(type.word(2));
sfricke-samsung0065ce02020-12-03 22:46:37 -0800714 }
715
sfricke-samsung94167ca2021-02-26 04:14:59 -0800716 if (type.opcode() != spv::OpTypeBool) {
sfricke-samsung0065ce02020-12-03 22:46:37 -0800717 // Both OpTypeInt and OpTypeFloat the width is in the 2nd word.
718 const uint32_t width = type.word(2);
Jeff Bolz526f2d52019-09-18 13:18:08 -0500719
sfricke-samsung0065ce02020-12-03 22:46:37 -0800720 if ((type.opcode() == spv::OpTypeFloat && width == 16) ||
721 (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) {
722 skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes,
723 "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes",
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700724 "VUID-RuntimeSpirv-None-06275");
Jeff Bolz526f2d52019-09-18 13:18:08 -0500725 }
726 }
727 }
Jeff Bolzee743412019-06-20 22:24:32 -0500728 }
729
730 return skip;
731}
732
sjfricke4f600c82022-06-09 14:21:32 +0900733bool CoreChecks::ValidateMemoryScope(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &insn) const {
ziga-lunarg70651522021-10-11 17:23:30 +0200734 bool skip = false;
735
sfricke-samsung3a25ed52022-01-20 02:24:36 -0800736 const auto &entry = OpcodeMemoryScopePosition(insn.opcode());
ziga-lunarg70651522021-10-11 17:23:30 +0200737 if (entry > 0) {
738 const uint32_t scope_id = insn.word(entry);
sjfricke4f600c82022-06-09 14:21:32 +0900739 const auto &scope_def = module_state.GetConstantDef(scope_id);
740 if (scope_def != module_state.end()) {
sjfricke3b0cb102022-08-10 16:27:45 +0900741 const auto scope_type = module_state.GetConstantValue(scope_def);
sfricke-samsunged00aa42022-01-27 19:03:01 -0800742 if (enabled_features.core12.vulkanMemoryModel && !enabled_features.core12.vulkanMemoryModelDeviceScope &&
743 scope_type == spv::Scope::ScopeDevice) {
744 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06265",
745 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is enabled and "
746 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModelDeviceScope is disabled, but\n%s\nuses "
747 "Device memory scope.",
sjfricke4f600c82022-06-09 14:21:32 +0900748 module_state.DescribeInstruction(insn).c_str());
sfricke-samsunged00aa42022-01-27 19:03:01 -0800749 } else if (!enabled_features.core12.vulkanMemoryModel && scope_type == spv::Scope::ScopeQueueFamily) {
750 skip |= LogError(device, "VUID-RuntimeSpirv-vulkanMemoryModel-06266",
751 "VkPhysicalDeviceVulkan12Features::vulkanMemoryModel is not enabled, but\n%s\nuses "
752 "QueueFamily memory scope.",
sjfricke4f600c82022-06-09 14:21:32 +0900753 module_state.DescribeInstruction(insn).c_str());
ziga-lunarg70651522021-10-11 17:23:30 +0200754 }
755 }
756 }
757
758 return skip;
759}
760
sjfricke4f600c82022-06-09 14:21:32 +0900761bool CoreChecks::ValidateShaderStageInputOutputLimits(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700762 safe_VkPipelineShaderStageCreateInfo const *pStage,
763 const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200764 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS ||
765 pStage->stage == VK_SHADER_STAGE_ALL) {
766 return false;
767 }
768
769 bool skip = false;
Mark Lobodzinski518eadc2019-03-09 12:07:30 -0700770 auto const &limits = phys_dev_props.limits;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200771
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700772 std::set<uint32_t> patch_i_ds;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200773 struct Variable {
774 uint32_t baseTypePtrID;
775 uint32_t ID;
776 uint32_t storageClass;
777 };
778 std::vector<Variable> variables;
779
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700780 uint32_t num_vertices = 0;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700781 bool is_iso_lines = false;
782 bool is_point_mode = false;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500783
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700784 auto entrypoint_variables = FindEntrypointInterfaces(entrypoint);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600785
sjfricke4f600c82022-06-09 14:21:32 +0900786 for (auto insn : module_state) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200787 switch (insn.opcode()) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500788 // Find all Patch decorations
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200789 case spv::OpDecorate:
790 switch (insn.word(2)) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500791 case spv::DecorationPatch: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700792 patch_i_ds.insert(insn.word(1));
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200793 break;
794 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200795 default:
796 break;
797 }
798 break;
799 // Find all input and output variables
800 case spv::OpVariable: {
801 Variable var = {};
802 var.storageClass = insn.word(3);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600803 if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) &&
804 // Only include variables in the entrypoint's interface
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700805 find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) {
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200806 var.baseTypePtrID = insn.word(1);
807 var.ID = insn.word(2);
808 variables.push_back(var);
809 }
810 break;
811 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500812 case spv::OpExecutionMode:
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800813 case spv::OpExecutionModeId:
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500814 if (insn.word(1) == entrypoint.word(2)) {
815 switch (insn.word(2)) {
816 default:
817 break;
818 case spv::ExecutionModeOutputVertices:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700819 num_vertices = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500820 break;
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700821 case spv::ExecutionModeIsolines:
822 is_iso_lines = true;
823 break;
824 case spv::ExecutionModePointMode:
825 is_point_mode = true;
826 break;
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500827 }
828 }
829 break;
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200830 default:
831 break;
832 }
833 }
834
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500835 bool strip_output_array_level =
836 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV);
837 bool strip_input_array_level =
838 (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT ||
839 pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT);
840
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700841 uint32_t num_comp_in = 0, num_comp_out = 0;
842 int max_comp_in = 0, max_comp_out = 0;
Jeff Bolzf234bf82019-11-04 14:07:15 -0600843
sjfricke4f600c82022-06-09 14:21:32 +0900844 auto inputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level);
845 auto outputs = module_state.CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600846
847 // Find max component location used for input variables.
848 for (auto &var : inputs) {
849 int location = var.first.first;
850 int component = var.first.second;
851 interface_var &iv = var.second;
852
853 // Only need to look at the first location, since we use the type's whole size
854 if (iv.offset != 0) {
855 continue;
856 }
857
858 if (iv.is_patch) {
859 continue;
860 }
861
sjfricke4f600c82022-06-09 14:21:32 +0900862 int num_components = module_state.GetComponentsConsumedByType(iv.type_id, strip_input_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700863 max_comp_in = std::max(max_comp_in, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600864 }
865
866 // Find max component location used for output variables.
867 for (auto &var : outputs) {
868 int location = var.first.first;
869 int component = var.first.second;
870 interface_var &iv = var.second;
871
872 // Only need to look at the first location, since we use the type's whole size
873 if (iv.offset != 0) {
874 continue;
875 }
876
877 if (iv.is_patch) {
878 continue;
879 }
880
sjfricke4f600c82022-06-09 14:21:32 +0900881 int num_components = module_state.GetComponentsConsumedByType(iv.type_id, strip_output_array_level);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700882 max_comp_out = std::max(max_comp_out, location * 4 + component + num_components);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600883 }
884
885 // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar),
886 // but that doesn't include builtins.
sfricke-samsung406766a2021-07-02 12:04:09 -0700887 // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know
888 // how many components were from builtins as it might not be obvious
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200889 for (auto &var : variables) {
Jeff Bolze9ee3d82019-05-29 13:45:13 -0500890 // Check if the variable is a patch. Patches can also be members of blocks,
891 // but if they are then the top-level arrayness has already been stripped
892 // by the time GetComponentsConsumedByType gets to it.
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700893 bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end();
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200894
895 if (var.storageClass == spv::StorageClassInput) {
sjfricke4f600c82022-06-09 14:21:32 +0900896 num_comp_in += module_state.GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200897 } else { // var.storageClass == spv::StorageClassOutput
sjfricke4f600c82022-06-09 14:21:32 +0900898 num_comp_out += module_state.GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200899 }
900 }
901
902 switch (pStage->stage) {
903 case VK_SHADER_STAGE_VERTEX_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700904 if (num_comp_out > limits.maxVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700905 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700906 "Invalid Pipeline CreateInfo State: Vertex shader exceeds "
907 "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u "
908 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700909 limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200910 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700911 if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700912 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700913 "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that "
914 "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)",
915 limits.maxVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600916 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200917 break;
918
919 case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700920 if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700921 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700922 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
923 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u "
924 "components by %u components",
925 limits.maxTessellationControlPerVertexInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700926 num_comp_in - limits.maxTessellationControlPerVertexInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200927 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700928 if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600929 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700930 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700931 "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that "
932 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)",
933 limits.maxTessellationControlPerVertexInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600934 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700935 if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700936 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700937 "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds "
938 "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u "
939 "components by %u components",
940 limits.maxTessellationControlPerVertexOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700941 num_comp_out - limits.maxTessellationControlPerVertexOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200942 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700943 if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600944 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700945 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700946 "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that "
947 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)",
948 limits.maxTessellationControlPerVertexOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600949 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200950 break;
951
952 case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700953 if (num_comp_in > limits.maxTessellationEvaluationInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700954 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700955 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
956 "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u "
957 "components by %u components",
958 limits.maxTessellationEvaluationInputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700959 num_comp_in - limits.maxTessellationEvaluationInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200960 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700961 if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600962 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700963 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700964 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that "
965 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)",
966 limits.maxTessellationEvaluationInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600967 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700968 if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700969 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700970 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds "
971 "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u "
972 "components by %u components",
973 limits.maxTessellationEvaluationOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700974 num_comp_out - limits.maxTessellationEvaluationOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200975 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700976 if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) {
Jeff Bolzf234bf82019-11-04 14:07:15 -0600977 skip |=
sfricke-samsung782b9cb2021-09-16 23:53:32 -0700978 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -0700979 "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that "
980 "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)",
981 limits.maxTessellationEvaluationOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -0600982 }
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700983 // Portability validation
984 if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) {
985 if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700986 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06326",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700987 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
988 " is using abstract patch type IsoLines, but this is not supported on this platform");
989 }
990 if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -0700991 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-tessellationShader-06327",
Nathaniel Cesario75fb7222020-12-07 10:54:53 -0700992 "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader"
993 " is using abstract patch type PointMode, but this is not supported on this platform");
994 }
995 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +0200996 break;
997
998 case VK_SHADER_STAGE_GEOMETRY_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700999 if (num_comp_in > limits.maxGeometryInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001000 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001001 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1002 "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u "
1003 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001004 limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001005 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001006 if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) {
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 input variable uses location that "
1009 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)",
1010 limits.maxGeometryInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001011 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001012 if (num_comp_out > limits.maxGeometryOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001013 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001014 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1015 "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u "
1016 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001017 limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001018 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001019 if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) {
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 output variable uses location that "
1022 "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)",
1023 limits.maxGeometryOutputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001024 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001025 if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001026 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001027 "Invalid Pipeline CreateInfo State: Geometry shader exceeds "
1028 "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u "
1029 "components by %u components",
1030 limits.maxGeometryTotalOutputComponents,
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001031 num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001032 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001033 break;
1034
1035 case VK_SHADER_STAGE_FRAGMENT_BIT:
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001036 if (num_comp_in > limits.maxFragmentInputComponents) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001037 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001038 "Invalid Pipeline CreateInfo State: Fragment shader exceeds "
1039 "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u "
1040 "components by %u components",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001041 limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents);
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001042 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001043 if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) {
sfricke-samsung782b9cb2021-09-16 23:53:32 -07001044 skip |= LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-Location-06272",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001045 "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that "
1046 "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)",
1047 limits.maxFragmentInputComponents);
Jeff Bolzf234bf82019-11-04 14:07:15 -06001048 }
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001049 break;
1050
sjfricke62366d32022-08-01 21:04:10 +09001051 case VK_SHADER_STAGE_RAYGEN_BIT_KHR:
1052 case VK_SHADER_STAGE_ANY_HIT_BIT_KHR:
1053 case VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR:
1054 case VK_SHADER_STAGE_MISS_BIT_KHR:
1055 case VK_SHADER_STAGE_INTERSECTION_BIT_KHR:
1056 case VK_SHADER_STAGE_CALLABLE_BIT_KHR:
Jeff Bolz148d94e2018-12-13 21:25:56 -06001057 case VK_SHADER_STAGE_TASK_BIT_NV:
1058 case VK_SHADER_STAGE_MESH_BIT_NV:
1059 break;
1060
Daniel Fedai Larsenc939abc2018-08-07 10:01:58 +02001061 default:
1062 assert(false); // This should never happen
1063 }
1064 return skip;
1065}
1066
sjfricke4f600c82022-06-09 14:21:32 +09001067bool CoreChecks::ValidateShaderStorageImageFormats(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &insn) const {
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001068 bool skip = false;
1069
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001070 switch (insn.opcode()) {
1071 // Go through all ImageRead/Write instructions
1072 case spv::OpImageSparseRead:
1073 case spv::OpImageRead: {
1074 // spirv-val validates this is an OpTypeImage
sjfricke4f600c82022-06-09 14:21:32 +09001075 const uint32_t image = module_state.GetTypeId(insn.word(3));
1076 const spirv_inst_iter image_def = module_state.get_def(image);
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001077
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001078 const uint32_t dim = image_def.word(3);
1079 const uint32_t image_format = image_def.word(8);
1080 // If the Image Dim operand is not SubpassData, the Image Format must not be Unknown, unless the
1081 // StorageImageReadWithoutFormat Capability was declared.
1082 if (dim != spv::DimSubpassData && image_format == spv::ImageFormatUnknown) {
1083 skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat,
1084 "shaderStorageImageReadWithoutFormat", kVUID_Features_shaderStorageImageReadWithoutFormat);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001085 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001086 break;
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001087 }
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001088 case spv::OpImageWrite: {
1089 // spirv-val validates this is an OpTypeImage
sjfricke4f600c82022-06-09 14:21:32 +09001090 const uint32_t image = module_state.GetTypeId(insn.word(1));
1091 const spirv_inst_iter image_def = module_state.get_def(image);
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001092
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001093 const uint32_t image_format = image_def.word(8);
1094 if (image_format == spv::ImageFormatUnknown) {
1095 skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat,
1096 "shaderStorageImageWriteWithoutFormat", kVUID_Features_shaderStorageImageWriteWithoutFormat);
1097 }
1098 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001099 }
1100
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001101 // Go through all variables for images and check decorations
1102 case spv::OpVariable: {
1103 // spirv-val validates this is an OpTypePointer
sjfricke4f600c82022-06-09 14:21:32 +09001104 const spirv_inst_iter pointer_def = module_state.get_def(insn.word(1));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001105 if (pointer_def.word(2) != spv::StorageClassUniformConstant) {
1106 break; // Vulkan Spec says storage image must be UniformConstant
1107 }
sjfricke4f600c82022-06-09 14:21:32 +09001108 spirv_inst_iter type_def = module_state.get_def(pointer_def.word(3));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001109
1110 // Unpack an optional level of arraying
1111 if (type_def.opcode() == spv::OpTypeArray || type_def.opcode() == spv::OpTypeRuntimeArray) {
sjfricke4f600c82022-06-09 14:21:32 +09001112 type_def = module_state.get_def(type_def.word(2));
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001113 }
1114
sjfricke4f600c82022-06-09 14:21:32 +09001115 if (type_def != module_state.end() && type_def.opcode() == spv::OpTypeImage) {
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001116 // Only check if the Image Dim operand is not SubpassData
1117 const uint32_t dim = type_def.word(3);
1118 // Only check storage images
1119 const uint32_t sampled = type_def.word(7);
1120 const uint32_t image_format = type_def.word(8);
1121 if ((dim == spv::DimSubpassData) || (sampled != 2) || (image_format != spv::ImageFormatUnknown)) {
1122 break;
1123 }
1124
1125 const uint32_t var_id = insn.word(2);
sjfricke4f600c82022-06-09 14:21:32 +09001126 decoration_set img_decorations = module_state.get_decorations(var_id);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001127
1128 if (!enabled_features.core.shaderStorageImageReadWithoutFormat &&
1129 !(img_decorations.flags & decoration_set::nonreadable_bit)) {
1130 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06270",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001131 "shaderStorageImageReadWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith Unknown "
1132 "format and is not decorated with NonReadable",
sjfricke4f600c82022-06-09 14:21:32 +09001133 module_state.DescribeInstruction(module_state.get_def(var_id)).c_str(),
1134 module_state.DescribeInstruction(type_def).c_str());
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001135 }
1136
1137 if (!enabled_features.core.shaderStorageImageWriteWithoutFormat &&
1138 !(img_decorations.flags & decoration_set::nonwritable_bit)) {
1139 skip |= LogError(device, "VUID-RuntimeSpirv-OpTypeImage-06269",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001140 "shaderStorageImageWriteWithoutFormat is not supported but\n%s\nhas an Image\n%s\nwith "
1141 "Unknown format and is not decorated with NonWritable",
sjfricke4f600c82022-06-09 14:21:32 +09001142 module_state.DescribeInstruction(module_state.get_def(var_id)).c_str(),
1143 module_state.DescribeInstruction(type_def).c_str());
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001144 }
1145 }
1146 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +03001147 }
1148 }
1149
Lionel Landwerlin892d6c32021-05-05 12:56:19 +03001150 return skip;
1151}
1152
sfricke-samsungdc96f302020-03-18 20:42:10 -07001153bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
1154 bool skip = false;
1155 uint32_t total_resources = 0;
1156
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001157 const auto &rp_state = pipeline->RenderPassState();
1158 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) && rp_state) {
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06001159 if (rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001160 total_resources += rp_state->dynamic_rendering_pipeline_create_info.colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001161 } else {
1162 // "For the fragment shader stage the framebuffer color attachments also count against this limit"
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001163 total_resources += rp_state->createInfo.pSubpasses[pipeline->Subpass()].colorAttachmentCount;
amhagana448ea52021-11-02 14:09:14 -04001164 }
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
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001169 const auto &layout_state = pipeline->PipelineLayoutState();
1170 if (layout_state) {
1171 for (auto set_layout : layout_state->set_layouts) {
1172 if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) {
1173 continue;
1174 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001175
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001176 for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) {
1177 const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx);
1178 // Bindings with a descriptorCount of 0 are "reserved" and should be skipped
1179 if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) {
1180 // Check only descriptor types listed in maxPerStageResources description in spec
1181 switch (binding->descriptorType) {
1182 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
1183 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
1184 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
1185 case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
1186 case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
1187 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
1188 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
1189 case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
1190 case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
1191 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
1192 total_resources += binding->descriptorCount;
1193 break;
1194 default:
1195 break;
1196 }
sfricke-samsungdc96f302020-03-18 20:42:10 -07001197 }
1198 }
1199 }
1200 }
1201
1202 if (total_resources > phys_dev_props.limits.maxPerStageResources) {
ziga-lunarg7d53c822022-05-08 23:06:10 +02001203 const char *vuid = nullptr;
1204 if (stage == VK_SHADER_STAGE_COMPUTE_BIT) {
1205 vuid = "VUID-VkComputePipelineCreateInfo-layout-01687";
1206 } else if ((stage & VK_SHADER_STAGE_ALL_GRAPHICS) == 0) {
1207 vuid = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03428";
1208 } else {
1209 vuid = "VUID-VkGraphicsPipelineCreateInfo-layout-01688";
1210 }
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001211 skip |= LogError(pipeline->pipeline(), vuid,
sfricke-samsungdc96f302020-03-18 20:42:10 -07001212 "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit "
1213 "VkPhysicalDeviceLimits::maxPerStageResources (%u)",
1214 string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources);
1215 }
1216
1217 return skip;
1218}
1219
Jeff Bolze4356752019-03-07 11:23:46 -06001220// copy the specialization constant value into buf, if it is present
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001221template <typename StageCreateInfo>
1222void GetSpecConstantValue(StageCreateInfo const *pStage, uint32_t spec_id, void *buf) {
1223 const auto *spec = pStage->pSpecializationInfo;
Jeff Bolze4356752019-03-07 11:23:46 -06001224
1225 if (spec && spec_id < spec->mapEntryCount) {
1226 memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size);
1227 }
1228}
1229
1230// Fill in value with the constant or specialization constant value, if available.
1231// Returns true if the value has been accurately filled out.
sjfricke4f600c82022-06-09 14:21:32 +09001232static bool GetIntConstantValue(spirv_inst_iter insn, const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001233 safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001234 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) {
sjfricke4f600c82022-06-09 14:21:32 +09001235 auto type_id = module_state.get_def(insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001236 if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) {
1237 return false;
1238 }
1239 switch (insn.opcode()) {
1240 case spv::OpSpecConstant:
1241 *value = insn.word(3);
1242 GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value);
1243 return true;
1244 case spv::OpConstant:
1245 *value = insn.word(3);
1246 return true;
1247 default:
1248 return false;
1249 }
1250}
1251
1252// Map SPIR-V type to VK_COMPONENT_TYPE enum
sjfricke4f600c82022-06-09 14:21:32 +09001253VkComponentTypeNV GetComponentType(spirv_inst_iter insn) {
Jeff Bolze4356752019-03-07 11:23:46 -06001254 switch (insn.opcode()) {
1255 case spv::OpTypeInt:
1256 switch (insn.word(2)) {
1257 case 8:
1258 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV;
1259 case 16:
1260 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV;
1261 case 32:
1262 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV;
1263 case 64:
1264 return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV;
1265 default:
1266 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1267 }
1268 case spv::OpTypeFloat:
1269 switch (insn.word(2)) {
1270 case 16:
1271 return VK_COMPONENT_TYPE_FLOAT16_NV;
1272 case 32:
1273 return VK_COMPONENT_TYPE_FLOAT32_NV;
1274 case 64:
1275 return VK_COMPONENT_TYPE_FLOAT64_NV;
1276 default:
1277 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1278 }
1279 default:
1280 return VK_COMPONENT_TYPE_MAX_ENUM_NV;
1281 }
1282}
1283
1284// Validate SPV_NV_cooperative_matrix behavior that can't be statically validated
1285// in SPIRV-Tools (e.g. due to specialization constant usage).
sjfricke4f600c82022-06-09 14:21:32 +09001286bool CoreChecks::ValidateCooperativeMatrix(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001287 safe_VkPipelineShaderStageCreateInfo const *pStage,
John Zulaufac4c6e12019-07-01 16:05:58 -06001288 const PIPELINE_STATE *pipeline) const {
Jeff Bolze4356752019-03-07 11:23:46 -06001289 bool skip = false;
1290
1291 // Map SPIR-V result ID to specialization constant id (SpecId decoration value)
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001292 layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001293 // Map SPIR-V result ID to the ID of its type.
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001294 layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id;
Jeff Bolze4356752019-03-07 11:23:46 -06001295
1296 struct CoopMatType {
1297 uint32_t scope, rows, cols;
1298 VkComponentTypeNV component_type;
1299 bool all_constant;
1300
1301 CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {}
1302
sjfricke4f600c82022-06-09 14:21:32 +09001303 void Init(uint32_t id, const SHADER_MODULE_STATE &module_state, safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeremy Gebbencbf22862021-03-03 12:01:22 -07001304 const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) {
sjfricke4f600c82022-06-09 14:21:32 +09001305 spirv_inst_iter insn = module_state.get_def(id);
Jeff Bolze4356752019-03-07 11:23:46 -06001306 uint32_t component_type_id = insn.word(2);
1307 uint32_t scope_id = insn.word(3);
1308 uint32_t rows_id = insn.word(4);
1309 uint32_t cols_id = insn.word(5);
sjfricke4f600c82022-06-09 14:21:32 +09001310 auto component_type_iter = module_state.get_def(component_type_id);
1311 auto scope_iter = module_state.get_def(scope_id);
1312 auto rows_iter = module_state.get_def(rows_id);
1313 auto cols_iter = module_state.get_def(cols_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001314
1315 all_constant = true;
sfricke-samsungef15e482022-01-26 11:32:49 -08001316 if (!GetIntConstantValue(scope_iter, module_state, pStage, id_to_spec_id, &scope)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001317 all_constant = false;
1318 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001319 if (!GetIntConstantValue(rows_iter, module_state, pStage, id_to_spec_id, &rows)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001320 all_constant = false;
1321 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001322 if (!GetIntConstantValue(cols_iter, module_state, pStage, id_to_spec_id, &cols)) {
Jeff Bolze4356752019-03-07 11:23:46 -06001323 all_constant = false;
1324 }
sjfricke4f600c82022-06-09 14:21:32 +09001325 component_type = GetComponentType(component_type_iter);
Jeff Bolze4356752019-03-07 11:23:46 -06001326 }
1327 };
1328
1329 bool seen_coopmat_capability = false;
1330
sjfricke4f600c82022-06-09 14:21:32 +09001331 for (auto insn : module_state) {
sjfrickeb0943832022-08-18 16:06:54 +09001332 if (OpcodeHasType(insn.opcode()) && OpcodeHasResult(insn.opcode())) {
1333 id_to_type_id[insn.word(2)] = insn.word(1);
Jeff Bolze4356752019-03-07 11:23:46 -06001334 }
1335
1336 switch (insn.opcode()) {
1337 case spv::OpDecorate:
1338 if (insn.word(2) == spv::DecorationSpecId) {
1339 id_to_spec_id[insn.word(1)] = insn.word(3);
1340 }
1341 break;
1342 case spv::OpCapability:
1343 if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) {
1344 seen_coopmat_capability = true;
1345
1346 if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001347 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001348 pipeline->pipeline(), "VUID-RuntimeSpirv-OpTypeCooperativeMatrixNV-06322",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001349 "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)",
1350 phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages);
Jeff Bolze4356752019-03-07 11:23:46 -06001351 }
1352 }
1353 break;
1354 case spv::OpMemoryModel:
1355 // If the capability isn't enabled, don't bother with the rest of this function.
1356 // OpMemoryModel is the first required instruction after all OpCapability instructions.
1357 if (!seen_coopmat_capability) {
1358 return skip;
1359 }
1360 break;
1361 case spv::OpTypeCooperativeMatrixNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001362 CoopMatType m;
sfricke-samsungef15e482022-01-26 11:32:49 -08001363 m.Init(insn.word(1), module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001364
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001365 if (m.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001366 // Validate that the type parameters are all supported for one of the
1367 // operands of a cooperative matrix property.
1368 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001369 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001370 if (cooperative_matrix_properties[i].AType == m.component_type &&
1371 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols &&
1372 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001373 valid = true;
1374 break;
1375 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001376 if (cooperative_matrix_properties[i].BType == m.component_type &&
1377 cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1378 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001379 valid = true;
1380 break;
1381 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001382 if (cooperative_matrix_properties[i].CType == m.component_type &&
1383 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1384 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001385 valid = true;
1386 break;
1387 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001388 if (cooperative_matrix_properties[i].DType == m.component_type &&
1389 cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols &&
1390 cooperative_matrix_properties[i].scope == m.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001391 valid = true;
1392 break;
1393 }
1394 }
1395 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001396 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001397 "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type",
1398 insn.word(1));
Jeff Bolze4356752019-03-07 11:23:46 -06001399 }
1400 }
1401 break;
1402 }
1403 case spv::OpCooperativeMatrixMulAddNV: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001404 CoopMatType a, b, c, d;
Jeff Bolze4356752019-03-07 11:23:46 -06001405 if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() ||
1406 id_to_type_id.find(insn.word(3)) == id_to_type_id.end() ||
1407 id_to_type_id.find(insn.word(4)) == id_to_type_id.end() ||
1408 id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) {
Mike Schuchardte48dc142019-04-18 09:12:03 -07001409 // Couldn't find type of matrix
1410 assert(false);
Jeff Bolze4356752019-03-07 11:23:46 -06001411 break;
1412 }
sfricke-samsungef15e482022-01-26 11:32:49 -08001413 d.Init(id_to_type_id[insn.word(2)], module_state, pStage, id_to_spec_id);
1414 a.Init(id_to_type_id[insn.word(3)], module_state, pStage, id_to_spec_id);
1415 b.Init(id_to_type_id[insn.word(4)], module_state, pStage, id_to_spec_id);
1416 c.Init(id_to_type_id[insn.word(5)], module_state, pStage, id_to_spec_id);
Jeff Bolze4356752019-03-07 11:23:46 -06001417
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001418 if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) {
Jeff Bolze4356752019-03-07 11:23:46 -06001419 // Validate that the type parameters are all supported for the same
1420 // cooperative matrix property.
1421 bool valid = false;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001422 for (uint32_t i = 0; i < cooperative_matrix_properties.size(); ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001423 if (cooperative_matrix_properties[i].AType == a.component_type &&
1424 cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols &&
1425 cooperative_matrix_properties[i].scope == a.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001426
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001427 cooperative_matrix_properties[i].BType == b.component_type &&
1428 cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols &&
1429 cooperative_matrix_properties[i].scope == b.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001430
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001431 cooperative_matrix_properties[i].CType == c.component_type &&
1432 cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols &&
1433 cooperative_matrix_properties[i].scope == c.scope &&
Jeff Bolze4356752019-03-07 11:23:46 -06001434
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001435 cooperative_matrix_properties[i].DType == d.component_type &&
1436 cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols &&
1437 cooperative_matrix_properties[i].scope == d.scope) {
Jeff Bolze4356752019-03-07 11:23:46 -06001438 valid = true;
1439 break;
1440 }
1441 }
1442 if (!valid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001443 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001444 "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix "
1445 "VkCooperativeMatrixPropertiesNV",
1446 insn.word(2));
Jeff Bolze4356752019-03-07 11:23:46 -06001447 }
1448 }
1449 break;
1450 }
1451 default:
1452 break;
1453 }
1454 }
1455
1456 return skip;
1457}
1458
sjfricke4f600c82022-06-09 14:21:32 +09001459bool CoreChecks::ValidateShaderResolveQCOM(const SHADER_MODULE_STATE &module_state,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001460 safe_VkPipelineShaderStageCreateInfo const *pStage,
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001461 const PIPELINE_STATE *pipeline) const {
1462 bool skip = false;
1463
1464 // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM,
1465 // then the fragment shader must not enable the SPIRV SampleRateShading capability.
1466 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09001467 for (auto insn : module_state) {
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001468 switch (insn.opcode()) {
1469 case spv::OpCapability:
1470 if (insn.word(1) == spv::CapabilitySampleRateShading) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001471 const auto &rp_state = pipeline->RenderPassState();
1472 auto subpass_flags = (!rp_state) ? 0 : rp_state->createInfo.pSubpasses[pipeline->Subpass()].flags;
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001473 if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) {
1474 skip |=
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001475 LogError(pipeline->pipeline(), "VUID-RuntimeSpirv-SampleRateShading-06378",
Jeff Leger9b3dcff2021-05-27 15:40:20 -04001476 "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability "
1477 "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM.");
1478 }
1479 }
1480 break;
1481 default:
1482 break;
1483 }
1484 }
1485 }
1486
1487 return skip;
1488}
1489
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001490bool CoreChecks::ValidateShaderSubgroupSizeControl(safe_VkPipelineShaderStageCreateInfo const *pStage) const {
ziga-lunarg73163742021-08-25 13:15:29 +02001491 bool skip = false;
1492
1493 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001494 !enabled_features.core13.subgroupSizeControl) {
ziga-lunarg73163742021-08-25 13:15:29 +02001495 skip |= LogError(
1496 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02784",
1497 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT, "
1498 "but the VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::subgroupSizeControl feature is not enabled.");
1499 }
1500
1501 if ((pStage->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) != 0 &&
Tony-LunarG273f32f2021-09-28 08:56:30 -06001502 !enabled_features.core13.computeFullSubgroups) {
ziga-lunarg73163742021-08-25 13:15:29 +02001503 skip |= LogError(
1504 device, "VUID-VkPipelineShaderStageCreateInfo-flags-02785",
1505 "VkPipelineShaderStageCreateInfo flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT, but the "
1506 "VkPhysicalDeviceSubgroupSizeControlFeaturesEXT::computeFullSubgroups feature is not enabled");
1507 }
1508
1509 return skip;
1510}
1511
sjfricke4f600c82022-06-09 14:21:32 +09001512bool CoreChecks::ValidateAtomicsTypes(const SHADER_MODULE_STATE &module_state) const {
sfricke-samsung58b84352021-07-31 21:41:04 -07001513 bool skip = false;
1514
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001515 // "If sparseImageInt64Atomics is enabled, shaderImageInt64Atomics must be enabled"
sfricke-samsung828e59d2021-08-22 23:20:49 -07001516 const bool valid_image_64_int = enabled_features.shader_image_atomic_int64_features.shaderImageInt64Atomics == VK_TRUE;
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001517
sfricke-samsungf5042b12021-08-05 01:09:40 -07001518 const VkPhysicalDeviceShaderAtomicFloatFeaturesEXT &float_features = enabled_features.shader_atomic_float_features;
1519 const VkPhysicalDeviceShaderAtomicFloat2FeaturesEXT &float2_features = enabled_features.shader_atomic_float2_features;
1520
1521 const bool valid_storage_buffer_float = (
1522 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1523 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1524 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1525 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1526 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1527 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1528 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1529 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1530 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE));
1531
1532 const bool valid_workgroup_float = (
1533 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1534 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1535 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1536 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1537 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1538 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1539 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE) ||
1540 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1541 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1542
1543 const bool valid_image_float = (
1544 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1545 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1546 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1547
1548 const bool valid_16_float = (
1549 (float2_features.shaderBufferFloat16Atomics == VK_TRUE) ||
1550 (float2_features.shaderBufferFloat16AtomicAdd == VK_TRUE) ||
1551 (float2_features.shaderBufferFloat16AtomicMinMax == VK_TRUE) ||
1552 (float2_features.shaderSharedFloat16Atomics == VK_TRUE) ||
1553 (float2_features.shaderSharedFloat16AtomicAdd == VK_TRUE) ||
1554 (float2_features.shaderSharedFloat16AtomicMinMax == VK_TRUE));
1555
1556 const bool valid_32_float = (
1557 (float_features.shaderBufferFloat32Atomics == VK_TRUE) ||
1558 (float_features.shaderBufferFloat32AtomicAdd == VK_TRUE) ||
1559 (float_features.shaderSharedFloat32Atomics == VK_TRUE) ||
1560 (float_features.shaderSharedFloat32AtomicAdd == VK_TRUE) ||
1561 (float_features.shaderImageFloat32Atomics == VK_TRUE) ||
1562 (float_features.shaderImageFloat32AtomicAdd == VK_TRUE) ||
1563 (float2_features.shaderBufferFloat32AtomicMinMax == VK_TRUE) ||
1564 (float2_features.shaderSharedFloat32AtomicMinMax == VK_TRUE) ||
1565 (float2_features.shaderImageFloat32AtomicMinMax == VK_TRUE));
1566
1567 const bool valid_64_float = (
1568 (float_features.shaderBufferFloat64Atomics == VK_TRUE) ||
1569 (float_features.shaderBufferFloat64AtomicAdd == VK_TRUE) ||
1570 (float_features.shaderSharedFloat64Atomics == VK_TRUE) ||
1571 (float_features.shaderSharedFloat64AtomicAdd == VK_TRUE) ||
1572 (float2_features.shaderBufferFloat64AtomicMinMax == VK_TRUE) ||
1573 (float2_features.shaderSharedFloat64AtomicMinMax == VK_TRUE));
1574 // clang-format on
1575
sjfricke4f600c82022-06-09 14:21:32 +09001576 for (const auto &atomic_inst : module_state.GetAtomicInstructions()) {
sfricke-samsung58b84352021-07-31 21:41:04 -07001577 const atomic_instruction &atomic = atomic_inst.second;
sjfricke4f600c82022-06-09 14:21:32 +09001578 const spirv_inst_iter atomic_def = module_state.at(atomic_inst.first);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001579 const uint32_t opcode = atomic_def.opcode();
sfricke-samsung58b84352021-07-31 21:41:04 -07001580
1581 if ((atomic.bit_width == 64) && (atomic.type == spv::OpTypeInt)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001582 // Validate 64-bit image atomics
sfricke-samsung58b84352021-07-31 21:41:04 -07001583 if (((atomic.storage_class == spv::StorageClassStorageBuffer) || (atomic.storage_class == spv::StorageClassUniform)) &&
1584 (enabled_features.core12.shaderBufferInt64Atomics == VK_FALSE)) {
sjfricke657dfdc2022-08-25 23:40:32 +09001585 skip |=
1586 LogError(device, "VUID-RuntimeSpirv-None-06278",
1587 "%s: Can't use 64-bit int atomics operations\n%s\nwith %s storage class without "
1588 "shaderBufferInt64Atomics enabled.",
1589 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1590 module_state.DescribeInstruction(atomic_def).c_str(), string_SpvStorageClass(atomic.storage_class));
sfricke-samsung58b84352021-07-31 21:41:04 -07001591 } else if ((atomic.storage_class == spv::StorageClassWorkgroup) &&
1592 (enabled_features.core12.shaderSharedInt64Atomics == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001593 skip |= LogError(device, "VUID-RuntimeSpirv-None-06279",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001594 "%s: Can't use 64-bit int atomics operations\n%s\nwith Workgroup storage class without "
sfricke-samsung58b84352021-07-31 21:41:04 -07001595 "shaderSharedInt64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001596 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1597 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001598 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_64_int == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001599 skip |= LogError(device, "VUID-RuntimeSpirv-None-06288",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001600 "%s: Can't use 64-bit int atomics operations\n%s\nwith Image storage class without "
sfricke-samsung6c9eb712021-08-04 09:38:54 -07001601 "shaderImageInt64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001602 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1603 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsung58b84352021-07-31 21:41:04 -07001604 }
sfricke-samsungf5042b12021-08-05 01:09:40 -07001605 } else if (atomic.type == spv::OpTypeFloat) {
1606 // Validate Floats
1607 if (atomic.storage_class == spv::StorageClassStorageBuffer) {
1608 if (valid_storage_buffer_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001609 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06284"
1610 : "VUID-RuntimeSpirv-None-06280";
1611 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001612 "%s: Can't use float atomics operations\n%s\nwith StorageBuffer storage class without "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001613 "shaderBufferFloat32Atomics or shaderBufferFloat32AtomicAdd or shaderBufferFloat64Atomics or "
1614 "shaderBufferFloat64AtomicAdd or shaderBufferFloat16Atomics or shaderBufferFloat16AtomicAdd "
1615 "or shaderBufferFloat16AtomicMinMax or shaderBufferFloat32AtomicMinMax or "
1616 "shaderBufferFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001617 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1618 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001619 } else if (opcode == spv::OpAtomicFAddEXT) {
1620 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicAdd == VK_FALSE)) {
1621 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001622 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001623 "StorageBuffer storage class without shaderBufferFloat16AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001624 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1625 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001626 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32AtomicAdd == VK_FALSE)) {
1627 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001628 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001629 "StorageBuffer storage class without shaderBufferFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001630 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1631 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001632 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64AtomicAdd == VK_FALSE)) {
1633 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001634 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001635 "StorageBuffer storage class without shaderBufferFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001636 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 }
1639 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1640 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001641 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1642 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1643 "StorageBuffer storage class without shaderBufferFloat16AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001644 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1645 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001646 } else if ((atomic.bit_width == 32) && (float2_features.shaderBufferFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001647 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1648 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1649 "StorageBuffer storage class without shaderBufferFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001650 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1651 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001652 } else if ((atomic.bit_width == 64) && (float2_features.shaderBufferFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001653 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1654 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1655 "StorageBuffer storage class without shaderBufferFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001656 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1657 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001658 }
1659 } else {
1660 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1661 if ((atomic.bit_width == 16) && (float2_features.shaderBufferFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001662 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1663 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith "
1664 "StorageBuffer storage class without shaderBufferFloat16Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001665 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1666 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001667 } else if ((atomic.bit_width == 32) && (float_features.shaderBufferFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001668 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1669 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith "
1670 "StorageBuffer storage class without shaderBufferFloat32Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001671 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1672 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001673 } else if ((atomic.bit_width == 64) && (float_features.shaderBufferFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001674 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1675 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith "
1676 "StorageBuffer storage class without shaderBufferFloat64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001677 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1678 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001679 }
1680 }
1681 } else if (atomic.storage_class == spv::StorageClassWorkgroup) {
1682 if (valid_workgroup_float == false) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001683 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06285"
1684 : "VUID-RuntimeSpirv-None-06281";
sfricke-samsungef15e482022-01-26 11:32:49 -08001685 skip |=
1686 LogError(device, vuid,
1687 "%s: Can't use float atomics operations\n%s\nwith Workgroup storage class without "
1688 "shaderSharedFloat32Atomics or "
1689 "shaderSharedFloat32AtomicAdd or shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd or "
1690 "shaderSharedFloat16Atomics or shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax or "
1691 "shaderSharedFloat32AtomicMinMax or shaderSharedFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001692 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1693 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001694 } else if (opcode == spv::OpAtomicFAddEXT) {
1695 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicAdd == VK_FALSE)) {
1696 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001697 "%s: Can't use 16-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001698 "storage class without shaderSharedFloat16AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001699 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1700 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001701 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32AtomicAdd == VK_FALSE)) {
1702 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001703 "%s: Can't use 32-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001704 "storage class without shaderSharedFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001705 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1706 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001707 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64AtomicAdd == VK_FALSE)) {
1708 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001709 "%s: Can't use 64-bit float atomics for add operations\n%s\nwith Workgroup "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001710 "storage class without shaderSharedFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001711 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 }
1714 } else if (opcode == spv::OpAtomicFMinEXT || opcode == spv::OpAtomicFMaxEXT) {
1715 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001716 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1717 "%s: Can't use 16-bit float atomics for min/max operations\n%s\nwith "
1718 "Workgroup storage class without shaderSharedFloat16AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001719 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1720 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001721 } else if ((atomic.bit_width == 32) && (float2_features.shaderSharedFloat32AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001722 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1723 "%s: Can't use 32-bit float atomics for min/max operations\n%s\nwith "
1724 "Workgroup storage class without shaderSharedFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001725 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1726 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001727 } else if ((atomic.bit_width == 64) && (float2_features.shaderSharedFloat64AtomicMinMax == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001728 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1729 "%s: Can't use 64-bit float atomics for min/max operations\n%s\nwith "
1730 "Workgroup storage class without shaderSharedFloat64AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001731 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1732 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001733 }
1734 } else {
1735 // Assume is valid load/store/exchange (rest of supported atomic operations) or else spirv-val will catch
1736 if ((atomic.bit_width == 16) && (float2_features.shaderSharedFloat16Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001737 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1738 "%s: Can't use 16-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1739 "storage class without shaderSharedFloat16Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001740 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1741 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001742 } else if ((atomic.bit_width == 32) && (float_features.shaderSharedFloat32Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001743 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1744 "%s: Can't use 32-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1745 "storage class without shaderSharedFloat32Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001746 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1747 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001748 } else if ((atomic.bit_width == 64) && (float_features.shaderSharedFloat64Atomics == VK_FALSE)) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001749 skip |= LogError(device, kVUID_Core_Shader_AtomicFeature,
1750 "%s: Can't use 64-bit float atomics for load/store/exhange operations\n%s\nwith Workgroup "
1751 "storage class without shaderSharedFloat64Atomics enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001752 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1753 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001754 }
1755 }
1756 } else if ((atomic.storage_class == spv::StorageClassImage) && (valid_image_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001757 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06286"
1758 : "VUID-RuntimeSpirv-None-06282";
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001759 skip |= LogError(
1760 device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001761 "%s: Can't use float atomics operations\n%s\nwith Image storage class without shaderImageFloat32Atomics or "
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07001762 "shaderImageFloat32AtomicAdd or shaderImageFloat32AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001763 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1764 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001765 } else if ((atomic.bit_width == 16) && (valid_16_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001766 skip |= LogError(device, "VUID-RuntimeSpirv-None-06337",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001767 "%s: Can't use 16-bit float atomics operations\n%s\nwithout shaderBufferFloat16Atomics, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001768 "shaderBufferFloat16AtomicAdd, shaderBufferFloat16AtomicMinMax, shaderSharedFloat16Atomics, "
1769 "shaderSharedFloat16AtomicAdd or shaderSharedFloat16AtomicMinMax enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001770 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1771 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001772 } else if ((atomic.bit_width == 32) && (valid_32_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001773 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06338"
1774 : "VUID-RuntimeSpirv-None-06335";
1775 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001776 "%s: Can't use 32-bit float atomics operations\n%s\nwithout shaderBufferFloat32AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001777 "shaderSharedFloat32AtomicMinMax, shaderImageFloat32AtomicMinMax, sparseImageFloat32AtomicMinMax, "
1778 "shaderBufferFloat32Atomics, shaderBufferFloat32AtomicAdd, shaderSharedFloat32Atomics, "
1779 "shaderSharedFloat32AtomicAdd, shaderImageFloat32Atomics, shaderImageFloat32AtomicAdd, "
1780 "sparseImageFloat32Atomics or sparseImageFloat32AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001781 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1782 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001783 } else if ((atomic.bit_width == 64) && (valid_64_float == false)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001784 const char *vuid = IsExtEnabled(device_extensions.vk_ext_shader_atomic_float2) ? "VUID-RuntimeSpirv-None-06339"
1785 : "VUID-RuntimeSpirv-None-06336";
1786 skip |= LogError(device, vuid,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08001787 "%s: Can't use 64-bit float atomics operations\n%s\nwithout shaderBufferFloat64AtomicMinMax, "
sfricke-samsungf5042b12021-08-05 01:09:40 -07001788 "shaderSharedFloat64AtomicMinMax, shaderBufferFloat64Atomics, shaderBufferFloat64AtomicAdd, "
1789 "shaderSharedFloat64Atomics or shaderSharedFloat64AtomicAdd enabled.",
sjfricke4f600c82022-06-09 14:21:32 +09001790 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
1791 module_state.DescribeInstruction(atomic_def).c_str());
sfricke-samsungf5042b12021-08-05 01:09:40 -07001792 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001793 }
1794 }
sfricke-samsung58b84352021-07-31 21:41:04 -07001795 return skip;
1796}
1797
sjfricke4f600c82022-06-09 14:21:32 +09001798bool CoreChecks::ValidateExecutionModes(const SHADER_MODULE_STATE &module_state, spirv_inst_iter entrypoint,
sfricke-samsungef15e482022-01-26 11:32:49 -08001799 VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001800 auto entrypoint_id = entrypoint.word(2);
1801
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001802 // The first denorm execution mode encountered, along with its bit width.
1803 // Used to check if SeparateDenormSettings is respected.
1804 std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001805
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001806 // The first rounding mode encountered, along with its bit width.
1807 // Used to check if SeparateRoundingModeSettings is respected.
1808 std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0);
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001809
1810 bool skip = false;
1811
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001812 uint32_t vertices_out = 0;
Jeff Bolze9ee3d82019-05-29 13:45:13 -05001813 uint32_t invocations = 0;
1814
sjfricke4f600c82022-06-09 14:21:32 +09001815 const auto &execution_mode_inst = module_state.GetExecutionModeInstructions();
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001816 auto it = execution_mode_inst.find(entrypoint_id);
1817 if (it != execution_mode_inst.end()) {
sfricke-samsung8a7341a2021-02-28 07:30:21 -08001818 for (auto insn : it->second) {
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001819 auto mode = insn.word(2);
1820 switch (mode) {
1821 case spv::ExecutionModeSignedZeroInfNanPreserve: {
1822 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001823 if (bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001824 skip |= LogError(
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001825 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat16-06293",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001826 "Shader requires SignedZeroInfNanPreserve for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001827 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001828 } else if (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) {
1829 skip |= LogError(
1830 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat32-06294",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001831 "Shader requires SignedZeroInfNanPreserve for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001832 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001833 } else if (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64) {
1834 skip |= LogError(
1835 device, "VUID-RuntimeSpirv-shaderSignedZeroInfNanPreserveFloat64-06295",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001836 "Shader requires SignedZeroInfNanPreserve for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001837 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001838 }
1839 break;
1840 }
1841
1842 case spv::ExecutionModeDenormPreserve: {
1843 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001844 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) {
1845 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat16-06296",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001846 "Shader requires DenormPreserve for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001847 module_state.DescribeInstruction(insn).c_str());
sfricke-samsunged00aa42022-01-27 19:03:01 -08001848 ;
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001849 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) {
1850 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat32-06297",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001851 "Shader requires DenormPreserve for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001852 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001853 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64) {
1854 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDenormPreserveFloat64-06298",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001855 "Shader requires DenormPreserve for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001856 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001857 }
1858
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001859 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1860 // Register the first denorm execution mode found
1861 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001862 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001863 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001864 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001865 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001866 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001867 "Shader uses different denorm execution modes for 16 and 64-bit but "
1868 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001869 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001870 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001871 }
1872 break;
1873
Mike Schuchardt2df08912020-12-15 16:28:09 -08001874 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001875 break;
1876
Mike Schuchardt2df08912020-12-15 16:28:09 -08001877 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001878 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001879 "Shader uses different denorm execution modes for different bit widths but "
1880 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001881 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001882 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001883 break;
1884
1885 default:
1886 break;
1887 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001888 }
1889 break;
1890 }
1891
1892 case spv::ExecutionModeDenormFlushToZero: {
1893 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001894 if (bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001895 skip |=
1896 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat16-06299",
1897 "Shader requires DenormFlushToZero for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001898 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001899 } else if (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001900 skip |=
1901 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat32-06300",
1902 "Shader requires DenormFlushToZero for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001903 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001904 } else if (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64) {
sfricke-samsunged00aa42022-01-27 19:03:01 -08001905 skip |=
1906 LogError(device, "VUID-RuntimeSpirv-shaderDenormFlushToZeroFloat64-06301",
1907 "Shader requires DenormFlushToZero for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001908 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001909 }
1910
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001911 if (first_denorm_execution_mode.first == spv::ExecutionModeMax) {
1912 // Register the first denorm execution mode found
1913 first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001914 } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001915 switch (phys_dev_props_core12.denormBehaviorIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001916 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001917 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001918 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06289",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001919 "Shader uses different denorm execution modes for 16 and 64-bit but "
1920 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001921 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001922 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001923 }
1924 break;
1925
Mike Schuchardt2df08912020-12-15 16:28:09 -08001926 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001927 break;
1928
Mike Schuchardt2df08912020-12-15 16:28:09 -08001929 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001930 skip |= LogError(device, "VUID-RuntimeSpirv-denormBehaviorIndependence-06290",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001931 "Shader uses different denorm execution modes for different bit widths but "
1932 "denormBehaviorIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001933 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001934 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001935 break;
1936
1937 default:
1938 break;
1939 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001940 }
1941 break;
1942 }
1943
1944 case spv::ExecutionModeRoundingModeRTE: {
1945 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001946 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) {
1947 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat16-06302",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001948 "Shader requires RoundingModeRTE for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001949 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001950 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) {
1951 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat32-06303",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001952 "Shader requires RoundingModeRTE for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001953 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001954 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64) {
1955 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTEFloat64-06304",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001956 "Shader requires RoundingModeRTE for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001957 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001958 }
1959
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01001960 if (first_rounding_mode.first == spv::ExecutionModeMax) {
1961 // Register the first rounding mode found
1962 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001963 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07001964 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08001965 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001966 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001967 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001968 "Shader uses different rounding modes for 16 and 64-bit but "
1969 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001970 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001971 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001972 }
1973 break;
1974
Mike Schuchardt2df08912020-12-15 16:28:09 -08001975 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001976 break;
1977
Mike Schuchardt2df08912020-12-15 16:28:09 -08001978 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001979 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07001980 "Shader uses different rounding modes for different bit widths but "
1981 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08001982 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001983 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05001984 break;
1985
1986 default:
1987 break;
1988 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00001989 }
1990 break;
1991 }
1992
1993 case spv::ExecutionModeRoundingModeRTZ: {
1994 auto bit_width = insn.word(3);
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001995 if (bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) {
1996 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat16-06305",
sfricke-samsunged00aa42022-01-27 19:03:01 -08001997 "Shader requires RoundingModeRTZ for bit width 16 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09001998 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07001999 } else if (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) {
2000 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat32-06306",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002001 "Shader requires RoundingModeRTZ for bit width 32 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002002 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002003 } else if (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64) {
2004 skip |= LogError(device, "VUID-RuntimeSpirv-shaderRoundingModeRTZFloat64-06307",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002005 "Shader requires RoundingModeRTZ for bit width 64 but it is not enabled on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002006 module_state.DescribeInstruction(insn).c_str());
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002007 }
2008
Attilio Provenzanof6c0e852019-04-09 11:01:18 +01002009 if (first_rounding_mode.first == spv::ExecutionModeMax) {
2010 // Register the first rounding mode found
2011 first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width);
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002012 } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) {
Piers Daniell41b8c5d2020-01-10 15:42:00 -07002013 switch (phys_dev_props_core12.roundingModeIndependence) {
Mike Schuchardt2df08912020-12-15 16:28:09 -08002014 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002015 if (first_rounding_mode.second != 32 && bit_width != 32) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002016 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06291",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002017 "Shader uses different rounding modes for 16 and 64-bit but "
2018 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002019 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002020 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002021 }
2022 break;
2023
Mike Schuchardt2df08912020-12-15 16:28:09 -08002024 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL:
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002025 break;
2026
Mike Schuchardt2df08912020-12-15 16:28:09 -08002027 case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE:
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002028 skip |= LogError(device, "VUID-RuntimeSpirv-roundingModeIndependence-06292",
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002029 "Shader uses different rounding modes for different bit widths but "
2030 "roundingModeIndependence is "
sfricke-samsunged00aa42022-01-27 19:03:01 -08002031 "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002032 module_state.DescribeInstruction(insn).c_str());
Jason Ekstrande1e06de2019-08-05 11:43:43 -05002033 break;
2034
2035 default:
2036 break;
2037 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002038 }
2039 break;
2040 }
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002041
2042 case spv::ExecutionModeOutputVertices: {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002043 vertices_out = insn.word(3);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002044 break;
2045 }
2046
2047 case spv::ExecutionModeInvocations: {
2048 invocations = insn.word(3);
2049 break;
2050 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002051
2052 case spv::ExecutionModeLocalSizeId: {
Tony-LunarG273f32f2021-09-28 08:56:30 -06002053 if (!enabled_features.core13.maintenance4) {
Piers Daniella7f93b62021-11-20 12:32:04 -07002054 skip |= LogError(device, "VUID-RuntimeSpirv-LocalSizeId-06434",
2055 "LocalSizeId execution mode used but maintenance4 feature not enabled");
2056 }
ziga-lunargf2aa8152022-04-17 13:03:29 +02002057 if (!IsExtEnabled(device_extensions.vk_khr_maintenance4)) {
2058 skip |= LogError(device, "VUID-RuntimeSpirv-LocalSizeId-06433",
2059 "LocalSizeId execution mode used but maintenance4 extension is not enabled and used Vulkan api version is 1.2 or less");
2060 }
Piers Daniella7f93b62021-11-20 12:32:04 -07002061 break;
2062 }
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002063
2064 case spv::ExecutionModeEarlyFragmentTests: {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002065 const auto *ds_state = (pipeline) ? pipeline->DepthStencilState() : nullptr;
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002066 if ((stage == VK_SHADER_STAGE_FRAGMENT_BIT) &&
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002067 (ds_state &&
2068 (ds_state->flags &
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002069 (VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_DEPTH_ACCESS_BIT_ARM |
2070 VK_PIPELINE_DEPTH_STENCIL_STATE_CREATE_RASTERIZATION_ORDER_ATTACHMENT_STENCIL_ACCESS_BIT_ARM)) != 0)) {
2071 skip |= LogError(
Nathaniel Cesario6a0ce2f2022-04-02 21:47:54 -06002072 device, "VUID-VkGraphicsPipelineCreateInfo-flags-06591",
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002073 "The fragment shader enables early fragment tests, but VkPipelineDepthStencilStateCreateInfo::flags == "
2074 "%s",
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002075 string_VkPipelineDepthStencilStateCreateFlags(ds_state->flags).c_str());
Younggwan Kim26b9abd2021-12-07 21:22:03 +00002076 }
2077 break;
2078 }
ziga-lunarge25f5f02022-04-16 15:07:35 +02002079 case spv::ExecutionModeSubgroupUniformControlFlowKHR: {
2080 if (!enabled_features.shader_subgroup_uniform_control_flow_features.shaderSubgroupUniformControlFlow ||
2081 (phys_dev_ext_props.subgroup_properties.supportedStages & stage) == 0 ||
sjfricke4f600c82022-06-09 14:21:32 +09002082 module_state.static_data_.has_invocation_repack_instruction) {
ziga-lunarge25f5f02022-04-16 15:07:35 +02002083 std::stringstream msg;
2084 if (!enabled_features.shader_subgroup_uniform_control_flow_features.shaderSubgroupUniformControlFlow) {
2085 msg << "shaderSubgroupUniformControlFlow feature must be enabled";
2086 } else if ((phys_dev_ext_props.subgroup_properties.supportedStages & stage) == 0) {
2087 msg << "stage" << string_VkShaderStageFlagBits(stage)
2088 << " must be in VkPhysicalDeviceSubgroupProperties::supportedStages("
2089 << string_VkShaderStageFlags(phys_dev_ext_props.subgroup_properties.supportedStages) << ")";
2090 } else {
2091 msg << "the shader must not use any invocation repack instructions";
2092 }
2093 skip |= LogError(device, "VUID-RuntimeSpirv-SubgroupUniformControlFlowKHR-06379",
2094 "If ExecutionModeSubgroupUniformControlFlowKHR is used %s.", msg.str().c_str());
2095 }
2096 } break;
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002097 }
2098 }
2099 }
2100
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002101 if (entrypoint.word(1) == spv::ExecutionModelGeometry) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002102 if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002103 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714",
2104 "Geometry shader entry point must have an OpExecutionMode instruction that "
2105 "specifies a maximum output vertex count that is greater than 0 and less "
2106 "than or equal to maxGeometryOutputVertices. "
2107 "OutputVertices=%d, maxGeometryOutputVertices=%d",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002108 vertices_out, phys_dev_props.limits.maxGeometryOutputVertices);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002109 }
2110
2111 if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002112 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715",
2113 "Geometry shader entry point must have an OpExecutionMode instruction that "
2114 "specifies an invocation count that is greater than 0 and less "
2115 "than or equal to maxGeometryShaderInvocations. "
2116 "Invocations=%d, maxGeometryShaderInvocations=%d",
2117 invocations, phys_dev_props.limits.maxGeometryShaderInvocations);
Jeff Bolze9ee3d82019-05-29 13:45:13 -05002118 }
2119 }
Attilio Provenzanoc5d50102019-03-25 17:40:37 +00002120 return skip;
2121}
2122
Chris Forbes47567b72017-06-09 12:09:45 -07002123// For given pipelineLayout verify that the set_layout_node at slot.first
2124// has the requested binding at slot.second and return ptr to that binding
Mark Lobodzinskica6ebe32019-04-25 11:43:37 -06002125static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002126 DescriptorSlot slot) {
Chris Forbes47567b72017-06-09 12:09:45 -07002127 if (!pipelineLayout) return nullptr;
2128
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002129 if (slot.set >= pipelineLayout->set_layouts.size()) return nullptr;
Chris Forbes47567b72017-06-09 12:09:45 -07002130
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002131 return pipelineLayout->set_layouts[slot.set]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.binding);
Chris Forbes47567b72017-06-09 12:09:45 -07002132}
2133
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002134// If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize
2135// o If there is only a vertex shader : gl_PointSize must be written when using points
2136// o If there is a geometry or tessellation shader:
2137// - If shaderTessellationAndGeometryPointSize feature is enabled:
2138// * gl_PointSize must be written in the final geometry stage
2139// - If shaderTessellationAndGeometryPointSize feature is disabled:
2140// * gl_PointSize must NOT be written and a default of 1.0 is assumed
sjfricke4f600c82022-06-09 14:21:32 +09002141bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, const SHADER_MODULE_STATE &module_state,
John Zulaufac4c6e12019-07-01 16:05:58 -06002142 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002143 if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
2144 return false;
2145 }
2146
2147 bool pointsize_written = false;
2148 bool skip = false;
2149
2150 // Search for PointSize built-in decorations
sjfricke4f600c82022-06-09 14:21:32 +09002151 for (const auto &set : module_state.GetBuiltinDecorationList()) {
2152 auto insn = module_state.at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002153 if (set.builtin == spv::BuiltInPointSize) {
sjfricke4f600c82022-06-09 14:21:32 +09002154 pointsize_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002155 if (pointsize_written) {
2156 break;
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002157 }
2158 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002159 }
2160
2161 if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) &&
Mark Lobodzinskid7b03cc2019-04-19 14:23:10 -06002162 !enabled_features.core.shaderTessellationAndGeometryPointSize) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002163 if (pointsize_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002164 skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002165 "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which "
2166 "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled.");
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002167 }
2168 } else if (!pointsize_written) {
2169 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002170 LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002171 "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.",
2172 string_VkShaderStageFlagBits(stage));
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002173 }
2174 return skip;
2175}
John Zulauf14c355b2019-06-27 16:09:37 -06002176
sjfricke4f600c82022-06-09 14:21:32 +09002177bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, const SHADER_MODULE_STATE &module_state,
Tobias Hector6663c9b2020-11-05 10:18:02 +00002178 spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const {
2179 bool primitiverate_written = false;
2180 bool viewportindex_written = false;
2181 bool viewportmask_written = false;
2182 bool skip = false;
2183
2184 // Check if the primitive shading rate is written
sjfricke4f600c82022-06-09 14:21:32 +09002185 for (const auto &set : module_state.GetBuiltinDecorationList()) {
2186 auto insn = module_state.at(set.offset);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002187 if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) {
sjfricke4f600c82022-06-09 14:21:32 +09002188 primitiverate_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002189 } else if (set.builtin == spv::BuiltInViewportIndex) {
sjfricke4f600c82022-06-09 14:21:32 +09002190 viewportindex_written = module_state.IsBuiltInWritten(insn, entrypoint);
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002191 } else if (set.builtin == spv::BuiltInViewportMaskNV) {
sjfricke4f600c82022-06-09 14:21:32 +09002192 viewportmask_written = module_state.IsBuiltInWritten(insn, entrypoint);
Tobias Hector6663c9b2020-11-05 10:18:02 +00002193 }
sfricke-samsungc0eb5282021-02-28 23:05:55 -08002194 if (primitiverate_written && viewportindex_written && viewportmask_written) {
2195 break;
2196 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00002197 }
2198
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002199 const auto viewport_state = pipeline->ViewportState();
Tony-LunarGd44844c2021-01-22 13:24:37 -07002200 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002201 (pipeline->GetPipelineType() == VK_PIPELINE_BIND_POINT_GRAPHICS) && viewport_state) {
2202 if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && viewport_state->viewportCount > 1 &&
2203 primitiverate_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002204 skip |= LogError(pipeline->pipeline(),
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07002205 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503",
2206 "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but "
2207 "multiple viewports "
2208 "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2209 string_VkShaderStageFlagBits(stage));
Tobias Hector6663c9b2020-11-05 10:18:02 +00002210 }
2211
2212 if (primitiverate_written && viewportindex_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002213 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002214 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504",
2215 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2216 "ViewportIndex built-ins,"
2217 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2218 string_VkShaderStageFlagBits(stage));
2219 }
2220
2221 if (primitiverate_written && viewportmask_written) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002222 skip |= LogError(pipeline->pipeline(),
Tobias Hector6663c9b2020-11-05 10:18:02 +00002223 "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505",
2224 "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and "
2225 "ViewportMaskNV built-ins,"
2226 "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
2227 string_VkShaderStageFlagBits(stage));
2228 }
2229 }
2230 return skip;
2231}
2232
sjfricke4f600c82022-06-09 14:21:32 +09002233bool CoreChecks::ValidateDecorations(const SHADER_MODULE_STATE &module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002234 bool skip = false;
2235
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002236 std::vector<spirv_inst_iter> xfb_streams;
2237 std::vector<spirv_inst_iter> xfb_buffers;
ziga-lunargef2c3172021-11-07 10:35:29 +01002238 std::vector<spirv_inst_iter> xfb_offsets;
2239
sjfricke4f600c82022-06-09 14:21:32 +09002240 for (const auto &op_decorate : module_state.GetDecorationInstructions()) {
ziga-lunargce66e542021-09-19 00:11:14 +02002241 uint32_t decoration = op_decorate.word(2);
2242 if (decoration == spv::DecorationXfbStride) {
2243 uint32_t stride = op_decorate.word(3);
2244 if (stride > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride) {
2245 skip |= LogError(
2246 device, "VUID-RuntimeSpirv-XfbStride-06313",
2247 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_stride (%" PRIu32
2248 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataStride (%" PRIu32
2249 ").",
2250 stride, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataStride);
2251 }
2252 }
ziga-lunarg423cf212021-11-07 00:00:27 +01002253 if (decoration == spv::DecorationStream) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002254 xfb_streams.push_back(op_decorate);
ziga-lunarg423cf212021-11-07 00:00:27 +01002255 uint32_t stream = op_decorate.word(3);
2256 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2257 skip |= LogError(
2258 device, "VUID-RuntimeSpirv-Stream-06312",
2259 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2260 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32 ").",
2261 stream, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
2262 }
2263 }
ziga-lunargef2c3172021-11-07 10:35:29 +01002264 if (decoration == spv::DecorationXfbBuffer) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002265 xfb_buffers.push_back(op_decorate);
ziga-lunargef2c3172021-11-07 10:35:29 +01002266 }
2267 if (decoration == spv::DecorationOffset) {
2268 xfb_offsets.push_back(op_decorate);
2269 }
2270 }
2271
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002272 // XfbBuffer, buffer data size
2273 std::vector<std::pair<uint32_t, uint32_t>> buffer_data_sizes;
ziga-lunargef2c3172021-11-07 10:35:29 +01002274 for (const auto &op_decorate : xfb_offsets) {
2275 for (const auto xfb_buffer : xfb_buffers) {
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002276 if (xfb_buffer.word(1) == op_decorate.word(1)) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002277 const auto offset = op_decorate.word(3);
sjfricke4f600c82022-06-09 14:21:32 +09002278 const auto def = module_state.get_def(xfb_buffer.word(1));
2279 const auto size = module_state.GetTypeBytesSize(def);
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002280 const uint32_t buffer_data_size = offset + size;
2281 if (buffer_data_size > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize) {
ziga-lunargef2c3172021-11-07 10:35:29 +01002282 skip |= LogError(
2283 device, "VUID-RuntimeSpirv-Offset-06308",
2284 "vkCreateGraphicsPipelines(): shader uses transform feedback with xfb_offset (%" PRIu32
2285 ") + size of variable (%" PRIu32 ") greater than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2286 "(%" PRIu32 ").",
2287 offset, size, phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2288 }
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002289
2290 bool found = false;
2291 for (auto &bds : buffer_data_sizes) {
2292 if (bds.first == xfb_buffer.word(1)) {
2293 bds.second = std::max(bds.second, buffer_data_size);
2294 found = true;
2295 break;
2296 }
2297 }
2298 if (!found) {
2299 buffer_data_sizes.emplace_back(xfb_buffer.word(1), buffer_data_size);
2300 }
2301
ziga-lunargef2c3172021-11-07 10:35:29 +01002302 break;
2303 }
2304 }
ziga-lunargce66e542021-09-19 00:11:14 +02002305 }
2306
ziga-lunargfe45a7c2021-11-10 15:37:04 +01002307 std::unordered_map<uint32_t, uint32_t> stream_data_size;
2308 for (const auto &xfb_stream : xfb_streams) {
2309 for (const auto& bds : buffer_data_sizes) {
2310 if (xfb_stream.word(1) == bds.first) {
2311 uint32_t stream = xfb_stream.word(3);
2312 const auto itr = stream_data_size.find(stream);
2313 if (itr != stream_data_size.end()) {
2314 itr->second += bds.second;
2315 } else {
2316 stream_data_size.insert({stream, bds.second});
2317 }
2318 }
2319 }
2320 }
2321
2322 for (const auto& stream : stream_data_size) {
2323 if (stream.second > phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreamDataSize) {
2324 skip |= LogError(device, "VUID-RuntimeSpirv-XfbBuffer-06309",
2325 "vkCreateGraphicsPipelines(): shader uses transform feedback with stream (%" PRIu32
2326 ") having the sum of buffer data sizes (%" PRIu32
2327 ") not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackBufferDataSize "
2328 "(%" PRIu32 ").",
2329 stream.first, stream.second,
2330 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackBufferDataSize);
2331 }
2332 }
2333
ziga-lunargce66e542021-09-19 00:11:14 +02002334 return skip;
2335}
2336
sjfrickede734312022-07-14 19:22:43 +09002337bool CoreChecks::ValidateComputeSharedMemory(const SHADER_MODULE_STATE &module_state, uint32_t total_shared_size) const {
sjfricke44d663c2022-06-01 06:42:58 +09002338 bool skip = false;
sjfrickede734312022-07-14 19:22:43 +09002339
2340 // If not found before with spec constants, find here
2341 if (total_shared_size == 0) {
2342 // when using WorkgroupMemoryExplicitLayoutKHR
2343 // either all or none the structs are decorated with Block,
2344 // if using block, all must decorated with Aliased.
2345 // In this case we want to find the MAX not ADD the block sizes
2346 bool find_max_block = false;
2347
sjfricke44d663c2022-06-01 06:42:58 +09002348 for (auto insn : module_state.static_data_.variable_inst) {
sjfrickede734312022-07-14 19:22:43 +09002349 // StorageClass Workgroup is shared memory
2350 if (insn.word(3) == spv::StorageClassWorkgroup) {
2351 if (module_state.get_decorations(insn.word(2)).flags & decoration_set::aliased_bit) {
2352 find_max_block = true;
2353 }
2354
sjfricke44d663c2022-06-01 06:42:58 +09002355 const uint32_t result_type_id = insn.word(1);
2356 const auto result_type = module_state.get_def(result_type_id);
2357 const auto type = module_state.get_def(result_type.word(3));
sjfrickede734312022-07-14 19:22:43 +09002358 const uint32_t variable_shared_size = module_state.GetTypeBytesSize(type);
2359
2360 if (find_max_block) {
2361 total_shared_size = std::max(total_shared_size, variable_shared_size);
2362 } else {
2363 total_shared_size += variable_shared_size;
2364 }
sjfricke44d663c2022-06-01 06:42:58 +09002365 }
2366 }
sjfrickede734312022-07-14 19:22:43 +09002367 }
2368
2369 if (total_shared_size > phys_dev_props.limits.maxComputeSharedMemorySize) {
2370 skip |=
2371 LogError(device, "VUID-RuntimeSpirv-Workgroup-06530",
2372 "Shader uses %" PRIu32
2373 " bytes of shared memory, more than allowed by physicalDeviceLimits::maxComputeSharedMemorySize (%" PRIu32 ")",
2374 total_shared_size, phys_dev_props.limits.maxComputeSharedMemorySize);
sjfricke44d663c2022-06-01 06:42:58 +09002375 }
2376 return skip;
2377}
2378
Tony-LunarG1672d002022-08-03 14:35:34 -06002379bool CoreChecks::ValidateShaderModuleId(const SHADER_MODULE_STATE &module_state, const PipelineStageState &stage_state,
2380 const safe_VkPipelineShaderStageCreateInfo *pStage, const VkPipelineCreateFlags flags) const {
2381 bool skip = false;
2382 const auto module_identifier = LvlFindInChain<VkPipelineShaderStageModuleIdentifierCreateInfoEXT>(pStage->pNext);
2383 const auto module_create_info = LvlFindInChain<VkShaderModuleCreateInfo>(pStage->pNext);
2384 if (module_identifier && (module_identifier->identifierSize > 0)) {
2385 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
2386 skip |= LogError(
2387 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-pNext-06850",
2388 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2389 "struct in the pNext chain but the shaderModuleIdentifier feature is not enabled",
2390 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2391 string_VkShaderStageFlagBits(stage_state.stage_flag));
2392 }
2393 if (!(flags & VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT)) {
2394 skip |= LogError(
2395 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-pNext-06851",
2396 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2397 "struct in the pNext chain whose identifierSize is > 0 (%" PRIu32
2398 "), but the "
2399 "VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT bit is not set in the pipeline create flags",
2400 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2401 string_VkShaderStageFlagBits(stage_state.stage_flag), module_identifier->identifierSize);
2402 }
2403 if (module_identifier->identifierSize > VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT) {
2404 skip |= LogError(
2405 device, "VUID-VkPipelineShaderStageModuleIdentifierCreateInfoEXT-identifierSize-06852",
2406 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2407 "struct in the pNext chain whose identifierSize (%" PRIu32
2408 ") is > VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT (%" PRIu32 ")",
2409 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2410 string_VkShaderStageFlagBits(stage_state.stage_flag), module_identifier->identifierSize,
2411 VK_MAX_SHADER_MODULE_IDENTIFIER_SIZE_EXT);
2412 }
2413 }
2414 if (module_identifier && module_create_info) {
2415 skip |= LogError(
2416 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06844",
2417 "%s module (stage %s) VkPipelineShaderStageCreateInfo has both a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2418 "struct and a VkShaderModuleCreateInfo struct in the pNext chain",
2419 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2420 string_VkShaderStageFlagBits(stage_state.stage_flag));
2421 }
2422 if (enabled_features.graphics_pipeline_library_features.graphicsPipelineLibrary) {
2423 if (!module_identifier && pStage->module == VK_NULL_HANDLE && !module_create_info) {
2424 skip |= LogError(
2425 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06845",
2426 "%s module (stage %s) VkPipelineShaderStageCreateInfo has no VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2427 "struct and no VkShaderModuleCreateInfo struct in the pNext chain, and module is not a valid VkShaderModule",
2428 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2429 string_VkShaderStageFlagBits(stage_state.stage_flag));
2430 }
2431 } else {
2432 if (!module_identifier && pStage->module == VK_NULL_HANDLE) {
2433 const char *vuid = IsExtEnabled(device_extensions.vk_khr_pipeline_library)
2434 ? "VUID-VkPipelineShaderStageCreateInfo-stage-06846"
2435 : "VUID-VkPipelineShaderStageCreateInfo-stage-06847";
2436 skip |= LogError(
2437 device, vuid,
2438 "%s module (stage %s) VkPipelineShaderStageCreateInfo has no VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2439 "struct in the pNext chain, the graphicsPipelineLibrary feature is not enabled, and module is not a valid "
2440 "VkShaderModule",
2441 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2442 string_VkShaderStageFlagBits(stage_state.stage_flag));
2443 }
2444 }
2445 if (module_identifier && pStage->module != VK_NULL_HANDLE) {
2446 skip |= LogError(
2447 device, "VUID-VkPipelineShaderStageCreateInfo-stage-06848",
2448 "%s module (stage %s) VkPipelineShaderStageCreateInfo has a VkPipelineShaderStageModuleIdentifierCreateInfoEXT "
2449 "struct in the pNext chain, and module is not VK_NULL_HANDLE",
2450 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2451 string_VkShaderStageFlagBits(stage_state.stage_flag));
2452 }
2453 return skip;
2454}
2455
sjfricke6a03e012022-06-23 17:54:11 +09002456// Temporary data of a OpVariable when validating it.
2457// If found useful in another location, can move out to the header
2458struct VariableInstInfo {
2459 bool has_8bit = false;
2460 bool has_16bit = false;
2461};
2462
2463// easier to use recursion to traverse the OpTypeStruct
2464static void GetVariableInfo(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &insn, VariableInstInfo &info) {
2465 if (insn.opcode() == spv::OpTypeFloat || insn.opcode() == spv::OpTypeInt) {
2466 const uint32_t bit_width = insn.word(2);
2467 info.has_8bit |= (bit_width == 8);
2468 info.has_16bit |= (bit_width == 16);
2469 } else if (insn.opcode() == spv::OpTypeStruct) {
2470 for (uint32_t i = 2; i < insn.len(); i++) {
2471 const auto &base_insn = GetBaseTypeIter(module_state, insn.word(i));
2472 GetVariableInfo(module_state, base_insn, info);
2473 }
2474 }
2475}
2476
sjfricke44d663c2022-06-01 06:42:58 +09002477bool CoreChecks::ValidateVariables(const SHADER_MODULE_STATE &module_state) const {
2478 bool skip = false;
2479
2480 for (auto insn : module_state.static_data_.variable_inst) {
2481 const uint32_t storage_class = insn.word(3);
2482
2483 if (storage_class == spv::StorageClassWorkgroup) {
2484 // If Workgroup variable is initalized, make sure the feature is enabled
2485 if (insn.len() > 4 &&
2486 !enabled_features.zero_initialize_work_group_memory_features.shaderZeroInitializeWorkgroupMemory) {
2487 const char *vuid = IsExtEnabled(device_extensions.vk_khr_zero_initialize_workgroup_memory)
2488 ? "VUID-RuntimeSpirv-shaderZeroInitializeWorkgroupMemory-06372"
2489 : "VUID-RuntimeSpirv-OpVariable-06373";
2490 skip |= LogError(
2491 device, vuid,
2492 "vkCreateShaderModule(): "
2493 "VkPhysicalDeviceZeroInitializeWorkgroupMemoryFeaturesKHR::shaderZeroInitializeWorkgroupMemory is not enabled, "
2494 "but shader contains an OpVariable with Workgroup Storage Class with an Initializer operand.\n%s",
2495 module_state.DescribeInstruction(insn).c_str());
2496 }
2497 }
sjfricke6a03e012022-06-23 17:54:11 +09002498
2499 const auto type_pointer = module_state.get_def(insn.word(1));
2500 const auto type = module_state.get_def(type_pointer.word(3));
2501 // type will either be a float, int, or struct and if struct need to traverse it
2502 VariableInstInfo info;
2503 GetVariableInfo(module_state, type, info);
2504
2505 if (info.has_8bit) {
2506 if (!enabled_features.core12.storageBuffer8BitAccess &&
2507 (storage_class == spv::StorageClassStorageBuffer || storage_class == spv::StorageClassShaderRecordBufferKHR || storage_class == spv::StorageClassPhysicalStorageBuffer)) {
2508 skip |= LogError(device, "VUID-RuntimeSpirv-storageBuffer8BitAccess-06328",
2509 "vkCreateShaderModule(): storageBuffer8BitAccess is not enabled, but shader contains an 8-bit "
2510 "OpVariable with %s Storage Class.\n%s",
sjfricke657dfdc2022-08-25 23:40:32 +09002511 string_SpvStorageClass(storage_class), module_state.DescribeInstruction(insn).c_str());
sjfricke6a03e012022-06-23 17:54:11 +09002512 }
2513 if (!enabled_features.core12.uniformAndStorageBuffer8BitAccess && storage_class == spv::StorageClassUniform) {
2514 skip |= LogError(device, "VUID-RuntimeSpirv-uniformAndStorageBuffer8BitAccess-06329",
2515 "vkCreateShaderModule(): uniformAndStorageBuffer8BitAccess is not enabled, but shader contains an "
2516 "8-bit OpVariable with Uniform Storage Class.\n%s",
2517 module_state.DescribeInstruction(insn).c_str());
2518 }
2519 if (!enabled_features.core12.storagePushConstant8 && storage_class == spv::StorageClassPushConstant) {
2520 skip |= LogError(device, "VUID-RuntimeSpirv-storagePushConstant8-06330",
2521 "vkCreateShaderModule(): storagePushConstant8 is not enabled, but shader contains an 8-bit "
2522 "OpVariable with PushConstant Storage Class.\n%s",
2523 module_state.DescribeInstruction(insn).c_str());
2524 }
2525 }
2526
2527 if (info.has_16bit) {
2528 if (!enabled_features.core11.storageBuffer16BitAccess &&
2529 (storage_class == spv::StorageClassStorageBuffer || storage_class == spv::StorageClassShaderRecordBufferKHR || storage_class == spv::StorageClassPhysicalStorageBuffer)) {
2530 skip |= LogError(device, "VUID-RuntimeSpirv-storageBuffer16BitAccess-06331",
2531 "vkCreateShaderModule(): storageBuffer16BitAccess is not enabled, but shader contains an 16-bit "
2532 "OpVariable with %s Storage Class.\n%s",
sjfricke657dfdc2022-08-25 23:40:32 +09002533 string_SpvStorageClass(storage_class), module_state.DescribeInstruction(insn).c_str());
sjfricke6a03e012022-06-23 17:54:11 +09002534 }
2535 if (!enabled_features.core11.uniformAndStorageBuffer16BitAccess && storage_class == spv::StorageClassUniform) {
2536 skip |= LogError(device, "VUID-RuntimeSpirv-uniformAndStorageBuffer16BitAccess-06332",
2537 "vkCreateShaderModule(): uniformAndStorageBuffer16BitAccess is not enabled, but shader contains an "
2538 "16-bit OpVariable with Uniform Storage Class.\n%s",
2539 module_state.DescribeInstruction(insn).c_str());
2540 }
2541 if (!enabled_features.core11.storagePushConstant16 && storage_class == spv::StorageClassPushConstant) {
2542 skip |= LogError(device, "VUID-RuntimeSpirv-storagePushConstant16-06333",
2543 "vkCreateShaderModule(): storagePushConstant16 is not enabled, but shader contains an 16-bit "
2544 "OpVariable with PushConstant Storage Class.\n%s",
2545 module_state.DescribeInstruction(insn).c_str());
2546 }
2547 if (!enabled_features.core11.storageInputOutput16 &&
2548 (storage_class == spv::StorageClassInput || storage_class == spv::StorageClassOutput)) {
2549 skip |= LogError(device, "VUID-RuntimeSpirv-storageInputOutput16-06334",
2550 "vkCreateShaderModule(): storageInputOutput16 is not enabled, but shader contains an 16-bit "
2551 "OpVariable with %s Storage Class.\n%s",
sjfricke657dfdc2022-08-25 23:40:32 +09002552 string_SpvStorageClass(storage_class), module_state.DescribeInstruction(insn).c_str());
sjfricke6a03e012022-06-23 17:54:11 +09002553 }
2554 }
sjfricke44d663c2022-06-01 06:42:58 +09002555 }
2556
2557 return skip;
2558}
2559
sjfricke4f600c82022-06-09 14:21:32 +09002560bool CoreChecks::ValidateTransformFeedback(const SHADER_MODULE_STATE &module_state) const {
ziga-lunargce66e542021-09-19 00:11:14 +02002561 bool skip = false;
2562
ziga-lunarg28d08792021-10-13 15:42:59 +02002563 // Temp workaround to prevent false positive errors
2564 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450
sjfricke4f600c82022-06-09 14:21:32 +09002565 if (module_state.HasMultipleEntryPoints()) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002566 return skip;
2567 }
2568
2569 layer_data::unordered_set<uint32_t> emitted_streams;
2570 bool output_points = false;
sjfricke4f600c82022-06-09 14:21:32 +09002571 for (const auto &insn : module_state) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002572 const uint32_t opcode = insn.opcode();
2573 if (opcode == spv::OpEmitStreamVertex) {
sjfricke4f600c82022-06-09 14:21:32 +09002574 emitted_streams.emplace(static_cast<uint32_t>(module_state.GetConstantValueById(insn.word(1))));
ziga-lunargce66e542021-09-19 00:11:14 +02002575 }
ziga-lunarg28d08792021-10-13 15:42:59 +02002576 if (opcode == spv::OpEmitStreamVertex || opcode == spv::OpEndStreamPrimitive) {
sjfricke4f600c82022-06-09 14:21:32 +09002577 uint32_t stream = static_cast<uint32_t>(module_state.GetConstantValueById(insn.word(1)));
ziga-lunarg28d08792021-10-13 15:42:59 +02002578 if (stream >= phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams) {
2579 skip |= LogError(
2580 device, "VUID-RuntimeSpirv-OpEmitStreamVertex-06310",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002581 "vkCreateGraphicsPipelines(): shader uses transform feedback stream\n%s\nwith index %" PRIu32
ziga-lunarg28d08792021-10-13 15:42:59 +02002582 ", which is not less than VkPhysicalDeviceTransformFeedbackPropertiesEXT::maxTransformFeedbackStreams (%" PRIu32
2583 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002584 module_state.DescribeInstruction(insn).c_str(), stream,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002585 phys_dev_ext_props.transform_feedback_props.maxTransformFeedbackStreams);
ziga-lunarg28d08792021-10-13 15:42:59 +02002586 }
2587 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002588 if ((opcode == spv::OpExecutionMode || opcode == spv::OpExecutionModeId) &&
2589 insn.word(2) == spv::ExecutionModeOutputPoints) {
ziga-lunarg28d08792021-10-13 15:42:59 +02002590 output_points = true;
2591 }
2592 }
2593
2594 const uint32_t emitted_streams_size = static_cast<uint32_t>(emitted_streams.size());
2595 if (emitted_streams_size > 1 && !output_points &&
2596 phys_dev_ext_props.transform_feedback_props.transformFeedbackStreamsLinesTriangles == VK_FALSE) {
2597 skip |= LogError(
2598 device, "VUID-RuntimeSpirv-transformFeedbackStreamsLinesTriangles-06311",
2599 "vkCreateGraphicsPipelines(): shader emits to %" PRIu32 " vertex streams and VkPhysicalDeviceTransformFeedbackPropertiesEXT::transformFeedbackStreamsLinesTriangles is VK_FALSE, but execution mode is not OutputPoints.",
2600 emitted_streams_size);
ziga-lunargce66e542021-09-19 00:11:14 +02002601 }
2602
2603 return skip;
2604}
2605
sfricke-samsung864162a2021-11-01 21:58:01 -07002606// Checks for both TexelOffset and TexelGatherOffset limits
sjfricke4f600c82022-06-09 14:21:32 +09002607bool CoreChecks::ValidateTexelOffsetLimits(const SHADER_MODULE_STATE &module_state, spirv_inst_iter &insn) const {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002608 bool skip = false;
2609
2610 const uint32_t opcode = insn.opcode();
sfricke-samsung864162a2021-11-01 21:58:01 -07002611 if (ImageGatherOperation(opcode) || ImageSampleOperation(opcode) || ImageFetchOperation(opcode)) {
sfricke-samsung3a25ed52022-01-20 02:24:36 -08002612 uint32_t image_operand_position = OpcodeImageOperandsPosition(opcode);
sfricke-samsung864162a2021-11-01 21:58:01 -07002613 // Image operands can be optional
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002614 if (image_operand_position != 0 && insn.len() > image_operand_position) {
2615 auto image_operand = insn.word(image_operand_position);
sfricke-samsung864162a2021-11-01 21:58:01 -07002616 // Bits we are validating (sample/fetch only check ConstOffset)
ziga-lunarga12c75a2021-09-16 16:36:16 +02002617 uint32_t offset_bits =
sfricke-samsung864162a2021-11-01 21:58:01 -07002618 ImageGatherOperation(opcode)
2619 ? (spv::ImageOperandsOffsetMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsConstOffsetsMask)
2620 : (spv::ImageOperandsConstOffsetMask);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002621 if (image_operand & (offset_bits)) {
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002622 // Operand values follow
2623 uint32_t index = image_operand_position + 1;
ziga-lunarga12c75a2021-09-16 16:36:16 +02002624 // Each bit has it's own operand, starts with the smallest set bit and loop to the highest bit among
2625 // ImageOperandsOffsetMask, ImageOperandsConstOffsetMask and ImageOperandsConstOffsetsMask
2626 for (uint32_t i = 1; i < spv::ImageOperandsConstOffsetsMask; i <<= 1) {
2627 if (image_operand & i) { // If the bit is set, consume operand
2628 if (insn.len() > index && (i & offset_bits)) {
2629 uint32_t constant_id = insn.word(index);
sjfricke4f600c82022-06-09 14:21:32 +09002630 const auto &constant = module_state.get_def(constant_id);
2631 const bool is_dynamic_offset = constant == module_state.end();
Shahbaz Youssefi7a6a5272021-10-06 15:07:10 -04002632 if (!is_dynamic_offset && constant.opcode() == spv::OpConstantComposite) {
ziga-lunarga12c75a2021-09-16 16:36:16 +02002633 for (uint32_t j = 3; j < constant.len(); ++j) {
2634 uint32_t comp_id = constant.word(j);
sjfricke4f600c82022-06-09 14:21:32 +09002635 const auto &comp = module_state.get_def(comp_id);
2636 const auto &comp_type = module_state.get_def(comp.word(1));
ziga-lunarga12c75a2021-09-16 16:36:16 +02002637 // Get operand value
sfricke-samsungef3fe742021-10-06 10:51:34 -07002638 const uint32_t offset = comp.word(3);
sfricke-samsung864162a2021-11-01 21:58:01 -07002639 // spec requires minTexelGatherOffset/minTexelOffset to be -8 or less so never can compare if
2640 // unsigned spec requires maxTexelGatherOffset/maxTexelOffset to be 7 or greater so never can
2641 // compare if signed is less then zero
sfricke-samsungef3fe742021-10-06 10:51:34 -07002642 const int32_t signed_offset = static_cast<int32_t>(offset);
2643 const bool use_signed = (comp_type.opcode() == spv::OpTypeInt && comp_type.word(3) != 0);
2644
sfricke-samsung864162a2021-11-01 21:58:01 -07002645 // There are 2 sets of VU being covered where the only main difference is the opcode
2646 if (ImageGatherOperation(opcode)) {
2647 // min/maxTexelGatherOffset
2648 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelGatherOffset)) {
2649 skip |=
2650 LogError(device, "VUID-RuntimeSpirv-OpImage-06376",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002651 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsungef3fe742021-10-06 10:51:34 -07002652 ") less than VkPhysicalDeviceLimits::minTexelGatherOffset (%" PRIi32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002653 module_state.DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung73f1a0f2021-10-28 23:27:25 -07002654 phys_dev_props.limits.minTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002655 } else if ((offset > phys_dev_props.limits.maxTexelGatherOffset) &&
2656 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002657 skip |= LogError(device, "VUID-RuntimeSpirv-OpImage-06377",
2658 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2659 ") greater than VkPhysicalDeviceLimits::maxTexelGatherOffset (%" PRIu32
2660 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002661 module_state.DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002662 phys_dev_props.limits.maxTexelGatherOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002663 }
2664 } else {
2665 // min/maxTexelOffset
2666 if (use_signed && (signed_offset < phys_dev_props.limits.minTexelOffset)) {
2667 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06435",
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002668 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIi32
sfricke-samsung864162a2021-11-01 21:58:01 -07002669 ") less than VkPhysicalDeviceLimits::minTexelOffset (%" PRIi32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002670 module_state.DescribeInstruction(insn).c_str(), signed_offset,
sfricke-samsung864162a2021-11-01 21:58:01 -07002671 phys_dev_props.limits.minTexelOffset);
2672 } else if ((offset > phys_dev_props.limits.maxTexelOffset) &&
2673 (!use_signed || (use_signed && signed_offset > 0))) {
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002674 skip |= LogError(device, "VUID-RuntimeSpirv-OpImageSample-06436",
2675 "vkCreateShaderModule(): Shader uses\n%s\nwith offset (%" PRIu32
2676 ") greater than VkPhysicalDeviceLimits::maxTexelOffset (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09002677 module_state.DescribeInstruction(insn).c_str(), offset,
sfricke-samsung7a9bdca2022-01-24 14:38:03 -08002678 phys_dev_props.limits.maxTexelOffset);
sfricke-samsung864162a2021-11-01 21:58:01 -07002679 }
ziga-lunarga12c75a2021-09-16 16:36:16 +02002680 }
2681 }
2682 }
2683 }
sfricke-samsung3511e312021-11-04 21:14:31 -07002684 index += ImageOperandsParamCount(i);
ziga-lunarga12c75a2021-09-16 16:36:16 +02002685 }
2686 }
2687 }
2688 }
2689 }
2690
2691 return skip;
2692}
2693
sjfricke4f600c82022-06-09 14:21:32 +09002694bool CoreChecks::ValidateShaderClock(const SHADER_MODULE_STATE &module_state, spirv_inst_iter &insn) const {
sfricke-samsung486a51e2021-01-02 00:10:15 -08002695 bool skip = false;
2696
sfricke-samsung94167ca2021-02-26 04:14:59 -08002697 switch (insn.opcode()) {
2698 case spv::OpReadClockKHR: {
sjfricke4f600c82022-06-09 14:21:32 +09002699 auto scope_id = module_state.get_def(insn.word(3));
sfricke-samsung94167ca2021-02-26 04:14:59 -08002700 auto scope_type = scope_id.word(3);
2701 // if scope isn't Subgroup or Device, spirv-val will catch
sfricke-samsung828e59d2021-08-22 23:20:49 -07002702 if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_features.shaderSubgroupClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002703 skip |= LogError(device, "VUID-RuntimeSpirv-shaderSubgroupClock-06267",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002704 "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002705 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2706 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung828e59d2021-08-22 23:20:49 -07002707 } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_features.shaderDeviceClock == VK_FALSE)) {
sfricke-samsung1ff329f2021-09-16 10:06:47 -07002708 skip |= LogError(device, "VUID-RuntimeSpirv-shaderDeviceClock-06268",
sfricke-samsunged00aa42022-01-27 19:03:01 -08002709 "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.\n%s",
sjfricke4f600c82022-06-09 14:21:32 +09002710 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
2711 module_state.DescribeInstruction(insn).c_str());
sfricke-samsung486a51e2021-01-02 00:10:15 -08002712 }
sfricke-samsung94167ca2021-02-26 04:14:59 -08002713 break;
sfricke-samsung486a51e2021-01-02 00:10:15 -08002714 }
2715 }
2716 return skip;
2717}
2718
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002719bool CoreChecks::ValidatePipelineShaderStage(const PIPELINE_STATE *pipeline, const PipelineStageState &stage_state,
2720 bool check_point_size) const {
John Zulauf14c355b2019-06-27 16:09:37 -06002721 bool skip = false;
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002722 const auto *pStage = stage_state.create_info;
sjfricke4f600c82022-06-09 14:21:32 +09002723 const SHADER_MODULE_STATE &module_state = *stage_state.module_state.get();
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002724 const auto &entrypoint = stage_state.entrypoint;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002725
Tony-LunarG1672d002022-08-03 14:35:34 -06002726 skip |= ValidateShaderModuleId(module_state, stage_state, pStage, pipeline->GetPipelineCreateFlags());
2727
Tony-LunarGcab5d812022-08-04 14:07:32 -06002728 if (module_state.vk_shader_module() == VK_NULL_HANDLE) return skip; // No real shader for further validation
2729
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002730 // to prevent const_cast on pipeline object, just store here as not needed outside function anyway
2731 uint32_t local_size_x = 0;
2732 uint32_t local_size_y = 0;
2733 uint32_t local_size_z = 0;
sjfrickede734312022-07-14 19:22:43 +09002734 uint32_t total_shared_size = 0;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002735
John Zulauf14c355b2019-06-27 16:09:37 -06002736 // Check the module
sjfricke4f600c82022-06-09 14:21:32 +09002737 if (!module_state.has_valid_spirv) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002738 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2739 "%s does not contain valid spirv for stage %s.",
sjfricke4f600c82022-06-09 14:21:32 +09002740 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
sfricke-samsungef15e482022-01-26 11:32:49 -08002741 string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002742 }
2743
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002744 // If specialization-constant instructions are present in the shader, the specializations should be applied.
sjfricke4f600c82022-06-09 14:21:32 +09002745 if (module_state.HasSpecConstants()) {
sfricke-samsung5628f982021-10-19 09:21:59 -07002746 // both spirv-opt and spirv-val will use the same flags
2747 spvtools::ValidatorOptions options;
2748 AdjustValidatorOptions(device_extensions, enabled_features, options);
2749
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002750 // setup the call back if the optimizer fails
sfricke-samsung45996a42021-09-16 13:45:27 -07002751 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002752 spvtools::Optimizer optimizer(spirv_environment);
sfricke-samsungef15e482022-01-26 11:32:49 -08002753 spvtools::MessageConsumer consumer = [&skip, &module_state, &stage_state, this](
2754 spv_message_level_t level, const char *source, const spv_position_t &position,
2755 const char *message) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002756 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter",
2757 "%s does not contain valid spirv for stage %s. %s",
sjfricke4f600c82022-06-09 14:21:32 +09002758 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002759 string_VkShaderStageFlagBits(stage_state.stage_flag), message);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002760 };
2761 optimizer.SetMessageConsumer(consumer);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002762
2763 // The app might be using the default spec constant values, but if they pass values at runtime to the pipeline then need to
2764 // use those values to apply to the spec constants
2765 if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 &&
2766 pStage->pSpecializationInfo->pMapEntries != nullptr) {
2767 // Gather the specialization-constant values.
2768 auto const &specialization_info = pStage->pSpecializationInfo;
2769 auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData);
2770 std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; // note: this must be std:: to work with spvtools
2771 id_value_map.reserve(specialization_info->mapEntryCount);
2772 for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) {
2773 auto const &map_entry = specialization_info->pMapEntries[i];
sjfricke4f600c82022-06-09 14:21:32 +09002774 const auto itr = module_state.GetSpecConstMap().find(map_entry.constantID);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002775 // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the
2776 // behavior of the pipeline."
sjfricke4f600c82022-06-09 14:21:32 +09002777 if (itr != module_state.GetSpecConstMap().cend()) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002778 // Make sure map_entry.size matches the spec constant's size
2779 uint32_t spec_const_size = decoration_set::kInvalidValue;
sjfricke4f600c82022-06-09 14:21:32 +09002780 const auto def_ins = module_state.get_def(itr->second);
2781 const auto type_ins = module_state.get_def(def_ins.word(1));
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002782 // Specialization constants can only be of type bool, scalar integer, or scalar floating point
2783 switch (type_ins.opcode()) {
2784 case spv::OpTypeBool:
2785 // "If the specialization constant is of type boolean, size must be the byte size of VkBool32"
2786 spec_const_size = sizeof(VkBool32);
2787 break;
2788 case spv::OpTypeInt:
2789 case spv::OpTypeFloat:
2790 spec_const_size = type_ins.word(2) / 8;
2791 break;
2792 default:
2793 // spirv-val should catch if SpecId is not used on a
2794 // OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant and OpSpecConstant is validated to be a
2795 // OpTypeInt or OpTypeFloat
2796 break;
2797 }
2798
2799 if (map_entry.size != spec_const_size) {
2800 skip |= LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776",
2801 "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32
2802 ") has invalid size %zu in shader module %s. Expected size is %" PRIu32
2803 " from shader definition.",
2804 map_entry.constantID, i, map_entry.size,
sjfricke4f600c82022-06-09 14:21:32 +09002805 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), spec_const_size);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002806 }
2807 }
2808
2809 if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) {
2810 // Allocate enough room for ceil(map_entry.size / 4) to store entries
2811 std::vector<uint32_t> entry_data((map_entry.size + 4 - 1) / 4, 0);
2812 uint8_t *out_p = reinterpret_cast<uint8_t *>(entry_data.data());
2813 const uint8_t *const start_in_p = specialization_data + map_entry.offset;
2814 const uint8_t *const end_in_p = start_in_p + map_entry.size;
2815
2816 std::copy(start_in_p, end_in_p, out_p);
2817 id_value_map.emplace(map_entry.constantID, std::move(entry_data));
2818 }
2819 }
2820
2821 // This pass takes the runtime spec const values and applies it into the SPIR-V
2822 // will turn a spec constant like
2823 // OpSpecConstant %uint 1
2824 // to a use the value passed in instead (for example if the value is 32) so now it looks like
2825 // OpSpecConstant %uint 32
2826 optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map));
2827 }
2828
2829 // This pass will turn OpSpecConstant into a OpConstant (also OpSpecConstantTrue/OpSpecConstantFalse)
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002830 optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass());
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002831 // Using the new frozen OpConstant all OpSpecConstantComposite can be resolved turning them into OpConstantComposite
2832 // This is need incase a shdaer looks like:
2833 //
2834 // layout(constant_id = 0) const uint x = 64;
2835 // shared uint arr[x > 64 ? 64 : x];
2836 //
2837 // this will generate branch/switch statements that we want to leverage spirv-opt to apply to make parsing easier
2838 optimizer.RegisterPass(spvtools::CreateFoldSpecConstantOpAndCompositePass());
sjfricke284a13f2022-08-16 15:34:31 +09002839 // Currently need to re-run the pass as spirv-opt has a bug and not folding everything sometimes
2840 // https://github.com/KhronosGroup/Vulkan-ValidationLayers/pull/4399#issuecomment-1216203563
2841 optimizer.RegisterPass(spvtools::CreateFoldSpecConstantOpAndCompositePass());
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002842
2843 // Apply the specialization-constant values and revalidate the shader module is valid.
Tony-LunarG1672d002022-08-03 14:35:34 -06002844 const char *pSpecializationInfo_vuid = IsExtEnabled(device_extensions.vk_ext_shader_module_identifier)
2845 ? "VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06849"
2846 : "VUID-VkPipelineShaderStageCreateInfo-pSpecializationInfo-06719";
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002847 std::vector<uint32_t> specialized_spirv;
sfricke-samsungef15e482022-01-26 11:32:49 -08002848 auto const optimized =
sjfricke4f600c82022-06-09 14:21:32 +09002849 optimizer.Run(module_state.words.data(), module_state.words.size(), &specialized_spirv, options, false);
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002850 if (optimized) {
2851 spv_context ctx = spvContextCreate(spirv_environment);
2852 spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()};
2853 spv_diagnostic diag = nullptr;
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002854 auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
2855 if (spv_valid != SPV_SUCCESS) {
Tony-LunarG1672d002022-08-03 14:35:34 -06002856 skip |= LogError(device, pSpecializationInfo_vuid,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07002857 "After specialization was applied, %s does not contain valid spirv for stage %s.",
sjfricke4f600c82022-06-09 14:21:32 +09002858 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002859 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002860 }
2861
sjfrickea11e42e2022-07-20 14:27:01 +09002862 // The new optimized SPIR-V will NOT match the original SHADER_MODULE_STATE object parsing, so a new SHADER_MODULE_STATE
2863 // object is needed. This an issue due to each pipeline being able to reuse the same shader module but with different
2864 // spec constant values.
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002865 SHADER_MODULE_STATE spec_mod(specialized_spirv);
2866
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002867 // According to https://github.com/KhronosGroup/Vulkan-Docs/issues/1671 anything labeled as "static use" (such as if an
2868 // input is used or not) don't have to be checked post spec constants freezing since the device compiler is not
2869 // guaranteed to run things such as dead-code elimination. The following checks are things that don't follow under
2870 // "static use" rules and need to be validated still.
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002871 auto specialized_it = spec_mod.begin();
sjfrickede734312022-07-14 19:22:43 +09002872
2873 // see ValidateComputeSharedMemory() for details why we might track max block size
2874 layer_data::unordered_set<uint32_t> aliased_id;
2875 bool find_max_block = false;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002876
2877 uint32_t workgroup_size_id = 0; // result id can't be zero
2878 uint32_t local_size_id_x = 0;
2879 uint32_t local_size_id_y = 0;
2880 uint32_t local_size_id_z = 0;
2881
2882 // make single interation through new shader
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002883 while (specialized_it != spec_mod.end()) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002884 const uint32_t opcode = specialized_it.opcode();
2885
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002886 if (opcode == spv::OpExecutionModeId && specialized_it.word(2) == spv::ExecutionModeLocalSizeId) {
2887 local_size_id_x = specialized_it.word(3);
2888 local_size_id_y = specialized_it.word(4);
2889 local_size_id_z = specialized_it.word(5);
2890 }
2891
sjfrickede734312022-07-14 19:22:43 +09002892 if (opcode == spv::OpDecorate) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002893 // Validate applied WorkgroupSize is still below maxComputeWorkGroupSize limit
sjfrickede734312022-07-14 19:22:43 +09002894 if (specialized_it.word(2) == spv::DecorationBuiltIn && specialized_it.word(3) == spv::BuiltInWorkgroupSize) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002895 // Will be a OpConstantComposite and always have the OpDecorate section
2896 workgroup_size_id = specialized_it.word(1);
2897 }
sjfrickede734312022-07-14 19:22:43 +09002898 if (specialized_it.word(2) == spv::DecorationAliased) {
2899 aliased_id.emplace(specialized_it.word(1));
2900 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002901 }
2902
2903 if (opcode == spv::OpConstantComposite && workgroup_size_id == specialized_it.word(2)) {
2904 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32 so this can be assuemd
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002905 local_size_x = spec_mod.get_def(specialized_it.word(3)).word(3);
2906 local_size_y = spec_mod.get_def(specialized_it.word(4)).word(3);
2907 local_size_z = spec_mod.get_def(specialized_it.word(5)).word(3);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002908 }
sjfrickede734312022-07-14 19:22:43 +09002909
2910 if (opcode == spv::OpVariable && specialized_it.word(3) == spv::StorageClassWorkgroup) {
2911 if (aliased_id.find(specialized_it.word(2)) != aliased_id.end()) {
2912 find_max_block = true;
2913 }
2914
2915 const uint32_t result_type_id = specialized_it.word(1);
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002916 const auto result_type = spec_mod.get_def(result_type_id);
2917 const auto type = spec_mod.get_def(result_type.word(3));
2918 const uint32_t variable_shared_size = spec_mod.GetTypeBitsSize(type) / 8;
sjfrickede734312022-07-14 19:22:43 +09002919
2920 if (find_max_block) {
2921 total_shared_size = std::max(total_shared_size, variable_shared_size);
2922 } else {
2923 total_shared_size += variable_shared_size;
2924 }
2925 }
2926
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002927 ++specialized_it;
2928 }
2929
2930 // if after no WorkgroupSize is found, then can apply any possible LocalSizeId due to precedence order
2931 if (local_size_x == 0 && local_size_id_x != 0) {
Nathaniel Cesarioea997aa2022-07-19 10:43:57 -06002932 local_size_x = spec_mod.get_def(local_size_id_x).word(3);
2933 local_size_y = spec_mod.get_def(local_size_id_y).word(3);
2934 local_size_z = spec_mod.get_def(local_size_id_z).word(3);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002935 }
2936
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002937 spvDiagnosticDestroy(diag);
2938 spvContextDestroy(ctx);
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002939 } else {
2940 // Should never get here, but better then asserting
Tony-LunarG1672d002022-08-03 14:35:34 -06002941 skip |= LogError(device, pSpecializationInfo_vuid,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002942 "%s module (stage %s) attempted to apply specialization constants with spirv-opt but failed.",
sjfricke4f600c82022-06-09 14:21:32 +09002943 report_data->FormatHandle(module_state.vk_shader_module()).c_str(),
sfricke-samsung61d50ec2022-02-13 17:01:25 -08002944 string_VkShaderStageFlagBits(stage_state.stage_flag));
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06002945 }
2946 }
2947
John Zulauf14c355b2019-06-27 16:09:37 -06002948 // Check the entrypoint
sjfricke4f600c82022-06-09 14:21:32 +09002949 if (entrypoint == module_state.end()) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002950 skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.",
2951 pStage->pName, string_VkShaderStageFlagBits(stage_state.stage_flag));
John Zulauf14c355b2019-06-27 16:09:37 -06002952 }
2953 if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage.
2954
2955 // Mark accessible ids
2956 auto &accessible_ids = stage_state.accessible_ids;
2957
Chris Forbes47567b72017-06-09 12:09:45 -07002958 // Validate descriptor set layout against what the entrypoint actually uses
Chris Forbes47567b72017-06-09 12:09:45 -07002959
sfricke-samsung94167ca2021-02-26 04:14:59 -08002960 // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless"
2961 // and mainly only checking the instruction in detail for a single operation
sjfricke4f600c82022-06-09 14:21:32 +09002962 for (auto insn : module_state) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002963 skip |= ValidateTexelOffsetLimits(module_state, insn);
2964 skip |= ValidateShaderCapabilitiesAndExtensions(insn);
2965 skip |= ValidateShaderClock(module_state, insn);
2966 skip |= ValidateShaderStageGroupNonUniform(module_state, pStage->stage, insn);
2967 skip |= ValidateMemoryScope(module_state, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08002968
2969 // Checks based off shaderStorageImage(Read|Write)WithoutFormat are
2970 // disabled if VK_KHR_format_feature_flags2 is supported.
2971 //
2972 // https://github.com/KhronosGroup/Vulkan-Docs/blob/6177645341afc/appendices/spirvenv.txt#L553
2973 //
2974 // The other checks need to take into account the format features and so
2975 // we apply that in the descriptor set matching validation code (see
2976 // descriptor_sets.cpp).
2977 if (!has_format_feature2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002978 skip |= ValidateShaderStorageImageFormats(module_state, insn);
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08002979 }
ziga-lunarga26b3602021-08-08 15:53:00 +02002980 }
2981
sfricke-samsungef15e482022-01-26 11:32:49 -08002982 skip |= ValidateTransformFeedback(module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06002983 skip |= ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, stage_state.has_writable_descriptor,
2984 stage_state.has_atomic_descriptor);
sfricke-samsungef15e482022-01-26 11:32:49 -08002985 skip |= ValidateShaderStageInputOutputLimits(module_state, pStage, pipeline, entrypoint);
sfricke-samsungdc96f302020-03-18 20:42:10 -07002986 skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline);
sfricke-samsungef15e482022-01-26 11:32:49 -08002987 skip |= ValidateAtomicsTypes(module_state);
2988 skip |= ValidateExecutionModes(module_state, entrypoint, pStage->stage, pipeline);
ziga-lunargae2a5c42021-07-23 16:18:09 +02002989 skip |= ValidateSpecializations(pStage);
sfricke-samsungef15e482022-01-26 11:32:49 -08002990 skip |= ValidateDecorations(module_state);
sjfricke4f600c82022-06-09 14:21:32 +09002991 skip |= ValidateVariables(module_state);
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002992 const auto *raster_state = pipeline->RasterizationState();
Nathaniel Cesario81257cb2022-02-16 17:15:58 -07002993 if (check_point_size && raster_state && !raster_state->rasterizerDiscardEnable) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002994 skip |= ValidatePointListShaderState(pipeline, module_state, entrypoint, pStage->stage);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06002995 }
sfricke-samsungef15e482022-01-26 11:32:49 -08002996 skip |= ValidateBuiltinLimits(module_state, entrypoint);
sfricke-samsungd093e522021-02-26 04:17:45 -08002997 if (enabled_features.cooperative_matrix_features.cooperativeMatrix) {
sfricke-samsungef15e482022-01-26 11:32:49 -08002998 skip |= ValidateCooperativeMatrix(module_state, pStage, pipeline);
sfricke-samsungd093e522021-02-26 04:17:45 -08002999 }
Tobias Hector6663c9b2020-11-05 10:18:02 +00003000 if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003001 skip |= ValidatePrimitiveRateShaderState(pipeline, module_state, entrypoint, pStage->stage);
Tobias Hector6663c9b2020-11-05 10:18:02 +00003002 }
sfricke-samsung45996a42021-09-16 13:45:27 -07003003 if (IsExtEnabled(device_extensions.vk_qcom_render_pass_shader_resolve)) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003004 skip |= ValidateShaderResolveQCOM(module_state, pStage, pipeline);
Jeff Leger9b3dcff2021-05-27 15:40:20 -04003005 }
ziga-lunarg73163742021-08-25 13:15:29 +02003006 if (IsExtEnabled(device_extensions.vk_ext_subgroup_size_control)) {
3007 skip |= ValidateShaderSubgroupSizeControl(pStage);
3008 }
Chris Forbes47567b72017-06-09 12:09:45 -07003009
sfricke-samsung7699b912021-04-12 23:01:51 -07003010 // "layout must be consistent with the layout of the * shader"
3011 // 'consistent' -> #descriptorsets-pipelinelayout-consistency
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003012 std::string vuid_layout_mismatch;
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003013 switch (pipeline->GetCreateInfoSType()) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003014 case VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO:
3015 vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756";
3016 break;
3017 case VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO:
3018 vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703";
3019 break;
3020 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR:
3021 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427";
3022 break;
3023 case VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV:
3024 vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427";
3025 break;
3026 default:
3027 assert(false);
3028 break;
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003029 }
3030
sfricke-samsung7699b912021-04-12 23:01:51 -07003031 // Validate Push Constants use
sfricke-samsungef15e482022-01-26 11:32:49 -08003032 skip |= ValidatePushConstantUsage(*pipeline, module_state, pStage, vuid_layout_mismatch);
sfricke-samsung7699b912021-04-12 23:01:51 -07003033
Chris Forbes47567b72017-06-09 12:09:45 -07003034 // Validate descriptor use
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003035 for (auto use : stage_state.descriptor_uses) {
Chris Forbes47567b72017-06-09 12:09:45 -07003036 // Verify given pipelineLayout has requested setLayout with requested binding
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003037 // const auto& layout_state = (stage_state.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) ?
3038 // pipeline->PreRasterPipelineLayoutState() : pipeline->FragmentShaderPipelineLayoutState();
3039 const auto &binding = GetDescriptorBinding(pipeline->PipelineLayoutState().get(), use.first);
3040 unsigned required_descriptor_count;
sourav parmarcd5fb182020-07-17 12:58:44 -07003041 bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
3042 std::set<uint32_t> descriptor_types =
sfricke-samsungef15e482022-01-26 11:32:49 -08003043 TypeToDescriptorTypeSet(module_state, use.second.type_id, required_descriptor_count, is_khr);
Chris Forbes47567b72017-06-09 12:09:45 -07003044
3045 if (!binding) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003046 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003047 "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003048 use.first.set, use.first.binding, string_descriptorTypes(descriptor_types).c_str());
Chris Forbes47567b72017-06-09 12:09:45 -07003049 } else if (~binding->stageFlags & pStage->stage) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003050 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003051 "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.set,
3052 use.first.binding, string_VkShaderStageFlagBits(pStage->stage));
Tony-LunarGf563b362021-03-18 16:13:18 -06003053 } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) &&
3054 (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003055 skip |= LogError(device, vuid_layout_mismatch,
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003056 "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.set,
3057 use.first.binding, string_descriptorTypes(descriptor_types).c_str(),
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003058 string_VkDescriptorType(binding->descriptorType));
Chris Forbes47567b72017-06-09 12:09:45 -07003059 } else if (binding->descriptorCount < required_descriptor_count) {
locke-lunarg9a16ebb2020-07-30 16:56:33 -06003060 skip |= LogError(device, vuid_layout_mismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003061 "Shader expects at least %u descriptors for binding %u.%u but only %u provided",
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06003062 required_descriptor_count, use.first.set, use.first.binding, binding->descriptorCount);
Chris Forbes47567b72017-06-09 12:09:45 -07003063 }
3064 }
3065
3066 // Validate use of input attachments against subpass structure
3067 if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09003068 auto input_attachment_uses = module_state.CollectInterfaceByInputAttachmentIndex(accessible_ids);
Chris Forbes47567b72017-06-09 12:09:45 -07003069
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003070 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06003071 if (rp_state && !rp_state->UsesDynamicRendering()) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003072 auto rpci = rp_state->createInfo.ptr();
3073 auto subpass = pipeline->Subpass();
amhagana448ea52021-11-02 14:09:14 -04003074 for (auto use : input_attachment_uses) {
3075 auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments;
3076 auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount)
3077 ? input_attachments[use.first].attachment
3078 : VK_ATTACHMENT_UNUSED;
Chris Forbes47567b72017-06-09 12:09:45 -07003079
amhagana448ea52021-11-02 14:09:14 -04003080 if (index == VK_ATTACHMENT_UNUSED) {
3081 skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment,
3082 "Shader consumes input attachment index %d but not provided in subpass", use.first);
sfricke-samsungef15e482022-01-26 11:32:49 -08003083 } else if (!(GetFormatType(rpci->pAttachments[index].format) &
sjfricke4f600c82022-06-09 14:21:32 +09003084 module_state.GetFundamentalType(use.second.type_id))) {
sfricke-samsungef15e482022-01-26 11:32:49 -08003085 skip |= LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch,
3086 "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first,
3087 string_VkFormat(rpci->pAttachments[index].format),
sjfricke4f600c82022-06-09 14:21:32 +09003088 module_state.DescribeType(use.second.type_id).c_str());
amhagana448ea52021-11-02 14:09:14 -04003089 }
Chris Forbes47567b72017-06-09 12:09:45 -07003090 }
3091 }
3092 }
Lockeaa8fdc02019-04-02 11:59:20 -06003093 if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003094 skip |= ValidateComputeWorkGroupSizes(module_state, entrypoint, stage_state, local_size_x, local_size_y, local_size_z);
sjfrickede734312022-07-14 19:22:43 +09003095 skip |= ValidateComputeSharedMemory(module_state, total_shared_size);
Lockeaa8fdc02019-04-02 11:59:20 -06003096 }
ziga-lunarg73163742021-08-25 13:15:29 +02003097
Chris Forbes47567b72017-06-09 12:09:45 -07003098 return skip;
3099}
3100
sjfricke4f600c82022-06-09 14:21:32 +09003101bool CoreChecks::ValidateInterfaceBetweenStages(const SHADER_MODULE_STATE &producer, spirv_inst_iter producer_entrypoint,
3102 shader_stage_attributes const *producer_stage, const SHADER_MODULE_STATE &consumer,
Mark Lobodzinskid8d658e2020-01-30 15:05:51 -07003103 spirv_inst_iter consumer_entrypoint,
3104 shader_stage_attributes const *consumer_stage) const {
Chris Forbes47567b72017-06-09 12:09:45 -07003105 bool skip = false;
3106
3107 auto outputs =
sjfricke4f600c82022-06-09 14:21:32 +09003108 producer.CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output);
3109 auto inputs = consumer.CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input);
Chris Forbes47567b72017-06-09 12:09:45 -07003110
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003111 auto output_it = outputs.begin();
3112 auto input_it = inputs.begin();
Chris Forbes47567b72017-06-09 12:09:45 -07003113
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003114 uint32_t output_component = 0;
3115 uint32_t input_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003116
Chris Forbes47567b72017-06-09 12:09:45 -07003117 // Maps sorted by key (location); walk them together to find mismatches
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003118 while ((outputs.size() > 0 && output_it != outputs.end()) || (inputs.size() && input_it != inputs.end())) {
3119 bool output_at_end = outputs.size() == 0 || output_it == outputs.end();
3120 bool input_at_end = inputs.size() == 0 || input_it == inputs.end();
3121 auto output_first = output_at_end ? std::make_pair(0u, 0u) : output_it->first;
3122 auto input_first = input_at_end ? std::make_pair(0u, 0u) : input_it->first;
Chris Forbes47567b72017-06-09 12:09:45 -07003123
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003124 output_first.second += output_component;
3125 input_first.second += input_component;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003126
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003127 const auto output_length =
3128 output_at_end ? 0 : producer.GetNumComponentsInBaseType(producer.get_def(output_it->second.type_id));
3129 const auto input_length =
3130 input_at_end ? 0 : consumer.GetNumComponentsInBaseType(consumer.get_def(input_it->second.type_id));
3131 assert(output_at_end || output_component < output_length);
3132 assert(input_at_end || input_component < input_length);
ziga-lunarg8346fe82021-08-22 17:30:50 +02003133
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003134 if (input_at_end || ((!output_at_end) && (output_first < input_first))) {
Stefan Dobrica43c84ca2022-05-30 16:22:36 +02003135 if (!enabled_features.core13.maintenance4) {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003136 const std::string msg = std::string{producer_stage->name} + " writes to output location " +
3137 std::to_string(output_first.first) + "." + std::to_string(output_first.second) +
3138 " which is not consumed by " + consumer_stage->name +
Nathaniel Cesario09fbe8a2022-08-03 16:24:25 -06003139 ". "
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003140 "Enable VK_KHR_maintenance4 device extension to allow relaxed interface matching between "
3141 "input and output vectors.";
Nathaniel Cesario09fbe8a2022-08-03 16:24:25 -06003142 // It is not an error if a stage does not consume all outputs from the previous stage
3143 skip |= LogPerformanceWarning(producer.vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed, "%s", msg.c_str());
Stefan Dobrica43c84ca2022-05-30 16:22:36 +02003144 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003145 if ((input_first.first > output_first.first) || input_at_end || (output_component + 1 == output_length)) {
3146 output_it++;
3147 output_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003148 } else {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003149 output_component++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003150 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003151 } else if (output_at_end || output_first > input_first) {
sjfricke4f600c82022-06-09 14:21:32 +09003152 skip |= LogError(consumer.vk_shader_module(), kVUID_Core_Shader_InputNotProduced,
ziga-lunarg8346fe82021-08-22 17:30:50 +02003153 "%s consumes input location %" PRIu32 ".%" PRIu32 " which is not written by %s", consumer_stage->name,
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003154 input_first.first, input_first.second, producer_stage->name);
3155 if ((output_first.first > input_first.first) || output_at_end || (input_component + 1 == input_length)) {
3156 input_it++;
3157 input_component = 0;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003158 } else {
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003159 input_component++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003160 }
Chris Forbes47567b72017-06-09 12:09:45 -07003161 } else {
3162 // subtleties of arrayed interfaces:
3163 // - if is_patch, then the member is not arrayed, even though the interface may be.
3164 // - if is_block_member, then the extra array level of an arrayed interface is not
3165 // expressed in the member type -- it's expressed in the block type.
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003166 if (!TypesMatch(producer, consumer, output_it->second.type_id, input_it->second.type_id)) {
sjfricke4f600c82022-06-09 14:21:32 +09003167 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
ziga-lunarge640e802022-04-04 21:36:53 +02003168 "Type mismatch on location %" PRIu32 ".%" PRIu32 ", between %s and %s: '%s' vs '%s'",
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003169 output_first.first, output_first.second, producer_stage->name, consumer_stage->name,
3170 producer.DescribeType(output_it->second.type_id).c_str(),
3171 consumer.DescribeType(input_it->second.type_id).c_str());
3172 output_it++;
3173 input_it++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003174 continue;
Chris Forbes47567b72017-06-09 12:09:45 -07003175 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003176 if (output_it->second.is_patch != input_it->second.is_patch) {
sjfricke4f600c82022-06-09 14:21:32 +09003177 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
3178 "Decoration mismatch on location %" PRIu32 ".%" PRIu32
3179 ": is per-%s in %s stage but per-%s in %s stage",
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003180 output_first.first, output_first.second, output_it->second.is_patch ? "patch" : "vertex",
3181 producer_stage->name, input_it->second.is_patch ? "patch" : "vertex", consumer_stage->name);
Chris Forbes47567b72017-06-09 12:09:45 -07003182 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003183 uint32_t output_remaining = output_length - output_component;
3184 uint32_t input_remaining = input_length - input_component;
3185 if (output_remaining == input_remaining) { // Sizes match so we can advance both output_it and input_it
3186 output_it++;
3187 input_it++;
3188 output_component = 0;
3189 input_component = 0;
3190 } else if (output_remaining > input_remaining) { // a has more components remaining
3191 output_component += input_remaining;
3192 input_component = 0;
3193 input_it++;
3194 } else if (input_remaining > output_remaining) { // b has more components remaining
3195 input_component += output_remaining;
3196 output_component = 0;
3197 output_it++;
ziga-lunarg8346fe82021-08-22 17:30:50 +02003198 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003199 if (output_component == 4) {
3200 output_component = 0;
3201 output_it++;
ziga-lunargb9fa0eb2022-04-01 23:31:06 +02003202 }
Nathaniel Cesario54acc5c2022-07-28 14:44:20 -06003203 if (input_component == 4) {
3204 input_component = 0;
3205 input_it++;
ziga-lunargb9fa0eb2022-04-01 23:31:06 +02003206 }
Chris Forbes47567b72017-06-09 12:09:45 -07003207 }
3208 }
3209
Ari Suonpaa696b3432019-03-11 14:02:57 +02003210 if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) {
sjfricke4f600c82022-06-09 14:21:32 +09003211 auto builtins_producer = producer.CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput);
3212 auto builtins_consumer = consumer.CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput);
Ari Suonpaa696b3432019-03-11 14:02:57 +02003213
3214 if (!builtins_producer.empty() && !builtins_consumer.empty()) {
3215 if (builtins_producer.size() != builtins_consumer.size()) {
sjfricke4f600c82022-06-09 14:21:32 +09003216 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003217 "Number of elements inside builtin block differ between stages (%s %d vs %s %d).",
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07003218 producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name,
3219 static_cast<int>(builtins_consumer.size()));
Ari Suonpaa696b3432019-03-11 14:02:57 +02003220 } else {
3221 auto it_producer = builtins_producer.begin();
3222 auto it_consumer = builtins_consumer.begin();
3223 while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) {
3224 if (*it_producer != *it_consumer) {
sjfricke4f600c82022-06-09 14:21:32 +09003225 skip |= LogError(producer.vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch,
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003226 "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name,
3227 consumer_stage->name);
Ari Suonpaa696b3432019-03-11 14:02:57 +02003228 break;
3229 }
3230 it_producer++;
3231 it_consumer++;
3232 }
3233 }
3234 }
3235 }
3236
Chris Forbes47567b72017-06-09 12:09:45 -07003237 return skip;
3238}
3239
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003240static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE &pipeline) {
3241 uint32_t stage_mask = pipeline.active_shaders;
3242 if (pipeline.topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003243 // Determine which shader in which PointSize should be written (the final geometry stage)
Jeff Bolz105d6492018-09-29 15:46:44 -05003244 if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) {
3245 stage_mask = VK_SHADER_STAGE_MESH_BIT_NV;
3246 } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) {
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003247 stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT;
3248 } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) {
3249 stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
3250 } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) {
3251 stage_mask = VK_SHADER_STAGE_VERTEX_BIT;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003252 }
3253 }
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003254 return stage_mask;
Mark Lobodzinski2c984cc2018-07-31 09:57:46 -06003255}
3256
Chris Forbes47567b72017-06-09 12:09:45 -07003257// Validate that the shaders used by the given pipeline and store the active_slots
3258// that are actually used by the pipeline into pPipeline->active_slots
John Zulaufac4c6e12019-07-01 16:05:58 -06003259bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const {
Chris Forbes47567b72017-06-09 12:09:45 -07003260 bool skip = false;
3261
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003262 if (pipeline->IsGraphicsLibrary()) {
3263 // Only validate stages in an executable pipeline, not a graphics library
3264 // TODO This currently makes executing executable pipeline more expensive than they need to be since we could be validating
3265 // more per library.
3266 return skip;
3267 }
3268
3269 uint32_t pointlist_stage_mask = DetermineFinalGeomStage(*pipeline);
Mark Lobodzinski1b4a8ed2018-08-07 08:47:05 -06003270
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003271 const PipelineStageState *vertex_stage = nullptr, *fragment_stage = nullptr;
3272 for (auto &stage : pipeline->stage_state) {
3273 skip |= ValidatePipelineShaderStage(pipeline, stage, (pointlist_stage_mask == stage.stage_flag));
3274 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT) {
3275 vertex_stage = &stage;
3276 }
3277 if (stage.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT) {
3278 fragment_stage = &stage;
3279 }
Chris Forbes47567b72017-06-09 12:09:45 -07003280 }
3281
3282 // if the shader stages are no good individually, cross-stage validation is pointless.
3283 if (skip) return true;
3284
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003285 auto vi_state = pipeline->InputState();
Chris Forbes47567b72017-06-09 12:09:45 -07003286
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003287 if (vi_state) {
3288 skip |= ValidateViConsistency(vi_state);
Chris Forbes47567b72017-06-09 12:09:45 -07003289 }
3290
sfricke-samsungef15e482022-01-26 11:32:49 -08003291 if (vertex_stage && vertex_stage->module_state->has_valid_spirv && !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) {
sjfricke4f600c82022-06-09 14:21:32 +09003292 skip |= ValidateViAgainstVsInputs(vi_state, *vertex_stage->module_state.get(), vertex_stage->entrypoint);
Chris Forbes47567b72017-06-09 12:09:45 -07003293 }
3294
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003295 for (size_t i = 1; i < pipeline->stage_state.size(); i++) {
3296 const auto &producer = pipeline->stage_state[i - 1];
3297 const auto &consumer = pipeline->stage_state[i];
sfricke-samsungef15e482022-01-26 11:32:49 -08003298 assert(producer.module_state);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003299 if (&producer == fragment_stage) {
3300 break;
3301 }
sfricke-samsungef15e482022-01-26 11:32:49 -08003302 if (consumer.module_state) {
3303 if (consumer.module_state->has_valid_spirv && producer.module_state->has_valid_spirv) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003304 auto producer_id = GetShaderStageId(producer.stage_flag);
3305 auto consumer_id = GetShaderStageId(consumer.stage_flag);
sjfricke4f600c82022-06-09 14:21:32 +09003306 skip |= ValidateInterfaceBetweenStages(*producer.module_state.get(), producer.entrypoint,
3307 &shader_stage_attribs[producer_id], *consumer.module_state.get(),
sfricke-samsungef15e482022-01-26 11:32:49 -08003308 consumer.entrypoint, &shader_stage_attribs[consumer_id]);
Chris Forbesdbb43fc2018-02-16 16:59:23 -08003309 }
Chris Forbes47567b72017-06-09 12:09:45 -07003310 }
3311 }
3312
sfricke-samsungef15e482022-01-26 11:32:49 -08003313 if (fragment_stage && fragment_stage->module_state->has_valid_spirv) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003314 const auto &rp_state = pipeline->RenderPassState();
Jeremy Gebbenb5dda542022-08-02 14:26:20 -06003315 if (rp_state && rp_state->UsesDynamicRendering()) {
sjfricke4f600c82022-06-09 14:21:32 +09003316 skip |= ValidateFsOutputsAgainstDynamicRenderingRenderPass(*fragment_stage->module_state.get(),
sfricke-samsungef15e482022-01-26 11:32:49 -08003317 fragment_stage->entrypoint, pipeline);
Aaron Hagan1209c782021-11-22 19:37:14 -05003318 } else {
sjfricke4f600c82022-06-09 14:21:32 +09003319 skip |= ValidateFsOutputsAgainstRenderPass(*fragment_stage->module_state.get(), fragment_stage->entrypoint, pipeline,
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003320 pipeline->Subpass());
Aaron Hagan1209c782021-11-22 19:37:14 -05003321 }
Chris Forbes47567b72017-06-09 12:09:45 -07003322 }
3323
3324 return skip;
3325}
3326
Tony-LunarGb2ded512021-02-02 16:03:30 -07003327bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB,
3328 const char *caller, const DrawDispatchVuid &vuid) const {
Tony-LunarGb2ded512021-02-02 16:03:30 -07003329 bool skip = false;
3330
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003331 for (auto &stage : pipeline->stage_state) {
3332 if (stage.stage_flag == VK_SHADER_STAGE_VERTEX_BIT || stage.stage_flag == VK_SHADER_STAGE_GEOMETRY_BIT ||
3333 stage.stage_flag == VK_SHADER_STAGE_MESH_BIT_NV) {
Tony-LunarGb2ded512021-02-02 16:03:30 -07003334 if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports &&
3335 IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) {
Jeremy Gebben3dfeacf2021-12-02 08:46:39 -07003336 if (stage.wrote_primitive_shading_rate) {
Tobias Hector6663c9b2020-11-05 10:18:02 +00003337 skip |=
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003338 LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate,
Tobias Hector6663c9b2020-11-05 10:18:02 +00003339 "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in"
3340 "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT,"
3341 "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.",
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003342 caller, string_VkShaderStageFlagBits(stage.stage_flag));
Tobias Hector6663c9b2020-11-05 10:18:02 +00003343 }
3344 }
3345 }
3346 }
3347
3348 return skip;
3349}
3350
sfricke-samsunge72a85e2020-02-29 21:48:37 -08003351bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003352 return ValidatePipelineShaderStage(pipeline, pipeline->stage_state[0], false);
Chris Forbes47567b72017-06-09 12:09:45 -07003353}
Chris Forbes4ae55b32017-06-09 14:42:56 -07003354
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003355uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE &pipeline, VkShaderStageFlagBits stageBit) const {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003356 uint32_t total = 0;
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003357 const auto stages = pipeline.GetShaderStages();
3358 for (const auto &stage : stages) {
3359 if (stage.stage == stageBit) {
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003360 total++;
3361 }
3362 }
3363
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003364 const auto rt_lib_info = pipeline.GetRayTracingLibraryCreateInfo();
3365 if (rt_lib_info) {
3366 for (uint32_t i = 0; i < rt_lib_info->libraryCount; ++i) {
3367 auto library_pipeline = Get<PIPELINE_STATE>(rt_lib_info->pLibraries[i]);
3368 total += CalcShaderStageCount(*library_pipeline, stageBit);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003369 }
3370 }
3371
3372 return total;
3373}
3374
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003375bool CoreChecks::GroupHasValidIndex(const PIPELINE_STATE &pipeline, uint32_t group, uint32_t stage) const {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003376 if (group == VK_SHADER_UNUSED_NV) {
3377 return true;
3378 }
3379
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003380 const auto stages = pipeline.GetShaderStages();
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003381
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003382 const auto num_stages = static_cast<uint32_t>(stages.size());
3383 if (group < num_stages) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003384 return (stages[group].stage & stage) != 0;
3385 }
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003386 group -= num_stages;
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003387
3388 // Search libraries
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003389 const auto rt_lib_info = pipeline.GetRayTracingLibraryCreateInfo();
3390 if (rt_lib_info) {
3391 for (uint32_t i = 0; i < rt_lib_info->libraryCount; ++i) {
3392 auto library_pipeline = Get<PIPELINE_STATE>(rt_lib_info->pLibraries[i]);
3393 const auto lib_stages = library_pipeline->GetShaderStages();
3394 const uint32_t stage_count = static_cast<uint32_t>(lib_stages.size());
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003395 if (group < stage_count) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003396 return (stages[group].stage & stage) != 0;
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003397 }
3398 group -= stage_count;
3399 }
3400 }
3401
3402 // group index too large
3403 return false;
3404}
3405
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003406bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, const safe_VkRayTracingPipelineCreateInfoCommon &create_info,
3407 VkPipelineCreateFlags flags, bool isKHR) const {
John Zulaufe4474e72019-07-01 17:28:27 -06003408 bool skip = false;
Jason Macnak15f95e82019-08-21 21:52:02 -04003409
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003410 if (isKHR) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003411 if (create_info.maxPipelineRayRecursionDepth > phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) {
3412 skip |=
3413 LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589",
3414 "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to "
3415 "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d",
3416 create_info.maxPipelineRayRecursionDepth, phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003417 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003418 if (create_info.pLibraryInfo) {
3419 for (uint32_t i = 0; i < create_info.pLibraryInfo->libraryCount; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07003420 const auto library_pipelinestate = Get<PIPELINE_STATE>(create_info.pLibraryInfo->pLibraries[i]);
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003421 const auto &library_create_info = library_pipelinestate->GetCreateInfo<VkRayTracingPipelineCreateInfoKHR>();
Jeremy Gebben11af9792021-08-20 10:20:09 -06003422 if (library_create_info.maxPipelineRayRecursionDepth != create_info.maxPipelineRayRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003423 skip |= LogError(
3424 device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591",
3425 "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been"
3426 "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003427 i, library_create_info.maxPipelineRayRecursionDepth, create_info.maxPipelineRayRecursionDepth);
sourav parmarcd5fb182020-07-17 12:58:44 -07003428 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003429 if (library_create_info.pLibraryInfo && (library_create_info.pLibraryInterface->maxPipelineRayHitAttributeSize !=
3430 create_info.pLibraryInterface->maxPipelineRayHitAttributeSize ||
3431 library_create_info.pLibraryInterface->maxPipelineRayPayloadSize !=
3432 create_info.pLibraryInterface->maxPipelineRayPayloadSize)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003433 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593",
3434 "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries "
3435 "member must have been created with values of the maxPipelineRayPayloadSize and "
3436 "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline");
3437 }
3438 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) &&
Jeremy Gebben11af9792021-08-20 10:20:09 -06003439 !(library_create_info.flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003440 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594",
3441 "vkCreateRayTracingPipelinesKHR: If flags includes "
3442 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of "
3443 "the pLibraries member of libraries must have been created with the "
3444 "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set");
3445 }
sourav parmar83c31b12020-05-06 12:30:54 -07003446 }
3447 }
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003448 } else {
Jeremy Gebben11af9792021-08-20 10:20:09 -06003449 if (create_info.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) {
sourav parmarcd5fb182020-07-17 12:58:44 -07003450 skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457",
3451 "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to "
3452 "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)",
Jeremy Gebben11af9792021-08-20 10:20:09 -06003453 create_info.maxRecursionDepth, phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth);
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003454 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003455 }
Jeremy Gebben11af9792021-08-20 10:20:09 -06003456 const auto *groups = create_info.ptr()->pGroups;
Jason Macnak15f95e82019-08-21 21:52:02 -04003457
Jeremy Gebben11af9792021-08-20 10:20:09 -06003458 for (uint32_t stage_index = 0; stage_index < create_info.stageCount; stage_index++) {
Jeremy Gebben84b838b2021-08-23 08:41:39 -06003459 skip |= ValidatePipelineShaderStage(pipeline, pipeline->stage_state[stage_index], false);
Jason Macnak15f95e82019-08-21 21:52:02 -04003460 }
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003461
Jeremy Gebben11af9792021-08-20 10:20:09 -06003462 if ((create_info.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003463 const uint32_t raygen_stages_count = CalcShaderStageCount(*pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003464 if (raygen_stages_count == 0) {
3465 skip |= LogError(
3466 device,
Mike Schuchardt7b152fa2021-08-03 16:30:27 -07003467 isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-06232",
Ricardo Garcia6f3477e2020-10-21 10:58:53 +02003468 " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR.");
3469 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003470 }
ziga-lunarg22f96832022-05-08 22:20:15 +02003471 if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR) != 0 &&
3472 (flags & VK_PIPELINE_CREATE_RAY_TRACING_SKIP_AABBS_BIT_KHR) != 0) {
3473 skip |= LogError(
3474 device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-06546",
3475 "vkCreateRayTracingPipelinesKHR: flags (%s) contains both VK_PIPELINE_CREATE_RAY_TRACING_SKIP_TRIANGLES_BIT_KHR and "
3476 "VK_PIPELINE_CREATE_RAY_TRACING_SKIP_AABBS_BIT_KHR bits.",
3477 string_VkPipelineCreateFlags(flags).c_str());
3478 }
Jason Macnak15f95e82019-08-21 21:52:02 -04003479
Jeremy Gebben11af9792021-08-20 10:20:09 -06003480 for (uint32_t group_index = 0; group_index < create_info.groupCount; group_index++) {
Jason Macnak15f95e82019-08-21 21:52:02 -04003481 const auto &group = groups[group_index];
3482
3483 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) {
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003484 if (!GroupHasValidIndex(
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003485 *pipeline, group.generalShader,
Sebastian Neubauer7c826172021-10-04 12:05:51 +02003486 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 -05003487 skip |= LogError(device,
3488 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474"
3489 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413",
3490 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003491 }
3492 if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV ||
3493 group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003494 skip |= LogError(device,
3495 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475"
3496 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414",
3497 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003498 }
3499 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) {
Nathaniel Cesariod4d8fce2022-05-06 15:01:10 -06003500 if (!GroupHasValidIndex(*pipeline, group.intersectionShader, VK_SHADER_STAGE_INTERSECTION_BIT_NV)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003501 skip |= LogError(device,
3502 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476"
3503 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415",
3504 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003505 }
3506 } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
3507 if (group.intersectionShader != VK_SHADER_UNUSED_NV) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003508 skip |= LogError(device,
3509 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477"
3510 : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416",
3511 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003512 }
3513 }
3514
3515 if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV ||
3516 group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) {
sjfricke62366d32022-08-01 21:04:10 +09003517 if (!GroupHasValidIndex(*pipeline, group.anyHitShader, VK_SHADER_STAGE_ANY_HIT_BIT_KHR)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003518 skip |= LogError(device,
3519 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479"
3520 : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418",
3521 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003522 }
sjfricke62366d32022-08-01 21:04:10 +09003523 if (!GroupHasValidIndex(*pipeline, group.closestHitShader, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR)) {
Jeff Bolz443c2ca2020-03-19 12:11:51 -05003524 skip |= LogError(device,
3525 isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478"
3526 : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417",
3527 ": pGroups[%d]", group_index);
Jason Macnak15f95e82019-08-21 21:52:02 -04003528 }
3529 }
John Zulaufe4474e72019-07-01 17:28:27 -06003530 }
3531 return skip;
Jeff Bolzfbe51582018-09-13 10:01:35 -05003532}
3533
Dave Houltona9df0ce2018-02-07 10:51:23 -07003534uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); }
Chris Forbes9a61e082017-07-24 15:35:29 -07003535
Dave Houltona9df0ce2018-02-07 10:51:23 -07003536static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) {
Mark Lobodzinski1f887d32020-12-30 15:31:33 -07003537 const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext);
John Zulauf25ea2432019-04-05 10:07:38 -06003538 if (validation_cache_ci) {
John Zulauf146ee802019-04-05 15:31:06 -06003539 return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003540 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003541 return nullptr;
3542}
3543
Mark Lobodzinskib56bbb92019-02-18 11:49:59 -07003544bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
Jeff Bolz5c801d12019-10-09 10:38:45 -05003545 const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003546 bool skip = false;
3547 spv_result_t spv_valid = SPV_SUCCESS;
Chris Forbes4ae55b32017-06-09 14:42:56 -07003548
Mark Lobodzinski90eea5b2020-05-15 12:54:00 -06003549 if (disabled[shader_validation]) {
Chris Forbes4ae55b32017-06-09 14:42:56 -07003550 return false;
3551 }
3552
sfricke-samsung45996a42021-09-16 13:45:27 -07003553 auto have_glsl_shader = IsExtEnabled(device_extensions.vk_nv_glsl_shader);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003554
3555 if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003556 skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376",
3557 "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".",
3558 pCreateInfo->codeSize);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003559 } else {
Chris Forbes9a61e082017-07-24 15:35:29 -07003560 auto cache = GetValidationCacheInfo(pCreateInfo);
3561 uint32_t hash = 0;
Tony-LunarG55fdf1e2021-01-13 14:32:56 -07003562 // If app isn't using a shader validation cache, use the default one from CoreChecks
3563 if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache);
Chris Forbes9a61e082017-07-24 15:35:29 -07003564 if (cache) {
3565 hash = ValidationCache::MakeShaderHash(pCreateInfo);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003566 if (cache->Contains(hash)) return false;
Chris Forbes9a61e082017-07-24 15:35:29 -07003567 }
3568
Jeremy Hayesb3e4d532019-08-16 10:08:49 -06003569 // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present,
3570 // the default values will be used during validation.
sfricke-samsung45996a42021-09-16 13:45:27 -07003571 spv_target_env spirv_environment = PickSpirvEnv(api_version, IsExtEnabled(device_extensions.vk_khr_spirv_1_4));
Dave Houlton0ea2d012018-06-21 14:00:26 -06003572 spv_context ctx = spvContextCreate(spirv_environment);
Dave Houltona9df0ce2018-02-07 10:51:23 -07003573 spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)};
Chris Forbes4ae55b32017-06-09 14:42:56 -07003574 spv_diagnostic diag = nullptr;
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003575 spvtools::ValidatorOptions options;
3576 AdjustValidatorOptions(device_extensions, enabled_features, options);
Karl Schultzfda1b382018-08-08 18:56:11 -06003577 spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag);
Chris Forbes4ae55b32017-06-09 14:42:56 -07003578 if (spv_valid != SPV_SUCCESS) {
3579 if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) {
Mark Lobodzinski12b9be92020-01-30 15:25:55 -07003580 if (spv_valid == SPV_WARNING) {
3581 skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3582 diag && diag->error ? diag->error : "(no error text)");
3583 } else {
3584 skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s",
3585 diag && diag->error ? diag->error : "(no error text)");
3586 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003587 }
Chris Forbes9a61e082017-07-24 15:35:29 -07003588 } else {
3589 if (cache) {
3590 cache->Insert(hash);
3591 }
Chris Forbes4ae55b32017-06-09 14:42:56 -07003592 }
3593
3594 spvDiagnosticDestroy(diag);
3595 spvContextDestroy(ctx);
3596 }
3597
Chris Forbes4ae55b32017-06-09 14:42:56 -07003598 return skip;
Mark Lobodzinski01734072019-02-13 17:39:15 -07003599}
3600
Tony-LunarG1672d002022-08-03 14:35:34 -06003601bool CoreChecks::PreCallValidateGetShaderModuleIdentifierEXT(VkDevice device, VkShaderModule shaderModule,
3602 VkShaderModuleIdentifierEXT *pIdentifier) const {
3603 bool skip = false;
3604 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
3605 skip |= LogError(device, "VUID-vkGetShaderModuleIdentifierEXT-shaderModuleIdentifier-06884",
3606 "vkGetShaderModuleIdentifierEXT() was called when the shaderModuleIdentifier feature was not enabled");
3607 }
3608 return skip;
3609}
3610
3611bool CoreChecks::PreCallValidateGetShaderModuleCreateInfoIdentifierEXT(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
3612 VkShaderModuleIdentifierEXT *pIdentifier) const {
3613 bool skip = false;
3614 if (!(enabled_features.shader_module_identifier_features.shaderModuleIdentifier)) {
3615 skip |= LogError(
3616 device, "VUID-vkGetShaderModuleCreateInfoIdentifierEXT-shaderModuleIdentifier-06885",
3617 "vkGetShaderModuleCreateInfoIdentifierEXT() was called when the shaderModuleIdentifier feature was not enabled");
3618 }
3619 return skip;
3620}
3621
sjfricke4f600c82022-06-09 14:21:32 +09003622bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE &module_state, const spirv_inst_iter &entrypoint,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003623 const PipelineStageState &stage_state, uint32_t local_size_x, uint32_t local_size_y,
3624 uint32_t local_size_z) const {
Lockeaa8fdc02019-04-02 11:59:20 -06003625 bool skip = false;
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003626 // If spec constants were used then the local size are already found if possible
3627 if (local_size_x == 0) {
sjfricke4f600c82022-06-09 14:21:32 +09003628 if (!module_state.FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003629 return skip; // no local size found
Lockeaa8fdc02019-04-02 11:59:20 -06003630 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003631 }
Lockeaa8fdc02019-04-02 11:59:20 -06003632
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003633 if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) {
sjfricke4f600c82022-06-09 14:21:32 +09003634 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-x-06429",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003635 "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003636 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003637 phys_dev_props.limits.maxComputeWorkGroupSize[0]);
3638 }
3639 if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) {
sjfricke4f600c82022-06-09 14:21:32 +09003640 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-y-06430",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003641 "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003642 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003643 phys_dev_props.limits.maxComputeWorkGroupSize[1]);
3644 }
3645 if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) {
sjfricke4f600c82022-06-09 14:21:32 +09003646 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-z-06431",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003647 "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003648 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003649 phys_dev_props.limits.maxComputeWorkGroupSize[2]);
3650 }
3651
3652 uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations;
3653 uint64_t invocations = local_size_x * local_size_y;
3654 // Prevent overflow.
3655 bool fail = false;
3656 if (invocations > UINT32_MAX || invocations > limit) {
3657 fail = true;
3658 }
3659 if (!fail) {
3660 invocations *= local_size_z;
Lockeaa8fdc02019-04-02 11:59:20 -06003661 if (invocations > UINT32_MAX || invocations > limit) {
3662 fail = true;
3663 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003664 }
3665 if (fail) {
sjfricke4f600c82022-06-09 14:21:32 +09003666 skip |= LogError(module_state.vk_shader_module(), "VUID-RuntimeSpirv-x-06432",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003667 "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3668 ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003669 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x, local_size_y,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003670 local_size_z, limit);
3671 }
ziga-lunarg11fecb92021-09-20 16:48:06 +02003672
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003673 const auto subgroup_flags = VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT |
3674 VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT;
ziga-lunargd46c7af2022-04-16 14:05:38 +02003675 const auto *required_subgroup_size_features =
3676 LvlFindInChain<VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT>(stage_state.create_info->pNext);
ziga-lunarg561d7222022-05-08 20:07:02 +02003677 if (required_subgroup_size_features) {
sjfrickef05418b2022-08-01 18:57:20 +09003678 const uint32_t requiredSubgroupSize = required_subgroup_size_features->requiredSubgroupSize;
ziga-lunarg561d7222022-05-08 20:07:02 +02003679 skip |= RequireFeature(enabled_features.core13.subgroupSizeControl, "subgroupSizeControl",
3680 "VUID-VkPipelineShaderStageCreateInfo-pNext-02755");
3681 if ((phys_dev_ext_props.subgroup_size_control_props.requiredSubgroupSizeStages & stage_state.stage_flag) == 0) {
3682 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003683 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02755",
ziga-lunarg561d7222022-05-08 20:07:02 +02003684 "Stage %s is not in VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%s).",
3685 string_VkShaderStageFlagBits(stage_state.stage_flag),
3686 string_VkShaderStageFlags(phys_dev_ext_props.subgroup_size_control_props.requiredSubgroupSizeStages).c_str());
3687 }
sjfrickef05418b2022-08-01 18:57:20 +09003688 if ((invocations > requiredSubgroupSize * phys_dev_ext_props.subgroup_size_control_props.maxComputeWorkgroupSubgroups)) {
ziga-lunarg561d7222022-05-08 20:07:02 +02003689 skip |=
sjfricke4f600c82022-06-09 14:21:32 +09003690 LogError(module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02756",
ziga-lunargd46c7af2022-04-16 14:05:38 +02003691 "Local workgroup size (%" PRIu32 ", %" PRIu32 ", %" PRIu32
3692 ") is greater than VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT::requiredSubgroupSize (%" PRIu32
3693 ") * maxComputeWorkgroupSubgroups (%" PRIu32 ").",
sjfrickef05418b2022-08-01 18:57:20 +09003694 local_size_x, local_size_y, local_size_z, requiredSubgroupSize,
ziga-lunargd46c7af2022-04-16 14:05:38 +02003695 phys_dev_ext_props.subgroup_size_control_props.maxComputeWorkgroupSubgroups);
ziga-lunarg561d7222022-05-08 20:07:02 +02003696 }
3697 if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT) > 0) {
sjfrickef05418b2022-08-01 18:57:20 +09003698 if (SafeModulo(local_size_x, requiredSubgroupSize) != 0) {
ziga-lunarg561d7222022-05-08 20:07:02 +02003699 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003700 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-pNext-02757",
ziga-lunarg561d7222022-05-08 20:07:02 +02003701 "Local workgroup size x (%" PRIu32
3702 ") is not a multiple of VkPipelineShaderStageRequiredSubgroupSizeCreateInfoEXT::requiredSubgroupSize (%" PRIu32
3703 ").",
sjfrickef05418b2022-08-01 18:57:20 +09003704 local_size_x, requiredSubgroupSize);
ziga-lunarg561d7222022-05-08 20:07:02 +02003705 }
ziga-lunargd46c7af2022-04-16 14:05:38 +02003706 }
sjfrickef05418b2022-08-01 18:57:20 +09003707 if (!IsPowerOfTwo(requiredSubgroupSize)) {
3708 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003709 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02760",
sjfrickef05418b2022-08-01 18:57:20 +09003710 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3711 ") is not a power of 2.",
3712 requiredSubgroupSize);
3713 }
3714 if (requiredSubgroupSize < phys_dev_ext_props.subgroup_size_control_props.minSubgroupSize) {
3715 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003716 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02761",
sjfrickef05418b2022-08-01 18:57:20 +09003717 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3718 ") is less than minSubgroupSize (%" PRIu32 ").",
3719 requiredSubgroupSize, phys_dev_ext_props.subgroup_size_control_props.minSubgroupSize);
3720 }
3721 if (requiredSubgroupSize > phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) {
3722 skip |= LogError(module_state.vk_shader_module(),
sjfrickebf1244c2022-08-01 18:57:28 +09003723 "VUID-VkPipelineShaderStageRequiredSubgroupSizeCreateInfo-requiredSubgroupSize-02762",
sjfrickef05418b2022-08-01 18:57:20 +09003724 "VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::requiredSubgroupSizeStages (%" PRIu32
3725 ") is greater than maxSubgroupSize (%" PRIu32 ").",
3726 requiredSubgroupSize, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3727 }
ziga-lunargd46c7af2022-04-16 14:05:38 +02003728 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003729 if ((stage_state.create_info->flags & subgroup_flags) == subgroup_flags) {
3730 if (SafeModulo(local_size_x, phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize) != 0) {
3731 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003732 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02758",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003733 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT and "
3734 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bits, but local workgroup size in the X "
3735 "dimension (%" PRIu32
3736 ") is not a multiple of VkPhysicalDeviceSubgroupSizeControlPropertiesEXT::maxSubgroupSize (%" PRIu32 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003737 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003738 phys_dev_ext_props.subgroup_size_control_props.maxSubgroupSize);
3739 }
3740 } else if ((stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT) &&
3741 (stage_state.create_info->flags & VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT) == 0) {
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003742 if (!required_subgroup_size_features) {
3743 if (SafeModulo(local_size_x, phys_dev_props_core11.subgroupSize) != 0) {
ziga-lunarg11fecb92021-09-20 16:48:06 +02003744 skip |= LogError(
sjfricke4f600c82022-06-09 14:21:32 +09003745 module_state.vk_shader_module(), "VUID-VkPipelineShaderStageCreateInfo-flags-02759",
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003746 "%s flags contain VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT bit, and not the"
3747 "VK_PIPELINE_SHADER_STAGE_CREATE_ALLOW_VARYING_SUBGROUP_SIZE_BIT_EXT bit, but local workgroup size in the "
3748 "X dimension (%" PRIu32 ") is not a multiple of VkPhysicalDeviceVulkan11Properties::subgroupSize (%" PRIu32
3749 ").",
sjfricke4f600c82022-06-09 14:21:32 +09003750 report_data->FormatHandle(module_state.vk_shader_module()).c_str(), local_size_x,
sfricke-samsung61d50ec2022-02-13 17:01:25 -08003751 phys_dev_props_core11.subgroupSize);
ziga-lunarg11fecb92021-09-20 16:48:06 +02003752 }
3753 }
Lockeaa8fdc02019-04-02 11:59:20 -06003754 }
3755 return skip;
3756}
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003757
3758spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) {
Tony-LunarGe67fcc22022-01-03 16:40:53 -07003759 if (api_version >= VK_API_VERSION_1_3) {
3760 return SPV_ENV_VULKAN_1_3;
3761 } else if (api_version >= VK_API_VERSION_1_2) {
Tony-LunarG8a51b7d2020-07-01 15:57:23 -06003762 return SPV_ENV_VULKAN_1_2;
3763 } else if (api_version >= VK_API_VERSION_1_1) {
3764 if (spirv_1_4) {
3765 return SPV_ENV_VULKAN_1_1_SPIRV_1_4;
3766 } else {
3767 return SPV_ENV_VULKAN_1_1;
3768 }
3769 }
3770 return SPV_ENV_VULKAN_1_0;
3771}
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003772
sfricke-samsungecc112a2021-09-03 05:32:17 -07003773// Some Vulkan extensions/features are just all done in spirv-val behind optional settings
Jeremy Gebben5d970742021-05-31 16:04:14 -06003774void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features,
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003775 spvtools::ValidatorOptions &options) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003776 // VK_KHR_relaxed_block_layout never had a feature bit so just enabling the extension allows relaxed layout
3777 // Was promotoed in Vulkan 1.1 so anyone using Vulkan 1.1 also gets this for free
sfricke-samsung45996a42021-09-16 13:45:27 -07003778 if (IsExtEnabled(device_extensions.vk_khr_relaxed_block_layout)) {
sfricke-samsungecc112a2021-09-03 05:32:17 -07003779 // --relax-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003780 options.SetRelaxBlockLayout(true);
3781 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003782
3783 // The rest of the settings are controlled from a feature bit, which are set correctly in the state tracking. Regardless of
3784 // Vulkan version used, the feature bit is needed (also described in the spec).
3785
3786 if (enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) {
3787 // --uniform-buffer-standard-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003788 options.SetUniformBufferStandardLayout(true);
3789 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003790 if (enabled_features.core12.scalarBlockLayout == VK_TRUE) {
3791 // --scalar-block-layout
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003792 options.SetScalarBlockLayout(true);
3793 }
sfricke-samsungecc112a2021-09-03 05:32:17 -07003794 if (enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) {
3795 // --workgroup-scalar-block-layout
Caio Marcelo de Oliveira Filhod1bfbcd2021-01-27 01:44:04 -08003796 options.SetWorkgroupScalarBlockLayout(true);
3797 }
Tony-LunarG273f32f2021-09-28 08:56:30 -06003798 if (enabled_features.core13.maintenance4) {
sfricke-samsungd3c917b2021-10-19 08:24:57 -07003799 // --allow-localsizeid
3800 options.SetAllowLocalSizeId(true);
3801 }
Tony-LunarG9fe69a42020-07-23 15:09:37 -06003802}