Tony-LunarG | 7371999 | 2020-01-15 10:20:28 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015-2020 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2020 Valve Corporation |
| 3 | * Copyright (c) 2015-2020 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2020 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. |
| 117 | if (func_set.offset) |
| 118 | func_set.op_lists.insert({insn.opcode(), insn.offset()}); |
| 119 | else if (entry_point) { |
| 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: { |
| 184 | auto targetId = insn.word(1); |
| 185 | decorations[targetId].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u); |
| 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 |
| 195 | auto entrypoint_name = (char const *)&insn.word(3); |
| 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 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 744 | if (builtin != -1) |
| 745 | continue; |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [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; |
| 769 | std::vector<uint32_t> builtinStructMembers; |
| 770 | std::vector<uint32_t> builtinDecorations; |
| 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) { |
| 777 | builtinStructMembers.push_back(insn.word(1)); |
| 778 | } |
| 779 | break; |
| 780 | // Find all built-in decorations |
| 781 | case spv::OpDecorate: |
| 782 | switch (insn.word(2)) { |
| 783 | case spv::DecorationBlock: { |
| 784 | uint32_t blockID = insn.word(1); |
| 785 | for (auto builtInBlockID : builtinStructMembers) { |
| 786 | // Check if one of the members of the block are built-in -> the block is built-in |
| 787 | if (blockID == builtInBlockID) { |
| 788 | builtinDecorations.push_back(blockID); |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | break; |
| 793 | } |
| 794 | case spv::DecorationBuiltIn: |
| 795 | builtinDecorations.push_back(insn.word(1)); |
| 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 |
| 816 | std::vector<uint32_t> builtinBlockMembers; |
| 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) { |
| 825 | for (auto builtInID : builtinDecorations) { |
| 826 | if (builtInID == def.word(1)) { |
| 827 | for (int i = 2; i < (int)def.len(); i++) |
| 828 | builtinBlockMembers.push_back(spv::BuiltInMax); // Start with undefined builtin for each struct member. |
| 829 | // These shouldn't be left after replacing. |
| 830 | for (auto insn : *src) { |
| 831 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == builtInID && |
| 832 | insn.word(3) == spv::DecorationBuiltIn) { |
| 833 | auto structIndex = insn.word(2); |
| 834 | assert(structIndex < builtinBlockMembers.size()); |
| 835 | builtinBlockMembers[structIndex] = insn.word(4); |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | return builtinBlockMembers; |
| 844 | } |
| 845 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 846 | static std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex( |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 847 | 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] | 848 | std::vector<std::pair<uint32_t, interface_var>> out; |
| 849 | |
| 850 | for (auto insn : *src) { |
| 851 | if (insn.opcode() == spv::OpDecorate) { |
| 852 | if (insn.word(2) == spv::DecorationInputAttachmentIndex) { |
| 853 | auto attachment_index = insn.word(3); |
| 854 | auto id = insn.word(1); |
| 855 | |
| 856 | if (accessible_ids.count(id)) { |
| 857 | auto def = src->get_def(id); |
| 858 | assert(def != src->end()); |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 859 | if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 860 | auto num_locations = GetLocationsConsumedByType(src, def.word(1), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 861 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 862 | interface_var v = {}; |
| 863 | v.id = id; |
| 864 | v.type_id = def.word(1); |
| 865 | v.offset = offset; |
| 866 | out.emplace_back(attachment_index + offset, v); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | return out; |
| 875 | } |
| 876 | |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 877 | static bool AtomicOperation(uint32_t opcode) { |
| 878 | switch (opcode) { |
| 879 | case spv::OpAtomicLoad: |
| 880 | case spv::OpAtomicStore: |
| 881 | case spv::OpAtomicExchange: |
| 882 | case spv::OpAtomicCompareExchange: |
| 883 | case spv::OpAtomicCompareExchangeWeak: |
| 884 | case spv::OpAtomicIIncrement: |
| 885 | case spv::OpAtomicIDecrement: |
| 886 | case spv::OpAtomicIAdd: |
| 887 | case spv::OpAtomicISub: |
| 888 | case spv::OpAtomicSMin: |
| 889 | case spv::OpAtomicUMin: |
| 890 | case spv::OpAtomicSMax: |
| 891 | case spv::OpAtomicUMax: |
| 892 | case spv::OpAtomicAnd: |
| 893 | case spv::OpAtomicOr: |
| 894 | case spv::OpAtomicXor: |
| 895 | case spv::OpAtomicFAddEXT: |
| 896 | return true; |
| 897 | default: |
| 898 | return false; |
| 899 | } |
| 900 | return false; |
| 901 | } |
| 902 | |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 903 | bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<unsigned> &operator_members, |
| 904 | const std::unordered_map<unsigned, unsigned> &load_members, |
| 905 | const std::unordered_map<unsigned, std::pair<unsigned, unsigned>> &accesschain_members) { |
| 906 | for (auto load_id : operator_members) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 907 | if (object_id == load_id) return true; |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 908 | auto load_it = load_members.find(load_id); |
| 909 | if (load_it == load_members.end()) { |
| 910 | continue; |
| 911 | } |
| 912 | if (load_it->second == object_id) { |
| 913 | return true; |
| 914 | } |
| 915 | |
| 916 | auto accesschain_it = accesschain_members.find(load_it->second); |
| 917 | if (accesschain_it == accesschain_members.end()) { |
| 918 | continue; |
| 919 | } |
| 920 | if (accesschain_it->second.first == object_id) { |
| 921 | return true; |
| 922 | } |
| 923 | } |
| 924 | return false; |
| 925 | } |
| 926 | |
locke-lunarg | ae2a43c | 2020-09-22 17:21:57 -0600 | [diff] [blame] | 927 | bool CheckImageOperandsBiasOffset(uint32_t type) { |
| 928 | return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask | |
| 929 | spv::ImageOperandsConstOffsetsMask) |
| 930 | ? true |
| 931 | : false; |
| 932 | } |
| 933 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 934 | struct shader_module_used_operators { |
| 935 | bool updated; |
| 936 | std::vector<unsigned> imagwrite_members; |
| 937 | std::vector<unsigned> atomic_members; |
| 938 | std::vector<unsigned> store_members; |
| 939 | std::vector<unsigned> atomic_store_members; |
| 940 | std::vector<unsigned> sampler_implicitLod_dref_proj_members; // sampler Load id |
| 941 | std::vector<unsigned> sampler_bias_offset_members; // sampler Load id |
| 942 | std::vector<std::pair<unsigned, unsigned>> sampledImage_members; |
| 943 | std::unordered_map<unsigned, unsigned> load_members; |
| 944 | std::unordered_map<unsigned, std::pair<unsigned, unsigned>> accesschain_members; |
| 945 | std::unordered_map<unsigned, unsigned> image_texel_pointer_members; |
| 946 | |
| 947 | shader_module_used_operators() : updated(false) {} |
| 948 | |
| 949 | void update(SHADER_MODULE_STATE const *module) { |
| 950 | if (updated) return; |
| 951 | updated = true; |
| 952 | |
| 953 | for (auto insn : *module) { |
| 954 | switch (insn.opcode()) { |
| 955 | case spv::OpImageSampleImplicitLod: |
| 956 | case spv::OpImageSampleProjImplicitLod: |
| 957 | case spv::OpImageSampleProjExplicitLod: |
| 958 | case spv::OpImageSparseSampleImplicitLod: |
| 959 | case spv::OpImageSparseSampleProjImplicitLod: |
| 960 | case spv::OpImageSparseSampleProjExplicitLod: { |
| 961 | sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id |
| 962 | // ImageOperands in index: 5 |
| 963 | if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) { |
| 964 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 965 | } |
| 966 | break; |
| 967 | } |
| 968 | case spv::OpImageSampleDrefImplicitLod: |
| 969 | case spv::OpImageSampleDrefExplicitLod: |
| 970 | case spv::OpImageSampleProjDrefImplicitLod: |
| 971 | case spv::OpImageSampleProjDrefExplicitLod: |
| 972 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 973 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 974 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 975 | case spv::OpImageSparseSampleProjDrefExplicitLod: { |
| 976 | sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id |
| 977 | // ImageOperands in index: 6 |
| 978 | if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) { |
| 979 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 980 | } |
| 981 | break; |
| 982 | } |
| 983 | case spv::OpImageSampleExplicitLod: |
| 984 | case spv::OpImageSparseSampleExplicitLod: { |
| 985 | // ImageOperands in index: 5 |
| 986 | if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) { |
| 987 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 988 | } |
| 989 | break; |
| 990 | } |
| 991 | case spv::OpStore: { |
| 992 | store_members.emplace_back(insn.word(1)); // object id or AccessChain id |
| 993 | break; |
| 994 | } |
| 995 | case spv::OpImageWrite: { |
| 996 | imagwrite_members.emplace_back(insn.word(1)); // Load id |
| 997 | break; |
| 998 | } |
| 999 | case spv::OpSampledImage: { |
| 1000 | // 3: image load id, 4: sampler load id |
| 1001 | sampledImage_members.emplace_back(std::pair<unsigned, unsigned>(insn.word(3), insn.word(4))); |
| 1002 | break; |
| 1003 | } |
| 1004 | case spv::OpLoad: { |
| 1005 | // 2: Load id, 3: object id or AccessChain id |
| 1006 | load_members.insert(std::make_pair(insn.word(2), insn.word(3))); |
| 1007 | break; |
| 1008 | } |
| 1009 | case spv::OpAccessChain: { |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1010 | if (insn.len() == 4) { |
| 1011 | // If it is for struct, the length is only 4. |
| 1012 | // 2: AccessChain id, 3: object id |
| 1013 | accesschain_members.insert(std::make_pair(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), 0))); |
| 1014 | } else { |
| 1015 | // 2: AccessChain id, 3: object id, 4: object id of array index |
| 1016 | accesschain_members.insert( |
| 1017 | std::make_pair(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), insn.word(4)))); |
| 1018 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | case spv::OpImageTexelPointer: { |
| 1022 | // 2: ImageTexelPointer id, 3: object id |
| 1023 | image_texel_pointer_members.insert(std::make_pair(insn.word(2), insn.word(3))); |
| 1024 | break; |
| 1025 | } |
| 1026 | default: { |
| 1027 | if (AtomicOperation(insn.opcode())) { |
| 1028 | if (insn.opcode() == spv::OpAtomicStore) { |
| 1029 | atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id |
| 1030 | } else { |
| 1031 | atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id |
| 1032 | } |
| 1033 | } |
| 1034 | break; |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | }; |
| 1040 | |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1041 | // Check writable, image atomic operation |
| 1042 | 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] | 1043 | bool is_check_writable, interface_var &out_interface_var, |
| 1044 | shader_module_used_operators &used_operators) { |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1045 | uint32_t type_id = id_it.word(1); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1046 | unsigned int id = id_it.word(2); |
| 1047 | |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1048 | auto type = module->get_def(type_id); |
| 1049 | |
| 1050 | // 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] | 1051 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray || |
| 1052 | type.opcode() == spv::OpTypeSampledImage) { |
| 1053 | if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray || |
| 1054 | type.opcode() == spv::OpTypeSampledImage) { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1055 | type = module->get_def(type.word(2)); // Element type |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1056 | } else { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1057 | type = module->get_def(type.word(3)); // Pointer type |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1058 | } |
| 1059 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1060 | switch (type.opcode()) { |
| 1061 | case spv::OpTypeImage: { |
| 1062 | auto dim = type.word(3); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1063 | if (dim != spv::DimSubpassData) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1064 | used_operators.update(module); |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1065 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1066 | if (CheckObjectIDFromOpLoad(id, used_operators.imagwrite_members, used_operators.load_members, |
| 1067 | used_operators.accesschain_members)) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1068 | out_interface_var.is_writable = true; |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 1069 | } |
| 1070 | if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members, |
| 1071 | used_operators.accesschain_members)) { |
| 1072 | out_interface_var.is_sampler_implicitLod_dref_proj = true; |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1073 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1074 | if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members, |
| 1075 | used_operators.accesschain_members)) { |
locke-lunarg | ae2a43c | 2020-09-22 17:21:57 -0600 | [diff] [blame] | 1076 | out_interface_var.is_sampler_bias_offset = true; |
| 1077 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1078 | if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members, |
| 1079 | used_operators.accesschain_members) || |
| 1080 | CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members, |
| 1081 | used_operators.accesschain_members)) { |
| 1082 | out_interface_var.is_atomic_operation = true; |
| 1083 | } |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1084 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1085 | for (auto &itp_id : used_operators.sampledImage_members) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1086 | // Find if image id match. |
| 1087 | uint32_t image_index = 0; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1088 | auto load_it = used_operators.load_members.find(itp_id.first); |
| 1089 | if (load_it == used_operators.load_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1090 | continue; |
| 1091 | } else { |
| 1092 | if (load_it->second != id) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1093 | auto accesschain_it = used_operators.accesschain_members.find(load_it->second); |
| 1094 | if (accesschain_it == used_operators.accesschain_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1095 | continue; |
| 1096 | } else { |
| 1097 | if (accesschain_it->second.first != id) { |
| 1098 | continue; |
| 1099 | } |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1100 | if (used_operators.load_members.end() != |
| 1101 | used_operators.load_members.find(accesschain_it->second.second)) { |
| 1102 | // image_index isn't a constant, skip. |
| 1103 | break; |
| 1104 | } |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1105 | image_index = GetConstantValue(module, accesschain_it->second.second); |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | // Find sampler's set binding. |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1110 | load_it = used_operators.load_members.find(itp_id.second); |
| 1111 | if (load_it == used_operators.load_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1112 | continue; |
| 1113 | } else { |
| 1114 | uint32_t sampler_id = load_it->second; |
| 1115 | uint32_t sampler_index = 0; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1116 | auto accesschain_it = used_operators.accesschain_members.find(load_it->second); |
| 1117 | if (accesschain_it != used_operators.accesschain_members.end()) { |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1118 | if (used_operators.load_members.end() != |
| 1119 | used_operators.load_members.find(accesschain_it->second.second)) { |
| 1120 | // sampler_index isn't a constant, skip. |
| 1121 | break; |
| 1122 | } |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1123 | sampler_id = accesschain_it->second.first; |
| 1124 | sampler_index = GetConstantValue(module, accesschain_it->second.second); |
| 1125 | } |
| 1126 | auto sampler_dec = module->get_decorations(sampler_id); |
locke-lunarg | 654a905 | 2020-10-13 16:28:42 -0600 | [diff] [blame] | 1127 | if (image_index >= out_interface_var.samplers_used_by_image.size()) { |
| 1128 | out_interface_var.samplers_used_by_image.resize(image_index + 1); |
| 1129 | } |
| 1130 | out_interface_var.samplers_used_by_image[image_index].emplace( |
| 1131 | 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] | 1132 | } |
| 1133 | } |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1134 | } |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1135 | return; |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1138 | case spv::OpTypeStruct: { |
| 1139 | std::unordered_set<unsigned> nonwritable_members; |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 1140 | 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] | 1141 | for (auto insn : *module) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 1142 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1) && |
| 1143 | insn.word(3) == spv::DecorationNonWritable) { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1144 | nonwritable_members.insert(insn.word(2)); |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1145 | } |
| 1146 | } |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1147 | |
| 1148 | // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated |
| 1149 | // as nonwritable. |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1150 | if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1151 | used_operators.update(module); |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1152 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1153 | for (auto oid : used_operators.store_members) { |
| 1154 | if (id == oid) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1155 | out_interface_var.is_writable = true; |
| 1156 | return; |
| 1157 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1158 | auto accesschain_it = used_operators.accesschain_members.find(oid); |
| 1159 | if (accesschain_it == used_operators.accesschain_members.end()) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1160 | continue; |
| 1161 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1162 | if (accesschain_it->second.first == id) { |
| 1163 | out_interface_var.is_writable = true; |
| 1164 | return; |
| 1165 | } |
| 1166 | } |
| 1167 | if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members, |
| 1168 | used_operators.accesschain_members)) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1169 | out_interface_var.is_writable = true; |
| 1170 | return; |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1171 | } |
| 1172 | } |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1173 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1174 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 1177 | std::vector<std::pair<descriptor_slot_t, interface_var>> CollectInterfaceByDescriptorSlot( |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 1178 | SHADER_MODULE_STATE const *src, std::unordered_set<uint32_t> const &accessible_ids, bool *has_writable_descriptor, |
| 1179 | bool *has_atomic_descriptor) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1180 | std::vector<std::pair<descriptor_slot_t, interface_var>> out; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1181 | shader_module_used_operators operators; |
| 1182 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1183 | for (auto id : accessible_ids) { |
| 1184 | auto insn = src->get_def(id); |
| 1185 | assert(insn != src->end()); |
| 1186 | |
| 1187 | if (insn.opcode() == spv::OpVariable && |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1188 | (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant || |
| 1189 | insn.word(3) == spv::StorageClassStorageBuffer)) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 1190 | auto d = src->get_decorations(insn.word(2)); |
| 1191 | unsigned set = d.descriptor_set; |
| 1192 | unsigned binding = d.binding; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1193 | |
| 1194 | interface_var v = {}; |
| 1195 | v.id = insn.word(2); |
| 1196 | v.type_id = insn.word(1); |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1197 | |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1198 | IsSpecificDescriptorType(src, insn, insn.word(3) == spv::StorageClassStorageBuffer, |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1199 | !(d.flags & decoration_set::nonwritable_bit), v, operators); |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 1200 | if (v.is_writable) *has_writable_descriptor = true; |
| 1201 | if (v.is_atomic_operation) *has_atomic_descriptor = true; |
locke-lunarg | 654e369 | 2020-06-04 17:19:15 -0600 | [diff] [blame] | 1202 | out.emplace_back(std::make_pair(set, binding), v); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | return out; |
| 1207 | } |
| 1208 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1209 | void DefineStructMember(const SHADER_MODULE_STATE &src, const spirv_inst_iter &it, |
| 1210 | const std::vector<uint32_t> &memberDecorate_offsets, shader_struct_member &data) { |
| 1211 | const auto struct_it = GetStructType(&src, it, false); |
| 1212 | assert(struct_it != src.end()); |
| 1213 | data.size = 0; |
| 1214 | |
| 1215 | shader_struct_member data1; |
| 1216 | uint32_t i = 2; |
| 1217 | uint32_t local_offset = 0; |
| 1218 | std::vector<uint32_t> offsets; |
| 1219 | offsets.resize(struct_it.len() - i); |
| 1220 | |
| 1221 | // The members of struct in SPRIV_R aren't always sort, so we need to know their order. |
| 1222 | for (const auto offset : memberDecorate_offsets) { |
| 1223 | const auto member_decorate = src.at(offset); |
| 1224 | if (member_decorate.word(1) != struct_it.word(1)) { |
| 1225 | continue; |
| 1226 | } |
| 1227 | |
| 1228 | offsets[member_decorate.word(2)] = member_decorate.word(4); |
| 1229 | } |
| 1230 | |
| 1231 | for (const auto offset : offsets) { |
| 1232 | local_offset = offset; |
| 1233 | data1 = {}; |
| 1234 | data1.root = data.root; |
| 1235 | data1.offset = local_offset; |
| 1236 | auto def_member = src.get_def(struct_it.word(i)); |
| 1237 | |
| 1238 | // Array could be multi-dimensional |
| 1239 | while (def_member.opcode() == spv::OpTypeArray) { |
| 1240 | const auto len_id = def_member.word(3); |
| 1241 | const auto def_len = src.get_def(len_id); |
| 1242 | data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length |
| 1243 | def_member = src.get_def(def_member.word(2)); |
| 1244 | } |
| 1245 | |
| 1246 | if (def_member.opcode() == spv::OpTypeStruct || def_member.opcode() == spv::OpTypePointer) { |
| 1247 | // If it's OpTypePointer. it means the member is a buffer, the type will be TypePointer, and then struct |
| 1248 | DefineStructMember(src, def_member, memberDecorate_offsets, data1); |
| 1249 | } else { |
| 1250 | if (def_member.opcode() == spv::OpTypeMatrix) { |
| 1251 | data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector. |
| 1252 | def_member = src.get_def(def_member.word(2)); |
| 1253 | } |
| 1254 | |
| 1255 | if (def_member.opcode() == spv::OpTypeVector) { |
| 1256 | data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length |
| 1257 | def_member = src.get_def(def_member.word(2)); |
| 1258 | } |
| 1259 | |
| 1260 | // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte. |
| 1261 | data1.size = (def_member.word(2) / 8); |
| 1262 | } |
| 1263 | const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size(); |
| 1264 | if (array_length_hierarchy_szie > 0) { |
| 1265 | data1.array_block_size.resize(array_length_hierarchy_szie, 1); |
| 1266 | |
| 1267 | for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) { |
| 1268 | data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2]; |
| 1269 | } |
| 1270 | } |
| 1271 | data.struct_members.emplace_back(data1); |
| 1272 | ++i; |
| 1273 | } |
| 1274 | uint32_t total_array_length = 1; |
| 1275 | for (const auto length : data1.array_length_hierarchy) { |
| 1276 | total_array_length *= length; |
| 1277 | } |
| 1278 | data.size = local_offset + data1.size * total_array_length; |
| 1279 | } |
| 1280 | |
| 1281 | uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) { |
| 1282 | int array_indices_size = static_cast<int>(array_indices.size()); |
| 1283 | if (array_indices_size) { |
| 1284 | uint32_t array_index = 0; |
| 1285 | uint32_t i = 0; |
| 1286 | for (const auto index : array_indices) { |
| 1287 | array_index += (data.array_block_size[i] * index); |
| 1288 | ++i; |
| 1289 | } |
| 1290 | offset += (array_index * data.size); |
| 1291 | } |
| 1292 | return offset; |
| 1293 | } |
| 1294 | |
| 1295 | void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) { |
| 1296 | int array_indices_size = static_cast<int>(array_indices.size()); |
| 1297 | uint32_t block_memory_size = data.size; |
| 1298 | for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) { |
| 1299 | block_memory_size *= data.array_length_hierarchy[i]; |
| 1300 | } |
| 1301 | |
| 1302 | offset = UpdateOffset(offset, array_indices, data); |
| 1303 | |
| 1304 | uint32_t end = offset + block_memory_size; |
| 1305 | auto used_bytes = data.GetUsedbytes(); |
| 1306 | if (used_bytes->size() < end) { |
| 1307 | used_bytes->resize(end, 0); |
| 1308 | } |
| 1309 | std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size)); |
| 1310 | } |
| 1311 | |
| 1312 | void RunUsedArray(const SHADER_MODULE_STATE &src, uint32_t offset, std::vector<uint32_t> array_indices, |
| 1313 | uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it, const shader_struct_member &data) { |
| 1314 | if (access_chain_word_index < access_chain_it.len()) { |
| 1315 | if (data.array_length_hierarchy.size() > array_indices.size()) { |
| 1316 | auto def_it = src.get_def(access_chain_it.word(access_chain_word_index)); |
| 1317 | ++access_chain_word_index; |
| 1318 | |
| 1319 | if (def_it != src.end() && def_it.opcode() == spv::OpConstant) { |
| 1320 | array_indices.emplace_back(def_it.word(3)); |
| 1321 | RunUsedArray(src, offset, array_indices, access_chain_word_index, access_chain_it, data); |
| 1322 | } else { |
| 1323 | // If it is a variable, set the all array is used. |
| 1324 | if (access_chain_word_index < access_chain_it.len()) { |
| 1325 | uint32_t array_length = data.array_length_hierarchy[array_indices.size()]; |
| 1326 | for (uint32_t i = 0; i < array_length; ++i) { |
| 1327 | auto array_indices2 = array_indices; |
| 1328 | array_indices2.emplace_back(i); |
| 1329 | RunUsedArray(src, offset, array_indices2, access_chain_word_index, access_chain_it, data); |
| 1330 | } |
| 1331 | } else { |
| 1332 | SetUsedBytes(offset, array_indices, data); |
| 1333 | } |
| 1334 | } |
| 1335 | } else { |
| 1336 | offset = UpdateOffset(offset, array_indices, data); |
| 1337 | RunUsedStruct(src, offset, access_chain_word_index, access_chain_it, data); |
| 1338 | } |
| 1339 | } else { |
| 1340 | SetUsedBytes(offset, array_indices, data); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | void RunUsedStruct(const SHADER_MODULE_STATE &src, uint32_t offset, uint32_t access_chain_word_index, |
| 1345 | spirv_inst_iter &access_chain_it, const shader_struct_member &data) { |
| 1346 | std::vector<uint32_t> array_indices_emptry; |
| 1347 | |
| 1348 | if (access_chain_word_index < access_chain_it.len()) { |
| 1349 | auto strcut_member_index = GetConstantValue(&src, access_chain_it.word(access_chain_word_index)); |
| 1350 | ++access_chain_word_index; |
| 1351 | |
| 1352 | auto data1 = data.struct_members[strcut_member_index]; |
| 1353 | RunUsedArray(src, offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | void SetUsedStructMember(const SHADER_MODULE_STATE &src, const uint32_t variable_id, |
| 1358 | const std::vector<function_set> &function_set_list, const shader_struct_member &data) { |
| 1359 | for (const auto &func_set : function_set_list) { |
| 1360 | auto range = func_set.op_lists.equal_range(spv::OpAccessChain); |
| 1361 | for (auto it = range.first; it != range.second; ++it) { |
| 1362 | auto access_chain = src.at(it->second); |
| 1363 | if (access_chain.word(3) == variable_id) { |
| 1364 | RunUsedStruct(src, 0, 4, access_chain, data); |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | void SetPushConstantUsedInShader(SHADER_MODULE_STATE &src) { |
| 1371 | for (auto &entrypoint : src.entry_points) { |
| 1372 | auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable); |
| 1373 | for (auto it = range.first; it != range.second; ++it) { |
| 1374 | const auto def_insn = src.at(it->second); |
| 1375 | |
| 1376 | if (def_insn.word(3) == spv::StorageClassPushConstant) { |
| 1377 | spirv_inst_iter type = src.get_def(def_insn.word(1)); |
| 1378 | const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate); |
| 1379 | std::vector<uint32_t> offsets; |
| 1380 | |
| 1381 | for (auto it2 = range2.first; it2 != range2.second; ++it2) { |
| 1382 | auto member_decorate = src.at(it2->second); |
| 1383 | if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) { |
| 1384 | offsets.emplace_back(member_decorate.offset()); |
| 1385 | } |
| 1386 | } |
| 1387 | entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader; |
| 1388 | DefineStructMember(src, type, offsets, entrypoint.second.push_constant_used_in_shader); |
| 1389 | SetUsedStructMember(src, def_insn.word(2), entrypoint.second.function_set_list, |
| 1390 | entrypoint.second.push_constant_used_in_shader); |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
locke-lunarg | 96dc963 | 2020-06-10 17:22:18 -0600 | [diff] [blame] | 1396 | std::unordered_set<uint32_t> CollectWritableOutputLocationinFS(const SHADER_MODULE_STATE &module, |
| 1397 | const VkPipelineShaderStageCreateInfo &stage_info) { |
| 1398 | std::unordered_set<uint32_t> location_list; |
| 1399 | if (stage_info.stage != VK_SHADER_STAGE_FRAGMENT_BIT) return location_list; |
| 1400 | const auto entrypoint = FindEntrypoint(&module, stage_info.pName, stage_info.stage); |
| 1401 | const auto outputs = CollectInterfaceByLocation(&module, entrypoint, spv::StorageClassOutput, false); |
| 1402 | std::unordered_set<unsigned> store_members; |
| 1403 | std::unordered_map<unsigned, unsigned> accesschain_members; |
| 1404 | |
| 1405 | for (auto insn : module) { |
| 1406 | switch (insn.opcode()) { |
| 1407 | case spv::OpStore: |
| 1408 | case spv::OpAtomicStore: { |
| 1409 | store_members.insert(insn.word(1)); // object id or AccessChain id |
| 1410 | break; |
| 1411 | } |
| 1412 | case spv::OpAccessChain: { |
| 1413 | // 2: AccessChain id, 3: object id |
| 1414 | if (insn.word(3)) accesschain_members.insert(std::make_pair(insn.word(2), insn.word(3))); |
| 1415 | break; |
| 1416 | } |
| 1417 | default: |
| 1418 | break; |
| 1419 | } |
| 1420 | } |
| 1421 | if (store_members.empty()) { |
| 1422 | return location_list; |
| 1423 | } |
| 1424 | for (auto output : outputs) { |
| 1425 | auto store_it = store_members.find(output.second.id); |
| 1426 | if (store_it != store_members.end()) { |
| 1427 | location_list.insert(output.first.first); |
| 1428 | store_members.erase(store_it); |
| 1429 | continue; |
| 1430 | } |
| 1431 | store_it = store_members.begin(); |
| 1432 | while (store_it != store_members.end()) { |
| 1433 | auto accesschain_it = accesschain_members.find(*store_it); |
| 1434 | if (accesschain_it == accesschain_members.end()) { |
| 1435 | ++store_it; |
| 1436 | continue; |
| 1437 | } |
| 1438 | if (accesschain_it->second == output.second.id) { |
| 1439 | location_list.insert(output.first.first); |
| 1440 | store_members.erase(store_it); |
| 1441 | accesschain_members.erase(accesschain_it); |
| 1442 | break; |
| 1443 | } |
| 1444 | ++store_it; |
| 1445 | } |
| 1446 | } |
| 1447 | return location_list; |
| 1448 | } |
| 1449 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1450 | bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1451 | // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should |
| 1452 | // be specified only once. |
| 1453 | std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings; |
| 1454 | bool skip = false; |
| 1455 | |
| 1456 | for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) { |
| 1457 | auto desc = &vi->pVertexBindingDescriptions[i]; |
| 1458 | auto &binding = bindings[desc->binding]; |
| 1459 | if (binding) { |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 1460 | // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps? |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1461 | skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d", |
| 1462 | desc->binding); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1463 | } else { |
| 1464 | binding = desc; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | return skip; |
| 1469 | } |
| 1470 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1471 | bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs, |
| 1472 | spirv_inst_iter entrypoint) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1473 | bool skip = false; |
| 1474 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1475 | const auto inputs = CollectInterfaceByLocation(vs, entrypoint, spv::StorageClassInput, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1476 | |
| 1477 | // Build index by location |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1478 | std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1479 | if (vi) { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1480 | for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) { |
| 1481 | const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format); |
| 1482 | for (uint32_t j = 0; j < num_locations; ++j) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1483 | attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i]; |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1488 | struct AttribInputPair { |
| 1489 | const VkVertexInputAttributeDescription *attrib = nullptr; |
| 1490 | const interface_var *input = nullptr; |
| 1491 | }; |
| 1492 | std::map<uint32_t, AttribInputPair> location_map; |
| 1493 | for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second; |
| 1494 | 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] | 1495 | |
Jamie Madill | c1f7ca8 | 2020-03-16 17:08:26 -0400 | [diff] [blame] | 1496 | for (const auto &location_it : location_map) { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1497 | const auto location = location_it.first; |
| 1498 | const auto attrib = location_it.second.attrib; |
| 1499 | const auto input = location_it.second.input; |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1500 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1501 | if (attrib && !input) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1502 | skip |= LogPerformanceWarning(vs->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 1503 | "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1504 | } else if (!attrib && input) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1505 | skip |= LogError(vs->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 1506 | "Vertex shader consumes input at location %" PRIu32 " but not provided", location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1507 | } else if (attrib && input) { |
| 1508 | const auto attrib_type = GetFormatType(attrib->format); |
| 1509 | const auto input_type = GetFundamentalType(vs, input->type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1510 | |
| 1511 | // Type checking |
| 1512 | if (!(attrib_type & input_type)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1513 | skip |= LogError(vs->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 1514 | "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`", |
| 1515 | string_VkFormat(attrib->format), location, DescribeType(vs, input->type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1516 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1517 | } else { // !attrib && !input |
| 1518 | assert(false); // at least one exists in the map |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | return skip; |
| 1523 | } |
| 1524 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1525 | bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint, |
| 1526 | PIPELINE_STATE const *pipeline, uint32_t subpass_index) const { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1527 | bool skip = false; |
Chris Forbes | 8bca165 | 2017-07-20 11:10:09 -0700 | [diff] [blame] | 1528 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1529 | const auto rpci = pipeline->rp_state->createInfo.ptr(); |
| 1530 | |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1531 | struct Attachment { |
| 1532 | const VkAttachmentReference2KHR *reference = nullptr; |
| 1533 | const VkAttachmentDescription2KHR *attachment = nullptr; |
| 1534 | const interface_var *output = nullptr; |
| 1535 | }; |
| 1536 | std::map<uint32_t, Attachment> location_map; |
| 1537 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1538 | const auto subpass = rpci->pSubpasses[subpass_index]; |
| 1539 | for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) { |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1540 | auto const &reference = subpass.pColorAttachments[i]; |
| 1541 | location_map[i].reference = &reference; |
| 1542 | if (reference.attachment != VK_ATTACHMENT_UNUSED && |
| 1543 | rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) { |
| 1544 | location_map[i].attachment = &rpci->pAttachments[reference.attachment]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1548 | // TODO: dual source blend index (spv::DecIndex, zero if not provided) |
| 1549 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1550 | const auto outputs = CollectInterfaceByLocation(fs, entrypoint, spv::StorageClassOutput, false); |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1551 | for (const auto &output_it : outputs) { |
| 1552 | auto const location = output_it.first.first; |
| 1553 | location_map[location].output = &output_it.second; |
| 1554 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1555 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1556 | const bool alphaToCoverageEnabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL && |
| 1557 | pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1558 | |
Jamie Madill | c1f7ca8 | 2020-03-16 17:08:26 -0400 | [diff] [blame] | 1559 | for (const auto &location_it : location_map) { |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1560 | const auto reference = location_it.second.reference; |
| 1561 | if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) { |
| 1562 | continue; |
| 1563 | } |
| 1564 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1565 | const auto location = location_it.first; |
| 1566 | const auto attachment = location_it.second.attachment; |
| 1567 | const auto output = location_it.second.output; |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1568 | if (attachment && !output) { |
| 1569 | if (pipeline->attachments[location].colorWriteMask != 0) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1570 | skip |= LogWarning(fs->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 1571 | "Attachment %" PRIu32 |
| 1572 | " not written by fragment shader; undefined values will be written to attachment", |
| 1573 | location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1574 | } |
| 1575 | } else if (!attachment && output) { |
| 1576 | if (!(alphaToCoverageEnabled && location == 0)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1577 | skip |= LogWarning(fs->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 1578 | "fragment shader writes to output location %" PRIu32 " with no matching attachment", location); |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1579 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1580 | } else if (attachment && output) { |
| 1581 | const auto attachment_type = GetFormatType(attachment->format); |
| 1582 | const auto output_type = GetFundamentalType(fs, output->type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1583 | |
| 1584 | // Type checking |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1585 | if (!(output_type & attachment_type)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1586 | skip |= |
| 1587 | LogWarning(fs->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 1588 | "Attachment %" PRIu32 |
| 1589 | " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined", |
| 1590 | location, string_VkFormat(attachment->format), DescribeType(fs, output->type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1591 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1592 | } else { // !attachment && !output |
| 1593 | assert(false); // at least one exists in the map |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1597 | const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr; |
| 1598 | bool locationZeroHasAlpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() && |
| 1599 | GetComponentsConsumedByType(fs, output_zero->type_id, false) == 4; |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1600 | if (alphaToCoverageEnabled && !locationZeroHasAlpha) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1601 | skip |= LogError(fs->vk_shader_module, kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage, |
| 1602 | "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] | 1603 | } |
| 1604 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1605 | return skip; |
| 1606 | } |
| 1607 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame^] | 1608 | // 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] | 1609 | // 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^] | 1610 | 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] | 1611 | auto type = builtin_instr.opcode(); |
| 1612 | uint32_t target_id = builtin_instr.word(1); |
| 1613 | bool init_complete = false; |
| 1614 | |
| 1615 | if (type == spv::OpMemberDecorate) { |
| 1616 | // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs |
| 1617 | auto insn = entrypoint; |
| 1618 | while (!init_complete && (insn.opcode() != spv::OpFunction)) { |
| 1619 | switch (insn.opcode()) { |
| 1620 | case spv::OpTypePointer: |
| 1621 | if ((insn.word(3) == target_id) && (insn.word(2) == spv::StorageClassOutput)) { |
| 1622 | target_id = insn.word(1); |
| 1623 | } |
| 1624 | break; |
| 1625 | case spv::OpVariable: |
| 1626 | if (insn.word(1) == target_id) { |
| 1627 | target_id = insn.word(2); |
| 1628 | init_complete = true; |
| 1629 | } |
| 1630 | break; |
| 1631 | } |
| 1632 | insn++; |
| 1633 | } |
| 1634 | } |
| 1635 | |
Mark Lobodzinski | f84b0b4 | 2018-09-11 14:54:32 -0600 | [diff] [blame] | 1636 | if (!init_complete && (type == spv::OpMemberDecorate)) return false; |
| 1637 | |
| 1638 | bool found_write = false; |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1639 | std::unordered_set<uint32_t> worklist; |
| 1640 | worklist.insert(entrypoint.word(2)); |
| 1641 | |
| 1642 | // Follow instructions in call graph looking for writes to target |
| 1643 | while (!worklist.empty() && !found_write) { |
| 1644 | auto id_iter = worklist.begin(); |
| 1645 | auto id = *id_iter; |
| 1646 | worklist.erase(id_iter); |
| 1647 | |
| 1648 | auto insn = src->get_def(id); |
| 1649 | if (insn == src->end()) { |
| 1650 | continue; |
| 1651 | } |
| 1652 | |
| 1653 | if (insn.opcode() == spv::OpFunction) { |
| 1654 | // Scan body of function looking for other function calls or items in our ID chain |
| 1655 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1656 | switch (insn.opcode()) { |
| 1657 | case spv::OpAccessChain: |
| 1658 | if (insn.word(3) == target_id) { |
| 1659 | if (type == spv::OpMemberDecorate) { |
| 1660 | auto value = GetConstantValue(src, insn.word(4)); |
| 1661 | if (value == builtin_instr.word(2)) { |
| 1662 | target_id = insn.word(2); |
| 1663 | } |
| 1664 | } else { |
| 1665 | target_id = insn.word(2); |
| 1666 | } |
| 1667 | } |
| 1668 | break; |
| 1669 | case spv::OpStore: |
| 1670 | if (insn.word(1) == target_id) { |
| 1671 | found_write = true; |
| 1672 | } |
| 1673 | break; |
| 1674 | case spv::OpFunctionCall: |
| 1675 | worklist.insert(insn.word(3)); |
| 1676 | break; |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | } |
| 1681 | return found_write; |
| 1682 | } |
| 1683 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1684 | // For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is |
| 1685 | // important for identifying the set of shader resources actually used by an entrypoint, for example. |
| 1686 | // Note: we only explore parts of the image which might actually contain ids we care about for the above analyses. |
| 1687 | // - NOT the shader input/output interfaces. |
| 1688 | // |
| 1689 | // TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth |
| 1690 | // 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] | 1691 | 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] | 1692 | std::unordered_set<uint32_t> ids; |
| 1693 | std::unordered_set<uint32_t> worklist; |
| 1694 | worklist.insert(entrypoint.word(2)); |
| 1695 | |
| 1696 | while (!worklist.empty()) { |
| 1697 | auto id_iter = worklist.begin(); |
| 1698 | auto id = *id_iter; |
| 1699 | worklist.erase(id_iter); |
| 1700 | |
| 1701 | auto insn = src->get_def(id); |
| 1702 | if (insn == src->end()) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1703 | // 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] | 1704 | // that we may not care about. |
| 1705 | continue; |
| 1706 | } |
| 1707 | |
| 1708 | // Try to add to the output set |
| 1709 | if (!ids.insert(id).second) { |
| 1710 | continue; // If we already saw this id, we don't want to walk it again. |
| 1711 | } |
| 1712 | |
| 1713 | switch (insn.opcode()) { |
| 1714 | case spv::OpFunction: |
| 1715 | // Scan whole body of the function, enlisting anything interesting |
| 1716 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1717 | switch (insn.opcode()) { |
| 1718 | case spv::OpLoad: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1719 | worklist.insert(insn.word(3)); // ptr |
| 1720 | break; |
| 1721 | case spv::OpStore: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1722 | worklist.insert(insn.word(1)); // ptr |
| 1723 | break; |
| 1724 | case spv::OpAccessChain: |
| 1725 | case spv::OpInBoundsAccessChain: |
| 1726 | worklist.insert(insn.word(3)); // base ptr |
| 1727 | break; |
| 1728 | case spv::OpSampledImage: |
| 1729 | case spv::OpImageSampleImplicitLod: |
| 1730 | case spv::OpImageSampleExplicitLod: |
| 1731 | case spv::OpImageSampleDrefImplicitLod: |
| 1732 | case spv::OpImageSampleDrefExplicitLod: |
| 1733 | case spv::OpImageSampleProjImplicitLod: |
| 1734 | case spv::OpImageSampleProjExplicitLod: |
| 1735 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1736 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1737 | case spv::OpImageFetch: |
| 1738 | case spv::OpImageGather: |
| 1739 | case spv::OpImageDrefGather: |
| 1740 | case spv::OpImageRead: |
| 1741 | case spv::OpImage: |
| 1742 | case spv::OpImageQueryFormat: |
| 1743 | case spv::OpImageQueryOrder: |
| 1744 | case spv::OpImageQuerySizeLod: |
| 1745 | case spv::OpImageQuerySize: |
| 1746 | case spv::OpImageQueryLod: |
| 1747 | case spv::OpImageQueryLevels: |
| 1748 | case spv::OpImageQuerySamples: |
| 1749 | case spv::OpImageSparseSampleImplicitLod: |
| 1750 | case spv::OpImageSparseSampleExplicitLod: |
| 1751 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 1752 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 1753 | case spv::OpImageSparseSampleProjImplicitLod: |
| 1754 | case spv::OpImageSparseSampleProjExplicitLod: |
| 1755 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 1756 | case spv::OpImageSparseSampleProjDrefExplicitLod: |
| 1757 | case spv::OpImageSparseFetch: |
| 1758 | case spv::OpImageSparseGather: |
| 1759 | case spv::OpImageSparseDrefGather: |
| 1760 | case spv::OpImageTexelPointer: |
| 1761 | worklist.insert(insn.word(3)); // Image or sampled image |
| 1762 | break; |
| 1763 | case spv::OpImageWrite: |
| 1764 | worklist.insert(insn.word(1)); // Image -- different operand order to above |
| 1765 | break; |
| 1766 | case spv::OpFunctionCall: |
| 1767 | for (uint32_t i = 3; i < insn.len(); i++) { |
| 1768 | worklist.insert(insn.word(i)); // fn itself, and all args |
| 1769 | } |
| 1770 | break; |
| 1771 | |
| 1772 | case spv::OpExtInst: |
| 1773 | for (uint32_t i = 5; i < insn.len(); i++) { |
| 1774 | worklist.insert(insn.word(i)); // Operands to ext inst |
| 1775 | } |
| 1776 | break; |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1777 | |
| 1778 | default: { |
| 1779 | if (AtomicOperation(insn.opcode())) { |
| 1780 | if (insn.opcode() == spv::OpAtomicStore) { |
| 1781 | worklist.insert(insn.word(1)); // ptr |
| 1782 | } else { |
| 1783 | worklist.insert(insn.word(3)); // ptr |
| 1784 | } |
| 1785 | } |
| 1786 | break; |
| 1787 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | break; |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | return ids; |
| 1795 | } |
| 1796 | |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1797 | PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update, |
| 1798 | const shader_struct_member &push_constant_used_in_shader, |
| 1799 | uint32_t &out_issue_index) const { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1800 | const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes(); |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1801 | const auto used_bytes_size = used_bytes->size(); |
| 1802 | if (used_bytes_size == 0) return PC_Byte_Updated; |
| 1803 | |
| 1804 | const auto push_constant_data_update_size = push_constant_data_update.size(); |
| 1805 | const auto *data = push_constant_data_update.data(); |
| 1806 | if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) { |
| 1807 | if (used_bytes_size <= push_constant_data_update_size) { |
| 1808 | return PC_Byte_Updated; |
| 1809 | } |
| 1810 | const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size; |
| 1811 | |
| 1812 | const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size; |
| 1813 | if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) { |
| 1814 | return PC_Byte_Updated; |
| 1815 | } |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1816 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1817 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1818 | uint32_t i = 0; |
| 1819 | for (const auto used : *used_bytes) { |
| 1820 | if (used) { |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1821 | 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] | 1822 | out_issue_index = i; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1823 | return PC_Byte_Not_Set; |
| 1824 | } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1825 | out_issue_index = i; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1826 | return PC_Byte_Not_Updated; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | ++i; |
| 1830 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1831 | return PC_Byte_Updated; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src, |
| 1835 | VkPipelineShaderStageCreateInfo const *pStage) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1836 | bool skip = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1837 | // 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] | 1838 | const auto *entrypoint = FindEntrypointStruct(src, pStage->pName, pStage->stage); |
| 1839 | if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) { |
| 1840 | return skip; |
| 1841 | } |
| 1842 | 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] | 1843 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1844 | bool found_stage = false; |
| 1845 | for (auto const &range : *push_constant_ranges) { |
| 1846 | if (range.stageFlags & pStage->stage) { |
| 1847 | found_stage = true; |
| 1848 | std::string location_desc; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1849 | std::vector<uint8_t> push_constant_bytes_set; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1850 | if (range.offset > 0) { |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1851 | push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1852 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1853 | push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1854 | uint32_t issue_index = 0; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1855 | const auto ret = |
| 1856 | 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] | 1857 | |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1858 | if (ret == PC_Byte_Not_Set) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1859 | const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index); |
| 1860 | LogObjectList objlist(src->vk_shader_module); |
| 1861 | objlist.add(pipeline.pipeline_layout->layout); |
| 1862 | skip |= LogError(objlist, kVUID_Core_Shader_PushConstantOutOfRange, |
| 1863 | "Push-constant buffer:%s in %s is out of range in %s.", loc_descr.c_str(), |
| 1864 | string_VkShaderStageFlags(pStage->stage).c_str(), |
| 1865 | report_data->FormatHandle(pipeline.pipeline_layout->layout).c_str()); |
| 1866 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1871 | if (!found_stage) { |
| 1872 | LogObjectList objlist(src->vk_shader_module); |
| 1873 | objlist.add(pipeline.pipeline_layout->layout); |
| 1874 | skip |= LogError( |
| 1875 | objlist, kVUID_Core_Shader_PushConstantOutOfRange, "Push constant is used in %s of %s. But %s doesn't set %s.", |
| 1876 | string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module).c_str(), |
| 1877 | 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] | 1878 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1879 | return skip; |
| 1880 | } |
| 1881 | |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 1882 | bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, const std::unordered_set<uint32_t> &accessible_ids, |
| 1883 | VkShaderStageFlagBits stage) const { |
| 1884 | bool skip = false; |
| 1885 | |
| 1886 | // Currently all builtin tested are only found in fragment shaders |
| 1887 | if (stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 1888 | return skip; |
| 1889 | } |
| 1890 | |
| 1891 | for (const auto id : accessible_ids) { |
| 1892 | auto insn = src->get_def(id); |
| 1893 | const decoration_set decorations = src->get_decorations(insn.word(2)); |
| 1894 | |
| 1895 | // Built-ins are obtained from OpVariable |
| 1896 | if (((decorations.flags & decoration_set::builtin_bit) != 0) && (insn.opcode() == spv::OpVariable)) { |
| 1897 | auto type_pointer = src->get_def(insn.word(1)); |
| 1898 | assert(type_pointer.opcode() == spv::OpTypePointer); |
| 1899 | |
| 1900 | auto type = src->get_def(type_pointer.word(3)); |
| 1901 | if (type.opcode() == spv::OpTypeArray) { |
| 1902 | uint32_t length = static_cast<uint32_t>(GetConstantValue(src, type.word(3))); |
| 1903 | |
| 1904 | switch (decorations.builtin) { |
| 1905 | case spv::BuiltInSampleMask: |
| 1906 | // Handles both the input and output sampleMask |
| 1907 | if (length > phys_dev_props.limits.maxSampleMaskWords) { |
| 1908 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711", |
| 1909 | "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds " |
| 1910 | "maxSampleMaskWords of %u in %s.", |
| 1911 | length, phys_dev_props.limits.maxSampleMaskWords, |
| 1912 | report_data->FormatHandle(src->vk_shader_module).c_str()); |
| 1913 | } |
| 1914 | break; |
| 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | return skip; |
| 1921 | } |
| 1922 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1923 | // 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] | 1924 | bool CoreChecks::ValidateSpecializationOffsets(VkPipelineShaderStageCreateInfo const *info) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1925 | bool skip = false; |
| 1926 | |
| 1927 | VkSpecializationInfo const *spec = info->pSpecializationInfo; |
| 1928 | |
| 1929 | if (spec) { |
| 1930 | for (auto i = 0u; i < spec->mapEntryCount; i++) { |
Jeremy Hayes | 6c555c3 | 2019-09-09 17:14:09 -0600 | [diff] [blame] | 1931 | if (spec->pMapEntries[i].offset >= spec->dataSize) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1932 | skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773", |
| 1933 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
| 1934 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..", |
| 1935 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
| 1936 | spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize); |
Jeremy Hayes | 6c555c3 | 2019-09-09 17:14:09 -0600 | [diff] [blame] | 1937 | |
| 1938 | continue; |
| 1939 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1940 | if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1941 | skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774", |
| 1942 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
| 1943 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..", |
| 1944 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
| 1945 | spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | return skip; |
| 1951 | } |
| 1952 | |
Jeff Bolz | 38b3ce7 | 2018-09-19 12:53:38 -0500 | [diff] [blame] | 1953 | // TODO (jbolz): Can this return a const reference? |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 1954 | static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count, |
| 1955 | bool is_khr) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1956 | auto type = module->get_def(type_id); |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1957 | bool is_storage_buffer = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1958 | descriptor_count = 1; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1959 | std::set<uint32_t> ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1960 | |
| 1961 | // 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] | 1962 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) { |
| 1963 | if (type.opcode() == spv::OpTypeRuntimeArray) { |
| 1964 | descriptor_count = 0; |
| 1965 | type = module->get_def(type.word(2)); |
| 1966 | } else if (type.opcode() == spv::OpTypeArray) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1967 | descriptor_count *= GetConstantValue(module, type.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1968 | type = module->get_def(type.word(2)); |
| 1969 | } else { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1970 | if (type.word(2) == spv::StorageClassStorageBuffer) { |
| 1971 | is_storage_buffer = true; |
| 1972 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1973 | type = module->get_def(type.word(3)); |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | switch (type.opcode()) { |
| 1978 | case spv::OpTypeStruct: { |
| 1979 | for (auto insn : *module) { |
| 1980 | if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) { |
| 1981 | if (insn.word(2) == spv::DecorationBlock) { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1982 | if (is_storage_buffer) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1983 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 1984 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 1985 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1986 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1987 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 1988 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC); |
| 1989 | ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT); |
| 1990 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1991 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1992 | } else if (insn.word(2) == spv::DecorationBufferBlock) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1993 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 1994 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 1995 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | // Invalid |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2001 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | case spv::OpTypeSampler: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2005 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER); |
| 2006 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2007 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2008 | |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2009 | case spv::OpTypeSampledImage: { |
| 2010 | // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel |
| 2011 | // buffer descriptor doesn't really provide one. Allow this slight mismatch. |
| 2012 | auto image_type = module->get_def(type.word(2)); |
| 2013 | auto dim = image_type.word(3); |
| 2014 | auto sampled = image_type.word(7); |
| 2015 | if (dim == spv::DimBuffer && sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2016 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 2017 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2018 | } |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2019 | } |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2020 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2021 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2022 | |
| 2023 | case spv::OpTypeImage: { |
| 2024 | // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler. |
| 2025 | // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable. |
| 2026 | auto dim = type.word(3); |
| 2027 | auto sampled = type.word(7); |
| 2028 | |
| 2029 | if (dim == spv::DimSubpassData) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2030 | ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT); |
| 2031 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2032 | } else if (dim == spv::DimBuffer) { |
| 2033 | if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2034 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 2035 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2036 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2037 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER); |
| 2038 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2039 | } |
| 2040 | } else if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2041 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); |
| 2042 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2043 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2044 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2045 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); |
| 2046 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2047 | } |
| 2048 | } |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 2049 | case spv::OpTypeAccelerationStructureNV: |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2050 | is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR) |
| 2051 | : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV); |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 2052 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2053 | |
| 2054 | // We shouldn't really see any other junk types -- but if we do, they're a mismatch. |
| 2055 | default: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2056 | return ret; // Matches nothing |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2057 | } |
| 2058 | } |
| 2059 | |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2060 | static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) { |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2061 | std::stringstream ss; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2062 | for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) { |
| 2063 | if (ss.tellp()) ss << ", "; |
| 2064 | ss << string_VkDescriptorType(VkDescriptorType(*it)); |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2065 | } |
| 2066 | return ss.str(); |
| 2067 | } |
| 2068 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2069 | bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2070 | if (!check) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2071 | if (LogError(device, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2072 | "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] | 2073 | return true; |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | return false; |
| 2078 | } |
| 2079 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2080 | bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2081 | if (!feature) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2082 | if (LogError(device, kVUID_Core_Shader_FeatureNotEnabled, "Shader requires %s but is not enabled on the device", |
| 2083 | feature_name)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2084 | return true; |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | return false; |
| 2089 | } |
| 2090 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2091 | bool CoreChecks::RequireExtension(bool extension, char const *extension_name) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2092 | if (!extension) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2093 | if (LogError(device, kVUID_Core_Shader_FeatureNotEnabled, "Shader requires extension %s but is not enabled on the device", |
| 2094 | extension_name)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2095 | return true; |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2102 | bool CoreChecks::ValidateShaderCapabilities(SHADER_MODULE_STATE const *src, VkShaderStageFlagBits stage) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2103 | bool skip = false; |
| 2104 | |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 2105 | struct FeaturePointer { |
| 2106 | // Callable object to test if this feature is enabled in the given aggregate feature struct |
| 2107 | const std::function<VkBool32(const DeviceFeatures &)> IsEnabled; |
| 2108 | |
| 2109 | // Test if feature pointer is populated |
| 2110 | explicit operator bool() const { return static_cast<bool>(IsEnabled); } |
| 2111 | |
| 2112 | // Default and nullptr constructor to create an empty FeaturePointer |
| 2113 | FeaturePointer() : IsEnabled(nullptr) {} |
| 2114 | FeaturePointer(std::nullptr_t ptr) : IsEnabled(nullptr) {} |
| 2115 | |
| 2116 | // Constructors to populate FeaturePointer based on given pointer to member |
| 2117 | FeaturePointer(VkBool32 VkPhysicalDeviceFeatures::*ptr) |
| 2118 | : IsEnabled([=](const DeviceFeatures &features) { return features.core.*ptr; }) {} |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2119 | FeaturePointer(VkBool32 VkPhysicalDeviceVulkan11Features::*ptr) |
| 2120 | : IsEnabled([=](const DeviceFeatures &features) { return features.core11.*ptr; }) {} |
| 2121 | FeaturePointer(VkBool32 VkPhysicalDeviceVulkan12Features::*ptr) |
| 2122 | : IsEnabled([=](const DeviceFeatures &features) { return features.core12.*ptr; }) {} |
Brett Lawson | bebfb6f | 2018-10-23 16:58:50 -0700 | [diff] [blame] | 2123 | FeaturePointer(VkBool32 VkPhysicalDeviceTransformFeedbackFeaturesEXT::*ptr) |
| 2124 | : IsEnabled([=](const DeviceFeatures &features) { return features.transform_feedback_features.*ptr; }) {} |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2125 | FeaturePointer(VkBool32 VkPhysicalDeviceCooperativeMatrixFeaturesNV::*ptr) |
| 2126 | : IsEnabled([=](const DeviceFeatures &features) { return features.cooperative_matrix_features.*ptr; }) {} |
Jason Macnak | c5a621d | 2019-06-10 12:42:50 -0700 | [diff] [blame] | 2127 | FeaturePointer(VkBool32 VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::*ptr) |
| 2128 | : IsEnabled([=](const DeviceFeatures &features) { return features.compute_shader_derivatives_features.*ptr; }) {} |
Jason Macnak | 325e8b5 | 2019-06-10 13:33:10 -0700 | [diff] [blame] | 2129 | FeaturePointer(VkBool32 VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::*ptr) |
| 2130 | : IsEnabled([=](const DeviceFeatures &features) { return features.fragment_shader_barycentric_features.*ptr; }) {} |
Jason Macnak | d7fddf8 | 2019-06-13 09:52:49 -0700 | [diff] [blame] | 2131 | FeaturePointer(VkBool32 VkPhysicalDeviceShaderImageFootprintFeaturesNV::*ptr) |
| 2132 | : IsEnabled([=](const DeviceFeatures &features) { return features.shader_image_footprint_features.*ptr; }) {} |
Jeff Bolz | 38f6cb5 | 2019-06-30 16:26:44 -0500 | [diff] [blame] | 2133 | FeaturePointer(VkBool32 VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::*ptr) |
| 2134 | : IsEnabled([=](const DeviceFeatures &features) { return features.fragment_shader_interlock_features.*ptr; }) {} |
Jeff Bolz | a38fd3b | 2019-07-21 11:42:11 -0500 | [diff] [blame] | 2135 | FeaturePointer(VkBool32 VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::*ptr) |
| 2136 | : IsEnabled([=](const DeviceFeatures &features) { return features.demote_to_helper_invocation_features.*ptr; }) {} |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2137 | FeaturePointer(VkBool32 VkPhysicalDeviceRayQueryFeaturesKHR::*ptr) |
| 2138 | : IsEnabled([=](const DeviceFeatures &features) { return features.ray_query_features.*ptr; }) {} |
| 2139 | FeaturePointer(VkBool32 VkPhysicalDeviceRayTracingPipelineFeaturesKHR::*ptr) |
| 2140 | : IsEnabled([=](const DeviceFeatures &features) { return features.ray_tracing_pipeline_features.*ptr; }) {} |
| 2141 | FeaturePointer(VkBool32 VkPhysicalDeviceAccelerationStructureFeaturesKHR::*ptr) |
| 2142 | : IsEnabled([=](const DeviceFeatures &features) { return features.ray_tracing_acceleration_structure_features.*ptr; }) { |
| 2143 | } |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 2144 | }; |
| 2145 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2146 | struct CapabilityInfo { |
| 2147 | char const *name; |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 2148 | FeaturePointer feature; |
Tony-LunarG | 2ec96bb | 2019-11-26 13:43:02 -0700 | [diff] [blame] | 2149 | ExtEnabled DeviceExtensions::*extension; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2150 | }; |
| 2151 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2152 | // clang-format off |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 2153 | static const std::unordered_multimap<uint32_t, CapabilityInfo> capabilities = { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2154 | // Capabilities always supported by a Vulkan 1.0 implementation -- no |
| 2155 | // feature bits. |
| 2156 | {spv::CapabilityMatrix, {nullptr}}, |
| 2157 | {spv::CapabilityShader, {nullptr}}, |
| 2158 | {spv::CapabilityInputAttachment, {nullptr}}, |
| 2159 | {spv::CapabilitySampled1D, {nullptr}}, |
| 2160 | {spv::CapabilityImage1D, {nullptr}}, |
| 2161 | {spv::CapabilitySampledBuffer, {nullptr}}, |
Toni Merilehti | b13a4a2 | 2019-05-21 12:58:44 +0300 | [diff] [blame] | 2162 | {spv::CapabilityStorageImageExtendedFormats, {nullptr}}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2163 | {spv::CapabilityImageQuery, {nullptr}}, |
| 2164 | {spv::CapabilityDerivativeControl, {nullptr}}, |
| 2165 | |
| 2166 | // Capabilities that are optionally supported, but require a feature to |
| 2167 | // be enabled on the device |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 2168 | {spv::CapabilityGeometry, {"VkPhysicalDeviceFeatures::geometryShader", &VkPhysicalDeviceFeatures::geometryShader}}, |
| 2169 | {spv::CapabilityTessellation, {"VkPhysicalDeviceFeatures::tessellationShader", &VkPhysicalDeviceFeatures::tessellationShader}}, |
| 2170 | {spv::CapabilityFloat64, {"VkPhysicalDeviceFeatures::shaderFloat64", &VkPhysicalDeviceFeatures::shaderFloat64}}, |
| 2171 | {spv::CapabilityInt64, {"VkPhysicalDeviceFeatures::shaderInt64", &VkPhysicalDeviceFeatures::shaderInt64}}, |
| 2172 | {spv::CapabilityTessellationPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}}, |
| 2173 | {spv::CapabilityGeometryPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}}, |
| 2174 | {spv::CapabilityImageGatherExtended, {"VkPhysicalDeviceFeatures::shaderImageGatherExtended", &VkPhysicalDeviceFeatures::shaderImageGatherExtended}}, |
| 2175 | {spv::CapabilityStorageImageMultisample, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}}, |
| 2176 | {spv::CapabilityUniformBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing}}, |
| 2177 | {spv::CapabilitySampledImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing}}, |
| 2178 | {spv::CapabilityStorageBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}}, |
| 2179 | {spv::CapabilityStorageImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}}, |
| 2180 | {spv::CapabilityClipDistance, {"VkPhysicalDeviceFeatures::shaderClipDistance", &VkPhysicalDeviceFeatures::shaderClipDistance}}, |
| 2181 | {spv::CapabilityCullDistance, {"VkPhysicalDeviceFeatures::shaderCullDistance", &VkPhysicalDeviceFeatures::shaderCullDistance}}, |
| 2182 | {spv::CapabilityImageCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}}, |
| 2183 | {spv::CapabilitySampleRateShading, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}}, |
| 2184 | {spv::CapabilitySparseResidency, {"VkPhysicalDeviceFeatures::shaderResourceResidency", &VkPhysicalDeviceFeatures::shaderResourceResidency}}, |
| 2185 | {spv::CapabilityMinLod, {"VkPhysicalDeviceFeatures::shaderResourceMinLod", &VkPhysicalDeviceFeatures::shaderResourceMinLod}}, |
| 2186 | {spv::CapabilitySampledCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}}, |
| 2187 | {spv::CapabilityImageMSArray, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}}, |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 2188 | {spv::CapabilityInterpolationFunction, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}}, |
| 2189 | {spv::CapabilityStorageImageReadWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat}}, |
| 2190 | {spv::CapabilityStorageImageWriteWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat}}, |
| 2191 | {spv::CapabilityMultiViewport, {"VkPhysicalDeviceFeatures::multiViewport", &VkPhysicalDeviceFeatures::multiViewport}}, |
Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 2192 | |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 2193 | {spv::CapabilityShaderNonUniformEXT, {VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_descriptor_indexing}}, |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2194 | {spv::CapabilityRuntimeDescriptorArrayEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::runtimeDescriptorArray", &VkPhysicalDeviceVulkan12Features::runtimeDescriptorArray}}, |
| 2195 | {spv::CapabilityInputAttachmentArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderInputAttachmentArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderInputAttachmentArrayDynamicIndexing}}, |
| 2196 | {spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformTexelBufferArrayDynamicIndexing}}, |
| 2197 | {spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageTexelBufferArrayDynamicIndexing}}, |
| 2198 | {spv::CapabilityUniformBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformBufferArrayNonUniformIndexing}}, |
| 2199 | {spv::CapabilitySampledImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderSampledImageArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderSampledImageArrayNonUniformIndexing}}, |
| 2200 | {spv::CapabilityStorageBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageBufferArrayNonUniformIndexing}}, |
| 2201 | {spv::CapabilityStorageImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageImageArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageImageArrayNonUniformIndexing}}, |
| 2202 | {spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderInputAttachmentArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderInputAttachmentArrayNonUniformIndexing}}, |
| 2203 | {spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderUniformTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderUniformTexelBufferArrayNonUniformIndexing}}, |
| 2204 | {spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeatures::shaderStorageTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceVulkan12Features::shaderStorageTexelBufferArrayNonUniformIndexing}}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2205 | |
| 2206 | // Capabilities that require an extension |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 2207 | {spv::CapabilityDrawParameters, {VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_draw_parameters}}, |
| 2208 | {spv::CapabilityGeometryShaderPassthroughNV, {VK_NV_GEOMETRY_SHADER_PASSTHROUGH_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_geometry_shader_passthrough}}, |
| 2209 | {spv::CapabilitySampleMaskOverrideCoverageNV, {VK_NV_SAMPLE_MASK_OVERRIDE_COVERAGE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_sample_mask_override_coverage}}, |
| 2210 | {spv::CapabilityShaderViewportIndexLayerEXT, {VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_viewport_index_layer}}, |
| 2211 | {spv::CapabilityShaderViewportIndexLayerNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}}, |
| 2212 | {spv::CapabilityShaderViewportMaskNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}}, |
| 2213 | {spv::CapabilitySubgroupBallotKHR, {VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_ballot }}, |
| 2214 | {spv::CapabilitySubgroupVoteKHR, {VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_vote }}, |
Jason Macnak | b7d091c | 2019-06-10 11:13:11 -0700 | [diff] [blame] | 2215 | {spv::CapabilityGroupNonUniformPartitionedNV, {VK_NV_SHADER_SUBGROUP_PARTITIONED_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_shader_subgroup_partitioned}}, |
aqnuep | 7033c70 | 2018-09-11 18:03:29 +0200 | [diff] [blame] | 2216 | {spv::CapabilityInt64Atomics, {VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_atomic_int64 }}, |
amhagan | fa0b34d | 2019-10-15 16:03:53 -0400 | [diff] [blame] | 2217 | {spv::CapabilityShaderClockKHR, {VK_KHR_SHADER_CLOCK_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_clock }}, |
Alexander Galazin | 3bd8e34 | 2018-06-14 15:49:07 +0200 | [diff] [blame] | 2218 | |
Jason Macnak | c5a621d | 2019-06-10 12:42:50 -0700 | [diff] [blame] | 2219 | {spv::CapabilityComputeDerivativeGroupQuadsNV, {"VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupQuads", &VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupQuads, &DeviceExtensions::vk_nv_compute_shader_derivatives}}, |
| 2220 | {spv::CapabilityComputeDerivativeGroupLinearNV, {"VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupLinear", &VkPhysicalDeviceComputeShaderDerivativesFeaturesNV::computeDerivativeGroupLinear, &DeviceExtensions::vk_nv_compute_shader_derivatives}}, |
Jason Macnak | f701958 | 2019-06-13 10:07:26 -0700 | [diff] [blame] | 2221 | {spv::CapabilityFragmentBarycentricNV, {"VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::fragmentShaderBarycentric", &VkPhysicalDeviceFragmentShaderBarycentricFeaturesNV::fragmentShaderBarycentric, &DeviceExtensions::vk_nv_fragment_shader_barycentric}}, |
Jason Macnak | c5a621d | 2019-06-10 12:42:50 -0700 | [diff] [blame] | 2222 | |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2223 | {spv::CapabilityStorageBuffer8BitAccess, {"VkPhysicalDevice8BitStorageFeaturesKHR::storageBuffer8BitAccess", &VkPhysicalDeviceVulkan12Features::storageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}}, |
| 2224 | {spv::CapabilityUniformAndStorageBuffer8BitAccess, {"VkPhysicalDevice8BitStorageFeaturesKHR::uniformAndStorageBuffer8BitAccess", &VkPhysicalDeviceVulkan12Features::uniformAndStorageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}}, |
| 2225 | {spv::CapabilityStoragePushConstant8, {"VkPhysicalDevice8BitStorageFeaturesKHR::storagePushConstant8", &VkPhysicalDeviceVulkan12Features::storagePushConstant8, &DeviceExtensions::vk_khr_8bit_storage}}, |
Brett Lawson | bebfb6f | 2018-10-23 16:58:50 -0700 | [diff] [blame] | 2226 | |
Jason Macnak | f701958 | 2019-06-13 10:07:26 -0700 | [diff] [blame] | 2227 | {spv::CapabilityTransformFeedback, { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback, &DeviceExtensions::vk_ext_transform_feedback}}, |
| 2228 | {spv::CapabilityGeometryStreams, { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams, &DeviceExtensions::vk_ext_transform_feedback}}, |
Jose-Emilio Munoz-Lopez | 1109b45 | 2018-08-21 09:44:07 +0100 | [diff] [blame] | 2229 | |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2230 | {spv::CapabilityFloat16, {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderFloat16", &VkPhysicalDeviceVulkan12Features::shaderFloat16, &DeviceExtensions::vk_khr_shader_float16_int8}}, |
| 2231 | {spv::CapabilityInt8, {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderInt8", &VkPhysicalDeviceVulkan12Features::shaderInt8, &DeviceExtensions::vk_khr_shader_float16_int8}}, |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2232 | |
Jason Macnak | d7fddf8 | 2019-06-13 09:52:49 -0700 | [diff] [blame] | 2233 | {spv::CapabilityImageFootprintNV, {"VkPhysicalDeviceShaderImageFootprintFeaturesNV::imageFootprint", &VkPhysicalDeviceShaderImageFootprintFeaturesNV::imageFootprint, &DeviceExtensions::vk_nv_shader_image_footprint}}, |
| 2234 | |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2235 | {spv::CapabilityCooperativeMatrixNV, {"VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix", &VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix, &DeviceExtensions::vk_nv_cooperative_matrix}}, |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2236 | |
Graeme Leese | 41e6b84 | 2019-08-02 10:49:14 +0100 | [diff] [blame] | 2237 | {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserve", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 2238 | {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserve", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 2239 | {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZero", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 2240 | {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTE", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 2241 | {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZ", nullptr, &DeviceExtensions::vk_khr_shader_float_controls}}, |
Jeff Bolz | 38f6cb5 | 2019-06-30 16:26:44 -0500 | [diff] [blame] | 2242 | |
| 2243 | {spv::CapabilityFragmentShaderSampleInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderSampleInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderSampleInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}}, |
| 2244 | {spv::CapabilityFragmentShaderPixelInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderPixelInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderPixelInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}}, |
| 2245 | {spv::CapabilityFragmentShaderShadingRateInterlockEXT, {"VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderShadingRateInterlock", &VkPhysicalDeviceFragmentShaderInterlockFeaturesEXT::fragmentShaderShadingRateInterlock, &DeviceExtensions::vk_ext_fragment_shader_interlock}}, |
Jeff Bolz | a38fd3b | 2019-07-21 11:42:11 -0500 | [diff] [blame] | 2246 | {spv::CapabilityDemoteToHelperInvocationEXT, {"VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::shaderDemoteToHelperInvocation", &VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT::shaderDemoteToHelperInvocation, &DeviceExtensions::vk_ext_shader_demote_to_helper_invocation}}, |
Jeff Bolz | 4563f2a | 2019-12-10 13:30:30 -0600 | [diff] [blame] | 2247 | |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2248 | {spv::CapabilityPhysicalStorageBufferAddresses, {"VkPhysicalDeviceBufferDeviceAddressFeatures::bufferDeviceAddress", &VkPhysicalDeviceVulkan12Features::bufferDeviceAddress, &DeviceExtensions::vk_ext_buffer_device_address}}, |
Jeff Bolz | 4563f2a | 2019-12-10 13:30:30 -0600 | [diff] [blame] | 2249 | // Should be non-EXT token, but Android SPIRV-Headers are out of date, and the token value is the same anyway |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2250 | {spv::CapabilityPhysicalStorageBufferAddressesEXT, {"VkPhysicalDeviceBufferDeviceAddressFeaturesEXT::bufferDeviceAddress", &VkPhysicalDeviceVulkan12Features::bufferDeviceAddress, &DeviceExtensions::vk_khr_buffer_device_address}}, |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 2251 | |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2252 | {spv::CapabilityRayTracingKHR, {"VkPhysicalDeviceRayTracingPipelineFeaturesKHR::rayTracingPipeline", &VkPhysicalDeviceRayTracingPipelineFeaturesKHR::rayTracingPipeline, &DeviceExtensions::vk_khr_ray_tracing_pipeline }}, |
| 2253 | {spv::CapabilityRayQueryKHR, {"VkPhysicalDeviceRayQueryFeaturesKHR::rayQuery", &VkPhysicalDeviceRayQueryFeaturesKHR::rayQuery, &DeviceExtensions::vk_khr_ray_query }}, |
| 2254 | {spv::CapabilityRayTraversalPrimitiveCullingKHR, {"VkPhysicalDeviceRayTracingPipelineFeaturesKHR::rayTracingPrimitiveCulling", &VkPhysicalDeviceRayTracingPipelineFeaturesKHR::rayTraversalPrimitiveCulling, &DeviceExtensions::vk_khr_ray_tracing_pipeline }}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2255 | }; |
| 2256 | // clang-format on |
| 2257 | |
| 2258 | for (auto insn : *src) { |
| 2259 | if (insn.opcode() == spv::OpCapability) { |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 2260 | size_t n = capabilities.count(insn.word(1)); |
| 2261 | if (1 == n) { // key occurs exactly once |
| 2262 | auto it = capabilities.find(insn.word(1)); |
| 2263 | if (it != capabilities.end()) { |
| 2264 | if (it->second.feature) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2265 | skip |= RequireFeature(it->second.feature.IsEnabled(enabled_features), it->second.name); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 2266 | } |
| 2267 | if (it->second.extension) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2268 | skip |= RequireExtension(IsExtEnabled((device_extensions.*(it->second.extension))), it->second.name); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 2269 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2270 | } |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 2271 | } else if (1 < n) { // key occurs multiple times, at least one must be enabled |
| 2272 | bool needs_feature = false, has_feature = false; |
| 2273 | bool needs_ext = false, has_ext = false; |
| 2274 | std::string feature_names = "(one of) [ "; |
| 2275 | std::string extension_names = feature_names; |
| 2276 | auto caps = capabilities.equal_range(insn.word(1)); |
| 2277 | for (auto it = caps.first; it != caps.second; ++it) { |
| 2278 | if (it->second.feature) { |
| 2279 | needs_feature = true; |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2280 | has_feature = has_feature || it->second.feature.IsEnabled(enabled_features); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 2281 | feature_names += it->second.name; |
| 2282 | feature_names += " "; |
| 2283 | } |
| 2284 | if (it->second.extension) { |
| 2285 | needs_ext = true; |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 2286 | has_ext = has_ext || device_extensions.*(it->second.extension); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 2287 | extension_names += it->second.name; |
| 2288 | extension_names += " "; |
| 2289 | } |
| 2290 | } |
| 2291 | if (needs_feature) { |
| 2292 | feature_names += "]"; |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2293 | skip |= RequireFeature(has_feature, feature_names.c_str()); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 2294 | } |
| 2295 | if (needs_ext) { |
| 2296 | extension_names += "]"; |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2297 | skip |= RequireExtension(has_ext, extension_names.c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2298 | } |
Graeme Leese | c82dbe0 | 2019-08-02 10:44:21 +0100 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | { // Do group non-uniform checks |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2302 | const VkSubgroupFeatureFlags supportedOperations = phys_dev_props_core11.subgroupSupportedOperations; |
| 2303 | const VkSubgroupFeatureFlags supportedStages = phys_dev_props_core11.subgroupSupportedStages; |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2304 | |
| 2305 | switch (insn.word(1)) { |
| 2306 | default: |
| 2307 | break; |
| 2308 | case spv::CapabilityGroupNonUniform: |
| 2309 | case spv::CapabilityGroupNonUniformVote: |
| 2310 | case spv::CapabilityGroupNonUniformArithmetic: |
| 2311 | case spv::CapabilityGroupNonUniformBallot: |
| 2312 | case spv::CapabilityGroupNonUniformShuffle: |
| 2313 | case spv::CapabilityGroupNonUniformShuffleRelative: |
| 2314 | case spv::CapabilityGroupNonUniformClustered: |
| 2315 | case spv::CapabilityGroupNonUniformQuad: |
| 2316 | case spv::CapabilityGroupNonUniformPartitionedNV: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2317 | RequirePropertyFlag(supportedStages & stage, string_VkShaderStageFlagBits(stage), |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2318 | "VkPhysicalDeviceSubgroupProperties::supportedStages"); |
| 2319 | break; |
| 2320 | } |
| 2321 | |
| 2322 | switch (insn.word(1)) { |
| 2323 | default: |
| 2324 | break; |
| 2325 | case spv::CapabilityGroupNonUniform: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2326 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT, "VK_SUBGROUP_FEATURE_BASIC_BIT", |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2327 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2328 | break; |
| 2329 | case spv::CapabilityGroupNonUniformVote: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2330 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_VOTE_BIT, "VK_SUBGROUP_FEATURE_VOTE_BIT", |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2331 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2332 | break; |
| 2333 | case spv::CapabilityGroupNonUniformArithmetic: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2334 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_ARITHMETIC_BIT, |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2335 | "VK_SUBGROUP_FEATURE_ARITHMETIC_BIT", |
| 2336 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2337 | break; |
| 2338 | case spv::CapabilityGroupNonUniformBallot: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2339 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_BALLOT_BIT, "VK_SUBGROUP_FEATURE_BALLOT_BIT", |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2340 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2341 | break; |
| 2342 | case spv::CapabilityGroupNonUniformShuffle: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2343 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_BIT, |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2344 | "VK_SUBGROUP_FEATURE_SHUFFLE_BIT", |
| 2345 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2346 | break; |
| 2347 | case spv::CapabilityGroupNonUniformShuffleRelative: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2348 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT, |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2349 | "VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT", |
| 2350 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2351 | break; |
| 2352 | case spv::CapabilityGroupNonUniformClustered: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2353 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_CLUSTERED_BIT, |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2354 | "VK_SUBGROUP_FEATURE_CLUSTERED_BIT", |
| 2355 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2356 | break; |
| 2357 | case spv::CapabilityGroupNonUniformQuad: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2358 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_QUAD_BIT, "VK_SUBGROUP_FEATURE_QUAD_BIT", |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2359 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2360 | break; |
| 2361 | case spv::CapabilityGroupNonUniformPartitionedNV: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2362 | RequirePropertyFlag(supportedOperations & VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV, |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2363 | "VK_SUBGROUP_FEATURE_PARTITIONED_BIT_NV", |
| 2364 | "VkPhysicalDeviceSubgroupProperties::supportedOperations"); |
| 2365 | break; |
| 2366 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2367 | } |
baldurk | 4095f93 | 2020-02-16 13:24:42 +0000 | [diff] [blame] | 2368 | } else if (insn.opcode() == spv::OpExtension) { |
| 2369 | std::string extension_name = (char const *)&insn.word(1); |
| 2370 | |
| 2371 | if (extension_name == "SPV_KHR_non_semantic_info") { |
| 2372 | skip |= RequireExtension(IsExtEnabled(device_extensions.vk_khr_shader_non_semantic_info), |
| 2373 | VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME); |
| 2374 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2375 | } |
| 2376 | } |
| 2377 | |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2378 | return skip; |
| 2379 | } |
| 2380 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 2381 | bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor, |
| 2382 | bool has_atomic_descriptor) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2383 | bool skip = false; |
| 2384 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 2385 | if (has_writable_descriptor || has_atomic_descriptor) { |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2386 | switch (stage) { |
| 2387 | case VK_SHADER_STAGE_COMPUTE_BIT: |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2388 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 2389 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 2390 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 2391 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 2392 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 2393 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 2394 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 2395 | case VK_SHADER_STAGE_MESH_BIT_NV: |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2396 | /* No feature requirements for writes and atomics from compute |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2397 | * raytracing, or mesh stages */ |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2398 | break; |
| 2399 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2400 | skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2401 | break; |
| 2402 | default: |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2403 | skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics"); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2404 | break; |
| 2405 | } |
| 2406 | } |
| 2407 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2408 | return skip; |
| 2409 | } |
| 2410 | |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2411 | bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2412 | bool skip = false; |
| 2413 | |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2414 | auto const subgroup_props = phys_dev_props_core11; |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2415 | |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2416 | for (auto inst : *module) { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2417 | // Check the quad operations. |
| 2418 | switch (inst.opcode()) { |
| 2419 | default: |
| 2420 | break; |
| 2421 | case spv::OpGroupNonUniformQuadBroadcast: |
| 2422 | case spv::OpGroupNonUniformQuadSwap: |
| 2423 | 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] | 2424 | skip |= RequireFeature(subgroup_props.subgroupQuadOperationsInAllStages, |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2425 | "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages"); |
| 2426 | } |
| 2427 | break; |
| 2428 | } |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2429 | |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2430 | if (!enabled_features.core12.shaderSubgroupExtendedTypes) { |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2431 | switch (inst.opcode()) { |
| 2432 | default: |
| 2433 | break; |
| 2434 | case spv::OpGroupNonUniformAllEqual: |
| 2435 | case spv::OpGroupNonUniformBroadcast: |
| 2436 | case spv::OpGroupNonUniformBroadcastFirst: |
| 2437 | case spv::OpGroupNonUniformShuffle: |
| 2438 | case spv::OpGroupNonUniformShuffleXor: |
| 2439 | case spv::OpGroupNonUniformShuffleUp: |
| 2440 | case spv::OpGroupNonUniformShuffleDown: |
| 2441 | case spv::OpGroupNonUniformIAdd: |
| 2442 | case spv::OpGroupNonUniformFAdd: |
| 2443 | case spv::OpGroupNonUniformIMul: |
| 2444 | case spv::OpGroupNonUniformFMul: |
| 2445 | case spv::OpGroupNonUniformSMin: |
| 2446 | case spv::OpGroupNonUniformUMin: |
| 2447 | case spv::OpGroupNonUniformFMin: |
| 2448 | case spv::OpGroupNonUniformSMax: |
| 2449 | case spv::OpGroupNonUniformUMax: |
| 2450 | case spv::OpGroupNonUniformFMax: |
| 2451 | case spv::OpGroupNonUniformBitwiseAnd: |
| 2452 | case spv::OpGroupNonUniformBitwiseOr: |
| 2453 | case spv::OpGroupNonUniformBitwiseXor: |
| 2454 | case spv::OpGroupNonUniformLogicalAnd: |
| 2455 | case spv::OpGroupNonUniformLogicalOr: |
| 2456 | case spv::OpGroupNonUniformLogicalXor: |
| 2457 | case spv::OpGroupNonUniformQuadBroadcast: |
| 2458 | case spv::OpGroupNonUniformQuadSwap: { |
| 2459 | auto type = module->get_def(inst.word(1)); |
| 2460 | |
| 2461 | if (type.opcode() == spv::OpTypeVector) { |
| 2462 | // Get the element type |
| 2463 | type = module->get_def(type.word(2)); |
| 2464 | } |
| 2465 | |
| 2466 | if (type.opcode() == spv::OpTypeBool) { |
| 2467 | break; |
| 2468 | } |
| 2469 | |
| 2470 | // Both OpTypeInt and OpTypeFloat the width is in the 2nd word. |
| 2471 | const uint32_t width = type.word(2); |
| 2472 | |
| 2473 | if ((type.opcode() == spv::OpTypeFloat && width == 16) || |
| 2474 | (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2475 | skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes, |
Tony-LunarG | a74d3fe | 2019-11-22 15:43:20 -0700 | [diff] [blame] | 2476 | "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes"); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2477 | } |
| 2478 | break; |
| 2479 | } |
| 2480 | } |
| 2481 | } |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | return skip; |
| 2485 | } |
| 2486 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2487 | bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2488 | const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2489 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS || |
| 2490 | pStage->stage == VK_SHADER_STAGE_ALL) { |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
| 2494 | bool skip = false; |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2495 | auto const &limits = phys_dev_props.limits; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2496 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2497 | std::set<uint32_t> patchIDs; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2498 | struct Variable { |
| 2499 | uint32_t baseTypePtrID; |
| 2500 | uint32_t ID; |
| 2501 | uint32_t storageClass; |
| 2502 | }; |
| 2503 | std::vector<Variable> variables; |
| 2504 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2505 | uint32_t numVertices = 0; |
| 2506 | |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2507 | auto entrypointVariables = FindEntrypointInterfaces(entrypoint); |
| 2508 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2509 | for (auto insn : *src) { |
| 2510 | switch (insn.opcode()) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2511 | // Find all Patch decorations |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2512 | case spv::OpDecorate: |
| 2513 | switch (insn.word(2)) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2514 | case spv::DecorationPatch: { |
| 2515 | patchIDs.insert(insn.word(1)); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2516 | break; |
| 2517 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2518 | default: |
| 2519 | break; |
| 2520 | } |
| 2521 | break; |
| 2522 | // Find all input and output variables |
| 2523 | case spv::OpVariable: { |
| 2524 | Variable var = {}; |
| 2525 | var.storageClass = insn.word(3); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2526 | if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) && |
| 2527 | // Only include variables in the entrypoint's interface |
| 2528 | find(entrypointVariables.begin(), entrypointVariables.end(), insn.word(2)) != entrypointVariables.end()) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2529 | var.baseTypePtrID = insn.word(1); |
| 2530 | var.ID = insn.word(2); |
| 2531 | variables.push_back(var); |
| 2532 | } |
| 2533 | break; |
| 2534 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2535 | case spv::OpExecutionMode: |
| 2536 | if (insn.word(1) == entrypoint.word(2)) { |
| 2537 | switch (insn.word(2)) { |
| 2538 | default: |
| 2539 | break; |
| 2540 | case spv::ExecutionModeOutputVertices: |
| 2541 | numVertices = insn.word(3); |
| 2542 | break; |
| 2543 | } |
| 2544 | } |
| 2545 | break; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2546 | default: |
| 2547 | break; |
| 2548 | } |
| 2549 | } |
| 2550 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2551 | bool strip_output_array_level = |
| 2552 | (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV); |
| 2553 | bool strip_input_array_level = |
| 2554 | (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || |
| 2555 | pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT); |
| 2556 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2557 | uint32_t numCompIn = 0, numCompOut = 0; |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2558 | int maxCompIn = 0, maxCompOut = 0; |
| 2559 | |
| 2560 | auto inputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassInput, strip_input_array_level); |
| 2561 | auto outputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassOutput, strip_output_array_level); |
| 2562 | |
| 2563 | // Find max component location used for input variables. |
| 2564 | for (auto &var : inputs) { |
| 2565 | int location = var.first.first; |
| 2566 | int component = var.first.second; |
| 2567 | interface_var &iv = var.second; |
| 2568 | |
| 2569 | // Only need to look at the first location, since we use the type's whole size |
| 2570 | if (iv.offset != 0) { |
| 2571 | continue; |
| 2572 | } |
| 2573 | |
| 2574 | if (iv.is_patch) { |
| 2575 | continue; |
| 2576 | } |
| 2577 | |
| 2578 | int numComponents = GetComponentsConsumedByType(src, iv.type_id, strip_input_array_level); |
| 2579 | maxCompIn = std::max(maxCompIn, location * 4 + component + numComponents); |
| 2580 | } |
| 2581 | |
| 2582 | // Find max component location used for output variables. |
| 2583 | for (auto &var : outputs) { |
| 2584 | int location = var.first.first; |
| 2585 | int component = var.first.second; |
| 2586 | interface_var &iv = var.second; |
| 2587 | |
| 2588 | // Only need to look at the first location, since we use the type's whole size |
| 2589 | if (iv.offset != 0) { |
| 2590 | continue; |
| 2591 | } |
| 2592 | |
| 2593 | if (iv.is_patch) { |
| 2594 | continue; |
| 2595 | } |
| 2596 | |
| 2597 | int numComponents = GetComponentsConsumedByType(src, iv.type_id, strip_output_array_level); |
| 2598 | maxCompOut = std::max(maxCompOut, location * 4 + component + numComponents); |
| 2599 | } |
| 2600 | |
| 2601 | // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar), |
| 2602 | // but that doesn't include builtins. |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2603 | for (auto &var : variables) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2604 | // Check if the variable is a patch. Patches can also be members of blocks, |
| 2605 | // but if they are then the top-level arrayness has already been stripped |
| 2606 | // by the time GetComponentsConsumedByType gets to it. |
| 2607 | bool isPatch = patchIDs.find(var.ID) != patchIDs.end(); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2608 | |
| 2609 | if (var.storageClass == spv::StorageClassInput) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2610 | numCompIn += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_input_array_level && !isPatch); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2611 | } else { // var.storageClass == spv::StorageClassOutput |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2612 | numCompOut += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_output_array_level && !isPatch); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | switch (pStage->stage) { |
| 2617 | case VK_SHADER_STAGE_VERTEX_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 2618 | if (numCompOut > limits.maxVertexOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2619 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2620 | "Invalid Pipeline CreateInfo State: Vertex shader exceeds " |
| 2621 | "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u " |
| 2622 | "components by %u components", |
| 2623 | limits.maxVertexOutputComponents, numCompOut - limits.maxVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2624 | } |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2625 | if (maxCompOut > (int)limits.maxVertexOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2626 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2627 | "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that " |
| 2628 | "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)", |
| 2629 | limits.maxVertexOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2630 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2631 | break; |
| 2632 | |
| 2633 | case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 2634 | if (numCompIn > limits.maxTessellationControlPerVertexInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2635 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2636 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 2637 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u " |
| 2638 | "components by %u components", |
| 2639 | limits.maxTessellationControlPerVertexInputComponents, |
| 2640 | numCompIn - limits.maxTessellationControlPerVertexInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2641 | } |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2642 | if (maxCompIn > (int)limits.maxTessellationControlPerVertexInputComponents) { |
| 2643 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2644 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2645 | "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that " |
| 2646 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)", |
| 2647 | limits.maxTessellationControlPerVertexInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2648 | } |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 2649 | if (numCompOut > limits.maxTessellationControlPerVertexOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2650 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2651 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 2652 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u " |
| 2653 | "components by %u components", |
| 2654 | limits.maxTessellationControlPerVertexOutputComponents, |
| 2655 | numCompOut - limits.maxTessellationControlPerVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2656 | } |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2657 | if (maxCompOut > (int)limits.maxTessellationControlPerVertexOutputComponents) { |
| 2658 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2659 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2660 | "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that " |
| 2661 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)", |
| 2662 | limits.maxTessellationControlPerVertexOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2663 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2664 | break; |
| 2665 | |
| 2666 | case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 2667 | if (numCompIn > limits.maxTessellationEvaluationInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2668 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2669 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 2670 | "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u " |
| 2671 | "components by %u components", |
| 2672 | limits.maxTessellationEvaluationInputComponents, |
| 2673 | numCompIn - limits.maxTessellationEvaluationInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2674 | } |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2675 | if (maxCompIn > (int)limits.maxTessellationEvaluationInputComponents) { |
| 2676 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2677 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2678 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that " |
| 2679 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)", |
| 2680 | limits.maxTessellationEvaluationInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2681 | } |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 2682 | if (numCompOut > limits.maxTessellationEvaluationOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2683 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2684 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 2685 | "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u " |
| 2686 | "components by %u components", |
| 2687 | limits.maxTessellationEvaluationOutputComponents, |
| 2688 | numCompOut - limits.maxTessellationEvaluationOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2689 | } |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2690 | if (maxCompOut > (int)limits.maxTessellationEvaluationOutputComponents) { |
| 2691 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2692 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2693 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that " |
| 2694 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)", |
| 2695 | limits.maxTessellationEvaluationOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2696 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2697 | break; |
| 2698 | |
| 2699 | case VK_SHADER_STAGE_GEOMETRY_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 2700 | if (numCompIn > limits.maxGeometryInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2701 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2702 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2703 | "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u " |
| 2704 | "components by %u components", |
| 2705 | limits.maxGeometryInputComponents, numCompIn - limits.maxGeometryInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2706 | } |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2707 | if (maxCompIn > (int)limits.maxGeometryInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2708 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2709 | "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that " |
| 2710 | "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)", |
| 2711 | limits.maxGeometryInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2712 | } |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 2713 | if (numCompOut > limits.maxGeometryOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2714 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2715 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2716 | "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u " |
| 2717 | "components by %u components", |
| 2718 | limits.maxGeometryOutputComponents, numCompOut - limits.maxGeometryOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2719 | } |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2720 | if (maxCompOut > (int)limits.maxGeometryOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2721 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2722 | "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that " |
| 2723 | "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)", |
| 2724 | limits.maxGeometryOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2725 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2726 | if (numCompOut * numVertices > limits.maxGeometryTotalOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2727 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2728 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2729 | "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u " |
| 2730 | "components by %u components", |
| 2731 | limits.maxGeometryTotalOutputComponents, |
| 2732 | numCompOut * numVertices - limits.maxGeometryTotalOutputComponents); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2733 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2734 | break; |
| 2735 | |
| 2736 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 2737 | if (numCompIn > limits.maxFragmentInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2738 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2739 | "Invalid Pipeline CreateInfo State: Fragment shader exceeds " |
| 2740 | "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u " |
| 2741 | "components by %u components", |
| 2742 | limits.maxFragmentInputComponents, numCompIn - limits.maxFragmentInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2743 | } |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2744 | if (maxCompIn > (int)limits.maxFragmentInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2745 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2746 | "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that " |
| 2747 | "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)", |
| 2748 | limits.maxFragmentInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2749 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2750 | break; |
| 2751 | |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2752 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 2753 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 2754 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 2755 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 2756 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 2757 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 2758 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 2759 | case VK_SHADER_STAGE_MESH_BIT_NV: |
| 2760 | break; |
| 2761 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2762 | default: |
| 2763 | assert(false); // This should never happen |
| 2764 | } |
| 2765 | return skip; |
| 2766 | } |
| 2767 | |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 2768 | bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const { |
| 2769 | bool skip = false; |
| 2770 | uint32_t total_resources = 0; |
| 2771 | |
| 2772 | // Only currently testing for graphics and compute pipelines |
| 2773 | // TODO: Add check and support for Ray Tracing pipeline VUID 03428 |
| 2774 | if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) { |
| 2775 | return false; |
| 2776 | } |
| 2777 | |
| 2778 | if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 2779 | // "For the fragment shader stage the framebuffer color attachments also count against this limit" |
| 2780 | total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].colorAttachmentCount; |
| 2781 | } |
| 2782 | |
| 2783 | // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle |
| 2784 | // input from CreatePipeline and CreatePipelineLayout level |
| 2785 | for (auto set_layout : pipeline->pipeline_layout->set_layouts) { |
| 2786 | if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) { |
| 2787 | continue; |
| 2788 | } |
| 2789 | |
| 2790 | for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) { |
| 2791 | const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx); |
| 2792 | // Bindings with a descriptorCount of 0 are "reserved" and should be skipped |
| 2793 | if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) { |
| 2794 | // Check only descriptor types listed in maxPerStageResources description in spec |
| 2795 | switch (binding->descriptorType) { |
| 2796 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 2797 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 2798 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 2799 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 2800 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 2801 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 2802 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 2803 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 2804 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 2805 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 2806 | total_resources += binding->descriptorCount; |
| 2807 | break; |
| 2808 | default: |
| 2809 | break; |
| 2810 | } |
| 2811 | } |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | if (total_resources > phys_dev_props.limits.maxPerStageResources) { |
| 2816 | const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687" |
| 2817 | : "VUID-VkGraphicsPipelineCreateInfo-layout-01688"; |
| 2818 | skip |= LogError(pipeline->pipeline, vuid, |
| 2819 | "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit " |
| 2820 | "VkPhysicalDeviceLimits::maxPerStageResources (%u)", |
| 2821 | string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources); |
| 2822 | } |
| 2823 | |
| 2824 | return skip; |
| 2825 | } |
| 2826 | |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2827 | // copy the specialization constant value into buf, if it is present |
| 2828 | void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) { |
| 2829 | VkSpecializationInfo const *spec = pStage->pSpecializationInfo; |
| 2830 | |
| 2831 | if (spec && spec_id < spec->mapEntryCount) { |
| 2832 | memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size); |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | // Fill in value with the constant or specialization constant value, if available. |
| 2837 | // Returns true if the value has been accurately filled out. |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2838 | 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] | 2839 | const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) { |
| 2840 | auto type_id = src->get_def(insn.word(1)); |
| 2841 | if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) { |
| 2842 | return false; |
| 2843 | } |
| 2844 | switch (insn.opcode()) { |
| 2845 | case spv::OpSpecConstant: |
| 2846 | *value = insn.word(3); |
| 2847 | GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value); |
| 2848 | return true; |
| 2849 | case spv::OpConstant: |
| 2850 | *value = insn.word(3); |
| 2851 | return true; |
| 2852 | default: |
| 2853 | return false; |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | // Map SPIR-V type to VK_COMPONENT_TYPE enum |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2858 | VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2859 | switch (insn.opcode()) { |
| 2860 | case spv::OpTypeInt: |
| 2861 | switch (insn.word(2)) { |
| 2862 | case 8: |
| 2863 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV; |
| 2864 | case 16: |
| 2865 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV; |
| 2866 | case 32: |
| 2867 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV; |
| 2868 | case 64: |
| 2869 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV; |
| 2870 | default: |
| 2871 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2872 | } |
| 2873 | case spv::OpTypeFloat: |
| 2874 | switch (insn.word(2)) { |
| 2875 | case 16: |
| 2876 | return VK_COMPONENT_TYPE_FLOAT16_NV; |
| 2877 | case 32: |
| 2878 | return VK_COMPONENT_TYPE_FLOAT32_NV; |
| 2879 | case 64: |
| 2880 | return VK_COMPONENT_TYPE_FLOAT64_NV; |
| 2881 | default: |
| 2882 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2883 | } |
| 2884 | default: |
| 2885 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2886 | } |
| 2887 | } |
| 2888 | |
| 2889 | // Validate SPV_NV_cooperative_matrix behavior that can't be statically validated |
| 2890 | // in SPIRV-Tools (e.g. due to specialization constant usage). |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2891 | bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2892 | const PIPELINE_STATE *pipeline) const { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2893 | bool skip = false; |
| 2894 | |
| 2895 | // Map SPIR-V result ID to specialization constant id (SpecId decoration value) |
| 2896 | std::unordered_map<uint32_t, uint32_t> id_to_spec_id; |
| 2897 | // Map SPIR-V result ID to the ID of its type. |
| 2898 | std::unordered_map<uint32_t, uint32_t> id_to_type_id; |
| 2899 | |
| 2900 | struct CoopMatType { |
| 2901 | uint32_t scope, rows, cols; |
| 2902 | VkComponentTypeNV component_type; |
| 2903 | bool all_constant; |
| 2904 | |
| 2905 | CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {} |
| 2906 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2907 | 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] | 2908 | const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id) { |
| 2909 | spirv_inst_iter insn = src->get_def(id); |
| 2910 | uint32_t component_type_id = insn.word(2); |
| 2911 | uint32_t scope_id = insn.word(3); |
| 2912 | uint32_t rows_id = insn.word(4); |
| 2913 | uint32_t cols_id = insn.word(5); |
| 2914 | auto component_type_iter = src->get_def(component_type_id); |
| 2915 | auto scope_iter = src->get_def(scope_id); |
| 2916 | auto rows_iter = src->get_def(rows_id); |
| 2917 | auto cols_iter = src->get_def(cols_id); |
| 2918 | |
| 2919 | all_constant = true; |
| 2920 | if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) { |
| 2921 | all_constant = false; |
| 2922 | } |
| 2923 | if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) { |
| 2924 | all_constant = false; |
| 2925 | } |
| 2926 | if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) { |
| 2927 | all_constant = false; |
| 2928 | } |
| 2929 | component_type = GetComponentType(component_type_iter, src); |
| 2930 | } |
| 2931 | }; |
| 2932 | |
| 2933 | bool seen_coopmat_capability = false; |
| 2934 | |
| 2935 | for (auto insn : *src) { |
| 2936 | // Whitelist instructions whose result can be a cooperative matrix type, and |
| 2937 | // keep track of their types. It would be nice if SPIRV-Headers generated code |
| 2938 | // to identify which instructions have a result type and result id. Lacking that, |
| 2939 | // this whitelist is based on the set of instructions that |
| 2940 | // SPV_NV_cooperative_matrix says can be used with cooperative matrix types. |
| 2941 | switch (insn.opcode()) { |
| 2942 | case spv::OpLoad: |
| 2943 | case spv::OpCooperativeMatrixLoadNV: |
| 2944 | case spv::OpCooperativeMatrixMulAddNV: |
| 2945 | case spv::OpSNegate: |
| 2946 | case spv::OpFNegate: |
| 2947 | case spv::OpIAdd: |
| 2948 | case spv::OpFAdd: |
| 2949 | case spv::OpISub: |
| 2950 | case spv::OpFSub: |
| 2951 | case spv::OpFDiv: |
| 2952 | case spv::OpSDiv: |
| 2953 | case spv::OpUDiv: |
| 2954 | case spv::OpMatrixTimesScalar: |
| 2955 | case spv::OpConstantComposite: |
| 2956 | case spv::OpCompositeConstruct: |
| 2957 | case spv::OpConvertFToU: |
| 2958 | case spv::OpConvertFToS: |
| 2959 | case spv::OpConvertSToF: |
| 2960 | case spv::OpConvertUToF: |
| 2961 | case spv::OpUConvert: |
| 2962 | case spv::OpSConvert: |
| 2963 | case spv::OpFConvert: |
| 2964 | id_to_type_id[insn.word(2)] = insn.word(1); |
| 2965 | break; |
| 2966 | default: |
| 2967 | break; |
| 2968 | } |
| 2969 | |
| 2970 | switch (insn.opcode()) { |
| 2971 | case spv::OpDecorate: |
| 2972 | if (insn.word(2) == spv::DecorationSpecId) { |
| 2973 | id_to_spec_id[insn.word(1)] = insn.word(3); |
| 2974 | } |
| 2975 | break; |
| 2976 | case spv::OpCapability: |
| 2977 | if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) { |
| 2978 | seen_coopmat_capability = true; |
| 2979 | |
| 2980 | if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2981 | skip |= LogError( |
| 2982 | pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixSupportedStages, |
| 2983 | "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)", |
| 2984 | phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | break; |
| 2988 | case spv::OpMemoryModel: |
| 2989 | // If the capability isn't enabled, don't bother with the rest of this function. |
| 2990 | // OpMemoryModel is the first required instruction after all OpCapability instructions. |
| 2991 | if (!seen_coopmat_capability) { |
| 2992 | return skip; |
| 2993 | } |
| 2994 | break; |
| 2995 | case spv::OpTypeCooperativeMatrixNV: { |
| 2996 | CoopMatType M; |
| 2997 | M.Init(insn.word(1), src, pStage, id_to_spec_id); |
| 2998 | |
| 2999 | if (M.all_constant) { |
| 3000 | // Validate that the type parameters are all supported for one of the |
| 3001 | // operands of a cooperative matrix property. |
| 3002 | bool valid = false; |
| 3003 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
| 3004 | if (cooperative_matrix_properties[i].AType == M.component_type && |
| 3005 | cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].KSize == M.cols && |
| 3006 | cooperative_matrix_properties[i].scope == M.scope) { |
| 3007 | valid = true; |
| 3008 | break; |
| 3009 | } |
| 3010 | if (cooperative_matrix_properties[i].BType == M.component_type && |
| 3011 | cooperative_matrix_properties[i].KSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols && |
| 3012 | cooperative_matrix_properties[i].scope == M.scope) { |
| 3013 | valid = true; |
| 3014 | break; |
| 3015 | } |
| 3016 | if (cooperative_matrix_properties[i].CType == M.component_type && |
| 3017 | cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols && |
| 3018 | cooperative_matrix_properties[i].scope == M.scope) { |
| 3019 | valid = true; |
| 3020 | break; |
| 3021 | } |
| 3022 | if (cooperative_matrix_properties[i].DType == M.component_type && |
| 3023 | cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols && |
| 3024 | cooperative_matrix_properties[i].scope == M.scope) { |
| 3025 | valid = true; |
| 3026 | break; |
| 3027 | } |
| 3028 | } |
| 3029 | if (!valid) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3030 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixType, |
| 3031 | "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type", |
| 3032 | insn.word(1)); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 3033 | } |
| 3034 | } |
| 3035 | break; |
| 3036 | } |
| 3037 | case spv::OpCooperativeMatrixMulAddNV: { |
| 3038 | CoopMatType A, B, C, D; |
| 3039 | if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() || |
| 3040 | id_to_type_id.find(insn.word(3)) == id_to_type_id.end() || |
| 3041 | id_to_type_id.find(insn.word(4)) == id_to_type_id.end() || |
| 3042 | 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] | 3043 | // Couldn't find type of matrix |
| 3044 | assert(false); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 3045 | break; |
| 3046 | } |
| 3047 | D.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id); |
| 3048 | A.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id); |
| 3049 | B.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id); |
| 3050 | C.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id); |
| 3051 | |
| 3052 | if (A.all_constant && B.all_constant && C.all_constant && D.all_constant) { |
| 3053 | // Validate that the type parameters are all supported for the same |
| 3054 | // cooperative matrix property. |
| 3055 | bool valid = false; |
| 3056 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
| 3057 | if (cooperative_matrix_properties[i].AType == A.component_type && |
| 3058 | cooperative_matrix_properties[i].MSize == A.rows && cooperative_matrix_properties[i].KSize == A.cols && |
| 3059 | cooperative_matrix_properties[i].scope == A.scope && |
| 3060 | |
| 3061 | cooperative_matrix_properties[i].BType == B.component_type && |
| 3062 | cooperative_matrix_properties[i].KSize == B.rows && cooperative_matrix_properties[i].NSize == B.cols && |
| 3063 | cooperative_matrix_properties[i].scope == B.scope && |
| 3064 | |
| 3065 | cooperative_matrix_properties[i].CType == C.component_type && |
| 3066 | cooperative_matrix_properties[i].MSize == C.rows && cooperative_matrix_properties[i].NSize == C.cols && |
| 3067 | cooperative_matrix_properties[i].scope == C.scope && |
| 3068 | |
| 3069 | cooperative_matrix_properties[i].DType == D.component_type && |
| 3070 | cooperative_matrix_properties[i].MSize == D.rows && cooperative_matrix_properties[i].NSize == D.cols && |
| 3071 | cooperative_matrix_properties[i].scope == D.scope) { |
| 3072 | valid = true; |
| 3073 | break; |
| 3074 | } |
| 3075 | } |
| 3076 | if (!valid) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3077 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixMulAdd, |
| 3078 | "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix " |
| 3079 | "VkCooperativeMatrixPropertiesNV", |
| 3080 | insn.word(2)); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 3081 | } |
| 3082 | } |
| 3083 | break; |
| 3084 | } |
| 3085 | default: |
| 3086 | break; |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | return skip; |
| 3091 | } |
| 3092 | |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3093 | 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] | 3094 | auto entrypoint_id = entrypoint.word(2); |
| 3095 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3096 | // The first denorm execution mode encountered, along with its bit width. |
| 3097 | // Used to check if SeparateDenormSettings is respected. |
| 3098 | 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] | 3099 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3100 | // The first rounding mode encountered, along with its bit width. |
| 3101 | // Used to check if SeparateRoundingModeSettings is respected. |
| 3102 | 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] | 3103 | |
| 3104 | bool skip = false; |
| 3105 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3106 | uint32_t verticesOut = 0; |
| 3107 | uint32_t invocations = 0; |
| 3108 | |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3109 | for (auto insn : *src) { |
| 3110 | if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) { |
| 3111 | auto mode = insn.word(2); |
| 3112 | switch (mode) { |
| 3113 | case spv::ExecutionModeSignedZeroInfNanPreserve: { |
| 3114 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3115 | if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) || |
| 3116 | (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) || |
| 3117 | (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3118 | skip |= LogError( |
| 3119 | device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3120 | "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device", |
| 3121 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3122 | } |
| 3123 | break; |
| 3124 | } |
| 3125 | |
| 3126 | case spv::ExecutionModeDenormPreserve: { |
| 3127 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3128 | if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) || |
| 3129 | (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) || |
| 3130 | (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3131 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3132 | "Shader requires DenormPreserve for bit width %d but it is not enabled on the device", |
| 3133 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3136 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 3137 | // Register the first denorm execution mode found |
| 3138 | 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] | 3139 | } 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] | 3140 | switch (phys_dev_props_core12.denormBehaviorIndependence) { |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3141 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR: |
| 3142 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3143 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3144 | "Shader uses different denorm execution modes for 16 and 64-bit but " |
| 3145 | "denormBehaviorIndependence is " |
| 3146 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3147 | } |
| 3148 | break; |
| 3149 | |
| 3150 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR: |
| 3151 | break; |
| 3152 | |
| 3153 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3154 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3155 | "Shader uses different denorm execution modes for different bit widths but " |
| 3156 | "denormBehaviorIndependence is " |
| 3157 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3158 | break; |
| 3159 | |
| 3160 | default: |
| 3161 | break; |
| 3162 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3163 | } |
| 3164 | break; |
| 3165 | } |
| 3166 | |
| 3167 | case spv::ExecutionModeDenormFlushToZero: { |
| 3168 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3169 | if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) || |
| 3170 | (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) || |
| 3171 | (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3172 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3173 | "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device", |
| 3174 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3177 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 3178 | // Register the first denorm execution mode found |
| 3179 | 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] | 3180 | } 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] | 3181 | switch (phys_dev_props_core12.denormBehaviorIndependence) { |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3182 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR: |
| 3183 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3184 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3185 | "Shader uses different denorm execution modes for 16 and 64-bit but " |
| 3186 | "denormBehaviorIndependence is " |
| 3187 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3188 | } |
| 3189 | break; |
| 3190 | |
| 3191 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR: |
| 3192 | break; |
| 3193 | |
| 3194 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3195 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3196 | "Shader uses different denorm execution modes for different bit widths but " |
| 3197 | "denormBehaviorIndependence is " |
| 3198 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3199 | break; |
| 3200 | |
| 3201 | default: |
| 3202 | break; |
| 3203 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3204 | } |
| 3205 | break; |
| 3206 | } |
| 3207 | |
| 3208 | case spv::ExecutionModeRoundingModeRTE: { |
| 3209 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3210 | if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) || |
| 3211 | (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) || |
| 3212 | (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3213 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3214 | "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device", |
| 3215 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3218 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 3219 | // Register the first rounding mode found |
| 3220 | 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] | 3221 | } 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] | 3222 | switch (phys_dev_props_core12.roundingModeIndependence) { |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3223 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR: |
| 3224 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3225 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3226 | "Shader uses different rounding modes for 16 and 64-bit but " |
| 3227 | "roundingModeIndependence is " |
| 3228 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3229 | } |
| 3230 | break; |
| 3231 | |
| 3232 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR: |
| 3233 | break; |
| 3234 | |
| 3235 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3236 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3237 | "Shader uses different rounding modes for different bit widths but " |
| 3238 | "roundingModeIndependence is " |
| 3239 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3240 | break; |
| 3241 | |
| 3242 | default: |
| 3243 | break; |
| 3244 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3245 | } |
| 3246 | break; |
| 3247 | } |
| 3248 | |
| 3249 | case spv::ExecutionModeRoundingModeRTZ: { |
| 3250 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3251 | if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) || |
| 3252 | (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) || |
| 3253 | (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3254 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3255 | "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device", |
| 3256 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3259 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 3260 | // Register the first rounding mode found |
| 3261 | 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] | 3262 | } 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] | 3263 | switch (phys_dev_props_core12.roundingModeIndependence) { |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3264 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR: |
| 3265 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3266 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3267 | "Shader uses different rounding modes for 16 and 64-bit but " |
| 3268 | "roundingModeIndependence is " |
| 3269 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY_KHR on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3270 | } |
| 3271 | break; |
| 3272 | |
| 3273 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL_KHR: |
| 3274 | break; |
| 3275 | |
| 3276 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3277 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3278 | "Shader uses different rounding modes for different bit widths but " |
| 3279 | "roundingModeIndependence is " |
| 3280 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE_KHR on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3281 | break; |
| 3282 | |
| 3283 | default: |
| 3284 | break; |
| 3285 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3286 | } |
| 3287 | break; |
| 3288 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3289 | |
| 3290 | case spv::ExecutionModeOutputVertices: { |
| 3291 | verticesOut = insn.word(3); |
| 3292 | break; |
| 3293 | } |
| 3294 | |
| 3295 | case spv::ExecutionModeInvocations: { |
| 3296 | invocations = insn.word(3); |
| 3297 | break; |
| 3298 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3299 | } |
| 3300 | } |
| 3301 | } |
| 3302 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3303 | if (entrypoint.word(1) == spv::ExecutionModelGeometry) { |
| 3304 | if (verticesOut == 0 || verticesOut > phys_dev_props.limits.maxGeometryOutputVertices) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3305 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714", |
| 3306 | "Geometry shader entry point must have an OpExecutionMode instruction that " |
| 3307 | "specifies a maximum output vertex count that is greater than 0 and less " |
| 3308 | "than or equal to maxGeometryOutputVertices. " |
| 3309 | "OutputVertices=%d, maxGeometryOutputVertices=%d", |
| 3310 | verticesOut, phys_dev_props.limits.maxGeometryOutputVertices); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3314 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715", |
| 3315 | "Geometry shader entry point must have an OpExecutionMode instruction that " |
| 3316 | "specifies an invocation count that is greater than 0 and less " |
| 3317 | "than or equal to maxGeometryShaderInvocations. " |
| 3318 | "Invocations=%d, maxGeometryShaderInvocations=%d", |
| 3319 | invocations, phys_dev_props.limits.maxGeometryShaderInvocations); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3320 | } |
| 3321 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3322 | return skip; |
| 3323 | } |
| 3324 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 3325 | uint32_t DescriptorTypeToReqs(SHADER_MODULE_STATE const *module, uint32_t type_id) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3326 | auto type = module->get_def(type_id); |
| 3327 | |
| 3328 | while (true) { |
| 3329 | switch (type.opcode()) { |
| 3330 | case spv::OpTypeArray: |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 3331 | case spv::OpTypeRuntimeArray: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3332 | case spv::OpTypeSampledImage: |
| 3333 | type = module->get_def(type.word(2)); |
| 3334 | break; |
| 3335 | case spv::OpTypePointer: |
| 3336 | type = module->get_def(type.word(3)); |
| 3337 | break; |
| 3338 | case spv::OpTypeImage: { |
| 3339 | auto dim = type.word(3); |
| 3340 | auto arrayed = type.word(5); |
| 3341 | auto msaa = type.word(6); |
| 3342 | |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3343 | uint32_t bits = 0; |
| 3344 | switch (GetFundamentalType(module, type.word(2))) { |
| 3345 | case FORMAT_TYPE_FLOAT: |
| 3346 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT; |
| 3347 | break; |
| 3348 | case FORMAT_TYPE_UINT: |
| 3349 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT; |
| 3350 | break; |
| 3351 | case FORMAT_TYPE_SINT: |
| 3352 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT; |
| 3353 | break; |
| 3354 | default: |
| 3355 | break; |
| 3356 | } |
| 3357 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3358 | switch (dim) { |
| 3359 | case spv::Dim1D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3360 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D; |
| 3361 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3362 | case spv::Dim2D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3363 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 3364 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D; |
| 3365 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3366 | case spv::Dim3D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3367 | bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D; |
| 3368 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3369 | case spv::DimCube: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3370 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE; |
| 3371 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3372 | case spv::DimSubpassData: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3373 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 3374 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3375 | default: // buffer, etc. |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3376 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3377 | } |
| 3378 | } |
| 3379 | default: |
| 3380 | return 0; |
| 3381 | } |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | // For given pipelineLayout verify that the set_layout_node at slot.first |
| 3386 | // 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] | 3387 | static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3388 | descriptor_slot_t slot) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3389 | if (!pipelineLayout) return nullptr; |
| 3390 | |
| 3391 | if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr; |
| 3392 | |
| 3393 | return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second); |
| 3394 | } |
| 3395 | |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 3396 | int32_t GetShaderResourceDimensionality(const SHADER_MODULE_STATE *module, const interface_var &resource) { |
| 3397 | if (module == nullptr) return -1; |
| 3398 | |
| 3399 | auto type = module->get_def(resource.type_id); |
| 3400 | while (true) { |
| 3401 | switch (type.opcode()) { |
| 3402 | case spv::OpTypeSampledImage: |
| 3403 | type = module->get_def(type.word(2)); |
| 3404 | break; |
| 3405 | case spv::OpTypePointer: |
| 3406 | type = module->get_def(type.word(3)); |
| 3407 | break; |
| 3408 | case spv::OpTypeImage: |
| 3409 | return type.word(3); |
| 3410 | default: |
| 3411 | return -1; |
| 3412 | } |
| 3413 | } |
| 3414 | } |
| 3415 | |
| 3416 | 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] | 3417 | for (auto insn : *src) { |
| 3418 | if (insn.opcode() == spv::OpEntryPoint) { |
| 3419 | auto executionModel = insn.word(1); |
| 3420 | auto entrypointStageBits = ExecutionModelToShaderStageFlagBits(executionModel); |
| 3421 | if (entrypointStageBits == VK_SHADER_STAGE_COMPUTE_BIT) { |
| 3422 | auto entrypoint_id = insn.word(2); |
| 3423 | for (auto insn1 : *src) { |
| 3424 | if (insn1.opcode() == spv::OpExecutionMode && insn1.word(1) == entrypoint_id && |
| 3425 | insn1.word(2) == spv::ExecutionModeLocalSize) { |
| 3426 | local_size_x = insn1.word(3); |
| 3427 | local_size_y = insn1.word(4); |
| 3428 | local_size_z = insn1.word(5); |
| 3429 | return true; |
| 3430 | } |
| 3431 | } |
| 3432 | } |
| 3433 | } |
| 3434 | } |
| 3435 | return false; |
| 3436 | } |
| 3437 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 3438 | 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] | 3439 | auto entrypoint_id = entrypoint.word(2); |
Chris Forbes | 0771b67 | 2018-03-22 21:13:46 -0700 | [diff] [blame] | 3440 | bool is_point_mode = false; |
| 3441 | |
| 3442 | for (auto insn : *src) { |
| 3443 | if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) { |
| 3444 | switch (insn.word(2)) { |
| 3445 | case spv::ExecutionModePointMode: |
| 3446 | // In tessellation shaders, PointMode is separate and trumps the tessellation topology. |
| 3447 | is_point_mode = true; |
| 3448 | break; |
| 3449 | |
| 3450 | case spv::ExecutionModeOutputPoints: |
| 3451 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 3452 | break; |
| 3453 | |
| 3454 | case spv::ExecutionModeIsolines: |
| 3455 | case spv::ExecutionModeOutputLineStrip: |
| 3456 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; |
| 3457 | break; |
| 3458 | |
| 3459 | case spv::ExecutionModeTriangles: |
| 3460 | case spv::ExecutionModeQuads: |
| 3461 | case spv::ExecutionModeOutputTriangleStrip: |
| 3462 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
| 3463 | break; |
| 3464 | } |
| 3465 | } |
| 3466 | } |
| 3467 | |
| 3468 | if (is_point_mode) pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 3469 | } |
| 3470 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3471 | // If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize |
| 3472 | // o If there is only a vertex shader : gl_PointSize must be written when using points |
| 3473 | // o If there is a geometry or tessellation shader: |
| 3474 | // - If shaderTessellationAndGeometryPointSize feature is enabled: |
| 3475 | // * gl_PointSize must be written in the final geometry stage |
| 3476 | // - If shaderTessellationAndGeometryPointSize feature is disabled: |
| 3477 | // * 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] | 3478 | bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3479 | spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3480 | if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 3481 | return false; |
| 3482 | } |
| 3483 | |
| 3484 | bool pointsize_written = false; |
| 3485 | bool skip = false; |
| 3486 | |
| 3487 | // Search for PointSize built-in decorations |
| 3488 | std::vector<uint32_t> pointsize_builtin_offsets; |
| 3489 | spirv_inst_iter insn = entrypoint; |
| 3490 | while (!pointsize_written && (insn.opcode() != spv::OpFunction)) { |
| 3491 | if (insn.opcode() == spv::OpMemberDecorate) { |
| 3492 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 3493 | if (insn.word(4) == spv::BuiltInPointSize) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame^] | 3494 | pointsize_written = IsBuiltInWritten(src, insn, entrypoint); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3495 | } |
| 3496 | } |
| 3497 | } else if (insn.opcode() == spv::OpDecorate) { |
| 3498 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 3499 | if (insn.word(3) == spv::BuiltInPointSize) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame^] | 3500 | pointsize_written = IsBuiltInWritten(src, insn, entrypoint); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3501 | } |
| 3502 | } |
| 3503 | } |
| 3504 | |
| 3505 | insn++; |
| 3506 | } |
| 3507 | |
| 3508 | 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] | 3509 | !enabled_features.core.shaderTessellationAndGeometryPointSize) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3510 | if (pointsize_written) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3511 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_PointSizeBuiltInOverSpecified, |
| 3512 | "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which " |
| 3513 | "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled."); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3514 | } |
| 3515 | } else if (!pointsize_written) { |
| 3516 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3517 | LogError(pipeline->pipeline, kVUID_Core_Shader_MissingPointSizeBuiltIn, |
| 3518 | "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.", |
| 3519 | string_VkShaderStageFlagBits(stage)); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3520 | } |
| 3521 | return skip; |
| 3522 | } |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3523 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame^] | 3524 | bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src, |
| 3525 | spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const { |
| 3526 | bool primitiverate_written = false; |
| 3527 | bool viewportindex_written = false; |
| 3528 | bool viewportmask_written = false; |
| 3529 | bool skip = false; |
| 3530 | |
| 3531 | // Check if the primitive shading rate is written |
| 3532 | spirv_inst_iter insn = entrypoint; |
| 3533 | while (!(primitiverate_written && viewportindex_written && viewportmask_written) && insn.opcode() != spv::OpFunction) { |
| 3534 | if (insn.opcode() == spv::OpMemberDecorate) { |
| 3535 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 3536 | if (insn.word(4) == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3537 | primitiverate_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3538 | } else if (insn.word(4) == spv::BuiltInViewportIndex) { |
| 3539 | viewportindex_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3540 | } else if (insn.word(4) == spv::BuiltInViewportMaskNV) { |
| 3541 | viewportmask_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3542 | } |
| 3543 | } |
| 3544 | } else if (insn.opcode() == spv::OpDecorate) { |
| 3545 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 3546 | if (insn.word(3) == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3547 | primitiverate_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3548 | } else if (insn.word(3) == spv::BuiltInViewportIndex) { |
| 3549 | viewportindex_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3550 | } else if (insn.word(3) == spv::BuiltInViewportMaskNV) { |
| 3551 | viewportmask_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3552 | } |
| 3553 | } |
| 3554 | } |
| 3555 | |
| 3556 | insn++; |
| 3557 | } |
| 3558 | |
| 3559 | if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports) { |
| 3560 | if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && |
| 3561 | pipeline->graphicsPipelineCI.pViewportState->viewportCount > 1 && primitiverate_written) { |
| 3562 | skip |= LogError( |
| 3563 | pipeline->pipeline, "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503", |
| 3564 | "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but multiple viewports " |
| 3565 | "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3566 | string_VkShaderStageFlagBits(stage)); |
| 3567 | } |
| 3568 | |
| 3569 | if (primitiverate_written && viewportindex_written) { |
| 3570 | skip |= LogError(pipeline->pipeline, |
| 3571 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504", |
| 3572 | "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and " |
| 3573 | "ViewportIndex built-ins," |
| 3574 | "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3575 | string_VkShaderStageFlagBits(stage)); |
| 3576 | } |
| 3577 | |
| 3578 | if (primitiverate_written && viewportmask_written) { |
| 3579 | skip |= LogError(pipeline->pipeline, |
| 3580 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505", |
| 3581 | "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and " |
| 3582 | "ViewportMaskNV built-ins," |
| 3583 | "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3584 | string_VkShaderStageFlagBits(stage)); |
| 3585 | } |
| 3586 | } |
| 3587 | return skip; |
| 3588 | } |
| 3589 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3590 | bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline, |
| 3591 | const PIPELINE_STATE::StageState &stage_state, const SHADER_MODULE_STATE *module, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3592 | const spirv_inst_iter &entrypoint, bool check_point_size) const { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3593 | bool skip = false; |
| 3594 | |
| 3595 | // Check the module |
| 3596 | if (!module->has_valid_spirv) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3597 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", |
| 3598 | "%s does not contain valid spirv for stage %s.", |
| 3599 | 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] | 3600 | } |
| 3601 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3602 | // If specialization-constant values are given and specialization-constant instructions are present in the shader, the |
| 3603 | // specializations should be applied and validated. |
| 3604 | if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 && |
| 3605 | pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) { |
| 3606 | // Gather the specialization-constant values. |
| 3607 | auto const &specialization_info = pStage->pSpecializationInfo; |
Jeremy Hayes | 521221d | 2020-01-15 16:48:49 -0700 | [diff] [blame] | 3608 | auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3609 | std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; |
| 3610 | id_value_map.reserve(specialization_info->mapEntryCount); |
| 3611 | for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) { |
| 3612 | auto const &map_entry = specialization_info->pMapEntries[i]; |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3613 | |
Jeremy Hayes | 521221d | 2020-01-15 16:48:49 -0700 | [diff] [blame] | 3614 | // Expect only scalar types. |
| 3615 | assert(map_entry.size == 1 || map_entry.size == 2 || map_entry.size == 4 || map_entry.size == 8); |
| 3616 | auto entry = id_value_map.emplace(map_entry.constantID, std::vector<uint32_t>(map_entry.size > 4 ? 2 : 1)); |
| 3617 | 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] | 3618 | } |
| 3619 | |
| 3620 | // Apply the specialization-constant values and revalidate the shader module. |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 3621 | 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] | 3622 | spvtools::Optimizer optimizer(spirv_environment); |
| 3623 | spvtools::MessageConsumer consumer = [&skip, &module, &pStage, this](spv_message_level_t level, const char *source, |
| 3624 | const spv_position_t &position, const char *message) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3625 | skip |= LogError( |
| 3626 | 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] | 3627 | report_data->FormatHandle(module->vk_shader_module).c_str(), string_VkShaderStageFlagBits(pStage->stage), message); |
| 3628 | }; |
| 3629 | optimizer.SetMessageConsumer(consumer); |
| 3630 | optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map)); |
| 3631 | optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass()); |
| 3632 | std::vector<uint32_t> specialized_spirv; |
| 3633 | auto const optimized = |
| 3634 | optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, spvtools::ValidatorOptions(), true); |
| 3635 | assert(optimized == true); |
| 3636 | |
| 3637 | if (optimized) { |
| 3638 | spv_context ctx = spvContextCreate(spirv_environment); |
| 3639 | spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()}; |
| 3640 | spv_diagnostic diag = nullptr; |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 3641 | spvtools::ValidatorOptions options; |
| 3642 | AdjustValidatorOptions(device_extensions, enabled_features, options); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3643 | auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
| 3644 | if (spv_valid != SPV_SUCCESS) { |
sfricke-samsung | d379380 | 2020-08-18 22:55:03 -0700 | [diff] [blame] | 3645 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3646 | "After specialization was applied, %s does not contain valid spirv for stage %s.", |
| 3647 | report_data->FormatHandle(module->vk_shader_module).c_str(), |
| 3648 | string_VkShaderStageFlagBits(pStage->stage)); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3649 | } |
| 3650 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3651 | spvDiagnosticDestroy(diag); |
| 3652 | spvContextDestroy(ctx); |
| 3653 | } |
| 3654 | } |
| 3655 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3656 | // Check the entrypoint |
| 3657 | if (entrypoint == module->end()) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3658 | skip |= |
| 3659 | LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s..", |
| 3660 | pStage->pName, string_VkShaderStageFlagBits(pStage->stage)); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3661 | } |
| 3662 | if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage. |
| 3663 | |
| 3664 | // Mark accessible ids |
| 3665 | auto &accessible_ids = stage_state.accessible_ids; |
| 3666 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3667 | // Validate descriptor set layout against what the entrypoint actually uses |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3668 | bool has_writable_descriptor = stage_state.has_writable_descriptor; |
| 3669 | auto &descriptor_uses = stage_state.descriptor_uses; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3670 | |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 3671 | // Validate shader capabilities against enabled device features |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 3672 | skip |= ValidateShaderCapabilities(module, pStage->stage); |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 3673 | skip |= |
| 3674 | ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, has_writable_descriptor, stage_state.has_atomic_descriptor); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3675 | skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint); |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 3676 | skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 3677 | skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3678 | skip |= ValidateExecutionModes(module, entrypoint); |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3679 | skip |= ValidateSpecializationOffsets(pStage); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 3680 | skip |= ValidatePushConstantUsage(*pipeline, module, pStage); |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 3681 | if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) { |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 3682 | skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3683 | } |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 3684 | skip |= ValidateBuiltinLimits(module, accessible_ids, pStage->stage); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 3685 | skip |= ValidateCooperativeMatrix(module, pStage, pipeline); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame^] | 3686 | if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) { |
| 3687 | skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage); |
| 3688 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3689 | |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3690 | std::string vuid_layout_mismatch; |
| 3691 | if (pipeline->graphicsPipelineCI.sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) { |
| 3692 | vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756"; |
| 3693 | } else if (pipeline->computePipelineCI.sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO) { |
| 3694 | vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703"; |
| 3695 | } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR) { |
| 3696 | vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427"; |
| 3697 | } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV) { |
| 3698 | vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427"; |
| 3699 | } |
| 3700 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3701 | // Validate descriptor use |
| 3702 | for (auto use : descriptor_uses) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3703 | // Verify given pipelineLayout has requested setLayout with requested binding |
Jeff Bolz | e7fc67b | 2019-10-04 12:29:31 -0500 | [diff] [blame] | 3704 | const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3705 | unsigned required_descriptor_count; |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3706 | bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR; |
| 3707 | std::set<uint32_t> descriptor_types = |
| 3708 | TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3709 | |
| 3710 | if (!binding) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3711 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3712 | "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout", |
| 3713 | use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3714 | } else if (~binding->stageFlags & pStage->stage) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3715 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3716 | "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first, |
| 3717 | use.first.second, string_VkShaderStageFlagBits(pStage->stage)); |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 3718 | } else if (descriptor_types.find(binding->descriptorType) == descriptor_types.end()) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3719 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3720 | "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first, |
| 3721 | use.first.second, string_descriptorTypes(descriptor_types).c_str(), |
| 3722 | string_VkDescriptorType(binding->descriptorType)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3723 | } else if (binding->descriptorCount < required_descriptor_count) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3724 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3725 | "Shader expects at least %u descriptors for binding %u.%u but only %u provided", |
| 3726 | required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3727 | } |
| 3728 | } |
| 3729 | |
| 3730 | // Validate use of input attachments against subpass structure |
| 3731 | if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3732 | auto input_attachment_uses = CollectInterfaceByInputAttachmentIndex(module, accessible_ids); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3733 | |
Petr Kraus | e91f7a1 | 2017-12-14 20:57:36 +0100 | [diff] [blame] | 3734 | auto rpci = pipeline->rp_state->createInfo.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3735 | auto subpass = pipeline->graphicsPipelineCI.subpass; |
| 3736 | |
| 3737 | for (auto use : input_attachment_uses) { |
| 3738 | auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments; |
| 3739 | auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount) |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3740 | ? input_attachments[use.first].attachment |
| 3741 | : VK_ATTACHMENT_UNUSED; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3742 | |
| 3743 | if (index == VK_ATTACHMENT_UNUSED) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3744 | skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment, |
| 3745 | "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] | 3746 | } 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] | 3747 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3748 | LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch, |
| 3749 | "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first, |
| 3750 | 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] | 3751 | } |
| 3752 | } |
| 3753 | } |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 3754 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) { |
| 3755 | skip |= ValidateComputeWorkGroupSizes(module); |
| 3756 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3757 | return skip; |
| 3758 | } |
| 3759 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3760 | bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint, |
| 3761 | shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer, |
| 3762 | spirv_inst_iter consumer_entrypoint, |
| 3763 | shader_stage_attributes const *consumer_stage) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3764 | bool skip = false; |
| 3765 | |
| 3766 | auto outputs = |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3767 | CollectInterfaceByLocation(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output); |
| 3768 | auto inputs = CollectInterfaceByLocation(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3769 | |
| 3770 | auto a_it = outputs.begin(); |
| 3771 | auto b_it = inputs.begin(); |
| 3772 | |
| 3773 | // Maps sorted by key (location); walk them together to find mismatches |
| 3774 | while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) { |
| 3775 | bool a_at_end = outputs.size() == 0 || a_it == outputs.end(); |
| 3776 | bool b_at_end = inputs.size() == 0 || b_it == inputs.end(); |
| 3777 | auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first; |
| 3778 | auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first; |
| 3779 | |
| 3780 | if (b_at_end || ((!a_at_end) && (a_first < b_first))) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3781 | skip |= LogPerformanceWarning(producer->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 3782 | "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, |
| 3783 | a_first.first, a_first.second, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3784 | a_it++; |
| 3785 | } else if (a_at_end || a_first > b_first) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3786 | skip |= LogError(consumer->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 3787 | "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first, |
| 3788 | b_first.second, producer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3789 | b_it++; |
| 3790 | } else { |
| 3791 | // subtleties of arrayed interfaces: |
| 3792 | // - if is_patch, then the member is not arrayed, even though the interface may be. |
| 3793 | // - if is_block_member, then the extra array level of an arrayed interface is not |
| 3794 | // expressed in the member type -- it's expressed in the block type. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3795 | if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id, |
| 3796 | producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member, |
| 3797 | 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] | 3798 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3799 | "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second, |
| 3800 | DescribeType(producer, a_it->second.type_id).c_str(), |
| 3801 | DescribeType(consumer, b_it->second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3802 | } |
| 3803 | if (a_it->second.is_patch != b_it->second.is_patch) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3804 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3805 | "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage", |
| 3806 | a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name, |
| 3807 | b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3808 | } |
| 3809 | 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] | 3810 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3811 | "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first, |
| 3812 | a_first.second, producer_stage->name, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3813 | } |
| 3814 | a_it++; |
| 3815 | b_it++; |
| 3816 | } |
| 3817 | } |
| 3818 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3819 | if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 3820 | auto builtins_producer = CollectBuiltinBlockMembers(producer, producer_entrypoint, spv::StorageClassOutput); |
| 3821 | auto builtins_consumer = CollectBuiltinBlockMembers(consumer, consumer_entrypoint, spv::StorageClassInput); |
| 3822 | |
| 3823 | if (!builtins_producer.empty() && !builtins_consumer.empty()) { |
| 3824 | if (builtins_producer.size() != builtins_consumer.size()) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3825 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3826 | "Number of elements inside builtin block differ between stages (%s %d vs %s %d).", |
| 3827 | producer_stage->name, (int)builtins_producer.size(), consumer_stage->name, |
| 3828 | (int)builtins_consumer.size()); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3829 | } else { |
| 3830 | auto it_producer = builtins_producer.begin(); |
| 3831 | auto it_consumer = builtins_consumer.begin(); |
| 3832 | while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) { |
| 3833 | if (*it_producer != *it_consumer) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3834 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3835 | "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name, |
| 3836 | consumer_stage->name); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3837 | break; |
| 3838 | } |
| 3839 | it_producer++; |
| 3840 | it_consumer++; |
| 3841 | } |
| 3842 | } |
| 3843 | } |
| 3844 | } |
| 3845 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3846 | return skip; |
| 3847 | } |
| 3848 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3849 | static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3850 | uint32_t stage_mask = 0; |
| 3851 | if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 3852 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
| 3853 | stage_mask |= pCreateInfo->pStages[i].stage; |
| 3854 | } |
| 3855 | // 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] | 3856 | if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) { |
| 3857 | stage_mask = VK_SHADER_STAGE_MESH_BIT_NV; |
| 3858 | } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3859 | stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT; |
| 3860 | } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) { |
| 3861 | stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 3862 | } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) { |
| 3863 | stage_mask = VK_SHADER_STAGE_VERTEX_BIT; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 3864 | } |
| 3865 | } |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3866 | return stage_mask; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 3867 | } |
| 3868 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3869 | // Validate that the shaders used by the given pipeline and store the active_slots |
| 3870 | // that are actually used by the pipeline into pPipeline->active_slots |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3871 | bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const { |
Chris Forbes | a400a8a | 2017-07-20 13:10:24 -0700 | [diff] [blame] | 3872 | auto pCreateInfo = pipeline->graphicsPipelineCI.ptr(); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3873 | int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 3874 | int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3875 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3876 | const SHADER_MODULE_STATE *shaders[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3877 | memset(shaders, 0, sizeof(shaders)); |
Jeff Bolz | 7e35c39 | 2018-09-04 15:30:41 -0500 | [diff] [blame] | 3878 | spirv_inst_iter entrypoints[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3879 | bool skip = false; |
| 3880 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3881 | uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, pCreateInfo); |
| 3882 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3883 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
| 3884 | auto pStage = &pCreateInfo->pStages[i]; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3885 | auto stage_id = GetShaderStageId(pStage->stage); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3886 | shaders[stage_id] = GetShaderModuleState(pStage->module); |
| 3887 | entrypoints[stage_id] = FindEntrypoint(shaders[stage_id], pStage->pName, pStage->stage); |
| 3888 | skip |= ValidatePipelineShaderStage(pStage, pipeline, pipeline->stage_state[i], shaders[stage_id], entrypoints[stage_id], |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3889 | (pointlist_stage_mask == pStage->stage)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | // if the shader stages are no good individually, cross-stage validation is pointless. |
| 3893 | if (skip) return true; |
| 3894 | |
| 3895 | auto vi = pCreateInfo->pVertexInputState; |
| 3896 | |
| 3897 | if (vi) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3898 | skip |= ValidateViConsistency(vi); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3902 | skip |= ValidateViAgainstVsInputs(vi, shaders[vertex_stage], entrypoints[vertex_stage]); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3903 | } |
| 3904 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3905 | int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 3906 | int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3907 | |
| 3908 | while (!shaders[producer] && producer != fragment_stage) { |
| 3909 | producer++; |
| 3910 | consumer++; |
| 3911 | } |
| 3912 | |
| 3913 | for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) { |
| 3914 | assert(shaders[producer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 3915 | if (shaders[consumer]) { |
| 3916 | if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3917 | skip |= ValidateInterfaceBetweenStages(shaders[producer], entrypoints[producer], &shader_stage_attribs[producer], |
| 3918 | shaders[consumer], entrypoints[consumer], &shader_stage_attribs[consumer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 3919 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3920 | |
| 3921 | producer = consumer; |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3926 | skip |= ValidateFsOutputsAgainstRenderPass(shaders[fragment_stage], entrypoints[fragment_stage], pipeline, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3927 | pCreateInfo->subpass); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3928 | } |
| 3929 | |
| 3930 | return skip; |
| 3931 | } |
| 3932 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame^] | 3933 | bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB, |
| 3934 | const char *caller, const DrawDispatchVuid &vuid) const { |
| 3935 | auto pCreateInfo = pipeline->graphicsPipelineCI.ptr(); |
| 3936 | |
| 3937 | const SHADER_MODULE_STATE *shaders[32]; |
| 3938 | memset(shaders, 0, sizeof(shaders)); |
| 3939 | spirv_inst_iter entrypoints[32]; |
| 3940 | memset(entrypoints, 0, sizeof(entrypoints)); |
| 3941 | bool skip = false; |
| 3942 | |
| 3943 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
| 3944 | auto pStage = &pCreateInfo->pStages[i]; |
| 3945 | auto stage_id = GetShaderStageId(pStage->stage); |
| 3946 | shaders[stage_id] = GetShaderModuleState(pStage->module); |
| 3947 | entrypoints[stage_id] = FindEntrypoint(shaders[stage_id], pStage->pName, pStage->stage); |
| 3948 | |
| 3949 | if (pStage->stage == VK_SHADER_STAGE_VERTEX_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT || |
| 3950 | pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV) { |
| 3951 | if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports && |
| 3952 | IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) { |
| 3953 | spirv_inst_iter insn = entrypoints[stage_id]; |
| 3954 | bool primitiverate_written = false; |
| 3955 | |
| 3956 | while (!primitiverate_written && (insn.opcode() != spv::OpFunction)) { |
| 3957 | if (insn.opcode() == spv::OpMemberDecorate) { |
| 3958 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 3959 | if (insn.word(4) == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3960 | primitiverate_written = IsBuiltInWritten(shaders[stage_id], insn, entrypoints[stage_id]); |
| 3961 | } |
| 3962 | } |
| 3963 | } else if (insn.opcode() == spv::OpDecorate) { |
| 3964 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 3965 | if (insn.word(3) == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3966 | primitiverate_written = IsBuiltInWritten(shaders[stage_id], insn, entrypoints[stage_id]); |
| 3967 | } |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | insn++; |
| 3972 | } |
| 3973 | |
| 3974 | if (primitiverate_written) { |
| 3975 | skip |= |
| 3976 | LogError(pipeline->pipeline, vuid.viewport_count_primitive_shading_rate, |
| 3977 | "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in" |
| 3978 | "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT," |
| 3979 | "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3980 | caller, string_VkShaderStageFlagBits(pStage->stage)); |
| 3981 | } |
| 3982 | } |
| 3983 | } |
| 3984 | } |
| 3985 | |
| 3986 | return skip; |
| 3987 | } |
| 3988 | |
sfricke-samsung | e72a85e | 2020-02-29 21:48:37 -0800 | [diff] [blame] | 3989 | bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3990 | const auto &stage = *pipeline->computePipelineCI.stage.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3991 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3992 | const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module); |
| 3993 | const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3994 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3995 | return ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[0], module, entrypoint, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3996 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3997 | |
Ricardo Garcia | 6f3477e | 2020-10-21 10:58:53 +0200 | [diff] [blame] | 3998 | uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const { |
| 3999 | uint32_t total = 0; |
| 4000 | |
| 4001 | const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages; |
| 4002 | for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) { |
| 4003 | if (stages[stage_index].stage == stageBit) { |
| 4004 | total++; |
| 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | if (pipeline->raytracingPipelineCI.pLibraryInfo) { |
| 4009 | for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) { |
| 4010 | const PIPELINE_STATE *library_pipeline = GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]); |
| 4011 | total += CalcShaderStageCount(library_pipeline, stageBit); |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | return total; |
| 4016 | } |
| 4017 | |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 4018 | bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const { |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 4019 | bool skip = false; |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4020 | |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4021 | if (isKHR) { |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 4022 | if (pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth > |
| 4023 | phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) { |
| 4024 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589", |
| 4025 | "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to " |
| 4026 | "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d", |
| 4027 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth, |
| 4028 | phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4029 | } |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 4030 | if (pipeline->raytracingPipelineCI.pLibraryInfo) { |
| 4031 | for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) { |
| 4032 | const PIPELINE_STATE *pLibrary_pipelinestate = |
| 4033 | GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]); |
| 4034 | if (pLibrary_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth != |
| 4035 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth) { |
| 4036 | skip |= LogError( |
| 4037 | device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591", |
| 4038 | "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been" |
| 4039 | "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .", |
| 4040 | i, pLibrary_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth, |
| 4041 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth); |
| 4042 | } |
| 4043 | if (pLibrary_pipelinestate->raytracingPipelineCI.pLibraryInfo && |
| 4044 | (pLibrary_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize != |
| 4045 | pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize || |
| 4046 | pLibrary_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize != |
| 4047 | pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize)) { |
| 4048 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593", |
| 4049 | "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries " |
| 4050 | "member must have been created with values of the maxPipelineRayPayloadSize and " |
| 4051 | "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline"); |
| 4052 | } |
| 4053 | if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) && |
| 4054 | !(pLibrary_pipelinestate->raytracingPipelineCI.flags & |
| 4055 | VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) { |
| 4056 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594", |
| 4057 | "vkCreateRayTracingPipelinesKHR: If flags includes " |
| 4058 | "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of " |
| 4059 | "the pLibraries member of libraries must have been created with the " |
| 4060 | "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set"); |
| 4061 | } |
sourav parmar | 83c31b1 | 2020-05-06 12:30:54 -0700 | [diff] [blame] | 4062 | } |
| 4063 | } |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4064 | } else { |
| 4065 | if (pipeline->raytracingPipelineCI.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) { |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 4066 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457", |
| 4067 | "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to " |
| 4068 | "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)", |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4069 | pipeline->raytracingPipelineCI.maxRecursionDepth, |
| 4070 | phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth); |
| 4071 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4072 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4073 | const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages; |
| 4074 | const auto *groups = pipeline->raytracingPipelineCI.ptr()->pGroups; |
| 4075 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 4076 | 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] | 4077 | const auto &stage = stages[stage_index]; |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 4078 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 4079 | const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module); |
| 4080 | const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage); |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 4081 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 4082 | skip |= ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[stage_index], module, entrypoint, false); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4083 | } |
Ricardo Garcia | 6f3477e | 2020-10-21 10:58:53 +0200 | [diff] [blame] | 4084 | |
| 4085 | if ((pipeline->raytracingPipelineCI.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) { |
| 4086 | const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR); |
| 4087 | if (raygen_stages_count == 0) { |
| 4088 | skip |= LogError( |
| 4089 | device, |
| 4090 | isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-03425", |
| 4091 | " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR."); |
| 4092 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | for (uint32_t group_index = 0; group_index < pipeline->raytracingPipelineCI.groupCount; group_index++) { |
| 4096 | const auto &group = groups[group_index]; |
| 4097 | |
| 4098 | if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) { |
| 4099 | if (group.generalShader >= pipeline->raytracingPipelineCI.stageCount || |
| 4100 | (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV && |
| 4101 | stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV && |
| 4102 | stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4103 | skip |= LogError(device, |
| 4104 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474" |
| 4105 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413", |
| 4106 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4107 | } |
| 4108 | if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV || |
| 4109 | group.intersectionShader != VK_SHADER_UNUSED_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4110 | skip |= LogError(device, |
| 4111 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475" |
| 4112 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414", |
| 4113 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4114 | } |
| 4115 | } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) { |
| 4116 | if (group.intersectionShader >= pipeline->raytracingPipelineCI.stageCount || |
| 4117 | stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4118 | skip |= LogError(device, |
| 4119 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476" |
| 4120 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415", |
| 4121 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4122 | } |
| 4123 | } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) { |
| 4124 | if (group.intersectionShader != VK_SHADER_UNUSED_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4125 | skip |= LogError(device, |
| 4126 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477" |
| 4127 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416", |
| 4128 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV || |
| 4133 | group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) { |
| 4134 | if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= pipeline->raytracingPipelineCI.stageCount || |
| 4135 | stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4136 | skip |= LogError(device, |
| 4137 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479" |
| 4138 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418", |
| 4139 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4140 | } |
| 4141 | if (group.closestHitShader != VK_SHADER_UNUSED_NV && |
| 4142 | (group.closestHitShader >= pipeline->raytracingPipelineCI.stageCount || |
| 4143 | stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 4144 | skip |= LogError(device, |
| 4145 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478" |
| 4146 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417", |
| 4147 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 4148 | } |
| 4149 | } |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 4150 | } |
| 4151 | return skip; |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 4152 | } |
| 4153 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 4154 | 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] | 4155 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 4156 | static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) { |
John Zulauf | 25ea243 | 2019-04-05 10:07:38 -0600 | [diff] [blame] | 4157 | const auto validation_cache_ci = lvl_find_in_chain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext); |
| 4158 | if (validation_cache_ci) { |
John Zulauf | 146ee80 | 2019-04-05 15:31:06 -0600 | [diff] [blame] | 4159 | return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4160 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4161 | return nullptr; |
| 4162 | } |
| 4163 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 4164 | bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 4165 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4166 | bool skip = false; |
| 4167 | spv_result_t spv_valid = SPV_SUCCESS; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4168 | |
Mark Lobodzinski | 90eea5b | 2020-05-15 12:54:00 -0600 | [diff] [blame] | 4169 | if (disabled[shader_validation]) { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4170 | return false; |
| 4171 | } |
| 4172 | |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 4173 | auto have_glsl_shader = device_extensions.vk_nv_glsl_shader; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4174 | |
| 4175 | if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4176 | skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376", |
| 4177 | "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".", |
| 4178 | pCreateInfo->codeSize); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4179 | } else { |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4180 | auto cache = GetValidationCacheInfo(pCreateInfo); |
| 4181 | uint32_t hash = 0; |
| 4182 | if (cache) { |
| 4183 | hash = ValidationCache::MakeShaderHash(pCreateInfo); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 4184 | if (cache->Contains(hash)) return false; |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4185 | } |
| 4186 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 4187 | // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present, |
| 4188 | // the default values will be used during validation. |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 4189 | 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] | 4190 | spv_context ctx = spvContextCreate(spirv_environment); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 4191 | spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)}; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4192 | spv_diagnostic diag = nullptr; |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 4193 | spvtools::ValidatorOptions options; |
| 4194 | AdjustValidatorOptions(device_extensions, enabled_features, options); |
Karl Schultz | fda1b38 | 2018-08-08 18:56:11 -0600 | [diff] [blame] | 4195 | spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4196 | if (spv_valid != SPV_SUCCESS) { |
| 4197 | if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4198 | if (spv_valid == SPV_WARNING) { |
| 4199 | skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s", |
| 4200 | diag && diag->error ? diag->error : "(no error text)"); |
| 4201 | } else { |
| 4202 | skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s", |
| 4203 | diag && diag->error ? diag->error : "(no error text)"); |
| 4204 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4205 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4206 | } else { |
| 4207 | if (cache) { |
| 4208 | cache->Insert(hash); |
| 4209 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | spvDiagnosticDestroy(diag); |
| 4213 | spvContextDestroy(ctx); |
| 4214 | } |
| 4215 | |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4216 | return skip; |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 4217 | } |
| 4218 | |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 4219 | bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader) const { |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4220 | bool skip = false; |
| 4221 | uint32_t local_size_x = 0; |
| 4222 | uint32_t local_size_y = 0; |
| 4223 | uint32_t local_size_z = 0; |
| 4224 | if (FindLocalSize(shader, local_size_x, local_size_y, local_size_z)) { |
| 4225 | if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4226 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4227 | "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").", |
| 4228 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4229 | phys_dev_props.limits.maxComputeWorkGroupSize[0]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4230 | } |
| 4231 | if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4232 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4233 | "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").", |
| 4234 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4235 | phys_dev_props.limits.maxComputeWorkGroupSize[1]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4236 | } |
| 4237 | if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4238 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4239 | "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").", |
| 4240 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4241 | phys_dev_props.limits.maxComputeWorkGroupSize[2]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations; |
| 4245 | uint64_t invocations = local_size_x * local_size_y; |
| 4246 | // Prevent overflow. |
| 4247 | bool fail = false; |
| 4248 | if (invocations > UINT32_MAX || invocations > limit) { |
| 4249 | fail = true; |
| 4250 | } |
| 4251 | if (!fail) { |
| 4252 | invocations *= local_size_z; |
| 4253 | if (invocations > UINT32_MAX || invocations > limit) { |
| 4254 | fail = true; |
| 4255 | } |
| 4256 | } |
| 4257 | if (fail) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4258 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations", |
| 4259 | "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32 |
| 4260 | ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").", |
| 4261 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, local_size_y, local_size_z, |
| 4262 | limit); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4263 | } |
| 4264 | } |
| 4265 | return skip; |
| 4266 | } |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 4267 | |
| 4268 | spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) { |
| 4269 | if (api_version >= VK_API_VERSION_1_2) { |
| 4270 | return SPV_ENV_VULKAN_1_2; |
| 4271 | } else if (api_version >= VK_API_VERSION_1_1) { |
| 4272 | if (spirv_1_4) { |
| 4273 | return SPV_ENV_VULKAN_1_1_SPIRV_1_4; |
| 4274 | } else { |
| 4275 | return SPV_ENV_VULKAN_1_1; |
| 4276 | } |
| 4277 | } |
| 4278 | return SPV_ENV_VULKAN_1_0; |
| 4279 | } |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 4280 | |
| 4281 | void AdjustValidatorOptions(const DeviceExtensions device_extensions, const DeviceFeatures enabled_features, |
| 4282 | spvtools::ValidatorOptions &options) { |
| 4283 | if (device_extensions.vk_khr_relaxed_block_layout) { |
| 4284 | options.SetRelaxBlockLayout(true); |
| 4285 | } |
| 4286 | if (device_extensions.vk_khr_uniform_buffer_standard_layout && enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) { |
| 4287 | options.SetUniformBufferStandardLayout(true); |
| 4288 | } |
| 4289 | if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) { |
| 4290 | options.SetScalarBlockLayout(true); |
| 4291 | } |
| 4292 | } |