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