sfricke-samsung | 691299b | 2021-01-01 20:48:48 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015-2021 The Khronos Group Inc. |
| 2 | * Copyright (c) 2015-2021 Valve Corporation |
| 3 | * Copyright (c) 2015-2021 LunarG, Inc. |
| 4 | * Copyright (C) 2015-2021 Google Inc. |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 5 | * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved. |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * Author: Chris Forbes <chrisf@ijw.co.nz> |
Dave Houlton | 5165390 | 2018-06-22 17:32:13 -0600 | [diff] [blame] | 20 | * Author: Dave Houlton <daveh@lunarg.com> |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 21 | * Author: Tobias Hector <tobias.hector@amd.com> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 24 | #include "shader_validation.h" |
| 25 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 26 | #include <cassert> |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 27 | #include <chrono> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 28 | #include <cinttypes> |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 29 | #include <cmath> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 30 | #include <sstream> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 31 | #include <string> |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 32 | #include <vector> |
| 33 | |
Mark Lobodzinski | 102687e | 2020-04-28 11:03:28 -0600 | [diff] [blame] | 34 | #include <spirv/unified1/spirv.hpp> |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 35 | #include "vk_loader_platform.h" |
| 36 | #include "vk_enum_string_helper.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 37 | #include "vk_layer_data.h" |
| 38 | #include "vk_layer_extension_utils.h" |
| 39 | #include "vk_layer_utils.h" |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 40 | #include "chassis.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 41 | #include "core_validation.h" |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 42 | |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 43 | #include "spirv-tools/libspirv.h" |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 44 | #include "xxhash.h" |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 45 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 46 | void decoration_set::add(uint32_t decoration, uint32_t value) { |
| 47 | switch (decoration) { |
| 48 | case spv::DecorationLocation: |
| 49 | flags |= location_bit; |
| 50 | location = value; |
| 51 | break; |
| 52 | case spv::DecorationPatch: |
| 53 | flags |= patch_bit; |
| 54 | break; |
| 55 | case spv::DecorationRelaxedPrecision: |
| 56 | flags |= relaxed_precision_bit; |
| 57 | break; |
| 58 | case spv::DecorationBlock: |
| 59 | flags |= block_bit; |
| 60 | break; |
| 61 | case spv::DecorationBufferBlock: |
| 62 | flags |= buffer_block_bit; |
| 63 | break; |
| 64 | case spv::DecorationComponent: |
| 65 | flags |= component_bit; |
| 66 | component = value; |
| 67 | break; |
| 68 | case spv::DecorationInputAttachmentIndex: |
| 69 | flags |= input_attachment_index_bit; |
| 70 | input_attachment_index = value; |
| 71 | break; |
| 72 | case spv::DecorationDescriptorSet: |
| 73 | flags |= descriptor_set_bit; |
| 74 | descriptor_set = value; |
| 75 | break; |
| 76 | case spv::DecorationBinding: |
| 77 | flags |= binding_bit; |
| 78 | binding = value; |
| 79 | break; |
| 80 | case spv::DecorationNonWritable: |
| 81 | flags |= nonwritable_bit; |
| 82 | break; |
| 83 | case spv::DecorationBuiltIn: |
| 84 | flags |= builtin_bit; |
| 85 | builtin = value; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 90 | enum FORMAT_TYPE { |
| 91 | FORMAT_TYPE_FLOAT = 1, // UNORM, SNORM, FLOAT, USCALED, SSCALED, SRGB -- anything we consider float in the shader |
| 92 | FORMAT_TYPE_SINT = 2, |
| 93 | FORMAT_TYPE_UINT = 4, |
| 94 | }; |
| 95 | |
| 96 | typedef std::pair<unsigned, unsigned> location_t; |
| 97 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 98 | static shader_stage_attributes shader_stage_attribs[] = { |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 99 | {"vertex shader", false, false, VK_SHADER_STAGE_VERTEX_BIT}, |
| 100 | {"tessellation control shader", true, true, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT}, |
| 101 | {"tessellation evaluation shader", true, false, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT}, |
| 102 | {"geometry shader", true, false, VK_SHADER_STAGE_GEOMETRY_BIT}, |
| 103 | {"fragment shader", false, false, VK_SHADER_STAGE_FRAGMENT_BIT}, |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 106 | unsigned ExecutionModelToShaderStageFlagBits(unsigned mode); |
| 107 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 108 | // SPIRV utility functions |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 109 | void SHADER_MODULE_STATE::BuildDefIndex() { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 110 | function_set func_set = {}; |
| 111 | EntryPoint *entry_point = nullptr; |
| 112 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 113 | for (auto insn : *this) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 114 | // offset is not 0, it means it's updated and the offset is in a Function. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 115 | if (func_set.offset) { |
Jeremy Gebben | fc6f815 | 2021-03-18 16:58:55 -0600 | [diff] [blame] | 116 | func_set.op_lists.emplace(insn.opcode(), insn.offset()); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 117 | } else if (entry_point) { |
Jeremy Gebben | fc6f815 | 2021-03-18 16:58:55 -0600 | [diff] [blame] | 118 | entry_point->decorate_list.emplace(insn.opcode(), insn.offset()); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 119 | } |
| 120 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 121 | switch (insn.opcode()) { |
| 122 | // Types |
| 123 | case spv::OpTypeVoid: |
| 124 | case spv::OpTypeBool: |
| 125 | case spv::OpTypeInt: |
| 126 | case spv::OpTypeFloat: |
| 127 | case spv::OpTypeVector: |
| 128 | case spv::OpTypeMatrix: |
| 129 | case spv::OpTypeImage: |
| 130 | case spv::OpTypeSampler: |
| 131 | case spv::OpTypeSampledImage: |
| 132 | case spv::OpTypeArray: |
| 133 | case spv::OpTypeRuntimeArray: |
| 134 | case spv::OpTypeStruct: |
| 135 | case spv::OpTypeOpaque: |
| 136 | case spv::OpTypePointer: |
| 137 | case spv::OpTypeFunction: |
| 138 | case spv::OpTypeEvent: |
| 139 | case spv::OpTypeDeviceEvent: |
| 140 | case spv::OpTypeReserveId: |
| 141 | case spv::OpTypeQueue: |
| 142 | case spv::OpTypePipe: |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 143 | case spv::OpTypeAccelerationStructureNV: |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 144 | case spv::OpTypeCooperativeMatrixNV: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 145 | def_index[insn.word(1)] = insn.offset(); |
| 146 | break; |
| 147 | |
| 148 | // Fixed constants |
| 149 | case spv::OpConstantTrue: |
| 150 | case spv::OpConstantFalse: |
| 151 | case spv::OpConstant: |
| 152 | case spv::OpConstantComposite: |
| 153 | case spv::OpConstantSampler: |
| 154 | case spv::OpConstantNull: |
| 155 | def_index[insn.word(2)] = insn.offset(); |
| 156 | break; |
| 157 | |
| 158 | // Specialization constants |
| 159 | case spv::OpSpecConstantTrue: |
| 160 | case spv::OpSpecConstantFalse: |
| 161 | case spv::OpSpecConstant: |
| 162 | case spv::OpSpecConstantComposite: |
| 163 | case spv::OpSpecConstantOp: |
| 164 | def_index[insn.word(2)] = insn.offset(); |
| 165 | break; |
| 166 | |
| 167 | // Variables |
| 168 | case spv::OpVariable: |
| 169 | def_index[insn.word(2)] = insn.offset(); |
| 170 | break; |
| 171 | |
| 172 | // Functions |
| 173 | case spv::OpFunction: |
| 174 | def_index[insn.word(2)] = insn.offset(); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 175 | func_set.id = insn.word(2); |
| 176 | func_set.offset = insn.offset(); |
| 177 | func_set.op_lists.clear(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 178 | break; |
| 179 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 180 | // Decorations |
| 181 | case spv::OpDecorate: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 182 | auto target_id = insn.word(1); |
| 183 | decorations[target_id].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u); |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 184 | decoration_inst.push_back(insn); |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 185 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 186 | builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(3))); |
| 187 | } |
| 188 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 189 | } break; |
| 190 | case spv::OpGroupDecorate: { |
| 191 | auto const &src = decorations[insn.word(1)]; |
| 192 | for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src); |
| 193 | } break; |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 194 | case spv::OpMemberDecorate: { |
| 195 | member_decoration_inst.push_back(insn); |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 196 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 197 | builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(4))); |
| 198 | } |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 199 | } break; |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 200 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 201 | // Entry points ... add to the entrypoint table |
| 202 | case spv::OpEntryPoint: { |
sfricke-samsung | 5c65b37 | 2021-03-25 05:39:57 -0700 | [diff] [blame] | 203 | if (entry_point != nullptr) { |
| 204 | multiple_entry_points = true; |
| 205 | } |
| 206 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 207 | // Entry points do not have an id (the id is the function id) and thus need their own table |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 208 | auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3)); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 209 | auto execution_model = insn.word(1); |
| 210 | auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 211 | entry_points.emplace(entrypoint_name, |
| 212 | EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)}); |
| 213 | |
| 214 | auto range = entry_points.equal_range(entrypoint_name); |
| 215 | for (auto it = range.first; it != range.second; ++it) { |
| 216 | if (it->second.offset == insn.offset()) { |
| 217 | entry_point = &(it->second); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | assert(entry_point != nullptr); |
| 222 | break; |
| 223 | } |
| 224 | case spv::OpFunctionEnd: { |
| 225 | assert(entry_point != nullptr); |
| 226 | func_set.length = insn.offset() - func_set.offset; |
| 227 | entry_point->function_set_list.emplace_back(func_set); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 228 | break; |
| 229 | } |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 230 | |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 231 | // Copy operations |
| 232 | case spv::OpCopyLogical: |
| 233 | case spv::OpCopyObject: { |
| 234 | def_index[insn.word(2)] = insn.offset(); |
| 235 | break; |
| 236 | } |
| 237 | |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 238 | // Execution Mode |
| 239 | case spv::OpExecutionMode: { |
| 240 | execution_mode_inst[insn.word(1)].push_back(insn); |
| 241 | } break; |
| 242 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 243 | default: |
| 244 | // We don't care about any other defs for now. |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 250 | unsigned ExecutionModelToShaderStageFlagBits(unsigned mode) { |
| 251 | switch (mode) { |
| 252 | case spv::ExecutionModelVertex: |
| 253 | return VK_SHADER_STAGE_VERTEX_BIT; |
| 254 | case spv::ExecutionModelTessellationControl: |
| 255 | return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 256 | case spv::ExecutionModelTessellationEvaluation: |
| 257 | return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 258 | case spv::ExecutionModelGeometry: |
| 259 | return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 260 | case spv::ExecutionModelFragment: |
| 261 | return VK_SHADER_STAGE_FRAGMENT_BIT; |
| 262 | case spv::ExecutionModelGLCompute: |
| 263 | return VK_SHADER_STAGE_COMPUTE_BIT; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 264 | case spv::ExecutionModelRayGenerationNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 265 | return VK_SHADER_STAGE_RAYGEN_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 266 | case spv::ExecutionModelAnyHitNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 267 | return VK_SHADER_STAGE_ANY_HIT_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 268 | case spv::ExecutionModelClosestHitNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 269 | return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 270 | case spv::ExecutionModelMissNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 271 | return VK_SHADER_STAGE_MISS_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 272 | case spv::ExecutionModelIntersectionNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 273 | return VK_SHADER_STAGE_INTERSECTION_BIT_NV; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 274 | case spv::ExecutionModelCallableNV: |
Eric Werness | 30127fd | 2018-10-31 21:01:03 -0700 | [diff] [blame] | 275 | return VK_SHADER_STAGE_CALLABLE_BIT_NV; |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 276 | case spv::ExecutionModelTaskNV: |
| 277 | return VK_SHADER_STAGE_TASK_BIT_NV; |
| 278 | case spv::ExecutionModelMeshNV: |
| 279 | return VK_SHADER_STAGE_MESH_BIT_NV; |
| 280 | default: |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 285 | const SHADER_MODULE_STATE::EntryPoint *FindEntrypointStruct(SHADER_MODULE_STATE const *src, char const *name, |
| 286 | VkShaderStageFlagBits stageBits) { |
| 287 | auto range = src->entry_points.equal_range(name); |
| 288 | for (auto it = range.first; it != range.second; ++it) { |
| 289 | if (it->second.stage == stageBits) { |
| 290 | return &(it->second); |
| 291 | } |
| 292 | } |
| 293 | return nullptr; |
| 294 | } |
| 295 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 296 | spirv_inst_iter FindEntrypoint(SHADER_MODULE_STATE const *src, char const *name, VkShaderStageFlagBits stageBits) { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 297 | auto range = src->entry_points.equal_range(name); |
| 298 | for (auto it = range.first; it != range.second; ++it) { |
| 299 | if (it->second.stage == stageBits) { |
| 300 | return src->at(it->second.offset); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 303 | return src->end(); |
| 304 | } |
| 305 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 306 | static char const *StorageClassName(unsigned sc) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 307 | switch (sc) { |
| 308 | case spv::StorageClassInput: |
| 309 | return "input"; |
| 310 | case spv::StorageClassOutput: |
| 311 | return "output"; |
| 312 | case spv::StorageClassUniformConstant: |
| 313 | return "const uniform"; |
| 314 | case spv::StorageClassUniform: |
| 315 | return "uniform"; |
| 316 | case spv::StorageClassWorkgroup: |
| 317 | return "workgroup local"; |
| 318 | case spv::StorageClassCrossWorkgroup: |
| 319 | return "workgroup global"; |
| 320 | case spv::StorageClassPrivate: |
| 321 | return "private global"; |
| 322 | case spv::StorageClassFunction: |
| 323 | return "function"; |
| 324 | case spv::StorageClassGeneric: |
| 325 | return "generic"; |
| 326 | case spv::StorageClassAtomicCounter: |
| 327 | return "atomic counter"; |
| 328 | case spv::StorageClassImage: |
| 329 | return "image"; |
| 330 | case spv::StorageClassPushConstant: |
| 331 | return "push constant"; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 332 | case spv::StorageClassStorageBuffer: |
| 333 | return "storage buffer"; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 334 | default: |
| 335 | return "unknown"; |
| 336 | } |
| 337 | } |
| 338 | |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 339 | // If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction. |
| 340 | // Otherwise, returns src->end(). |
| 341 | spirv_inst_iter GetConstantDef(SHADER_MODULE_STATE const *src, unsigned id) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 342 | auto value = src->get_def(id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 343 | |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 344 | // If id is a copy, see where it was copied from |
| 345 | if ((src->end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) { |
| 346 | id = value.word(3); |
| 347 | value = src->get_def(id); |
| 348 | } |
| 349 | |
| 350 | if ((src->end() != value) && (value.opcode() == spv::OpConstant)) { |
| 351 | return value; |
| 352 | } |
| 353 | return src->end(); |
| 354 | } |
| 355 | |
| 356 | // Assumes itr points to an OpConstant instruction |
| 357 | uint32_t GetConstantValue(const spirv_inst_iter &itr) { return itr.word(3); } |
| 358 | |
| 359 | // Either returns the constant value described by the instruction at id, or 1 |
| 360 | uint32_t GetConstantValue(SHADER_MODULE_STATE const *src, unsigned id) { |
| 361 | auto value = GetConstantDef(src, id); |
| 362 | |
| 363 | if (src->end() == value) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 364 | // TODO: Either ensure that the specialization transform is already performed on a module we're |
| 365 | // considering here, OR -- specialize on the fly now. |
| 366 | return 1; |
| 367 | } |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 368 | return GetConstantValue(value); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 371 | static void DescribeTypeInner(std::ostringstream &ss, SHADER_MODULE_STATE const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 372 | auto insn = src->get_def(type); |
| 373 | assert(insn != src->end()); |
| 374 | |
| 375 | switch (insn.opcode()) { |
| 376 | case spv::OpTypeBool: |
| 377 | ss << "bool"; |
| 378 | break; |
| 379 | case spv::OpTypeInt: |
| 380 | ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2); |
| 381 | break; |
| 382 | case spv::OpTypeFloat: |
| 383 | ss << "float" << insn.word(2); |
| 384 | break; |
| 385 | case spv::OpTypeVector: |
| 386 | ss << "vec" << insn.word(3) << " of "; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 387 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 388 | break; |
| 389 | case spv::OpTypeMatrix: |
| 390 | ss << "mat" << insn.word(3) << " of "; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 391 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 392 | break; |
| 393 | case spv::OpTypeArray: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 394 | ss << "arr[" << GetConstantValue(src, insn.word(3)) << "] of "; |
| 395 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 396 | break; |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 397 | case spv::OpTypeRuntimeArray: |
| 398 | ss << "runtime arr[] of "; |
| 399 | DescribeTypeInner(ss, src, insn.word(2)); |
| 400 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 401 | case spv::OpTypePointer: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 402 | ss << "ptr to " << StorageClassName(insn.word(2)) << " "; |
| 403 | DescribeTypeInner(ss, src, insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 404 | break; |
| 405 | case spv::OpTypeStruct: { |
| 406 | ss << "struct of ("; |
| 407 | for (unsigned i = 2; i < insn.len(); i++) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 408 | DescribeTypeInner(ss, src, insn.word(i)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 409 | if (i == insn.len() - 1) { |
| 410 | ss << ")"; |
| 411 | } else { |
| 412 | ss << ", "; |
| 413 | } |
| 414 | } |
| 415 | break; |
| 416 | } |
| 417 | case spv::OpTypeSampler: |
| 418 | ss << "sampler"; |
| 419 | break; |
| 420 | case spv::OpTypeSampledImage: |
| 421 | ss << "sampler+"; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 422 | DescribeTypeInner(ss, src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 423 | break; |
| 424 | case spv::OpTypeImage: |
| 425 | ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")"; |
| 426 | break; |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 427 | case spv::OpTypeAccelerationStructureNV: |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 428 | ss << "accelerationStruture"; |
| 429 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 430 | default: |
| 431 | ss << "oddtype"; |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 436 | static std::string DescribeType(SHADER_MODULE_STATE const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 437 | std::ostringstream ss; |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 438 | DescribeTypeInner(ss, src, type); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 439 | return ss.str(); |
| 440 | } |
| 441 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 442 | static bool IsNarrowNumericType(spirv_inst_iter type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 443 | if (type.opcode() != spv::OpTypeInt && type.opcode() != spv::OpTypeFloat) return false; |
| 444 | return type.word(2) < 64; |
| 445 | } |
| 446 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 447 | static bool TypesMatch(SHADER_MODULE_STATE const *a, SHADER_MODULE_STATE const *b, unsigned a_type, unsigned b_type, bool a_arrayed, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 448 | bool b_arrayed, bool relaxed) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 449 | // Walk two type trees together, and complain about differences |
| 450 | auto a_insn = a->get_def(a_type); |
| 451 | auto b_insn = b->get_def(b_type); |
| 452 | assert(a_insn != a->end()); |
| 453 | assert(b_insn != b->end()); |
| 454 | |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 455 | // Ignore runtime-sized arrays-- they cannot appear in these interfaces. |
| 456 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 457 | if (a_arrayed && a_insn.opcode() == spv::OpTypeArray) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 458 | 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] | 459 | } |
| 460 | |
| 461 | if (b_arrayed && b_insn.opcode() == spv::OpTypeArray) { |
| 462 | // 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] | 463 | 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] | 464 | } |
| 465 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 466 | if (a_insn.opcode() == spv::OpTypeVector && relaxed && IsNarrowNumericType(b_insn)) { |
| 467 | 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] | 468 | } |
| 469 | |
| 470 | if (a_insn.opcode() != b_insn.opcode()) { |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | if (a_insn.opcode() == spv::OpTypePointer) { |
| 475 | // Match on pointee type. storage class is expected to differ |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 476 | 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] | 477 | } |
| 478 | |
| 479 | if (a_arrayed || b_arrayed) { |
| 480 | // If we havent resolved array-of-verts by here, we're not going to. |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | switch (a_insn.opcode()) { |
| 485 | case spv::OpTypeBool: |
| 486 | return true; |
| 487 | case spv::OpTypeInt: |
| 488 | // Match on width, signedness |
| 489 | return a_insn.word(2) == b_insn.word(2) && a_insn.word(3) == b_insn.word(3); |
| 490 | case spv::OpTypeFloat: |
| 491 | // Match on width |
| 492 | return a_insn.word(2) == b_insn.word(2); |
| 493 | case spv::OpTypeVector: |
| 494 | // Match on element type, count. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 495 | if (!TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false)) return false; |
| 496 | if (relaxed && IsNarrowNumericType(a->get_def(a_insn.word(2)))) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 497 | return a_insn.word(3) >= b_insn.word(3); |
| 498 | } else { |
| 499 | return a_insn.word(3) == b_insn.word(3); |
| 500 | } |
| 501 | case spv::OpTypeMatrix: |
| 502 | // Match on element type, count. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 503 | 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] | 504 | a_insn.word(3) == b_insn.word(3); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 505 | case spv::OpTypeArray: |
| 506 | // Match on element type, count. these all have the same layout. we don't get here if b_arrayed. This differs from |
| 507 | // 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] | 508 | return TypesMatch(a, b, a_insn.word(2), b_insn.word(2), a_arrayed, b_arrayed, false) && |
| 509 | GetConstantValue(a, a_insn.word(3)) == GetConstantValue(b, b_insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 510 | case spv::OpTypeStruct: |
| 511 | // Match on all element types |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 512 | { |
| 513 | if (a_insn.len() != b_insn.len()) { |
| 514 | return false; // Structs cannot match if member counts differ |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 515 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 516 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 517 | for (unsigned i = 2; i < a_insn.len(); i++) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 518 | 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] | 519 | return false; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return true; |
| 524 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 525 | default: |
| 526 | // Remaining types are CLisms, or may not appear in the interfaces we are interested in. Just claim no match. |
| 527 | return false; |
| 528 | } |
| 529 | } |
| 530 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 531 | static unsigned GetLocationsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 532 | auto insn = src->get_def(type); |
| 533 | assert(insn != src->end()); |
| 534 | |
| 535 | switch (insn.opcode()) { |
| 536 | case spv::OpTypePointer: |
| 537 | // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing |
| 538 | // pointers around. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 539 | return GetLocationsConsumedByType(src, insn.word(3), strip_array_level); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 540 | case spv::OpTypeArray: |
| 541 | if (strip_array_level) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 542 | return GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 543 | } else { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 544 | return GetConstantValue(src, insn.word(3)) * GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 545 | } |
| 546 | case spv::OpTypeMatrix: |
| 547 | // Num locations is the dimension * element size |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 548 | return insn.word(3) * GetLocationsConsumedByType(src, insn.word(2), false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 549 | case spv::OpTypeVector: { |
| 550 | auto scalar_type = src->get_def(insn.word(2)); |
| 551 | auto bit_width = |
| 552 | (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32; |
| 553 | |
| 554 | // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two. |
| 555 | return (bit_width * insn.word(3) + 127) / 128; |
| 556 | } |
| 557 | default: |
| 558 | // Everything else is just 1. |
| 559 | return 1; |
| 560 | |
| 561 | // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations. |
| 562 | } |
| 563 | } |
| 564 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 565 | static unsigned GetComponentsConsumedByType(SHADER_MODULE_STATE const *src, unsigned type, bool strip_array_level) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 566 | auto insn = src->get_def(type); |
| 567 | assert(insn != src->end()); |
| 568 | |
| 569 | switch (insn.opcode()) { |
| 570 | case spv::OpTypePointer: |
| 571 | // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing |
| 572 | // pointers around. |
| 573 | return GetComponentsConsumedByType(src, insn.word(3), strip_array_level); |
| 574 | case spv::OpTypeStruct: { |
| 575 | uint32_t sum = 0; |
| 576 | for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct |
| 577 | sum += GetComponentsConsumedByType(src, insn.word(i), false); |
| 578 | } |
| 579 | return sum; |
| 580 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 581 | case spv::OpTypeArray: |
| 582 | if (strip_array_level) { |
| 583 | return GetComponentsConsumedByType(src, insn.word(2), false); |
| 584 | } else { |
| 585 | return GetConstantValue(src, insn.word(3)) * GetComponentsConsumedByType(src, insn.word(2), false); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 586 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 587 | case spv::OpTypeMatrix: |
| 588 | // Num locations is the dimension * element size |
| 589 | return insn.word(3) * GetComponentsConsumedByType(src, insn.word(2), false); |
| 590 | case spv::OpTypeVector: { |
| 591 | auto scalar_type = src->get_def(insn.word(2)); |
| 592 | auto bit_width = |
| 593 | (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32; |
| 594 | // One component is 32-bit |
| 595 | return (bit_width * insn.word(3) + 31) / 32; |
| 596 | } |
| 597 | case spv::OpTypeFloat: { |
| 598 | auto bit_width = insn.word(2); |
| 599 | return (bit_width + 31) / 32; |
| 600 | } |
| 601 | case spv::OpTypeInt: { |
| 602 | auto bit_width = insn.word(2); |
| 603 | return (bit_width + 31) / 32; |
| 604 | } |
| 605 | case spv::OpConstant: |
| 606 | return GetComponentsConsumedByType(src, insn.word(1), false); |
| 607 | default: |
| 608 | return 0; |
| 609 | } |
| 610 | } |
| 611 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 612 | static unsigned GetLocationsConsumedByFormat(VkFormat format) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 613 | switch (format) { |
| 614 | case VK_FORMAT_R64G64B64A64_SFLOAT: |
| 615 | case VK_FORMAT_R64G64B64A64_SINT: |
| 616 | case VK_FORMAT_R64G64B64A64_UINT: |
| 617 | case VK_FORMAT_R64G64B64_SFLOAT: |
| 618 | case VK_FORMAT_R64G64B64_SINT: |
| 619 | case VK_FORMAT_R64G64B64_UINT: |
| 620 | return 2; |
| 621 | default: |
| 622 | return 1; |
| 623 | } |
| 624 | } |
| 625 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 626 | static unsigned GetFormatType(VkFormat fmt) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 627 | if (FormatIsSInt(fmt)) return FORMAT_TYPE_SINT; |
| 628 | if (FormatIsUInt(fmt)) return FORMAT_TYPE_UINT; |
| 629 | if (FormatIsDepthAndStencil(fmt)) return FORMAT_TYPE_FLOAT | FORMAT_TYPE_UINT; |
| 630 | if (fmt == VK_FORMAT_UNDEFINED) return 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 631 | // everything else -- UNORM/SNORM/FLOAT/USCALED/SSCALED is all float in the shader. |
| 632 | return FORMAT_TYPE_FLOAT; |
| 633 | } |
| 634 | |
| 635 | // 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] | 636 | // also used for input attachments, as we statically know their format. |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 637 | static unsigned GetFundamentalType(SHADER_MODULE_STATE const *src, unsigned type) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 638 | auto insn = src->get_def(type); |
| 639 | assert(insn != src->end()); |
| 640 | |
| 641 | switch (insn.opcode()) { |
| 642 | case spv::OpTypeInt: |
| 643 | return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT; |
| 644 | case spv::OpTypeFloat: |
| 645 | return FORMAT_TYPE_FLOAT; |
| 646 | case spv::OpTypeVector: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 647 | case spv::OpTypeMatrix: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 648 | case spv::OpTypeArray: |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 649 | case spv::OpTypeRuntimeArray: |
| 650 | case spv::OpTypeImage: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 651 | return GetFundamentalType(src, insn.word(2)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 652 | case spv::OpTypePointer: |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 653 | return GetFundamentalType(src, insn.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 654 | |
| 655 | default: |
| 656 | return 0; |
| 657 | } |
| 658 | } |
| 659 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 660 | static uint32_t GetShaderStageId(VkShaderStageFlagBits stage) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 661 | uint32_t bit_pos = uint32_t(u_ffs(stage)); |
| 662 | return bit_pos - 1; |
| 663 | } |
| 664 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 665 | static spirv_inst_iter GetStructType(SHADER_MODULE_STATE const *src, spirv_inst_iter def, bool is_array_of_verts) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 666 | while (true) { |
| 667 | if (def.opcode() == spv::OpTypePointer) { |
| 668 | def = src->get_def(def.word(3)); |
| 669 | } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) { |
| 670 | def = src->get_def(def.word(2)); |
| 671 | is_array_of_verts = false; |
| 672 | } else if (def.opcode() == spv::OpTypeStruct) { |
| 673 | return def; |
| 674 | } else { |
| 675 | return src->end(); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 680 | static bool CollectInterfaceBlockMembers(SHADER_MODULE_STATE const *src, std::map<location_t, interface_var> *out, |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 681 | bool is_array_of_verts, uint32_t id, uint32_t type_id, bool is_patch, |
| 682 | int /*first_location*/) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 683 | // 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] | 684 | auto type = GetStructType(src, src->get_def(type_id), is_array_of_verts && !is_patch); |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 685 | if (type == src->end() || !(src->get_decorations(type.word(1)).flags & decoration_set::block_bit)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 686 | // This isn't an interface block. |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 687 | return false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 690 | layer_data::unordered_map<unsigned, unsigned> member_components; |
| 691 | layer_data::unordered_map<unsigned, unsigned> member_relaxed_precision; |
| 692 | layer_data::unordered_map<unsigned, unsigned> member_patch; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 693 | |
| 694 | // Walk all the OpMemberDecorate for type's result id -- first pass, collect components. |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 695 | for (auto insn : src->member_decoration_inst) { |
| 696 | if (insn.word(1) == type.word(1)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 697 | unsigned member_index = insn.word(2); |
| 698 | |
| 699 | if (insn.word(3) == spv::DecorationComponent) { |
| 700 | unsigned component = insn.word(4); |
| 701 | member_components[member_index] = component; |
| 702 | } |
| 703 | |
| 704 | if (insn.word(3) == spv::DecorationRelaxedPrecision) { |
| 705 | member_relaxed_precision[member_index] = 1; |
| 706 | } |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 707 | |
| 708 | if (insn.word(3) == spv::DecorationPatch) { |
| 709 | member_patch[member_index] = 1; |
| 710 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 714 | // TODO: correctly handle location assignment from outside |
| 715 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 716 | // Second pass -- produce the output, from Location decorations |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 717 | for (auto insn : src->member_decoration_inst) { |
| 718 | if (insn.word(1) == type.word(1)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 719 | unsigned member_index = insn.word(2); |
| 720 | unsigned member_type_id = type.word(2 + member_index); |
| 721 | |
| 722 | if (insn.word(3) == spv::DecorationLocation) { |
| 723 | unsigned location = insn.word(4); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 724 | unsigned num_locations = GetLocationsConsumedByType(src, member_type_id, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 725 | auto component_it = member_components.find(member_index); |
| 726 | unsigned component = component_it == member_components.end() ? 0 : component_it->second; |
| 727 | 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] | 728 | bool member_is_patch = is_patch || member_patch.count(member_index) > 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 729 | |
| 730 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 731 | interface_var v = {}; |
| 732 | v.id = id; |
| 733 | // TODO: member index in interface_var too? |
| 734 | v.type_id = member_type_id; |
| 735 | v.offset = offset; |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 736 | v.is_patch = member_is_patch; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 737 | v.is_block_member = true; |
| 738 | v.is_relaxed_precision = is_relaxed_precision; |
| 739 | (*out)[std::make_pair(location + offset, component)] = v; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | } |
Chris Forbes | a313d77 | 2017-06-13 13:59:41 -0700 | [diff] [blame] | 744 | |
| 745 | return true; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 748 | static std::vector<uint32_t> FindEntrypointInterfaces(spirv_inst_iter entrypoint) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 749 | assert(entrypoint.opcode() == spv::OpEntryPoint); |
| 750 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 751 | std::vector<uint32_t> interfaces; |
| 752 | // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the |
| 753 | // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator. |
| 754 | uint32_t word = 3; |
| 755 | while (entrypoint.word(word) & 0xff000000u) { |
| 756 | ++word; |
| 757 | } |
| 758 | ++word; |
| 759 | |
| 760 | for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word)); |
| 761 | |
| 762 | return interfaces; |
| 763 | } |
| 764 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 765 | static std::map<location_t, interface_var> CollectInterfaceByLocation(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 766 | spv::StorageClass sinterface, bool is_array_of_verts) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 767 | // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber. |
| 768 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 769 | std::map<location_t, interface_var> out; |
| 770 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 771 | for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) { |
| 772 | auto insn = src->get_def(iid); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 773 | assert(insn != src->end()); |
| 774 | assert(insn.opcode() == spv::OpVariable); |
| 775 | |
| 776 | if (insn.word(3) == static_cast<uint32_t>(sinterface)) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 777 | auto d = src->get_decorations(iid); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 778 | unsigned id = insn.word(2); |
| 779 | unsigned type = insn.word(1); |
| 780 | |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 781 | int location = d.location; |
| 782 | int builtin = d.builtin; |
| 783 | unsigned component = d.component; |
| 784 | bool is_patch = (d.flags & decoration_set::patch_bit) != 0; |
| 785 | bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 786 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 787 | if (builtin != -1) { |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 788 | continue; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 789 | } else if (!CollectInterfaceBlockMembers(src, &out, is_array_of_verts, id, type, is_patch, location)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 790 | // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit |
| 791 | // one result for each. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 792 | unsigned num_locations = GetLocationsConsumedByType(src, type, is_array_of_verts && !is_patch); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 793 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 794 | interface_var v = {}; |
| 795 | v.id = id; |
| 796 | v.type_id = type; |
| 797 | v.offset = offset; |
| 798 | v.is_patch = is_patch; |
| 799 | v.is_relaxed_precision = is_relaxed_precision; |
| 800 | out[std::make_pair(location + offset, component)] = v; |
| 801 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | return out; |
| 807 | } |
| 808 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 809 | static std::vector<uint32_t> CollectBuiltinBlockMembers(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint, |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 810 | uint32_t storageClass) { |
| 811 | std::vector<uint32_t> variables; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 812 | std::vector<uint32_t> builtin_struct_members; |
| 813 | std::vector<uint32_t> builtin_decorations; |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 814 | |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 815 | for (auto insn : src->member_decoration_inst) { |
| 816 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 817 | builtin_struct_members.push_back(insn.word(1)); |
| 818 | } |
| 819 | } |
| 820 | for (auto insn : src->decoration_inst) { |
| 821 | switch (insn.word(2)) { |
| 822 | case spv::DecorationBlock: { |
| 823 | uint32_t block_id = insn.word(1); |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 824 | for (auto builtin_block_id : builtin_struct_members) { |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 825 | // Check if one of the members of the block are built-in -> the block is built-in |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 826 | if (block_id == builtin_block_id) { |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 827 | builtin_decorations.push_back(block_id); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 828 | break; |
| 829 | } |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 830 | } |
| 831 | break; |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 832 | } |
| 833 | case spv::DecorationBuiltIn: |
| 834 | builtin_decorations.push_back(insn.word(1)); |
| 835 | break; |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 836 | default: |
| 837 | break; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | // Find all interface variables belonging to the entrypoint and matching the storage class |
| 842 | for (uint32_t id : FindEntrypointInterfaces(entrypoint)) { |
| 843 | auto def = src->get_def(id); |
| 844 | assert(def != src->end()); |
| 845 | assert(def.opcode() == spv::OpVariable); |
| 846 | |
| 847 | if (def.word(3) == storageClass) variables.push_back(def.word(1)); |
| 848 | } |
| 849 | |
| 850 | // Find all members belonging to the builtin block selected |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 851 | std::vector<uint32_t> builtin_block_members; |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 852 | for (auto &var : variables) { |
| 853 | auto def = src->get_def(src->get_def(var).word(3)); |
| 854 | |
| 855 | // It could be an array of IO blocks. The element type should be the struct defining the block contents |
| 856 | if (def.opcode() == spv::OpTypeArray) def = src->get_def(def.word(2)); |
| 857 | |
| 858 | // Now find all members belonging to the struct defining the IO block |
| 859 | if (def.opcode() == spv::OpTypeStruct) { |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 860 | for (auto builtin_id : builtin_decorations) { |
| 861 | if (builtin_id == def.word(1)) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 862 | for (int i = 2; i < static_cast<int>(def.len()); i++) { |
| 863 | builtin_block_members.push_back(spv::BuiltInMax); // Start with undefined builtin for each struct member. |
| 864 | } |
| 865 | // These shouldn't be left after replacing. |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 866 | for (auto insn : src->member_decoration_inst) { |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 867 | if (insn.word(1) == builtin_id && insn.word(3) == spv::DecorationBuiltIn) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 868 | auto struct_index = insn.word(2); |
| 869 | assert(struct_index < builtin_block_members.size()); |
| 870 | builtin_block_members[struct_index] = insn.word(4); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 878 | return builtin_block_members; |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 879 | } |
| 880 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 881 | static std::vector<std::pair<uint32_t, interface_var>> CollectInterfaceByInputAttachmentIndex( |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 882 | SHADER_MODULE_STATE const *src, layer_data::unordered_set<uint32_t> const &accessible_ids) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 883 | std::vector<std::pair<uint32_t, interface_var>> out; |
| 884 | |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 885 | for (auto insn : src->decoration_inst) { |
| 886 | if (insn.word(2) == spv::DecorationInputAttachmentIndex) { |
| 887 | auto attachment_index = insn.word(3); |
| 888 | auto id = insn.word(1); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 889 | |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 890 | if (accessible_ids.count(id)) { |
| 891 | auto def = src->get_def(id); |
| 892 | assert(def != src->end()); |
| 893 | if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) { |
| 894 | auto num_locations = GetLocationsConsumedByType(src, def.word(1), false); |
| 895 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 896 | interface_var v = {}; |
| 897 | v.id = id; |
| 898 | v.type_id = def.word(1); |
| 899 | v.offset = offset; |
| 900 | out.emplace_back(attachment_index + offset, v); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | return out; |
| 908 | } |
| 909 | |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 910 | static bool AtomicOperation(uint32_t opcode) { |
| 911 | switch (opcode) { |
| 912 | case spv::OpAtomicLoad: |
| 913 | case spv::OpAtomicStore: |
| 914 | case spv::OpAtomicExchange: |
| 915 | case spv::OpAtomicCompareExchange: |
| 916 | case spv::OpAtomicCompareExchangeWeak: |
| 917 | case spv::OpAtomicIIncrement: |
| 918 | case spv::OpAtomicIDecrement: |
| 919 | case spv::OpAtomicIAdd: |
| 920 | case spv::OpAtomicISub: |
| 921 | case spv::OpAtomicSMin: |
| 922 | case spv::OpAtomicUMin: |
| 923 | case spv::OpAtomicSMax: |
| 924 | case spv::OpAtomicUMax: |
| 925 | case spv::OpAtomicAnd: |
| 926 | case spv::OpAtomicOr: |
| 927 | case spv::OpAtomicXor: |
| 928 | case spv::OpAtomicFAddEXT: |
| 929 | return true; |
| 930 | default: |
| 931 | return false; |
| 932 | } |
| 933 | return false; |
| 934 | } |
| 935 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 936 | // Only includes valid group operations used in Vulkan (for now thats only subgroup ops) and any non supported operation will be |
| 937 | // covered with VUID 01090 |
| 938 | static bool GroupOperation(uint32_t opcode) { |
| 939 | switch (opcode) { |
| 940 | case spv::OpGroupNonUniformElect: |
| 941 | case spv::OpGroupNonUniformAll: |
| 942 | case spv::OpGroupNonUniformAny: |
| 943 | case spv::OpGroupNonUniformAllEqual: |
| 944 | case spv::OpGroupNonUniformBroadcast: |
| 945 | case spv::OpGroupNonUniformBroadcastFirst: |
| 946 | case spv::OpGroupNonUniformBallot: |
| 947 | case spv::OpGroupNonUniformInverseBallot: |
| 948 | case spv::OpGroupNonUniformBallotBitExtract: |
| 949 | case spv::OpGroupNonUniformBallotBitCount: |
| 950 | case spv::OpGroupNonUniformBallotFindLSB: |
| 951 | case spv::OpGroupNonUniformBallotFindMSB: |
| 952 | case spv::OpGroupNonUniformShuffle: |
| 953 | case spv::OpGroupNonUniformShuffleXor: |
| 954 | case spv::OpGroupNonUniformShuffleUp: |
| 955 | case spv::OpGroupNonUniformShuffleDown: |
| 956 | case spv::OpGroupNonUniformIAdd: |
| 957 | case spv::OpGroupNonUniformFAdd: |
| 958 | case spv::OpGroupNonUniformIMul: |
| 959 | case spv::OpGroupNonUniformFMul: |
| 960 | case spv::OpGroupNonUniformSMin: |
| 961 | case spv::OpGroupNonUniformUMin: |
| 962 | case spv::OpGroupNonUniformFMin: |
| 963 | case spv::OpGroupNonUniformSMax: |
| 964 | case spv::OpGroupNonUniformUMax: |
| 965 | case spv::OpGroupNonUniformFMax: |
| 966 | case spv::OpGroupNonUniformBitwiseAnd: |
| 967 | case spv::OpGroupNonUniformBitwiseOr: |
| 968 | case spv::OpGroupNonUniformBitwiseXor: |
| 969 | case spv::OpGroupNonUniformLogicalAnd: |
| 970 | case spv::OpGroupNonUniformLogicalOr: |
| 971 | case spv::OpGroupNonUniformLogicalXor: |
| 972 | case spv::OpGroupNonUniformQuadBroadcast: |
| 973 | case spv::OpGroupNonUniformQuadSwap: |
| 974 | case spv::OpGroupNonUniformPartitionNV: |
| 975 | return true; |
| 976 | default: |
| 977 | return false; |
| 978 | } |
| 979 | return false; |
| 980 | } |
| 981 | |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 982 | bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<unsigned> &operator_members, |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 983 | const layer_data::unordered_map<unsigned, unsigned> &load_members, |
| 984 | const layer_data::unordered_map<unsigned, std::pair<unsigned, unsigned>> &accesschain_members) { |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 985 | for (auto load_id : operator_members) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 986 | if (object_id == load_id) return true; |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 987 | auto load_it = load_members.find(load_id); |
| 988 | if (load_it == load_members.end()) { |
| 989 | continue; |
| 990 | } |
| 991 | if (load_it->second == object_id) { |
| 992 | return true; |
| 993 | } |
| 994 | |
| 995 | auto accesschain_it = accesschain_members.find(load_it->second); |
| 996 | if (accesschain_it == accesschain_members.end()) { |
| 997 | continue; |
| 998 | } |
| 999 | if (accesschain_it->second.first == object_id) { |
| 1000 | return true; |
| 1001 | } |
| 1002 | } |
| 1003 | return false; |
| 1004 | } |
| 1005 | |
locke-lunarg | ae2a43c | 2020-09-22 17:21:57 -0600 | [diff] [blame] | 1006 | bool CheckImageOperandsBiasOffset(uint32_t type) { |
| 1007 | return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask | |
| 1008 | spv::ImageOperandsConstOffsetsMask) |
| 1009 | ? true |
| 1010 | : false; |
| 1011 | } |
| 1012 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1013 | struct shader_module_used_operators { |
| 1014 | bool updated; |
| 1015 | std::vector<unsigned> imagwrite_members; |
| 1016 | std::vector<unsigned> atomic_members; |
| 1017 | std::vector<unsigned> store_members; |
| 1018 | std::vector<unsigned> atomic_store_members; |
| 1019 | std::vector<unsigned> sampler_implicitLod_dref_proj_members; // sampler Load id |
| 1020 | std::vector<unsigned> sampler_bias_offset_members; // sampler Load id |
sfricke-samsung | 691299b | 2021-01-01 20:48:48 -0800 | [diff] [blame] | 1021 | std::vector<std::pair<unsigned, unsigned>> sampledImage_members; // <image,sampler> Load id |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1022 | layer_data::unordered_map<unsigned, unsigned> load_members; |
| 1023 | layer_data::unordered_map<unsigned, std::pair<unsigned, unsigned>> accesschain_members; |
| 1024 | layer_data::unordered_map<unsigned, unsigned> image_texel_pointer_members; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1025 | |
| 1026 | shader_module_used_operators() : updated(false) {} |
| 1027 | |
| 1028 | void update(SHADER_MODULE_STATE const *module) { |
| 1029 | if (updated) return; |
| 1030 | updated = true; |
| 1031 | |
| 1032 | for (auto insn : *module) { |
| 1033 | switch (insn.opcode()) { |
| 1034 | case spv::OpImageSampleImplicitLod: |
| 1035 | case spv::OpImageSampleProjImplicitLod: |
| 1036 | case spv::OpImageSampleProjExplicitLod: |
| 1037 | case spv::OpImageSparseSampleImplicitLod: |
| 1038 | case spv::OpImageSparseSampleProjImplicitLod: |
| 1039 | case spv::OpImageSparseSampleProjExplicitLod: { |
| 1040 | sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id |
| 1041 | // ImageOperands in index: 5 |
| 1042 | if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) { |
| 1043 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1044 | } |
| 1045 | break; |
| 1046 | } |
| 1047 | case spv::OpImageSampleDrefImplicitLod: |
| 1048 | case spv::OpImageSampleDrefExplicitLod: |
| 1049 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1050 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1051 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 1052 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 1053 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 1054 | case spv::OpImageSparseSampleProjDrefExplicitLod: { |
| 1055 | sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id |
| 1056 | // ImageOperands in index: 6 |
| 1057 | if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) { |
| 1058 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1059 | } |
| 1060 | break; |
| 1061 | } |
| 1062 | case spv::OpImageSampleExplicitLod: |
| 1063 | case spv::OpImageSparseSampleExplicitLod: { |
| 1064 | // ImageOperands in index: 5 |
| 1065 | if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) { |
| 1066 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1067 | } |
| 1068 | break; |
| 1069 | } |
| 1070 | case spv::OpStore: { |
| 1071 | store_members.emplace_back(insn.word(1)); // object id or AccessChain id |
| 1072 | break; |
| 1073 | } |
| 1074 | case spv::OpImageWrite: { |
| 1075 | imagwrite_members.emplace_back(insn.word(1)); // Load id |
| 1076 | break; |
| 1077 | } |
| 1078 | case spv::OpSampledImage: { |
| 1079 | // 3: image load id, 4: sampler load id |
| 1080 | sampledImage_members.emplace_back(std::pair<unsigned, unsigned>(insn.word(3), insn.word(4))); |
| 1081 | break; |
| 1082 | } |
| 1083 | case spv::OpLoad: { |
| 1084 | // 2: Load id, 3: object id or AccessChain id |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1085 | load_members.emplace(insn.word(2), insn.word(3)); |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1086 | break; |
| 1087 | } |
| 1088 | case spv::OpAccessChain: { |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1089 | if (insn.len() == 4) { |
| 1090 | // If it is for struct, the length is only 4. |
| 1091 | // 2: AccessChain id, 3: object id |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1092 | accesschain_members.emplace(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), 0)); |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1093 | } else { |
| 1094 | // 2: AccessChain id, 3: object id, 4: object id of array index |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1095 | accesschain_members.emplace(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), insn.word(4))); |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1096 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | case spv::OpImageTexelPointer: { |
| 1100 | // 2: ImageTexelPointer id, 3: object id |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1101 | image_texel_pointer_members.emplace(insn.word(2), insn.word(3)); |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1102 | break; |
| 1103 | } |
| 1104 | default: { |
| 1105 | if (AtomicOperation(insn.opcode())) { |
| 1106 | if (insn.opcode() == spv::OpAtomicStore) { |
| 1107 | atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id |
| 1108 | } else { |
| 1109 | atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id |
| 1110 | } |
| 1111 | } |
| 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | }; |
| 1118 | |
sfricke-samsung | 691299b | 2021-01-01 20:48:48 -0800 | [diff] [blame] | 1119 | // Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image |
| 1120 | // atomic operation, matching images to samplers, etc |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1121 | static void IsSpecificDescriptorType(SHADER_MODULE_STATE const *module, const spirv_inst_iter &id_it, bool is_storage_buffer, |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1122 | bool is_check_writable, interface_var &out_interface_var, |
| 1123 | shader_module_used_operators &used_operators) { |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1124 | uint32_t type_id = id_it.word(1); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1125 | unsigned int id = id_it.word(2); |
| 1126 | |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1127 | auto type = module->get_def(type_id); |
| 1128 | |
| 1129 | // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension. |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 1130 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray || |
| 1131 | type.opcode() == spv::OpTypeSampledImage) { |
| 1132 | if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray || |
| 1133 | type.opcode() == spv::OpTypeSampledImage) { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1134 | type = module->get_def(type.word(2)); // Element type |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1135 | } else { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1136 | type = module->get_def(type.word(3)); // Pointer type |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1137 | } |
| 1138 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1139 | switch (type.opcode()) { |
| 1140 | case spv::OpTypeImage: { |
| 1141 | auto dim = type.word(3); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1142 | if (dim != spv::DimSubpassData) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1143 | used_operators.update(module); |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1144 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1145 | if (CheckObjectIDFromOpLoad(id, used_operators.imagwrite_members, used_operators.load_members, |
| 1146 | used_operators.accesschain_members)) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1147 | out_interface_var.is_writable = true; |
locke-lunarg | 12d2099 | 2020-09-21 12:46:49 -0600 | [diff] [blame] | 1148 | } |
| 1149 | if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members, |
| 1150 | used_operators.accesschain_members)) { |
| 1151 | out_interface_var.is_sampler_implicitLod_dref_proj = true; |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1152 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1153 | if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members, |
| 1154 | used_operators.accesschain_members)) { |
locke-lunarg | ae2a43c | 2020-09-22 17:21:57 -0600 | [diff] [blame] | 1155 | out_interface_var.is_sampler_bias_offset = true; |
| 1156 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1157 | if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members, |
| 1158 | used_operators.accesschain_members) || |
| 1159 | CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members, |
| 1160 | used_operators.accesschain_members)) { |
| 1161 | out_interface_var.is_atomic_operation = true; |
| 1162 | } |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1163 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1164 | for (auto &itp_id : used_operators.sampledImage_members) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1165 | // Find if image id match. |
| 1166 | uint32_t image_index = 0; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1167 | auto load_it = used_operators.load_members.find(itp_id.first); |
| 1168 | if (load_it == used_operators.load_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1169 | continue; |
| 1170 | } else { |
| 1171 | if (load_it->second != id) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1172 | auto accesschain_it = used_operators.accesschain_members.find(load_it->second); |
| 1173 | if (accesschain_it == used_operators.accesschain_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1174 | continue; |
| 1175 | } else { |
| 1176 | if (accesschain_it->second.first != id) { |
| 1177 | continue; |
| 1178 | } |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 1179 | |
| 1180 | const auto const_itr = GetConstantDef(module, accesschain_it->second.second); |
| 1181 | if (const_itr == module->end()) { |
| 1182 | // access chain index not a constant, skip. |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1183 | break; |
| 1184 | } |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 1185 | image_index = GetConstantValue(const_itr); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | // Find sampler's set binding. |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1190 | load_it = used_operators.load_members.find(itp_id.second); |
| 1191 | if (load_it == used_operators.load_members.end()) { |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1192 | continue; |
| 1193 | } else { |
| 1194 | uint32_t sampler_id = load_it->second; |
| 1195 | uint32_t sampler_index = 0; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1196 | auto accesschain_it = used_operators.accesschain_members.find(load_it->second); |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 1197 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1198 | if (accesschain_it != used_operators.accesschain_members.end()) { |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 1199 | const auto const_itr = GetConstantDef(module, accesschain_it->second.second); |
| 1200 | if (const_itr == module->end()) { |
| 1201 | // access chain index representing sampler index is not a constant, skip. |
locke-lunarg | 025daa7 | 2020-10-13 11:07:51 -0600 | [diff] [blame] | 1202 | break; |
| 1203 | } |
Nathaniel Cesario | 2d3dc2b | 2021-02-16 19:51:03 -0700 | [diff] [blame] | 1204 | sampler_id = const_itr.offset(); |
| 1205 | sampler_index = GetConstantValue(const_itr); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1206 | } |
| 1207 | auto sampler_dec = module->get_decorations(sampler_id); |
locke-lunarg | 654a905 | 2020-10-13 16:28:42 -0600 | [diff] [blame] | 1208 | if (image_index >= out_interface_var.samplers_used_by_image.size()) { |
| 1209 | out_interface_var.samplers_used_by_image.resize(image_index + 1); |
| 1210 | } |
| 1211 | out_interface_var.samplers_used_by_image[image_index].emplace( |
| 1212 | SamplerUsedByImage{descriptor_slot_t{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index}); |
locke-lunarg | 3604599 | 2020-08-20 16:54:37 -0600 | [diff] [blame] | 1213 | } |
| 1214 | } |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1215 | } |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1216 | return; |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1219 | case spv::OpTypeStruct: { |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1220 | layer_data::unordered_set<unsigned> nonwritable_members; |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 1221 | if (module->get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true; |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 1222 | for (auto insn : module->member_decoration_inst) { |
| 1223 | if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) { |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1224 | nonwritable_members.insert(insn.word(2)); |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1225 | } |
| 1226 | } |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1227 | |
| 1228 | // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated |
| 1229 | // as nonwritable. |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1230 | if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) { |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1231 | used_operators.update(module); |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1232 | |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1233 | for (auto oid : used_operators.store_members) { |
| 1234 | if (id == oid) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1235 | out_interface_var.is_writable = true; |
| 1236 | return; |
| 1237 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1238 | auto accesschain_it = used_operators.accesschain_members.find(oid); |
| 1239 | if (accesschain_it == used_operators.accesschain_members.end()) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1240 | continue; |
| 1241 | } |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1242 | if (accesschain_it->second.first == id) { |
| 1243 | out_interface_var.is_writable = true; |
| 1244 | return; |
| 1245 | } |
| 1246 | } |
| 1247 | if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members, |
| 1248 | used_operators.accesschain_members)) { |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1249 | out_interface_var.is_writable = true; |
| 1250 | return; |
locke-lunarg | 6f760f1 | 2020-06-05 16:19:37 -0600 | [diff] [blame] | 1251 | } |
| 1252 | } |
Chris Forbes | 8d31e5d | 2018-10-08 17:19:15 -0700 | [diff] [blame] | 1253 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1254 | } |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 1257 | std::vector<std::pair<descriptor_slot_t, interface_var>> CollectInterfaceByDescriptorSlot( |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1258 | SHADER_MODULE_STATE const *src, layer_data::unordered_set<uint32_t> const &accessible_ids, bool *has_writable_descriptor, |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 1259 | bool *has_atomic_descriptor) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1260 | std::vector<std::pair<descriptor_slot_t, interface_var>> out; |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1261 | shader_module_used_operators operators; |
| 1262 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1263 | for (auto id : accessible_ids) { |
| 1264 | auto insn = src->get_def(id); |
| 1265 | assert(insn != src->end()); |
| 1266 | |
| 1267 | if (insn.opcode() == spv::OpVariable && |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 1268 | (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant || |
| 1269 | insn.word(3) == spv::StorageClassStorageBuffer)) { |
Chris Forbes | 8a6d8cb | 2019-02-14 14:33:08 -0800 | [diff] [blame] | 1270 | auto d = src->get_decorations(insn.word(2)); |
| 1271 | unsigned set = d.descriptor_set; |
| 1272 | unsigned binding = d.binding; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1273 | |
| 1274 | interface_var v = {}; |
| 1275 | v.id = insn.word(2); |
| 1276 | v.type_id = insn.word(1); |
Chris Forbes | 8af2452 | 2018-03-07 11:37:45 -0800 | [diff] [blame] | 1277 | |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1278 | IsSpecificDescriptorType(src, insn, insn.word(3) == spv::StorageClassStorageBuffer, |
locke-lunarg | d3da042 | 2020-09-23 01:02:11 -0600 | [diff] [blame] | 1279 | !(d.flags & decoration_set::nonwritable_bit), v, operators); |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 1280 | if (v.is_writable) *has_writable_descriptor = true; |
| 1281 | if (v.is_atomic_operation) *has_atomic_descriptor = true; |
locke-lunarg | 654e369 | 2020-06-04 17:19:15 -0600 | [diff] [blame] | 1282 | out.emplace_back(std::make_pair(set, binding), v); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return out; |
| 1287 | } |
| 1288 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1289 | void DefineStructMember(const SHADER_MODULE_STATE &src, const spirv_inst_iter &it, |
| 1290 | const std::vector<uint32_t> &memberDecorate_offsets, shader_struct_member &data) { |
| 1291 | const auto struct_it = GetStructType(&src, it, false); |
| 1292 | assert(struct_it != src.end()); |
| 1293 | data.size = 0; |
| 1294 | |
| 1295 | shader_struct_member data1; |
| 1296 | uint32_t i = 2; |
| 1297 | uint32_t local_offset = 0; |
| 1298 | std::vector<uint32_t> offsets; |
| 1299 | offsets.resize(struct_it.len() - i); |
| 1300 | |
| 1301 | // The members of struct in SPRIV_R aren't always sort, so we need to know their order. |
| 1302 | for (const auto offset : memberDecorate_offsets) { |
| 1303 | const auto member_decorate = src.at(offset); |
| 1304 | if (member_decorate.word(1) != struct_it.word(1)) { |
| 1305 | continue; |
| 1306 | } |
| 1307 | |
| 1308 | offsets[member_decorate.word(2)] = member_decorate.word(4); |
| 1309 | } |
| 1310 | |
| 1311 | for (const auto offset : offsets) { |
| 1312 | local_offset = offset; |
| 1313 | data1 = {}; |
| 1314 | data1.root = data.root; |
| 1315 | data1.offset = local_offset; |
| 1316 | auto def_member = src.get_def(struct_it.word(i)); |
| 1317 | |
| 1318 | // Array could be multi-dimensional |
| 1319 | while (def_member.opcode() == spv::OpTypeArray) { |
| 1320 | const auto len_id = def_member.word(3); |
| 1321 | const auto def_len = src.get_def(len_id); |
| 1322 | data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length |
| 1323 | def_member = src.get_def(def_member.word(2)); |
| 1324 | } |
| 1325 | |
Nathaniel Cesario | 85caecf | 2021-01-14 10:28:05 -0700 | [diff] [blame] | 1326 | if (def_member.opcode() == spv::OpTypeStruct) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1327 | DefineStructMember(src, def_member, memberDecorate_offsets, data1); |
Nathaniel Cesario | 85caecf | 2021-01-14 10:28:05 -0700 | [diff] [blame] | 1328 | } else if (def_member.opcode() == spv::OpTypePointer) { |
| 1329 | if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) { |
| 1330 | // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address |
| 1331 | // that "points to something." |
| 1332 | data1.size = 8; |
| 1333 | } else { |
| 1334 | // If it's OpTypePointer. it means the member is a buffer, the type will be TypePointer, and then struct |
| 1335 | DefineStructMember(src, def_member, memberDecorate_offsets, data1); |
| 1336 | } |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1337 | } else { |
| 1338 | if (def_member.opcode() == spv::OpTypeMatrix) { |
| 1339 | data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector. |
| 1340 | def_member = src.get_def(def_member.word(2)); |
| 1341 | } |
| 1342 | |
| 1343 | if (def_member.opcode() == spv::OpTypeVector) { |
| 1344 | data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length |
| 1345 | def_member = src.get_def(def_member.word(2)); |
| 1346 | } |
| 1347 | |
| 1348 | // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte. |
| 1349 | data1.size = (def_member.word(2) / 8); |
| 1350 | } |
| 1351 | const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size(); |
| 1352 | if (array_length_hierarchy_szie > 0) { |
| 1353 | data1.array_block_size.resize(array_length_hierarchy_szie, 1); |
| 1354 | |
| 1355 | for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) { |
| 1356 | data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2]; |
| 1357 | } |
| 1358 | } |
| 1359 | data.struct_members.emplace_back(data1); |
| 1360 | ++i; |
| 1361 | } |
| 1362 | uint32_t total_array_length = 1; |
| 1363 | for (const auto length : data1.array_length_hierarchy) { |
| 1364 | total_array_length *= length; |
| 1365 | } |
| 1366 | data.size = local_offset + data1.size * total_array_length; |
| 1367 | } |
| 1368 | |
| 1369 | uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) { |
| 1370 | int array_indices_size = static_cast<int>(array_indices.size()); |
| 1371 | if (array_indices_size) { |
| 1372 | uint32_t array_index = 0; |
| 1373 | uint32_t i = 0; |
| 1374 | for (const auto index : array_indices) { |
| 1375 | array_index += (data.array_block_size[i] * index); |
| 1376 | ++i; |
| 1377 | } |
| 1378 | offset += (array_index * data.size); |
| 1379 | } |
| 1380 | return offset; |
| 1381 | } |
| 1382 | |
| 1383 | void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) { |
| 1384 | int array_indices_size = static_cast<int>(array_indices.size()); |
| 1385 | uint32_t block_memory_size = data.size; |
| 1386 | for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) { |
| 1387 | block_memory_size *= data.array_length_hierarchy[i]; |
| 1388 | } |
| 1389 | |
| 1390 | offset = UpdateOffset(offset, array_indices, data); |
| 1391 | |
| 1392 | uint32_t end = offset + block_memory_size; |
| 1393 | auto used_bytes = data.GetUsedbytes(); |
| 1394 | if (used_bytes->size() < end) { |
| 1395 | used_bytes->resize(end, 0); |
| 1396 | } |
| 1397 | std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size)); |
| 1398 | } |
| 1399 | |
| 1400 | void RunUsedArray(const SHADER_MODULE_STATE &src, uint32_t offset, std::vector<uint32_t> array_indices, |
| 1401 | uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it, const shader_struct_member &data) { |
| 1402 | if (access_chain_word_index < access_chain_it.len()) { |
| 1403 | if (data.array_length_hierarchy.size() > array_indices.size()) { |
| 1404 | auto def_it = src.get_def(access_chain_it.word(access_chain_word_index)); |
| 1405 | ++access_chain_word_index; |
| 1406 | |
| 1407 | if (def_it != src.end() && def_it.opcode() == spv::OpConstant) { |
| 1408 | array_indices.emplace_back(def_it.word(3)); |
| 1409 | RunUsedArray(src, offset, array_indices, access_chain_word_index, access_chain_it, data); |
| 1410 | } else { |
| 1411 | // If it is a variable, set the all array is used. |
| 1412 | if (access_chain_word_index < access_chain_it.len()) { |
| 1413 | uint32_t array_length = data.array_length_hierarchy[array_indices.size()]; |
| 1414 | for (uint32_t i = 0; i < array_length; ++i) { |
| 1415 | auto array_indices2 = array_indices; |
| 1416 | array_indices2.emplace_back(i); |
| 1417 | RunUsedArray(src, offset, array_indices2, access_chain_word_index, access_chain_it, data); |
| 1418 | } |
| 1419 | } else { |
| 1420 | SetUsedBytes(offset, array_indices, data); |
| 1421 | } |
| 1422 | } |
| 1423 | } else { |
| 1424 | offset = UpdateOffset(offset, array_indices, data); |
| 1425 | RunUsedStruct(src, offset, access_chain_word_index, access_chain_it, data); |
| 1426 | } |
| 1427 | } else { |
| 1428 | SetUsedBytes(offset, array_indices, data); |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | void RunUsedStruct(const SHADER_MODULE_STATE &src, uint32_t offset, uint32_t access_chain_word_index, |
| 1433 | spirv_inst_iter &access_chain_it, const shader_struct_member &data) { |
| 1434 | std::vector<uint32_t> array_indices_emptry; |
| 1435 | |
| 1436 | if (access_chain_word_index < access_chain_it.len()) { |
| 1437 | auto strcut_member_index = GetConstantValue(&src, access_chain_it.word(access_chain_word_index)); |
| 1438 | ++access_chain_word_index; |
| 1439 | |
| 1440 | auto data1 = data.struct_members[strcut_member_index]; |
| 1441 | RunUsedArray(src, offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | void SetUsedStructMember(const SHADER_MODULE_STATE &src, const uint32_t variable_id, |
| 1446 | const std::vector<function_set> &function_set_list, const shader_struct_member &data) { |
| 1447 | for (const auto &func_set : function_set_list) { |
| 1448 | auto range = func_set.op_lists.equal_range(spv::OpAccessChain); |
| 1449 | for (auto it = range.first; it != range.second; ++it) { |
| 1450 | auto access_chain = src.at(it->second); |
| 1451 | if (access_chain.word(3) == variable_id) { |
| 1452 | RunUsedStruct(src, 0, 4, access_chain, data); |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | void SetPushConstantUsedInShader(SHADER_MODULE_STATE &src) { |
| 1459 | for (auto &entrypoint : src.entry_points) { |
| 1460 | auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable); |
| 1461 | for (auto it = range.first; it != range.second; ++it) { |
| 1462 | const auto def_insn = src.at(it->second); |
| 1463 | |
| 1464 | if (def_insn.word(3) == spv::StorageClassPushConstant) { |
| 1465 | spirv_inst_iter type = src.get_def(def_insn.word(1)); |
| 1466 | const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate); |
| 1467 | std::vector<uint32_t> offsets; |
| 1468 | |
| 1469 | for (auto it2 = range2.first; it2 != range2.second; ++it2) { |
| 1470 | auto member_decorate = src.at(it2->second); |
| 1471 | if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) { |
| 1472 | offsets.emplace_back(member_decorate.offset()); |
| 1473 | } |
| 1474 | } |
| 1475 | entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader; |
| 1476 | DefineStructMember(src, type, offsets, entrypoint.second.push_constant_used_in_shader); |
| 1477 | SetUsedStructMember(src, def_insn.word(2), entrypoint.second.function_set_list, |
| 1478 | entrypoint.second.push_constant_used_in_shader); |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1484 | layer_data::unordered_set<uint32_t> CollectWritableOutputLocationinFS(const SHADER_MODULE_STATE &module, |
locke-lunarg | 96dc963 | 2020-06-10 17:22:18 -0600 | [diff] [blame] | 1485 | const VkPipelineShaderStageCreateInfo &stage_info) { |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1486 | layer_data::unordered_set<uint32_t> location_list; |
locke-lunarg | 96dc963 | 2020-06-10 17:22:18 -0600 | [diff] [blame] | 1487 | if (stage_info.stage != VK_SHADER_STAGE_FRAGMENT_BIT) return location_list; |
| 1488 | const auto entrypoint = FindEntrypoint(&module, stage_info.pName, stage_info.stage); |
| 1489 | const auto outputs = CollectInterfaceByLocation(&module, entrypoint, spv::StorageClassOutput, false); |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1490 | layer_data::unordered_set<unsigned> store_members; |
| 1491 | layer_data::unordered_map<unsigned, unsigned> accesschain_members; |
locke-lunarg | 96dc963 | 2020-06-10 17:22:18 -0600 | [diff] [blame] | 1492 | |
| 1493 | for (auto insn : module) { |
| 1494 | switch (insn.opcode()) { |
| 1495 | case spv::OpStore: |
| 1496 | case spv::OpAtomicStore: { |
| 1497 | store_members.insert(insn.word(1)); // object id or AccessChain id |
| 1498 | break; |
| 1499 | } |
| 1500 | case spv::OpAccessChain: { |
| 1501 | // 2: AccessChain id, 3: object id |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1502 | if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3)); |
locke-lunarg | 96dc963 | 2020-06-10 17:22:18 -0600 | [diff] [blame] | 1503 | break; |
| 1504 | } |
| 1505 | default: |
| 1506 | break; |
| 1507 | } |
| 1508 | } |
| 1509 | if (store_members.empty()) { |
| 1510 | return location_list; |
| 1511 | } |
| 1512 | for (auto output : outputs) { |
| 1513 | auto store_it = store_members.find(output.second.id); |
| 1514 | if (store_it != store_members.end()) { |
| 1515 | location_list.insert(output.first.first); |
| 1516 | store_members.erase(store_it); |
| 1517 | continue; |
| 1518 | } |
| 1519 | store_it = store_members.begin(); |
| 1520 | while (store_it != store_members.end()) { |
| 1521 | auto accesschain_it = accesschain_members.find(*store_it); |
| 1522 | if (accesschain_it == accesschain_members.end()) { |
| 1523 | ++store_it; |
| 1524 | continue; |
| 1525 | } |
| 1526 | if (accesschain_it->second == output.second.id) { |
| 1527 | location_list.insert(output.first.first); |
| 1528 | store_members.erase(store_it); |
| 1529 | accesschain_members.erase(accesschain_it); |
| 1530 | break; |
| 1531 | } |
| 1532 | ++store_it; |
| 1533 | } |
| 1534 | } |
| 1535 | return location_list; |
| 1536 | } |
| 1537 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1538 | bool CoreChecks::ValidateViConsistency(VkPipelineVertexInputStateCreateInfo const *vi) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1539 | // Walk the binding descriptions, which describe the step rate and stride of each vertex buffer. Each binding should |
| 1540 | // be specified only once. |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1541 | layer_data::unordered_map<uint32_t, VkVertexInputBindingDescription const *> bindings; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1542 | bool skip = false; |
| 1543 | |
| 1544 | for (unsigned i = 0; i < vi->vertexBindingDescriptionCount; i++) { |
| 1545 | auto desc = &vi->pVertexBindingDescriptions[i]; |
| 1546 | auto &binding = bindings[desc->binding]; |
| 1547 | if (binding) { |
Dave Houlton | 78d0992 | 2018-05-17 15:48:45 -0600 | [diff] [blame] | 1548 | // TODO: "VUID-VkGraphicsPipelineCreateInfo-pStages-00742" perhaps? |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1549 | skip |= LogError(device, kVUID_Core_Shader_InconsistentVi, "Duplicate vertex input binding descriptions for binding %d", |
| 1550 | desc->binding); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1551 | } else { |
| 1552 | binding = desc; |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | return skip; |
| 1557 | } |
| 1558 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1559 | bool CoreChecks::ValidateViAgainstVsInputs(VkPipelineVertexInputStateCreateInfo const *vi, SHADER_MODULE_STATE const *vs, |
| 1560 | spirv_inst_iter entrypoint) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1561 | bool skip = false; |
| 1562 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1563 | const auto inputs = CollectInterfaceByLocation(vs, entrypoint, spv::StorageClassInput, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1564 | |
| 1565 | // Build index by location |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1566 | std::map<uint32_t, const VkVertexInputAttributeDescription *> attribs; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1567 | if (vi) { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1568 | for (uint32_t i = 0; i < vi->vertexAttributeDescriptionCount; ++i) { |
| 1569 | const auto num_locations = GetLocationsConsumedByFormat(vi->pVertexAttributeDescriptions[i].format); |
| 1570 | for (uint32_t j = 0; j < num_locations; ++j) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1571 | attribs[vi->pVertexAttributeDescriptions[i].location + j] = &vi->pVertexAttributeDescriptions[i]; |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1576 | struct AttribInputPair { |
| 1577 | const VkVertexInputAttributeDescription *attrib = nullptr; |
| 1578 | const interface_var *input = nullptr; |
| 1579 | }; |
| 1580 | std::map<uint32_t, AttribInputPair> location_map; |
| 1581 | for (const auto &attrib_it : attribs) location_map[attrib_it.first].attrib = attrib_it.second; |
| 1582 | for (const auto &input_it : inputs) location_map[input_it.first.first].input = &input_it.second; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1583 | |
Jamie Madill | c1f7ca8 | 2020-03-16 17:08:26 -0400 | [diff] [blame] | 1584 | for (const auto &location_it : location_map) { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1585 | const auto location = location_it.first; |
| 1586 | const auto attrib = location_it.second.attrib; |
| 1587 | const auto input = location_it.second.input; |
Mark Lobodzinski | 7caa39c | 2018-07-25 15:48:34 -0600 | [diff] [blame] | 1588 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1589 | if (attrib && !input) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1590 | skip |= LogPerformanceWarning(vs->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 1591 | "Vertex attribute at location %" PRIu32 " not consumed by vertex shader", location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1592 | } else if (!attrib && input) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1593 | skip |= LogError(vs->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 1594 | "Vertex shader consumes input at location %" PRIu32 " but not provided", location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1595 | } else if (attrib && input) { |
| 1596 | const auto attrib_type = GetFormatType(attrib->format); |
| 1597 | const auto input_type = GetFundamentalType(vs, input->type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1598 | |
| 1599 | // Type checking |
| 1600 | if (!(attrib_type & input_type)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1601 | skip |= LogError(vs->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 1602 | "Attribute type of `%s` at location %" PRIu32 " does not match vertex shader input type of `%s`", |
| 1603 | string_VkFormat(attrib->format), location, DescribeType(vs, input->type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1604 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1605 | } else { // !attrib && !input |
| 1606 | assert(false); // at least one exists in the map |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | return skip; |
| 1611 | } |
| 1612 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 1613 | bool CoreChecks::ValidateFsOutputsAgainstRenderPass(SHADER_MODULE_STATE const *fs, spirv_inst_iter entrypoint, |
| 1614 | PIPELINE_STATE const *pipeline, uint32_t subpass_index) const { |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1615 | bool skip = false; |
Chris Forbes | 8bca165 | 2017-07-20 11:10:09 -0700 | [diff] [blame] | 1616 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1617 | const auto rpci = pipeline->rp_state->createInfo.ptr(); |
| 1618 | |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1619 | struct Attachment { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 1620 | const VkAttachmentReference2 *reference = nullptr; |
| 1621 | const VkAttachmentDescription2 *attachment = nullptr; |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1622 | const interface_var *output = nullptr; |
| 1623 | }; |
| 1624 | std::map<uint32_t, Attachment> location_map; |
| 1625 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1626 | const auto subpass = rpci->pSubpasses[subpass_index]; |
| 1627 | for (uint32_t i = 0; i < subpass.colorAttachmentCount; ++i) { |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1628 | auto const &reference = subpass.pColorAttachments[i]; |
| 1629 | location_map[i].reference = &reference; |
| 1630 | if (reference.attachment != VK_ATTACHMENT_UNUSED && |
| 1631 | rpci->pAttachments[reference.attachment].format != VK_FORMAT_UNDEFINED) { |
| 1632 | location_map[i].attachment = &rpci->pAttachments[reference.attachment]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1636 | // TODO: dual source blend index (spv::DecIndex, zero if not provided) |
| 1637 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1638 | const auto outputs = CollectInterfaceByLocation(fs, entrypoint, spv::StorageClassOutput, false); |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1639 | for (const auto &output_it : outputs) { |
| 1640 | auto const location = output_it.first.first; |
| 1641 | location_map[location].output = &output_it.second; |
| 1642 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1643 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1644 | const bool alpha_to_coverage_enabled = pipeline->graphicsPipelineCI.pMultisampleState != NULL && |
| 1645 | pipeline->graphicsPipelineCI.pMultisampleState->alphaToCoverageEnable == VK_TRUE; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1646 | |
Jamie Madill | c1f7ca8 | 2020-03-16 17:08:26 -0400 | [diff] [blame] | 1647 | for (const auto &location_it : location_map) { |
Jeremy Hayes | 3699c7c | 2019-10-09 12:24:55 -0600 | [diff] [blame] | 1648 | const auto reference = location_it.second.reference; |
| 1649 | if (reference != nullptr && reference->attachment == VK_ATTACHMENT_UNUSED) { |
| 1650 | continue; |
| 1651 | } |
| 1652 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1653 | const auto location = location_it.first; |
| 1654 | const auto attachment = location_it.second.attachment; |
| 1655 | const auto output = location_it.second.output; |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1656 | if (attachment && !output) { |
| 1657 | if (pipeline->attachments[location].colorWriteMask != 0) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1658 | skip |= LogWarning(fs->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 1659 | "Attachment %" PRIu32 |
| 1660 | " not written by fragment shader; undefined values will be written to attachment", |
| 1661 | location); |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1662 | } |
| 1663 | } else if (!attachment && output) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1664 | if (!(alpha_to_coverage_enabled && location == 0)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1665 | skip |= LogWarning(fs->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 1666 | "fragment shader writes to output location %" PRIu32 " with no matching attachment", location); |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1667 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1668 | } else if (attachment && output) { |
| 1669 | const auto attachment_type = GetFormatType(attachment->format); |
| 1670 | const auto output_type = GetFundamentalType(fs, output->type_id); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1671 | |
| 1672 | // Type checking |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1673 | if (!(output_type & attachment_type)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1674 | skip |= |
| 1675 | LogWarning(fs->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 1676 | "Attachment %" PRIu32 |
| 1677 | " of type `%s` does not match fragment shader output type of `%s`; resulting values are undefined", |
| 1678 | location, string_VkFormat(attachment->format), DescribeType(fs, output->type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1679 | } |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1680 | } else { // !attachment && !output |
| 1681 | assert(false); // at least one exists in the map |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | |
Petr Kraus | 25810d0 | 2019-08-27 17:41:15 +0200 | [diff] [blame] | 1685 | const auto output_zero = location_map.count(0) ? location_map[0].output : nullptr; |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 1686 | bool location_zero_has_alpha = output_zero && fs->get_def(output_zero->type_id) != fs->end() && |
| 1687 | GetComponentsConsumedByType(fs, output_zero->type_id, false) == 4; |
| 1688 | if (alpha_to_coverage_enabled && !location_zero_has_alpha) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 1689 | skip |= LogError(fs->vk_shader_module, kVUID_Core_Shader_NoAlphaAtLocation0WithAlphaToCoverage, |
| 1690 | "fragment shader doesn't declare alpha output at location 0 even though alpha to coverage is enabled."); |
Ari Suonpaa | 412b23b | 2019-02-26 07:56:58 +0200 | [diff] [blame] | 1691 | } |
| 1692 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1693 | return skip; |
| 1694 | } |
| 1695 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1696 | // For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to. |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1697 | // This function examines instructions in the static call tree for a write to this variable. |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 1698 | static bool IsBuiltInWritten(SHADER_MODULE_STATE const *src, spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1699 | auto type = builtin_instr.opcode(); |
| 1700 | uint32_t target_id = builtin_instr.word(1); |
| 1701 | bool init_complete = false; |
| 1702 | |
| 1703 | if (type == spv::OpMemberDecorate) { |
| 1704 | // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs |
| 1705 | auto insn = entrypoint; |
| 1706 | while (!init_complete && (insn.opcode() != spv::OpFunction)) { |
| 1707 | switch (insn.opcode()) { |
| 1708 | case spv::OpTypePointer: |
| 1709 | if ((insn.word(3) == target_id) && (insn.word(2) == spv::StorageClassOutput)) { |
| 1710 | target_id = insn.word(1); |
| 1711 | } |
| 1712 | break; |
| 1713 | case spv::OpVariable: |
| 1714 | if (insn.word(1) == target_id) { |
| 1715 | target_id = insn.word(2); |
| 1716 | init_complete = true; |
| 1717 | } |
| 1718 | break; |
| 1719 | } |
| 1720 | insn++; |
| 1721 | } |
| 1722 | } |
| 1723 | |
Mark Lobodzinski | f84b0b4 | 2018-09-11 14:54:32 -0600 | [diff] [blame] | 1724 | if (!init_complete && (type == spv::OpMemberDecorate)) return false; |
| 1725 | |
| 1726 | bool found_write = false; |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1727 | layer_data::unordered_set<uint32_t> worklist; |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 1728 | worklist.insert(entrypoint.word(2)); |
| 1729 | |
| 1730 | // Follow instructions in call graph looking for writes to target |
| 1731 | while (!worklist.empty() && !found_write) { |
| 1732 | auto id_iter = worklist.begin(); |
| 1733 | auto id = *id_iter; |
| 1734 | worklist.erase(id_iter); |
| 1735 | |
| 1736 | auto insn = src->get_def(id); |
| 1737 | if (insn == src->end()) { |
| 1738 | continue; |
| 1739 | } |
| 1740 | |
| 1741 | if (insn.opcode() == spv::OpFunction) { |
| 1742 | // Scan body of function looking for other function calls or items in our ID chain |
| 1743 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1744 | switch (insn.opcode()) { |
| 1745 | case spv::OpAccessChain: |
| 1746 | if (insn.word(3) == target_id) { |
| 1747 | if (type == spv::OpMemberDecorate) { |
| 1748 | auto value = GetConstantValue(src, insn.word(4)); |
| 1749 | if (value == builtin_instr.word(2)) { |
| 1750 | target_id = insn.word(2); |
| 1751 | } |
| 1752 | } else { |
| 1753 | target_id = insn.word(2); |
| 1754 | } |
| 1755 | } |
| 1756 | break; |
| 1757 | case spv::OpStore: |
| 1758 | if (insn.word(1) == target_id) { |
| 1759 | found_write = true; |
| 1760 | } |
| 1761 | break; |
| 1762 | case spv::OpFunctionCall: |
| 1763 | worklist.insert(insn.word(3)); |
| 1764 | break; |
| 1765 | } |
| 1766 | } |
| 1767 | } |
| 1768 | } |
| 1769 | return found_write; |
| 1770 | } |
| 1771 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1772 | // For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is |
| 1773 | // important for identifying the set of shader resources actually used by an entrypoint, for example. |
| 1774 | // Note: we only explore parts of the image which might actually contain ids we care about for the above analyses. |
| 1775 | // - NOT the shader input/output interfaces. |
| 1776 | // |
| 1777 | // TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth |
| 1778 | // converting parts of this to be generated from the machine-readable spec instead. |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1779 | layer_data::unordered_set<uint32_t> MarkAccessibleIds(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) { |
| 1780 | layer_data::unordered_set<uint32_t> ids; |
| 1781 | layer_data::unordered_set<uint32_t> worklist; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1782 | worklist.insert(entrypoint.word(2)); |
| 1783 | |
| 1784 | while (!worklist.empty()) { |
| 1785 | auto id_iter = worklist.begin(); |
| 1786 | auto id = *id_iter; |
| 1787 | worklist.erase(id_iter); |
| 1788 | |
| 1789 | auto insn = src->get_def(id); |
| 1790 | if (insn == src->end()) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 1791 | // 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] | 1792 | // that we may not care about. |
| 1793 | continue; |
| 1794 | } |
| 1795 | |
| 1796 | // Try to add to the output set |
| 1797 | if (!ids.insert(id).second) { |
| 1798 | continue; // If we already saw this id, we don't want to walk it again. |
| 1799 | } |
| 1800 | |
| 1801 | switch (insn.opcode()) { |
| 1802 | case spv::OpFunction: |
| 1803 | // Scan whole body of the function, enlisting anything interesting |
| 1804 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1805 | switch (insn.opcode()) { |
| 1806 | case spv::OpLoad: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1807 | worklist.insert(insn.word(3)); // ptr |
| 1808 | break; |
| 1809 | case spv::OpStore: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1810 | worklist.insert(insn.word(1)); // ptr |
| 1811 | break; |
| 1812 | case spv::OpAccessChain: |
| 1813 | case spv::OpInBoundsAccessChain: |
| 1814 | worklist.insert(insn.word(3)); // base ptr |
| 1815 | break; |
| 1816 | case spv::OpSampledImage: |
| 1817 | case spv::OpImageSampleImplicitLod: |
| 1818 | case spv::OpImageSampleExplicitLod: |
| 1819 | case spv::OpImageSampleDrefImplicitLod: |
| 1820 | case spv::OpImageSampleDrefExplicitLod: |
| 1821 | case spv::OpImageSampleProjImplicitLod: |
| 1822 | case spv::OpImageSampleProjExplicitLod: |
| 1823 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1824 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1825 | case spv::OpImageFetch: |
| 1826 | case spv::OpImageGather: |
| 1827 | case spv::OpImageDrefGather: |
| 1828 | case spv::OpImageRead: |
| 1829 | case spv::OpImage: |
| 1830 | case spv::OpImageQueryFormat: |
| 1831 | case spv::OpImageQueryOrder: |
| 1832 | case spv::OpImageQuerySizeLod: |
| 1833 | case spv::OpImageQuerySize: |
| 1834 | case spv::OpImageQueryLod: |
| 1835 | case spv::OpImageQueryLevels: |
| 1836 | case spv::OpImageQuerySamples: |
| 1837 | case spv::OpImageSparseSampleImplicitLod: |
| 1838 | case spv::OpImageSparseSampleExplicitLod: |
| 1839 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 1840 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 1841 | case spv::OpImageSparseSampleProjImplicitLod: |
| 1842 | case spv::OpImageSparseSampleProjExplicitLod: |
| 1843 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 1844 | case spv::OpImageSparseSampleProjDrefExplicitLod: |
| 1845 | case spv::OpImageSparseFetch: |
| 1846 | case spv::OpImageSparseGather: |
| 1847 | case spv::OpImageSparseDrefGather: |
| 1848 | case spv::OpImageTexelPointer: |
| 1849 | worklist.insert(insn.word(3)); // Image or sampled image |
| 1850 | break; |
| 1851 | case spv::OpImageWrite: |
| 1852 | worklist.insert(insn.word(1)); // Image -- different operand order to above |
| 1853 | break; |
| 1854 | case spv::OpFunctionCall: |
| 1855 | for (uint32_t i = 3; i < insn.len(); i++) { |
| 1856 | worklist.insert(insn.word(i)); // fn itself, and all args |
| 1857 | } |
| 1858 | break; |
| 1859 | |
| 1860 | case spv::OpExtInst: |
| 1861 | for (uint32_t i = 5; i < insn.len(); i++) { |
| 1862 | worklist.insert(insn.word(i)); // Operands to ext inst |
| 1863 | } |
| 1864 | break; |
locke-lunarg | 25b6c35 | 2020-08-06 17:44:18 -0600 | [diff] [blame] | 1865 | |
| 1866 | default: { |
| 1867 | if (AtomicOperation(insn.opcode())) { |
| 1868 | if (insn.opcode() == spv::OpAtomicStore) { |
| 1869 | worklist.insert(insn.word(1)); // ptr |
| 1870 | } else { |
| 1871 | worklist.insert(insn.word(3)); // ptr |
| 1872 | } |
| 1873 | } |
| 1874 | break; |
| 1875 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1876 | } |
| 1877 | } |
| 1878 | break; |
| 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | return ids; |
| 1883 | } |
| 1884 | |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1885 | PushConstantByteState CoreChecks::ValidatePushConstantSetUpdate(const std::vector<uint8_t> &push_constant_data_update, |
| 1886 | const shader_struct_member &push_constant_used_in_shader, |
| 1887 | uint32_t &out_issue_index) const { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1888 | const auto *used_bytes = push_constant_used_in_shader.GetUsedbytes(); |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1889 | const auto used_bytes_size = used_bytes->size(); |
| 1890 | if (used_bytes_size == 0) return PC_Byte_Updated; |
| 1891 | |
| 1892 | const auto push_constant_data_update_size = push_constant_data_update.size(); |
| 1893 | const auto *data = push_constant_data_update.data(); |
| 1894 | if ((*data == PC_Byte_Updated) && std::memcmp(data, data + 1, push_constant_data_update_size - 1) == 0) { |
| 1895 | if (used_bytes_size <= push_constant_data_update_size) { |
| 1896 | return PC_Byte_Updated; |
| 1897 | } |
| 1898 | const auto used_bytes_size1 = used_bytes_size - push_constant_data_update_size; |
| 1899 | |
| 1900 | const auto *used_bytes_data1 = used_bytes->data() + push_constant_data_update_size; |
| 1901 | if ((*used_bytes_data1 == 0) && std::memcmp(used_bytes_data1, used_bytes_data1 + 1, used_bytes_size1 - 1) == 0) { |
| 1902 | return PC_Byte_Updated; |
| 1903 | } |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1904 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1905 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1906 | uint32_t i = 0; |
| 1907 | for (const auto used : *used_bytes) { |
| 1908 | if (used) { |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1909 | if (i >= push_constant_data_update.size() || push_constant_data_update[i] == PC_Byte_Not_Set) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1910 | out_issue_index = i; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1911 | return PC_Byte_Not_Set; |
| 1912 | } else if (push_constant_data_update[i] == PC_Byte_Not_Updated) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1913 | out_issue_index = i; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1914 | return PC_Byte_Not_Updated; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | ++i; |
| 1918 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1919 | return PC_Byte_Updated; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | bool CoreChecks::ValidatePushConstantUsage(const PIPELINE_STATE &pipeline, SHADER_MODULE_STATE const *src, |
| 1923 | VkPipelineShaderStageCreateInfo const *pStage) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1924 | bool skip = false; |
sfricke-samsung | 5c65b37 | 2021-03-25 05:39:57 -0700 | [diff] [blame] | 1925 | // Temp workaround to prevent false positive errors |
| 1926 | // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/2450 |
| 1927 | if (src->multiple_entry_points) { |
| 1928 | return skip; |
| 1929 | } |
| 1930 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1931 | // Validate directly off the offsets. this isn't quite correct for arrays and matrices, but is a good first step. |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1932 | const auto *entrypoint = FindEntrypointStruct(src, pStage->pName, pStage->stage); |
| 1933 | if (!entrypoint || !entrypoint->push_constant_used_in_shader.IsUsed()) { |
| 1934 | return skip; |
| 1935 | } |
| 1936 | std::vector<VkPushConstantRange> const *push_constant_ranges = pipeline.pipeline_layout->push_constant_ranges.get(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1937 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1938 | bool found_stage = false; |
| 1939 | for (auto const &range : *push_constant_ranges) { |
| 1940 | if (range.stageFlags & pStage->stage) { |
| 1941 | found_stage = true; |
| 1942 | std::string location_desc; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1943 | std::vector<uint8_t> push_constant_bytes_set; |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1944 | if (range.offset > 0) { |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1945 | push_constant_bytes_set.resize(range.offset, PC_Byte_Not_Set); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1946 | } |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1947 | push_constant_bytes_set.resize(range.offset + range.size, PC_Byte_Updated); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1948 | uint32_t issue_index = 0; |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1949 | const auto ret = |
| 1950 | ValidatePushConstantSetUpdate(push_constant_bytes_set, entrypoint->push_constant_used_in_shader, issue_index); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1951 | |
locke-lunarg | 3d8b8f3 | 2020-10-26 17:04:16 -0600 | [diff] [blame] | 1952 | if (ret == PC_Byte_Not_Set) { |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1953 | const auto loc_descr = entrypoint->push_constant_used_in_shader.GetLocationDesc(issue_index); |
| 1954 | LogObjectList objlist(src->vk_shader_module); |
| 1955 | objlist.add(pipeline.pipeline_layout->layout); |
| 1956 | skip |= LogError(objlist, kVUID_Core_Shader_PushConstantOutOfRange, |
| 1957 | "Push-constant buffer:%s in %s is out of range in %s.", loc_descr.c_str(), |
| 1958 | string_VkShaderStageFlags(pStage->stage).c_str(), |
| 1959 | report_data->FormatHandle(pipeline.pipeline_layout->layout).c_str()); |
| 1960 | break; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | } |
| 1964 | |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 1965 | if (!found_stage) { |
| 1966 | LogObjectList objlist(src->vk_shader_module); |
| 1967 | objlist.add(pipeline.pipeline_layout->layout); |
| 1968 | skip |= LogError( |
| 1969 | objlist, kVUID_Core_Shader_PushConstantOutOfRange, "Push constant is used in %s of %s. But %s doesn't set %s.", |
| 1970 | string_VkShaderStageFlags(pStage->stage).c_str(), report_data->FormatHandle(src->vk_shader_module).c_str(), |
| 1971 | report_data->FormatHandle(pipeline.pipeline_layout->layout).c_str(), string_VkShaderStageFlags(pStage->stage).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1972 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 1973 | return skip; |
| 1974 | } |
| 1975 | |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 1976 | bool CoreChecks::ValidateBuiltinLimits(SHADER_MODULE_STATE const *src, const layer_data::unordered_set<uint32_t> &accessible_ids, |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 1977 | VkShaderStageFlagBits stage) const { |
| 1978 | bool skip = false; |
| 1979 | |
| 1980 | // Currently all builtin tested are only found in fragment shaders |
| 1981 | if (stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 1982 | return skip; |
| 1983 | } |
| 1984 | |
| 1985 | for (const auto id : accessible_ids) { |
| 1986 | auto insn = src->get_def(id); |
| 1987 | const decoration_set decorations = src->get_decorations(insn.word(2)); |
| 1988 | |
| 1989 | // Built-ins are obtained from OpVariable |
| 1990 | if (((decorations.flags & decoration_set::builtin_bit) != 0) && (insn.opcode() == spv::OpVariable)) { |
| 1991 | auto type_pointer = src->get_def(insn.word(1)); |
| 1992 | assert(type_pointer.opcode() == spv::OpTypePointer); |
| 1993 | |
| 1994 | auto type = src->get_def(type_pointer.word(3)); |
| 1995 | if (type.opcode() == spv::OpTypeArray) { |
| 1996 | uint32_t length = static_cast<uint32_t>(GetConstantValue(src, type.word(3))); |
| 1997 | |
| 1998 | switch (decorations.builtin) { |
| 1999 | case spv::BuiltInSampleMask: |
| 2000 | // Handles both the input and output sampleMask |
| 2001 | if (length > phys_dev_props.limits.maxSampleMaskWords) { |
| 2002 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-maxSampleMaskWords-00711", |
| 2003 | "vkCreateGraphicsPipelines(): The BuiltIns SampleMask array sizes is %u which exceeds " |
| 2004 | "maxSampleMaskWords of %u in %s.", |
| 2005 | length, phys_dev_props.limits.maxSampleMaskWords, |
| 2006 | report_data->FormatHandle(src->vk_shader_module).c_str()); |
| 2007 | } |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | return skip; |
| 2015 | } |
| 2016 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2017 | // Validate that data for each specialization entry is fully contained within the buffer. |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 2018 | bool CoreChecks::ValidateSpecializationOffsets(VkPipelineShaderStageCreateInfo const *info) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2019 | bool skip = false; |
| 2020 | |
| 2021 | VkSpecializationInfo const *spec = info->pSpecializationInfo; |
| 2022 | |
| 2023 | if (spec) { |
| 2024 | for (auto i = 0u; i < spec->mapEntryCount; i++) { |
Jeremy Hayes | 6c555c3 | 2019-09-09 17:14:09 -0600 | [diff] [blame] | 2025 | if (spec->pMapEntries[i].offset >= spec->dataSize) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2026 | skip |= LogError(device, "VUID-VkSpecializationInfo-offset-00773", |
| 2027 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
| 2028 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..", |
| 2029 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
| 2030 | spec->pMapEntries[i].offset + spec->dataSize - 1, spec->dataSize); |
Jeremy Hayes | 6c555c3 | 2019-09-09 17:14:09 -0600 | [diff] [blame] | 2031 | |
| 2032 | continue; |
| 2033 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2034 | if (spec->pMapEntries[i].offset + spec->pMapEntries[i].size > spec->dataSize) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2035 | skip |= LogError(device, "VUID-VkSpecializationInfo-pMapEntries-00774", |
| 2036 | "Specialization entry %u (for constant id %u) references memory outside provided specialization " |
| 2037 | "data (bytes %u.." PRINTF_SIZE_T_SPECIFIER "; " PRINTF_SIZE_T_SPECIFIER " bytes provided)..", |
| 2038 | i, spec->pMapEntries[i].constantID, spec->pMapEntries[i].offset, |
| 2039 | spec->pMapEntries[i].offset + spec->pMapEntries[i].size - 1, spec->dataSize); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | return skip; |
| 2045 | } |
| 2046 | |
Jeff Bolz | 38b3ce7 | 2018-09-19 12:53:38 -0500 | [diff] [blame] | 2047 | // TODO (jbolz): Can this return a const reference? |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2048 | static std::set<uint32_t> TypeToDescriptorTypeSet(SHADER_MODULE_STATE const *module, uint32_t type_id, unsigned &descriptor_count, |
| 2049 | bool is_khr) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2050 | auto type = module->get_def(type_id); |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2051 | bool is_storage_buffer = false; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2052 | descriptor_count = 1; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2053 | std::set<uint32_t> ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2054 | |
| 2055 | // 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] | 2056 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray) { |
| 2057 | if (type.opcode() == spv::OpTypeRuntimeArray) { |
| 2058 | descriptor_count = 0; |
| 2059 | type = module->get_def(type.word(2)); |
| 2060 | } else if (type.opcode() == spv::OpTypeArray) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 2061 | descriptor_count *= GetConstantValue(module, type.word(3)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2062 | type = module->get_def(type.word(2)); |
| 2063 | } else { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2064 | if (type.word(2) == spv::StorageClassStorageBuffer) { |
| 2065 | is_storage_buffer = true; |
| 2066 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2067 | type = module->get_def(type.word(3)); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | switch (type.opcode()) { |
| 2072 | case spv::OpTypeStruct: { |
sfricke-samsung | 94d71a5 | 2021-02-26 05:25:43 -0800 | [diff] [blame] | 2073 | for (auto insn : module->decoration_inst) { |
| 2074 | if (insn.word(1) == type.word(1)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2075 | if (insn.word(2) == spv::DecorationBlock) { |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2076 | if (is_storage_buffer) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2077 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 2078 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 2079 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2080 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2081 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
| 2082 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC); |
| 2083 | ret.insert(VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT); |
| 2084 | return ret; |
Chris Forbes | 9f89d75 | 2018-03-07 12:57:48 -0800 | [diff] [blame] | 2085 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2086 | } else if (insn.word(2) == spv::DecorationBufferBlock) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2087 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); |
| 2088 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC); |
| 2089 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | // Invalid |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2095 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | case spv::OpTypeSampler: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2099 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLER); |
| 2100 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2101 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2102 | |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2103 | case spv::OpTypeSampledImage: { |
| 2104 | // Slight relaxation for some GLSL historical madness: samplerBuffer doesn't really have a sampler, and a texel |
| 2105 | // buffer descriptor doesn't really provide one. Allow this slight mismatch. |
| 2106 | auto image_type = module->get_def(type.word(2)); |
| 2107 | auto dim = image_type.word(3); |
| 2108 | auto sampled = image_type.word(7); |
| 2109 | if (dim == spv::DimBuffer && sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2110 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 2111 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2112 | } |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2113 | } |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2114 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2115 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2116 | |
| 2117 | case spv::OpTypeImage: { |
| 2118 | // Many descriptor types backing image types-- depends on dimension and whether the image will be used with a sampler. |
| 2119 | // SPIRV for Vulkan requires that sampled be 1 or 2 -- leaving the decision to runtime is unacceptable. |
| 2120 | auto dim = type.word(3); |
| 2121 | auto sampled = type.word(7); |
| 2122 | |
| 2123 | if (dim == spv::DimSubpassData) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2124 | ret.insert(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT); |
| 2125 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2126 | } else if (dim == spv::DimBuffer) { |
| 2127 | if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2128 | ret.insert(VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER); |
| 2129 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2130 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2131 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER); |
| 2132 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2133 | } |
| 2134 | } else if (sampled == 1) { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2135 | ret.insert(VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE); |
| 2136 | ret.insert(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER); |
| 2137 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2138 | } else { |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2139 | ret.insert(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE); |
| 2140 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2141 | } |
| 2142 | } |
Shannon McPherson | 0fa2823 | 2018-11-01 11:59:02 -0600 | [diff] [blame] | 2143 | case spv::OpTypeAccelerationStructureNV: |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 2144 | is_khr ? ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR) |
| 2145 | : ret.insert(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_NV); |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 2146 | return ret; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2147 | |
| 2148 | // We shouldn't really see any other junk types -- but if we do, they're a mismatch. |
| 2149 | default: |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2150 | return ret; // Matches nothing |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2154 | static std::string string_descriptorTypes(const std::set<uint32_t> &descriptor_types) { |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2155 | std::stringstream ss; |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 2156 | for (auto it = descriptor_types.begin(); it != descriptor_types.end(); ++it) { |
| 2157 | if (ss.tellp()) ss << ", "; |
| 2158 | ss << string_VkDescriptorType(VkDescriptorType(*it)); |
Chris Forbes | 73c00bf | 2018-06-22 16:28:06 -0700 | [diff] [blame] | 2159 | } |
| 2160 | return ss.str(); |
| 2161 | } |
| 2162 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2163 | bool CoreChecks::RequirePropertyFlag(VkBool32 check, char const *flag, char const *structure, const char *vuid) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2164 | if (!check) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2165 | if (LogError(device, vuid, "Shader requires flag %s set in %s but it is not set on the device", flag, structure)) { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2166 | return true; |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | return false; |
| 2171 | } |
| 2172 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2173 | bool CoreChecks::RequireFeature(VkBool32 feature, char const *feature_name, const char *vuid) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2174 | if (!feature) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2175 | if (LogError(device, vuid, "Shader requires %s but is not enabled on the device", feature_name)) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2176 | return true; |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | return false; |
| 2181 | } |
| 2182 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 2183 | bool CoreChecks::ValidateShaderStageWritableOrAtomicDescriptor(VkShaderStageFlagBits stage, bool has_writable_descriptor, |
| 2184 | bool has_atomic_descriptor) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2185 | bool skip = false; |
| 2186 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 2187 | if (has_writable_descriptor || has_atomic_descriptor) { |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2188 | switch (stage) { |
| 2189 | case VK_SHADER_STAGE_COMPUTE_BIT: |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2190 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 2191 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 2192 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 2193 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 2194 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 2195 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 2196 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 2197 | case VK_SHADER_STAGE_MESH_BIT_NV: |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2198 | /* No feature requirements for writes and atomics from compute |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2199 | * raytracing, or mesh stages */ |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2200 | break; |
| 2201 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2202 | skip |= RequireFeature(enabled_features.core.fragmentStoresAndAtomics, "fragmentStoresAndAtomics", |
| 2203 | kVUID_Core_Shader_FeatureNotEnabled); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2204 | break; |
| 2205 | default: |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2206 | skip |= RequireFeature(enabled_features.core.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics", |
| 2207 | kVUID_Core_Shader_FeatureNotEnabled); |
Chris Forbes | 349b313 | 2018-03-07 11:38:08 -0800 | [diff] [blame] | 2208 | break; |
| 2209 | } |
| 2210 | } |
| 2211 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 2212 | return skip; |
| 2213 | } |
| 2214 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 2215 | bool CoreChecks::ValidateShaderStageGroupNonUniform(SHADER_MODULE_STATE const *module, VkShaderStageFlagBits stage, |
| 2216 | spirv_inst_iter &insn) const { |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2217 | bool skip = false; |
| 2218 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 2219 | // Check anything using a group operation (which currently is only OpGroupNonUnifrom* operations) |
| 2220 | if (GroupOperation(insn.opcode()) == true) { |
| 2221 | // Check the quad operations. |
| 2222 | if ((insn.opcode() == spv::OpGroupNonUniformQuadBroadcast) || (insn.opcode() == spv::OpGroupNonUniformQuadSwap)) { |
| 2223 | if ((stage != VK_SHADER_STAGE_FRAGMENT_BIT) && (stage != VK_SHADER_STAGE_COMPUTE_BIT)) { |
| 2224 | skip |= RequireFeature(phys_dev_props_core11.subgroupQuadOperationsInAllStages, |
| 2225 | "VkPhysicalDeviceSubgroupProperties::quadOperationsInAllStages", |
| 2226 | kVUID_Core_Shader_FeatureNotEnabled); |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2227 | } |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 2228 | } |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2229 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 2230 | uint32_t scope_type = spv::ScopeMax; |
| 2231 | if (insn.opcode() == spv::OpGroupNonUniformPartitionNV) { |
| 2232 | // OpGroupNonUniformPartitionNV always assumed subgroup as missing operand |
| 2233 | scope_type = spv::ScopeSubgroup; |
| 2234 | } else { |
| 2235 | // "All <id> used for Scope <id> must be of an OpConstant" |
| 2236 | auto scope_id = module->get_def(insn.word(3)); |
| 2237 | scope_type = scope_id.word(3); |
| 2238 | } |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2239 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 2240 | if (scope_type == spv::ScopeSubgroup) { |
| 2241 | // "Group operations with subgroup scope" must have stage support |
| 2242 | const VkSubgroupFeatureFlags supported_stages = phys_dev_props_core11.subgroupSupportedStages; |
| 2243 | skip |= RequirePropertyFlag(supported_stages & stage, string_VkShaderStageFlagBits(stage), |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2244 | "VkPhysicalDeviceSubgroupProperties::supportedStages", kVUID_Core_Shader_ExceedDeviceLimit); |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | if (!enabled_features.core12.shaderSubgroupExtendedTypes) { |
| 2248 | auto type = module->get_def(insn.word(1)); |
| 2249 | |
| 2250 | if (type.opcode() == spv::OpTypeVector) { |
| 2251 | // Get the element type |
| 2252 | type = module->get_def(type.word(2)); |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2253 | } |
| 2254 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 2255 | if (type.opcode() != spv::OpTypeBool) { |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2256 | // Both OpTypeInt and OpTypeFloat the width is in the 2nd word. |
| 2257 | const uint32_t width = type.word(2); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2258 | |
sfricke-samsung | 0065ce0 | 2020-12-03 22:46:37 -0800 | [diff] [blame] | 2259 | if ((type.opcode() == spv::OpTypeFloat && width == 16) || |
| 2260 | (type.opcode() == spv::OpTypeInt && (width == 8 || width == 16 || width == 64))) { |
| 2261 | skip |= RequireFeature(enabled_features.core12.shaderSubgroupExtendedTypes, |
| 2262 | "VkPhysicalDeviceShaderSubgroupExtendedTypesFeatures::shaderSubgroupExtendedTypes", |
| 2263 | kVUID_Core_Shader_FeatureNotEnabled); |
Jeff Bolz | 526f2d5 | 2019-09-18 13:18:08 -0500 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | } |
Jeff Bolz | ee74341 | 2019-06-20 22:24:32 -0500 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | return skip; |
| 2270 | } |
| 2271 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2272 | bool CoreChecks::ValidateShaderStageInputOutputLimits(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2273 | const PIPELINE_STATE *pipeline, spirv_inst_iter entrypoint) const { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2274 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT || pStage->stage == VK_SHADER_STAGE_ALL_GRAPHICS || |
| 2275 | pStage->stage == VK_SHADER_STAGE_ALL) { |
| 2276 | return false; |
| 2277 | } |
| 2278 | |
| 2279 | bool skip = false; |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 2280 | auto const &limits = phys_dev_props.limits; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2281 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2282 | std::set<uint32_t> patch_i_ds; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2283 | struct Variable { |
| 2284 | uint32_t baseTypePtrID; |
| 2285 | uint32_t ID; |
| 2286 | uint32_t storageClass; |
| 2287 | }; |
| 2288 | std::vector<Variable> variables; |
| 2289 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2290 | uint32_t num_vertices = 0; |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 2291 | bool is_iso_lines = false; |
| 2292 | bool is_point_mode = false; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2293 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2294 | auto entrypoint_variables = FindEntrypointInterfaces(entrypoint); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2295 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2296 | for (auto insn : *src) { |
| 2297 | switch (insn.opcode()) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2298 | // Find all Patch decorations |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2299 | case spv::OpDecorate: |
| 2300 | switch (insn.word(2)) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2301 | case spv::DecorationPatch: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2302 | patch_i_ds.insert(insn.word(1)); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2303 | break; |
| 2304 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2305 | default: |
| 2306 | break; |
| 2307 | } |
| 2308 | break; |
| 2309 | // Find all input and output variables |
| 2310 | case spv::OpVariable: { |
| 2311 | Variable var = {}; |
| 2312 | var.storageClass = insn.word(3); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2313 | if ((var.storageClass == spv::StorageClassInput || var.storageClass == spv::StorageClassOutput) && |
| 2314 | // Only include variables in the entrypoint's interface |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2315 | find(entrypoint_variables.begin(), entrypoint_variables.end(), insn.word(2)) != entrypoint_variables.end()) { |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2316 | var.baseTypePtrID = insn.word(1); |
| 2317 | var.ID = insn.word(2); |
| 2318 | variables.push_back(var); |
| 2319 | } |
| 2320 | break; |
| 2321 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2322 | case spv::OpExecutionMode: |
| 2323 | if (insn.word(1) == entrypoint.word(2)) { |
| 2324 | switch (insn.word(2)) { |
| 2325 | default: |
| 2326 | break; |
| 2327 | case spv::ExecutionModeOutputVertices: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2328 | num_vertices = insn.word(3); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2329 | break; |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 2330 | case spv::ExecutionModeIsolines: |
| 2331 | is_iso_lines = true; |
| 2332 | break; |
| 2333 | case spv::ExecutionModePointMode: |
| 2334 | is_point_mode = true; |
| 2335 | break; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2336 | } |
| 2337 | } |
| 2338 | break; |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2339 | default: |
| 2340 | break; |
| 2341 | } |
| 2342 | } |
| 2343 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2344 | bool strip_output_array_level = |
| 2345 | (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || pStage->stage == VK_SHADER_STAGE_MESH_BIT_NV); |
| 2346 | bool strip_input_array_level = |
| 2347 | (pStage->stage == VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT || |
| 2348 | pStage->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || pStage->stage == VK_SHADER_STAGE_GEOMETRY_BIT); |
| 2349 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2350 | uint32_t num_comp_in = 0, num_comp_out = 0; |
| 2351 | int max_comp_in = 0, max_comp_out = 0; |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2352 | |
| 2353 | auto inputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassInput, strip_input_array_level); |
| 2354 | auto outputs = CollectInterfaceByLocation(src, entrypoint, spv::StorageClassOutput, strip_output_array_level); |
| 2355 | |
| 2356 | // Find max component location used for input variables. |
| 2357 | for (auto &var : inputs) { |
| 2358 | int location = var.first.first; |
| 2359 | int component = var.first.second; |
| 2360 | interface_var &iv = var.second; |
| 2361 | |
| 2362 | // Only need to look at the first location, since we use the type's whole size |
| 2363 | if (iv.offset != 0) { |
| 2364 | continue; |
| 2365 | } |
| 2366 | |
| 2367 | if (iv.is_patch) { |
| 2368 | continue; |
| 2369 | } |
| 2370 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2371 | int num_components = GetComponentsConsumedByType(src, iv.type_id, strip_input_array_level); |
| 2372 | max_comp_in = std::max(max_comp_in, location * 4 + component + num_components); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | // Find max component location used for output variables. |
| 2376 | for (auto &var : outputs) { |
| 2377 | int location = var.first.first; |
| 2378 | int component = var.first.second; |
| 2379 | interface_var &iv = var.second; |
| 2380 | |
| 2381 | // Only need to look at the first location, since we use the type's whole size |
| 2382 | if (iv.offset != 0) { |
| 2383 | continue; |
| 2384 | } |
| 2385 | |
| 2386 | if (iv.is_patch) { |
| 2387 | continue; |
| 2388 | } |
| 2389 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2390 | int num_components = GetComponentsConsumedByType(src, iv.type_id, strip_output_array_level); |
| 2391 | max_comp_out = std::max(max_comp_out, location * 4 + component + num_components); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | // XXX TODO: Would be nice to rewrite this to use CollectInterfaceByLocation (or something similar), |
| 2395 | // but that doesn't include builtins. |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2396 | for (auto &var : variables) { |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2397 | // Check if the variable is a patch. Patches can also be members of blocks, |
| 2398 | // but if they are then the top-level arrayness has already been stripped |
| 2399 | // by the time GetComponentsConsumedByType gets to it. |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2400 | bool is_patch = patch_i_ds.find(var.ID) != patch_i_ds.end(); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2401 | |
| 2402 | if (var.storageClass == spv::StorageClassInput) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2403 | num_comp_in += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_input_array_level && !is_patch); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2404 | } else { // var.storageClass == spv::StorageClassOutput |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2405 | num_comp_out += GetComponentsConsumedByType(src, var.baseTypePtrID, strip_output_array_level && !is_patch); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | switch (pStage->stage) { |
| 2410 | case VK_SHADER_STAGE_VERTEX_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2411 | if (num_comp_out > limits.maxVertexOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2412 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2413 | "Invalid Pipeline CreateInfo State: Vertex shader exceeds " |
| 2414 | "VkPhysicalDeviceLimits::maxVertexOutputComponents of %u " |
| 2415 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2416 | limits.maxVertexOutputComponents, num_comp_out - limits.maxVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2417 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2418 | if (max_comp_out > static_cast<int>(limits.maxVertexOutputComponents)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2419 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2420 | "Invalid Pipeline CreateInfo State: Vertex shader output variable uses location that " |
| 2421 | "exceeds component limit VkPhysicalDeviceLimits::maxVertexOutputComponents (%u)", |
| 2422 | limits.maxVertexOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2423 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2424 | break; |
| 2425 | |
| 2426 | case VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2427 | if (num_comp_in > limits.maxTessellationControlPerVertexInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2428 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2429 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 2430 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents of %u " |
| 2431 | "components by %u components", |
| 2432 | limits.maxTessellationControlPerVertexInputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2433 | num_comp_in - limits.maxTessellationControlPerVertexInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2434 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2435 | if (max_comp_in > static_cast<int>(limits.maxTessellationControlPerVertexInputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2436 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2437 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2438 | "Invalid Pipeline CreateInfo State: Tessellation control shader input variable uses location that " |
| 2439 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexInputComponents (%u)", |
| 2440 | limits.maxTessellationControlPerVertexInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2441 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2442 | if (num_comp_out > limits.maxTessellationControlPerVertexOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2443 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2444 | "Invalid Pipeline CreateInfo State: Tessellation control shader exceeds " |
| 2445 | "VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents of %u " |
| 2446 | "components by %u components", |
| 2447 | limits.maxTessellationControlPerVertexOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2448 | num_comp_out - limits.maxTessellationControlPerVertexOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2449 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2450 | if (max_comp_out > static_cast<int>(limits.maxTessellationControlPerVertexOutputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2451 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2452 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2453 | "Invalid Pipeline CreateInfo State: Tessellation control shader output variable uses location that " |
| 2454 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationControlPerVertexOutputComponents (%u)", |
| 2455 | limits.maxTessellationControlPerVertexOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2456 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2457 | break; |
| 2458 | |
| 2459 | case VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2460 | if (num_comp_in > limits.maxTessellationEvaluationInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2461 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2462 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 2463 | "VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents of %u " |
| 2464 | "components by %u components", |
| 2465 | limits.maxTessellationEvaluationInputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2466 | num_comp_in - limits.maxTessellationEvaluationInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2467 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2468 | if (max_comp_in > static_cast<int>(limits.maxTessellationEvaluationInputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2469 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2470 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2471 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader input variable uses location that " |
| 2472 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationInputComponents (%u)", |
| 2473 | limits.maxTessellationEvaluationInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2474 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2475 | if (num_comp_out > limits.maxTessellationEvaluationOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2476 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2477 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader exceeds " |
| 2478 | "VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents of %u " |
| 2479 | "components by %u components", |
| 2480 | limits.maxTessellationEvaluationOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2481 | num_comp_out - limits.maxTessellationEvaluationOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2482 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2483 | if (max_comp_out > static_cast<int>(limits.maxTessellationEvaluationOutputComponents)) { |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2484 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2485 | LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2486 | "Invalid Pipeline CreateInfo State: Tessellation evaluation shader output variable uses location that " |
| 2487 | "exceeds component limit VkPhysicalDeviceLimits::maxTessellationEvaluationOutputComponents (%u)", |
| 2488 | limits.maxTessellationEvaluationOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2489 | } |
Nathaniel Cesario | 75fb722 | 2020-12-07 10:54:53 -0700 | [diff] [blame] | 2490 | // Portability validation |
| 2491 | if (IsExtEnabled(device_extensions.vk_khr_portability_subset)) { |
| 2492 | if (is_iso_lines && (VK_FALSE == enabled_features.portability_subset_features.tessellationIsolines)) { |
| 2493 | skip |= LogError(pipeline->pipeline, kVUID_Portability_Tessellation_Isolines, |
| 2494 | "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader" |
| 2495 | " is using abstract patch type IsoLines, but this is not supported on this platform"); |
| 2496 | } |
| 2497 | if (is_point_mode && (VK_FALSE == enabled_features.portability_subset_features.tessellationPointMode)) { |
| 2498 | skip |= LogError(pipeline->pipeline, kVUID_Portability_Tessellation_PointMode, |
| 2499 | "Invalid Pipeline CreateInfo state (portability error): Tessellation evaluation shader" |
| 2500 | " is using abstract patch type PointMode, but this is not supported on this platform"); |
| 2501 | } |
| 2502 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2503 | break; |
| 2504 | |
| 2505 | case VK_SHADER_STAGE_GEOMETRY_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2506 | if (num_comp_in > limits.maxGeometryInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2507 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2508 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2509 | "VkPhysicalDeviceLimits::maxGeometryInputComponents of %u " |
| 2510 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2511 | limits.maxGeometryInputComponents, num_comp_in - limits.maxGeometryInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2512 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2513 | if (max_comp_in > static_cast<int>(limits.maxGeometryInputComponents)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2514 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2515 | "Invalid Pipeline CreateInfo State: Geometry shader input variable uses location that " |
| 2516 | "exceeds component limit VkPhysicalDeviceLimits::maxGeometryInputComponents (%u)", |
| 2517 | limits.maxGeometryInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2518 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2519 | if (num_comp_out > limits.maxGeometryOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2520 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2521 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2522 | "VkPhysicalDeviceLimits::maxGeometryOutputComponents of %u " |
| 2523 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2524 | limits.maxGeometryOutputComponents, num_comp_out - limits.maxGeometryOutputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2525 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2526 | if (max_comp_out > static_cast<int>(limits.maxGeometryOutputComponents)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2527 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2528 | "Invalid Pipeline CreateInfo State: Geometry shader output variable uses location that " |
| 2529 | "exceeds component limit VkPhysicalDeviceLimits::maxGeometryOutputComponents (%u)", |
| 2530 | limits.maxGeometryOutputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2531 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2532 | if (num_comp_out * num_vertices > limits.maxGeometryTotalOutputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2533 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2534 | "Invalid Pipeline CreateInfo State: Geometry shader exceeds " |
| 2535 | "VkPhysicalDeviceLimits::maxGeometryTotalOutputComponents of %u " |
| 2536 | "components by %u components", |
| 2537 | limits.maxGeometryTotalOutputComponents, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2538 | num_comp_out * num_vertices - limits.maxGeometryTotalOutputComponents); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2539 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2540 | break; |
| 2541 | |
| 2542 | case VK_SHADER_STAGE_FRAGMENT_BIT: |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2543 | if (num_comp_in > limits.maxFragmentInputComponents) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2544 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2545 | "Invalid Pipeline CreateInfo State: Fragment shader exceeds " |
| 2546 | "VkPhysicalDeviceLimits::maxFragmentInputComponents of %u " |
| 2547 | "components by %u components", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2548 | limits.maxFragmentInputComponents, num_comp_in - limits.maxFragmentInputComponents); |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2549 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2550 | if (max_comp_in > static_cast<int>(limits.maxFragmentInputComponents)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2551 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_ExceedDeviceLimit, |
| 2552 | "Invalid Pipeline CreateInfo State: Fragment shader input variable uses location that " |
| 2553 | "exceeds component limit VkPhysicalDeviceLimits::maxFragmentInputComponents (%u)", |
| 2554 | limits.maxFragmentInputComponents); |
Jeff Bolz | f234bf8 | 2019-11-04 14:07:15 -0600 | [diff] [blame] | 2555 | } |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2556 | break; |
| 2557 | |
Jeff Bolz | 148d94e | 2018-12-13 21:25:56 -0600 | [diff] [blame] | 2558 | case VK_SHADER_STAGE_RAYGEN_BIT_NV: |
| 2559 | case VK_SHADER_STAGE_ANY_HIT_BIT_NV: |
| 2560 | case VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV: |
| 2561 | case VK_SHADER_STAGE_MISS_BIT_NV: |
| 2562 | case VK_SHADER_STAGE_INTERSECTION_BIT_NV: |
| 2563 | case VK_SHADER_STAGE_CALLABLE_BIT_NV: |
| 2564 | case VK_SHADER_STAGE_TASK_BIT_NV: |
| 2565 | case VK_SHADER_STAGE_MESH_BIT_NV: |
| 2566 | break; |
| 2567 | |
Daniel Fedai Larsen | c939abc | 2018-08-07 10:01:58 +0200 | [diff] [blame] | 2568 | default: |
| 2569 | assert(false); // This should never happen |
| 2570 | } |
| 2571 | return skip; |
| 2572 | } |
| 2573 | |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 2574 | bool CoreChecks::ValidateShaderStageMaxResources(VkShaderStageFlagBits stage, const PIPELINE_STATE *pipeline) const { |
| 2575 | bool skip = false; |
| 2576 | uint32_t total_resources = 0; |
| 2577 | |
| 2578 | // Only currently testing for graphics and compute pipelines |
| 2579 | // TODO: Add check and support for Ray Tracing pipeline VUID 03428 |
| 2580 | if ((stage & (VK_SHADER_STAGE_ALL_GRAPHICS | VK_SHADER_STAGE_COMPUTE_BIT)) == 0) { |
| 2581 | return false; |
| 2582 | } |
| 2583 | |
| 2584 | if (stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 2585 | // "For the fragment shader stage the framebuffer color attachments also count against this limit" |
| 2586 | total_resources += pipeline->rp_state->createInfo.pSubpasses[pipeline->graphicsPipelineCI.subpass].colorAttachmentCount; |
| 2587 | } |
| 2588 | |
| 2589 | // TODO: This reuses a lot of GetDescriptorCountMaxPerStage but currently would need to make it agnostic in a way to handle |
| 2590 | // input from CreatePipeline and CreatePipelineLayout level |
| 2591 | for (auto set_layout : pipeline->pipeline_layout->set_layouts) { |
| 2592 | if ((set_layout->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT) != 0) { |
| 2593 | continue; |
| 2594 | } |
| 2595 | |
| 2596 | for (uint32_t binding_idx = 0; binding_idx < set_layout->GetBindingCount(); binding_idx++) { |
| 2597 | const VkDescriptorSetLayoutBinding *binding = set_layout->GetDescriptorSetLayoutBindingPtrFromIndex(binding_idx); |
| 2598 | // Bindings with a descriptorCount of 0 are "reserved" and should be skipped |
| 2599 | if (((stage & binding->stageFlags) != 0) && (binding->descriptorCount > 0)) { |
| 2600 | // Check only descriptor types listed in maxPerStageResources description in spec |
| 2601 | switch (binding->descriptorType) { |
| 2602 | case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: |
| 2603 | case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: |
| 2604 | case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE: |
| 2605 | case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: |
| 2606 | case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: |
| 2607 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER: |
| 2608 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: |
| 2609 | case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 2610 | case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: |
| 2611 | case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: |
| 2612 | total_resources += binding->descriptorCount; |
| 2613 | break; |
| 2614 | default: |
| 2615 | break; |
| 2616 | } |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | |
| 2621 | if (total_resources > phys_dev_props.limits.maxPerStageResources) { |
| 2622 | const char *vuid = (stage == VK_SHADER_STAGE_COMPUTE_BIT) ? "VUID-VkComputePipelineCreateInfo-layout-01687" |
| 2623 | : "VUID-VkGraphicsPipelineCreateInfo-layout-01688"; |
| 2624 | skip |= LogError(pipeline->pipeline, vuid, |
| 2625 | "Invalid Pipeline CreateInfo State: Shader Stage %s exceeds component limit " |
| 2626 | "VkPhysicalDeviceLimits::maxPerStageResources (%u)", |
| 2627 | string_VkShaderStageFlagBits(stage), phys_dev_props.limits.maxPerStageResources); |
| 2628 | } |
| 2629 | |
| 2630 | return skip; |
| 2631 | } |
| 2632 | |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2633 | // copy the specialization constant value into buf, if it is present |
| 2634 | void GetSpecConstantValue(VkPipelineShaderStageCreateInfo const *pStage, uint32_t spec_id, void *buf) { |
| 2635 | VkSpecializationInfo const *spec = pStage->pSpecializationInfo; |
| 2636 | |
| 2637 | if (spec && spec_id < spec->mapEntryCount) { |
| 2638 | memcpy(buf, (uint8_t *)spec->pData + spec->pMapEntries[spec_id].offset, spec->pMapEntries[spec_id].size); |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | // Fill in value with the constant or specialization constant value, if available. |
| 2643 | // Returns true if the value has been accurately filled out. |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2644 | static bool GetIntConstantValue(spirv_inst_iter insn, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 2645 | const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id, uint32_t *value) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2646 | auto type_id = src->get_def(insn.word(1)); |
| 2647 | if (type_id.opcode() != spv::OpTypeInt || type_id.word(2) != 32) { |
| 2648 | return false; |
| 2649 | } |
| 2650 | switch (insn.opcode()) { |
| 2651 | case spv::OpSpecConstant: |
| 2652 | *value = insn.word(3); |
| 2653 | GetSpecConstantValue(pStage, id_to_spec_id.at(insn.word(2)), value); |
| 2654 | return true; |
| 2655 | case spv::OpConstant: |
| 2656 | *value = insn.word(3); |
| 2657 | return true; |
| 2658 | default: |
| 2659 | return false; |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | // Map SPIR-V type to VK_COMPONENT_TYPE enum |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2664 | VkComponentTypeNV GetComponentType(spirv_inst_iter insn, SHADER_MODULE_STATE const *src) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2665 | switch (insn.opcode()) { |
| 2666 | case spv::OpTypeInt: |
| 2667 | switch (insn.word(2)) { |
| 2668 | case 8: |
| 2669 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT8_NV : VK_COMPONENT_TYPE_UINT8_NV; |
| 2670 | case 16: |
| 2671 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT16_NV : VK_COMPONENT_TYPE_UINT16_NV; |
| 2672 | case 32: |
| 2673 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT32_NV : VK_COMPONENT_TYPE_UINT32_NV; |
| 2674 | case 64: |
| 2675 | return insn.word(3) != 0 ? VK_COMPONENT_TYPE_SINT64_NV : VK_COMPONENT_TYPE_UINT64_NV; |
| 2676 | default: |
| 2677 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2678 | } |
| 2679 | case spv::OpTypeFloat: |
| 2680 | switch (insn.word(2)) { |
| 2681 | case 16: |
| 2682 | return VK_COMPONENT_TYPE_FLOAT16_NV; |
| 2683 | case 32: |
| 2684 | return VK_COMPONENT_TYPE_FLOAT32_NV; |
| 2685 | case 64: |
| 2686 | return VK_COMPONENT_TYPE_FLOAT64_NV; |
| 2687 | default: |
| 2688 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2689 | } |
| 2690 | default: |
| 2691 | return VK_COMPONENT_TYPE_MAX_ENUM_NV; |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | // Validate SPV_NV_cooperative_matrix behavior that can't be statically validated |
| 2696 | // in SPIRV-Tools (e.g. due to specialization constant usage). |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2697 | bool CoreChecks::ValidateCooperativeMatrix(SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2698 | const PIPELINE_STATE *pipeline) const { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2699 | bool skip = false; |
| 2700 | |
| 2701 | // Map SPIR-V result ID to specialization constant id (SpecId decoration value) |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 2702 | layer_data::unordered_map<uint32_t, uint32_t> id_to_spec_id; |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2703 | // Map SPIR-V result ID to the ID of its type. |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 2704 | layer_data::unordered_map<uint32_t, uint32_t> id_to_type_id; |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2705 | |
| 2706 | struct CoopMatType { |
| 2707 | uint32_t scope, rows, cols; |
| 2708 | VkComponentTypeNV component_type; |
| 2709 | bool all_constant; |
| 2710 | |
| 2711 | CoopMatType() : scope(0), rows(0), cols(0), component_type(VK_COMPONENT_TYPE_MAX_ENUM_NV), all_constant(false) {} |
| 2712 | |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 2713 | void Init(uint32_t id, SHADER_MODULE_STATE const *src, VkPipelineShaderStageCreateInfo const *pStage, |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 2714 | const layer_data::unordered_map<uint32_t, uint32_t> &id_to_spec_id) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2715 | spirv_inst_iter insn = src->get_def(id); |
| 2716 | uint32_t component_type_id = insn.word(2); |
| 2717 | uint32_t scope_id = insn.word(3); |
| 2718 | uint32_t rows_id = insn.word(4); |
| 2719 | uint32_t cols_id = insn.word(5); |
| 2720 | auto component_type_iter = src->get_def(component_type_id); |
| 2721 | auto scope_iter = src->get_def(scope_id); |
| 2722 | auto rows_iter = src->get_def(rows_id); |
| 2723 | auto cols_iter = src->get_def(cols_id); |
| 2724 | |
| 2725 | all_constant = true; |
| 2726 | if (!GetIntConstantValue(scope_iter, src, pStage, id_to_spec_id, &scope)) { |
| 2727 | all_constant = false; |
| 2728 | } |
| 2729 | if (!GetIntConstantValue(rows_iter, src, pStage, id_to_spec_id, &rows)) { |
| 2730 | all_constant = false; |
| 2731 | } |
| 2732 | if (!GetIntConstantValue(cols_iter, src, pStage, id_to_spec_id, &cols)) { |
| 2733 | all_constant = false; |
| 2734 | } |
| 2735 | component_type = GetComponentType(component_type_iter, src); |
| 2736 | } |
| 2737 | }; |
| 2738 | |
| 2739 | bool seen_coopmat_capability = false; |
| 2740 | |
| 2741 | for (auto insn : *src) { |
| 2742 | // Whitelist instructions whose result can be a cooperative matrix type, and |
| 2743 | // keep track of their types. It would be nice if SPIRV-Headers generated code |
| 2744 | // to identify which instructions have a result type and result id. Lacking that, |
| 2745 | // this whitelist is based on the set of instructions that |
| 2746 | // SPV_NV_cooperative_matrix says can be used with cooperative matrix types. |
| 2747 | switch (insn.opcode()) { |
| 2748 | case spv::OpLoad: |
| 2749 | case spv::OpCooperativeMatrixLoadNV: |
| 2750 | case spv::OpCooperativeMatrixMulAddNV: |
| 2751 | case spv::OpSNegate: |
| 2752 | case spv::OpFNegate: |
| 2753 | case spv::OpIAdd: |
| 2754 | case spv::OpFAdd: |
| 2755 | case spv::OpISub: |
| 2756 | case spv::OpFSub: |
| 2757 | case spv::OpFDiv: |
| 2758 | case spv::OpSDiv: |
| 2759 | case spv::OpUDiv: |
| 2760 | case spv::OpMatrixTimesScalar: |
| 2761 | case spv::OpConstantComposite: |
| 2762 | case spv::OpCompositeConstruct: |
| 2763 | case spv::OpConvertFToU: |
| 2764 | case spv::OpConvertFToS: |
| 2765 | case spv::OpConvertSToF: |
| 2766 | case spv::OpConvertUToF: |
| 2767 | case spv::OpUConvert: |
| 2768 | case spv::OpSConvert: |
| 2769 | case spv::OpFConvert: |
| 2770 | id_to_type_id[insn.word(2)] = insn.word(1); |
| 2771 | break; |
| 2772 | default: |
| 2773 | break; |
| 2774 | } |
| 2775 | |
| 2776 | switch (insn.opcode()) { |
| 2777 | case spv::OpDecorate: |
| 2778 | if (insn.word(2) == spv::DecorationSpecId) { |
| 2779 | id_to_spec_id[insn.word(1)] = insn.word(3); |
| 2780 | } |
| 2781 | break; |
| 2782 | case spv::OpCapability: |
| 2783 | if (insn.word(1) == spv::CapabilityCooperativeMatrixNV) { |
| 2784 | seen_coopmat_capability = true; |
| 2785 | |
| 2786 | if (!(pStage->stage & phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2787 | skip |= LogError( |
| 2788 | pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixSupportedStages, |
| 2789 | "OpTypeCooperativeMatrixNV used in shader stage not in cooperativeMatrixSupportedStages (= %u)", |
| 2790 | phys_dev_ext_props.cooperative_matrix_props.cooperativeMatrixSupportedStages); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | break; |
| 2794 | case spv::OpMemoryModel: |
| 2795 | // If the capability isn't enabled, don't bother with the rest of this function. |
| 2796 | // OpMemoryModel is the first required instruction after all OpCapability instructions. |
| 2797 | if (!seen_coopmat_capability) { |
| 2798 | return skip; |
| 2799 | } |
| 2800 | break; |
| 2801 | case spv::OpTypeCooperativeMatrixNV: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2802 | CoopMatType m; |
| 2803 | m.Init(insn.word(1), src, pStage, id_to_spec_id); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2804 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2805 | if (m.all_constant) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2806 | // Validate that the type parameters are all supported for one of the |
| 2807 | // operands of a cooperative matrix property. |
| 2808 | bool valid = false; |
| 2809 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2810 | if (cooperative_matrix_properties[i].AType == m.component_type && |
| 2811 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].KSize == m.cols && |
| 2812 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2813 | valid = true; |
| 2814 | break; |
| 2815 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2816 | if (cooperative_matrix_properties[i].BType == m.component_type && |
| 2817 | cooperative_matrix_properties[i].KSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 2818 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2819 | valid = true; |
| 2820 | break; |
| 2821 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2822 | if (cooperative_matrix_properties[i].CType == m.component_type && |
| 2823 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 2824 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2825 | valid = true; |
| 2826 | break; |
| 2827 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2828 | if (cooperative_matrix_properties[i].DType == m.component_type && |
| 2829 | cooperative_matrix_properties[i].MSize == m.rows && cooperative_matrix_properties[i].NSize == m.cols && |
| 2830 | cooperative_matrix_properties[i].scope == m.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2831 | valid = true; |
| 2832 | break; |
| 2833 | } |
| 2834 | } |
| 2835 | if (!valid) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2836 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixType, |
| 2837 | "OpTypeCooperativeMatrixNV (result id = %u) operands don't match a supported matrix type", |
| 2838 | insn.word(1)); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | break; |
| 2842 | } |
| 2843 | case spv::OpCooperativeMatrixMulAddNV: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2844 | CoopMatType a, b, c, d; |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2845 | if (id_to_type_id.find(insn.word(2)) == id_to_type_id.end() || |
| 2846 | id_to_type_id.find(insn.word(3)) == id_to_type_id.end() || |
| 2847 | id_to_type_id.find(insn.word(4)) == id_to_type_id.end() || |
| 2848 | id_to_type_id.find(insn.word(5)) == id_to_type_id.end()) { |
Mike Schuchardt | e48dc14 | 2019-04-18 09:12:03 -0700 | [diff] [blame] | 2849 | // Couldn't find type of matrix |
| 2850 | assert(false); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2851 | break; |
| 2852 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2853 | d.Init(id_to_type_id[insn.word(2)], src, pStage, id_to_spec_id); |
| 2854 | a.Init(id_to_type_id[insn.word(3)], src, pStage, id_to_spec_id); |
| 2855 | b.Init(id_to_type_id[insn.word(4)], src, pStage, id_to_spec_id); |
| 2856 | c.Init(id_to_type_id[insn.word(5)], src, pStage, id_to_spec_id); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2857 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2858 | if (a.all_constant && b.all_constant && c.all_constant && d.all_constant) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2859 | // Validate that the type parameters are all supported for the same |
| 2860 | // cooperative matrix property. |
| 2861 | bool valid = false; |
| 2862 | for (unsigned i = 0; i < cooperative_matrix_properties.size(); ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2863 | if (cooperative_matrix_properties[i].AType == a.component_type && |
| 2864 | cooperative_matrix_properties[i].MSize == a.rows && cooperative_matrix_properties[i].KSize == a.cols && |
| 2865 | cooperative_matrix_properties[i].scope == a.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2866 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2867 | cooperative_matrix_properties[i].BType == b.component_type && |
| 2868 | cooperative_matrix_properties[i].KSize == b.rows && cooperative_matrix_properties[i].NSize == b.cols && |
| 2869 | cooperative_matrix_properties[i].scope == b.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2870 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2871 | cooperative_matrix_properties[i].CType == c.component_type && |
| 2872 | cooperative_matrix_properties[i].MSize == c.rows && cooperative_matrix_properties[i].NSize == c.cols && |
| 2873 | cooperative_matrix_properties[i].scope == c.scope && |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2874 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2875 | cooperative_matrix_properties[i].DType == d.component_type && |
| 2876 | cooperative_matrix_properties[i].MSize == d.rows && cooperative_matrix_properties[i].NSize == d.cols && |
| 2877 | cooperative_matrix_properties[i].scope == d.scope) { |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2878 | valid = true; |
| 2879 | break; |
| 2880 | } |
| 2881 | } |
| 2882 | if (!valid) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2883 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_CooperativeMatrixMulAdd, |
| 2884 | "OpCooperativeMatrixMulAddNV (result id = %u) operands don't match a supported matrix " |
| 2885 | "VkCooperativeMatrixPropertiesNV", |
| 2886 | insn.word(2)); |
Jeff Bolz | e435675 | 2019-03-07 11:23:46 -0600 | [diff] [blame] | 2887 | } |
| 2888 | } |
| 2889 | break; |
| 2890 | } |
| 2891 | default: |
| 2892 | break; |
| 2893 | } |
| 2894 | } |
| 2895 | |
| 2896 | return skip; |
| 2897 | } |
| 2898 | |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 2899 | bool CoreChecks::ValidateExecutionModes(SHADER_MODULE_STATE const *src, spirv_inst_iter entrypoint) const { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2900 | auto entrypoint_id = entrypoint.word(2); |
| 2901 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2902 | // The first denorm execution mode encountered, along with its bit width. |
| 2903 | // Used to check if SeparateDenormSettings is respected. |
| 2904 | std::pair<spv::ExecutionMode, uint32_t> first_denorm_execution_mode = std::make_pair(spv::ExecutionModeMax, 0); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2905 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2906 | // The first rounding mode encountered, along with its bit width. |
| 2907 | // Used to check if SeparateRoundingModeSettings is respected. |
| 2908 | std::pair<spv::ExecutionMode, uint32_t> first_rounding_mode = std::make_pair(spv::ExecutionModeMax, 0); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2909 | |
| 2910 | bool skip = false; |
| 2911 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 2912 | uint32_t vertices_out = 0; |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 2913 | uint32_t invocations = 0; |
| 2914 | |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 2915 | auto it = src->execution_mode_inst.find(entrypoint_id); |
| 2916 | if (it != src->execution_mode_inst.end()) { |
| 2917 | for (auto insn : it->second) { |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2918 | auto mode = insn.word(2); |
| 2919 | switch (mode) { |
| 2920 | case spv::ExecutionModeSignedZeroInfNanPreserve: { |
| 2921 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2922 | if ((bit_width == 16 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat16) || |
| 2923 | (bit_width == 32 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat32) || |
| 2924 | (bit_width == 64 && !phys_dev_props_core12.shaderSignedZeroInfNanPreserveFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2925 | skip |= LogError( |
| 2926 | device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2927 | "Shader requires SignedZeroInfNanPreserve for bit width %d but it is not enabled on the device", |
| 2928 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2929 | } |
| 2930 | break; |
| 2931 | } |
| 2932 | |
| 2933 | case spv::ExecutionModeDenormPreserve: { |
| 2934 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2935 | if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormPreserveFloat16) || |
| 2936 | (bit_width == 32 && !phys_dev_props_core12.shaderDenormPreserveFloat32) || |
| 2937 | (bit_width == 64 && !phys_dev_props_core12.shaderDenormPreserveFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2938 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2939 | "Shader requires DenormPreserve for bit width %d but it is not enabled on the device", |
| 2940 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2943 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 2944 | // Register the first denorm execution mode found |
| 2945 | first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2946 | } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) { |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2947 | switch (phys_dev_props_core12.denormBehaviorIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2948 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2949 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2950 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2951 | "Shader uses different denorm execution modes for 16 and 64-bit but " |
| 2952 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2953 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2954 | } |
| 2955 | break; |
| 2956 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2957 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2958 | break; |
| 2959 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2960 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2961 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2962 | "Shader uses different denorm execution modes for different bit widths but " |
| 2963 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2964 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2965 | break; |
| 2966 | |
| 2967 | default: |
| 2968 | break; |
| 2969 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2970 | } |
| 2971 | break; |
| 2972 | } |
| 2973 | |
| 2974 | case spv::ExecutionModeDenormFlushToZero: { |
| 2975 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2976 | if ((bit_width == 16 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat16) || |
| 2977 | (bit_width == 32 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat32) || |
| 2978 | (bit_width == 64 && !phys_dev_props_core12.shaderDenormFlushToZeroFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2979 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2980 | "Shader requires DenormFlushToZero for bit width %d but it is not enabled on the device", |
| 2981 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 2984 | if (first_denorm_execution_mode.first == spv::ExecutionModeMax) { |
| 2985 | // Register the first denorm execution mode found |
| 2986 | first_denorm_execution_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2987 | } else if (first_denorm_execution_mode.first != mode && first_denorm_execution_mode.second != bit_width) { |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 2988 | switch (phys_dev_props_core12.denormBehaviorIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2989 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2990 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 2991 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 2992 | "Shader uses different denorm execution modes for 16 and 64-bit but " |
| 2993 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2994 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2995 | } |
| 2996 | break; |
| 2997 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 2998 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 2999 | break; |
| 3000 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3001 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3002 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3003 | "Shader uses different denorm execution modes for different bit widths but " |
| 3004 | "denormBehaviorIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3005 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3006 | break; |
| 3007 | |
| 3008 | default: |
| 3009 | break; |
| 3010 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3011 | } |
| 3012 | break; |
| 3013 | } |
| 3014 | |
| 3015 | case spv::ExecutionModeRoundingModeRTE: { |
| 3016 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3017 | if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTEFloat16) || |
| 3018 | (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTEFloat32) || |
| 3019 | (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTEFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3020 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3021 | "Shader requires RoundingModeRTE for bit width %d but it is not enabled on the device", |
| 3022 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3025 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 3026 | // Register the first rounding mode found |
| 3027 | first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3028 | } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) { |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3029 | switch (phys_dev_props_core12.roundingModeIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3030 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3031 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3032 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3033 | "Shader uses different rounding modes for 16 and 64-bit but " |
| 3034 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3035 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3036 | } |
| 3037 | break; |
| 3038 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3039 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3040 | break; |
| 3041 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3042 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3043 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3044 | "Shader uses different rounding modes for different bit widths but " |
| 3045 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3046 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3047 | break; |
| 3048 | |
| 3049 | default: |
| 3050 | break; |
| 3051 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3052 | } |
| 3053 | break; |
| 3054 | } |
| 3055 | |
| 3056 | case spv::ExecutionModeRoundingModeRTZ: { |
| 3057 | auto bit_width = insn.word(3); |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3058 | if ((bit_width == 16 && !phys_dev_props_core12.shaderRoundingModeRTZFloat16) || |
| 3059 | (bit_width == 32 && !phys_dev_props_core12.shaderRoundingModeRTZFloat32) || |
| 3060 | (bit_width == 64 && !phys_dev_props_core12.shaderRoundingModeRTZFloat64)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3061 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3062 | "Shader requires RoundingModeRTZ for bit width %d but it is not enabled on the device", |
| 3063 | bit_width); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
Attilio Provenzano | f6c0e85 | 2019-04-09 11:01:18 +0100 | [diff] [blame] | 3066 | if (first_rounding_mode.first == spv::ExecutionModeMax) { |
| 3067 | // Register the first rounding mode found |
| 3068 | first_rounding_mode = std::make_pair(static_cast<spv::ExecutionMode>(mode), bit_width); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3069 | } else if (first_rounding_mode.first != mode && first_rounding_mode.second != bit_width) { |
Piers Daniell | 41b8c5d | 2020-01-10 15:42:00 -0700 | [diff] [blame] | 3070 | switch (phys_dev_props_core12.roundingModeIndependence) { |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3071 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3072 | if (first_rounding_mode.second != 32 && bit_width != 32) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3073 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3074 | "Shader uses different rounding modes for 16 and 64-bit but " |
| 3075 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3076 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3077 | } |
| 3078 | break; |
| 3079 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3080 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL: |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3081 | break; |
| 3082 | |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3083 | case VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE: |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3084 | skip |= LogError(device, kVUID_Core_Shader_FeatureNotEnabled, |
| 3085 | "Shader uses different rounding modes for different bit widths but " |
| 3086 | "roundingModeIndependence is " |
Mike Schuchardt | 2df0891 | 2020-12-15 16:28:09 -0800 | [diff] [blame] | 3087 | "VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_NONE on the device"); |
Jason Ekstrand | e1e06de | 2019-08-05 11:43:43 -0500 | [diff] [blame] | 3088 | break; |
| 3089 | |
| 3090 | default: |
| 3091 | break; |
| 3092 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3093 | } |
| 3094 | break; |
| 3095 | } |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3096 | |
| 3097 | case spv::ExecutionModeOutputVertices: { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3098 | vertices_out = insn.word(3); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3099 | break; |
| 3100 | } |
| 3101 | |
| 3102 | case spv::ExecutionModeInvocations: { |
| 3103 | invocations = insn.word(3); |
| 3104 | break; |
| 3105 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3106 | } |
| 3107 | } |
| 3108 | } |
| 3109 | |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3110 | if (entrypoint.word(1) == spv::ExecutionModelGeometry) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3111 | if (vertices_out == 0 || vertices_out > phys_dev_props.limits.maxGeometryOutputVertices) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3112 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00714", |
| 3113 | "Geometry shader entry point must have an OpExecutionMode instruction that " |
| 3114 | "specifies a maximum output vertex count that is greater than 0 and less " |
| 3115 | "than or equal to maxGeometryOutputVertices. " |
| 3116 | "OutputVertices=%d, maxGeometryOutputVertices=%d", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3117 | vertices_out, phys_dev_props.limits.maxGeometryOutputVertices); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | if (invocations == 0 || invocations > phys_dev_props.limits.maxGeometryShaderInvocations) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3121 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-stage-00715", |
| 3122 | "Geometry shader entry point must have an OpExecutionMode instruction that " |
| 3123 | "specifies an invocation count that is greater than 0 and less " |
| 3124 | "than or equal to maxGeometryShaderInvocations. " |
| 3125 | "Invocations=%d, maxGeometryShaderInvocations=%d", |
| 3126 | invocations, phys_dev_props.limits.maxGeometryShaderInvocations); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3127 | } |
| 3128 | } |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3129 | return skip; |
| 3130 | } |
| 3131 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 3132 | uint32_t DescriptorTypeToReqs(SHADER_MODULE_STATE const *module, uint32_t type_id) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3133 | auto type = module->get_def(type_id); |
| 3134 | |
| 3135 | while (true) { |
| 3136 | switch (type.opcode()) { |
| 3137 | case spv::OpTypeArray: |
Chris Forbes | 062f122 | 2018-08-21 15:34:15 -0700 | [diff] [blame] | 3138 | case spv::OpTypeRuntimeArray: |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3139 | case spv::OpTypeSampledImage: |
| 3140 | type = module->get_def(type.word(2)); |
| 3141 | break; |
| 3142 | case spv::OpTypePointer: |
| 3143 | type = module->get_def(type.word(3)); |
| 3144 | break; |
| 3145 | case spv::OpTypeImage: { |
| 3146 | auto dim = type.word(3); |
| 3147 | auto arrayed = type.word(5); |
| 3148 | auto msaa = type.word(6); |
| 3149 | |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3150 | uint32_t bits = 0; |
| 3151 | switch (GetFundamentalType(module, type.word(2))) { |
| 3152 | case FORMAT_TYPE_FLOAT: |
| 3153 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT; |
| 3154 | break; |
| 3155 | case FORMAT_TYPE_UINT: |
| 3156 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT; |
| 3157 | break; |
| 3158 | case FORMAT_TYPE_SINT: |
| 3159 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT; |
| 3160 | break; |
| 3161 | default: |
| 3162 | break; |
| 3163 | } |
| 3164 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3165 | switch (dim) { |
| 3166 | case spv::Dim1D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3167 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D; |
| 3168 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3169 | case spv::Dim2D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3170 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 3171 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D; |
| 3172 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3173 | case spv::Dim3D: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3174 | bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D; |
| 3175 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3176 | case spv::DimCube: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3177 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE; |
| 3178 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3179 | case spv::DimSubpassData: |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3180 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 3181 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3182 | default: // buffer, etc. |
Chris Forbes | 74ba223 | 2018-08-27 15:19:27 -0700 | [diff] [blame] | 3183 | return bits; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | default: |
| 3187 | return 0; |
| 3188 | } |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | // For given pipelineLayout verify that the set_layout_node at slot.first |
| 3193 | // has the requested binding at slot.second and return ptr to that binding |
Mark Lobodzinski | ca6ebe3 | 2019-04-25 11:43:37 -0600 | [diff] [blame] | 3194 | static VkDescriptorSetLayoutBinding const *GetDescriptorBinding(PIPELINE_LAYOUT_STATE const *pipelineLayout, |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3195 | descriptor_slot_t slot) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3196 | if (!pipelineLayout) return nullptr; |
| 3197 | |
| 3198 | if (slot.first >= pipelineLayout->set_layouts.size()) return nullptr; |
| 3199 | |
| 3200 | return pipelineLayout->set_layouts[slot.first]->GetDescriptorSetLayoutBindingPtrFromBinding(slot.second); |
| 3201 | } |
| 3202 | |
Sam Walls | d7ab6db | 2020-06-19 20:41:54 +0100 | [diff] [blame] | 3203 | int32_t GetShaderResourceDimensionality(const SHADER_MODULE_STATE *module, const interface_var &resource) { |
| 3204 | if (module == nullptr) return -1; |
| 3205 | |
| 3206 | auto type = module->get_def(resource.type_id); |
| 3207 | while (true) { |
| 3208 | switch (type.opcode()) { |
| 3209 | case spv::OpTypeSampledImage: |
| 3210 | type = module->get_def(type.word(2)); |
| 3211 | break; |
| 3212 | case spv::OpTypePointer: |
| 3213 | type = module->get_def(type.word(3)); |
| 3214 | break; |
| 3215 | case spv::OpTypeImage: |
| 3216 | return type.word(3); |
| 3217 | default: |
| 3218 | return -1; |
| 3219 | } |
| 3220 | } |
| 3221 | } |
| 3222 | |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 3223 | // Because the following is legal, need the entry point |
| 3224 | // OpEntryPoint GLCompute %main "name_a" |
| 3225 | // OpEntryPoint GLCompute %main "name_b" |
| 3226 | bool FindLocalSize(SHADER_MODULE_STATE const *src, const spirv_inst_iter &entrypoint, uint32_t &local_size_x, |
| 3227 | uint32_t &local_size_y, uint32_t &local_size_z) { |
| 3228 | auto entrypoint_id = entrypoint.word(2); |
| 3229 | auto it = src->execution_mode_inst.find(entrypoint_id); |
| 3230 | if (it != src->execution_mode_inst.end()) { |
| 3231 | for (auto insn : it->second) { |
| 3232 | // Future Note: For now, Vulkan doesn't have a valid mode that can makes use of OpExecutionModeId |
| 3233 | // In the future if something like LocalSizeId is supported, the <id> will need to be checked also |
| 3234 | assert(insn.opcode() == spv::OpExecutionMode); |
| 3235 | if (insn.word(2) == spv::ExecutionModeLocalSize) { |
| 3236 | local_size_x = insn.word(3); |
| 3237 | local_size_y = insn.word(4); |
| 3238 | local_size_z = insn.word(5); |
| 3239 | return true; |
Locke | 1ec6d95 | 2019-04-02 11:57:21 -0600 | [diff] [blame] | 3240 | } |
| 3241 | } |
| 3242 | } |
| 3243 | return false; |
| 3244 | } |
| 3245 | |
locke-lunarg | d9a069d | 2019-09-17 01:50:19 -0600 | [diff] [blame] | 3246 | void ProcessExecutionModes(SHADER_MODULE_STATE const *src, const spirv_inst_iter &entrypoint, PIPELINE_STATE *pipeline) { |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 3247 | auto entrypoint_id = entrypoint.word(2); |
Chris Forbes | 0771b67 | 2018-03-22 21:13:46 -0700 | [diff] [blame] | 3248 | bool is_point_mode = false; |
| 3249 | |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 3250 | auto it = src->execution_mode_inst.find(entrypoint_id); |
| 3251 | if (it != src->execution_mode_inst.end()) { |
| 3252 | for (auto insn : it->second) { |
Chris Forbes | 0771b67 | 2018-03-22 21:13:46 -0700 | [diff] [blame] | 3253 | switch (insn.word(2)) { |
| 3254 | case spv::ExecutionModePointMode: |
| 3255 | // In tessellation shaders, PointMode is separate and trumps the tessellation topology. |
| 3256 | is_point_mode = true; |
| 3257 | break; |
| 3258 | |
| 3259 | case spv::ExecutionModeOutputPoints: |
| 3260 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 3261 | break; |
| 3262 | |
| 3263 | case spv::ExecutionModeIsolines: |
| 3264 | case spv::ExecutionModeOutputLineStrip: |
| 3265 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; |
| 3266 | break; |
| 3267 | |
| 3268 | case spv::ExecutionModeTriangles: |
| 3269 | case spv::ExecutionModeQuads: |
| 3270 | case spv::ExecutionModeOutputTriangleStrip: |
| 3271 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
| 3272 | break; |
| 3273 | } |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | if (is_point_mode) pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 3278 | } |
| 3279 | |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3280 | // If PointList topology is specified in the pipeline, verify that a shader geometry stage writes PointSize |
| 3281 | // o If there is only a vertex shader : gl_PointSize must be written when using points |
| 3282 | // o If there is a geometry or tessellation shader: |
| 3283 | // - If shaderTessellationAndGeometryPointSize feature is enabled: |
| 3284 | // * gl_PointSize must be written in the final geometry stage |
| 3285 | // - If shaderTessellationAndGeometryPointSize feature is disabled: |
| 3286 | // * gl_PointSize must NOT be written and a default of 1.0 is assumed |
Mark Lobodzinski | 3c59d97 | 2019-04-25 11:28:14 -0600 | [diff] [blame] | 3287 | bool CoreChecks::ValidatePointListShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3288 | spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3289 | if (pipeline->topology_at_rasterizer != VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 3290 | return false; |
| 3291 | } |
| 3292 | |
| 3293 | bool pointsize_written = false; |
| 3294 | bool skip = false; |
| 3295 | |
| 3296 | // Search for PointSize built-in decorations |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 3297 | for (auto set : src->builtin_decoration_list) { |
| 3298 | auto insn = src->at(set.offset); |
| 3299 | if (set.builtin == spv::BuiltInPointSize) { |
| 3300 | pointsize_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3301 | if (pointsize_written) { |
| 3302 | break; |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3303 | } |
| 3304 | } |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | if ((stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT || stage == VK_SHADER_STAGE_GEOMETRY_BIT) && |
Mark Lobodzinski | d7b03cc | 2019-04-19 14:23:10 -0600 | [diff] [blame] | 3308 | !enabled_features.core.shaderTessellationAndGeometryPointSize) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3309 | if (pointsize_written) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3310 | skip |= LogError(pipeline->pipeline, kVUID_Core_Shader_PointSizeBuiltInOverSpecified, |
| 3311 | "Pipeline topology is set to POINT_LIST and geometry or tessellation shaders write PointSize which " |
| 3312 | "is prohibited when the shaderTessellationAndGeometryPointSize feature is not enabled."); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3313 | } |
| 3314 | } else if (!pointsize_written) { |
| 3315 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3316 | LogError(pipeline->pipeline, kVUID_Core_Shader_MissingPointSizeBuiltIn, |
| 3317 | "Pipeline topology is set to POINT_LIST, but PointSize is not written to in the shader corresponding to %s.", |
| 3318 | string_VkShaderStageFlagBits(stage)); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3319 | } |
| 3320 | return skip; |
| 3321 | } |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3322 | |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3323 | bool CoreChecks::ValidatePrimitiveRateShaderState(const PIPELINE_STATE *pipeline, SHADER_MODULE_STATE const *src, |
| 3324 | spirv_inst_iter entrypoint, VkShaderStageFlagBits stage) const { |
| 3325 | bool primitiverate_written = false; |
| 3326 | bool viewportindex_written = false; |
| 3327 | bool viewportmask_written = false; |
| 3328 | bool skip = false; |
| 3329 | |
| 3330 | // Check if the primitive shading rate is written |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 3331 | for (auto set : src->builtin_decoration_list) { |
| 3332 | auto insn = src->at(set.offset); |
| 3333 | if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3334 | primitiverate_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3335 | } else if (set.builtin == spv::BuiltInViewportIndex) { |
| 3336 | viewportindex_written = IsBuiltInWritten(src, insn, entrypoint); |
| 3337 | } else if (set.builtin == spv::BuiltInViewportMaskNV) { |
| 3338 | viewportmask_written = IsBuiltInWritten(src, insn, entrypoint); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3339 | } |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 3340 | if (primitiverate_written && viewportindex_written && viewportmask_written) { |
| 3341 | break; |
| 3342 | } |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
Tony-LunarG | d44844c | 2021-01-22 13:24:37 -0700 | [diff] [blame] | 3345 | if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports && |
| 3346 | pipeline->graphicsPipelineCI.pViewportState) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3347 | if (!IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && |
| 3348 | pipeline->graphicsPipelineCI.pViewportState->viewportCount > 1 && primitiverate_written) { |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 3349 | skip |= LogError(pipeline->pipeline, |
| 3350 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04503", |
| 3351 | "vkCreateGraphicsPipelines: %s shader statically writes to PrimitiveShadingRateKHR built-in, but " |
| 3352 | "multiple viewports " |
| 3353 | "are used and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3354 | string_VkShaderStageFlagBits(stage)); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | if (primitiverate_written && viewportindex_written) { |
| 3358 | skip |= LogError(pipeline->pipeline, |
| 3359 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04504", |
| 3360 | "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and " |
| 3361 | "ViewportIndex built-ins," |
| 3362 | "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3363 | string_VkShaderStageFlagBits(stage)); |
| 3364 | } |
| 3365 | |
| 3366 | if (primitiverate_written && viewportmask_written) { |
| 3367 | skip |= LogError(pipeline->pipeline, |
| 3368 | "VUID-VkGraphicsPipelineCreateInfo-primitiveFragmentShadingRateWithMultipleViewports-04505", |
| 3369 | "vkCreateGraphicsPipelines: %s shader statically writes to both PrimitiveShadingRateKHR and " |
| 3370 | "ViewportMaskNV built-ins," |
| 3371 | "but the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
| 3372 | string_VkShaderStageFlagBits(stage)); |
| 3373 | } |
| 3374 | } |
| 3375 | return skip; |
| 3376 | } |
| 3377 | |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame] | 3378 | // Validate runtime usage of various opcodes that depends on what Vulkan properties or features are exposed |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 3379 | bool CoreChecks::ValidatePropertiesAndFeatures(SHADER_MODULE_STATE const *module, spirv_inst_iter &insn) const { |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame] | 3380 | bool skip = false; |
| 3381 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 3382 | switch (insn.opcode()) { |
| 3383 | case spv::OpReadClockKHR: { |
| 3384 | auto scope_id = module->get_def(insn.word(3)); |
| 3385 | auto scope_type = scope_id.word(3); |
| 3386 | // if scope isn't Subgroup or Device, spirv-val will catch |
| 3387 | if ((scope_type == spv::ScopeSubgroup) && (enabled_features.shader_clock_feature.shaderSubgroupClock == VK_FALSE)) { |
| 3388 | skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderSubgroupClock", |
| 3389 | "%s: OpReadClockKHR is used with a Subgroup scope but shaderSubgroupClock was not enabled.", |
| 3390 | report_data->FormatHandle(module->vk_shader_module).c_str()); |
| 3391 | } else if ((scope_type == spv::ScopeDevice) && (enabled_features.shader_clock_feature.shaderDeviceClock == VK_FALSE)) { |
| 3392 | skip |= LogError(device, "UNASSIGNED-spirv-shaderClock-shaderDeviceClock", |
| 3393 | "%s: OpReadClockKHR is used with a Device scope but shaderDeviceClock was not enabled.", |
| 3394 | report_data->FormatHandle(module->vk_shader_module).c_str()); |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame] | 3395 | } |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 3396 | break; |
sfricke-samsung | 486a51e | 2021-01-02 00:10:15 -0800 | [diff] [blame] | 3397 | } |
| 3398 | } |
| 3399 | return skip; |
| 3400 | } |
| 3401 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3402 | bool CoreChecks::ValidatePipelineShaderStage(VkPipelineShaderStageCreateInfo const *pStage, const PIPELINE_STATE *pipeline, |
| 3403 | const PIPELINE_STATE::StageState &stage_state, const SHADER_MODULE_STATE *module, |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3404 | const spirv_inst_iter &entrypoint, bool check_point_size) const { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3405 | bool skip = false; |
| 3406 | |
| 3407 | // Check the module |
| 3408 | if (!module->has_valid_spirv) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3409 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", |
| 3410 | "%s does not contain valid spirv for stage %s.", |
| 3411 | report_data->FormatHandle(module->vk_shader_module).c_str(), string_VkShaderStageFlagBits(pStage->stage)); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3412 | } |
| 3413 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3414 | // If specialization-constant values are given and specialization-constant instructions are present in the shader, the |
| 3415 | // specializations should be applied and validated. |
| 3416 | if (pStage->pSpecializationInfo != nullptr && pStage->pSpecializationInfo->mapEntryCount > 0 && |
| 3417 | pStage->pSpecializationInfo->pMapEntries != nullptr && module->has_specialization_constants) { |
| 3418 | // Gather the specialization-constant values. |
| 3419 | auto const &specialization_info = pStage->pSpecializationInfo; |
Jeremy Hayes | 521221d | 2020-01-15 16:48:49 -0700 | [diff] [blame] | 3420 | auto const &specialization_data = reinterpret_cast<uint8_t const *>(specialization_info->pData); |
Jeremy Gebben | cbf2286 | 2021-03-03 12:01:22 -0700 | [diff] [blame] | 3421 | std::unordered_map<uint32_t, std::vector<uint32_t>> id_value_map; // note: this must be std:: to work with spvtools |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3422 | id_value_map.reserve(specialization_info->mapEntryCount); |
| 3423 | for (auto i = 0u; i < specialization_info->mapEntryCount; ++i) { |
| 3424 | auto const &map_entry = specialization_info->pMapEntries[i]; |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3425 | |
Jeremy Hayes | 521221d | 2020-01-15 16:48:49 -0700 | [diff] [blame] | 3426 | // Expect only scalar types. |
| 3427 | assert(map_entry.size == 1 || map_entry.size == 2 || map_entry.size == 4 || map_entry.size == 8); |
| 3428 | auto entry = id_value_map.emplace(map_entry.constantID, std::vector<uint32_t>(map_entry.size > 4 ? 2 : 1)); |
| 3429 | memcpy(entry.first->second.data(), specialization_data + map_entry.offset, map_entry.size); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | // Apply the specialization-constant values and revalidate the shader module. |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 3433 | spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled)); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3434 | spvtools::Optimizer optimizer(spirv_environment); |
| 3435 | spvtools::MessageConsumer consumer = [&skip, &module, &pStage, this](spv_message_level_t level, const char *source, |
| 3436 | const spv_position_t &position, const char *message) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3437 | skip |= LogError( |
| 3438 | device, "VUID-VkPipelineShaderStageCreateInfo-module-parameter", "%s does not contain valid spirv for stage %s. %s", |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3439 | report_data->FormatHandle(module->vk_shader_module).c_str(), string_VkShaderStageFlagBits(pStage->stage), message); |
| 3440 | }; |
| 3441 | optimizer.SetMessageConsumer(consumer); |
| 3442 | optimizer.RegisterPass(spvtools::CreateSetSpecConstantDefaultValuePass(id_value_map)); |
| 3443 | optimizer.RegisterPass(spvtools::CreateFreezeSpecConstantValuePass()); |
| 3444 | std::vector<uint32_t> specialized_spirv; |
| 3445 | auto const optimized = |
| 3446 | optimizer.Run(module->words.data(), module->words.size(), &specialized_spirv, spvtools::ValidatorOptions(), true); |
| 3447 | assert(optimized == true); |
| 3448 | |
| 3449 | if (optimized) { |
| 3450 | spv_context ctx = spvContextCreate(spirv_environment); |
| 3451 | spv_const_binary_t binary{specialized_spirv.data(), specialized_spirv.size()}; |
| 3452 | spv_diagnostic diag = nullptr; |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 3453 | spvtools::ValidatorOptions options; |
| 3454 | AdjustValidatorOptions(device_extensions, enabled_features, options); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3455 | auto const spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
| 3456 | if (spv_valid != SPV_SUCCESS) { |
sfricke-samsung | d379380 | 2020-08-18 22:55:03 -0700 | [diff] [blame] | 3457 | skip |= LogError(device, "VUID-VkPipelineShaderStageCreateInfo-module-04145", |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3458 | "After specialization was applied, %s does not contain valid spirv for stage %s.", |
| 3459 | report_data->FormatHandle(module->vk_shader_module).c_str(), |
| 3460 | string_VkShaderStageFlagBits(pStage->stage)); |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3461 | } |
| 3462 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 3463 | spvDiagnosticDestroy(diag); |
| 3464 | spvContextDestroy(ctx); |
| 3465 | } |
| 3466 | } |
| 3467 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3468 | // Check the entrypoint |
| 3469 | if (entrypoint == module->end()) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3470 | skip |= |
| 3471 | LogError(device, "VUID-VkPipelineShaderStageCreateInfo-pName-00707", "No entrypoint found named `%s` for stage %s..", |
| 3472 | pStage->pName, string_VkShaderStageFlagBits(pStage->stage)); |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3473 | } |
| 3474 | if (skip) return true; // no point continuing beyond here, any analysis is just going to be garbage. |
| 3475 | |
| 3476 | // Mark accessible ids |
| 3477 | auto &accessible_ids = stage_state.accessible_ids; |
| 3478 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3479 | // Validate descriptor set layout against what the entrypoint actually uses |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3480 | bool has_writable_descriptor = stage_state.has_writable_descriptor; |
| 3481 | auto &descriptor_uses = stage_state.descriptor_uses; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3482 | |
sfricke-samsung | 94167ca | 2021-02-26 04:14:59 -0800 | [diff] [blame] | 3483 | // The following tries to limit the number of passes through the shader module. The validation passes in here are "stateless" |
| 3484 | // and mainly only checking the instruction in detail for a single operation |
| 3485 | for (auto insn : *module) { |
| 3486 | skip |= ValidateShaderCapabilitiesAndExtensions(module, insn); |
| 3487 | skip |= ValidatePropertiesAndFeatures(module, insn); |
| 3488 | skip |= ValidateShaderStageGroupNonUniform(module, pStage->stage, insn); |
| 3489 | } |
| 3490 | |
locke-lunarg | 63e4daf | 2020-08-17 17:53:25 -0600 | [diff] [blame] | 3491 | skip |= |
| 3492 | ValidateShaderStageWritableOrAtomicDescriptor(pStage->stage, has_writable_descriptor, stage_state.has_atomic_descriptor); |
Jeff Bolz | e9ee3d8 | 2019-05-29 13:45:13 -0500 | [diff] [blame] | 3493 | skip |= ValidateShaderStageInputOutputLimits(module, pStage, pipeline, entrypoint); |
sfricke-samsung | dc96f30 | 2020-03-18 20:42:10 -0700 | [diff] [blame] | 3494 | skip |= ValidateShaderStageMaxResources(pStage->stage, pipeline); |
Attilio Provenzano | c5d5010 | 2019-03-25 17:40:37 +0000 | [diff] [blame] | 3495 | skip |= ValidateExecutionModes(module, entrypoint); |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3496 | skip |= ValidateSpecializationOffsets(pStage); |
locke-lunarg | de3f0fa | 2020-09-10 11:55:31 -0600 | [diff] [blame] | 3497 | skip |= ValidatePushConstantUsage(*pipeline, module, pStage); |
Jeff Bolz | e54ae89 | 2018-09-08 12:16:29 -0500 | [diff] [blame] | 3498 | if (check_point_size && !pipeline->graphicsPipelineCI.pRasterizationState->rasterizerDiscardEnable) { |
Mark Lobodzinski | 518eadc | 2019-03-09 12:07:30 -0700 | [diff] [blame] | 3499 | skip |= ValidatePointListShaderState(pipeline, module, entrypoint, pStage->stage); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3500 | } |
sfricke-samsung | ef2a68c | 2020-10-26 04:22:46 -0700 | [diff] [blame] | 3501 | skip |= ValidateBuiltinLimits(module, accessible_ids, pStage->stage); |
sfricke-samsung | d093e52 | 2021-02-26 04:17:45 -0800 | [diff] [blame] | 3502 | if (enabled_features.cooperative_matrix_features.cooperativeMatrix) { |
| 3503 | skip |= ValidateCooperativeMatrix(module, pStage, pipeline); |
| 3504 | } |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3505 | if (enabled_features.fragment_shading_rate_features.primitiveFragmentShadingRate) { |
| 3506 | skip |= ValidatePrimitiveRateShaderState(pipeline, module, entrypoint, pStage->stage); |
| 3507 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3508 | |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3509 | std::string vuid_layout_mismatch; |
| 3510 | if (pipeline->graphicsPipelineCI.sType == VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) { |
| 3511 | vuid_layout_mismatch = "VUID-VkGraphicsPipelineCreateInfo-layout-00756"; |
| 3512 | } else if (pipeline->computePipelineCI.sType == VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO) { |
| 3513 | vuid_layout_mismatch = "VUID-VkComputePipelineCreateInfo-layout-00703"; |
| 3514 | } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_KHR) { |
| 3515 | vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoKHR-layout-03427"; |
| 3516 | } else if (pipeline->raytracingPipelineCI.sType == VK_STRUCTURE_TYPE_RAY_TRACING_PIPELINE_CREATE_INFO_NV) { |
| 3517 | vuid_layout_mismatch = "VUID-VkRayTracingPipelineCreateInfoNV-layout-03427"; |
| 3518 | } |
| 3519 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3520 | // Validate descriptor use |
| 3521 | for (auto use : descriptor_uses) { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3522 | // Verify given pipelineLayout has requested setLayout with requested binding |
Jeff Bolz | e7fc67b | 2019-10-04 12:29:31 -0500 | [diff] [blame] | 3523 | const auto &binding = GetDescriptorBinding(pipeline->pipeline_layout.get(), use.first); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3524 | unsigned required_descriptor_count; |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3525 | bool is_khr = binding && binding->descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR; |
| 3526 | std::set<uint32_t> descriptor_types = |
| 3527 | TypeToDescriptorTypeSet(module, use.second.type_id, required_descriptor_count, is_khr); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3528 | |
| 3529 | if (!binding) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3530 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3531 | "Shader uses descriptor slot %u.%u (expected `%s`) but not declared in pipeline layout", |
| 3532 | use.first.first, use.first.second, string_descriptorTypes(descriptor_types).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3533 | } else if (~binding->stageFlags & pStage->stage) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3534 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3535 | "Shader uses descriptor slot %u.%u but descriptor not accessible from stage %s", use.first.first, |
| 3536 | use.first.second, string_VkShaderStageFlagBits(pStage->stage)); |
Tony-LunarG | f563b36 | 2021-03-18 16:13:18 -0600 | [diff] [blame^] | 3537 | } else if ((binding->descriptorType != VK_DESCRIPTOR_TYPE_MUTABLE_VALVE) && |
| 3538 | (descriptor_types.find(binding->descriptorType) == descriptor_types.end())) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3539 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3540 | "Type mismatch on descriptor slot %u.%u (expected `%s`) but descriptor of type %s", use.first.first, |
| 3541 | use.first.second, string_descriptorTypes(descriptor_types).c_str(), |
| 3542 | string_VkDescriptorType(binding->descriptorType)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3543 | } else if (binding->descriptorCount < required_descriptor_count) { |
locke-lunarg | 9a16ebb | 2020-07-30 16:56:33 -0600 | [diff] [blame] | 3544 | skip |= LogError(device, vuid_layout_mismatch, |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3545 | "Shader expects at least %u descriptors for binding %u.%u but only %u provided", |
| 3546 | required_descriptor_count, use.first.first, use.first.second, binding->descriptorCount); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | // Validate use of input attachments against subpass structure |
| 3551 | if (pStage->stage == VK_SHADER_STAGE_FRAGMENT_BIT) { |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3552 | auto input_attachment_uses = CollectInterfaceByInputAttachmentIndex(module, accessible_ids); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3553 | |
Petr Kraus | e91f7a1 | 2017-12-14 20:57:36 +0100 | [diff] [blame] | 3554 | auto rpci = pipeline->rp_state->createInfo.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3555 | auto subpass = pipeline->graphicsPipelineCI.subpass; |
| 3556 | |
| 3557 | for (auto use : input_attachment_uses) { |
| 3558 | auto input_attachments = rpci->pSubpasses[subpass].pInputAttachments; |
| 3559 | auto index = (input_attachments && use.first < rpci->pSubpasses[subpass].inputAttachmentCount) |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3560 | ? input_attachments[use.first].attachment |
| 3561 | : VK_ATTACHMENT_UNUSED; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3562 | |
| 3563 | if (index == VK_ATTACHMENT_UNUSED) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3564 | skip |= LogError(device, kVUID_Core_Shader_MissingInputAttachment, |
| 3565 | "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] | 3566 | } 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] | 3567 | skip |= |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3568 | LogError(device, kVUID_Core_Shader_InputAttachmentTypeMismatch, |
| 3569 | "Subpass input attachment %u format of %s does not match type used in shader `%s`", use.first, |
| 3570 | 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] | 3571 | } |
| 3572 | } |
| 3573 | } |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 3574 | if (pStage->stage == VK_SHADER_STAGE_COMPUTE_BIT) { |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 3575 | skip |= ValidateComputeWorkGroupSizes(module, entrypoint); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 3576 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3577 | return skip; |
| 3578 | } |
| 3579 | |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3580 | bool CoreChecks::ValidateInterfaceBetweenStages(SHADER_MODULE_STATE const *producer, spirv_inst_iter producer_entrypoint, |
| 3581 | shader_stage_attributes const *producer_stage, SHADER_MODULE_STATE const *consumer, |
| 3582 | spirv_inst_iter consumer_entrypoint, |
| 3583 | shader_stage_attributes const *consumer_stage) const { |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3584 | bool skip = false; |
| 3585 | |
| 3586 | auto outputs = |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3587 | CollectInterfaceByLocation(producer, producer_entrypoint, spv::StorageClassOutput, producer_stage->arrayed_output); |
| 3588 | auto inputs = CollectInterfaceByLocation(consumer, consumer_entrypoint, spv::StorageClassInput, consumer_stage->arrayed_input); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3589 | |
| 3590 | auto a_it = outputs.begin(); |
| 3591 | auto b_it = inputs.begin(); |
| 3592 | |
| 3593 | // Maps sorted by key (location); walk them together to find mismatches |
| 3594 | while ((outputs.size() > 0 && a_it != outputs.end()) || (inputs.size() && b_it != inputs.end())) { |
| 3595 | bool a_at_end = outputs.size() == 0 || a_it == outputs.end(); |
| 3596 | bool b_at_end = inputs.size() == 0 || b_it == inputs.end(); |
| 3597 | auto a_first = a_at_end ? std::make_pair(0u, 0u) : a_it->first; |
| 3598 | auto b_first = b_at_end ? std::make_pair(0u, 0u) : b_it->first; |
| 3599 | |
| 3600 | if (b_at_end || ((!a_at_end) && (a_first < b_first))) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3601 | skip |= LogPerformanceWarning(producer->vk_shader_module, kVUID_Core_Shader_OutputNotConsumed, |
| 3602 | "%s writes to output location %u.%u which is not consumed by %s", producer_stage->name, |
| 3603 | a_first.first, a_first.second, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3604 | a_it++; |
| 3605 | } else if (a_at_end || a_first > b_first) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3606 | skip |= LogError(consumer->vk_shader_module, kVUID_Core_Shader_InputNotProduced, |
| 3607 | "%s consumes input location %u.%u which is not written by %s", consumer_stage->name, b_first.first, |
| 3608 | b_first.second, producer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3609 | b_it++; |
| 3610 | } else { |
| 3611 | // subtleties of arrayed interfaces: |
| 3612 | // - if is_patch, then the member is not arrayed, even though the interface may be. |
| 3613 | // - if is_block_member, then the extra array level of an arrayed interface is not |
| 3614 | // expressed in the member type -- it's expressed in the block type. |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3615 | if (!TypesMatch(producer, consumer, a_it->second.type_id, b_it->second.type_id, |
| 3616 | producer_stage->arrayed_output && !a_it->second.is_patch && !a_it->second.is_block_member, |
| 3617 | consumer_stage->arrayed_input && !b_it->second.is_patch && !b_it->second.is_block_member, true)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3618 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3619 | "Type mismatch on location %u.%u: '%s' vs '%s'", a_first.first, a_first.second, |
| 3620 | DescribeType(producer, a_it->second.type_id).c_str(), |
| 3621 | DescribeType(consumer, b_it->second.type_id).c_str()); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3622 | } |
| 3623 | if (a_it->second.is_patch != b_it->second.is_patch) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3624 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3625 | "Decoration mismatch on location %u.%u: is per-%s in %s stage but per-%s in %s stage", |
| 3626 | a_first.first, a_first.second, a_it->second.is_patch ? "patch" : "vertex", producer_stage->name, |
| 3627 | b_it->second.is_patch ? "patch" : "vertex", consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3628 | } |
| 3629 | if (a_it->second.is_relaxed_precision != b_it->second.is_relaxed_precision) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3630 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3631 | "Decoration mismatch on location %u.%u: %s and %s stages differ in precision", a_first.first, |
| 3632 | a_first.second, producer_stage->name, consumer_stage->name); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3633 | } |
| 3634 | a_it++; |
| 3635 | b_it++; |
| 3636 | } |
| 3637 | } |
| 3638 | |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3639 | if (consumer_stage->stage != VK_SHADER_STAGE_FRAGMENT_BIT) { |
| 3640 | auto builtins_producer = CollectBuiltinBlockMembers(producer, producer_entrypoint, spv::StorageClassOutput); |
| 3641 | auto builtins_consumer = CollectBuiltinBlockMembers(consumer, consumer_entrypoint, spv::StorageClassInput); |
| 3642 | |
| 3643 | if (!builtins_producer.empty() && !builtins_consumer.empty()) { |
| 3644 | if (builtins_producer.size() != builtins_consumer.size()) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3645 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3646 | "Number of elements inside builtin block differ between stages (%s %d vs %s %d).", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3647 | producer_stage->name, static_cast<int>(builtins_producer.size()), consumer_stage->name, |
| 3648 | static_cast<int>(builtins_consumer.size())); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3649 | } else { |
| 3650 | auto it_producer = builtins_producer.begin(); |
| 3651 | auto it_consumer = builtins_consumer.begin(); |
| 3652 | while (it_producer != builtins_producer.end() && it_consumer != builtins_consumer.end()) { |
| 3653 | if (*it_producer != *it_consumer) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 3654 | skip |= LogError(producer->vk_shader_module, kVUID_Core_Shader_InterfaceTypeMismatch, |
| 3655 | "Builtin variable inside block doesn't match between %s and %s.", producer_stage->name, |
| 3656 | consumer_stage->name); |
Ari Suonpaa | 696b343 | 2019-03-11 14:02:57 +0200 | [diff] [blame] | 3657 | break; |
| 3658 | } |
| 3659 | it_producer++; |
| 3660 | it_consumer++; |
| 3661 | } |
| 3662 | } |
| 3663 | } |
| 3664 | } |
| 3665 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3666 | return skip; |
| 3667 | } |
| 3668 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3669 | static inline uint32_t DetermineFinalGeomStage(const PIPELINE_STATE *pipeline, const VkGraphicsPipelineCreateInfo *pCreateInfo) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3670 | uint32_t stage_mask = 0; |
| 3671 | if (pipeline->topology_at_rasterizer == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) { |
| 3672 | for (uint32_t i = 0; i < pCreateInfo->stageCount; i++) { |
| 3673 | stage_mask |= pCreateInfo->pStages[i].stage; |
| 3674 | } |
| 3675 | // Determine which shader in which PointSize should be written (the final geometry stage) |
Jeff Bolz | 105d649 | 2018-09-29 15:46:44 -0500 | [diff] [blame] | 3676 | if (stage_mask & VK_SHADER_STAGE_MESH_BIT_NV) { |
| 3677 | stage_mask = VK_SHADER_STAGE_MESH_BIT_NV; |
| 3678 | } else if (stage_mask & VK_SHADER_STAGE_GEOMETRY_BIT) { |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3679 | stage_mask = VK_SHADER_STAGE_GEOMETRY_BIT; |
| 3680 | } else if (stage_mask & VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT) { |
| 3681 | stage_mask = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 3682 | } else if (stage_mask & VK_SHADER_STAGE_VERTEX_BIT) { |
| 3683 | stage_mask = VK_SHADER_STAGE_VERTEX_BIT; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 3684 | } |
| 3685 | } |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3686 | return stage_mask; |
Mark Lobodzinski | 2c984cc | 2018-07-31 09:57:46 -0600 | [diff] [blame] | 3687 | } |
| 3688 | |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3689 | // Validate that the shaders used by the given pipeline and store the active_slots |
| 3690 | // that are actually used by the pipeline into pPipeline->active_slots |
John Zulauf | ac4c6e1 | 2019-07-01 16:05:58 -0600 | [diff] [blame] | 3691 | bool CoreChecks::ValidateGraphicsPipelineShaderState(const PIPELINE_STATE *pipeline) const { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3692 | auto create_info = pipeline->graphicsPipelineCI.ptr(); |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3693 | int vertex_stage = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 3694 | int fragment_stage = GetShaderStageId(VK_SHADER_STAGE_FRAGMENT_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3695 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3696 | const SHADER_MODULE_STATE *shaders[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3697 | memset(shaders, 0, sizeof(shaders)); |
Jeff Bolz | 7e35c39 | 2018-09-04 15:30:41 -0500 | [diff] [blame] | 3698 | spirv_inst_iter entrypoints[32]; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3699 | bool skip = false; |
| 3700 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3701 | uint32_t pointlist_stage_mask = DetermineFinalGeomStage(pipeline, create_info); |
Mark Lobodzinski | 1b4a8ed | 2018-08-07 08:47:05 -0600 | [diff] [blame] | 3702 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3703 | for (uint32_t i = 0; i < create_info->stageCount; i++) { |
| 3704 | auto stage = &create_info->pStages[i]; |
| 3705 | auto stage_id = GetShaderStageId(stage->stage); |
| 3706 | shaders[stage_id] = GetShaderModuleState(stage->module); |
| 3707 | entrypoints[stage_id] = FindEntrypoint(shaders[stage_id], stage->pName, stage->stage); |
| 3708 | skip |= ValidatePipelineShaderStage(stage, pipeline, pipeline->stage_state[i], shaders[stage_id], entrypoints[stage_id], |
| 3709 | (pointlist_stage_mask == stage->stage)); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | // if the shader stages are no good individually, cross-stage validation is pointless. |
| 3713 | if (skip) return true; |
| 3714 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3715 | auto vi = create_info->pVertexInputState; |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3716 | |
| 3717 | if (vi) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3718 | skip |= ValidateViConsistency(vi); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | if (shaders[vertex_stage] && shaders[vertex_stage]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3722 | skip |= ValidateViAgainstVsInputs(vi, shaders[vertex_stage], entrypoints[vertex_stage]); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3723 | } |
| 3724 | |
Shannon McPherson | c06c33d | 2018-06-28 17:21:12 -0600 | [diff] [blame] | 3725 | int producer = GetShaderStageId(VK_SHADER_STAGE_VERTEX_BIT); |
| 3726 | int consumer = GetShaderStageId(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3727 | |
| 3728 | while (!shaders[producer] && producer != fragment_stage) { |
| 3729 | producer++; |
| 3730 | consumer++; |
| 3731 | } |
| 3732 | |
| 3733 | for (; producer != fragment_stage && consumer <= fragment_stage; consumer++) { |
| 3734 | assert(shaders[producer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 3735 | if (shaders[consumer]) { |
| 3736 | if (shaders[consumer]->has_valid_spirv && shaders[producer]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3737 | skip |= ValidateInterfaceBetweenStages(shaders[producer], entrypoints[producer], &shader_stage_attribs[producer], |
| 3738 | shaders[consumer], entrypoints[consumer], &shader_stage_attribs[consumer]); |
Chris Forbes | dbb43fc | 2018-02-16 16:59:23 -0800 | [diff] [blame] | 3739 | } |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3740 | |
| 3741 | producer = consumer; |
| 3742 | } |
| 3743 | } |
| 3744 | |
| 3745 | if (shaders[fragment_stage] && shaders[fragment_stage]->has_valid_spirv) { |
Mark Lobodzinski | d8d658e | 2020-01-30 15:05:51 -0700 | [diff] [blame] | 3746 | skip |= ValidateFsOutputsAgainstRenderPass(shaders[fragment_stage], entrypoints[fragment_stage], pipeline, |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3747 | create_info->subpass); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | return skip; |
| 3751 | } |
| 3752 | |
Tony-LunarG | b2ded51 | 2021-02-02 16:03:30 -0700 | [diff] [blame] | 3753 | void CoreChecks::RecordGraphicsPipelineShaderDynamicState(PIPELINE_STATE *pipeline_state) { |
| 3754 | auto create_info = pipeline_state->graphicsPipelineCI.ptr(); |
| 3755 | |
| 3756 | if (phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports || |
| 3757 | !IsDynamic(pipeline_state, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT)) { |
| 3758 | return; |
| 3759 | } |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3760 | |
Nathaniel Cesario | 1c3d365 | 2021-01-25 18:35:12 -0700 | [diff] [blame] | 3761 | std::array<const SHADER_MODULE_STATE *, 32> shaders; |
| 3762 | std::fill(shaders.begin(), shaders.end(), nullptr); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3763 | spirv_inst_iter entrypoints[32]; |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3764 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3765 | for (uint32_t i = 0; i < create_info->stageCount; i++) { |
| 3766 | auto stage = &create_info->pStages[i]; |
| 3767 | auto stage_id = GetShaderStageId(stage->stage); |
| 3768 | shaders[stage_id] = GetShaderModuleState(stage->module); |
| 3769 | entrypoints[stage_id] = FindEntrypoint(shaders[stage_id], stage->pName, stage->stage); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3770 | |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3771 | if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT || |
| 3772 | stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) { |
Tony-LunarG | b2ded51 | 2021-02-02 16:03:30 -0700 | [diff] [blame] | 3773 | bool primitiverate_written = false; |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3774 | |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 3775 | for (auto set : shaders[stage_id]->builtin_decoration_list) { |
| 3776 | auto insn = shaders[stage_id]->at(set.offset); |
| 3777 | if (set.builtin == spv::BuiltInPrimitiveShadingRateKHR) { |
| 3778 | primitiverate_written = IsBuiltInWritten(shaders[stage_id], insn, entrypoints[stage_id]); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3779 | } |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 3780 | if (primitiverate_written) { |
| 3781 | break; |
| 3782 | } |
Tony-LunarG | b2ded51 | 2021-02-02 16:03:30 -0700 | [diff] [blame] | 3783 | } |
sfricke-samsung | c0eb528 | 2021-02-28 23:05:55 -0800 | [diff] [blame] | 3784 | |
Tony-LunarG | b2ded51 | 2021-02-02 16:03:30 -0700 | [diff] [blame] | 3785 | if (primitiverate_written) { |
| 3786 | pipeline_state->wrote_primitive_shading_rate.insert(stage->stage); |
| 3787 | } |
| 3788 | } |
| 3789 | } |
| 3790 | } |
| 3791 | |
| 3792 | bool CoreChecks::ValidateGraphicsPipelineShaderDynamicState(const PIPELINE_STATE *pipeline, const CMD_BUFFER_STATE *pCB, |
| 3793 | const char *caller, const DrawDispatchVuid &vuid) const { |
| 3794 | auto create_info = pipeline->graphicsPipelineCI.ptr(); |
| 3795 | bool skip = false; |
| 3796 | |
| 3797 | for (uint32_t i = 0; i < create_info->stageCount; i++) { |
| 3798 | auto stage = &create_info->pStages[i]; |
| 3799 | if (stage->stage == VK_SHADER_STAGE_VERTEX_BIT || stage->stage == VK_SHADER_STAGE_GEOMETRY_BIT || |
| 3800 | stage->stage == VK_SHADER_STAGE_MESH_BIT_NV) { |
| 3801 | if (!phys_dev_ext_props.fragment_shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports && |
| 3802 | IsDynamic(pipeline, VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT_EXT) && pCB->viewportWithCountCount != 1) { |
| 3803 | if (pipeline->wrote_primitive_shading_rate.find(stage->stage) != pipeline->wrote_primitive_shading_rate.end()) { |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3804 | skip |= |
| 3805 | LogError(pipeline->pipeline, vuid.viewport_count_primitive_shading_rate, |
| 3806 | "%s: %s shader of currently bound pipeline statically writes to PrimitiveShadingRateKHR built-in" |
| 3807 | "but multiple viewports are set by the last call to vkCmdSetViewportWithCountEXT," |
| 3808 | "and the primitiveFragmentShadingRateWithMultipleViewports limit is not supported.", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3809 | caller, string_VkShaderStageFlagBits(stage->stage)); |
Tobias Hector | 6663c9b | 2020-11-05 10:18:02 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | } |
| 3813 | } |
| 3814 | |
| 3815 | return skip; |
| 3816 | } |
| 3817 | |
sfricke-samsung | e72a85e | 2020-02-29 21:48:37 -0800 | [diff] [blame] | 3818 | bool CoreChecks::ValidateComputePipelineShaderState(PIPELINE_STATE *pipeline) const { |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3819 | const auto &stage = *pipeline->computePipelineCI.stage.ptr(); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3820 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3821 | const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module); |
| 3822 | const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3823 | |
John Zulauf | 14c355b | 2019-06-27 16:09:37 -0600 | [diff] [blame] | 3824 | return ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[0], module, entrypoint, false); |
Chris Forbes | 47567b7 | 2017-06-09 12:09:45 -0700 | [diff] [blame] | 3825 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3826 | |
Ricardo Garcia | 6f3477e | 2020-10-21 10:58:53 +0200 | [diff] [blame] | 3827 | uint32_t CoreChecks::CalcShaderStageCount(const PIPELINE_STATE *pipeline, VkShaderStageFlagBits stageBit) const { |
| 3828 | uint32_t total = 0; |
| 3829 | |
| 3830 | const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages; |
| 3831 | for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) { |
| 3832 | if (stages[stage_index].stage == stageBit) { |
| 3833 | total++; |
| 3834 | } |
| 3835 | } |
| 3836 | |
| 3837 | if (pipeline->raytracingPipelineCI.pLibraryInfo) { |
| 3838 | for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) { |
| 3839 | const PIPELINE_STATE *library_pipeline = GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]); |
| 3840 | total += CalcShaderStageCount(library_pipeline, stageBit); |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | return total; |
| 3845 | } |
| 3846 | |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3847 | bool CoreChecks::ValidateRayTracingPipeline(PIPELINE_STATE *pipeline, VkPipelineCreateFlags flags, bool isKHR) const { |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3848 | bool skip = false; |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3849 | |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3850 | if (isKHR) { |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3851 | if (pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth > |
| 3852 | phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth) { |
| 3853 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-maxPipelineRayRecursionDepth-03589", |
| 3854 | "vkCreateRayTracingPipelinesKHR: maxPipelineRayRecursionDepth (%d ) must be less than or equal to " |
| 3855 | "VkPhysicalDeviceRayTracingPipelinePropertiesKHR::maxRayRecursionDepth %d", |
| 3856 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth, |
| 3857 | phys_dev_ext_props.ray_tracing_propsKHR.maxRayRecursionDepth); |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3858 | } |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3859 | if (pipeline->raytracingPipelineCI.pLibraryInfo) { |
| 3860 | for (uint32_t i = 0; i < pipeline->raytracingPipelineCI.pLibraryInfo->libraryCount; ++i) { |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3861 | const PIPELINE_STATE *library_pipelinestate = |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3862 | GetPipelineState(pipeline->raytracingPipelineCI.pLibraryInfo->pLibraries[i]); |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3863 | if (library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3864 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth) { |
| 3865 | skip |= LogError( |
| 3866 | device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraries-03591", |
| 3867 | "vkCreateRayTracingPipelinesKHR: Each element (%d) of the pLibraries member of libraries must have been" |
| 3868 | "created with the value of maxPipelineRayRecursionDepth (%d) equal to that in this pipeline (%d) .", |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3869 | i, library_pipelinestate->raytracingPipelineCI.maxPipelineRayRecursionDepth, |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3870 | pipeline->raytracingPipelineCI.maxPipelineRayRecursionDepth); |
| 3871 | } |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3872 | if (library_pipelinestate->raytracingPipelineCI.pLibraryInfo && |
| 3873 | (library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3874 | pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayHitAttributeSize || |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3875 | library_pipelinestate->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize != |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3876 | pipeline->raytracingPipelineCI.pLibraryInterface->maxPipelineRayPayloadSize)) { |
| 3877 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-pLibraryInfo-03593", |
| 3878 | "vkCreateRayTracingPipelinesKHR: If pLibraryInfo is not NULL, each element of its pLibraries " |
| 3879 | "member must have been created with values of the maxPipelineRayPayloadSize and " |
| 3880 | "maxPipelineRayHitAttributeSize members of pLibraryInterface equal to those in this pipeline"); |
| 3881 | } |
| 3882 | if ((flags & VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR) && |
Nathaniel Cesario | ce9b481 | 2020-12-17 08:55:28 -0700 | [diff] [blame] | 3883 | !(library_pipelinestate->raytracingPipelineCI.flags & |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3884 | VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR)) { |
| 3885 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoKHR-flags-03594", |
| 3886 | "vkCreateRayTracingPipelinesKHR: If flags includes " |
| 3887 | "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR, each element of " |
| 3888 | "the pLibraries member of libraries must have been created with the " |
| 3889 | "VK_PIPELINE_CREATE_RAY_TRACING_SHADER_GROUP_HANDLE_CAPTURE_REPLAY_BIT_KHR bit set"); |
| 3890 | } |
sourav parmar | 83c31b1 | 2020-05-06 12:30:54 -0700 | [diff] [blame] | 3891 | } |
| 3892 | } |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3893 | } else { |
| 3894 | if (pipeline->raytracingPipelineCI.maxRecursionDepth > phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth) { |
sourav parmar | cd5fb18 | 2020-07-17 12:58:44 -0700 | [diff] [blame] | 3895 | skip |= LogError(device, "VUID-VkRayTracingPipelineCreateInfoNV-maxRecursionDepth-03457", |
| 3896 | "vkCreateRayTracingPipelinesNV: maxRecursionDepth (%d) must be less than or equal to " |
| 3897 | "VkPhysicalDeviceRayTracingPropertiesNV::maxRecursionDepth (%d)", |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3898 | pipeline->raytracingPipelineCI.maxRecursionDepth, |
| 3899 | phys_dev_ext_props.ray_tracing_propsNV.maxRecursionDepth); |
| 3900 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3901 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3902 | const auto *stages = pipeline->raytracingPipelineCI.ptr()->pStages; |
| 3903 | const auto *groups = pipeline->raytracingPipelineCI.ptr()->pGroups; |
| 3904 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3905 | for (uint32_t stage_index = 0; stage_index < pipeline->raytracingPipelineCI.stageCount; stage_index++) { |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3906 | const auto &stage = stages[stage_index]; |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 3907 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3908 | const SHADER_MODULE_STATE *module = GetShaderModuleState(stage.module); |
| 3909 | const spirv_inst_iter entrypoint = FindEntrypoint(module, stage.pName, stage.stage); |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 3910 | |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3911 | skip |= ValidatePipelineShaderStage(&stage, pipeline, pipeline->stage_state[stage_index], module, entrypoint, false); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3912 | } |
Ricardo Garcia | 6f3477e | 2020-10-21 10:58:53 +0200 | [diff] [blame] | 3913 | |
| 3914 | if ((pipeline->raytracingPipelineCI.flags & VK_PIPELINE_CREATE_LIBRARY_BIT_KHR) == 0) { |
| 3915 | const uint32_t raygen_stages_count = CalcShaderStageCount(pipeline, VK_SHADER_STAGE_RAYGEN_BIT_KHR); |
| 3916 | if (raygen_stages_count == 0) { |
| 3917 | skip |= LogError( |
| 3918 | device, |
| 3919 | isKHR ? "VUID-VkRayTracingPipelineCreateInfoKHR-stage-03425" : "VUID-VkRayTracingPipelineCreateInfoNV-stage-03425", |
| 3920 | " : The stage member of at least one element of pStages must be VK_SHADER_STAGE_RAYGEN_BIT_KHR."); |
| 3921 | } |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | for (uint32_t group_index = 0; group_index < pipeline->raytracingPipelineCI.groupCount; group_index++) { |
| 3925 | const auto &group = groups[group_index]; |
| 3926 | |
| 3927 | if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_NV) { |
| 3928 | if (group.generalShader >= pipeline->raytracingPipelineCI.stageCount || |
| 3929 | (stages[group.generalShader].stage != VK_SHADER_STAGE_RAYGEN_BIT_NV && |
| 3930 | stages[group.generalShader].stage != VK_SHADER_STAGE_MISS_BIT_NV && |
| 3931 | stages[group.generalShader].stage != VK_SHADER_STAGE_CALLABLE_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3932 | skip |= LogError(device, |
| 3933 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03474" |
| 3934 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02413", |
| 3935 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3936 | } |
| 3937 | if (group.anyHitShader != VK_SHADER_UNUSED_NV || group.closestHitShader != VK_SHADER_UNUSED_NV || |
| 3938 | group.intersectionShader != VK_SHADER_UNUSED_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3939 | skip |= LogError(device, |
| 3940 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03475" |
| 3941 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02414", |
| 3942 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3943 | } |
| 3944 | } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV) { |
| 3945 | if (group.intersectionShader >= pipeline->raytracingPipelineCI.stageCount || |
| 3946 | stages[group.intersectionShader].stage != VK_SHADER_STAGE_INTERSECTION_BIT_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3947 | skip |= LogError(device, |
| 3948 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03476" |
| 3949 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02415", |
| 3950 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3951 | } |
| 3952 | } else if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) { |
| 3953 | if (group.intersectionShader != VK_SHADER_UNUSED_NV) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3954 | skip |= LogError(device, |
| 3955 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-type-03477" |
| 3956 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-type-02416", |
| 3957 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | if (group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_PROCEDURAL_HIT_GROUP_NV || |
| 3962 | group.type == VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_NV) { |
| 3963 | if (group.anyHitShader != VK_SHADER_UNUSED_NV && (group.anyHitShader >= pipeline->raytracingPipelineCI.stageCount || |
| 3964 | stages[group.anyHitShader].stage != VK_SHADER_STAGE_ANY_HIT_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3965 | skip |= LogError(device, |
| 3966 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-anyHitShader-03479" |
| 3967 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-anyHitShader-02418", |
| 3968 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3969 | } |
| 3970 | if (group.closestHitShader != VK_SHADER_UNUSED_NV && |
| 3971 | (group.closestHitShader >= pipeline->raytracingPipelineCI.stageCount || |
| 3972 | stages[group.closestHitShader].stage != VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV)) { |
Jeff Bolz | 443c2ca | 2020-03-19 12:11:51 -0500 | [diff] [blame] | 3973 | skip |= LogError(device, |
| 3974 | isKHR ? "VUID-VkRayTracingShaderGroupCreateInfoKHR-closestHitShader-03478" |
| 3975 | : "VUID-VkRayTracingShaderGroupCreateInfoNV-closestHitShader-02417", |
| 3976 | ": pGroups[%d]", group_index); |
Jason Macnak | 15f95e8 | 2019-08-21 21:52:02 -0400 | [diff] [blame] | 3977 | } |
| 3978 | } |
John Zulauf | e4474e7 | 2019-07-01 17:28:27 -0600 | [diff] [blame] | 3979 | } |
| 3980 | return skip; |
Jeff Bolz | fbe5158 | 2018-09-13 10:01:35 -0500 | [diff] [blame] | 3981 | } |
| 3982 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3983 | 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] | 3984 | |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 3985 | static ValidationCache *GetValidationCacheInfo(VkShaderModuleCreateInfo const *pCreateInfo) { |
Mark Lobodzinski | 1f887d3 | 2020-12-30 15:31:33 -0700 | [diff] [blame] | 3986 | const auto validation_cache_ci = LvlFindInChain<VkShaderModuleValidationCacheCreateInfoEXT>(pCreateInfo->pNext); |
John Zulauf | 25ea243 | 2019-04-05 10:07:38 -0600 | [diff] [blame] | 3987 | if (validation_cache_ci) { |
John Zulauf | 146ee80 | 2019-04-05 15:31:06 -0600 | [diff] [blame] | 3988 | return CastFromHandle<ValidationCache *>(validation_cache_ci->validationCache); |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 3989 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 3990 | return nullptr; |
| 3991 | } |
| 3992 | |
Mark Lobodzinski | b56bbb9 | 2019-02-18 11:49:59 -0700 | [diff] [blame] | 3993 | bool CoreChecks::PreCallValidateCreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo, |
Jeff Bolz | 5c801d1 | 2019-10-09 10:38:45 -0500 | [diff] [blame] | 3994 | const VkAllocationCallbacks *pAllocator, VkShaderModule *pShaderModule) const { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3995 | bool skip = false; |
| 3996 | spv_result_t spv_valid = SPV_SUCCESS; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3997 | |
Mark Lobodzinski | 90eea5b | 2020-05-15 12:54:00 -0600 | [diff] [blame] | 3998 | if (disabled[shader_validation]) { |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 3999 | return false; |
| 4000 | } |
| 4001 | |
Mark Lobodzinski | f45e45f | 2019-04-19 14:15:39 -0600 | [diff] [blame] | 4002 | auto have_glsl_shader = device_extensions.vk_nv_glsl_shader; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4003 | |
| 4004 | if (!have_glsl_shader && (pCreateInfo->codeSize % 4)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4005 | skip |= LogError(device, "VUID-VkShaderModuleCreateInfo-pCode-01376", |
| 4006 | "SPIR-V module not valid: Codesize must be a multiple of 4 but is " PRINTF_SIZE_T_SPECIFIER ".", |
| 4007 | pCreateInfo->codeSize); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4008 | } else { |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4009 | auto cache = GetValidationCacheInfo(pCreateInfo); |
| 4010 | uint32_t hash = 0; |
| 4011 | if (cache) { |
| 4012 | hash = ValidationCache::MakeShaderHash(pCreateInfo); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 4013 | if (cache->Contains(hash)) return false; |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4014 | } |
| 4015 | |
Jeremy Hayes | b3e4d53 | 2019-08-16 10:08:49 -0600 | [diff] [blame] | 4016 | // Use SPIRV-Tools validator to try and catch any issues with the module itself. If specialization constants are present, |
| 4017 | // the default values will be used during validation. |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 4018 | spv_target_env spirv_environment = PickSpirvEnv(api_version, (device_extensions.vk_khr_spirv_1_4 != kNotEnabled)); |
Dave Houlton | 0ea2d01 | 2018-06-21 14:00:26 -0600 | [diff] [blame] | 4019 | spv_context ctx = spvContextCreate(spirv_environment); |
Dave Houlton | a9df0ce | 2018-02-07 10:51:23 -0700 | [diff] [blame] | 4020 | spv_const_binary_t binary{pCreateInfo->pCode, pCreateInfo->codeSize / sizeof(uint32_t)}; |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4021 | spv_diagnostic diag = nullptr; |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 4022 | spvtools::ValidatorOptions options; |
| 4023 | AdjustValidatorOptions(device_extensions, enabled_features, options); |
Karl Schultz | fda1b38 | 2018-08-08 18:56:11 -0600 | [diff] [blame] | 4024 | spv_valid = spvValidateWithOptions(ctx, options, &binary, &diag); |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4025 | if (spv_valid != SPV_SUCCESS) { |
| 4026 | if (!have_glsl_shader || (pCreateInfo->pCode[0] == spv::MagicNumber)) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4027 | if (spv_valid == SPV_WARNING) { |
| 4028 | skip |= LogWarning(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s", |
| 4029 | diag && diag->error ? diag->error : "(no error text)"); |
| 4030 | } else { |
| 4031 | skip |= LogError(device, kVUID_Core_Shader_InconsistentSpirv, "SPIR-V module not valid: %s", |
| 4032 | diag && diag->error ? diag->error : "(no error text)"); |
| 4033 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4034 | } |
Chris Forbes | 9a61e08 | 2017-07-24 15:35:29 -0700 | [diff] [blame] | 4035 | } else { |
| 4036 | if (cache) { |
| 4037 | cache->Insert(hash); |
| 4038 | } |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | spvDiagnosticDestroy(diag); |
| 4042 | spvContextDestroy(ctx); |
| 4043 | } |
| 4044 | |
Chris Forbes | 4ae55b3 | 2017-06-09 14:42:56 -0700 | [diff] [blame] | 4045 | return skip; |
Mark Lobodzinski | 0173407 | 2019-02-13 17:39:15 -0700 | [diff] [blame] | 4046 | } |
| 4047 | |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 4048 | bool CoreChecks::ValidateComputeWorkGroupSizes(const SHADER_MODULE_STATE *shader, const spirv_inst_iter &entrypoint) const { |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4049 | bool skip = false; |
| 4050 | uint32_t local_size_x = 0; |
| 4051 | uint32_t local_size_y = 0; |
| 4052 | uint32_t local_size_z = 0; |
sfricke-samsung | 8a7341a | 2021-02-28 07:30:21 -0800 | [diff] [blame] | 4053 | if (FindLocalSize(shader, entrypoint, local_size_x, local_size_y, local_size_z)) { |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4054 | if (local_size_x > phys_dev_props.limits.maxComputeWorkGroupSize[0]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4055 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4056 | "%s local_size_x (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[0] (%" PRIu32 ").", |
| 4057 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4058 | phys_dev_props.limits.maxComputeWorkGroupSize[0]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4059 | } |
| 4060 | if (local_size_y > phys_dev_props.limits.maxComputeWorkGroupSize[1]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4061 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4062 | "%s local_size_y (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[1] (%" PRIu32 ").", |
| 4063 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4064 | phys_dev_props.limits.maxComputeWorkGroupSize[1]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4065 | } |
| 4066 | if (local_size_z > phys_dev_props.limits.maxComputeWorkGroupSize[2]) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4067 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupSize", |
| 4068 | "%s local_size_z (%" PRIu32 ") exceeds device limit maxComputeWorkGroupSize[2] (%" PRIu32 ").", |
| 4069 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, |
| 4070 | phys_dev_props.limits.maxComputeWorkGroupSize[2]); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | uint32_t limit = phys_dev_props.limits.maxComputeWorkGroupInvocations; |
| 4074 | uint64_t invocations = local_size_x * local_size_y; |
| 4075 | // Prevent overflow. |
| 4076 | bool fail = false; |
| 4077 | if (invocations > UINT32_MAX || invocations > limit) { |
| 4078 | fail = true; |
| 4079 | } |
| 4080 | if (!fail) { |
| 4081 | invocations *= local_size_z; |
| 4082 | if (invocations > UINT32_MAX || invocations > limit) { |
| 4083 | fail = true; |
| 4084 | } |
| 4085 | } |
| 4086 | if (fail) { |
Mark Lobodzinski | 12b9be9 | 2020-01-30 15:25:55 -0700 | [diff] [blame] | 4087 | skip |= LogError(shader->vk_shader_module, "UNASSIGNED-features-limits-maxComputeWorkGroupInvocations", |
| 4088 | "%s local_size (%" PRIu32 ", %" PRIu32 ", %" PRIu32 |
| 4089 | ") exceeds device limit maxComputeWorkGroupInvocations (%" PRIu32 ").", |
| 4090 | report_data->FormatHandle(shader->vk_shader_module).c_str(), local_size_x, local_size_y, local_size_z, |
| 4091 | limit); |
Locke | aa8fdc0 | 2019-04-02 11:59:20 -0600 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | return skip; |
| 4095 | } |
Tony-LunarG | 8a51b7d | 2020-07-01 15:57:23 -0600 | [diff] [blame] | 4096 | |
| 4097 | spv_target_env PickSpirvEnv(uint32_t api_version, bool spirv_1_4) { |
| 4098 | if (api_version >= VK_API_VERSION_1_2) { |
| 4099 | return SPV_ENV_VULKAN_1_2; |
| 4100 | } else if (api_version >= VK_API_VERSION_1_1) { |
| 4101 | if (spirv_1_4) { |
| 4102 | return SPV_ENV_VULKAN_1_1_SPIRV_1_4; |
| 4103 | } else { |
| 4104 | return SPV_ENV_VULKAN_1_1; |
| 4105 | } |
| 4106 | } |
| 4107 | return SPV_ENV_VULKAN_1_0; |
| 4108 | } |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 4109 | |
| 4110 | void AdjustValidatorOptions(const DeviceExtensions device_extensions, const DeviceFeatures enabled_features, |
| 4111 | spvtools::ValidatorOptions &options) { |
| 4112 | if (device_extensions.vk_khr_relaxed_block_layout) { |
| 4113 | options.SetRelaxBlockLayout(true); |
| 4114 | } |
| 4115 | if (device_extensions.vk_khr_uniform_buffer_standard_layout && enabled_features.core12.uniformBufferStandardLayout == VK_TRUE) { |
| 4116 | options.SetUniformBufferStandardLayout(true); |
| 4117 | } |
| 4118 | if (device_extensions.vk_ext_scalar_block_layout && enabled_features.core12.scalarBlockLayout == VK_TRUE) { |
| 4119 | options.SetScalarBlockLayout(true); |
| 4120 | } |
Caio Marcelo de Oliveira Filho | d1bfbcd | 2021-01-27 01:44:04 -0800 | [diff] [blame] | 4121 | if (device_extensions.vk_khr_workgroup_memory_explicit_layout && |
| 4122 | enabled_features.workgroup_memory_explicit_layout_features.workgroupMemoryExplicitLayoutScalarBlockLayout) { |
| 4123 | options.SetWorkgroupScalarBlockLayout(true); |
| 4124 | } |
Tony-LunarG | 9fe69a4 | 2020-07-23 15:09:37 -0600 | [diff] [blame] | 4125 | } |