Karl Schultz | 7b024b4 | 2018-08-30 16:18:18 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2015-2019 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2019 Valve Corporation |
| 3 | * Copyright (c) 2015-2019 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2019 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 | |
| 22 | #include <cinttypes> |
| 23 | #include <cassert> |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 24 | #include <chrono> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | #include <unordered_map> |
| 27 | #include <string> |
| 28 | #include <sstream> |
| 29 | #include <SPIRV/spirv.hpp> |
| 30 | #include "vk_loader_platform.h" |
| 31 | #include "vk_enum_string_helper.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 32 | #include "vk_layer_data.h" |
| 33 | #include "vk_layer_extension_utils.h" |
| 34 | #include "vk_layer_utils.h" |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 35 | #include "chassis.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 36 | #include "core_validation.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 37 | #include "shader_validation.h" |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 38 | #include "spirv-tools/libspirv.h" |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 39 | #include "xxhash.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 40 | |
| 41 | enum FORMAT_TYPE { |
| 42 | FORMAT_TYPE_FLOAT = 1, // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader |
| 43 | FORMAT_TYPE_SINT = 2, |
| 44 | FORMAT_TYPE_UINT = 4, |
| 45 | }; |
| 46 | |
| 47 | typedef std::pair<unsigned, unsigned> location_t; |
| 48 | |
| 49 | struct interface_var { |
| 50 | uint32_t id; |
| 51 | uint32_t type_id; |
| 52 | uint32_t offset; |
| 53 | bool is_patch; |
| 54 | bool is_block_member; |
| 55 | bool is_relaxed_precision; |
| 56 | // TODO: collect the name, too? Isn't required to be present. |
| 57 | }; |
| 58 | |
| 59 | struct shader_stage_attributes { |
| 60 | char const *const name; |
| 61 | bool arrayed_input; |
| 62 | bool arrayed_output; |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 63 | VkShaderStageFlags stage; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static shader_stage_attributes shader_stage_attribs[] = { |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 67 | {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT}, |
| 68 | {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT}, |
| 69 | {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT}, |
| 70 | {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT}, |
| 71 | {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // SPIRV utility functions |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 75 | void shader_module::BuildDefIndex() { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 76 | for (auto insn : *this) { |
| 77 | switch (insn.opcode()) { |
| 78 | // Types |
| 79 | case spv::OpTypeVoid: |
| 80 | case spv::OpTypeBool: |
| 81 | case spv::OpTypeInt: |
| 82 | case spv::OpTypeFloat: |
| 83 | case spv::OpTypeVector: |
| 84 | case spv::OpTypeMatrix: |
| 85 | case spv::OpTypeImage: |
| 86 | case spv::OpTypeSampler: |
| 87 | case spv::OpTypeSampledImage: |
| 88 | case spv::OpTypeArray: |
| 89 | case spv::OpTypeRuntimeArray: |
| 90 | case spv::OpTypeStruct: |
| 91 | case spv::OpTypeOpaque: |
| 92 | case spv::OpTypePointer: |
| 93 | case spv::OpTypeFunction: |
| 94 | case spv::OpTypeEvent: |
| 95 | case spv::OpTypeDeviceEvent: |
| 96 | case spv::OpTypeReserveId: |
| 97 | case spv::OpTypeQueue: |
| 98 | case spv::OpTypePipe: |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 99 | case spv::OpTypeAccelerationStructureNV: |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 100 | case spv::OpTypeCooperativeMatrixNV: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 101 | def_index[insn.word(1)] = insn.offset(); |
| 102 | break; |
| 103 | |
| 104 | // Fixed constants |
| 105 | case spv::OpConstantTrue: |
| 106 | case spv::OpConstantFalse: |
| 107 | case spv::OpConstant: |
| 108 | case spv::OpConstantComposite: |
| 109 | case spv::OpConstantSampler: |
| 110 | case spv::OpConstantNull: |
| 111 | def_index[insn.word(2)] = insn.offset(); |
| 112 | break; |
| 113 | |
| 114 | // Specialization constants |
| 115 | case spv::OpSpecConstantTrue: |
| 116 | case spv::OpSpecConstantFalse: |
| 117 | case spv::OpSpecConstant: |
| 118 | case spv::OpSpecConstantComposite: |
| 119 | case spv::OpSpecConstantOp: |
| 120 | def_index[insn.word(2)] = insn.offset(); |
| 121 | break; |
| 122 | |
| 123 | // Variables |
| 124 | case spv::OpVariable: |
| 125 | def_index[insn.word(2)] = insn.offset(); |
| 126 | break; |
| 127 | |
| 128 | // Functions |
| 129 | case spv::OpFunction: |
| 130 | def_index[insn.word(2)] = insn.offset(); |
| 131 | break; |
| 132 | |
| 133 | default: |
| 134 | // We don't care about any other defs for now. |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 140 | unsigned ExecutionModelToShaderStageFlagBits(unsigned mode) { |
| 141 | switch (mode) { |
| 142 | case spv::ExecutionModelVertex: |
| 143 | return VK_SHADER_STAGE_VERTEX_BIT; |
| 144 | case spv::ExecutionModelTessellationControl: |
| 145 | return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 146 | case spv::ExecutionModelTessellationEvaluation: |
| 147 | return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 148 | case spv::ExecutionModelGeometry: |
| 149 | return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 150 | case spv::ExecutionModelFragment: |
| 151 | return VK_SHADER_STAGE_FRAGMENT_BIT; |
| 152 | case spv::ExecutionModelGLCompute: |
| 153 | return VK_SHADER_STAGE_COMPUTE_BIT; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 154 | case spv::ExecutionModelRayGenerationNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 155 | return VK_SHADER_STAGE_RAYGEN_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 156 | case spv::ExecutionModelAnyHitNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 157 | return VK_SHADER_STAGE_ANY_HIT_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 158 | case spv::ExecutionModelClosestHitNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 159 | return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 160 | case spv::ExecutionModelMissNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 161 | return VK_SHADER_STAGE_MISS_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 162 | case spv::ExecutionModelIntersectionNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 163 | return VK_SHADER_STAGE_INTERSECTION_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 164 | case spv::ExecutionModelCallableNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 165 | return VK_SHADER_STAGE_CALLABLE_BIT_NV; |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 166 | case spv::ExecutionModelTaskNV: |
| 167 | return VK_SHADER_STAGE_TASK_BIT_NV; |
| 168 | case spv::ExecutionModelMeshNV: |
| 169 | return VK_SHADER_STAGE_MESH_BIT_NV; |
| 170 | default: |
| 171 | return 0; |
| 172 | } |
| 173 | } |
| 174 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 175 | static spirv_inst_iter FindEntrypoint(shader_module const *src, char const *name, VkShaderStageFlagBits stageBits) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 176 | for (auto insn : *src) { |
| 177 | if (insn.opcode() == spv::OpEntryPoint) { |
| 178 | auto entrypointName = (char const *)&insn.word(3); |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 179 | auto executionModel = insn.word(1); |
| 180 | auto entrypointStageBits = ExecutionModelToShaderStageFlagBits(executionModel); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 181 | |
| 182 | if (!strcmp(entrypointName, name) && (entrypointStageBits & stageBits)) { |
| 183 | return insn; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return src->end(); |
| 189 | } |
| 190 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 191 | static char const *StorageClassName(unsigned sc) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 192 | switch (sc) { |
| 193 | case spv::StorageClassInput: |
| 194 | return "input"; |
| 195 | case spv::StorageClassOutput: |
| 196 | return "output"; |
| 197 | case spv::StorageClassUniformConstant: |
| 198 | return "const uniform"; |
| 199 | case spv::StorageClassUniform: |
| 200 | return "uniform"; |
| 201 | case spv::StorageClassWorkgroup: |
| 202 | return "workgroup local"; |
| 203 | case spv::StorageClassCrossWorkgroup: |
| 204 | return "workgroup global"; |
| 205 | case spv::StorageClassPrivate: |
| 206 | return "private global"; |
| 207 | case spv::StorageClassFunction: |
| 208 | return "function"; |
| 209 | case spv::StorageClassGeneric: |
| 210 | return "generic"; |
| 211 | case spv::StorageClassAtomicCounter: |
| 212 | return "atomic counter"; |
| 213 | case spv::StorageClassImage: |
| 214 | return "image"; |
| 215 | case spv::StorageClassPushConstant: |
| 216 | return "push constant"; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 217 | case spv::StorageClassStorageBuffer: |
| 218 | return "storage buffer"; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 219 | default: |
| 220 | return "unknown"; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Get the value of an integral constant |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 225 | unsigned GetConstantValue(shader_module const *src, unsigned id) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 226 | auto value = src->get_def(id); |
| 227 | assert(value != src->end()); |
| 228 | |
| 229 | if (value.opcode() != spv::OpConstant) { |
| 230 | // TODO: Either ensure that the specialization transform is already performed on a module we're |
| 231 | // considering here, OR -- specialize on the fly now. |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | return value.word(3); |
| 236 | } |
| 237 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 238 | static void DescribeTypeInner(std::ostringstream &ss, shader_module const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 239 | auto insn = src->get_def(type); |
| 240 | assert(insn != src->end()); |
| 241 | |
| 242 | switch (insn.opcode()) { |
| 243 | case spv::OpTypeBool: |
| 244 | ss << "bool"; |
| 245 | break; |
| 246 | case spv::OpTypeInt: |
| 247 | ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2); |
| 248 | break; |
| 249 | case spv::OpTypeFloat: |
| 250 | ss << "float" << insn.word(2); |
| 251 | break; |
| 252 | case spv::OpTypeVector: |
| 253 | ss << "vec" << insn.word(3) << " of "; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 254 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 255 | break; |
| 256 | case spv::OpTypeMatrix: |
| 257 | ss << "mat" << insn.word(3) << " of "; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 258 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 259 | break; |
| 260 | case spv::OpTypeArray: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 261 | ss << "arr[" << GetConstantValue(src, insn.word(3)) << "] of "; |
| 262 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 263 | break; |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 264 | case spv::OpTypeRuntimeArray: |
| 265 | ss << "runtime arr[] of "; |
| 266 | DescribeTypeInner(ss, src, insn.word(2)); |
| 267 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 268 | case spv::OpTypePointer: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 269 | ss << "ptr to " << StorageClassName(insn.word(2)) << " "; |
| 270 | DescribeTypeInner(ss, src, insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 271 | break; |
| 272 | case spv::OpTypeStruct: { |
| 273 | ss << "struct of ("; |
| 274 | for (unsigned i = 2; i < insn.len(); i++) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 275 | DescribeTypeInner(ss, src, insn.word(i)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 276 | if (i == insn.len() - 1) { |
| 277 | ss << ")"; |
| 278 | } else { |
| 279 | ss << ", "; |
| 280 | } |
| 281 | } |
| 282 | break; |
| 283 | } |
| 284 | case spv::OpTypeSampler: |
| 285 | ss << "sampler"; |
| 286 | break; |
| 287 | case spv::OpTypeSampledImage: |
| 288 | ss << "sampler+"; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 289 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 290 | break; |
| 291 | case spv::OpTypeImage: |
| 292 | ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")"; |
| 293 | break; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 294 | case spv::OpTypeAccelerationStructureNV: |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 295 | ss << "accelerationStruture"; |
| 296 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 297 | default: |
| 298 | ss << "oddtype"; |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 303 | static std::string DescribeType(shader_module const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 304 | std::ostringstream ss; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 305 | DescribeTypeInner(ss, src, type); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 306 | return ss.str(); |
| 307 | } |
| 308 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 309 | static bool IsNarrowNumericType(spirv_inst_iter type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 310 | if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false; |
| 311 | return type.word(2) < 64; |
| 312 | } |
| 313 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 314 | static bool TypesMatch(shader_module const *a, shader_module const *b, unsigned a_type, unsigned b_type, bool a_arrayed, |
| 315 | bool b_arrayed, bool relaxed) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 316 | // Walk two type trees together, and complain about differences |
| 317 | auto a_insn = a->get_def(a_type); |
| 318 | auto b_insn = b->get_def(b_type); |
| 319 | assert(a_insn != a->end()); |
| 320 | assert(b_insn != b->end()); |
| 321 | |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 322 | // Ignore runtime-sized arrays-- they cannot appear in these interfaces. |
| 323 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 324 | if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 325 | 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] | 326 | } |
| 327 | |
| 328 | if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) { |
| 329 | // 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] | 330 | 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] | 331 | } |
| 332 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 333 | if (a_insn.opcode() == spv::OpTypeVector && relaxed && IsNarrowNumericType(b_insn)) { |
| 334 | 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] | 335 | } |
| 336 | |
| 337 | if (a_insn.opcode() != b_insn.opcode()) { |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | if (a_insn.opcode() == spv::OpTypePointer) { |
| 342 | // Match on pointee type. storage class is expected to differ |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 343 | 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] | 344 | } |
| 345 | |
| 346 | if (a_arrayed || b_arrayed) { |
| 347 | // If we havent resolved array-of-verts by here, we're not going to. |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | switch (a_insn.opcode()) { |
| 352 | case spv::OpTypeBool: |
| 353 | return true; |
| 354 | case spv::OpTypeInt: |
| 355 | // Match on width, signedness |
| 356 | return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3); |
| 357 | case spv::OpTypeFloat: |
| 358 | // Match on width |
| 359 | return a_insn.word(2) == b_insn.word(2); |
| 360 | case spv::OpTypeVector: |
| 361 | // Match on element type, count. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 362 | if (!TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false; |
| 363 | if (relaxed && IsNarrowNumericType(a->get_def(a_insn.word(2)))) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 364 | return a_insn.word(3) >= b_insn.word(3); |
| 365 | } else { |
| 366 | return a_insn.word(3) == b_insn.word(3); |
| 367 | } |
| 368 | case spv::OpTypeMatrix: |
| 369 | // Match on element type, count. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 370 | 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] | 371 | a_insn.word(3) == b_insn.word(3); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 372 | case spv::OpTypeArray: |
| 373 | // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from |
| 374 | // 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] | 375 | return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) && |
| 376 | GetConstantValue(a, a_insn.word(3)) == GetConstantValue(b, b_insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 377 | case spv::OpTypeStruct: |
| 378 | // Match on all element types |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 379 | { |
| 380 | if (a_insn.len() != b_insn.len()) { |
| 381 | return false; // Structs cannot match if member counts differ |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 382 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 383 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 384 | for (unsigned i = 2; i < a_insn.len(); i++) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 385 | 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] | 386 | return false; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return true; |
| 391 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 392 | default: |
| 393 | // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match. |
| 394 | return false; |
| 395 | } |
| 396 | } |
| 397 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 398 | static unsigned ValueOrDefault(std::unordered_map<unsigned, unsigned> const &map, unsigned id, unsigned def) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 399 | auto it = map.find(id); |
| 400 | if (it == map.end()) |
| 401 | return def; |
| 402 | else |
| 403 | return it->second; |
| 404 | } |
| 405 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 406 | static unsigned GetLocationsConsumedByType(shader_module const *src, unsigned type, bool strip_array_level) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 407 | auto insn = src->get_def(type); |
| 408 | assert(insn != src->end()); |
| 409 | |
| 410 | switch (insn.opcode()) { |
| 411 | case spv::OpTypePointer: |
| 412 | // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing |
| 413 | // pointers around. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 414 | return GetLocationsConsumedByType(src, insn.word(3), strip_array_level); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 415 | case spv::OpTypeArray: |
| 416 | if (strip_array_level) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 417 | return GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 418 | } else { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 419 | return GetConstantValue(src, insn.word(3)) * GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 420 | } |
| 421 | case spv::OpTypeMatrix: |
| 422 | // Num locations is the dimension * element size |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 423 | return insn.word(3) * GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 424 | case spv::OpTypeVector: { |
| 425 | auto scalar_type = src->get_def(insn.word(2)); |
| 426 | auto bit_width = |
| 427 | (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32; |
| 428 | |
| 429 | // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two. |
| 430 | return (bit_width * insn.word(3) + 127) / 128; |
| 431 | } |
| 432 | default: |
| 433 | // Everything else is just 1. |
| 434 | return 1; |
| 435 | |
| 436 | // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations. |
| 437 | } |
| 438 | } |
| 439 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 440 | static unsigned GetComponentsConsumedByType(shader_module const *src, unsigned type, bool strip_array_level) { |
| 441 | auto insn = src->get_def(type); |
| 442 | assert(insn != src->end()); |
| 443 | |
| 444 | switch (insn.opcode()) { |
| 445 | case spv::OpTypePointer: |
| 446 | // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing |
| 447 | // pointers around. |
| 448 | return GetComponentsConsumedByType(src, insn.word(3), strip_array_level); |
| 449 | case spv::OpTypeStruct: { |
| 450 | uint32_t sum = 0; |
| 451 | for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct |
| 452 | sum += GetComponentsConsumedByType(src, insn.word(i), false); |
| 453 | } |
| 454 | return sum; |
| 455 | } |
| 456 | case spv::OpTypeArray: { |
| 457 | uint32_t sum = 0; |
| 458 | for (uint32_t i = 2; i < insn.len(); i++) { |
| 459 | sum += GetComponentsConsumedByType(src, insn.word(i), false); |
| 460 | } |
| 461 | return sum; |
| 462 | } |
| 463 | case spv::OpTypeMatrix: |
| 464 | // Num locations is the dimension * element size |
| 465 | return insn.word(3) * GetComponentsConsumedByType(src, insn.word(2), false); |
| 466 | case spv::OpTypeVector: { |
| 467 | auto scalar_type = src->get_def(insn.word(2)); |
| 468 | auto bit_width = |
| 469 | (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32; |
| 470 | // One component is 32-bit |
| 471 | return (bit_width * insn.word(3) + 31) / 32; |
| 472 | } |
| 473 | case spv::OpTypeFloat: { |
| 474 | auto bit_width = insn.word(2); |
| 475 | return (bit_width + 31) / 32; |
| 476 | } |
| 477 | case spv::OpTypeInt: { |
| 478 | auto bit_width = insn.word(2); |
| 479 | return (bit_width + 31) / 32; |
| 480 | } |
| 481 | case spv::OpConstant: |
| 482 | return GetComponentsConsumedByType(src, insn.word(1), false); |
| 483 | default: |
| 484 | return 0; |
| 485 | } |
| 486 | } |
| 487 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 488 | static unsigned GetLocationsConsumedByFormat(VkFormat format) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 489 | switch (format) { |
| 490 | case VK_FORMAT_R64G64B64A64_SFLOAT: |
| 491 | case VK_FORMAT_R64G64B64A64_SINT: |
| 492 | case VK_FORMAT_R64G64B64A64_UINT: |
| 493 | case VK_FORMAT_R64G64B64_SFLOAT: |
| 494 | case VK_FORMAT_R64G64B64_SINT: |
| 495 | case VK_FORMAT_R64G64B64_UINT: |
| 496 | return 2; |
| 497 | default: |
| 498 | return 1; |
| 499 | } |
| 500 | } |
| 501 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 502 | static unsigned GetFormatType(VkFormat fmt) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 503 | if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT; |
| 504 | if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT; |
| 505 | if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT; |
| 506 | if (fmt == VK_FORMAT_UNDEFINED) return 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 507 | // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader. |
| 508 | return FORMAT_TYPE_FLOAT; |
| 509 | } |
| 510 | |
| 511 | // 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] | 512 | // also used for input attachments, as we statically know their format. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 513 | static unsigned GetFundamentalType(shader_module const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 514 | auto insn = src->get_def(type); |
| 515 | assert(insn != src->end()); |
| 516 | |
| 517 | switch (insn.opcode()) { |
| 518 | case spv::OpTypeInt: |
| 519 | return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT; |
| 520 | case spv::OpTypeFloat: |
| 521 | return FORMAT_TYPE_FLOAT; |
| 522 | case spv::OpTypeVector: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 523 | case spv::OpTypeMatrix: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 524 | case spv::OpTypeArray: |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 525 | case spv::OpTypeRuntimeArray: |
| 526 | case spv::OpTypeImage: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 527 | return GetFundamentalType(src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 528 | case spv::OpTypePointer: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 529 | return GetFundamentalType(src, insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 530 | |
| 531 | default: |
| 532 | return 0; |
| 533 | } |
| 534 | } |
| 535 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 536 | static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 537 | uint32_t bit_pos = uint32_t(u_ffs(stage)); |
| 538 | return bit_pos - 1; |
| 539 | } |
| 540 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 541 | static spirv_inst_iter GetStructType(shader_module const *src, spirv_inst_iter def, bool is_array_of_verts) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 542 | while (true) { |
| 543 | if (def.opcode() == spv::OpTypePointer) { |
| 544 | def = src->get_def(def.word(3)); |
| 545 | } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) { |
| 546 | def = src->get_def(def.word(2)); |
| 547 | is_array_of_verts = false; |
| 548 | } else if (def.opcode() == spv::OpTypeStruct) { |
| 549 | return def; |
| 550 | } else { |
| 551 | return src->end(); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 556 | static bool CollectInterfaceBlockMembers(shader_module const *src, std::map<location_t, interface_var> *out, |
| 557 | std::unordered_map<unsigned, unsigned> const &blocks, bool is_array_of_verts, uint32_t id, |
| 558 | uint32_t type_id, bool is_patch, int /*first_location*/) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 559 | // 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] | 560 | auto type = GetStructType(src, src->get_def(type_id), is_array_of_verts && !is_patch); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 561 | if (type == src->end() || blocks.find(type.word(1)) == blocks.end()) { |
| 562 | // This isn't an interface block. |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 563 | return false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | std::unordered_map<unsigned, unsigned> member_components; |
| 567 | std::unordered_map<unsigned, unsigned> member_relaxed_precision; |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 568 | std::unordered_map<unsigned, unsigned> member_patch; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 569 | |
| 570 | // Walk all the OpMemberDecorate for type's result id -- first pass, collect components. |
| 571 | for (auto insn : *src) { |
| 572 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) { |
| 573 | unsigned member_index = insn.word(2); |
| 574 | |
| 575 | if (insn.word(3) == spv::DecorationComponent) { |
| 576 | unsigned component = insn.word(4); |
| 577 | member_components[member_index] = component; |
| 578 | } |
| 579 | |
| 580 | if (insn.word(3) == spv::DecorationRelaxedPrecision) { |
| 581 | member_relaxed_precision[member_index] = 1; |
| 582 | } |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 583 | |
| 584 | if (insn.word(3) == spv::DecorationPatch) { |
| 585 | member_patch[member_index] = 1; |
| 586 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 590 | // TODO: correctly handle location assignment from outside |
| 591 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 592 | // Second pass -- produce the output, from Location decorations |
| 593 | for (auto insn : *src) { |
| 594 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) { |
| 595 | unsigned member_index = insn.word(2); |
| 596 | unsigned member_type_id = type.word(2 + member_index); |
| 597 | |
| 598 | if (insn.word(3) == spv::DecorationLocation) { |
| 599 | unsigned location = insn.word(4); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 600 | unsigned num_locations = GetLocationsConsumedByType(src, member_type_id, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 601 | auto component_it = member_components.find(member_index); |
| 602 | unsigned component = component_it == member_components.end() ? 0 : component_it->second; |
| 603 | 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] | 604 | bool member_is_patch = is_patch || member_patch.count(member_index) > 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 605 | |
| 606 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 607 | interface_var v = {}; |
| 608 | v.id = id; |
| 609 | // TODO: member index in interface_var too? |
| 610 | v.type_id = member_type_id; |
| 611 | v.offset = offset; |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 612 | v.is_patch = member_is_patch; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 613 | v.is_block_member = true; |
| 614 | v.is_relaxed_precision = is_relaxed_precision; |
| 615 | (*out)[std::make_pair(location + offset, component)] = v; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 620 | |
| 621 | return true; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 624 | static std::vector<uint32_t> FindEntrypointInterfaces(spirv_inst_iter entrypoint) { |
| 625 | std::vector<uint32_t> interfaces; |
| 626 | // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the |
| 627 | // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator. |
| 628 | uint32_t word = 3; |
| 629 | while (entrypoint.word(word) & 0xff000000u) { |
| 630 | ++word; |
| 631 | } |
| 632 | ++word; |
| 633 | |
| 634 | for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word)); |
| 635 | |
| 636 | return interfaces; |
| 637 | } |
| 638 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 639 | static std::map<location_t, interface_var> CollectInterfaceByLocation(shader_module const *src, spirv_inst_iter entrypoint, |
| 640 | spv::StorageClass sinterface, bool is_array_of_verts) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 641 | std::unordered_map<unsigned, unsigned> var_locations; |
| 642 | std::unordered_map<unsigned, unsigned> var_builtins; |
| 643 | std::unordered_map<unsigned, unsigned> var_components; |
| 644 | std::unordered_map<unsigned, unsigned> blocks; |
| 645 | std::unordered_map<unsigned, unsigned> var_patch; |
| 646 | std::unordered_map<unsigned, unsigned> var_relaxed_precision; |
| 647 | |
| 648 | for (auto insn : *src) { |
| 649 | // We consider two interface models: SSO rendezvous-by-location, and builtins. Complain about anything that |
| 650 | // fits neither model. |
| 651 | if (insn.opcode() == spv::OpDecorate) { |
| 652 | if (insn.word(2) == spv::DecorationLocation) { |
| 653 | var_locations[insn.word(1)] = insn.word(3); |
| 654 | } |
| 655 | |
| 656 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 657 | var_builtins[insn.word(1)] = insn.word(3); |
| 658 | } |
| 659 | |
| 660 | if (insn.word(2) == spv::DecorationComponent) { |
| 661 | var_components[insn.word(1)] = insn.word(3); |
| 662 | } |
| 663 | |
| 664 | if (insn.word(2) == spv::DecorationBlock) { |
| 665 | blocks[insn.word(1)] = 1; |
| 666 | } |
| 667 | |
| 668 | if (insn.word(2) == spv::DecorationPatch) { |
| 669 | var_patch[insn.word(1)] = 1; |
| 670 | } |
| 671 | |
| 672 | if (insn.word(2) == spv::DecorationRelaxedPrecision) { |
| 673 | var_relaxed_precision[insn.word(1)] = 1; |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | // TODO: handle grouped decorations |
| 679 | // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber. |
| 680 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 681 | std::map<location_t, interface_var> out; |
| 682 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 683 | for (uint32_t word : FindEntrypointInterfaces(entrypoint)) { |
| 684 | auto insn = src->get_def(word); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 685 | assert(insn != src->end()); |
| 686 | assert(insn.opcode() == spv::OpVariable); |
| 687 | |
| 688 | if (insn.word(3) == static_cast<uint32_t>(sinterface)) { |
| 689 | unsigned id = insn.word(2); |
| 690 | unsigned type = insn.word(1); |
| 691 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 692 | int location = ValueOrDefault(var_locations, id, static_cast<unsigned>(-1)); |
| 693 | int builtin = ValueOrDefault(var_builtins, id, static_cast<unsigned>(-1)); |
| 694 | unsigned component = ValueOrDefault(var_components, id, 0); // Unspecified is OK, is 0 |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 695 | bool is_patch = var_patch.find(id) != var_patch.end(); |
| 696 | bool is_relaxed_precision = var_relaxed_precision.find(id) != var_relaxed_precision.end(); |
| 697 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 698 | if (builtin != -1) |
| 699 | continue; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 700 | else if (!CollectInterfaceBlockMembers(src, &out, blocks, is_array_of_verts, id, type, is_patch, location)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 701 | // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit |
| 702 | // one result for each. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 703 | unsigned num_locations = GetLocationsConsumedByType(src, type, is_array_of_verts && !is_patch); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 704 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 705 | interface_var v = {}; |
| 706 | v.id = id; |
| 707 | v.type_id = type; |
| 708 | v.offset = offset; |
| 709 | v.is_patch = is_patch; |
| 710 | v.is_relaxed_precision = is_relaxed_precision; |
| 711 | out[std::make_pair(location + offset, component)] = v; |
| 712 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | return out; |
| 718 | } |
| 719 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 720 | static std::vector<uint32_t> CollectBuiltinBlockMembers(shader_module const *src, spirv_inst_iter entrypoint, |
| 721 | uint32_t storageClass) { |
| 722 | std::vector<uint32_t> variables; |
| 723 | std::vector<uint32_t> builtinStructMembers; |
| 724 | std::vector<uint32_t> builtinDecorations; |
| 725 | |
| 726 | for (auto insn : *src) { |
| 727 | switch (insn.opcode()) { |
| 728 | // Find all built-in member decorations |
| 729 | case spv::OpMemberDecorate: |
| 730 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 731 | builtinStructMembers.push_back(insn.word(1)); |
| 732 | } |
| 733 | break; |
| 734 | // Find all built-in decorations |
| 735 | case spv::OpDecorate: |
| 736 | switch (insn.word(2)) { |
| 737 | case spv::DecorationBlock: { |
| 738 | uint32_t blockID = insn.word(1); |
| 739 | for (auto builtInBlockID : builtinStructMembers) { |
| 740 | // Check if one of the members of the block are built-in -> the block is built-in |
| 741 | if (blockID == builtInBlockID) { |
| 742 | builtinDecorations.push_back(blockID); |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | break; |
| 747 | } |
| 748 | case spv::DecorationBuiltIn: |
| 749 | builtinDecorations.push_back(insn.word(1)); |
| 750 | break; |
| 751 | default: |
| 752 | break; |
| 753 | } |
| 754 | break; |
| 755 | default: |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | // Find all interface variables belonging to the entrypoint and matching the storage class |
| 761 | for (uint32_t id : FindEntrypointInterfaces(entrypoint)) { |
| 762 | auto def = src->get_def(id); |
| 763 | assert(def != src->end()); |
| 764 | assert(def.opcode() == spv::OpVariable); |
| 765 | |
| 766 | if (def.word(3) == storageClass) variables.push_back(def.word(1)); |
| 767 | } |
| 768 | |
| 769 | // Find all members belonging to the builtin block selected |
| 770 | std::vector<uint32_t> builtinBlockMembers; |
| 771 | for (auto &var : variables) { |
| 772 | auto def = src->get_def(src->get_def(var).word(3)); |
| 773 | |
| 774 | // It could be an array of IO blocks. The element type should be the struct defining the block contents |
| 775 | if (def.opcode() == spv::OpTypeArray) def = src->get_def(def.word(2)); |
| 776 | |
| 777 | // Now find all members belonging to the struct defining the IO block |
| 778 | if (def.opcode() == spv::OpTypeStruct) { |
| 779 | for (auto builtInID : builtinDecorations) { |
| 780 | if (builtInID == def.word(1)) { |
| 781 | for (int i = 2; i < (int)def.len(); i++) |
| 782 | builtinBlockMembers.push_back(spv::BuiltInMax); // Start with undefined builtin for each struct member. |
| 783 | // These shouldn't be left after replacing. |
| 784 | for (auto insn : *src) { |
| 785 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == builtInID && |
| 786 | insn.word(3) == spv::DecorationBuiltIn) { |
| 787 | auto structIndex = insn.word(2); |
| 788 | assert(structIndex < builtinBlockMembers.size()); |
| 789 | builtinBlockMembers[structIndex] = insn.word(4); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | return builtinBlockMembers; |
| 798 | } |
| 799 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 800 | static std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex( |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 801 | shader_module const *src, std::unordered_set<uint32_t> const &accessible_ids) { |
| 802 | std::vector<std::pair<uint32_t, interface_var>> out; |
| 803 | |
| 804 | for (auto insn : *src) { |
| 805 | if (insn.opcode() == spv::OpDecorate) { |
| 806 | if (insn.word(2) == spv::DecorationInputAttachmentIndex) { |
| 807 | auto attachment_index = insn.word(3); |
| 808 | auto id = insn.word(1); |
| 809 | |
| 810 | if (accessible_ids.count(id)) { |
| 811 | auto def = src->get_def(id); |
| 812 | assert(def != src->end()); |
| 813 | |
| 814 | if (def.opcode() == spv::OpVariable && insn.word(3) == spv::StorageClassUniformConstant) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 815 | auto num_locations = GetLocationsConsumedByType(src, def.word(1), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 816 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 817 | interface_var v = {}; |
| 818 | v.id = id; |
| 819 | v.type_id = def.word(1); |
| 820 | v.offset = offset; |
| 821 | out.emplace_back(attachment_index + offset, v); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return out; |
| 830 | } |
| 831 | |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 832 | static bool IsWritableDescriptorType(shader_module const *module, uint32_t type_id, bool is_storage_buffer) { |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 833 | auto type = module->get_def(type_id); |
| 834 | |
| 835 | // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension. |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 836 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) { |
| 837 | if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray) { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 838 | type = module->get_def(type.word(2)); // Element type |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 839 | } else { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 840 | type = module->get_def(type.word(3)); // Pointee type |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
| 844 | switch (type.opcode()) { |
| 845 | case spv::OpTypeImage: { |
| 846 | auto dim = type.word(3); |
| 847 | auto sampled = type.word(7); |
| 848 | return sampled == 2 && dim != spv::DimSubpassData; |
| 849 | } |
| 850 | |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 851 | case spv::OpTypeStruct: { |
| 852 | std::unordered_set<unsigned> nonwritable_members; |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 853 | for (auto insn : *module) { |
| 854 | if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) { |
| 855 | if (insn.word(2) == spv::DecorationBufferBlock) { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 856 | // Legacy storage block in the Uniform storage class |
| 857 | // has its struct type decorated with BufferBlock. |
| 858 | is_storage_buffer = true; |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 859 | } |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 860 | } else if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1) && |
| 861 | insn.word(3) == spv::DecorationNonWritable) { |
| 862 | nonwritable_members.insert(insn.word(2)); |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 863 | } |
| 864 | } |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 865 | |
| 866 | // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated |
| 867 | // as nonwritable. |
| 868 | return is_storage_buffer && nonwritable_members.size() != type.len() - 2; |
| 869 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | return false; |
| 873 | } |
| 874 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 875 | static std::vector<std::pair<descriptor_slot_t, interface_var>> CollectInterfaceByDescriptorSlot( |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 876 | debug_report_data const *report_data, shader_module const *src, std::unordered_set<uint32_t> const &accessible_ids, |
| 877 | bool *has_writable_descriptor) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 878 | std::unordered_map<unsigned, unsigned> var_sets; |
| 879 | std::unordered_map<unsigned, unsigned> var_bindings; |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 880 | std::unordered_map<unsigned, unsigned> var_nonwritable; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 881 | |
| 882 | for (auto insn : *src) { |
| 883 | // All variables in the Uniform or UniformConstant storage classes are required to be decorated with both |
| 884 | // DecorationDescriptorSet and DecorationBinding. |
| 885 | if (insn.opcode() == spv::OpDecorate) { |
| 886 | if (insn.word(2) == spv::DecorationDescriptorSet) { |
| 887 | var_sets[insn.word(1)] = insn.word(3); |
| 888 | } |
| 889 | |
| 890 | if (insn.word(2) == spv::DecorationBinding) { |
| 891 | var_bindings[insn.word(1)] = insn.word(3); |
| 892 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 893 | |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 894 | // Note: do toplevel DecorationNonWritable out here; it applies to |
| 895 | // the OpVariable rather than the type. |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 896 | if (insn.word(2) == spv::DecorationNonWritable) { |
| 897 | var_nonwritable[insn.word(1)] = 1; |
| 898 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
| 902 | std::vector<std::pair<descriptor_slot_t, interface_var>> out; |
| 903 | |
| 904 | for (auto id : accessible_ids) { |
| 905 | auto insn = src->get_def(id); |
| 906 | assert(insn != src->end()); |
| 907 | |
| 908 | if (insn.opcode() == spv::OpVariable && |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 909 | (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant || |
| 910 | insn.word(3) == spv::StorageClassStorageBuffer)) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 911 | unsigned set = ValueOrDefault(var_sets, insn.word(2), 0); |
| 912 | unsigned binding = ValueOrDefault(var_bindings, insn.word(2), 0); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 913 | |
| 914 | interface_var v = {}; |
| 915 | v.id = insn.word(2); |
| 916 | v.type_id = insn.word(1); |
| 917 | out.emplace_back(std::make_pair(set, binding), v); |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 918 | |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 919 | if (var_nonwritable.find(id) == var_nonwritable.end() && |
| 920 | IsWritableDescriptorType(src, insn.word(1), insn.word(3) == spv::StorageClassStorageBuffer)) { |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 921 | *has_writable_descriptor = true; |
| 922 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
| 926 | return out; |
| 927 | } |
| 928 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 929 | static bool ValidateViConsistency(debug_report_data const *report_data, VkPipelineVertexInputStateCreateInfo const *vi) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 930 | // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should |
| 931 | // be specified only once. |
| 932 | std::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings; |
| 933 | bool skip = false; |
| 934 | |
| 935 | for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) { |
| 936 | auto desc = &vi->pVertexBindingDescriptions[i]; |
| 937 | auto &binding = bindings[desc->binding]; |
| 938 | if (binding) { |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 939 | // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps? |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 940 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 941 | kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d", |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 942 | desc->binding); |
| 943 | } else { |
| 944 | binding = desc; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | return skip; |
| 949 | } |
| 950 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 951 | static bool ValidateViAgainstVsInputs(debug_report_data const *report_data, VkPipelineVertexInputStateCreateInfo const *vi, |
| 952 | shader_module const *vs, spirv_inst_iter entrypoint) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 953 | bool skip = false; |
| 954 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 955 | auto inputs = CollectInterfaceByLocation(vs, entrypoint, spv::StorageClassInput, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 956 | |
| 957 | // Build index by location |
| 958 | std::map<uint32_t, VkVertexInputAttributeDescription const *> attribs; |
| 959 | if (vi) { |
| 960 | for (unsigned i = 0; i < vi->vertexAttributeDescriptionCount; i++) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 961 | auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 962 | for (auto j = 0u; j < num_locations; j++) { |
| 963 | attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i]; |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | auto it_a = attribs.begin(); |
| 969 | auto it_b = inputs.begin(); |
| 970 | bool used = false; |
| 971 | |
| 972 | while ((attribs.size() > 0 && it_a != attribs.end()) || (inputs.size() > 0 && it_b != inputs.end())) { |
| 973 | bool a_at_end = attribs.size() == 0 || it_a == attribs.end(); |
| 974 | bool b_at_end = inputs.size() == 0 || it_b == inputs.end(); |
| 975 | auto a_first = a_at_end ? 0 : it_a->first; |
| 976 | auto b_first = b_at_end ? 0 : it_b->first.first; |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 977 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 978 | if (!a_at_end && (b_at_end || a_first < b_first)) { |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 979 | if (!used && |
| 980 | log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 981 | HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed, |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 982 | "Vertex attribute at location %d not consumed by vertex shader", a_first)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 983 | skip = true; |
| 984 | } |
| 985 | used = false; |
| 986 | it_a++; |
| 987 | } else if (!b_at_end && (a_at_end || b_first < a_first)) { |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 988 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 989 | HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_InputNotProduced, |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 990 | "Vertex shader consumes input at location %d but not provided", b_first); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 991 | it_b++; |
| 992 | } else { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 993 | unsigned attrib_type = GetFormatType(it_a->second->format); |
| 994 | unsigned input_type = GetFundamentalType(vs, it_b->second.type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 995 | |
| 996 | // Type checking |
| 997 | if (!(attrib_type & input_type)) { |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 998 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 999 | HandleToUint64(vs->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1000 | "Attribute type of `%s` at location %d does not match vertex shader input type of `%s`", |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1001 | string_VkFormat(it_a->second->format), a_first, DescribeType(vs, it_b->second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | // OK! |
| 1005 | used = true; |
| 1006 | it_b++; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | return skip; |
| 1011 | } |
| 1012 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1013 | static bool ValidateFsOutputsAgainstRenderPass(debug_report_data const *report_data, shader_module const *fs, |
| 1014 | spirv_inst_iter entrypoint, PIPELINE_STATE const *pipeline, uint32_t subpass_index) { |
Petr Kraus | e91f7a1 | 2017-12-14 20:57:36 +0100 | [diff] [blame] | 1015 | auto rpci = pipeline->rp_state->createInfo.ptr(); |
Chris Forbes | 8bca165 | 2017-07-20 11:10:09 -0700 | [diff] [blame] | 1016 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1017 | std::map<uint32_t, VkFormat> color_attachments; |
| 1018 | auto subpass = rpci->pSubpasses[subpass_index]; |
| 1019 | for (auto i = 0u; i < subpass.colorAttachmentCount; ++i) { |
| 1020 | uint32_t attachment = subpass.pColorAttachments[i].attachment; |
| 1021 | if (attachment == VK_ATTACHMENT_UNUSED) continue; |
| 1022 | if (rpci->pAttachments[attachment].format != VK_FORMAT_UNDEFINED) { |
| 1023 | color_attachments[i] = rpci->pAttachments[attachment].format; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | bool skip = false; |
| 1028 | |
| 1029 | // TODO: dual source blend index (spv::DecIndex, zero if not provided) |
| 1030 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1031 | auto outputs = CollectInterfaceByLocation(fs, entrypoint, spv::StorageClassOutput, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1032 | |
| 1033 | auto it_a = outputs.begin(); |
| 1034 | auto it_b = color_attachments.begin(); |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1035 | bool used = false; |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1036 | bool alphaToCoverageEnabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL && |
| 1037 | pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE; |
| 1038 | bool locationZeroHasAlpha = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1039 | |
| 1040 | // Walk attachment list and outputs together |
| 1041 | |
| 1042 | while ((outputs.size() > 0 && it_a != outputs.end()) || (color_attachments.size() > 0 && it_b != color_attachments.end())) { |
| 1043 | bool a_at_end = outputs.size() == 0 || it_a == outputs.end(); |
| 1044 | bool b_at_end = color_attachments.size() == 0 || it_b == color_attachments.end(); |
| 1045 | |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1046 | if (!a_at_end && it_a->first.first == 0 && fs->get_def(it_a->second.type_id) != fs->end() && |
| 1047 | GetComponentsConsumedByType(fs, it_a->second.type_id, false) == 4) |
| 1048 | locationZeroHasAlpha = true; |
| 1049 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1050 | if (!a_at_end && (b_at_end || it_a->first.first < it_b->first)) { |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1051 | if (!alphaToCoverageEnabled || it_a->first.first != 0) { |
| 1052 | skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 1053 | HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed, |
| 1054 | "fragment shader writes to output location %d with no matching attachment", it_a->first.first); |
| 1055 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1056 | it_a++; |
| 1057 | } else if (!b_at_end && (a_at_end || it_a->first.first > it_b->first)) { |
Chris Forbes | efdd408 | 2017-07-20 11:19:16 -0700 | [diff] [blame] | 1058 | // Only complain if there are unmasked channels for this attachment. If the writemask is 0, it's acceptable for the |
| 1059 | // shader to not produce a matching output. |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1060 | if (!used) { |
| 1061 | if (pipeline->attachments[it_b->first].colorWriteMask != 0) { |
Chris Forbes | cfe4dca | 2018-10-05 10:15:00 -0700 | [diff] [blame] | 1062 | skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1063 | HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_InputNotProduced, |
Chris Forbes | cfe4dca | 2018-10-05 10:15:00 -0700 | [diff] [blame] | 1064 | "Attachment %d not written by fragment shader; undefined values will be written to attachment", |
| 1065 | it_b->first); |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1066 | } |
Chris Forbes | efdd408 | 2017-07-20 11:19:16 -0700 | [diff] [blame] | 1067 | } |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1068 | used = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1069 | it_b++; |
| 1070 | } else { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1071 | unsigned output_type = GetFundamentalType(fs, it_a->second.type_id); |
| 1072 | unsigned att_type = GetFormatType(it_b->second); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1073 | |
| 1074 | // Type checking |
| 1075 | if (!(output_type & att_type)) { |
Chris Forbes | cfe4dca | 2018-10-05 10:15:00 -0700 | [diff] [blame] | 1076 | skip |= log_msg( |
| 1077 | report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 1078 | HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch, |
| 1079 | "Attachment %d of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined", |
| 1080 | it_b->first, string_VkFormat(it_b->second), DescribeType(fs, it_a->second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | // OK! |
| 1084 | it_a++; |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1085 | used = true; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1089 | if (alphaToCoverageEnabled && !locationZeroHasAlpha) { |
| 1090 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 1091 | HandleToUint64(fs->vk_shader_module), kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage, |
| 1092 | "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled."); |
| 1093 | } |
| 1094 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1095 | return skip; |
| 1096 | } |
| 1097 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1098 | // For PointSize analysis we need to know if the variable decorated with the PointSize built-in was actually written to. |
| 1099 | // This function examines instructions in the static call tree for a write to this variable. |
| 1100 | static bool IsPointSizeWritten(shader_module const *src, spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) { |
| 1101 | auto type = builtin_instr.opcode(); |
| 1102 | uint32_t target_id = builtin_instr.word(1); |
| 1103 | bool init_complete = false; |
| 1104 | |
| 1105 | if (type == spv::OpMemberDecorate) { |
| 1106 | // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs |
| 1107 | auto insn = entrypoint; |
| 1108 | while (!init_complete && (insn.opcode() != spv::OpFunction)) { |
| 1109 | switch (insn.opcode()) { |
| 1110 | case spv::OpTypePointer: |
| 1111 | if ((insn.word(3) == target_id) && (insn.word(2) == spv::StorageClassOutput)) { |
| 1112 | target_id = insn.word(1); |
| 1113 | } |
| 1114 | break; |
| 1115 | case spv::OpVariable: |
| 1116 | if (insn.word(1) == target_id) { |
| 1117 | target_id = insn.word(2); |
| 1118 | init_complete = true; |
| 1119 | } |
| 1120 | break; |
| 1121 | } |
| 1122 | insn++; |
| 1123 | } |
| 1124 | } |
| 1125 | |
Mark Lobodzinski | f84b0b4 | 2018-09-11 14:54:32 -0600 | [diff] [blame] | 1126 | if (!init_complete && (type == spv::OpMemberDecorate)) return false; |
| 1127 | |
| 1128 | bool found_write = false; |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1129 | std::unordered_set<uint32_t> worklist; |
| 1130 | worklist.insert(entrypoint.word(2)); |
| 1131 | |
| 1132 | // Follow instructions in call graph looking for writes to target |
| 1133 | while (!worklist.empty() && !found_write) { |
| 1134 | auto id_iter = worklist.begin(); |
| 1135 | auto id = *id_iter; |
| 1136 | worklist.erase(id_iter); |
| 1137 | |
| 1138 | auto insn = src->get_def(id); |
| 1139 | if (insn == src->end()) { |
| 1140 | continue; |
| 1141 | } |
| 1142 | |
| 1143 | if (insn.opcode() == spv::OpFunction) { |
| 1144 | // Scan body of function looking for other function calls or items in our ID chain |
| 1145 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1146 | switch (insn.opcode()) { |
| 1147 | case spv::OpAccessChain: |
| 1148 | if (insn.word(3) == target_id) { |
| 1149 | if (type == spv::OpMemberDecorate) { |
| 1150 | auto value = GetConstantValue(src, insn.word(4)); |
| 1151 | if (value == builtin_instr.word(2)) { |
| 1152 | target_id = insn.word(2); |
| 1153 | } |
| 1154 | } else { |
| 1155 | target_id = insn.word(2); |
| 1156 | } |
| 1157 | } |
| 1158 | break; |
| 1159 | case spv::OpStore: |
| 1160 | if (insn.word(1) == target_id) { |
| 1161 | found_write = true; |
| 1162 | } |
| 1163 | break; |
| 1164 | case spv::OpFunctionCall: |
| 1165 | worklist.insert(insn.word(3)); |
| 1166 | break; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | return found_write; |
| 1172 | } |
| 1173 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1174 | // For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is |
| 1175 | // important for identifying the set of shader resources actually used by an entrypoint, for example. |
| 1176 | // Note: we only explore parts of the image which might actually contain ids we care about for the above analyses. |
| 1177 | // - NOT the shader input/output interfaces. |
| 1178 | // |
| 1179 | // TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth |
| 1180 | // converting parts of this to be generated from the machine-readable spec instead. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1181 | static std::unordered_set<uint32_t> MarkAccessibleIds(shader_module const *src, spirv_inst_iter entrypoint) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1182 | std::unordered_set<uint32_t> ids; |
| 1183 | std::unordered_set<uint32_t> worklist; |
| 1184 | worklist.insert(entrypoint.word(2)); |
| 1185 | |
| 1186 | while (!worklist.empty()) { |
| 1187 | auto id_iter = worklist.begin(); |
| 1188 | auto id = *id_iter; |
| 1189 | worklist.erase(id_iter); |
| 1190 | |
| 1191 | auto insn = src->get_def(id); |
| 1192 | if (insn == src->end()) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1193 | // 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] | 1194 | // that we may not care about. |
| 1195 | continue; |
| 1196 | } |
| 1197 | |
| 1198 | // Try to add to the output set |
| 1199 | if (!ids.insert(id).second) { |
| 1200 | continue; // If we already saw this id, we don't want to walk it again. |
| 1201 | } |
| 1202 | |
| 1203 | switch (insn.opcode()) { |
| 1204 | case spv::OpFunction: |
| 1205 | // Scan whole body of the function, enlisting anything interesting |
| 1206 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1207 | switch (insn.opcode()) { |
| 1208 | case spv::OpLoad: |
| 1209 | case spv::OpAtomicLoad: |
| 1210 | case spv::OpAtomicExchange: |
| 1211 | case spv::OpAtomicCompareExchange: |
| 1212 | case spv::OpAtomicCompareExchangeWeak: |
| 1213 | case spv::OpAtomicIIncrement: |
| 1214 | case spv::OpAtomicIDecrement: |
| 1215 | case spv::OpAtomicIAdd: |
| 1216 | case spv::OpAtomicISub: |
| 1217 | case spv::OpAtomicSMin: |
| 1218 | case spv::OpAtomicUMin: |
| 1219 | case spv::OpAtomicSMax: |
| 1220 | case spv::OpAtomicUMax: |
| 1221 | case spv::OpAtomicAnd: |
| 1222 | case spv::OpAtomicOr: |
| 1223 | case spv::OpAtomicXor: |
| 1224 | worklist.insert(insn.word(3)); // ptr |
| 1225 | break; |
| 1226 | case spv::OpStore: |
| 1227 | case spv::OpAtomicStore: |
| 1228 | worklist.insert(insn.word(1)); // ptr |
| 1229 | break; |
| 1230 | case spv::OpAccessChain: |
| 1231 | case spv::OpInBoundsAccessChain: |
| 1232 | worklist.insert(insn.word(3)); // base ptr |
| 1233 | break; |
| 1234 | case spv::OpSampledImage: |
| 1235 | case spv::OpImageSampleImplicitLod: |
| 1236 | case spv::OpImageSampleExplicitLod: |
| 1237 | case spv::OpImageSampleDrefImplicitLod: |
| 1238 | case spv::OpImageSampleDrefExplicitLod: |
| 1239 | case spv::OpImageSampleProjImplicitLod: |
| 1240 | case spv::OpImageSampleProjExplicitLod: |
| 1241 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1242 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1243 | case spv::OpImageFetch: |
| 1244 | case spv::OpImageGather: |
| 1245 | case spv::OpImageDrefGather: |
| 1246 | case spv::OpImageRead: |
| 1247 | case spv::OpImage: |
| 1248 | case spv::OpImageQueryFormat: |
| 1249 | case spv::OpImageQueryOrder: |
| 1250 | case spv::OpImageQuerySizeLod: |
| 1251 | case spv::OpImageQuerySize: |
| 1252 | case spv::OpImageQueryLod: |
| 1253 | case spv::OpImageQueryLevels: |
| 1254 | case spv::OpImageQuerySamples: |
| 1255 | case spv::OpImageSparseSampleImplicitLod: |
| 1256 | case spv::OpImageSparseSampleExplicitLod: |
| 1257 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 1258 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 1259 | case spv::OpImageSparseSampleProjImplicitLod: |
| 1260 | case spv::OpImageSparseSampleProjExplicitLod: |
| 1261 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 1262 | case spv::OpImageSparseSampleProjDrefExplicitLod: |
| 1263 | case spv::OpImageSparseFetch: |
| 1264 | case spv::OpImageSparseGather: |
| 1265 | case spv::OpImageSparseDrefGather: |
| 1266 | case spv::OpImageTexelPointer: |
| 1267 | worklist.insert(insn.word(3)); // Image or sampled image |
| 1268 | break; |
| 1269 | case spv::OpImageWrite: |
| 1270 | worklist.insert(insn.word(1)); // Image -- different operand order to above |
| 1271 | break; |
| 1272 | case spv::OpFunctionCall: |
| 1273 | for (uint32_t i = 3; i < insn.len(); i++) { |
| 1274 | worklist.insert(insn.word(i)); // fn itself, and all args |
| 1275 | } |
| 1276 | break; |
| 1277 | |
| 1278 | case spv::OpExtInst: |
| 1279 | for (uint32_t i = 5; i < insn.len(); i++) { |
| 1280 | worklist.insert(insn.word(i)); // Operands to ext inst |
| 1281 | } |
| 1282 | break; |
| 1283 | } |
| 1284 | } |
| 1285 | break; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | return ids; |
| 1290 | } |
| 1291 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1292 | static bool ValidatePushConstantBlockAgainstPipeline(debug_report_data const *report_data, |
| 1293 | std::vector<VkPushConstantRange> const *push_constant_ranges, |
| 1294 | shader_module const *src, spirv_inst_iter type, VkShaderStageFlagBits stage) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1295 | bool skip = false; |
| 1296 | |
| 1297 | // Strip off ptrs etc |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1298 | type = GetStructType(src, type, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1299 | assert(type != src->end()); |
| 1300 | |
| 1301 | // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step. |
| 1302 | // TODO: arrays, matrices, weird sizes |
| 1303 | for (auto insn : *src) { |
| 1304 | if (insn.opcode() == spv::OpMemberDecorate && insn.word(1) == type.word(1)) { |
| 1305 | if (insn.word(3) == spv::DecorationOffset) { |
| 1306 | unsigned offset = insn.word(4); |
| 1307 | auto size = 4; // Bytes; TODO: calculate this based on the type |
| 1308 | |
| 1309 | bool found_range = false; |
| 1310 | for (auto const &range : *push_constant_ranges) { |
| 1311 | if (range.offset <= offset && range.offset + range.size >= offset + size) { |
| 1312 | found_range = true; |
| 1313 | |
| 1314 | if ((range.stageFlags & stage) == 0) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 1315 | skip |= |
| 1316 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 1317 | kVUID_Core_Shader_PushConstantNotAccessibleFromStage, |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 1318 | "Push constant range covering variable starting at offset %u not accessible from stage %s", |
| 1319 | offset, string_VkShaderStageFlagBits(stage)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | break; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | if (!found_range) { |
| 1327 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 1328 | kVUID_Core_Shader_PushConstantOutOfRange, |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 1329 | "Push constant range covering variable starting at offset %u not declared in layout", offset); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | return skip; |
| 1336 | } |
| 1337 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1338 | static bool ValidatePushConstantUsage(debug_report_data const *report_data, |
| 1339 | std::vector<VkPushConstantRange> const *push_constant_ranges, shader_module const *src, |
| 1340 | std::unordered_set<uint32_t> accessible_ids, VkShaderStageFlagBits stage) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1341 | bool skip = false; |
| 1342 | |
| 1343 | for (auto id : accessible_ids) { |
| 1344 | auto def_insn = src->get_def(id); |
| 1345 | if (def_insn.opcode() == spv::OpVariable && def_insn.word(3) == spv::StorageClassPushConstant) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1346 | skip |= ValidatePushConstantBlockAgainstPipeline(report_data, push_constant_ranges, src, src->get_def(def_insn.word(1)), |
| 1347 | stage); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | return skip; |
| 1352 | } |
| 1353 | |
| 1354 | // Validate that data for each specialization entry is fully contained within the buffer. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1355 | static bool ValidateSpecializationOffsets(debug_report_data const *report_data, VkPipelineShaderStageCreateInfo const *info) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1356 | bool skip = false; |
| 1357 | |
| 1358 | VkSpecializationInfo const *spec = info->pSpecializationInfo; |
| 1359 | |
| 1360 | if (spec) { |
| 1361 | for (auto i = 0u; i < spec->mapEntryCount; i++) { |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 1362 | // TODO: This is a good place for "VUID-VkSpecializationInfo-offset-00773". |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1363 | if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 1364 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 1365 | "VUID-VkSpecializationInfo-pMapEntries-00774", |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 1366 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
Mark Lobodzinski | 487a0d1 | 2018-03-30 10:09:03 -0600 | [diff] [blame] | 1367 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..", |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 1368 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
Mark Lobodzinski | 487a0d1 | 2018-03-30 10:09:03 -0600 | [diff] [blame] | 1369 | spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | return skip; |
| 1375 | } |
| 1376 | |
Jeff Bolz | 38b3ce7 | 2018-09-19 12:53:38 -0500 | [diff] [blame] | 1377 | // TODO (jbolz): Can this return a const reference? |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1378 | static std::set<uint32_t> TypeToDescriptorTypeSet(shader_module const *module, uint32_t type_id, unsigned &descriptor_count) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1379 | auto type = module->get_def(type_id); |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1380 | bool is_storage_buffer = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1381 | descriptor_count = 1; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1382 | std::set<uint32_t> ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1383 | |
| 1384 | // 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] | 1385 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) { |
| 1386 | if (type.opcode() == spv::OpTypeRuntimeArray) { |
| 1387 | descriptor_count = 0; |
| 1388 | type = module->get_def(type.word(2)); |
| 1389 | } else if (type.opcode() == spv::OpTypeArray) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1390 | descriptor_count *= GetConstantValue(module, type.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1391 | type = module->get_def(type.word(2)); |
| 1392 | } else { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1393 | if (type.word(2) == spv::StorageClassStorageBuffer) { |
| 1394 | is_storage_buffer = true; |
| 1395 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1396 | type = module->get_def(type.word(3)); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | switch (type.opcode()) { |
| 1401 | case spv::OpTypeStruct: { |
| 1402 | for (auto insn : *module) { |
| 1403 | if (insn.opcode() == spv::OpDecorate && insn.word(1) == type.word(1)) { |
| 1404 | if (insn.word(2) == spv::DecorationBlock) { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1405 | if (is_storage_buffer) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1406 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 1407 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 1408 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1409 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1410 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 1411 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC); |
| 1412 | ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT); |
| 1413 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1414 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1415 | } else if (insn.word(2) == spv::DecorationBufferBlock) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1416 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 1417 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 1418 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | // Invalid |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1424 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | case spv::OpTypeSampler: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1428 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER); |
| 1429 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 1430 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1431 | |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 1432 | case spv::OpTypeSampledImage: { |
| 1433 | // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel |
| 1434 | // buffer descriptor doesn't really provide one. Allow this slight mismatch. |
| 1435 | auto image_type = module->get_def(type.word(2)); |
| 1436 | auto dim = image_type.word(3); |
| 1437 | auto sampled = image_type.word(7); |
| 1438 | if (dim == spv::DimBuffer && sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1439 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 1440 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1441 | } |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 1442 | } |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1443 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 1444 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1445 | |
| 1446 | case spv::OpTypeImage: { |
| 1447 | // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler. |
| 1448 | // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable. |
| 1449 | auto dim = type.word(3); |
| 1450 | auto sampled = type.word(7); |
| 1451 | |
| 1452 | if (dim == spv::DimSubpassData) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1453 | ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT); |
| 1454 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1455 | } else if (dim == spv::DimBuffer) { |
| 1456 | if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1457 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 1458 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1459 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1460 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER); |
| 1461 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1462 | } |
| 1463 | } else if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1464 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); |
| 1465 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 1466 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1467 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1468 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); |
| 1469 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1470 | } |
| 1471 | } |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 1472 | case spv::OpTypeAccelerationStructureNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 1473 | ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV); |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 1474 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1475 | |
| 1476 | // We shouldn't really see any other junk types -- but if we do, they're a mismatch. |
| 1477 | default: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1478 | return ret; // Matches nothing |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1482 | static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) { |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 1483 | std::stringstream ss; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 1484 | for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) { |
| 1485 | if (ss.tellp()) ss << ", "; |
| 1486 | ss << string_VkDescriptorType(VkDescriptorType(*it)); |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 1487 | } |
| 1488 | return ss.str(); |
| 1489 | } |
| 1490 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1491 | static bool RequireFeature(debug_report_data const *report_data, VkBool32 feature, char const *feature_name) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1492 | if (!feature) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 1493 | if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 1494 | kVUID_Core_Shader_FeatureNotEnabled, "Shader requires %s but is not enabled on the device", feature_name)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1495 | return true; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | return false; |
| 1500 | } |
| 1501 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1502 | static bool RequireExtension(debug_report_data const *report_data, bool extension, char const *extension_name) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1503 | if (!extension) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 1504 | if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 1505 | kVUID_Core_Shader_FeatureNotEnabled, "Shader requires extension %s but is not enabled on the device", |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1506 | extension_name)) { |
| 1507 | return true; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | return false; |
| 1512 | } |
| 1513 | |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 1514 | bool CoreChecks::ValidateShaderCapabilities(shader_module const *src, VkShaderStageFlagBits stage, bool has_writable_descriptor) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1515 | bool skip = false; |
| 1516 | |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 1517 | struct FeaturePointer { |
| 1518 | // Callable object to test if this feature is enabled in the given aggregate feature struct |
| 1519 | const std::function<VkBool32(const DeviceFeatures &)> IsEnabled; |
| 1520 | |
| 1521 | // Test if feature pointer is populated |
| 1522 | explicit operator bool() const { return static_cast<bool>(IsEnabled); } |
| 1523 | |
| 1524 | // Default and nullptr constructor to create an empty FeaturePointer |
| 1525 | FeaturePointer() : IsEnabled(nullptr) {} |
| 1526 | FeaturePointer(std::nullptr_t ptr) : IsEnabled(nullptr) {} |
| 1527 | |
| 1528 | // Constructors to populate FeaturePointer based on given pointer to member |
| 1529 | FeaturePointer(VkBool32 VkPhysicalDeviceFeatures::*ptr) |
| 1530 | : IsEnabled([=](const DeviceFeatures &features) { return features.core.*ptr; }) {} |
| 1531 | FeaturePointer(VkBool32 VkPhysicalDeviceDescriptorIndexingFeaturesEXT::*ptr) |
| 1532 | : IsEnabled([=](const DeviceFeatures &features) { return features.descriptor_indexing.*ptr; }) {} |
| 1533 | FeaturePointer(VkBool32 VkPhysicalDevice8BitStorageFeaturesKHR::*ptr) |
| 1534 | : IsEnabled([=](const DeviceFeatures &features) { return features.eight_bit_storage.*ptr; }) {} |
Brett Lawson | bebfb6f | 2018-10-23 16:58:50 -0700 | [diff] [blame] | 1535 | FeaturePointer(VkBool32 VkPhysicalDeviceTransformFeedbackFeaturesEXT::*ptr) |
| 1536 | : IsEnabled([=](const DeviceFeatures &features) { return features.transform_feedback_features.*ptr; }) {} |
Jose-Emilio Munoz-Lopez | 1109b45 | 2018-08-21 09:44:07 +0100 | [diff] [blame] | 1537 | FeaturePointer(VkBool32 VkPhysicalDeviceFloat16Int8FeaturesKHR::*ptr) |
| 1538 | : IsEnabled([=](const DeviceFeatures &features) { return features.float16_int8.*ptr; }) {} |
Tobias Hector | 6a0ece7 | 2018-12-10 12:24:05 +0000 | [diff] [blame] | 1539 | FeaturePointer(VkBool32 VkPhysicalDeviceScalarBlockLayoutFeaturesEXT::*ptr) |
| 1540 | : IsEnabled([=](const DeviceFeatures &features) { return features.scalar_block_layout_features.*ptr; }) {} |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1541 | FeaturePointer(VkBool32 VkPhysicalDeviceCooperativeMatrixFeaturesNV::*ptr) |
| 1542 | : IsEnabled([=](const DeviceFeatures &features) { return features.cooperative_matrix_features.*ptr; }) {} |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1543 | FeaturePointer(VkBool32 VkPhysicalDeviceFloatControlsPropertiesKHR::*ptr) |
| 1544 | : IsEnabled([=](const DeviceFeatures &features) { return features.float_controls.*ptr; }) {} |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 1545 | }; |
| 1546 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1547 | struct CapabilityInfo { |
| 1548 | char const *name; |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 1549 | FeaturePointer feature; |
| 1550 | bool DeviceExtensions::*extension; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1551 | }; |
| 1552 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1553 | // clang-format off |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 1554 | static const std::unordered_multimap<uint32_t, CapabilityInfo> capabilities = { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1555 | // Capabilities always supported by a Vulkan 1.0 implementation -- no |
| 1556 | // feature bits. |
| 1557 | {spv::CapabilityMatrix, {nullptr}}, |
| 1558 | {spv::CapabilityShader, {nullptr}}, |
| 1559 | {spv::CapabilityInputAttachment, {nullptr}}, |
| 1560 | {spv::CapabilitySampled1D, {nullptr}}, |
| 1561 | {spv::CapabilityImage1D, {nullptr}}, |
| 1562 | {spv::CapabilitySampledBuffer, {nullptr}}, |
| 1563 | {spv::CapabilityImageQuery, {nullptr}}, |
| 1564 | {spv::CapabilityDerivativeControl, {nullptr}}, |
| 1565 | |
| 1566 | // Capabilities that are optionally supported, but require a feature to |
| 1567 | // be enabled on the device |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 1568 | {spv::CapabilityGeometry, {"VkPhysicalDeviceFeatures::geometryShader", &VkPhysicalDeviceFeatures::geometryShader}}, |
| 1569 | {spv::CapabilityTessellation, {"VkPhysicalDeviceFeatures::tessellationShader", &VkPhysicalDeviceFeatures::tessellationShader}}, |
| 1570 | {spv::CapabilityFloat64, {"VkPhysicalDeviceFeatures::shaderFloat64", &VkPhysicalDeviceFeatures::shaderFloat64}}, |
| 1571 | {spv::CapabilityInt64, {"VkPhysicalDeviceFeatures::shaderInt64", &VkPhysicalDeviceFeatures::shaderInt64}}, |
| 1572 | {spv::CapabilityTessellationPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}}, |
| 1573 | {spv::CapabilityGeometryPointSize, {"VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize", &VkPhysicalDeviceFeatures::shaderTessellationAndGeometryPointSize}}, |
| 1574 | {spv::CapabilityImageGatherExtended, {"VkPhysicalDeviceFeatures::shaderImageGatherExtended", &VkPhysicalDeviceFeatures::shaderImageGatherExtended}}, |
| 1575 | {spv::CapabilityStorageImageMultisample, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}}, |
| 1576 | {spv::CapabilityUniformBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderUniformBufferArrayDynamicIndexing}}, |
| 1577 | {spv::CapabilitySampledImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderSampledImageArrayDynamicIndexing}}, |
| 1578 | {spv::CapabilityStorageBufferArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}}, |
| 1579 | {spv::CapabilityStorageImageArrayDynamicIndexing, {"VkPhysicalDeviceFeatures::shaderStorageImageArrayDynamicIndexing", &VkPhysicalDeviceFeatures::shaderStorageBufferArrayDynamicIndexing}}, |
| 1580 | {spv::CapabilityClipDistance, {"VkPhysicalDeviceFeatures::shaderClipDistance", &VkPhysicalDeviceFeatures::shaderClipDistance}}, |
| 1581 | {spv::CapabilityCullDistance, {"VkPhysicalDeviceFeatures::shaderCullDistance", &VkPhysicalDeviceFeatures::shaderCullDistance}}, |
| 1582 | {spv::CapabilityImageCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}}, |
| 1583 | {spv::CapabilitySampleRateShading, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}}, |
| 1584 | {spv::CapabilitySparseResidency, {"VkPhysicalDeviceFeatures::shaderResourceResidency", &VkPhysicalDeviceFeatures::shaderResourceResidency}}, |
| 1585 | {spv::CapabilityMinLod, {"VkPhysicalDeviceFeatures::shaderResourceMinLod", &VkPhysicalDeviceFeatures::shaderResourceMinLod}}, |
| 1586 | {spv::CapabilitySampledCubeArray, {"VkPhysicalDeviceFeatures::imageCubeArray", &VkPhysicalDeviceFeatures::imageCubeArray}}, |
| 1587 | {spv::CapabilityImageMSArray, {"VkPhysicalDeviceFeatures::shaderStorageImageMultisample", &VkPhysicalDeviceFeatures::shaderStorageImageMultisample}}, |
| 1588 | {spv::CapabilityStorageImageExtendedFormats, {"VkPhysicalDeviceFeatures::shaderStorageImageExtendedFormats", &VkPhysicalDeviceFeatures::shaderStorageImageExtendedFormats}}, |
| 1589 | {spv::CapabilityInterpolationFunction, {"VkPhysicalDeviceFeatures::sampleRateShading", &VkPhysicalDeviceFeatures::sampleRateShading}}, |
| 1590 | {spv::CapabilityStorageImageReadWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageReadWithoutFormat}}, |
| 1591 | {spv::CapabilityStorageImageWriteWithoutFormat, {"VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat", &VkPhysicalDeviceFeatures::shaderStorageImageWriteWithoutFormat}}, |
| 1592 | {spv::CapabilityMultiViewport, {"VkPhysicalDeviceFeatures::multiViewport", &VkPhysicalDeviceFeatures::multiViewport}}, |
Jeff Bolz | fdf9607 | 2018-04-10 14:32:18 -0500 | [diff] [blame] | 1593 | |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 1594 | {spv::CapabilityShaderNonUniformEXT, {VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_descriptor_indexing}}, |
| 1595 | {spv::CapabilityRuntimeDescriptorArrayEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::runtimeDescriptorArray", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::runtimeDescriptorArray}}, |
| 1596 | {spv::CapabilityInputAttachmentArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderInputAttachmentArrayDynamicIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderInputAttachmentArrayDynamicIndexing}}, |
| 1597 | {spv::CapabilityUniformTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformTexelBufferArrayDynamicIndexing}}, |
| 1598 | {spv::CapabilityStorageTexelBufferArrayDynamicIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageTexelBufferArrayDynamicIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageTexelBufferArrayDynamicIndexing}}, |
| 1599 | {spv::CapabilityUniformBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformBufferArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformBufferArrayNonUniformIndexing}}, |
| 1600 | {spv::CapabilitySampledImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderSampledImageArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderSampledImageArrayNonUniformIndexing}}, |
| 1601 | {spv::CapabilityStorageBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageBufferArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageBufferArrayNonUniformIndexing}}, |
| 1602 | {spv::CapabilityStorageImageArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageImageArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageImageArrayNonUniformIndexing}}, |
| 1603 | {spv::CapabilityInputAttachmentArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderInputAttachmentArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderInputAttachmentArrayNonUniformIndexing}}, |
| 1604 | {spv::CapabilityUniformTexelBufferArrayNonUniformIndexingEXT, {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderUniformTexelBufferArrayNonUniformIndexing}}, |
| 1605 | {spv::CapabilityStorageTexelBufferArrayNonUniformIndexingEXT , {"VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageTexelBufferArrayNonUniformIndexing", &VkPhysicalDeviceDescriptorIndexingFeaturesEXT::shaderStorageTexelBufferArrayNonUniformIndexing}}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1606 | |
| 1607 | // Capabilities that require an extension |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 1608 | {spv::CapabilityDrawParameters, {VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_draw_parameters}}, |
| 1609 | {spv::CapabilityGeometryShaderPassthroughNV, {VK_NV_GEOMETRY_SHADER_PASSTHROUGH_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_geometry_shader_passthrough}}, |
| 1610 | {spv::CapabilitySampleMaskOverrideCoverageNV, {VK_NV_SAMPLE_MASK_OVERRIDE_COVERAGE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_sample_mask_override_coverage}}, |
| 1611 | {spv::CapabilityShaderViewportIndexLayerEXT, {VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_viewport_index_layer}}, |
| 1612 | {spv::CapabilityShaderViewportIndexLayerNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}}, |
| 1613 | {spv::CapabilityShaderViewportMaskNV, {VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_nv_viewport_array2}}, |
| 1614 | {spv::CapabilitySubgroupBallotKHR, {VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_ballot }}, |
| 1615 | {spv::CapabilitySubgroupVoteKHR, {VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_ext_shader_subgroup_vote }}, |
aqnuep | 7033c70 | 2018-09-11 18:03:29 +0200 | [diff] [blame] | 1616 | {spv::CapabilityInt64Atomics, {VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, nullptr, &DeviceExtensions::vk_khr_shader_atomic_int64 }}, |
Alexander Galazin | 3bd8e34 | 2018-06-14 15:49:07 +0200 | [diff] [blame] | 1617 | |
Mike Schuchardt | 8ed5ea0 | 2018-07-20 18:24:17 -0600 | [diff] [blame] | 1618 | {spv::CapabilityStorageBuffer8BitAccess , {"VkPhysicalDevice8BitStorageFeaturesKHR::storageBuffer8BitAccess", &VkPhysicalDevice8BitStorageFeaturesKHR::storageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}}, |
| 1619 | {spv::CapabilityUniformAndStorageBuffer8BitAccess , {"VkPhysicalDevice8BitStorageFeaturesKHR::uniformAndStorageBuffer8BitAccess", &VkPhysicalDevice8BitStorageFeaturesKHR::uniformAndStorageBuffer8BitAccess, &DeviceExtensions::vk_khr_8bit_storage}}, |
| 1620 | {spv::CapabilityStoragePushConstant8 , {"VkPhysicalDevice8BitStorageFeaturesKHR::storagePushConstant8", &VkPhysicalDevice8BitStorageFeaturesKHR::storagePushConstant8, &DeviceExtensions::vk_khr_8bit_storage}}, |
Brett Lawson | bebfb6f | 2018-10-23 16:58:50 -0700 | [diff] [blame] | 1621 | |
| 1622 | {spv::CapabilityTransformFeedback , { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::transformFeedback, &DeviceExtensions::vk_ext_transform_feedback}}, |
Jose-Emilio Munoz-Lopez | 1109b45 | 2018-08-21 09:44:07 +0100 | [diff] [blame] | 1623 | {spv::CapabilityGeometryStreams , { "VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams", &VkPhysicalDeviceTransformFeedbackFeaturesEXT::geometryStreams, &DeviceExtensions::vk_ext_transform_feedback}}, |
| 1624 | |
| 1625 | {spv::CapabilityFloat16 , {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderFloat16", &VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderFloat16, &DeviceExtensions::vk_khr_shader_float16_int8}}, |
| 1626 | {spv::CapabilityInt8 , {"VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderInt8", &VkPhysicalDeviceFloat16Int8FeaturesKHR::shaderInt8, &DeviceExtensions::vk_khr_shader_float16_int8}}, |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1627 | |
| 1628 | {spv::CapabilityCooperativeMatrixNV, {"VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix", &VkPhysicalDeviceCooperativeMatrixFeaturesNV::cooperativeMatrix, &DeviceExtensions::vk_nv_cooperative_matrix}}, |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 1629 | |
| 1630 | {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat16, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1631 | {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat32, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1632 | {spv::CapabilitySignedZeroInfNanPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderSignedZeroInfNanPreserveFloat64, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1633 | {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat16, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1634 | {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat32, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1635 | {spv::CapabilityDenormPreserve, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormPreserveFloat64, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1636 | {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat16, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1637 | {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat32, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1638 | {spv::CapabilityDenormFlushToZero, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderDenormFlushToZeroFloat64, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1639 | {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat16, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1640 | {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat32, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1641 | {spv::CapabilityRoundingModeRTE, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTEFloat64, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1642 | {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat16", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat16, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1643 | {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat32", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat32, &DeviceExtensions::vk_khr_shader_float_controls}}, |
| 1644 | {spv::CapabilityRoundingModeRTZ, {"VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat64", &VkPhysicalDeviceFloatControlsPropertiesKHR::shaderRoundingModeRTZFloat64, &DeviceExtensions::vk_khr_shader_float_controls}}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1645 | }; |
| 1646 | // clang-format on |
| 1647 | |
| 1648 | for (auto insn : *src) { |
| 1649 | if (insn.opcode() == spv::OpCapability) { |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 1650 | size_t n = capabilities.count(insn.word(1)); |
| 1651 | if (1 == n) { // key occurs exactly once |
| 1652 | auto it = capabilities.find(insn.word(1)); |
| 1653 | if (it != capabilities.end()) { |
| 1654 | if (it->second.feature) { |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 1655 | skip |= RequireFeature(report_data, it->second.feature.IsEnabled(enabled_features), it->second.name); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 1656 | } |
| 1657 | if (it->second.extension) { |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 1658 | skip |= RequireExtension(report_data, device_extensions.*(it->second.extension), it->second.name); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 1659 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1660 | } |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 1661 | } else if (1 < n) { // key occurs multiple times, at least one must be enabled |
| 1662 | bool needs_feature = false, has_feature = false; |
| 1663 | bool needs_ext = false, has_ext = false; |
| 1664 | std::string feature_names = "(one of) [ "; |
| 1665 | std::string extension_names = feature_names; |
| 1666 | auto caps = capabilities.equal_range(insn.word(1)); |
| 1667 | for (auto it = caps.first; it != caps.second; ++it) { |
| 1668 | if (it->second.feature) { |
| 1669 | needs_feature = true; |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 1670 | has_feature = has_feature || it->second.feature.IsEnabled(enabled_features); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 1671 | feature_names += it->second.name; |
| 1672 | feature_names += " "; |
| 1673 | } |
| 1674 | if (it->second.extension) { |
| 1675 | needs_ext = true; |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 1676 | has_ext = has_ext || device_extensions.*(it->second.extension); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 1677 | extension_names += it->second.name; |
| 1678 | extension_names += " "; |
| 1679 | } |
| 1680 | } |
| 1681 | if (needs_feature) { |
| 1682 | feature_names += "]"; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1683 | skip |= RequireFeature(report_data, has_feature, feature_names.c_str()); |
Dave Houlton | eb10ea8 | 2017-12-22 12:21:50 -0700 | [diff] [blame] | 1684 | } |
| 1685 | if (needs_ext) { |
| 1686 | extension_names += "]"; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1687 | skip |= RequireExtension(report_data, has_ext, extension_names.c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 1693 | if (has_writable_descriptor) { |
| 1694 | switch (stage) { |
| 1695 | case VK_SHADER_STAGE_COMPUTE_BIT: |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 1696 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 1697 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 1698 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 1699 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 1700 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 1701 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 1702 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 1703 | case VK_SHADER_STAGE_MESH_BIT_NV: |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 1704 | /* No feature requirements for writes and atomics from compute |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 1705 | * raytracing, or mesh stages */ |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 1706 | break; |
| 1707 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 1708 | skip |= RequireFeature(report_data, enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 1709 | break; |
| 1710 | default: |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 1711 | skip |= RequireFeature(report_data, enabled_features.core.vertexPipelineStoresAndAtomics, |
| 1712 | "vertexPipelineStoresAndAtomics"); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 1713 | break; |
| 1714 | } |
| 1715 | } |
| 1716 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1717 | return skip; |
| 1718 | } |
| 1719 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1720 | static bool VariableIsBuiltIn(shader_module const *src, const uint32_t ID, std::vector<uint32_t> const &builtInBlockIDs, |
| 1721 | std::vector<uint32_t> const &builtInIDs) { |
| 1722 | auto insn = src->get_def(ID); |
| 1723 | |
| 1724 | switch (insn.opcode()) { |
| 1725 | case spv::OpVariable: { |
| 1726 | // First check if the variable is a "pure" built-in type, e.g. gl_ViewportIndex |
| 1727 | uint32_t ID = insn.word(2); |
| 1728 | for (auto builtInID : builtInIDs) { |
| 1729 | if (ID == builtInID) { |
| 1730 | return true; |
| 1731 | } |
| 1732 | } |
| 1733 | |
Ari Suonpaa | 89c6082 | 2019-03-25 14:13:02 +0200 | [diff] [blame] | 1734 | return VariableIsBuiltIn(src, insn.word(1), builtInBlockIDs, builtInIDs); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1735 | } |
| 1736 | case spv::OpTypePointer: |
Ari Suonpaa | 89c6082 | 2019-03-25 14:13:02 +0200 | [diff] [blame] | 1737 | return VariableIsBuiltIn(src, insn.word(3), builtInBlockIDs, builtInIDs); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1738 | case spv::OpTypeArray: |
Ari Suonpaa | 89c6082 | 2019-03-25 14:13:02 +0200 | [diff] [blame] | 1739 | return VariableIsBuiltIn(src, insn.word(2), builtInBlockIDs, builtInIDs); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1740 | case spv::OpTypeStruct: { |
| 1741 | uint32_t ID = insn.word(1); // We only need to check the first member as either all will be, or none will be built-in |
| 1742 | for (auto builtInBlockID : builtInBlockIDs) { |
| 1743 | if (ID == builtInBlockID) { |
| 1744 | return true; |
| 1745 | } |
| 1746 | } |
| 1747 | return false; |
| 1748 | } |
| 1749 | default: |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | return false; |
| 1754 | } |
| 1755 | |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 1756 | bool CoreChecks::ValidateShaderStageInputOutputLimits(shader_module const *src, VkPipelineShaderStageCreateInfo const *pStage, |
| 1757 | PIPELINE_STATE *pipeline) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1758 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS || |
| 1759 | pStage->stage == VK_SHADER_STAGE_ALL) { |
| 1760 | return false; |
| 1761 | } |
| 1762 | |
| 1763 | bool skip = false; |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 1764 | auto const &limits = phys_dev_props.limits; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1765 | |
| 1766 | std::vector<uint32_t> builtInBlockIDs; |
| 1767 | std::vector<uint32_t> builtInIDs; |
| 1768 | struct Variable { |
| 1769 | uint32_t baseTypePtrID; |
| 1770 | uint32_t ID; |
| 1771 | uint32_t storageClass; |
| 1772 | }; |
| 1773 | std::vector<Variable> variables; |
| 1774 | |
| 1775 | for (auto insn : *src) { |
| 1776 | switch (insn.opcode()) { |
| 1777 | // Find all built-in member decorations |
| 1778 | case spv::OpMemberDecorate: |
| 1779 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 1780 | builtInBlockIDs.push_back(insn.word(1)); |
| 1781 | } |
| 1782 | break; |
| 1783 | // Find all built-in decorations |
| 1784 | case spv::OpDecorate: |
| 1785 | switch (insn.word(2)) { |
| 1786 | case spv::DecorationBlock: { |
| 1787 | uint32_t blockID = insn.word(1); |
| 1788 | for (auto builtInBlockID : builtInBlockIDs) { |
| 1789 | // Check if one of the members of the block are built-in -> the block is built-in |
| 1790 | if (blockID == builtInBlockID) { |
| 1791 | builtInIDs.push_back(blockID); |
| 1792 | break; |
| 1793 | } |
| 1794 | } |
| 1795 | break; |
| 1796 | } |
| 1797 | case spv::DecorationBuiltIn: |
| 1798 | builtInIDs.push_back(insn.word(1)); |
| 1799 | break; |
| 1800 | default: |
| 1801 | break; |
| 1802 | } |
| 1803 | break; |
| 1804 | // Find all input and output variables |
| 1805 | case spv::OpVariable: { |
| 1806 | Variable var = {}; |
| 1807 | var.storageClass = insn.word(3); |
| 1808 | if (var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) { |
| 1809 | var.baseTypePtrID = insn.word(1); |
| 1810 | var.ID = insn.word(2); |
| 1811 | variables.push_back(var); |
| 1812 | } |
| 1813 | break; |
| 1814 | } |
| 1815 | default: |
| 1816 | break; |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | uint32_t numCompIn = 0, numCompOut = 0; |
| 1821 | for (auto &var : variables) { |
| 1822 | // Check the variable's ID |
| 1823 | if (VariableIsBuiltIn(src, var.ID, builtInBlockIDs, builtInIDs)) { |
| 1824 | continue; |
| 1825 | } |
| 1826 | // Check the variable's type's ID - e.g. gl_PerVertex is made of basic types, not built-in types |
| 1827 | if (VariableIsBuiltIn(src, src->get_def(var.baseTypePtrID).word(3), builtInBlockIDs, builtInIDs)) { |
| 1828 | continue; |
| 1829 | } |
| 1830 | |
| 1831 | if (var.storageClass == spv::StorageClassInput) { |
| 1832 | numCompIn += GetComponentsConsumedByType(src, var.baseTypePtrID, false); |
| 1833 | } else { // var.storageClass == spv::StorageClassOutput |
| 1834 | numCompOut += GetComponentsConsumedByType(src, var.baseTypePtrID, false); |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | switch (pStage->stage) { |
| 1839 | case VK_SHADER_STAGE_VERTEX_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1840 | if (numCompOut > limits.maxVertexOutputComponents) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1841 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
| 1842 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit, |
| 1843 | "Invalid Pipeline CreateInfo State: Vertex shader exceeds " |
| 1844 | "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u " |
| 1845 | "components by %u components", |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1846 | limits.maxVertexOutputComponents, numCompOut - limits.maxVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1847 | } |
| 1848 | break; |
| 1849 | |
| 1850 | case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1851 | if (numCompIn > limits.maxTessellationControlPerVertexInputComponents) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1852 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
| 1853 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit, |
| 1854 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 1855 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u " |
| 1856 | "components by %u components", |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1857 | limits.maxTessellationControlPerVertexInputComponents, |
| 1858 | numCompIn - limits.maxTessellationControlPerVertexInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1859 | } |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1860 | if (numCompOut > limits.maxTessellationControlPerVertexOutputComponents) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1861 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
| 1862 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit, |
| 1863 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 1864 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u " |
| 1865 | "components by %u components", |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1866 | limits.maxTessellationControlPerVertexOutputComponents, |
| 1867 | numCompOut - limits.maxTessellationControlPerVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1868 | } |
| 1869 | break; |
| 1870 | |
| 1871 | case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1872 | if (numCompIn > limits.maxTessellationEvaluationInputComponents) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1873 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
| 1874 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit, |
| 1875 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 1876 | "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u " |
| 1877 | "components by %u components", |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1878 | limits.maxTessellationEvaluationInputComponents, |
| 1879 | numCompIn - limits.maxTessellationEvaluationInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1880 | } |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1881 | if (numCompOut > limits.maxTessellationEvaluationOutputComponents) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1882 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
| 1883 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit, |
| 1884 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 1885 | "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u " |
| 1886 | "components by %u components", |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1887 | limits.maxTessellationEvaluationOutputComponents, |
| 1888 | numCompOut - limits.maxTessellationEvaluationOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1889 | } |
| 1890 | break; |
| 1891 | |
| 1892 | case VK_SHADER_STAGE_GEOMETRY_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1893 | if (numCompIn > limits.maxGeometryInputComponents) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1894 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
| 1895 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit, |
| 1896 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 1897 | "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u " |
| 1898 | "components by %u components", |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1899 | limits.maxGeometryInputComponents, numCompIn - limits.maxGeometryInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1900 | } |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1901 | if (numCompOut > limits.maxGeometryOutputComponents) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1902 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
| 1903 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit, |
| 1904 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 1905 | "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u " |
| 1906 | "components by %u components", |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1907 | limits.maxGeometryOutputComponents, numCompOut - limits.maxGeometryOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1908 | } |
| 1909 | break; |
| 1910 | |
| 1911 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1912 | if (numCompIn > limits.maxFragmentInputComponents) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1913 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
| 1914 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_ExceedDeviceLimit, |
| 1915 | "Invalid Pipeline CreateInfo State: Fragment shader exceeds " |
| 1916 | "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u " |
| 1917 | "components by %u components", |
Mark Lobodzinski | 57a4427 | 2019-02-27 12:40:50 -0700 | [diff] [blame] | 1918 | limits.maxFragmentInputComponents, numCompIn - limits.maxFragmentInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1919 | } |
| 1920 | break; |
| 1921 | |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 1922 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 1923 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 1924 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 1925 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 1926 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 1927 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 1928 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 1929 | case VK_SHADER_STAGE_MESH_BIT_NV: |
| 1930 | break; |
| 1931 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 1932 | default: |
| 1933 | assert(false); // This should never happen |
| 1934 | } |
| 1935 | return skip; |
| 1936 | } |
| 1937 | |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 1938 | // copy the specialization constant value into buf, if it is present |
| 1939 | void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) { |
| 1940 | VkSpecializationInfo const *spec = pStage->pSpecializationInfo; |
| 1941 | |
| 1942 | if (spec && spec_id < spec->mapEntryCount) { |
| 1943 | memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size); |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | // Fill in value with the constant or specialization constant value, if available. |
| 1948 | // Returns true if the value has been accurately filled out. |
| 1949 | static bool GetIntConstantValue(spirv_inst_iter insn, shader_module const *src, VkPipelineShaderStageCreateInfo const *pStage, |
| 1950 | const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) { |
| 1951 | auto type_id = src->get_def(insn.word(1)); |
| 1952 | if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) { |
| 1953 | return false; |
| 1954 | } |
| 1955 | switch (insn.opcode()) { |
| 1956 | case spv::OpSpecConstant: |
| 1957 | *value = insn.word(3); |
| 1958 | GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value); |
| 1959 | return true; |
| 1960 | case spv::OpConstant: |
| 1961 | *value = insn.word(3); |
| 1962 | return true; |
| 1963 | default: |
| 1964 | return false; |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | // Map SPIR-V type to VK_COMPONENT_TYPE enum |
| 1969 | VkComponentTypeNV GetComponentType(spirv_inst_iter insn, shader_module const *src) { |
| 1970 | switch (insn.opcode()) { |
| 1971 | case spv::OpTypeInt: |
| 1972 | switch (insn.word(2)) { |
| 1973 | case 8: |
| 1974 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV; |
| 1975 | case 16: |
| 1976 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV; |
| 1977 | case 32: |
| 1978 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV; |
| 1979 | case 64: |
| 1980 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV; |
| 1981 | default: |
| 1982 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 1983 | } |
| 1984 | case spv::OpTypeFloat: |
| 1985 | switch (insn.word(2)) { |
| 1986 | case 16: |
| 1987 | return VK_COMPONENT_TYPE_FLOAT16_NV; |
| 1988 | case 32: |
| 1989 | return VK_COMPONENT_TYPE_FLOAT32_NV; |
| 1990 | case 64: |
| 1991 | return VK_COMPONENT_TYPE_FLOAT64_NV; |
| 1992 | default: |
| 1993 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 1994 | } |
| 1995 | default: |
| 1996 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | // Validate SPV_NV_cooperative_matrix behavior that can't be statically validated |
| 2001 | // in SPIRV-Tools (e.g. due to specialization constant usage). |
| 2002 | bool CoreChecks::ValidateCooperativeMatrix(shader_module const *src, VkPipelineShaderStageCreateInfo const *pStage, |
| 2003 | PIPELINE_STATE *pipeline) { |
| 2004 | bool skip = false; |
| 2005 | |
| 2006 | // Map SPIR-V result ID to specialization constant id (SpecId decoration value) |
| 2007 | std::unordered_map<uint32_t, uint32_t> id_to_spec_id; |
| 2008 | // Map SPIR-V result ID to the ID of its type. |
| 2009 | std::unordered_map<uint32_t, uint32_t> id_to_type_id; |
| 2010 | |
| 2011 | struct CoopMatType { |
| 2012 | uint32_t scope, rows, cols; |
| 2013 | VkComponentTypeNV component_type; |
| 2014 | bool all_constant; |
| 2015 | |
| 2016 | CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {} |
| 2017 | |
| 2018 | void Init(uint32_t id, shader_module const *src, VkPipelineShaderStageCreateInfo const *pStage, |
| 2019 | const std::unordered_map<uint32_t, uint32_t> &id_to_spec_id) { |
| 2020 | spirv_inst_iter insn = src->get_def(id); |
| 2021 | uint32_t component_type_id = insn.word(2); |
| 2022 | uint32_t scope_id = insn.word(3); |
| 2023 | uint32_t rows_id = insn.word(4); |
| 2024 | uint32_t cols_id = insn.word(5); |
| 2025 | auto component_type_iter = src->get_def(component_type_id); |
| 2026 | auto scope_iter = src->get_def(scope_id); |
| 2027 | auto rows_iter = src->get_def(rows_id); |
| 2028 | auto cols_iter = src->get_def(cols_id); |
| 2029 | |
| 2030 | all_constant = true; |
| 2031 | if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) { |
| 2032 | all_constant = false; |
| 2033 | } |
| 2034 | if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) { |
| 2035 | all_constant = false; |
| 2036 | } |
| 2037 | if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) { |
| 2038 | all_constant = false; |
| 2039 | } |
| 2040 | component_type = GetComponentType(component_type_iter, src); |
| 2041 | } |
| 2042 | }; |
| 2043 | |
| 2044 | bool seen_coopmat_capability = false; |
| 2045 | |
| 2046 | for (auto insn : *src) { |
| 2047 | // Whitelist instructions whose result can be a cooperative matrix type, and |
| 2048 | // keep track of their types. It would be nice if SPIRV-Headers generated code |
| 2049 | // to identify which instructions have a result type and result id. Lacking that, |
| 2050 | // this whitelist is based on the set of instructions that |
| 2051 | // SPV_NV_cooperative_matrix says can be used with cooperative matrix types. |
| 2052 | switch (insn.opcode()) { |
| 2053 | case spv::OpLoad: |
| 2054 | case spv::OpCooperativeMatrixLoadNV: |
| 2055 | case spv::OpCooperativeMatrixMulAddNV: |
| 2056 | case spv::OpSNegate: |
| 2057 | case spv::OpFNegate: |
| 2058 | case spv::OpIAdd: |
| 2059 | case spv::OpFAdd: |
| 2060 | case spv::OpISub: |
| 2061 | case spv::OpFSub: |
| 2062 | case spv::OpFDiv: |
| 2063 | case spv::OpSDiv: |
| 2064 | case spv::OpUDiv: |
| 2065 | case spv::OpMatrixTimesScalar: |
| 2066 | case spv::OpConstantComposite: |
| 2067 | case spv::OpCompositeConstruct: |
| 2068 | case spv::OpConvertFToU: |
| 2069 | case spv::OpConvertFToS: |
| 2070 | case spv::OpConvertSToF: |
| 2071 | case spv::OpConvertUToF: |
| 2072 | case spv::OpUConvert: |
| 2073 | case spv::OpSConvert: |
| 2074 | case spv::OpFConvert: |
| 2075 | id_to_type_id[insn.word(2)] = insn.word(1); |
| 2076 | break; |
| 2077 | default: |
| 2078 | break; |
| 2079 | } |
| 2080 | |
| 2081 | switch (insn.opcode()) { |
| 2082 | case spv::OpDecorate: |
| 2083 | if (insn.word(2) == spv::DecorationSpecId) { |
| 2084 | id_to_spec_id[insn.word(1)] = insn.word(3); |
| 2085 | } |
| 2086 | break; |
| 2087 | case spv::OpCapability: |
| 2088 | if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) { |
| 2089 | seen_coopmat_capability = true; |
| 2090 | |
| 2091 | if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) { |
| 2092 | skip |= |
Mark Lobodzinski | 93a1fa7 | 2019-04-19 12:12:25 -0600 | [diff] [blame] | 2093 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2094 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixSupportedStages, |
| 2095 | "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)", |
| 2096 | phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages); |
| 2097 | } |
| 2098 | } |
| 2099 | break; |
| 2100 | case spv::OpMemoryModel: |
| 2101 | // If the capability isn't enabled, don't bother with the rest of this function. |
| 2102 | // OpMemoryModel is the first required instruction after all OpCapability instructions. |
| 2103 | if (!seen_coopmat_capability) { |
| 2104 | return skip; |
| 2105 | } |
| 2106 | break; |
| 2107 | case spv::OpTypeCooperativeMatrixNV: { |
| 2108 | CoopMatType M; |
| 2109 | M.Init(insn.word(1), src, pStage, id_to_spec_id); |
| 2110 | |
| 2111 | if (M.all_constant) { |
| 2112 | // Validate that the type parameters are all supported for one of the |
| 2113 | // operands of a cooperative matrix property. |
| 2114 | bool valid = false; |
| 2115 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
| 2116 | if (cooperative_matrix_properties[i].AType == M.component_type && |
| 2117 | cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].KSize == M.cols && |
| 2118 | cooperative_matrix_properties[i].scope == M.scope) { |
| 2119 | valid = true; |
| 2120 | break; |
| 2121 | } |
| 2122 | if (cooperative_matrix_properties[i].BType == M.component_type && |
| 2123 | cooperative_matrix_properties[i].KSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols && |
| 2124 | cooperative_matrix_properties[i].scope == M.scope) { |
| 2125 | valid = true; |
| 2126 | break; |
| 2127 | } |
| 2128 | if (cooperative_matrix_properties[i].CType == M.component_type && |
| 2129 | cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols && |
| 2130 | cooperative_matrix_properties[i].scope == M.scope) { |
| 2131 | valid = true; |
| 2132 | break; |
| 2133 | } |
| 2134 | if (cooperative_matrix_properties[i].DType == M.component_type && |
| 2135 | cooperative_matrix_properties[i].MSize == M.rows && cooperative_matrix_properties[i].NSize == M.cols && |
| 2136 | cooperative_matrix_properties[i].scope == M.scope) { |
| 2137 | valid = true; |
| 2138 | break; |
| 2139 | } |
| 2140 | } |
| 2141 | if (!valid) { |
Mark Lobodzinski | 93a1fa7 | 2019-04-19 12:12:25 -0600 | [diff] [blame] | 2142 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2143 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixType, |
| 2144 | "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type", |
| 2145 | insn.word(1)); |
| 2146 | } |
| 2147 | } |
| 2148 | break; |
| 2149 | } |
| 2150 | case spv::OpCooperativeMatrixMulAddNV: { |
| 2151 | CoopMatType A, B, C, D; |
| 2152 | if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() || |
| 2153 | id_to_type_id.find(insn.word(3)) == id_to_type_id.end() || |
| 2154 | id_to_type_id.find(insn.word(4)) == id_to_type_id.end() || |
| 2155 | id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) { |
| 2156 | assert(!"Couldn't find type of matrix"); |
| 2157 | break; |
| 2158 | } |
| 2159 | D.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id); |
| 2160 | A.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id); |
| 2161 | B.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id); |
| 2162 | C.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id); |
| 2163 | |
| 2164 | if (A.all_constant && B.all_constant && C.all_constant && D.all_constant) { |
| 2165 | // Validate that the type parameters are all supported for the same |
| 2166 | // cooperative matrix property. |
| 2167 | bool valid = false; |
| 2168 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
| 2169 | if (cooperative_matrix_properties[i].AType == A.component_type && |
| 2170 | cooperative_matrix_properties[i].MSize == A.rows && cooperative_matrix_properties[i].KSize == A.cols && |
| 2171 | cooperative_matrix_properties[i].scope == A.scope && |
| 2172 | |
| 2173 | cooperative_matrix_properties[i].BType == B.component_type && |
| 2174 | cooperative_matrix_properties[i].KSize == B.rows && cooperative_matrix_properties[i].NSize == B.cols && |
| 2175 | cooperative_matrix_properties[i].scope == B.scope && |
| 2176 | |
| 2177 | cooperative_matrix_properties[i].CType == C.component_type && |
| 2178 | cooperative_matrix_properties[i].MSize == C.rows && cooperative_matrix_properties[i].NSize == C.cols && |
| 2179 | cooperative_matrix_properties[i].scope == C.scope && |
| 2180 | |
| 2181 | cooperative_matrix_properties[i].DType == D.component_type && |
| 2182 | cooperative_matrix_properties[i].MSize == D.rows && cooperative_matrix_properties[i].NSize == D.cols && |
| 2183 | cooperative_matrix_properties[i].scope == D.scope) { |
| 2184 | valid = true; |
| 2185 | break; |
| 2186 | } |
| 2187 | } |
| 2188 | if (!valid) { |
Mark Lobodzinski | 93a1fa7 | 2019-04-19 12:12:25 -0600 | [diff] [blame] | 2189 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2190 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_CooperativeMatrixMulAdd, |
| 2191 | "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix " |
| 2192 | "VkCooperativeMatrixPropertiesNV", |
| 2193 | insn.word(2)); |
| 2194 | } |
| 2195 | } |
| 2196 | break; |
| 2197 | } |
| 2198 | default: |
| 2199 | break; |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | return skip; |
| 2204 | } |
| 2205 | |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2206 | bool CoreChecks::ValidateExecutionModes(shader_module const *src, spirv_inst_iter entrypoint) { |
| 2207 | auto entrypoint_id = entrypoint.word(2); |
| 2208 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2209 | // The first denorm execution mode encountered, along with its bit width. |
| 2210 | // Used to check if SeparateDenormSettings is respected. |
| 2211 | 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] | 2212 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2213 | // The first rounding mode encountered, along with its bit width. |
| 2214 | // Used to check if SeparateRoundingModeSettings is respected. |
| 2215 | 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] | 2216 | |
| 2217 | bool skip = false; |
| 2218 | |
| 2219 | for (auto insn : *src) { |
| 2220 | if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) { |
| 2221 | auto mode = insn.word(2); |
| 2222 | switch (mode) { |
| 2223 | case spv::ExecutionModeSignedZeroInfNanPreserve: { |
| 2224 | auto bit_width = insn.word(3); |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2225 | if ((bit_width == 16 && !enabled_features.float_controls.shaderSignedZeroInfNanPreserveFloat16) || |
| 2226 | (bit_width == 32 && !enabled_features.float_controls.shaderSignedZeroInfNanPreserveFloat32) || |
| 2227 | (bit_width == 64 && !enabled_features.float_controls.shaderSignedZeroInfNanPreserveFloat64)) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2228 | skip |= |
| 2229 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2230 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2231 | "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device", |
| 2232 | bit_width); |
| 2233 | } |
| 2234 | break; |
| 2235 | } |
| 2236 | |
| 2237 | case spv::ExecutionModeDenormPreserve: { |
| 2238 | auto bit_width = insn.word(3); |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2239 | if ((bit_width == 16 && !enabled_features.float_controls.shaderDenormPreserveFloat16) || |
| 2240 | (bit_width == 32 && !enabled_features.float_controls.shaderDenormPreserveFloat32) || |
| 2241 | (bit_width == 64 && !enabled_features.float_controls.shaderDenormPreserveFloat64)) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2242 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2243 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2244 | "Shader requires DenormPreserve for bit width %d but it is not enabled on the device", |
| 2245 | bit_width); |
| 2246 | } |
| 2247 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2248 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 2249 | // Register the first denorm execution mode found |
| 2250 | first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
| 2251 | } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width && |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2252 | !enabled_features.float_controls.separateDenormSettings) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2253 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2254 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2255 | "Shader uses separate denorm execution modes for different bit widths but " |
| 2256 | "SeparateDenormSettings is not enabled on the device"); |
| 2257 | } |
| 2258 | break; |
| 2259 | } |
| 2260 | |
| 2261 | case spv::ExecutionModeDenormFlushToZero: { |
| 2262 | auto bit_width = insn.word(3); |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2263 | if ((bit_width == 16 && !enabled_features.float_controls.shaderDenormFlushToZeroFloat16) || |
| 2264 | (bit_width == 32 && !enabled_features.float_controls.shaderDenormFlushToZeroFloat32) || |
| 2265 | (bit_width == 64 && !enabled_features.float_controls.shaderDenormFlushToZeroFloat64)) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2266 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2267 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2268 | "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device", |
| 2269 | bit_width); |
| 2270 | } |
| 2271 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2272 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 2273 | // Register the first denorm execution mode found |
| 2274 | first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
| 2275 | } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width && |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2276 | !enabled_features.float_controls.separateDenormSettings) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2277 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2278 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2279 | "Shader uses separate denorm execution modes for different bit widths but " |
| 2280 | "SeparateDenormSettings is not enabled on the device"); |
| 2281 | } |
| 2282 | break; |
| 2283 | } |
| 2284 | |
| 2285 | case spv::ExecutionModeRoundingModeRTE: { |
| 2286 | auto bit_width = insn.word(3); |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2287 | if ((bit_width == 16 && !enabled_features.float_controls.shaderRoundingModeRTEFloat16) || |
| 2288 | (bit_width == 32 && !enabled_features.float_controls.shaderRoundingModeRTEFloat32) || |
| 2289 | (bit_width == 64 && !enabled_features.float_controls.shaderRoundingModeRTEFloat64)) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2290 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2291 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2292 | "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device", |
| 2293 | bit_width); |
| 2294 | } |
| 2295 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2296 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 2297 | // Register the first rounding mode found |
| 2298 | first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
| 2299 | } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width && |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2300 | !enabled_features.float_controls.separateRoundingModeSettings) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2301 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2302 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2303 | "Shader uses separate rounding modes for different bit widths but " |
| 2304 | "SeparateRoundingModeSettings is not enabled on the device"); |
| 2305 | } |
| 2306 | break; |
| 2307 | } |
| 2308 | |
| 2309 | case spv::ExecutionModeRoundingModeRTZ: { |
| 2310 | auto bit_width = insn.word(3); |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2311 | if ((bit_width == 16 && !enabled_features.float_controls.shaderRoundingModeRTZFloat16) || |
| 2312 | (bit_width == 32 && !enabled_features.float_controls.shaderRoundingModeRTZFloat32) || |
| 2313 | (bit_width == 64 && !enabled_features.float_controls.shaderRoundingModeRTZFloat64)) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2314 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2315 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2316 | "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device", |
| 2317 | bit_width); |
| 2318 | } |
| 2319 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2320 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 2321 | // Register the first rounding mode found |
| 2322 | first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
| 2323 | } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width && |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2324 | !enabled_features.float_controls.separateRoundingModeSettings) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2325 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
| 2326 | kVUID_Core_Shader_FeatureNotEnabled, |
| 2327 | "Shader uses separate rounding modes for different bit widths but " |
| 2328 | "SeparateRoundingModeSettings is not enabled on the device"); |
| 2329 | } |
| 2330 | break; |
| 2331 | } |
| 2332 | } |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | return skip; |
| 2337 | } |
| 2338 | |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2339 | static uint32_t DescriptorTypeToReqs(shader_module const *module, uint32_t type_id) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2340 | auto type = module->get_def(type_id); |
| 2341 | |
| 2342 | while (true) { |
| 2343 | switch (type.opcode()) { |
| 2344 | case spv::OpTypeArray: |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 2345 | case spv::OpTypeRuntimeArray: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2346 | case spv::OpTypeSampledImage: |
| 2347 | type = module->get_def(type.word(2)); |
| 2348 | break; |
| 2349 | case spv::OpTypePointer: |
| 2350 | type = module->get_def(type.word(3)); |
| 2351 | break; |
| 2352 | case spv::OpTypeImage: { |
| 2353 | auto dim = type.word(3); |
| 2354 | auto arrayed = type.word(5); |
| 2355 | auto msaa = type.word(6); |
| 2356 | |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 2357 | uint32_t bits = 0; |
| 2358 | switch (GetFundamentalType(module, type.word(2))) { |
| 2359 | case FORMAT_TYPE_FLOAT: |
| 2360 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT; |
| 2361 | break; |
| 2362 | case FORMAT_TYPE_UINT: |
| 2363 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT; |
| 2364 | break; |
| 2365 | case FORMAT_TYPE_SINT: |
| 2366 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT; |
| 2367 | break; |
| 2368 | default: |
| 2369 | break; |
| 2370 | } |
| 2371 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2372 | switch (dim) { |
| 2373 | case spv::Dim1D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 2374 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D; |
| 2375 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2376 | case spv::Dim2D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 2377 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 2378 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D; |
| 2379 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2380 | case spv::Dim3D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 2381 | bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D; |
| 2382 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2383 | case spv::DimCube: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 2384 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE; |
| 2385 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2386 | case spv::DimSubpassData: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 2387 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 2388 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2389 | default: // buffer, etc. |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 2390 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | default: |
| 2394 | return 0; |
| 2395 | } |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | // For given pipelineLayout verify that the set_layout_node at slot.first |
| 2400 | // has the requested binding at slot.second and return ptr to that binding |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2401 | static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_NODE const *pipelineLayout, |
| 2402 | descriptor_slot_t slot) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2403 | if (!pipelineLayout) return nullptr; |
| 2404 | |
| 2405 | if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr; |
| 2406 | |
| 2407 | return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second); |
| 2408 | } |
| 2409 | |
Locke | 1ec6d95 | 2019-04-02 11:57:21 -0600 | [diff] [blame] | 2410 | static bool FindLocalSize(shader_module const *src, uint32_t &local_size_x, uint32_t &local_size_y, uint32_t &local_size_z) { |
| 2411 | for (auto insn : *src) { |
| 2412 | if (insn.opcode() == spv::OpEntryPoint) { |
| 2413 | auto executionModel = insn.word(1); |
| 2414 | auto entrypointStageBits = ExecutionModelToShaderStageFlagBits(executionModel); |
| 2415 | if (entrypointStageBits == VK_SHADER_STAGE_COMPUTE_BIT) { |
| 2416 | auto entrypoint_id = insn.word(2); |
| 2417 | for (auto insn1 : *src) { |
| 2418 | if (insn1.opcode() == spv::OpExecutionMode && insn1.word(1) == entrypoint_id && |
| 2419 | insn1.word(2) == spv::ExecutionModeLocalSize) { |
| 2420 | local_size_x = insn1.word(3); |
| 2421 | local_size_y = insn1.word(4); |
| 2422 | local_size_z = insn1.word(5); |
| 2423 | return true; |
| 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | return false; |
| 2430 | } |
| 2431 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2432 | static void ProcessExecutionModes(shader_module const *src, spirv_inst_iter entrypoint, PIPELINE_STATE *pipeline) { |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 2433 | auto entrypoint_id = entrypoint.word(2); |
Chris Forbes | 0771b67 | 2018-03-22 21:13:46 -0700 | [diff] [blame] | 2434 | bool is_point_mode = false; |
| 2435 | |
| 2436 | for (auto insn : *src) { |
| 2437 | if (insn.opcode() == spv::OpExecutionMode && insn.word(1) == entrypoint_id) { |
| 2438 | switch (insn.word(2)) { |
| 2439 | case spv::ExecutionModePointMode: |
| 2440 | // In tessellation shaders, PointMode is separate and trumps the tessellation topology. |
| 2441 | is_point_mode = true; |
| 2442 | break; |
| 2443 | |
| 2444 | case spv::ExecutionModeOutputPoints: |
| 2445 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 2446 | break; |
| 2447 | |
| 2448 | case spv::ExecutionModeIsolines: |
| 2449 | case spv::ExecutionModeOutputLineStrip: |
| 2450 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; |
| 2451 | break; |
| 2452 | |
| 2453 | case spv::ExecutionModeTriangles: |
| 2454 | case spv::ExecutionModeQuads: |
| 2455 | case spv::ExecutionModeOutputTriangleStrip: |
| 2456 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
| 2457 | break; |
| 2458 | } |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | if (is_point_mode) pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 2463 | } |
| 2464 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2465 | // If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize |
| 2466 | // o If there is only a vertex shader : gl_PointSize must be written when using points |
| 2467 | // o If there is a geometry or tessellation shader: |
| 2468 | // - If shaderTessellationAndGeometryPointSize feature is enabled: |
| 2469 | // * gl_PointSize must be written in the final geometry stage |
| 2470 | // - If shaderTessellationAndGeometryPointSize feature is disabled: |
| 2471 | // * gl_PointSize must NOT be written and a default of 1.0 is assumed |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2472 | bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, shader_module const *src, spirv_inst_iter entrypoint, |
| 2473 | VkShaderStageFlagBits stage) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2474 | if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 2475 | return false; |
| 2476 | } |
| 2477 | |
| 2478 | bool pointsize_written = false; |
| 2479 | bool skip = false; |
| 2480 | |
| 2481 | // Search for PointSize built-in decorations |
| 2482 | std::vector<uint32_t> pointsize_builtin_offsets; |
| 2483 | spirv_inst_iter insn = entrypoint; |
| 2484 | while (!pointsize_written && (insn.opcode() != spv::OpFunction)) { |
| 2485 | if (insn.opcode() == spv::OpMemberDecorate) { |
| 2486 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 2487 | if (insn.word(4) == spv::BuiltInPointSize) { |
| 2488 | pointsize_written = IsPointSizeWritten(src, insn, entrypoint); |
| 2489 | } |
| 2490 | } |
| 2491 | } else if (insn.opcode() == spv::OpDecorate) { |
| 2492 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 2493 | if (insn.word(3) == spv::BuiltInPointSize) { |
| 2494 | pointsize_written = IsPointSizeWritten(src, insn, entrypoint); |
| 2495 | } |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | insn++; |
| 2500 | } |
| 2501 | |
| 2502 | 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] | 2503 | !enabled_features.core.shaderTessellationAndGeometryPointSize) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2504 | if (pointsize_written) { |
Mark Lobodzinski | 93a1fa7 | 2019-04-19 12:12:25 -0600 | [diff] [blame] | 2505 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2506 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_PointSizeBuiltInOverSpecified, |
| 2507 | "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which " |
| 2508 | "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled."); |
| 2509 | } |
| 2510 | } else if (!pointsize_written) { |
| 2511 | skip |= |
Mark Lobodzinski | 93a1fa7 | 2019-04-19 12:12:25 -0600 | [diff] [blame] | 2512 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_PIPELINE_EXT, |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2513 | HandleToUint64(pipeline->pipeline), kVUID_Core_Shader_MissingPointSizeBuiltIn, |
| 2514 | "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.", |
| 2515 | string_VkShaderStageFlagBits(stage)); |
| 2516 | } |
| 2517 | return skip; |
| 2518 | } |
| 2519 | |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2520 | bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, PIPELINE_STATE *pipeline, |
| 2521 | shader_module const **out_module, spirv_inst_iter *out_entrypoint, |
| 2522 | bool check_point_size) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2523 | bool skip = false; |
Mark Lobodzinski | 9e9da29 | 2019-03-06 16:19:55 -0700 | [diff] [blame] | 2524 | auto module = *out_module = GetShaderModuleState(pStage->module); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2525 | |
| 2526 | if (!module->has_valid_spirv) return false; |
| 2527 | |
| 2528 | // Find the entrypoint |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2529 | auto entrypoint = *out_entrypoint = FindEntrypoint(module, pStage->pName, pStage->stage); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2530 | if (entrypoint == module->end()) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 2531 | if (log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 2532 | "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s..", |
| 2533 | pStage->pName, string_VkShaderStageFlagBits(pStage->stage))) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2534 | return true; // no point continuing beyond here, any analysis is just going to be garbage. |
| 2535 | } |
| 2536 | } |
| 2537 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2538 | // Mark accessible ids |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2539 | auto accessible_ids = MarkAccessibleIds(module, entrypoint); |
| 2540 | ProcessExecutionModes(module, entrypoint, pipeline); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2541 | |
| 2542 | // Validate descriptor set layout against what the entrypoint actually uses |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 2543 | bool has_writable_descriptor = false; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2544 | auto descriptor_uses = CollectInterfaceByDescriptorSlot(report_data, module, accessible_ids, &has_writable_descriptor); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2545 | |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2546 | // Validate shader capabilities against enabled device features |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2547 | skip |= ValidateShaderCapabilities(module, pStage->stage, has_writable_descriptor); |
| 2548 | skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2549 | skip |= ValidateExecutionModes(module, entrypoint); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2550 | skip |= ValidateSpecializationOffsets(report_data, pStage); |
| 2551 | skip |= ValidatePushConstantUsage(report_data, pipeline->pipeline_layout.push_constant_ranges.get(), module, accessible_ids, |
| 2552 | pStage->stage); |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2553 | if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) { |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2554 | skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2555 | } |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2556 | skip |= ValidateCooperativeMatrix(module, pStage, pipeline); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2557 | |
| 2558 | // Validate descriptor use |
| 2559 | for (auto use : descriptor_uses) { |
| 2560 | // While validating shaders capture which slots are used by the pipeline |
| 2561 | auto &reqs = pipeline->active_slots[use.first.first][use.first.second]; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2562 | reqs = descriptor_req(reqs | DescriptorTypeToReqs(module, use.second.type_id)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2563 | |
| 2564 | // Verify given pipelineLayout has requested setLayout with requested binding |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2565 | const auto &binding = GetDescriptorBinding(&pipeline->pipeline_layout, use.first); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2566 | unsigned required_descriptor_count; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2567 | std::set<uint32_t> descriptor_types = TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2568 | |
| 2569 | if (!binding) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 2570 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2571 | kVUID_Core_Shader_MissingDescriptor, |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2572 | "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout", |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2573 | use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2574 | } else if (~binding->stageFlags & pStage->stage) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 2575 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2576 | kVUID_Core_Shader_DescriptorNotAccessibleFromStage, |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2577 | "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first, |
| 2578 | use.first.second, string_VkShaderStageFlagBits(pStage->stage)); |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2579 | } else if (descriptor_types.find(binding->descriptorType) == descriptor_types.end()) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 2580 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2581 | kVUID_Core_Shader_DescriptorTypeMismatch, |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2582 | "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first, |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2583 | use.first.second, string_descriptorTypes(descriptor_types).c_str(), |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2584 | string_VkDescriptorType(binding->descriptorType)); |
| 2585 | } else if (binding->descriptorCount < required_descriptor_count) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 2586 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2587 | kVUID_Core_Shader_DescriptorTypeMismatch, |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2588 | "Shader expects at least %u descriptors for binding %u.%u but only %u provided", |
| 2589 | required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | // Validate use of input attachments against subpass structure |
| 2594 | if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2595 | auto input_attachment_uses = CollectInterfaceByInputAttachmentIndex(module, accessible_ids); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2596 | |
Petr Kraus | e91f7a1 | 2017-12-14 20:57:36 +0100 | [diff] [blame] | 2597 | auto rpci = pipeline->rp_state->createInfo.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2598 | auto subpass = pipeline->graphicsPipelineCI.subpass; |
| 2599 | |
| 2600 | for (auto use : input_attachment_uses) { |
| 2601 | auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments; |
| 2602 | auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount) |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2603 | ? input_attachments[use.first].attachment |
| 2604 | : VK_ATTACHMENT_UNUSED; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2605 | |
| 2606 | if (index == VK_ATTACHMENT_UNUSED) { |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 2607 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2608 | kVUID_Core_Shader_MissingInputAttachment, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2609 | "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] | 2610 | } 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] | 2611 | skip |= |
Mark Lobodzinski | b1fd9d1 | 2018-03-30 14:26:00 -0600 | [diff] [blame] | 2612 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2613 | kVUID_Core_Shader_InputAttachmentTypeMismatch, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2614 | "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2615 | 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] | 2616 | } |
| 2617 | } |
| 2618 | } |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame^] | 2619 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) { |
| 2620 | skip |= ValidateComputeWorkGroupSizes(module); |
| 2621 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2622 | return skip; |
| 2623 | } |
| 2624 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2625 | static bool ValidateInterfaceBetweenStages(debug_report_data const *report_data, shader_module const *producer, |
| 2626 | spirv_inst_iter producer_entrypoint, shader_stage_attributes const *producer_stage, |
| 2627 | shader_module const *consumer, spirv_inst_iter consumer_entrypoint, |
| 2628 | shader_stage_attributes const *consumer_stage) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2629 | bool skip = false; |
| 2630 | |
| 2631 | auto outputs = |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2632 | CollectInterfaceByLocation(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output); |
| 2633 | auto inputs = CollectInterfaceByLocation(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2634 | |
| 2635 | auto a_it = outputs.begin(); |
| 2636 | auto b_it = inputs.begin(); |
| 2637 | |
| 2638 | // Maps sorted by key (location); walk them together to find mismatches |
| 2639 | while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) { |
| 2640 | bool a_at_end = outputs.size() == 0 || a_it == outputs.end(); |
| 2641 | bool b_at_end = inputs.size() == 0 || b_it == inputs.end(); |
| 2642 | auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first; |
| 2643 | auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first; |
| 2644 | |
| 2645 | if (b_at_end || ((!a_at_end) && (a_first < b_first))) { |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 2646 | skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2647 | HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_OutputNotConsumed, |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 2648 | "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, a_first.first, |
| 2649 | a_first.second, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2650 | a_it++; |
| 2651 | } else if (a_at_end || a_first > b_first) { |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 2652 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2653 | HandleToUint64(consumer->vk_shader_module), kVUID_Core_Shader_InputNotProduced, |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 2654 | "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first, |
| 2655 | b_first.second, producer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2656 | b_it++; |
| 2657 | } else { |
| 2658 | // subtleties of arrayed interfaces: |
| 2659 | // - if is_patch, then the member is not arrayed, even though the interface may be. |
| 2660 | // - if is_block_member, then the extra array level of an arrayed interface is not |
| 2661 | // expressed in the member type -- it's expressed in the block type. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2662 | if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id, |
| 2663 | producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member, |
| 2664 | consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) { |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 2665 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2666 | HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch, |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 2667 | "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2668 | DescribeType(producer, a_it->second.type_id).c_str(), |
| 2669 | DescribeType(consumer, b_it->second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2670 | } |
| 2671 | if (a_it->second.is_patch != b_it->second.is_patch) { |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 2672 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2673 | HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch, |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2674 | "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage", |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2675 | a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name, |
| 2676 | b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name); |
| 2677 | } |
| 2678 | if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) { |
Mark Young | 4e919b2 | 2018-05-21 15:53:59 -0600 | [diff] [blame] | 2679 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 2680 | HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2681 | "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first, |
| 2682 | a_first.second, producer_stage->name, consumer_stage->name); |
| 2683 | } |
| 2684 | a_it++; |
| 2685 | b_it++; |
| 2686 | } |
| 2687 | } |
| 2688 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 2689 | if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 2690 | auto builtins_producer = CollectBuiltinBlockMembers(producer, producer_entrypoint, spv::StorageClassOutput); |
| 2691 | auto builtins_consumer = CollectBuiltinBlockMembers(consumer, consumer_entrypoint, spv::StorageClassInput); |
| 2692 | |
| 2693 | if (!builtins_producer.empty() && !builtins_consumer.empty()) { |
| 2694 | if (builtins_producer.size() != builtins_consumer.size()) { |
| 2695 | skip |= |
| 2696 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 2697 | HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch, |
| 2698 | "Number of elements inside builtin block differ between stages (%s %d vs %s %d).", producer_stage->name, |
| 2699 | (int)builtins_producer.size(), consumer_stage->name, (int)builtins_consumer.size()); |
| 2700 | } else { |
| 2701 | auto it_producer = builtins_producer.begin(); |
| 2702 | auto it_consumer = builtins_consumer.begin(); |
| 2703 | while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) { |
| 2704 | if (*it_producer != *it_consumer) { |
| 2705 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 2706 | HandleToUint64(producer->vk_shader_module), kVUID_Core_Shader_InterfaceTypeMismatch, |
| 2707 | "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name, |
| 2708 | consumer_stage->name); |
| 2709 | break; |
| 2710 | } |
| 2711 | it_producer++; |
| 2712 | it_consumer++; |
| 2713 | } |
| 2714 | } |
| 2715 | } |
| 2716 | } |
| 2717 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2718 | return skip; |
| 2719 | } |
| 2720 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2721 | static inline uint32_t DetermineFinalGeomStage(PIPELINE_STATE *pipeline, VkGraphicsPipelineCreateInfo *pCreateInfo) { |
| 2722 | uint32_t stage_mask = 0; |
| 2723 | if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 2724 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
| 2725 | stage_mask |= pCreateInfo->pStages[i].stage; |
| 2726 | } |
| 2727 | // 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] | 2728 | if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) { |
| 2729 | stage_mask = VK_SHADER_STAGE_MESH_BIT_NV; |
| 2730 | } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2731 | stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT; |
| 2732 | } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) { |
| 2733 | stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 2734 | } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) { |
| 2735 | stage_mask = VK_SHADER_STAGE_VERTEX_BIT; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 2736 | } |
| 2737 | } |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2738 | return stage_mask; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 2739 | } |
| 2740 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2741 | // Validate that the shaders used by the given pipeline and store the active_slots |
| 2742 | // that are actually used by the pipeline into pPipeline->active_slots |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2743 | bool CoreChecks::ValidateAndCapturePipelineShaderState(PIPELINE_STATE *pipeline) { |
Chris Forbes | a400a8a | 2017-07-20 13:10:24 -0700 | [diff] [blame] | 2744 | auto pCreateInfo = pipeline->graphicsPipelineCI.ptr(); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2745 | int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 2746 | int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2747 | |
Jeff Bolz | 7e35c39 | 2018-09-04 15:30:41 -0500 | [diff] [blame] | 2748 | shader_module const *shaders[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2749 | memset(shaders, 0, sizeof(shaders)); |
Jeff Bolz | 7e35c39 | 2018-09-04 15:30:41 -0500 | [diff] [blame] | 2750 | spirv_inst_iter entrypoints[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2751 | memset(entrypoints, 0, sizeof(entrypoints)); |
| 2752 | bool skip = false; |
| 2753 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2754 | uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, pCreateInfo); |
| 2755 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2756 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
| 2757 | auto pStage = &pCreateInfo->pStages[i]; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2758 | auto stage_id = GetShaderStageId(pStage->stage); |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2759 | skip |= ValidatePipelineShaderStage(pStage, pipeline, &shaders[stage_id], &entrypoints[stage_id], |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 2760 | (pointlist_stage_mask == pStage->stage)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | // if the shader stages are no good individually, cross-stage validation is pointless. |
| 2764 | if (skip) return true; |
| 2765 | |
| 2766 | auto vi = pCreateInfo->pVertexInputState; |
| 2767 | |
| 2768 | if (vi) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2769 | skip |= ValidateViConsistency(report_data, vi); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2773 | skip |= ValidateViAgainstVsInputs(report_data, vi, shaders[vertex_stage], entrypoints[vertex_stage]); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2776 | int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 2777 | int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2778 | |
| 2779 | while (!shaders[producer] && producer != fragment_stage) { |
| 2780 | producer++; |
| 2781 | consumer++; |
| 2782 | } |
| 2783 | |
| 2784 | for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) { |
| 2785 | assert(shaders[producer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 2786 | if (shaders[consumer]) { |
| 2787 | if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2788 | skip |= ValidateInterfaceBetweenStages(report_data, shaders[producer], entrypoints[producer], |
| 2789 | &shader_stage_attribs[producer], shaders[consumer], entrypoints[consumer], |
| 2790 | &shader_stage_attribs[consumer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 2791 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2792 | |
| 2793 | producer = consumer; |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2798 | skip |= ValidateFsOutputsAgainstRenderPass(report_data, shaders[fragment_stage], entrypoints[fragment_stage], pipeline, |
| 2799 | pCreateInfo->subpass); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | return skip; |
| 2803 | } |
| 2804 | |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2805 | bool CoreChecks::ValidateComputePipeline(PIPELINE_STATE *pipeline) { |
Chris Forbes | a400a8a | 2017-07-20 13:10:24 -0700 | [diff] [blame] | 2806 | auto pCreateInfo = pipeline->computePipelineCI.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2807 | |
| 2808 | shader_module const *module; |
| 2809 | spirv_inst_iter entrypoint; |
| 2810 | |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2811 | return ValidatePipelineShaderStage(&pCreateInfo->stage, pipeline, &module, &entrypoint, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2812 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2813 | |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2814 | bool CoreChecks::ValidateRayTracingPipelineNV(PIPELINE_STATE *pipeline) { |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 2815 | auto pCreateInfo = pipeline->raytracingPipelineCI.ptr(); |
| 2816 | |
| 2817 | shader_module const *module; |
| 2818 | spirv_inst_iter entrypoint; |
| 2819 | |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2820 | return ValidatePipelineShaderStage(pCreateInfo->pStages, pipeline, &module, &entrypoint, false); |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 2821 | } |
| 2822 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2823 | 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] | 2824 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2825 | static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) { |
John Zulauf | 25ea243 | 2019-04-05 10:07:38 -0600 | [diff] [blame] | 2826 | const auto validation_cache_ci = lvl_find_in_chain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext); |
| 2827 | if (validation_cache_ci) { |
John Zulauf | 146ee80 | 2019-04-05 15:31:06 -0600 | [diff] [blame] | 2828 | return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2829 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2830 | return nullptr; |
| 2831 | } |
| 2832 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 2833 | bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
| 2834 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2835 | bool skip = false; |
| 2836 | spv_result_t spv_valid = SPV_SUCCESS; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2837 | |
Mark Lobodzinski | b02a485 | 2019-04-19 12:35:30 -0600 | [diff] [blame] | 2838 | if (disabled.shader_validation) { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2839 | return false; |
| 2840 | } |
| 2841 | |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 2842 | auto have_glsl_shader = device_extensions.vk_nv_glsl_shader; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2843 | |
| 2844 | if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) { |
Mark Lobodzinski | 7767ad8 | 2019-03-09 13:35:25 -0700 | [diff] [blame] | 2845 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 2846 | "VUID-VkShaderModuleCreateInfo-pCode-01376", |
| 2847 | "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".", |
| 2848 | pCreateInfo->codeSize); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2849 | } else { |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2850 | auto cache = GetValidationCacheInfo(pCreateInfo); |
| 2851 | uint32_t hash = 0; |
| 2852 | if (cache) { |
| 2853 | hash = ValidationCache::MakeShaderHash(pCreateInfo); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2854 | if (cache->Contains(hash)) return false; |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2857 | // Use SPIRV-Tools validator to try and catch any issues with the module itself |
Dave Houlton | 0ea2d01 | 2018-06-21 14:00:26 -0600 | [diff] [blame] | 2858 | spv_target_env spirv_environment = SPV_ENV_VULKAN_1_0; |
Mark Lobodzinski | 544def7 | 2019-04-19 14:25:59 -0600 | [diff] [blame] | 2859 | if (api_version >= VK_API_VERSION_1_1) { |
Dave Houlton | 0ea2d01 | 2018-06-21 14:00:26 -0600 | [diff] [blame] | 2860 | spirv_environment = SPV_ENV_VULKAN_1_1; |
| 2861 | } |
| 2862 | spv_context ctx = spvContextCreate(spirv_environment); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 2863 | spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)}; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2864 | spv_diagnostic diag = nullptr; |
Karl Schultz | fda1b38 | 2018-08-08 18:56:11 -0600 | [diff] [blame] | 2865 | spv_validator_options options = spvValidatorOptionsCreate(); |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 2866 | if (device_extensions.vk_khr_relaxed_block_layout) { |
Karl Schultz | fda1b38 | 2018-08-08 18:56:11 -0600 | [diff] [blame] | 2867 | spvValidatorOptionsSetRelaxBlockLayout(options, true); |
| 2868 | } |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 2869 | if (device_extensions.vk_ext_scalar_block_layout && |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 2870 | enabled_features.scalar_block_layout_features.scalarBlockLayout == VK_TRUE) { |
Tobias Hector | 6a0ece7 | 2018-12-10 12:24:05 +0000 | [diff] [blame] | 2871 | spvValidatorOptionsSetScalarBlockLayout(options, true); |
| 2872 | } |
Karl Schultz | fda1b38 | 2018-08-08 18:56:11 -0600 | [diff] [blame] | 2873 | spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2874 | if (spv_valid != SPV_SUCCESS) { |
| 2875 | if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) { |
Mark Lobodzinski | 7767ad8 | 2019-03-09 13:35:25 -0700 | [diff] [blame] | 2876 | skip |= |
| 2877 | log_msg(report_data, spv_valid == SPV_WARNING ? VK_DEBUG_REPORT_WARNING_BIT_EXT : VK_DEBUG_REPORT_ERROR_BIT_EXT, |
| 2878 | VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, kVUID_Core_Shader_InconsistentSpirv, |
| 2879 | "SPIR-V module not valid: %s", diag && diag->error ? diag->error : "(no error text)"); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2880 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 2881 | } else { |
| 2882 | if (cache) { |
| 2883 | cache->Insert(hash); |
| 2884 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2885 | } |
| 2886 | |
Karl Schultz | fda1b38 | 2018-08-08 18:56:11 -0600 | [diff] [blame] | 2887 | spvValidatorOptionsDestroy(options); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2888 | spvDiagnosticDestroy(diag); |
| 2889 | spvContextDestroy(ctx); |
| 2890 | } |
| 2891 | |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 2892 | return skip; |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 2893 | } |
| 2894 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 2895 | void CoreChecks::PreCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
| 2896 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule, |
| 2897 | void *csm_state_data) { |
Mark Lobodzinski | 1db77e8 | 2019-03-01 10:02:54 -0700 | [diff] [blame] | 2898 | create_shader_module_api_state *csm_state = reinterpret_cast<create_shader_module_api_state *>(csm_state_data); |
Mark Lobodzinski | b02a485 | 2019-04-19 12:35:30 -0600 | [diff] [blame] | 2899 | if (enabled.gpu_validation) { |
Mark Lobodzinski | 586d10e | 2019-03-08 18:19:48 -0700 | [diff] [blame] | 2900 | GpuPreCallCreateShaderModule(pCreateInfo, pAllocator, pShaderModule, &csm_state->unique_shader_id, |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 2901 | &csm_state->instrumented_create_info, &csm_state->instrumented_pgm); |
| 2902 | } |
| 2903 | } |
| 2904 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 2905 | void CoreChecks::PostCallRecordCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
| 2906 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule, |
| 2907 | VkResult result, void *csm_state_data) { |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 2908 | if (VK_SUCCESS != result) return; |
Mark Lobodzinski | 1db77e8 | 2019-03-01 10:02:54 -0700 | [diff] [blame] | 2909 | create_shader_module_api_state *csm_state = reinterpret_cast<create_shader_module_api_state *>(csm_state_data); |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 2910 | |
Mark Lobodzinski | 544def7 | 2019-04-19 14:25:59 -0600 | [diff] [blame] | 2911 | spv_target_env spirv_environment = ((api_version >= VK_API_VERSION_1_1) ? SPV_ENV_VULKAN_1_1 : SPV_ENV_VULKAN_1_0); |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 2912 | bool is_spirv = (pCreateInfo->pCode[0] == spv::MagicNumber); |
| 2913 | std::unique_ptr<shader_module> new_shader_module( |
| 2914 | is_spirv ? new shader_module(pCreateInfo, *pShaderModule, spirv_environment, csm_state->unique_shader_id) |
| 2915 | : new shader_module()); |
Mark Lobodzinski | 7767ad8 | 2019-03-09 13:35:25 -0700 | [diff] [blame] | 2916 | shaderModuleMap[*pShaderModule] = std::move(new_shader_module); |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 2917 | } |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame^] | 2918 | |
| 2919 | bool CoreChecks::ValidateComputeWorkGroupSizes(const shader_module *shader) { |
| 2920 | bool skip = false; |
| 2921 | uint32_t local_size_x = 0; |
| 2922 | uint32_t local_size_y = 0; |
| 2923 | uint32_t local_size_z = 0; |
| 2924 | if (FindLocalSize(shader, local_size_x, local_size_y, local_size_z)) { |
| 2925 | if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) { |
| 2926 | skip |= |
| 2927 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 2928 | HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 2929 | "ShaderMdoule %s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").", |
| 2930 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 2931 | phys_dev_props.limits.maxComputeWorkGroupSize[0]); |
| 2932 | } |
| 2933 | if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) { |
| 2934 | skip |= |
| 2935 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 2936 | HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 2937 | "ShaderMdoule %s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").", |
| 2938 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 2939 | phys_dev_props.limits.maxComputeWorkGroupSize[1]); |
| 2940 | } |
| 2941 | if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) { |
| 2942 | skip |= |
| 2943 | log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 2944 | HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 2945 | "ShaderMdoule %s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").", |
| 2946 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 2947 | phys_dev_props.limits.maxComputeWorkGroupSize[2]); |
| 2948 | } |
| 2949 | |
| 2950 | uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations; |
| 2951 | uint64_t invocations = local_size_x * local_size_y; |
| 2952 | // Prevent overflow. |
| 2953 | bool fail = false; |
| 2954 | if (invocations > UINT32_MAX || invocations > limit) { |
| 2955 | fail = true; |
| 2956 | } |
| 2957 | if (!fail) { |
| 2958 | invocations *= local_size_z; |
| 2959 | if (invocations > UINT32_MAX || invocations > limit) { |
| 2960 | fail = true; |
| 2961 | } |
| 2962 | } |
| 2963 | if (fail) { |
| 2964 | skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_SHADER_MODULE_EXT, |
| 2965 | HandleToUint64(shader->vk_shader_module), "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations", |
| 2966 | "ShaderMdoule %s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32 |
| 2967 | ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").", |
| 2968 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, local_size_y, local_size_z, |
| 2969 | limit); |
| 2970 | } |
| 2971 | } |
| 2972 | return skip; |
| 2973 | } |