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