sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2021 The Khronos Group Inc. |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | * |
| 15 | * Author: Spencer Fricke <s.fricke@samsung.com> |
| 16 | */ |
| 17 | |
| 18 | #include "shader_module.h" |
| 19 | |
| 20 | #include <sstream> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "vk_layer_data.h" |
| 24 | #include "vk_layer_utils.h" |
Jeremy Gebben | 5d97074 | 2021-05-31 16:04:14 -0600 | [diff] [blame] | 25 | #include "pipeline_state.h" |
| 26 | #include "descriptor_sets.h" |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 27 | |
| 28 | void decoration_set::merge(decoration_set const &other) { |
| 29 | if (other.flags & location_bit) location = other.location; |
| 30 | if (other.flags & component_bit) component = other.component; |
| 31 | if (other.flags & input_attachment_index_bit) input_attachment_index = other.input_attachment_index; |
| 32 | if (other.flags & descriptor_set_bit) descriptor_set = other.descriptor_set; |
| 33 | if (other.flags & binding_bit) binding = other.binding; |
| 34 | if (other.flags & builtin_bit) builtin = other.builtin; |
| 35 | flags |= other.flags; |
| 36 | } |
| 37 | |
| 38 | void decoration_set::add(uint32_t decoration, uint32_t value) { |
| 39 | switch (decoration) { |
| 40 | case spv::DecorationLocation: |
| 41 | flags |= location_bit; |
| 42 | location = value; |
| 43 | break; |
| 44 | case spv::DecorationPatch: |
| 45 | flags |= patch_bit; |
| 46 | break; |
| 47 | case spv::DecorationRelaxedPrecision: |
| 48 | flags |= relaxed_precision_bit; |
| 49 | break; |
| 50 | case spv::DecorationBlock: |
| 51 | flags |= block_bit; |
| 52 | break; |
| 53 | case spv::DecorationBufferBlock: |
| 54 | flags |= buffer_block_bit; |
| 55 | break; |
| 56 | case spv::DecorationComponent: |
| 57 | flags |= component_bit; |
| 58 | component = value; |
| 59 | break; |
| 60 | case spv::DecorationInputAttachmentIndex: |
| 61 | flags |= input_attachment_index_bit; |
| 62 | input_attachment_index = value; |
| 63 | break; |
| 64 | case spv::DecorationDescriptorSet: |
| 65 | flags |= descriptor_set_bit; |
| 66 | descriptor_set = value; |
| 67 | break; |
| 68 | case spv::DecorationBinding: |
| 69 | flags |= binding_bit; |
| 70 | binding = value; |
| 71 | break; |
| 72 | case spv::DecorationNonWritable: |
| 73 | flags |= nonwritable_bit; |
| 74 | break; |
| 75 | case spv::DecorationBuiltIn: |
| 76 | flags |= builtin_bit; |
| 77 | builtin = value; |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | std::string shader_struct_member::GetLocationDesc(uint32_t index_used_bytes) const { |
| 83 | std::string desc = ""; |
| 84 | if (array_length_hierarchy.size() > 0) { |
| 85 | desc += " index:"; |
| 86 | for (const auto block_size : array_block_size) { |
| 87 | desc += "["; |
| 88 | desc += std::to_string(index_used_bytes / (block_size * size)); |
| 89 | desc += "]"; |
| 90 | index_used_bytes = index_used_bytes % (block_size * size); |
| 91 | } |
| 92 | } |
| 93 | const int struct_members_size = static_cast<int>(struct_members.size()); |
| 94 | if (struct_members_size > 0) { |
| 95 | desc += " member:"; |
| 96 | for (int i = struct_members_size - 1; i >= 0; --i) { |
| 97 | if (index_used_bytes > struct_members[i].offset) { |
| 98 | desc += std::to_string(i); |
| 99 | desc += struct_members[i].GetLocationDesc(index_used_bytes - struct_members[i].offset); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } else { |
| 104 | desc += " offset:"; |
| 105 | desc += std::to_string(index_used_bytes); |
| 106 | } |
| 107 | return desc; |
| 108 | } |
| 109 | |
| 110 | static unsigned ExecutionModelToShaderStageFlagBits(unsigned mode) { |
| 111 | switch (mode) { |
| 112 | case spv::ExecutionModelVertex: |
| 113 | return VK_SHADER_STAGE_VERTEX_BIT; |
| 114 | case spv::ExecutionModelTessellationControl: |
| 115 | return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; |
| 116 | case spv::ExecutionModelTessellationEvaluation: |
| 117 | return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; |
| 118 | case spv::ExecutionModelGeometry: |
| 119 | return VK_SHADER_STAGE_GEOMETRY_BIT; |
| 120 | case spv::ExecutionModelFragment: |
| 121 | return VK_SHADER_STAGE_FRAGMENT_BIT; |
| 122 | case spv::ExecutionModelGLCompute: |
| 123 | return VK_SHADER_STAGE_COMPUTE_BIT; |
| 124 | case spv::ExecutionModelRayGenerationNV: |
| 125 | return VK_SHADER_STAGE_RAYGEN_BIT_NV; |
| 126 | case spv::ExecutionModelAnyHitNV: |
| 127 | return VK_SHADER_STAGE_ANY_HIT_BIT_NV; |
| 128 | case spv::ExecutionModelClosestHitNV: |
| 129 | return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV; |
| 130 | case spv::ExecutionModelMissNV: |
| 131 | return VK_SHADER_STAGE_MISS_BIT_NV; |
| 132 | case spv::ExecutionModelIntersectionNV: |
| 133 | return VK_SHADER_STAGE_INTERSECTION_BIT_NV; |
| 134 | case spv::ExecutionModelCallableNV: |
| 135 | return VK_SHADER_STAGE_CALLABLE_BIT_NV; |
| 136 | case spv::ExecutionModelTaskNV: |
| 137 | return VK_SHADER_STAGE_TASK_BIT_NV; |
| 138 | case spv::ExecutionModelMeshNV: |
| 139 | return VK_SHADER_STAGE_MESH_BIT_NV; |
| 140 | default: |
| 141 | return 0; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is |
| 146 | // important for identifying the set of shader resources actually used by an entrypoint, for example. |
| 147 | // Note: we only explore parts of the image which might actually contain ids we care about for the above analyses. |
| 148 | // - NOT the shader input/output interfaces. |
| 149 | // |
| 150 | // TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth |
| 151 | // converting parts of this to be generated from the machine-readable spec instead. |
| 152 | layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::MarkAccessibleIds(spirv_inst_iter entrypoint) const { |
| 153 | layer_data::unordered_set<uint32_t> ids; |
| 154 | layer_data::unordered_set<uint32_t> worklist; |
| 155 | worklist.insert(entrypoint.word(2)); |
| 156 | |
| 157 | while (!worklist.empty()) { |
| 158 | auto id_iter = worklist.begin(); |
| 159 | auto id = *id_iter; |
| 160 | worklist.erase(id_iter); |
| 161 | |
| 162 | auto insn = get_def(id); |
| 163 | if (insn == end()) { |
| 164 | // ID is something we didn't collect in BuildDefIndex. that's OK -- we'll stumble across all kinds of things here |
| 165 | // that we may not care about. |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | // Try to add to the output set |
| 170 | if (!ids.insert(id).second) { |
| 171 | continue; // If we already saw this id, we don't want to walk it again. |
| 172 | } |
| 173 | |
| 174 | switch (insn.opcode()) { |
| 175 | case spv::OpFunction: |
| 176 | // Scan whole body of the function, enlisting anything interesting |
| 177 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 178 | switch (insn.opcode()) { |
| 179 | case spv::OpLoad: |
| 180 | worklist.insert(insn.word(3)); // ptr |
| 181 | break; |
| 182 | case spv::OpStore: |
| 183 | worklist.insert(insn.word(1)); // ptr |
| 184 | break; |
| 185 | case spv::OpAccessChain: |
| 186 | case spv::OpInBoundsAccessChain: |
| 187 | worklist.insert(insn.word(3)); // base ptr |
| 188 | break; |
| 189 | case spv::OpSampledImage: |
| 190 | case spv::OpImageSampleImplicitLod: |
| 191 | case spv::OpImageSampleExplicitLod: |
| 192 | case spv::OpImageSampleDrefImplicitLod: |
| 193 | case spv::OpImageSampleDrefExplicitLod: |
| 194 | case spv::OpImageSampleProjImplicitLod: |
| 195 | case spv::OpImageSampleProjExplicitLod: |
| 196 | case spv::OpImageSampleProjDrefImplicitLod: |
| 197 | case spv::OpImageSampleProjDrefExplicitLod: |
| 198 | case spv::OpImageFetch: |
| 199 | case spv::OpImageGather: |
| 200 | case spv::OpImageDrefGather: |
| 201 | case spv::OpImageRead: |
| 202 | case spv::OpImage: |
| 203 | case spv::OpImageQueryFormat: |
| 204 | case spv::OpImageQueryOrder: |
| 205 | case spv::OpImageQuerySizeLod: |
| 206 | case spv::OpImageQuerySize: |
| 207 | case spv::OpImageQueryLod: |
| 208 | case spv::OpImageQueryLevels: |
| 209 | case spv::OpImageQuerySamples: |
| 210 | case spv::OpImageSparseSampleImplicitLod: |
| 211 | case spv::OpImageSparseSampleExplicitLod: |
| 212 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 213 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 214 | case spv::OpImageSparseSampleProjImplicitLod: |
| 215 | case spv::OpImageSparseSampleProjExplicitLod: |
| 216 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 217 | case spv::OpImageSparseSampleProjDrefExplicitLod: |
| 218 | case spv::OpImageSparseFetch: |
| 219 | case spv::OpImageSparseGather: |
| 220 | case spv::OpImageSparseDrefGather: |
| 221 | case spv::OpImageTexelPointer: |
| 222 | worklist.insert(insn.word(3)); // Image or sampled image |
| 223 | break; |
| 224 | case spv::OpImageWrite: |
| 225 | worklist.insert(insn.word(1)); // Image -- different operand order to above |
| 226 | break; |
| 227 | case spv::OpFunctionCall: |
| 228 | for (uint32_t i = 3; i < insn.len(); i++) { |
| 229 | worklist.insert(insn.word(i)); // fn itself, and all args |
| 230 | } |
| 231 | break; |
| 232 | |
| 233 | case spv::OpExtInst: |
| 234 | for (uint32_t i = 5; i < insn.len(); i++) { |
| 235 | worklist.insert(insn.word(i)); // Operands to ext inst |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | default: { |
| 240 | if (AtomicOperation(insn.opcode())) { |
| 241 | if (insn.opcode() == spv::OpAtomicStore) { |
| 242 | worklist.insert(insn.word(1)); // ptr |
| 243 | } else { |
| 244 | worklist.insert(insn.word(3)); // ptr |
| 245 | } |
| 246 | } |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return ids; |
| 256 | } |
| 257 | |
| 258 | void SHADER_MODULE_STATE::ProcessExecutionModes(const spirv_inst_iter &entrypoint, PIPELINE_STATE *pipeline) const { |
| 259 | auto entrypoint_id = entrypoint.word(2); |
| 260 | bool is_point_mode = false; |
| 261 | |
| 262 | auto it = execution_mode_inst.find(entrypoint_id); |
| 263 | if (it != execution_mode_inst.end()) { |
| 264 | for (auto insn : it->second) { |
| 265 | switch (insn.word(2)) { |
| 266 | case spv::ExecutionModePointMode: |
| 267 | // In tessellation shaders, PointMode is separate and trumps the tessellation topology. |
| 268 | is_point_mode = true; |
| 269 | break; |
| 270 | |
| 271 | case spv::ExecutionModeOutputPoints: |
| 272 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 273 | break; |
| 274 | |
| 275 | case spv::ExecutionModeIsolines: |
| 276 | case spv::ExecutionModeOutputLineStrip: |
| 277 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; |
| 278 | break; |
| 279 | |
| 280 | case spv::ExecutionModeTriangles: |
| 281 | case spv::ExecutionModeQuads: |
| 282 | case spv::ExecutionModeOutputTriangleStrip: |
| 283 | case spv::ExecutionModeOutputTrianglesNV: |
| 284 | pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (is_point_mode) pipeline->topology_at_rasterizer = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; |
| 291 | } |
| 292 | |
| 293 | void SHADER_MODULE_STATE::BuildDefIndex() { |
| 294 | function_set func_set = {}; |
| 295 | EntryPoint *entry_point = nullptr; |
| 296 | |
| 297 | for (auto insn : *this) { |
| 298 | // offset is not 0, it means it's updated and the offset is in a Function. |
| 299 | if (func_set.offset) { |
| 300 | func_set.op_lists.emplace(insn.opcode(), insn.offset()); |
| 301 | } else if (entry_point) { |
| 302 | entry_point->decorate_list.emplace(insn.opcode(), insn.offset()); |
| 303 | } |
| 304 | |
| 305 | switch (insn.opcode()) { |
| 306 | // Types |
| 307 | case spv::OpTypeVoid: |
| 308 | case spv::OpTypeBool: |
| 309 | case spv::OpTypeInt: |
| 310 | case spv::OpTypeFloat: |
| 311 | case spv::OpTypeVector: |
| 312 | case spv::OpTypeMatrix: |
| 313 | case spv::OpTypeImage: |
| 314 | case spv::OpTypeSampler: |
| 315 | case spv::OpTypeSampledImage: |
| 316 | case spv::OpTypeArray: |
| 317 | case spv::OpTypeRuntimeArray: |
| 318 | case spv::OpTypeStruct: |
| 319 | case spv::OpTypeOpaque: |
| 320 | case spv::OpTypePointer: |
| 321 | case spv::OpTypeFunction: |
| 322 | case spv::OpTypeEvent: |
| 323 | case spv::OpTypeDeviceEvent: |
| 324 | case spv::OpTypeReserveId: |
| 325 | case spv::OpTypeQueue: |
| 326 | case spv::OpTypePipe: |
| 327 | case spv::OpTypeAccelerationStructureNV: |
| 328 | case spv::OpTypeCooperativeMatrixNV: |
| 329 | def_index[insn.word(1)] = insn.offset(); |
| 330 | break; |
| 331 | |
| 332 | // Fixed constants |
| 333 | case spv::OpConstantTrue: |
| 334 | case spv::OpConstantFalse: |
| 335 | case spv::OpConstant: |
| 336 | case spv::OpConstantComposite: |
| 337 | case spv::OpConstantSampler: |
| 338 | case spv::OpConstantNull: |
| 339 | def_index[insn.word(2)] = insn.offset(); |
| 340 | break; |
| 341 | |
| 342 | // Specialization constants |
| 343 | case spv::OpSpecConstantTrue: |
| 344 | case spv::OpSpecConstantFalse: |
| 345 | case spv::OpSpecConstant: |
| 346 | case spv::OpSpecConstantComposite: |
| 347 | case spv::OpSpecConstantOp: |
| 348 | def_index[insn.word(2)] = insn.offset(); |
| 349 | break; |
| 350 | |
| 351 | // Variables |
| 352 | case spv::OpVariable: |
| 353 | def_index[insn.word(2)] = insn.offset(); |
| 354 | break; |
| 355 | |
| 356 | // Functions |
| 357 | case spv::OpFunction: |
| 358 | def_index[insn.word(2)] = insn.offset(); |
| 359 | func_set.id = insn.word(2); |
| 360 | func_set.offset = insn.offset(); |
| 361 | func_set.op_lists.clear(); |
| 362 | break; |
| 363 | |
| 364 | // Decorations |
| 365 | case spv::OpDecorate: { |
| 366 | auto target_id = insn.word(1); |
| 367 | decorations[target_id].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u); |
| 368 | decoration_inst.push_back(insn); |
| 369 | if (insn.word(2) == spv::DecorationBuiltIn) { |
| 370 | builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(3))); |
Nathaniel Cesario | cf69bda | 2021-06-22 13:23:42 -0600 | [diff] [blame^] | 371 | } else if (insn.word(2) == spv::DecorationSpecId) { |
| 372 | spec_const_map[insn.word(3)] = target_id; |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | } break; |
| 376 | case spv::OpGroupDecorate: { |
| 377 | auto const &src = decorations[insn.word(1)]; |
| 378 | for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src); |
| 379 | } break; |
| 380 | case spv::OpMemberDecorate: { |
| 381 | member_decoration_inst.push_back(insn); |
| 382 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 383 | builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(4))); |
| 384 | } |
| 385 | } break; |
| 386 | |
| 387 | // Entry points ... add to the entrypoint table |
| 388 | case spv::OpEntryPoint: { |
| 389 | if (entry_point != nullptr) { |
| 390 | multiple_entry_points = true; |
| 391 | } |
| 392 | |
| 393 | // Entry points do not have an id (the id is the function id) and thus need their own table |
| 394 | auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3)); |
| 395 | auto execution_model = insn.word(1); |
| 396 | auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model); |
| 397 | entry_points.emplace(entrypoint_name, |
| 398 | EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)}); |
| 399 | |
| 400 | auto range = entry_points.equal_range(entrypoint_name); |
| 401 | for (auto it = range.first; it != range.second; ++it) { |
| 402 | if (it->second.offset == insn.offset()) { |
| 403 | entry_point = &(it->second); |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | assert(entry_point != nullptr); |
| 408 | break; |
| 409 | } |
| 410 | case spv::OpFunctionEnd: { |
| 411 | assert(entry_point != nullptr); |
| 412 | func_set.length = insn.offset() - func_set.offset; |
| 413 | entry_point->function_set_list.emplace_back(func_set); |
| 414 | break; |
| 415 | } |
| 416 | |
| 417 | // Copy operations |
| 418 | case spv::OpCopyLogical: |
| 419 | case spv::OpCopyObject: { |
| 420 | def_index[insn.word(2)] = insn.offset(); |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | // Execution Mode |
| 425 | case spv::OpExecutionMode: { |
| 426 | execution_mode_inst[insn.word(1)].push_back(insn); |
| 427 | } break; |
| 428 | |
| 429 | default: |
| 430 | // We don't care about any other defs for now. |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | std::vector<uint32_t> SHADER_MODULE_STATE::PreprocessShaderBinary(uint32_t *src_binary, size_t binary_size, spv_target_env env) { |
| 437 | std::vector<uint32_t> src(src_binary, src_binary + binary_size / sizeof(uint32_t)); |
| 438 | |
| 439 | // Check if there are any group decoration instructions, and flatten them if found. |
| 440 | bool has_group_decoration = false; |
| 441 | bool done = false; |
| 442 | |
| 443 | // Walk through the first part of the SPIR-V module, looking for group decoration and specialization constant instructions. |
| 444 | // Skip the header (5 words). |
| 445 | auto itr = spirv_inst_iter(src.begin(), src.begin() + 5); |
| 446 | auto itrend = spirv_inst_iter(src.begin(), src.end()); |
| 447 | while (itr != itrend && !done) { |
| 448 | spv::Op opcode = (spv::Op)itr.opcode(); |
| 449 | switch (opcode) { |
| 450 | case spv::OpDecorationGroup: |
| 451 | case spv::OpGroupDecorate: |
| 452 | case spv::OpGroupMemberDecorate: |
| 453 | has_group_decoration = true; |
| 454 | break; |
| 455 | case spv::OpSpecConstantTrue: |
| 456 | case spv::OpSpecConstantFalse: |
| 457 | case spv::OpSpecConstant: |
| 458 | case spv::OpSpecConstantComposite: |
| 459 | case spv::OpSpecConstantOp: |
| 460 | has_specialization_constants = true; |
| 461 | break; |
| 462 | case spv::OpFunction: |
| 463 | // An OpFunction indicates there are no more decorations |
| 464 | done = true; |
| 465 | break; |
| 466 | default: |
| 467 | break; |
| 468 | } |
| 469 | itr++; |
| 470 | } |
| 471 | |
| 472 | if (has_group_decoration) { |
| 473 | spvtools::Optimizer optimizer(env); |
| 474 | optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass()); |
| 475 | std::vector<uint32_t> optimized_binary; |
| 476 | // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator |
| 477 | auto result = |
| 478 | optimizer.Run(src_binary, binary_size / sizeof(uint32_t), &optimized_binary, spvtools::ValidatorOptions(), true); |
| 479 | if (result) { |
| 480 | return optimized_binary; |
| 481 | } |
| 482 | } |
| 483 | // Return the original module. |
| 484 | return src; |
| 485 | } |
| 486 | |
| 487 | static char const *StorageClassName(unsigned sc) { |
| 488 | switch (sc) { |
| 489 | case spv::StorageClassInput: |
| 490 | return "input"; |
| 491 | case spv::StorageClassOutput: |
| 492 | return "output"; |
| 493 | case spv::StorageClassUniformConstant: |
| 494 | return "const uniform"; |
| 495 | case spv::StorageClassUniform: |
| 496 | return "uniform"; |
| 497 | case spv::StorageClassWorkgroup: |
| 498 | return "workgroup local"; |
| 499 | case spv::StorageClassCrossWorkgroup: |
| 500 | return "workgroup global"; |
| 501 | case spv::StorageClassPrivate: |
| 502 | return "private global"; |
| 503 | case spv::StorageClassFunction: |
| 504 | return "function"; |
| 505 | case spv::StorageClassGeneric: |
| 506 | return "generic"; |
| 507 | case spv::StorageClassAtomicCounter: |
| 508 | return "atomic counter"; |
| 509 | case spv::StorageClassImage: |
| 510 | return "image"; |
| 511 | case spv::StorageClassPushConstant: |
| 512 | return "push constant"; |
| 513 | case spv::StorageClassStorageBuffer: |
| 514 | return "storage buffer"; |
| 515 | default: |
| 516 | return "unknown"; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | void SHADER_MODULE_STATE::DescribeTypeInner(std::ostringstream &ss, unsigned type) const { |
| 521 | auto insn = get_def(type); |
| 522 | assert(insn != end()); |
| 523 | |
| 524 | switch (insn.opcode()) { |
| 525 | case spv::OpTypeBool: |
| 526 | ss << "bool"; |
| 527 | break; |
| 528 | case spv::OpTypeInt: |
| 529 | ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2); |
| 530 | break; |
| 531 | case spv::OpTypeFloat: |
| 532 | ss << "float" << insn.word(2); |
| 533 | break; |
| 534 | case spv::OpTypeVector: |
| 535 | ss << "vec" << insn.word(3) << " of "; |
| 536 | DescribeTypeInner(ss, insn.word(2)); |
| 537 | break; |
| 538 | case spv::OpTypeMatrix: |
| 539 | ss << "mat" << insn.word(3) << " of "; |
| 540 | DescribeTypeInner(ss, insn.word(2)); |
| 541 | break; |
| 542 | case spv::OpTypeArray: |
| 543 | ss << "arr[" << GetConstantValueById(insn.word(3)) << "] of "; |
| 544 | DescribeTypeInner(ss, insn.word(2)); |
| 545 | break; |
| 546 | case spv::OpTypeRuntimeArray: |
| 547 | ss << "runtime arr[] of "; |
| 548 | DescribeTypeInner(ss, insn.word(2)); |
| 549 | break; |
| 550 | case spv::OpTypePointer: |
| 551 | ss << "ptr to " << StorageClassName(insn.word(2)) << " "; |
| 552 | DescribeTypeInner(ss, insn.word(3)); |
| 553 | break; |
| 554 | case spv::OpTypeStruct: { |
| 555 | ss << "struct of ("; |
| 556 | for (unsigned i = 2; i < insn.len(); i++) { |
| 557 | DescribeTypeInner(ss, insn.word(i)); |
| 558 | if (i == insn.len() - 1) { |
| 559 | ss << ")"; |
| 560 | } else { |
| 561 | ss << ", "; |
| 562 | } |
| 563 | } |
| 564 | break; |
| 565 | } |
| 566 | case spv::OpTypeSampler: |
| 567 | ss << "sampler"; |
| 568 | break; |
| 569 | case spv::OpTypeSampledImage: |
| 570 | ss << "sampler+"; |
| 571 | DescribeTypeInner(ss, insn.word(2)); |
| 572 | break; |
| 573 | case spv::OpTypeImage: |
| 574 | ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")"; |
| 575 | break; |
| 576 | case spv::OpTypeAccelerationStructureNV: |
| 577 | ss << "accelerationStruture"; |
| 578 | break; |
| 579 | default: |
| 580 | ss << "oddtype"; |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | std::string SHADER_MODULE_STATE::DescribeType(unsigned type) const { |
| 586 | std::ostringstream ss; |
| 587 | DescribeTypeInner(ss, type); |
| 588 | return ss.str(); |
| 589 | } |
| 590 | |
| 591 | const SHADER_MODULE_STATE::EntryPoint *SHADER_MODULE_STATE::FindEntrypointStruct(char const *name, |
| 592 | VkShaderStageFlagBits stageBits) const { |
| 593 | auto range = entry_points.equal_range(name); |
| 594 | for (auto it = range.first; it != range.second; ++it) { |
| 595 | if (it->second.stage == stageBits) { |
| 596 | return &(it->second); |
| 597 | } |
| 598 | } |
| 599 | return nullptr; |
| 600 | } |
| 601 | |
| 602 | spirv_inst_iter SHADER_MODULE_STATE::FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const { |
| 603 | auto range = entry_points.equal_range(name); |
| 604 | for (auto it = range.first; it != range.second; ++it) { |
| 605 | if (it->second.stage == stageBits) { |
| 606 | return at(it->second.offset); |
| 607 | } |
| 608 | } |
| 609 | return end(); |
| 610 | } |
| 611 | |
| 612 | // Because the following is legal, need the entry point |
| 613 | // OpEntryPoint GLCompute %main "name_a" |
| 614 | // OpEntryPoint GLCompute %main "name_b" |
| 615 | bool SHADER_MODULE_STATE::FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y, |
| 616 | uint32_t &local_size_z) const { |
| 617 | auto entrypoint_id = entrypoint.word(2); |
| 618 | auto it = execution_mode_inst.find(entrypoint_id); |
| 619 | if (it != execution_mode_inst.end()) { |
| 620 | for (auto insn : it->second) { |
| 621 | // Future Note: For now, Vulkan doesn't have a valid mode that can makes use of OpExecutionModeId |
| 622 | // In the future if something like LocalSizeId is supported, the <id> will need to be checked also |
| 623 | assert(insn.opcode() == spv::OpExecutionMode); |
| 624 | if (insn.word(2) == spv::ExecutionModeLocalSize) { |
| 625 | local_size_x = insn.word(3); |
| 626 | local_size_y = insn.word(4); |
| 627 | local_size_z = insn.word(5); |
| 628 | return true; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | // If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction. |
| 636 | // Otherwise, returns src->end(). |
| 637 | spirv_inst_iter SHADER_MODULE_STATE::GetConstantDef(unsigned id) const { |
| 638 | auto value = get_def(id); |
| 639 | |
| 640 | // If id is a copy, see where it was copied from |
| 641 | if ((end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) { |
| 642 | id = value.word(3); |
| 643 | value = get_def(id); |
| 644 | } |
| 645 | |
| 646 | if ((end() != value) && (value.opcode() == spv::OpConstant)) { |
| 647 | return value; |
| 648 | } |
| 649 | return end(); |
| 650 | } |
| 651 | |
| 652 | // Either returns the constant value described by the instruction at id, or 1 |
| 653 | uint32_t SHADER_MODULE_STATE::GetConstantValueById(unsigned id) const { |
| 654 | auto value = GetConstantDef(id); |
| 655 | |
| 656 | if (end() == value) { |
| 657 | // TODO: Either ensure that the specialization transform is already performed on a module we're |
| 658 | // considering here, OR -- specialize on the fly now. |
| 659 | return 1; |
| 660 | } |
| 661 | return GetConstantValue(value); |
| 662 | } |
| 663 | |
| 664 | // Returns an int32_t corresponding to the spv::Dim of the given resource, when positive, and corresponding to an unknown type, when |
| 665 | // negative. |
| 666 | int32_t SHADER_MODULE_STATE::GetShaderResourceDimensionality(const interface_var &resource) const { |
| 667 | auto type = get_def(resource.type_id); |
| 668 | while (true) { |
| 669 | switch (type.opcode()) { |
| 670 | case spv::OpTypeSampledImage: |
| 671 | type = get_def(type.word(2)); |
| 672 | break; |
| 673 | case spv::OpTypePointer: |
| 674 | type = get_def(type.word(3)); |
| 675 | break; |
| 676 | case spv::OpTypeImage: |
| 677 | return type.word(3); |
| 678 | default: |
| 679 | return -1; |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | unsigned SHADER_MODULE_STATE::GetLocationsConsumedByType(unsigned type, bool strip_array_level) const { |
| 685 | auto insn = get_def(type); |
| 686 | assert(insn != end()); |
| 687 | |
| 688 | switch (insn.opcode()) { |
| 689 | case spv::OpTypePointer: |
| 690 | // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing |
| 691 | // pointers around. |
| 692 | return GetLocationsConsumedByType(insn.word(3), strip_array_level); |
| 693 | case spv::OpTypeArray: |
| 694 | if (strip_array_level) { |
| 695 | return GetLocationsConsumedByType(insn.word(2), false); |
| 696 | } else { |
| 697 | return GetConstantValueById(insn.word(3)) * GetLocationsConsumedByType(insn.word(2), false); |
| 698 | } |
| 699 | case spv::OpTypeMatrix: |
| 700 | // Num locations is the dimension * element size |
| 701 | return insn.word(3) * GetLocationsConsumedByType(insn.word(2), false); |
| 702 | case spv::OpTypeVector: { |
| 703 | auto scalar_type = get_def(insn.word(2)); |
| 704 | auto bit_width = |
| 705 | (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32; |
| 706 | |
| 707 | // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two. |
| 708 | return (bit_width * insn.word(3) + 127) / 128; |
| 709 | } |
| 710 | default: |
| 711 | // Everything else is just 1. |
| 712 | return 1; |
| 713 | |
| 714 | // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations. |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | unsigned SHADER_MODULE_STATE::GetComponentsConsumedByType(unsigned type, bool strip_array_level) const { |
| 719 | auto insn = get_def(type); |
| 720 | assert(insn != end()); |
| 721 | |
| 722 | switch (insn.opcode()) { |
| 723 | case spv::OpTypePointer: |
| 724 | // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing |
| 725 | // pointers around. |
| 726 | return GetComponentsConsumedByType(insn.word(3), strip_array_level); |
| 727 | case spv::OpTypeStruct: { |
| 728 | uint32_t sum = 0; |
| 729 | for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct |
| 730 | sum += GetComponentsConsumedByType(insn.word(i), false); |
| 731 | } |
| 732 | return sum; |
| 733 | } |
| 734 | case spv::OpTypeArray: |
| 735 | if (strip_array_level) { |
| 736 | return GetComponentsConsumedByType(insn.word(2), false); |
| 737 | } else { |
| 738 | return GetConstantValueById(insn.word(3)) * GetComponentsConsumedByType(insn.word(2), false); |
| 739 | } |
| 740 | case spv::OpTypeMatrix: |
| 741 | // Num locations is the dimension * element size |
| 742 | return insn.word(3) * GetComponentsConsumedByType(insn.word(2), false); |
| 743 | case spv::OpTypeVector: { |
| 744 | auto scalar_type = get_def(insn.word(2)); |
| 745 | auto bit_width = |
| 746 | (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32; |
| 747 | // One component is 32-bit |
| 748 | return (bit_width * insn.word(3) + 31) / 32; |
| 749 | } |
| 750 | case spv::OpTypeFloat: { |
| 751 | auto bit_width = insn.word(2); |
| 752 | return (bit_width + 31) / 32; |
| 753 | } |
| 754 | case spv::OpTypeInt: { |
| 755 | auto bit_width = insn.word(2); |
| 756 | return (bit_width + 31) / 32; |
| 757 | } |
| 758 | case spv::OpConstant: |
| 759 | return GetComponentsConsumedByType(insn.word(1), false); |
| 760 | default: |
| 761 | return 0; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | // characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above. |
| 766 | // also used for input attachments, as we statically know their format. |
| 767 | unsigned SHADER_MODULE_STATE::GetFundamentalType(unsigned type) const { |
| 768 | auto insn = get_def(type); |
| 769 | assert(insn != end()); |
| 770 | |
| 771 | switch (insn.opcode()) { |
| 772 | case spv::OpTypeInt: |
| 773 | return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT; |
| 774 | case spv::OpTypeFloat: |
| 775 | return FORMAT_TYPE_FLOAT; |
| 776 | case spv::OpTypeVector: |
| 777 | case spv::OpTypeMatrix: |
| 778 | case spv::OpTypeArray: |
| 779 | case spv::OpTypeRuntimeArray: |
| 780 | case spv::OpTypeImage: |
| 781 | return GetFundamentalType(insn.word(2)); |
| 782 | case spv::OpTypePointer: |
| 783 | return GetFundamentalType(insn.word(3)); |
| 784 | |
| 785 | default: |
| 786 | return 0; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | spirv_inst_iter SHADER_MODULE_STATE::GetStructType(spirv_inst_iter def, bool is_array_of_verts) const { |
| 791 | while (true) { |
| 792 | if (def.opcode() == spv::OpTypePointer) { |
| 793 | def = get_def(def.word(3)); |
| 794 | } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) { |
| 795 | def = get_def(def.word(2)); |
| 796 | is_array_of_verts = false; |
| 797 | } else if (def.opcode() == spv::OpTypeStruct) { |
| 798 | return def; |
| 799 | } else { |
| 800 | return end(); |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | void SHADER_MODULE_STATE::DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &memberDecorate_offsets, |
| 806 | shader_struct_member &data) const { |
| 807 | const auto struct_it = GetStructType(it, false); |
| 808 | assert(struct_it != end()); |
| 809 | data.size = 0; |
| 810 | |
| 811 | shader_struct_member data1; |
| 812 | uint32_t i = 2; |
| 813 | uint32_t local_offset = 0; |
| 814 | std::vector<uint32_t> offsets; |
| 815 | offsets.resize(struct_it.len() - i); |
| 816 | |
| 817 | // The members of struct in SPRIV_R aren't always sort, so we need to know their order. |
| 818 | for (const auto offset : memberDecorate_offsets) { |
| 819 | const auto member_decorate = at(offset); |
| 820 | if (member_decorate.word(1) != struct_it.word(1)) { |
| 821 | continue; |
| 822 | } |
| 823 | |
| 824 | offsets[member_decorate.word(2)] = member_decorate.word(4); |
| 825 | } |
| 826 | |
| 827 | for (const auto offset : offsets) { |
| 828 | local_offset = offset; |
| 829 | data1 = {}; |
| 830 | data1.root = data.root; |
| 831 | data1.offset = local_offset; |
| 832 | auto def_member = get_def(struct_it.word(i)); |
| 833 | |
| 834 | // Array could be multi-dimensional |
| 835 | while (def_member.opcode() == spv::OpTypeArray) { |
| 836 | const auto len_id = def_member.word(3); |
| 837 | const auto def_len = get_def(len_id); |
| 838 | data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length |
| 839 | def_member = get_def(def_member.word(2)); |
| 840 | } |
| 841 | |
| 842 | if (def_member.opcode() == spv::OpTypeStruct) { |
| 843 | DefineStructMember(def_member, memberDecorate_offsets, data1); |
| 844 | } else if (def_member.opcode() == spv::OpTypePointer) { |
| 845 | if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) { |
| 846 | // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address |
| 847 | // that "points to something." |
| 848 | data1.size = 8; |
| 849 | } else { |
| 850 | // If it's OpTypePointer. it means the member is a buffer, the type will be TypePointer, and then struct |
| 851 | DefineStructMember(def_member, memberDecorate_offsets, data1); |
| 852 | } |
| 853 | } else { |
| 854 | if (def_member.opcode() == spv::OpTypeMatrix) { |
| 855 | data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector. |
| 856 | def_member = get_def(def_member.word(2)); |
| 857 | } |
| 858 | |
| 859 | if (def_member.opcode() == spv::OpTypeVector) { |
| 860 | data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length |
| 861 | def_member = get_def(def_member.word(2)); |
| 862 | } |
| 863 | |
| 864 | // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte. |
| 865 | data1.size = (def_member.word(2) / 8); |
| 866 | } |
| 867 | const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size(); |
| 868 | if (array_length_hierarchy_szie > 0) { |
| 869 | data1.array_block_size.resize(array_length_hierarchy_szie, 1); |
| 870 | |
| 871 | for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) { |
| 872 | data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2]; |
| 873 | } |
| 874 | } |
| 875 | data.struct_members.emplace_back(data1); |
| 876 | ++i; |
| 877 | } |
| 878 | uint32_t total_array_length = 1; |
| 879 | for (const auto length : data1.array_length_hierarchy) { |
| 880 | total_array_length *= length; |
| 881 | } |
| 882 | data.size = local_offset + data1.size * total_array_length; |
| 883 | } |
| 884 | |
| 885 | static uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) { |
| 886 | int array_indices_size = static_cast<int>(array_indices.size()); |
| 887 | if (array_indices_size) { |
| 888 | uint32_t array_index = 0; |
| 889 | uint32_t i = 0; |
| 890 | for (const auto index : array_indices) { |
| 891 | array_index += (data.array_block_size[i] * index); |
| 892 | ++i; |
| 893 | } |
| 894 | offset += (array_index * data.size); |
| 895 | } |
| 896 | return offset; |
| 897 | } |
| 898 | |
| 899 | static void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) { |
| 900 | int array_indices_size = static_cast<int>(array_indices.size()); |
| 901 | uint32_t block_memory_size = data.size; |
| 902 | for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) { |
| 903 | block_memory_size *= data.array_length_hierarchy[i]; |
| 904 | } |
| 905 | |
| 906 | offset = UpdateOffset(offset, array_indices, data); |
| 907 | |
| 908 | uint32_t end = offset + block_memory_size; |
| 909 | auto used_bytes = data.GetUsedbytes(); |
| 910 | if (used_bytes->size() < end) { |
| 911 | used_bytes->resize(end, 0); |
| 912 | } |
| 913 | std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size)); |
| 914 | } |
| 915 | |
| 916 | void SHADER_MODULE_STATE::RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index, |
| 917 | spirv_inst_iter &access_chain_it, const shader_struct_member &data) const { |
| 918 | if (access_chain_word_index < access_chain_it.len()) { |
| 919 | if (data.array_length_hierarchy.size() > array_indices.size()) { |
| 920 | auto def_it = get_def(access_chain_it.word(access_chain_word_index)); |
| 921 | ++access_chain_word_index; |
| 922 | |
| 923 | if (def_it != end() && def_it.opcode() == spv::OpConstant) { |
| 924 | array_indices.emplace_back(def_it.word(3)); |
| 925 | RunUsedArray(offset, array_indices, access_chain_word_index, access_chain_it, data); |
| 926 | } else { |
| 927 | // If it is a variable, set the all array is used. |
| 928 | if (access_chain_word_index < access_chain_it.len()) { |
| 929 | uint32_t array_length = data.array_length_hierarchy[array_indices.size()]; |
| 930 | for (uint32_t i = 0; i < array_length; ++i) { |
| 931 | auto array_indices2 = array_indices; |
| 932 | array_indices2.emplace_back(i); |
| 933 | RunUsedArray(offset, array_indices2, access_chain_word_index, access_chain_it, data); |
| 934 | } |
| 935 | } else { |
| 936 | SetUsedBytes(offset, array_indices, data); |
| 937 | } |
| 938 | } |
| 939 | } else { |
| 940 | offset = UpdateOffset(offset, array_indices, data); |
| 941 | RunUsedStruct(offset, access_chain_word_index, access_chain_it, data); |
| 942 | } |
| 943 | } else { |
| 944 | SetUsedBytes(offset, array_indices, data); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | void SHADER_MODULE_STATE::RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it, |
| 949 | const shader_struct_member &data) const { |
| 950 | std::vector<uint32_t> array_indices_emptry; |
| 951 | |
| 952 | if (access_chain_word_index < access_chain_it.len()) { |
| 953 | auto strcut_member_index = GetConstantValueById(access_chain_it.word(access_chain_word_index)); |
| 954 | ++access_chain_word_index; |
| 955 | |
| 956 | auto data1 = data.struct_members[strcut_member_index]; |
| 957 | RunUsedArray(offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | void SHADER_MODULE_STATE::SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list, |
| 962 | const shader_struct_member &data) const { |
| 963 | for (const auto &func_set : function_set_list) { |
| 964 | auto range = func_set.op_lists.equal_range(spv::OpAccessChain); |
| 965 | for (auto it = range.first; it != range.second; ++it) { |
| 966 | auto access_chain = at(it->second); |
| 967 | if (access_chain.word(3) == variable_id) { |
| 968 | RunUsedStruct(0, 4, access_chain, data); |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | void SHADER_MODULE_STATE::SetPushConstantUsedInShader() { |
| 975 | for (auto &entrypoint : entry_points) { |
| 976 | auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable); |
| 977 | for (auto it = range.first; it != range.second; ++it) { |
| 978 | const auto def_insn = at(it->second); |
| 979 | |
| 980 | if (def_insn.word(3) == spv::StorageClassPushConstant) { |
| 981 | spirv_inst_iter type = get_def(def_insn.word(1)); |
| 982 | const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate); |
| 983 | std::vector<uint32_t> offsets; |
| 984 | |
| 985 | for (auto it2 = range2.first; it2 != range2.second; ++it2) { |
| 986 | auto member_decorate = at(it2->second); |
| 987 | if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) { |
| 988 | offsets.emplace_back(member_decorate.offset()); |
| 989 | } |
| 990 | } |
| 991 | entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader; |
| 992 | DefineStructMember(type, offsets, entrypoint.second.push_constant_used_in_shader); |
| 993 | SetUsedStructMember(def_insn.word(2), entrypoint.second.function_set_list, |
| 994 | entrypoint.second.push_constant_used_in_shader); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | uint32_t SHADER_MODULE_STATE::DescriptorTypeToReqs(uint32_t type_id) const { |
| 1001 | auto type = get_def(type_id); |
| 1002 | |
| 1003 | while (true) { |
| 1004 | switch (type.opcode()) { |
| 1005 | case spv::OpTypeArray: |
| 1006 | case spv::OpTypeRuntimeArray: |
| 1007 | case spv::OpTypeSampledImage: |
| 1008 | type = get_def(type.word(2)); |
| 1009 | break; |
| 1010 | case spv::OpTypePointer: |
| 1011 | type = get_def(type.word(3)); |
| 1012 | break; |
| 1013 | case spv::OpTypeImage: { |
| 1014 | auto dim = type.word(3); |
| 1015 | auto arrayed = type.word(5); |
| 1016 | auto msaa = type.word(6); |
| 1017 | |
| 1018 | uint32_t bits = 0; |
| 1019 | switch (GetFundamentalType(type.word(2))) { |
| 1020 | case FORMAT_TYPE_FLOAT: |
| 1021 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT; |
| 1022 | break; |
| 1023 | case FORMAT_TYPE_UINT: |
| 1024 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT; |
| 1025 | break; |
| 1026 | case FORMAT_TYPE_SINT: |
| 1027 | bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT; |
| 1028 | break; |
| 1029 | default: |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | switch (dim) { |
| 1034 | case spv::Dim1D: |
| 1035 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D; |
| 1036 | return bits; |
| 1037 | case spv::Dim2D: |
| 1038 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 1039 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D; |
| 1040 | return bits; |
| 1041 | case spv::Dim3D: |
| 1042 | bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D; |
| 1043 | return bits; |
| 1044 | case spv::DimCube: |
| 1045 | bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE; |
| 1046 | return bits; |
| 1047 | case spv::DimSubpassData: |
| 1048 | bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE; |
| 1049 | return bits; |
| 1050 | default: // buffer, etc. |
| 1051 | return bits; |
| 1052 | } |
| 1053 | } |
| 1054 | default: |
| 1055 | return 0; |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | // For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to. |
| 1061 | // This function examines instructions in the static call tree for a write to this variable. |
| 1062 | bool SHADER_MODULE_STATE::IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const { |
| 1063 | auto type = builtin_instr.opcode(); |
| 1064 | uint32_t target_id = builtin_instr.word(1); |
| 1065 | bool init_complete = false; |
| 1066 | |
| 1067 | if (type == spv::OpMemberDecorate) { |
| 1068 | // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs |
| 1069 | auto insn = entrypoint; |
| 1070 | while (!init_complete && (insn.opcode() != spv::OpFunction)) { |
| 1071 | switch (insn.opcode()) { |
| 1072 | case spv::OpTypePointer: |
| 1073 | if ((insn.word(3) == target_id) && (insn.word(2) == spv::StorageClassOutput)) { |
| 1074 | target_id = insn.word(1); |
| 1075 | } |
| 1076 | break; |
| 1077 | case spv::OpVariable: |
| 1078 | if (insn.word(1) == target_id) { |
| 1079 | target_id = insn.word(2); |
| 1080 | init_complete = true; |
| 1081 | } |
| 1082 | break; |
| 1083 | } |
| 1084 | insn++; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | if (!init_complete && (type == spv::OpMemberDecorate)) return false; |
| 1089 | |
| 1090 | bool found_write = false; |
| 1091 | layer_data::unordered_set<uint32_t> worklist; |
| 1092 | worklist.insert(entrypoint.word(2)); |
| 1093 | |
| 1094 | // Follow instructions in call graph looking for writes to target |
| 1095 | while (!worklist.empty() && !found_write) { |
| 1096 | auto id_iter = worklist.begin(); |
| 1097 | auto id = *id_iter; |
| 1098 | worklist.erase(id_iter); |
| 1099 | |
| 1100 | auto insn = get_def(id); |
| 1101 | if (insn == end()) { |
| 1102 | continue; |
| 1103 | } |
| 1104 | |
| 1105 | if (insn.opcode() == spv::OpFunction) { |
| 1106 | // Scan body of function looking for other function calls or items in our ID chain |
| 1107 | while (++insn, insn.opcode() != spv::OpFunctionEnd) { |
| 1108 | switch (insn.opcode()) { |
| 1109 | case spv::OpAccessChain: |
| 1110 | if (insn.word(3) == target_id) { |
| 1111 | if (type == spv::OpMemberDecorate) { |
| 1112 | auto value = GetConstantValueById(insn.word(4)); |
| 1113 | if (value == builtin_instr.word(2)) { |
| 1114 | target_id = insn.word(2); |
| 1115 | } |
| 1116 | } else { |
| 1117 | target_id = insn.word(2); |
| 1118 | } |
| 1119 | } |
| 1120 | break; |
| 1121 | case spv::OpStore: |
| 1122 | if (insn.word(1) == target_id) { |
| 1123 | found_write = true; |
| 1124 | } |
| 1125 | break; |
| 1126 | case spv::OpFunctionCall: |
| 1127 | worklist.insert(insn.word(3)); |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | return found_write; |
| 1134 | } |
| 1135 | |
| 1136 | // Used by the collection functions to help aid in state tracking |
| 1137 | struct shader_module_used_operators { |
| 1138 | bool updated; |
| 1139 | std::vector<unsigned> imagwrite_members; |
| 1140 | std::vector<unsigned> atomic_members; |
| 1141 | std::vector<unsigned> store_members; |
| 1142 | std::vector<unsigned> atomic_store_members; |
| 1143 | std::vector<unsigned> sampler_implicitLod_dref_proj_members; // sampler Load id |
| 1144 | std::vector<unsigned> sampler_bias_offset_members; // sampler Load id |
| 1145 | std::vector<std::pair<unsigned, unsigned>> sampledImage_members; // <image,sampler> Load id |
| 1146 | layer_data::unordered_map<unsigned, unsigned> load_members; |
| 1147 | layer_data::unordered_map<unsigned, std::pair<unsigned, unsigned>> accesschain_members; |
| 1148 | layer_data::unordered_map<unsigned, unsigned> image_texel_pointer_members; |
| 1149 | |
| 1150 | shader_module_used_operators() : updated(false) {} |
| 1151 | |
| 1152 | bool CheckImageOperandsBiasOffset(uint32_t type) { |
| 1153 | return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask | |
| 1154 | spv::ImageOperandsConstOffsetsMask) |
| 1155 | ? true |
| 1156 | : false; |
| 1157 | } |
| 1158 | |
| 1159 | void update(SHADER_MODULE_STATE const *module) { |
| 1160 | if (updated) return; |
| 1161 | updated = true; |
| 1162 | |
| 1163 | for (auto insn : *module) { |
| 1164 | switch (insn.opcode()) { |
| 1165 | case spv::OpImageSampleImplicitLod: |
| 1166 | case spv::OpImageSampleProjImplicitLod: |
| 1167 | case spv::OpImageSampleProjExplicitLod: |
| 1168 | case spv::OpImageSparseSampleImplicitLod: |
| 1169 | case spv::OpImageSparseSampleProjImplicitLod: |
| 1170 | case spv::OpImageSparseSampleProjExplicitLod: { |
| 1171 | sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id |
| 1172 | // ImageOperands in index: 5 |
| 1173 | if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) { |
| 1174 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1175 | } |
| 1176 | break; |
| 1177 | } |
| 1178 | case spv::OpImageSampleDrefImplicitLod: |
| 1179 | case spv::OpImageSampleDrefExplicitLod: |
| 1180 | case spv::OpImageSampleProjDrefImplicitLod: |
| 1181 | case spv::OpImageSampleProjDrefExplicitLod: |
| 1182 | case spv::OpImageSparseSampleDrefImplicitLod: |
| 1183 | case spv::OpImageSparseSampleDrefExplicitLod: |
| 1184 | case spv::OpImageSparseSampleProjDrefImplicitLod: |
| 1185 | case spv::OpImageSparseSampleProjDrefExplicitLod: { |
| 1186 | sampler_implicitLod_dref_proj_members.emplace_back(insn.word(3)); // Load id |
| 1187 | // ImageOperands in index: 6 |
| 1188 | if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) { |
| 1189 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1190 | } |
| 1191 | break; |
| 1192 | } |
| 1193 | case spv::OpImageSampleExplicitLod: |
| 1194 | case spv::OpImageSparseSampleExplicitLod: { |
| 1195 | // ImageOperands in index: 5 |
| 1196 | if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) { |
| 1197 | sampler_bias_offset_members.emplace_back(insn.word(3)); |
| 1198 | } |
| 1199 | break; |
| 1200 | } |
| 1201 | case spv::OpStore: { |
| 1202 | store_members.emplace_back(insn.word(1)); // object id or AccessChain id |
| 1203 | break; |
| 1204 | } |
| 1205 | case spv::OpImageWrite: { |
| 1206 | imagwrite_members.emplace_back(insn.word(1)); // Load id |
| 1207 | break; |
| 1208 | } |
| 1209 | case spv::OpSampledImage: { |
| 1210 | // 3: image load id, 4: sampler load id |
| 1211 | sampledImage_members.emplace_back(std::pair<unsigned, unsigned>(insn.word(3), insn.word(4))); |
| 1212 | break; |
| 1213 | } |
| 1214 | case spv::OpLoad: { |
| 1215 | // 2: Load id, 3: object id or AccessChain id |
| 1216 | load_members.emplace(insn.word(2), insn.word(3)); |
| 1217 | break; |
| 1218 | } |
| 1219 | case spv::OpAccessChain: { |
| 1220 | if (insn.len() == 4) { |
| 1221 | // If it is for struct, the length is only 4. |
| 1222 | // 2: AccessChain id, 3: object id |
| 1223 | accesschain_members.emplace(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), 0)); |
| 1224 | } else { |
| 1225 | // 2: AccessChain id, 3: object id, 4: object id of array index |
| 1226 | accesschain_members.emplace(insn.word(2), std::pair<unsigned, unsigned>(insn.word(3), insn.word(4))); |
| 1227 | } |
| 1228 | break; |
| 1229 | } |
| 1230 | case spv::OpImageTexelPointer: { |
| 1231 | // 2: ImageTexelPointer id, 3: object id |
| 1232 | image_texel_pointer_members.emplace(insn.word(2), insn.word(3)); |
| 1233 | break; |
| 1234 | } |
| 1235 | default: { |
| 1236 | if (AtomicOperation(insn.opcode())) { |
| 1237 | if (insn.opcode() == spv::OpAtomicStore) { |
| 1238 | atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id |
| 1239 | } else { |
| 1240 | atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id |
| 1241 | } |
| 1242 | } |
| 1243 | break; |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | } |
| 1248 | }; |
| 1249 | |
| 1250 | static bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<unsigned> &operator_members, |
| 1251 | const layer_data::unordered_map<unsigned, unsigned> &load_members, |
| 1252 | const layer_data::unordered_map<unsigned, std::pair<unsigned, unsigned>> &accesschain_members) { |
| 1253 | for (auto load_id : operator_members) { |
| 1254 | if (object_id == load_id) return true; |
| 1255 | auto load_it = load_members.find(load_id); |
| 1256 | if (load_it == load_members.end()) { |
| 1257 | continue; |
| 1258 | } |
| 1259 | if (load_it->second == object_id) { |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | auto accesschain_it = accesschain_members.find(load_it->second); |
| 1264 | if (accesschain_it == accesschain_members.end()) { |
| 1265 | continue; |
| 1266 | } |
| 1267 | if (accesschain_it->second.first == object_id) { |
| 1268 | return true; |
| 1269 | } |
| 1270 | } |
| 1271 | return false; |
| 1272 | } |
| 1273 | |
| 1274 | // Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image |
| 1275 | // atomic operation, matching images to samplers, etc |
| 1276 | void SHADER_MODULE_STATE::IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable, |
| 1277 | interface_var &out_interface_var, |
| 1278 | shader_module_used_operators &used_operators) const { |
| 1279 | uint32_t type_id = id_it.word(1); |
| 1280 | unsigned int id = id_it.word(2); |
| 1281 | |
| 1282 | auto type = get_def(type_id); |
| 1283 | |
| 1284 | // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension. |
| 1285 | while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray || |
| 1286 | type.opcode() == spv::OpTypeSampledImage) { |
| 1287 | if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray || |
| 1288 | type.opcode() == spv::OpTypeSampledImage) { |
| 1289 | type = get_def(type.word(2)); // Element type |
| 1290 | } else { |
| 1291 | type = get_def(type.word(3)); // Pointer type |
| 1292 | } |
| 1293 | } |
| 1294 | switch (type.opcode()) { |
| 1295 | case spv::OpTypeImage: { |
| 1296 | auto dim = type.word(3); |
| 1297 | if (dim != spv::DimSubpassData) { |
| 1298 | used_operators.update(this); |
| 1299 | |
| 1300 | if (CheckObjectIDFromOpLoad(id, used_operators.imagwrite_members, used_operators.load_members, |
| 1301 | used_operators.accesschain_members)) { |
| 1302 | out_interface_var.is_writable = true; |
| 1303 | } |
| 1304 | if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members, |
| 1305 | used_operators.accesschain_members)) { |
| 1306 | out_interface_var.is_sampler_implicitLod_dref_proj = true; |
| 1307 | } |
| 1308 | if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members, |
| 1309 | used_operators.accesschain_members)) { |
| 1310 | out_interface_var.is_sampler_bias_offset = true; |
| 1311 | } |
| 1312 | if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members, |
| 1313 | used_operators.accesschain_members) || |
| 1314 | CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members, |
| 1315 | used_operators.accesschain_members)) { |
| 1316 | out_interface_var.is_atomic_operation = true; |
| 1317 | } |
| 1318 | |
| 1319 | for (auto &itp_id : used_operators.sampledImage_members) { |
| 1320 | // Find if image id match. |
| 1321 | uint32_t image_index = 0; |
| 1322 | auto load_it = used_operators.load_members.find(itp_id.first); |
| 1323 | if (load_it == used_operators.load_members.end()) { |
| 1324 | continue; |
| 1325 | } else { |
| 1326 | if (load_it->second != id) { |
| 1327 | auto accesschain_it = used_operators.accesschain_members.find(load_it->second); |
| 1328 | if (accesschain_it == used_operators.accesschain_members.end()) { |
| 1329 | continue; |
| 1330 | } else { |
| 1331 | if (accesschain_it->second.first != id) { |
| 1332 | continue; |
| 1333 | } |
| 1334 | |
| 1335 | const auto const_itr = GetConstantDef(accesschain_it->second.second); |
| 1336 | if (const_itr == end()) { |
| 1337 | // access chain index not a constant, skip. |
| 1338 | break; |
| 1339 | } |
| 1340 | image_index = GetConstantValue(const_itr); |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | // Find sampler's set binding. |
| 1345 | load_it = used_operators.load_members.find(itp_id.second); |
| 1346 | if (load_it == used_operators.load_members.end()) { |
| 1347 | continue; |
| 1348 | } else { |
| 1349 | uint32_t sampler_id = load_it->second; |
| 1350 | uint32_t sampler_index = 0; |
| 1351 | auto accesschain_it = used_operators.accesschain_members.find(load_it->second); |
| 1352 | |
| 1353 | if (accesschain_it != used_operators.accesschain_members.end()) { |
| 1354 | const auto const_itr = GetConstantDef(accesschain_it->second.second); |
| 1355 | if (const_itr == end()) { |
| 1356 | // access chain index representing sampler index is not a constant, skip. |
| 1357 | break; |
| 1358 | } |
| 1359 | sampler_id = const_itr.offset(); |
| 1360 | sampler_index = GetConstantValue(const_itr); |
| 1361 | } |
| 1362 | auto sampler_dec = get_decorations(sampler_id); |
| 1363 | if (image_index >= out_interface_var.samplers_used_by_image.size()) { |
| 1364 | out_interface_var.samplers_used_by_image.resize(image_index + 1); |
| 1365 | } |
| 1366 | out_interface_var.samplers_used_by_image[image_index].emplace( |
| 1367 | SamplerUsedByImage{descriptor_slot_t{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index}); |
| 1368 | } |
| 1369 | } |
| 1370 | } |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | case spv::OpTypeStruct: { |
| 1375 | layer_data::unordered_set<unsigned> nonwritable_members; |
| 1376 | if (get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true; |
| 1377 | for (auto insn : member_decoration_inst) { |
| 1378 | if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) { |
| 1379 | nonwritable_members.insert(insn.word(2)); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated |
| 1384 | // as nonwritable. |
| 1385 | if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) { |
| 1386 | used_operators.update(this); |
| 1387 | |
| 1388 | for (auto oid : used_operators.store_members) { |
| 1389 | if (id == oid) { |
| 1390 | out_interface_var.is_writable = true; |
| 1391 | return; |
| 1392 | } |
| 1393 | auto accesschain_it = used_operators.accesschain_members.find(oid); |
| 1394 | if (accesschain_it == used_operators.accesschain_members.end()) { |
| 1395 | continue; |
| 1396 | } |
| 1397 | if (accesschain_it->second.first == id) { |
| 1398 | out_interface_var.is_writable = true; |
| 1399 | return; |
| 1400 | } |
| 1401 | } |
| 1402 | if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members, |
| 1403 | used_operators.accesschain_members)) { |
| 1404 | out_interface_var.is_writable = true; |
| 1405 | return; |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | std::vector<std::pair<descriptor_slot_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByDescriptorSlot( |
| 1413 | layer_data::unordered_set<uint32_t> const &accessible_ids, bool *has_writable_descriptor, bool *has_atomic_descriptor) const { |
| 1414 | std::vector<std::pair<descriptor_slot_t, interface_var>> out; |
| 1415 | shader_module_used_operators operators; |
| 1416 | |
| 1417 | for (auto id : accessible_ids) { |
| 1418 | auto insn = get_def(id); |
| 1419 | assert(insn != end()); |
| 1420 | |
| 1421 | if (insn.opcode() == spv::OpVariable && |
| 1422 | (insn.word(3) == spv::StorageClassUniform || insn.word(3) == spv::StorageClassUniformConstant || |
| 1423 | insn.word(3) == spv::StorageClassStorageBuffer)) { |
| 1424 | auto d = get_decorations(insn.word(2)); |
| 1425 | unsigned set = d.descriptor_set; |
| 1426 | unsigned binding = d.binding; |
| 1427 | |
| 1428 | interface_var v = {}; |
| 1429 | v.id = insn.word(2); |
| 1430 | v.type_id = insn.word(1); |
| 1431 | |
| 1432 | IsSpecificDescriptorType(insn, insn.word(3) == spv::StorageClassStorageBuffer, |
| 1433 | !(d.flags & decoration_set::nonwritable_bit), v, operators); |
| 1434 | if (v.is_writable) *has_writable_descriptor = true; |
| 1435 | if (v.is_atomic_operation) *has_atomic_descriptor = true; |
| 1436 | out.emplace_back(std::make_pair(set, binding), v); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | return out; |
| 1441 | } |
| 1442 | |
| 1443 | layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::CollectWritableOutputLocationinFS( |
| 1444 | const VkPipelineShaderStageCreateInfo &stage_info) const { |
| 1445 | layer_data::unordered_set<uint32_t> location_list; |
| 1446 | if (stage_info.stage != VK_SHADER_STAGE_FRAGMENT_BIT) return location_list; |
| 1447 | const auto entrypoint = FindEntrypoint(stage_info.pName, stage_info.stage); |
| 1448 | const auto outputs = CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false); |
| 1449 | layer_data::unordered_set<unsigned> store_members; |
| 1450 | layer_data::unordered_map<unsigned, unsigned> accesschain_members; |
| 1451 | |
| 1452 | for (auto insn : *this) { |
| 1453 | switch (insn.opcode()) { |
| 1454 | case spv::OpStore: |
| 1455 | case spv::OpAtomicStore: { |
| 1456 | store_members.insert(insn.word(1)); // object id or AccessChain id |
| 1457 | break; |
| 1458 | } |
| 1459 | case spv::OpAccessChain: { |
| 1460 | // 2: AccessChain id, 3: object id |
| 1461 | if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3)); |
| 1462 | break; |
| 1463 | } |
| 1464 | default: |
| 1465 | break; |
| 1466 | } |
| 1467 | } |
| 1468 | if (store_members.empty()) { |
| 1469 | return location_list; |
| 1470 | } |
| 1471 | for (auto output : outputs) { |
| 1472 | auto store_it = store_members.find(output.second.id); |
| 1473 | if (store_it != store_members.end()) { |
| 1474 | location_list.insert(output.first.first); |
| 1475 | store_members.erase(store_it); |
| 1476 | continue; |
| 1477 | } |
| 1478 | store_it = store_members.begin(); |
| 1479 | while (store_it != store_members.end()) { |
| 1480 | auto accesschain_it = accesschain_members.find(*store_it); |
| 1481 | if (accesschain_it == accesschain_members.end()) { |
| 1482 | ++store_it; |
| 1483 | continue; |
| 1484 | } |
| 1485 | if (accesschain_it->second == output.second.id) { |
| 1486 | location_list.insert(output.first.first); |
| 1487 | store_members.erase(store_it); |
| 1488 | accesschain_members.erase(accesschain_it); |
| 1489 | break; |
| 1490 | } |
| 1491 | ++store_it; |
| 1492 | } |
| 1493 | } |
| 1494 | return location_list; |
| 1495 | } |
| 1496 | |
| 1497 | bool SHADER_MODULE_STATE::CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts, |
| 1498 | uint32_t id, uint32_t type_id, bool is_patch, int /*first_location*/) const { |
| 1499 | // Walk down the type_id presented, trying to determine whether it's actually an interface block. |
| 1500 | auto type = GetStructType(get_def(type_id), is_array_of_verts && !is_patch); |
| 1501 | if (type == end() || !(get_decorations(type.word(1)).flags & decoration_set::block_bit)) { |
| 1502 | // This isn't an interface block. |
| 1503 | return false; |
| 1504 | } |
| 1505 | |
| 1506 | layer_data::unordered_map<unsigned, unsigned> member_components; |
| 1507 | layer_data::unordered_map<unsigned, unsigned> member_relaxed_precision; |
| 1508 | layer_data::unordered_map<unsigned, unsigned> member_patch; |
| 1509 | |
| 1510 | // Walk all the OpMemberDecorate for type's result id -- first pass, collect components. |
| 1511 | for (auto insn : member_decoration_inst) { |
| 1512 | if (insn.word(1) == type.word(1)) { |
| 1513 | unsigned member_index = insn.word(2); |
| 1514 | |
| 1515 | if (insn.word(3) == spv::DecorationComponent) { |
| 1516 | unsigned component = insn.word(4); |
| 1517 | member_components[member_index] = component; |
| 1518 | } |
| 1519 | |
| 1520 | if (insn.word(3) == spv::DecorationRelaxedPrecision) { |
| 1521 | member_relaxed_precision[member_index] = 1; |
| 1522 | } |
| 1523 | |
| 1524 | if (insn.word(3) == spv::DecorationPatch) { |
| 1525 | member_patch[member_index] = 1; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | // TODO: correctly handle location assignment from outside |
| 1531 | |
| 1532 | // Second pass -- produce the output, from Location decorations |
| 1533 | for (auto insn : member_decoration_inst) { |
| 1534 | if (insn.word(1) == type.word(1)) { |
| 1535 | unsigned member_index = insn.word(2); |
| 1536 | unsigned member_type_id = type.word(2 + member_index); |
| 1537 | |
| 1538 | if (insn.word(3) == spv::DecorationLocation) { |
| 1539 | unsigned location = insn.word(4); |
| 1540 | unsigned num_locations = GetLocationsConsumedByType(member_type_id, false); |
| 1541 | auto component_it = member_components.find(member_index); |
| 1542 | unsigned component = component_it == member_components.end() ? 0 : component_it->second; |
| 1543 | bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end(); |
| 1544 | bool member_is_patch = is_patch || member_patch.count(member_index) > 0; |
| 1545 | |
| 1546 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 1547 | interface_var v = {}; |
| 1548 | v.id = id; |
| 1549 | // TODO: member index in interface_var too? |
| 1550 | v.type_id = member_type_id; |
| 1551 | v.offset = offset; |
| 1552 | v.is_patch = member_is_patch; |
| 1553 | v.is_block_member = true; |
| 1554 | v.is_relaxed_precision = is_relaxed_precision; |
| 1555 | (*out)[std::make_pair(location + offset, component)] = v; |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | return true; |
| 1562 | } |
| 1563 | |
| 1564 | std::map<location_t, interface_var> SHADER_MODULE_STATE::CollectInterfaceByLocation(spirv_inst_iter entrypoint, |
| 1565 | spv::StorageClass sinterface, |
| 1566 | bool is_array_of_verts) const { |
| 1567 | // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber. |
| 1568 | |
| 1569 | std::map<location_t, interface_var> out; |
| 1570 | |
| 1571 | for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) { |
| 1572 | auto insn = get_def(iid); |
| 1573 | assert(insn != end()); |
| 1574 | assert(insn.opcode() == spv::OpVariable); |
| 1575 | |
| 1576 | if (insn.word(3) == static_cast<uint32_t>(sinterface)) { |
| 1577 | auto d = get_decorations(iid); |
| 1578 | unsigned id = insn.word(2); |
| 1579 | unsigned type = insn.word(1); |
| 1580 | |
| 1581 | int location = d.location; |
| 1582 | int builtin = d.builtin; |
| 1583 | unsigned component = d.component; |
| 1584 | bool is_patch = (d.flags & decoration_set::patch_bit) != 0; |
| 1585 | bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0; |
| 1586 | |
| 1587 | if (builtin != -1) { |
| 1588 | continue; |
| 1589 | } else if (!CollectInterfaceBlockMembers(&out, is_array_of_verts, id, type, is_patch, location)) { |
| 1590 | // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit |
| 1591 | // one result for each. |
| 1592 | unsigned num_locations = GetLocationsConsumedByType(type, is_array_of_verts && !is_patch); |
| 1593 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 1594 | interface_var v = {}; |
| 1595 | v.id = id; |
| 1596 | v.type_id = type; |
| 1597 | v.offset = offset; |
| 1598 | v.is_patch = is_patch; |
| 1599 | v.is_relaxed_precision = is_relaxed_precision; |
| 1600 | out[std::make_pair(location + offset, component)] = v; |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | return out; |
| 1607 | } |
| 1608 | |
| 1609 | std::vector<uint32_t> SHADER_MODULE_STATE::CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const { |
| 1610 | std::vector<uint32_t> variables; |
| 1611 | std::vector<uint32_t> builtin_struct_members; |
| 1612 | std::vector<uint32_t> builtin_decorations; |
| 1613 | |
| 1614 | for (auto insn : member_decoration_inst) { |
| 1615 | if (insn.word(3) == spv::DecorationBuiltIn) { |
| 1616 | builtin_struct_members.push_back(insn.word(1)); |
| 1617 | } |
| 1618 | } |
| 1619 | for (auto insn : decoration_inst) { |
| 1620 | switch (insn.word(2)) { |
| 1621 | case spv::DecorationBlock: { |
| 1622 | uint32_t block_id = insn.word(1); |
| 1623 | for (auto builtin_block_id : builtin_struct_members) { |
| 1624 | // Check if one of the members of the block are built-in -> the block is built-in |
| 1625 | if (block_id == builtin_block_id) { |
| 1626 | builtin_decorations.push_back(block_id); |
| 1627 | break; |
| 1628 | } |
| 1629 | } |
| 1630 | break; |
| 1631 | } |
| 1632 | case spv::DecorationBuiltIn: |
| 1633 | builtin_decorations.push_back(insn.word(1)); |
| 1634 | break; |
| 1635 | default: |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | // Find all interface variables belonging to the entrypoint and matching the storage class |
| 1641 | for (uint32_t id : FindEntrypointInterfaces(entrypoint)) { |
| 1642 | auto def = get_def(id); |
| 1643 | assert(def != end()); |
| 1644 | assert(def.opcode() == spv::OpVariable); |
| 1645 | |
| 1646 | if (def.word(3) == storageClass) variables.push_back(def.word(1)); |
| 1647 | } |
| 1648 | |
| 1649 | // Find all members belonging to the builtin block selected |
| 1650 | std::vector<uint32_t> builtin_block_members; |
| 1651 | for (auto &var : variables) { |
| 1652 | auto def = get_def(get_def(var).word(3)); |
| 1653 | |
| 1654 | // It could be an array of IO blocks. The element type should be the struct defining the block contents |
| 1655 | if (def.opcode() == spv::OpTypeArray) def = get_def(def.word(2)); |
| 1656 | |
| 1657 | // Now find all members belonging to the struct defining the IO block |
| 1658 | if (def.opcode() == spv::OpTypeStruct) { |
| 1659 | for (auto builtin_id : builtin_decorations) { |
| 1660 | if (builtin_id == def.word(1)) { |
| 1661 | for (int i = 2; i < static_cast<int>(def.len()); i++) { |
| 1662 | builtin_block_members.push_back(spv::BuiltInMax); // Start with undefined builtin for each struct member. |
| 1663 | } |
| 1664 | // These shouldn't be left after replacing. |
| 1665 | for (auto insn : member_decoration_inst) { |
| 1666 | if (insn.word(1) == builtin_id && insn.word(3) == spv::DecorationBuiltIn) { |
| 1667 | auto struct_index = insn.word(2); |
| 1668 | assert(struct_index < builtin_block_members.size()); |
| 1669 | builtin_block_members[struct_index] = insn.word(4); |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | return builtin_block_members; |
| 1678 | } |
| 1679 | |
| 1680 | std::vector<std::pair<uint32_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByInputAttachmentIndex( |
| 1681 | layer_data::unordered_set<uint32_t> const &accessible_ids) const { |
| 1682 | std::vector<std::pair<uint32_t, interface_var>> out; |
| 1683 | |
| 1684 | for (auto insn : decoration_inst) { |
| 1685 | if (insn.word(2) == spv::DecorationInputAttachmentIndex) { |
| 1686 | auto attachment_index = insn.word(3); |
| 1687 | auto id = insn.word(1); |
| 1688 | |
| 1689 | if (accessible_ids.count(id)) { |
| 1690 | auto def = get_def(id); |
| 1691 | assert(def != end()); |
| 1692 | if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) { |
| 1693 | auto num_locations = GetLocationsConsumedByType(def.word(1), false); |
| 1694 | for (unsigned int offset = 0; offset < num_locations; offset++) { |
| 1695 | interface_var v = {}; |
| 1696 | v.id = id; |
| 1697 | v.type_id = def.word(1); |
| 1698 | v.offset = offset; |
| 1699 | out.emplace_back(attachment_index + offset, v); |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | return out; |
| 1707 | } |
| 1708 | |
Nathaniel Cesario | cf69bda | 2021-06-22 13:23:42 -0600 | [diff] [blame^] | 1709 | uint32_t SHADER_MODULE_STATE::GetSpecConstantByteSize(uint32_t const_id) const { |
| 1710 | auto itr = spec_const_map.find(const_id); |
| 1711 | if (itr != spec_const_map.cend()) { |
| 1712 | const auto def_ins = get_def(itr->second); |
| 1713 | const auto type_ins = get_def(def_ins.word(1)); |
| 1714 | |
| 1715 | // Specialization constants can only be of type bool, scalar integer, or scalar floating point |
| 1716 | switch (type_ins.opcode()) { |
| 1717 | case spv::OpTypeBool: |
| 1718 | // VUID 00776 spec states: ...If the specialization constant is of type boolean, size must be the byte size of |
| 1719 | // VkBool32 |
| 1720 | return sizeof(VkBool32); |
| 1721 | case spv::OpTypeInt: |
| 1722 | return type_ins.word(2) / 8; |
| 1723 | case spv::OpTypeFloat: |
| 1724 | return type_ins.word(2) / 8; |
| 1725 | default: |
| 1726 | return decoration_set::kInvalidValue; |
| 1727 | } |
| 1728 | } |
| 1729 | return decoration_set::kInvalidValue; |
| 1730 | } |
| 1731 | |
sfricke-samsung | 962cad9 | 2021-04-13 00:46:29 -0700 | [diff] [blame] | 1732 | // Assumes itr points to an OpConstant instruction |
| 1733 | uint32_t GetConstantValue(const spirv_inst_iter &itr) { return itr.word(3); } |
| 1734 | |
| 1735 | std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint) { |
| 1736 | assert(entrypoint.opcode() == spv::OpEntryPoint); |
| 1737 | |
| 1738 | std::vector<uint32_t> interfaces; |
| 1739 | // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the |
| 1740 | // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator. |
| 1741 | uint32_t word = 3; |
| 1742 | while (entrypoint.word(word) & 0xff000000u) { |
| 1743 | ++word; |
| 1744 | } |
| 1745 | ++word; |
| 1746 | |
| 1747 | for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word)); |
| 1748 | |
| 1749 | return interfaces; |
| 1750 | } |
| 1751 | |
| 1752 | bool AtomicOperation(uint32_t opcode) { |
| 1753 | switch (opcode) { |
| 1754 | case spv::OpAtomicLoad: |
| 1755 | case spv::OpAtomicStore: |
| 1756 | case spv::OpAtomicExchange: |
| 1757 | case spv::OpAtomicCompareExchange: |
| 1758 | case spv::OpAtomicCompareExchangeWeak: |
| 1759 | case spv::OpAtomicIIncrement: |
| 1760 | case spv::OpAtomicIDecrement: |
| 1761 | case spv::OpAtomicIAdd: |
| 1762 | case spv::OpAtomicISub: |
| 1763 | case spv::OpAtomicSMin: |
| 1764 | case spv::OpAtomicUMin: |
| 1765 | case spv::OpAtomicSMax: |
| 1766 | case spv::OpAtomicUMax: |
| 1767 | case spv::OpAtomicAnd: |
| 1768 | case spv::OpAtomicOr: |
| 1769 | case spv::OpAtomicXor: |
| 1770 | case spv::OpAtomicFAddEXT: |
| 1771 | return true; |
| 1772 | default: |
| 1773 | return false; |
| 1774 | } |
| 1775 | return false; |
| 1776 | } |
| 1777 | |
| 1778 | // Only includes valid group operations used in Vulkan (for now thats only subgroup ops) and any non supported operation will be |
| 1779 | // covered with VUID 01090 |
| 1780 | bool GroupOperation(uint32_t opcode) { |
| 1781 | switch (opcode) { |
| 1782 | case spv::OpGroupNonUniformElect: |
| 1783 | case spv::OpGroupNonUniformAll: |
| 1784 | case spv::OpGroupNonUniformAny: |
| 1785 | case spv::OpGroupNonUniformAllEqual: |
| 1786 | case spv::OpGroupNonUniformBroadcast: |
| 1787 | case spv::OpGroupNonUniformBroadcastFirst: |
| 1788 | case spv::OpGroupNonUniformBallot: |
| 1789 | case spv::OpGroupNonUniformInverseBallot: |
| 1790 | case spv::OpGroupNonUniformBallotBitExtract: |
| 1791 | case spv::OpGroupNonUniformBallotBitCount: |
| 1792 | case spv::OpGroupNonUniformBallotFindLSB: |
| 1793 | case spv::OpGroupNonUniformBallotFindMSB: |
| 1794 | case spv::OpGroupNonUniformShuffle: |
| 1795 | case spv::OpGroupNonUniformShuffleXor: |
| 1796 | case spv::OpGroupNonUniformShuffleUp: |
| 1797 | case spv::OpGroupNonUniformShuffleDown: |
| 1798 | case spv::OpGroupNonUniformIAdd: |
| 1799 | case spv::OpGroupNonUniformFAdd: |
| 1800 | case spv::OpGroupNonUniformIMul: |
| 1801 | case spv::OpGroupNonUniformFMul: |
| 1802 | case spv::OpGroupNonUniformSMin: |
| 1803 | case spv::OpGroupNonUniformUMin: |
| 1804 | case spv::OpGroupNonUniformFMin: |
| 1805 | case spv::OpGroupNonUniformSMax: |
| 1806 | case spv::OpGroupNonUniformUMax: |
| 1807 | case spv::OpGroupNonUniformFMax: |
| 1808 | case spv::OpGroupNonUniformBitwiseAnd: |
| 1809 | case spv::OpGroupNonUniformBitwiseOr: |
| 1810 | case spv::OpGroupNonUniformBitwiseXor: |
| 1811 | case spv::OpGroupNonUniformLogicalAnd: |
| 1812 | case spv::OpGroupNonUniformLogicalOr: |
| 1813 | case spv::OpGroupNonUniformLogicalXor: |
| 1814 | case spv::OpGroupNonUniformQuadBroadcast: |
| 1815 | case spv::OpGroupNonUniformQuadSwap: |
| 1816 | case spv::OpGroupNonUniformPartitionNV: |
| 1817 | return true; |
| 1818 | default: |
| 1819 | return false; |
| 1820 | } |
| 1821 | return false; |
Jeremy Gebben | 5d97074 | 2021-05-31 16:04:14 -0600 | [diff] [blame] | 1822 | } |