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