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