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> |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 27 | #include <chrono> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 28 | #include <cinttypes> |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 29 | #include <cmath> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 30 | #include <map> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 31 | #include <sstream> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 32 | #include <string> |
| 33 | #include <unordered_map> |
| 34 | #include <vector> |
| 35 | |
Mark Lobodzinski | 102687e | 2020-04-28 11:03:28 -0600 | [diff] [blame] | 36 | #include <spirv/unified1/spirv.hpp> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 37 | #include "vk_loader_platform.h" |
| 38 | #include "vk_enum_string_helper.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 39 | #include "vk_layer_data.h" |
| 40 | #include "vk_layer_extension_utils.h" |
| 41 | #include "vk_layer_utils.h" |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 42 | #include "chassis.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 43 | #include "core_validation.h" |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 44 | |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 45 | #include "spirv-tools/libspirv.h" |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 46 | #include "xxhash.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 47 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 48 | void decoration_set::add(uint32_t decoration, uint32_t value) { |
| 49 | switch (decoration) { |
| 50 | case spv::DecorationLocation: |
| 51 | flags |= location_bit; |
| 52 | location = value; |
| 53 | break; |
| 54 | case spv::DecorationPatch: |
| 55 | flags |= patch_bit; |
| 56 | break; |
| 57 | case spv::DecorationRelaxedPrecision: |
| 58 | flags |= relaxed_precision_bit; |
| 59 | break; |
| 60 | case spv::DecorationBlock: |
| 61 | flags |= block_bit; |
| 62 | break; |
| 63 | case spv::DecorationBufferBlock: |
| 64 | flags |= buffer_block_bit; |
| 65 | break; |
| 66 | case spv::DecorationComponent: |
| 67 | flags |= component_bit; |
| 68 | component = value; |
| 69 | break; |
| 70 | case spv::DecorationInputAttachmentIndex: |
| 71 | flags |= input_attachment_index_bit; |
| 72 | input_attachment_index = value; |
| 73 | break; |
| 74 | case spv::DecorationDescriptorSet: |
| 75 | flags |= descriptor_set_bit; |
| 76 | descriptor_set = value; |
| 77 | break; |
| 78 | case spv::DecorationBinding: |
| 79 | flags |= binding_bit; |
| 80 | binding = value; |
| 81 | break; |
| 82 | case spv::DecorationNonWritable: |
| 83 | flags |= nonwritable_bit; |
| 84 | break; |
| 85 | case spv::DecorationBuiltIn: |
| 86 | flags |= builtin_bit; |
| 87 | builtin = value; |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 92 | enum FORMAT_TYPE { |
| 93 | FORMAT_TYPE_FLOAT = 1, // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader |
| 94 | FORMAT_TYPE_SINT = 2, |
| 95 | FORMAT_TYPE_UINT = 4, |
| 96 | }; |
| 97 | |
| 98 | typedef std::pair<unsigned, unsigned> location_t; |
| 99 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 100 | static shader_stage_attributes shader_stage_attribs[] = { |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 101 | {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT}, |
| 102 | {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT}, |
| 103 | {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT}, |
| 104 | {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT}, |
| 105 | {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 108 | unsigned ExecutionModelToShaderStageFlagBits(unsigned mode); |
| 109 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 110 | // SPIRV utility functions |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 111 | void SHADER_MODULE_STATE::BuildDefIndex() { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 112 | function_set func_set = {}; |
| 113 | EntryPoint *entry_point = nullptr; |
| 114 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 115 | for (auto insn : *this) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 116 | // offset is not 0, it means it's updated and the offset is in a Function. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 117 | if (func_set.offset) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 118 | func_set.op_lists.insert({insn.opcode(), insn.offset()}); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 119 | } else if (entry_point) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 120 | entry_point->decorate_list.insert({insn.opcode(), insn.offset()}); |
| 121 | } |
| 122 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 123 | switch (insn.opcode()) { |
| 124 | // Types |
| 125 | case spv::OpTypeVoid: |
| 126 | case spv::OpTypeBool: |
| 127 | case spv::OpTypeInt: |
| 128 | case spv::OpTypeFloat: |
| 129 | case spv::OpTypeVector: |
| 130 | case spv::OpTypeMatrix: |
| 131 | case spv::OpTypeImage: |
| 132 | case spv::OpTypeSampler: |
| 133 | case spv::OpTypeSampledImage: |
| 134 | case spv::OpTypeArray: |
| 135 | case spv::OpTypeRuntimeArray: |
| 136 | case spv::OpTypeStruct: |
| 137 | case spv::OpTypeOpaque: |
| 138 | case spv::OpTypePointer: |
| 139 | case spv::OpTypeFunction: |
| 140 | case spv::OpTypeEvent: |
| 141 | case spv::OpTypeDeviceEvent: |
| 142 | case spv::OpTypeReserveId: |
| 143 | case spv::OpTypeQueue: |
| 144 | case spv::OpTypePipe: |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 145 | case spv::OpTypeAccelerationStructureNV: |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 146 | case spv::OpTypeCooperativeMatrixNV: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 147 | def_index[insn.word(1)] = insn.offset(); |
| 148 | break; |
| 149 | |
| 150 | // Fixed constants |
| 151 | case spv::OpConstantTrue: |
| 152 | case spv::OpConstantFalse: |
| 153 | case spv::OpConstant: |
| 154 | case spv::OpConstantComposite: |
| 155 | case spv::OpConstantSampler: |
| 156 | case spv::OpConstantNull: |
| 157 | def_index[insn.word(2)] = insn.offset(); |
| 158 | break; |
| 159 | |
| 160 | // Specialization constants |
| 161 | case spv::OpSpecConstantTrue: |
| 162 | case spv::OpSpecConstantFalse: |
| 163 | case spv::OpSpecConstant: |
| 164 | case spv::OpSpecConstantComposite: |
| 165 | case spv::OpSpecConstantOp: |
| 166 | def_index[insn.word(2)] = insn.offset(); |
| 167 | break; |
| 168 | |
| 169 | // Variables |
| 170 | case spv::OpVariable: |
| 171 | def_index[insn.word(2)] = insn.offset(); |
| 172 | break; |
| 173 | |
| 174 | // Functions |
| 175 | case spv::OpFunction: |
| 176 | def_index[insn.word(2)] = insn.offset(); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 177 | func_set.id = insn.word(2); |
| 178 | func_set.offset = insn.offset(); |
| 179 | func_set.op_lists.clear(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 180 | break; |
| 181 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 182 | // Decorations |
| 183 | case spv::OpDecorate: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 184 | auto target_id = insn.word(1); |
| 185 | decorations[target_id].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u); |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 186 | } break; |
| 187 | case spv::OpGroupDecorate: { |
| 188 | auto const &src = decorations[insn.word(1)]; |
| 189 | for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src); |
| 190 | } break; |
| 191 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 192 | // Entry points ... add to the entrypoint table |
| 193 | case spv::OpEntryPoint: { |
| 194 | // Entry points do not have an id (the id is the function id) and thus need their own table |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 195 | auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3)); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 196 | auto execution_model = insn.word(1); |
| 197 | auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 198 | entry_points.emplace(entrypoint_name, |
| 199 | EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)}); |
| 200 | |
| 201 | auto range = entry_points.equal_range(entrypoint_name); |
| 202 | for (auto it = range.first; it != range.second; ++it) { |
| 203 | if (it->second.offset == insn.offset()) { |
| 204 | entry_point = &(it->second); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | assert(entry_point != nullptr); |
| 209 | break; |
| 210 | } |
| 211 | case spv::OpFunctionEnd: { |
| 212 | assert(entry_point != nullptr); |
| 213 | func_set.length = insn.offset() - func_set.offset; |
| 214 | entry_point->function_set_list.emplace_back(func_set); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 215 | break; |
| 216 | } |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 217 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 218 | default: |
| 219 | // We don't care about any other defs for now. |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 225 | unsigned ExecutionModelToShaderStageFlagBits(unsigned mode) { |
| 226 | switch (mode) { |
| 227 | case spv::ExecutionModelVertex: |
| 228 | return VK_SHADER_STAGE_VERTEX_BIT; |
| 229 | case spv::ExecutionModelTessellationControl: |
| 230 | return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 231 | case spv::ExecutionModelTessellationEvaluation: |
| 232 | return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 233 | case spv::ExecutionModelGeometry: |
| 234 | return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 235 | case spv::ExecutionModelFragment: |
| 236 | return VK_SHADER_STAGE_FRAGMENT_BIT; |
| 237 | case spv::ExecutionModelGLCompute: |
| 238 | return VK_SHADER_STAGE_COMPUTE_BIT; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 239 | case spv::ExecutionModelRayGenerationNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 240 | return VK_SHADER_STAGE_RAYGEN_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 241 | case spv::ExecutionModelAnyHitNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 242 | return VK_SHADER_STAGE_ANY_HIT_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 243 | case spv::ExecutionModelClosestHitNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 244 | return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 245 | case spv::ExecutionModelMissNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 246 | return VK_SHADER_STAGE_MISS_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 247 | case spv::ExecutionModelIntersectionNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 248 | return VK_SHADER_STAGE_INTERSECTION_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 249 | case spv::ExecutionModelCallableNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 250 | return VK_SHADER_STAGE_CALLABLE_BIT_NV; |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 251 | case spv::ExecutionModelTaskNV: |
| 252 | return VK_SHADER_STAGE_TASK_BIT_NV; |
| 253 | case spv::ExecutionModelMeshNV: |
| 254 | return VK_SHADER_STAGE_MESH_BIT_NV; |
| 255 | default: |
| 256 | return 0; |
| 257 | } |
| 258 | } |
| 259 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 260 | const SHADER_MODULE_STATE::EntryPoint *FindEntrypointStruct(SHADER_MODULE_STATE const *src, char const *name, |
| 261 | VkShaderStageFlagBits stageBits) { |
| 262 | auto range = src->entry_points.equal_range(name); |
| 263 | for (auto it = range.first; it != range.second; ++it) { |
| 264 | if (it->second.stage == stageBits) { |
| 265 | return &(it->second); |
| 266 | } |
| 267 | } |
| 268 | return nullptr; |
| 269 | } |
| 270 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 271 | spirv_inst_iter FindEntrypoint(SHADER_MODULE_STATE const *src, char const *name, VkShaderStageFlagBits stageBits) { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 272 | auto range = src->entry_points.equal_range(name); |
| 273 | for (auto it = range.first; it != range.second; ++it) { |
| 274 | if (it->second.stage == stageBits) { |
| 275 | return src->at(it->second.offset); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 278 | return src->end(); |
| 279 | } |
| 280 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 281 | static char const *StorageClassName(unsigned sc) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 282 | switch (sc) { |
| 283 | case spv::StorageClassInput: |
| 284 | return "input"; |
| 285 | case spv::StorageClassOutput: |
| 286 | return "output"; |
| 287 | case spv::StorageClassUniformConstant: |
| 288 | return "const uniform"; |
| 289 | case spv::StorageClassUniform: |
| 290 | return "uniform"; |
| 291 | case spv::StorageClassWorkgroup: |
| 292 | return "workgroup local"; |
| 293 | case spv::StorageClassCrossWorkgroup: |
| 294 | return "workgroup global"; |
| 295 | case spv::StorageClassPrivate: |
| 296 | return "private global"; |
| 297 | case spv::StorageClassFunction: |
| 298 | return "function"; |
| 299 | case spv::StorageClassGeneric: |
| 300 | return "generic"; |
| 301 | case spv::StorageClassAtomicCounter: |
| 302 | return "atomic counter"; |
| 303 | case spv::StorageClassImage: |
| 304 | return "image"; |
| 305 | case spv::StorageClassPushConstant: |
| 306 | return "push constant"; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 307 | case spv::StorageClassStorageBuffer: |
| 308 | return "storage buffer"; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 309 | default: |
| 310 | return "unknown"; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Get the value of an integral constant |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 315 | unsigned GetConstantValue(SHADER_MODULE_STATE const *src, unsigned id) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 316 | auto value = src->get_def(id); |
| 317 | assert(value != src->end()); |
| 318 | |
| 319 | if (value.opcode() != spv::OpConstant) { |
| 320 | // TODO: Either ensure that the specialization transform is already performed on a module we're |
| 321 | // considering here, OR -- specialize on the fly now. |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | return value.word(3); |
| 326 | } |
| 327 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 328 | static void DescribeTypeInner(std::ostringstream &ss, SHADER_MODULE_STATE const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 329 | auto insn = src->get_def(type); |
| 330 | assert(insn != src->end()); |
| 331 | |
| 332 | switch (insn.opcode()) { |
| 333 | case spv::OpTypeBool: |
| 334 | ss << "bool"; |
| 335 | break; |
| 336 | case spv::OpTypeInt: |
| 337 | ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2); |
| 338 | break; |
| 339 | case spv::OpTypeFloat: |
| 340 | ss << "float" << insn.word(2); |
| 341 | break; |
| 342 | case spv::OpTypeVector: |
| 343 | ss << "vec" << insn.word(3) << " of "; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 344 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 345 | break; |
| 346 | case spv::OpTypeMatrix: |
| 347 | ss << "mat" << insn.word(3) << " of "; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 348 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 349 | break; |
| 350 | case spv::OpTypeArray: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 351 | ss << "arr[" << GetConstantValue(src, insn.word(3)) << "] of "; |
| 352 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 353 | break; |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 354 | case spv::OpTypeRuntimeArray: |
| 355 | ss << "runtime arr[] of "; |
| 356 | DescribeTypeInner(ss, src, insn.word(2)); |
| 357 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 358 | case spv::OpTypePointer: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 359 | ss << "ptr to " << StorageClassName(insn.word(2)) << " "; |
| 360 | DescribeTypeInner(ss, src, insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 361 | break; |
| 362 | case spv::OpTypeStruct: { |
| 363 | ss << "struct of ("; |
| 364 | for (unsigned i = 2; i < insn.len(); i++) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 365 | DescribeTypeInner(ss, src, insn.word(i)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 366 | if (i == insn.len() - 1) { |
| 367 | ss << ")"; |
| 368 | } else { |
| 369 | ss << ", "; |
| 370 | } |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | case spv::OpTypeSampler: |
| 375 | ss << "sampler"; |
| 376 | break; |
| 377 | case spv::OpTypeSampledImage: |
| 378 | ss << "sampler+"; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 379 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 380 | break; |
| 381 | case spv::OpTypeImage: |
| 382 | ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")"; |
| 383 | break; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 384 | case spv::OpTypeAccelerationStructureNV: |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 385 | ss << "accelerationStruture"; |
| 386 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 387 | default: |
| 388 | ss << "oddtype"; |
| 389 | break; |
| 390 | } |
| 391 | } |
| 392 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 393 | static std::string DescribeType(SHADER_MODULE_STATE const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 394 | std::ostringstream ss; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 395 | DescribeTypeInner(ss, src, type); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 396 | return ss.str(); |
| 397 | } |
| 398 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 399 | static bool IsNarrowNumericType(spirv_inst_iter type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 400 | if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false; |
| 401 | return type.word(2) < 64; |
| 402 | } |
| 403 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 404 | 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] | 405 | bool b_arrayed, bool relaxed) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 406 | // Walk two type trees together, and complain about differences |
| 407 | auto a_insn = a->get_def(a_type); |
| 408 | auto b_insn = b->get_def(b_type); |
| 409 | assert(a_insn != a->end()); |
| 410 | assert(b_insn != b->end()); |
| 411 | |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 412 | // Ignore runtime-sized arrays-- they cannot appear in these interfaces. |
| 413 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 414 | if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 415 | 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] | 416 | } |
| 417 | |
| 418 | if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) { |
| 419 | // 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] | 420 | 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] | 421 | } |
| 422 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 423 | if (a_insn.opcode() == spv::OpTypeVector && relaxed && IsNarrowNumericType(b_insn)) { |
| 424 | 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] | 425 | } |
| 426 | |
| 427 | if (a_insn.opcode() != b_insn.opcode()) { |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | if (a_insn.opcode() == spv::OpTypePointer) { |
| 432 | // Match on pointee type. storage class is expected to differ |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 433 | 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] | 434 | } |
| 435 | |
| 436 | if (a_arrayed || b_arrayed) { |
| 437 | // If we havent resolved array-of-verts by here, we're not going to. |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | switch (a_insn.opcode()) { |
| 442 | case spv::OpTypeBool: |
| 443 | return true; |
| 444 | case spv::OpTypeInt: |
| 445 | // Match on width, signedness |
| 446 | return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3); |
| 447 | case spv::OpTypeFloat: |
| 448 | // Match on width |
| 449 | return a_insn.word(2) == b_insn.word(2); |
| 450 | case spv::OpTypeVector: |
| 451 | // Match on element type, count. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 452 | if (!TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false; |
| 453 | if (relaxed && IsNarrowNumericType(a->get_def(a_insn.word(2)))) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 454 | return a_insn.word(3) >= b_insn.word(3); |
| 455 | } else { |
| 456 | return a_insn.word(3) == b_insn.word(3); |
| 457 | } |
| 458 | case spv::OpTypeMatrix: |
| 459 | // Match on element type, count. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 460 | 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] | 461 | a_insn.word(3) == b_insn.word(3); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 462 | case spv::OpTypeArray: |
| 463 | // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from |
| 464 | // 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] | 465 | return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) && |
| 466 | GetConstantValue(a, a_insn.word(3)) == GetConstantValue(b, b_insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 467 | case spv::OpTypeStruct: |
| 468 | // Match on all element types |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 469 | { |
| 470 | if (a_insn.len() != b_insn.len()) { |
| 471 | return false; // Structs cannot match if member counts differ |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 472 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 473 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 474 | for (unsigned i = 2; i < a_insn.len(); i++) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 475 | 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] | 476 | return false; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | return true; |
| 481 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 482 | default: |
| 483 | // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match. |
| 484 | return false; |
| 485 | } |
| 486 | } |
| 487 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 488 | static unsigned GetLocationsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 489 | auto insn = src->get_def(type); |
| 490 | assert(insn != src->end()); |
| 491 | |
| 492 | switch (insn.opcode()) { |
| 493 | case spv::OpTypePointer: |
| 494 | // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing |
| 495 | // pointers around. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 496 | return GetLocationsConsumedByType(src, insn.word(3), strip_array_level); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 497 | case spv::OpTypeArray: |
| 498 | if (strip_array_level) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 499 | return GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 500 | } else { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 501 | return GetConstantValue(src, insn.word(3)) * GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 502 | } |
| 503 | case spv::OpTypeMatrix: |
| 504 | // Num locations is the dimension * element size |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 505 | return insn.word(3) * GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 506 | case spv::OpTypeVector: { |
| 507 | auto scalar_type = src->get_def(insn.word(2)); |
| 508 | auto bit_width = |
| 509 | (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32; |
| 510 | |
| 511 | // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two. |
| 512 | return (bit_width * insn.word(3) + 127) / 128; |
| 513 | } |
| 514 | default: |
| 515 | // Everything else is just 1. |
| 516 | return 1; |
| 517 | |
| 518 | // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations. |
| 519 | } |
| 520 | } |
| 521 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 522 | static unsigned GetComponentsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 523 | auto insn = src->get_def(type); |
| 524 | assert(insn != src->end()); |
| 525 | |
| 526 | switch (insn.opcode()) { |
| 527 | case spv::OpTypePointer: |
| 528 | // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing |
| 529 | // pointers around. |
| 530 | return GetComponentsConsumedByType(src, insn.word(3), strip_array_level); |
| 531 | case spv::OpTypeStruct: { |
| 532 | uint32_t sum = 0; |
| 533 | for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct |
| 534 | sum += GetComponentsConsumedByType(src, insn.word(i), false); |
| 535 | } |
| 536 | return sum; |
| 537 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 538 | case spv::OpTypeArray: |
| 539 | if (strip_array_level) { |
| 540 | return GetComponentsConsumedByType(src, insn.word(2), false); |
| 541 | } else { |
| 542 | return GetConstantValue(src, insn.word(3)) * GetComponentsConsumedByType(src, insn.word(2), false); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 543 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 544 | case spv::OpTypeMatrix: |
| 545 | // Num locations is the dimension * element size |
| 546 | return insn.word(3) * GetComponentsConsumedByType(src, insn.word(2), false); |
| 547 | case spv::OpTypeVector: { |
| 548 | auto scalar_type = src->get_def(insn.word(2)); |
| 549 | auto bit_width = |
| 550 | (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32; |
| 551 | // One component is 32-bit |
| 552 | return (bit_width * insn.word(3) + 31) / 32; |
| 553 | } |
| 554 | case spv::OpTypeFloat: { |
| 555 | auto bit_width = insn.word(2); |
| 556 | return (bit_width + 31) / 32; |
| 557 | } |
| 558 | case spv::OpTypeInt: { |
| 559 | auto bit_width = insn.word(2); |
| 560 | return (bit_width + 31) / 32; |
| 561 | } |
| 562 | case spv::OpConstant: |
| 563 | return GetComponentsConsumedByType(src, insn.word(1), false); |
| 564 | default: |
| 565 | return 0; |
| 566 | } |
| 567 | } |
| 568 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 569 | static unsigned GetLocationsConsumedByFormat(VkFormat format) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 570 | switch (format) { |
| 571 | case VK_FORMAT_R64G64B64A64_SFLOAT: |
| 572 | case VK_FORMAT_R64G64B64A64_SINT: |
| 573 | case VK_FORMAT_R64G64B64A64_UINT: |
| 574 | case VK_FORMAT_R64G64B64_SFLOAT: |
| 575 | case VK_FORMAT_R64G64B64_SINT: |
| 576 | case VK_FORMAT_R64G64B64_UINT: |
| 577 | return 2; |
| 578 | default: |
| 579 | return 1; |
| 580 | } |
| 581 | } |
| 582 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 583 | static unsigned GetFormatType(VkFormat fmt) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 584 | if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT; |
| 585 | if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT; |
| 586 | if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT; |
| 587 | if (fmt == VK_FORMAT_UNDEFINED) return 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 588 | // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader. |
| 589 | return FORMAT_TYPE_FLOAT; |
| 590 | } |
| 591 | |
| 592 | // characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above. |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 593 | // also used for input attachments, as we statically know their format. |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 594 | static unsigned GetFundamentalType(SHADER_MODULE_STATE const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 595 | auto insn = src->get_def(type); |
| 596 | assert(insn != src->end()); |
| 597 | |
| 598 | switch (insn.opcode()) { |
| 599 | case spv::OpTypeInt: |
| 600 | return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT; |
| 601 | case spv::OpTypeFloat: |
| 602 | return FORMAT_TYPE_FLOAT; |
| 603 | case spv::OpTypeVector: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 604 | case spv::OpTypeMatrix: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 605 | case spv::OpTypeArray: |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 606 | case spv::OpTypeRuntimeArray: |
| 607 | case spv::OpTypeImage: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 608 | return GetFundamentalType(src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 609 | case spv::OpTypePointer: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 610 | return GetFundamentalType(src, insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 611 | |
| 612 | default: |
| 613 | return 0; |
| 614 | } |
| 615 | } |
| 616 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 617 | static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 618 | uint32_t bit_pos = uint32_t(u_ffs(stage)); |
| 619 | return bit_pos - 1; |
| 620 | } |
| 621 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 622 | static spirv_inst_iter GetStructType(SHADER_MODULE_STATE const *src, spirv_inst_iter def, bool is_array_of_verts) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 623 | while (true) { |
| 624 | if (def.opcode() == spv::OpTypePointer) { |
| 625 | def = src->get_def(def.word(3)); |
| 626 | } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) { |
| 627 | def = src->get_def(def.word(2)); |
| 628 | is_array_of_verts = false; |
| 629 | } else if (def.opcode() == spv::OpTypeStruct) { |
| 630 | return def; |
| 631 | } else { |
| 632 | return src->end(); |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 637 | static bool CollectInterfaceBlockMembers(SHADER_MODULE_STATE const *src, std::map<location_t, interface_var> *out, |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 638 | bool is_array_of_verts, uint32_t id, uint32_t type_id, bool is_patch, |
| 639 | int /*first_location*/) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 640 | // Walk down the type_id presented, trying to determine whether it's actually an interface block. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 641 | auto type = GetStructType(src, src->get_def(type_id), is_array_of_verts && !is_patch); |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 642 | if (type == src->end() || !(src->get_decorations(type.word(1)).flags & decoration_set::block_bit)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 643 | // This isn't an interface block. |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 644 | return false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | std::unordered_map<unsigned, unsigned> member_components; |
| 648 | std::unordered_map<unsigned, unsigned> member_relaxed_precision; |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 649 | std::unordered_map<unsigned, unsigned> member_patch; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 650 | |
| 651 | // Walk all the OpMemberDecorate for type's result id -- first pass, collect components. |
| 652 | for (auto insn : *src) { |
| 653 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) { |
| 654 | unsigned member_index = insn.word(2); |
| 655 | |
| 656 | if (insn.word(3) == spv::DecorationComponent) { |
| 657 | unsigned component = insn.word(4); |
| 658 | member_components[member_index] = component; |
| 659 | } |
| 660 | |
| 661 | if (insn.word(3) == spv::DecorationRelaxedPrecision) { |
| 662 | member_relaxed_precision[member_index] = 1; |
| 663 | } |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 664 | |
| 665 | if (insn.word(3) == spv::DecorationPatch) { |
| 666 | member_patch[member_index] = 1; |
| 667 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 671 | // TODO: correctly handle location assignment from outside |
| 672 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 673 | // Second pass -- produce the output, from Location decorations |
| 674 | for (auto insn : *src) { |
| 675 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) { |
| 676 | unsigned member_index = insn.word(2); |
| 677 | unsigned member_type_id = type.word(2 + member_index); |
| 678 | |
| 679 | if (insn.word(3) == spv::DecorationLocation) { |
| 680 | unsigned location = insn.word(4); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 681 | unsigned num_locations = GetLocationsConsumedByType(src, member_type_id, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 682 | auto component_it = member_components.find(member_index); |
| 683 | unsigned component = component_it == member_components.end() ? 0 : component_it->second; |
| 684 | bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end(); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 685 | bool member_is_patch = is_patch || member_patch.count(member_index) > 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 686 | |
| 687 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 688 | interface_var v = {}; |
| 689 | v.id = id; |
| 690 | // TODO: member index in interface_var too? |
| 691 | v.type_id = member_type_id; |
| 692 | v.offset = offset; |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 693 | v.is_patch = member_is_patch; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 694 | v.is_block_member = true; |
| 695 | v.is_relaxed_precision = is_relaxed_precision; |
| 696 | (*out)[std::make_pair(location + offset, component)] = v; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 701 | |
| 702 | return true; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 705 | static std::vector<uint32_t> FindEntrypointInterfaces(spirv_inst_iter entrypoint) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 706 | assert(entrypoint.opcode() == spv::OpEntryPoint); |
| 707 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 708 | std::vector<uint32_t> interfaces; |
| 709 | // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the |
| 710 | // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator. |
| 711 | uint32_t word = 3; |
| 712 | while (entrypoint.word(word) & 0xff000000u) { |
| 713 | ++word; |
| 714 | } |
| 715 | ++word; |
| 716 | |
| 717 | for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word)); |
| 718 | |
| 719 | return interfaces; |
| 720 | } |
| 721 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 722 | static std::map<location_t, interface_var> CollectInterfaceByLocation(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 723 | spv::StorageClass sinterface, bool is_array_of_verts) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 724 | // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber. |
| 725 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 726 | std::map<location_t, interface_var> out; |
| 727 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 728 | for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) { |
| 729 | auto insn = src->get_def(iid); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 730 | assert(insn != src->end()); |
| 731 | assert(insn.opcode() == spv::OpVariable); |
| 732 | |
| 733 | if (insn.word(3) == static_cast<uint32_t>(sinterface)) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 734 | auto d = src->get_decorations(iid); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 735 | unsigned id = insn.word(2); |
| 736 | unsigned type = insn.word(1); |
| 737 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 738 | int location = d.location; |
| 739 | int builtin = d.builtin; |
| 740 | unsigned component = d.component; |
| 741 | bool is_patch = (d.flags & decoration_set::patch_bit) != 0; |
| 742 | bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 743 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 744 | if (builtin != -1) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 745 | continue; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 746 | } else if (!CollectInterfaceBlockMembers(src, &out, is_array_of_verts, id, type, is_patch, location)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 747 | // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit |
| 748 | // one result for each. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 749 | unsigned num_locations = GetLocationsConsumedByType(src, type, is_array_of_verts && !is_patch); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 750 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 751 | interface_var v = {}; |
| 752 | v.id = id; |
| 753 | v.type_id = type; |
| 754 | v.offset = offset; |
| 755 | v.is_patch = is_patch; |
| 756 | v.is_relaxed_precision = is_relaxed_precision; |
| 757 | out[std::make_pair(location + offset, component)] = v; |
| 758 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | return out; |
| 764 | } |
| 765 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 766 | static std::vector<uint32_t> CollectBuiltinBlockMembers(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint, |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 767 | uint32_t storageClass) { |
| 768 | std::vector<uint32_t> variables; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 769 | std::vector<uint32_t> builtin_struct_members; |
| 770 | std::vector<uint32_t> builtin_decorations; |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 771 | |
| 772 | for (auto insn : *src) { |
| 773 | switch (insn.opcode()) { |
| 774 | // Find all built-in member decorations |
| 775 | case spv::OpMemberDecorate: |
| 776 | if (insn.word(3) == spv::DecorationBuiltIn) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 777 | builtin_struct_members.push_back(insn.word(1)); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 778 | } |
| 779 | break; |
| 780 | // Find all built-in decorations |
| 781 | case spv::OpDecorate: |
| 782 | switch (insn.word(2)) { |
| 783 | case spv::DecorationBlock: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 784 | uint32_t block_id = insn.word(1); |
| 785 | for (auto built_in_block_id : builtin_struct_members) { |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 786 | // Check if one of the members of the block are built-in -> the block is built-in |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 787 | if (block_id == built_in_block_id) { |
| 788 | builtin_decorations.push_back(block_id); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 789 | break; |
| 790 | } |
| 791 | } |
| 792 | break; |
| 793 | } |
| 794 | case spv::DecorationBuiltIn: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 795 | builtin_decorations.push_back(insn.word(1)); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 796 | break; |
| 797 | default: |
| 798 | break; |
| 799 | } |
| 800 | break; |
| 801 | default: |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | // Find all interface variables belonging to the entrypoint and matching the storage class |
| 807 | for (uint32_t id : FindEntrypointInterfaces(entrypoint)) { |
| 808 | auto def = src->get_def(id); |
| 809 | assert(def != src->end()); |
| 810 | assert(def.opcode() == spv::OpVariable); |
| 811 | |
| 812 | if (def.word(3) == storageClass) variables.push_back(def.word(1)); |
| 813 | } |
| 814 | |
| 815 | // Find all members belonging to the builtin block selected |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 816 | std::vector<uint32_t> builtin_block_members; |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 817 | for (auto &var : variables) { |
| 818 | auto def = src->get_def(src->get_def(var).word(3)); |
| 819 | |
| 820 | // It could be an array of IO blocks. The element type should be the struct defining the block contents |
| 821 | if (def.opcode() == spv::OpTypeArray) def = src->get_def(def.word(2)); |
| 822 | |
| 823 | // Now find all members belonging to the struct defining the IO block |
| 824 | if (def.opcode() == spv::OpTypeStruct) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 825 | for (auto built_in_id : builtin_decorations) { |
| 826 | if (built_in_id == def.word(1)) { |
| 827 | for (int i = 2; i < static_cast<int>(def.len()); i++) { |
| 828 | builtin_block_members.push_back(spv::BuiltInMax); // Start with undefined builtin for each struct member. |
| 829 | } |
| 830 | // These shouldn't be left after replacing. |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 831 | for (auto insn : *src) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 832 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == built_in_id && |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 833 | insn.word(3) == spv::DecorationBuiltIn) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 834 | auto struct_index = insn.word(2); |
| 835 | assert(struct_index < builtin_block_members.size()); |
| 836 | builtin_block_members[struct_index] = insn.word(4); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 844 | return builtin_block_members; |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 847 | static std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex( |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 848 | SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 849 | std::vector<std::pair<uint32_t, interface_var>> out; |
| 850 | |
| 851 | for (auto insn : *src) { |
| 852 | if (insn.opcode() == spv::OpDecorate) { |
| 853 | if (insn.word(2) == spv::DecorationInputAttachmentIndex) { |
| 854 | auto attachment_index = insn.word(3); |
| 855 | auto id = insn.word(1); |
| 856 | |
| 857 | if (accessible_ids.count(id)) { |
| 858 | auto def = src->get_def(id); |
| 859 | assert(def != src->end()); |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 860 | if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 861 | auto num_locations = GetLocationsConsumedByType(src, def.word(1), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 862 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 863 | interface_var v = {}; |
| 864 | v.id = id; |
| 865 | v.type_id = def.word(1); |
| 866 | v.offset = offset; |
| 867 | out.emplace_back(attachment_index + offset, v); |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | return out; |
| 876 | } |
| 877 | |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 878 | static bool AtomicOperation(uint32_t opcode) { |
| 879 | switch (opcode) { |
| 880 | case spv::OpAtomicLoad: |
| 881 | case spv::OpAtomicStore: |
| 882 | case spv::OpAtomicExchange: |
| 883 | case spv::OpAtomicCompareExchange: |
| 884 | case spv::OpAtomicCompareExchangeWeak: |
| 885 | case spv::OpAtomicIIncrement: |
| 886 | case spv::OpAtomicIDecrement: |
| 887 | case spv::OpAtomicIAdd: |
| 888 | case spv::OpAtomicISub: |
| 889 | case spv::OpAtomicSMin: |
| 890 | case spv::OpAtomicUMin: |
| 891 | case spv::OpAtomicSMax: |
| 892 | case spv::OpAtomicUMax: |
| 893 | case spv::OpAtomicAnd: |
| 894 | case spv::OpAtomicOr: |
| 895 | case spv::OpAtomicXor: |
| 896 | case spv::OpAtomicFAddEXT: |
| 897 | return true; |
| 898 | default: |
| 899 | return false; |
| 900 | } |
| 901 | return false; |
| 902 | } |
| 903 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 904 | // Only includes valid group operations used in Vulkan (for now thats only subgroup ops) and any non supported operation will be |
| 905 | // covered with VUID 01090 |
| 906 | static bool GroupOperation(uint32_t opcode) { |
| 907 | switch (opcode) { |
| 908 | case spv::OpGroupNonUniformElect: |
| 909 | case spv::OpGroupNonUniformAll: |
| 910 | case spv::OpGroupNonUniformAny: |
| 911 | case spv::OpGroupNonUniformAllEqual: |
| 912 | case spv::OpGroupNonUniformBroadcast: |
| 913 | case spv::OpGroupNonUniformBroadcastFirst: |
| 914 | case spv::OpGroupNonUniformBallot: |
| 915 | case spv::OpGroupNonUniformInverseBallot: |
| 916 | case spv::OpGroupNonUniformBallotBitExtract: |
| 917 | case spv::OpGroupNonUniformBallotBitCount: |
| 918 | case spv::OpGroupNonUniformBallotFindLSB: |
| 919 | case spv::OpGroupNonUniformBallotFindMSB: |
| 920 | case spv::OpGroupNonUniformShuffle: |
| 921 | case spv::OpGroupNonUniformShuffleXor: |
| 922 | case spv::OpGroupNonUniformShuffleUp: |
| 923 | case spv::OpGroupNonUniformShuffleDown: |
| 924 | case spv::OpGroupNonUniformIAdd: |
| 925 | case spv::OpGroupNonUniformFAdd: |
| 926 | case spv::OpGroupNonUniformIMul: |
| 927 | case spv::OpGroupNonUniformFMul: |
| 928 | case spv::OpGroupNonUniformSMin: |
| 929 | case spv::OpGroupNonUniformUMin: |
| 930 | case spv::OpGroupNonUniformFMin: |
| 931 | case spv::OpGroupNonUniformSMax: |
| 932 | case spv::OpGroupNonUniformUMax: |
| 933 | case spv::OpGroupNonUniformFMax: |
| 934 | case spv::OpGroupNonUniformBitwiseAnd: |
| 935 | case spv::OpGroupNonUniformBitwiseOr: |
| 936 | case spv::OpGroupNonUniformBitwiseXor: |
| 937 | case spv::OpGroupNonUniformLogicalAnd: |
| 938 | case spv::OpGroupNonUniformLogicalOr: |
| 939 | case spv::OpGroupNonUniformLogicalXor: |
| 940 | case spv::OpGroupNonUniformQuadBroadcast: |
| 941 | case spv::OpGroupNonUniformQuadSwap: |
| 942 | case spv::OpGroupNonUniformPartitionNV: |
| 943 | return true; |
| 944 | default: |
| 945 | return false; |
| 946 | } |
| 947 | return false; |
| 948 | } |
| 949 | |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 950 | bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<unsigned> &operator_members, |
| 951 | const std::unordered_map<unsigned, unsigned> &load_members, |
| 952 | const std::unordered_map<unsigned, std::pair<unsigned, unsigned>> &accesschain_members) { |
| 953 | for (auto load_id : operator_members) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 954 | if (object_id == load_id) return true; |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 955 | auto load_it = load_members.find(load_id); |
| 956 | if (load_it == load_members.end()) { |
| 957 | continue; |
| 958 | } |
| 959 | if (load_it->second == object_id) { |
| 960 | return true; |
| 961 | } |
| 962 | |
| 963 | auto accesschain_it = accesschain_members.find(load_it->second); |
| 964 | if (accesschain_it == accesschain_members.end()) { |
| 965 | continue; |
| 966 | } |
| 967 | if (accesschain_it->second.first == object_id) { |
| 968 | return true; |
| 969 | } |
| 970 | } |
| 971 | return false; |
| 972 | } |
| 973 | |
locke-lunarg | ae2a43c | 2020-09-22 17:21:57 -0600 | [diff] [blame] | 974 | bool CheckImageOperandsBiasOffset(uint32_t type) { |
| 975 | return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask | |
| 976 | spv::ImageOperandsConstOffsetsMask) |
| 977 | ? true |
| 978 | : false; |
| 979 | } |
| 980 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 981 | struct shader_module_used_operators { |
| 982 | bool updated; |
| 983 | std::vector<unsigned> imagwrite_members; |
| 984 | std::vector<unsigned> atomic_members; |
| 985 | std::vector<unsigned> store_members; |
| 986 | std::vector<unsigned> atomic_store_members; |
| 987 | std::vector<unsigned> sampler_implicitLod_dref_proj_members; // sampler Load id |
| 988 | std::vector<unsigned> sampler_bias_offset_members; // sampler Load id |
sfricke-samsung | 691299b | 2021-01-01 20:48:48 -0800 | [diff] [blame] | 989 | std::vector<std::pair<unsigned, unsigned>> sampledImage_members; // <image,sampler> Load id |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 990 | std::unordered_map<unsigned, unsigned> load_members; |
| 991 | std::unordered_map<unsigned, std::pair<unsigned, unsigned>> accesschain_members; |
| 992 | std::unordered_map<unsigned, unsigned> image_texel_pointer_members; |
| 993 | |
| 994 | shader_module_used_operators() : updated(false) {} |
| 995 | |
| 996 | void update(SHADER_MODULE_STATE const *module) { |
| 997 | if (updated) return; |
| 998 | updated = true; |
| 999 | |
| 1000 | for (auto insn : *module) { |
| 1001 | switch (insn.opcode()) { |
| 1002 | case spv::OpImageSampleImplicitLod: |
| 1003 | case spv::OpImageSampleProjImplicitLod: |
| 1004 | case spv::OpImageSampleProjExplicitLod: |
| 1005 | case spv::OpImageSparseSampleImplicitLod: |
| 1006 | case spv::OpImageSparseSampleProjImplicitLod: |
| 1007 | case spv::OpImageSparseSampleProjExplicitLod: { |
| 1008 | sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id |
| 1009 | // ImageOperands in index: 5 |
| 1010 | if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) { |
| 1011 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1012 | } |
| 1013 | break; |
| 1014 | } |
| 1015 | case spv::OpImageSampleDrefImplicitLod: |
| 1016 | case spv::OpImageSampleDrefExplicitLod: |
| 1017 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1018 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1019 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 1020 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 1021 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 1022 | case spv::OpImageSparseSampleProjDrefExplicitLod: { |
| 1023 | sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id |
| 1024 | // ImageOperands in index: 6 |
| 1025 | if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) { |
| 1026 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1027 | } |
| 1028 | break; |
| 1029 | } |
| 1030 | case spv::OpImageSampleExplicitLod: |
| 1031 | case spv::OpImageSparseSampleExplicitLod: { |
| 1032 | // ImageOperands in index: 5 |
| 1033 | if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) { |
| 1034 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1035 | } |
| 1036 | break; |
| 1037 | } |
| 1038 | case spv::OpStore: { |
| 1039 | store_members.emplace_back(insn.word(1)); // object id or AccessChain id |
| 1040 | break; |
| 1041 | } |
| 1042 | case spv::OpImageWrite: { |
| 1043 | imagwrite_members.emplace_back(insn.word(1)); // Load id |
| 1044 | break; |
| 1045 | } |
| 1046 | case spv::OpSampledImage: { |
| 1047 | // 3: image load id, 4: sampler load id |
| 1048 | sampledImage_members.emplace_back(std::pair<unsigned, unsigned>(insn.word(3), insn.word(4))); |
| 1049 | break; |
| 1050 | } |
| 1051 | case spv::OpLoad: { |
| 1052 | // 2: Load id, 3: object id or AccessChain id |
| 1053 | load_members.insert(std::make_pair(insn.word(2), insn.word(3))); |
| 1054 | break; |
| 1055 | } |
| 1056 | case spv::OpAccessChain: { |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1057 | if (insn.len() == 4) { |
| 1058 | // If it is for struct, the length is only 4. |
| 1059 | // 2: AccessChain id, 3: object id |
| 1060 | accesschain_members.insert(std::make_pair(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), 0))); |
| 1061 | } else { |
| 1062 | // 2: AccessChain id, 3: object id, 4: object id of array index |
| 1063 | accesschain_members.insert( |
| 1064 | std::make_pair(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), insn.word(4)))); |
| 1065 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1066 | break; |
| 1067 | } |
| 1068 | case spv::OpImageTexelPointer: { |
| 1069 | // 2: ImageTexelPointer id, 3: object id |
| 1070 | image_texel_pointer_members.insert(std::make_pair(insn.word(2), insn.word(3))); |
| 1071 | break; |
| 1072 | } |
| 1073 | default: { |
| 1074 | if (AtomicOperation(insn.opcode())) { |
| 1075 | if (insn.opcode() == spv::OpAtomicStore) { |
| 1076 | atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id |
| 1077 | } else { |
| 1078 | atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id |
| 1079 | } |
| 1080 | } |
| 1081 | break; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | }; |
| 1087 | |
sfricke-samsung | 691299b | 2021-01-01 20:48:48 -0800 | [diff] [blame] | 1088 | // Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image |
| 1089 | // atomic operation, matching images to samplers, etc |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1090 | static void IsSpecificDescriptorType(SHADER_MODULE_STATE const *module, const spirv_inst_iter &id_it, bool is_storage_buffer, |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1091 | bool is_check_writable, interface_var &out_interface_var, |
| 1092 | shader_module_used_operators &used_operators) { |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1093 | uint32_t type_id = id_it.word(1); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1094 | unsigned int id = id_it.word(2); |
| 1095 | |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1096 | auto type = module->get_def(type_id); |
| 1097 | |
| 1098 | // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension. |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 1099 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray || |
| 1100 | type.opcode() == spv::OpTypeSampledImage) { |
| 1101 | if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray || |
| 1102 | type.opcode() == spv::OpTypeSampledImage) { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1103 | type = module->get_def(type.word(2)); // Element type |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1104 | } else { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1105 | type = module->get_def(type.word(3)); // Pointer type |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1106 | } |
| 1107 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1108 | switch (type.opcode()) { |
| 1109 | case spv::OpTypeImage: { |
| 1110 | auto dim = type.word(3); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1111 | if (dim != spv::DimSubpassData) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1112 | used_operators.update(module); |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1113 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1114 | if (CheckObjectIDFromOpLoad(id, used_operators.imagwrite_members, used_operators.load_members, |
| 1115 | used_operators.accesschain_members)) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1116 | out_interface_var.is_writable = true; |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 1117 | } |
| 1118 | if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members, |
| 1119 | used_operators.accesschain_members)) { |
| 1120 | out_interface_var.is_sampler_implicitLod_dref_proj = true; |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1121 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1122 | if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members, |
| 1123 | used_operators.accesschain_members)) { |
locke-lunarg | ae2a43c | 2020-09-22 17:21:57 -0600 | [diff] [blame] | 1124 | out_interface_var.is_sampler_bias_offset = true; |
| 1125 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1126 | if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members, |
| 1127 | used_operators.accesschain_members) || |
| 1128 | CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members, |
| 1129 | used_operators.accesschain_members)) { |
| 1130 | out_interface_var.is_atomic_operation = true; |
| 1131 | } |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1132 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1133 | for (auto &itp_id : used_operators.sampledImage_members) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1134 | // Find if image id match. |
| 1135 | uint32_t image_index = 0; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1136 | auto load_it = used_operators.load_members.find(itp_id.first); |
| 1137 | if (load_it == used_operators.load_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1138 | continue; |
| 1139 | } else { |
| 1140 | if (load_it->second != id) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1141 | auto accesschain_it = used_operators.accesschain_members.find(load_it->second); |
| 1142 | if (accesschain_it == used_operators.accesschain_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1143 | continue; |
| 1144 | } else { |
| 1145 | if (accesschain_it->second.first != id) { |
| 1146 | continue; |
| 1147 | } |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1148 | if (used_operators.load_members.end() != |
| 1149 | used_operators.load_members.find(accesschain_it->second.second)) { |
| 1150 | // image_index isn't a constant, skip. |
| 1151 | break; |
| 1152 | } |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1153 | image_index = GetConstantValue(module, accesschain_it->second.second); |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | // Find sampler's set binding. |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1158 | load_it = used_operators.load_members.find(itp_id.second); |
| 1159 | if (load_it == used_operators.load_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1160 | continue; |
| 1161 | } else { |
| 1162 | uint32_t sampler_id = load_it->second; |
| 1163 | uint32_t sampler_index = 0; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1164 | auto accesschain_it = used_operators.accesschain_members.find(load_it->second); |
| 1165 | if (accesschain_it != used_operators.accesschain_members.end()) { |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1166 | if (used_operators.load_members.end() != |
| 1167 | used_operators.load_members.find(accesschain_it->second.second)) { |
| 1168 | // sampler_index isn't a constant, skip. |
| 1169 | break; |
| 1170 | } |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1171 | sampler_id = accesschain_it->second.first; |
| 1172 | sampler_index = GetConstantValue(module, accesschain_it->second.second); |
| 1173 | } |
| 1174 | auto sampler_dec = module->get_decorations(sampler_id); |
locke-lunarg | 654a905 | 2020-10-13 16:28:42 -0600 | [diff] [blame] | 1175 | if (image_index >= out_interface_var.samplers_used_by_image.size()) { |
| 1176 | out_interface_var.samplers_used_by_image.resize(image_index + 1); |
| 1177 | } |
| 1178 | out_interface_var.samplers_used_by_image[image_index].emplace( |
| 1179 | SamplerUsedByImage{descriptor_slot_t{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index}); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1180 | } |
| 1181 | } |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1182 | } |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1183 | return; |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1186 | case spv::OpTypeStruct: { |
| 1187 | std::unordered_set<unsigned> nonwritable_members; |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 1188 | if (module->get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true; |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1189 | for (auto insn : *module) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 1190 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1) && |
| 1191 | insn.word(3) == spv::DecorationNonWritable) { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1192 | nonwritable_members.insert(insn.word(2)); |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1193 | } |
| 1194 | } |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1195 | |
| 1196 | // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated |
| 1197 | // as nonwritable. |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1198 | if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1199 | used_operators.update(module); |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1200 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1201 | for (auto oid : used_operators.store_members) { |
| 1202 | if (id == oid) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1203 | out_interface_var.is_writable = true; |
| 1204 | return; |
| 1205 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1206 | auto accesschain_it = used_operators.accesschain_members.find(oid); |
| 1207 | if (accesschain_it == used_operators.accesschain_members.end()) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1208 | continue; |
| 1209 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1210 | if (accesschain_it->second.first == id) { |
| 1211 | out_interface_var.is_writable = true; |
| 1212 | return; |
| 1213 | } |
| 1214 | } |
| 1215 | if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members, |
| 1216 | used_operators.accesschain_members)) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1217 | out_interface_var.is_writable = true; |
| 1218 | return; |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1219 | } |
| 1220 | } |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1221 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1222 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 1225 | std::vector<std::pair<descriptor_slot_t, interface_var>> CollectInterfaceByDescriptorSlot( |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 1226 | SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids, bool *has_writable_descriptor, |
| 1227 | bool *has_atomic_descriptor) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1228 | std::vector<std::pair<descriptor_slot_t, interface_var>> out; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1229 | shader_module_used_operators operators; |
| 1230 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1231 | for (auto id : accessible_ids) { |
| 1232 | auto insn = src->get_def(id); |
| 1233 | assert(insn != src->end()); |
| 1234 | |
| 1235 | if (insn.opcode() == spv::OpVariable && |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1236 | (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant || |
| 1237 | insn.word(3) == spv::StorageClassStorageBuffer)) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 1238 | auto d = src->get_decorations(insn.word(2)); |
| 1239 | unsigned set = d.descriptor_set; |
| 1240 | unsigned binding = d.binding; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1241 | |
| 1242 | interface_var v = {}; |
| 1243 | v.id = insn.word(2); |
| 1244 | v.type_id = insn.word(1); |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1245 | |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1246 | IsSpecificDescriptorType(src, insn, insn.word(3) == spv::StorageClassStorageBuffer, |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1247 | !(d.flags & decoration_set::nonwritable_bit), v, operators); |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 1248 | if (v.is_writable) *has_writable_descriptor = true; |
| 1249 | if (v.is_atomic_operation) *has_atomic_descriptor = true; |
locke-lunarg | 654e369 | 2020-06-04 17:19:15 -0600 | [diff] [blame] | 1250 | out.emplace_back(std::make_pair(set, binding), v); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | return out; |
| 1255 | } |
| 1256 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1257 | void DefineStructMember(const SHADER_MODULE_STATE &src, const spirv_inst_iter &it, |
| 1258 | const std::vector<uint32_t> &memberDecorate_offsets, shader_struct_member &data) { |
| 1259 | const auto struct_it = GetStructType(&src, it, false); |
| 1260 | assert(struct_it != src.end()); |
| 1261 | data.size = 0; |
| 1262 | |
| 1263 | shader_struct_member data1; |
| 1264 | uint32_t i = 2; |
| 1265 | uint32_t local_offset = 0; |
| 1266 | std::vector<uint32_t> offsets; |
| 1267 | offsets.resize(struct_it.len() - i); |
| 1268 | |
| 1269 | // The members of struct in SPRIV_R aren't always sort, so we need to know their order. |
| 1270 | for (const auto offset : memberDecorate_offsets) { |
| 1271 | const auto member_decorate = src.at(offset); |
| 1272 | if (member_decorate.word(1) != struct_it.word(1)) { |
| 1273 | continue; |
| 1274 | } |
| 1275 | |
| 1276 | offsets[member_decorate.word(2)] = member_decorate.word(4); |
| 1277 | } |
| 1278 | |
| 1279 | for (const auto offset : offsets) { |
| 1280 | local_offset = offset; |
| 1281 | data1 = {}; |
| 1282 | data1.root = data.root; |
| 1283 | data1.offset = local_offset; |
| 1284 | auto def_member = src.get_def(struct_it.word(i)); |
| 1285 | |
| 1286 | // Array could be multi-dimensional |
| 1287 | while (def_member.opcode() == spv::OpTypeArray) { |
| 1288 | const auto len_id = def_member.word(3); |
| 1289 | const auto def_len = src.get_def(len_id); |
| 1290 | data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length |
| 1291 | def_member = src.get_def(def_member.word(2)); |
| 1292 | } |
| 1293 | |
| 1294 | if (def_member.opcode() == spv::OpTypeStruct || def_member.opcode() == spv::OpTypePointer) { |
| 1295 | // If it's OpTypePointer. it means the member is a buffer, the type will be TypePointer, and then struct |
| 1296 | DefineStructMember(src, def_member, memberDecorate_offsets, data1); |
| 1297 | } else { |
| 1298 | if (def_member.opcode() == spv::OpTypeMatrix) { |
| 1299 | data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector. |
| 1300 | def_member = src.get_def(def_member.word(2)); |
| 1301 | } |
| 1302 | |
| 1303 | if (def_member.opcode() == spv::OpTypeVector) { |
| 1304 | data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length |
| 1305 | def_member = src.get_def(def_member.word(2)); |
| 1306 | } |
| 1307 | |
| 1308 | // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte. |
| 1309 | data1.size = (def_member.word(2) / 8); |
| 1310 | } |
| 1311 | const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size(); |
| 1312 | if (array_length_hierarchy_szie > 0) { |
| 1313 | data1.array_block_size.resize(array_length_hierarchy_szie, 1); |
| 1314 | |
| 1315 | for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) { |
| 1316 | data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2]; |
| 1317 | } |
| 1318 | } |
| 1319 | data.struct_members.emplace_back(data1); |
| 1320 | ++i; |
| 1321 | } |
| 1322 | uint32_t total_array_length = 1; |
| 1323 | for (const auto length : data1.array_length_hierarchy) { |
| 1324 | total_array_length *= length; |
| 1325 | } |
| 1326 | data.size = local_offset + data1.size * total_array_length; |
| 1327 | } |
| 1328 | |
| 1329 | uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) { |
| 1330 | int array_indices_size = static_cast<int>(array_indices.size()); |
| 1331 | if (array_indices_size) { |
| 1332 | uint32_t array_index = 0; |
| 1333 | uint32_t i = 0; |
| 1334 | for (const auto index : array_indices) { |
| 1335 | array_index += (data.array_block_size[i] * index); |
| 1336 | ++i; |
| 1337 | } |
| 1338 | offset += (array_index * data.size); |
| 1339 | } |
| 1340 | return offset; |
| 1341 | } |
| 1342 | |
| 1343 | void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) { |
| 1344 | int array_indices_size = static_cast<int>(array_indices.size()); |
| 1345 | uint32_t block_memory_size = data.size; |
| 1346 | for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) { |
| 1347 | block_memory_size *= data.array_length_hierarchy[i]; |
| 1348 | } |
| 1349 | |
| 1350 | offset = UpdateOffset(offset, array_indices, data); |
| 1351 | |
| 1352 | uint32_t end = offset + block_memory_size; |
| 1353 | auto used_bytes = data.GetUsedbytes(); |
| 1354 | if (used_bytes->size() < end) { |
| 1355 | used_bytes->resize(end, 0); |
| 1356 | } |
| 1357 | std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size)); |
| 1358 | } |
| 1359 | |
| 1360 | void RunUsedArray(const SHADER_MODULE_STATE &src, uint32_t offset, std::vector<uint32_t> array_indices, |
| 1361 | uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it, const shader_struct_member &data) { |
| 1362 | if (access_chain_word_index < access_chain_it.len()) { |
| 1363 | if (data.array_length_hierarchy.size() > array_indices.size()) { |
| 1364 | auto def_it = src.get_def(access_chain_it.word(access_chain_word_index)); |
| 1365 | ++access_chain_word_index; |
| 1366 | |
| 1367 | if (def_it != src.end() && def_it.opcode() == spv::OpConstant) { |
| 1368 | array_indices.emplace_back(def_it.word(3)); |
| 1369 | RunUsedArray(src, offset, array_indices, access_chain_word_index, access_chain_it, data); |
| 1370 | } else { |
| 1371 | // If it is a variable, set the all array is used. |
| 1372 | if (access_chain_word_index < access_chain_it.len()) { |
| 1373 | uint32_t array_length = data.array_length_hierarchy[array_indices.size()]; |
| 1374 | for (uint32_t i = 0; i < array_length; ++i) { |
| 1375 | auto array_indices2 = array_indices; |
| 1376 | array_indices2.emplace_back(i); |
| 1377 | RunUsedArray(src, offset, array_indices2, access_chain_word_index, access_chain_it, data); |
| 1378 | } |
| 1379 | } else { |
| 1380 | SetUsedBytes(offset, array_indices, data); |
| 1381 | } |
| 1382 | } |
| 1383 | } else { |
| 1384 | offset = UpdateOffset(offset, array_indices, data); |
| 1385 | RunUsedStruct(src, offset, access_chain_word_index, access_chain_it, data); |
| 1386 | } |
| 1387 | } else { |
| 1388 | SetUsedBytes(offset, array_indices, data); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | void RunUsedStruct(const SHADER_MODULE_STATE &src, uint32_t offset, uint32_t access_chain_word_index, |
| 1393 | spirv_inst_iter &access_chain_it, const shader_struct_member &data) { |
| 1394 | std::vector<uint32_t> array_indices_emptry; |
| 1395 | |
| 1396 | if (access_chain_word_index < access_chain_it.len()) { |
| 1397 | auto strcut_member_index = GetConstantValue(&src, access_chain_it.word(access_chain_word_index)); |
| 1398 | ++access_chain_word_index; |
| 1399 | |
| 1400 | auto data1 = data.struct_members[strcut_member_index]; |
| 1401 | RunUsedArray(src, offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1); |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | void SetUsedStructMember(const SHADER_MODULE_STATE &src, const uint32_t variable_id, |
| 1406 | const std::vector<function_set> &function_set_list, const shader_struct_member &data) { |
| 1407 | for (const auto &func_set : function_set_list) { |
| 1408 | auto range = func_set.op_lists.equal_range(spv::OpAccessChain); |
| 1409 | for (auto it = range.first; it != range.second; ++it) { |
| 1410 | auto access_chain = src.at(it->second); |
| 1411 | if (access_chain.word(3) == variable_id) { |
| 1412 | RunUsedStruct(src, 0, 4, access_chain, data); |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | void SetPushConstantUsedInShader(SHADER_MODULE_STATE &src) { |
| 1419 | for (auto &entrypoint : src.entry_points) { |
| 1420 | auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable); |
| 1421 | for (auto it = range.first; it != range.second; ++it) { |
| 1422 | const auto def_insn = src.at(it->second); |
| 1423 | |
| 1424 | if (def_insn.word(3) == spv::StorageClassPushConstant) { |
| 1425 | spirv_inst_iter type = src.get_def(def_insn.word(1)); |
| 1426 | const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate); |
| 1427 | std::vector<uint32_t> offsets; |
| 1428 | |
| 1429 | for (auto it2 = range2.first; it2 != range2.second; ++it2) { |
| 1430 | auto member_decorate = src.at(it2->second); |
| 1431 | if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) { |
| 1432 | offsets.emplace_back(member_decorate.offset()); |
| 1433 | } |
| 1434 | } |
| 1435 | entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader; |
| 1436 | DefineStructMember(src, type, offsets, entrypoint.second.push_constant_used_in_shader); |
| 1437 | SetUsedStructMember(src, def_insn.word(2), entrypoint.second.function_set_list, |
| 1438 | entrypoint.second.push_constant_used_in_shader); |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
locke-lunarg | 96dc963 | 2020-06-10 17:22:18 -0600 | [diff] [blame] | 1444 | std::unordered_set<uint32_t> CollectWritableOutputLocationinFS(const SHADER_MODULE_STATE &module, |
| 1445 | const VkPipelineShaderStageCreateInfo &stage_info) { |
| 1446 | std::unordered_set<uint32_t> location_list; |
| 1447 | if (stage_info.stage != VK_SHADER_STAGE_FRAGMENT_BIT) return location_list; |
| 1448 | const auto entrypoint = FindEntrypoint(&module, stage_info.pName, stage_info.stage); |
| 1449 | const auto outputs = CollectInterfaceByLocation(&module, entrypoint, spv::StorageClassOutput, false); |
| 1450 | std::unordered_set<unsigned> store_members; |
| 1451 | std::unordered_map<unsigned, unsigned> accesschain_members; |
| 1452 | |
| 1453 | for (auto insn : module) { |
| 1454 | switch (insn.opcode()) { |
| 1455 | case spv::OpStore: |
| 1456 | case spv::OpAtomicStore: { |
| 1457 | store_members.insert(insn.word(1)); // object id or AccessChain id |
| 1458 | break; |
| 1459 | } |
| 1460 | case spv::OpAccessChain: { |
| 1461 | // 2: AccessChain id, 3: object id |
| 1462 | if (insn.word(3)) accesschain_members.insert(std::make_pair(insn.word(2), insn.word(3))); |
| 1463 | break; |
| 1464 | } |
| 1465 | default: |
| 1466 | break; |
| 1467 | } |
| 1468 | } |
| 1469 | if (store_members.empty()) { |
| 1470 | return location_list; |
| 1471 | } |
| 1472 | for (auto output : outputs) { |
| 1473 | auto store_it = store_members.find(output.second.id); |
| 1474 | if (store_it != store_members.end()) { |
| 1475 | location_list.insert(output.first.first); |
| 1476 | store_members.erase(store_it); |
| 1477 | continue; |
| 1478 | } |
| 1479 | store_it = store_members.begin(); |
| 1480 | while (store_it != store_members.end()) { |
| 1481 | auto accesschain_it = accesschain_members.find(*store_it); |
| 1482 | if (accesschain_it == accesschain_members.end()) { |
| 1483 | ++store_it; |
| 1484 | continue; |
| 1485 | } |
| 1486 | if (accesschain_it->second == output.second.id) { |
| 1487 | location_list.insert(output.first.first); |
| 1488 | store_members.erase(store_it); |
| 1489 | accesschain_members.erase(accesschain_it); |
| 1490 | break; |
| 1491 | } |
| 1492 | ++store_it; |
| 1493 | } |
| 1494 | } |
| 1495 | return location_list; |
| 1496 | } |
| 1497 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1498 | bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1499 | // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should |
| 1500 | // be specified only once. |
| 1501 | std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings; |
| 1502 | bool skip = false; |
| 1503 | |
| 1504 | for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) { |
| 1505 | auto desc = &vi->pVertexBindingDescriptions[i]; |
| 1506 | auto &binding = bindings[desc->binding]; |
| 1507 | if (binding) { |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 1508 | // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps? |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1509 | skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d", |
| 1510 | desc->binding); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1511 | } else { |
| 1512 | binding = desc; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | return skip; |
| 1517 | } |
| 1518 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1519 | bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs, |
| 1520 | spirv_inst_iter entrypoint) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1521 | bool skip = false; |
| 1522 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1523 | const auto inputs = CollectInterfaceByLocation(vs, entrypoint, spv::StorageClassInput, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1524 | |
| 1525 | // Build index by location |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1526 | std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1527 | if (vi) { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1528 | for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) { |
| 1529 | const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format); |
| 1530 | for (uint32_t j = 0; j < num_locations; ++j) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1531 | attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i]; |
| 1532 | } |
| 1533 | } |
| 1534 | } |
| 1535 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1536 | struct AttribInputPair { |
| 1537 | const VkVertexInputAttributeDescription *attrib = nullptr; |
| 1538 | const interface_var *input = nullptr; |
| 1539 | }; |
| 1540 | std::map<uint32_t, AttribInputPair> location_map; |
| 1541 | for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second; |
| 1542 | 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] | 1543 | |
Jamie Madill | c1f7ca8 | 2020-03-16 17:08:26 -0400 | [diff] [blame] | 1544 | for (const auto &location_it : location_map) { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1545 | const auto location = location_it.first; |
| 1546 | const auto attrib = location_it.second.attrib; |
| 1547 | const auto input = location_it.second.input; |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1548 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1549 | if (attrib && !input) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1550 | skip |= LogPerformanceWarning(vs->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 1551 | "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1552 | } else if (!attrib && input) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1553 | skip |= LogError(vs->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 1554 | "Vertex shader consumes input at location %" PRIu32 " but not provided", location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1555 | } else if (attrib && input) { |
| 1556 | const auto attrib_type = GetFormatType(attrib->format); |
| 1557 | const auto input_type = GetFundamentalType(vs, input->type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1558 | |
| 1559 | // Type checking |
| 1560 | if (!(attrib_type & input_type)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1561 | skip |= LogError(vs->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 1562 | "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`", |
| 1563 | string_VkFormat(attrib->format), location, DescribeType(vs, input->type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1564 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1565 | } else { // !attrib && !input |
| 1566 | assert(false); // at least one exists in the map |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | return skip; |
| 1571 | } |
| 1572 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1573 | bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint, |
| 1574 | PIPELINE_STATE const *pipeline, uint32_t subpass_index) const { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1575 | bool skip = false; |
Chris Forbes | 8bca165 | 2017-07-20 11:10:09 -0700 | [diff] [blame] | 1576 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1577 | const auto rpci = pipeline->rp_state->createInfo.ptr(); |
| 1578 | |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1579 | struct Attachment { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1580 | const VkAttachmentReference2 *reference = nullptr; |
| 1581 | const VkAttachmentDescription2 *attachment = nullptr; |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1582 | const interface_var *output = nullptr; |
| 1583 | }; |
| 1584 | std::map<uint32_t, Attachment> location_map; |
| 1585 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1586 | const auto subpass = rpci->pSubpasses[subpass_index]; |
| 1587 | for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) { |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1588 | auto const &reference = subpass.pColorAttachments[i]; |
| 1589 | location_map[i].reference = &reference; |
| 1590 | if (reference.attachment != VK_ATTACHMENT_UNUSED && |
| 1591 | rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) { |
| 1592 | location_map[i].attachment = &rpci->pAttachments[reference.attachment]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1596 | // TODO: dual source blend index (spv::DecIndex, zero if not provided) |
| 1597 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1598 | const auto outputs = CollectInterfaceByLocation(fs, entrypoint, spv::StorageClassOutput, false); |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1599 | for (const auto &output_it : outputs) { |
| 1600 | auto const location = output_it.first.first; |
| 1601 | location_map[location].output = &output_it.second; |
| 1602 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1603 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1604 | const bool alpha_to_coverage_enabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL && |
| 1605 | pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1606 | |
Jamie Madill | c1f7ca8 | 2020-03-16 17:08:26 -0400 | [diff] [blame] | 1607 | for (const auto &location_it : location_map) { |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1608 | const auto reference = location_it.second.reference; |
| 1609 | if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) { |
| 1610 | continue; |
| 1611 | } |
| 1612 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1613 | const auto location = location_it.first; |
| 1614 | const auto attachment = location_it.second.attachment; |
| 1615 | const auto output = location_it.second.output; |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1616 | if (attachment && !output) { |
| 1617 | if (pipeline->attachments[location].colorWriteMask != 0) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1618 | skip |= LogWarning(fs->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 1619 | "Attachment %" PRIu32 |
| 1620 | " not written by fragment shader; undefined values will be written to attachment", |
| 1621 | location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1622 | } |
| 1623 | } else if (!attachment && output) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1624 | if (!(alpha_to_coverage_enabled && location == 0)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1625 | skip |= LogWarning(fs->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 1626 | "fragment shader writes to output location %" PRIu32 " with no matching attachment", location); |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1627 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1628 | } else if (attachment && output) { |
| 1629 | const auto attachment_type = GetFormatType(attachment->format); |
| 1630 | const auto output_type = GetFundamentalType(fs, output->type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1631 | |
| 1632 | // Type checking |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1633 | if (!(output_type & attachment_type)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1634 | skip |= |
| 1635 | LogWarning(fs->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 1636 | "Attachment %" PRIu32 |
| 1637 | " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined", |
| 1638 | location, string_VkFormat(attachment->format), DescribeType(fs, output->type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1639 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1640 | } else { // !attachment && !output |
| 1641 | assert(false); // at least one exists in the map |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1645 | 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] | 1646 | bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() && |
| 1647 | GetComponentsConsumedByType(fs, output_zero->type_id, false) == 4; |
| 1648 | if (alpha_to_coverage_enabled && !location_zero_has_alpha) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1649 | skip |= LogError(fs->vk_shader_module, kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage, |
| 1650 | "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] | 1651 | } |
| 1652 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1653 | return skip; |
| 1654 | } |
| 1655 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1656 | // For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to. |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1657 | // This function examines instructions in the static call tree for a write to this variable. |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1658 | static bool IsBuiltInWritten(SHADER_MODULE_STATE const *src, spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1659 | auto type = builtin_instr.opcode(); |
| 1660 | uint32_t target_id = builtin_instr.word(1); |
| 1661 | bool init_complete = false; |
| 1662 | |
| 1663 | if (type == spv::OpMemberDecorate) { |
| 1664 | // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs |
| 1665 | auto insn = entrypoint; |
| 1666 | while (!init_complete && (insn.opcode() != spv::OpFunction)) { |
| 1667 | switch (insn.opcode()) { |
| 1668 | case spv::OpTypePointer: |
| 1669 | if ((insn.word(3) == target_id) && (insn.word(2) == spv::StorageClassOutput)) { |
| 1670 | target_id = insn.word(1); |
| 1671 | } |
| 1672 | break; |
| 1673 | case spv::OpVariable: |
| 1674 | if (insn.word(1) == target_id) { |
| 1675 | target_id = insn.word(2); |
| 1676 | init_complete = true; |
| 1677 | } |
| 1678 | break; |
| 1679 | } |
| 1680 | insn++; |
| 1681 | } |
| 1682 | } |
| 1683 | |
Mark Lobodzinski | f84b0b4 | 2018-09-11 14:54:32 -0600 | [diff] [blame] | 1684 | if (!init_complete && (type == spv::OpMemberDecorate)) return false; |
| 1685 | |
| 1686 | bool found_write = false; |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1687 | std::unordered_set<uint32_t> worklist; |
| 1688 | worklist.insert(entrypoint.word(2)); |
| 1689 | |
| 1690 | // Follow instructions in call graph looking for writes to target |
| 1691 | while (!worklist.empty() && !found_write) { |
| 1692 | auto id_iter = worklist.begin(); |
| 1693 | auto id = *id_iter; |
| 1694 | worklist.erase(id_iter); |
| 1695 | |
| 1696 | auto insn = src->get_def(id); |
| 1697 | if (insn == src->end()) { |
| 1698 | continue; |
| 1699 | } |
| 1700 | |
| 1701 | if (insn.opcode() == spv::OpFunction) { |
| 1702 | // Scan body of function looking for other function calls or items in our ID chain |
| 1703 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1704 | switch (insn.opcode()) { |
| 1705 | case spv::OpAccessChain: |
| 1706 | if (insn.word(3) == target_id) { |
| 1707 | if (type == spv::OpMemberDecorate) { |
| 1708 | auto value = GetConstantValue(src, insn.word(4)); |
| 1709 | if (value == builtin_instr.word(2)) { |
| 1710 | target_id = insn.word(2); |
| 1711 | } |
| 1712 | } else { |
| 1713 | target_id = insn.word(2); |
| 1714 | } |
| 1715 | } |
| 1716 | break; |
| 1717 | case spv::OpStore: |
| 1718 | if (insn.word(1) == target_id) { |
| 1719 | found_write = true; |
| 1720 | } |
| 1721 | break; |
| 1722 | case spv::OpFunctionCall: |
| 1723 | worklist.insert(insn.word(3)); |
| 1724 | break; |
| 1725 | } |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | return found_write; |
| 1730 | } |
| 1731 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1732 | // For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is |
| 1733 | // important for identifying the set of shader resources actually used by an entrypoint, for example. |
| 1734 | // Note: we only explore parts of the image which might actually contain ids we care about for the above analyses. |
| 1735 | // - NOT the shader input/output interfaces. |
| 1736 | // |
| 1737 | // TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth |
| 1738 | // converting parts of this to be generated from the machine-readable spec instead. |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 1739 | std::unordered_set<uint32_t> MarkAccessibleIds(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1740 | std::unordered_set<uint32_t> ids; |
| 1741 | std::unordered_set<uint32_t> worklist; |
| 1742 | worklist.insert(entrypoint.word(2)); |
| 1743 | |
| 1744 | while (!worklist.empty()) { |
| 1745 | auto id_iter = worklist.begin(); |
| 1746 | auto id = *id_iter; |
| 1747 | worklist.erase(id_iter); |
| 1748 | |
| 1749 | auto insn = src->get_def(id); |
| 1750 | if (insn == src->end()) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1751 | // ID is something we didn't collect in BuildDefIndex. that's OK -- we'll stumble across all kinds of things here |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1752 | // that we may not care about. |
| 1753 | continue; |
| 1754 | } |
| 1755 | |
| 1756 | // Try to add to the output set |
| 1757 | if (!ids.insert(id).second) { |
| 1758 | continue; // If we already saw this id, we don't want to walk it again. |
| 1759 | } |
| 1760 | |
| 1761 | switch (insn.opcode()) { |
| 1762 | case spv::OpFunction: |
| 1763 | // Scan whole body of the function, enlisting anything interesting |
| 1764 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1765 | switch (insn.opcode()) { |
| 1766 | case spv::OpLoad: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1767 | worklist.insert(insn.word(3)); // ptr |
| 1768 | break; |
| 1769 | case spv::OpStore: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1770 | worklist.insert(insn.word(1)); // ptr |
| 1771 | break; |
| 1772 | case spv::OpAccessChain: |
| 1773 | case spv::OpInBoundsAccessChain: |
| 1774 | worklist.insert(insn.word(3)); // base ptr |
| 1775 | break; |
| 1776 | case spv::OpSampledImage: |
| 1777 | case spv::OpImageSampleImplicitLod: |
| 1778 | case spv::OpImageSampleExplicitLod: |
| 1779 | case spv::OpImageSampleDrefImplicitLod: |
| 1780 | case spv::OpImageSampleDrefExplicitLod: |
| 1781 | case spv::OpImageSampleProjImplicitLod: |
| 1782 | case spv::OpImageSampleProjExplicitLod: |
| 1783 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1784 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1785 | case spv::OpImageFetch: |
| 1786 | case spv::OpImageGather: |
| 1787 | case spv::OpImageDrefGather: |
| 1788 | case spv::OpImageRead: |
| 1789 | case spv::OpImage: |
| 1790 | case spv::OpImageQueryFormat: |
| 1791 | case spv::OpImageQueryOrder: |
| 1792 | case spv::OpImageQuerySizeLod: |
| 1793 | case spv::OpImageQuerySize: |
| 1794 | case spv::OpImageQueryLod: |
| 1795 | case spv::OpImageQueryLevels: |
| 1796 | case spv::OpImageQuerySamples: |
| 1797 | case spv::OpImageSparseSampleImplicitLod: |
| 1798 | case spv::OpImageSparseSampleExplicitLod: |
| 1799 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 1800 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 1801 | case spv::OpImageSparseSampleProjImplicitLod: |
| 1802 | case spv::OpImageSparseSampleProjExplicitLod: |
| 1803 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 1804 | case spv::OpImageSparseSampleProjDrefExplicitLod: |
| 1805 | case spv::OpImageSparseFetch: |
| 1806 | case spv::OpImageSparseGather: |
| 1807 | case spv::OpImageSparseDrefGather: |
| 1808 | case spv::OpImageTexelPointer: |
| 1809 | worklist.insert(insn.word(3)); // Image or sampled image |
| 1810 | break; |
| 1811 | case spv::OpImageWrite: |
| 1812 | worklist.insert(insn.word(1)); // Image -- different operand order to above |
| 1813 | break; |
| 1814 | case spv::OpFunctionCall: |
| 1815 | for (uint32_t i = 3; i < insn.len(); i++) { |
| 1816 | worklist.insert(insn.word(i)); // fn itself, and all args |
| 1817 | } |
| 1818 | break; |
| 1819 | |
| 1820 | case spv::OpExtInst: |
| 1821 | for (uint32_t i = 5; i < insn.len(); i++) { |
| 1822 | worklist.insert(insn.word(i)); // Operands to ext inst |
| 1823 | } |
| 1824 | break; |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1825 | |
| 1826 | default: { |
| 1827 | if (AtomicOperation(insn.opcode())) { |
| 1828 | if (insn.opcode() == spv::OpAtomicStore) { |
| 1829 | worklist.insert(insn.word(1)); // ptr |
| 1830 | } else { |
| 1831 | worklist.insert(insn.word(3)); // ptr |
| 1832 | } |
| 1833 | } |
| 1834 | break; |
| 1835 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | break; |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | return ids; |
| 1843 | } |
| 1844 | |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1845 | PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update, |
| 1846 | const shader_struct_member &push_constant_used_in_shader, |
| 1847 | uint32_t &out_issue_index) const { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1848 | const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes(); |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1849 | const auto used_bytes_size = used_bytes->size(); |
| 1850 | if (used_bytes_size == 0) return PC_Byte_Updated; |
| 1851 | |
| 1852 | const auto push_constant_data_update_size = push_constant_data_update.size(); |
| 1853 | const auto *data = push_constant_data_update.data(); |
| 1854 | if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) { |
| 1855 | if (used_bytes_size <= push_constant_data_update_size) { |
| 1856 | return PC_Byte_Updated; |
| 1857 | } |
| 1858 | const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size; |
| 1859 | |
| 1860 | const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size; |
| 1861 | if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) { |
| 1862 | return PC_Byte_Updated; |
| 1863 | } |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1864 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1865 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1866 | uint32_t i = 0; |
| 1867 | for (const auto used : *used_bytes) { |
| 1868 | if (used) { |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1869 | 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] | 1870 | out_issue_index = i; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1871 | return PC_Byte_Not_Set; |
| 1872 | } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1873 | out_issue_index = i; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1874 | return PC_Byte_Not_Updated; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | ++i; |
| 1878 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1879 | return PC_Byte_Updated; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src, |
| 1883 | VkPipelineShaderStageCreateInfo const *pStage) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1884 | bool skip = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1885 | // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step. |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1886 | const auto *entrypoint = FindEntrypointStruct(src, pStage->pName, pStage->stage); |
| 1887 | if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) { |
| 1888 | return skip; |
| 1889 | } |
| 1890 | 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] | 1891 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1892 | bool found_stage = false; |
| 1893 | for (auto const &range : *push_constant_ranges) { |
| 1894 | if (range.stageFlags & pStage->stage) { |
| 1895 | found_stage = true; |
| 1896 | std::string location_desc; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1897 | std::vector<uint8_t> push_constant_bytes_set; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1898 | if (range.offset > 0) { |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1899 | push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1900 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1901 | push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1902 | uint32_t issue_index = 0; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1903 | const auto ret = |
| 1904 | 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] | 1905 | |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1906 | if (ret == PC_Byte_Not_Set) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1907 | const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index); |
| 1908 | LogObjectList objlist(src->vk_shader_module); |
| 1909 | objlist.add(pipeline.pipeline_layout->layout); |
| 1910 | skip |= LogError(objlist, kVUID_Core_Shader_PushConstantOutOfRange, |
| 1911 | "Push-constant buffer:%s in %s is out of range in %s.", loc_descr.c_str(), |
| 1912 | string_VkShaderStageFlags(pStage->stage).c_str(), |
| 1913 | report_data->FormatHandle(pipeline.pipeline_layout->layout).c_str()); |
| 1914 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1919 | if (!found_stage) { |
| 1920 | LogObjectList objlist(src->vk_shader_module); |
| 1921 | objlist.add(pipeline.pipeline_layout->layout); |
| 1922 | skip |= LogError( |
| 1923 | objlist, kVUID_Core_Shader_PushConstantOutOfRange, "Push constant is used in %s of %s. But %s doesn't set %s.", |
| 1924 | string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module).c_str(), |
| 1925 | report_data->FormatHandle(pipeline.pipeline_layout->layout).c_str(), string_VkShaderStageFlags(pStage->stage).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1926 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1927 | return skip; |
| 1928 | } |
| 1929 | |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 1930 | bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, const std::unordered_set<uint32_t> &accessible_ids, |
| 1931 | VkShaderStageFlagBits stage) const { |
| 1932 | bool skip = false; |
| 1933 | |
| 1934 | // Currently all builtin tested are only found in fragment shaders |
| 1935 | if (stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 1936 | return skip; |
| 1937 | } |
| 1938 | |
| 1939 | for (const auto id : accessible_ids) { |
| 1940 | auto insn = src->get_def(id); |
| 1941 | const decoration_set decorations = src->get_decorations(insn.word(2)); |
| 1942 | |
| 1943 | // Built-ins are obtained from OpVariable |
| 1944 | if (((decorations.flags & decoration_set::builtin_bit) != 0) && (insn.opcode() == spv::OpVariable)) { |
| 1945 | auto type_pointer = src->get_def(insn.word(1)); |
| 1946 | assert(type_pointer.opcode() == spv::OpTypePointer); |
| 1947 | |
| 1948 | auto type = src->get_def(type_pointer.word(3)); |
| 1949 | if (type.opcode() == spv::OpTypeArray) { |
| 1950 | uint32_t length = static_cast<uint32_t>(GetConstantValue(src, type.word(3))); |
| 1951 | |
| 1952 | switch (decorations.builtin) { |
| 1953 | case spv::BuiltInSampleMask: |
| 1954 | // Handles both the input and output sampleMask |
| 1955 | if (length > phys_dev_props.limits.maxSampleMaskWords) { |
| 1956 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711", |
| 1957 | "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds " |
| 1958 | "maxSampleMaskWords of %u in %s.", |
| 1959 | length, phys_dev_props.limits.maxSampleMaskWords, |
| 1960 | report_data->FormatHandle(src->vk_shader_module).c_str()); |
| 1961 | } |
| 1962 | break; |
| 1963 | } |
| 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | return skip; |
| 1969 | } |
| 1970 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1971 | // 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] | 1972 | bool CoreChecks::ValidateSpecializationOffsets(VkPipelineShaderStageCreateInfo const *info) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1973 | bool skip = false; |
| 1974 | |
| 1975 | VkSpecializationInfo const *spec = info->pSpecializationInfo; |
| 1976 | |
| 1977 | if (spec) { |
| 1978 | for (auto i = 0u; i < spec->mapEntryCount; i++) { |
Jeremy Hayes | 6c555c3 | 2019-09-09 17:14:09 -0600 | [diff] [blame] | 1979 | if (spec->pMapEntries[i].offset >= spec->dataSize) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1980 | skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773", |
| 1981 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
| 1982 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..", |
| 1983 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
| 1984 | spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize); |
Jeremy Hayes | 6c555c3 | 2019-09-09 17:14:09 -0600 | [diff] [blame] | 1985 | |
| 1986 | continue; |
| 1987 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1988 | if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1989 | skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774", |
| 1990 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
| 1991 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..", |
| 1992 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
| 1993 | spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | return skip; |
| 1999 | } |
| 2000 | |
Jeff Bolz | 38b3ce7 | 2018-09-19 12:53:38 -0500 | [diff] [blame] | 2001 | // TODO (jbolz): Can this return a const reference? |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2002 | static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count, |
| 2003 | bool is_khr) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2004 | auto type = module->get_def(type_id); |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2005 | bool is_storage_buffer = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2006 | descriptor_count = 1; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2007 | std::set<uint32_t> ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2008 | |
| 2009 | // 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] | 2010 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) { |
| 2011 | if (type.opcode() == spv::OpTypeRuntimeArray) { |
| 2012 | descriptor_count = 0; |
| 2013 | type = module->get_def(type.word(2)); |
| 2014 | } else if (type.opcode() == spv::OpTypeArray) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2015 | descriptor_count *= GetConstantValue(module, type.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2016 | type = module->get_def(type.word(2)); |
| 2017 | } else { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2018 | if (type.word(2) == spv::StorageClassStorageBuffer) { |
| 2019 | is_storage_buffer = true; |
| 2020 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2021 | type = module->get_def(type.word(3)); |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | switch (type.opcode()) { |
| 2026 | case spv::OpTypeStruct: { |
| 2027 | for (auto insn : *module) { |
| 2028 | if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) { |
| 2029 | if (insn.word(2) == spv::DecorationBlock) { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2030 | if (is_storage_buffer) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2031 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 2032 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 2033 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2034 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2035 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 2036 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC); |
| 2037 | ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT); |
| 2038 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2039 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2040 | } else if (insn.word(2) == spv::DecorationBufferBlock) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2041 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 2042 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 2043 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | // Invalid |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2049 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | case spv::OpTypeSampler: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2053 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER); |
| 2054 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2055 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2056 | |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2057 | case spv::OpTypeSampledImage: { |
| 2058 | // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel |
| 2059 | // buffer descriptor doesn't really provide one. Allow this slight mismatch. |
| 2060 | auto image_type = module->get_def(type.word(2)); |
| 2061 | auto dim = image_type.word(3); |
| 2062 | auto sampled = image_type.word(7); |
| 2063 | if (dim == spv::DimBuffer && sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2064 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 2065 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2066 | } |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2067 | } |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2068 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2069 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2070 | |
| 2071 | case spv::OpTypeImage: { |
| 2072 | // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler. |
| 2073 | // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable. |
| 2074 | auto dim = type.word(3); |
| 2075 | auto sampled = type.word(7); |
| 2076 | |
| 2077 | if (dim == spv::DimSubpassData) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2078 | ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT); |
| 2079 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2080 | } else if (dim == spv::DimBuffer) { |
| 2081 | if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2082 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 2083 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2084 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2085 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER); |
| 2086 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2087 | } |
| 2088 | } else if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2089 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); |
| 2090 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2091 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2092 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2093 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); |
| 2094 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2095 | } |
| 2096 | } |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 2097 | case spv::OpTypeAccelerationStructureNV: |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2098 | is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR) |
| 2099 | : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV); |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 2100 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2101 | |
| 2102 | // We shouldn't really see any other junk types -- but if we do, they're a mismatch. |
| 2103 | default: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2104 | return ret; // Matches nothing |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2105 | } |
| 2106 | } |
| 2107 | |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2108 | static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) { |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2109 | std::stringstream ss; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2110 | for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) { |
| 2111 | if (ss.tellp()) ss << ", "; |
| 2112 | ss << string_VkDescriptorType(VkDescriptorType(*it)); |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2113 | } |
| 2114 | return ss.str(); |
| 2115 | } |
| 2116 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2117 | 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] | 2118 | if (!check) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2119 | 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] | 2120 | return true; |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | return false; |
| 2125 | } |
| 2126 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2127 | 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] | 2128 | if (!feature) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2129 | 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] | 2130 | return true; |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | return false; |
| 2135 | } |
| 2136 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 2137 | bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor, |
| 2138 | bool has_atomic_descriptor) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2139 | bool skip = false; |
| 2140 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 2141 | if (has_writable_descriptor || has_atomic_descriptor) { |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2142 | switch (stage) { |
| 2143 | case VK_SHADER_STAGE_COMPUTE_BIT: |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2144 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 2145 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 2146 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 2147 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 2148 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 2149 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 2150 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 2151 | case VK_SHADER_STAGE_MESH_BIT_NV: |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2152 | /* No feature requirements for writes and atomics from compute |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2153 | * raytracing, or mesh stages */ |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2154 | break; |
| 2155 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2156 | skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics", |
| 2157 | kVUID_Core_Shader_FeatureNotEnabled); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2158 | break; |
| 2159 | default: |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2160 | skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics", |
| 2161 | kVUID_Core_Shader_FeatureNotEnabled); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2162 | break; |
| 2163 | } |
| 2164 | } |
| 2165 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2166 | return skip; |
| 2167 | } |
| 2168 | |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2169 | bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2170 | bool skip = false; |
| 2171 | |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2172 | auto const subgroup_props = phys_dev_props_core11; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2173 | const VkSubgroupFeatureFlags supported_stages = subgroup_props.subgroupSupportedStages; |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2174 | |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2175 | for (auto inst : *module) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2176 | // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations) |
| 2177 | if (GroupOperation(inst.opcode()) == true) { |
| 2178 | // Check the quad operations. |
| 2179 | if ((inst.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (inst.opcode() == spv::OpGroupNonUniformQuadSwap)) { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2180 | if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2181 | skip |= RequireFeature(subgroup_props.subgroupQuadOperationsInAllStages, |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2182 | "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", |
| 2183 | kVUID_Core_Shader_FeatureNotEnabled); |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2184 | } |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2185 | } |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2186 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2187 | uint32_t scope_type = spv::ScopeMax; |
sfricke-samsung | 9518014 | 2020-12-10 20:58:20 -0800 | [diff] [blame] | 2188 | if (inst.opcode() == spv::OpGroupNonUniformPartitionNV) { |
| 2189 | // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand |
| 2190 | scope_type = spv::ScopeSubgroup; |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2191 | } else { |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame^] | 2192 | // "All <id> used for Scope <id> must be of an OpConstant" |
sfricke-samsung | 9518014 | 2020-12-10 20:58:20 -0800 | [diff] [blame] | 2193 | auto scope_id = module->get_def(inst.word(3)); |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame^] | 2194 | scope_type = scope_id.word(3); |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | if (scope_type == spv::ScopeSubgroup) { |
| 2198 | // "Group operations with subgroup scope" must have stage support |
| 2199 | skip |= |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2200 | RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage), |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2201 | "VkPhysicalDeviceSubgroupProperties::supportedStages", kVUID_Core_Shader_ExceedDeviceLimit); |
| 2202 | } |
| 2203 | |
| 2204 | if (!enabled_features.core12.shaderSubgroupExtendedTypes) { |
| 2205 | auto type = module->get_def(inst.word(1)); |
| 2206 | |
| 2207 | if (type.opcode() == spv::OpTypeVector) { |
| 2208 | // Get the element type |
| 2209 | type = module->get_def(type.word(2)); |
| 2210 | } |
| 2211 | |
| 2212 | if (type.opcode() == spv::OpTypeBool) { |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2213 | break; |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2214 | } |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2215 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2216 | // Both OpTypeInt and OpTypeFloat the width is in the 2nd word. |
| 2217 | const uint32_t width = type.word(2); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2218 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2219 | if ((type.opcode() == spv::OpTypeFloat && width == 16) || |
| 2220 | (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) { |
| 2221 | skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes, |
| 2222 | "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes", |
| 2223 | kVUID_Core_Shader_FeatureNotEnabled); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2224 | } |
| 2225 | } |
| 2226 | } |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | return skip; |
| 2230 | } |
| 2231 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2232 | bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2233 | const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2234 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS || |
| 2235 | pStage->stage == VK_SHADER_STAGE_ALL) { |
| 2236 | return false; |
| 2237 | } |
| 2238 | |
| 2239 | bool skip = false; |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2240 | auto const &limits = phys_dev_props.limits; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2241 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2242 | std::set<uint32_t> patch_i_ds; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2243 | struct Variable { |
| 2244 | uint32_t baseTypePtrID; |
| 2245 | uint32_t ID; |
| 2246 | uint32_t storageClass; |
| 2247 | }; |
| 2248 | std::vector<Variable> variables; |
| 2249 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2250 | uint32_t num_vertices = 0; |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 2251 | bool is_iso_lines = false; |
| 2252 | bool is_point_mode = false; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2253 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2254 | auto entrypoint_variables = FindEntrypointInterfaces(entrypoint); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2255 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2256 | for (auto insn : *src) { |
| 2257 | switch (insn.opcode()) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2258 | // Find all Patch decorations |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2259 | case spv::OpDecorate: |
| 2260 | switch (insn.word(2)) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2261 | case spv::DecorationPatch: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2262 | patch_i_ds.insert(insn.word(1)); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2263 | break; |
| 2264 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2265 | default: |
| 2266 | break; |
| 2267 | } |
| 2268 | break; |
| 2269 | // Find all input and output variables |
| 2270 | case spv::OpVariable: { |
| 2271 | Variable var = {}; |
| 2272 | var.storageClass = insn.word(3); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2273 | if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) && |
| 2274 | // Only include variables in the entrypoint's interface |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2275 | 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] | 2276 | var.baseTypePtrID = insn.word(1); |
| 2277 | var.ID = insn.word(2); |
| 2278 | variables.push_back(var); |
| 2279 | } |
| 2280 | break; |
| 2281 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2282 | case spv::OpExecutionMode: |
| 2283 | if (insn.word(1) == entrypoint.word(2)) { |
| 2284 | switch (insn.word(2)) { |
| 2285 | default: |
| 2286 | break; |
| 2287 | case spv::ExecutionModeOutputVertices: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2288 | num_vertices = insn.word(3); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2289 | break; |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 2290 | case spv::ExecutionModeIsolines: |
| 2291 | is_iso_lines = true; |
| 2292 | break; |
| 2293 | case spv::ExecutionModePointMode: |
| 2294 | is_point_mode = true; |
| 2295 | break; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2296 | } |
| 2297 | } |
| 2298 | break; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2299 | default: |
| 2300 | break; |
| 2301 | } |
| 2302 | } |
| 2303 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2304 | bool strip_output_array_level = |
| 2305 | (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV); |
| 2306 | bool strip_input_array_level = |
| 2307 | (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || |
| 2308 | pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT); |
| 2309 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2310 | uint32_t num_comp_in = 0, num_comp_out = 0; |
| 2311 | int max_comp_in = 0, max_comp_out = 0; |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2312 | |
| 2313 | auto inputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassInput, strip_input_array_level); |
| 2314 | auto outputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassOutput, strip_output_array_level); |
| 2315 | |
| 2316 | // Find max component location used for input variables. |
| 2317 | for (auto &var : inputs) { |
| 2318 | int location = var.first.first; |
| 2319 | int component = var.first.second; |
| 2320 | interface_var &iv = var.second; |
| 2321 | |
| 2322 | // Only need to look at the first location, since we use the type's whole size |
| 2323 | if (iv.offset != 0) { |
| 2324 | continue; |
| 2325 | } |
| 2326 | |
| 2327 | if (iv.is_patch) { |
| 2328 | continue; |
| 2329 | } |
| 2330 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2331 | int num_components = GetComponentsConsumedByType(src, iv.type_id, strip_input_array_level); |
| 2332 | 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] | 2333 | } |
| 2334 | |
| 2335 | // Find max component location used for output variables. |
| 2336 | for (auto &var : outputs) { |
| 2337 | int location = var.first.first; |
| 2338 | int component = var.first.second; |
| 2339 | interface_var &iv = var.second; |
| 2340 | |
| 2341 | // Only need to look at the first location, since we use the type's whole size |
| 2342 | if (iv.offset != 0) { |
| 2343 | continue; |
| 2344 | } |
| 2345 | |
| 2346 | if (iv.is_patch) { |
| 2347 | continue; |
| 2348 | } |
| 2349 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2350 | int num_components = GetComponentsConsumedByType(src, iv.type_id, strip_output_array_level); |
| 2351 | 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] | 2352 | } |
| 2353 | |
| 2354 | // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar), |
| 2355 | // but that doesn't include builtins. |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2356 | for (auto &var : variables) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2357 | // Check if the variable is a patch. Patches can also be members of blocks, |
| 2358 | // but if they are then the top-level arrayness has already been stripped |
| 2359 | // by the time GetComponentsConsumedByType gets to it. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2360 | 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] | 2361 | |
| 2362 | if (var.storageClass == spv::StorageClassInput) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2363 | num_comp_in += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_input_array_level && !is_patch); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2364 | } else { // var.storageClass == spv::StorageClassOutput |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2365 | num_comp_out += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_output_array_level && !is_patch); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | switch (pStage->stage) { |
| 2370 | case VK_SHADER_STAGE_VERTEX_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2371 | if (num_comp_out > limits.maxVertexOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2372 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2373 | "Invalid Pipeline CreateInfo State: Vertex shader exceeds " |
| 2374 | "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u " |
| 2375 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2376 | limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2377 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2378 | if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2379 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2380 | "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that " |
| 2381 | "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)", |
| 2382 | limits.maxVertexOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2383 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2384 | break; |
| 2385 | |
| 2386 | case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2387 | if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2388 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2389 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 2390 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u " |
| 2391 | "components by %u components", |
| 2392 | limits.maxTessellationControlPerVertexInputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2393 | num_comp_in - limits.maxTessellationControlPerVertexInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2394 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2395 | if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2396 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2397 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2398 | "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that " |
| 2399 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)", |
| 2400 | limits.maxTessellationControlPerVertexInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2401 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2402 | if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2403 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2404 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 2405 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u " |
| 2406 | "components by %u components", |
| 2407 | limits.maxTessellationControlPerVertexOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2408 | num_comp_out - limits.maxTessellationControlPerVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2409 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2410 | if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2411 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2412 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2413 | "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that " |
| 2414 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)", |
| 2415 | limits.maxTessellationControlPerVertexOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2416 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2417 | break; |
| 2418 | |
| 2419 | case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2420 | if (num_comp_in > limits.maxTessellationEvaluationInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2421 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2422 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 2423 | "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u " |
| 2424 | "components by %u components", |
| 2425 | limits.maxTessellationEvaluationInputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2426 | num_comp_in - limits.maxTessellationEvaluationInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2427 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2428 | if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2429 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2430 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2431 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that " |
| 2432 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)", |
| 2433 | limits.maxTessellationEvaluationInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2434 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2435 | if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2436 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2437 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 2438 | "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u " |
| 2439 | "components by %u components", |
| 2440 | limits.maxTessellationEvaluationOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2441 | num_comp_out - limits.maxTessellationEvaluationOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2442 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2443 | if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2444 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2445 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2446 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that " |
| 2447 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)", |
| 2448 | limits.maxTessellationEvaluationOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2449 | } |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 2450 | // Portability validation |
| 2451 | if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) { |
| 2452 | if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) { |
| 2453 | skip |= LogError(pipeline->pipeline, kVUID_Portability_Tessellation_Isolines, |
| 2454 | "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader" |
| 2455 | " is using abstract patch type IsoLines, but this is not supported on this platform"); |
| 2456 | } |
| 2457 | if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) { |
| 2458 | skip |= LogError(pipeline->pipeline, kVUID_Portability_Tessellation_PointMode, |
| 2459 | "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader" |
| 2460 | " is using abstract patch type PointMode, but this is not supported on this platform"); |
| 2461 | } |
| 2462 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2463 | break; |
| 2464 | |
| 2465 | case VK_SHADER_STAGE_GEOMETRY_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2466 | if (num_comp_in > limits.maxGeometryInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2467 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2468 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2469 | "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u " |
| 2470 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2471 | limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2472 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2473 | if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2474 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2475 | "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that " |
| 2476 | "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)", |
| 2477 | limits.maxGeometryInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2478 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2479 | if (num_comp_out > limits.maxGeometryOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2480 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2481 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2482 | "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u " |
| 2483 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2484 | limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2485 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2486 | if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2487 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2488 | "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that " |
| 2489 | "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)", |
| 2490 | limits.maxGeometryOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2491 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2492 | if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2493 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2494 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2495 | "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u " |
| 2496 | "components by %u components", |
| 2497 | limits.maxGeometryTotalOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2498 | num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2499 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2500 | break; |
| 2501 | |
| 2502 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2503 | if (num_comp_in > limits.maxFragmentInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2504 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2505 | "Invalid Pipeline CreateInfo State: Fragment shader exceeds " |
| 2506 | "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u " |
| 2507 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2508 | limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2509 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2510 | if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2511 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2512 | "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that " |
| 2513 | "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)", |
| 2514 | limits.maxFragmentInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2515 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2516 | break; |
| 2517 | |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2518 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 2519 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 2520 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 2521 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 2522 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 2523 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 2524 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 2525 | case VK_SHADER_STAGE_MESH_BIT_NV: |
| 2526 | break; |
| 2527 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2528 | default: |
| 2529 | assert(false); // This should never happen |
| 2530 | } |
| 2531 | return skip; |
| 2532 | } |
| 2533 | |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 2534 | bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const { |
| 2535 | bool skip = false; |
| 2536 | uint32_t total_resources = 0; |
| 2537 | |
| 2538 | // Only currently testing for graphics and compute pipelines |
| 2539 | // TODO: Add check and support for Ray Tracing pipeline VUID 03428 |
| 2540 | if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) { |
| 2541 | return false; |
| 2542 | } |
| 2543 | |
| 2544 | if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 2545 | // "For the fragment shader stage the framebuffer color attachments also count against this limit" |
| 2546 | total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].colorAttachmentCount; |
| 2547 | } |
| 2548 | |
| 2549 | // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle |
| 2550 | // input from CreatePipeline and CreatePipelineLayout level |
| 2551 | for (auto set_layout : pipeline->pipeline_layout->set_layouts) { |
| 2552 | if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) { |
| 2553 | continue; |
| 2554 | } |
| 2555 | |
| 2556 | for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) { |
| 2557 | const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx); |
| 2558 | // Bindings with a descriptorCount of 0 are "reserved" and should be skipped |
| 2559 | if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) { |
| 2560 | // Check only descriptor types listed in maxPerStageResources description in spec |
| 2561 | switch (binding->descriptorType) { |
| 2562 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 2563 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 2564 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 2565 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 2566 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 2567 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 2568 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 2569 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 2570 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 2571 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 2572 | total_resources += binding->descriptorCount; |
| 2573 | break; |
| 2574 | default: |
| 2575 | break; |
| 2576 | } |
| 2577 | } |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | if (total_resources > phys_dev_props.limits.maxPerStageResources) { |
| 2582 | const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687" |
| 2583 | : "VUID-VkGraphicsPipelineCreateInfo-layout-01688"; |
| 2584 | skip |= LogError(pipeline->pipeline, vuid, |
| 2585 | "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit " |
| 2586 | "VkPhysicalDeviceLimits::maxPerStageResources (%u)", |
| 2587 | string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources); |
| 2588 | } |
| 2589 | |
| 2590 | return skip; |
| 2591 | } |
| 2592 | |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2593 | // copy the specialization constant value into buf, if it is present |
| 2594 | void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) { |
| 2595 | VkSpecializationInfo const *spec = pStage->pSpecializationInfo; |
| 2596 | |
| 2597 | if (spec && spec_id < spec->mapEntryCount) { |
| 2598 | memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size); |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | // Fill in value with the constant or specialization constant value, if available. |
| 2603 | // Returns true if the value has been accurately filled out. |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2604 | static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2605 | const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) { |
| 2606 | auto type_id = src->get_def(insn.word(1)); |
| 2607 | if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) { |
| 2608 | return false; |
| 2609 | } |
| 2610 | switch (insn.opcode()) { |
| 2611 | case spv::OpSpecConstant: |
| 2612 | *value = insn.word(3); |
| 2613 | GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value); |
| 2614 | return true; |
| 2615 | case spv::OpConstant: |
| 2616 | *value = insn.word(3); |
| 2617 | return true; |
| 2618 | default: |
| 2619 | return false; |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | // Map SPIR-V type to VK_COMPONENT_TYPE enum |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2624 | VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2625 | switch (insn.opcode()) { |
| 2626 | case spv::OpTypeInt: |
| 2627 | switch (insn.word(2)) { |
| 2628 | case 8: |
| 2629 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV; |
| 2630 | case 16: |
| 2631 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV; |
| 2632 | case 32: |
| 2633 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV; |
| 2634 | case 64: |
| 2635 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV; |
| 2636 | default: |
| 2637 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2638 | } |
| 2639 | case spv::OpTypeFloat: |
| 2640 | switch (insn.word(2)) { |
| 2641 | case 16: |
| 2642 | return VK_COMPONENT_TYPE_FLOAT16_NV; |
| 2643 | case 32: |
| 2644 | return VK_COMPONENT_TYPE_FLOAT32_NV; |
| 2645 | case 64: |
| 2646 | return VK_COMPONENT_TYPE_FLOAT64_NV; |
| 2647 | default: |
| 2648 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2649 | } |
| 2650 | default: |
| 2651 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | // Validate SPV_NV_cooperative_matrix behavior that can't be statically validated |
| 2656 | // in SPIRV-Tools (e.g. due to specialization constant usage). |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2657 | bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2658 | const PIPELINE_STATE *pipeline) const { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2659 | bool skip = false; |
| 2660 | |
| 2661 | // Map SPIR-V result ID to specialization constant id (SpecId decoration value) |
| 2662 | std::unordered_map<uint32_t, uint32_t> id_to_spec_id; |
| 2663 | // Map SPIR-V result ID to the ID of its type. |
| 2664 | std::unordered_map<uint32_t, uint32_t> id_to_type_id; |
| 2665 | |
| 2666 | struct CoopMatType { |
| 2667 | uint32_t scope, rows, cols; |
| 2668 | VkComponentTypeNV component_type; |
| 2669 | bool all_constant; |
| 2670 | |
| 2671 | CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {} |
| 2672 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2673 | void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2674 | const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id) { |
| 2675 | spirv_inst_iter insn = src->get_def(id); |
| 2676 | uint32_t component_type_id = insn.word(2); |
| 2677 | uint32_t scope_id = insn.word(3); |
| 2678 | uint32_t rows_id = insn.word(4); |
| 2679 | uint32_t cols_id = insn.word(5); |
| 2680 | auto component_type_iter = src->get_def(component_type_id); |
| 2681 | auto scope_iter = src->get_def(scope_id); |
| 2682 | auto rows_iter = src->get_def(rows_id); |
| 2683 | auto cols_iter = src->get_def(cols_id); |
| 2684 | |
| 2685 | all_constant = true; |
| 2686 | if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) { |
| 2687 | all_constant = false; |
| 2688 | } |
| 2689 | if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) { |
| 2690 | all_constant = false; |
| 2691 | } |
| 2692 | if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) { |
| 2693 | all_constant = false; |
| 2694 | } |
| 2695 | component_type = GetComponentType(component_type_iter, src); |
| 2696 | } |
| 2697 | }; |
| 2698 | |
| 2699 | bool seen_coopmat_capability = false; |
| 2700 | |
| 2701 | for (auto insn : *src) { |
| 2702 | // Whitelist instructions whose result can be a cooperative matrix type, and |
| 2703 | // keep track of their types. It would be nice if SPIRV-Headers generated code |
| 2704 | // to identify which instructions have a result type and result id. Lacking that, |
| 2705 | // this whitelist is based on the set of instructions that |
| 2706 | // SPV_NV_cooperative_matrix says can be used with cooperative matrix types. |
| 2707 | switch (insn.opcode()) { |
| 2708 | case spv::OpLoad: |
| 2709 | case spv::OpCooperativeMatrixLoadNV: |
| 2710 | case spv::OpCooperativeMatrixMulAddNV: |
| 2711 | case spv::OpSNegate: |
| 2712 | case spv::OpFNegate: |
| 2713 | case spv::OpIAdd: |
| 2714 | case spv::OpFAdd: |
| 2715 | case spv::OpISub: |
| 2716 | case spv::OpFSub: |
| 2717 | case spv::OpFDiv: |
| 2718 | case spv::OpSDiv: |
| 2719 | case spv::OpUDiv: |
| 2720 | case spv::OpMatrixTimesScalar: |
| 2721 | case spv::OpConstantComposite: |
| 2722 | case spv::OpCompositeConstruct: |
| 2723 | case spv::OpConvertFToU: |
| 2724 | case spv::OpConvertFToS: |
| 2725 | case spv::OpConvertSToF: |
| 2726 | case spv::OpConvertUToF: |
| 2727 | case spv::OpUConvert: |
| 2728 | case spv::OpSConvert: |
| 2729 | case spv::OpFConvert: |
| 2730 | id_to_type_id[insn.word(2)] = insn.word(1); |
| 2731 | break; |
| 2732 | default: |
| 2733 | break; |
| 2734 | } |
| 2735 | |
| 2736 | switch (insn.opcode()) { |
| 2737 | case spv::OpDecorate: |
| 2738 | if (insn.word(2) == spv::DecorationSpecId) { |
| 2739 | id_to_spec_id[insn.word(1)] = insn.word(3); |
| 2740 | } |
| 2741 | break; |
| 2742 | case spv::OpCapability: |
| 2743 | if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) { |
| 2744 | seen_coopmat_capability = true; |
| 2745 | |
| 2746 | if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2747 | skip |= LogError( |
| 2748 | pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixSupportedStages, |
| 2749 | "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)", |
| 2750 | phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2751 | } |
| 2752 | } |
| 2753 | break; |
| 2754 | case spv::OpMemoryModel: |
| 2755 | // If the capability isn't enabled, don't bother with the rest of this function. |
| 2756 | // OpMemoryModel is the first required instruction after all OpCapability instructions. |
| 2757 | if (!seen_coopmat_capability) { |
| 2758 | return skip; |
| 2759 | } |
| 2760 | break; |
| 2761 | case spv::OpTypeCooperativeMatrixNV: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2762 | CoopMatType m; |
| 2763 | m.Init(insn.word(1), src, pStage, id_to_spec_id); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2764 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2765 | if (m.all_constant) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2766 | // Validate that the type parameters are all supported for one of the |
| 2767 | // operands of a cooperative matrix property. |
| 2768 | bool valid = false; |
| 2769 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2770 | if (cooperative_matrix_properties[i].AType == m.component_type && |
| 2771 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols && |
| 2772 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2773 | valid = true; |
| 2774 | break; |
| 2775 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2776 | if (cooperative_matrix_properties[i].BType == m.component_type && |
| 2777 | cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 2778 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2779 | valid = true; |
| 2780 | break; |
| 2781 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2782 | if (cooperative_matrix_properties[i].CType == m.component_type && |
| 2783 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 2784 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2785 | valid = true; |
| 2786 | break; |
| 2787 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2788 | if (cooperative_matrix_properties[i].DType == m.component_type && |
| 2789 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 2790 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2791 | valid = true; |
| 2792 | break; |
| 2793 | } |
| 2794 | } |
| 2795 | if (!valid) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2796 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixType, |
| 2797 | "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type", |
| 2798 | insn.word(1)); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2799 | } |
| 2800 | } |
| 2801 | break; |
| 2802 | } |
| 2803 | case spv::OpCooperativeMatrixMulAddNV: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2804 | CoopMatType a, b, c, d; |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2805 | if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() || |
| 2806 | id_to_type_id.find(insn.word(3)) == id_to_type_id.end() || |
| 2807 | id_to_type_id.find(insn.word(4)) == id_to_type_id.end() || |
| 2808 | 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] | 2809 | // Couldn't find type of matrix |
| 2810 | assert(false); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2811 | break; |
| 2812 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2813 | d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id); |
| 2814 | a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id); |
| 2815 | b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id); |
| 2816 | 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] | 2817 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2818 | 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] | 2819 | // Validate that the type parameters are all supported for the same |
| 2820 | // cooperative matrix property. |
| 2821 | bool valid = false; |
| 2822 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2823 | if (cooperative_matrix_properties[i].AType == a.component_type && |
| 2824 | cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols && |
| 2825 | cooperative_matrix_properties[i].scope == a.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2826 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2827 | cooperative_matrix_properties[i].BType == b.component_type && |
| 2828 | cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols && |
| 2829 | cooperative_matrix_properties[i].scope == b.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2830 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2831 | cooperative_matrix_properties[i].CType == c.component_type && |
| 2832 | cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols && |
| 2833 | cooperative_matrix_properties[i].scope == c.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2834 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2835 | cooperative_matrix_properties[i].DType == d.component_type && |
| 2836 | cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols && |
| 2837 | cooperative_matrix_properties[i].scope == d.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2838 | valid = true; |
| 2839 | break; |
| 2840 | } |
| 2841 | } |
| 2842 | if (!valid) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2843 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixMulAdd, |
| 2844 | "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix " |
| 2845 | "VkCooperativeMatrixPropertiesNV", |
| 2846 | insn.word(2)); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2847 | } |
| 2848 | } |
| 2849 | break; |
| 2850 | } |
| 2851 | default: |
| 2852 | break; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | return skip; |
| 2857 | } |
| 2858 | |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2859 | 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] | 2860 | auto entrypoint_id = entrypoint.word(2); |
| 2861 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2862 | // The first denorm execution mode encountered, along with its bit width. |
| 2863 | // Used to check if SeparateDenormSettings is respected. |
| 2864 | 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] | 2865 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2866 | // The first rounding mode encountered, along with its bit width. |
| 2867 | // Used to check if SeparateRoundingModeSettings is respected. |
| 2868 | 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] | 2869 | |
| 2870 | bool skip = false; |
| 2871 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2872 | uint32_t vertices_out = 0; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2873 | uint32_t invocations = 0; |
| 2874 | |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2875 | for (auto insn : *src) { |
| 2876 | if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) { |
| 2877 | auto mode = insn.word(2); |
| 2878 | switch (mode) { |
| 2879 | case spv::ExecutionModeSignedZeroInfNanPreserve: { |
| 2880 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2881 | if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) || |
| 2882 | (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) || |
| 2883 | (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2884 | skip |= LogError( |
| 2885 | device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2886 | "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device", |
| 2887 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2888 | } |
| 2889 | break; |
| 2890 | } |
| 2891 | |
| 2892 | case spv::ExecutionModeDenormPreserve: { |
| 2893 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2894 | if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) || |
| 2895 | (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) || |
| 2896 | (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2897 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2898 | "Shader requires DenormPreserve for bit width %d but it is not enabled on the device", |
| 2899 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2900 | } |
| 2901 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2902 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 2903 | // Register the first denorm execution mode found |
| 2904 | 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] | 2905 | } 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] | 2906 | switch (phys_dev_props_core12.denormBehaviorIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2907 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2908 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2909 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2910 | "Shader uses different denorm execution modes for 16 and 64-bit but " |
| 2911 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2912 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2913 | } |
| 2914 | break; |
| 2915 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2916 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2917 | break; |
| 2918 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2919 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2920 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2921 | "Shader uses different denorm execution modes for different bit widths but " |
| 2922 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2923 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2924 | break; |
| 2925 | |
| 2926 | default: |
| 2927 | break; |
| 2928 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2929 | } |
| 2930 | break; |
| 2931 | } |
| 2932 | |
| 2933 | case spv::ExecutionModeDenormFlushToZero: { |
| 2934 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2935 | if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) || |
| 2936 | (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) || |
| 2937 | (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2938 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2939 | "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device", |
| 2940 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2943 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 2944 | // Register the first denorm execution mode found |
| 2945 | 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] | 2946 | } 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] | 2947 | switch (phys_dev_props_core12.denormBehaviorIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2948 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2949 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2950 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2951 | "Shader uses different denorm execution modes for 16 and 64-bit but " |
| 2952 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2953 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2954 | } |
| 2955 | break; |
| 2956 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2957 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2958 | break; |
| 2959 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2960 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2961 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2962 | "Shader uses different denorm execution modes for different bit widths but " |
| 2963 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2964 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2965 | break; |
| 2966 | |
| 2967 | default: |
| 2968 | break; |
| 2969 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2970 | } |
| 2971 | break; |
| 2972 | } |
| 2973 | |
| 2974 | case spv::ExecutionModeRoundingModeRTE: { |
| 2975 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2976 | if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) || |
| 2977 | (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) || |
| 2978 | (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2979 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2980 | "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device", |
| 2981 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2984 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 2985 | // Register the first rounding mode found |
| 2986 | 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] | 2987 | } 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] | 2988 | switch (phys_dev_props_core12.roundingModeIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2989 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2990 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2991 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2992 | "Shader uses different rounding modes for 16 and 64-bit but " |
| 2993 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2994 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2995 | } |
| 2996 | break; |
| 2997 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2998 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2999 | break; |
| 3000 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3001 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3002 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3003 | "Shader uses different rounding modes for different bit widths but " |
| 3004 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3005 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3006 | break; |
| 3007 | |
| 3008 | default: |
| 3009 | break; |
| 3010 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3011 | } |
| 3012 | break; |
| 3013 | } |
| 3014 | |
| 3015 | case spv::ExecutionModeRoundingModeRTZ: { |
| 3016 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3017 | if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) || |
| 3018 | (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) || |
| 3019 | (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3020 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3021 | "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device", |
| 3022 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3025 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 3026 | // Register the first rounding mode found |
| 3027 | 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] | 3028 | } 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] | 3029 | switch (phys_dev_props_core12.roundingModeIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3030 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3031 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3032 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3033 | "Shader uses different rounding modes for 16 and 64-bit but " |
| 3034 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3035 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3036 | } |
| 3037 | break; |
| 3038 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3039 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3040 | break; |
| 3041 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3042 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3043 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3044 | "Shader uses different rounding modes for different bit widths but " |
| 3045 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3046 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3047 | break; |
| 3048 | |
| 3049 | default: |
| 3050 | break; |
| 3051 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3052 | } |
| 3053 | break; |
| 3054 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3055 | |
| 3056 | case spv::ExecutionModeOutputVertices: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3057 | vertices_out = insn.word(3); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3058 | break; |
| 3059 | } |
| 3060 | |
| 3061 | case spv::ExecutionModeInvocations: { |
| 3062 | invocations = insn.word(3); |
| 3063 | break; |
| 3064 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3065 | } |
| 3066 | } |
| 3067 | } |
| 3068 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3069 | if (entrypoint.word(1) == spv::ExecutionModelGeometry) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3070 | if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3071 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714", |
| 3072 | "Geometry shader entry point must have an OpExecutionMode instruction that " |
| 3073 | "specifies a maximum output vertex count that is greater than 0 and less " |
| 3074 | "than or equal to maxGeometryOutputVertices. " |
| 3075 | "OutputVertices=%d, maxGeometryOutputVertices=%d", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3076 | vertices_out, phys_dev_props.limits.maxGeometryOutputVertices); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3080 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715", |
| 3081 | "Geometry shader entry point must have an OpExecutionMode instruction that " |
| 3082 | "specifies an invocation count that is greater than 0 and less " |
| 3083 | "than or equal to maxGeometryShaderInvocations. " |
| 3084 | "Invocations=%d, maxGeometryShaderInvocations=%d", |
| 3085 | invocations, phys_dev_props.limits.maxGeometryShaderInvocations); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3086 | } |
| 3087 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3088 | return skip; |
| 3089 | } |
| 3090 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 3091 | uint32_t DescriptorTypeToReqs(SHADER_MODULE_STATE const *module, uint32_t type_id) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3092 | auto type = module->get_def(type_id); |
| 3093 | |
| 3094 | while (true) { |
| 3095 | switch (type.opcode()) { |
| 3096 | case spv::OpTypeArray: |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 3097 | case spv::OpTypeRuntimeArray: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3098 | case spv::OpTypeSampledImage: |
| 3099 | type = module->get_def(type.word(2)); |
| 3100 | break; |
| 3101 | case spv::OpTypePointer: |
| 3102 | type = module->get_def(type.word(3)); |
| 3103 | break; |
| 3104 | case spv::OpTypeImage: { |
| 3105 | auto dim = type.word(3); |
| 3106 | auto arrayed = type.word(5); |
| 3107 | auto msaa = type.word(6); |
| 3108 | |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3109 | uint32_t bits = 0; |
| 3110 | switch (GetFundamentalType(module, type.word(2))) { |
| 3111 | case FORMAT_TYPE_FLOAT: |
| 3112 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT; |
| 3113 | break; |
| 3114 | case FORMAT_TYPE_UINT: |
| 3115 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT; |
| 3116 | break; |
| 3117 | case FORMAT_TYPE_SINT: |
| 3118 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT; |
| 3119 | break; |
| 3120 | default: |
| 3121 | break; |
| 3122 | } |
| 3123 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3124 | switch (dim) { |
| 3125 | case spv::Dim1D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3126 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D; |
| 3127 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3128 | case spv::Dim2D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3129 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 3130 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D; |
| 3131 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3132 | case spv::Dim3D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3133 | bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D; |
| 3134 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3135 | case spv::DimCube: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3136 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE; |
| 3137 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3138 | case spv::DimSubpassData: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3139 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 3140 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3141 | default: // buffer, etc. |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3142 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3143 | } |
| 3144 | } |
| 3145 | default: |
| 3146 | return 0; |
| 3147 | } |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | // For given pipelineLayout verify that the set_layout_node at slot.first |
| 3152 | // 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] | 3153 | static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3154 | descriptor_slot_t slot) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3155 | if (!pipelineLayout) return nullptr; |
| 3156 | |
| 3157 | if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr; |
| 3158 | |
| 3159 | return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second); |
| 3160 | } |
| 3161 | |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 3162 | int32_t GetShaderResourceDimensionality(const SHADER_MODULE_STATE *module, const interface_var &resource) { |
| 3163 | if (module == nullptr) return -1; |
| 3164 | |
| 3165 | auto type = module->get_def(resource.type_id); |
| 3166 | while (true) { |
| 3167 | switch (type.opcode()) { |
| 3168 | case spv::OpTypeSampledImage: |
| 3169 | type = module->get_def(type.word(2)); |
| 3170 | break; |
| 3171 | case spv::OpTypePointer: |
| 3172 | type = module->get_def(type.word(3)); |
| 3173 | break; |
| 3174 | case spv::OpTypeImage: |
| 3175 | return type.word(3); |
| 3176 | default: |
| 3177 | return -1; |
| 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | bool FindLocalSize(SHADER_MODULE_STATE const *src, uint32_t &local_size_x, uint32_t &local_size_y, uint32_t &local_size_z) { |
Locke | 1ec6d95 | 2019-04-02 11:57:21 -0600 | [diff] [blame] | 3183 | for (auto insn : *src) { |
| 3184 | if (insn.opcode() == spv::OpEntryPoint) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3185 | auto execution_model = insn.word(1); |
| 3186 | auto entrypoint_stage_bits = ExecutionModelToShaderStageFlagBits(execution_model); |
| 3187 | if (entrypoint_stage_bits == VK_SHADER_STAGE_COMPUTE_BIT) { |
Locke | 1ec6d95 | 2019-04-02 11:57:21 -0600 | [diff] [blame] | 3188 | auto entrypoint_id = insn.word(2); |
| 3189 | for (auto insn1 : *src) { |
| 3190 | if (insn1.opcode() == spv::OpExecutionMode && insn1.word(1) == entrypoint_id && |
| 3191 | insn1.word(2) == spv::ExecutionModeLocalSize) { |
| 3192 | local_size_x = insn1.word(3); |
| 3193 | local_size_y = insn1.word(4); |
| 3194 | local_size_z = insn1.word(5); |
| 3195 | return true; |
| 3196 | } |
| 3197 | } |
| 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | return false; |
| 3202 | } |
| 3203 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 3204 | void ProcessExecutionModes(SHADER_MODULE_STATE const *src, const spirv_inst_iter &entrypoint, PIPELINE_STATE *pipeline) { |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 3205 | auto entrypoint_id = entrypoint.word(2); |
Chris Forbes | 0771b67 | 2018-03-22 21:13:46 -0700 | [diff] [blame] | 3206 | bool is_point_mode = false; |
| 3207 | |
| 3208 | for (auto insn : *src) { |
| 3209 | if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) { |
| 3210 | switch (insn.word(2)) { |
| 3211 | case spv::ExecutionModePointMode: |
| 3212 | // In tessellation shaders, PointMode is separate and trumps the tessellation topology. |
| 3213 | is_point_mode = true; |
| 3214 | break; |
| 3215 | |
| 3216 | case spv::ExecutionModeOutputPoints: |
| 3217 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 3218 | break; |
| 3219 | |
| 3220 | case spv::ExecutionModeIsolines: |
| 3221 | case spv::ExecutionModeOutputLineStrip: |
| 3222 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; |
| 3223 | break; |
| 3224 | |
| 3225 | case spv::ExecutionModeTriangles: |
| 3226 | case spv::ExecutionModeQuads: |
| 3227 | case spv::ExecutionModeOutputTriangleStrip: |
| 3228 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
| 3229 | break; |
| 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | if (is_point_mode) pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 3235 | } |
| 3236 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3237 | // If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize |
| 3238 | // o If there is only a vertex shader : gl_PointSize must be written when using points |
| 3239 | // o If there is a geometry or tessellation shader: |
| 3240 | // - If shaderTessellationAndGeometryPointSize feature is enabled: |
| 3241 | // * gl_PointSize must be written in the final geometry stage |
| 3242 | // - If shaderTessellationAndGeometryPointSize feature is disabled: |
| 3243 | // * 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] | 3244 | bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3245 | spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3246 | if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 3247 | return false; |
| 3248 | } |
| 3249 | |
| 3250 | bool pointsize_written = false; |
| 3251 | bool skip = false; |
| 3252 | |
| 3253 | // Search for PointSize built-in decorations |
| 3254 | std::vector<uint32_t> pointsize_builtin_offsets; |
| 3255 | spirv_inst_iter insn = entrypoint; |
| 3256 | while (!pointsize_written && (insn.opcode() != spv::OpFunction)) { |
| 3257 | if (insn.opcode() == spv::OpMemberDecorate) { |
| 3258 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 3259 | if (insn.word(4) == spv::BuiltInPointSize) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3260 | pointsize_written = IsBuiltInWritten(src, insn, entrypoint); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3261 | } |
| 3262 | } |
| 3263 | } else if (insn.opcode() == spv::OpDecorate) { |
| 3264 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 3265 | if (insn.word(3) == spv::BuiltInPointSize) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3266 | pointsize_written = IsBuiltInWritten(src, insn, entrypoint); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3267 | } |
| 3268 | } |
| 3269 | } |
| 3270 | |
| 3271 | insn++; |
| 3272 | } |
| 3273 | |
| 3274 | 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] | 3275 | !enabled_features.core.shaderTessellationAndGeometryPointSize) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3276 | if (pointsize_written) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3277 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_PointSizeBuiltInOverSpecified, |
| 3278 | "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which " |
| 3279 | "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled."); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3280 | } |
| 3281 | } else if (!pointsize_written) { |
| 3282 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3283 | LogError(pipeline->pipeline, kVUID_Core_Shader_MissingPointSizeBuiltIn, |
| 3284 | "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.", |
| 3285 | string_VkShaderStageFlagBits(stage)); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3286 | } |
| 3287 | return skip; |
| 3288 | } |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3289 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3290 | bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src, |
| 3291 | spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const { |
| 3292 | bool primitiverate_written = false; |
| 3293 | bool viewportindex_written = false; |
| 3294 | bool viewportmask_written = false; |
| 3295 | bool skip = false; |
| 3296 | |
| 3297 | // Check if the primitive shading rate is written |
| 3298 | spirv_inst_iter insn = entrypoint; |
| 3299 | while (!(primitiverate_written && viewportindex_written && viewportmask_written) && insn.opcode() != spv::OpFunction) { |
| 3300 | if (insn.opcode() == spv::OpMemberDecorate) { |
| 3301 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 3302 | if (insn.word(4) == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3303 | primitiverate_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3304 | } else if (insn.word(4) == spv::BuiltInViewportIndex) { |
| 3305 | viewportindex_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3306 | } else if (insn.word(4) == spv::BuiltInViewportMaskNV) { |
| 3307 | viewportmask_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3308 | } |
| 3309 | } |
| 3310 | } else if (insn.opcode() == spv::OpDecorate) { |
| 3311 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 3312 | if (insn.word(3) == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3313 | primitiverate_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3314 | } else if (insn.word(3) == spv::BuiltInViewportIndex) { |
| 3315 | viewportindex_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3316 | } else if (insn.word(3) == spv::BuiltInViewportMaskNV) { |
| 3317 | viewportmask_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3318 | } |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | insn++; |
| 3323 | } |
| 3324 | |
| 3325 | if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports) { |
| 3326 | if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && |
| 3327 | pipeline->graphicsPipelineCI.pViewportState->viewportCount > 1 && primitiverate_written) { |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 3328 | skip |= LogError(pipeline->pipeline, |
| 3329 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503", |
| 3330 | "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but " |
| 3331 | "multiple viewports " |
| 3332 | "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3333 | string_VkShaderStageFlagBits(stage)); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | if (primitiverate_written && viewportindex_written) { |
| 3337 | skip |= LogError(pipeline->pipeline, |
| 3338 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504", |
| 3339 | "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and " |
| 3340 | "ViewportIndex built-ins," |
| 3341 | "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3342 | string_VkShaderStageFlagBits(stage)); |
| 3343 | } |
| 3344 | |
| 3345 | if (primitiverate_written && viewportmask_written) { |
| 3346 | skip |= LogError(pipeline->pipeline, |
| 3347 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505", |
| 3348 | "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and " |
| 3349 | "ViewportMaskNV built-ins," |
| 3350 | "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3351 | string_VkShaderStageFlagBits(stage)); |
| 3352 | } |
| 3353 | } |
| 3354 | return skip; |
| 3355 | } |
| 3356 | |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame^] | 3357 | // Validate runtime usage of various opcodes that depends on what Vulkan properties or features are exposed |
| 3358 | bool CoreChecks::ValidatePropertiesAndFeatures(SHADER_MODULE_STATE const *module) const { |
| 3359 | bool skip = false; |
| 3360 | |
| 3361 | for (auto insn : *module) { |
| 3362 | switch (insn.opcode()) { |
| 3363 | case spv::OpReadClockKHR: { |
| 3364 | auto scope_id = module->get_def(insn.word(3)); |
| 3365 | auto scope_type = scope_id.word(3); |
| 3366 | // if scope isn't Subgroup or Device, spirv-val will catch |
| 3367 | if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_feature.shaderSubgroupClock == VK_FALSE)) { |
| 3368 | skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderSubgroupClock", |
| 3369 | "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.", |
| 3370 | report_data->FormatHandle(module->vk_shader_module).c_str()); |
| 3371 | } else if ((scope_type == spv::ScopeDevice) && |
| 3372 | (enabled_features.shader_clock_feature.shaderDeviceClock == VK_FALSE)) { |
| 3373 | skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderDeviceClock", |
| 3374 | "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.", |
| 3375 | report_data->FormatHandle(module->vk_shader_module).c_str()); |
| 3376 | } |
| 3377 | break; |
| 3378 | } |
| 3379 | } |
| 3380 | } |
| 3381 | return skip; |
| 3382 | } |
| 3383 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3384 | bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline, |
| 3385 | const PIPELINE_STATE::StageState &stage_state, const SHADER_MODULE_STATE *module, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3386 | const spirv_inst_iter &entrypoint, bool check_point_size) const { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3387 | bool skip = false; |
| 3388 | |
| 3389 | // Check the module |
| 3390 | if (!module->has_valid_spirv) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3391 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", |
| 3392 | "%s does not contain valid spirv for stage %s.", |
| 3393 | 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] | 3394 | } |
| 3395 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3396 | // If specialization-constant values are given and specialization-constant instructions are present in the shader, the |
| 3397 | // specializations should be applied and validated. |
| 3398 | if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 && |
| 3399 | pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) { |
| 3400 | // Gather the specialization-constant values. |
| 3401 | auto const &specialization_info = pStage->pSpecializationInfo; |
Jeremy Hayes | 521221d | 2020-01-15 16:48:49 -0700 | [diff] [blame] | 3402 | auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3403 | std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; |
| 3404 | id_value_map.reserve(specialization_info->mapEntryCount); |
| 3405 | for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) { |
| 3406 | auto const &map_entry = specialization_info->pMapEntries[i]; |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3407 | |
Jeremy Hayes | 521221d | 2020-01-15 16:48:49 -0700 | [diff] [blame] | 3408 | // Expect only scalar types. |
| 3409 | assert(map_entry.size == 1 || map_entry.size == 2 || map_entry.size == 4 || map_entry.size == 8); |
| 3410 | auto entry = id_value_map.emplace(map_entry.constantID, std::vector<uint32_t>(map_entry.size > 4 ? 2 : 1)); |
| 3411 | memcpy(entry.first->second.data(), specialization_data + map_entry.offset, map_entry.size); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | // Apply the specialization-constant values and revalidate the shader module. |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 3415 | 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] | 3416 | spvtools::Optimizer optimizer(spirv_environment); |
| 3417 | spvtools::MessageConsumer consumer = [&skip, &module, &pStage, this](spv_message_level_t level, const char *source, |
| 3418 | const spv_position_t &position, const char *message) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3419 | skip |= LogError( |
| 3420 | device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s. %s", |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3421 | report_data->FormatHandle(module->vk_shader_module).c_str(), string_VkShaderStageFlagBits(pStage->stage), message); |
| 3422 | }; |
| 3423 | optimizer.SetMessageConsumer(consumer); |
| 3424 | optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map)); |
| 3425 | optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass()); |
| 3426 | std::vector<uint32_t> specialized_spirv; |
| 3427 | auto const optimized = |
| 3428 | optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, spvtools::ValidatorOptions(), true); |
| 3429 | assert(optimized == true); |
| 3430 | |
| 3431 | if (optimized) { |
| 3432 | spv_context ctx = spvContextCreate(spirv_environment); |
| 3433 | spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()}; |
| 3434 | spv_diagnostic diag = nullptr; |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 3435 | spvtools::ValidatorOptions options; |
| 3436 | AdjustValidatorOptions(device_extensions, enabled_features, options); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3437 | auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
| 3438 | if (spv_valid != SPV_SUCCESS) { |
sfricke-samsung | d379380 | 2020-08-18 22:55:03 -0700 | [diff] [blame] | 3439 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3440 | "After specialization was applied, %s does not contain valid spirv for stage %s.", |
| 3441 | report_data->FormatHandle(module->vk_shader_module).c_str(), |
| 3442 | string_VkShaderStageFlagBits(pStage->stage)); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3443 | } |
| 3444 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3445 | spvDiagnosticDestroy(diag); |
| 3446 | spvContextDestroy(ctx); |
| 3447 | } |
| 3448 | } |
| 3449 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3450 | // Check the entrypoint |
| 3451 | if (entrypoint == module->end()) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3452 | skip |= |
| 3453 | LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s..", |
| 3454 | pStage->pName, string_VkShaderStageFlagBits(pStage->stage)); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3455 | } |
| 3456 | if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage. |
| 3457 | |
| 3458 | // Mark accessible ids |
| 3459 | auto &accessible_ids = stage_state.accessible_ids; |
| 3460 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3461 | // Validate descriptor set layout against what the entrypoint actually uses |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3462 | bool has_writable_descriptor = stage_state.has_writable_descriptor; |
| 3463 | auto &descriptor_uses = stage_state.descriptor_uses; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3464 | |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 3465 | // Validate shader capabilities against enabled device features |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 3466 | skip |= ValidateShaderCapabilitiesAndExtensions(module); |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 3467 | skip |= |
| 3468 | ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, has_writable_descriptor, stage_state.has_atomic_descriptor); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3469 | skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint); |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 3470 | skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 3471 | skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3472 | skip |= ValidateExecutionModes(module, entrypoint); |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3473 | skip |= ValidateSpecializationOffsets(pStage); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 3474 | skip |= ValidatePushConstantUsage(*pipeline, module, pStage); |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 3475 | if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) { |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 3476 | skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3477 | } |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 3478 | skip |= ValidateBuiltinLimits(module, accessible_ids, pStage->stage); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 3479 | skip |= ValidateCooperativeMatrix(module, pStage, pipeline); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3480 | if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) { |
| 3481 | skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage); |
| 3482 | } |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame^] | 3483 | skip |= ValidatePropertiesAndFeatures(module); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3484 | |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3485 | std::string vuid_layout_mismatch; |
| 3486 | if (pipeline->graphicsPipelineCI.sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) { |
| 3487 | vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756"; |
| 3488 | } else if (pipeline->computePipelineCI.sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO) { |
| 3489 | vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703"; |
| 3490 | } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR) { |
| 3491 | vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427"; |
| 3492 | } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV) { |
| 3493 | vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427"; |
| 3494 | } |
| 3495 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3496 | // Validate descriptor use |
| 3497 | for (auto use : descriptor_uses) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3498 | // Verify given pipelineLayout has requested setLayout with requested binding |
Jeff Bolz | e7fc67b | 2019-10-04 12:29:31 -0500 | [diff] [blame] | 3499 | const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3500 | unsigned required_descriptor_count; |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3501 | bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR; |
| 3502 | std::set<uint32_t> descriptor_types = |
| 3503 | TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3504 | |
| 3505 | if (!binding) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3506 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3507 | "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout", |
| 3508 | use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3509 | } else if (~binding->stageFlags & pStage->stage) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3510 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3511 | "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first, |
| 3512 | use.first.second, string_VkShaderStageFlagBits(pStage->stage)); |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 3513 | } else if (descriptor_types.find(binding->descriptorType) == descriptor_types.end()) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3514 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3515 | "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first, |
| 3516 | use.first.second, string_descriptorTypes(descriptor_types).c_str(), |
| 3517 | string_VkDescriptorType(binding->descriptorType)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3518 | } else if (binding->descriptorCount < required_descriptor_count) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3519 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3520 | "Shader expects at least %u descriptors for binding %u.%u but only %u provided", |
| 3521 | required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | // Validate use of input attachments against subpass structure |
| 3526 | if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3527 | auto input_attachment_uses = CollectInterfaceByInputAttachmentIndex(module, accessible_ids); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3528 | |
Petr Kraus | e91f7a1 | 2017-12-14 20:57:36 +0100 | [diff] [blame] | 3529 | auto rpci = pipeline->rp_state->createInfo.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3530 | auto subpass = pipeline->graphicsPipelineCI.subpass; |
| 3531 | |
| 3532 | for (auto use : input_attachment_uses) { |
| 3533 | auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments; |
| 3534 | auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount) |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3535 | ? input_attachments[use.first].attachment |
| 3536 | : VK_ATTACHMENT_UNUSED; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3537 | |
| 3538 | if (index == VK_ATTACHMENT_UNUSED) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3539 | skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment, |
| 3540 | "Shader consumes input attachment index %d but not provided in subpass", use.first); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3541 | } else if (!(GetFormatType(rpci->pAttachments[index].format) & GetFundamentalType(module, use.second.type_id))) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3542 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3543 | LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch, |
| 3544 | "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first, |
| 3545 | string_VkFormat(rpci->pAttachments[index].format), DescribeType(module, use.second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3546 | } |
| 3547 | } |
| 3548 | } |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 3549 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) { |
| 3550 | skip |= ValidateComputeWorkGroupSizes(module); |
| 3551 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3552 | return skip; |
| 3553 | } |
| 3554 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3555 | bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint, |
| 3556 | shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer, |
| 3557 | spirv_inst_iter consumer_entrypoint, |
| 3558 | shader_stage_attributes const *consumer_stage) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3559 | bool skip = false; |
| 3560 | |
| 3561 | auto outputs = |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3562 | CollectInterfaceByLocation(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output); |
| 3563 | auto inputs = CollectInterfaceByLocation(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3564 | |
| 3565 | auto a_it = outputs.begin(); |
| 3566 | auto b_it = inputs.begin(); |
| 3567 | |
| 3568 | // Maps sorted by key (location); walk them together to find mismatches |
| 3569 | while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) { |
| 3570 | bool a_at_end = outputs.size() == 0 || a_it == outputs.end(); |
| 3571 | bool b_at_end = inputs.size() == 0 || b_it == inputs.end(); |
| 3572 | auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first; |
| 3573 | auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first; |
| 3574 | |
| 3575 | if (b_at_end || ((!a_at_end) && (a_first < b_first))) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3576 | skip |= LogPerformanceWarning(producer->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 3577 | "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, |
| 3578 | a_first.first, a_first.second, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3579 | a_it++; |
| 3580 | } else if (a_at_end || a_first > b_first) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3581 | skip |= LogError(consumer->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 3582 | "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first, |
| 3583 | b_first.second, producer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3584 | b_it++; |
| 3585 | } else { |
| 3586 | // subtleties of arrayed interfaces: |
| 3587 | // - if is_patch, then the member is not arrayed, even though the interface may be. |
| 3588 | // - if is_block_member, then the extra array level of an arrayed interface is not |
| 3589 | // expressed in the member type -- it's expressed in the block type. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3590 | if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id, |
| 3591 | producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member, |
| 3592 | consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3593 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3594 | "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second, |
| 3595 | DescribeType(producer, a_it->second.type_id).c_str(), |
| 3596 | DescribeType(consumer, b_it->second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3597 | } |
| 3598 | if (a_it->second.is_patch != b_it->second.is_patch) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3599 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3600 | "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage", |
| 3601 | a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name, |
| 3602 | b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3603 | } |
| 3604 | if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3605 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3606 | "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first, |
| 3607 | a_first.second, producer_stage->name, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3608 | } |
| 3609 | a_it++; |
| 3610 | b_it++; |
| 3611 | } |
| 3612 | } |
| 3613 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3614 | if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 3615 | auto builtins_producer = CollectBuiltinBlockMembers(producer, producer_entrypoint, spv::StorageClassOutput); |
| 3616 | auto builtins_consumer = CollectBuiltinBlockMembers(consumer, consumer_entrypoint, spv::StorageClassInput); |
| 3617 | |
| 3618 | if (!builtins_producer.empty() && !builtins_consumer.empty()) { |
| 3619 | if (builtins_producer.size() != builtins_consumer.size()) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3620 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3621 | "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] | 3622 | producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name, |
| 3623 | static_cast<int>(builtins_consumer.size())); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3624 | } else { |
| 3625 | auto it_producer = builtins_producer.begin(); |
| 3626 | auto it_consumer = builtins_consumer.begin(); |
| 3627 | while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) { |
| 3628 | if (*it_producer != *it_consumer) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3629 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3630 | "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name, |
| 3631 | consumer_stage->name); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3632 | break; |
| 3633 | } |
| 3634 | it_producer++; |
| 3635 | it_consumer++; |
| 3636 | } |
| 3637 | } |
| 3638 | } |
| 3639 | } |
| 3640 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3641 | return skip; |
| 3642 | } |
| 3643 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3644 | static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3645 | uint32_t stage_mask = 0; |
| 3646 | if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 3647 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
| 3648 | stage_mask |= pCreateInfo->pStages[i].stage; |
| 3649 | } |
| 3650 | // 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] | 3651 | if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) { |
| 3652 | stage_mask = VK_SHADER_STAGE_MESH_BIT_NV; |
| 3653 | } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3654 | stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT; |
| 3655 | } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) { |
| 3656 | stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 3657 | } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) { |
| 3658 | stage_mask = VK_SHADER_STAGE_VERTEX_BIT; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 3659 | } |
| 3660 | } |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3661 | return stage_mask; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 3662 | } |
| 3663 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3664 | // Validate that the shaders used by the given pipeline and store the active_slots |
| 3665 | // that are actually used by the pipeline into pPipeline->active_slots |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3666 | bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3667 | auto create_info = pipeline->graphicsPipelineCI.ptr(); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3668 | int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 3669 | int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3670 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3671 | const SHADER_MODULE_STATE *shaders[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3672 | memset(shaders, 0, sizeof(shaders)); |
Jeff Bolz | 7e35c39 | 2018-09-04 15:30:41 -0500 | [diff] [blame] | 3673 | spirv_inst_iter entrypoints[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3674 | bool skip = false; |
| 3675 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3676 | uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3677 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3678 | for (uint32_t i = 0; i < create_info->stageCount; i++) { |
| 3679 | auto stage = &create_info->pStages[i]; |
| 3680 | auto stage_id = GetShaderStageId(stage->stage); |
| 3681 | shaders[stage_id] = GetShaderModuleState(stage->module); |
| 3682 | entrypoints[stage_id] = FindEntrypoint(shaders[stage_id], stage->pName, stage->stage); |
| 3683 | skip |= ValidatePipelineShaderStage(stage, pipeline, pipeline->stage_state[i], shaders[stage_id], entrypoints[stage_id], |
| 3684 | (pointlist_stage_mask == stage->stage)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | // if the shader stages are no good individually, cross-stage validation is pointless. |
| 3688 | if (skip) return true; |
| 3689 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3690 | auto vi = create_info->pVertexInputState; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3691 | |
| 3692 | if (vi) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3693 | skip |= ValidateViConsistency(vi); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3697 | skip |= ValidateViAgainstVsInputs(vi, shaders[vertex_stage], entrypoints[vertex_stage]); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3698 | } |
| 3699 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3700 | int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 3701 | int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3702 | |
| 3703 | while (!shaders[producer] && producer != fragment_stage) { |
| 3704 | producer++; |
| 3705 | consumer++; |
| 3706 | } |
| 3707 | |
| 3708 | for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) { |
| 3709 | assert(shaders[producer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 3710 | if (shaders[consumer]) { |
| 3711 | if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3712 | skip |= ValidateInterfaceBetweenStages(shaders[producer], entrypoints[producer], &shader_stage_attribs[producer], |
| 3713 | shaders[consumer], entrypoints[consumer], &shader_stage_attribs[consumer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 3714 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3715 | |
| 3716 | producer = consumer; |
| 3717 | } |
| 3718 | } |
| 3719 | |
| 3720 | if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3721 | skip |= ValidateFsOutputsAgainstRenderPass(shaders[fragment_stage], entrypoints[fragment_stage], pipeline, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3722 | create_info->subpass); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | return skip; |
| 3726 | } |
| 3727 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3728 | bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB, |
| 3729 | const char *caller, const DrawDispatchVuid &vuid) const { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3730 | auto create_info = pipeline->graphicsPipelineCI.ptr(); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3731 | |
| 3732 | const SHADER_MODULE_STATE *shaders[32]; |
| 3733 | memset(shaders, 0, sizeof(shaders)); |
| 3734 | spirv_inst_iter entrypoints[32]; |
| 3735 | memset(entrypoints, 0, sizeof(entrypoints)); |
| 3736 | bool skip = false; |
| 3737 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3738 | for (uint32_t i = 0; i < create_info->stageCount; i++) { |
| 3739 | auto stage = &create_info->pStages[i]; |
| 3740 | auto stage_id = GetShaderStageId(stage->stage); |
| 3741 | shaders[stage_id] = GetShaderModuleState(stage->module); |
| 3742 | entrypoints[stage_id] = FindEntrypoint(shaders[stage_id], stage->pName, stage->stage); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3743 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3744 | if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT || |
| 3745 | stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3746 | if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports && |
| 3747 | IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) { |
| 3748 | spirv_inst_iter insn = entrypoints[stage_id]; |
| 3749 | bool primitiverate_written = false; |
| 3750 | |
| 3751 | while (!primitiverate_written && (insn.opcode() != spv::OpFunction)) { |
| 3752 | if (insn.opcode() == spv::OpMemberDecorate) { |
| 3753 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 3754 | if (insn.word(4) == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3755 | primitiverate_written = IsBuiltInWritten(shaders[stage_id], insn, entrypoints[stage_id]); |
| 3756 | } |
| 3757 | } |
| 3758 | } else if (insn.opcode() == spv::OpDecorate) { |
| 3759 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 3760 | if (insn.word(3) == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3761 | primitiverate_written = IsBuiltInWritten(shaders[stage_id], insn, entrypoints[stage_id]); |
| 3762 | } |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | insn++; |
| 3767 | } |
| 3768 | |
| 3769 | if (primitiverate_written) { |
| 3770 | skip |= |
| 3771 | LogError(pipeline->pipeline, vuid.viewport_count_primitive_shading_rate, |
| 3772 | "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in" |
| 3773 | "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT," |
| 3774 | "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3775 | caller, string_VkShaderStageFlagBits(stage->stage)); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3776 | } |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | |
| 3781 | return skip; |
| 3782 | } |
| 3783 | |
sfricke-samsung | e72a85e | 2020-02-29 21:48:37 -0800 | [diff] [blame] | 3784 | bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3785 | const auto &stage = *pipeline->computePipelineCI.stage.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3786 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3787 | const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module); |
| 3788 | const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3789 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3790 | return ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[0], module, entrypoint, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3791 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3792 | |
Ricardo Garcia | 6f3477e | 2020-10-21 10:58:53 +0200 | [diff] [blame] | 3793 | uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const { |
| 3794 | uint32_t total = 0; |
| 3795 | |
| 3796 | const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages; |
| 3797 | for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) { |
| 3798 | if (stages[stage_index].stage == stageBit) { |
| 3799 | total++; |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | if (pipeline->raytracingPipelineCI.pLibraryInfo) { |
| 3804 | for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) { |
| 3805 | const PIPELINE_STATE *library_pipeline = GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]); |
| 3806 | total += CalcShaderStageCount(library_pipeline, stageBit); |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | return total; |
| 3811 | } |
| 3812 | |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3813 | bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const { |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3814 | bool skip = false; |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3815 | |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3816 | if (isKHR) { |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3817 | if (pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth > |
| 3818 | phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) { |
| 3819 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589", |
| 3820 | "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to " |
| 3821 | "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d", |
| 3822 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth, |
| 3823 | phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3824 | } |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3825 | if (pipeline->raytracingPipelineCI.pLibraryInfo) { |
| 3826 | for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3827 | const PIPELINE_STATE *library_pipelinestate = |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3828 | GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3829 | if (library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3830 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth) { |
| 3831 | skip |= LogError( |
| 3832 | device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591", |
| 3833 | "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been" |
| 3834 | "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] | 3835 | i, library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3836 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth); |
| 3837 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3838 | if (library_pipelinestate->raytracingPipelineCI.pLibraryInfo && |
| 3839 | (library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3840 | pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize || |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3841 | library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3842 | pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize)) { |
| 3843 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593", |
| 3844 | "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries " |
| 3845 | "member must have been created with values of the maxPipelineRayPayloadSize and " |
| 3846 | "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline"); |
| 3847 | } |
| 3848 | 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] | 3849 | !(library_pipelinestate->raytracingPipelineCI.flags & |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3850 | VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) { |
| 3851 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594", |
| 3852 | "vkCreateRayTracingPipelinesKHR: If flags includes " |
| 3853 | "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of " |
| 3854 | "the pLibraries member of libraries must have been created with the " |
| 3855 | "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set"); |
| 3856 | } |
sourav parmar | 83c31b1 | 2020-05-06 12:30:54 -0700 | [diff] [blame] | 3857 | } |
| 3858 | } |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3859 | } else { |
| 3860 | if (pipeline->raytracingPipelineCI.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) { |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3861 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457", |
| 3862 | "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to " |
| 3863 | "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)", |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3864 | pipeline->raytracingPipelineCI.maxRecursionDepth, |
| 3865 | phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth); |
| 3866 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3867 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3868 | const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages; |
| 3869 | const auto *groups = pipeline->raytracingPipelineCI.ptr()->pGroups; |
| 3870 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3871 | 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] | 3872 | const auto &stage = stages[stage_index]; |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 3873 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3874 | const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module); |
| 3875 | const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage); |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 3876 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3877 | skip |= ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[stage_index], module, entrypoint, false); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3878 | } |
Ricardo Garcia | 6f3477e | 2020-10-21 10:58:53 +0200 | [diff] [blame] | 3879 | |
| 3880 | if ((pipeline->raytracingPipelineCI.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) { |
| 3881 | const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR); |
| 3882 | if (raygen_stages_count == 0) { |
| 3883 | skip |= LogError( |
| 3884 | device, |
| 3885 | isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-03425", |
| 3886 | " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR."); |
| 3887 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | for (uint32_t group_index = 0; group_index < pipeline->raytracingPipelineCI.groupCount; group_index++) { |
| 3891 | const auto &group = groups[group_index]; |
| 3892 | |
| 3893 | if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) { |
| 3894 | if (group.generalShader >= pipeline->raytracingPipelineCI.stageCount || |
| 3895 | (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV && |
| 3896 | stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV && |
| 3897 | stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3898 | skip |= LogError(device, |
| 3899 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474" |
| 3900 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413", |
| 3901 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3902 | } |
| 3903 | if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV || |
| 3904 | group.intersectionShader != VK_SHADER_UNUSED_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3905 | skip |= LogError(device, |
| 3906 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475" |
| 3907 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414", |
| 3908 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3909 | } |
| 3910 | } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) { |
| 3911 | if (group.intersectionShader >= pipeline->raytracingPipelineCI.stageCount || |
| 3912 | stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3913 | skip |= LogError(device, |
| 3914 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476" |
| 3915 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415", |
| 3916 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3917 | } |
| 3918 | } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) { |
| 3919 | if (group.intersectionShader != VK_SHADER_UNUSED_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3920 | skip |= LogError(device, |
| 3921 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477" |
| 3922 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416", |
| 3923 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3924 | } |
| 3925 | } |
| 3926 | |
| 3927 | if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV || |
| 3928 | group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) { |
| 3929 | if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= pipeline->raytracingPipelineCI.stageCount || |
| 3930 | stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3931 | skip |= LogError(device, |
| 3932 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479" |
| 3933 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418", |
| 3934 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3935 | } |
| 3936 | if (group.closestHitShader != VK_SHADER_UNUSED_NV && |
| 3937 | (group.closestHitShader >= pipeline->raytracingPipelineCI.stageCount || |
| 3938 | stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3939 | skip |= LogError(device, |
| 3940 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478" |
| 3941 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417", |
| 3942 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3943 | } |
| 3944 | } |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3945 | } |
| 3946 | return skip; |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 3947 | } |
| 3948 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3949 | 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] | 3950 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3951 | static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) { |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 3952 | const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext); |
John Zulauf | 25ea243 | 2019-04-05 10:07:38 -0600 | [diff] [blame] | 3953 | if (validation_cache_ci) { |
John Zulauf | 146ee80 | 2019-04-05 15:31:06 -0600 | [diff] [blame] | 3954 | return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 3955 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 3956 | return nullptr; |
| 3957 | } |
| 3958 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 3959 | bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 3960 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3961 | bool skip = false; |
| 3962 | spv_result_t spv_valid = SPV_SUCCESS; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3963 | |
Mark Lobodzinski | 90eea5b | 2020-05-15 12:54:00 -0600 | [diff] [blame] | 3964 | if (disabled[shader_validation]) { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3965 | return false; |
| 3966 | } |
| 3967 | |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 3968 | auto have_glsl_shader = device_extensions.vk_nv_glsl_shader; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3969 | |
| 3970 | if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3971 | skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376", |
| 3972 | "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".", |
| 3973 | pCreateInfo->codeSize); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3974 | } else { |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 3975 | auto cache = GetValidationCacheInfo(pCreateInfo); |
| 3976 | uint32_t hash = 0; |
| 3977 | if (cache) { |
| 3978 | hash = ValidationCache::MakeShaderHash(pCreateInfo); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3979 | if (cache->Contains(hash)) return false; |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 3980 | } |
| 3981 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3982 | // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present, |
| 3983 | // the default values will be used during validation. |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 3984 | 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] | 3985 | spv_context ctx = spvContextCreate(spirv_environment); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3986 | spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)}; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3987 | spv_diagnostic diag = nullptr; |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 3988 | spvtools::ValidatorOptions options; |
| 3989 | AdjustValidatorOptions(device_extensions, enabled_features, options); |
Karl Schultz | fda1b38 | 2018-08-08 18:56:11 -0600 | [diff] [blame] | 3990 | spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3991 | if (spv_valid != SPV_SUCCESS) { |
| 3992 | if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3993 | if (spv_valid == SPV_WARNING) { |
| 3994 | skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s", |
| 3995 | diag && diag->error ? diag->error : "(no error text)"); |
| 3996 | } else { |
| 3997 | skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s", |
| 3998 | diag && diag->error ? diag->error : "(no error text)"); |
| 3999 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4000 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4001 | } else { |
| 4002 | if (cache) { |
| 4003 | cache->Insert(hash); |
| 4004 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | spvDiagnosticDestroy(diag); |
| 4008 | spvContextDestroy(ctx); |
| 4009 | } |
| 4010 | |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4011 | return skip; |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 4012 | } |
| 4013 | |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 4014 | bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader) const { |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4015 | bool skip = false; |
| 4016 | uint32_t local_size_x = 0; |
| 4017 | uint32_t local_size_y = 0; |
| 4018 | uint32_t local_size_z = 0; |
| 4019 | if (FindLocalSize(shader, local_size_x, local_size_y, local_size_z)) { |
| 4020 | if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4021 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4022 | "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").", |
| 4023 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4024 | phys_dev_props.limits.maxComputeWorkGroupSize[0]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4025 | } |
| 4026 | if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4027 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4028 | "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").", |
| 4029 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4030 | phys_dev_props.limits.maxComputeWorkGroupSize[1]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4031 | } |
| 4032 | if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4033 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4034 | "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").", |
| 4035 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4036 | phys_dev_props.limits.maxComputeWorkGroupSize[2]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations; |
| 4040 | uint64_t invocations = local_size_x * local_size_y; |
| 4041 | // Prevent overflow. |
| 4042 | bool fail = false; |
| 4043 | if (invocations > UINT32_MAX || invocations > limit) { |
| 4044 | fail = true; |
| 4045 | } |
| 4046 | if (!fail) { |
| 4047 | invocations *= local_size_z; |
| 4048 | if (invocations > UINT32_MAX || invocations > limit) { |
| 4049 | fail = true; |
| 4050 | } |
| 4051 | } |
| 4052 | if (fail) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4053 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations", |
| 4054 | "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32 |
| 4055 | ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").", |
| 4056 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, local_size_y, local_size_z, |
| 4057 | limit); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4058 | } |
| 4059 | } |
| 4060 | return skip; |
| 4061 | } |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 4062 | |
| 4063 | spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) { |
| 4064 | if (api_version >= VK_API_VERSION_1_2) { |
| 4065 | return SPV_ENV_VULKAN_1_2; |
| 4066 | } else if (api_version >= VK_API_VERSION_1_1) { |
| 4067 | if (spirv_1_4) { |
| 4068 | return SPV_ENV_VULKAN_1_1_SPIRV_1_4; |
| 4069 | } else { |
| 4070 | return SPV_ENV_VULKAN_1_1; |
| 4071 | } |
| 4072 | } |
| 4073 | return SPV_ENV_VULKAN_1_0; |
| 4074 | } |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 4075 | |
| 4076 | void AdjustValidatorOptions(const DeviceExtensions device_extensions, const DeviceFeatures enabled_features, |
| 4077 | spvtools::ValidatorOptions &options) { |
| 4078 | if (device_extensions.vk_khr_relaxed_block_layout) { |
| 4079 | options.SetRelaxBlockLayout(true); |
| 4080 | } |
| 4081 | if (device_extensions.vk_khr_uniform_buffer_standard_layout && enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) { |
| 4082 | options.SetUniformBufferStandardLayout(true); |
| 4083 | } |
| 4084 | if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) { |
| 4085 | options.SetScalarBlockLayout(true); |
| 4086 | } |
| 4087 | } |