blob: 510c2dd58e2f09ec985bae59f9ee6f56897797a9 [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;
sjfrickede734312022-07-14 19:22:43 +090089 case spv::DecorationAliased:
90 flags |= aliased_bit;
91 break;
sfricke-samsung962cad92021-04-13 00:46:29 -070092 }
93}
94
95std::string shader_struct_member::GetLocationDesc(uint32_t index_used_bytes) const {
96 std::string desc = "";
97 if (array_length_hierarchy.size() > 0) {
98 desc += " index:";
99 for (const auto block_size : array_block_size) {
100 desc += "[";
101 desc += std::to_string(index_used_bytes / (block_size * size));
102 desc += "]";
103 index_used_bytes = index_used_bytes % (block_size * size);
104 }
105 }
106 const int struct_members_size = static_cast<int>(struct_members.size());
107 if (struct_members_size > 0) {
108 desc += " member:";
109 for (int i = struct_members_size - 1; i >= 0; --i) {
110 if (index_used_bytes > struct_members[i].offset) {
111 desc += std::to_string(i);
112 desc += struct_members[i].GetLocationDesc(index_used_bytes - struct_members[i].offset);
113 break;
114 }
115 }
116 } else {
117 desc += " offset:";
118 desc += std::to_string(index_used_bytes);
119 }
120 return desc;
121}
122
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800123static uint32_t ExecutionModelToShaderStageFlagBits(uint32_t mode) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700124 switch (mode) {
125 case spv::ExecutionModelVertex:
126 return VK_SHADER_STAGE_VERTEX_BIT;
127 case spv::ExecutionModelTessellationControl:
128 return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
129 case spv::ExecutionModelTessellationEvaluation:
130 return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
131 case spv::ExecutionModelGeometry:
132 return VK_SHADER_STAGE_GEOMETRY_BIT;
133 case spv::ExecutionModelFragment:
134 return VK_SHADER_STAGE_FRAGMENT_BIT;
135 case spv::ExecutionModelGLCompute:
136 return VK_SHADER_STAGE_COMPUTE_BIT;
sjfricke62366d32022-08-01 21:04:10 +0900137 case spv::ExecutionModelRayGenerationKHR:
138 return VK_SHADER_STAGE_RAYGEN_BIT_KHR;
139 case spv::ExecutionModelAnyHitKHR:
140 return VK_SHADER_STAGE_ANY_HIT_BIT_KHR;
141 case spv::ExecutionModelClosestHitKHR:
142 return VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR;
143 case spv::ExecutionModelMissKHR:
144 return VK_SHADER_STAGE_MISS_BIT_KHR;
145 case spv::ExecutionModelIntersectionKHR:
146 return VK_SHADER_STAGE_INTERSECTION_BIT_KHR;
147 case spv::ExecutionModelCallableKHR:
148 return VK_SHADER_STAGE_CALLABLE_BIT_KHR;
sfricke-samsung962cad92021-04-13 00:46:29 -0700149 case spv::ExecutionModelTaskNV:
150 return VK_SHADER_STAGE_TASK_BIT_NV;
151 case spv::ExecutionModelMeshNV:
152 return VK_SHADER_STAGE_MESH_BIT_NV;
153 default:
154 return 0;
155 }
156}
157
158// For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is
159// important for identifying the set of shader resources actually used by an entrypoint, for example.
160// Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
161// - NOT the shader input/output interfaces.
162//
163// TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
164// converting parts of this to be generated from the machine-readable spec instead.
165layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::MarkAccessibleIds(spirv_inst_iter entrypoint) const {
166 layer_data::unordered_set<uint32_t> ids;
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600167 if (entrypoint == end() || !has_valid_spirv) {
168 return ids;
169 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700170 layer_data::unordered_set<uint32_t> worklist;
171 worklist.insert(entrypoint.word(2));
172
173 while (!worklist.empty()) {
174 auto id_iter = worklist.begin();
175 auto id = *id_iter;
176 worklist.erase(id_iter);
177
178 auto insn = get_def(id);
179 if (insn == end()) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600180 // 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 -0700181 // that we may not care about.
182 continue;
183 }
184
185 // Try to add to the output set
186 if (!ids.insert(id).second) {
187 continue; // If we already saw this id, we don't want to walk it again.
188 }
189
190 switch (insn.opcode()) {
191 case spv::OpFunction:
192 // Scan whole body of the function, enlisting anything interesting
193 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
194 switch (insn.opcode()) {
195 case spv::OpLoad:
196 worklist.insert(insn.word(3)); // ptr
197 break;
198 case spv::OpStore:
199 worklist.insert(insn.word(1)); // ptr
200 break;
201 case spv::OpAccessChain:
202 case spv::OpInBoundsAccessChain:
203 worklist.insert(insn.word(3)); // base ptr
204 break;
205 case spv::OpSampledImage:
206 case spv::OpImageSampleImplicitLod:
207 case spv::OpImageSampleExplicitLod:
208 case spv::OpImageSampleDrefImplicitLod:
209 case spv::OpImageSampleDrefExplicitLod:
210 case spv::OpImageSampleProjImplicitLod:
211 case spv::OpImageSampleProjExplicitLod:
212 case spv::OpImageSampleProjDrefImplicitLod:
213 case spv::OpImageSampleProjDrefExplicitLod:
214 case spv::OpImageFetch:
215 case spv::OpImageGather:
216 case spv::OpImageDrefGather:
217 case spv::OpImageRead:
218 case spv::OpImage:
219 case spv::OpImageQueryFormat:
220 case spv::OpImageQueryOrder:
221 case spv::OpImageQuerySizeLod:
222 case spv::OpImageQuerySize:
223 case spv::OpImageQueryLod:
224 case spv::OpImageQueryLevels:
225 case spv::OpImageQuerySamples:
226 case spv::OpImageSparseSampleImplicitLod:
227 case spv::OpImageSparseSampleExplicitLod:
228 case spv::OpImageSparseSampleDrefImplicitLod:
229 case spv::OpImageSparseSampleDrefExplicitLod:
230 case spv::OpImageSparseSampleProjImplicitLod:
231 case spv::OpImageSparseSampleProjExplicitLod:
232 case spv::OpImageSparseSampleProjDrefImplicitLod:
233 case spv::OpImageSparseSampleProjDrefExplicitLod:
234 case spv::OpImageSparseFetch:
235 case spv::OpImageSparseGather:
236 case spv::OpImageSparseDrefGather:
237 case spv::OpImageTexelPointer:
238 worklist.insert(insn.word(3)); // Image or sampled image
239 break;
240 case spv::OpImageWrite:
241 worklist.insert(insn.word(1)); // Image -- different operand order to above
242 break;
243 case spv::OpFunctionCall:
244 for (uint32_t i = 3; i < insn.len(); i++) {
245 worklist.insert(insn.word(i)); // fn itself, and all args
246 }
247 break;
248
249 case spv::OpExtInst:
250 for (uint32_t i = 5; i < insn.len(); i++) {
251 worklist.insert(insn.word(i)); // Operands to ext inst
252 }
253 break;
254
255 default: {
256 if (AtomicOperation(insn.opcode())) {
257 if (insn.opcode() == spv::OpAtomicStore) {
258 worklist.insert(insn.word(1)); // ptr
259 } else {
260 worklist.insert(insn.word(3)); // ptr
261 }
262 }
263 break;
264 }
265 }
266 }
267 break;
268 }
269 }
270
271 return ids;
272}
273
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600274layer_data::optional<VkPrimitiveTopology> SHADER_MODULE_STATE::GetTopology(const spirv_inst_iter &entrypoint) const {
275 layer_data::optional<VkPrimitiveTopology> result;
276
sfricke-samsung962cad92021-04-13 00:46:29 -0700277 auto entrypoint_id = entrypoint.word(2);
278 bool is_point_mode = false;
279
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600280 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
281 if (it != static_data_.execution_mode_inst.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700282 for (auto insn : it->second) {
283 switch (insn.word(2)) {
284 case spv::ExecutionModePointMode:
285 // In tessellation shaders, PointMode is separate and trumps the tessellation topology.
286 is_point_mode = true;
287 break;
288
289 case spv::ExecutionModeOutputPoints:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600290 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
sfricke-samsung962cad92021-04-13 00:46:29 -0700291 break;
292
293 case spv::ExecutionModeIsolines:
294 case spv::ExecutionModeOutputLineStrip:
Ricardo Garcia122f8f02021-09-28 16:47:19 +0200295 case spv::ExecutionModeOutputLinesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600296 result.emplace(VK_PRIMITIVE_TOPOLOGY_LINE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700297 break;
298
299 case spv::ExecutionModeTriangles:
300 case spv::ExecutionModeQuads:
301 case spv::ExecutionModeOutputTriangleStrip:
302 case spv::ExecutionModeOutputTrianglesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600303 result.emplace(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700304 break;
305 }
306 }
307 }
308
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600309 if (is_point_mode) {
310 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
311 }
312
313 return result;
sfricke-samsung962cad92021-04-13 00:46:29 -0700314}
315
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700316layer_data::optional<VkPrimitiveTopology> SHADER_MODULE_STATE::GetTopology() const {
317 if (static_data_.entry_points.size() > 0) {
318 const auto entrypoint = static_data_.entry_points.cbegin()->second;
319 return GetTopology(get_def(entrypoint.offset));
320 }
321 return {};
322}
323
sfricke-samsungef15e482022-01-26 11:32:49 -0800324SHADER_MODULE_STATE::SpirvStaticData::SpirvStaticData(const SHADER_MODULE_STATE &module_state) {
325 for (auto insn : module_state) {
sfricke-samsung5a48ed42022-02-13 17:37:13 -0800326 const uint32_t result_word = OpcodeResultWord(insn.opcode());
327 if (result_word != 0) {
328 def_index[insn.word(result_word)] = insn.offset();
329 }
330
sfricke-samsung962cad92021-04-13 00:46:29 -0700331 switch (insn.opcode()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700332 // Specialization constants
333 case spv::OpSpecConstantTrue:
334 case spv::OpSpecConstantFalse:
335 case spv::OpSpecConstant:
336 case spv::OpSpecConstantComposite:
337 case spv::OpSpecConstantOp:
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600338 has_specialization_constants = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700339 break;
340
sfricke-samsung962cad92021-04-13 00:46:29 -0700341 // Decorations
342 case spv::OpDecorate: {
343 auto target_id = insn.word(1);
344 decorations[target_id].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u);
345 decoration_inst.push_back(insn);
346 if (insn.word(2) == spv::DecorationBuiltIn) {
347 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(3)));
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -0600348 } else if (insn.word(2) == spv::DecorationSpecId) {
349 spec_const_map[insn.word(3)] = target_id;
sfricke-samsung962cad92021-04-13 00:46:29 -0700350 }
351
352 } break;
353 case spv::OpGroupDecorate: {
354 auto const &src = decorations[insn.word(1)];
355 for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600356 has_group_decoration = true;
357 } break;
358 case spv::OpDecorationGroup:
359 case spv::OpGroupMemberDecorate: {
360 has_group_decoration = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700361 } break;
362 case spv::OpMemberDecorate: {
363 member_decoration_inst.push_back(insn);
364 if (insn.word(3) == spv::DecorationBuiltIn) {
365 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(4)));
366 }
367 } break;
368
ziga-lunargbe003732022-04-22 00:20:13 +0200369 case spv::OpCapability:
370 capability_list.push_back(static_cast<spv::Capability>(insn.word(1)));
371 break;
372
sjfricke44d663c2022-06-01 06:42:58 +0900373 case spv::OpVariable:
374 variable_inst.push_back(insn);
375 break;
376
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600377 // Execution Mode
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800378 case spv::OpExecutionMode:
379 case spv::OpExecutionModeId: {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600380 execution_mode_inst[insn.word(1)].push_back(insn);
381 } break;
ziga-lunarge25f5f02022-04-16 15:07:35 +0200382 // Once https://github.com/KhronosGroup/SPIRV-Headers/issues/276 is added this should be derived from SPIR-V grammar
383 // json
384 case spv::OpTraceRayKHR:
385 case spv::OpTraceRayMotionNV:
386 case spv::OpReportIntersectionKHR:
387 case spv::OpExecuteCallableKHR:
388 has_invocation_repack_instruction = true;
389 break;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600390
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600391 default:
392 if (AtomicOperation(insn.opcode()) == true) {
393 // All atomics have a pointer referenced
394 spirv_inst_iter access;
395 if (insn.opcode() == spv::OpAtomicStore) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800396 access = module_state.get_def(insn.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600397 } else {
sfricke-samsungef15e482022-01-26 11:32:49 -0800398 access = module_state.get_def(insn.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600399 }
400
401 atomic_instruction atomic;
402
sfricke-samsungef15e482022-01-26 11:32:49 -0800403 auto pointer = module_state.get_def(access.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600404 // spirv-val should catch if not pointer
405 assert(pointer.opcode() == spv::OpTypePointer);
406 atomic.storage_class = pointer.word(2);
407
sfricke-samsungef15e482022-01-26 11:32:49 -0800408 auto data_type = module_state.get_def(pointer.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600409 atomic.type = data_type.opcode();
410
411 // TODO - Should have a proper GetBitWidth like spirv-val does
412 assert(data_type.opcode() == spv::OpTypeFloat || data_type.opcode() == spv::OpTypeInt);
413 atomic.bit_width = data_type.word(2);
414
415 atomic_inst[insn.offset()] = atomic;
416 }
417 // We don't care about any other defs for now.
418 break;
419 }
420 }
421
sfricke-samsungef15e482022-01-26 11:32:49 -0800422 entry_points = SHADER_MODULE_STATE::ProcessEntryPoints(module_state);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600423 multiple_entry_points = entry_points.size() > 1;
424}
425
426// static
427std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> SHADER_MODULE_STATE::ProcessEntryPoints(
sfricke-samsungef15e482022-01-26 11:32:49 -0800428 const SHADER_MODULE_STATE &module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600429 std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> entry_points;
430 function_set func_set = {};
431 EntryPoint *entry_point = nullptr;
432
sfricke-samsungef15e482022-01-26 11:32:49 -0800433 for (auto insn : module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600434 // offset is not 0, it means it's updated and the offset is in a Function.
435 if (func_set.offset) {
436 func_set.op_lists.emplace(insn.opcode(), insn.offset());
437 } else if (entry_point) {
438 entry_point->decorate_list.emplace(insn.opcode(), insn.offset());
439 }
440
441 switch (insn.opcode()) {
442 // Functions
443 case spv::OpFunction:
444 func_set.id = insn.word(2);
445 func_set.offset = insn.offset();
446 func_set.op_lists.clear();
447 break;
448
449 // Entry points ... add to the entrypoint table
450 case spv::OpEntryPoint: {
sfricke-samsung962cad92021-04-13 00:46:29 -0700451 // Entry points do not have an id (the id is the function id) and thus need their own table
452 auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3));
453 auto execution_model = insn.word(1);
454 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
455 entry_points.emplace(entrypoint_name,
456 EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)});
457
458 auto range = entry_points.equal_range(entrypoint_name);
459 for (auto it = range.first; it != range.second; ++it) {
460 if (it->second.offset == insn.offset()) {
461 entry_point = &(it->second);
462 break;
463 }
464 }
465 assert(entry_point != nullptr);
466 break;
467 }
468 case spv::OpFunctionEnd: {
469 assert(entry_point != nullptr);
470 func_set.length = insn.offset() - func_set.offset;
471 entry_point->function_set_list.emplace_back(func_set);
472 break;
473 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700474 }
475 }
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600476
sfricke-samsungef15e482022-01-26 11:32:49 -0800477 SHADER_MODULE_STATE::SetPushConstantUsedInShader(module_state, entry_points);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600478 return entry_points;
sfricke-samsung962cad92021-04-13 00:46:29 -0700479}
480
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600481void SHADER_MODULE_STATE::PreprocessShaderBinary(const spv_target_env env) {
482 if (static_data_.has_group_decoration) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700483 spvtools::Optimizer optimizer(env);
484 optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass());
485 std::vector<uint32_t> optimized_binary;
486 // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600487 auto result = optimizer.Run(words.data(), words.size(), &optimized_binary, spvtools::ValidatorOptions(), true);
488
sfricke-samsung962cad92021-04-13 00:46:29 -0700489 if (result) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600490 // NOTE: We need to update words with the result from the spirv-tools optimizer.
491 // **THIS ONLY HAPPENS ON INITIALIZATION**. words should remain const for the lifetime
492 // of the SHADER_MODULE_STATE instance.
493 *const_cast<std::vector<uint32_t> *>(&words) = std::move(optimized_binary);
sfricke-samsung962cad92021-04-13 00:46:29 -0700494 }
495 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700496}
497
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800498char const *StorageClassName(uint32_t sc) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700499 switch (sc) {
500 case spv::StorageClassInput:
501 return "input";
502 case spv::StorageClassOutput:
503 return "output";
504 case spv::StorageClassUniformConstant:
505 return "const uniform";
506 case spv::StorageClassUniform:
507 return "uniform";
508 case spv::StorageClassWorkgroup:
509 return "workgroup local";
510 case spv::StorageClassCrossWorkgroup:
511 return "workgroup global";
512 case spv::StorageClassPrivate:
513 return "private global";
514 case spv::StorageClassFunction:
515 return "function";
516 case spv::StorageClassGeneric:
517 return "generic";
518 case spv::StorageClassAtomicCounter:
519 return "atomic counter";
520 case spv::StorageClassImage:
521 return "image";
522 case spv::StorageClassPushConstant:
523 return "push constant";
524 case spv::StorageClassStorageBuffer:
525 return "storage buffer";
526 default:
527 return "unknown";
528 }
529}
530
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800531void SHADER_MODULE_STATE::DescribeTypeInner(std::ostringstream &ss, uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700532 auto insn = get_def(type);
533 assert(insn != end());
534
535 switch (insn.opcode()) {
536 case spv::OpTypeBool:
537 ss << "bool";
538 break;
539 case spv::OpTypeInt:
540 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
541 break;
542 case spv::OpTypeFloat:
543 ss << "float" << insn.word(2);
544 break;
545 case spv::OpTypeVector:
546 ss << "vec" << insn.word(3) << " of ";
547 DescribeTypeInner(ss, insn.word(2));
548 break;
549 case spv::OpTypeMatrix:
550 ss << "mat" << insn.word(3) << " of ";
551 DescribeTypeInner(ss, insn.word(2));
552 break;
553 case spv::OpTypeArray:
554 ss << "arr[" << GetConstantValueById(insn.word(3)) << "] of ";
555 DescribeTypeInner(ss, insn.word(2));
556 break;
557 case spv::OpTypeRuntimeArray:
558 ss << "runtime arr[] of ";
559 DescribeTypeInner(ss, insn.word(2));
560 break;
561 case spv::OpTypePointer:
562 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
563 DescribeTypeInner(ss, insn.word(3));
564 break;
565 case spv::OpTypeStruct: {
566 ss << "struct of (";
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800567 for (uint32_t i = 2; i < insn.len(); i++) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700568 DescribeTypeInner(ss, insn.word(i));
569 if (i == insn.len() - 1) {
570 ss << ")";
571 } else {
572 ss << ", ";
573 }
574 }
575 break;
576 }
577 case spv::OpTypeSampler:
578 ss << "sampler";
579 break;
580 case spv::OpTypeSampledImage:
581 ss << "sampler+";
582 DescribeTypeInner(ss, insn.word(2));
583 break;
584 case spv::OpTypeImage:
585 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
586 break;
587 case spv::OpTypeAccelerationStructureNV:
588 ss << "accelerationStruture";
589 break;
590 default:
591 ss << "oddtype";
592 break;
593 }
594}
595
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800596std::string SHADER_MODULE_STATE::DescribeType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700597 std::ostringstream ss;
598 DescribeTypeInner(ss, type);
599 return ss.str();
600}
601
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800602std::string SHADER_MODULE_STATE::DescribeInstruction(const spirv_inst_iter &insn) const {
603 std::ostringstream ss;
604 const uint32_t opcode = insn.opcode();
605 uint32_t operand_offset = 1; // where to start printing operands
606 // common disassembled for SPIR-V is
607 // %result = Opcode %result_type %operands
608 if (OpcodeHasResult(opcode)) {
609 operand_offset++;
610 ss << "%" << (OpcodeHasType(opcode) ? insn.word(2) : insn.word(1)) << " = ";
611 }
612
613 ss << string_SpvOpcode(opcode);
614
615 if (OpcodeHasType(opcode)) {
616 operand_offset++;
617 ss << " %" << insn.word(1);
618 }
619
sfricke-samsunged00aa42022-01-27 19:03:01 -0800620 // TODO - For now don't list the '%' for any operands since they are only for reference IDs. Without generating a table of each
621 // instructions operand types and covering the many edge cases (such as optional, paired, or variable operands) this is the
622 // simplest way to print the instruction and give the developer something to look into when an error occurs.
623 //
624 // For now this safely should be able to assume it will never come across a LiteralString such as in OpExtInstImport or
625 // OpEntryPoint
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800626 for (uint32_t i = operand_offset; i < insn.len(); i++) {
sfricke-samsunged00aa42022-01-27 19:03:01 -0800627 ss << " " << insn.word(i);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800628 }
629 return ss.str();
630}
631
sfricke-samsung962cad92021-04-13 00:46:29 -0700632const SHADER_MODULE_STATE::EntryPoint *SHADER_MODULE_STATE::FindEntrypointStruct(char const *name,
633 VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600634 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700635 for (auto it = range.first; it != range.second; ++it) {
636 if (it->second.stage == stageBits) {
637 return &(it->second);
638 }
639 }
640 return nullptr;
641}
642
643spirv_inst_iter SHADER_MODULE_STATE::FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600644 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700645 for (auto it = range.first; it != range.second; ++it) {
646 if (it->second.stage == stageBits) {
647 return at(it->second.offset);
648 }
649 }
650 return end();
651}
652
653// Because the following is legal, need the entry point
654// OpEntryPoint GLCompute %main "name_a"
655// OpEntryPoint GLCompute %main "name_b"
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800656// Assumes shader module contains no spec constants used to set the local size values
sfricke-samsung962cad92021-04-13 00:46:29 -0700657bool SHADER_MODULE_STATE::FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y,
658 uint32_t &local_size_z) const {
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800659 // "If an object is decorated with the WorkgroupSize decoration, this takes precedence over any LocalSize or LocalSizeId
660 // execution mode."
661 for (const auto &builtin : static_data_.builtin_decoration_list) {
662 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
663 const uint32_t workgroup_size_id = at(builtin.offset).word(1);
664 auto composite_def = get_def(workgroup_size_id);
665 if (composite_def.opcode() == spv::OpConstantComposite) {
666 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32
sjfricke3b0cb102022-08-10 16:27:45 +0900667 local_size_x = GetConstantValueById(composite_def.word(3));
668 local_size_y = GetConstantValueById(composite_def.word(4));
669 local_size_z = GetConstantValueById(composite_def.word(5));
sfricke-samsung962cad92021-04-13 00:46:29 -0700670 return true;
671 }
672 }
673 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800674
675 auto entrypoint_id = entrypoint.word(2);
676 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
677 if (it != static_data_.execution_mode_inst.end()) {
678 for (auto insn : it->second) {
679 if (insn.opcode() == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeLocalSize) {
680 local_size_x = insn.word(3);
681 local_size_y = insn.word(4);
682 local_size_z = insn.word(5);
683 return true;
684 } else if (insn.opcode() == spv::OpExecutionModeId && insn.word(2) == spv::ExecutionModeLocalSizeId) {
685 local_size_x = GetConstantValueById(insn.word(3));
686 local_size_y = GetConstantValueById(insn.word(4));
687 local_size_z = GetConstantValueById(insn.word(5));
688 return true;
689 }
690 }
691 }
692 return false; // not found
sfricke-samsung962cad92021-04-13 00:46:29 -0700693}
694
695// If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction.
696// Otherwise, returns src->end().
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800697spirv_inst_iter SHADER_MODULE_STATE::GetConstantDef(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700698 auto value = get_def(id);
699
700 // If id is a copy, see where it was copied from
701 if ((end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) {
702 id = value.word(3);
703 value = get_def(id);
704 }
705
706 if ((end() != value) && (value.opcode() == spv::OpConstant)) {
707 return value;
708 }
709 return end();
710}
711
sjfricke3b0cb102022-08-10 16:27:45 +0900712// While simple, function name provides a more human readable description why word(3) is used
713uint32_t SHADER_MODULE_STATE::GetConstantValue(const spirv_inst_iter &itr) const {
714 assert(itr.opcode() == spv::OpConstant);
715 return itr.word(3);
716}
717
sfricke-samsung962cad92021-04-13 00:46:29 -0700718// Either returns the constant value described by the instruction at id, or 1
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800719uint32_t SHADER_MODULE_STATE::GetConstantValueById(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700720 auto value = GetConstantDef(id);
721
722 if (end() == value) {
723 // TODO: Either ensure that the specialization transform is already performed on a module we're
724 // considering here, OR -- specialize on the fly now.
725 return 1;
726 }
sjfricke3b0cb102022-08-10 16:27:45 +0900727
sfricke-samsung962cad92021-04-13 00:46:29 -0700728 return GetConstantValue(value);
729}
730
731// Returns an int32_t corresponding to the spv::Dim of the given resource, when positive, and corresponding to an unknown type, when
732// negative.
733int32_t SHADER_MODULE_STATE::GetShaderResourceDimensionality(const interface_var &resource) const {
734 auto type = get_def(resource.type_id);
735 while (true) {
736 switch (type.opcode()) {
737 case spv::OpTypeSampledImage:
738 type = get_def(type.word(2));
739 break;
740 case spv::OpTypePointer:
741 type = get_def(type.word(3));
742 break;
743 case spv::OpTypeImage:
744 return type.word(3);
745 default:
746 return -1;
747 }
748 }
749}
750
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800751uint32_t SHADER_MODULE_STATE::GetLocationsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700752 auto insn = get_def(type);
753 assert(insn != end());
754
755 switch (insn.opcode()) {
756 case spv::OpTypePointer:
757 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
758 // pointers around.
759 return GetLocationsConsumedByType(insn.word(3), strip_array_level);
760 case spv::OpTypeArray:
761 if (strip_array_level) {
762 return GetLocationsConsumedByType(insn.word(2), false);
763 } else {
764 return GetConstantValueById(insn.word(3)) * GetLocationsConsumedByType(insn.word(2), false);
765 }
766 case spv::OpTypeMatrix:
767 // Num locations is the dimension * element size
768 return insn.word(3) * GetLocationsConsumedByType(insn.word(2), false);
769 case spv::OpTypeVector: {
770 auto scalar_type = get_def(insn.word(2));
771 auto bit_width =
772 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
773
774 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
775 return (bit_width * insn.word(3) + 127) / 128;
776 }
777 default:
778 // Everything else is just 1.
779 return 1;
780
781 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
782 }
783}
784
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800785uint32_t SHADER_MODULE_STATE::GetComponentsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700786 auto insn = get_def(type);
787 assert(insn != end());
788
789 switch (insn.opcode()) {
790 case spv::OpTypePointer:
791 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
792 // pointers around.
793 return GetComponentsConsumedByType(insn.word(3), strip_array_level);
794 case spv::OpTypeStruct: {
795 uint32_t sum = 0;
796 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
797 sum += GetComponentsConsumedByType(insn.word(i), false);
798 }
799 return sum;
800 }
801 case spv::OpTypeArray:
802 if (strip_array_level) {
803 return GetComponentsConsumedByType(insn.word(2), false);
804 } else {
805 return GetConstantValueById(insn.word(3)) * GetComponentsConsumedByType(insn.word(2), false);
806 }
807 case spv::OpTypeMatrix:
808 // Num locations is the dimension * element size
809 return insn.word(3) * GetComponentsConsumedByType(insn.word(2), false);
810 case spv::OpTypeVector: {
811 auto scalar_type = get_def(insn.word(2));
812 auto bit_width =
813 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
814 // One component is 32-bit
815 return (bit_width * insn.word(3) + 31) / 32;
816 }
817 case spv::OpTypeFloat: {
818 auto bit_width = insn.word(2);
819 return (bit_width + 31) / 32;
820 }
821 case spv::OpTypeInt: {
822 auto bit_width = insn.word(2);
823 return (bit_width + 31) / 32;
824 }
825 case spv::OpConstant:
826 return GetComponentsConsumedByType(insn.word(1), false);
827 default:
828 return 0;
829 }
830}
831
832// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
833// also used for input attachments, as we statically know their format.
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800834uint32_t SHADER_MODULE_STATE::GetFundamentalType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700835 auto insn = get_def(type);
836 assert(insn != end());
837
838 switch (insn.opcode()) {
839 case spv::OpTypeInt:
840 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
841 case spv::OpTypeFloat:
842 return FORMAT_TYPE_FLOAT;
843 case spv::OpTypeVector:
844 case spv::OpTypeMatrix:
845 case spv::OpTypeArray:
846 case spv::OpTypeRuntimeArray:
847 case spv::OpTypeImage:
848 return GetFundamentalType(insn.word(2));
849 case spv::OpTypePointer:
850 return GetFundamentalType(insn.word(3));
851
852 default:
853 return 0;
854 }
855}
856
857spirv_inst_iter SHADER_MODULE_STATE::GetStructType(spirv_inst_iter def, bool is_array_of_verts) const {
858 while (true) {
859 if (def.opcode() == spv::OpTypePointer) {
860 def = get_def(def.word(3));
861 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
862 def = get_def(def.word(2));
863 is_array_of_verts = false;
864 } else if (def.opcode() == spv::OpTypeStruct) {
865 return def;
866 } else {
867 return end();
868 }
869 }
870}
871
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800872void SHADER_MODULE_STATE::DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &member_decorate_offsets,
sfricke-samsung962cad92021-04-13 00:46:29 -0700873 shader_struct_member &data) const {
874 const auto struct_it = GetStructType(it, false);
875 assert(struct_it != end());
876 data.size = 0;
877
878 shader_struct_member data1;
879 uint32_t i = 2;
880 uint32_t local_offset = 0;
881 std::vector<uint32_t> offsets;
882 offsets.resize(struct_it.len() - i);
883
884 // 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 -0800885 for (const auto offset : member_decorate_offsets) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700886 const auto member_decorate = at(offset);
887 if (member_decorate.word(1) != struct_it.word(1)) {
888 continue;
889 }
890
891 offsets[member_decorate.word(2)] = member_decorate.word(4);
892 }
893
894 for (const auto offset : offsets) {
895 local_offset = offset;
896 data1 = {};
897 data1.root = data.root;
898 data1.offset = local_offset;
899 auto def_member = get_def(struct_it.word(i));
900
901 // Array could be multi-dimensional
902 while (def_member.opcode() == spv::OpTypeArray) {
903 const auto len_id = def_member.word(3);
904 const auto def_len = get_def(len_id);
905 data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length
906 def_member = get_def(def_member.word(2));
907 }
908
909 if (def_member.opcode() == spv::OpTypeStruct) {
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800910 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700911 } else if (def_member.opcode() == spv::OpTypePointer) {
912 if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) {
913 // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address
914 // that "points to something."
915 data1.size = 8;
916 } else {
917 // 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 -0800918 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700919 }
920 } else {
921 if (def_member.opcode() == spv::OpTypeMatrix) {
922 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector.
923 def_member = get_def(def_member.word(2));
924 }
925
926 if (def_member.opcode() == spv::OpTypeVector) {
927 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length
928 def_member = get_def(def_member.word(2));
929 }
930
931 // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte.
932 data1.size = (def_member.word(2) / 8);
933 }
934 const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size();
935 if (array_length_hierarchy_szie > 0) {
936 data1.array_block_size.resize(array_length_hierarchy_szie, 1);
937
938 for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) {
939 data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2];
940 }
941 }
942 data.struct_members.emplace_back(data1);
943 ++i;
944 }
945 uint32_t total_array_length = 1;
946 for (const auto length : data1.array_length_hierarchy) {
947 total_array_length *= length;
948 }
949 data.size = local_offset + data1.size * total_array_length;
950}
951
952static uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
953 int array_indices_size = static_cast<int>(array_indices.size());
954 if (array_indices_size) {
955 uint32_t array_index = 0;
956 uint32_t i = 0;
957 for (const auto index : array_indices) {
958 array_index += (data.array_block_size[i] * index);
959 ++i;
960 }
961 offset += (array_index * data.size);
962 }
963 return offset;
964}
965
966static void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
967 int array_indices_size = static_cast<int>(array_indices.size());
968 uint32_t block_memory_size = data.size;
969 for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) {
970 block_memory_size *= data.array_length_hierarchy[i];
971 }
972
973 offset = UpdateOffset(offset, array_indices, data);
974
975 uint32_t end = offset + block_memory_size;
976 auto used_bytes = data.GetUsedbytes();
977 if (used_bytes->size() < end) {
978 used_bytes->resize(end, 0);
979 }
980 std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size));
981}
982
983void SHADER_MODULE_STATE::RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index,
984 spirv_inst_iter &access_chain_it, const shader_struct_member &data) const {
985 if (access_chain_word_index < access_chain_it.len()) {
986 if (data.array_length_hierarchy.size() > array_indices.size()) {
987 auto def_it = get_def(access_chain_it.word(access_chain_word_index));
988 ++access_chain_word_index;
989
990 if (def_it != end() && def_it.opcode() == spv::OpConstant) {
991 array_indices.emplace_back(def_it.word(3));
992 RunUsedArray(offset, array_indices, access_chain_word_index, access_chain_it, data);
993 } else {
994 // If it is a variable, set the all array is used.
995 if (access_chain_word_index < access_chain_it.len()) {
996 uint32_t array_length = data.array_length_hierarchy[array_indices.size()];
997 for (uint32_t i = 0; i < array_length; ++i) {
998 auto array_indices2 = array_indices;
999 array_indices2.emplace_back(i);
1000 RunUsedArray(offset, array_indices2, access_chain_word_index, access_chain_it, data);
1001 }
1002 } else {
1003 SetUsedBytes(offset, array_indices, data);
1004 }
1005 }
1006 } else {
1007 offset = UpdateOffset(offset, array_indices, data);
1008 RunUsedStruct(offset, access_chain_word_index, access_chain_it, data);
1009 }
1010 } else {
1011 SetUsedBytes(offset, array_indices, data);
1012 }
1013}
1014
1015void SHADER_MODULE_STATE::RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it,
1016 const shader_struct_member &data) const {
1017 std::vector<uint32_t> array_indices_emptry;
1018
1019 if (access_chain_word_index < access_chain_it.len()) {
1020 auto strcut_member_index = GetConstantValueById(access_chain_it.word(access_chain_word_index));
1021 ++access_chain_word_index;
1022
1023 auto data1 = data.struct_members[strcut_member_index];
1024 RunUsedArray(offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1);
1025 }
1026}
1027
1028void SHADER_MODULE_STATE::SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list,
1029 const shader_struct_member &data) const {
1030 for (const auto &func_set : function_set_list) {
1031 auto range = func_set.op_lists.equal_range(spv::OpAccessChain);
1032 for (auto it = range.first; it != range.second; ++it) {
1033 auto access_chain = at(it->second);
1034 if (access_chain.word(3) == variable_id) {
1035 RunUsedStruct(0, 4, access_chain, data);
1036 }
1037 }
1038 }
1039}
1040
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001041// static
1042void SHADER_MODULE_STATE::SetPushConstantUsedInShader(
sfricke-samsungef15e482022-01-26 11:32:49 -08001043 const SHADER_MODULE_STATE &module_state, std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001044 for (auto &entrypoint : entry_points) {
1045 auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable);
1046 for (auto it = range.first; it != range.second; ++it) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001047 const auto def_insn = module_state.at(it->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001048
1049 if (def_insn.word(3) == spv::StorageClassPushConstant) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001050 spirv_inst_iter type = module_state.get_def(def_insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -07001051 const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate);
1052 std::vector<uint32_t> offsets;
1053
1054 for (auto it2 = range2.first; it2 != range2.second; ++it2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001055 auto member_decorate = module_state.at(it2->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001056 if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) {
1057 offsets.emplace_back(member_decorate.offset());
1058 }
1059 }
1060 entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader;
sfricke-samsungef15e482022-01-26 11:32:49 -08001061 module_state.DefineStructMember(type, offsets, entrypoint.second.push_constant_used_in_shader);
1062 module_state.SetUsedStructMember(def_insn.word(2), entrypoint.second.function_set_list,
1063 entrypoint.second.push_constant_used_in_shader);
sfricke-samsung962cad92021-04-13 00:46:29 -07001064 }
1065 }
1066 }
1067}
1068
1069uint32_t SHADER_MODULE_STATE::DescriptorTypeToReqs(uint32_t type_id) const {
1070 auto type = get_def(type_id);
1071
1072 while (true) {
1073 switch (type.opcode()) {
1074 case spv::OpTypeArray:
1075 case spv::OpTypeRuntimeArray:
1076 case spv::OpTypeSampledImage:
1077 type = get_def(type.word(2));
1078 break;
1079 case spv::OpTypePointer:
1080 type = get_def(type.word(3));
1081 break;
1082 case spv::OpTypeImage: {
1083 auto dim = type.word(3);
1084 auto arrayed = type.word(5);
1085 auto msaa = type.word(6);
1086
1087 uint32_t bits = 0;
1088 switch (GetFundamentalType(type.word(2))) {
1089 case FORMAT_TYPE_FLOAT:
1090 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
1091 break;
1092 case FORMAT_TYPE_UINT:
1093 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
1094 break;
1095 case FORMAT_TYPE_SINT:
1096 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
1097 break;
1098 default:
1099 break;
1100 }
1101
1102 switch (dim) {
1103 case spv::Dim1D:
1104 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1105 return bits;
1106 case spv::Dim2D:
1107 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1108 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
1109 return bits;
1110 case spv::Dim3D:
1111 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
1112 return bits;
1113 case spv::DimCube:
1114 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1115 return bits;
1116 case spv::DimSubpassData:
1117 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1118 return bits;
1119 default: // buffer, etc.
1120 return bits;
1121 }
1122 }
1123 default:
1124 return 0;
1125 }
1126 }
1127}
1128
1129// For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to.
1130// This function examines instructions in the static call tree for a write to this variable.
1131bool SHADER_MODULE_STATE::IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const {
1132 auto type = builtin_instr.opcode();
1133 uint32_t target_id = builtin_instr.word(1);
1134 bool init_complete = false;
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001135 uint32_t target_member_offset = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001136
1137 if (type == spv::OpMemberDecorate) {
1138 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1139 auto insn = entrypoint;
1140 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1141 switch (insn.opcode()) {
1142 case spv::OpTypePointer:
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001143 if (insn.word(2) == spv::StorageClassOutput) {
1144 const auto type_id = insn.word(3);
1145 if (type_id == target_id) {
1146 target_id = insn.word(1);
1147 } else {
1148 // If the output is an array, check if the element type is what we're looking for
1149 const auto type_insn = get_def(type_id);
1150 if ((type_insn.opcode() == spv::OpTypeArray) && (type_insn.word(2) == target_id)) {
1151 target_id = insn.word(1);
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001152 target_member_offset = 1;
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001153 }
1154 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001155 }
1156 break;
1157 case spv::OpVariable:
1158 if (insn.word(1) == target_id) {
1159 target_id = insn.word(2);
1160 init_complete = true;
1161 }
1162 break;
1163 }
1164 insn++;
1165 }
1166 }
1167
1168 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1169
1170 bool found_write = false;
1171 layer_data::unordered_set<uint32_t> worklist;
1172 worklist.insert(entrypoint.word(2));
1173
1174 // Follow instructions in call graph looking for writes to target
1175 while (!worklist.empty() && !found_write) {
1176 auto id_iter = worklist.begin();
1177 auto id = *id_iter;
1178 worklist.erase(id_iter);
1179
1180 auto insn = get_def(id);
1181 if (insn == end()) {
1182 continue;
1183 }
1184
1185 if (insn.opcode() == spv::OpFunction) {
1186 // Scan body of function looking for other function calls or items in our ID chain
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001187 while (++insn, (insn.opcode() != spv::OpFunctionEnd) && !found_write) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001188 switch (insn.opcode()) {
1189 case spv::OpAccessChain:
1190 if (insn.word(3) == target_id) {
1191 if (type == spv::OpMemberDecorate) {
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001192 // Get the target member of the struct
1193 // NOTE: this will only work for structs and arrays of structs. Deeper levels of nesting (e.g.,
1194 // arrays of structs of structs) is not currently supported.
1195 const auto value_itr = GetConstantDef(insn.word(4 + target_member_offset));
1196 if (value_itr != end()) {
1197 auto value = GetConstantValue(value_itr);
1198 if (value == builtin_instr.word(2)) {
1199 target_id = insn.word(2);
1200 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001201 }
1202 } else {
1203 target_id = insn.word(2);
1204 }
1205 }
1206 break;
1207 case spv::OpStore:
1208 if (insn.word(1) == target_id) {
1209 found_write = true;
1210 }
1211 break;
1212 case spv::OpFunctionCall:
1213 worklist.insert(insn.word(3));
1214 break;
1215 }
1216 }
1217 }
1218 }
1219 return found_write;
1220}
1221
1222// Used by the collection functions to help aid in state tracking
1223struct shader_module_used_operators {
1224 bool updated;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001225 std::vector<uint32_t> image_read_members;
1226 std::vector<uint32_t> image_write_members;
1227 std::vector<uint32_t> atomic_members;
1228 std::vector<uint32_t> store_members;
1229 std::vector<uint32_t> atomic_store_members;
1230 std::vector<uint32_t> sampler_implicitLod_dref_proj_members; // sampler Load id
1231 std::vector<uint32_t> sampler_bias_offset_members; // sampler Load id
1232 std::vector<uint32_t> image_dref_members;
1233 std::vector<std::pair<uint32_t, uint32_t>> sampled_image_members; // <image,sampler> Load id
1234 layer_data::unordered_map<uint32_t, uint32_t> load_members;
1235 layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> accesschain_members;
1236 layer_data::unordered_map<uint32_t, uint32_t> image_texel_pointer_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001237
1238 shader_module_used_operators() : updated(false) {}
1239
1240 bool CheckImageOperandsBiasOffset(uint32_t type) {
1241 return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask |
1242 spv::ImageOperandsConstOffsetsMask)
1243 ? true
1244 : false;
1245 }
1246
sfricke-samsungef15e482022-01-26 11:32:49 -08001247 void update(SHADER_MODULE_STATE const *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001248 if (updated) return;
1249 updated = true;
1250
sfricke-samsungef15e482022-01-26 11:32:49 -08001251 for (auto insn : *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001252 switch (insn.opcode()) {
1253 case spv::OpImageSampleImplicitLod:
1254 case spv::OpImageSampleProjImplicitLod:
1255 case spv::OpImageSampleProjExplicitLod:
1256 case spv::OpImageSparseSampleImplicitLod:
1257 case spv::OpImageSparseSampleProjImplicitLod:
1258 case spv::OpImageSparseSampleProjExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001259 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001260 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001261 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1262 sampler_implicitLod_dref_proj_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001263 // ImageOperands in index: 5
1264 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001265 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001266 }
1267 break;
1268 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001269 case spv::OpImageDrefGather:
1270 case spv::OpImageSparseDrefGather: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001271 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001272 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001273 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1274 image_dref_members.emplace_back(load_id);
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001275 break;
1276 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001277 case spv::OpImageSampleDrefImplicitLod:
1278 case spv::OpImageSampleDrefExplicitLod:
1279 case spv::OpImageSampleProjDrefImplicitLod:
1280 case spv::OpImageSampleProjDrefExplicitLod:
1281 case spv::OpImageSparseSampleDrefImplicitLod:
1282 case spv::OpImageSparseSampleDrefExplicitLod:
1283 case spv::OpImageSparseSampleProjDrefImplicitLod:
1284 case spv::OpImageSparseSampleProjDrefExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001285 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001286 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001287 auto sampler_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1288 auto image_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1289
1290 image_dref_members.emplace_back(image_load_id);
1291 sampler_implicitLod_dref_proj_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001292 // ImageOperands in index: 6
1293 if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001294 sampler_bias_offset_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001295 }
1296 break;
1297 }
1298 case spv::OpImageSampleExplicitLod:
1299 case spv::OpImageSparseSampleExplicitLod: {
1300 // ImageOperands in index: 5
1301 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001302 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001303 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001304 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1305 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001306 }
1307 break;
1308 }
1309 case spv::OpStore: {
1310 store_members.emplace_back(insn.word(1)); // object id or AccessChain id
1311 break;
1312 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001313 case spv::OpImageRead:
1314 case spv::OpImageSparseRead: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001315 image_read_members.emplace_back(insn.word(3)); // Load id
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001316 break;
1317 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001318 case spv::OpImageWrite: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001319 image_write_members.emplace_back(insn.word(1)); // Load id
sfricke-samsung962cad92021-04-13 00:46:29 -07001320 break;
1321 }
1322 case spv::OpSampledImage: {
1323 // 3: image load id, 4: sampler load id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001324 sampled_image_members.emplace_back(std::pair<uint32_t, uint32_t>(insn.word(3), insn.word(4)));
sfricke-samsung962cad92021-04-13 00:46:29 -07001325 break;
1326 }
1327 case spv::OpLoad: {
1328 // 2: Load id, 3: object id or AccessChain id
1329 load_members.emplace(insn.word(2), insn.word(3));
1330 break;
1331 }
1332 case spv::OpAccessChain: {
1333 if (insn.len() == 4) {
1334 // If it is for struct, the length is only 4.
1335 // 2: AccessChain id, 3: object id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001336 accesschain_members.emplace(insn.word(2), std::pair<uint32_t, uint32_t>(insn.word(3), 0));
sfricke-samsung962cad92021-04-13 00:46:29 -07001337 } else {
1338 // 2: AccessChain id, 3: object id, 4: object id of array index
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001339 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 -07001340 }
1341 break;
1342 }
1343 case spv::OpImageTexelPointer: {
1344 // 2: ImageTexelPointer id, 3: object id
1345 image_texel_pointer_members.emplace(insn.word(2), insn.word(3));
1346 break;
1347 }
1348 default: {
1349 if (AtomicOperation(insn.opcode())) {
1350 if (insn.opcode() == spv::OpAtomicStore) {
1351 atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id
1352 } else {
1353 atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id
1354 }
1355 }
1356 break;
1357 }
1358 }
1359 }
1360 }
1361};
1362
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001363static bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<uint32_t> &operator_members,
1364 const layer_data::unordered_map<uint32_t, uint32_t> &load_members,
1365 const layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> &accesschain_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001366 for (auto load_id : operator_members) {
1367 if (object_id == load_id) return true;
1368 auto load_it = load_members.find(load_id);
1369 if (load_it == load_members.end()) {
1370 continue;
1371 }
1372 if (load_it->second == object_id) {
1373 return true;
1374 }
1375
1376 auto accesschain_it = accesschain_members.find(load_it->second);
1377 if (accesschain_it == accesschain_members.end()) {
1378 continue;
1379 }
1380 if (accesschain_it->second.first == object_id) {
1381 return true;
1382 }
1383 }
1384 return false;
1385}
1386
1387// Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image
1388// atomic operation, matching images to samplers, etc
1389void SHADER_MODULE_STATE::IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable,
1390 interface_var &out_interface_var,
1391 shader_module_used_operators &used_operators) const {
1392 uint32_t type_id = id_it.word(1);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001393 uint32_t id = id_it.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001394
1395 auto type = get_def(type_id);
1396
1397 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
1398 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray ||
1399 type.opcode() == spv::OpTypeSampledImage) {
1400 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray ||
1401 type.opcode() == spv::OpTypeSampledImage) {
1402 type = get_def(type.word(2)); // Element type
1403 } else {
1404 type = get_def(type.word(3)); // Pointer type
1405 }
1406 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001407
sfricke-samsung962cad92021-04-13 00:46:29 -07001408 switch (type.opcode()) {
1409 case spv::OpTypeImage: {
1410 auto dim = type.word(3);
1411 if (dim != spv::DimSubpassData) {
1412 used_operators.update(this);
1413
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001414 // Sampled == 2 indicates used without a sampler (a storage image)
1415 bool is_image_without_format = false;
1416 if (type.word(7) == 2) is_image_without_format = type.word(8) == spv::ImageFormatUnknown;
1417
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001418 if (CheckObjectIDFromOpLoad(id, used_operators.image_write_members, used_operators.load_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001419 used_operators.accesschain_members)) {
1420 out_interface_var.is_writable = true;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001421 if (is_image_without_format) out_interface_var.is_write_without_format = true;
1422 }
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001423 if (CheckObjectIDFromOpLoad(id, used_operators.image_read_members, used_operators.load_members,
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001424 used_operators.accesschain_members)) {
1425 out_interface_var.is_readable = true;
1426 if (is_image_without_format) out_interface_var.is_read_without_format = true;
sfricke-samsung962cad92021-04-13 00:46:29 -07001427 }
1428 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members,
1429 used_operators.accesschain_members)) {
1430 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1431 }
1432 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members,
1433 used_operators.accesschain_members)) {
1434 out_interface_var.is_sampler_bias_offset = true;
1435 }
1436 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members,
1437 used_operators.accesschain_members) ||
1438 CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1439 used_operators.accesschain_members)) {
1440 out_interface_var.is_atomic_operation = true;
1441 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001442 if (CheckObjectIDFromOpLoad(id, used_operators.image_dref_members, used_operators.load_members,
1443 used_operators.accesschain_members)) {
1444 out_interface_var.is_dref_operation = true;
1445 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001446
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001447 for (auto &itp_id : used_operators.sampled_image_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001448 // Find if image id match.
1449 uint32_t image_index = 0;
1450 auto load_it = used_operators.load_members.find(itp_id.first);
1451 if (load_it == used_operators.load_members.end()) {
1452 continue;
1453 } else {
1454 if (load_it->second != id) {
1455 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1456 if (accesschain_it == used_operators.accesschain_members.end()) {
1457 continue;
1458 } else {
1459 if (accesschain_it->second.first != id) {
1460 continue;
1461 }
1462
1463 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1464 if (const_itr == end()) {
1465 // access chain index not a constant, skip.
1466 break;
1467 }
1468 image_index = GetConstantValue(const_itr);
1469 }
1470 }
1471 }
1472 // Find sampler's set binding.
1473 load_it = used_operators.load_members.find(itp_id.second);
1474 if (load_it == used_operators.load_members.end()) {
1475 continue;
1476 } else {
1477 uint32_t sampler_id = load_it->second;
1478 uint32_t sampler_index = 0;
1479 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1480
1481 if (accesschain_it != used_operators.accesschain_members.end()) {
1482 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1483 if (const_itr == end()) {
1484 // access chain index representing sampler index is not a constant, skip.
1485 break;
1486 }
1487 sampler_id = const_itr.offset();
1488 sampler_index = GetConstantValue(const_itr);
1489 }
1490 auto sampler_dec = get_decorations(sampler_id);
1491 if (image_index >= out_interface_var.samplers_used_by_image.size()) {
1492 out_interface_var.samplers_used_by_image.resize(image_index + 1);
1493 }
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001494
1495 // Need to check again for these properties in case not using a combined image sampler
1496 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_implicitLod_dref_proj_members,
1497 used_operators.load_members, used_operators.accesschain_members)) {
1498 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1499 }
1500 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_bias_offset_members,
1501 used_operators.load_members, used_operators.accesschain_members)) {
1502 out_interface_var.is_sampler_bias_offset = true;
1503 }
1504
sfricke-samsung962cad92021-04-13 00:46:29 -07001505 out_interface_var.samplers_used_by_image[image_index].emplace(
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001506 SamplerUsedByImage{DescriptorSlot{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index});
sfricke-samsung962cad92021-04-13 00:46:29 -07001507 }
1508 }
1509 }
1510 return;
1511 }
1512
1513 case spv::OpTypeStruct: {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001514 layer_data::unordered_set<uint32_t> nonwritable_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001515 if (get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001516 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001517 if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) {
1518 nonwritable_members.insert(insn.word(2));
1519 }
1520 }
1521
1522 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
1523 // as nonwritable.
1524 if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) {
1525 used_operators.update(this);
1526
1527 for (auto oid : used_operators.store_members) {
1528 if (id == oid) {
1529 out_interface_var.is_writable = true;
1530 return;
1531 }
1532 auto accesschain_it = used_operators.accesschain_members.find(oid);
1533 if (accesschain_it == used_operators.accesschain_members.end()) {
1534 continue;
1535 }
1536 if (accesschain_it->second.first == id) {
1537 out_interface_var.is_writable = true;
1538 return;
1539 }
1540 }
1541 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1542 used_operators.accesschain_members)) {
1543 out_interface_var.is_writable = true;
1544 return;
1545 }
1546 }
1547 }
1548 }
1549}
1550
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001551std::vector<std::pair<DescriptorSlot, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByDescriptorSlot(
ziga-lunargc2de4782022-04-14 19:49:07 +02001552 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001553 std::vector<std::pair<DescriptorSlot, interface_var>> out;
sfricke-samsung962cad92021-04-13 00:46:29 -07001554 shader_module_used_operators operators;
1555
ziga-lunargc2de4782022-04-14 19:49:07 +02001556 for (auto id : accessible_ids) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001557 auto insn = get_def(id);
1558 assert(insn != end());
1559
1560 if (insn.opcode() == spv::OpVariable &&
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001561 (insn.word(3) == spv::StorageClassUniform ||
1562 insn.word(3) == spv::StorageClassUniformConstant ||
sfricke-samsung962cad92021-04-13 00:46:29 -07001563 insn.word(3) == spv::StorageClassStorageBuffer)) {
1564 auto d = get_decorations(insn.word(2));
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001565 uint32_t set = d.descriptor_set;
1566 uint32_t binding = d.binding;
sfricke-samsung962cad92021-04-13 00:46:29 -07001567
1568 interface_var v = {};
1569 v.id = insn.word(2);
1570 v.type_id = insn.word(1);
1571
1572 IsSpecificDescriptorType(insn, insn.word(3) == spv::StorageClassStorageBuffer,
1573 !(d.flags & decoration_set::nonwritable_bit), v, operators);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001574 out.emplace_back(DescriptorSlot{set, binding}, v);
sfricke-samsung962cad92021-04-13 00:46:29 -07001575 }
1576 }
1577
1578 return out;
1579}
1580
1581layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::CollectWritableOutputLocationinFS(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001582 const spirv_inst_iter &entrypoint) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001583 layer_data::unordered_set<uint32_t> location_list;
sfricke-samsung962cad92021-04-13 00:46:29 -07001584 const auto outputs = CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001585 layer_data::unordered_set<uint32_t> store_members;
1586 layer_data::unordered_map<uint32_t, uint32_t> accesschain_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001587
1588 for (auto insn : *this) {
1589 switch (insn.opcode()) {
1590 case spv::OpStore:
1591 case spv::OpAtomicStore: {
1592 store_members.insert(insn.word(1)); // object id or AccessChain id
1593 break;
1594 }
1595 case spv::OpAccessChain: {
1596 // 2: AccessChain id, 3: object id
1597 if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3));
1598 break;
1599 }
1600 default:
1601 break;
1602 }
1603 }
1604 if (store_members.empty()) {
1605 return location_list;
1606 }
1607 for (auto output : outputs) {
1608 auto store_it = store_members.find(output.second.id);
1609 if (store_it != store_members.end()) {
1610 location_list.insert(output.first.first);
1611 store_members.erase(store_it);
1612 continue;
1613 }
1614 store_it = store_members.begin();
1615 while (store_it != store_members.end()) {
1616 auto accesschain_it = accesschain_members.find(*store_it);
1617 if (accesschain_it == accesschain_members.end()) {
1618 ++store_it;
1619 continue;
1620 }
1621 if (accesschain_it->second == output.second.id) {
1622 location_list.insert(output.first.first);
1623 store_members.erase(store_it);
1624 accesschain_members.erase(accesschain_it);
1625 break;
1626 }
1627 ++store_it;
1628 }
1629 }
1630 return location_list;
1631}
1632
1633bool SHADER_MODULE_STATE::CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts,
ziga-lunarg9e94e112021-09-27 00:21:10 +02001634 uint32_t id, uint32_t type_id, bool is_patch,
1635 uint32_t /*first_location*/) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001636 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
1637 auto type = GetStructType(get_def(type_id), is_array_of_verts && !is_patch);
1638 if (type == end() || !(get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
1639 // This isn't an interface block.
1640 return false;
1641 }
1642
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001643 layer_data::unordered_map<uint32_t, uint32_t> member_components;
1644 layer_data::unordered_map<uint32_t, uint32_t> member_relaxed_precision;
1645 layer_data::unordered_map<uint32_t, uint32_t> member_patch;
sfricke-samsung962cad92021-04-13 00:46:29 -07001646
1647 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001648 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001649 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001650 uint32_t member_index = insn.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001651
1652 if (insn.word(3) == spv::DecorationComponent) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001653 uint32_t component = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001654 member_components[member_index] = component;
1655 }
1656
1657 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
1658 member_relaxed_precision[member_index] = 1;
1659 }
1660
1661 if (insn.word(3) == spv::DecorationPatch) {
1662 member_patch[member_index] = 1;
1663 }
1664 }
1665 }
1666
1667 // TODO: correctly handle location assignment from outside
1668
1669 // Second pass -- produce the output, from Location decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001670 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001671 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001672 uint32_t member_index = insn.word(2);
1673 uint32_t member_type_id = type.word(2 + member_index);
sfricke-samsung962cad92021-04-13 00:46:29 -07001674
1675 if (insn.word(3) == spv::DecorationLocation) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001676 uint32_t location = insn.word(4);
1677 uint32_t num_locations = GetLocationsConsumedByType(member_type_id, false);
sfricke-samsung962cad92021-04-13 00:46:29 -07001678 auto component_it = member_components.find(member_index);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001679 uint32_t component = component_it == member_components.end() ? 0 : component_it->second;
sfricke-samsung962cad92021-04-13 00:46:29 -07001680 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
1681 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
1682
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001683 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001684 interface_var v = {};
1685 v.id = id;
1686 // TODO: member index in interface_var too?
1687 v.type_id = member_type_id;
1688 v.offset = offset;
1689 v.is_patch = member_is_patch;
1690 v.is_block_member = true;
1691 v.is_relaxed_precision = is_relaxed_precision;
1692 (*out)[std::make_pair(location + offset, component)] = v;
1693 }
1694 }
1695 }
1696 }
1697
1698 return true;
1699}
1700
1701std::map<location_t, interface_var> SHADER_MODULE_STATE::CollectInterfaceByLocation(spirv_inst_iter entrypoint,
1702 spv::StorageClass sinterface,
1703 bool is_array_of_verts) const {
1704 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
1705
1706 std::map<location_t, interface_var> out;
1707
1708 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
1709 auto insn = get_def(iid);
1710 assert(insn != end());
1711 assert(insn.opcode() == spv::OpVariable);
1712
ziga-lunarg9e94e112021-09-27 00:21:10 +02001713 const auto d = get_decorations(iid);
1714 bool passthrough = sinterface == spv::StorageClassOutput && insn.word(3) == spv::StorageClassInput &&
1715 (d.flags & decoration_set::passthrough_bit) != 0;
1716 if (insn.word(3) == static_cast<uint32_t>(sinterface) || passthrough) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001717 uint32_t id = insn.word(2);
1718 uint32_t type = insn.word(1);
sfricke-samsung962cad92021-04-13 00:46:29 -07001719
ziga-lunarg9e94e112021-09-27 00:21:10 +02001720 auto location = d.location;
sfricke-samsung962cad92021-04-13 00:46:29 -07001721 int builtin = d.builtin;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001722 uint32_t component = d.component;
sfricke-samsung962cad92021-04-13 00:46:29 -07001723 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
1724 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001725 bool is_per_vertex = (d.flags & decoration_set::per_vertex_bit) != 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001726
1727 if (builtin != -1) {
1728 continue;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001729 } else if (!CollectInterfaceBlockMembers(&out, is_array_of_verts, id, type, is_patch, location) ||
1730 location != decoration_set::kInvalidValue) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001731 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
1732 // one result for each.
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001733 uint32_t num_locations = GetLocationsConsumedByType(type, (is_array_of_verts && !is_patch) || is_per_vertex);
1734 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001735 interface_var v = {};
1736 v.id = id;
1737 v.type_id = type;
1738 v.offset = offset;
1739 v.is_patch = is_patch;
1740 v.is_relaxed_precision = is_relaxed_precision;
1741 out[std::make_pair(location + offset, component)] = v;
1742 }
1743 }
1744 }
1745 }
1746
1747 return out;
1748}
1749
1750std::vector<uint32_t> SHADER_MODULE_STATE::CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001751 // Find all interface variables belonging to the entrypoint and matching the storage class
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001752 std::vector<uint32_t> variables;
sfricke-samsung962cad92021-04-13 00:46:29 -07001753 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
1754 auto def = get_def(id);
1755 assert(def != end());
1756 assert(def.opcode() == spv::OpVariable);
1757
1758 if (def.word(3) == storageClass) variables.push_back(def.word(1));
1759 }
1760
1761 // Find all members belonging to the builtin block selected
1762 std::vector<uint32_t> builtin_block_members;
1763 for (auto &var : variables) {
1764 auto def = get_def(get_def(var).word(3));
1765
1766 // It could be an array of IO blocks. The element type should be the struct defining the block contents
1767 if (def.opcode() == spv::OpTypeArray) def = get_def(def.word(2));
1768
1769 // Now find all members belonging to the struct defining the IO block
1770 if (def.opcode() == spv::OpTypeStruct) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001771 for (auto set : static_data_.builtin_decoration_list) {
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001772 auto insn = at(set.offset);
1773 if ((insn.opcode() == spv::OpMemberDecorate) && (def.word(1) == insn.word(1))) {
1774 // Start with undefined builtin for each struct member.
1775 // But only when confirmed the struct is the built-in inteface block (can only be one per shader)
1776 if (builtin_block_members.size() == 0) {
1777 builtin_block_members.resize(def.len() - 2, spv::BuiltInMax);
sfricke-samsung962cad92021-04-13 00:46:29 -07001778 }
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001779 auto struct_index = insn.word(2);
1780 assert(struct_index < builtin_block_members.size());
1781 builtin_block_members[struct_index] = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001782 }
1783 }
1784 }
1785 }
1786
1787 return builtin_block_members;
1788}
1789
1790std::vector<std::pair<uint32_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByInputAttachmentIndex(
1791 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
1792 std::vector<std::pair<uint32_t, interface_var>> out;
1793
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001794 for (auto insn : static_data_.decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001795 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
1796 auto attachment_index = insn.word(3);
1797 auto id = insn.word(1);
1798
1799 if (accessible_ids.count(id)) {
1800 auto def = get_def(id);
1801 assert(def != end());
1802 if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) {
1803 auto num_locations = GetLocationsConsumedByType(def.word(1), false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001804 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001805 interface_var v = {};
1806 v.id = id;
1807 v.type_id = def.word(1);
1808 v.offset = offset;
1809 out.emplace_back(attachment_index + offset, v);
1810 }
1811 }
1812 }
1813 }
1814 }
1815
1816 return out;
1817}
1818
ziga-lunarg8346fe82021-08-22 17:30:50 +02001819uint32_t SHADER_MODULE_STATE::GetNumComponentsInBaseType(const spirv_inst_iter &iter) const {
1820 const uint32_t opcode = iter.opcode();
1821 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1822 return 1;
1823 } else if (opcode == spv::OpTypeVector) {
1824 const uint32_t component_count = iter.word(3);
1825 return component_count;
1826 } else if (opcode == spv::OpTypeMatrix) {
1827 const auto column_type = get_def(iter.word(2));
1828 const uint32_t vector_length = GetNumComponentsInBaseType(column_type);
ziga-lunarg38e44982022-04-05 00:10:46 +02001829 // Because we are calculating components for a single location we do not care about column count
1830 return vector_length;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001831 } else if (opcode == spv::OpTypeArray) {
1832 const auto element_type = get_def(iter.word(2));
1833 const uint32_t element_length = GetNumComponentsInBaseType(element_type);
1834 return element_length;
1835 } else if (opcode == spv::OpTypeStruct) {
1836 uint32_t total_size = 0;
1837 for (uint32_t i = 2; i < iter.len(); ++i) {
1838 total_size += GetNumComponentsInBaseType(get_def(iter.word(i)));
1839 }
1840 return total_size;
1841 } else if (opcode == spv::OpTypePointer) {
1842 const auto type = get_def(iter.word(3));
1843 return GetNumComponentsInBaseType(type);
1844 }
1845 return 0;
1846}
1847
ziga-lunarga26b3602021-08-08 15:53:00 +02001848uint32_t SHADER_MODULE_STATE::GetTypeBitsSize(const spirv_inst_iter &iter) const {
1849 const uint32_t opcode = iter.opcode();
1850 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1851 return iter.word(2);
1852 } else if (opcode == spv::OpTypeVector) {
1853 const auto component_type = get_def(iter.word(2));
1854 uint32_t scalar_width = GetTypeBitsSize(component_type);
1855 uint32_t component_count = iter.word(3);
1856 return scalar_width * component_count;
1857 } else if (opcode == spv::OpTypeMatrix) {
1858 const auto column_type = get_def(iter.word(2));
1859 uint32_t vector_width = GetTypeBitsSize(column_type);
1860 uint32_t column_count = iter.word(3);
1861 return vector_width * column_count;
1862 } else if (opcode == spv::OpTypeArray) {
1863 const auto element_type = get_def(iter.word(2));
1864 uint32_t element_width = GetTypeBitsSize(element_type);
1865 const auto length_type = get_def(iter.word(3));
1866 uint32_t length = GetConstantValue(length_type);
1867 return element_width * length;
1868 } else if (opcode == spv::OpTypeStruct) {
1869 uint32_t total_size = 0;
1870 for (uint32_t i = 2; i < iter.len(); ++i) {
1871 total_size += GetTypeBitsSize(get_def(iter.word(i)));
1872 }
1873 return total_size;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001874 } else if (opcode == spv::OpTypePointer) {
1875 const auto type = get_def(iter.word(3));
1876 return GetTypeBitsSize(type);
ziga-lunargef2c3172021-11-07 10:35:29 +01001877 } else if (opcode == spv::OpVariable) {
1878 const auto type = get_def(iter.word(1));
1879 return GetTypeBitsSize(type);
ziga-lunarga26b3602021-08-08 15:53:00 +02001880 }
1881 return 0;
1882}
1883
1884uint32_t SHADER_MODULE_STATE::GetTypeBytesSize(const spirv_inst_iter &iter) const { return GetTypeBitsSize(iter) / 8; }
1885
ziga-lunarg19fc6ae2021-09-09 00:05:19 +02001886// 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 +02001887uint32_t SHADER_MODULE_STATE::GetBaseType(const spirv_inst_iter &iter) const {
1888 const uint32_t opcode = iter.opcode();
1889 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt || opcode == spv::OpTypeStruct) {
1890 return iter.word(1);
1891 } else if (opcode == spv::OpTypeVector) {
1892 const auto& component_type = get_def(iter.word(2));
1893 return GetBaseType(component_type);
1894 } else if (opcode == spv::OpTypeMatrix) {
1895 const auto& column_type = get_def(iter.word(2));
1896 return GetBaseType(column_type);
1897 } else if (opcode == spv::OpTypeArray) {
1898 const auto& element_type = get_def(iter.word(2));
1899 return GetBaseType(element_type);
1900 } else if (opcode == spv::OpTypePointer) {
1901 const auto& type = get_def(iter.word(3));
1902 return GetBaseType(type);
1903 }
1904 return 0;
1905}
1906
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001907// Returns type_id if id has type or zero otherwise
1908uint32_t SHADER_MODULE_STATE::GetTypeId(uint32_t id) const {
1909 const auto type = get_def(id);
1910 return OpcodeHasType(type.opcode()) ? type.word(1) : 0;
1911}
1912
sfricke-samsung962cad92021-04-13 00:46:29 -07001913std::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}