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