blob: aa3475b682c083d6996129c4ebde10a08cd83c8b [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
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600370 // Execution Mode
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800371 case spv::OpExecutionMode:
372 case spv::OpExecutionModeId: {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600373 execution_mode_inst[insn.word(1)].push_back(insn);
374 } break;
ziga-lunarge25f5f02022-04-16 15:07:35 +0200375 // Once https://github.com/KhronosGroup/SPIRV-Headers/issues/276 is added this should be derived from SPIR-V grammar
376 // json
377 case spv::OpTraceRayKHR:
378 case spv::OpTraceRayMotionNV:
379 case spv::OpReportIntersectionKHR:
380 case spv::OpExecuteCallableKHR:
381 has_invocation_repack_instruction = true;
382 break;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600383
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600384 default:
385 if (AtomicOperation(insn.opcode()) == true) {
386 // All atomics have a pointer referenced
387 spirv_inst_iter access;
388 if (insn.opcode() == spv::OpAtomicStore) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800389 access = module_state.get_def(insn.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600390 } else {
sfricke-samsungef15e482022-01-26 11:32:49 -0800391 access = module_state.get_def(insn.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600392 }
393
394 atomic_instruction atomic;
395
sfricke-samsungef15e482022-01-26 11:32:49 -0800396 auto pointer = module_state.get_def(access.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600397 // spirv-val should catch if not pointer
398 assert(pointer.opcode() == spv::OpTypePointer);
399 atomic.storage_class = pointer.word(2);
400
sfricke-samsungef15e482022-01-26 11:32:49 -0800401 auto data_type = module_state.get_def(pointer.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600402 atomic.type = data_type.opcode();
403
404 // TODO - Should have a proper GetBitWidth like spirv-val does
405 assert(data_type.opcode() == spv::OpTypeFloat || data_type.opcode() == spv::OpTypeInt);
406 atomic.bit_width = data_type.word(2);
407
408 atomic_inst[insn.offset()] = atomic;
409 }
410 // We don't care about any other defs for now.
411 break;
412 }
413 }
414
sfricke-samsungef15e482022-01-26 11:32:49 -0800415 entry_points = SHADER_MODULE_STATE::ProcessEntryPoints(module_state);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600416 multiple_entry_points = entry_points.size() > 1;
417}
418
419// static
420std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> SHADER_MODULE_STATE::ProcessEntryPoints(
sfricke-samsungef15e482022-01-26 11:32:49 -0800421 const SHADER_MODULE_STATE &module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600422 std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> entry_points;
423 function_set func_set = {};
424 EntryPoint *entry_point = nullptr;
425
sfricke-samsungef15e482022-01-26 11:32:49 -0800426 for (auto insn : module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600427 // offset is not 0, it means it's updated and the offset is in a Function.
428 if (func_set.offset) {
429 func_set.op_lists.emplace(insn.opcode(), insn.offset());
430 } else if (entry_point) {
431 entry_point->decorate_list.emplace(insn.opcode(), insn.offset());
432 }
433
434 switch (insn.opcode()) {
435 // Functions
436 case spv::OpFunction:
437 func_set.id = insn.word(2);
438 func_set.offset = insn.offset();
439 func_set.op_lists.clear();
440 break;
441
442 // Entry points ... add to the entrypoint table
443 case spv::OpEntryPoint: {
sfricke-samsung962cad92021-04-13 00:46:29 -0700444 // Entry points do not have an id (the id is the function id) and thus need their own table
445 auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3));
446 auto execution_model = insn.word(1);
447 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
448 entry_points.emplace(entrypoint_name,
449 EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)});
450
451 auto range = entry_points.equal_range(entrypoint_name);
452 for (auto it = range.first; it != range.second; ++it) {
453 if (it->second.offset == insn.offset()) {
454 entry_point = &(it->second);
455 break;
456 }
457 }
458 assert(entry_point != nullptr);
459 break;
460 }
461 case spv::OpFunctionEnd: {
462 assert(entry_point != nullptr);
463 func_set.length = insn.offset() - func_set.offset;
464 entry_point->function_set_list.emplace_back(func_set);
465 break;
466 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700467 }
468 }
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600469
sfricke-samsungef15e482022-01-26 11:32:49 -0800470 SHADER_MODULE_STATE::SetPushConstantUsedInShader(module_state, entry_points);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600471 return entry_points;
sfricke-samsung962cad92021-04-13 00:46:29 -0700472}
473
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600474void SHADER_MODULE_STATE::PreprocessShaderBinary(const spv_target_env env) {
475 if (static_data_.has_group_decoration) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700476 spvtools::Optimizer optimizer(env);
477 optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass());
478 std::vector<uint32_t> optimized_binary;
479 // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600480 auto result = optimizer.Run(words.data(), words.size(), &optimized_binary, spvtools::ValidatorOptions(), true);
481
sfricke-samsung962cad92021-04-13 00:46:29 -0700482 if (result) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600483 // NOTE: We need to update words with the result from the spirv-tools optimizer.
484 // **THIS ONLY HAPPENS ON INITIALIZATION**. words should remain const for the lifetime
485 // of the SHADER_MODULE_STATE instance.
486 *const_cast<std::vector<uint32_t> *>(&words) = std::move(optimized_binary);
sfricke-samsung962cad92021-04-13 00:46:29 -0700487 }
488 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700489}
490
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800491char const *StorageClassName(uint32_t sc) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700492 switch (sc) {
493 case spv::StorageClassInput:
494 return "input";
495 case spv::StorageClassOutput:
496 return "output";
497 case spv::StorageClassUniformConstant:
498 return "const uniform";
499 case spv::StorageClassUniform:
500 return "uniform";
501 case spv::StorageClassWorkgroup:
502 return "workgroup local";
503 case spv::StorageClassCrossWorkgroup:
504 return "workgroup global";
505 case spv::StorageClassPrivate:
506 return "private global";
507 case spv::StorageClassFunction:
508 return "function";
509 case spv::StorageClassGeneric:
510 return "generic";
511 case spv::StorageClassAtomicCounter:
512 return "atomic counter";
513 case spv::StorageClassImage:
514 return "image";
515 case spv::StorageClassPushConstant:
516 return "push constant";
517 case spv::StorageClassStorageBuffer:
518 return "storage buffer";
519 default:
520 return "unknown";
521 }
522}
523
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800524void SHADER_MODULE_STATE::DescribeTypeInner(std::ostringstream &ss, uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700525 auto insn = get_def(type);
526 assert(insn != end());
527
528 switch (insn.opcode()) {
529 case spv::OpTypeBool:
530 ss << "bool";
531 break;
532 case spv::OpTypeInt:
533 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
534 break;
535 case spv::OpTypeFloat:
536 ss << "float" << insn.word(2);
537 break;
538 case spv::OpTypeVector:
539 ss << "vec" << insn.word(3) << " of ";
540 DescribeTypeInner(ss, insn.word(2));
541 break;
542 case spv::OpTypeMatrix:
543 ss << "mat" << insn.word(3) << " of ";
544 DescribeTypeInner(ss, insn.word(2));
545 break;
546 case spv::OpTypeArray:
547 ss << "arr[" << GetConstantValueById(insn.word(3)) << "] of ";
548 DescribeTypeInner(ss, insn.word(2));
549 break;
550 case spv::OpTypeRuntimeArray:
551 ss << "runtime arr[] of ";
552 DescribeTypeInner(ss, insn.word(2));
553 break;
554 case spv::OpTypePointer:
555 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
556 DescribeTypeInner(ss, insn.word(3));
557 break;
558 case spv::OpTypeStruct: {
559 ss << "struct of (";
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800560 for (uint32_t i = 2; i < insn.len(); i++) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700561 DescribeTypeInner(ss, insn.word(i));
562 if (i == insn.len() - 1) {
563 ss << ")";
564 } else {
565 ss << ", ";
566 }
567 }
568 break;
569 }
570 case spv::OpTypeSampler:
571 ss << "sampler";
572 break;
573 case spv::OpTypeSampledImage:
574 ss << "sampler+";
575 DescribeTypeInner(ss, insn.word(2));
576 break;
577 case spv::OpTypeImage:
578 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
579 break;
580 case spv::OpTypeAccelerationStructureNV:
581 ss << "accelerationStruture";
582 break;
583 default:
584 ss << "oddtype";
585 break;
586 }
587}
588
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800589std::string SHADER_MODULE_STATE::DescribeType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700590 std::ostringstream ss;
591 DescribeTypeInner(ss, type);
592 return ss.str();
593}
594
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800595std::string SHADER_MODULE_STATE::DescribeInstruction(const spirv_inst_iter &insn) const {
596 std::ostringstream ss;
597 const uint32_t opcode = insn.opcode();
598 uint32_t operand_offset = 1; // where to start printing operands
599 // common disassembled for SPIR-V is
600 // %result = Opcode %result_type %operands
601 if (OpcodeHasResult(opcode)) {
602 operand_offset++;
603 ss << "%" << (OpcodeHasType(opcode) ? insn.word(2) : insn.word(1)) << " = ";
604 }
605
606 ss << string_SpvOpcode(opcode);
607
608 if (OpcodeHasType(opcode)) {
609 operand_offset++;
610 ss << " %" << insn.word(1);
611 }
612
sfricke-samsunged00aa42022-01-27 19:03:01 -0800613 // TODO - For now don't list the '%' for any operands since they are only for reference IDs. Without generating a table of each
614 // instructions operand types and covering the many edge cases (such as optional, paired, or variable operands) this is the
615 // simplest way to print the instruction and give the developer something to look into when an error occurs.
616 //
617 // For now this safely should be able to assume it will never come across a LiteralString such as in OpExtInstImport or
618 // OpEntryPoint
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800619 for (uint32_t i = operand_offset; i < insn.len(); i++) {
sfricke-samsunged00aa42022-01-27 19:03:01 -0800620 ss << " " << insn.word(i);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800621 }
622 return ss.str();
623}
624
sfricke-samsung962cad92021-04-13 00:46:29 -0700625const SHADER_MODULE_STATE::EntryPoint *SHADER_MODULE_STATE::FindEntrypointStruct(char const *name,
626 VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600627 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700628 for (auto it = range.first; it != range.second; ++it) {
629 if (it->second.stage == stageBits) {
630 return &(it->second);
631 }
632 }
633 return nullptr;
634}
635
636spirv_inst_iter SHADER_MODULE_STATE::FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600637 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700638 for (auto it = range.first; it != range.second; ++it) {
639 if (it->second.stage == stageBits) {
640 return at(it->second.offset);
641 }
642 }
643 return end();
644}
645
646// Because the following is legal, need the entry point
647// OpEntryPoint GLCompute %main "name_a"
648// OpEntryPoint GLCompute %main "name_b"
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800649// Assumes shader module contains no spec constants used to set the local size values
sfricke-samsung962cad92021-04-13 00:46:29 -0700650bool SHADER_MODULE_STATE::FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y,
651 uint32_t &local_size_z) const {
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800652 // "If an object is decorated with the WorkgroupSize decoration, this takes precedence over any LocalSize or LocalSizeId
653 // execution mode."
654 for (const auto &builtin : static_data_.builtin_decoration_list) {
655 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
656 const uint32_t workgroup_size_id = at(builtin.offset).word(1);
657 auto composite_def = get_def(workgroup_size_id);
658 if (composite_def.opcode() == spv::OpConstantComposite) {
659 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32
660 local_size_x = GetConstantValue(get_def(composite_def.word(3)));
661 local_size_y = GetConstantValue(get_def(composite_def.word(4)));
662 local_size_z = GetConstantValue(get_def(composite_def.word(5)));
sfricke-samsung962cad92021-04-13 00:46:29 -0700663 return true;
664 }
665 }
666 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800667
668 auto entrypoint_id = entrypoint.word(2);
669 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
670 if (it != static_data_.execution_mode_inst.end()) {
671 for (auto insn : it->second) {
672 if (insn.opcode() == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeLocalSize) {
673 local_size_x = insn.word(3);
674 local_size_y = insn.word(4);
675 local_size_z = insn.word(5);
676 return true;
677 } else if (insn.opcode() == spv::OpExecutionModeId && insn.word(2) == spv::ExecutionModeLocalSizeId) {
678 local_size_x = GetConstantValueById(insn.word(3));
679 local_size_y = GetConstantValueById(insn.word(4));
680 local_size_z = GetConstantValueById(insn.word(5));
681 return true;
682 }
683 }
684 }
685 return false; // not found
sfricke-samsung962cad92021-04-13 00:46:29 -0700686}
687
688// If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction.
689// Otherwise, returns src->end().
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800690spirv_inst_iter SHADER_MODULE_STATE::GetConstantDef(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700691 auto value = get_def(id);
692
693 // If id is a copy, see where it was copied from
694 if ((end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) {
695 id = value.word(3);
696 value = get_def(id);
697 }
698
699 if ((end() != value) && (value.opcode() == spv::OpConstant)) {
700 return value;
701 }
702 return end();
703}
704
705// Either returns the constant value described by the instruction at id, or 1
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800706uint32_t SHADER_MODULE_STATE::GetConstantValueById(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700707 auto value = GetConstantDef(id);
708
709 if (end() == value) {
710 // TODO: Either ensure that the specialization transform is already performed on a module we're
711 // considering here, OR -- specialize on the fly now.
712 return 1;
713 }
714 return GetConstantValue(value);
715}
716
717// Returns an int32_t corresponding to the spv::Dim of the given resource, when positive, and corresponding to an unknown type, when
718// negative.
719int32_t SHADER_MODULE_STATE::GetShaderResourceDimensionality(const interface_var &resource) const {
720 auto type = get_def(resource.type_id);
721 while (true) {
722 switch (type.opcode()) {
723 case spv::OpTypeSampledImage:
724 type = get_def(type.word(2));
725 break;
726 case spv::OpTypePointer:
727 type = get_def(type.word(3));
728 break;
729 case spv::OpTypeImage:
730 return type.word(3);
731 default:
732 return -1;
733 }
734 }
735}
736
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800737uint32_t SHADER_MODULE_STATE::GetLocationsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700738 auto insn = get_def(type);
739 assert(insn != end());
740
741 switch (insn.opcode()) {
742 case spv::OpTypePointer:
743 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
744 // pointers around.
745 return GetLocationsConsumedByType(insn.word(3), strip_array_level);
746 case spv::OpTypeArray:
747 if (strip_array_level) {
748 return GetLocationsConsumedByType(insn.word(2), false);
749 } else {
750 return GetConstantValueById(insn.word(3)) * GetLocationsConsumedByType(insn.word(2), false);
751 }
752 case spv::OpTypeMatrix:
753 // Num locations is the dimension * element size
754 return insn.word(3) * GetLocationsConsumedByType(insn.word(2), false);
755 case spv::OpTypeVector: {
756 auto scalar_type = get_def(insn.word(2));
757 auto bit_width =
758 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
759
760 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
761 return (bit_width * insn.word(3) + 127) / 128;
762 }
763 default:
764 // Everything else is just 1.
765 return 1;
766
767 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
768 }
769}
770
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800771uint32_t SHADER_MODULE_STATE::GetComponentsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700772 auto insn = get_def(type);
773 assert(insn != end());
774
775 switch (insn.opcode()) {
776 case spv::OpTypePointer:
777 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
778 // pointers around.
779 return GetComponentsConsumedByType(insn.word(3), strip_array_level);
780 case spv::OpTypeStruct: {
781 uint32_t sum = 0;
782 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
783 sum += GetComponentsConsumedByType(insn.word(i), false);
784 }
785 return sum;
786 }
787 case spv::OpTypeArray:
788 if (strip_array_level) {
789 return GetComponentsConsumedByType(insn.word(2), false);
790 } else {
791 return GetConstantValueById(insn.word(3)) * GetComponentsConsumedByType(insn.word(2), false);
792 }
793 case spv::OpTypeMatrix:
794 // Num locations is the dimension * element size
795 return insn.word(3) * GetComponentsConsumedByType(insn.word(2), false);
796 case spv::OpTypeVector: {
797 auto scalar_type = get_def(insn.word(2));
798 auto bit_width =
799 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
800 // One component is 32-bit
801 return (bit_width * insn.word(3) + 31) / 32;
802 }
803 case spv::OpTypeFloat: {
804 auto bit_width = insn.word(2);
805 return (bit_width + 31) / 32;
806 }
807 case spv::OpTypeInt: {
808 auto bit_width = insn.word(2);
809 return (bit_width + 31) / 32;
810 }
811 case spv::OpConstant:
812 return GetComponentsConsumedByType(insn.word(1), false);
813 default:
814 return 0;
815 }
816}
817
818// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
819// also used for input attachments, as we statically know their format.
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800820uint32_t SHADER_MODULE_STATE::GetFundamentalType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700821 auto insn = get_def(type);
822 assert(insn != end());
823
824 switch (insn.opcode()) {
825 case spv::OpTypeInt:
826 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
827 case spv::OpTypeFloat:
828 return FORMAT_TYPE_FLOAT;
829 case spv::OpTypeVector:
830 case spv::OpTypeMatrix:
831 case spv::OpTypeArray:
832 case spv::OpTypeRuntimeArray:
833 case spv::OpTypeImage:
834 return GetFundamentalType(insn.word(2));
835 case spv::OpTypePointer:
836 return GetFundamentalType(insn.word(3));
837
838 default:
839 return 0;
840 }
841}
842
843spirv_inst_iter SHADER_MODULE_STATE::GetStructType(spirv_inst_iter def, bool is_array_of_verts) const {
844 while (true) {
845 if (def.opcode() == spv::OpTypePointer) {
846 def = get_def(def.word(3));
847 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
848 def = get_def(def.word(2));
849 is_array_of_verts = false;
850 } else if (def.opcode() == spv::OpTypeStruct) {
851 return def;
852 } else {
853 return end();
854 }
855 }
856}
857
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800858void SHADER_MODULE_STATE::DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &member_decorate_offsets,
sfricke-samsung962cad92021-04-13 00:46:29 -0700859 shader_struct_member &data) const {
860 const auto struct_it = GetStructType(it, false);
861 assert(struct_it != end());
862 data.size = 0;
863
864 shader_struct_member data1;
865 uint32_t i = 2;
866 uint32_t local_offset = 0;
867 std::vector<uint32_t> offsets;
868 offsets.resize(struct_it.len() - i);
869
870 // 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 -0800871 for (const auto offset : member_decorate_offsets) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700872 const auto member_decorate = at(offset);
873 if (member_decorate.word(1) != struct_it.word(1)) {
874 continue;
875 }
876
877 offsets[member_decorate.word(2)] = member_decorate.word(4);
878 }
879
880 for (const auto offset : offsets) {
881 local_offset = offset;
882 data1 = {};
883 data1.root = data.root;
884 data1.offset = local_offset;
885 auto def_member = get_def(struct_it.word(i));
886
887 // Array could be multi-dimensional
888 while (def_member.opcode() == spv::OpTypeArray) {
889 const auto len_id = def_member.word(3);
890 const auto def_len = get_def(len_id);
891 data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length
892 def_member = get_def(def_member.word(2));
893 }
894
895 if (def_member.opcode() == spv::OpTypeStruct) {
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800896 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700897 } else if (def_member.opcode() == spv::OpTypePointer) {
898 if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) {
899 // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address
900 // that "points to something."
901 data1.size = 8;
902 } else {
903 // 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 -0800904 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700905 }
906 } else {
907 if (def_member.opcode() == spv::OpTypeMatrix) {
908 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector.
909 def_member = get_def(def_member.word(2));
910 }
911
912 if (def_member.opcode() == spv::OpTypeVector) {
913 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length
914 def_member = get_def(def_member.word(2));
915 }
916
917 // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte.
918 data1.size = (def_member.word(2) / 8);
919 }
920 const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size();
921 if (array_length_hierarchy_szie > 0) {
922 data1.array_block_size.resize(array_length_hierarchy_szie, 1);
923
924 for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) {
925 data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2];
926 }
927 }
928 data.struct_members.emplace_back(data1);
929 ++i;
930 }
931 uint32_t total_array_length = 1;
932 for (const auto length : data1.array_length_hierarchy) {
933 total_array_length *= length;
934 }
935 data.size = local_offset + data1.size * total_array_length;
936}
937
938static uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
939 int array_indices_size = static_cast<int>(array_indices.size());
940 if (array_indices_size) {
941 uint32_t array_index = 0;
942 uint32_t i = 0;
943 for (const auto index : array_indices) {
944 array_index += (data.array_block_size[i] * index);
945 ++i;
946 }
947 offset += (array_index * data.size);
948 }
949 return offset;
950}
951
952static void SetUsedBytes(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 uint32_t block_memory_size = data.size;
955 for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) {
956 block_memory_size *= data.array_length_hierarchy[i];
957 }
958
959 offset = UpdateOffset(offset, array_indices, data);
960
961 uint32_t end = offset + block_memory_size;
962 auto used_bytes = data.GetUsedbytes();
963 if (used_bytes->size() < end) {
964 used_bytes->resize(end, 0);
965 }
966 std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size));
967}
968
969void SHADER_MODULE_STATE::RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index,
970 spirv_inst_iter &access_chain_it, const shader_struct_member &data) const {
971 if (access_chain_word_index < access_chain_it.len()) {
972 if (data.array_length_hierarchy.size() > array_indices.size()) {
973 auto def_it = get_def(access_chain_it.word(access_chain_word_index));
974 ++access_chain_word_index;
975
976 if (def_it != end() && def_it.opcode() == spv::OpConstant) {
977 array_indices.emplace_back(def_it.word(3));
978 RunUsedArray(offset, array_indices, access_chain_word_index, access_chain_it, data);
979 } else {
980 // If it is a variable, set the all array is used.
981 if (access_chain_word_index < access_chain_it.len()) {
982 uint32_t array_length = data.array_length_hierarchy[array_indices.size()];
983 for (uint32_t i = 0; i < array_length; ++i) {
984 auto array_indices2 = array_indices;
985 array_indices2.emplace_back(i);
986 RunUsedArray(offset, array_indices2, access_chain_word_index, access_chain_it, data);
987 }
988 } else {
989 SetUsedBytes(offset, array_indices, data);
990 }
991 }
992 } else {
993 offset = UpdateOffset(offset, array_indices, data);
994 RunUsedStruct(offset, access_chain_word_index, access_chain_it, data);
995 }
996 } else {
997 SetUsedBytes(offset, array_indices, data);
998 }
999}
1000
1001void SHADER_MODULE_STATE::RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it,
1002 const shader_struct_member &data) const {
1003 std::vector<uint32_t> array_indices_emptry;
1004
1005 if (access_chain_word_index < access_chain_it.len()) {
1006 auto strcut_member_index = GetConstantValueById(access_chain_it.word(access_chain_word_index));
1007 ++access_chain_word_index;
1008
1009 auto data1 = data.struct_members[strcut_member_index];
1010 RunUsedArray(offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1);
1011 }
1012}
1013
1014void SHADER_MODULE_STATE::SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list,
1015 const shader_struct_member &data) const {
1016 for (const auto &func_set : function_set_list) {
1017 auto range = func_set.op_lists.equal_range(spv::OpAccessChain);
1018 for (auto it = range.first; it != range.second; ++it) {
1019 auto access_chain = at(it->second);
1020 if (access_chain.word(3) == variable_id) {
1021 RunUsedStruct(0, 4, access_chain, data);
1022 }
1023 }
1024 }
1025}
1026
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001027// static
1028void SHADER_MODULE_STATE::SetPushConstantUsedInShader(
sfricke-samsungef15e482022-01-26 11:32:49 -08001029 const SHADER_MODULE_STATE &module_state, std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001030 for (auto &entrypoint : entry_points) {
1031 auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable);
1032 for (auto it = range.first; it != range.second; ++it) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001033 const auto def_insn = module_state.at(it->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001034
1035 if (def_insn.word(3) == spv::StorageClassPushConstant) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001036 spirv_inst_iter type = module_state.get_def(def_insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -07001037 const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate);
1038 std::vector<uint32_t> offsets;
1039
1040 for (auto it2 = range2.first; it2 != range2.second; ++it2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001041 auto member_decorate = module_state.at(it2->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001042 if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) {
1043 offsets.emplace_back(member_decorate.offset());
1044 }
1045 }
1046 entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader;
sfricke-samsungef15e482022-01-26 11:32:49 -08001047 module_state.DefineStructMember(type, offsets, entrypoint.second.push_constant_used_in_shader);
1048 module_state.SetUsedStructMember(def_insn.word(2), entrypoint.second.function_set_list,
1049 entrypoint.second.push_constant_used_in_shader);
sfricke-samsung962cad92021-04-13 00:46:29 -07001050 }
1051 }
1052 }
1053}
1054
1055uint32_t SHADER_MODULE_STATE::DescriptorTypeToReqs(uint32_t type_id) const {
1056 auto type = get_def(type_id);
1057
1058 while (true) {
1059 switch (type.opcode()) {
1060 case spv::OpTypeArray:
1061 case spv::OpTypeRuntimeArray:
1062 case spv::OpTypeSampledImage:
1063 type = get_def(type.word(2));
1064 break;
1065 case spv::OpTypePointer:
1066 type = get_def(type.word(3));
1067 break;
1068 case spv::OpTypeImage: {
1069 auto dim = type.word(3);
1070 auto arrayed = type.word(5);
1071 auto msaa = type.word(6);
1072
1073 uint32_t bits = 0;
1074 switch (GetFundamentalType(type.word(2))) {
1075 case FORMAT_TYPE_FLOAT:
1076 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
1077 break;
1078 case FORMAT_TYPE_UINT:
1079 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
1080 break;
1081 case FORMAT_TYPE_SINT:
1082 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
1083 break;
1084 default:
1085 break;
1086 }
1087
1088 switch (dim) {
1089 case spv::Dim1D:
1090 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1091 return bits;
1092 case spv::Dim2D:
1093 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1094 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
1095 return bits;
1096 case spv::Dim3D:
1097 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
1098 return bits;
1099 case spv::DimCube:
1100 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1101 return bits;
1102 case spv::DimSubpassData:
1103 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1104 return bits;
1105 default: // buffer, etc.
1106 return bits;
1107 }
1108 }
1109 default:
1110 return 0;
1111 }
1112 }
1113}
1114
1115// For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to.
1116// This function examines instructions in the static call tree for a write to this variable.
1117bool SHADER_MODULE_STATE::IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const {
1118 auto type = builtin_instr.opcode();
1119 uint32_t target_id = builtin_instr.word(1);
1120 bool init_complete = false;
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001121 uint32_t target_member_offset = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001122
1123 if (type == spv::OpMemberDecorate) {
1124 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1125 auto insn = entrypoint;
1126 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1127 switch (insn.opcode()) {
1128 case spv::OpTypePointer:
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001129 if (insn.word(2) == spv::StorageClassOutput) {
1130 const auto type_id = insn.word(3);
1131 if (type_id == target_id) {
1132 target_id = insn.word(1);
1133 } else {
1134 // If the output is an array, check if the element type is what we're looking for
1135 const auto type_insn = get_def(type_id);
1136 if ((type_insn.opcode() == spv::OpTypeArray) && (type_insn.word(2) == target_id)) {
1137 target_id = insn.word(1);
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001138 target_member_offset = 1;
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001139 }
1140 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001141 }
1142 break;
1143 case spv::OpVariable:
1144 if (insn.word(1) == target_id) {
1145 target_id = insn.word(2);
1146 init_complete = true;
1147 }
1148 break;
1149 }
1150 insn++;
1151 }
1152 }
1153
1154 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1155
1156 bool found_write = false;
1157 layer_data::unordered_set<uint32_t> worklist;
1158 worklist.insert(entrypoint.word(2));
1159
1160 // Follow instructions in call graph looking for writes to target
1161 while (!worklist.empty() && !found_write) {
1162 auto id_iter = worklist.begin();
1163 auto id = *id_iter;
1164 worklist.erase(id_iter);
1165
1166 auto insn = get_def(id);
1167 if (insn == end()) {
1168 continue;
1169 }
1170
1171 if (insn.opcode() == spv::OpFunction) {
1172 // Scan body of function looking for other function calls or items in our ID chain
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001173 while (++insn, (insn.opcode() != spv::OpFunctionEnd) && !found_write) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001174 switch (insn.opcode()) {
1175 case spv::OpAccessChain:
1176 if (insn.word(3) == target_id) {
1177 if (type == spv::OpMemberDecorate) {
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001178 // Get the target member of the struct
1179 // NOTE: this will only work for structs and arrays of structs. Deeper levels of nesting (e.g.,
1180 // arrays of structs of structs) is not currently supported.
1181 const auto value_itr = GetConstantDef(insn.word(4 + target_member_offset));
1182 if (value_itr != end()) {
1183 auto value = GetConstantValue(value_itr);
1184 if (value == builtin_instr.word(2)) {
1185 target_id = insn.word(2);
1186 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001187 }
1188 } else {
1189 target_id = insn.word(2);
1190 }
1191 }
1192 break;
1193 case spv::OpStore:
1194 if (insn.word(1) == target_id) {
1195 found_write = true;
1196 }
1197 break;
1198 case spv::OpFunctionCall:
1199 worklist.insert(insn.word(3));
1200 break;
1201 }
1202 }
1203 }
1204 }
1205 return found_write;
1206}
1207
1208// Used by the collection functions to help aid in state tracking
1209struct shader_module_used_operators {
1210 bool updated;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001211 std::vector<uint32_t> image_read_members;
1212 std::vector<uint32_t> image_write_members;
1213 std::vector<uint32_t> atomic_members;
1214 std::vector<uint32_t> store_members;
1215 std::vector<uint32_t> atomic_store_members;
1216 std::vector<uint32_t> sampler_implicitLod_dref_proj_members; // sampler Load id
1217 std::vector<uint32_t> sampler_bias_offset_members; // sampler Load id
1218 std::vector<uint32_t> image_dref_members;
1219 std::vector<std::pair<uint32_t, uint32_t>> sampled_image_members; // <image,sampler> Load id
1220 layer_data::unordered_map<uint32_t, uint32_t> load_members;
1221 layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> accesschain_members;
1222 layer_data::unordered_map<uint32_t, uint32_t> image_texel_pointer_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001223
1224 shader_module_used_operators() : updated(false) {}
1225
1226 bool CheckImageOperandsBiasOffset(uint32_t type) {
1227 return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask |
1228 spv::ImageOperandsConstOffsetsMask)
1229 ? true
1230 : false;
1231 }
1232
sfricke-samsungef15e482022-01-26 11:32:49 -08001233 void update(SHADER_MODULE_STATE const *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001234 if (updated) return;
1235 updated = true;
1236
sfricke-samsungef15e482022-01-26 11:32:49 -08001237 for (auto insn : *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001238 switch (insn.opcode()) {
1239 case spv::OpImageSampleImplicitLod:
1240 case spv::OpImageSampleProjImplicitLod:
1241 case spv::OpImageSampleProjExplicitLod:
1242 case spv::OpImageSparseSampleImplicitLod:
1243 case spv::OpImageSparseSampleProjImplicitLod:
1244 case spv::OpImageSparseSampleProjExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001245 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001246 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001247 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1248 sampler_implicitLod_dref_proj_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001249 // ImageOperands in index: 5
1250 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001251 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001252 }
1253 break;
1254 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001255 case spv::OpImageDrefGather:
1256 case spv::OpImageSparseDrefGather: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001257 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001258 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001259 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1260 image_dref_members.emplace_back(load_id);
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001261 break;
1262 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001263 case spv::OpImageSampleDrefImplicitLod:
1264 case spv::OpImageSampleDrefExplicitLod:
1265 case spv::OpImageSampleProjDrefImplicitLod:
1266 case spv::OpImageSampleProjDrefExplicitLod:
1267 case spv::OpImageSparseSampleDrefImplicitLod:
1268 case spv::OpImageSparseSampleDrefExplicitLod:
1269 case spv::OpImageSparseSampleProjDrefImplicitLod:
1270 case spv::OpImageSparseSampleProjDrefExplicitLod: {
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 sampler_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1274 auto image_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1275
1276 image_dref_members.emplace_back(image_load_id);
1277 sampler_implicitLod_dref_proj_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001278 // ImageOperands in index: 6
1279 if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001280 sampler_bias_offset_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001281 }
1282 break;
1283 }
1284 case spv::OpImageSampleExplicitLod:
1285 case spv::OpImageSparseSampleExplicitLod: {
1286 // ImageOperands in index: 5
1287 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001288 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001289 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001290 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1291 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001292 }
1293 break;
1294 }
1295 case spv::OpStore: {
1296 store_members.emplace_back(insn.word(1)); // object id or AccessChain id
1297 break;
1298 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001299 case spv::OpImageRead:
1300 case spv::OpImageSparseRead: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001301 image_read_members.emplace_back(insn.word(3)); // Load id
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001302 break;
1303 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001304 case spv::OpImageWrite: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001305 image_write_members.emplace_back(insn.word(1)); // Load id
sfricke-samsung962cad92021-04-13 00:46:29 -07001306 break;
1307 }
1308 case spv::OpSampledImage: {
1309 // 3: image load id, 4: sampler load id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001310 sampled_image_members.emplace_back(std::pair<uint32_t, uint32_t>(insn.word(3), insn.word(4)));
sfricke-samsung962cad92021-04-13 00:46:29 -07001311 break;
1312 }
1313 case spv::OpLoad: {
1314 // 2: Load id, 3: object id or AccessChain id
1315 load_members.emplace(insn.word(2), insn.word(3));
1316 break;
1317 }
1318 case spv::OpAccessChain: {
1319 if (insn.len() == 4) {
1320 // If it is for struct, the length is only 4.
1321 // 2: AccessChain id, 3: object id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001322 accesschain_members.emplace(insn.word(2), std::pair<uint32_t, uint32_t>(insn.word(3), 0));
sfricke-samsung962cad92021-04-13 00:46:29 -07001323 } else {
1324 // 2: AccessChain id, 3: object id, 4: object id of array index
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001325 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 -07001326 }
1327 break;
1328 }
1329 case spv::OpImageTexelPointer: {
1330 // 2: ImageTexelPointer id, 3: object id
1331 image_texel_pointer_members.emplace(insn.word(2), insn.word(3));
1332 break;
1333 }
1334 default: {
1335 if (AtomicOperation(insn.opcode())) {
1336 if (insn.opcode() == spv::OpAtomicStore) {
1337 atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id
1338 } else {
1339 atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id
1340 }
1341 }
1342 break;
1343 }
1344 }
1345 }
1346 }
1347};
1348
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001349static bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<uint32_t> &operator_members,
1350 const layer_data::unordered_map<uint32_t, uint32_t> &load_members,
1351 const layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> &accesschain_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001352 for (auto load_id : operator_members) {
1353 if (object_id == load_id) return true;
1354 auto load_it = load_members.find(load_id);
1355 if (load_it == load_members.end()) {
1356 continue;
1357 }
1358 if (load_it->second == object_id) {
1359 return true;
1360 }
1361
1362 auto accesschain_it = accesschain_members.find(load_it->second);
1363 if (accesschain_it == accesschain_members.end()) {
1364 continue;
1365 }
1366 if (accesschain_it->second.first == object_id) {
1367 return true;
1368 }
1369 }
1370 return false;
1371}
1372
1373// Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image
1374// atomic operation, matching images to samplers, etc
1375void SHADER_MODULE_STATE::IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable,
1376 interface_var &out_interface_var,
1377 shader_module_used_operators &used_operators) const {
1378 uint32_t type_id = id_it.word(1);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001379 uint32_t id = id_it.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001380
1381 auto type = get_def(type_id);
1382
1383 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
1384 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray ||
1385 type.opcode() == spv::OpTypeSampledImage) {
1386 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray ||
1387 type.opcode() == spv::OpTypeSampledImage) {
1388 type = get_def(type.word(2)); // Element type
1389 } else {
1390 type = get_def(type.word(3)); // Pointer type
1391 }
1392 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001393
sfricke-samsung962cad92021-04-13 00:46:29 -07001394 switch (type.opcode()) {
1395 case spv::OpTypeImage: {
1396 auto dim = type.word(3);
1397 if (dim != spv::DimSubpassData) {
1398 used_operators.update(this);
1399
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001400 // Sampled == 2 indicates used without a sampler (a storage image)
1401 bool is_image_without_format = false;
1402 if (type.word(7) == 2) is_image_without_format = type.word(8) == spv::ImageFormatUnknown;
1403
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001404 if (CheckObjectIDFromOpLoad(id, used_operators.image_write_members, used_operators.load_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001405 used_operators.accesschain_members)) {
1406 out_interface_var.is_writable = true;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001407 if (is_image_without_format) out_interface_var.is_write_without_format = true;
1408 }
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001409 if (CheckObjectIDFromOpLoad(id, used_operators.image_read_members, used_operators.load_members,
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001410 used_operators.accesschain_members)) {
1411 out_interface_var.is_readable = true;
1412 if (is_image_without_format) out_interface_var.is_read_without_format = true;
sfricke-samsung962cad92021-04-13 00:46:29 -07001413 }
1414 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members,
1415 used_operators.accesschain_members)) {
1416 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1417 }
1418 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members,
1419 used_operators.accesschain_members)) {
1420 out_interface_var.is_sampler_bias_offset = true;
1421 }
1422 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members,
1423 used_operators.accesschain_members) ||
1424 CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1425 used_operators.accesschain_members)) {
1426 out_interface_var.is_atomic_operation = true;
1427 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001428 if (CheckObjectIDFromOpLoad(id, used_operators.image_dref_members, used_operators.load_members,
1429 used_operators.accesschain_members)) {
1430 out_interface_var.is_dref_operation = true;
1431 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001432
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001433 for (auto &itp_id : used_operators.sampled_image_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001434 // Find if image id match.
1435 uint32_t image_index = 0;
1436 auto load_it = used_operators.load_members.find(itp_id.first);
1437 if (load_it == used_operators.load_members.end()) {
1438 continue;
1439 } else {
1440 if (load_it->second != id) {
1441 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1442 if (accesschain_it == used_operators.accesschain_members.end()) {
1443 continue;
1444 } else {
1445 if (accesschain_it->second.first != id) {
1446 continue;
1447 }
1448
1449 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1450 if (const_itr == end()) {
1451 // access chain index not a constant, skip.
1452 break;
1453 }
1454 image_index = GetConstantValue(const_itr);
1455 }
1456 }
1457 }
1458 // Find sampler's set binding.
1459 load_it = used_operators.load_members.find(itp_id.second);
1460 if (load_it == used_operators.load_members.end()) {
1461 continue;
1462 } else {
1463 uint32_t sampler_id = load_it->second;
1464 uint32_t sampler_index = 0;
1465 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1466
1467 if (accesschain_it != used_operators.accesschain_members.end()) {
1468 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1469 if (const_itr == end()) {
1470 // access chain index representing sampler index is not a constant, skip.
1471 break;
1472 }
1473 sampler_id = const_itr.offset();
1474 sampler_index = GetConstantValue(const_itr);
1475 }
1476 auto sampler_dec = get_decorations(sampler_id);
1477 if (image_index >= out_interface_var.samplers_used_by_image.size()) {
1478 out_interface_var.samplers_used_by_image.resize(image_index + 1);
1479 }
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001480
1481 // Need to check again for these properties in case not using a combined image sampler
1482 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_implicitLod_dref_proj_members,
1483 used_operators.load_members, used_operators.accesschain_members)) {
1484 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1485 }
1486 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_bias_offset_members,
1487 used_operators.load_members, used_operators.accesschain_members)) {
1488 out_interface_var.is_sampler_bias_offset = true;
1489 }
1490
sfricke-samsung962cad92021-04-13 00:46:29 -07001491 out_interface_var.samplers_used_by_image[image_index].emplace(
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001492 SamplerUsedByImage{DescriptorSlot{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index});
sfricke-samsung962cad92021-04-13 00:46:29 -07001493 }
1494 }
1495 }
1496 return;
1497 }
1498
1499 case spv::OpTypeStruct: {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001500 layer_data::unordered_set<uint32_t> nonwritable_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001501 if (get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001502 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001503 if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) {
1504 nonwritable_members.insert(insn.word(2));
1505 }
1506 }
1507
1508 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
1509 // as nonwritable.
1510 if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) {
1511 used_operators.update(this);
1512
1513 for (auto oid : used_operators.store_members) {
1514 if (id == oid) {
1515 out_interface_var.is_writable = true;
1516 return;
1517 }
1518 auto accesschain_it = used_operators.accesschain_members.find(oid);
1519 if (accesschain_it == used_operators.accesschain_members.end()) {
1520 continue;
1521 }
1522 if (accesschain_it->second.first == id) {
1523 out_interface_var.is_writable = true;
1524 return;
1525 }
1526 }
1527 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1528 used_operators.accesschain_members)) {
1529 out_interface_var.is_writable = true;
1530 return;
1531 }
1532 }
1533 }
1534 }
1535}
1536
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001537std::vector<std::pair<DescriptorSlot, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByDescriptorSlot(
ziga-lunargc2de4782022-04-14 19:49:07 +02001538 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001539 std::vector<std::pair<DescriptorSlot, interface_var>> out;
sfricke-samsung962cad92021-04-13 00:46:29 -07001540 shader_module_used_operators operators;
1541
ziga-lunargc2de4782022-04-14 19:49:07 +02001542 for (auto id : accessible_ids) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001543 auto insn = get_def(id);
1544 assert(insn != end());
1545
1546 if (insn.opcode() == spv::OpVariable &&
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001547 (insn.word(3) == spv::StorageClassUniform ||
1548 insn.word(3) == spv::StorageClassUniformConstant ||
sfricke-samsung962cad92021-04-13 00:46:29 -07001549 insn.word(3) == spv::StorageClassStorageBuffer)) {
1550 auto d = get_decorations(insn.word(2));
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001551 uint32_t set = d.descriptor_set;
1552 uint32_t binding = d.binding;
sfricke-samsung962cad92021-04-13 00:46:29 -07001553
1554 interface_var v = {};
1555 v.id = insn.word(2);
1556 v.type_id = insn.word(1);
1557
1558 IsSpecificDescriptorType(insn, insn.word(3) == spv::StorageClassStorageBuffer,
1559 !(d.flags & decoration_set::nonwritable_bit), v, operators);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001560 out.emplace_back(DescriptorSlot{set, binding}, v);
sfricke-samsung962cad92021-04-13 00:46:29 -07001561 }
1562 }
1563
1564 return out;
1565}
1566
1567layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::CollectWritableOutputLocationinFS(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001568 const spirv_inst_iter &entrypoint) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001569 layer_data::unordered_set<uint32_t> location_list;
sfricke-samsung962cad92021-04-13 00:46:29 -07001570 const auto outputs = CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001571 layer_data::unordered_set<uint32_t> store_members;
1572 layer_data::unordered_map<uint32_t, uint32_t> accesschain_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001573
1574 for (auto insn : *this) {
1575 switch (insn.opcode()) {
1576 case spv::OpStore:
1577 case spv::OpAtomicStore: {
1578 store_members.insert(insn.word(1)); // object id or AccessChain id
1579 break;
1580 }
1581 case spv::OpAccessChain: {
1582 // 2: AccessChain id, 3: object id
1583 if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3));
1584 break;
1585 }
1586 default:
1587 break;
1588 }
1589 }
1590 if (store_members.empty()) {
1591 return location_list;
1592 }
1593 for (auto output : outputs) {
1594 auto store_it = store_members.find(output.second.id);
1595 if (store_it != store_members.end()) {
1596 location_list.insert(output.first.first);
1597 store_members.erase(store_it);
1598 continue;
1599 }
1600 store_it = store_members.begin();
1601 while (store_it != store_members.end()) {
1602 auto accesschain_it = accesschain_members.find(*store_it);
1603 if (accesschain_it == accesschain_members.end()) {
1604 ++store_it;
1605 continue;
1606 }
1607 if (accesschain_it->second == output.second.id) {
1608 location_list.insert(output.first.first);
1609 store_members.erase(store_it);
1610 accesschain_members.erase(accesschain_it);
1611 break;
1612 }
1613 ++store_it;
1614 }
1615 }
1616 return location_list;
1617}
1618
1619bool SHADER_MODULE_STATE::CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts,
ziga-lunarg9e94e112021-09-27 00:21:10 +02001620 uint32_t id, uint32_t type_id, bool is_patch,
1621 uint32_t /*first_location*/) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001622 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
1623 auto type = GetStructType(get_def(type_id), is_array_of_verts && !is_patch);
1624 if (type == end() || !(get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
1625 // This isn't an interface block.
1626 return false;
1627 }
1628
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001629 layer_data::unordered_map<uint32_t, uint32_t> member_components;
1630 layer_data::unordered_map<uint32_t, uint32_t> member_relaxed_precision;
1631 layer_data::unordered_map<uint32_t, uint32_t> member_patch;
sfricke-samsung962cad92021-04-13 00:46:29 -07001632
1633 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001634 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001635 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001636 uint32_t member_index = insn.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001637
1638 if (insn.word(3) == spv::DecorationComponent) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001639 uint32_t component = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001640 member_components[member_index] = component;
1641 }
1642
1643 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
1644 member_relaxed_precision[member_index] = 1;
1645 }
1646
1647 if (insn.word(3) == spv::DecorationPatch) {
1648 member_patch[member_index] = 1;
1649 }
1650 }
1651 }
1652
1653 // TODO: correctly handle location assignment from outside
1654
1655 // Second pass -- produce the output, from Location decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001656 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001657 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001658 uint32_t member_index = insn.word(2);
1659 uint32_t member_type_id = type.word(2 + member_index);
sfricke-samsung962cad92021-04-13 00:46:29 -07001660
1661 if (insn.word(3) == spv::DecorationLocation) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001662 uint32_t location = insn.word(4);
1663 uint32_t num_locations = GetLocationsConsumedByType(member_type_id, false);
sfricke-samsung962cad92021-04-13 00:46:29 -07001664 auto component_it = member_components.find(member_index);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001665 uint32_t component = component_it == member_components.end() ? 0 : component_it->second;
sfricke-samsung962cad92021-04-13 00:46:29 -07001666 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
1667 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
1668
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001669 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001670 interface_var v = {};
1671 v.id = id;
1672 // TODO: member index in interface_var too?
1673 v.type_id = member_type_id;
1674 v.offset = offset;
1675 v.is_patch = member_is_patch;
1676 v.is_block_member = true;
1677 v.is_relaxed_precision = is_relaxed_precision;
1678 (*out)[std::make_pair(location + offset, component)] = v;
1679 }
1680 }
1681 }
1682 }
1683
1684 return true;
1685}
1686
1687std::map<location_t, interface_var> SHADER_MODULE_STATE::CollectInterfaceByLocation(spirv_inst_iter entrypoint,
1688 spv::StorageClass sinterface,
1689 bool is_array_of_verts) const {
1690 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
1691
1692 std::map<location_t, interface_var> out;
1693
1694 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
1695 auto insn = get_def(iid);
1696 assert(insn != end());
1697 assert(insn.opcode() == spv::OpVariable);
1698
ziga-lunarg9e94e112021-09-27 00:21:10 +02001699 const auto d = get_decorations(iid);
1700 bool passthrough = sinterface == spv::StorageClassOutput && insn.word(3) == spv::StorageClassInput &&
1701 (d.flags & decoration_set::passthrough_bit) != 0;
1702 if (insn.word(3) == static_cast<uint32_t>(sinterface) || passthrough) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001703 uint32_t id = insn.word(2);
1704 uint32_t type = insn.word(1);
sfricke-samsung962cad92021-04-13 00:46:29 -07001705
ziga-lunarg9e94e112021-09-27 00:21:10 +02001706 auto location = d.location;
sfricke-samsung962cad92021-04-13 00:46:29 -07001707 int builtin = d.builtin;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001708 uint32_t component = d.component;
sfricke-samsung962cad92021-04-13 00:46:29 -07001709 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
1710 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001711 bool is_per_vertex = (d.flags & decoration_set::per_vertex_bit) != 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001712
1713 if (builtin != -1) {
1714 continue;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001715 } else if (!CollectInterfaceBlockMembers(&out, is_array_of_verts, id, type, is_patch, location) ||
1716 location != decoration_set::kInvalidValue) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001717 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
1718 // one result for each.
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001719 uint32_t num_locations = GetLocationsConsumedByType(type, (is_array_of_verts && !is_patch) || is_per_vertex);
1720 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001721 interface_var v = {};
1722 v.id = id;
1723 v.type_id = type;
1724 v.offset = offset;
1725 v.is_patch = is_patch;
1726 v.is_relaxed_precision = is_relaxed_precision;
1727 out[std::make_pair(location + offset, component)] = v;
1728 }
1729 }
1730 }
1731 }
1732
1733 return out;
1734}
1735
1736std::vector<uint32_t> SHADER_MODULE_STATE::CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001737 // Find all interface variables belonging to the entrypoint and matching the storage class
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001738 std::vector<uint32_t> variables;
sfricke-samsung962cad92021-04-13 00:46:29 -07001739 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
1740 auto def = get_def(id);
1741 assert(def != end());
1742 assert(def.opcode() == spv::OpVariable);
1743
1744 if (def.word(3) == storageClass) variables.push_back(def.word(1));
1745 }
1746
1747 // Find all members belonging to the builtin block selected
1748 std::vector<uint32_t> builtin_block_members;
1749 for (auto &var : variables) {
1750 auto def = get_def(get_def(var).word(3));
1751
1752 // It could be an array of IO blocks. The element type should be the struct defining the block contents
1753 if (def.opcode() == spv::OpTypeArray) def = get_def(def.word(2));
1754
1755 // Now find all members belonging to the struct defining the IO block
1756 if (def.opcode() == spv::OpTypeStruct) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001757 for (auto set : static_data_.builtin_decoration_list) {
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001758 auto insn = at(set.offset);
1759 if ((insn.opcode() == spv::OpMemberDecorate) && (def.word(1) == insn.word(1))) {
1760 // Start with undefined builtin for each struct member.
1761 // But only when confirmed the struct is the built-in inteface block (can only be one per shader)
1762 if (builtin_block_members.size() == 0) {
1763 builtin_block_members.resize(def.len() - 2, spv::BuiltInMax);
sfricke-samsung962cad92021-04-13 00:46:29 -07001764 }
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001765 auto struct_index = insn.word(2);
1766 assert(struct_index < builtin_block_members.size());
1767 builtin_block_members[struct_index] = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001768 }
1769 }
1770 }
1771 }
1772
1773 return builtin_block_members;
1774}
1775
1776std::vector<std::pair<uint32_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByInputAttachmentIndex(
1777 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
1778 std::vector<std::pair<uint32_t, interface_var>> out;
1779
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001780 for (auto insn : static_data_.decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001781 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
1782 auto attachment_index = insn.word(3);
1783 auto id = insn.word(1);
1784
1785 if (accessible_ids.count(id)) {
1786 auto def = get_def(id);
1787 assert(def != end());
1788 if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) {
1789 auto num_locations = GetLocationsConsumedByType(def.word(1), false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001790 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001791 interface_var v = {};
1792 v.id = id;
1793 v.type_id = def.word(1);
1794 v.offset = offset;
1795 out.emplace_back(attachment_index + offset, v);
1796 }
1797 }
1798 }
1799 }
1800 }
1801
1802 return out;
1803}
1804
ziga-lunarg8346fe82021-08-22 17:30:50 +02001805uint32_t SHADER_MODULE_STATE::GetNumComponentsInBaseType(const spirv_inst_iter &iter) const {
1806 const uint32_t opcode = iter.opcode();
1807 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1808 return 1;
1809 } else if (opcode == spv::OpTypeVector) {
1810 const uint32_t component_count = iter.word(3);
1811 return component_count;
1812 } else if (opcode == spv::OpTypeMatrix) {
1813 const auto column_type = get_def(iter.word(2));
1814 const uint32_t vector_length = GetNumComponentsInBaseType(column_type);
ziga-lunarg38e44982022-04-05 00:10:46 +02001815 // Because we are calculating components for a single location we do not care about column count
1816 return vector_length;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001817 } else if (opcode == spv::OpTypeArray) {
1818 const auto element_type = get_def(iter.word(2));
1819 const uint32_t element_length = GetNumComponentsInBaseType(element_type);
1820 return element_length;
1821 } else if (opcode == spv::OpTypeStruct) {
1822 uint32_t total_size = 0;
1823 for (uint32_t i = 2; i < iter.len(); ++i) {
1824 total_size += GetNumComponentsInBaseType(get_def(iter.word(i)));
1825 }
1826 return total_size;
1827 } else if (opcode == spv::OpTypePointer) {
1828 const auto type = get_def(iter.word(3));
1829 return GetNumComponentsInBaseType(type);
1830 }
1831 return 0;
1832}
1833
ziga-lunarga26b3602021-08-08 15:53:00 +02001834uint32_t SHADER_MODULE_STATE::GetTypeBitsSize(const spirv_inst_iter &iter) const {
1835 const uint32_t opcode = iter.opcode();
1836 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1837 return iter.word(2);
1838 } else if (opcode == spv::OpTypeVector) {
1839 const auto component_type = get_def(iter.word(2));
1840 uint32_t scalar_width = GetTypeBitsSize(component_type);
1841 uint32_t component_count = iter.word(3);
1842 return scalar_width * component_count;
1843 } else if (opcode == spv::OpTypeMatrix) {
1844 const auto column_type = get_def(iter.word(2));
1845 uint32_t vector_width = GetTypeBitsSize(column_type);
1846 uint32_t column_count = iter.word(3);
1847 return vector_width * column_count;
1848 } else if (opcode == spv::OpTypeArray) {
1849 const auto element_type = get_def(iter.word(2));
1850 uint32_t element_width = GetTypeBitsSize(element_type);
1851 const auto length_type = get_def(iter.word(3));
1852 uint32_t length = GetConstantValue(length_type);
1853 return element_width * length;
1854 } else if (opcode == spv::OpTypeStruct) {
1855 uint32_t total_size = 0;
1856 for (uint32_t i = 2; i < iter.len(); ++i) {
1857 total_size += GetTypeBitsSize(get_def(iter.word(i)));
1858 }
1859 return total_size;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001860 } else if (opcode == spv::OpTypePointer) {
1861 const auto type = get_def(iter.word(3));
1862 return GetTypeBitsSize(type);
ziga-lunargef2c3172021-11-07 10:35:29 +01001863 } else if (opcode == spv::OpVariable) {
1864 const auto type = get_def(iter.word(1));
1865 return GetTypeBitsSize(type);
ziga-lunarga26b3602021-08-08 15:53:00 +02001866 }
1867 return 0;
1868}
1869
1870uint32_t SHADER_MODULE_STATE::GetTypeBytesSize(const spirv_inst_iter &iter) const { return GetTypeBitsSize(iter) / 8; }
1871
ziga-lunarg19fc6ae2021-09-09 00:05:19 +02001872// 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 +02001873uint32_t SHADER_MODULE_STATE::GetBaseType(const spirv_inst_iter &iter) const {
1874 const uint32_t opcode = iter.opcode();
1875 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt || opcode == spv::OpTypeStruct) {
1876 return iter.word(1);
1877 } else if (opcode == spv::OpTypeVector) {
1878 const auto& component_type = get_def(iter.word(2));
1879 return GetBaseType(component_type);
1880 } else if (opcode == spv::OpTypeMatrix) {
1881 const auto& column_type = get_def(iter.word(2));
1882 return GetBaseType(column_type);
1883 } else if (opcode == spv::OpTypeArray) {
1884 const auto& element_type = get_def(iter.word(2));
1885 return GetBaseType(element_type);
1886 } else if (opcode == spv::OpTypePointer) {
1887 const auto& type = get_def(iter.word(3));
1888 return GetBaseType(type);
1889 }
1890 return 0;
1891}
1892
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001893// Returns type_id if id has type or zero otherwise
1894uint32_t SHADER_MODULE_STATE::GetTypeId(uint32_t id) const {
1895 const auto type = get_def(id);
1896 return OpcodeHasType(type.opcode()) ? type.word(1) : 0;
1897}
1898
ziga-lunarga26b3602021-08-08 15:53:00 +02001899uint32_t SHADER_MODULE_STATE::CalcComputeSharedMemory(VkShaderStageFlagBits stage,
1900 const spirv_inst_iter &insn) const {
1901 if (stage == VK_SHADER_STAGE_COMPUTE_BIT && insn.opcode() == spv::OpVariable) {
1902 uint32_t storage_class = insn.word(3);
1903 if (storage_class == spv::StorageClassWorkgroup) { // StorageClass Workgroup is shared memory
1904 uint32_t result_type_id = insn.word(1);
1905 auto result_type = get_def(result_type_id);
1906 auto type = get_def(result_type.word(3));
1907 return GetTypeBytesSize(type);
1908 }
1909 }
1910
1911 return 0;
1912}
1913
sfricke-samsung962cad92021-04-13 00:46:29 -07001914// Assumes itr points to an OpConstant instruction
1915uint32_t GetConstantValue(const spirv_inst_iter &itr) { return itr.word(3); }
1916
1917std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint) {
1918 assert(entrypoint.opcode() == spv::OpEntryPoint);
1919
1920 std::vector<uint32_t> interfaces;
1921 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
1922 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
1923 uint32_t word = 3;
1924 while (entrypoint.word(word) & 0xff000000u) {
1925 ++word;
1926 }
1927 ++word;
1928
1929 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
1930
1931 return interfaces;
1932}