sfricke-samsung | 691299b | 2021-01-01 20:48:48 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2021 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2021 Valve Corporation |
| 3 | * Copyright (c) 2015-2021 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2021 Google Inc. |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 5 | * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved. |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 6 | * |
| 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 Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 20 | * Author: Dave Houlton <daveh@lunarg.com> |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 21 | * Author: Tobias Hector <tobias.hector@amd.com> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 24 | #include "shader_validation.h" |
| 25 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 26 | #include <cassert> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 27 | #include <cinttypes> |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 28 | #include <cmath> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 29 | #include <sstream> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 30 | #include <string> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Mark Lobodzinski | 102687e | 2020-04-28 11:03:28 -0600 | [diff] [blame] | 33 | #include <spirv/unified1/spirv.hpp> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 34 | #include "vk_enum_string_helper.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 35 | #include "vk_layer_data.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 36 | #include "vk_layer_utils.h" |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 37 | #include "chassis.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 38 | #include "core_validation.h" |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 39 | |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 40 | #include "xxhash.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 41 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 42 | static shader_stage_attributes shader_stage_attribs[] = { |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 43 | {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT}, |
| 44 | {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT}, |
| 45 | {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT}, |
| 46 | {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT}, |
| 47 | {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 50 | static bool IsNarrowNumericType(spirv_inst_iter type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 51 | if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false; |
| 52 | return type.word(2) < 64; |
| 53 | } |
| 54 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 55 | static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, unsigned a_type, unsigned b_type, bool a_arrayed, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 56 | bool b_arrayed, bool relaxed) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 57 | // Walk two type trees together, and complain about differences |
| 58 | auto a_insn = a->get_def(a_type); |
| 59 | auto b_insn = b->get_def(b_type); |
| 60 | assert(a_insn != a->end()); |
| 61 | assert(b_insn != b->end()); |
| 62 | |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 63 | // Ignore runtime-sized arrays-- they cannot appear in these interfaces. |
| 64 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 65 | if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 66 | return TypesMatch(a, b, a_insn.word(2), b_type, false, b_arrayed, relaxed); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) { |
| 70 | // We probably just found the extra level of arrayness in b_type: compare the type inside it to a_type |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 71 | return TypesMatch(a, b, a_type, b_insn.word(2), a_arrayed, false, relaxed); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 74 | if (a_insn.opcode() == spv::OpTypeVector && relaxed && IsNarrowNumericType(b_insn)) { |
| 75 | return TypesMatch(a, b, a_insn.word(2), b_type, a_arrayed, b_arrayed, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | if (a_insn.opcode() != b_insn.opcode()) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (a_insn.opcode() == spv::OpTypePointer) { |
| 83 | // Match on pointee type. storage class is expected to differ |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 84 | return TypesMatch(a, b, a_insn.word(3), b_insn.word(3), a_arrayed, b_arrayed, relaxed); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (a_arrayed || b_arrayed) { |
| 88 | // If we havent resolved array-of-verts by here, we're not going to. |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | switch (a_insn.opcode()) { |
| 93 | case spv::OpTypeBool: |
| 94 | return true; |
| 95 | case spv::OpTypeInt: |
| 96 | // Match on width, signedness |
| 97 | return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3); |
| 98 | case spv::OpTypeFloat: |
| 99 | // Match on width |
| 100 | return a_insn.word(2) == b_insn.word(2); |
| 101 | case spv::OpTypeVector: |
| 102 | // Match on element type, count. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 103 | if (!TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false; |
| 104 | if (relaxed && IsNarrowNumericType(a->get_def(a_insn.word(2)))) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 105 | return a_insn.word(3) >= b_insn.word(3); |
| 106 | } else { |
| 107 | return a_insn.word(3) == b_insn.word(3); |
| 108 | } |
| 109 | case spv::OpTypeMatrix: |
| 110 | // Match on element type, count. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 111 | return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) && |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 112 | a_insn.word(3) == b_insn.word(3); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 113 | case spv::OpTypeArray: |
| 114 | // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from |
| 115 | // vector & matrix types in that the array size is the id of a constant instruction, * not a literal within OpTypeArray |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 116 | return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) && |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 117 | a->GetConstantValueById(a_insn.word(3)) == b->GetConstantValueById(b_insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 118 | case spv::OpTypeStruct: |
| 119 | // Match on all element types |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 120 | { |
| 121 | if (a_insn.len() != b_insn.len()) { |
| 122 | return false; // Structs cannot match if member counts differ |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 123 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 124 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 125 | for (unsigned i = 2; i < a_insn.len(); i++) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 126 | if (!TypesMatch(a, b, a_insn.word(i), b_insn.word(i), a_arrayed, b_arrayed, false)) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 133 | default: |
| 134 | // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match. |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 139 | static unsigned GetLocationsConsumedByFormat(VkFormat format) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 140 | switch (format) { |
| 141 | case VK_FORMAT_R64G64B64A64_SFLOAT: |
| 142 | case VK_FORMAT_R64G64B64A64_SINT: |
| 143 | case VK_FORMAT_R64G64B64A64_UINT: |
| 144 | case VK_FORMAT_R64G64B64_SFLOAT: |
| 145 | case VK_FORMAT_R64G64B64_SINT: |
| 146 | case VK_FORMAT_R64G64B64_UINT: |
| 147 | return 2; |
| 148 | default: |
| 149 | return 1; |
| 150 | } |
| 151 | } |
| 152 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 153 | static unsigned GetFormatType(VkFormat fmt) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 154 | if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT; |
| 155 | if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT; |
| 156 | if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT; |
| 157 | if (fmt == VK_FORMAT_UNDEFINED) return 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 158 | // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader. |
| 159 | return FORMAT_TYPE_FLOAT; |
| 160 | } |
| 161 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 162 | static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 163 | uint32_t bit_pos = uint32_t(u_ffs(stage)); |
| 164 | return bit_pos - 1; |
| 165 | } |
| 166 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 167 | bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 168 | // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should |
| 169 | // be specified only once. |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 170 | layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 171 | bool skip = false; |
| 172 | |
| 173 | for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) { |
| 174 | auto desc = &vi->pVertexBindingDescriptions[i]; |
| 175 | auto &binding = bindings[desc->binding]; |
| 176 | if (binding) { |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 177 | // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps? |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 178 | skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d", |
| 179 | desc->binding); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 180 | } else { |
| 181 | binding = desc; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return skip; |
| 186 | } |
| 187 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 188 | bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs, |
| 189 | spirv_inst_iter entrypoint) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 190 | bool skip = false; |
| 191 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 192 | const auto inputs = vs->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 193 | |
| 194 | // Build index by location |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 195 | std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 196 | if (vi) { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 197 | for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) { |
| 198 | const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format); |
| 199 | for (uint32_t j = 0; j < num_locations; ++j) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 200 | attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i]; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 205 | struct AttribInputPair { |
| 206 | const VkVertexInputAttributeDescription *attrib = nullptr; |
| 207 | const interface_var *input = nullptr; |
| 208 | }; |
| 209 | std::map<uint32_t, AttribInputPair> location_map; |
| 210 | for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second; |
| 211 | for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 212 | |
Jamie Madill | c1f7ca8 | 2020-03-16 17:08:26 -0400 | [diff] [blame] | 213 | for (const auto &location_it : location_map) { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 214 | const auto location = location_it.first; |
| 215 | const auto attrib = location_it.second.attrib; |
| 216 | const auto input = location_it.second.input; |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 217 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 218 | if (attrib && !input) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 219 | skip |= LogPerformanceWarning(vs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 220 | "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 221 | } else if (!attrib && input) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 222 | skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 223 | "Vertex shader consumes input at location %" PRIu32 " but not provided", location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 224 | } else if (attrib && input) { |
| 225 | const auto attrib_type = GetFormatType(attrib->format); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 226 | const auto input_type = vs->GetFundamentalType(input->type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 227 | |
| 228 | // Type checking |
| 229 | if (!(attrib_type & input_type)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 230 | skip |= LogError(vs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 231 | "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`", |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 232 | string_VkFormat(attrib->format), location, vs->DescribeType(input->type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 233 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 234 | } else { // !attrib && !input |
| 235 | assert(false); // at least one exists in the map |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | return skip; |
| 240 | } |
| 241 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 242 | bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint, |
| 243 | PIPELINE_STATE const *pipeline, uint32_t subpass_index) const { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 244 | bool skip = false; |
Chris Forbes | 8bca165 | 2017-07-20 11:10:09 -0700 | [diff] [blame] | 245 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 246 | const auto rpci = pipeline->rp_state->createInfo.ptr(); |
| 247 | |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 248 | struct Attachment { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 249 | const VkAttachmentReference2 *reference = nullptr; |
| 250 | const VkAttachmentDescription2 *attachment = nullptr; |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 251 | const interface_var *output = nullptr; |
| 252 | }; |
| 253 | std::map<uint32_t, Attachment> location_map; |
| 254 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 255 | const auto subpass = rpci->pSubpasses[subpass_index]; |
| 256 | for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) { |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 257 | auto const &reference = subpass.pColorAttachments[i]; |
| 258 | location_map[i].reference = &reference; |
| 259 | if (reference.attachment != VK_ATTACHMENT_UNUSED && |
| 260 | rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) { |
| 261 | location_map[i].attachment = &rpci->pAttachments[reference.attachment]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 265 | // TODO: dual source blend index (spv::DecIndex, zero if not provided) |
| 266 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 267 | const auto outputs = fs->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false); |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 268 | for (const auto &output_it : outputs) { |
| 269 | auto const location = output_it.first.first; |
| 270 | location_map[location].output = &output_it.second; |
| 271 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 272 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 273 | const bool alpha_to_coverage_enabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL && |
| 274 | pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 275 | |
Jamie Madill | c1f7ca8 | 2020-03-16 17:08:26 -0400 | [diff] [blame] | 276 | for (const auto &location_it : location_map) { |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 277 | const auto reference = location_it.second.reference; |
| 278 | if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) { |
| 279 | continue; |
| 280 | } |
| 281 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 282 | const auto location = location_it.first; |
| 283 | const auto attachment = location_it.second.attachment; |
| 284 | const auto output = location_it.second.output; |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 285 | if (attachment && !output) { |
| 286 | if (pipeline->attachments[location].colorWriteMask != 0) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 287 | skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InputNotProduced, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 288 | "Attachment %" PRIu32 |
| 289 | " not written by fragment shader; undefined values will be written to attachment", |
| 290 | location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 291 | } |
| 292 | } else if (!attachment && output) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 293 | if (!(alpha_to_coverage_enabled && location == 0)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 294 | skip |= LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 295 | "fragment shader writes to output location %" PRIu32 " with no matching attachment", location); |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 296 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 297 | } else if (attachment && output) { |
| 298 | const auto attachment_type = GetFormatType(attachment->format); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 299 | const auto output_type = fs->GetFundamentalType(output->type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 300 | |
| 301 | // Type checking |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 302 | if (!(output_type & attachment_type)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 303 | skip |= |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 304 | LogWarning(fs->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 305 | "Attachment %" PRIu32 |
| 306 | " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined", |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 307 | location, string_VkFormat(attachment->format), fs->DescribeType(output->type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 308 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 309 | } else { // !attachment && !output |
| 310 | assert(false); // at least one exists in the map |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 314 | const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 315 | bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() && |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 316 | fs->GetComponentsConsumedByType(output_zero->type_id, false) == 4; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 317 | if (alpha_to_coverage_enabled && !location_zero_has_alpha) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 318 | skip |= LogError(fs->vk_shader_module(), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 319 | "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled."); |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 322 | return skip; |
| 323 | } |
| 324 | |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 325 | PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update, |
| 326 | const shader_struct_member &push_constant_used_in_shader, |
| 327 | uint32_t &out_issue_index) const { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 328 | const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes(); |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 329 | const auto used_bytes_size = used_bytes->size(); |
| 330 | if (used_bytes_size == 0) return PC_Byte_Updated; |
| 331 | |
| 332 | const auto push_constant_data_update_size = push_constant_data_update.size(); |
| 333 | const auto *data = push_constant_data_update.data(); |
| 334 | if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) { |
| 335 | if (used_bytes_size <= push_constant_data_update_size) { |
| 336 | return PC_Byte_Updated; |
| 337 | } |
| 338 | const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size; |
| 339 | |
| 340 | const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size; |
| 341 | if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) { |
| 342 | return PC_Byte_Updated; |
| 343 | } |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 344 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 345 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 346 | uint32_t i = 0; |
| 347 | for (const auto used : *used_bytes) { |
| 348 | if (used) { |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 349 | if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 350 | out_issue_index = i; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 351 | return PC_Byte_Not_Set; |
| 352 | } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 353 | out_issue_index = i; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 354 | return PC_Byte_Not_Updated; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | ++i; |
| 358 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 359 | return PC_Byte_Updated; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src, |
sfricke-samsung | 7699b91 | 2021-04-12 23:01:51 -0700 | [diff] [blame] | 363 | VkPipelineShaderStageCreateInfo const *pStage, const std::string &vuid) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 364 | bool skip = false; |
sfricke-samsung | 5c65b37 | 2021-03-25 05:39:57 -0700 | [diff] [blame] | 365 | // Temp workaround to prevent false positive errors |
| 366 | // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450 |
| 367 | if (src->multiple_entry_points) { |
| 368 | return skip; |
| 369 | } |
| 370 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 371 | // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step. |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 372 | const auto *entrypoint = src->FindEntrypointStruct(pStage->pName, pStage->stage); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 373 | if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) { |
| 374 | return skip; |
| 375 | } |
| 376 | std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 377 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 378 | bool found_stage = false; |
| 379 | for (auto const &range : *push_constant_ranges) { |
| 380 | if (range.stageFlags & pStage->stage) { |
| 381 | found_stage = true; |
| 382 | std::string location_desc; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 383 | std::vector<uint8_t> push_constant_bytes_set; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 384 | if (range.offset > 0) { |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 385 | push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 386 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 387 | push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 388 | uint32_t issue_index = 0; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 389 | const auto ret = |
| 390 | ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 391 | |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 392 | if (ret == PC_Byte_Not_Set) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 393 | const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index); |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 394 | LogObjectList objlist(src->vk_shader_module()); |
| 395 | objlist.add(pipeline.pipeline_layout->layout()); |
sfricke-samsung | 7699b91 | 2021-04-12 23:01:51 -0700 | [diff] [blame] | 396 | skip |= LogError(objlist, vuid, "Push constant buffer:%s in %s is out of range in %s.", loc_descr.c_str(), |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 397 | string_VkShaderStageFlags(pStage->stage).c_str(), |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 398 | report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str()); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 399 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 404 | if (!found_stage) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 405 | LogObjectList objlist(src->vk_shader_module()); |
| 406 | objlist.add(pipeline.pipeline_layout->layout()); |
sfricke-samsung | 7699b91 | 2021-04-12 23:01:51 -0700 | [diff] [blame] | 407 | skip |= LogError(objlist, vuid, "Push constant is used in %s of %s. But %s doesn't set %s.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 408 | string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module()).c_str(), |
| 409 | report_data->FormatHandle(pipeline.pipeline_layout->layout()).c_str(), |
sfricke-samsung | 7699b91 | 2021-04-12 23:01:51 -0700 | [diff] [blame] | 410 | string_VkShaderStageFlags(pStage->stage).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 411 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 412 | return skip; |
| 413 | } |
| 414 | |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 415 | bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, const layer_data::unordered_set<uint32_t> &accessible_ids, |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 416 | VkShaderStageFlagBits stage) const { |
| 417 | bool skip = false; |
| 418 | |
| 419 | // Currently all builtin tested are only found in fragment shaders |
| 420 | if (stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 421 | return skip; |
| 422 | } |
| 423 | |
| 424 | for (const auto id : accessible_ids) { |
| 425 | auto insn = src->get_def(id); |
| 426 | const decoration_set decorations = src->get_decorations(insn.word(2)); |
| 427 | |
| 428 | // Built-ins are obtained from OpVariable |
| 429 | if (((decorations.flags & decoration_set::builtin_bit) != 0) && (insn.opcode() == spv::OpVariable)) { |
| 430 | auto type_pointer = src->get_def(insn.word(1)); |
| 431 | assert(type_pointer.opcode() == spv::OpTypePointer); |
| 432 | |
| 433 | auto type = src->get_def(type_pointer.word(3)); |
| 434 | if (type.opcode() == spv::OpTypeArray) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 435 | uint32_t length = static_cast<uint32_t>(src->GetConstantValueById(type.word(3))); |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 436 | |
| 437 | switch (decorations.builtin) { |
| 438 | case spv::BuiltInSampleMask: |
| 439 | // Handles both the input and output sampleMask |
| 440 | if (length > phys_dev_props.limits.maxSampleMaskWords) { |
| 441 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711", |
| 442 | "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds " |
| 443 | "maxSampleMaskWords of %u in %s.", |
| 444 | length, phys_dev_props.limits.maxSampleMaskWords, |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 445 | report_data->FormatHandle(src->vk_shader_module()).c_str()); |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 446 | } |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return skip; |
| 454 | } |
| 455 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 456 | // Validate that data for each specialization entry is fully contained within the buffer. |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 457 | bool CoreChecks::ValidateSpecializationOffsets(VkPipelineShaderStageCreateInfo const *info) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 458 | bool skip = false; |
| 459 | |
| 460 | VkSpecializationInfo const *spec = info->pSpecializationInfo; |
| 461 | |
| 462 | if (spec) { |
| 463 | for (auto i = 0u; i < spec->mapEntryCount; i++) { |
Jeremy Hayes | 6c555c3 | 2019-09-09 17:14:09 -0600 | [diff] [blame] | 464 | if (spec->pMapEntries[i].offset >= spec->dataSize) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 465 | skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773", |
| 466 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
Petr Kraus | b0d5e59 | 2021-05-21 23:37:11 +0200 | [diff] [blame] | 467 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 468 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
| 469 | spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize); |
Jeremy Hayes | 6c555c3 | 2019-09-09 17:14:09 -0600 | [diff] [blame] | 470 | |
| 471 | continue; |
| 472 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 473 | if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 474 | skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774", |
| 475 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
Petr Kraus | b0d5e59 | 2021-05-21 23:37:11 +0200 | [diff] [blame] | 476 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided).", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 477 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
| 478 | spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | return skip; |
| 484 | } |
| 485 | |
Jeff Bolz | 38b3ce7 | 2018-09-19 12:53:38 -0500 | [diff] [blame] | 486 | // TODO (jbolz): Can this return a const reference? |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 487 | static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count, |
| 488 | bool is_khr) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 489 | auto type = module->get_def(type_id); |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 490 | bool is_storage_buffer = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 491 | descriptor_count = 1; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 492 | std::set<uint32_t> ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 493 | |
| 494 | // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension. |
Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 495 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) { |
| 496 | if (type.opcode() == spv::OpTypeRuntimeArray) { |
| 497 | descriptor_count = 0; |
| 498 | type = module->get_def(type.word(2)); |
| 499 | } else if (type.opcode() == spv::OpTypeArray) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 500 | descriptor_count *= module->GetConstantValueById(type.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 501 | type = module->get_def(type.word(2)); |
| 502 | } else { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 503 | if (type.word(2) == spv::StorageClassStorageBuffer) { |
| 504 | is_storage_buffer = true; |
| 505 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 506 | type = module->get_def(type.word(3)); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | switch (type.opcode()) { |
| 511 | case spv::OpTypeStruct: { |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 512 | for (auto insn : module->decoration_inst) { |
| 513 | if (insn.word(1) == type.word(1)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 514 | if (insn.word(2) == spv::DecorationBlock) { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 515 | if (is_storage_buffer) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 516 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 517 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 518 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 519 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 520 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 521 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC); |
| 522 | ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT); |
| 523 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 524 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 525 | } else if (insn.word(2) == spv::DecorationBufferBlock) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 526 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 527 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 528 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // Invalid |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 534 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | case spv::OpTypeSampler: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 538 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER); |
| 539 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 540 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 541 | |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 542 | case spv::OpTypeSampledImage: { |
| 543 | // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel |
| 544 | // buffer descriptor doesn't really provide one. Allow this slight mismatch. |
| 545 | auto image_type = module->get_def(type.word(2)); |
| 546 | auto dim = image_type.word(3); |
| 547 | auto sampled = image_type.word(7); |
| 548 | if (dim == spv::DimBuffer && sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 549 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 550 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 551 | } |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 552 | } |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 553 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 554 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 555 | |
| 556 | case spv::OpTypeImage: { |
| 557 | // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler. |
| 558 | // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable. |
| 559 | auto dim = type.word(3); |
| 560 | auto sampled = type.word(7); |
| 561 | |
| 562 | if (dim == spv::DimSubpassData) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 563 | ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT); |
| 564 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 565 | } else if (dim == spv::DimBuffer) { |
| 566 | if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 567 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 568 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 569 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 570 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER); |
| 571 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 572 | } |
| 573 | } else if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 574 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); |
| 575 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 576 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 577 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 578 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); |
| 579 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 582 | case spv::OpTypeAccelerationStructureNV: |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 583 | is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR) |
| 584 | : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV); |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 585 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 586 | |
| 587 | // We shouldn't really see any other junk types -- but if we do, they're a mismatch. |
| 588 | default: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 589 | return ret; // Matches nothing |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 593 | static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) { |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 594 | std::stringstream ss; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 595 | for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) { |
| 596 | if (ss.tellp()) ss << ", "; |
| 597 | ss << string_VkDescriptorType(VkDescriptorType(*it)); |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 598 | } |
| 599 | return ss.str(); |
| 600 | } |
| 601 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 602 | bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 603 | if (!check) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 604 | if (LogError(device, vuid, "Shader requires flag %s set in %s but it is not set on the device", flag, structure)) { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 605 | return true; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return false; |
| 610 | } |
| 611 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 612 | bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 613 | if (!feature) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 614 | if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 615 | return true; |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | return false; |
| 620 | } |
| 621 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 622 | bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor, |
| 623 | bool has_atomic_descriptor) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 624 | bool skip = false; |
| 625 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 626 | if (has_writable_descriptor || has_atomic_descriptor) { |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 627 | switch (stage) { |
| 628 | case VK_SHADER_STAGE_COMPUTE_BIT: |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 629 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 630 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 631 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 632 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 633 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 634 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 635 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 636 | case VK_SHADER_STAGE_MESH_BIT_NV: |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 637 | /* No feature requirements for writes and atomics from compute |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 638 | * raytracing, or mesh stages */ |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 639 | break; |
| 640 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 641 | skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics", |
| 642 | kVUID_Core_Shader_FeatureNotEnabled); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 643 | break; |
| 644 | default: |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 645 | skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics", |
| 646 | kVUID_Core_Shader_FeatureNotEnabled); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 647 | break; |
| 648 | } |
| 649 | } |
| 650 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 651 | return skip; |
| 652 | } |
| 653 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 654 | bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage, |
| 655 | spirv_inst_iter &insn) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 656 | bool skip = false; |
| 657 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 658 | // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations) |
| 659 | if (GroupOperation(insn.opcode()) == true) { |
| 660 | // Check the quad operations. |
| 661 | if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) { |
| 662 | if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) { |
| 663 | skip |= RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages, |
| 664 | "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", |
| 665 | kVUID_Core_Shader_FeatureNotEnabled); |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 666 | } |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 667 | } |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 668 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 669 | uint32_t scope_type = spv::ScopeMax; |
| 670 | if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) { |
| 671 | // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand |
| 672 | scope_type = spv::ScopeSubgroup; |
| 673 | } else { |
| 674 | // "All <id> used for Scope <id> must be of an OpConstant" |
| 675 | auto scope_id = module->get_def(insn.word(3)); |
| 676 | scope_type = scope_id.word(3); |
| 677 | } |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 678 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 679 | if (scope_type == spv::ScopeSubgroup) { |
| 680 | // "Group operations with subgroup scope" must have stage support |
| 681 | const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages; |
| 682 | skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage), |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 683 | "VkPhysicalDeviceSubgroupProperties::supportedStages", kVUID_Core_Shader_ExceedDeviceLimit); |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | if (!enabled_features.core12.shaderSubgroupExtendedTypes) { |
| 687 | auto type = module->get_def(insn.word(1)); |
| 688 | |
| 689 | if (type.opcode() == spv::OpTypeVector) { |
| 690 | // Get the element type |
| 691 | type = module->get_def(type.word(2)); |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 692 | } |
| 693 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 694 | if (type.opcode() != spv::OpTypeBool) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 695 | // Both OpTypeInt and OpTypeFloat the width is in the 2nd word. |
| 696 | const uint32_t width = type.word(2); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 697 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 698 | if ((type.opcode() == spv::OpTypeFloat && width == 16) || |
| 699 | (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) { |
| 700 | skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes, |
| 701 | "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes", |
| 702 | kVUID_Core_Shader_FeatureNotEnabled); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | } |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | return skip; |
| 709 | } |
| 710 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 711 | bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 712 | const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 713 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS || |
| 714 | pStage->stage == VK_SHADER_STAGE_ALL) { |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | bool skip = false; |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 719 | auto const &limits = phys_dev_props.limits; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 720 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 721 | std::set<uint32_t> patch_i_ds; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 722 | struct Variable { |
| 723 | uint32_t baseTypePtrID; |
| 724 | uint32_t ID; |
| 725 | uint32_t storageClass; |
| 726 | }; |
| 727 | std::vector<Variable> variables; |
| 728 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 729 | uint32_t num_vertices = 0; |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 730 | bool is_iso_lines = false; |
| 731 | bool is_point_mode = false; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 732 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 733 | auto entrypoint_variables = FindEntrypointInterfaces(entrypoint); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 734 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 735 | for (auto insn : *src) { |
| 736 | switch (insn.opcode()) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 737 | // Find all Patch decorations |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 738 | case spv::OpDecorate: |
| 739 | switch (insn.word(2)) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 740 | case spv::DecorationPatch: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 741 | patch_i_ds.insert(insn.word(1)); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 742 | break; |
| 743 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 744 | default: |
| 745 | break; |
| 746 | } |
| 747 | break; |
| 748 | // Find all input and output variables |
| 749 | case spv::OpVariable: { |
| 750 | Variable var = {}; |
| 751 | var.storageClass = insn.word(3); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 752 | if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) && |
| 753 | // Only include variables in the entrypoint's interface |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 754 | find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 755 | var.baseTypePtrID = insn.word(1); |
| 756 | var.ID = insn.word(2); |
| 757 | variables.push_back(var); |
| 758 | } |
| 759 | break; |
| 760 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 761 | case spv::OpExecutionMode: |
| 762 | if (insn.word(1) == entrypoint.word(2)) { |
| 763 | switch (insn.word(2)) { |
| 764 | default: |
| 765 | break; |
| 766 | case spv::ExecutionModeOutputVertices: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 767 | num_vertices = insn.word(3); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 768 | break; |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 769 | case spv::ExecutionModeIsolines: |
| 770 | is_iso_lines = true; |
| 771 | break; |
| 772 | case spv::ExecutionModePointMode: |
| 773 | is_point_mode = true; |
| 774 | break; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | break; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 778 | default: |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 783 | bool strip_output_array_level = |
| 784 | (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV); |
| 785 | bool strip_input_array_level = |
| 786 | (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || |
| 787 | pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT); |
| 788 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 789 | uint32_t num_comp_in = 0, num_comp_out = 0; |
| 790 | int max_comp_in = 0, max_comp_out = 0; |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 791 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 792 | auto inputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassInput, strip_input_array_level); |
| 793 | auto outputs = src->CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, strip_output_array_level); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 794 | |
| 795 | // Find max component location used for input variables. |
| 796 | for (auto &var : inputs) { |
| 797 | int location = var.first.first; |
| 798 | int component = var.first.second; |
| 799 | interface_var &iv = var.second; |
| 800 | |
| 801 | // Only need to look at the first location, since we use the type's whole size |
| 802 | if (iv.offset != 0) { |
| 803 | continue; |
| 804 | } |
| 805 | |
| 806 | if (iv.is_patch) { |
| 807 | continue; |
| 808 | } |
| 809 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 810 | int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_input_array_level); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 811 | max_comp_in = std::max(max_comp_in, location * 4 + component + num_components); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | // Find max component location used for output variables. |
| 815 | for (auto &var : outputs) { |
| 816 | int location = var.first.first; |
| 817 | int component = var.first.second; |
| 818 | interface_var &iv = var.second; |
| 819 | |
| 820 | // Only need to look at the first location, since we use the type's whole size |
| 821 | if (iv.offset != 0) { |
| 822 | continue; |
| 823 | } |
| 824 | |
| 825 | if (iv.is_patch) { |
| 826 | continue; |
| 827 | } |
| 828 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 829 | int num_components = src->GetComponentsConsumedByType(iv.type_id, strip_output_array_level); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 830 | max_comp_out = std::max(max_comp_out, location * 4 + component + num_components); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar), |
| 834 | // but that doesn't include builtins. |
sfricke-samsung | 406766a | 2021-07-02 12:04:09 -0700 | [diff] [blame] | 835 | // When rewritten, using the CreatePipelineExceedVertexMaxComponentsWithBuiltins test it would be nice to also let the user know |
| 836 | // how many components were from builtins as it might not be obvious |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 837 | for (auto &var : variables) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 838 | // Check if the variable is a patch. Patches can also be members of blocks, |
| 839 | // but if they are then the top-level arrayness has already been stripped |
| 840 | // by the time GetComponentsConsumedByType gets to it. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 841 | bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end(); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 842 | |
| 843 | if (var.storageClass == spv::StorageClassInput) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 844 | num_comp_in += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_input_array_level && !is_patch); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 845 | } else { // var.storageClass == spv::StorageClassOutput |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 846 | num_comp_out += src->GetComponentsConsumedByType(var.baseTypePtrID, strip_output_array_level && !is_patch); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
| 850 | switch (pStage->stage) { |
| 851 | case VK_SHADER_STAGE_VERTEX_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 852 | if (num_comp_out > limits.maxVertexOutputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 853 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 854 | "Invalid Pipeline CreateInfo State: Vertex shader exceeds " |
| 855 | "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u " |
| 856 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 857 | limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 858 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 859 | if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 860 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 861 | "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that " |
| 862 | "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)", |
| 863 | limits.maxVertexOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 864 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 865 | break; |
| 866 | |
| 867 | case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 868 | if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 869 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 870 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 871 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u " |
| 872 | "components by %u components", |
| 873 | limits.maxTessellationControlPerVertexInputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 874 | num_comp_in - limits.maxTessellationControlPerVertexInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 875 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 876 | if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 877 | skip |= |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 878 | LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 879 | "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that " |
| 880 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)", |
| 881 | limits.maxTessellationControlPerVertexInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 882 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 883 | if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 884 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 885 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 886 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u " |
| 887 | "components by %u components", |
| 888 | limits.maxTessellationControlPerVertexOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 889 | num_comp_out - limits.maxTessellationControlPerVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 890 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 891 | if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 892 | skip |= |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 893 | LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 894 | "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that " |
| 895 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)", |
| 896 | limits.maxTessellationControlPerVertexOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 897 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 898 | break; |
| 899 | |
| 900 | case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 901 | if (num_comp_in > limits.maxTessellationEvaluationInputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 902 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 903 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 904 | "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u " |
| 905 | "components by %u components", |
| 906 | limits.maxTessellationEvaluationInputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 907 | num_comp_in - limits.maxTessellationEvaluationInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 908 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 909 | if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 910 | skip |= |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 911 | LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 912 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that " |
| 913 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)", |
| 914 | limits.maxTessellationEvaluationInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 915 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 916 | if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 917 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 918 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 919 | "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u " |
| 920 | "components by %u components", |
| 921 | limits.maxTessellationEvaluationOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 922 | num_comp_out - limits.maxTessellationEvaluationOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 923 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 924 | if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 925 | skip |= |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 926 | LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 927 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that " |
| 928 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)", |
| 929 | limits.maxTessellationEvaluationOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 930 | } |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 931 | // Portability validation |
| 932 | if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) { |
| 933 | if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 934 | skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_Isolines, |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 935 | "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader" |
| 936 | " is using abstract patch type IsoLines, but this is not supported on this platform"); |
| 937 | } |
| 938 | if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 939 | skip |= LogError(pipeline->pipeline(), kVUID_Portability_Tessellation_PointMode, |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 940 | "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader" |
| 941 | " is using abstract patch type PointMode, but this is not supported on this platform"); |
| 942 | } |
| 943 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 944 | break; |
| 945 | |
| 946 | case VK_SHADER_STAGE_GEOMETRY_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 947 | if (num_comp_in > limits.maxGeometryInputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 948 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 949 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 950 | "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u " |
| 951 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 952 | limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 953 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 954 | if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 955 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 956 | "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that " |
| 957 | "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)", |
| 958 | limits.maxGeometryInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 959 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 960 | if (num_comp_out > limits.maxGeometryOutputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 961 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 962 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 963 | "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u " |
| 964 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 965 | limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 966 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 967 | if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 968 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 969 | "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that " |
| 970 | "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)", |
| 971 | limits.maxGeometryOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 972 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 973 | if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 974 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 975 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 976 | "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u " |
| 977 | "components by %u components", |
| 978 | limits.maxGeometryTotalOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 979 | num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 980 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 981 | break; |
| 982 | |
| 983 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 984 | if (num_comp_in > limits.maxFragmentInputComponents) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 985 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 986 | "Invalid Pipeline CreateInfo State: Fragment shader exceeds " |
| 987 | "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u " |
| 988 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 989 | limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 990 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 991 | if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 992 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_ExceedDeviceLimit, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 993 | "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that " |
| 994 | "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)", |
| 995 | limits.maxFragmentInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 996 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 997 | break; |
| 998 | |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 999 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 1000 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 1001 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 1002 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 1003 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 1004 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 1005 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 1006 | case VK_SHADER_STAGE_MESH_BIT_NV: |
| 1007 | break; |
| 1008 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1009 | default: |
| 1010 | assert(false); // This should never happen |
| 1011 | } |
| 1012 | return skip; |
| 1013 | } |
| 1014 | |
Lionel Landwerlin | 892d6c3 | 2021-05-05 12:56:19 +0300 | [diff] [blame] | 1015 | bool CoreChecks::ValidateShaderStorageImageFormats(SHADER_MODULE_STATE const *src) const { |
| 1016 | bool skip = false; |
| 1017 | |
| 1018 | std::map<uint32_t, uint32_t> loads; |
| 1019 | for (auto insn : *src) { |
| 1020 | switch (insn.opcode()) { |
| 1021 | case spv::OpLoad: { |
| 1022 | loads.insert(std::make_pair(insn.word(2), insn.word(1))); |
| 1023 | break; |
| 1024 | } |
| 1025 | default: |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | // Got through all ImageRead/Write instructions |
| 1031 | for (auto insn : *src) { |
| 1032 | switch (insn.opcode()) { |
| 1033 | case spv::OpImageSparseRead: |
| 1034 | case spv::OpImageRead: { |
| 1035 | spirv_inst_iter type_def = src->GetImageFormatInst(loads, insn.word(3)); |
| 1036 | if (type_def != src->end()) { |
| 1037 | if (type_def.word(8) == spv::ImageFormatUnknown) { |
| 1038 | skip |= RequireFeature(enabled_features.core.shaderStorageImageReadWithoutFormat, |
| 1039 | "shaderStorageImageReadWithoutFormat", |
| 1040 | kVUID_Features_shaderStorageImageReadWithoutFormat); |
| 1041 | } |
| 1042 | } else { |
| 1043 | skip |= LogWarning(device, kVUIDUndefined, |
| 1044 | "Cannot find image definition (id = %" PRIu32 ")", |
| 1045 | insn.word(3)); |
| 1046 | } |
| 1047 | break; |
| 1048 | } |
| 1049 | case spv::OpImageWrite: { |
| 1050 | spirv_inst_iter type_def = src->GetImageFormatInst(loads, insn.word(1)); |
| 1051 | if (type_def != src->end()) { |
| 1052 | if (type_def.word(8) == spv::ImageFormatUnknown) { |
| 1053 | skip |= RequireFeature(enabled_features.core.shaderStorageImageWriteWithoutFormat, |
| 1054 | "shaderStorageImageWriteWithoutFormat", |
| 1055 | kVUID_Features_shaderStorageImageWriteWithoutFormat); |
| 1056 | } |
| 1057 | } else { |
| 1058 | skip |= LogWarning(device, kVUIDUndefined, |
| 1059 | "Cannot find image definition (id = %" PRIu32 ")", |
| 1060 | insn.word(1)); |
| 1061 | } |
| 1062 | break; |
| 1063 | } |
| 1064 | |
| 1065 | } |
| 1066 | } |
| 1067 | |
Lionel Landwerlin | 38d2e12 | 2021-07-21 14:21:47 +0300 | [diff] [blame^] | 1068 | // Go through all variables for images and check decorations |
| 1069 | for (auto insn : *src) { |
| 1070 | if (insn.opcode() != spv::OpVariable) |
| 1071 | continue; |
| 1072 | |
| 1073 | uint32_t var = insn.word(2); |
| 1074 | spirv_inst_iter type_def = src->GetImageFormatInst(loads, insn.word(1)); |
| 1075 | if (type_def == src->end()) |
| 1076 | continue; |
| 1077 | if (type_def.word(8) != spv::ImageFormatUnknown) |
| 1078 | continue; |
| 1079 | |
| 1080 | decoration_set img_decorations = src->get_decorations(var); |
| 1081 | |
| 1082 | if (!enabled_features.core.shaderStorageImageReadWithoutFormat && |
| 1083 | !(img_decorations.flags & decoration_set::nonreadable_bit)) { |
| 1084 | skip |= LogError(device, |
| 1085 | kVUID_Features_shaderStorageImageReadWithoutFormat_NonReadable, |
| 1086 | "shaderStorageImageReadWithoutFormat not supported but variable %" PRIu32 " " |
| 1087 | " without format not marked a NonReadable", var); |
| 1088 | } |
| 1089 | |
| 1090 | if (!enabled_features.core.shaderStorageImageWriteWithoutFormat && |
| 1091 | !(img_decorations.flags & decoration_set::nonwritable_bit)) { |
| 1092 | skip |= LogError(device, |
| 1093 | kVUID_Features_shaderStorageImageWriteWithoutFormat_NonWritable, |
| 1094 | "shaderStorageImageWriteWithoutFormat not supported but variable %" PRIu32 " " |
| 1095 | "without format not marked a NonWritable", var); |
| 1096 | } |
| 1097 | } |
| 1098 | |
Lionel Landwerlin | 892d6c3 | 2021-05-05 12:56:19 +0300 | [diff] [blame] | 1099 | return skip; |
| 1100 | } |
| 1101 | |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 1102 | bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const { |
| 1103 | bool skip = false; |
| 1104 | uint32_t total_resources = 0; |
| 1105 | |
| 1106 | // Only currently testing for graphics and compute pipelines |
| 1107 | // TODO: Add check and support for Ray Tracing pipeline VUID 03428 |
| 1108 | if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) { |
| 1109 | return false; |
| 1110 | } |
| 1111 | |
| 1112 | if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 1113 | // "For the fragment shader stage the framebuffer color attachments also count against this limit" |
| 1114 | total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].colorAttachmentCount; |
| 1115 | } |
| 1116 | |
| 1117 | // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle |
| 1118 | // input from CreatePipeline and CreatePipelineLayout level |
| 1119 | for (auto set_layout : pipeline->pipeline_layout->set_layouts) { |
| 1120 | if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) { |
| 1121 | continue; |
| 1122 | } |
| 1123 | |
| 1124 | for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) { |
| 1125 | const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx); |
| 1126 | // Bindings with a descriptorCount of 0 are "reserved" and should be skipped |
| 1127 | if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) { |
| 1128 | // Check only descriptor types listed in maxPerStageResources description in spec |
| 1129 | switch (binding->descriptorType) { |
| 1130 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 1131 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 1132 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 1133 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 1134 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 1135 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 1136 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 1137 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 1138 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 1139 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 1140 | total_resources += binding->descriptorCount; |
| 1141 | break; |
| 1142 | default: |
| 1143 | break; |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | if (total_resources > phys_dev_props.limits.maxPerStageResources) { |
| 1150 | const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687" |
| 1151 | : "VUID-VkGraphicsPipelineCreateInfo-layout-01688"; |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1152 | skip |= LogError(pipeline->pipeline(), vuid, |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 1153 | "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit " |
| 1154 | "VkPhysicalDeviceLimits::maxPerStageResources (%u)", |
| 1155 | string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources); |
| 1156 | } |
| 1157 | |
| 1158 | return skip; |
| 1159 | } |
| 1160 | |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1161 | // copy the specialization constant value into buf, if it is present |
| 1162 | void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) { |
| 1163 | VkSpecializationInfo const *spec = pStage->pSpecializationInfo; |
| 1164 | |
| 1165 | if (spec && spec_id < spec->mapEntryCount) { |
| 1166 | memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | // Fill in value with the constant or specialization constant value, if available. |
| 1171 | // Returns true if the value has been accurately filled out. |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 1172 | static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1173 | const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1174 | auto type_id = src->get_def(insn.word(1)); |
| 1175 | if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) { |
| 1176 | return false; |
| 1177 | } |
| 1178 | switch (insn.opcode()) { |
| 1179 | case spv::OpSpecConstant: |
| 1180 | *value = insn.word(3); |
| 1181 | GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value); |
| 1182 | return true; |
| 1183 | case spv::OpConstant: |
| 1184 | *value = insn.word(3); |
| 1185 | return true; |
| 1186 | default: |
| 1187 | return false; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | // Map SPIR-V type to VK_COMPONENT_TYPE enum |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 1192 | VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1193 | switch (insn.opcode()) { |
| 1194 | case spv::OpTypeInt: |
| 1195 | switch (insn.word(2)) { |
| 1196 | case 8: |
| 1197 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV; |
| 1198 | case 16: |
| 1199 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV; |
| 1200 | case 32: |
| 1201 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV; |
| 1202 | case 64: |
| 1203 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV; |
| 1204 | default: |
| 1205 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 1206 | } |
| 1207 | case spv::OpTypeFloat: |
| 1208 | switch (insn.word(2)) { |
| 1209 | case 16: |
| 1210 | return VK_COMPONENT_TYPE_FLOAT16_NV; |
| 1211 | case 32: |
| 1212 | return VK_COMPONENT_TYPE_FLOAT32_NV; |
| 1213 | case 64: |
| 1214 | return VK_COMPONENT_TYPE_FLOAT64_NV; |
| 1215 | default: |
| 1216 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 1217 | } |
| 1218 | default: |
| 1219 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | // Validate SPV_NV_cooperative_matrix behavior that can't be statically validated |
| 1224 | // in SPIRV-Tools (e.g. due to specialization constant usage). |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 1225 | bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 1226 | const PIPELINE_STATE *pipeline) const { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1227 | bool skip = false; |
| 1228 | |
| 1229 | // Map SPIR-V result ID to specialization constant id (SpecId decoration value) |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1230 | layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id; |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1231 | // Map SPIR-V result ID to the ID of its type. |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1232 | layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id; |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1233 | |
| 1234 | struct CoopMatType { |
| 1235 | uint32_t scope, rows, cols; |
| 1236 | VkComponentTypeNV component_type; |
| 1237 | bool all_constant; |
| 1238 | |
| 1239 | CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {} |
| 1240 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 1241 | void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1242 | const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1243 | spirv_inst_iter insn = src->get_def(id); |
| 1244 | uint32_t component_type_id = insn.word(2); |
| 1245 | uint32_t scope_id = insn.word(3); |
| 1246 | uint32_t rows_id = insn.word(4); |
| 1247 | uint32_t cols_id = insn.word(5); |
| 1248 | auto component_type_iter = src->get_def(component_type_id); |
| 1249 | auto scope_iter = src->get_def(scope_id); |
| 1250 | auto rows_iter = src->get_def(rows_id); |
| 1251 | auto cols_iter = src->get_def(cols_id); |
| 1252 | |
| 1253 | all_constant = true; |
| 1254 | if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) { |
| 1255 | all_constant = false; |
| 1256 | } |
| 1257 | if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) { |
| 1258 | all_constant = false; |
| 1259 | } |
| 1260 | if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) { |
| 1261 | all_constant = false; |
| 1262 | } |
| 1263 | component_type = GetComponentType(component_type_iter, src); |
| 1264 | } |
| 1265 | }; |
| 1266 | |
| 1267 | bool seen_coopmat_capability = false; |
| 1268 | |
| 1269 | for (auto insn : *src) { |
| 1270 | // Whitelist instructions whose result can be a cooperative matrix type, and |
| 1271 | // keep track of their types. It would be nice if SPIRV-Headers generated code |
| 1272 | // to identify which instructions have a result type and result id. Lacking that, |
| 1273 | // this whitelist is based on the set of instructions that |
| 1274 | // SPV_NV_cooperative_matrix says can be used with cooperative matrix types. |
| 1275 | switch (insn.opcode()) { |
| 1276 | case spv::OpLoad: |
| 1277 | case spv::OpCooperativeMatrixLoadNV: |
| 1278 | case spv::OpCooperativeMatrixMulAddNV: |
| 1279 | case spv::OpSNegate: |
| 1280 | case spv::OpFNegate: |
| 1281 | case spv::OpIAdd: |
| 1282 | case spv::OpFAdd: |
| 1283 | case spv::OpISub: |
| 1284 | case spv::OpFSub: |
| 1285 | case spv::OpFDiv: |
| 1286 | case spv::OpSDiv: |
| 1287 | case spv::OpUDiv: |
| 1288 | case spv::OpMatrixTimesScalar: |
| 1289 | case spv::OpConstantComposite: |
| 1290 | case spv::OpCompositeConstruct: |
| 1291 | case spv::OpConvertFToU: |
| 1292 | case spv::OpConvertFToS: |
| 1293 | case spv::OpConvertSToF: |
| 1294 | case spv::OpConvertUToF: |
| 1295 | case spv::OpUConvert: |
| 1296 | case spv::OpSConvert: |
| 1297 | case spv::OpFConvert: |
| 1298 | id_to_type_id[insn.word(2)] = insn.word(1); |
| 1299 | break; |
| 1300 | default: |
| 1301 | break; |
| 1302 | } |
| 1303 | |
| 1304 | switch (insn.opcode()) { |
| 1305 | case spv::OpDecorate: |
| 1306 | if (insn.word(2) == spv::DecorationSpecId) { |
| 1307 | id_to_spec_id[insn.word(1)] = insn.word(3); |
| 1308 | } |
| 1309 | break; |
| 1310 | case spv::OpCapability: |
| 1311 | if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) { |
| 1312 | seen_coopmat_capability = true; |
| 1313 | |
| 1314 | if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1315 | skip |= LogError( |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1316 | pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixSupportedStages, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1317 | "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)", |
| 1318 | phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | break; |
| 1322 | case spv::OpMemoryModel: |
| 1323 | // If the capability isn't enabled, don't bother with the rest of this function. |
| 1324 | // OpMemoryModel is the first required instruction after all OpCapability instructions. |
| 1325 | if (!seen_coopmat_capability) { |
| 1326 | return skip; |
| 1327 | } |
| 1328 | break; |
| 1329 | case spv::OpTypeCooperativeMatrixNV: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1330 | CoopMatType m; |
| 1331 | m.Init(insn.word(1), src, pStage, id_to_spec_id); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1332 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1333 | if (m.all_constant) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1334 | // Validate that the type parameters are all supported for one of the |
| 1335 | // operands of a cooperative matrix property. |
| 1336 | bool valid = false; |
| 1337 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1338 | if (cooperative_matrix_properties[i].AType == m.component_type && |
| 1339 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols && |
| 1340 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1341 | valid = true; |
| 1342 | break; |
| 1343 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1344 | if (cooperative_matrix_properties[i].BType == m.component_type && |
| 1345 | cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 1346 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1347 | valid = true; |
| 1348 | break; |
| 1349 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1350 | if (cooperative_matrix_properties[i].CType == m.component_type && |
| 1351 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 1352 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1353 | valid = true; |
| 1354 | break; |
| 1355 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1356 | if (cooperative_matrix_properties[i].DType == m.component_type && |
| 1357 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 1358 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1359 | valid = true; |
| 1360 | break; |
| 1361 | } |
| 1362 | } |
| 1363 | if (!valid) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1364 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixType, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1365 | "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type", |
| 1366 | insn.word(1)); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | break; |
| 1370 | } |
| 1371 | case spv::OpCooperativeMatrixMulAddNV: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1372 | CoopMatType a, b, c, d; |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1373 | if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() || |
| 1374 | id_to_type_id.find(insn.word(3)) == id_to_type_id.end() || |
| 1375 | id_to_type_id.find(insn.word(4)) == id_to_type_id.end() || |
| 1376 | id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) { |
Mike Schuchardt | e48dc14 | 2019-04-18 09:12:03 -0700 | [diff] [blame] | 1377 | // Couldn't find type of matrix |
| 1378 | assert(false); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1379 | break; |
| 1380 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1381 | d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id); |
| 1382 | a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id); |
| 1383 | b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id); |
| 1384 | c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1385 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1386 | if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1387 | // Validate that the type parameters are all supported for the same |
| 1388 | // cooperative matrix property. |
| 1389 | bool valid = false; |
| 1390 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1391 | if (cooperative_matrix_properties[i].AType == a.component_type && |
| 1392 | cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols && |
| 1393 | cooperative_matrix_properties[i].scope == a.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1394 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1395 | cooperative_matrix_properties[i].BType == b.component_type && |
| 1396 | cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols && |
| 1397 | cooperative_matrix_properties[i].scope == b.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1398 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1399 | cooperative_matrix_properties[i].CType == c.component_type && |
| 1400 | cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols && |
| 1401 | cooperative_matrix_properties[i].scope == c.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1402 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1403 | cooperative_matrix_properties[i].DType == d.component_type && |
| 1404 | cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols && |
| 1405 | cooperative_matrix_properties[i].scope == d.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1406 | valid = true; |
| 1407 | break; |
| 1408 | } |
| 1409 | } |
| 1410 | if (!valid) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1411 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_CooperativeMatrixMulAdd, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1412 | "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix " |
| 1413 | "VkCooperativeMatrixPropertiesNV", |
| 1414 | insn.word(2)); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | break; |
| 1418 | } |
| 1419 | default: |
| 1420 | break; |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | return skip; |
| 1425 | } |
| 1426 | |
Jeff Leger | 9b3dcff | 2021-05-27 15:40:20 -0400 | [diff] [blame] | 1427 | bool CoreChecks::ValidateShaderResolveQCOM(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
| 1428 | const PIPELINE_STATE *pipeline) const { |
| 1429 | bool skip = false; |
| 1430 | |
| 1431 | // If the pipeline's subpass description contains flag VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM, |
| 1432 | // then the fragment shader must not enable the SPIRV SampleRateShading capability. |
| 1433 | if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 1434 | for (auto insn : *src) { |
| 1435 | switch (insn.opcode()) { |
| 1436 | case spv::OpCapability: |
| 1437 | if (insn.word(1) == spv::CapabilitySampleRateShading) { |
| 1438 | auto subpass_flags = |
| 1439 | (pipeline->rp_state == nullptr) |
| 1440 | ? 0 |
| 1441 | : pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].flags; |
| 1442 | if ((subpass_flags & VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM) != 0) { |
| 1443 | skip |= |
| 1444 | LogError(pipeline->pipeline(), kVUID_Core_Shader_ResolveQCOM_InvalidCapability, |
| 1445 | "Invalid Pipeline CreateInfo State: fragment shader enables SampleRateShading capability " |
| 1446 | "and the subpass flags includes VK_SUBPASS_DESCRIPTION_FRAGMENT_REGION_BIT_QCOM."); |
| 1447 | } |
| 1448 | } |
| 1449 | break; |
| 1450 | default: |
| 1451 | break; |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | return skip; |
| 1457 | } |
| 1458 | |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 1459 | bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1460 | auto entrypoint_id = entrypoint.word(2); |
| 1461 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 1462 | // The first denorm execution mode encountered, along with its bit width. |
| 1463 | // Used to check if SeparateDenormSettings is respected. |
| 1464 | std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1465 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 1466 | // The first rounding mode encountered, along with its bit width. |
| 1467 | // Used to check if SeparateRoundingModeSettings is respected. |
| 1468 | std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1469 | |
| 1470 | bool skip = false; |
| 1471 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1472 | uint32_t vertices_out = 0; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 1473 | uint32_t invocations = 0; |
| 1474 | |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 1475 | auto it = src->execution_mode_inst.find(entrypoint_id); |
| 1476 | if (it != src->execution_mode_inst.end()) { |
| 1477 | for (auto insn : it->second) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1478 | auto mode = insn.word(2); |
| 1479 | switch (mode) { |
| 1480 | case spv::ExecutionModeSignedZeroInfNanPreserve: { |
| 1481 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1482 | if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) || |
| 1483 | (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) || |
| 1484 | (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1485 | skip |= LogError( |
| 1486 | device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1487 | "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device", |
| 1488 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1489 | } |
| 1490 | break; |
| 1491 | } |
| 1492 | |
| 1493 | case spv::ExecutionModeDenormPreserve: { |
| 1494 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1495 | if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) || |
| 1496 | (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) || |
| 1497 | (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1498 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1499 | "Shader requires DenormPreserve for bit width %d but it is not enabled on the device", |
| 1500 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 1503 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 1504 | // Register the first denorm execution mode found |
| 1505 | first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1506 | } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) { |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1507 | switch (phys_dev_props_core12.denormBehaviorIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1508 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1509 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1510 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1511 | "Shader uses different denorm execution modes for 16 and 64-bit but " |
| 1512 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1513 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1514 | } |
| 1515 | break; |
| 1516 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1517 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1518 | break; |
| 1519 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1520 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1521 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1522 | "Shader uses different denorm execution modes for different bit widths but " |
| 1523 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1524 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1525 | break; |
| 1526 | |
| 1527 | default: |
| 1528 | break; |
| 1529 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1530 | } |
| 1531 | break; |
| 1532 | } |
| 1533 | |
| 1534 | case spv::ExecutionModeDenormFlushToZero: { |
| 1535 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1536 | if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) || |
| 1537 | (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) || |
| 1538 | (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1539 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1540 | "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device", |
| 1541 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 1544 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 1545 | // Register the first denorm execution mode found |
| 1546 | first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1547 | } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) { |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1548 | switch (phys_dev_props_core12.denormBehaviorIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1549 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1550 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1551 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1552 | "Shader uses different denorm execution modes for 16 and 64-bit but " |
| 1553 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1554 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1555 | } |
| 1556 | break; |
| 1557 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1558 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1559 | break; |
| 1560 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1561 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1562 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1563 | "Shader uses different denorm execution modes for different bit widths but " |
| 1564 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1565 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1566 | break; |
| 1567 | |
| 1568 | default: |
| 1569 | break; |
| 1570 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1571 | } |
| 1572 | break; |
| 1573 | } |
| 1574 | |
| 1575 | case spv::ExecutionModeRoundingModeRTE: { |
| 1576 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1577 | if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) || |
| 1578 | (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) || |
| 1579 | (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1580 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1581 | "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device", |
| 1582 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 1585 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 1586 | // Register the first rounding mode found |
| 1587 | first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1588 | } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) { |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1589 | switch (phys_dev_props_core12.roundingModeIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1590 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1591 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1592 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1593 | "Shader uses different rounding modes for 16 and 64-bit but " |
| 1594 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1595 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1596 | } |
| 1597 | break; |
| 1598 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1599 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1600 | break; |
| 1601 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1602 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1603 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1604 | "Shader uses different rounding modes for different bit widths but " |
| 1605 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1606 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1607 | break; |
| 1608 | |
| 1609 | default: |
| 1610 | break; |
| 1611 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1612 | } |
| 1613 | break; |
| 1614 | } |
| 1615 | |
| 1616 | case spv::ExecutionModeRoundingModeRTZ: { |
| 1617 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1618 | if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) || |
| 1619 | (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) || |
| 1620 | (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1621 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1622 | "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device", |
| 1623 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 1626 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 1627 | // Register the first rounding mode found |
| 1628 | first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1629 | } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) { |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 1630 | switch (phys_dev_props_core12.roundingModeIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1631 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1632 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1633 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1634 | "Shader uses different rounding modes for 16 and 64-bit but " |
| 1635 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1636 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1637 | } |
| 1638 | break; |
| 1639 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1640 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1641 | break; |
| 1642 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1643 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1644 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 1645 | "Shader uses different rounding modes for different bit widths but " |
| 1646 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1647 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 1648 | break; |
| 1649 | |
| 1650 | default: |
| 1651 | break; |
| 1652 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1653 | } |
| 1654 | break; |
| 1655 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 1656 | |
| 1657 | case spv::ExecutionModeOutputVertices: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1658 | vertices_out = insn.word(3); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 1659 | break; |
| 1660 | } |
| 1661 | |
| 1662 | case spv::ExecutionModeInvocations: { |
| 1663 | invocations = insn.word(3); |
| 1664 | break; |
| 1665 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 1670 | if (entrypoint.word(1) == spv::ExecutionModelGeometry) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1671 | if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1672 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714", |
| 1673 | "Geometry shader entry point must have an OpExecutionMode instruction that " |
| 1674 | "specifies a maximum output vertex count that is greater than 0 and less " |
| 1675 | "than or equal to maxGeometryOutputVertices. " |
| 1676 | "OutputVertices=%d, maxGeometryOutputVertices=%d", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1677 | vertices_out, phys_dev_props.limits.maxGeometryOutputVertices); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1681 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715", |
| 1682 | "Geometry shader entry point must have an OpExecutionMode instruction that " |
| 1683 | "specifies an invocation count that is greater than 0 and less " |
| 1684 | "than or equal to maxGeometryShaderInvocations. " |
| 1685 | "Invocations=%d, maxGeometryShaderInvocations=%d", |
| 1686 | invocations, phys_dev_props.limits.maxGeometryShaderInvocations); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 1687 | } |
| 1688 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1689 | return skip; |
| 1690 | } |
| 1691 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1692 | // For given pipelineLayout verify that the set_layout_node at slot.first |
| 1693 | // has the requested binding at slot.second and return ptr to that binding |
Mark Lobodzinski | ca6ebe3 | 2019-04-25 11:43:37 -0600 | [diff] [blame] | 1694 | static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1695 | descriptor_slot_t slot) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1696 | if (!pipelineLayout) return nullptr; |
| 1697 | |
| 1698 | if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr; |
| 1699 | |
| 1700 | return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second); |
| 1701 | } |
| 1702 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1703 | // If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize |
| 1704 | // o If there is only a vertex shader : gl_PointSize must be written when using points |
| 1705 | // o If there is a geometry or tessellation shader: |
| 1706 | // - If shaderTessellationAndGeometryPointSize feature is enabled: |
| 1707 | // * gl_PointSize must be written in the final geometry stage |
| 1708 | // - If shaderTessellationAndGeometryPointSize feature is disabled: |
| 1709 | // * gl_PointSize must NOT be written and a default of 1.0 is assumed |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 1710 | bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 1711 | spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1712 | if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 1713 | return false; |
| 1714 | } |
| 1715 | |
| 1716 | bool pointsize_written = false; |
| 1717 | bool skip = false; |
| 1718 | |
| 1719 | // Search for PointSize built-in decorations |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 1720 | for (auto set : src->builtin_decoration_list) { |
| 1721 | auto insn = src->at(set.offset); |
| 1722 | if (set.builtin == spv::BuiltInPointSize) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 1723 | pointsize_written = src->IsBuiltInWritten(insn, entrypoint); |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 1724 | if (pointsize_written) { |
| 1725 | break; |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1726 | } |
| 1727 | } |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) && |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 1731 | !enabled_features.core.shaderTessellationAndGeometryPointSize) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1732 | if (pointsize_written) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1733 | skip |= LogError(pipeline->pipeline(), kVUID_Core_Shader_PointSizeBuiltInOverSpecified, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1734 | "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which " |
| 1735 | "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled."); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1736 | } |
| 1737 | } else if (!pointsize_written) { |
| 1738 | skip |= |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1739 | LogError(pipeline->pipeline(), kVUID_Core_Shader_MissingPointSizeBuiltIn, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1740 | "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.", |
| 1741 | string_VkShaderStageFlagBits(stage)); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1742 | } |
| 1743 | return skip; |
| 1744 | } |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 1745 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1746 | bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src, |
| 1747 | spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const { |
| 1748 | bool primitiverate_written = false; |
| 1749 | bool viewportindex_written = false; |
| 1750 | bool viewportmask_written = false; |
| 1751 | bool skip = false; |
| 1752 | |
| 1753 | // Check if the primitive shading rate is written |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 1754 | for (auto set : src->builtin_decoration_list) { |
| 1755 | auto insn = src->at(set.offset); |
| 1756 | if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 1757 | primitiverate_written = src->IsBuiltInWritten(insn, entrypoint); |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 1758 | } else if (set.builtin == spv::BuiltInViewportIndex) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 1759 | viewportindex_written = src->IsBuiltInWritten(insn, entrypoint); |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 1760 | } else if (set.builtin == spv::BuiltInViewportMaskNV) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 1761 | viewportmask_written = src->IsBuiltInWritten(insn, entrypoint); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1762 | } |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 1763 | if (primitiverate_written && viewportindex_written && viewportmask_written) { |
| 1764 | break; |
| 1765 | } |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Tony-LunarG | d44844c | 2021-01-22 13:24:37 -0700 | [diff] [blame] | 1768 | if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports && |
| 1769 | pipeline->graphicsPipelineCI.pViewportState) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1770 | if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && |
| 1771 | pipeline->graphicsPipelineCI.pViewportState->viewportCount > 1 && primitiverate_written) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1772 | skip |= LogError(pipeline->pipeline(), |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 1773 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503", |
| 1774 | "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but " |
| 1775 | "multiple viewports " |
| 1776 | "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 1777 | string_VkShaderStageFlagBits(stage)); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
| 1780 | if (primitiverate_written && viewportindex_written) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1781 | skip |= LogError(pipeline->pipeline(), |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1782 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504", |
| 1783 | "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and " |
| 1784 | "ViewportIndex built-ins," |
| 1785 | "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 1786 | string_VkShaderStageFlagBits(stage)); |
| 1787 | } |
| 1788 | |
| 1789 | if (primitiverate_written && viewportmask_written) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1790 | skip |= LogError(pipeline->pipeline(), |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1791 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505", |
| 1792 | "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and " |
| 1793 | "ViewportMaskNV built-ins," |
| 1794 | "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 1795 | string_VkShaderStageFlagBits(stage)); |
| 1796 | } |
| 1797 | } |
| 1798 | return skip; |
| 1799 | } |
| 1800 | |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame] | 1801 | // Validate runtime usage of various opcodes that depends on what Vulkan properties or features are exposed |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 1802 | bool CoreChecks::ValidatePropertiesAndFeatures(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const { |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame] | 1803 | bool skip = false; |
| 1804 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 1805 | switch (insn.opcode()) { |
| 1806 | case spv::OpReadClockKHR: { |
| 1807 | auto scope_id = module->get_def(insn.word(3)); |
| 1808 | auto scope_type = scope_id.word(3); |
| 1809 | // if scope isn't Subgroup or Device, spirv-val will catch |
| 1810 | if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_feature.shaderSubgroupClock == VK_FALSE)) { |
| 1811 | skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderSubgroupClock", |
| 1812 | "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1813 | report_data->FormatHandle(module->vk_shader_module()).c_str()); |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 1814 | } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_feature.shaderDeviceClock == VK_FALSE)) { |
| 1815 | skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderDeviceClock", |
| 1816 | "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1817 | report_data->FormatHandle(module->vk_shader_module()).c_str()); |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame] | 1818 | } |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 1819 | break; |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | return skip; |
| 1823 | } |
| 1824 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 1825 | bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline, |
Jeremy Gebben | 159b3cc | 2021-06-03 09:09:03 -0600 | [diff] [blame] | 1826 | const PipelineStageState &stage_state, const SHADER_MODULE_STATE *module, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 1827 | const spirv_inst_iter &entrypoint, bool check_point_size) const { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 1828 | bool skip = false; |
| 1829 | |
| 1830 | // Check the module |
| 1831 | if (!module->has_valid_spirv) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1832 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", |
| 1833 | "%s does not contain valid spirv for stage %s.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1834 | report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(pStage->stage)); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 1835 | } |
| 1836 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1837 | // If specialization-constant values are given and specialization-constant instructions are present in the shader, the |
| 1838 | // specializations should be applied and validated. |
| 1839 | if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 && |
| 1840 | pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) { |
| 1841 | // Gather the specialization-constant values. |
| 1842 | auto const &specialization_info = pStage->pSpecializationInfo; |
Jeremy Hayes | 521221d | 2020-01-15 16:48:49 -0700 | [diff] [blame] | 1843 | auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData); |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1844 | std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; // note: this must be std:: to work with spvtools |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1845 | id_value_map.reserve(specialization_info->mapEntryCount); |
| 1846 | for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) { |
| 1847 | auto const &map_entry = specialization_info->pMapEntries[i]; |
sfricke-samsung | 033b026 | 2021-07-09 00:53:06 -0700 | [diff] [blame] | 1848 | auto itr = module->spec_const_map.find(map_entry.constantID); |
| 1849 | // "If a constantID value is not a specialization constant ID used in the shader, that map entry does not affect the |
| 1850 | // behavior of the pipeline." |
| 1851 | if (itr != module->spec_const_map.cend()) { |
| 1852 | // Make sure map_entry.size matches the spec constant's size |
| 1853 | uint32_t spec_const_size = decoration_set::kInvalidValue; |
| 1854 | const auto def_ins = module->get_def(itr->second); |
| 1855 | const auto type_ins = module->get_def(def_ins.word(1)); |
| 1856 | // Specialization constants can only be of type bool, scalar integer, or scalar floating point |
| 1857 | switch (type_ins.opcode()) { |
| 1858 | case spv::OpTypeBool: |
| 1859 | // "If the specialization constant is of type boolean, size must be the byte size of VkBool32" |
| 1860 | spec_const_size = sizeof(VkBool32); |
| 1861 | break; |
| 1862 | case spv::OpTypeInt: |
| 1863 | case spv::OpTypeFloat: |
| 1864 | spec_const_size = type_ins.word(2) / 8; |
| 1865 | break; |
| 1866 | default: |
| 1867 | // spirv-val should catch if SpecId is not used on a OpSpecConstantTrue/OpSpecConstantFalse/OpSpecConstant |
| 1868 | // and OpSpecConstant is validated to be a OpTypeInt or OpTypeFloat |
| 1869 | break; |
| 1870 | } |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1871 | |
sfricke-samsung | 033b026 | 2021-07-09 00:53:06 -0700 | [diff] [blame] | 1872 | if (map_entry.size != spec_const_size) { |
| 1873 | skip |= |
| 1874 | LogError(device, "VUID-VkSpecializationMapEntry-constantID-00776", |
Nathaniel Cesario | cf69bda | 2021-06-22 13:23:42 -0600 | [diff] [blame] | 1875 | "Specialization constant (ID = %" PRIu32 ", entry = %" PRIu32 |
| 1876 | ") has invalid size %zu in shader module %s. Expected size is %" PRIu32 " from shader definition.", |
| 1877 | map_entry.constantID, i, map_entry.size, |
| 1878 | report_data->FormatHandle(module->vk_shader_module()).c_str(), spec_const_size); |
sfricke-samsung | 033b026 | 2021-07-09 00:53:06 -0700 | [diff] [blame] | 1879 | } |
Nathaniel Cesario | cf69bda | 2021-06-22 13:23:42 -0600 | [diff] [blame] | 1880 | } |
| 1881 | |
Jeremy Gebben | 12933ef | 2021-05-12 17:16:27 -0600 | [diff] [blame] | 1882 | if ((map_entry.offset + map_entry.size) <= specialization_info->dataSize) { |
| 1883 | auto entry = id_value_map.emplace(map_entry.constantID, std::vector<uint32_t>(map_entry.size > 4 ? 2 : 1)); |
| 1884 | memcpy(entry.first->second.data(), specialization_data + map_entry.offset, map_entry.size); |
| 1885 | } |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | // Apply the specialization-constant values and revalidate the shader module. |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 1889 | spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled)); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1890 | spvtools::Optimizer optimizer(spirv_environment); |
| 1891 | spvtools::MessageConsumer consumer = [&skip, &module, &pStage, this](spv_message_level_t level, const char *source, |
| 1892 | const spv_position_t &position, const char *message) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1893 | skip |= LogError( |
| 1894 | device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s. %s", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1895 | report_data->FormatHandle(module->vk_shader_module()).c_str(), string_VkShaderStageFlagBits(pStage->stage), message); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1896 | }; |
| 1897 | optimizer.SetMessageConsumer(consumer); |
| 1898 | optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map)); |
| 1899 | optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass()); |
| 1900 | std::vector<uint32_t> specialized_spirv; |
| 1901 | auto const optimized = |
| 1902 | optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, spvtools::ValidatorOptions(), true); |
| 1903 | assert(optimized == true); |
| 1904 | |
| 1905 | if (optimized) { |
| 1906 | spv_context ctx = spvContextCreate(spirv_environment); |
| 1907 | spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()}; |
| 1908 | spv_diagnostic diag = nullptr; |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 1909 | spvtools::ValidatorOptions options; |
| 1910 | AdjustValidatorOptions(device_extensions, enabled_features, options); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1911 | auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
| 1912 | if (spv_valid != SPV_SUCCESS) { |
sfricke-samsung | d379380 | 2020-08-18 22:55:03 -0700 | [diff] [blame] | 1913 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1914 | "After specialization was applied, %s does not contain valid spirv for stage %s.", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 1915 | report_data->FormatHandle(module->vk_shader_module()).c_str(), |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1916 | string_VkShaderStageFlagBits(pStage->stage)); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1917 | } |
| 1918 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 1919 | spvDiagnosticDestroy(diag); |
| 1920 | spvContextDestroy(ctx); |
| 1921 | } |
| 1922 | } |
| 1923 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 1924 | // Check the entrypoint |
| 1925 | if (entrypoint == module->end()) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1926 | skip |= |
Petr Kraus | b0d5e59 | 2021-05-21 23:37:11 +0200 | [diff] [blame] | 1927 | LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s.", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1928 | pStage->pName, string_VkShaderStageFlagBits(pStage->stage)); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 1929 | } |
| 1930 | if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage. |
| 1931 | |
| 1932 | // Mark accessible ids |
| 1933 | auto &accessible_ids = stage_state.accessible_ids; |
| 1934 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1935 | // Validate descriptor set layout against what the entrypoint actually uses |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 1936 | bool has_writable_descriptor = stage_state.has_writable_descriptor; |
| 1937 | auto &descriptor_uses = stage_state.descriptor_uses; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1938 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 1939 | // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless" |
| 1940 | // and mainly only checking the instruction in detail for a single operation |
| 1941 | for (auto insn : *module) { |
| 1942 | skip |= ValidateShaderCapabilitiesAndExtensions(module, insn); |
| 1943 | skip |= ValidatePropertiesAndFeatures(module, insn); |
| 1944 | skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn); |
| 1945 | } |
| 1946 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 1947 | skip |= |
| 1948 | ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, has_writable_descriptor, stage_state.has_atomic_descriptor); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 1949 | skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint); |
Lionel Landwerlin | 892d6c3 | 2021-05-05 12:56:19 +0300 | [diff] [blame] | 1950 | skip |= ValidateShaderStorageImageFormats(module); |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 1951 | skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1952 | skip |= ValidateExecutionModes(module, entrypoint); |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1953 | skip |= ValidateSpecializationOffsets(pStage); |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1954 | if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) { |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 1955 | skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1956 | } |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 1957 | skip |= ValidateBuiltinLimits(module, accessible_ids, pStage->stage); |
sfricke-samsung | d093e52 | 2021-02-26 04:17:45 -0800 | [diff] [blame] | 1958 | if (enabled_features.cooperative_matrix_features.cooperativeMatrix) { |
| 1959 | skip |= ValidateCooperativeMatrix(module, pStage, pipeline); |
| 1960 | } |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1961 | if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) { |
| 1962 | skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage); |
| 1963 | } |
Jeff Leger | 9b3dcff | 2021-05-27 15:40:20 -0400 | [diff] [blame] | 1964 | if (device_extensions.vk_qcom_render_pass_shader_resolve != kNotEnabled) { |
| 1965 | skip |= ValidateShaderResolveQCOM(module, pStage, pipeline); |
| 1966 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1967 | |
sfricke-samsung | 7699b91 | 2021-04-12 23:01:51 -0700 | [diff] [blame] | 1968 | // "layout must be consistent with the layout of the * shader" |
| 1969 | // 'consistent' -> #descriptorsets-pipelinelayout-consistency |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 1970 | std::string vuid_layout_mismatch; |
| 1971 | if (pipeline->graphicsPipelineCI.sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) { |
| 1972 | vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756"; |
| 1973 | } else if (pipeline->computePipelineCI.sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO) { |
| 1974 | vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703"; |
| 1975 | } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR) { |
| 1976 | vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427"; |
| 1977 | } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV) { |
| 1978 | vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427"; |
| 1979 | } |
| 1980 | |
sfricke-samsung | 7699b91 | 2021-04-12 23:01:51 -0700 | [diff] [blame] | 1981 | // Validate Push Constants use |
| 1982 | skip |= ValidatePushConstantUsage(*pipeline, module, pStage, vuid_layout_mismatch); |
| 1983 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1984 | // Validate descriptor use |
| 1985 | for (auto use : descriptor_uses) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1986 | // Verify given pipelineLayout has requested setLayout with requested binding |
Jeff Bolz | e7fc67b | 2019-10-04 12:29:31 -0500 | [diff] [blame] | 1987 | const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1988 | unsigned required_descriptor_count; |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1989 | bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR; |
| 1990 | std::set<uint32_t> descriptor_types = |
| 1991 | TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1992 | |
| 1993 | if (!binding) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 1994 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1995 | "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout", |
| 1996 | use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1997 | } else if (~binding->stageFlags & pStage->stage) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 1998 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1999 | "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first, |
| 2000 | use.first.second, string_VkShaderStageFlagBits(pStage->stage)); |
Tony-LunarG | f563b36 | 2021-03-18 16:13:18 -0600 | [diff] [blame] | 2001 | } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) && |
| 2002 | (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 2003 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2004 | "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first, |
| 2005 | use.first.second, string_descriptorTypes(descriptor_types).c_str(), |
| 2006 | string_VkDescriptorType(binding->descriptorType)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2007 | } else if (binding->descriptorCount < required_descriptor_count) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 2008 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2009 | "Shader expects at least %u descriptors for binding %u.%u but only %u provided", |
| 2010 | required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | // Validate use of input attachments against subpass structure |
| 2015 | if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2016 | auto input_attachment_uses = module->CollectInterfaceByInputAttachmentIndex(accessible_ids); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2017 | |
Petr Kraus | e91f7a1 | 2017-12-14 20:57:36 +0100 | [diff] [blame] | 2018 | auto rpci = pipeline->rp_state->createInfo.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2019 | auto subpass = pipeline->graphicsPipelineCI.subpass; |
| 2020 | |
| 2021 | for (auto use : input_attachment_uses) { |
| 2022 | auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments; |
| 2023 | auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount) |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2024 | ? input_attachments[use.first].attachment |
| 2025 | : VK_ATTACHMENT_UNUSED; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2026 | |
| 2027 | if (index == VK_ATTACHMENT_UNUSED) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2028 | skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment, |
| 2029 | "Shader consumes input attachment index %d but not provided in subpass", use.first); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2030 | } else if (!(GetFormatType(rpci->pAttachments[index].format) & module->GetFundamentalType(use.second.type_id))) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2031 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2032 | LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch, |
| 2033 | "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first, |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2034 | string_VkFormat(rpci->pAttachments[index].format), module->DescribeType(use.second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | } |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 2038 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) { |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 2039 | skip |= ValidateComputeWorkGroupSizes(module, entrypoint); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 2040 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2041 | return skip; |
| 2042 | } |
| 2043 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2044 | bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint, |
| 2045 | shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer, |
| 2046 | spirv_inst_iter consumer_entrypoint, |
| 2047 | shader_stage_attributes const *consumer_stage) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2048 | bool skip = false; |
| 2049 | |
| 2050 | auto outputs = |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2051 | producer->CollectInterfaceByLocation(producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output); |
| 2052 | auto inputs = consumer->CollectInterfaceByLocation(consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2053 | |
| 2054 | auto a_it = outputs.begin(); |
| 2055 | auto b_it = inputs.begin(); |
| 2056 | |
| 2057 | // Maps sorted by key (location); walk them together to find mismatches |
| 2058 | while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) { |
| 2059 | bool a_at_end = outputs.size() == 0 || a_it == outputs.end(); |
| 2060 | bool b_at_end = inputs.size() == 0 || b_it == inputs.end(); |
| 2061 | auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first; |
| 2062 | auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first; |
| 2063 | |
| 2064 | if (b_at_end || ((!a_at_end) && (a_first < b_first))) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2065 | skip |= LogPerformanceWarning(producer->vk_shader_module(), kVUID_Core_Shader_OutputNotConsumed, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2066 | "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, |
| 2067 | a_first.first, a_first.second, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2068 | a_it++; |
| 2069 | } else if (a_at_end || a_first > b_first) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2070 | skip |= LogError(consumer->vk_shader_module(), kVUID_Core_Shader_InputNotProduced, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2071 | "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first, |
| 2072 | b_first.second, producer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2073 | b_it++; |
| 2074 | } else { |
| 2075 | // subtleties of arrayed interfaces: |
| 2076 | // - if is_patch, then the member is not arrayed, even though the interface may be. |
| 2077 | // - if is_block_member, then the extra array level of an arrayed interface is not |
| 2078 | // expressed in the member type -- it's expressed in the block type. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2079 | if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id, |
| 2080 | producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member, |
| 2081 | consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2082 | skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2083 | "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second, |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2084 | producer->DescribeType(a_it->second.type_id).c_str(), |
| 2085 | consumer->DescribeType(b_it->second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2086 | } |
| 2087 | if (a_it->second.is_patch != b_it->second.is_patch) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2088 | skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2089 | "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage", |
| 2090 | a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name, |
| 2091 | b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2092 | } |
| 2093 | if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2094 | skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2095 | "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first, |
| 2096 | a_first.second, producer_stage->name, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2097 | } |
| 2098 | a_it++; |
| 2099 | b_it++; |
| 2100 | } |
| 2101 | } |
| 2102 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 2103 | if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2104 | auto builtins_producer = producer->CollectBuiltinBlockMembers(producer_entrypoint, spv::StorageClassOutput); |
| 2105 | auto builtins_consumer = consumer->CollectBuiltinBlockMembers(consumer_entrypoint, spv::StorageClassInput); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 2106 | |
| 2107 | if (!builtins_producer.empty() && !builtins_consumer.empty()) { |
| 2108 | if (builtins_producer.size() != builtins_consumer.size()) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2109 | skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2110 | "Number of elements inside builtin block differ between stages (%s %d vs %s %d).", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2111 | producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name, |
| 2112 | static_cast<int>(builtins_consumer.size())); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 2113 | } else { |
| 2114 | auto it_producer = builtins_producer.begin(); |
| 2115 | auto it_consumer = builtins_consumer.begin(); |
| 2116 | while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) { |
| 2117 | if (*it_producer != *it_consumer) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2118 | skip |= LogError(producer->vk_shader_module(), kVUID_Core_Shader_InterfaceTypeMismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2119 | "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name, |
| 2120 | consumer_stage->name); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 2121 | break; |
| 2122 | } |
| 2123 | it_producer++; |
| 2124 | it_consumer++; |
| 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2130 | return skip; |
| 2131 | } |
| 2132 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 2133 | static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2134 | uint32_t stage_mask = 0; |
| 2135 | if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 2136 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
| 2137 | stage_mask |= pCreateInfo->pStages[i].stage; |
| 2138 | } |
| 2139 | // Determine which shader in which PointSize should be written (the final geometry stage) |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 2140 | if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) { |
| 2141 | stage_mask = VK_SHADER_STAGE_MESH_BIT_NV; |
| 2142 | } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2143 | stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT; |
| 2144 | } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) { |
| 2145 | stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 2146 | } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) { |
| 2147 | stage_mask = VK_SHADER_STAGE_VERTEX_BIT; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 2148 | } |
| 2149 | } |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2150 | return stage_mask; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 2151 | } |
| 2152 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2153 | // Validate that the shaders used by the given pipeline and store the active_slots |
| 2154 | // that are actually used by the pipeline into pPipeline->active_slots |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2155 | bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2156 | auto create_info = pipeline->graphicsPipelineCI.ptr(); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2157 | int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 2158 | int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2159 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 2160 | const SHADER_MODULE_STATE *shaders[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2161 | memset(shaders, 0, sizeof(shaders)); |
Jeff Bolz | 7e35c39 | 2018-09-04 15:30:41 -0500 | [diff] [blame] | 2162 | spirv_inst_iter entrypoints[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2163 | bool skip = false; |
| 2164 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2165 | uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2166 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2167 | for (uint32_t i = 0; i < create_info->stageCount; i++) { |
| 2168 | auto stage = &create_info->pStages[i]; |
| 2169 | auto stage_id = GetShaderStageId(stage->stage); |
| 2170 | shaders[stage_id] = GetShaderModuleState(stage->module); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2171 | entrypoints[stage_id] = shaders[stage_id]->FindEntrypoint(stage->pName, stage->stage); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2172 | skip |= ValidatePipelineShaderStage(stage, pipeline, pipeline->stage_state[i], shaders[stage_id], entrypoints[stage_id], |
| 2173 | (pointlist_stage_mask == stage->stage)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | // if the shader stages are no good individually, cross-stage validation is pointless. |
| 2177 | if (skip) return true; |
| 2178 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2179 | auto vi = create_info->pVertexInputState; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2180 | |
| 2181 | if (vi) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2182 | skip |= ValidateViConsistency(vi); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
Piers Daniell | 924cd83 | 2021-05-18 13:48:47 -0600 | [diff] [blame] | 2185 | if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv && |
| 2186 | !IsDynamic(pipeline, VK_DYNAMIC_STATE_VERTEX_INPUT_EXT)) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2187 | skip |= ValidateViAgainstVsInputs(vi, shaders[vertex_stage], entrypoints[vertex_stage]); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2190 | int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 2191 | int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2192 | |
| 2193 | while (!shaders[producer] && producer != fragment_stage) { |
| 2194 | producer++; |
| 2195 | consumer++; |
| 2196 | } |
| 2197 | |
| 2198 | for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) { |
| 2199 | assert(shaders[producer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 2200 | if (shaders[consumer]) { |
| 2201 | if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2202 | skip |= ValidateInterfaceBetweenStages(shaders[producer], entrypoints[producer], &shader_stage_attribs[producer], |
| 2203 | shaders[consumer], entrypoints[consumer], &shader_stage_attribs[consumer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 2204 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2205 | |
| 2206 | producer = consumer; |
| 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2211 | skip |= ValidateFsOutputsAgainstRenderPass(shaders[fragment_stage], entrypoints[fragment_stage], pipeline, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2212 | create_info->subpass); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | return skip; |
| 2216 | } |
| 2217 | |
Tony-LunarG | b2ded51 | 2021-02-02 16:03:30 -0700 | [diff] [blame] | 2218 | void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) { |
| 2219 | auto create_info = pipeline_state->graphicsPipelineCI.ptr(); |
| 2220 | |
| 2221 | if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports || |
| 2222 | !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) { |
| 2223 | return; |
| 2224 | } |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2225 | |
Nathaniel Cesario | 1c3d365 | 2021-01-25 18:35:12 -0700 | [diff] [blame] | 2226 | std::array<const SHADER_MODULE_STATE *, 32> shaders; |
| 2227 | std::fill(shaders.begin(), shaders.end(), nullptr); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2228 | spirv_inst_iter entrypoints[32]; |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2229 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2230 | for (uint32_t i = 0; i < create_info->stageCount; i++) { |
| 2231 | auto stage = &create_info->pStages[i]; |
| 2232 | auto stage_id = GetShaderStageId(stage->stage); |
| 2233 | shaders[stage_id] = GetShaderModuleState(stage->module); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2234 | entrypoints[stage_id] = shaders[stage_id]->FindEntrypoint(stage->pName, stage->stage); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2235 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2236 | if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT || |
| 2237 | stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) { |
Tony-LunarG | b2ded51 | 2021-02-02 16:03:30 -0700 | [diff] [blame] | 2238 | bool primitiverate_written = false; |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2239 | |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 2240 | for (auto set : shaders[stage_id]->builtin_decoration_list) { |
| 2241 | auto insn = shaders[stage_id]->at(set.offset); |
| 2242 | if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) { |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2243 | primitiverate_written = shaders[stage_id]->IsBuiltInWritten(insn, entrypoints[stage_id]); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2244 | } |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 2245 | if (primitiverate_written) { |
| 2246 | break; |
| 2247 | } |
Tony-LunarG | b2ded51 | 2021-02-02 16:03:30 -0700 | [diff] [blame] | 2248 | } |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 2249 | |
Tony-LunarG | b2ded51 | 2021-02-02 16:03:30 -0700 | [diff] [blame] | 2250 | if (primitiverate_written) { |
| 2251 | pipeline_state->wrote_primitive_shading_rate.insert(stage->stage); |
| 2252 | } |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB, |
| 2258 | const char *caller, const DrawDispatchVuid &vuid) const { |
| 2259 | auto create_info = pipeline->graphicsPipelineCI.ptr(); |
| 2260 | bool skip = false; |
| 2261 | |
| 2262 | for (uint32_t i = 0; i < create_info->stageCount; i++) { |
| 2263 | auto stage = &create_info->pStages[i]; |
| 2264 | if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT || |
| 2265 | stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) { |
| 2266 | if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports && |
| 2267 | IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) { |
| 2268 | if (pipeline->wrote_primitive_shading_rate.find(stage->stage) != pipeline->wrote_primitive_shading_rate.end()) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2269 | skip |= |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2270 | LogError(pipeline->pipeline(), vuid.viewport_count_primitive_shading_rate, |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2271 | "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in" |
| 2272 | "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT," |
| 2273 | "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2274 | caller, string_VkShaderStageFlagBits(stage->stage)); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | return skip; |
| 2281 | } |
| 2282 | |
sfricke-samsung | e72a85e | 2020-02-29 21:48:37 -0800 | [diff] [blame] | 2283 | bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 2284 | const auto &stage = *pipeline->computePipelineCI.stage.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2285 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 2286 | const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2287 | const spirv_inst_iter entrypoint = module->FindEntrypoint(stage.pName, stage.stage); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2288 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 2289 | return ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[0], module, entrypoint, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2290 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2291 | |
Ricardo Garcia | 6f3477e | 2020-10-21 10:58:53 +0200 | [diff] [blame] | 2292 | uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const { |
| 2293 | uint32_t total = 0; |
| 2294 | |
| 2295 | const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages; |
| 2296 | for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) { |
| 2297 | if (stages[stage_index].stage == stageBit) { |
| 2298 | total++; |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | if (pipeline->raytracingPipelineCI.pLibraryInfo) { |
| 2303 | for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) { |
| 2304 | const PIPELINE_STATE *library_pipeline = GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]); |
| 2305 | total += CalcShaderStageCount(library_pipeline, stageBit); |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | return total; |
| 2310 | } |
| 2311 | |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2312 | bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const { |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 2313 | bool skip = false; |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2314 | |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2315 | if (isKHR) { |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2316 | if (pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth > |
| 2317 | phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) { |
| 2318 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589", |
| 2319 | "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to " |
| 2320 | "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d", |
| 2321 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth, |
| 2322 | phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2323 | } |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2324 | if (pipeline->raytracingPipelineCI.pLibraryInfo) { |
| 2325 | for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2326 | const PIPELINE_STATE *library_pipelinestate = |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2327 | GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2328 | if (library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2329 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth) { |
| 2330 | skip |= LogError( |
| 2331 | device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591", |
| 2332 | "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been" |
| 2333 | "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2334 | i, library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2335 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth); |
| 2336 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2337 | if (library_pipelinestate->raytracingPipelineCI.pLibraryInfo && |
| 2338 | (library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2339 | pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize || |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2340 | library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2341 | pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize)) { |
| 2342 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593", |
| 2343 | "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries " |
| 2344 | "member must have been created with values of the maxPipelineRayPayloadSize and " |
| 2345 | "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline"); |
| 2346 | } |
| 2347 | if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) && |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2348 | !(library_pipelinestate->raytracingPipelineCI.flags & |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2349 | VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) { |
| 2350 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594", |
| 2351 | "vkCreateRayTracingPipelinesKHR: If flags includes " |
| 2352 | "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of " |
| 2353 | "the pLibraries member of libraries must have been created with the " |
| 2354 | "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set"); |
| 2355 | } |
sourav parmar | 83c31b1 | 2020-05-06 12:30:54 -0700 | [diff] [blame] | 2356 | } |
| 2357 | } |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2358 | } else { |
| 2359 | if (pipeline->raytracingPipelineCI.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) { |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2360 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457", |
| 2361 | "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to " |
| 2362 | "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)", |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2363 | pipeline->raytracingPipelineCI.maxRecursionDepth, |
| 2364 | phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth); |
| 2365 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2366 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2367 | const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages; |
| 2368 | const auto *groups = pipeline->raytracingPipelineCI.ptr()->pGroups; |
| 2369 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 2370 | for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) { |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2371 | const auto &stage = stages[stage_index]; |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 2372 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 2373 | const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module); |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2374 | const spirv_inst_iter entrypoint = module->FindEntrypoint(stage.pName, stage.stage); |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 2375 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 2376 | skip |= ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[stage_index], module, entrypoint, false); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2377 | } |
Ricardo Garcia | 6f3477e | 2020-10-21 10:58:53 +0200 | [diff] [blame] | 2378 | |
| 2379 | if ((pipeline->raytracingPipelineCI.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) { |
| 2380 | const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR); |
| 2381 | if (raygen_stages_count == 0) { |
| 2382 | skip |= LogError( |
| 2383 | device, |
| 2384 | isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-03425", |
| 2385 | " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR."); |
| 2386 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | for (uint32_t group_index = 0; group_index < pipeline->raytracingPipelineCI.groupCount; group_index++) { |
| 2390 | const auto &group = groups[group_index]; |
| 2391 | |
| 2392 | if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) { |
| 2393 | if (group.generalShader >= pipeline->raytracingPipelineCI.stageCount || |
| 2394 | (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV && |
| 2395 | stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV && |
| 2396 | stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2397 | skip |= LogError(device, |
| 2398 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474" |
| 2399 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413", |
| 2400 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2401 | } |
| 2402 | if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV || |
| 2403 | group.intersectionShader != VK_SHADER_UNUSED_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2404 | skip |= LogError(device, |
| 2405 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475" |
| 2406 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414", |
| 2407 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2408 | } |
| 2409 | } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) { |
| 2410 | if (group.intersectionShader >= pipeline->raytracingPipelineCI.stageCount || |
| 2411 | stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2412 | skip |= LogError(device, |
| 2413 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476" |
| 2414 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415", |
| 2415 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2416 | } |
| 2417 | } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) { |
| 2418 | if (group.intersectionShader != VK_SHADER_UNUSED_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2419 | skip |= LogError(device, |
| 2420 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477" |
| 2421 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416", |
| 2422 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV || |
| 2427 | group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) { |
| 2428 | if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= pipeline->raytracingPipelineCI.stageCount || |
| 2429 | stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2430 | skip |= LogError(device, |
| 2431 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479" |
| 2432 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418", |
| 2433 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2434 | } |
| 2435 | if (group.closestHitShader != VK_SHADER_UNUSED_NV && |
| 2436 | (group.closestHitShader >= pipeline->raytracingPipelineCI.stageCount || |
| 2437 | stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2438 | skip |= LogError(device, |
| 2439 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478" |
| 2440 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417", |
| 2441 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 2442 | } |
| 2443 | } |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 2444 | } |
| 2445 | return skip; |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 2446 | } |
| 2447 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2448 | uint32_t ValidationCache::MakeShaderHash(VkShaderModuleCreateInfo const *smci) { return XXH32(smci->pCode, smci->codeSize, 0); } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2449 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2450 | static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) { |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 2451 | const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext); |
John Zulauf | 25ea243 | 2019-04-05 10:07:38 -0600 | [diff] [blame] | 2452 | if (validation_cache_ci) { |
John Zulauf | 146ee80 | 2019-04-05 15:31:06 -0600 | [diff] [blame] | 2453 | return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2454 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2455 | return nullptr; |
| 2456 | } |
| 2457 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 2458 | bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 2459 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2460 | bool skip = false; |
| 2461 | spv_result_t spv_valid = SPV_SUCCESS; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2462 | |
Mark Lobodzinski | 90eea5b | 2020-05-15 12:54:00 -0600 | [diff] [blame] | 2463 | if (disabled[shader_validation]) { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2464 | return false; |
| 2465 | } |
| 2466 | |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 2467 | auto have_glsl_shader = device_extensions.vk_nv_glsl_shader; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2468 | |
| 2469 | if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2470 | skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376", |
| 2471 | "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".", |
| 2472 | pCreateInfo->codeSize); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2473 | } else { |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2474 | auto cache = GetValidationCacheInfo(pCreateInfo); |
| 2475 | uint32_t hash = 0; |
Tony-LunarG | 55fdf1e | 2021-01-13 14:32:56 -0700 | [diff] [blame] | 2476 | // If app isn't using a shader validation cache, use the default one from CoreChecks |
| 2477 | if (!cache) cache = CastFromHandle<ValidationCache *>(core_validation_cache); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2478 | if (cache) { |
| 2479 | hash = ValidationCache::MakeShaderHash(pCreateInfo); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2480 | if (cache->Contains(hash)) return false; |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2481 | } |
| 2482 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 2483 | // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present, |
| 2484 | // the default values will be used during validation. |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 2485 | spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled)); |
Dave Houlton | 0ea2d01 | 2018-06-21 14:00:26 -0600 | [diff] [blame] | 2486 | spv_context ctx = spvContextCreate(spirv_environment); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2487 | spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)}; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2488 | spv_diagnostic diag = nullptr; |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 2489 | spvtools::ValidatorOptions options; |
| 2490 | AdjustValidatorOptions(device_extensions, enabled_features, options); |
Karl Schultz | fda1b38 | 2018-08-08 18:56:11 -0600 | [diff] [blame] | 2491 | spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2492 | if (spv_valid != SPV_SUCCESS) { |
| 2493 | if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2494 | if (spv_valid == SPV_WARNING) { |
| 2495 | skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s", |
| 2496 | diag && diag->error ? diag->error : "(no error text)"); |
| 2497 | } else { |
| 2498 | skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s", |
| 2499 | diag && diag->error ? diag->error : "(no error text)"); |
| 2500 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2501 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2502 | } else { |
| 2503 | if (cache) { |
| 2504 | cache->Insert(hash); |
| 2505 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | spvDiagnosticDestroy(diag); |
| 2509 | spvContextDestroy(ctx); |
| 2510 | } |
| 2511 | |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2512 | return skip; |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 2513 | } |
| 2514 | |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 2515 | bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint) const { |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 2516 | bool skip = false; |
| 2517 | uint32_t local_size_x = 0; |
| 2518 | uint32_t local_size_y = 0; |
| 2519 | uint32_t local_size_z = 0; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 2520 | if (shader->FindLocalSize(entrypoint, local_size_x, local_size_y, local_size_z)) { |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 2521 | if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2522 | skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2523 | "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2524 | report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2525 | phys_dev_props.limits.maxComputeWorkGroupSize[0]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 2526 | } |
| 2527 | if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2528 | skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2529 | "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2530 | report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2531 | phys_dev_props.limits.maxComputeWorkGroupSize[1]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 2532 | } |
| 2533 | if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2534 | skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2535 | "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2536 | report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2537 | phys_dev_props.limits.maxComputeWorkGroupSize[2]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations; |
| 2541 | uint64_t invocations = local_size_x * local_size_y; |
| 2542 | // Prevent overflow. |
| 2543 | bool fail = false; |
| 2544 | if (invocations > UINT32_MAX || invocations > limit) { |
| 2545 | fail = true; |
| 2546 | } |
| 2547 | if (!fail) { |
| 2548 | invocations *= local_size_z; |
| 2549 | if (invocations > UINT32_MAX || invocations > limit) { |
| 2550 | fail = true; |
| 2551 | } |
| 2552 | } |
| 2553 | if (fail) { |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2554 | skip |= LogError(shader->vk_shader_module(), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2555 | "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32 |
| 2556 | ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").", |
Jeremy Gebben | 14b0d1a | 2021-05-15 20:15:41 -0600 | [diff] [blame] | 2557 | report_data->FormatHandle(shader->vk_shader_module()).c_str(), local_size_x, local_size_y, local_size_z, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2558 | limit); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | return skip; |
| 2562 | } |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 2563 | |
| 2564 | spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) { |
| 2565 | if (api_version >= VK_API_VERSION_1_2) { |
| 2566 | return SPV_ENV_VULKAN_1_2; |
| 2567 | } else if (api_version >= VK_API_VERSION_1_1) { |
| 2568 | if (spirv_1_4) { |
| 2569 | return SPV_ENV_VULKAN_1_1_SPIRV_1_4; |
| 2570 | } else { |
| 2571 | return SPV_ENV_VULKAN_1_1; |
| 2572 | } |
| 2573 | } |
| 2574 | return SPV_ENV_VULKAN_1_0; |
| 2575 | } |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 2576 | |
Jeremy Gebben | 5d97074 | 2021-05-31 16:04:14 -0600 | [diff] [blame] | 2577 | void AdjustValidatorOptions(const DeviceExtensions &device_extensions, const DeviceFeatures &enabled_features, |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 2578 | spvtools::ValidatorOptions &options) { |
| 2579 | if (device_extensions.vk_khr_relaxed_block_layout) { |
| 2580 | options.SetRelaxBlockLayout(true); |
| 2581 | } |
| 2582 | if (device_extensions.vk_khr_uniform_buffer_standard_layout && enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) { |
| 2583 | options.SetUniformBufferStandardLayout(true); |
| 2584 | } |
| 2585 | if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) { |
| 2586 | options.SetScalarBlockLayout(true); |
| 2587 | } |
Caio Marcelo de Oliveira Filho | d1bfbcd | 2021-01-27 01:44:04 -0800 | [diff] [blame] | 2588 | if (device_extensions.vk_khr_workgroup_memory_explicit_layout && |
| 2589 | enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) { |
| 2590 | options.SetWorkgroupScalarBlockLayout(true); |
| 2591 | } |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 2592 | } |