blob: 47dc006c43689ebef7c244f7c99471ffcbe15e05 [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;
sjfrickedb3441a2022-09-13 21:22:47 +0900153 case spv::ExecutionModelTaskEXT:
154 return VK_SHADER_STAGE_TASK_BIT_EXT;
155 case spv::ExecutionModelMeshEXT:
156 return VK_SHADER_STAGE_MESH_BIT_EXT;
sfricke-samsung962cad92021-04-13 00:46:29 -0700157 default:
158 return 0;
159 }
160}
161
162// For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is
163// important for identifying the set of shader resources actually used by an entrypoint, for example.
164// Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
165// - NOT the shader input/output interfaces.
166//
167// TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
168// converting parts of this to be generated from the machine-readable spec instead.
169layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::MarkAccessibleIds(spirv_inst_iter entrypoint) const {
170 layer_data::unordered_set<uint32_t> ids;
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600171 if (entrypoint == end() || !has_valid_spirv) {
172 return ids;
173 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700174 layer_data::unordered_set<uint32_t> worklist;
175 worklist.insert(entrypoint.word(2));
176
177 while (!worklist.empty()) {
178 auto id_iter = worklist.begin();
179 auto id = *id_iter;
180 worklist.erase(id_iter);
181
182 auto insn = get_def(id);
183 if (insn == end()) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600184 // 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 -0700185 // that we may not care about.
186 continue;
187 }
188
189 // Try to add to the output set
190 if (!ids.insert(id).second) {
191 continue; // If we already saw this id, we don't want to walk it again.
192 }
193
194 switch (insn.opcode()) {
195 case spv::OpFunction:
196 // Scan whole body of the function, enlisting anything interesting
197 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
198 switch (insn.opcode()) {
199 case spv::OpLoad:
200 worklist.insert(insn.word(3)); // ptr
201 break;
202 case spv::OpStore:
203 worklist.insert(insn.word(1)); // ptr
204 break;
205 case spv::OpAccessChain:
206 case spv::OpInBoundsAccessChain:
207 worklist.insert(insn.word(3)); // base ptr
208 break;
209 case spv::OpSampledImage:
210 case spv::OpImageSampleImplicitLod:
211 case spv::OpImageSampleExplicitLod:
212 case spv::OpImageSampleDrefImplicitLod:
213 case spv::OpImageSampleDrefExplicitLod:
214 case spv::OpImageSampleProjImplicitLod:
215 case spv::OpImageSampleProjExplicitLod:
216 case spv::OpImageSampleProjDrefImplicitLod:
217 case spv::OpImageSampleProjDrefExplicitLod:
218 case spv::OpImageFetch:
219 case spv::OpImageGather:
220 case spv::OpImageDrefGather:
221 case spv::OpImageRead:
222 case spv::OpImage:
223 case spv::OpImageQueryFormat:
224 case spv::OpImageQueryOrder:
225 case spv::OpImageQuerySizeLod:
226 case spv::OpImageQuerySize:
227 case spv::OpImageQueryLod:
228 case spv::OpImageQueryLevels:
229 case spv::OpImageQuerySamples:
230 case spv::OpImageSparseSampleImplicitLod:
231 case spv::OpImageSparseSampleExplicitLod:
232 case spv::OpImageSparseSampleDrefImplicitLod:
233 case spv::OpImageSparseSampleDrefExplicitLod:
234 case spv::OpImageSparseSampleProjImplicitLod:
235 case spv::OpImageSparseSampleProjExplicitLod:
236 case spv::OpImageSparseSampleProjDrefImplicitLod:
237 case spv::OpImageSparseSampleProjDrefExplicitLod:
238 case spv::OpImageSparseFetch:
239 case spv::OpImageSparseGather:
240 case spv::OpImageSparseDrefGather:
241 case spv::OpImageTexelPointer:
242 worklist.insert(insn.word(3)); // Image or sampled image
243 break;
244 case spv::OpImageWrite:
245 worklist.insert(insn.word(1)); // Image -- different operand order to above
246 break;
247 case spv::OpFunctionCall:
248 for (uint32_t i = 3; i < insn.len(); i++) {
249 worklist.insert(insn.word(i)); // fn itself, and all args
250 }
251 break;
252
253 case spv::OpExtInst:
254 for (uint32_t i = 5; i < insn.len(); i++) {
255 worklist.insert(insn.word(i)); // Operands to ext inst
256 }
257 break;
258
259 default: {
260 if (AtomicOperation(insn.opcode())) {
261 if (insn.opcode() == spv::OpAtomicStore) {
262 worklist.insert(insn.word(1)); // ptr
263 } else {
264 worklist.insert(insn.word(3)); // ptr
265 }
266 }
267 break;
268 }
269 }
270 }
271 break;
272 }
273 }
274
275 return ids;
276}
277
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600278layer_data::optional<VkPrimitiveTopology> SHADER_MODULE_STATE::GetTopology(const spirv_inst_iter &entrypoint) const {
279 layer_data::optional<VkPrimitiveTopology> result;
280
sfricke-samsung962cad92021-04-13 00:46:29 -0700281 auto entrypoint_id = entrypoint.word(2);
282 bool is_point_mode = false;
283
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600284 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
285 if (it != static_data_.execution_mode_inst.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700286 for (auto insn : it->second) {
287 switch (insn.word(2)) {
288 case spv::ExecutionModePointMode:
289 // In tessellation shaders, PointMode is separate and trumps the tessellation topology.
290 is_point_mode = true;
291 break;
292
293 case spv::ExecutionModeOutputPoints:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600294 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
sfricke-samsung962cad92021-04-13 00:46:29 -0700295 break;
296
297 case spv::ExecutionModeIsolines:
298 case spv::ExecutionModeOutputLineStrip:
Ricardo Garcia122f8f02021-09-28 16:47:19 +0200299 case spv::ExecutionModeOutputLinesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600300 result.emplace(VK_PRIMITIVE_TOPOLOGY_LINE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700301 break;
302
303 case spv::ExecutionModeTriangles:
304 case spv::ExecutionModeQuads:
305 case spv::ExecutionModeOutputTriangleStrip:
306 case spv::ExecutionModeOutputTrianglesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600307 result.emplace(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700308 break;
309 }
310 }
311 }
312
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600313 if (is_point_mode) {
314 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
315 }
316
317 return result;
sfricke-samsung962cad92021-04-13 00:46:29 -0700318}
319
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -0700320layer_data::optional<VkPrimitiveTopology> SHADER_MODULE_STATE::GetTopology() const {
321 if (static_data_.entry_points.size() > 0) {
322 const auto entrypoint = static_data_.entry_points.cbegin()->second;
323 return GetTopology(get_def(entrypoint.offset));
324 }
325 return {};
326}
327
sfricke-samsungef15e482022-01-26 11:32:49 -0800328SHADER_MODULE_STATE::SpirvStaticData::SpirvStaticData(const SHADER_MODULE_STATE &module_state) {
329 for (auto insn : module_state) {
sfricke-samsung5a48ed42022-02-13 17:37:13 -0800330 const uint32_t result_word = OpcodeResultWord(insn.opcode());
331 if (result_word != 0) {
332 def_index[insn.word(result_word)] = insn.offset();
333 }
334
sfricke-samsung962cad92021-04-13 00:46:29 -0700335 switch (insn.opcode()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700336 // Specialization constants
337 case spv::OpSpecConstantTrue:
338 case spv::OpSpecConstantFalse:
339 case spv::OpSpecConstant:
340 case spv::OpSpecConstantComposite:
341 case spv::OpSpecConstantOp:
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600342 has_specialization_constants = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700343 break;
344
sfricke-samsung962cad92021-04-13 00:46:29 -0700345 // Decorations
346 case spv::OpDecorate: {
347 auto target_id = insn.word(1);
348 decorations[target_id].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u);
349 decoration_inst.push_back(insn);
350 if (insn.word(2) == spv::DecorationBuiltIn) {
351 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(3)));
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -0600352 } else if (insn.word(2) == spv::DecorationSpecId) {
353 spec_const_map[insn.word(3)] = target_id;
sfricke-samsung962cad92021-04-13 00:46:29 -0700354 }
355
356 } break;
357 case spv::OpGroupDecorate: {
358 auto const &src = decorations[insn.word(1)];
359 for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600360 has_group_decoration = true;
361 } break;
362 case spv::OpDecorationGroup:
363 case spv::OpGroupMemberDecorate: {
364 has_group_decoration = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700365 } break;
366 case spv::OpMemberDecorate: {
367 member_decoration_inst.push_back(insn);
368 if (insn.word(3) == spv::DecorationBuiltIn) {
369 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(4)));
370 }
371 } break;
372
ziga-lunargbe003732022-04-22 00:20:13 +0200373 case spv::OpCapability:
374 capability_list.push_back(static_cast<spv::Capability>(insn.word(1)));
375 break;
376
sjfricke44d663c2022-06-01 06:42:58 +0900377 case spv::OpVariable:
378 variable_inst.push_back(insn);
379 break;
380
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600381 // Execution Mode
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800382 case spv::OpExecutionMode:
383 case spv::OpExecutionModeId: {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600384 execution_mode_inst[insn.word(1)].push_back(insn);
385 } break;
sjfricke657dfdc2022-08-25 23:40:32 +0900386 // Listed from vkspec.html#ray-tracing-repack
ziga-lunarge25f5f02022-04-16 15:07:35 +0200387 case spv::OpTraceRayKHR:
388 case spv::OpTraceRayMotionNV:
389 case spv::OpReportIntersectionKHR:
390 case spv::OpExecuteCallableKHR:
391 has_invocation_repack_instruction = true;
392 break;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600393
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600394 default:
395 if (AtomicOperation(insn.opcode()) == true) {
396 // All atomics have a pointer referenced
397 spirv_inst_iter access;
398 if (insn.opcode() == spv::OpAtomicStore) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800399 access = module_state.get_def(insn.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600400 } else {
sfricke-samsungef15e482022-01-26 11:32:49 -0800401 access = module_state.get_def(insn.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600402 }
403
404 atomic_instruction atomic;
405
sfricke-samsungef15e482022-01-26 11:32:49 -0800406 auto pointer = module_state.get_def(access.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600407 // spirv-val should catch if not pointer
408 assert(pointer.opcode() == spv::OpTypePointer);
409 atomic.storage_class = pointer.word(2);
410
sfricke-samsungef15e482022-01-26 11:32:49 -0800411 auto data_type = module_state.get_def(pointer.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600412 atomic.type = data_type.opcode();
413
414 // TODO - Should have a proper GetBitWidth like spirv-val does
415 assert(data_type.opcode() == spv::OpTypeFloat || data_type.opcode() == spv::OpTypeInt);
416 atomic.bit_width = data_type.word(2);
417
418 atomic_inst[insn.offset()] = atomic;
419 }
420 // We don't care about any other defs for now.
421 break;
422 }
423 }
424
sfricke-samsungef15e482022-01-26 11:32:49 -0800425 entry_points = SHADER_MODULE_STATE::ProcessEntryPoints(module_state);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600426 multiple_entry_points = entry_points.size() > 1;
427}
428
429// static
430std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> SHADER_MODULE_STATE::ProcessEntryPoints(
sfricke-samsungef15e482022-01-26 11:32:49 -0800431 const SHADER_MODULE_STATE &module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600432 std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> entry_points;
433 function_set func_set = {};
434 EntryPoint *entry_point = nullptr;
435
sfricke-samsungef15e482022-01-26 11:32:49 -0800436 for (auto insn : module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600437 // offset is not 0, it means it's updated and the offset is in a Function.
438 if (func_set.offset) {
439 func_set.op_lists.emplace(insn.opcode(), insn.offset());
440 } else if (entry_point) {
441 entry_point->decorate_list.emplace(insn.opcode(), insn.offset());
442 }
443
444 switch (insn.opcode()) {
445 // Functions
446 case spv::OpFunction:
447 func_set.id = insn.word(2);
448 func_set.offset = insn.offset();
449 func_set.op_lists.clear();
450 break;
451
452 // Entry points ... add to the entrypoint table
453 case spv::OpEntryPoint: {
sfricke-samsung962cad92021-04-13 00:46:29 -0700454 // Entry points do not have an id (the id is the function id) and thus need their own table
455 auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3));
456 auto execution_model = insn.word(1);
457 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
458 entry_points.emplace(entrypoint_name,
459 EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)});
460
461 auto range = entry_points.equal_range(entrypoint_name);
462 for (auto it = range.first; it != range.second; ++it) {
463 if (it->second.offset == insn.offset()) {
464 entry_point = &(it->second);
465 break;
466 }
467 }
468 assert(entry_point != nullptr);
469 break;
470 }
471 case spv::OpFunctionEnd: {
472 assert(entry_point != nullptr);
473 func_set.length = insn.offset() - func_set.offset;
474 entry_point->function_set_list.emplace_back(func_set);
475 break;
476 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700477 }
478 }
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600479
sfricke-samsungef15e482022-01-26 11:32:49 -0800480 SHADER_MODULE_STATE::SetPushConstantUsedInShader(module_state, entry_points);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600481 return entry_points;
sfricke-samsung962cad92021-04-13 00:46:29 -0700482}
483
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600484void SHADER_MODULE_STATE::PreprocessShaderBinary(const spv_target_env env) {
485 if (static_data_.has_group_decoration) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700486 spvtools::Optimizer optimizer(env);
487 optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass());
488 std::vector<uint32_t> optimized_binary;
489 // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600490 auto result = optimizer.Run(words.data(), words.size(), &optimized_binary, spvtools::ValidatorOptions(), true);
491
sfricke-samsung962cad92021-04-13 00:46:29 -0700492 if (result) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600493 // NOTE: We need to update words with the result from the spirv-tools optimizer.
494 // **THIS ONLY HAPPENS ON INITIALIZATION**. words should remain const for the lifetime
495 // of the SHADER_MODULE_STATE instance.
496 *const_cast<std::vector<uint32_t> *>(&words) = std::move(optimized_binary);
sjfrickededec242022-08-25 15:18:38 +0900497 // Will need to update static data now the words have changed or else the def_index will not align
498 // It is really rare this will get here as Group Decorations have been deprecated and before this was added no one ever
499 // raised an issue for a bug that would crash the layers that was around for many releases
500 *const_cast<SpirvStaticData *>(&static_data_) = SpirvStaticData(*this);
sfricke-samsung962cad92021-04-13 00:46:29 -0700501 }
502 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700503}
504
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800505void SHADER_MODULE_STATE::DescribeTypeInner(std::ostringstream &ss, uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700506 auto insn = get_def(type);
507 assert(insn != end());
508
509 switch (insn.opcode()) {
510 case spv::OpTypeBool:
511 ss << "bool";
512 break;
513 case spv::OpTypeInt:
514 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
515 break;
516 case spv::OpTypeFloat:
517 ss << "float" << insn.word(2);
518 break;
519 case spv::OpTypeVector:
520 ss << "vec" << insn.word(3) << " of ";
521 DescribeTypeInner(ss, insn.word(2));
522 break;
523 case spv::OpTypeMatrix:
524 ss << "mat" << insn.word(3) << " of ";
525 DescribeTypeInner(ss, insn.word(2));
526 break;
527 case spv::OpTypeArray:
528 ss << "arr[" << GetConstantValueById(insn.word(3)) << "] of ";
529 DescribeTypeInner(ss, insn.word(2));
530 break;
531 case spv::OpTypeRuntimeArray:
532 ss << "runtime arr[] of ";
533 DescribeTypeInner(ss, insn.word(2));
534 break;
535 case spv::OpTypePointer:
sjfricke657dfdc2022-08-25 23:40:32 +0900536 ss << "ptr to " << string_SpvStorageClass(insn.word(2)) << " ";
sfricke-samsung962cad92021-04-13 00:46:29 -0700537 DescribeTypeInner(ss, insn.word(3));
538 break;
539 case spv::OpTypeStruct: {
540 ss << "struct of (";
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800541 for (uint32_t i = 2; i < insn.len(); i++) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700542 DescribeTypeInner(ss, insn.word(i));
543 if (i == insn.len() - 1) {
544 ss << ")";
545 } else {
546 ss << ", ";
547 }
548 }
549 break;
550 }
551 case spv::OpTypeSampler:
552 ss << "sampler";
553 break;
554 case spv::OpTypeSampledImage:
555 ss << "sampler+";
556 DescribeTypeInner(ss, insn.word(2));
557 break;
558 case spv::OpTypeImage:
559 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
560 break;
561 case spv::OpTypeAccelerationStructureNV:
562 ss << "accelerationStruture";
563 break;
564 default:
565 ss << "oddtype";
566 break;
567 }
568}
569
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800570std::string SHADER_MODULE_STATE::DescribeType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700571 std::ostringstream ss;
572 DescribeTypeInner(ss, type);
573 return ss.str();
574}
575
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800576std::string SHADER_MODULE_STATE::DescribeInstruction(const spirv_inst_iter &insn) const {
577 std::ostringstream ss;
578 const uint32_t opcode = insn.opcode();
579 uint32_t operand_offset = 1; // where to start printing operands
580 // common disassembled for SPIR-V is
581 // %result = Opcode %result_type %operands
582 if (OpcodeHasResult(opcode)) {
583 operand_offset++;
584 ss << "%" << (OpcodeHasType(opcode) ? insn.word(2) : insn.word(1)) << " = ";
585 }
586
587 ss << string_SpvOpcode(opcode);
588
589 if (OpcodeHasType(opcode)) {
590 operand_offset++;
591 ss << " %" << insn.word(1);
592 }
593
sfricke-samsunged00aa42022-01-27 19:03:01 -0800594 // TODO - For now don't list the '%' for any operands since they are only for reference IDs. Without generating a table of each
595 // instructions operand types and covering the many edge cases (such as optional, paired, or variable operands) this is the
596 // simplest way to print the instruction and give the developer something to look into when an error occurs.
597 //
598 // For now this safely should be able to assume it will never come across a LiteralString such as in OpExtInstImport or
599 // OpEntryPoint
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800600 for (uint32_t i = operand_offset; i < insn.len(); i++) {
sfricke-samsunged00aa42022-01-27 19:03:01 -0800601 ss << " " << insn.word(i);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800602 }
603 return ss.str();
604}
605
sfricke-samsung962cad92021-04-13 00:46:29 -0700606const SHADER_MODULE_STATE::EntryPoint *SHADER_MODULE_STATE::FindEntrypointStruct(char const *name,
607 VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600608 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700609 for (auto it = range.first; it != range.second; ++it) {
610 if (it->second.stage == stageBits) {
611 return &(it->second);
612 }
613 }
614 return nullptr;
615}
616
617spirv_inst_iter SHADER_MODULE_STATE::FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600618 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700619 for (auto it = range.first; it != range.second; ++it) {
620 if (it->second.stage == stageBits) {
621 return at(it->second.offset);
622 }
623 }
624 return end();
625}
626
627// Because the following is legal, need the entry point
628// OpEntryPoint GLCompute %main "name_a"
629// OpEntryPoint GLCompute %main "name_b"
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800630// Assumes shader module contains no spec constants used to set the local size values
sfricke-samsung962cad92021-04-13 00:46:29 -0700631bool SHADER_MODULE_STATE::FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y,
632 uint32_t &local_size_z) const {
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800633 // "If an object is decorated with the WorkgroupSize decoration, this takes precedence over any LocalSize or LocalSizeId
634 // execution mode."
635 for (const auto &builtin : static_data_.builtin_decoration_list) {
636 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
637 const uint32_t workgroup_size_id = at(builtin.offset).word(1);
638 auto composite_def = get_def(workgroup_size_id);
639 if (composite_def.opcode() == spv::OpConstantComposite) {
640 // VUID-WorkgroupSize-WorkgroupSize-04427 makes sure this is a OpTypeVector of int32
sjfricke3b0cb102022-08-10 16:27:45 +0900641 local_size_x = GetConstantValueById(composite_def.word(3));
642 local_size_y = GetConstantValueById(composite_def.word(4));
643 local_size_z = GetConstantValueById(composite_def.word(5));
sfricke-samsung962cad92021-04-13 00:46:29 -0700644 return true;
645 }
646 }
647 }
sfricke-samsung61d50ec2022-02-13 17:01:25 -0800648
649 auto entrypoint_id = entrypoint.word(2);
650 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
651 if (it != static_data_.execution_mode_inst.end()) {
652 for (auto insn : it->second) {
653 if (insn.opcode() == spv::OpExecutionMode && insn.word(2) == spv::ExecutionModeLocalSize) {
654 local_size_x = insn.word(3);
655 local_size_y = insn.word(4);
656 local_size_z = insn.word(5);
657 return true;
658 } else if (insn.opcode() == spv::OpExecutionModeId && insn.word(2) == spv::ExecutionModeLocalSizeId) {
659 local_size_x = GetConstantValueById(insn.word(3));
660 local_size_y = GetConstantValueById(insn.word(4));
661 local_size_z = GetConstantValueById(insn.word(5));
662 return true;
663 }
664 }
665 }
666 return false; // not found
sfricke-samsung962cad92021-04-13 00:46:29 -0700667}
668
669// If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction.
670// Otherwise, returns src->end().
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800671spirv_inst_iter SHADER_MODULE_STATE::GetConstantDef(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700672 auto value = get_def(id);
673
674 // If id is a copy, see where it was copied from
675 if ((end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) {
676 id = value.word(3);
677 value = get_def(id);
678 }
679
680 if ((end() != value) && (value.opcode() == spv::OpConstant)) {
681 return value;
682 }
683 return end();
684}
685
sjfricke3b0cb102022-08-10 16:27:45 +0900686// While simple, function name provides a more human readable description why word(3) is used
687uint32_t SHADER_MODULE_STATE::GetConstantValue(const spirv_inst_iter &itr) const {
sjfricke284a13f2022-08-16 15:34:31 +0900688 // This should be a OpConstant (not a OpSpecConstant), if this asserts then 2 things are happening
689 // 1. This function is being used where we don't actually know it is a constant and is a bug in the validation layers
690 // 2. The CreateFoldSpecConstantOpAndCompositePass didn't fully fold everything and is a bug in spirv-opt
sjfricke3b0cb102022-08-10 16:27:45 +0900691 assert(itr.opcode() == spv::OpConstant);
692 return itr.word(3);
693}
694
sfricke-samsung962cad92021-04-13 00:46:29 -0700695// Either returns the constant value described by the instruction at id, or 1
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800696uint32_t SHADER_MODULE_STATE::GetConstantValueById(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700697 auto value = GetConstantDef(id);
698
699 if (end() == value) {
700 // TODO: Either ensure that the specialization transform is already performed on a module we're
701 // considering here, OR -- specialize on the fly now.
702 return 1;
703 }
sjfricke3b0cb102022-08-10 16:27:45 +0900704
sfricke-samsung962cad92021-04-13 00:46:29 -0700705 return GetConstantValue(value);
706}
707
708// Returns an int32_t corresponding to the spv::Dim of the given resource, when positive, and corresponding to an unknown type, when
709// negative.
710int32_t SHADER_MODULE_STATE::GetShaderResourceDimensionality(const interface_var &resource) const {
711 auto type = get_def(resource.type_id);
712 while (true) {
713 switch (type.opcode()) {
714 case spv::OpTypeSampledImage:
715 type = get_def(type.word(2));
716 break;
717 case spv::OpTypePointer:
718 type = get_def(type.word(3));
719 break;
720 case spv::OpTypeImage:
721 return type.word(3);
722 default:
723 return -1;
724 }
725 }
726}
727
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800728uint32_t SHADER_MODULE_STATE::GetLocationsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700729 auto insn = get_def(type);
730 assert(insn != end());
731
732 switch (insn.opcode()) {
733 case spv::OpTypePointer:
734 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
735 // pointers around.
736 return GetLocationsConsumedByType(insn.word(3), strip_array_level);
737 case spv::OpTypeArray:
738 if (strip_array_level) {
739 return GetLocationsConsumedByType(insn.word(2), false);
740 } else {
741 return GetConstantValueById(insn.word(3)) * GetLocationsConsumedByType(insn.word(2), false);
742 }
743 case spv::OpTypeMatrix:
744 // Num locations is the dimension * element size
745 return insn.word(3) * GetLocationsConsumedByType(insn.word(2), false);
746 case spv::OpTypeVector: {
747 auto scalar_type = get_def(insn.word(2));
748 auto bit_width =
749 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
750
751 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
752 return (bit_width * insn.word(3) + 127) / 128;
753 }
754 default:
755 // Everything else is just 1.
756 return 1;
757
758 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
759 }
760}
761
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800762uint32_t SHADER_MODULE_STATE::GetComponentsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700763 auto insn = get_def(type);
764 assert(insn != end());
765
766 switch (insn.opcode()) {
767 case spv::OpTypePointer:
768 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
769 // pointers around.
770 return GetComponentsConsumedByType(insn.word(3), strip_array_level);
771 case spv::OpTypeStruct: {
772 uint32_t sum = 0;
773 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
774 sum += GetComponentsConsumedByType(insn.word(i), false);
775 }
776 return sum;
777 }
778 case spv::OpTypeArray:
779 if (strip_array_level) {
780 return GetComponentsConsumedByType(insn.word(2), false);
781 } else {
782 return GetConstantValueById(insn.word(3)) * GetComponentsConsumedByType(insn.word(2), false);
783 }
784 case spv::OpTypeMatrix:
785 // Num locations is the dimension * element size
786 return insn.word(3) * GetComponentsConsumedByType(insn.word(2), false);
787 case spv::OpTypeVector: {
788 auto scalar_type = get_def(insn.word(2));
789 auto bit_width =
790 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
791 // One component is 32-bit
792 return (bit_width * insn.word(3) + 31) / 32;
793 }
794 case spv::OpTypeFloat: {
795 auto bit_width = insn.word(2);
796 return (bit_width + 31) / 32;
797 }
798 case spv::OpTypeInt: {
799 auto bit_width = insn.word(2);
800 return (bit_width + 31) / 32;
801 }
802 case spv::OpConstant:
803 return GetComponentsConsumedByType(insn.word(1), false);
804 default:
805 return 0;
806 }
807}
808
809// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
810// also used for input attachments, as we statically know their format.
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800811uint32_t SHADER_MODULE_STATE::GetFundamentalType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700812 auto insn = get_def(type);
813 assert(insn != end());
814
815 switch (insn.opcode()) {
816 case spv::OpTypeInt:
817 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
818 case spv::OpTypeFloat:
819 return FORMAT_TYPE_FLOAT;
820 case spv::OpTypeVector:
821 case spv::OpTypeMatrix:
822 case spv::OpTypeArray:
823 case spv::OpTypeRuntimeArray:
824 case spv::OpTypeImage:
825 return GetFundamentalType(insn.word(2));
826 case spv::OpTypePointer:
827 return GetFundamentalType(insn.word(3));
828
829 default:
830 return 0;
831 }
832}
833
834spirv_inst_iter SHADER_MODULE_STATE::GetStructType(spirv_inst_iter def, bool is_array_of_verts) const {
835 while (true) {
836 if (def.opcode() == spv::OpTypePointer) {
837 def = get_def(def.word(3));
838 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
839 def = get_def(def.word(2));
840 is_array_of_verts = false;
841 } else if (def.opcode() == spv::OpTypeStruct) {
842 return def;
843 } else {
844 return end();
845 }
846 }
847}
848
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800849void SHADER_MODULE_STATE::DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &member_decorate_offsets,
sfricke-samsung962cad92021-04-13 00:46:29 -0700850 shader_struct_member &data) const {
851 const auto struct_it = GetStructType(it, false);
852 assert(struct_it != end());
853 data.size = 0;
854
855 shader_struct_member data1;
856 uint32_t i = 2;
857 uint32_t local_offset = 0;
858 std::vector<uint32_t> offsets;
859 offsets.resize(struct_it.len() - i);
860
861 // 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 -0800862 for (const auto offset : member_decorate_offsets) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700863 const auto member_decorate = at(offset);
864 if (member_decorate.word(1) != struct_it.word(1)) {
865 continue;
866 }
867
868 offsets[member_decorate.word(2)] = member_decorate.word(4);
869 }
870
871 for (const auto offset : offsets) {
872 local_offset = offset;
873 data1 = {};
874 data1.root = data.root;
875 data1.offset = local_offset;
876 auto def_member = get_def(struct_it.word(i));
877
878 // Array could be multi-dimensional
879 while (def_member.opcode() == spv::OpTypeArray) {
880 const auto len_id = def_member.word(3);
881 const auto def_len = get_def(len_id);
882 data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length
883 def_member = get_def(def_member.word(2));
884 }
885
886 if (def_member.opcode() == spv::OpTypeStruct) {
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800887 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700888 } else if (def_member.opcode() == spv::OpTypePointer) {
889 if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) {
890 // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address
891 // that "points to something."
892 data1.size = 8;
893 } else {
894 // 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 -0800895 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700896 }
897 } else {
898 if (def_member.opcode() == spv::OpTypeMatrix) {
899 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector.
900 def_member = get_def(def_member.word(2));
901 }
902
903 if (def_member.opcode() == spv::OpTypeVector) {
904 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length
905 def_member = get_def(def_member.word(2));
906 }
907
908 // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte.
909 data1.size = (def_member.word(2) / 8);
910 }
911 const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size();
912 if (array_length_hierarchy_szie > 0) {
913 data1.array_block_size.resize(array_length_hierarchy_szie, 1);
914
915 for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) {
916 data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2];
917 }
918 }
919 data.struct_members.emplace_back(data1);
920 ++i;
921 }
922 uint32_t total_array_length = 1;
923 for (const auto length : data1.array_length_hierarchy) {
924 total_array_length *= length;
925 }
926 data.size = local_offset + data1.size * total_array_length;
927}
928
929static uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
930 int array_indices_size = static_cast<int>(array_indices.size());
931 if (array_indices_size) {
932 uint32_t array_index = 0;
933 uint32_t i = 0;
934 for (const auto index : array_indices) {
935 array_index += (data.array_block_size[i] * index);
936 ++i;
937 }
938 offset += (array_index * data.size);
939 }
940 return offset;
941}
942
943static void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
944 int array_indices_size = static_cast<int>(array_indices.size());
945 uint32_t block_memory_size = data.size;
946 for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) {
947 block_memory_size *= data.array_length_hierarchy[i];
948 }
949
950 offset = UpdateOffset(offset, array_indices, data);
951
952 uint32_t end = offset + block_memory_size;
953 auto used_bytes = data.GetUsedbytes();
954 if (used_bytes->size() < end) {
955 used_bytes->resize(end, 0);
956 }
957 std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size));
958}
959
960void SHADER_MODULE_STATE::RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index,
961 spirv_inst_iter &access_chain_it, const shader_struct_member &data) const {
962 if (access_chain_word_index < access_chain_it.len()) {
963 if (data.array_length_hierarchy.size() > array_indices.size()) {
964 auto def_it = get_def(access_chain_it.word(access_chain_word_index));
965 ++access_chain_word_index;
966
967 if (def_it != end() && def_it.opcode() == spv::OpConstant) {
968 array_indices.emplace_back(def_it.word(3));
969 RunUsedArray(offset, array_indices, access_chain_word_index, access_chain_it, data);
970 } else {
971 // If it is a variable, set the all array is used.
972 if (access_chain_word_index < access_chain_it.len()) {
973 uint32_t array_length = data.array_length_hierarchy[array_indices.size()];
974 for (uint32_t i = 0; i < array_length; ++i) {
975 auto array_indices2 = array_indices;
976 array_indices2.emplace_back(i);
977 RunUsedArray(offset, array_indices2, access_chain_word_index, access_chain_it, data);
978 }
979 } else {
980 SetUsedBytes(offset, array_indices, data);
981 }
982 }
983 } else {
984 offset = UpdateOffset(offset, array_indices, data);
985 RunUsedStruct(offset, access_chain_word_index, access_chain_it, data);
986 }
987 } else {
988 SetUsedBytes(offset, array_indices, data);
989 }
990}
991
992void SHADER_MODULE_STATE::RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it,
993 const shader_struct_member &data) const {
994 std::vector<uint32_t> array_indices_emptry;
995
996 if (access_chain_word_index < access_chain_it.len()) {
997 auto strcut_member_index = GetConstantValueById(access_chain_it.word(access_chain_word_index));
998 ++access_chain_word_index;
999
1000 auto data1 = data.struct_members[strcut_member_index];
1001 RunUsedArray(offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1);
1002 }
1003}
1004
1005void SHADER_MODULE_STATE::SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list,
1006 const shader_struct_member &data) const {
1007 for (const auto &func_set : function_set_list) {
1008 auto range = func_set.op_lists.equal_range(spv::OpAccessChain);
1009 for (auto it = range.first; it != range.second; ++it) {
1010 auto access_chain = at(it->second);
1011 if (access_chain.word(3) == variable_id) {
1012 RunUsedStruct(0, 4, access_chain, data);
1013 }
1014 }
1015 }
1016}
1017
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001018// static
1019void SHADER_MODULE_STATE::SetPushConstantUsedInShader(
sfricke-samsungef15e482022-01-26 11:32:49 -08001020 const SHADER_MODULE_STATE &module_state, std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001021 for (auto &entrypoint : entry_points) {
1022 auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable);
1023 for (auto it = range.first; it != range.second; ++it) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001024 const auto def_insn = module_state.at(it->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001025
1026 if (def_insn.word(3) == spv::StorageClassPushConstant) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001027 spirv_inst_iter type = module_state.get_def(def_insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -07001028 const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate);
1029 std::vector<uint32_t> offsets;
1030
1031 for (auto it2 = range2.first; it2 != range2.second; ++it2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001032 auto member_decorate = module_state.at(it2->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001033 if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) {
1034 offsets.emplace_back(member_decorate.offset());
1035 }
1036 }
1037 entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader;
sfricke-samsungef15e482022-01-26 11:32:49 -08001038 module_state.DefineStructMember(type, offsets, entrypoint.second.push_constant_used_in_shader);
1039 module_state.SetUsedStructMember(def_insn.word(2), entrypoint.second.function_set_list,
1040 entrypoint.second.push_constant_used_in_shader);
sfricke-samsung962cad92021-04-13 00:46:29 -07001041 }
1042 }
1043 }
1044}
1045
1046uint32_t SHADER_MODULE_STATE::DescriptorTypeToReqs(uint32_t type_id) const {
1047 auto type = get_def(type_id);
1048
1049 while (true) {
1050 switch (type.opcode()) {
1051 case spv::OpTypeArray:
1052 case spv::OpTypeRuntimeArray:
1053 case spv::OpTypeSampledImage:
1054 type = get_def(type.word(2));
1055 break;
1056 case spv::OpTypePointer:
1057 type = get_def(type.word(3));
1058 break;
1059 case spv::OpTypeImage: {
1060 auto dim = type.word(3);
1061 auto arrayed = type.word(5);
1062 auto msaa = type.word(6);
1063
1064 uint32_t bits = 0;
1065 switch (GetFundamentalType(type.word(2))) {
1066 case FORMAT_TYPE_FLOAT:
1067 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
1068 break;
1069 case FORMAT_TYPE_UINT:
1070 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
1071 break;
1072 case FORMAT_TYPE_SINT:
1073 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
1074 break;
1075 default:
1076 break;
1077 }
1078
1079 switch (dim) {
1080 case spv::Dim1D:
1081 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1082 return bits;
1083 case spv::Dim2D:
1084 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1085 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
1086 return bits;
1087 case spv::Dim3D:
1088 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
1089 return bits;
1090 case spv::DimCube:
1091 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1092 return bits;
1093 case spv::DimSubpassData:
1094 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1095 return bits;
1096 default: // buffer, etc.
1097 return bits;
1098 }
1099 }
1100 default:
1101 return 0;
1102 }
1103 }
1104}
1105
1106// For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to.
1107// This function examines instructions in the static call tree for a write to this variable.
1108bool SHADER_MODULE_STATE::IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const {
1109 auto type = builtin_instr.opcode();
1110 uint32_t target_id = builtin_instr.word(1);
1111 bool init_complete = false;
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001112 uint32_t target_member_offset = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001113
1114 if (type == spv::OpMemberDecorate) {
1115 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1116 auto insn = entrypoint;
1117 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1118 switch (insn.opcode()) {
1119 case spv::OpTypePointer:
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001120 if (insn.word(2) == spv::StorageClassOutput) {
1121 const auto type_id = insn.word(3);
1122 if (type_id == target_id) {
1123 target_id = insn.word(1);
1124 } else {
1125 // If the output is an array, check if the element type is what we're looking for
1126 const auto type_insn = get_def(type_id);
1127 if ((type_insn.opcode() == spv::OpTypeArray) && (type_insn.word(2) == target_id)) {
1128 target_id = insn.word(1);
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001129 target_member_offset = 1;
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001130 }
1131 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001132 }
1133 break;
1134 case spv::OpVariable:
1135 if (insn.word(1) == target_id) {
1136 target_id = insn.word(2);
1137 init_complete = true;
1138 }
1139 break;
1140 }
1141 insn++;
1142 }
1143 }
1144
1145 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1146
1147 bool found_write = false;
1148 layer_data::unordered_set<uint32_t> worklist;
1149 worklist.insert(entrypoint.word(2));
1150
1151 // Follow instructions in call graph looking for writes to target
1152 while (!worklist.empty() && !found_write) {
1153 auto id_iter = worklist.begin();
1154 auto id = *id_iter;
1155 worklist.erase(id_iter);
1156
1157 auto insn = get_def(id);
1158 if (insn == end()) {
1159 continue;
1160 }
1161
1162 if (insn.opcode() == spv::OpFunction) {
1163 // Scan body of function looking for other function calls or items in our ID chain
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001164 while (++insn, (insn.opcode() != spv::OpFunctionEnd) && !found_write) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001165 switch (insn.opcode()) {
1166 case spv::OpAccessChain:
sjfrickeeaa434d2022-06-22 04:55:28 +09001167 case spv::OpInBoundsAccessChain:
sfricke-samsung962cad92021-04-13 00:46:29 -07001168 if (insn.word(3) == target_id) {
1169 if (type == spv::OpMemberDecorate) {
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001170 // Get the target member of the struct
1171 // NOTE: this will only work for structs and arrays of structs. Deeper levels of nesting (e.g.,
1172 // arrays of structs of structs) is not currently supported.
1173 const auto value_itr = GetConstantDef(insn.word(4 + target_member_offset));
1174 if (value_itr != end()) {
1175 auto value = GetConstantValue(value_itr);
1176 if (value == builtin_instr.word(2)) {
1177 target_id = insn.word(2);
1178 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001179 }
1180 } else {
1181 target_id = insn.word(2);
1182 }
1183 }
1184 break;
1185 case spv::OpStore:
1186 if (insn.word(1) == target_id) {
1187 found_write = true;
1188 }
1189 break;
1190 case spv::OpFunctionCall:
1191 worklist.insert(insn.word(3));
1192 break;
1193 }
1194 }
1195 }
1196 }
1197 return found_write;
1198}
1199
1200// Used by the collection functions to help aid in state tracking
1201struct shader_module_used_operators {
1202 bool updated;
sjfrickec53a4cb2022-09-12 16:55:28 +09001203 std::vector<uint32_t> image_read_load_ids;
1204 std::vector<uint32_t> image_write_load_ids;
1205 std::vector<uint32_t> atomic_pointer_ids;
1206 std::vector<uint32_t> store_pointer_ids;
1207 std::vector<uint32_t> atomic_store_pointer_ids;
1208 std::vector<uint32_t> sampler_implicitLod_dref_proj_load_ids;
1209 std::vector<uint32_t> sampler_bias_offset_load_ids;
1210 std::vector<uint32_t> image_dref_load_ids;
1211 std::vector<std::pair<uint32_t, uint32_t>> sampled_image_load_ids; // <image, sampler>
1212 layer_data::unordered_map<uint32_t, uint32_t> load_members; // <result id, pointer>
1213 layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> accesschain_members; // <result id, <base,index[0]>>
1214 layer_data::unordered_map<uint32_t, uint32_t> image_texel_pointer_members; // <result id, image>
sfricke-samsung962cad92021-04-13 00:46:29 -07001215
1216 shader_module_used_operators() : updated(false) {}
1217
1218 bool CheckImageOperandsBiasOffset(uint32_t type) {
1219 return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask |
1220 spv::ImageOperandsConstOffsetsMask)
1221 ? true
1222 : false;
1223 }
1224
sfricke-samsungef15e482022-01-26 11:32:49 -08001225 void update(SHADER_MODULE_STATE const *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001226 if (updated) return;
1227 updated = true;
1228
sfricke-samsungef15e482022-01-26 11:32:49 -08001229 for (auto insn : *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001230 switch (insn.opcode()) {
1231 case spv::OpImageSampleImplicitLod:
1232 case spv::OpImageSampleProjImplicitLod:
1233 case spv::OpImageSampleProjExplicitLod:
1234 case spv::OpImageSparseSampleImplicitLod:
1235 case spv::OpImageSparseSampleProjImplicitLod:
1236 case spv::OpImageSparseSampleProjExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001237 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001238 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001239 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
sjfrickec53a4cb2022-09-12 16:55:28 +09001240 sampler_implicitLod_dref_proj_load_ids.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001241 // ImageOperands in index: 5
1242 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sjfrickec53a4cb2022-09-12 16:55:28 +09001243 sampler_bias_offset_load_ids.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001244 }
1245 break;
1246 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001247 case spv::OpImageDrefGather:
1248 case spv::OpImageSparseDrefGather: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001249 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001250 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001251 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
sjfrickec53a4cb2022-09-12 16:55:28 +09001252 image_dref_load_ids.emplace_back(load_id);
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001253 break;
1254 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001255 case spv::OpImageSampleDrefImplicitLod:
1256 case spv::OpImageSampleDrefExplicitLod:
1257 case spv::OpImageSampleProjDrefImplicitLod:
1258 case spv::OpImageSampleProjDrefExplicitLod:
1259 case spv::OpImageSparseSampleDrefImplicitLod:
1260 case spv::OpImageSparseSampleDrefExplicitLod:
1261 case spv::OpImageSparseSampleProjDrefImplicitLod:
1262 case spv::OpImageSparseSampleProjDrefExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001263 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001264 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001265 auto sampler_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1266 auto image_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1267
sjfrickec53a4cb2022-09-12 16:55:28 +09001268 image_dref_load_ids.emplace_back(image_load_id);
1269 sampler_implicitLod_dref_proj_load_ids.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001270 // ImageOperands in index: 6
1271 if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) {
sjfrickec53a4cb2022-09-12 16:55:28 +09001272 sampler_bias_offset_load_ids.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001273 }
1274 break;
1275 }
1276 case spv::OpImageSampleExplicitLod:
1277 case spv::OpImageSparseSampleExplicitLod: {
1278 // ImageOperands in index: 5
1279 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001280 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001281 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001282 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
sjfrickec53a4cb2022-09-12 16:55:28 +09001283 sampler_bias_offset_load_ids.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001284 }
1285 break;
1286 }
1287 case spv::OpStore: {
sjfrickec53a4cb2022-09-12 16:55:28 +09001288 store_pointer_ids.emplace_back(insn.word(1)); // object id or AccessChain id
sfricke-samsung962cad92021-04-13 00:46:29 -07001289 break;
1290 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001291 case spv::OpImageRead:
1292 case spv::OpImageSparseRead: {
sjfrickec53a4cb2022-09-12 16:55:28 +09001293 image_read_load_ids.emplace_back(insn.word(3));
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001294 break;
1295 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001296 case spv::OpImageWrite: {
sjfrickec53a4cb2022-09-12 16:55:28 +09001297 image_write_load_ids.emplace_back(insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -07001298 break;
1299 }
1300 case spv::OpSampledImage: {
1301 // 3: image load id, 4: sampler load id
sjfrickec53a4cb2022-09-12 16:55:28 +09001302 sampled_image_load_ids.emplace_back(std::pair<uint32_t, uint32_t>(insn.word(3), insn.word(4)));
sfricke-samsung962cad92021-04-13 00:46:29 -07001303 break;
1304 }
1305 case spv::OpLoad: {
1306 // 2: Load id, 3: object id or AccessChain id
1307 load_members.emplace(insn.word(2), insn.word(3));
1308 break;
1309 }
sjfrickeeaa434d2022-06-22 04:55:28 +09001310 case spv::OpAccessChain:
1311 case spv::OpInBoundsAccessChain: {
sfricke-samsung962cad92021-04-13 00:46:29 -07001312 if (insn.len() == 4) {
1313 // If it is for struct, the length is only 4.
1314 // 2: AccessChain id, 3: object id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001315 accesschain_members.emplace(insn.word(2), std::pair<uint32_t, uint32_t>(insn.word(3), 0));
sfricke-samsung962cad92021-04-13 00:46:29 -07001316 } else {
1317 // 2: AccessChain id, 3: object id, 4: object id of array index
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001318 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 -07001319 }
1320 break;
1321 }
1322 case spv::OpImageTexelPointer: {
1323 // 2: ImageTexelPointer id, 3: object id
1324 image_texel_pointer_members.emplace(insn.word(2), insn.word(3));
1325 break;
1326 }
1327 default: {
1328 if (AtomicOperation(insn.opcode())) {
1329 if (insn.opcode() == spv::OpAtomicStore) {
sjfrickec53a4cb2022-09-12 16:55:28 +09001330 atomic_store_pointer_ids.emplace_back(insn.word(1));
1331 atomic_pointer_ids.emplace_back(insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -07001332 } else {
sjfrickec53a4cb2022-09-12 16:55:28 +09001333 atomic_pointer_ids.emplace_back(insn.word(3));
sfricke-samsung962cad92021-04-13 00:46:29 -07001334 }
1335 }
1336 break;
1337 }
1338 }
1339 }
1340 }
1341};
1342
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001343static bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<uint32_t> &operator_members,
1344 const layer_data::unordered_map<uint32_t, uint32_t> &load_members,
1345 const layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> &accesschain_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001346 for (auto load_id : operator_members) {
1347 if (object_id == load_id) return true;
1348 auto load_it = load_members.find(load_id);
1349 if (load_it == load_members.end()) {
1350 continue;
1351 }
1352 if (load_it->second == object_id) {
1353 return true;
1354 }
1355
1356 auto accesschain_it = accesschain_members.find(load_it->second);
1357 if (accesschain_it == accesschain_members.end()) {
1358 continue;
1359 }
1360 if (accesschain_it->second.first == object_id) {
1361 return true;
1362 }
1363 }
1364 return false;
1365}
1366
1367// Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image
1368// atomic operation, matching images to samplers, etc
1369void SHADER_MODULE_STATE::IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable,
sjfrickeeaa434d2022-06-22 04:55:28 +09001370 interface_var &out_interface_var) const {
1371 shader_module_used_operators used_operators;
sfricke-samsung962cad92021-04-13 00:46:29 -07001372 uint32_t type_id = id_it.word(1);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001373 uint32_t id = id_it.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001374
1375 auto type = get_def(type_id);
1376
1377 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
1378 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray ||
1379 type.opcode() == spv::OpTypeSampledImage) {
1380 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray ||
1381 type.opcode() == spv::OpTypeSampledImage) {
1382 type = get_def(type.word(2)); // Element type
1383 } else {
1384 type = get_def(type.word(3)); // Pointer type
1385 }
1386 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001387
sfricke-samsung962cad92021-04-13 00:46:29 -07001388 switch (type.opcode()) {
1389 case spv::OpTypeImage: {
1390 auto dim = type.word(3);
1391 if (dim != spv::DimSubpassData) {
1392 used_operators.update(this);
1393
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001394 // Sampled == 2 indicates used without a sampler (a storage image)
1395 bool is_image_without_format = false;
1396 if (type.word(7) == 2) is_image_without_format = type.word(8) == spv::ImageFormatUnknown;
1397
sjfrickec53a4cb2022-09-12 16:55:28 +09001398 if (CheckObjectIDFromOpLoad(id, used_operators.image_write_load_ids, used_operators.load_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001399 used_operators.accesschain_members)) {
1400 out_interface_var.is_writable = true;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001401 if (is_image_without_format) out_interface_var.is_write_without_format = true;
1402 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001403 if (CheckObjectIDFromOpLoad(id, used_operators.image_read_load_ids, used_operators.load_members,
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001404 used_operators.accesschain_members)) {
1405 out_interface_var.is_readable = true;
1406 if (is_image_without_format) out_interface_var.is_read_without_format = true;
sfricke-samsung962cad92021-04-13 00:46:29 -07001407 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001408 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_load_ids, used_operators.load_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001409 used_operators.accesschain_members)) {
1410 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1411 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001412 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_load_ids, used_operators.load_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001413 used_operators.accesschain_members)) {
1414 out_interface_var.is_sampler_bias_offset = true;
1415 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001416 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_pointer_ids, used_operators.image_texel_pointer_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001417 used_operators.accesschain_members)) {
1418 out_interface_var.is_atomic_operation = true;
1419 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001420 if (CheckObjectIDFromOpLoad(id, used_operators.image_dref_load_ids, used_operators.load_members,
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001421 used_operators.accesschain_members)) {
1422 out_interface_var.is_dref_operation = true;
1423 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001424
sjfrickec53a4cb2022-09-12 16:55:28 +09001425 for (auto &itp_id : used_operators.sampled_image_load_ids) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001426 // Find if image id match.
1427 uint32_t image_index = 0;
1428 auto load_it = used_operators.load_members.find(itp_id.first);
1429 if (load_it == used_operators.load_members.end()) {
1430 continue;
1431 } else {
1432 if (load_it->second != id) {
1433 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1434 if (accesschain_it == used_operators.accesschain_members.end()) {
1435 continue;
1436 } else {
1437 if (accesschain_it->second.first != id) {
1438 continue;
1439 }
1440
1441 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1442 if (const_itr == end()) {
1443 // access chain index not a constant, skip.
1444 break;
1445 }
1446 image_index = GetConstantValue(const_itr);
1447 }
1448 }
1449 }
1450 // Find sampler's set binding.
1451 load_it = used_operators.load_members.find(itp_id.second);
1452 if (load_it == used_operators.load_members.end()) {
1453 continue;
1454 } else {
1455 uint32_t sampler_id = load_it->second;
1456 uint32_t sampler_index = 0;
1457 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1458
1459 if (accesschain_it != used_operators.accesschain_members.end()) {
1460 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1461 if (const_itr == end()) {
1462 // access chain index representing sampler index is not a constant, skip.
1463 break;
1464 }
1465 sampler_id = const_itr.offset();
1466 sampler_index = GetConstantValue(const_itr);
1467 }
1468 auto sampler_dec = get_decorations(sampler_id);
1469 if (image_index >= out_interface_var.samplers_used_by_image.size()) {
1470 out_interface_var.samplers_used_by_image.resize(image_index + 1);
1471 }
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001472
1473 // Need to check again for these properties in case not using a combined image sampler
sjfrickec53a4cb2022-09-12 16:55:28 +09001474 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_implicitLod_dref_proj_load_ids,
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001475 used_operators.load_members, used_operators.accesschain_members)) {
1476 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1477 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001478 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_bias_offset_load_ids,
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001479 used_operators.load_members, used_operators.accesschain_members)) {
1480 out_interface_var.is_sampler_bias_offset = true;
1481 }
1482
sfricke-samsung962cad92021-04-13 00:46:29 -07001483 out_interface_var.samplers_used_by_image[image_index].emplace(
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001484 SamplerUsedByImage{DescriptorSlot{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index});
sfricke-samsung962cad92021-04-13 00:46:29 -07001485 }
1486 }
1487 }
1488 return;
1489 }
1490
1491 case spv::OpTypeStruct: {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001492 layer_data::unordered_set<uint32_t> nonwritable_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001493 if (get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001494 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001495 if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) {
1496 nonwritable_members.insert(insn.word(2));
1497 }
1498 }
1499
1500 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
1501 // as nonwritable.
1502 if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) {
1503 used_operators.update(this);
1504
sjfrickec53a4cb2022-09-12 16:55:28 +09001505 for (auto oid : used_operators.store_pointer_ids) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001506 if (id == oid) {
1507 out_interface_var.is_writable = true;
1508 return;
1509 }
1510 auto accesschain_it = used_operators.accesschain_members.find(oid);
1511 if (accesschain_it == used_operators.accesschain_members.end()) {
1512 continue;
1513 }
1514 if (accesschain_it->second.first == id) {
1515 out_interface_var.is_writable = true;
1516 return;
1517 }
1518 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001519 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_pointer_ids, used_operators.image_texel_pointer_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001520 used_operators.accesschain_members)) {
1521 out_interface_var.is_writable = true;
1522 return;
1523 }
1524 }
1525 }
1526 }
1527}
1528
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001529std::vector<std::pair<DescriptorSlot, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByDescriptorSlot(
ziga-lunargc2de4782022-04-14 19:49:07 +02001530 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001531 std::vector<std::pair<DescriptorSlot, interface_var>> out;
sfricke-samsung962cad92021-04-13 00:46:29 -07001532
ziga-lunargc2de4782022-04-14 19:49:07 +02001533 for (auto id : accessible_ids) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001534 auto insn = get_def(id);
1535 assert(insn != end());
1536
1537 if (insn.opcode() == spv::OpVariable &&
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001538 (insn.word(3) == spv::StorageClassUniform ||
1539 insn.word(3) == spv::StorageClassUniformConstant ||
sfricke-samsung962cad92021-04-13 00:46:29 -07001540 insn.word(3) == spv::StorageClassStorageBuffer)) {
1541 auto d = get_decorations(insn.word(2));
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001542 uint32_t set = d.descriptor_set;
1543 uint32_t binding = d.binding;
sfricke-samsung962cad92021-04-13 00:46:29 -07001544
1545 interface_var v = {};
1546 v.id = insn.word(2);
1547 v.type_id = insn.word(1);
1548
1549 IsSpecificDescriptorType(insn, insn.word(3) == spv::StorageClassStorageBuffer,
sjfrickeeaa434d2022-06-22 04:55:28 +09001550 !(d.flags & decoration_set::nonwritable_bit), v);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001551 out.emplace_back(DescriptorSlot{set, binding}, v);
sfricke-samsung962cad92021-04-13 00:46:29 -07001552 }
1553 }
1554
1555 return out;
1556}
1557
1558layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::CollectWritableOutputLocationinFS(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001559 const spirv_inst_iter &entrypoint) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001560 layer_data::unordered_set<uint32_t> location_list;
sfricke-samsung962cad92021-04-13 00:46:29 -07001561 const auto outputs = CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
sjfrickec53a4cb2022-09-12 16:55:28 +09001562 layer_data::unordered_set<uint32_t> store_pointer_ids;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001563 layer_data::unordered_map<uint32_t, uint32_t> accesschain_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001564
1565 for (auto insn : *this) {
1566 switch (insn.opcode()) {
1567 case spv::OpStore:
1568 case spv::OpAtomicStore: {
sjfrickec53a4cb2022-09-12 16:55:28 +09001569 store_pointer_ids.insert(insn.word(1)); // object id or AccessChain id
sfricke-samsung962cad92021-04-13 00:46:29 -07001570 break;
1571 }
sjfrickeeaa434d2022-06-22 04:55:28 +09001572 case spv::OpAccessChain:
1573 case spv::OpInBoundsAccessChain: {
sfricke-samsung962cad92021-04-13 00:46:29 -07001574 // 2: AccessChain id, 3: object id
1575 if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3));
1576 break;
1577 }
1578 default:
1579 break;
1580 }
1581 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001582 if (store_pointer_ids.empty()) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001583 return location_list;
1584 }
1585 for (auto output : outputs) {
sjfrickec53a4cb2022-09-12 16:55:28 +09001586 auto store_it = store_pointer_ids.find(output.second.id);
1587 if (store_it != store_pointer_ids.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001588 location_list.insert(output.first.first);
sjfrickec53a4cb2022-09-12 16:55:28 +09001589 store_pointer_ids.erase(store_it);
sfricke-samsung962cad92021-04-13 00:46:29 -07001590 continue;
1591 }
sjfrickec53a4cb2022-09-12 16:55:28 +09001592 store_it = store_pointer_ids.begin();
1593 while (store_it != store_pointer_ids.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001594 auto accesschain_it = accesschain_members.find(*store_it);
1595 if (accesschain_it == accesschain_members.end()) {
1596 ++store_it;
1597 continue;
1598 }
1599 if (accesschain_it->second == output.second.id) {
1600 location_list.insert(output.first.first);
sjfrickec53a4cb2022-09-12 16:55:28 +09001601 store_pointer_ids.erase(store_it);
sfricke-samsung962cad92021-04-13 00:46:29 -07001602 accesschain_members.erase(accesschain_it);
1603 break;
1604 }
1605 ++store_it;
1606 }
1607 }
1608 return location_list;
1609}
1610
1611bool SHADER_MODULE_STATE::CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts,
ziga-lunarg9e94e112021-09-27 00:21:10 +02001612 uint32_t id, uint32_t type_id, bool is_patch,
1613 uint32_t /*first_location*/) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001614 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
1615 auto type = GetStructType(get_def(type_id), is_array_of_verts && !is_patch);
1616 if (type == end() || !(get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
1617 // This isn't an interface block.
1618 return false;
1619 }
1620
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001621 layer_data::unordered_map<uint32_t, uint32_t> member_components;
1622 layer_data::unordered_map<uint32_t, uint32_t> member_relaxed_precision;
1623 layer_data::unordered_map<uint32_t, uint32_t> member_patch;
sfricke-samsung962cad92021-04-13 00:46:29 -07001624
1625 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001626 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001627 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001628 uint32_t member_index = insn.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001629
1630 if (insn.word(3) == spv::DecorationComponent) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001631 uint32_t component = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001632 member_components[member_index] = component;
1633 }
1634
1635 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
1636 member_relaxed_precision[member_index] = 1;
1637 }
1638
1639 if (insn.word(3) == spv::DecorationPatch) {
1640 member_patch[member_index] = 1;
1641 }
1642 }
1643 }
1644
1645 // TODO: correctly handle location assignment from outside
1646
1647 // Second pass -- produce the output, from Location decorations
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);
1651 uint32_t member_type_id = type.word(2 + member_index);
sfricke-samsung962cad92021-04-13 00:46:29 -07001652
1653 if (insn.word(3) == spv::DecorationLocation) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001654 uint32_t location = insn.word(4);
1655 uint32_t num_locations = GetLocationsConsumedByType(member_type_id, false);
sfricke-samsung962cad92021-04-13 00:46:29 -07001656 auto component_it = member_components.find(member_index);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001657 uint32_t component = component_it == member_components.end() ? 0 : component_it->second;
sfricke-samsung962cad92021-04-13 00:46:29 -07001658 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
1659 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
1660
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001661 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001662 interface_var v = {};
1663 v.id = id;
1664 // TODO: member index in interface_var too?
1665 v.type_id = member_type_id;
1666 v.offset = offset;
1667 v.is_patch = member_is_patch;
1668 v.is_block_member = true;
1669 v.is_relaxed_precision = is_relaxed_precision;
1670 (*out)[std::make_pair(location + offset, component)] = v;
1671 }
1672 }
1673 }
1674 }
1675
1676 return true;
1677}
1678
1679std::map<location_t, interface_var> SHADER_MODULE_STATE::CollectInterfaceByLocation(spirv_inst_iter entrypoint,
1680 spv::StorageClass sinterface,
1681 bool is_array_of_verts) const {
1682 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
1683
1684 std::map<location_t, interface_var> out;
1685
1686 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
1687 auto insn = get_def(iid);
1688 assert(insn != end());
1689 assert(insn.opcode() == spv::OpVariable);
1690
ziga-lunarg9e94e112021-09-27 00:21:10 +02001691 const auto d = get_decorations(iid);
1692 bool passthrough = sinterface == spv::StorageClassOutput && insn.word(3) == spv::StorageClassInput &&
1693 (d.flags & decoration_set::passthrough_bit) != 0;
1694 if (insn.word(3) == static_cast<uint32_t>(sinterface) || passthrough) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001695 uint32_t id = insn.word(2);
1696 uint32_t type = insn.word(1);
sfricke-samsung962cad92021-04-13 00:46:29 -07001697
ziga-lunarg9e94e112021-09-27 00:21:10 +02001698 auto location = d.location;
sfricke-samsung962cad92021-04-13 00:46:29 -07001699 int builtin = d.builtin;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001700 uint32_t component = d.component;
sfricke-samsung962cad92021-04-13 00:46:29 -07001701 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
1702 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001703 bool is_per_vertex = (d.flags & decoration_set::per_vertex_bit) != 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001704
1705 if (builtin != -1) {
1706 continue;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001707 } else if (!CollectInterfaceBlockMembers(&out, is_array_of_verts, id, type, is_patch, location) ||
1708 location != decoration_set::kInvalidValue) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001709 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
1710 // one result for each.
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001711 uint32_t num_locations = GetLocationsConsumedByType(type, (is_array_of_verts && !is_patch) || is_per_vertex);
1712 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001713 interface_var v = {};
1714 v.id = id;
1715 v.type_id = type;
1716 v.offset = offset;
1717 v.is_patch = is_patch;
1718 v.is_relaxed_precision = is_relaxed_precision;
1719 out[std::make_pair(location + offset, component)] = v;
1720 }
1721 }
1722 }
1723 }
1724
1725 return out;
1726}
1727
1728std::vector<uint32_t> SHADER_MODULE_STATE::CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001729 // Find all interface variables belonging to the entrypoint and matching the storage class
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001730 std::vector<uint32_t> variables;
sfricke-samsung962cad92021-04-13 00:46:29 -07001731 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
1732 auto def = get_def(id);
1733 assert(def != end());
1734 assert(def.opcode() == spv::OpVariable);
1735
1736 if (def.word(3) == storageClass) variables.push_back(def.word(1));
1737 }
1738
1739 // Find all members belonging to the builtin block selected
1740 std::vector<uint32_t> builtin_block_members;
1741 for (auto &var : variables) {
1742 auto def = get_def(get_def(var).word(3));
1743
1744 // It could be an array of IO blocks. The element type should be the struct defining the block contents
1745 if (def.opcode() == spv::OpTypeArray) def = get_def(def.word(2));
1746
1747 // Now find all members belonging to the struct defining the IO block
1748 if (def.opcode() == spv::OpTypeStruct) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001749 for (auto set : static_data_.builtin_decoration_list) {
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001750 auto insn = at(set.offset);
1751 if ((insn.opcode() == spv::OpMemberDecorate) && (def.word(1) == insn.word(1))) {
1752 // Start with undefined builtin for each struct member.
1753 // But only when confirmed the struct is the built-in inteface block (can only be one per shader)
1754 if (builtin_block_members.size() == 0) {
1755 builtin_block_members.resize(def.len() - 2, spv::BuiltInMax);
sfricke-samsung962cad92021-04-13 00:46:29 -07001756 }
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001757 auto struct_index = insn.word(2);
1758 assert(struct_index < builtin_block_members.size());
1759 builtin_block_members[struct_index] = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001760 }
1761 }
1762 }
1763 }
1764
1765 return builtin_block_members;
1766}
1767
1768std::vector<std::pair<uint32_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByInputAttachmentIndex(
1769 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
1770 std::vector<std::pair<uint32_t, interface_var>> out;
1771
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001772 for (auto insn : static_data_.decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001773 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
1774 auto attachment_index = insn.word(3);
1775 auto id = insn.word(1);
1776
1777 if (accessible_ids.count(id)) {
1778 auto def = get_def(id);
1779 assert(def != end());
1780 if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) {
1781 auto num_locations = GetLocationsConsumedByType(def.word(1), false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001782 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001783 interface_var v = {};
1784 v.id = id;
1785 v.type_id = def.word(1);
1786 v.offset = offset;
1787 out.emplace_back(attachment_index + offset, v);
1788 }
1789 }
1790 }
1791 }
1792 }
1793
1794 return out;
1795}
1796
ziga-lunarg8346fe82021-08-22 17:30:50 +02001797uint32_t SHADER_MODULE_STATE::GetNumComponentsInBaseType(const spirv_inst_iter &iter) const {
1798 const uint32_t opcode = iter.opcode();
1799 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1800 return 1;
1801 } else if (opcode == spv::OpTypeVector) {
1802 const uint32_t component_count = iter.word(3);
1803 return component_count;
1804 } else if (opcode == spv::OpTypeMatrix) {
1805 const auto column_type = get_def(iter.word(2));
1806 const uint32_t vector_length = GetNumComponentsInBaseType(column_type);
ziga-lunarg38e44982022-04-05 00:10:46 +02001807 // Because we are calculating components for a single location we do not care about column count
1808 return vector_length;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001809 } else if (opcode == spv::OpTypeArray) {
1810 const auto element_type = get_def(iter.word(2));
1811 const uint32_t element_length = GetNumComponentsInBaseType(element_type);
1812 return element_length;
1813 } else if (opcode == spv::OpTypeStruct) {
1814 uint32_t total_size = 0;
1815 for (uint32_t i = 2; i < iter.len(); ++i) {
1816 total_size += GetNumComponentsInBaseType(get_def(iter.word(i)));
1817 }
1818 return total_size;
1819 } else if (opcode == spv::OpTypePointer) {
1820 const auto type = get_def(iter.word(3));
1821 return GetNumComponentsInBaseType(type);
1822 }
1823 return 0;
1824}
1825
ziga-lunarga26b3602021-08-08 15:53:00 +02001826uint32_t SHADER_MODULE_STATE::GetTypeBitsSize(const spirv_inst_iter &iter) const {
1827 const uint32_t opcode = iter.opcode();
1828 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1829 return iter.word(2);
1830 } else if (opcode == spv::OpTypeVector) {
1831 const auto component_type = get_def(iter.word(2));
1832 uint32_t scalar_width = GetTypeBitsSize(component_type);
1833 uint32_t component_count = iter.word(3);
1834 return scalar_width * component_count;
1835 } else if (opcode == spv::OpTypeMatrix) {
1836 const auto column_type = get_def(iter.word(2));
1837 uint32_t vector_width = GetTypeBitsSize(column_type);
1838 uint32_t column_count = iter.word(3);
1839 return vector_width * column_count;
1840 } else if (opcode == spv::OpTypeArray) {
1841 const auto element_type = get_def(iter.word(2));
1842 uint32_t element_width = GetTypeBitsSize(element_type);
1843 const auto length_type = get_def(iter.word(3));
1844 uint32_t length = GetConstantValue(length_type);
1845 return element_width * length;
1846 } else if (opcode == spv::OpTypeStruct) {
1847 uint32_t total_size = 0;
1848 for (uint32_t i = 2; i < iter.len(); ++i) {
1849 total_size += GetTypeBitsSize(get_def(iter.word(i)));
1850 }
1851 return total_size;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001852 } else if (opcode == spv::OpTypePointer) {
1853 const auto type = get_def(iter.word(3));
1854 return GetTypeBitsSize(type);
ziga-lunargef2c3172021-11-07 10:35:29 +01001855 } else if (opcode == spv::OpVariable) {
1856 const auto type = get_def(iter.word(1));
1857 return GetTypeBitsSize(type);
sjfricke5531a092022-09-16 15:25:05 +09001858 } else if (opcode == spv::OpTypeBool) {
1859 // The Spec states:
1860 // "Boolean values considered as 32-bit integer values for the purpose of this calculation"
1861 // when getting the size for the limits
1862 return 32;
ziga-lunarga26b3602021-08-08 15:53:00 +02001863 }
1864 return 0;
1865}
1866
1867uint32_t SHADER_MODULE_STATE::GetTypeBytesSize(const spirv_inst_iter &iter) const { return GetTypeBitsSize(iter) / 8; }
1868
ziga-lunarg19fc6ae2021-09-09 00:05:19 +02001869// Returns the base type (float, int or unsigned int) or struct (can have multiple different base types inside)
sjfricke6086f792022-08-25 16:38:15 +09001870// Will return 0 if it can not be determined
ziga-lunarg8346fe82021-08-22 17:30:50 +02001871uint32_t SHADER_MODULE_STATE::GetBaseType(const spirv_inst_iter &iter) const {
1872 const uint32_t opcode = iter.opcode();
sjfricke10f74a82022-08-18 18:12:56 +09001873 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt || opcode == spv::OpTypeBool || opcode == spv::OpTypeStruct) {
1874 // point to itself as its the base type (or a struct that needs to be traversed still)
ziga-lunarg8346fe82021-08-22 17:30:50 +02001875 return iter.word(1);
1876 } else if (opcode == spv::OpTypeVector) {
1877 const auto& component_type = get_def(iter.word(2));
1878 return GetBaseType(component_type);
1879 } else if (opcode == spv::OpTypeMatrix) {
1880 const auto& column_type = get_def(iter.word(2));
1881 return GetBaseType(column_type);
sjfricke6a03e012022-06-23 17:54:11 +09001882 } else if (opcode == spv::OpTypeArray || opcode == spv::OpTypeRuntimeArray) {
ziga-lunarg8346fe82021-08-22 17:30:50 +02001883 const auto& element_type = get_def(iter.word(2));
1884 return GetBaseType(element_type);
1885 } else if (opcode == spv::OpTypePointer) {
sjfricke6086f792022-08-25 16:38:15 +09001886 const auto &storage_class = iter.word(2);
ziga-lunarg8346fe82021-08-22 17:30:50 +02001887 const auto& type = get_def(iter.word(3));
sjfricke6086f792022-08-25 16:38:15 +09001888 if (storage_class == spv::StorageClassPhysicalStorageBuffer && type.opcode() == spv::OpTypeStruct) {
1889 // A physical storage buffer to a struct has a chance to point to itself and can't resolve a baseType
1890 // GLSL example:
1891 // layout(buffer_reference) buffer T1 {
1892 // T1 b[2];
1893 // };
1894 return 0;
1895 }
ziga-lunarg8346fe82021-08-22 17:30:50 +02001896 return GetBaseType(type);
1897 }
sjfricke10f74a82022-08-18 18:12:56 +09001898 // If we assert here, we are missing a valid base type that must be handled. Without this assert, a return value of 0 will
1899 // produce a hard bug to track
1900 assert(false);
ziga-lunarg8346fe82021-08-22 17:30:50 +02001901 return 0;
1902}
1903
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001904// Returns type_id if id has type or zero otherwise
1905uint32_t SHADER_MODULE_STATE::GetTypeId(uint32_t id) const {
1906 const auto type = get_def(id);
1907 return OpcodeHasType(type.opcode()) ? type.word(1) : 0;
1908}
1909
sfricke-samsung962cad92021-04-13 00:46:29 -07001910std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint) {
1911 assert(entrypoint.opcode() == spv::OpEntryPoint);
1912
1913 std::vector<uint32_t> interfaces;
1914 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
1915 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
1916 uint32_t word = 3;
1917 while (entrypoint.word(word) & 0xff000000u) {
1918 ++word;
1919 }
1920 ++word;
1921
1922 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
1923
1924 return interfaces;
1925}