blob: 3c9139e7934700bfe4dcf7d6238652602abdce95 [file] [log] [blame]
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001/* Copyright (c) 2021-2022 The Khronos Group Inc.
sfricke-samsung962cad92021-04-13 00:46:29 -07002 *
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 Gebben5d970742021-05-31 16:04:14 -060025#include "pipeline_state.h"
26#include "descriptor_sets.h"
sfricke-samsung3c5dee22021-10-14 09:58:14 -070027#include "spirv_grammar_helper.h"
sfricke-samsung962cad92021-04-13 00:46:29 -070028
29void 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
39void 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 Landwerlin38d2e122021-07-21 14:21:47 +030080 case spv::DecorationNonReadable:
81 flags |= nonreadable_bit;
82 break;
ziga-lunarg9e94e112021-09-27 00:21:10 +020083 case spv::DecorationPerVertexNV:
84 flags |= per_vertex_bit;
85 break;
86 case spv::DecorationPassthroughNV:
87 flags |= passthrough_bit;
88 break;
sfricke-samsung962cad92021-04-13 00:46:29 -070089 }
90}
91
92std::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-samsung7fac88a2022-01-26 11:44:22 -0800120static uint32_t ExecutionModelToShaderStageFlagBits(uint32_t mode) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700121 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.
162layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::MarkAccessibleIds(spirv_inst_iter entrypoint) const {
163 layer_data::unordered_set<uint32_t> ids;
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600164 if (entrypoint == end() || !has_valid_spirv) {
165 return ids;
166 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700167 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 Cesario77cd59b2021-10-11 23:52:24 -0600177 // ID is something we didn't collect in SpirvStaticData. that's OK -- we'll stumble across all kinds of things here
sfricke-samsung962cad92021-04-13 00:46:29 -0700178 // 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 Gebben84b838b2021-08-23 08:41:39 -0600271layer_data::optional<VkPrimitiveTopology> SHADER_MODULE_STATE::GetTopology(const spirv_inst_iter &entrypoint) const {
272 layer_data::optional<VkPrimitiveTopology> result;
273
sfricke-samsung962cad92021-04-13 00:46:29 -0700274 auto entrypoint_id = entrypoint.word(2);
275 bool is_point_mode = false;
276
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600277 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
278 if (it != static_data_.execution_mode_inst.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700279 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 Gebben84b838b2021-08-23 08:41:39 -0600287 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
sfricke-samsung962cad92021-04-13 00:46:29 -0700288 break;
289
290 case spv::ExecutionModeIsolines:
291 case spv::ExecutionModeOutputLineStrip:
Ricardo Garcia122f8f02021-09-28 16:47:19 +0200292 case spv::ExecutionModeOutputLinesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600293 result.emplace(VK_PRIMITIVE_TOPOLOGY_LINE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700294 break;
295
296 case spv::ExecutionModeTriangles:
297 case spv::ExecutionModeQuads:
298 case spv::ExecutionModeOutputTriangleStrip:
299 case spv::ExecutionModeOutputTrianglesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600300 result.emplace(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700301 break;
302 }
303 }
304 }
305
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600306 if (is_point_mode) {
307 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
308 }
309
310 return result;
sfricke-samsung962cad92021-04-13 00:46:29 -0700311}
312
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700313layer_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-samsungef15e482022-01-26 11:32:49 -0800321SHADER_MODULE_STATE::SpirvStaticData::SpirvStaticData(const SHADER_MODULE_STATE &module_state) {
322 for (auto insn : module_state) {
sfricke-samsung5a48ed42022-02-13 17:37:13 -0800323 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-samsung962cad92021-04-13 00:46:29 -0700328 switch (insn.opcode()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700329 // Specialization constants
330 case spv::OpSpecConstantTrue:
331 case spv::OpSpecConstantFalse:
332 case spv::OpSpecConstant:
333 case spv::OpSpecConstantComposite:
334 case spv::OpSpecConstantOp:
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600335 has_specialization_constants = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700336 break;
337
sfricke-samsung962cad92021-04-13 00:46:29 -0700338 // 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 Cesariocf69bda2021-06-22 13:23:42 -0600345 } else if (insn.word(2) == spv::DecorationSpecId) {
346 spec_const_map[insn.word(3)] = target_id;
sfricke-samsung962cad92021-04-13 00:46:29 -0700347 }
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 Cesario77cd59b2021-10-11 23:52:24 -0600353 has_group_decoration = true;
354 } break;
355 case spv::OpDecorationGroup:
356 case spv::OpGroupMemberDecorate: {
357 has_group_decoration = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700358 } 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
ziga-lunargbe003732022-04-22 00:20:13 +0200366 case spv::OpCapability:
367 capability_list.push_back(static_cast<spv::Capability>(insn.word(1)));
368 break;
369
sjfricke44d663c2022-06-01 06:42:58 +0900370 case spv::OpVariable:
371 variable_inst.push_back(insn);
372 break;
373
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600374 // Execution Mode
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800375 case spv::OpExecutionMode:
376 case spv::OpExecutionModeId: {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600377 execution_mode_inst[insn.word(1)].push_back(insn);
378 } break;
ziga-lunarge25f5f02022-04-16 15:07:35 +0200379 // Once https://github.com/KhronosGroup/SPIRV-Headers/issues/276 is added this should be derived from SPIR-V grammar
380 // json
381 case spv::OpTraceRayKHR:
382 case spv::OpTraceRayMotionNV:
383 case spv::OpReportIntersectionKHR:
384 case spv::OpExecuteCallableKHR:
385 has_invocation_repack_instruction = true;
386 break;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600387
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600388 default:
389 if (AtomicOperation(insn.opcode()) == true) {
390 // All atomics have a pointer referenced
391 spirv_inst_iter access;
392 if (insn.opcode() == spv::OpAtomicStore) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800393 access = module_state.get_def(insn.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600394 } else {
sfricke-samsungef15e482022-01-26 11:32:49 -0800395 access = module_state.get_def(insn.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600396 }
397
398 atomic_instruction atomic;
399
sfricke-samsungef15e482022-01-26 11:32:49 -0800400 auto pointer = module_state.get_def(access.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600401 // spirv-val should catch if not pointer
402 assert(pointer.opcode() == spv::OpTypePointer);
403 atomic.storage_class = pointer.word(2);
404
sfricke-samsungef15e482022-01-26 11:32:49 -0800405 auto data_type = module_state.get_def(pointer.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600406 atomic.type = data_type.opcode();
407
408 // TODO - Should have a proper GetBitWidth like spirv-val does
409 assert(data_type.opcode() == spv::OpTypeFloat || data_type.opcode() == spv::OpTypeInt);
410 atomic.bit_width = data_type.word(2);
411
412 atomic_inst[insn.offset()] = atomic;
413 }
414 // We don't care about any other defs for now.
415 break;
416 }
417 }
418
sfricke-samsungef15e482022-01-26 11:32:49 -0800419 entry_points = SHADER_MODULE_STATE::ProcessEntryPoints(module_state);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600420 multiple_entry_points = entry_points.size() > 1;
421}
422
423// static
424std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> SHADER_MODULE_STATE::ProcessEntryPoints(
sfricke-samsungef15e482022-01-26 11:32:49 -0800425 const SHADER_MODULE_STATE &module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600426 std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> entry_points;
427 function_set func_set = {};
428 EntryPoint *entry_point = nullptr;
429
sfricke-samsungef15e482022-01-26 11:32:49 -0800430 for (auto insn : module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600431 // offset is not 0, it means it's updated and the offset is in a Function.
432 if (func_set.offset) {
433 func_set.op_lists.emplace(insn.opcode(), insn.offset());
434 } else if (entry_point) {
435 entry_point->decorate_list.emplace(insn.opcode(), insn.offset());
436 }
437
438 switch (insn.opcode()) {
439 // Functions
440 case spv::OpFunction:
441 func_set.id = insn.word(2);
442 func_set.offset = insn.offset();
443 func_set.op_lists.clear();
444 break;
445
446 // Entry points ... add to the entrypoint table
447 case spv::OpEntryPoint: {
sfricke-samsung962cad92021-04-13 00:46:29 -0700448 // Entry points do not have an id (the id is the function id) and thus need their own table
449 auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3));
450 auto execution_model = insn.word(1);
451 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
452 entry_points.emplace(entrypoint_name,
453 EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)});
454
455 auto range = entry_points.equal_range(entrypoint_name);
456 for (auto it = range.first; it != range.second; ++it) {
457 if (it->second.offset == insn.offset()) {
458 entry_point = &(it->second);
459 break;
460 }
461 }
462 assert(entry_point != nullptr);
463 break;
464 }
465 case spv::OpFunctionEnd: {
466 assert(entry_point != nullptr);
467 func_set.length = insn.offset() - func_set.offset;
468 entry_point->function_set_list.emplace_back(func_set);
469 break;
470 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700471 }
472 }
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600473
sfricke-samsungef15e482022-01-26 11:32:49 -0800474 SHADER_MODULE_STATE::SetPushConstantUsedInShader(module_state, entry_points);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600475 return entry_points;
sfricke-samsung962cad92021-04-13 00:46:29 -0700476}
477
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600478void SHADER_MODULE_STATE::PreprocessShaderBinary(const spv_target_env env) {
479 if (static_data_.has_group_decoration) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700480 spvtools::Optimizer optimizer(env);
481 optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass());
482 std::vector<uint32_t> optimized_binary;
483 // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600484 auto result = optimizer.Run(words.data(), words.size(), &optimized_binary, spvtools::ValidatorOptions(), true);
485
sfricke-samsung962cad92021-04-13 00:46:29 -0700486 if (result) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600487 // NOTE: We need to update words with the result from the spirv-tools optimizer.
488 // **THIS ONLY HAPPENS ON INITIALIZATION**. words should remain const for the lifetime
489 // of the SHADER_MODULE_STATE instance.
490 *const_cast<std::vector<uint32_t> *>(&words) = std::move(optimized_binary);
sfricke-samsung962cad92021-04-13 00:46:29 -0700491 }
492 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700493}
494
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800495char const *StorageClassName(uint32_t sc) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700496 switch (sc) {
497 case spv::StorageClassInput:
498 return "input";
499 case spv::StorageClassOutput:
500 return "output";
501 case spv::StorageClassUniformConstant:
502 return "const uniform";
503 case spv::StorageClassUniform:
504 return "uniform";
505 case spv::StorageClassWorkgroup:
506 return "workgroup local";
507 case spv::StorageClassCrossWorkgroup:
508 return "workgroup global";
509 case spv::StorageClassPrivate:
510 return "private global";
511 case spv::StorageClassFunction:
512 return "function";
513 case spv::StorageClassGeneric:
514 return "generic";
515 case spv::StorageClassAtomicCounter:
516 return "atomic counter";
517 case spv::StorageClassImage:
518 return "image";
519 case spv::StorageClassPushConstant:
520 return "push constant";
521 case spv::StorageClassStorageBuffer:
522 return "storage buffer";
523 default:
524 return "unknown";
525 }
526}
527
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800528void SHADER_MODULE_STATE::DescribeTypeInner(std::ostringstream &ss, uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700529 auto insn = get_def(type);
530 assert(insn != end());
531
532 switch (insn.opcode()) {
533 case spv::OpTypeBool:
534 ss << "bool";
535 break;
536 case spv::OpTypeInt:
537 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
538 break;
539 case spv::OpTypeFloat:
540 ss << "float" << insn.word(2);
541 break;
542 case spv::OpTypeVector:
543 ss << "vec" << insn.word(3) << " of ";
544 DescribeTypeInner(ss, insn.word(2));
545 break;
546 case spv::OpTypeMatrix:
547 ss << "mat" << insn.word(3) << " of ";
548 DescribeTypeInner(ss, insn.word(2));
549 break;
550 case spv::OpTypeArray:
551 ss << "arr[" << GetConstantValueById(insn.word(3)) << "] of ";
552 DescribeTypeInner(ss, insn.word(2));
553 break;
554 case spv::OpTypeRuntimeArray:
555 ss << "runtime arr[] of ";
556 DescribeTypeInner(ss, insn.word(2));
557 break;
558 case spv::OpTypePointer:
559 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
560 DescribeTypeInner(ss, insn.word(3));
561 break;
562 case spv::OpTypeStruct: {
563 ss << "struct of (";
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800564 for (uint32_t i = 2; i < insn.len(); i++) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700565 DescribeTypeInner(ss, insn.word(i));
566 if (i == insn.len() - 1) {
567 ss << ")";
568 } else {
569 ss << ", ";
570 }
571 }
572 break;
573 }
574 case spv::OpTypeSampler:
575 ss << "sampler";
576 break;
577 case spv::OpTypeSampledImage:
578 ss << "sampler+";
579 DescribeTypeInner(ss, insn.word(2));
580 break;
581 case spv::OpTypeImage:
582 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
583 break;
584 case spv::OpTypeAccelerationStructureNV:
585 ss << "accelerationStruture";
586 break;
587 default:
588 ss << "oddtype";
589 break;
590 }
591}
592
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800593std::string SHADER_MODULE_STATE::DescribeType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700594 std::ostringstream ss;
595 DescribeTypeInner(ss, type);
596 return ss.str();
597}
598
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800599std::string SHADER_MODULE_STATE::DescribeInstruction(const spirv_inst_iter &insn) const {
600 std::ostringstream ss;
601 const uint32_t opcode = insn.opcode();
602 uint32_t operand_offset = 1; // where to start printing operands
603 // common disassembled for SPIR-V is
604 // %result = Opcode %result_type %operands
605 if (OpcodeHasResult(opcode)) {
606 operand_offset++;
607 ss << "%" << (OpcodeHasType(opcode) ? insn.word(2) : insn.word(1)) << " = ";
608 }
609
610 ss << string_SpvOpcode(opcode);
611
612 if (OpcodeHasType(opcode)) {
613 operand_offset++;
614 ss << " %" << insn.word(1);
615 }
616
sfricke-samsunged00aa42022-01-27 19:03:01 -0800617 // TODO - For now don't list the '%' for any operands since they are only for reference IDs. Without generating a table of each
618 // instructions operand types and covering the many edge cases (such as optional, paired, or variable operands) this is the
619 // simplest way to print the instruction and give the developer something to look into when an error occurs.
620 //
621 // For now this safely should be able to assume it will never come across a LiteralString such as in OpExtInstImport or
622 // OpEntryPoint
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800623 for (uint32_t i = operand_offset; i < insn.len(); i++) {
sfricke-samsunged00aa42022-01-27 19:03:01 -0800624 ss << " " << insn.word(i);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800625 }
626 return ss.str();
627}
628
sfricke-samsung962cad92021-04-13 00:46:29 -0700629const SHADER_MODULE_STATE::EntryPoint *SHADER_MODULE_STATE::FindEntrypointStruct(char const *name,
630 VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600631 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700632 for (auto it = range.first; it != range.second; ++it) {
633 if (it->second.stage == stageBits) {
634 return &(it->second);
635 }
636 }
637 return nullptr;
638}
639
640spirv_inst_iter SHADER_MODULE_STATE::FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600641 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700642 for (auto it = range.first; it != range.second; ++it) {
643 if (it->second.stage == stageBits) {
644 return at(it->second.offset);
645 }
646 }
647 return end();
648}
649
650// Because the following is legal, need the entry point
651// OpEntryPoint GLCompute %main "name_a"
652// OpEntryPoint GLCompute %main "name_b"
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800653// Assumes shader module contains no spec constants used to set the local size values
sfricke-samsung962cad92021-04-13 00:46:29 -0700654bool SHADER_MODULE_STATE::FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y,
655 uint32_t &local_size_z) const {
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800656 // "If an object is decorated with the WorkgroupSize decoration, this takes precedence over any LocalSize or LocalSizeId
657 // execution mode."
658 for (const auto &builtin : static_data_.builtin_decoration_list) {
659 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
660 const uint32_t workgroup_size_id = at(builtin.offset).word(1);
661 auto composite_def = get_def(workgroup_size_id);
662 if (composite_def.opcode() == spv::OpConstantComposite) {
663 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32
664 local_size_x = GetConstantValue(get_def(composite_def.word(3)));
665 local_size_y = GetConstantValue(get_def(composite_def.word(4)));
666 local_size_z = GetConstantValue(get_def(composite_def.word(5)));
sfricke-samsung962cad92021-04-13 00:46:29 -0700667 return true;
668 }
669 }
670 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800671
672 auto entrypoint_id = entrypoint.word(2);
673 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
674 if (it != static_data_.execution_mode_inst.end()) {
675 for (auto insn : it->second) {
676 if (insn.opcode() == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeLocalSize) {
677 local_size_x = insn.word(3);
678 local_size_y = insn.word(4);
679 local_size_z = insn.word(5);
680 return true;
681 } else if (insn.opcode() == spv::OpExecutionModeId && insn.word(2) == spv::ExecutionModeLocalSizeId) {
682 local_size_x = GetConstantValueById(insn.word(3));
683 local_size_y = GetConstantValueById(insn.word(4));
684 local_size_z = GetConstantValueById(insn.word(5));
685 return true;
686 }
687 }
688 }
689 return false; // not found
sfricke-samsung962cad92021-04-13 00:46:29 -0700690}
691
692// If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction.
693// Otherwise, returns src->end().
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800694spirv_inst_iter SHADER_MODULE_STATE::GetConstantDef(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700695 auto value = get_def(id);
696
697 // If id is a copy, see where it was copied from
698 if ((end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) {
699 id = value.word(3);
700 value = get_def(id);
701 }
702
703 if ((end() != value) && (value.opcode() == spv::OpConstant)) {
704 return value;
705 }
706 return end();
707}
708
709// Either returns the constant value described by the instruction at id, or 1
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800710uint32_t SHADER_MODULE_STATE::GetConstantValueById(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700711 auto value = GetConstantDef(id);
712
713 if (end() == value) {
714 // TODO: Either ensure that the specialization transform is already performed on a module we're
715 // considering here, OR -- specialize on the fly now.
716 return 1;
717 }
718 return GetConstantValue(value);
719}
720
721// Returns an int32_t corresponding to the spv::Dim of the given resource, when positive, and corresponding to an unknown type, when
722// negative.
723int32_t SHADER_MODULE_STATE::GetShaderResourceDimensionality(const interface_var &resource) const {
724 auto type = get_def(resource.type_id);
725 while (true) {
726 switch (type.opcode()) {
727 case spv::OpTypeSampledImage:
728 type = get_def(type.word(2));
729 break;
730 case spv::OpTypePointer:
731 type = get_def(type.word(3));
732 break;
733 case spv::OpTypeImage:
734 return type.word(3);
735 default:
736 return -1;
737 }
738 }
739}
740
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800741uint32_t SHADER_MODULE_STATE::GetLocationsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700742 auto insn = get_def(type);
743 assert(insn != end());
744
745 switch (insn.opcode()) {
746 case spv::OpTypePointer:
747 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
748 // pointers around.
749 return GetLocationsConsumedByType(insn.word(3), strip_array_level);
750 case spv::OpTypeArray:
751 if (strip_array_level) {
752 return GetLocationsConsumedByType(insn.word(2), false);
753 } else {
754 return GetConstantValueById(insn.word(3)) * GetLocationsConsumedByType(insn.word(2), false);
755 }
756 case spv::OpTypeMatrix:
757 // Num locations is the dimension * element size
758 return insn.word(3) * GetLocationsConsumedByType(insn.word(2), false);
759 case spv::OpTypeVector: {
760 auto scalar_type = get_def(insn.word(2));
761 auto bit_width =
762 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
763
764 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
765 return (bit_width * insn.word(3) + 127) / 128;
766 }
767 default:
768 // Everything else is just 1.
769 return 1;
770
771 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
772 }
773}
774
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800775uint32_t SHADER_MODULE_STATE::GetComponentsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700776 auto insn = get_def(type);
777 assert(insn != end());
778
779 switch (insn.opcode()) {
780 case spv::OpTypePointer:
781 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
782 // pointers around.
783 return GetComponentsConsumedByType(insn.word(3), strip_array_level);
784 case spv::OpTypeStruct: {
785 uint32_t sum = 0;
786 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
787 sum += GetComponentsConsumedByType(insn.word(i), false);
788 }
789 return sum;
790 }
791 case spv::OpTypeArray:
792 if (strip_array_level) {
793 return GetComponentsConsumedByType(insn.word(2), false);
794 } else {
795 return GetConstantValueById(insn.word(3)) * GetComponentsConsumedByType(insn.word(2), false);
796 }
797 case spv::OpTypeMatrix:
798 // Num locations is the dimension * element size
799 return insn.word(3) * GetComponentsConsumedByType(insn.word(2), false);
800 case spv::OpTypeVector: {
801 auto scalar_type = get_def(insn.word(2));
802 auto bit_width =
803 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
804 // One component is 32-bit
805 return (bit_width * insn.word(3) + 31) / 32;
806 }
807 case spv::OpTypeFloat: {
808 auto bit_width = insn.word(2);
809 return (bit_width + 31) / 32;
810 }
811 case spv::OpTypeInt: {
812 auto bit_width = insn.word(2);
813 return (bit_width + 31) / 32;
814 }
815 case spv::OpConstant:
816 return GetComponentsConsumedByType(insn.word(1), false);
817 default:
818 return 0;
819 }
820}
821
822// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
823// also used for input attachments, as we statically know their format.
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800824uint32_t SHADER_MODULE_STATE::GetFundamentalType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700825 auto insn = get_def(type);
826 assert(insn != end());
827
828 switch (insn.opcode()) {
829 case spv::OpTypeInt:
830 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
831 case spv::OpTypeFloat:
832 return FORMAT_TYPE_FLOAT;
833 case spv::OpTypeVector:
834 case spv::OpTypeMatrix:
835 case spv::OpTypeArray:
836 case spv::OpTypeRuntimeArray:
837 case spv::OpTypeImage:
838 return GetFundamentalType(insn.word(2));
839 case spv::OpTypePointer:
840 return GetFundamentalType(insn.word(3));
841
842 default:
843 return 0;
844 }
845}
846
847spirv_inst_iter SHADER_MODULE_STATE::GetStructType(spirv_inst_iter def, bool is_array_of_verts) const {
848 while (true) {
849 if (def.opcode() == spv::OpTypePointer) {
850 def = get_def(def.word(3));
851 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
852 def = get_def(def.word(2));
853 is_array_of_verts = false;
854 } else if (def.opcode() == spv::OpTypeStruct) {
855 return def;
856 } else {
857 return end();
858 }
859 }
860}
861
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800862void SHADER_MODULE_STATE::DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &member_decorate_offsets,
sfricke-samsung962cad92021-04-13 00:46:29 -0700863 shader_struct_member &data) const {
864 const auto struct_it = GetStructType(it, false);
865 assert(struct_it != end());
866 data.size = 0;
867
868 shader_struct_member data1;
869 uint32_t i = 2;
870 uint32_t local_offset = 0;
871 std::vector<uint32_t> offsets;
872 offsets.resize(struct_it.len() - i);
873
874 // The members of struct in SPRIV_R aren't always sort, so we need to know their order.
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800875 for (const auto offset : member_decorate_offsets) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700876 const auto member_decorate = at(offset);
877 if (member_decorate.word(1) != struct_it.word(1)) {
878 continue;
879 }
880
881 offsets[member_decorate.word(2)] = member_decorate.word(4);
882 }
883
884 for (const auto offset : offsets) {
885 local_offset = offset;
886 data1 = {};
887 data1.root = data.root;
888 data1.offset = local_offset;
889 auto def_member = get_def(struct_it.word(i));
890
891 // Array could be multi-dimensional
892 while (def_member.opcode() == spv::OpTypeArray) {
893 const auto len_id = def_member.word(3);
894 const auto def_len = get_def(len_id);
895 data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length
896 def_member = get_def(def_member.word(2));
897 }
898
899 if (def_member.opcode() == spv::OpTypeStruct) {
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800900 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700901 } else if (def_member.opcode() == spv::OpTypePointer) {
902 if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) {
903 // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address
904 // that "points to something."
905 data1.size = 8;
906 } else {
907 // If it's OpTypePointer. it means the member is a buffer, the type will be TypePointer, and then struct
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800908 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700909 }
910 } else {
911 if (def_member.opcode() == spv::OpTypeMatrix) {
912 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector.
913 def_member = get_def(def_member.word(2));
914 }
915
916 if (def_member.opcode() == spv::OpTypeVector) {
917 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length
918 def_member = get_def(def_member.word(2));
919 }
920
921 // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte.
922 data1.size = (def_member.word(2) / 8);
923 }
924 const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size();
925 if (array_length_hierarchy_szie > 0) {
926 data1.array_block_size.resize(array_length_hierarchy_szie, 1);
927
928 for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) {
929 data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2];
930 }
931 }
932 data.struct_members.emplace_back(data1);
933 ++i;
934 }
935 uint32_t total_array_length = 1;
936 for (const auto length : data1.array_length_hierarchy) {
937 total_array_length *= length;
938 }
939 data.size = local_offset + data1.size * total_array_length;
940}
941
942static uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
943 int array_indices_size = static_cast<int>(array_indices.size());
944 if (array_indices_size) {
945 uint32_t array_index = 0;
946 uint32_t i = 0;
947 for (const auto index : array_indices) {
948 array_index += (data.array_block_size[i] * index);
949 ++i;
950 }
951 offset += (array_index * data.size);
952 }
953 return offset;
954}
955
956static void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
957 int array_indices_size = static_cast<int>(array_indices.size());
958 uint32_t block_memory_size = data.size;
959 for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) {
960 block_memory_size *= data.array_length_hierarchy[i];
961 }
962
963 offset = UpdateOffset(offset, array_indices, data);
964
965 uint32_t end = offset + block_memory_size;
966 auto used_bytes = data.GetUsedbytes();
967 if (used_bytes->size() < end) {
968 used_bytes->resize(end, 0);
969 }
970 std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size));
971}
972
973void SHADER_MODULE_STATE::RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index,
974 spirv_inst_iter &access_chain_it, const shader_struct_member &data) const {
975 if (access_chain_word_index < access_chain_it.len()) {
976 if (data.array_length_hierarchy.size() > array_indices.size()) {
977 auto def_it = get_def(access_chain_it.word(access_chain_word_index));
978 ++access_chain_word_index;
979
980 if (def_it != end() && def_it.opcode() == spv::OpConstant) {
981 array_indices.emplace_back(def_it.word(3));
982 RunUsedArray(offset, array_indices, access_chain_word_index, access_chain_it, data);
983 } else {
984 // If it is a variable, set the all array is used.
985 if (access_chain_word_index < access_chain_it.len()) {
986 uint32_t array_length = data.array_length_hierarchy[array_indices.size()];
987 for (uint32_t i = 0; i < array_length; ++i) {
988 auto array_indices2 = array_indices;
989 array_indices2.emplace_back(i);
990 RunUsedArray(offset, array_indices2, access_chain_word_index, access_chain_it, data);
991 }
992 } else {
993 SetUsedBytes(offset, array_indices, data);
994 }
995 }
996 } else {
997 offset = UpdateOffset(offset, array_indices, data);
998 RunUsedStruct(offset, access_chain_word_index, access_chain_it, data);
999 }
1000 } else {
1001 SetUsedBytes(offset, array_indices, data);
1002 }
1003}
1004
1005void SHADER_MODULE_STATE::RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it,
1006 const shader_struct_member &data) const {
1007 std::vector<uint32_t> array_indices_emptry;
1008
1009 if (access_chain_word_index < access_chain_it.len()) {
1010 auto strcut_member_index = GetConstantValueById(access_chain_it.word(access_chain_word_index));
1011 ++access_chain_word_index;
1012
1013 auto data1 = data.struct_members[strcut_member_index];
1014 RunUsedArray(offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1);
1015 }
1016}
1017
1018void SHADER_MODULE_STATE::SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list,
1019 const shader_struct_member &data) const {
1020 for (const auto &func_set : function_set_list) {
1021 auto range = func_set.op_lists.equal_range(spv::OpAccessChain);
1022 for (auto it = range.first; it != range.second; ++it) {
1023 auto access_chain = at(it->second);
1024 if (access_chain.word(3) == variable_id) {
1025 RunUsedStruct(0, 4, access_chain, data);
1026 }
1027 }
1028 }
1029}
1030
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001031// static
1032void SHADER_MODULE_STATE::SetPushConstantUsedInShader(
sfricke-samsungef15e482022-01-26 11:32:49 -08001033 const SHADER_MODULE_STATE &module_state, std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001034 for (auto &entrypoint : entry_points) {
1035 auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable);
1036 for (auto it = range.first; it != range.second; ++it) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001037 const auto def_insn = module_state.at(it->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001038
1039 if (def_insn.word(3) == spv::StorageClassPushConstant) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001040 spirv_inst_iter type = module_state.get_def(def_insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -07001041 const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate);
1042 std::vector<uint32_t> offsets;
1043
1044 for (auto it2 = range2.first; it2 != range2.second; ++it2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001045 auto member_decorate = module_state.at(it2->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001046 if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) {
1047 offsets.emplace_back(member_decorate.offset());
1048 }
1049 }
1050 entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader;
sfricke-samsungef15e482022-01-26 11:32:49 -08001051 module_state.DefineStructMember(type, offsets, entrypoint.second.push_constant_used_in_shader);
1052 module_state.SetUsedStructMember(def_insn.word(2), entrypoint.second.function_set_list,
1053 entrypoint.second.push_constant_used_in_shader);
sfricke-samsung962cad92021-04-13 00:46:29 -07001054 }
1055 }
1056 }
1057}
1058
1059uint32_t SHADER_MODULE_STATE::DescriptorTypeToReqs(uint32_t type_id) const {
1060 auto type = get_def(type_id);
1061
1062 while (true) {
1063 switch (type.opcode()) {
1064 case spv::OpTypeArray:
1065 case spv::OpTypeRuntimeArray:
1066 case spv::OpTypeSampledImage:
1067 type = get_def(type.word(2));
1068 break;
1069 case spv::OpTypePointer:
1070 type = get_def(type.word(3));
1071 break;
1072 case spv::OpTypeImage: {
1073 auto dim = type.word(3);
1074 auto arrayed = type.word(5);
1075 auto msaa = type.word(6);
1076
1077 uint32_t bits = 0;
1078 switch (GetFundamentalType(type.word(2))) {
1079 case FORMAT_TYPE_FLOAT:
1080 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
1081 break;
1082 case FORMAT_TYPE_UINT:
1083 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
1084 break;
1085 case FORMAT_TYPE_SINT:
1086 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
1087 break;
1088 default:
1089 break;
1090 }
1091
1092 switch (dim) {
1093 case spv::Dim1D:
1094 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1095 return bits;
1096 case spv::Dim2D:
1097 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1098 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
1099 return bits;
1100 case spv::Dim3D:
1101 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
1102 return bits;
1103 case spv::DimCube:
1104 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1105 return bits;
1106 case spv::DimSubpassData:
1107 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1108 return bits;
1109 default: // buffer, etc.
1110 return bits;
1111 }
1112 }
1113 default:
1114 return 0;
1115 }
1116 }
1117}
1118
1119// For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to.
1120// This function examines instructions in the static call tree for a write to this variable.
1121bool SHADER_MODULE_STATE::IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const {
1122 auto type = builtin_instr.opcode();
1123 uint32_t target_id = builtin_instr.word(1);
1124 bool init_complete = false;
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001125 uint32_t target_member_offset = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001126
1127 if (type == spv::OpMemberDecorate) {
1128 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1129 auto insn = entrypoint;
1130 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1131 switch (insn.opcode()) {
1132 case spv::OpTypePointer:
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001133 if (insn.word(2) == spv::StorageClassOutput) {
1134 const auto type_id = insn.word(3);
1135 if (type_id == target_id) {
1136 target_id = insn.word(1);
1137 } else {
1138 // If the output is an array, check if the element type is what we're looking for
1139 const auto type_insn = get_def(type_id);
1140 if ((type_insn.opcode() == spv::OpTypeArray) && (type_insn.word(2) == target_id)) {
1141 target_id = insn.word(1);
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001142 target_member_offset = 1;
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001143 }
1144 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001145 }
1146 break;
1147 case spv::OpVariable:
1148 if (insn.word(1) == target_id) {
1149 target_id = insn.word(2);
1150 init_complete = true;
1151 }
1152 break;
1153 }
1154 insn++;
1155 }
1156 }
1157
1158 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1159
1160 bool found_write = false;
1161 layer_data::unordered_set<uint32_t> worklist;
1162 worklist.insert(entrypoint.word(2));
1163
1164 // Follow instructions in call graph looking for writes to target
1165 while (!worklist.empty() && !found_write) {
1166 auto id_iter = worklist.begin();
1167 auto id = *id_iter;
1168 worklist.erase(id_iter);
1169
1170 auto insn = get_def(id);
1171 if (insn == end()) {
1172 continue;
1173 }
1174
1175 if (insn.opcode() == spv::OpFunction) {
1176 // Scan body of function looking for other function calls or items in our ID chain
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001177 while (++insn, (insn.opcode() != spv::OpFunctionEnd) && !found_write) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001178 switch (insn.opcode()) {
1179 case spv::OpAccessChain:
1180 if (insn.word(3) == target_id) {
1181 if (type == spv::OpMemberDecorate) {
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001182 // Get the target member of the struct
1183 // NOTE: this will only work for structs and arrays of structs. Deeper levels of nesting (e.g.,
1184 // arrays of structs of structs) is not currently supported.
1185 const auto value_itr = GetConstantDef(insn.word(4 + target_member_offset));
1186 if (value_itr != end()) {
1187 auto value = GetConstantValue(value_itr);
1188 if (value == builtin_instr.word(2)) {
1189 target_id = insn.word(2);
1190 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001191 }
1192 } else {
1193 target_id = insn.word(2);
1194 }
1195 }
1196 break;
1197 case spv::OpStore:
1198 if (insn.word(1) == target_id) {
1199 found_write = true;
1200 }
1201 break;
1202 case spv::OpFunctionCall:
1203 worklist.insert(insn.word(3));
1204 break;
1205 }
1206 }
1207 }
1208 }
1209 return found_write;
1210}
1211
1212// Used by the collection functions to help aid in state tracking
1213struct shader_module_used_operators {
1214 bool updated;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001215 std::vector<uint32_t> image_read_members;
1216 std::vector<uint32_t> image_write_members;
1217 std::vector<uint32_t> atomic_members;
1218 std::vector<uint32_t> store_members;
1219 std::vector<uint32_t> atomic_store_members;
1220 std::vector<uint32_t> sampler_implicitLod_dref_proj_members; // sampler Load id
1221 std::vector<uint32_t> sampler_bias_offset_members; // sampler Load id
1222 std::vector<uint32_t> image_dref_members;
1223 std::vector<std::pair<uint32_t, uint32_t>> sampled_image_members; // <image,sampler> Load id
1224 layer_data::unordered_map<uint32_t, uint32_t> load_members;
1225 layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> accesschain_members;
1226 layer_data::unordered_map<uint32_t, uint32_t> image_texel_pointer_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001227
1228 shader_module_used_operators() : updated(false) {}
1229
1230 bool CheckImageOperandsBiasOffset(uint32_t type) {
1231 return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask |
1232 spv::ImageOperandsConstOffsetsMask)
1233 ? true
1234 : false;
1235 }
1236
sfricke-samsungef15e482022-01-26 11:32:49 -08001237 void update(SHADER_MODULE_STATE const *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001238 if (updated) return;
1239 updated = true;
1240
sfricke-samsungef15e482022-01-26 11:32:49 -08001241 for (auto insn : *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001242 switch (insn.opcode()) {
1243 case spv::OpImageSampleImplicitLod:
1244 case spv::OpImageSampleProjImplicitLod:
1245 case spv::OpImageSampleProjExplicitLod:
1246 case spv::OpImageSparseSampleImplicitLod:
1247 case spv::OpImageSparseSampleProjImplicitLod:
1248 case spv::OpImageSparseSampleProjExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001249 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001250 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001251 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1252 sampler_implicitLod_dref_proj_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001253 // ImageOperands in index: 5
1254 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001255 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001256 }
1257 break;
1258 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001259 case spv::OpImageDrefGather:
1260 case spv::OpImageSparseDrefGather: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001261 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001262 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001263 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1264 image_dref_members.emplace_back(load_id);
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001265 break;
1266 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001267 case spv::OpImageSampleDrefImplicitLod:
1268 case spv::OpImageSampleDrefExplicitLod:
1269 case spv::OpImageSampleProjDrefImplicitLod:
1270 case spv::OpImageSampleProjDrefExplicitLod:
1271 case spv::OpImageSparseSampleDrefImplicitLod:
1272 case spv::OpImageSparseSampleDrefExplicitLod:
1273 case spv::OpImageSparseSampleProjDrefImplicitLod:
1274 case spv::OpImageSparseSampleProjDrefExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001275 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001276 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001277 auto sampler_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1278 auto image_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1279
1280 image_dref_members.emplace_back(image_load_id);
1281 sampler_implicitLod_dref_proj_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001282 // ImageOperands in index: 6
1283 if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001284 sampler_bias_offset_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001285 }
1286 break;
1287 }
1288 case spv::OpImageSampleExplicitLod:
1289 case spv::OpImageSparseSampleExplicitLod: {
1290 // ImageOperands in index: 5
1291 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001292 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001293 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001294 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1295 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001296 }
1297 break;
1298 }
1299 case spv::OpStore: {
1300 store_members.emplace_back(insn.word(1)); // object id or AccessChain id
1301 break;
1302 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001303 case spv::OpImageRead:
1304 case spv::OpImageSparseRead: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001305 image_read_members.emplace_back(insn.word(3)); // Load id
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001306 break;
1307 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001308 case spv::OpImageWrite: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001309 image_write_members.emplace_back(insn.word(1)); // Load id
sfricke-samsung962cad92021-04-13 00:46:29 -07001310 break;
1311 }
1312 case spv::OpSampledImage: {
1313 // 3: image load id, 4: sampler load id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001314 sampled_image_members.emplace_back(std::pair<uint32_t, uint32_t>(insn.word(3), insn.word(4)));
sfricke-samsung962cad92021-04-13 00:46:29 -07001315 break;
1316 }
1317 case spv::OpLoad: {
1318 // 2: Load id, 3: object id or AccessChain id
1319 load_members.emplace(insn.word(2), insn.word(3));
1320 break;
1321 }
1322 case spv::OpAccessChain: {
1323 if (insn.len() == 4) {
1324 // If it is for struct, the length is only 4.
1325 // 2: AccessChain id, 3: object id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001326 accesschain_members.emplace(insn.word(2), std::pair<uint32_t, uint32_t>(insn.word(3), 0));
sfricke-samsung962cad92021-04-13 00:46:29 -07001327 } else {
1328 // 2: AccessChain id, 3: object id, 4: object id of array index
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001329 accesschain_members.emplace(insn.word(2), std::pair<uint32_t, uint32_t>(insn.word(3), insn.word(4)));
sfricke-samsung962cad92021-04-13 00:46:29 -07001330 }
1331 break;
1332 }
1333 case spv::OpImageTexelPointer: {
1334 // 2: ImageTexelPointer id, 3: object id
1335 image_texel_pointer_members.emplace(insn.word(2), insn.word(3));
1336 break;
1337 }
1338 default: {
1339 if (AtomicOperation(insn.opcode())) {
1340 if (insn.opcode() == spv::OpAtomicStore) {
1341 atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id
1342 } else {
1343 atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id
1344 }
1345 }
1346 break;
1347 }
1348 }
1349 }
1350 }
1351};
1352
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001353static bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<uint32_t> &operator_members,
1354 const layer_data::unordered_map<uint32_t, uint32_t> &load_members,
1355 const layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> &accesschain_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001356 for (auto load_id : operator_members) {
1357 if (object_id == load_id) return true;
1358 auto load_it = load_members.find(load_id);
1359 if (load_it == load_members.end()) {
1360 continue;
1361 }
1362 if (load_it->second == object_id) {
1363 return true;
1364 }
1365
1366 auto accesschain_it = accesschain_members.find(load_it->second);
1367 if (accesschain_it == accesschain_members.end()) {
1368 continue;
1369 }
1370 if (accesschain_it->second.first == object_id) {
1371 return true;
1372 }
1373 }
1374 return false;
1375}
1376
1377// Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image
1378// atomic operation, matching images to samplers, etc
1379void SHADER_MODULE_STATE::IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable,
1380 interface_var &out_interface_var,
1381 shader_module_used_operators &used_operators) const {
1382 uint32_t type_id = id_it.word(1);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001383 uint32_t id = id_it.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001384
1385 auto type = get_def(type_id);
1386
1387 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
1388 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray ||
1389 type.opcode() == spv::OpTypeSampledImage) {
1390 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray ||
1391 type.opcode() == spv::OpTypeSampledImage) {
1392 type = get_def(type.word(2)); // Element type
1393 } else {
1394 type = get_def(type.word(3)); // Pointer type
1395 }
1396 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001397
sfricke-samsung962cad92021-04-13 00:46:29 -07001398 switch (type.opcode()) {
1399 case spv::OpTypeImage: {
1400 auto dim = type.word(3);
1401 if (dim != spv::DimSubpassData) {
1402 used_operators.update(this);
1403
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001404 // Sampled == 2 indicates used without a sampler (a storage image)
1405 bool is_image_without_format = false;
1406 if (type.word(7) == 2) is_image_without_format = type.word(8) == spv::ImageFormatUnknown;
1407
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001408 if (CheckObjectIDFromOpLoad(id, used_operators.image_write_members, used_operators.load_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001409 used_operators.accesschain_members)) {
1410 out_interface_var.is_writable = true;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001411 if (is_image_without_format) out_interface_var.is_write_without_format = true;
1412 }
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001413 if (CheckObjectIDFromOpLoad(id, used_operators.image_read_members, used_operators.load_members,
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001414 used_operators.accesschain_members)) {
1415 out_interface_var.is_readable = true;
1416 if (is_image_without_format) out_interface_var.is_read_without_format = true;
sfricke-samsung962cad92021-04-13 00:46:29 -07001417 }
1418 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members,
1419 used_operators.accesschain_members)) {
1420 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1421 }
1422 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members,
1423 used_operators.accesschain_members)) {
1424 out_interface_var.is_sampler_bias_offset = true;
1425 }
1426 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members,
1427 used_operators.accesschain_members) ||
1428 CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1429 used_operators.accesschain_members)) {
1430 out_interface_var.is_atomic_operation = true;
1431 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001432 if (CheckObjectIDFromOpLoad(id, used_operators.image_dref_members, used_operators.load_members,
1433 used_operators.accesschain_members)) {
1434 out_interface_var.is_dref_operation = true;
1435 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001436
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001437 for (auto &itp_id : used_operators.sampled_image_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001438 // Find if image id match.
1439 uint32_t image_index = 0;
1440 auto load_it = used_operators.load_members.find(itp_id.first);
1441 if (load_it == used_operators.load_members.end()) {
1442 continue;
1443 } else {
1444 if (load_it->second != id) {
1445 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1446 if (accesschain_it == used_operators.accesschain_members.end()) {
1447 continue;
1448 } else {
1449 if (accesschain_it->second.first != id) {
1450 continue;
1451 }
1452
1453 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1454 if (const_itr == end()) {
1455 // access chain index not a constant, skip.
1456 break;
1457 }
1458 image_index = GetConstantValue(const_itr);
1459 }
1460 }
1461 }
1462 // Find sampler's set binding.
1463 load_it = used_operators.load_members.find(itp_id.second);
1464 if (load_it == used_operators.load_members.end()) {
1465 continue;
1466 } else {
1467 uint32_t sampler_id = load_it->second;
1468 uint32_t sampler_index = 0;
1469 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1470
1471 if (accesschain_it != used_operators.accesschain_members.end()) {
1472 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1473 if (const_itr == end()) {
1474 // access chain index representing sampler index is not a constant, skip.
1475 break;
1476 }
1477 sampler_id = const_itr.offset();
1478 sampler_index = GetConstantValue(const_itr);
1479 }
1480 auto sampler_dec = get_decorations(sampler_id);
1481 if (image_index >= out_interface_var.samplers_used_by_image.size()) {
1482 out_interface_var.samplers_used_by_image.resize(image_index + 1);
1483 }
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001484
1485 // Need to check again for these properties in case not using a combined image sampler
1486 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_implicitLod_dref_proj_members,
1487 used_operators.load_members, used_operators.accesschain_members)) {
1488 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1489 }
1490 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_bias_offset_members,
1491 used_operators.load_members, used_operators.accesschain_members)) {
1492 out_interface_var.is_sampler_bias_offset = true;
1493 }
1494
sfricke-samsung962cad92021-04-13 00:46:29 -07001495 out_interface_var.samplers_used_by_image[image_index].emplace(
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001496 SamplerUsedByImage{DescriptorSlot{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index});
sfricke-samsung962cad92021-04-13 00:46:29 -07001497 }
1498 }
1499 }
1500 return;
1501 }
1502
1503 case spv::OpTypeStruct: {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001504 layer_data::unordered_set<uint32_t> nonwritable_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001505 if (get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001506 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001507 if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) {
1508 nonwritable_members.insert(insn.word(2));
1509 }
1510 }
1511
1512 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
1513 // as nonwritable.
1514 if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) {
1515 used_operators.update(this);
1516
1517 for (auto oid : used_operators.store_members) {
1518 if (id == oid) {
1519 out_interface_var.is_writable = true;
1520 return;
1521 }
1522 auto accesschain_it = used_operators.accesschain_members.find(oid);
1523 if (accesschain_it == used_operators.accesschain_members.end()) {
1524 continue;
1525 }
1526 if (accesschain_it->second.first == id) {
1527 out_interface_var.is_writable = true;
1528 return;
1529 }
1530 }
1531 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1532 used_operators.accesschain_members)) {
1533 out_interface_var.is_writable = true;
1534 return;
1535 }
1536 }
1537 }
1538 }
1539}
1540
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001541std::vector<std::pair<DescriptorSlot, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByDescriptorSlot(
ziga-lunargc2de4782022-04-14 19:49:07 +02001542 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001543 std::vector<std::pair<DescriptorSlot, interface_var>> out;
sfricke-samsung962cad92021-04-13 00:46:29 -07001544 shader_module_used_operators operators;
1545
ziga-lunargc2de4782022-04-14 19:49:07 +02001546 for (auto id : accessible_ids) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001547 auto insn = get_def(id);
1548 assert(insn != end());
1549
1550 if (insn.opcode() == spv::OpVariable &&
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001551 (insn.word(3) == spv::StorageClassUniform ||
1552 insn.word(3) == spv::StorageClassUniformConstant ||
sfricke-samsung962cad92021-04-13 00:46:29 -07001553 insn.word(3) == spv::StorageClassStorageBuffer)) {
1554 auto d = get_decorations(insn.word(2));
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001555 uint32_t set = d.descriptor_set;
1556 uint32_t binding = d.binding;
sfricke-samsung962cad92021-04-13 00:46:29 -07001557
1558 interface_var v = {};
1559 v.id = insn.word(2);
1560 v.type_id = insn.word(1);
1561
1562 IsSpecificDescriptorType(insn, insn.word(3) == spv::StorageClassStorageBuffer,
1563 !(d.flags & decoration_set::nonwritable_bit), v, operators);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001564 out.emplace_back(DescriptorSlot{set, binding}, v);
sfricke-samsung962cad92021-04-13 00:46:29 -07001565 }
1566 }
1567
1568 return out;
1569}
1570
1571layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::CollectWritableOutputLocationinFS(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001572 const spirv_inst_iter &entrypoint) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001573 layer_data::unordered_set<uint32_t> location_list;
sfricke-samsung962cad92021-04-13 00:46:29 -07001574 const auto outputs = CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001575 layer_data::unordered_set<uint32_t> store_members;
1576 layer_data::unordered_map<uint32_t, uint32_t> accesschain_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001577
1578 for (auto insn : *this) {
1579 switch (insn.opcode()) {
1580 case spv::OpStore:
1581 case spv::OpAtomicStore: {
1582 store_members.insert(insn.word(1)); // object id or AccessChain id
1583 break;
1584 }
1585 case spv::OpAccessChain: {
1586 // 2: AccessChain id, 3: object id
1587 if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3));
1588 break;
1589 }
1590 default:
1591 break;
1592 }
1593 }
1594 if (store_members.empty()) {
1595 return location_list;
1596 }
1597 for (auto output : outputs) {
1598 auto store_it = store_members.find(output.second.id);
1599 if (store_it != store_members.end()) {
1600 location_list.insert(output.first.first);
1601 store_members.erase(store_it);
1602 continue;
1603 }
1604 store_it = store_members.begin();
1605 while (store_it != store_members.end()) {
1606 auto accesschain_it = accesschain_members.find(*store_it);
1607 if (accesschain_it == accesschain_members.end()) {
1608 ++store_it;
1609 continue;
1610 }
1611 if (accesschain_it->second == output.second.id) {
1612 location_list.insert(output.first.first);
1613 store_members.erase(store_it);
1614 accesschain_members.erase(accesschain_it);
1615 break;
1616 }
1617 ++store_it;
1618 }
1619 }
1620 return location_list;
1621}
1622
1623bool SHADER_MODULE_STATE::CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts,
ziga-lunarg9e94e112021-09-27 00:21:10 +02001624 uint32_t id, uint32_t type_id, bool is_patch,
1625 uint32_t /*first_location*/) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001626 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
1627 auto type = GetStructType(get_def(type_id), is_array_of_verts && !is_patch);
1628 if (type == end() || !(get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
1629 // This isn't an interface block.
1630 return false;
1631 }
1632
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001633 layer_data::unordered_map<uint32_t, uint32_t> member_components;
1634 layer_data::unordered_map<uint32_t, uint32_t> member_relaxed_precision;
1635 layer_data::unordered_map<uint32_t, uint32_t> member_patch;
sfricke-samsung962cad92021-04-13 00:46:29 -07001636
1637 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001638 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001639 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001640 uint32_t member_index = insn.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001641
1642 if (insn.word(3) == spv::DecorationComponent) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001643 uint32_t component = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001644 member_components[member_index] = component;
1645 }
1646
1647 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
1648 member_relaxed_precision[member_index] = 1;
1649 }
1650
1651 if (insn.word(3) == spv::DecorationPatch) {
1652 member_patch[member_index] = 1;
1653 }
1654 }
1655 }
1656
1657 // TODO: correctly handle location assignment from outside
1658
1659 // Second pass -- produce the output, from Location decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001660 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001661 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001662 uint32_t member_index = insn.word(2);
1663 uint32_t member_type_id = type.word(2 + member_index);
sfricke-samsung962cad92021-04-13 00:46:29 -07001664
1665 if (insn.word(3) == spv::DecorationLocation) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001666 uint32_t location = insn.word(4);
1667 uint32_t num_locations = GetLocationsConsumedByType(member_type_id, false);
sfricke-samsung962cad92021-04-13 00:46:29 -07001668 auto component_it = member_components.find(member_index);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001669 uint32_t component = component_it == member_components.end() ? 0 : component_it->second;
sfricke-samsung962cad92021-04-13 00:46:29 -07001670 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
1671 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
1672
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001673 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001674 interface_var v = {};
1675 v.id = id;
1676 // TODO: member index in interface_var too?
1677 v.type_id = member_type_id;
1678 v.offset = offset;
1679 v.is_patch = member_is_patch;
1680 v.is_block_member = true;
1681 v.is_relaxed_precision = is_relaxed_precision;
1682 (*out)[std::make_pair(location + offset, component)] = v;
1683 }
1684 }
1685 }
1686 }
1687
1688 return true;
1689}
1690
1691std::map<location_t, interface_var> SHADER_MODULE_STATE::CollectInterfaceByLocation(spirv_inst_iter entrypoint,
1692 spv::StorageClass sinterface,
1693 bool is_array_of_verts) const {
1694 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
1695
1696 std::map<location_t, interface_var> out;
1697
1698 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
1699 auto insn = get_def(iid);
1700 assert(insn != end());
1701 assert(insn.opcode() == spv::OpVariable);
1702
ziga-lunarg9e94e112021-09-27 00:21:10 +02001703 const auto d = get_decorations(iid);
1704 bool passthrough = sinterface == spv::StorageClassOutput && insn.word(3) == spv::StorageClassInput &&
1705 (d.flags & decoration_set::passthrough_bit) != 0;
1706 if (insn.word(3) == static_cast<uint32_t>(sinterface) || passthrough) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001707 uint32_t id = insn.word(2);
1708 uint32_t type = insn.word(1);
sfricke-samsung962cad92021-04-13 00:46:29 -07001709
ziga-lunarg9e94e112021-09-27 00:21:10 +02001710 auto location = d.location;
sfricke-samsung962cad92021-04-13 00:46:29 -07001711 int builtin = d.builtin;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001712 uint32_t component = d.component;
sfricke-samsung962cad92021-04-13 00:46:29 -07001713 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
1714 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001715 bool is_per_vertex = (d.flags & decoration_set::per_vertex_bit) != 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001716
1717 if (builtin != -1) {
1718 continue;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001719 } else if (!CollectInterfaceBlockMembers(&out, is_array_of_verts, id, type, is_patch, location) ||
1720 location != decoration_set::kInvalidValue) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001721 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
1722 // one result for each.
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001723 uint32_t num_locations = GetLocationsConsumedByType(type, (is_array_of_verts && !is_patch) || is_per_vertex);
1724 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001725 interface_var v = {};
1726 v.id = id;
1727 v.type_id = type;
1728 v.offset = offset;
1729 v.is_patch = is_patch;
1730 v.is_relaxed_precision = is_relaxed_precision;
1731 out[std::make_pair(location + offset, component)] = v;
1732 }
1733 }
1734 }
1735 }
1736
1737 return out;
1738}
1739
1740std::vector<uint32_t> SHADER_MODULE_STATE::CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001741 // Find all interface variables belonging to the entrypoint and matching the storage class
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001742 std::vector<uint32_t> variables;
sfricke-samsung962cad92021-04-13 00:46:29 -07001743 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
1744 auto def = get_def(id);
1745 assert(def != end());
1746 assert(def.opcode() == spv::OpVariable);
1747
1748 if (def.word(3) == storageClass) variables.push_back(def.word(1));
1749 }
1750
1751 // Find all members belonging to the builtin block selected
1752 std::vector<uint32_t> builtin_block_members;
1753 for (auto &var : variables) {
1754 auto def = get_def(get_def(var).word(3));
1755
1756 // It could be an array of IO blocks. The element type should be the struct defining the block contents
1757 if (def.opcode() == spv::OpTypeArray) def = get_def(def.word(2));
1758
1759 // Now find all members belonging to the struct defining the IO block
1760 if (def.opcode() == spv::OpTypeStruct) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001761 for (auto set : static_data_.builtin_decoration_list) {
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001762 auto insn = at(set.offset);
1763 if ((insn.opcode() == spv::OpMemberDecorate) && (def.word(1) == insn.word(1))) {
1764 // Start with undefined builtin for each struct member.
1765 // But only when confirmed the struct is the built-in inteface block (can only be one per shader)
1766 if (builtin_block_members.size() == 0) {
1767 builtin_block_members.resize(def.len() - 2, spv::BuiltInMax);
sfricke-samsung962cad92021-04-13 00:46:29 -07001768 }
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001769 auto struct_index = insn.word(2);
1770 assert(struct_index < builtin_block_members.size());
1771 builtin_block_members[struct_index] = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001772 }
1773 }
1774 }
1775 }
1776
1777 return builtin_block_members;
1778}
1779
1780std::vector<std::pair<uint32_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByInputAttachmentIndex(
1781 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
1782 std::vector<std::pair<uint32_t, interface_var>> out;
1783
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001784 for (auto insn : static_data_.decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001785 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
1786 auto attachment_index = insn.word(3);
1787 auto id = insn.word(1);
1788
1789 if (accessible_ids.count(id)) {
1790 auto def = get_def(id);
1791 assert(def != end());
1792 if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) {
1793 auto num_locations = GetLocationsConsumedByType(def.word(1), false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001794 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001795 interface_var v = {};
1796 v.id = id;
1797 v.type_id = def.word(1);
1798 v.offset = offset;
1799 out.emplace_back(attachment_index + offset, v);
1800 }
1801 }
1802 }
1803 }
1804 }
1805
1806 return out;
1807}
1808
ziga-lunarg8346fe82021-08-22 17:30:50 +02001809uint32_t SHADER_MODULE_STATE::GetNumComponentsInBaseType(const spirv_inst_iter &iter) const {
1810 const uint32_t opcode = iter.opcode();
1811 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1812 return 1;
1813 } else if (opcode == spv::OpTypeVector) {
1814 const uint32_t component_count = iter.word(3);
1815 return component_count;
1816 } else if (opcode == spv::OpTypeMatrix) {
1817 const auto column_type = get_def(iter.word(2));
1818 const uint32_t vector_length = GetNumComponentsInBaseType(column_type);
ziga-lunarg38e44982022-04-05 00:10:46 +02001819 // Because we are calculating components for a single location we do not care about column count
1820 return vector_length;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001821 } else if (opcode == spv::OpTypeArray) {
1822 const auto element_type = get_def(iter.word(2));
1823 const uint32_t element_length = GetNumComponentsInBaseType(element_type);
1824 return element_length;
1825 } else if (opcode == spv::OpTypeStruct) {
1826 uint32_t total_size = 0;
1827 for (uint32_t i = 2; i < iter.len(); ++i) {
1828 total_size += GetNumComponentsInBaseType(get_def(iter.word(i)));
1829 }
1830 return total_size;
1831 } else if (opcode == spv::OpTypePointer) {
1832 const auto type = get_def(iter.word(3));
1833 return GetNumComponentsInBaseType(type);
1834 }
1835 return 0;
1836}
1837
ziga-lunarga26b3602021-08-08 15:53:00 +02001838uint32_t SHADER_MODULE_STATE::GetTypeBitsSize(const spirv_inst_iter &iter) const {
1839 const uint32_t opcode = iter.opcode();
1840 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1841 return iter.word(2);
1842 } else if (opcode == spv::OpTypeVector) {
1843 const auto component_type = get_def(iter.word(2));
1844 uint32_t scalar_width = GetTypeBitsSize(component_type);
1845 uint32_t component_count = iter.word(3);
1846 return scalar_width * component_count;
1847 } else if (opcode == spv::OpTypeMatrix) {
1848 const auto column_type = get_def(iter.word(2));
1849 uint32_t vector_width = GetTypeBitsSize(column_type);
1850 uint32_t column_count = iter.word(3);
1851 return vector_width * column_count;
1852 } else if (opcode == spv::OpTypeArray) {
1853 const auto element_type = get_def(iter.word(2));
1854 uint32_t element_width = GetTypeBitsSize(element_type);
1855 const auto length_type = get_def(iter.word(3));
1856 uint32_t length = GetConstantValue(length_type);
1857 return element_width * length;
1858 } else if (opcode == spv::OpTypeStruct) {
1859 uint32_t total_size = 0;
1860 for (uint32_t i = 2; i < iter.len(); ++i) {
1861 total_size += GetTypeBitsSize(get_def(iter.word(i)));
1862 }
1863 return total_size;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001864 } else if (opcode == spv::OpTypePointer) {
1865 const auto type = get_def(iter.word(3));
1866 return GetTypeBitsSize(type);
ziga-lunargef2c3172021-11-07 10:35:29 +01001867 } else if (opcode == spv::OpVariable) {
1868 const auto type = get_def(iter.word(1));
1869 return GetTypeBitsSize(type);
ziga-lunarga26b3602021-08-08 15:53:00 +02001870 }
1871 return 0;
1872}
1873
1874uint32_t SHADER_MODULE_STATE::GetTypeBytesSize(const spirv_inst_iter &iter) const { return GetTypeBitsSize(iter) / 8; }
1875
ziga-lunarg19fc6ae2021-09-09 00:05:19 +02001876// Returns the base type (float, int or unsigned int) or struct (can have multiple different base types inside)
ziga-lunarg8346fe82021-08-22 17:30:50 +02001877uint32_t SHADER_MODULE_STATE::GetBaseType(const spirv_inst_iter &iter) const {
1878 const uint32_t opcode = iter.opcode();
1879 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt || opcode == spv::OpTypeStruct) {
1880 return iter.word(1);
1881 } else if (opcode == spv::OpTypeVector) {
1882 const auto& component_type = get_def(iter.word(2));
1883 return GetBaseType(component_type);
1884 } else if (opcode == spv::OpTypeMatrix) {
1885 const auto& column_type = get_def(iter.word(2));
1886 return GetBaseType(column_type);
1887 } else if (opcode == spv::OpTypeArray) {
1888 const auto& element_type = get_def(iter.word(2));
1889 return GetBaseType(element_type);
1890 } else if (opcode == spv::OpTypePointer) {
1891 const auto& type = get_def(iter.word(3));
1892 return GetBaseType(type);
1893 }
1894 return 0;
1895}
1896
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001897// Returns type_id if id has type or zero otherwise
1898uint32_t SHADER_MODULE_STATE::GetTypeId(uint32_t id) const {
1899 const auto type = get_def(id);
1900 return OpcodeHasType(type.opcode()) ? type.word(1) : 0;
1901}
1902
sfricke-samsung962cad92021-04-13 00:46:29 -07001903// Assumes itr points to an OpConstant instruction
1904uint32_t GetConstantValue(const spirv_inst_iter &itr) { return itr.word(3); }
1905
1906std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint) {
1907 assert(entrypoint.opcode() == spv::OpEntryPoint);
1908
1909 std::vector<uint32_t> interfaces;
1910 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
1911 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
1912 uint32_t word = 3;
1913 while (entrypoint.word(word) & 0xff000000u) {
1914 ++word;
1915 }
1916 ++word;
1917
1918 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
1919
1920 return interfaces;
1921}