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