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