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